Fix pos-visible-in-window-p for zero-column composed character.
[emacs.git] / src / xdisp.c
blob9b2b0da4317e5d59e056740be564045b128cbc45
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2011 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22 Redisplay.
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
53 expose_window (asynchronous) |
55 X expose events -----+
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
84 . try_cursor_movement
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
90 . try_window_reusing_current_matrix
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
96 . try_window_id
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
102 . try_window
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
115 Desired matrices.
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
160 Frame matrices.
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
181 Bidirectional display.
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
224 Bidirectional display and character compositions
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
251 Moving an iterator in bidirectional text
252 without producing glyphs
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
276 #include <setjmp.h>
278 #include "lisp.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "buffer.h"
285 #include "character.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
302 #ifdef HAVE_X_WINDOWS
303 #include "xterm.h"
304 #endif
305 #ifdef WINDOWSNT
306 #include "w32term.h"
307 #endif
308 #ifdef HAVE_NS
309 #include "nsterm.h"
310 #endif
311 #ifdef USE_GTK
312 #include "gtkutil.h"
313 #endif
315 #include "font.h"
317 #ifndef FRAME_X_OUTPUT
318 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
319 #endif
321 #define INFINITY 10000000
323 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
324 Lisp_Object Qwindow_scroll_functions;
325 static Lisp_Object Qwindow_text_change_functions;
326 static Lisp_Object Qredisplay_end_trigger_functions;
327 Lisp_Object Qinhibit_point_motion_hooks;
328 static Lisp_Object QCeval, QCpropertize;
329 Lisp_Object QCfile, QCdata;
330 static Lisp_Object Qfontified;
331 static Lisp_Object Qgrow_only;
332 static Lisp_Object Qinhibit_eval_during_redisplay;
333 static Lisp_Object Qbuffer_position, Qposition, Qobject;
334 static Lisp_Object Qright_to_left, Qleft_to_right;
336 /* Cursor shapes */
337 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
339 /* Pointer shapes */
340 static Lisp_Object Qarrow, Qhand;
341 Lisp_Object Qtext;
343 /* Holds the list (error). */
344 static Lisp_Object list_of_error;
346 static Lisp_Object Qfontification_functions;
348 static Lisp_Object Qwrap_prefix;
349 static Lisp_Object Qline_prefix;
351 /* Non-nil means don't actually do any redisplay. */
353 Lisp_Object Qinhibit_redisplay;
355 /* Names of text properties relevant for redisplay. */
357 Lisp_Object Qdisplay;
359 Lisp_Object Qspace, QCalign_to;
360 static Lisp_Object QCrelative_width, QCrelative_height;
361 Lisp_Object Qleft_margin, Qright_margin;
362 static Lisp_Object Qspace_width, Qraise;
363 static Lisp_Object Qslice;
364 Lisp_Object Qcenter;
365 static Lisp_Object Qmargin, Qpointer;
366 static Lisp_Object Qline_height;
368 #ifdef HAVE_WINDOW_SYSTEM
370 /* Test if overflow newline into fringe. Called with iterator IT
371 at or past right window margin, and with IT->current_x set. */
373 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
374 (!NILP (Voverflow_newline_into_fringe) \
375 && FRAME_WINDOW_P ((IT)->f) \
376 && ((IT)->bidi_it.paragraph_dir == R2L \
377 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
378 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
379 && (IT)->current_x == (IT)->last_visible_x \
380 && (IT)->line_wrap != WORD_WRAP)
382 #else /* !HAVE_WINDOW_SYSTEM */
383 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
384 #endif /* HAVE_WINDOW_SYSTEM */
386 /* Test if the display element loaded in IT is a space or tab
387 character. This is used to determine word wrapping. */
389 #define IT_DISPLAYING_WHITESPACE(it) \
390 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
392 /* Name of the face used to highlight trailing whitespace. */
394 static Lisp_Object Qtrailing_whitespace;
396 /* Name and number of the face used to highlight escape glyphs. */
398 static Lisp_Object Qescape_glyph;
400 /* Name and number of the face used to highlight non-breaking spaces. */
402 static Lisp_Object Qnobreak_space;
404 /* The symbol `image' which is the car of the lists used to represent
405 images in Lisp. Also a tool bar style. */
407 Lisp_Object Qimage;
409 /* The image map types. */
410 Lisp_Object QCmap;
411 static Lisp_Object QCpointer;
412 static Lisp_Object Qrect, Qcircle, Qpoly;
414 /* Tool bar styles */
415 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
417 /* Non-zero means print newline to stdout before next mini-buffer
418 message. */
420 int noninteractive_need_newline;
422 /* Non-zero means print newline to message log before next message. */
424 static int message_log_need_newline;
426 /* Three markers that message_dolog uses.
427 It could allocate them itself, but that causes trouble
428 in handling memory-full errors. */
429 static Lisp_Object message_dolog_marker1;
430 static Lisp_Object message_dolog_marker2;
431 static Lisp_Object message_dolog_marker3;
433 /* The buffer position of the first character appearing entirely or
434 partially on the line of the selected window which contains the
435 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
436 redisplay optimization in redisplay_internal. */
438 static struct text_pos this_line_start_pos;
440 /* Number of characters past the end of the line above, including the
441 terminating newline. */
443 static struct text_pos this_line_end_pos;
445 /* The vertical positions and the height of this line. */
447 static int this_line_vpos;
448 static int this_line_y;
449 static int this_line_pixel_height;
451 /* X position at which this display line starts. Usually zero;
452 negative if first character is partially visible. */
454 static int this_line_start_x;
456 /* The smallest character position seen by move_it_* functions as they
457 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
458 hscrolled lines, see display_line. */
460 static struct text_pos this_line_min_pos;
462 /* Buffer that this_line_.* variables are referring to. */
464 static struct buffer *this_line_buffer;
467 /* Values of those variables at last redisplay are stored as
468 properties on `overlay-arrow-position' symbol. However, if
469 Voverlay_arrow_position is a marker, last-arrow-position is its
470 numerical position. */
472 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
474 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
475 properties on a symbol in overlay-arrow-variable-list. */
477 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
479 Lisp_Object Qmenu_bar_update_hook;
481 /* Nonzero if an overlay arrow has been displayed in this window. */
483 static int overlay_arrow_seen;
485 /* Number of windows showing the buffer of the selected window (or
486 another buffer with the same base buffer). keyboard.c refers to
487 this. */
489 int buffer_shared;
491 /* Vector containing glyphs for an ellipsis `...'. */
493 static Lisp_Object default_invis_vector[3];
495 /* This is the window where the echo area message was displayed. It
496 is always a mini-buffer window, but it may not be the same window
497 currently active as a mini-buffer. */
499 Lisp_Object echo_area_window;
501 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
502 pushes the current message and the value of
503 message_enable_multibyte on the stack, the function restore_message
504 pops the stack and displays MESSAGE again. */
506 static Lisp_Object Vmessage_stack;
508 /* Nonzero means multibyte characters were enabled when the echo area
509 message was specified. */
511 static int message_enable_multibyte;
513 /* Nonzero if we should redraw the mode lines on the next redisplay. */
515 int update_mode_lines;
517 /* Nonzero if window sizes or contents have changed since last
518 redisplay that finished. */
520 int windows_or_buffers_changed;
522 /* Nonzero means a frame's cursor type has been changed. */
524 int cursor_type_changed;
526 /* Nonzero after display_mode_line if %l was used and it displayed a
527 line number. */
529 static int line_number_displayed;
531 /* The name of the *Messages* buffer, a string. */
533 static Lisp_Object Vmessages_buffer_name;
535 /* Current, index 0, and last displayed echo area message. Either
536 buffers from echo_buffers, or nil to indicate no message. */
538 Lisp_Object echo_area_buffer[2];
540 /* The buffers referenced from echo_area_buffer. */
542 static Lisp_Object echo_buffer[2];
544 /* A vector saved used in with_area_buffer to reduce consing. */
546 static Lisp_Object Vwith_echo_area_save_vector;
548 /* Non-zero means display_echo_area should display the last echo area
549 message again. Set by redisplay_preserve_echo_area. */
551 static int display_last_displayed_message_p;
553 /* Nonzero if echo area is being used by print; zero if being used by
554 message. */
556 static int message_buf_print;
558 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
560 static Lisp_Object Qinhibit_menubar_update;
561 static Lisp_Object Qmessage_truncate_lines;
563 /* Set to 1 in clear_message to make redisplay_internal aware
564 of an emptied echo area. */
566 static int message_cleared_p;
568 /* A scratch glyph row with contents used for generating truncation
569 glyphs. Also used in direct_output_for_insert. */
571 #define MAX_SCRATCH_GLYPHS 100
572 static struct glyph_row scratch_glyph_row;
573 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
575 /* Ascent and height of the last line processed by move_it_to. */
577 static int last_max_ascent, last_height;
579 /* Non-zero if there's a help-echo in the echo area. */
581 int help_echo_showing_p;
583 /* If >= 0, computed, exact values of mode-line and header-line height
584 to use in the macros CURRENT_MODE_LINE_HEIGHT and
585 CURRENT_HEADER_LINE_HEIGHT. */
587 int current_mode_line_height, current_header_line_height;
589 /* The maximum distance to look ahead for text properties. Values
590 that are too small let us call compute_char_face and similar
591 functions too often which is expensive. Values that are too large
592 let us call compute_char_face and alike too often because we
593 might not be interested in text properties that far away. */
595 #define TEXT_PROP_DISTANCE_LIMIT 100
597 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
598 iterator state and later restore it. This is needed because the
599 bidi iterator on bidi.c keeps a stacked cache of its states, which
600 is really a singleton. When we use scratch iterator objects to
601 move around the buffer, we can cause the bidi cache to be pushed or
602 popped, and therefore we need to restore the cache state when we
603 return to the original iterator. */
604 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
605 do { \
606 if (CACHE) \
607 bidi_unshelve_cache (CACHE, 1); \
608 ITCOPY = ITORIG; \
609 CACHE = bidi_shelve_cache(); \
610 } while (0)
612 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
613 do { \
614 if (pITORIG != pITCOPY) \
615 *(pITORIG) = *(pITCOPY); \
616 bidi_unshelve_cache (CACHE, 0); \
617 CACHE = NULL; \
618 } while (0)
620 #if GLYPH_DEBUG
622 /* Non-zero means print traces of redisplay if compiled with
623 GLYPH_DEBUG != 0. */
625 int trace_redisplay_p;
627 #endif /* GLYPH_DEBUG */
629 #ifdef DEBUG_TRACE_MOVE
630 /* Non-zero means trace with TRACE_MOVE to stderr. */
631 int trace_move;
633 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
634 #else
635 #define TRACE_MOVE(x) (void) 0
636 #endif
638 static Lisp_Object Qauto_hscroll_mode;
640 /* Buffer being redisplayed -- for redisplay_window_error. */
642 static struct buffer *displayed_buffer;
644 /* Value returned from text property handlers (see below). */
646 enum prop_handled
648 HANDLED_NORMALLY,
649 HANDLED_RECOMPUTE_PROPS,
650 HANDLED_OVERLAY_STRING_CONSUMED,
651 HANDLED_RETURN
654 /* A description of text properties that redisplay is interested
655 in. */
657 struct props
659 /* The name of the property. */
660 Lisp_Object *name;
662 /* A unique index for the property. */
663 enum prop_idx idx;
665 /* A handler function called to set up iterator IT from the property
666 at IT's current position. Value is used to steer handle_stop. */
667 enum prop_handled (*handler) (struct it *it);
670 static enum prop_handled handle_face_prop (struct it *);
671 static enum prop_handled handle_invisible_prop (struct it *);
672 static enum prop_handled handle_display_prop (struct it *);
673 static enum prop_handled handle_composition_prop (struct it *);
674 static enum prop_handled handle_overlay_change (struct it *);
675 static enum prop_handled handle_fontified_prop (struct it *);
677 /* Properties handled by iterators. */
679 static struct props it_props[] =
681 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
682 /* Handle `face' before `display' because some sub-properties of
683 `display' need to know the face. */
684 {&Qface, FACE_PROP_IDX, handle_face_prop},
685 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
686 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
687 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
688 {NULL, 0, NULL}
691 /* Value is the position described by X. If X is a marker, value is
692 the marker_position of X. Otherwise, value is X. */
694 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
696 /* Enumeration returned by some move_it_.* functions internally. */
698 enum move_it_result
700 /* Not used. Undefined value. */
701 MOVE_UNDEFINED,
703 /* Move ended at the requested buffer position or ZV. */
704 MOVE_POS_MATCH_OR_ZV,
706 /* Move ended at the requested X pixel position. */
707 MOVE_X_REACHED,
709 /* Move within a line ended at the end of a line that must be
710 continued. */
711 MOVE_LINE_CONTINUED,
713 /* Move within a line ended at the end of a line that would
714 be displayed truncated. */
715 MOVE_LINE_TRUNCATED,
717 /* Move within a line ended at a line end. */
718 MOVE_NEWLINE_OR_CR
721 /* This counter is used to clear the face cache every once in a while
722 in redisplay_internal. It is incremented for each redisplay.
723 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
724 cleared. */
726 #define CLEAR_FACE_CACHE_COUNT 500
727 static int clear_face_cache_count;
729 /* Similarly for the image cache. */
731 #ifdef HAVE_WINDOW_SYSTEM
732 #define CLEAR_IMAGE_CACHE_COUNT 101
733 static int clear_image_cache_count;
735 /* Null glyph slice */
736 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
737 #endif
739 /* Non-zero while redisplay_internal is in progress. */
741 int redisplaying_p;
743 static Lisp_Object Qinhibit_free_realized_faces;
745 /* If a string, XTread_socket generates an event to display that string.
746 (The display is done in read_char.) */
748 Lisp_Object help_echo_string;
749 Lisp_Object help_echo_window;
750 Lisp_Object help_echo_object;
751 EMACS_INT help_echo_pos;
753 /* Temporary variable for XTread_socket. */
755 Lisp_Object previous_help_echo_string;
757 /* Platform-independent portion of hourglass implementation. */
759 /* Non-zero means an hourglass cursor is currently shown. */
760 int hourglass_shown_p;
762 /* If non-null, an asynchronous timer that, when it expires, displays
763 an hourglass cursor on all frames. */
764 struct atimer *hourglass_atimer;
766 /* Name of the face used to display glyphless characters. */
767 Lisp_Object Qglyphless_char;
769 /* Symbol for the purpose of Vglyphless_char_display. */
770 static Lisp_Object Qglyphless_char_display;
772 /* Method symbols for Vglyphless_char_display. */
773 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
775 /* Default pixel width of `thin-space' display method. */
776 #define THIN_SPACE_WIDTH 1
778 /* Default number of seconds to wait before displaying an hourglass
779 cursor. */
780 #define DEFAULT_HOURGLASS_DELAY 1
783 /* Function prototypes. */
785 static void setup_for_ellipsis (struct it *, int);
786 static void set_iterator_to_next (struct it *, int);
787 static void mark_window_display_accurate_1 (struct window *, int);
788 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
789 static int display_prop_string_p (Lisp_Object, Lisp_Object);
790 static int cursor_row_p (struct glyph_row *);
791 static int redisplay_mode_lines (Lisp_Object, int);
792 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
794 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
796 static void handle_line_prefix (struct it *);
798 static void pint2str (char *, int, EMACS_INT);
799 static void pint2hrstr (char *, int, EMACS_INT);
800 static struct text_pos run_window_scroll_functions (Lisp_Object,
801 struct text_pos);
802 static void reconsider_clip_changes (struct window *, struct buffer *);
803 static int text_outside_line_unchanged_p (struct window *,
804 EMACS_INT, EMACS_INT);
805 static void store_mode_line_noprop_char (char);
806 static int store_mode_line_noprop (const char *, int, int);
807 static void handle_stop (struct it *);
808 static void handle_stop_backwards (struct it *, EMACS_INT);
809 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
810 static void ensure_echo_area_buffers (void);
811 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
812 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
813 static int with_echo_area_buffer (struct window *, int,
814 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
815 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
816 static void clear_garbaged_frames (void);
817 static int current_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
818 static void pop_message (void);
819 static int truncate_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
820 static void set_message (const char *, Lisp_Object, EMACS_INT, int);
821 static int set_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
822 static int display_echo_area (struct window *);
823 static int display_echo_area_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
824 static int resize_mini_window_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
825 static Lisp_Object unwind_redisplay (Lisp_Object);
826 static int string_char_and_length (const unsigned char *, int *);
827 static struct text_pos display_prop_end (struct it *, Lisp_Object,
828 struct text_pos);
829 static int compute_window_start_on_continuation_line (struct window *);
830 static Lisp_Object safe_eval_handler (Lisp_Object);
831 static void insert_left_trunc_glyphs (struct it *);
832 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
833 Lisp_Object);
834 static void extend_face_to_end_of_line (struct it *);
835 static int append_space_for_newline (struct it *, int);
836 static int cursor_row_fully_visible_p (struct window *, int, int);
837 static int try_scrolling (Lisp_Object, int, EMACS_INT, EMACS_INT, int, int);
838 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
839 static int trailing_whitespace_p (EMACS_INT);
840 static intmax_t message_log_check_duplicate (EMACS_INT, EMACS_INT);
841 static void push_it (struct it *, struct text_pos *);
842 static void pop_it (struct it *);
843 static void sync_frame_with_window_matrix_rows (struct window *);
844 static void select_frame_for_redisplay (Lisp_Object);
845 static void redisplay_internal (void);
846 static int echo_area_display (int);
847 static void redisplay_windows (Lisp_Object);
848 static void redisplay_window (Lisp_Object, int);
849 static Lisp_Object redisplay_window_error (Lisp_Object);
850 static Lisp_Object redisplay_window_0 (Lisp_Object);
851 static Lisp_Object redisplay_window_1 (Lisp_Object);
852 static int set_cursor_from_row (struct window *, struct glyph_row *,
853 struct glyph_matrix *, EMACS_INT, EMACS_INT,
854 int, int);
855 static int update_menu_bar (struct frame *, int, int);
856 static int try_window_reusing_current_matrix (struct window *);
857 static int try_window_id (struct window *);
858 static int display_line (struct it *);
859 static int display_mode_lines (struct window *);
860 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
861 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
862 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
863 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
864 static void display_menu_bar (struct window *);
865 static EMACS_INT display_count_lines (EMACS_INT, EMACS_INT, EMACS_INT,
866 EMACS_INT *);
867 static int display_string (const char *, Lisp_Object, Lisp_Object,
868 EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
869 static void compute_line_metrics (struct it *);
870 static void run_redisplay_end_trigger_hook (struct it *);
871 static int get_overlay_strings (struct it *, EMACS_INT);
872 static int get_overlay_strings_1 (struct it *, EMACS_INT, int);
873 static void next_overlay_string (struct it *);
874 static void reseat (struct it *, struct text_pos, int);
875 static void reseat_1 (struct it *, struct text_pos, int);
876 static void back_to_previous_visible_line_start (struct it *);
877 void reseat_at_previous_visible_line_start (struct it *);
878 static void reseat_at_next_visible_line_start (struct it *, int);
879 static int next_element_from_ellipsis (struct it *);
880 static int next_element_from_display_vector (struct it *);
881 static int next_element_from_string (struct it *);
882 static int next_element_from_c_string (struct it *);
883 static int next_element_from_buffer (struct it *);
884 static int next_element_from_composition (struct it *);
885 static int next_element_from_image (struct it *);
886 static int next_element_from_stretch (struct it *);
887 static void load_overlay_strings (struct it *, EMACS_INT);
888 static int init_from_display_pos (struct it *, struct window *,
889 struct display_pos *);
890 static void reseat_to_string (struct it *, const char *,
891 Lisp_Object, EMACS_INT, EMACS_INT, int, int);
892 static int get_next_display_element (struct it *);
893 static enum move_it_result
894 move_it_in_display_line_to (struct it *, EMACS_INT, int,
895 enum move_operation_enum);
896 void move_it_vertically_backward (struct it *, int);
897 static void init_to_row_start (struct it *, struct window *,
898 struct glyph_row *);
899 static int init_to_row_end (struct it *, struct window *,
900 struct glyph_row *);
901 static void back_to_previous_line_start (struct it *);
902 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
903 static struct text_pos string_pos_nchars_ahead (struct text_pos,
904 Lisp_Object, EMACS_INT);
905 static struct text_pos string_pos (EMACS_INT, Lisp_Object);
906 static struct text_pos c_string_pos (EMACS_INT, const char *, int);
907 static EMACS_INT number_of_chars (const char *, int);
908 static void compute_stop_pos (struct it *);
909 static void compute_string_pos (struct text_pos *, struct text_pos,
910 Lisp_Object);
911 static int face_before_or_after_it_pos (struct it *, int);
912 static EMACS_INT next_overlay_change (EMACS_INT);
913 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
914 Lisp_Object, struct text_pos *, EMACS_INT, int);
915 static int handle_single_display_spec (struct it *, Lisp_Object,
916 Lisp_Object, Lisp_Object,
917 struct text_pos *, EMACS_INT, int, int);
918 static int underlying_face_id (struct it *);
919 static int in_ellipses_for_invisible_text_p (struct display_pos *,
920 struct window *);
922 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
923 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
925 #ifdef HAVE_WINDOW_SYSTEM
927 static void x_consider_frame_title (Lisp_Object);
928 static int tool_bar_lines_needed (struct frame *, int *);
929 static void update_tool_bar (struct frame *, int);
930 static void build_desired_tool_bar_string (struct frame *f);
931 static int redisplay_tool_bar (struct frame *);
932 static void display_tool_bar_line (struct it *, int);
933 static void notice_overwritten_cursor (struct window *,
934 enum glyph_row_area,
935 int, int, int, int);
936 static void append_stretch_glyph (struct it *, Lisp_Object,
937 int, int, int);
940 #endif /* HAVE_WINDOW_SYSTEM */
942 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
943 static int coords_in_mouse_face_p (struct window *, int, int);
947 /***********************************************************************
948 Window display dimensions
949 ***********************************************************************/
951 /* Return the bottom boundary y-position for text lines in window W.
952 This is the first y position at which a line cannot start.
953 It is relative to the top of the window.
955 This is the height of W minus the height of a mode line, if any. */
957 inline int
958 window_text_bottom_y (struct window *w)
960 int height = WINDOW_TOTAL_HEIGHT (w);
962 if (WINDOW_WANTS_MODELINE_P (w))
963 height -= CURRENT_MODE_LINE_HEIGHT (w);
964 return height;
967 /* Return the pixel width of display area AREA of window W. AREA < 0
968 means return the total width of W, not including fringes to
969 the left and right of the window. */
971 inline int
972 window_box_width (struct window *w, int area)
974 int cols = XFASTINT (w->total_cols);
975 int pixels = 0;
977 if (!w->pseudo_window_p)
979 cols -= WINDOW_SCROLL_BAR_COLS (w);
981 if (area == TEXT_AREA)
983 if (INTEGERP (w->left_margin_cols))
984 cols -= XFASTINT (w->left_margin_cols);
985 if (INTEGERP (w->right_margin_cols))
986 cols -= XFASTINT (w->right_margin_cols);
987 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
989 else if (area == LEFT_MARGIN_AREA)
991 cols = (INTEGERP (w->left_margin_cols)
992 ? XFASTINT (w->left_margin_cols) : 0);
993 pixels = 0;
995 else if (area == RIGHT_MARGIN_AREA)
997 cols = (INTEGERP (w->right_margin_cols)
998 ? XFASTINT (w->right_margin_cols) : 0);
999 pixels = 0;
1003 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1007 /* Return the pixel height of the display area of window W, not
1008 including mode lines of W, if any. */
1010 inline int
1011 window_box_height (struct window *w)
1013 struct frame *f = XFRAME (w->frame);
1014 int height = WINDOW_TOTAL_HEIGHT (w);
1016 xassert (height >= 0);
1018 /* Note: the code below that determines the mode-line/header-line
1019 height is essentially the same as that contained in the macro
1020 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1021 the appropriate glyph row has its `mode_line_p' flag set,
1022 and if it doesn't, uses estimate_mode_line_height instead. */
1024 if (WINDOW_WANTS_MODELINE_P (w))
1026 struct glyph_row *ml_row
1027 = (w->current_matrix && w->current_matrix->rows
1028 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1029 : 0);
1030 if (ml_row && ml_row->mode_line_p)
1031 height -= ml_row->height;
1032 else
1033 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1036 if (WINDOW_WANTS_HEADER_LINE_P (w))
1038 struct glyph_row *hl_row
1039 = (w->current_matrix && w->current_matrix->rows
1040 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1041 : 0);
1042 if (hl_row && hl_row->mode_line_p)
1043 height -= hl_row->height;
1044 else
1045 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1048 /* With a very small font and a mode-line that's taller than
1049 default, we might end up with a negative height. */
1050 return max (0, height);
1053 /* Return the window-relative coordinate of the left edge of display
1054 area AREA of window W. AREA < 0 means return the left edge of the
1055 whole window, to the right of the left fringe of W. */
1057 inline int
1058 window_box_left_offset (struct window *w, int area)
1060 int x;
1062 if (w->pseudo_window_p)
1063 return 0;
1065 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1067 if (area == TEXT_AREA)
1068 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1069 + window_box_width (w, LEFT_MARGIN_AREA));
1070 else if (area == RIGHT_MARGIN_AREA)
1071 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1072 + window_box_width (w, LEFT_MARGIN_AREA)
1073 + window_box_width (w, TEXT_AREA)
1074 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1076 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1077 else if (area == LEFT_MARGIN_AREA
1078 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1079 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1081 return x;
1085 /* Return the window-relative coordinate of the right edge of display
1086 area AREA of window W. AREA < 0 means return the right edge of the
1087 whole window, to the left of the right fringe of W. */
1089 inline int
1090 window_box_right_offset (struct window *w, int area)
1092 return window_box_left_offset (w, area) + window_box_width (w, area);
1095 /* Return the frame-relative coordinate of the left edge of display
1096 area AREA of window W. AREA < 0 means return the left edge of the
1097 whole window, to the right of the left fringe of W. */
1099 inline int
1100 window_box_left (struct window *w, int area)
1102 struct frame *f = XFRAME (w->frame);
1103 int x;
1105 if (w->pseudo_window_p)
1106 return FRAME_INTERNAL_BORDER_WIDTH (f);
1108 x = (WINDOW_LEFT_EDGE_X (w)
1109 + window_box_left_offset (w, area));
1111 return x;
1115 /* Return the frame-relative coordinate of the right edge of display
1116 area AREA of window W. AREA < 0 means return the right edge of the
1117 whole window, to the left of the right fringe of W. */
1119 inline int
1120 window_box_right (struct window *w, int area)
1122 return window_box_left (w, area) + window_box_width (w, area);
1125 /* Get the bounding box of the display area AREA of window W, without
1126 mode lines, in frame-relative coordinates. AREA < 0 means the
1127 whole window, not including the left and right fringes of
1128 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1129 coordinates of the upper-left corner of the box. Return in
1130 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1132 inline void
1133 window_box (struct window *w, int area, int *box_x, int *box_y,
1134 int *box_width, int *box_height)
1136 if (box_width)
1137 *box_width = window_box_width (w, area);
1138 if (box_height)
1139 *box_height = window_box_height (w);
1140 if (box_x)
1141 *box_x = window_box_left (w, area);
1142 if (box_y)
1144 *box_y = WINDOW_TOP_EDGE_Y (w);
1145 if (WINDOW_WANTS_HEADER_LINE_P (w))
1146 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1151 /* Get the bounding box of the display area AREA of window W, without
1152 mode lines. AREA < 0 means the whole window, not including the
1153 left and right fringe of the window. Return in *TOP_LEFT_X
1154 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1155 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1156 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1157 box. */
1159 static inline void
1160 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1161 int *bottom_right_x, int *bottom_right_y)
1163 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1164 bottom_right_y);
1165 *bottom_right_x += *top_left_x;
1166 *bottom_right_y += *top_left_y;
1171 /***********************************************************************
1172 Utilities
1173 ***********************************************************************/
1175 /* Return the bottom y-position of the line the iterator IT is in.
1176 This can modify IT's settings. */
1179 line_bottom_y (struct it *it)
1181 int line_height = it->max_ascent + it->max_descent;
1182 int line_top_y = it->current_y;
1184 if (line_height == 0)
1186 if (last_height)
1187 line_height = last_height;
1188 else if (IT_CHARPOS (*it) < ZV)
1190 move_it_by_lines (it, 1);
1191 line_height = (it->max_ascent || it->max_descent
1192 ? it->max_ascent + it->max_descent
1193 : last_height);
1195 else
1197 struct glyph_row *row = it->glyph_row;
1199 /* Use the default character height. */
1200 it->glyph_row = NULL;
1201 it->what = IT_CHARACTER;
1202 it->c = ' ';
1203 it->len = 1;
1204 PRODUCE_GLYPHS (it);
1205 line_height = it->ascent + it->descent;
1206 it->glyph_row = row;
1210 return line_top_y + line_height;
1214 /* Return 1 if position CHARPOS is visible in window W.
1215 CHARPOS < 0 means return info about WINDOW_END position.
1216 If visible, set *X and *Y to pixel coordinates of top left corner.
1217 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1218 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1221 pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1222 int *rtop, int *rbot, int *rowh, int *vpos)
1224 struct it it;
1225 void *itdata = bidi_shelve_cache ();
1226 struct text_pos top;
1227 int visible_p = 0;
1228 struct buffer *old_buffer = NULL;
1230 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1231 return visible_p;
1233 if (XBUFFER (w->buffer) != current_buffer)
1235 old_buffer = current_buffer;
1236 set_buffer_internal_1 (XBUFFER (w->buffer));
1239 SET_TEXT_POS_FROM_MARKER (top, w->start);
1241 /* Compute exact mode line heights. */
1242 if (WINDOW_WANTS_MODELINE_P (w))
1243 current_mode_line_height
1244 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1245 BVAR (current_buffer, mode_line_format));
1247 if (WINDOW_WANTS_HEADER_LINE_P (w))
1248 current_header_line_height
1249 = display_mode_line (w, HEADER_LINE_FACE_ID,
1250 BVAR (current_buffer, header_line_format));
1252 start_display (&it, w, top);
1253 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1254 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1256 if (charpos >= 0
1257 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1258 && IT_CHARPOS (it) >= charpos)
1259 /* When scanning backwards under bidi iteration, move_it_to
1260 stops at or _before_ CHARPOS, because it stops at or to
1261 the _right_ of the character at CHARPOS. */
1262 || (it.bidi_p && it.bidi_it.scan_dir == -1
1263 && IT_CHARPOS (it) <= charpos)))
1265 /* We have reached CHARPOS, or passed it. How the call to
1266 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1267 or covered by a display property, move_it_to stops at the end
1268 of the invisible text, to the right of CHARPOS. (ii) If
1269 CHARPOS is in a display vector, move_it_to stops on its last
1270 glyph. */
1271 int top_x = it.current_x;
1272 int top_y = it.current_y;
1273 enum it_method it_method = it.method;
1274 /* Calling line_bottom_y may change it.method, it.position, etc. */
1275 int bottom_y = (last_height = 0, line_bottom_y (&it));
1276 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1278 if (top_y < window_top_y)
1279 visible_p = bottom_y > window_top_y;
1280 else if (top_y < it.last_visible_y)
1281 visible_p = 1;
1282 if (visible_p)
1284 if (it_method == GET_FROM_DISPLAY_VECTOR)
1286 /* We stopped on the last glyph of a display vector.
1287 Try and recompute. Hack alert! */
1288 if (charpos < 2 || top.charpos >= charpos)
1289 top_x = it.glyph_row->x;
1290 else
1292 struct it it2;
1293 start_display (&it2, w, top);
1294 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1295 get_next_display_element (&it2);
1296 PRODUCE_GLYPHS (&it2);
1297 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1298 || it2.current_x > it2.last_visible_x)
1299 top_x = it.glyph_row->x;
1300 else
1302 top_x = it2.current_x;
1303 top_y = it2.current_y;
1308 *x = top_x;
1309 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1310 *rtop = max (0, window_top_y - top_y);
1311 *rbot = max (0, bottom_y - it.last_visible_y);
1312 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1313 - max (top_y, window_top_y)));
1314 *vpos = it.vpos;
1317 else
1319 /* We were asked to provide info about WINDOW_END. */
1320 struct it it2;
1321 void *it2data = NULL;
1323 SAVE_IT (it2, it, it2data);
1324 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1325 move_it_by_lines (&it, 1);
1326 if (charpos < IT_CHARPOS (it)
1327 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1329 visible_p = 1;
1330 RESTORE_IT (&it2, &it2, it2data);
1331 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1332 *x = it2.current_x;
1333 *y = it2.current_y + it2.max_ascent - it2.ascent;
1334 *rtop = max (0, -it2.current_y);
1335 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1336 - it.last_visible_y));
1337 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1338 it.last_visible_y)
1339 - max (it2.current_y,
1340 WINDOW_HEADER_LINE_HEIGHT (w))));
1341 *vpos = it2.vpos;
1343 else
1344 bidi_unshelve_cache (it2data, 1);
1346 bidi_unshelve_cache (itdata, 0);
1348 if (old_buffer)
1349 set_buffer_internal_1 (old_buffer);
1351 current_header_line_height = current_mode_line_height = -1;
1353 if (visible_p && XFASTINT (w->hscroll) > 0)
1354 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1356 #if 0
1357 /* Debugging code. */
1358 if (visible_p)
1359 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1360 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1361 else
1362 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1363 #endif
1365 return visible_p;
1369 /* Return the next character from STR. Return in *LEN the length of
1370 the character. This is like STRING_CHAR_AND_LENGTH but never
1371 returns an invalid character. If we find one, we return a `?', but
1372 with the length of the invalid character. */
1374 static inline int
1375 string_char_and_length (const unsigned char *str, int *len)
1377 int c;
1379 c = STRING_CHAR_AND_LENGTH (str, *len);
1380 if (!CHAR_VALID_P (c))
1381 /* We may not change the length here because other places in Emacs
1382 don't use this function, i.e. they silently accept invalid
1383 characters. */
1384 c = '?';
1386 return c;
1391 /* Given a position POS containing a valid character and byte position
1392 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1394 static struct text_pos
1395 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1397 xassert (STRINGP (string) && nchars >= 0);
1399 if (STRING_MULTIBYTE (string))
1401 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1402 int len;
1404 while (nchars--)
1406 string_char_and_length (p, &len);
1407 p += len;
1408 CHARPOS (pos) += 1;
1409 BYTEPOS (pos) += len;
1412 else
1413 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1415 return pos;
1419 /* Value is the text position, i.e. character and byte position,
1420 for character position CHARPOS in STRING. */
1422 static inline struct text_pos
1423 string_pos (EMACS_INT charpos, Lisp_Object string)
1425 struct text_pos pos;
1426 xassert (STRINGP (string));
1427 xassert (charpos >= 0);
1428 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1429 return pos;
1433 /* Value is a text position, i.e. character and byte position, for
1434 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1435 means recognize multibyte characters. */
1437 static struct text_pos
1438 c_string_pos (EMACS_INT charpos, const char *s, int multibyte_p)
1440 struct text_pos pos;
1442 xassert (s != NULL);
1443 xassert (charpos >= 0);
1445 if (multibyte_p)
1447 int len;
1449 SET_TEXT_POS (pos, 0, 0);
1450 while (charpos--)
1452 string_char_and_length ((const unsigned char *) s, &len);
1453 s += len;
1454 CHARPOS (pos) += 1;
1455 BYTEPOS (pos) += len;
1458 else
1459 SET_TEXT_POS (pos, charpos, charpos);
1461 return pos;
1465 /* Value is the number of characters in C string S. MULTIBYTE_P
1466 non-zero means recognize multibyte characters. */
1468 static EMACS_INT
1469 number_of_chars (const char *s, int multibyte_p)
1471 EMACS_INT nchars;
1473 if (multibyte_p)
1475 EMACS_INT rest = strlen (s);
1476 int len;
1477 const unsigned char *p = (const unsigned char *) s;
1479 for (nchars = 0; rest > 0; ++nchars)
1481 string_char_and_length (p, &len);
1482 rest -= len, p += len;
1485 else
1486 nchars = strlen (s);
1488 return nchars;
1492 /* Compute byte position NEWPOS->bytepos corresponding to
1493 NEWPOS->charpos. POS is a known position in string STRING.
1494 NEWPOS->charpos must be >= POS.charpos. */
1496 static void
1497 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1499 xassert (STRINGP (string));
1500 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1502 if (STRING_MULTIBYTE (string))
1503 *newpos = string_pos_nchars_ahead (pos, string,
1504 CHARPOS (*newpos) - CHARPOS (pos));
1505 else
1506 BYTEPOS (*newpos) = CHARPOS (*newpos);
1509 /* EXPORT:
1510 Return an estimation of the pixel height of mode or header lines on
1511 frame F. FACE_ID specifies what line's height to estimate. */
1514 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1516 #ifdef HAVE_WINDOW_SYSTEM
1517 if (FRAME_WINDOW_P (f))
1519 int height = FONT_HEIGHT (FRAME_FONT (f));
1521 /* This function is called so early when Emacs starts that the face
1522 cache and mode line face are not yet initialized. */
1523 if (FRAME_FACE_CACHE (f))
1525 struct face *face = FACE_FROM_ID (f, face_id);
1526 if (face)
1528 if (face->font)
1529 height = FONT_HEIGHT (face->font);
1530 if (face->box_line_width > 0)
1531 height += 2 * face->box_line_width;
1535 return height;
1537 #endif
1539 return 1;
1542 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1543 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1544 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1545 not force the value into range. */
1547 void
1548 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1549 int *x, int *y, NativeRectangle *bounds, int noclip)
1552 #ifdef HAVE_WINDOW_SYSTEM
1553 if (FRAME_WINDOW_P (f))
1555 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1556 even for negative values. */
1557 if (pix_x < 0)
1558 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1559 if (pix_y < 0)
1560 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1562 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1563 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1565 if (bounds)
1566 STORE_NATIVE_RECT (*bounds,
1567 FRAME_COL_TO_PIXEL_X (f, pix_x),
1568 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1569 FRAME_COLUMN_WIDTH (f) - 1,
1570 FRAME_LINE_HEIGHT (f) - 1);
1572 if (!noclip)
1574 if (pix_x < 0)
1575 pix_x = 0;
1576 else if (pix_x > FRAME_TOTAL_COLS (f))
1577 pix_x = FRAME_TOTAL_COLS (f);
1579 if (pix_y < 0)
1580 pix_y = 0;
1581 else if (pix_y > FRAME_LINES (f))
1582 pix_y = FRAME_LINES (f);
1585 #endif
1587 *x = pix_x;
1588 *y = pix_y;
1592 /* Find the glyph under window-relative coordinates X/Y in window W.
1593 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1594 strings. Return in *HPOS and *VPOS the row and column number of
1595 the glyph found. Return in *AREA the glyph area containing X.
1596 Value is a pointer to the glyph found or null if X/Y is not on
1597 text, or we can't tell because W's current matrix is not up to
1598 date. */
1600 static
1601 struct glyph *
1602 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1603 int *dx, int *dy, int *area)
1605 struct glyph *glyph, *end;
1606 struct glyph_row *row = NULL;
1607 int x0, i;
1609 /* Find row containing Y. Give up if some row is not enabled. */
1610 for (i = 0; i < w->current_matrix->nrows; ++i)
1612 row = MATRIX_ROW (w->current_matrix, i);
1613 if (!row->enabled_p)
1614 return NULL;
1615 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1616 break;
1619 *vpos = i;
1620 *hpos = 0;
1622 /* Give up if Y is not in the window. */
1623 if (i == w->current_matrix->nrows)
1624 return NULL;
1626 /* Get the glyph area containing X. */
1627 if (w->pseudo_window_p)
1629 *area = TEXT_AREA;
1630 x0 = 0;
1632 else
1634 if (x < window_box_left_offset (w, TEXT_AREA))
1636 *area = LEFT_MARGIN_AREA;
1637 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1639 else if (x < window_box_right_offset (w, TEXT_AREA))
1641 *area = TEXT_AREA;
1642 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1644 else
1646 *area = RIGHT_MARGIN_AREA;
1647 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1651 /* Find glyph containing X. */
1652 glyph = row->glyphs[*area];
1653 end = glyph + row->used[*area];
1654 x -= x0;
1655 while (glyph < end && x >= glyph->pixel_width)
1657 x -= glyph->pixel_width;
1658 ++glyph;
1661 if (glyph == end)
1662 return NULL;
1664 if (dx)
1666 *dx = x;
1667 *dy = y - (row->y + row->ascent - glyph->ascent);
1670 *hpos = glyph - row->glyphs[*area];
1671 return glyph;
1674 /* Convert frame-relative x/y to coordinates relative to window W.
1675 Takes pseudo-windows into account. */
1677 static void
1678 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1680 if (w->pseudo_window_p)
1682 /* A pseudo-window is always full-width, and starts at the
1683 left edge of the frame, plus a frame border. */
1684 struct frame *f = XFRAME (w->frame);
1685 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1686 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1688 else
1690 *x -= WINDOW_LEFT_EDGE_X (w);
1691 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1695 #ifdef HAVE_WINDOW_SYSTEM
1697 /* EXPORT:
1698 Return in RECTS[] at most N clipping rectangles for glyph string S.
1699 Return the number of stored rectangles. */
1702 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1704 XRectangle r;
1706 if (n <= 0)
1707 return 0;
1709 if (s->row->full_width_p)
1711 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1712 r.x = WINDOW_LEFT_EDGE_X (s->w);
1713 r.width = WINDOW_TOTAL_WIDTH (s->w);
1715 /* Unless displaying a mode or menu bar line, which are always
1716 fully visible, clip to the visible part of the row. */
1717 if (s->w->pseudo_window_p)
1718 r.height = s->row->visible_height;
1719 else
1720 r.height = s->height;
1722 else
1724 /* This is a text line that may be partially visible. */
1725 r.x = window_box_left (s->w, s->area);
1726 r.width = window_box_width (s->w, s->area);
1727 r.height = s->row->visible_height;
1730 if (s->clip_head)
1731 if (r.x < s->clip_head->x)
1733 if (r.width >= s->clip_head->x - r.x)
1734 r.width -= s->clip_head->x - r.x;
1735 else
1736 r.width = 0;
1737 r.x = s->clip_head->x;
1739 if (s->clip_tail)
1740 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1742 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1743 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1744 else
1745 r.width = 0;
1748 /* If S draws overlapping rows, it's sufficient to use the top and
1749 bottom of the window for clipping because this glyph string
1750 intentionally draws over other lines. */
1751 if (s->for_overlaps)
1753 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1754 r.height = window_text_bottom_y (s->w) - r.y;
1756 /* Alas, the above simple strategy does not work for the
1757 environments with anti-aliased text: if the same text is
1758 drawn onto the same place multiple times, it gets thicker.
1759 If the overlap we are processing is for the erased cursor, we
1760 take the intersection with the rectagle of the cursor. */
1761 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1763 XRectangle rc, r_save = r;
1765 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1766 rc.y = s->w->phys_cursor.y;
1767 rc.width = s->w->phys_cursor_width;
1768 rc.height = s->w->phys_cursor_height;
1770 x_intersect_rectangles (&r_save, &rc, &r);
1773 else
1775 /* Don't use S->y for clipping because it doesn't take partially
1776 visible lines into account. For example, it can be negative for
1777 partially visible lines at the top of a window. */
1778 if (!s->row->full_width_p
1779 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1780 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1781 else
1782 r.y = max (0, s->row->y);
1785 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1787 /* If drawing the cursor, don't let glyph draw outside its
1788 advertised boundaries. Cleartype does this under some circumstances. */
1789 if (s->hl == DRAW_CURSOR)
1791 struct glyph *glyph = s->first_glyph;
1792 int height, max_y;
1794 if (s->x > r.x)
1796 r.width -= s->x - r.x;
1797 r.x = s->x;
1799 r.width = min (r.width, glyph->pixel_width);
1801 /* If r.y is below window bottom, ensure that we still see a cursor. */
1802 height = min (glyph->ascent + glyph->descent,
1803 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1804 max_y = window_text_bottom_y (s->w) - height;
1805 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1806 if (s->ybase - glyph->ascent > max_y)
1808 r.y = max_y;
1809 r.height = height;
1811 else
1813 /* Don't draw cursor glyph taller than our actual glyph. */
1814 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1815 if (height < r.height)
1817 max_y = r.y + r.height;
1818 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1819 r.height = min (max_y - r.y, height);
1824 if (s->row->clip)
1826 XRectangle r_save = r;
1828 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
1829 r.width = 0;
1832 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1833 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1835 #ifdef CONVERT_FROM_XRECT
1836 CONVERT_FROM_XRECT (r, *rects);
1837 #else
1838 *rects = r;
1839 #endif
1840 return 1;
1842 else
1844 /* If we are processing overlapping and allowed to return
1845 multiple clipping rectangles, we exclude the row of the glyph
1846 string from the clipping rectangle. This is to avoid drawing
1847 the same text on the environment with anti-aliasing. */
1848 #ifdef CONVERT_FROM_XRECT
1849 XRectangle rs[2];
1850 #else
1851 XRectangle *rs = rects;
1852 #endif
1853 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1855 if (s->for_overlaps & OVERLAPS_PRED)
1857 rs[i] = r;
1858 if (r.y + r.height > row_y)
1860 if (r.y < row_y)
1861 rs[i].height = row_y - r.y;
1862 else
1863 rs[i].height = 0;
1865 i++;
1867 if (s->for_overlaps & OVERLAPS_SUCC)
1869 rs[i] = r;
1870 if (r.y < row_y + s->row->visible_height)
1872 if (r.y + r.height > row_y + s->row->visible_height)
1874 rs[i].y = row_y + s->row->visible_height;
1875 rs[i].height = r.y + r.height - rs[i].y;
1877 else
1878 rs[i].height = 0;
1880 i++;
1883 n = i;
1884 #ifdef CONVERT_FROM_XRECT
1885 for (i = 0; i < n; i++)
1886 CONVERT_FROM_XRECT (rs[i], rects[i]);
1887 #endif
1888 return n;
1892 /* EXPORT:
1893 Return in *NR the clipping rectangle for glyph string S. */
1895 void
1896 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
1898 get_glyph_string_clip_rects (s, nr, 1);
1902 /* EXPORT:
1903 Return the position and height of the phys cursor in window W.
1904 Set w->phys_cursor_width to width of phys cursor.
1907 void
1908 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
1909 struct glyph *glyph, int *xp, int *yp, int *heightp)
1911 struct frame *f = XFRAME (WINDOW_FRAME (w));
1912 int x, y, wd, h, h0, y0;
1914 /* Compute the width of the rectangle to draw. If on a stretch
1915 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1916 rectangle as wide as the glyph, but use a canonical character
1917 width instead. */
1918 wd = glyph->pixel_width - 1;
1919 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
1920 wd++; /* Why? */
1921 #endif
1923 x = w->phys_cursor.x;
1924 if (x < 0)
1926 wd += x;
1927 x = 0;
1930 if (glyph->type == STRETCH_GLYPH
1931 && !x_stretch_cursor_p)
1932 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1933 w->phys_cursor_width = wd;
1935 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1937 /* If y is below window bottom, ensure that we still see a cursor. */
1938 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1940 h = max (h0, glyph->ascent + glyph->descent);
1941 h0 = min (h0, glyph->ascent + glyph->descent);
1943 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
1944 if (y < y0)
1946 h = max (h - (y0 - y) + 1, h0);
1947 y = y0 - 1;
1949 else
1951 y0 = window_text_bottom_y (w) - h0;
1952 if (y > y0)
1954 h += y - y0;
1955 y = y0;
1959 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
1960 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
1961 *heightp = h;
1965 * Remember which glyph the mouse is over.
1968 void
1969 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
1971 Lisp_Object window;
1972 struct window *w;
1973 struct glyph_row *r, *gr, *end_row;
1974 enum window_part part;
1975 enum glyph_row_area area;
1976 int x, y, width, height;
1978 /* Try to determine frame pixel position and size of the glyph under
1979 frame pixel coordinates X/Y on frame F. */
1981 if (!f->glyphs_initialized_p
1982 || (window = window_from_coordinates (f, gx, gy, &part, 0),
1983 NILP (window)))
1985 width = FRAME_SMALLEST_CHAR_WIDTH (f);
1986 height = FRAME_SMALLEST_FONT_HEIGHT (f);
1987 goto virtual_glyph;
1990 w = XWINDOW (window);
1991 width = WINDOW_FRAME_COLUMN_WIDTH (w);
1992 height = WINDOW_FRAME_LINE_HEIGHT (w);
1994 x = window_relative_x_coord (w, part, gx);
1995 y = gy - WINDOW_TOP_EDGE_Y (w);
1997 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1998 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2000 if (w->pseudo_window_p)
2002 area = TEXT_AREA;
2003 part = ON_MODE_LINE; /* Don't adjust margin. */
2004 goto text_glyph;
2007 switch (part)
2009 case ON_LEFT_MARGIN:
2010 area = LEFT_MARGIN_AREA;
2011 goto text_glyph;
2013 case ON_RIGHT_MARGIN:
2014 area = RIGHT_MARGIN_AREA;
2015 goto text_glyph;
2017 case ON_HEADER_LINE:
2018 case ON_MODE_LINE:
2019 gr = (part == ON_HEADER_LINE
2020 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2021 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2022 gy = gr->y;
2023 area = TEXT_AREA;
2024 goto text_glyph_row_found;
2026 case ON_TEXT:
2027 area = TEXT_AREA;
2029 text_glyph:
2030 gr = 0; gy = 0;
2031 for (; r <= end_row && r->enabled_p; ++r)
2032 if (r->y + r->height > y)
2034 gr = r; gy = r->y;
2035 break;
2038 text_glyph_row_found:
2039 if (gr && gy <= y)
2041 struct glyph *g = gr->glyphs[area];
2042 struct glyph *end = g + gr->used[area];
2044 height = gr->height;
2045 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2046 if (gx + g->pixel_width > x)
2047 break;
2049 if (g < end)
2051 if (g->type == IMAGE_GLYPH)
2053 /* Don't remember when mouse is over image, as
2054 image may have hot-spots. */
2055 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2056 return;
2058 width = g->pixel_width;
2060 else
2062 /* Use nominal char spacing at end of line. */
2063 x -= gx;
2064 gx += (x / width) * width;
2067 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2068 gx += window_box_left_offset (w, area);
2070 else
2072 /* Use nominal line height at end of window. */
2073 gx = (x / width) * width;
2074 y -= gy;
2075 gy += (y / height) * height;
2077 break;
2079 case ON_LEFT_FRINGE:
2080 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2081 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2082 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2083 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2084 goto row_glyph;
2086 case ON_RIGHT_FRINGE:
2087 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2088 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2089 : window_box_right_offset (w, TEXT_AREA));
2090 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2091 goto row_glyph;
2093 case ON_SCROLL_BAR:
2094 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2096 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2097 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2098 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2099 : 0)));
2100 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2102 row_glyph:
2103 gr = 0, gy = 0;
2104 for (; r <= end_row && r->enabled_p; ++r)
2105 if (r->y + r->height > y)
2107 gr = r; gy = r->y;
2108 break;
2111 if (gr && gy <= y)
2112 height = gr->height;
2113 else
2115 /* Use nominal line height at end of window. */
2116 y -= gy;
2117 gy += (y / height) * height;
2119 break;
2121 default:
2123 virtual_glyph:
2124 /* If there is no glyph under the mouse, then we divide the screen
2125 into a grid of the smallest glyph in the frame, and use that
2126 as our "glyph". */
2128 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2129 round down even for negative values. */
2130 if (gx < 0)
2131 gx -= width - 1;
2132 if (gy < 0)
2133 gy -= height - 1;
2135 gx = (gx / width) * width;
2136 gy = (gy / height) * height;
2138 goto store_rect;
2141 gx += WINDOW_LEFT_EDGE_X (w);
2142 gy += WINDOW_TOP_EDGE_Y (w);
2144 store_rect:
2145 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2147 /* Visible feedback for debugging. */
2148 #if 0
2149 #if HAVE_X_WINDOWS
2150 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2151 f->output_data.x->normal_gc,
2152 gx, gy, width, height);
2153 #endif
2154 #endif
2158 #endif /* HAVE_WINDOW_SYSTEM */
2161 /***********************************************************************
2162 Lisp form evaluation
2163 ***********************************************************************/
2165 /* Error handler for safe_eval and safe_call. */
2167 static Lisp_Object
2168 safe_eval_handler (Lisp_Object arg)
2170 add_to_log ("Error during redisplay: %S", arg, Qnil);
2171 return Qnil;
2175 /* Evaluate SEXPR and return the result, or nil if something went
2176 wrong. Prevent redisplay during the evaluation. */
2178 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2179 Return the result, or nil if something went wrong. Prevent
2180 redisplay during the evaluation. */
2182 Lisp_Object
2183 safe_call (ptrdiff_t nargs, Lisp_Object *args)
2185 Lisp_Object val;
2187 if (inhibit_eval_during_redisplay)
2188 val = Qnil;
2189 else
2191 int count = SPECPDL_INDEX ();
2192 struct gcpro gcpro1;
2194 GCPRO1 (args[0]);
2195 gcpro1.nvars = nargs;
2196 specbind (Qinhibit_redisplay, Qt);
2197 /* Use Qt to ensure debugger does not run,
2198 so there is no possibility of wanting to redisplay. */
2199 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2200 safe_eval_handler);
2201 UNGCPRO;
2202 val = unbind_to (count, val);
2205 return val;
2209 /* Call function FN with one argument ARG.
2210 Return the result, or nil if something went wrong. */
2212 Lisp_Object
2213 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2215 Lisp_Object args[2];
2216 args[0] = fn;
2217 args[1] = arg;
2218 return safe_call (2, args);
2221 static Lisp_Object Qeval;
2223 Lisp_Object
2224 safe_eval (Lisp_Object sexpr)
2226 return safe_call1 (Qeval, sexpr);
2229 /* Call function FN with one argument ARG.
2230 Return the result, or nil if something went wrong. */
2232 Lisp_Object
2233 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2235 Lisp_Object args[3];
2236 args[0] = fn;
2237 args[1] = arg1;
2238 args[2] = arg2;
2239 return safe_call (3, args);
2244 /***********************************************************************
2245 Debugging
2246 ***********************************************************************/
2248 #if 0
2250 /* Define CHECK_IT to perform sanity checks on iterators.
2251 This is for debugging. It is too slow to do unconditionally. */
2253 static void
2254 check_it (struct it *it)
2256 if (it->method == GET_FROM_STRING)
2258 xassert (STRINGP (it->string));
2259 xassert (IT_STRING_CHARPOS (*it) >= 0);
2261 else
2263 xassert (IT_STRING_CHARPOS (*it) < 0);
2264 if (it->method == GET_FROM_BUFFER)
2266 /* Check that character and byte positions agree. */
2267 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2271 if (it->dpvec)
2272 xassert (it->current.dpvec_index >= 0);
2273 else
2274 xassert (it->current.dpvec_index < 0);
2277 #define CHECK_IT(IT) check_it ((IT))
2279 #else /* not 0 */
2281 #define CHECK_IT(IT) (void) 0
2283 #endif /* not 0 */
2286 #if GLYPH_DEBUG && XASSERTS
2288 /* Check that the window end of window W is what we expect it
2289 to be---the last row in the current matrix displaying text. */
2291 static void
2292 check_window_end (struct window *w)
2294 if (!MINI_WINDOW_P (w)
2295 && !NILP (w->window_end_valid))
2297 struct glyph_row *row;
2298 xassert ((row = MATRIX_ROW (w->current_matrix,
2299 XFASTINT (w->window_end_vpos)),
2300 !row->enabled_p
2301 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2302 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2306 #define CHECK_WINDOW_END(W) check_window_end ((W))
2308 #else
2310 #define CHECK_WINDOW_END(W) (void) 0
2312 #endif
2316 /***********************************************************************
2317 Iterator initialization
2318 ***********************************************************************/
2320 /* Initialize IT for displaying current_buffer in window W, starting
2321 at character position CHARPOS. CHARPOS < 0 means that no buffer
2322 position is specified which is useful when the iterator is assigned
2323 a position later. BYTEPOS is the byte position corresponding to
2324 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2326 If ROW is not null, calls to produce_glyphs with IT as parameter
2327 will produce glyphs in that row.
2329 BASE_FACE_ID is the id of a base face to use. It must be one of
2330 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2331 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2332 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2334 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2335 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2336 will be initialized to use the corresponding mode line glyph row of
2337 the desired matrix of W. */
2339 void
2340 init_iterator (struct it *it, struct window *w,
2341 EMACS_INT charpos, EMACS_INT bytepos,
2342 struct glyph_row *row, enum face_id base_face_id)
2344 int highlight_region_p;
2345 enum face_id remapped_base_face_id = base_face_id;
2347 /* Some precondition checks. */
2348 xassert (w != NULL && it != NULL);
2349 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2350 && charpos <= ZV));
2352 /* If face attributes have been changed since the last redisplay,
2353 free realized faces now because they depend on face definitions
2354 that might have changed. Don't free faces while there might be
2355 desired matrices pending which reference these faces. */
2356 if (face_change_count && !inhibit_free_realized_faces)
2358 face_change_count = 0;
2359 free_all_realized_faces (Qnil);
2362 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2363 if (! NILP (Vface_remapping_alist))
2364 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2366 /* Use one of the mode line rows of W's desired matrix if
2367 appropriate. */
2368 if (row == NULL)
2370 if (base_face_id == MODE_LINE_FACE_ID
2371 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2372 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2373 else if (base_face_id == HEADER_LINE_FACE_ID)
2374 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2377 /* Clear IT. */
2378 memset (it, 0, sizeof *it);
2379 it->current.overlay_string_index = -1;
2380 it->current.dpvec_index = -1;
2381 it->base_face_id = remapped_base_face_id;
2382 it->string = Qnil;
2383 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2384 it->paragraph_embedding = L2R;
2385 it->bidi_it.string.lstring = Qnil;
2386 it->bidi_it.string.s = NULL;
2387 it->bidi_it.string.bufpos = 0;
2389 /* The window in which we iterate over current_buffer: */
2390 XSETWINDOW (it->window, w);
2391 it->w = w;
2392 it->f = XFRAME (w->frame);
2394 it->cmp_it.id = -1;
2396 /* Extra space between lines (on window systems only). */
2397 if (base_face_id == DEFAULT_FACE_ID
2398 && FRAME_WINDOW_P (it->f))
2400 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2401 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2402 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2403 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2404 * FRAME_LINE_HEIGHT (it->f));
2405 else if (it->f->extra_line_spacing > 0)
2406 it->extra_line_spacing = it->f->extra_line_spacing;
2407 it->max_extra_line_spacing = 0;
2410 /* If realized faces have been removed, e.g. because of face
2411 attribute changes of named faces, recompute them. When running
2412 in batch mode, the face cache of the initial frame is null. If
2413 we happen to get called, make a dummy face cache. */
2414 if (FRAME_FACE_CACHE (it->f) == NULL)
2415 init_frame_faces (it->f);
2416 if (FRAME_FACE_CACHE (it->f)->used == 0)
2417 recompute_basic_faces (it->f);
2419 /* Current value of the `slice', `space-width', and 'height' properties. */
2420 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2421 it->space_width = Qnil;
2422 it->font_height = Qnil;
2423 it->override_ascent = -1;
2425 /* Are control characters displayed as `^C'? */
2426 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2428 /* -1 means everything between a CR and the following line end
2429 is invisible. >0 means lines indented more than this value are
2430 invisible. */
2431 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2432 ? XINT (BVAR (current_buffer, selective_display))
2433 : (!NILP (BVAR (current_buffer, selective_display))
2434 ? -1 : 0));
2435 it->selective_display_ellipsis_p
2436 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2438 /* Display table to use. */
2439 it->dp = window_display_table (w);
2441 /* Are multibyte characters enabled in current_buffer? */
2442 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2444 /* Non-zero if we should highlight the region. */
2445 highlight_region_p
2446 = (!NILP (Vtransient_mark_mode)
2447 && !NILP (BVAR (current_buffer, mark_active))
2448 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2450 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2451 start and end of a visible region in window IT->w. Set both to
2452 -1 to indicate no region. */
2453 if (highlight_region_p
2454 /* Maybe highlight only in selected window. */
2455 && (/* Either show region everywhere. */
2456 highlight_nonselected_windows
2457 /* Or show region in the selected window. */
2458 || w == XWINDOW (selected_window)
2459 /* Or show the region if we are in the mini-buffer and W is
2460 the window the mini-buffer refers to. */
2461 || (MINI_WINDOW_P (XWINDOW (selected_window))
2462 && WINDOWP (minibuf_selected_window)
2463 && w == XWINDOW (minibuf_selected_window))))
2465 EMACS_INT markpos = marker_position (BVAR (current_buffer, mark));
2466 it->region_beg_charpos = min (PT, markpos);
2467 it->region_end_charpos = max (PT, markpos);
2469 else
2470 it->region_beg_charpos = it->region_end_charpos = -1;
2472 /* Get the position at which the redisplay_end_trigger hook should
2473 be run, if it is to be run at all. */
2474 if (MARKERP (w->redisplay_end_trigger)
2475 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2476 it->redisplay_end_trigger_charpos
2477 = marker_position (w->redisplay_end_trigger);
2478 else if (INTEGERP (w->redisplay_end_trigger))
2479 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2481 /* Correct bogus values of tab_width. */
2482 it->tab_width = XINT (BVAR (current_buffer, tab_width));
2483 if (it->tab_width <= 0 || it->tab_width > 1000)
2484 it->tab_width = 8;
2486 /* Are lines in the display truncated? */
2487 if (base_face_id != DEFAULT_FACE_ID
2488 || XINT (it->w->hscroll)
2489 || (! WINDOW_FULL_WIDTH_P (it->w)
2490 && ((!NILP (Vtruncate_partial_width_windows)
2491 && !INTEGERP (Vtruncate_partial_width_windows))
2492 || (INTEGERP (Vtruncate_partial_width_windows)
2493 && (WINDOW_TOTAL_COLS (it->w)
2494 < XINT (Vtruncate_partial_width_windows))))))
2495 it->line_wrap = TRUNCATE;
2496 else if (NILP (BVAR (current_buffer, truncate_lines)))
2497 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2498 ? WINDOW_WRAP : WORD_WRAP;
2499 else
2500 it->line_wrap = TRUNCATE;
2502 /* Get dimensions of truncation and continuation glyphs. These are
2503 displayed as fringe bitmaps under X, so we don't need them for such
2504 frames. */
2505 if (!FRAME_WINDOW_P (it->f))
2507 if (it->line_wrap == TRUNCATE)
2509 /* We will need the truncation glyph. */
2510 xassert (it->glyph_row == NULL);
2511 produce_special_glyphs (it, IT_TRUNCATION);
2512 it->truncation_pixel_width = it->pixel_width;
2514 else
2516 /* We will need the continuation glyph. */
2517 xassert (it->glyph_row == NULL);
2518 produce_special_glyphs (it, IT_CONTINUATION);
2519 it->continuation_pixel_width = it->pixel_width;
2522 /* Reset these values to zero because the produce_special_glyphs
2523 above has changed them. */
2524 it->pixel_width = it->ascent = it->descent = 0;
2525 it->phys_ascent = it->phys_descent = 0;
2528 /* Set this after getting the dimensions of truncation and
2529 continuation glyphs, so that we don't produce glyphs when calling
2530 produce_special_glyphs, above. */
2531 it->glyph_row = row;
2532 it->area = TEXT_AREA;
2534 /* Forget any previous info about this row being reversed. */
2535 if (it->glyph_row)
2536 it->glyph_row->reversed_p = 0;
2538 /* Get the dimensions of the display area. The display area
2539 consists of the visible window area plus a horizontally scrolled
2540 part to the left of the window. All x-values are relative to the
2541 start of this total display area. */
2542 if (base_face_id != DEFAULT_FACE_ID)
2544 /* Mode lines, menu bar in terminal frames. */
2545 it->first_visible_x = 0;
2546 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2548 else
2550 it->first_visible_x
2551 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2552 it->last_visible_x = (it->first_visible_x
2553 + window_box_width (w, TEXT_AREA));
2555 /* If we truncate lines, leave room for the truncator glyph(s) at
2556 the right margin. Otherwise, leave room for the continuation
2557 glyph(s). Truncation and continuation glyphs are not inserted
2558 for window-based redisplay. */
2559 if (!FRAME_WINDOW_P (it->f))
2561 if (it->line_wrap == TRUNCATE)
2562 it->last_visible_x -= it->truncation_pixel_width;
2563 else
2564 it->last_visible_x -= it->continuation_pixel_width;
2567 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2568 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2571 /* Leave room for a border glyph. */
2572 if (!FRAME_WINDOW_P (it->f)
2573 && !WINDOW_RIGHTMOST_P (it->w))
2574 it->last_visible_x -= 1;
2576 it->last_visible_y = window_text_bottom_y (w);
2578 /* For mode lines and alike, arrange for the first glyph having a
2579 left box line if the face specifies a box. */
2580 if (base_face_id != DEFAULT_FACE_ID)
2582 struct face *face;
2584 it->face_id = remapped_base_face_id;
2586 /* If we have a boxed mode line, make the first character appear
2587 with a left box line. */
2588 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2589 if (face->box != FACE_NO_BOX)
2590 it->start_of_box_run_p = 1;
2593 /* If a buffer position was specified, set the iterator there,
2594 getting overlays and face properties from that position. */
2595 if (charpos >= BUF_BEG (current_buffer))
2597 it->end_charpos = ZV;
2598 it->face_id = -1;
2599 IT_CHARPOS (*it) = charpos;
2601 /* Compute byte position if not specified. */
2602 if (bytepos < charpos)
2603 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2604 else
2605 IT_BYTEPOS (*it) = bytepos;
2607 it->start = it->current;
2608 /* Do we need to reorder bidirectional text? Not if this is a
2609 unibyte buffer: by definition, none of the single-byte
2610 characters are strong R2L, so no reordering is needed. And
2611 bidi.c doesn't support unibyte buffers anyway. */
2612 it->bidi_p =
2613 !NILP (BVAR (current_buffer, bidi_display_reordering))
2614 && it->multibyte_p;
2616 /* If we are to reorder bidirectional text, init the bidi
2617 iterator. */
2618 if (it->bidi_p)
2620 /* Note the paragraph direction that this buffer wants to
2621 use. */
2622 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2623 Qleft_to_right))
2624 it->paragraph_embedding = L2R;
2625 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2626 Qright_to_left))
2627 it->paragraph_embedding = R2L;
2628 else
2629 it->paragraph_embedding = NEUTRAL_DIR;
2630 bidi_unshelve_cache (NULL, 0);
2631 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2632 &it->bidi_it);
2635 /* Compute faces etc. */
2636 reseat (it, it->current.pos, 1);
2639 CHECK_IT (it);
2643 /* Initialize IT for the display of window W with window start POS. */
2645 void
2646 start_display (struct it *it, struct window *w, struct text_pos pos)
2648 struct glyph_row *row;
2649 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2651 row = w->desired_matrix->rows + first_vpos;
2652 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2653 it->first_vpos = first_vpos;
2655 /* Don't reseat to previous visible line start if current start
2656 position is in a string or image. */
2657 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2659 int start_at_line_beg_p;
2660 int first_y = it->current_y;
2662 /* If window start is not at a line start, skip forward to POS to
2663 get the correct continuation lines width. */
2664 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2665 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2666 if (!start_at_line_beg_p)
2668 int new_x;
2670 reseat_at_previous_visible_line_start (it);
2671 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2673 new_x = it->current_x + it->pixel_width;
2675 /* If lines are continued, this line may end in the middle
2676 of a multi-glyph character (e.g. a control character
2677 displayed as \003, or in the middle of an overlay
2678 string). In this case move_it_to above will not have
2679 taken us to the start of the continuation line but to the
2680 end of the continued line. */
2681 if (it->current_x > 0
2682 && it->line_wrap != TRUNCATE /* Lines are continued. */
2683 && (/* And glyph doesn't fit on the line. */
2684 new_x > it->last_visible_x
2685 /* Or it fits exactly and we're on a window
2686 system frame. */
2687 || (new_x == it->last_visible_x
2688 && FRAME_WINDOW_P (it->f))))
2690 if (it->current.dpvec_index >= 0
2691 || it->current.overlay_string_index >= 0)
2693 set_iterator_to_next (it, 1);
2694 move_it_in_display_line_to (it, -1, -1, 0);
2697 it->continuation_lines_width += it->current_x;
2700 /* We're starting a new display line, not affected by the
2701 height of the continued line, so clear the appropriate
2702 fields in the iterator structure. */
2703 it->max_ascent = it->max_descent = 0;
2704 it->max_phys_ascent = it->max_phys_descent = 0;
2706 it->current_y = first_y;
2707 it->vpos = 0;
2708 it->current_x = it->hpos = 0;
2714 /* Return 1 if POS is a position in ellipses displayed for invisible
2715 text. W is the window we display, for text property lookup. */
2717 static int
2718 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2720 Lisp_Object prop, window;
2721 int ellipses_p = 0;
2722 EMACS_INT charpos = CHARPOS (pos->pos);
2724 /* If POS specifies a position in a display vector, this might
2725 be for an ellipsis displayed for invisible text. We won't
2726 get the iterator set up for delivering that ellipsis unless
2727 we make sure that it gets aware of the invisible text. */
2728 if (pos->dpvec_index >= 0
2729 && pos->overlay_string_index < 0
2730 && CHARPOS (pos->string_pos) < 0
2731 && charpos > BEGV
2732 && (XSETWINDOW (window, w),
2733 prop = Fget_char_property (make_number (charpos),
2734 Qinvisible, window),
2735 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2737 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2738 window);
2739 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2742 return ellipses_p;
2746 /* Initialize IT for stepping through current_buffer in window W,
2747 starting at position POS that includes overlay string and display
2748 vector/ control character translation position information. Value
2749 is zero if there are overlay strings with newlines at POS. */
2751 static int
2752 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2754 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2755 int i, overlay_strings_with_newlines = 0;
2757 /* If POS specifies a position in a display vector, this might
2758 be for an ellipsis displayed for invisible text. We won't
2759 get the iterator set up for delivering that ellipsis unless
2760 we make sure that it gets aware of the invisible text. */
2761 if (in_ellipses_for_invisible_text_p (pos, w))
2763 --charpos;
2764 bytepos = 0;
2767 /* Keep in mind: the call to reseat in init_iterator skips invisible
2768 text, so we might end up at a position different from POS. This
2769 is only a problem when POS is a row start after a newline and an
2770 overlay starts there with an after-string, and the overlay has an
2771 invisible property. Since we don't skip invisible text in
2772 display_line and elsewhere immediately after consuming the
2773 newline before the row start, such a POS will not be in a string,
2774 but the call to init_iterator below will move us to the
2775 after-string. */
2776 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2778 /* This only scans the current chunk -- it should scan all chunks.
2779 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2780 to 16 in 22.1 to make this a lesser problem. */
2781 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2783 const char *s = SSDATA (it->overlay_strings[i]);
2784 const char *e = s + SBYTES (it->overlay_strings[i]);
2786 while (s < e && *s != '\n')
2787 ++s;
2789 if (s < e)
2791 overlay_strings_with_newlines = 1;
2792 break;
2796 /* If position is within an overlay string, set up IT to the right
2797 overlay string. */
2798 if (pos->overlay_string_index >= 0)
2800 int relative_index;
2802 /* If the first overlay string happens to have a `display'
2803 property for an image, the iterator will be set up for that
2804 image, and we have to undo that setup first before we can
2805 correct the overlay string index. */
2806 if (it->method == GET_FROM_IMAGE)
2807 pop_it (it);
2809 /* We already have the first chunk of overlay strings in
2810 IT->overlay_strings. Load more until the one for
2811 pos->overlay_string_index is in IT->overlay_strings. */
2812 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2814 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2815 it->current.overlay_string_index = 0;
2816 while (n--)
2818 load_overlay_strings (it, 0);
2819 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2823 it->current.overlay_string_index = pos->overlay_string_index;
2824 relative_index = (it->current.overlay_string_index
2825 % OVERLAY_STRING_CHUNK_SIZE);
2826 it->string = it->overlay_strings[relative_index];
2827 xassert (STRINGP (it->string));
2828 it->current.string_pos = pos->string_pos;
2829 it->method = GET_FROM_STRING;
2832 if (CHARPOS (pos->string_pos) >= 0)
2834 /* Recorded position is not in an overlay string, but in another
2835 string. This can only be a string from a `display' property.
2836 IT should already be filled with that string. */
2837 it->current.string_pos = pos->string_pos;
2838 xassert (STRINGP (it->string));
2841 /* Restore position in display vector translations, control
2842 character translations or ellipses. */
2843 if (pos->dpvec_index >= 0)
2845 if (it->dpvec == NULL)
2846 get_next_display_element (it);
2847 xassert (it->dpvec && it->current.dpvec_index == 0);
2848 it->current.dpvec_index = pos->dpvec_index;
2851 CHECK_IT (it);
2852 return !overlay_strings_with_newlines;
2856 /* Initialize IT for stepping through current_buffer in window W
2857 starting at ROW->start. */
2859 static void
2860 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
2862 init_from_display_pos (it, w, &row->start);
2863 it->start = row->start;
2864 it->continuation_lines_width = row->continuation_lines_width;
2865 CHECK_IT (it);
2869 /* Initialize IT for stepping through current_buffer in window W
2870 starting in the line following ROW, i.e. starting at ROW->end.
2871 Value is zero if there are overlay strings with newlines at ROW's
2872 end position. */
2874 static int
2875 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
2877 int success = 0;
2879 if (init_from_display_pos (it, w, &row->end))
2881 if (row->continued_p)
2882 it->continuation_lines_width
2883 = row->continuation_lines_width + row->pixel_width;
2884 CHECK_IT (it);
2885 success = 1;
2888 return success;
2894 /***********************************************************************
2895 Text properties
2896 ***********************************************************************/
2898 /* Called when IT reaches IT->stop_charpos. Handle text property and
2899 overlay changes. Set IT->stop_charpos to the next position where
2900 to stop. */
2902 static void
2903 handle_stop (struct it *it)
2905 enum prop_handled handled;
2906 int handle_overlay_change_p;
2907 struct props *p;
2909 it->dpvec = NULL;
2910 it->current.dpvec_index = -1;
2911 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
2912 it->ignore_overlay_strings_at_pos_p = 0;
2913 it->ellipsis_p = 0;
2915 /* Use face of preceding text for ellipsis (if invisible) */
2916 if (it->selective_display_ellipsis_p)
2917 it->saved_face_id = it->face_id;
2921 handled = HANDLED_NORMALLY;
2923 /* Call text property handlers. */
2924 for (p = it_props; p->handler; ++p)
2926 handled = p->handler (it);
2928 if (handled == HANDLED_RECOMPUTE_PROPS)
2929 break;
2930 else if (handled == HANDLED_RETURN)
2932 /* We still want to show before and after strings from
2933 overlays even if the actual buffer text is replaced. */
2934 if (!handle_overlay_change_p
2935 || it->sp > 1
2936 || !get_overlay_strings_1 (it, 0, 0))
2938 if (it->ellipsis_p)
2939 setup_for_ellipsis (it, 0);
2940 /* When handling a display spec, we might load an
2941 empty string. In that case, discard it here. We
2942 used to discard it in handle_single_display_spec,
2943 but that causes get_overlay_strings_1, above, to
2944 ignore overlay strings that we must check. */
2945 if (STRINGP (it->string) && !SCHARS (it->string))
2946 pop_it (it);
2947 return;
2949 else if (STRINGP (it->string) && !SCHARS (it->string))
2950 pop_it (it);
2951 else
2953 it->ignore_overlay_strings_at_pos_p = 1;
2954 it->string_from_display_prop_p = 0;
2955 it->from_disp_prop_p = 0;
2956 handle_overlay_change_p = 0;
2958 handled = HANDLED_RECOMPUTE_PROPS;
2959 break;
2961 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2962 handle_overlay_change_p = 0;
2965 if (handled != HANDLED_RECOMPUTE_PROPS)
2967 /* Don't check for overlay strings below when set to deliver
2968 characters from a display vector. */
2969 if (it->method == GET_FROM_DISPLAY_VECTOR)
2970 handle_overlay_change_p = 0;
2972 /* Handle overlay changes.
2973 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
2974 if it finds overlays. */
2975 if (handle_overlay_change_p)
2976 handled = handle_overlay_change (it);
2979 if (it->ellipsis_p)
2981 setup_for_ellipsis (it, 0);
2982 break;
2985 while (handled == HANDLED_RECOMPUTE_PROPS);
2987 /* Determine where to stop next. */
2988 if (handled == HANDLED_NORMALLY)
2989 compute_stop_pos (it);
2993 /* Compute IT->stop_charpos from text property and overlay change
2994 information for IT's current position. */
2996 static void
2997 compute_stop_pos (struct it *it)
2999 register INTERVAL iv, next_iv;
3000 Lisp_Object object, limit, position;
3001 EMACS_INT charpos, bytepos;
3003 /* If nowhere else, stop at the end. */
3004 it->stop_charpos = it->end_charpos;
3006 if (STRINGP (it->string))
3008 /* Strings are usually short, so don't limit the search for
3009 properties. */
3010 object = it->string;
3011 limit = Qnil;
3012 charpos = IT_STRING_CHARPOS (*it);
3013 bytepos = IT_STRING_BYTEPOS (*it);
3015 else
3017 EMACS_INT pos;
3019 /* If next overlay change is in front of the current stop pos
3020 (which is IT->end_charpos), stop there. Note: value of
3021 next_overlay_change is point-max if no overlay change
3022 follows. */
3023 charpos = IT_CHARPOS (*it);
3024 bytepos = IT_BYTEPOS (*it);
3025 pos = next_overlay_change (charpos);
3026 if (pos < it->stop_charpos)
3027 it->stop_charpos = pos;
3029 /* If showing the region, we have to stop at the region
3030 start or end because the face might change there. */
3031 if (it->region_beg_charpos > 0)
3033 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3034 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3035 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3036 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3039 /* Set up variables for computing the stop position from text
3040 property changes. */
3041 XSETBUFFER (object, current_buffer);
3042 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3045 /* Get the interval containing IT's position. Value is a null
3046 interval if there isn't such an interval. */
3047 position = make_number (charpos);
3048 iv = validate_interval_range (object, &position, &position, 0);
3049 if (!NULL_INTERVAL_P (iv))
3051 Lisp_Object values_here[LAST_PROP_IDX];
3052 struct props *p;
3054 /* Get properties here. */
3055 for (p = it_props; p->handler; ++p)
3056 values_here[p->idx] = textget (iv->plist, *p->name);
3058 /* Look for an interval following iv that has different
3059 properties. */
3060 for (next_iv = next_interval (iv);
3061 (!NULL_INTERVAL_P (next_iv)
3062 && (NILP (limit)
3063 || XFASTINT (limit) > next_iv->position));
3064 next_iv = next_interval (next_iv))
3066 for (p = it_props; p->handler; ++p)
3068 Lisp_Object new_value;
3070 new_value = textget (next_iv->plist, *p->name);
3071 if (!EQ (values_here[p->idx], new_value))
3072 break;
3075 if (p->handler)
3076 break;
3079 if (!NULL_INTERVAL_P (next_iv))
3081 if (INTEGERP (limit)
3082 && next_iv->position >= XFASTINT (limit))
3083 /* No text property change up to limit. */
3084 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3085 else
3086 /* Text properties change in next_iv. */
3087 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3091 if (it->cmp_it.id < 0)
3093 EMACS_INT stoppos = it->end_charpos;
3095 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3096 stoppos = -1;
3097 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3098 stoppos, it->string);
3101 xassert (STRINGP (it->string)
3102 || (it->stop_charpos >= BEGV
3103 && it->stop_charpos >= IT_CHARPOS (*it)));
3107 /* Return the position of the next overlay change after POS in
3108 current_buffer. Value is point-max if no overlay change
3109 follows. This is like `next-overlay-change' but doesn't use
3110 xmalloc. */
3112 static EMACS_INT
3113 next_overlay_change (EMACS_INT pos)
3115 ptrdiff_t i, noverlays;
3116 EMACS_INT endpos;
3117 Lisp_Object *overlays;
3119 /* Get all overlays at the given position. */
3120 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3122 /* If any of these overlays ends before endpos,
3123 use its ending point instead. */
3124 for (i = 0; i < noverlays; ++i)
3126 Lisp_Object oend;
3127 EMACS_INT oendpos;
3129 oend = OVERLAY_END (overlays[i]);
3130 oendpos = OVERLAY_POSITION (oend);
3131 endpos = min (endpos, oendpos);
3134 return endpos;
3137 /* How many characters forward to search for a display property or
3138 display string. Enough for a screenful of 100 lines x 50
3139 characters in a line. */
3140 #define MAX_DISP_SCAN 5000
3142 /* Return the character position of a display string at or after
3143 position specified by POSITION. If no display string exists at or
3144 after POSITION, return ZV. A display string is either an overlay
3145 with `display' property whose value is a string, or a `display'
3146 text property whose value is a string. STRING is data about the
3147 string to iterate; if STRING->lstring is nil, we are iterating a
3148 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3149 on a GUI frame. */
3150 EMACS_INT
3151 compute_display_string_pos (struct text_pos *position,
3152 struct bidi_string_data *string,
3153 int frame_window_p, int *disp_prop_p)
3155 /* OBJECT = nil means current buffer. */
3156 Lisp_Object object =
3157 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3158 Lisp_Object pos, spec, limpos;
3159 int string_p = (string && (STRINGP (string->lstring) || string->s));
3160 EMACS_INT eob = string_p ? string->schars : ZV;
3161 EMACS_INT begb = string_p ? 0 : BEGV;
3162 EMACS_INT bufpos, charpos = CHARPOS (*position);
3163 EMACS_INT lim =
3164 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3165 struct text_pos tpos;
3167 *disp_prop_p = 1;
3169 if (charpos >= eob
3170 /* We don't support display properties whose values are strings
3171 that have display string properties. */
3172 || string->from_disp_str
3173 /* C strings cannot have display properties. */
3174 || (string->s && !STRINGP (object)))
3176 *disp_prop_p = 0;
3177 return eob;
3180 /* If the character at CHARPOS is where the display string begins,
3181 return CHARPOS. */
3182 pos = make_number (charpos);
3183 if (STRINGP (object))
3184 bufpos = string->bufpos;
3185 else
3186 bufpos = charpos;
3187 tpos = *position;
3188 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3189 && (charpos <= begb
3190 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3191 object),
3192 spec))
3193 && handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3194 frame_window_p))
3196 return charpos;
3199 /* Look forward for the first character with a `display' property
3200 that will replace the underlying text when displayed. */
3201 limpos = make_number (lim);
3202 do {
3203 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3204 CHARPOS (tpos) = XFASTINT (pos);
3205 if (CHARPOS (tpos) >= lim)
3207 *disp_prop_p = 0;
3208 break;
3210 if (STRINGP (object))
3211 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3212 else
3213 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3214 spec = Fget_char_property (pos, Qdisplay, object);
3215 if (!STRINGP (object))
3216 bufpos = CHARPOS (tpos);
3217 } while (NILP (spec)
3218 || !handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3219 frame_window_p));
3221 return CHARPOS (tpos);
3224 /* Return the character position of the end of the display string that
3225 started at CHARPOS. A display string is either an overlay with
3226 `display' property whose value is a string or a `display' text
3227 property whose value is a string. */
3228 EMACS_INT
3229 compute_display_string_end (EMACS_INT charpos, struct bidi_string_data *string)
3231 /* OBJECT = nil means current buffer. */
3232 Lisp_Object object =
3233 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3234 Lisp_Object pos = make_number (charpos);
3235 EMACS_INT eob =
3236 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3238 if (charpos >= eob || (string->s && !STRINGP (object)))
3239 return eob;
3241 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3242 abort ();
3244 /* Look forward for the first character where the `display' property
3245 changes. */
3246 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3248 return XFASTINT (pos);
3253 /***********************************************************************
3254 Fontification
3255 ***********************************************************************/
3257 /* Handle changes in the `fontified' property of the current buffer by
3258 calling hook functions from Qfontification_functions to fontify
3259 regions of text. */
3261 static enum prop_handled
3262 handle_fontified_prop (struct it *it)
3264 Lisp_Object prop, pos;
3265 enum prop_handled handled = HANDLED_NORMALLY;
3267 if (!NILP (Vmemory_full))
3268 return handled;
3270 /* Get the value of the `fontified' property at IT's current buffer
3271 position. (The `fontified' property doesn't have a special
3272 meaning in strings.) If the value is nil, call functions from
3273 Qfontification_functions. */
3274 if (!STRINGP (it->string)
3275 && it->s == NULL
3276 && !NILP (Vfontification_functions)
3277 && !NILP (Vrun_hooks)
3278 && (pos = make_number (IT_CHARPOS (*it)),
3279 prop = Fget_char_property (pos, Qfontified, Qnil),
3280 /* Ignore the special cased nil value always present at EOB since
3281 no amount of fontifying will be able to change it. */
3282 NILP (prop) && IT_CHARPOS (*it) < Z))
3284 int count = SPECPDL_INDEX ();
3285 Lisp_Object val;
3286 struct buffer *obuf = current_buffer;
3287 int begv = BEGV, zv = ZV;
3288 int old_clip_changed = current_buffer->clip_changed;
3290 val = Vfontification_functions;
3291 specbind (Qfontification_functions, Qnil);
3293 xassert (it->end_charpos == ZV);
3295 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3296 safe_call1 (val, pos);
3297 else
3299 Lisp_Object fns, fn;
3300 struct gcpro gcpro1, gcpro2;
3302 fns = Qnil;
3303 GCPRO2 (val, fns);
3305 for (; CONSP (val); val = XCDR (val))
3307 fn = XCAR (val);
3309 if (EQ (fn, Qt))
3311 /* A value of t indicates this hook has a local
3312 binding; it means to run the global binding too.
3313 In a global value, t should not occur. If it
3314 does, we must ignore it to avoid an endless
3315 loop. */
3316 for (fns = Fdefault_value (Qfontification_functions);
3317 CONSP (fns);
3318 fns = XCDR (fns))
3320 fn = XCAR (fns);
3321 if (!EQ (fn, Qt))
3322 safe_call1 (fn, pos);
3325 else
3326 safe_call1 (fn, pos);
3329 UNGCPRO;
3332 unbind_to (count, Qnil);
3334 /* Fontification functions routinely call `save-restriction'.
3335 Normally, this tags clip_changed, which can confuse redisplay
3336 (see discussion in Bug#6671). Since we don't perform any
3337 special handling of fontification changes in the case where
3338 `save-restriction' isn't called, there's no point doing so in
3339 this case either. So, if the buffer's restrictions are
3340 actually left unchanged, reset clip_changed. */
3341 if (obuf == current_buffer)
3343 if (begv == BEGV && zv == ZV)
3344 current_buffer->clip_changed = old_clip_changed;
3346 /* There isn't much we can reasonably do to protect against
3347 misbehaving fontification, but here's a fig leaf. */
3348 else if (!NILP (BVAR (obuf, name)))
3349 set_buffer_internal_1 (obuf);
3351 /* The fontification code may have added/removed text.
3352 It could do even a lot worse, but let's at least protect against
3353 the most obvious case where only the text past `pos' gets changed',
3354 as is/was done in grep.el where some escapes sequences are turned
3355 into face properties (bug#7876). */
3356 it->end_charpos = ZV;
3358 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3359 something. This avoids an endless loop if they failed to
3360 fontify the text for which reason ever. */
3361 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3362 handled = HANDLED_RECOMPUTE_PROPS;
3365 return handled;
3370 /***********************************************************************
3371 Faces
3372 ***********************************************************************/
3374 /* Set up iterator IT from face properties at its current position.
3375 Called from handle_stop. */
3377 static enum prop_handled
3378 handle_face_prop (struct it *it)
3380 int new_face_id;
3381 EMACS_INT next_stop;
3383 if (!STRINGP (it->string))
3385 new_face_id
3386 = face_at_buffer_position (it->w,
3387 IT_CHARPOS (*it),
3388 it->region_beg_charpos,
3389 it->region_end_charpos,
3390 &next_stop,
3391 (IT_CHARPOS (*it)
3392 + TEXT_PROP_DISTANCE_LIMIT),
3393 0, it->base_face_id);
3395 /* Is this a start of a run of characters with box face?
3396 Caveat: this can be called for a freshly initialized
3397 iterator; face_id is -1 in this case. We know that the new
3398 face will not change until limit, i.e. if the new face has a
3399 box, all characters up to limit will have one. But, as
3400 usual, we don't know whether limit is really the end. */
3401 if (new_face_id != it->face_id)
3403 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3405 /* If new face has a box but old face has not, this is
3406 the start of a run of characters with box, i.e. it has
3407 a shadow on the left side. The value of face_id of the
3408 iterator will be -1 if this is the initial call that gets
3409 the face. In this case, we have to look in front of IT's
3410 position and see whether there is a face != new_face_id. */
3411 it->start_of_box_run_p
3412 = (new_face->box != FACE_NO_BOX
3413 && (it->face_id >= 0
3414 || IT_CHARPOS (*it) == BEG
3415 || new_face_id != face_before_it_pos (it)));
3416 it->face_box_p = new_face->box != FACE_NO_BOX;
3419 else
3421 int base_face_id;
3422 EMACS_INT bufpos;
3423 int i;
3424 Lisp_Object from_overlay
3425 = (it->current.overlay_string_index >= 0
3426 ? it->string_overlays[it->current.overlay_string_index]
3427 : Qnil);
3429 /* See if we got to this string directly or indirectly from
3430 an overlay property. That includes the before-string or
3431 after-string of an overlay, strings in display properties
3432 provided by an overlay, their text properties, etc.
3434 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3435 if (! NILP (from_overlay))
3436 for (i = it->sp - 1; i >= 0; i--)
3438 if (it->stack[i].current.overlay_string_index >= 0)
3439 from_overlay
3440 = it->string_overlays[it->stack[i].current.overlay_string_index];
3441 else if (! NILP (it->stack[i].from_overlay))
3442 from_overlay = it->stack[i].from_overlay;
3444 if (!NILP (from_overlay))
3445 break;
3448 if (! NILP (from_overlay))
3450 bufpos = IT_CHARPOS (*it);
3451 /* For a string from an overlay, the base face depends
3452 only on text properties and ignores overlays. */
3453 base_face_id
3454 = face_for_overlay_string (it->w,
3455 IT_CHARPOS (*it),
3456 it->region_beg_charpos,
3457 it->region_end_charpos,
3458 &next_stop,
3459 (IT_CHARPOS (*it)
3460 + TEXT_PROP_DISTANCE_LIMIT),
3462 from_overlay);
3464 else
3466 bufpos = 0;
3468 /* For strings from a `display' property, use the face at
3469 IT's current buffer position as the base face to merge
3470 with, so that overlay strings appear in the same face as
3471 surrounding text, unless they specify their own
3472 faces. */
3473 base_face_id = underlying_face_id (it);
3476 new_face_id = face_at_string_position (it->w,
3477 it->string,
3478 IT_STRING_CHARPOS (*it),
3479 bufpos,
3480 it->region_beg_charpos,
3481 it->region_end_charpos,
3482 &next_stop,
3483 base_face_id, 0);
3485 /* Is this a start of a run of characters with box? Caveat:
3486 this can be called for a freshly allocated iterator; face_id
3487 is -1 is this case. We know that the new face will not
3488 change until the next check pos, i.e. if the new face has a
3489 box, all characters up to that position will have a
3490 box. But, as usual, we don't know whether that position
3491 is really the end. */
3492 if (new_face_id != it->face_id)
3494 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3495 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3497 /* If new face has a box but old face hasn't, this is the
3498 start of a run of characters with box, i.e. it has a
3499 shadow on the left side. */
3500 it->start_of_box_run_p
3501 = new_face->box && (old_face == NULL || !old_face->box);
3502 it->face_box_p = new_face->box != FACE_NO_BOX;
3506 it->face_id = new_face_id;
3507 return HANDLED_NORMALLY;
3511 /* Return the ID of the face ``underlying'' IT's current position,
3512 which is in a string. If the iterator is associated with a
3513 buffer, return the face at IT's current buffer position.
3514 Otherwise, use the iterator's base_face_id. */
3516 static int
3517 underlying_face_id (struct it *it)
3519 int face_id = it->base_face_id, i;
3521 xassert (STRINGP (it->string));
3523 for (i = it->sp - 1; i >= 0; --i)
3524 if (NILP (it->stack[i].string))
3525 face_id = it->stack[i].face_id;
3527 return face_id;
3531 /* Compute the face one character before or after the current position
3532 of IT, in the visual order. BEFORE_P non-zero means get the face
3533 in front (to the left in L2R paragraphs, to the right in R2L
3534 paragraphs) of IT's screen position. Value is the ID of the face. */
3536 static int
3537 face_before_or_after_it_pos (struct it *it, int before_p)
3539 int face_id, limit;
3540 EMACS_INT next_check_charpos;
3541 struct it it_copy;
3542 void *it_copy_data = NULL;
3544 xassert (it->s == NULL);
3546 if (STRINGP (it->string))
3548 EMACS_INT bufpos, charpos;
3549 int base_face_id;
3551 /* No face change past the end of the string (for the case
3552 we are padding with spaces). No face change before the
3553 string start. */
3554 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3555 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3556 return it->face_id;
3558 if (!it->bidi_p)
3560 /* Set charpos to the position before or after IT's current
3561 position, in the logical order, which in the non-bidi
3562 case is the same as the visual order. */
3563 if (before_p)
3564 charpos = IT_STRING_CHARPOS (*it) - 1;
3565 else if (it->what == IT_COMPOSITION)
3566 /* For composition, we must check the character after the
3567 composition. */
3568 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3569 else
3570 charpos = IT_STRING_CHARPOS (*it) + 1;
3572 else
3574 if (before_p)
3576 /* With bidi iteration, the character before the current
3577 in the visual order cannot be found by simple
3578 iteration, because "reverse" reordering is not
3579 supported. Instead, we need to use the move_it_*
3580 family of functions. */
3581 /* Ignore face changes before the first visible
3582 character on this display line. */
3583 if (it->current_x <= it->first_visible_x)
3584 return it->face_id;
3585 SAVE_IT (it_copy, *it, it_copy_data);
3586 /* Implementation note: Since move_it_in_display_line
3587 works in the iterator geometry, and thinks the first
3588 character is always the leftmost, even in R2L lines,
3589 we don't need to distinguish between the R2L and L2R
3590 cases here. */
3591 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3592 it_copy.current_x - 1, MOVE_TO_X);
3593 charpos = IT_STRING_CHARPOS (it_copy);
3594 RESTORE_IT (it, it, it_copy_data);
3596 else
3598 /* Set charpos to the string position of the character
3599 that comes after IT's current position in the visual
3600 order. */
3601 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3603 it_copy = *it;
3604 while (n--)
3605 bidi_move_to_visually_next (&it_copy.bidi_it);
3607 charpos = it_copy.bidi_it.charpos;
3610 xassert (0 <= charpos && charpos <= SCHARS (it->string));
3612 if (it->current.overlay_string_index >= 0)
3613 bufpos = IT_CHARPOS (*it);
3614 else
3615 bufpos = 0;
3617 base_face_id = underlying_face_id (it);
3619 /* Get the face for ASCII, or unibyte. */
3620 face_id = face_at_string_position (it->w,
3621 it->string,
3622 charpos,
3623 bufpos,
3624 it->region_beg_charpos,
3625 it->region_end_charpos,
3626 &next_check_charpos,
3627 base_face_id, 0);
3629 /* Correct the face for charsets different from ASCII. Do it
3630 for the multibyte case only. The face returned above is
3631 suitable for unibyte text if IT->string is unibyte. */
3632 if (STRING_MULTIBYTE (it->string))
3634 struct text_pos pos1 = string_pos (charpos, it->string);
3635 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3636 int c, len;
3637 struct face *face = FACE_FROM_ID (it->f, face_id);
3639 c = string_char_and_length (p, &len);
3640 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3643 else
3645 struct text_pos pos;
3647 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3648 || (IT_CHARPOS (*it) <= BEGV && before_p))
3649 return it->face_id;
3651 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3652 pos = it->current.pos;
3654 if (!it->bidi_p)
3656 if (before_p)
3657 DEC_TEXT_POS (pos, it->multibyte_p);
3658 else
3660 if (it->what == IT_COMPOSITION)
3662 /* For composition, we must check the position after
3663 the composition. */
3664 pos.charpos += it->cmp_it.nchars;
3665 pos.bytepos += it->len;
3667 else
3668 INC_TEXT_POS (pos, it->multibyte_p);
3671 else
3673 if (before_p)
3675 /* With bidi iteration, the character before the current
3676 in the visual order cannot be found by simple
3677 iteration, because "reverse" reordering is not
3678 supported. Instead, we need to use the move_it_*
3679 family of functions. */
3680 /* Ignore face changes before the first visible
3681 character on this display line. */
3682 if (it->current_x <= it->first_visible_x)
3683 return it->face_id;
3684 SAVE_IT (it_copy, *it, it_copy_data);
3685 /* Implementation note: Since move_it_in_display_line
3686 works in the iterator geometry, and thinks the first
3687 character is always the leftmost, even in R2L lines,
3688 we don't need to distinguish between the R2L and L2R
3689 cases here. */
3690 move_it_in_display_line (&it_copy, ZV,
3691 it_copy.current_x - 1, MOVE_TO_X);
3692 pos = it_copy.current.pos;
3693 RESTORE_IT (it, it, it_copy_data);
3695 else
3697 /* Set charpos to the buffer position of the character
3698 that comes after IT's current position in the visual
3699 order. */
3700 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3702 it_copy = *it;
3703 while (n--)
3704 bidi_move_to_visually_next (&it_copy.bidi_it);
3706 SET_TEXT_POS (pos,
3707 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3710 xassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
3712 /* Determine face for CHARSET_ASCII, or unibyte. */
3713 face_id = face_at_buffer_position (it->w,
3714 CHARPOS (pos),
3715 it->region_beg_charpos,
3716 it->region_end_charpos,
3717 &next_check_charpos,
3718 limit, 0, -1);
3720 /* Correct the face for charsets different from ASCII. Do it
3721 for the multibyte case only. The face returned above is
3722 suitable for unibyte text if current_buffer is unibyte. */
3723 if (it->multibyte_p)
3725 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3726 struct face *face = FACE_FROM_ID (it->f, face_id);
3727 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3731 return face_id;
3736 /***********************************************************************
3737 Invisible text
3738 ***********************************************************************/
3740 /* Set up iterator IT from invisible properties at its current
3741 position. Called from handle_stop. */
3743 static enum prop_handled
3744 handle_invisible_prop (struct it *it)
3746 enum prop_handled handled = HANDLED_NORMALLY;
3748 if (STRINGP (it->string))
3750 Lisp_Object prop, end_charpos, limit, charpos;
3752 /* Get the value of the invisible text property at the
3753 current position. Value will be nil if there is no such
3754 property. */
3755 charpos = make_number (IT_STRING_CHARPOS (*it));
3756 prop = Fget_text_property (charpos, Qinvisible, it->string);
3758 if (!NILP (prop)
3759 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3761 EMACS_INT endpos;
3763 handled = HANDLED_RECOMPUTE_PROPS;
3765 /* Get the position at which the next change of the
3766 invisible text property can be found in IT->string.
3767 Value will be nil if the property value is the same for
3768 all the rest of IT->string. */
3769 XSETINT (limit, SCHARS (it->string));
3770 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3771 it->string, limit);
3773 /* Text at current position is invisible. The next
3774 change in the property is at position end_charpos.
3775 Move IT's current position to that position. */
3776 if (INTEGERP (end_charpos)
3777 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
3779 struct text_pos old;
3780 EMACS_INT oldpos;
3782 old = it->current.string_pos;
3783 oldpos = CHARPOS (old);
3784 if (it->bidi_p)
3786 if (it->bidi_it.first_elt
3787 && it->bidi_it.charpos < SCHARS (it->string))
3788 bidi_paragraph_init (it->paragraph_embedding,
3789 &it->bidi_it, 1);
3790 /* Bidi-iterate out of the invisible text. */
3793 bidi_move_to_visually_next (&it->bidi_it);
3795 while (oldpos <= it->bidi_it.charpos
3796 && it->bidi_it.charpos < endpos);
3798 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
3799 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
3800 if (IT_CHARPOS (*it) >= endpos)
3801 it->prev_stop = endpos;
3803 else
3805 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3806 compute_string_pos (&it->current.string_pos, old, it->string);
3809 else
3811 /* The rest of the string is invisible. If this is an
3812 overlay string, proceed with the next overlay string
3813 or whatever comes and return a character from there. */
3814 if (it->current.overlay_string_index >= 0)
3816 next_overlay_string (it);
3817 /* Don't check for overlay strings when we just
3818 finished processing them. */
3819 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3821 else
3823 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3824 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3829 else
3831 int invis_p;
3832 EMACS_INT newpos, next_stop, start_charpos, tem;
3833 Lisp_Object pos, prop, overlay;
3835 /* First of all, is there invisible text at this position? */
3836 tem = start_charpos = IT_CHARPOS (*it);
3837 pos = make_number (tem);
3838 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3839 &overlay);
3840 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3842 /* If we are on invisible text, skip over it. */
3843 if (invis_p && start_charpos < it->end_charpos)
3845 /* Record whether we have to display an ellipsis for the
3846 invisible text. */
3847 int display_ellipsis_p = invis_p == 2;
3849 handled = HANDLED_RECOMPUTE_PROPS;
3851 /* Loop skipping over invisible text. The loop is left at
3852 ZV or with IT on the first char being visible again. */
3855 /* Try to skip some invisible text. Return value is the
3856 position reached which can be equal to where we start
3857 if there is nothing invisible there. This skips both
3858 over invisible text properties and overlays with
3859 invisible property. */
3860 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3862 /* If we skipped nothing at all we weren't at invisible
3863 text in the first place. If everything to the end of
3864 the buffer was skipped, end the loop. */
3865 if (newpos == tem || newpos >= ZV)
3866 invis_p = 0;
3867 else
3869 /* We skipped some characters but not necessarily
3870 all there are. Check if we ended up on visible
3871 text. Fget_char_property returns the property of
3872 the char before the given position, i.e. if we
3873 get invis_p = 0, this means that the char at
3874 newpos is visible. */
3875 pos = make_number (newpos);
3876 prop = Fget_char_property (pos, Qinvisible, it->window);
3877 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3880 /* If we ended up on invisible text, proceed to
3881 skip starting with next_stop. */
3882 if (invis_p)
3883 tem = next_stop;
3885 /* If there are adjacent invisible texts, don't lose the
3886 second one's ellipsis. */
3887 if (invis_p == 2)
3888 display_ellipsis_p = 1;
3890 while (invis_p);
3892 /* The position newpos is now either ZV or on visible text. */
3893 if (it->bidi_p && newpos < ZV)
3895 /* With bidi iteration, the region of invisible text
3896 could start and/or end in the middle of a non-base
3897 embedding level. Therefore, we need to skip
3898 invisible text using the bidi iterator, starting at
3899 IT's current position, until we find ourselves
3900 outside the invisible text. Skipping invisible text
3901 _after_ bidi iteration avoids affecting the visual
3902 order of the displayed text when invisible properties
3903 are added or removed. */
3904 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
3906 /* If we were `reseat'ed to a new paragraph,
3907 determine the paragraph base direction. We need
3908 to do it now because next_element_from_buffer may
3909 not have a chance to do it, if we are going to
3910 skip any text at the beginning, which resets the
3911 FIRST_ELT flag. */
3912 bidi_paragraph_init (it->paragraph_embedding,
3913 &it->bidi_it, 1);
3917 bidi_move_to_visually_next (&it->bidi_it);
3919 while (it->stop_charpos <= it->bidi_it.charpos
3920 && it->bidi_it.charpos < newpos);
3921 IT_CHARPOS (*it) = it->bidi_it.charpos;
3922 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3923 /* If we overstepped NEWPOS, record its position in the
3924 iterator, so that we skip invisible text if later the
3925 bidi iteration lands us in the invisible region
3926 again. */
3927 if (IT_CHARPOS (*it) >= newpos)
3928 it->prev_stop = newpos;
3930 else
3932 IT_CHARPOS (*it) = newpos;
3933 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3936 /* If there are before-strings at the start of invisible
3937 text, and the text is invisible because of a text
3938 property, arrange to show before-strings because 20.x did
3939 it that way. (If the text is invisible because of an
3940 overlay property instead of a text property, this is
3941 already handled in the overlay code.) */
3942 if (NILP (overlay)
3943 && get_overlay_strings (it, it->stop_charpos))
3945 handled = HANDLED_RECOMPUTE_PROPS;
3946 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3948 else if (display_ellipsis_p)
3950 /* Make sure that the glyphs of the ellipsis will get
3951 correct `charpos' values. If we would not update
3952 it->position here, the glyphs would belong to the
3953 last visible character _before_ the invisible
3954 text, which confuses `set_cursor_from_row'.
3956 We use the last invisible position instead of the
3957 first because this way the cursor is always drawn on
3958 the first "." of the ellipsis, whenever PT is inside
3959 the invisible text. Otherwise the cursor would be
3960 placed _after_ the ellipsis when the point is after the
3961 first invisible character. */
3962 if (!STRINGP (it->object))
3964 it->position.charpos = newpos - 1;
3965 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3967 it->ellipsis_p = 1;
3968 /* Let the ellipsis display before
3969 considering any properties of the following char.
3970 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3971 handled = HANDLED_RETURN;
3976 return handled;
3980 /* Make iterator IT return `...' next.
3981 Replaces LEN characters from buffer. */
3983 static void
3984 setup_for_ellipsis (struct it *it, int len)
3986 /* Use the display table definition for `...'. Invalid glyphs
3987 will be handled by the method returning elements from dpvec. */
3988 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3990 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3991 it->dpvec = v->contents;
3992 it->dpend = v->contents + v->header.size;
3994 else
3996 /* Default `...'. */
3997 it->dpvec = default_invis_vector;
3998 it->dpend = default_invis_vector + 3;
4001 it->dpvec_char_len = len;
4002 it->current.dpvec_index = 0;
4003 it->dpvec_face_id = -1;
4005 /* Remember the current face id in case glyphs specify faces.
4006 IT's face is restored in set_iterator_to_next.
4007 saved_face_id was set to preceding char's face in handle_stop. */
4008 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4009 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4011 it->method = GET_FROM_DISPLAY_VECTOR;
4012 it->ellipsis_p = 1;
4017 /***********************************************************************
4018 'display' property
4019 ***********************************************************************/
4021 /* Set up iterator IT from `display' property at its current position.
4022 Called from handle_stop.
4023 We return HANDLED_RETURN if some part of the display property
4024 overrides the display of the buffer text itself.
4025 Otherwise we return HANDLED_NORMALLY. */
4027 static enum prop_handled
4028 handle_display_prop (struct it *it)
4030 Lisp_Object propval, object, overlay;
4031 struct text_pos *position;
4032 EMACS_INT bufpos;
4033 /* Nonzero if some property replaces the display of the text itself. */
4034 int display_replaced_p = 0;
4036 if (STRINGP (it->string))
4038 object = it->string;
4039 position = &it->current.string_pos;
4040 bufpos = CHARPOS (it->current.pos);
4042 else
4044 XSETWINDOW (object, it->w);
4045 position = &it->current.pos;
4046 bufpos = CHARPOS (*position);
4049 /* Reset those iterator values set from display property values. */
4050 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4051 it->space_width = Qnil;
4052 it->font_height = Qnil;
4053 it->voffset = 0;
4055 /* We don't support recursive `display' properties, i.e. string
4056 values that have a string `display' property, that have a string
4057 `display' property etc. */
4058 if (!it->string_from_display_prop_p)
4059 it->area = TEXT_AREA;
4061 propval = get_char_property_and_overlay (make_number (position->charpos),
4062 Qdisplay, object, &overlay);
4063 if (NILP (propval))
4064 return HANDLED_NORMALLY;
4065 /* Now OVERLAY is the overlay that gave us this property, or nil
4066 if it was a text property. */
4068 if (!STRINGP (it->string))
4069 object = it->w->buffer;
4071 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4072 position, bufpos,
4073 FRAME_WINDOW_P (it->f));
4075 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4078 /* Subroutine of handle_display_prop. Returns non-zero if the display
4079 specification in SPEC is a replacing specification, i.e. it would
4080 replace the text covered by `display' property with something else,
4081 such as an image or a display string.
4083 See handle_single_display_spec for documentation of arguments.
4084 frame_window_p is non-zero if the window being redisplayed is on a
4085 GUI frame; this argument is used only if IT is NULL, see below.
4087 IT can be NULL, if this is called by the bidi reordering code
4088 through compute_display_string_pos, which see. In that case, this
4089 function only examines SPEC, but does not otherwise "handle" it, in
4090 the sense that it doesn't set up members of IT from the display
4091 spec. */
4092 static int
4093 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4094 Lisp_Object overlay, struct text_pos *position,
4095 EMACS_INT bufpos, int frame_window_p)
4097 int replacing_p = 0;
4099 if (CONSP (spec)
4100 /* Simple specerties. */
4101 && !EQ (XCAR (spec), Qimage)
4102 && !EQ (XCAR (spec), Qspace)
4103 && !EQ (XCAR (spec), Qwhen)
4104 && !EQ (XCAR (spec), Qslice)
4105 && !EQ (XCAR (spec), Qspace_width)
4106 && !EQ (XCAR (spec), Qheight)
4107 && !EQ (XCAR (spec), Qraise)
4108 /* Marginal area specifications. */
4109 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4110 && !EQ (XCAR (spec), Qleft_fringe)
4111 && !EQ (XCAR (spec), Qright_fringe)
4112 && !NILP (XCAR (spec)))
4114 for (; CONSP (spec); spec = XCDR (spec))
4116 if (handle_single_display_spec (it, XCAR (spec), object, overlay,
4117 position, bufpos, replacing_p,
4118 frame_window_p))
4120 replacing_p = 1;
4121 /* If some text in a string is replaced, `position' no
4122 longer points to the position of `object'. */
4123 if (!it || STRINGP (object))
4124 break;
4128 else if (VECTORP (spec))
4130 int i;
4131 for (i = 0; i < ASIZE (spec); ++i)
4132 if (handle_single_display_spec (it, AREF (spec, i), object, overlay,
4133 position, bufpos, replacing_p,
4134 frame_window_p))
4136 replacing_p = 1;
4137 /* If some text in a string is replaced, `position' no
4138 longer points to the position of `object'. */
4139 if (!it || STRINGP (object))
4140 break;
4143 else
4145 if (handle_single_display_spec (it, spec, object, overlay,
4146 position, bufpos, 0, frame_window_p))
4147 replacing_p = 1;
4150 return replacing_p;
4153 /* Value is the position of the end of the `display' property starting
4154 at START_POS in OBJECT. */
4156 static struct text_pos
4157 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4159 Lisp_Object end;
4160 struct text_pos end_pos;
4162 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4163 Qdisplay, object, Qnil);
4164 CHARPOS (end_pos) = XFASTINT (end);
4165 if (STRINGP (object))
4166 compute_string_pos (&end_pos, start_pos, it->string);
4167 else
4168 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4170 return end_pos;
4174 /* Set up IT from a single `display' property specification SPEC. OBJECT
4175 is the object in which the `display' property was found. *POSITION
4176 is the position in OBJECT at which the `display' property was found.
4177 BUFPOS is the buffer position of OBJECT (different from POSITION if
4178 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4179 previously saw a display specification which already replaced text
4180 display with something else, for example an image; we ignore such
4181 properties after the first one has been processed.
4183 OVERLAY is the overlay this `display' property came from,
4184 or nil if it was a text property.
4186 If SPEC is a `space' or `image' specification, and in some other
4187 cases too, set *POSITION to the position where the `display'
4188 property ends.
4190 If IT is NULL, only examine the property specification in SPEC, but
4191 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4192 is intended to be displayed in a window on a GUI frame.
4194 Value is non-zero if something was found which replaces the display
4195 of buffer or string text. */
4197 static int
4198 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4199 Lisp_Object overlay, struct text_pos *position,
4200 EMACS_INT bufpos, int display_replaced_p,
4201 int frame_window_p)
4203 Lisp_Object form;
4204 Lisp_Object location, value;
4205 struct text_pos start_pos = *position;
4206 int valid_p;
4208 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4209 If the result is non-nil, use VALUE instead of SPEC. */
4210 form = Qt;
4211 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4213 spec = XCDR (spec);
4214 if (!CONSP (spec))
4215 return 0;
4216 form = XCAR (spec);
4217 spec = XCDR (spec);
4220 if (!NILP (form) && !EQ (form, Qt))
4222 int count = SPECPDL_INDEX ();
4223 struct gcpro gcpro1;
4225 /* Bind `object' to the object having the `display' property, a
4226 buffer or string. Bind `position' to the position in the
4227 object where the property was found, and `buffer-position'
4228 to the current position in the buffer. */
4230 if (NILP (object))
4231 XSETBUFFER (object, current_buffer);
4232 specbind (Qobject, object);
4233 specbind (Qposition, make_number (CHARPOS (*position)));
4234 specbind (Qbuffer_position, make_number (bufpos));
4235 GCPRO1 (form);
4236 form = safe_eval (form);
4237 UNGCPRO;
4238 unbind_to (count, Qnil);
4241 if (NILP (form))
4242 return 0;
4244 /* Handle `(height HEIGHT)' specifications. */
4245 if (CONSP (spec)
4246 && EQ (XCAR (spec), Qheight)
4247 && CONSP (XCDR (spec)))
4249 if (it)
4251 if (!FRAME_WINDOW_P (it->f))
4252 return 0;
4254 it->font_height = XCAR (XCDR (spec));
4255 if (!NILP (it->font_height))
4257 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4258 int new_height = -1;
4260 if (CONSP (it->font_height)
4261 && (EQ (XCAR (it->font_height), Qplus)
4262 || EQ (XCAR (it->font_height), Qminus))
4263 && CONSP (XCDR (it->font_height))
4264 && INTEGERP (XCAR (XCDR (it->font_height))))
4266 /* `(+ N)' or `(- N)' where N is an integer. */
4267 int steps = XINT (XCAR (XCDR (it->font_height)));
4268 if (EQ (XCAR (it->font_height), Qplus))
4269 steps = - steps;
4270 it->face_id = smaller_face (it->f, it->face_id, steps);
4272 else if (FUNCTIONP (it->font_height))
4274 /* Call function with current height as argument.
4275 Value is the new height. */
4276 Lisp_Object height;
4277 height = safe_call1 (it->font_height,
4278 face->lface[LFACE_HEIGHT_INDEX]);
4279 if (NUMBERP (height))
4280 new_height = XFLOATINT (height);
4282 else if (NUMBERP (it->font_height))
4284 /* Value is a multiple of the canonical char height. */
4285 struct face *f;
4287 f = FACE_FROM_ID (it->f,
4288 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4289 new_height = (XFLOATINT (it->font_height)
4290 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4292 else
4294 /* Evaluate IT->font_height with `height' bound to the
4295 current specified height to get the new height. */
4296 int count = SPECPDL_INDEX ();
4298 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4299 value = safe_eval (it->font_height);
4300 unbind_to (count, Qnil);
4302 if (NUMBERP (value))
4303 new_height = XFLOATINT (value);
4306 if (new_height > 0)
4307 it->face_id = face_with_height (it->f, it->face_id, new_height);
4311 return 0;
4314 /* Handle `(space-width WIDTH)'. */
4315 if (CONSP (spec)
4316 && EQ (XCAR (spec), Qspace_width)
4317 && CONSP (XCDR (spec)))
4319 if (it)
4321 if (!FRAME_WINDOW_P (it->f))
4322 return 0;
4324 value = XCAR (XCDR (spec));
4325 if (NUMBERP (value) && XFLOATINT (value) > 0)
4326 it->space_width = value;
4329 return 0;
4332 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4333 if (CONSP (spec)
4334 && EQ (XCAR (spec), Qslice))
4336 Lisp_Object tem;
4338 if (it)
4340 if (!FRAME_WINDOW_P (it->f))
4341 return 0;
4343 if (tem = XCDR (spec), CONSP (tem))
4345 it->slice.x = XCAR (tem);
4346 if (tem = XCDR (tem), CONSP (tem))
4348 it->slice.y = XCAR (tem);
4349 if (tem = XCDR (tem), CONSP (tem))
4351 it->slice.width = XCAR (tem);
4352 if (tem = XCDR (tem), CONSP (tem))
4353 it->slice.height = XCAR (tem);
4359 return 0;
4362 /* Handle `(raise FACTOR)'. */
4363 if (CONSP (spec)
4364 && EQ (XCAR (spec), Qraise)
4365 && CONSP (XCDR (spec)))
4367 if (it)
4369 if (!FRAME_WINDOW_P (it->f))
4370 return 0;
4372 #ifdef HAVE_WINDOW_SYSTEM
4373 value = XCAR (XCDR (spec));
4374 if (NUMBERP (value))
4376 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4377 it->voffset = - (XFLOATINT (value)
4378 * (FONT_HEIGHT (face->font)));
4380 #endif /* HAVE_WINDOW_SYSTEM */
4383 return 0;
4386 /* Don't handle the other kinds of display specifications
4387 inside a string that we got from a `display' property. */
4388 if (it && it->string_from_display_prop_p)
4389 return 0;
4391 /* Characters having this form of property are not displayed, so
4392 we have to find the end of the property. */
4393 if (it)
4395 start_pos = *position;
4396 *position = display_prop_end (it, object, start_pos);
4398 value = Qnil;
4400 /* Stop the scan at that end position--we assume that all
4401 text properties change there. */
4402 if (it)
4403 it->stop_charpos = position->charpos;
4405 /* Handle `(left-fringe BITMAP [FACE])'
4406 and `(right-fringe BITMAP [FACE])'. */
4407 if (CONSP (spec)
4408 && (EQ (XCAR (spec), Qleft_fringe)
4409 || EQ (XCAR (spec), Qright_fringe))
4410 && CONSP (XCDR (spec)))
4412 int fringe_bitmap;
4414 if (it)
4416 if (!FRAME_WINDOW_P (it->f))
4417 /* If we return here, POSITION has been advanced
4418 across the text with this property. */
4419 return 0;
4421 else if (!frame_window_p)
4422 return 0;
4424 #ifdef HAVE_WINDOW_SYSTEM
4425 value = XCAR (XCDR (spec));
4426 if (!SYMBOLP (value)
4427 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4428 /* If we return here, POSITION has been advanced
4429 across the text with this property. */
4430 return 0;
4432 if (it)
4434 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4436 if (CONSP (XCDR (XCDR (spec))))
4438 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4439 int face_id2 = lookup_derived_face (it->f, face_name,
4440 FRINGE_FACE_ID, 0);
4441 if (face_id2 >= 0)
4442 face_id = face_id2;
4445 /* Save current settings of IT so that we can restore them
4446 when we are finished with the glyph property value. */
4447 push_it (it, position);
4449 it->area = TEXT_AREA;
4450 it->what = IT_IMAGE;
4451 it->image_id = -1; /* no image */
4452 it->position = start_pos;
4453 it->object = NILP (object) ? it->w->buffer : object;
4454 it->method = GET_FROM_IMAGE;
4455 it->from_overlay = Qnil;
4456 it->face_id = face_id;
4457 it->from_disp_prop_p = 1;
4459 /* Say that we haven't consumed the characters with
4460 `display' property yet. The call to pop_it in
4461 set_iterator_to_next will clean this up. */
4462 *position = start_pos;
4464 if (EQ (XCAR (spec), Qleft_fringe))
4466 it->left_user_fringe_bitmap = fringe_bitmap;
4467 it->left_user_fringe_face_id = face_id;
4469 else
4471 it->right_user_fringe_bitmap = fringe_bitmap;
4472 it->right_user_fringe_face_id = face_id;
4475 #endif /* HAVE_WINDOW_SYSTEM */
4476 return 1;
4479 /* Prepare to handle `((margin left-margin) ...)',
4480 `((margin right-margin) ...)' and `((margin nil) ...)'
4481 prefixes for display specifications. */
4482 location = Qunbound;
4483 if (CONSP (spec) && CONSP (XCAR (spec)))
4485 Lisp_Object tem;
4487 value = XCDR (spec);
4488 if (CONSP (value))
4489 value = XCAR (value);
4491 tem = XCAR (spec);
4492 if (EQ (XCAR (tem), Qmargin)
4493 && (tem = XCDR (tem),
4494 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4495 (NILP (tem)
4496 || EQ (tem, Qleft_margin)
4497 || EQ (tem, Qright_margin))))
4498 location = tem;
4501 if (EQ (location, Qunbound))
4503 location = Qnil;
4504 value = spec;
4507 /* After this point, VALUE is the property after any
4508 margin prefix has been stripped. It must be a string,
4509 an image specification, or `(space ...)'.
4511 LOCATION specifies where to display: `left-margin',
4512 `right-margin' or nil. */
4514 valid_p = (STRINGP (value)
4515 #ifdef HAVE_WINDOW_SYSTEM
4516 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4517 && valid_image_p (value))
4518 #endif /* not HAVE_WINDOW_SYSTEM */
4519 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4521 if (valid_p && !display_replaced_p)
4523 if (!it)
4524 return 1;
4526 /* Save current settings of IT so that we can restore them
4527 when we are finished with the glyph property value. */
4528 push_it (it, position);
4529 it->from_overlay = overlay;
4530 it->from_disp_prop_p = 1;
4532 if (NILP (location))
4533 it->area = TEXT_AREA;
4534 else if (EQ (location, Qleft_margin))
4535 it->area = LEFT_MARGIN_AREA;
4536 else
4537 it->area = RIGHT_MARGIN_AREA;
4539 if (STRINGP (value))
4541 it->string = value;
4542 it->multibyte_p = STRING_MULTIBYTE (it->string);
4543 it->current.overlay_string_index = -1;
4544 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4545 it->end_charpos = it->string_nchars = SCHARS (it->string);
4546 it->method = GET_FROM_STRING;
4547 it->stop_charpos = 0;
4548 it->prev_stop = 0;
4549 it->base_level_stop = 0;
4550 it->string_from_display_prop_p = 1;
4551 /* Say that we haven't consumed the characters with
4552 `display' property yet. The call to pop_it in
4553 set_iterator_to_next will clean this up. */
4554 if (BUFFERP (object))
4555 *position = start_pos;
4557 /* Force paragraph direction to be that of the parent
4558 object. If the parent object's paragraph direction is
4559 not yet determined, default to L2R. */
4560 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4561 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4562 else
4563 it->paragraph_embedding = L2R;
4565 /* Set up the bidi iterator for this display string. */
4566 if (it->bidi_p)
4568 it->bidi_it.string.lstring = it->string;
4569 it->bidi_it.string.s = NULL;
4570 it->bidi_it.string.schars = it->end_charpos;
4571 it->bidi_it.string.bufpos = bufpos;
4572 it->bidi_it.string.from_disp_str = 1;
4573 it->bidi_it.string.unibyte = !it->multibyte_p;
4574 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4577 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4579 it->method = GET_FROM_STRETCH;
4580 it->object = value;
4581 *position = it->position = start_pos;
4583 #ifdef HAVE_WINDOW_SYSTEM
4584 else
4586 it->what = IT_IMAGE;
4587 it->image_id = lookup_image (it->f, value);
4588 it->position = start_pos;
4589 it->object = NILP (object) ? it->w->buffer : object;
4590 it->method = GET_FROM_IMAGE;
4592 /* Say that we haven't consumed the characters with
4593 `display' property yet. The call to pop_it in
4594 set_iterator_to_next will clean this up. */
4595 *position = start_pos;
4597 #endif /* HAVE_WINDOW_SYSTEM */
4599 return 1;
4602 /* Invalid property or property not supported. Restore
4603 POSITION to what it was before. */
4604 *position = start_pos;
4605 return 0;
4608 /* Check if PROP is a display property value whose text should be
4609 treated as intangible. OVERLAY is the overlay from which PROP
4610 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4611 specify the buffer position covered by PROP. */
4614 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4615 EMACS_INT charpos, EMACS_INT bytepos)
4617 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4618 struct text_pos position;
4620 SET_TEXT_POS (position, charpos, bytepos);
4621 return handle_display_spec (NULL, prop, Qnil, overlay,
4622 &position, charpos, frame_window_p);
4626 /* Return 1 if PROP is a display sub-property value containing STRING.
4628 Implementation note: this and the following function are really
4629 special cases of handle_display_spec and
4630 handle_single_display_spec, and should ideally use the same code.
4631 Until they do, these two pairs must be consistent and must be
4632 modified in sync. */
4634 static int
4635 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4637 if (EQ (string, prop))
4638 return 1;
4640 /* Skip over `when FORM'. */
4641 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4643 prop = XCDR (prop);
4644 if (!CONSP (prop))
4645 return 0;
4646 /* Actually, the condition following `when' should be eval'ed,
4647 like handle_single_display_spec does, and we should return
4648 zero if it evaluates to nil. However, this function is
4649 called only when the buffer was already displayed and some
4650 glyph in the glyph matrix was found to come from a display
4651 string. Therefore, the condition was already evaluated, and
4652 the result was non-nil, otherwise the display string wouldn't
4653 have been displayed and we would have never been called for
4654 this property. Thus, we can skip the evaluation and assume
4655 its result is non-nil. */
4656 prop = XCDR (prop);
4659 if (CONSP (prop))
4660 /* Skip over `margin LOCATION'. */
4661 if (EQ (XCAR (prop), Qmargin))
4663 prop = XCDR (prop);
4664 if (!CONSP (prop))
4665 return 0;
4667 prop = XCDR (prop);
4668 if (!CONSP (prop))
4669 return 0;
4672 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
4676 /* Return 1 if STRING appears in the `display' property PROP. */
4678 static int
4679 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4681 if (CONSP (prop)
4682 && !EQ (XCAR (prop), Qwhen)
4683 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
4685 /* A list of sub-properties. */
4686 while (CONSP (prop))
4688 if (single_display_spec_string_p (XCAR (prop), string))
4689 return 1;
4690 prop = XCDR (prop);
4693 else if (VECTORP (prop))
4695 /* A vector of sub-properties. */
4696 int i;
4697 for (i = 0; i < ASIZE (prop); ++i)
4698 if (single_display_spec_string_p (AREF (prop, i), string))
4699 return 1;
4701 else
4702 return single_display_spec_string_p (prop, string);
4704 return 0;
4707 /* Look for STRING in overlays and text properties in the current
4708 buffer, between character positions FROM and TO (excluding TO).
4709 BACK_P non-zero means look back (in this case, TO is supposed to be
4710 less than FROM).
4711 Value is the first character position where STRING was found, or
4712 zero if it wasn't found before hitting TO.
4714 This function may only use code that doesn't eval because it is
4715 called asynchronously from note_mouse_highlight. */
4717 static EMACS_INT
4718 string_buffer_position_lim (Lisp_Object string,
4719 EMACS_INT from, EMACS_INT to, int back_p)
4721 Lisp_Object limit, prop, pos;
4722 int found = 0;
4724 pos = make_number (from);
4726 if (!back_p) /* looking forward */
4728 limit = make_number (min (to, ZV));
4729 while (!found && !EQ (pos, limit))
4731 prop = Fget_char_property (pos, Qdisplay, Qnil);
4732 if (!NILP (prop) && display_prop_string_p (prop, string))
4733 found = 1;
4734 else
4735 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4736 limit);
4739 else /* looking back */
4741 limit = make_number (max (to, BEGV));
4742 while (!found && !EQ (pos, limit))
4744 prop = Fget_char_property (pos, Qdisplay, Qnil);
4745 if (!NILP (prop) && display_prop_string_p (prop, string))
4746 found = 1;
4747 else
4748 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4749 limit);
4753 return found ? XINT (pos) : 0;
4756 /* Determine which buffer position in current buffer STRING comes from.
4757 AROUND_CHARPOS is an approximate position where it could come from.
4758 Value is the buffer position or 0 if it couldn't be determined.
4760 This function is necessary because we don't record buffer positions
4761 in glyphs generated from strings (to keep struct glyph small).
4762 This function may only use code that doesn't eval because it is
4763 called asynchronously from note_mouse_highlight. */
4765 static EMACS_INT
4766 string_buffer_position (Lisp_Object string, EMACS_INT around_charpos)
4768 const int MAX_DISTANCE = 1000;
4769 EMACS_INT found = string_buffer_position_lim (string, around_charpos,
4770 around_charpos + MAX_DISTANCE,
4773 if (!found)
4774 found = string_buffer_position_lim (string, around_charpos,
4775 around_charpos - MAX_DISTANCE, 1);
4776 return found;
4781 /***********************************************************************
4782 `composition' property
4783 ***********************************************************************/
4785 /* Set up iterator IT from `composition' property at its current
4786 position. Called from handle_stop. */
4788 static enum prop_handled
4789 handle_composition_prop (struct it *it)
4791 Lisp_Object prop, string;
4792 EMACS_INT pos, pos_byte, start, end;
4794 if (STRINGP (it->string))
4796 unsigned char *s;
4798 pos = IT_STRING_CHARPOS (*it);
4799 pos_byte = IT_STRING_BYTEPOS (*it);
4800 string = it->string;
4801 s = SDATA (string) + pos_byte;
4802 it->c = STRING_CHAR (s);
4804 else
4806 pos = IT_CHARPOS (*it);
4807 pos_byte = IT_BYTEPOS (*it);
4808 string = Qnil;
4809 it->c = FETCH_CHAR (pos_byte);
4812 /* If there's a valid composition and point is not inside of the
4813 composition (in the case that the composition is from the current
4814 buffer), draw a glyph composed from the composition components. */
4815 if (find_composition (pos, -1, &start, &end, &prop, string)
4816 && COMPOSITION_VALID_P (start, end, prop)
4817 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4819 if (start < pos)
4820 /* As we can't handle this situation (perhaps font-lock added
4821 a new composition), we just return here hoping that next
4822 redisplay will detect this composition much earlier. */
4823 return HANDLED_NORMALLY;
4824 if (start != pos)
4826 if (STRINGP (it->string))
4827 pos_byte = string_char_to_byte (it->string, start);
4828 else
4829 pos_byte = CHAR_TO_BYTE (start);
4831 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4832 prop, string);
4834 if (it->cmp_it.id >= 0)
4836 it->cmp_it.ch = -1;
4837 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4838 it->cmp_it.nglyphs = -1;
4842 return HANDLED_NORMALLY;
4847 /***********************************************************************
4848 Overlay strings
4849 ***********************************************************************/
4851 /* The following structure is used to record overlay strings for
4852 later sorting in load_overlay_strings. */
4854 struct overlay_entry
4856 Lisp_Object overlay;
4857 Lisp_Object string;
4858 int priority;
4859 int after_string_p;
4863 /* Set up iterator IT from overlay strings at its current position.
4864 Called from handle_stop. */
4866 static enum prop_handled
4867 handle_overlay_change (struct it *it)
4869 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4870 return HANDLED_RECOMPUTE_PROPS;
4871 else
4872 return HANDLED_NORMALLY;
4876 /* Set up the next overlay string for delivery by IT, if there is an
4877 overlay string to deliver. Called by set_iterator_to_next when the
4878 end of the current overlay string is reached. If there are more
4879 overlay strings to display, IT->string and
4880 IT->current.overlay_string_index are set appropriately here.
4881 Otherwise IT->string is set to nil. */
4883 static void
4884 next_overlay_string (struct it *it)
4886 ++it->current.overlay_string_index;
4887 if (it->current.overlay_string_index == it->n_overlay_strings)
4889 /* No more overlay strings. Restore IT's settings to what
4890 they were before overlay strings were processed, and
4891 continue to deliver from current_buffer. */
4893 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4894 pop_it (it);
4895 xassert (it->sp > 0
4896 || (NILP (it->string)
4897 && it->method == GET_FROM_BUFFER
4898 && it->stop_charpos >= BEGV
4899 && it->stop_charpos <= it->end_charpos));
4900 it->current.overlay_string_index = -1;
4901 it->n_overlay_strings = 0;
4902 it->overlay_strings_charpos = -1;
4904 /* If we're at the end of the buffer, record that we have
4905 processed the overlay strings there already, so that
4906 next_element_from_buffer doesn't try it again. */
4907 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4908 it->overlay_strings_at_end_processed_p = 1;
4910 else
4912 /* There are more overlay strings to process. If
4913 IT->current.overlay_string_index has advanced to a position
4914 where we must load IT->overlay_strings with more strings, do
4915 it. We must load at the IT->overlay_strings_charpos where
4916 IT->n_overlay_strings was originally computed; when invisible
4917 text is present, this might not be IT_CHARPOS (Bug#7016). */
4918 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4920 if (it->current.overlay_string_index && i == 0)
4921 load_overlay_strings (it, it->overlay_strings_charpos);
4923 /* Initialize IT to deliver display elements from the overlay
4924 string. */
4925 it->string = it->overlay_strings[i];
4926 it->multibyte_p = STRING_MULTIBYTE (it->string);
4927 SET_TEXT_POS (it->current.string_pos, 0, 0);
4928 it->method = GET_FROM_STRING;
4929 it->stop_charpos = 0;
4930 if (it->cmp_it.stop_pos >= 0)
4931 it->cmp_it.stop_pos = 0;
4932 it->prev_stop = 0;
4933 it->base_level_stop = 0;
4935 /* Set up the bidi iterator for this overlay string. */
4936 if (it->bidi_p)
4938 it->bidi_it.string.lstring = it->string;
4939 it->bidi_it.string.s = NULL;
4940 it->bidi_it.string.schars = SCHARS (it->string);
4941 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
4942 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
4943 it->bidi_it.string.unibyte = !it->multibyte_p;
4944 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4948 CHECK_IT (it);
4952 /* Compare two overlay_entry structures E1 and E2. Used as a
4953 comparison function for qsort in load_overlay_strings. Overlay
4954 strings for the same position are sorted so that
4956 1. All after-strings come in front of before-strings, except
4957 when they come from the same overlay.
4959 2. Within after-strings, strings are sorted so that overlay strings
4960 from overlays with higher priorities come first.
4962 2. Within before-strings, strings are sorted so that overlay
4963 strings from overlays with higher priorities come last.
4965 Value is analogous to strcmp. */
4968 static int
4969 compare_overlay_entries (const void *e1, const void *e2)
4971 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4972 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4973 int result;
4975 if (entry1->after_string_p != entry2->after_string_p)
4977 /* Let after-strings appear in front of before-strings if
4978 they come from different overlays. */
4979 if (EQ (entry1->overlay, entry2->overlay))
4980 result = entry1->after_string_p ? 1 : -1;
4981 else
4982 result = entry1->after_string_p ? -1 : 1;
4984 else if (entry1->after_string_p)
4985 /* After-strings sorted in order of decreasing priority. */
4986 result = entry2->priority - entry1->priority;
4987 else
4988 /* Before-strings sorted in order of increasing priority. */
4989 result = entry1->priority - entry2->priority;
4991 return result;
4995 /* Load the vector IT->overlay_strings with overlay strings from IT's
4996 current buffer position, or from CHARPOS if that is > 0. Set
4997 IT->n_overlays to the total number of overlay strings found.
4999 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5000 a time. On entry into load_overlay_strings,
5001 IT->current.overlay_string_index gives the number of overlay
5002 strings that have already been loaded by previous calls to this
5003 function.
5005 IT->add_overlay_start contains an additional overlay start
5006 position to consider for taking overlay strings from, if non-zero.
5007 This position comes into play when the overlay has an `invisible'
5008 property, and both before and after-strings. When we've skipped to
5009 the end of the overlay, because of its `invisible' property, we
5010 nevertheless want its before-string to appear.
5011 IT->add_overlay_start will contain the overlay start position
5012 in this case.
5014 Overlay strings are sorted so that after-string strings come in
5015 front of before-string strings. Within before and after-strings,
5016 strings are sorted by overlay priority. See also function
5017 compare_overlay_entries. */
5019 static void
5020 load_overlay_strings (struct it *it, EMACS_INT charpos)
5022 Lisp_Object overlay, window, str, invisible;
5023 struct Lisp_Overlay *ov;
5024 EMACS_INT start, end;
5025 int size = 20;
5026 int n = 0, i, j, invis_p;
5027 struct overlay_entry *entries
5028 = (struct overlay_entry *) alloca (size * sizeof *entries);
5030 if (charpos <= 0)
5031 charpos = IT_CHARPOS (*it);
5033 /* Append the overlay string STRING of overlay OVERLAY to vector
5034 `entries' which has size `size' and currently contains `n'
5035 elements. AFTER_P non-zero means STRING is an after-string of
5036 OVERLAY. */
5037 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5038 do \
5040 Lisp_Object priority; \
5042 if (n == size) \
5044 int new_size = 2 * size; \
5045 struct overlay_entry *old = entries; \
5046 entries = \
5047 (struct overlay_entry *) alloca (new_size \
5048 * sizeof *entries); \
5049 memcpy (entries, old, size * sizeof *entries); \
5050 size = new_size; \
5053 entries[n].string = (STRING); \
5054 entries[n].overlay = (OVERLAY); \
5055 priority = Foverlay_get ((OVERLAY), Qpriority); \
5056 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5057 entries[n].after_string_p = (AFTER_P); \
5058 ++n; \
5060 while (0)
5062 /* Process overlay before the overlay center. */
5063 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5065 XSETMISC (overlay, ov);
5066 xassert (OVERLAYP (overlay));
5067 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5068 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5070 if (end < charpos)
5071 break;
5073 /* Skip this overlay if it doesn't start or end at IT's current
5074 position. */
5075 if (end != charpos && start != charpos)
5076 continue;
5078 /* Skip this overlay if it doesn't apply to IT->w. */
5079 window = Foverlay_get (overlay, Qwindow);
5080 if (WINDOWP (window) && XWINDOW (window) != it->w)
5081 continue;
5083 /* If the text ``under'' the overlay is invisible, both before-
5084 and after-strings from this overlay are visible; start and
5085 end position are indistinguishable. */
5086 invisible = Foverlay_get (overlay, Qinvisible);
5087 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5089 /* If overlay has a non-empty before-string, record it. */
5090 if ((start == charpos || (end == charpos && invis_p))
5091 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5092 && SCHARS (str))
5093 RECORD_OVERLAY_STRING (overlay, str, 0);
5095 /* If overlay has a non-empty after-string, record it. */
5096 if ((end == charpos || (start == charpos && invis_p))
5097 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5098 && SCHARS (str))
5099 RECORD_OVERLAY_STRING (overlay, str, 1);
5102 /* Process overlays after the overlay center. */
5103 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5105 XSETMISC (overlay, ov);
5106 xassert (OVERLAYP (overlay));
5107 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5108 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5110 if (start > charpos)
5111 break;
5113 /* Skip this overlay if it doesn't start or end at IT's current
5114 position. */
5115 if (end != charpos && start != charpos)
5116 continue;
5118 /* Skip this overlay if it doesn't apply to IT->w. */
5119 window = Foverlay_get (overlay, Qwindow);
5120 if (WINDOWP (window) && XWINDOW (window) != it->w)
5121 continue;
5123 /* If the text ``under'' the overlay is invisible, it has a zero
5124 dimension, and both before- and after-strings apply. */
5125 invisible = Foverlay_get (overlay, Qinvisible);
5126 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5128 /* If overlay has a non-empty before-string, record it. */
5129 if ((start == charpos || (end == charpos && invis_p))
5130 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5131 && SCHARS (str))
5132 RECORD_OVERLAY_STRING (overlay, str, 0);
5134 /* If overlay has a non-empty after-string, record it. */
5135 if ((end == charpos || (start == charpos && invis_p))
5136 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5137 && SCHARS (str))
5138 RECORD_OVERLAY_STRING (overlay, str, 1);
5141 #undef RECORD_OVERLAY_STRING
5143 /* Sort entries. */
5144 if (n > 1)
5145 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5147 /* Record number of overlay strings, and where we computed it. */
5148 it->n_overlay_strings = n;
5149 it->overlay_strings_charpos = charpos;
5151 /* IT->current.overlay_string_index is the number of overlay strings
5152 that have already been consumed by IT. Copy some of the
5153 remaining overlay strings to IT->overlay_strings. */
5154 i = 0;
5155 j = it->current.overlay_string_index;
5156 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5158 it->overlay_strings[i] = entries[j].string;
5159 it->string_overlays[i++] = entries[j++].overlay;
5162 CHECK_IT (it);
5166 /* Get the first chunk of overlay strings at IT's current buffer
5167 position, or at CHARPOS if that is > 0. Value is non-zero if at
5168 least one overlay string was found. */
5170 static int
5171 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
5173 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5174 process. This fills IT->overlay_strings with strings, and sets
5175 IT->n_overlay_strings to the total number of strings to process.
5176 IT->pos.overlay_string_index has to be set temporarily to zero
5177 because load_overlay_strings needs this; it must be set to -1
5178 when no overlay strings are found because a zero value would
5179 indicate a position in the first overlay string. */
5180 it->current.overlay_string_index = 0;
5181 load_overlay_strings (it, charpos);
5183 /* If we found overlay strings, set up IT to deliver display
5184 elements from the first one. Otherwise set up IT to deliver
5185 from current_buffer. */
5186 if (it->n_overlay_strings)
5188 /* Make sure we know settings in current_buffer, so that we can
5189 restore meaningful values when we're done with the overlay
5190 strings. */
5191 if (compute_stop_p)
5192 compute_stop_pos (it);
5193 xassert (it->face_id >= 0);
5195 /* Save IT's settings. They are restored after all overlay
5196 strings have been processed. */
5197 xassert (!compute_stop_p || it->sp == 0);
5199 /* When called from handle_stop, there might be an empty display
5200 string loaded. In that case, don't bother saving it. */
5201 if (!STRINGP (it->string) || SCHARS (it->string))
5202 push_it (it, NULL);
5204 /* Set up IT to deliver display elements from the first overlay
5205 string. */
5206 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5207 it->string = it->overlay_strings[0];
5208 it->from_overlay = Qnil;
5209 it->stop_charpos = 0;
5210 xassert (STRINGP (it->string));
5211 it->end_charpos = SCHARS (it->string);
5212 it->prev_stop = 0;
5213 it->base_level_stop = 0;
5214 it->multibyte_p = STRING_MULTIBYTE (it->string);
5215 it->method = GET_FROM_STRING;
5216 it->from_disp_prop_p = 0;
5218 /* Force paragraph direction to be that of the parent
5219 buffer. */
5220 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5221 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5222 else
5223 it->paragraph_embedding = L2R;
5225 /* Set up the bidi iterator for this overlay string. */
5226 if (it->bidi_p)
5228 EMACS_INT pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5230 it->bidi_it.string.lstring = it->string;
5231 it->bidi_it.string.s = NULL;
5232 it->bidi_it.string.schars = SCHARS (it->string);
5233 it->bidi_it.string.bufpos = pos;
5234 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5235 it->bidi_it.string.unibyte = !it->multibyte_p;
5236 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5238 return 1;
5241 it->current.overlay_string_index = -1;
5242 return 0;
5245 static int
5246 get_overlay_strings (struct it *it, EMACS_INT charpos)
5248 it->string = Qnil;
5249 it->method = GET_FROM_BUFFER;
5251 (void) get_overlay_strings_1 (it, charpos, 1);
5253 CHECK_IT (it);
5255 /* Value is non-zero if we found at least one overlay string. */
5256 return STRINGP (it->string);
5261 /***********************************************************************
5262 Saving and restoring state
5263 ***********************************************************************/
5265 /* Save current settings of IT on IT->stack. Called, for example,
5266 before setting up IT for an overlay string, to be able to restore
5267 IT's settings to what they were after the overlay string has been
5268 processed. If POSITION is non-NULL, it is the position to save on
5269 the stack instead of IT->position. */
5271 static void
5272 push_it (struct it *it, struct text_pos *position)
5274 struct iterator_stack_entry *p;
5276 xassert (it->sp < IT_STACK_SIZE);
5277 p = it->stack + it->sp;
5279 p->stop_charpos = it->stop_charpos;
5280 p->prev_stop = it->prev_stop;
5281 p->base_level_stop = it->base_level_stop;
5282 p->cmp_it = it->cmp_it;
5283 xassert (it->face_id >= 0);
5284 p->face_id = it->face_id;
5285 p->string = it->string;
5286 p->method = it->method;
5287 p->from_overlay = it->from_overlay;
5288 switch (p->method)
5290 case GET_FROM_IMAGE:
5291 p->u.image.object = it->object;
5292 p->u.image.image_id = it->image_id;
5293 p->u.image.slice = it->slice;
5294 break;
5295 case GET_FROM_STRETCH:
5296 p->u.stretch.object = it->object;
5297 break;
5299 p->position = position ? *position : it->position;
5300 p->current = it->current;
5301 p->end_charpos = it->end_charpos;
5302 p->string_nchars = it->string_nchars;
5303 p->area = it->area;
5304 p->multibyte_p = it->multibyte_p;
5305 p->avoid_cursor_p = it->avoid_cursor_p;
5306 p->space_width = it->space_width;
5307 p->font_height = it->font_height;
5308 p->voffset = it->voffset;
5309 p->string_from_display_prop_p = it->string_from_display_prop_p;
5310 p->display_ellipsis_p = 0;
5311 p->line_wrap = it->line_wrap;
5312 p->bidi_p = it->bidi_p;
5313 p->paragraph_embedding = it->paragraph_embedding;
5314 p->from_disp_prop_p = it->from_disp_prop_p;
5315 ++it->sp;
5317 /* Save the state of the bidi iterator as well. */
5318 if (it->bidi_p)
5319 bidi_push_it (&it->bidi_it);
5322 static void
5323 iterate_out_of_display_property (struct it *it)
5325 int buffer_p = BUFFERP (it->object);
5326 EMACS_INT eob = (buffer_p ? ZV : it->end_charpos);
5327 EMACS_INT bob = (buffer_p ? BEGV : 0);
5329 xassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5331 /* Maybe initialize paragraph direction. If we are at the beginning
5332 of a new paragraph, next_element_from_buffer may not have a
5333 chance to do that. */
5334 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5335 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5336 /* prev_stop can be zero, so check against BEGV as well. */
5337 while (it->bidi_it.charpos >= bob
5338 && it->prev_stop <= it->bidi_it.charpos
5339 && it->bidi_it.charpos < CHARPOS (it->position)
5340 && it->bidi_it.charpos < eob)
5341 bidi_move_to_visually_next (&it->bidi_it);
5342 /* Record the stop_pos we just crossed, for when we cross it
5343 back, maybe. */
5344 if (it->bidi_it.charpos > CHARPOS (it->position))
5345 it->prev_stop = CHARPOS (it->position);
5346 /* If we ended up not where pop_it put us, resync IT's
5347 positional members with the bidi iterator. */
5348 if (it->bidi_it.charpos != CHARPOS (it->position))
5349 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5350 if (buffer_p)
5351 it->current.pos = it->position;
5352 else
5353 it->current.string_pos = it->position;
5356 /* Restore IT's settings from IT->stack. Called, for example, when no
5357 more overlay strings must be processed, and we return to delivering
5358 display elements from a buffer, or when the end of a string from a
5359 `display' property is reached and we return to delivering display
5360 elements from an overlay string, or from a buffer. */
5362 static void
5363 pop_it (struct it *it)
5365 struct iterator_stack_entry *p;
5366 int from_display_prop = it->from_disp_prop_p;
5368 xassert (it->sp > 0);
5369 --it->sp;
5370 p = it->stack + it->sp;
5371 it->stop_charpos = p->stop_charpos;
5372 it->prev_stop = p->prev_stop;
5373 it->base_level_stop = p->base_level_stop;
5374 it->cmp_it = p->cmp_it;
5375 it->face_id = p->face_id;
5376 it->current = p->current;
5377 it->position = p->position;
5378 it->string = p->string;
5379 it->from_overlay = p->from_overlay;
5380 if (NILP (it->string))
5381 SET_TEXT_POS (it->current.string_pos, -1, -1);
5382 it->method = p->method;
5383 switch (it->method)
5385 case GET_FROM_IMAGE:
5386 it->image_id = p->u.image.image_id;
5387 it->object = p->u.image.object;
5388 it->slice = p->u.image.slice;
5389 break;
5390 case GET_FROM_STRETCH:
5391 it->object = p->u.stretch.object;
5392 break;
5393 case GET_FROM_BUFFER:
5394 it->object = it->w->buffer;
5395 break;
5396 case GET_FROM_STRING:
5397 it->object = it->string;
5398 break;
5399 case GET_FROM_DISPLAY_VECTOR:
5400 if (it->s)
5401 it->method = GET_FROM_C_STRING;
5402 else if (STRINGP (it->string))
5403 it->method = GET_FROM_STRING;
5404 else
5406 it->method = GET_FROM_BUFFER;
5407 it->object = it->w->buffer;
5410 it->end_charpos = p->end_charpos;
5411 it->string_nchars = p->string_nchars;
5412 it->area = p->area;
5413 it->multibyte_p = p->multibyte_p;
5414 it->avoid_cursor_p = p->avoid_cursor_p;
5415 it->space_width = p->space_width;
5416 it->font_height = p->font_height;
5417 it->voffset = p->voffset;
5418 it->string_from_display_prop_p = p->string_from_display_prop_p;
5419 it->line_wrap = p->line_wrap;
5420 it->bidi_p = p->bidi_p;
5421 it->paragraph_embedding = p->paragraph_embedding;
5422 it->from_disp_prop_p = p->from_disp_prop_p;
5423 if (it->bidi_p)
5425 bidi_pop_it (&it->bidi_it);
5426 /* Bidi-iterate until we get out of the portion of text, if any,
5427 covered by a `display' text property or by an overlay with
5428 `display' property. (We cannot just jump there, because the
5429 internal coherency of the bidi iterator state can not be
5430 preserved across such jumps.) We also must determine the
5431 paragraph base direction if the overlay we just processed is
5432 at the beginning of a new paragraph. */
5433 if (from_display_prop
5434 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5435 iterate_out_of_display_property (it);
5437 xassert ((BUFFERP (it->object)
5438 && IT_CHARPOS (*it) == it->bidi_it.charpos
5439 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5440 || (STRINGP (it->object)
5441 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5442 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos));
5448 /***********************************************************************
5449 Moving over lines
5450 ***********************************************************************/
5452 /* Set IT's current position to the previous line start. */
5454 static void
5455 back_to_previous_line_start (struct it *it)
5457 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5458 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5462 /* Move IT to the next line start.
5464 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5465 we skipped over part of the text (as opposed to moving the iterator
5466 continuously over the text). Otherwise, don't change the value
5467 of *SKIPPED_P.
5469 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5470 iterator on the newline, if it was found.
5472 Newlines may come from buffer text, overlay strings, or strings
5473 displayed via the `display' property. That's the reason we can't
5474 simply use find_next_newline_no_quit.
5476 Note that this function may not skip over invisible text that is so
5477 because of text properties and immediately follows a newline. If
5478 it would, function reseat_at_next_visible_line_start, when called
5479 from set_iterator_to_next, would effectively make invisible
5480 characters following a newline part of the wrong glyph row, which
5481 leads to wrong cursor motion. */
5483 static int
5484 forward_to_next_line_start (struct it *it, int *skipped_p,
5485 struct bidi_it *bidi_it_prev)
5487 EMACS_INT old_selective;
5488 int newline_found_p, n;
5489 const int MAX_NEWLINE_DISTANCE = 500;
5491 /* If already on a newline, just consume it to avoid unintended
5492 skipping over invisible text below. */
5493 if (it->what == IT_CHARACTER
5494 && it->c == '\n'
5495 && CHARPOS (it->position) == IT_CHARPOS (*it))
5497 if (it->bidi_p && bidi_it_prev)
5498 *bidi_it_prev = it->bidi_it;
5499 set_iterator_to_next (it, 0);
5500 it->c = 0;
5501 return 1;
5504 /* Don't handle selective display in the following. It's (a)
5505 unnecessary because it's done by the caller, and (b) leads to an
5506 infinite recursion because next_element_from_ellipsis indirectly
5507 calls this function. */
5508 old_selective = it->selective;
5509 it->selective = 0;
5511 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5512 from buffer text. */
5513 for (n = newline_found_p = 0;
5514 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5515 n += STRINGP (it->string) ? 0 : 1)
5517 if (!get_next_display_element (it))
5518 return 0;
5519 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5520 if (newline_found_p && it->bidi_p && bidi_it_prev)
5521 *bidi_it_prev = it->bidi_it;
5522 set_iterator_to_next (it, 0);
5525 /* If we didn't find a newline near enough, see if we can use a
5526 short-cut. */
5527 if (!newline_found_p)
5529 EMACS_INT start = IT_CHARPOS (*it);
5530 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5531 Lisp_Object pos;
5533 xassert (!STRINGP (it->string));
5535 /* If there isn't any `display' property in sight, and no
5536 overlays, we can just use the position of the newline in
5537 buffer text. */
5538 if (it->stop_charpos >= limit
5539 || ((pos = Fnext_single_property_change (make_number (start),
5540 Qdisplay, Qnil,
5541 make_number (limit)),
5542 NILP (pos))
5543 && next_overlay_change (start) == ZV))
5545 if (!it->bidi_p)
5547 IT_CHARPOS (*it) = limit;
5548 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5550 else
5552 struct bidi_it bprev;
5554 /* Help bidi.c avoid expensive searches for display
5555 properties and overlays, by telling it that there are
5556 none up to `limit'. */
5557 if (it->bidi_it.disp_pos < limit)
5559 it->bidi_it.disp_pos = limit;
5560 it->bidi_it.disp_prop_p = 0;
5562 do {
5563 bprev = it->bidi_it;
5564 bidi_move_to_visually_next (&it->bidi_it);
5565 } while (it->bidi_it.charpos != limit);
5566 IT_CHARPOS (*it) = limit;
5567 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5568 if (bidi_it_prev)
5569 *bidi_it_prev = bprev;
5571 *skipped_p = newline_found_p = 1;
5573 else
5575 while (get_next_display_element (it)
5576 && !newline_found_p)
5578 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5579 if (newline_found_p && it->bidi_p && bidi_it_prev)
5580 *bidi_it_prev = it->bidi_it;
5581 set_iterator_to_next (it, 0);
5586 it->selective = old_selective;
5587 return newline_found_p;
5591 /* Set IT's current position to the previous visible line start. Skip
5592 invisible text that is so either due to text properties or due to
5593 selective display. Caution: this does not change IT->current_x and
5594 IT->hpos. */
5596 static void
5597 back_to_previous_visible_line_start (struct it *it)
5599 while (IT_CHARPOS (*it) > BEGV)
5601 back_to_previous_line_start (it);
5603 if (IT_CHARPOS (*it) <= BEGV)
5604 break;
5606 /* If selective > 0, then lines indented more than its value are
5607 invisible. */
5608 if (it->selective > 0
5609 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5610 it->selective))
5611 continue;
5613 /* Check the newline before point for invisibility. */
5615 Lisp_Object prop;
5616 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5617 Qinvisible, it->window);
5618 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5619 continue;
5622 if (IT_CHARPOS (*it) <= BEGV)
5623 break;
5626 struct it it2;
5627 void *it2data = NULL;
5628 EMACS_INT pos;
5629 EMACS_INT beg, end;
5630 Lisp_Object val, overlay;
5632 SAVE_IT (it2, *it, it2data);
5634 /* If newline is part of a composition, continue from start of composition */
5635 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5636 && beg < IT_CHARPOS (*it))
5637 goto replaced;
5639 /* If newline is replaced by a display property, find start of overlay
5640 or interval and continue search from that point. */
5641 pos = --IT_CHARPOS (it2);
5642 --IT_BYTEPOS (it2);
5643 it2.sp = 0;
5644 bidi_unshelve_cache (NULL, 0);
5645 it2.string_from_display_prop_p = 0;
5646 it2.from_disp_prop_p = 0;
5647 if (handle_display_prop (&it2) == HANDLED_RETURN
5648 && !NILP (val = get_char_property_and_overlay
5649 (make_number (pos), Qdisplay, Qnil, &overlay))
5650 && (OVERLAYP (overlay)
5651 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5652 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5654 RESTORE_IT (it, it, it2data);
5655 goto replaced;
5658 /* Newline is not replaced by anything -- so we are done. */
5659 RESTORE_IT (it, it, it2data);
5660 break;
5662 replaced:
5663 if (beg < BEGV)
5664 beg = BEGV;
5665 IT_CHARPOS (*it) = beg;
5666 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5670 it->continuation_lines_width = 0;
5672 xassert (IT_CHARPOS (*it) >= BEGV);
5673 xassert (IT_CHARPOS (*it) == BEGV
5674 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5675 CHECK_IT (it);
5679 /* Reseat iterator IT at the previous visible line start. Skip
5680 invisible text that is so either due to text properties or due to
5681 selective display. At the end, update IT's overlay information,
5682 face information etc. */
5684 void
5685 reseat_at_previous_visible_line_start (struct it *it)
5687 back_to_previous_visible_line_start (it);
5688 reseat (it, it->current.pos, 1);
5689 CHECK_IT (it);
5693 /* Reseat iterator IT on the next visible line start in the current
5694 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5695 preceding the line start. Skip over invisible text that is so
5696 because of selective display. Compute faces, overlays etc at the
5697 new position. Note that this function does not skip over text that
5698 is invisible because of text properties. */
5700 static void
5701 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5703 int newline_found_p, skipped_p = 0;
5704 struct bidi_it bidi_it_prev;
5706 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
5708 /* Skip over lines that are invisible because they are indented
5709 more than the value of IT->selective. */
5710 if (it->selective > 0)
5711 while (IT_CHARPOS (*it) < ZV
5712 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5713 it->selective))
5715 xassert (IT_BYTEPOS (*it) == BEGV
5716 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5717 newline_found_p =
5718 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
5721 /* Position on the newline if that's what's requested. */
5722 if (on_newline_p && newline_found_p)
5724 if (STRINGP (it->string))
5726 if (IT_STRING_CHARPOS (*it) > 0)
5728 if (!it->bidi_p)
5730 --IT_STRING_CHARPOS (*it);
5731 --IT_STRING_BYTEPOS (*it);
5733 else
5735 /* We need to restore the bidi iterator to the state
5736 it had on the newline, and resync the IT's
5737 position with that. */
5738 it->bidi_it = bidi_it_prev;
5739 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
5740 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
5744 else if (IT_CHARPOS (*it) > BEGV)
5746 if (!it->bidi_p)
5748 --IT_CHARPOS (*it);
5749 --IT_BYTEPOS (*it);
5751 else
5753 /* We need to restore the bidi iterator to the state it
5754 had on the newline and resync IT with that. */
5755 it->bidi_it = bidi_it_prev;
5756 IT_CHARPOS (*it) = it->bidi_it.charpos;
5757 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5759 reseat (it, it->current.pos, 0);
5762 else if (skipped_p)
5763 reseat (it, it->current.pos, 0);
5765 CHECK_IT (it);
5770 /***********************************************************************
5771 Changing an iterator's position
5772 ***********************************************************************/
5774 /* Change IT's current position to POS in current_buffer. If FORCE_P
5775 is non-zero, always check for text properties at the new position.
5776 Otherwise, text properties are only looked up if POS >=
5777 IT->check_charpos of a property. */
5779 static void
5780 reseat (struct it *it, struct text_pos pos, int force_p)
5782 EMACS_INT original_pos = IT_CHARPOS (*it);
5784 reseat_1 (it, pos, 0);
5786 /* Determine where to check text properties. Avoid doing it
5787 where possible because text property lookup is very expensive. */
5788 if (force_p
5789 || CHARPOS (pos) > it->stop_charpos
5790 || CHARPOS (pos) < original_pos)
5792 if (it->bidi_p)
5794 /* For bidi iteration, we need to prime prev_stop and
5795 base_level_stop with our best estimations. */
5796 /* Implementation note: Of course, POS is not necessarily a
5797 stop position, so assigning prev_pos to it is a lie; we
5798 should have called compute_stop_backwards. However, if
5799 the current buffer does not include any R2L characters,
5800 that call would be a waste of cycles, because the
5801 iterator will never move back, and thus never cross this
5802 "fake" stop position. So we delay that backward search
5803 until the time we really need it, in next_element_from_buffer. */
5804 if (CHARPOS (pos) != it->prev_stop)
5805 it->prev_stop = CHARPOS (pos);
5806 if (CHARPOS (pos) < it->base_level_stop)
5807 it->base_level_stop = 0; /* meaning it's unknown */
5808 handle_stop (it);
5810 else
5812 handle_stop (it);
5813 it->prev_stop = it->base_level_stop = 0;
5818 CHECK_IT (it);
5822 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5823 IT->stop_pos to POS, also. */
5825 static void
5826 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
5828 /* Don't call this function when scanning a C string. */
5829 xassert (it->s == NULL);
5831 /* POS must be a reasonable value. */
5832 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5834 it->current.pos = it->position = pos;
5835 it->end_charpos = ZV;
5836 it->dpvec = NULL;
5837 it->current.dpvec_index = -1;
5838 it->current.overlay_string_index = -1;
5839 IT_STRING_CHARPOS (*it) = -1;
5840 IT_STRING_BYTEPOS (*it) = -1;
5841 it->string = Qnil;
5842 it->method = GET_FROM_BUFFER;
5843 it->object = it->w->buffer;
5844 it->area = TEXT_AREA;
5845 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
5846 it->sp = 0;
5847 it->string_from_display_prop_p = 0;
5848 it->from_disp_prop_p = 0;
5849 it->face_before_selective_p = 0;
5850 if (it->bidi_p)
5852 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
5853 &it->bidi_it);
5854 bidi_unshelve_cache (NULL, 0);
5855 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5856 it->bidi_it.string.s = NULL;
5857 it->bidi_it.string.lstring = Qnil;
5858 it->bidi_it.string.bufpos = 0;
5859 it->bidi_it.string.unibyte = 0;
5862 if (set_stop_p)
5864 it->stop_charpos = CHARPOS (pos);
5865 it->base_level_stop = CHARPOS (pos);
5870 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5871 If S is non-null, it is a C string to iterate over. Otherwise,
5872 STRING gives a Lisp string to iterate over.
5874 If PRECISION > 0, don't return more then PRECISION number of
5875 characters from the string.
5877 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5878 characters have been returned. FIELD_WIDTH < 0 means an infinite
5879 field width.
5881 MULTIBYTE = 0 means disable processing of multibyte characters,
5882 MULTIBYTE > 0 means enable it,
5883 MULTIBYTE < 0 means use IT->multibyte_p.
5885 IT must be initialized via a prior call to init_iterator before
5886 calling this function. */
5888 static void
5889 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
5890 EMACS_INT charpos, EMACS_INT precision, int field_width,
5891 int multibyte)
5893 /* No region in strings. */
5894 it->region_beg_charpos = it->region_end_charpos = -1;
5896 /* No text property checks performed by default, but see below. */
5897 it->stop_charpos = -1;
5899 /* Set iterator position and end position. */
5900 memset (&it->current, 0, sizeof it->current);
5901 it->current.overlay_string_index = -1;
5902 it->current.dpvec_index = -1;
5903 xassert (charpos >= 0);
5905 /* If STRING is specified, use its multibyteness, otherwise use the
5906 setting of MULTIBYTE, if specified. */
5907 if (multibyte >= 0)
5908 it->multibyte_p = multibyte > 0;
5910 /* Bidirectional reordering of strings is controlled by the default
5911 value of bidi-display-reordering. */
5912 it->bidi_p = !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
5914 if (s == NULL)
5916 xassert (STRINGP (string));
5917 it->string = string;
5918 it->s = NULL;
5919 it->end_charpos = it->string_nchars = SCHARS (string);
5920 it->method = GET_FROM_STRING;
5921 it->current.string_pos = string_pos (charpos, string);
5923 if (it->bidi_p)
5925 it->bidi_it.string.lstring = string;
5926 it->bidi_it.string.s = NULL;
5927 it->bidi_it.string.schars = it->end_charpos;
5928 it->bidi_it.string.bufpos = 0;
5929 it->bidi_it.string.from_disp_str = 0;
5930 it->bidi_it.string.unibyte = !it->multibyte_p;
5931 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
5932 FRAME_WINDOW_P (it->f), &it->bidi_it);
5935 else
5937 it->s = (const unsigned char *) s;
5938 it->string = Qnil;
5940 /* Note that we use IT->current.pos, not it->current.string_pos,
5941 for displaying C strings. */
5942 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5943 if (it->multibyte_p)
5945 it->current.pos = c_string_pos (charpos, s, 1);
5946 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5948 else
5950 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5951 it->end_charpos = it->string_nchars = strlen (s);
5954 if (it->bidi_p)
5956 it->bidi_it.string.lstring = Qnil;
5957 it->bidi_it.string.s = (const unsigned char *) s;
5958 it->bidi_it.string.schars = it->end_charpos;
5959 it->bidi_it.string.bufpos = 0;
5960 it->bidi_it.string.from_disp_str = 0;
5961 it->bidi_it.string.unibyte = !it->multibyte_p;
5962 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
5963 &it->bidi_it);
5965 it->method = GET_FROM_C_STRING;
5968 /* PRECISION > 0 means don't return more than PRECISION characters
5969 from the string. */
5970 if (precision > 0 && it->end_charpos - charpos > precision)
5972 it->end_charpos = it->string_nchars = charpos + precision;
5973 if (it->bidi_p)
5974 it->bidi_it.string.schars = it->end_charpos;
5977 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5978 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5979 FIELD_WIDTH < 0 means infinite field width. This is useful for
5980 padding with `-' at the end of a mode line. */
5981 if (field_width < 0)
5982 field_width = INFINITY;
5983 /* Implementation note: We deliberately don't enlarge
5984 it->bidi_it.string.schars here to fit it->end_charpos, because
5985 the bidi iterator cannot produce characters out of thin air. */
5986 if (field_width > it->end_charpos - charpos)
5987 it->end_charpos = charpos + field_width;
5989 /* Use the standard display table for displaying strings. */
5990 if (DISP_TABLE_P (Vstandard_display_table))
5991 it->dp = XCHAR_TABLE (Vstandard_display_table);
5993 it->stop_charpos = charpos;
5994 it->prev_stop = charpos;
5995 it->base_level_stop = 0;
5996 if (it->bidi_p)
5998 it->bidi_it.first_elt = 1;
5999 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6000 it->bidi_it.disp_pos = -1;
6002 if (s == NULL && it->multibyte_p)
6004 EMACS_INT endpos = SCHARS (it->string);
6005 if (endpos > it->end_charpos)
6006 endpos = it->end_charpos;
6007 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6008 it->string);
6010 CHECK_IT (it);
6015 /***********************************************************************
6016 Iteration
6017 ***********************************************************************/
6019 /* Map enum it_method value to corresponding next_element_from_* function. */
6021 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6023 next_element_from_buffer,
6024 next_element_from_display_vector,
6025 next_element_from_string,
6026 next_element_from_c_string,
6027 next_element_from_image,
6028 next_element_from_stretch
6031 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6034 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6035 (possibly with the following characters). */
6037 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6038 ((IT)->cmp_it.id >= 0 \
6039 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6040 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6041 END_CHARPOS, (IT)->w, \
6042 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6043 (IT)->string)))
6046 /* Lookup the char-table Vglyphless_char_display for character C (-1
6047 if we want information for no-font case), and return the display
6048 method symbol. By side-effect, update it->what and
6049 it->glyphless_method. This function is called from
6050 get_next_display_element for each character element, and from
6051 x_produce_glyphs when no suitable font was found. */
6053 Lisp_Object
6054 lookup_glyphless_char_display (int c, struct it *it)
6056 Lisp_Object glyphless_method = Qnil;
6058 if (CHAR_TABLE_P (Vglyphless_char_display)
6059 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6061 if (c >= 0)
6063 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6064 if (CONSP (glyphless_method))
6065 glyphless_method = FRAME_WINDOW_P (it->f)
6066 ? XCAR (glyphless_method)
6067 : XCDR (glyphless_method);
6069 else
6070 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6073 retry:
6074 if (NILP (glyphless_method))
6076 if (c >= 0)
6077 /* The default is to display the character by a proper font. */
6078 return Qnil;
6079 /* The default for the no-font case is to display an empty box. */
6080 glyphless_method = Qempty_box;
6082 if (EQ (glyphless_method, Qzero_width))
6084 if (c >= 0)
6085 return glyphless_method;
6086 /* This method can't be used for the no-font case. */
6087 glyphless_method = Qempty_box;
6089 if (EQ (glyphless_method, Qthin_space))
6090 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6091 else if (EQ (glyphless_method, Qempty_box))
6092 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6093 else if (EQ (glyphless_method, Qhex_code))
6094 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6095 else if (STRINGP (glyphless_method))
6096 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6097 else
6099 /* Invalid value. We use the default method. */
6100 glyphless_method = Qnil;
6101 goto retry;
6103 it->what = IT_GLYPHLESS;
6104 return glyphless_method;
6107 /* Load IT's display element fields with information about the next
6108 display element from the current position of IT. Value is zero if
6109 end of buffer (or C string) is reached. */
6111 static struct frame *last_escape_glyph_frame = NULL;
6112 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6113 static int last_escape_glyph_merged_face_id = 0;
6115 struct frame *last_glyphless_glyph_frame = NULL;
6116 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6117 int last_glyphless_glyph_merged_face_id = 0;
6119 static int
6120 get_next_display_element (struct it *it)
6122 /* Non-zero means that we found a display element. Zero means that
6123 we hit the end of what we iterate over. Performance note: the
6124 function pointer `method' used here turns out to be faster than
6125 using a sequence of if-statements. */
6126 int success_p;
6128 get_next:
6129 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6131 if (it->what == IT_CHARACTER)
6133 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6134 and only if (a) the resolved directionality of that character
6135 is R..." */
6136 /* FIXME: Do we need an exception for characters from display
6137 tables? */
6138 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6139 it->c = bidi_mirror_char (it->c);
6140 /* Map via display table or translate control characters.
6141 IT->c, IT->len etc. have been set to the next character by
6142 the function call above. If we have a display table, and it
6143 contains an entry for IT->c, translate it. Don't do this if
6144 IT->c itself comes from a display table, otherwise we could
6145 end up in an infinite recursion. (An alternative could be to
6146 count the recursion depth of this function and signal an
6147 error when a certain maximum depth is reached.) Is it worth
6148 it? */
6149 if (success_p && it->dpvec == NULL)
6151 Lisp_Object dv;
6152 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6153 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
6154 nbsp_or_shy = char_is_other;
6155 int c = it->c; /* This is the character to display. */
6157 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6159 xassert (SINGLE_BYTE_CHAR_P (c));
6160 if (unibyte_display_via_language_environment)
6162 c = DECODE_CHAR (unibyte, c);
6163 if (c < 0)
6164 c = BYTE8_TO_CHAR (it->c);
6166 else
6167 c = BYTE8_TO_CHAR (it->c);
6170 if (it->dp
6171 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6172 VECTORP (dv)))
6174 struct Lisp_Vector *v = XVECTOR (dv);
6176 /* Return the first character from the display table
6177 entry, if not empty. If empty, don't display the
6178 current character. */
6179 if (v->header.size)
6181 it->dpvec_char_len = it->len;
6182 it->dpvec = v->contents;
6183 it->dpend = v->contents + v->header.size;
6184 it->current.dpvec_index = 0;
6185 it->dpvec_face_id = -1;
6186 it->saved_face_id = it->face_id;
6187 it->method = GET_FROM_DISPLAY_VECTOR;
6188 it->ellipsis_p = 0;
6190 else
6192 set_iterator_to_next (it, 0);
6194 goto get_next;
6197 if (! NILP (lookup_glyphless_char_display (c, it)))
6199 if (it->what == IT_GLYPHLESS)
6200 goto done;
6201 /* Don't display this character. */
6202 set_iterator_to_next (it, 0);
6203 goto get_next;
6206 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6207 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
6208 : c == 0xAD ? char_is_soft_hyphen
6209 : char_is_other);
6211 /* Translate control characters into `\003' or `^C' form.
6212 Control characters coming from a display table entry are
6213 currently not translated because we use IT->dpvec to hold
6214 the translation. This could easily be changed but I
6215 don't believe that it is worth doing.
6217 NBSP and SOFT-HYPEN are property translated too.
6219 Non-printable characters and raw-byte characters are also
6220 translated to octal form. */
6221 if (((c < ' ' || c == 127) /* ASCII control chars */
6222 ? (it->area != TEXT_AREA
6223 /* In mode line, treat \n, \t like other crl chars. */
6224 || (c != '\t'
6225 && it->glyph_row
6226 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6227 || (c != '\n' && c != '\t'))
6228 : (nbsp_or_shy
6229 || CHAR_BYTE8_P (c)
6230 || ! CHAR_PRINTABLE_P (c))))
6232 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
6233 or a non-printable character which must be displayed
6234 either as '\003' or as `^C' where the '\\' and '^'
6235 can be defined in the display table. Fill
6236 IT->ctl_chars with glyphs for what we have to
6237 display. Then, set IT->dpvec to these glyphs. */
6238 Lisp_Object gc;
6239 int ctl_len;
6240 int face_id;
6241 EMACS_INT lface_id = 0;
6242 int escape_glyph;
6244 /* Handle control characters with ^. */
6246 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6248 int g;
6250 g = '^'; /* default glyph for Control */
6251 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6252 if (it->dp
6253 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
6254 && GLYPH_CODE_CHAR_VALID_P (gc))
6256 g = GLYPH_CODE_CHAR (gc);
6257 lface_id = GLYPH_CODE_FACE (gc);
6259 if (lface_id)
6261 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6263 else if (it->f == last_escape_glyph_frame
6264 && it->face_id == last_escape_glyph_face_id)
6266 face_id = last_escape_glyph_merged_face_id;
6268 else
6270 /* Merge the escape-glyph face into the current face. */
6271 face_id = merge_faces (it->f, Qescape_glyph, 0,
6272 it->face_id);
6273 last_escape_glyph_frame = it->f;
6274 last_escape_glyph_face_id = it->face_id;
6275 last_escape_glyph_merged_face_id = face_id;
6278 XSETINT (it->ctl_chars[0], g);
6279 XSETINT (it->ctl_chars[1], c ^ 0100);
6280 ctl_len = 2;
6281 goto display_control;
6284 /* Handle non-break space in the mode where it only gets
6285 highlighting. */
6287 if (EQ (Vnobreak_char_display, Qt)
6288 && nbsp_or_shy == char_is_nbsp)
6290 /* Merge the no-break-space face into the current face. */
6291 face_id = merge_faces (it->f, Qnobreak_space, 0,
6292 it->face_id);
6294 c = ' ';
6295 XSETINT (it->ctl_chars[0], ' ');
6296 ctl_len = 1;
6297 goto display_control;
6300 /* Handle sequences that start with the "escape glyph". */
6302 /* the default escape glyph is \. */
6303 escape_glyph = '\\';
6305 if (it->dp
6306 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
6307 && GLYPH_CODE_CHAR_VALID_P (gc))
6309 escape_glyph = GLYPH_CODE_CHAR (gc);
6310 lface_id = GLYPH_CODE_FACE (gc);
6312 if (lface_id)
6314 /* The display table specified a face.
6315 Merge it into face_id and also into escape_glyph. */
6316 face_id = merge_faces (it->f, Qt, lface_id,
6317 it->face_id);
6319 else if (it->f == last_escape_glyph_frame
6320 && it->face_id == last_escape_glyph_face_id)
6322 face_id = last_escape_glyph_merged_face_id;
6324 else
6326 /* Merge the escape-glyph face into the current face. */
6327 face_id = merge_faces (it->f, Qescape_glyph, 0,
6328 it->face_id);
6329 last_escape_glyph_frame = it->f;
6330 last_escape_glyph_face_id = it->face_id;
6331 last_escape_glyph_merged_face_id = face_id;
6334 /* Handle soft hyphens in the mode where they only get
6335 highlighting. */
6337 if (EQ (Vnobreak_char_display, Qt)
6338 && nbsp_or_shy == char_is_soft_hyphen)
6340 XSETINT (it->ctl_chars[0], '-');
6341 ctl_len = 1;
6342 goto display_control;
6345 /* Handle non-break space and soft hyphen
6346 with the escape glyph. */
6348 if (nbsp_or_shy)
6350 XSETINT (it->ctl_chars[0], escape_glyph);
6351 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
6352 XSETINT (it->ctl_chars[1], c);
6353 ctl_len = 2;
6354 goto display_control;
6358 char str[10];
6359 int len, i;
6361 if (CHAR_BYTE8_P (c))
6362 /* Display \200 instead of \17777600. */
6363 c = CHAR_TO_BYTE8 (c);
6364 len = sprintf (str, "%03o", c);
6366 XSETINT (it->ctl_chars[0], escape_glyph);
6367 for (i = 0; i < len; i++)
6368 XSETINT (it->ctl_chars[i + 1], str[i]);
6369 ctl_len = len + 1;
6372 display_control:
6373 /* Set up IT->dpvec and return first character from it. */
6374 it->dpvec_char_len = it->len;
6375 it->dpvec = it->ctl_chars;
6376 it->dpend = it->dpvec + ctl_len;
6377 it->current.dpvec_index = 0;
6378 it->dpvec_face_id = face_id;
6379 it->saved_face_id = it->face_id;
6380 it->method = GET_FROM_DISPLAY_VECTOR;
6381 it->ellipsis_p = 0;
6382 goto get_next;
6384 it->char_to_display = c;
6386 else if (success_p)
6388 it->char_to_display = it->c;
6392 /* Adjust face id for a multibyte character. There are no multibyte
6393 character in unibyte text. */
6394 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6395 && it->multibyte_p
6396 && success_p
6397 && FRAME_WINDOW_P (it->f))
6399 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6401 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6403 /* Automatic composition with glyph-string. */
6404 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6406 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6408 else
6410 EMACS_INT pos = (it->s ? -1
6411 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6412 : IT_CHARPOS (*it));
6413 int c;
6415 if (it->what == IT_CHARACTER)
6416 c = it->char_to_display;
6417 else
6419 struct composition *cmp = composition_table[it->cmp_it.id];
6420 int i;
6422 c = ' ';
6423 for (i = 0; i < cmp->glyph_len; i++)
6424 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6425 break;
6427 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6431 done:
6432 /* Is this character the last one of a run of characters with
6433 box? If yes, set IT->end_of_box_run_p to 1. */
6434 if (it->face_box_p
6435 && it->s == NULL)
6437 if (it->method == GET_FROM_STRING && it->sp)
6439 int face_id = underlying_face_id (it);
6440 struct face *face = FACE_FROM_ID (it->f, face_id);
6442 if (face)
6444 if (face->box == FACE_NO_BOX)
6446 /* If the box comes from face properties in a
6447 display string, check faces in that string. */
6448 int string_face_id = face_after_it_pos (it);
6449 it->end_of_box_run_p
6450 = (FACE_FROM_ID (it->f, string_face_id)->box
6451 == FACE_NO_BOX);
6453 /* Otherwise, the box comes from the underlying face.
6454 If this is the last string character displayed, check
6455 the next buffer location. */
6456 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6457 && (it->current.overlay_string_index
6458 == it->n_overlay_strings - 1))
6460 EMACS_INT ignore;
6461 int next_face_id;
6462 struct text_pos pos = it->current.pos;
6463 INC_TEXT_POS (pos, it->multibyte_p);
6465 next_face_id = face_at_buffer_position
6466 (it->w, CHARPOS (pos), it->region_beg_charpos,
6467 it->region_end_charpos, &ignore,
6468 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6469 -1);
6470 it->end_of_box_run_p
6471 = (FACE_FROM_ID (it->f, next_face_id)->box
6472 == FACE_NO_BOX);
6476 else
6478 int face_id = face_after_it_pos (it);
6479 it->end_of_box_run_p
6480 = (face_id != it->face_id
6481 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6485 /* Value is 0 if end of buffer or string reached. */
6486 return success_p;
6490 /* Move IT to the next display element.
6492 RESEAT_P non-zero means if called on a newline in buffer text,
6493 skip to the next visible line start.
6495 Functions get_next_display_element and set_iterator_to_next are
6496 separate because I find this arrangement easier to handle than a
6497 get_next_display_element function that also increments IT's
6498 position. The way it is we can first look at an iterator's current
6499 display element, decide whether it fits on a line, and if it does,
6500 increment the iterator position. The other way around we probably
6501 would either need a flag indicating whether the iterator has to be
6502 incremented the next time, or we would have to implement a
6503 decrement position function which would not be easy to write. */
6505 void
6506 set_iterator_to_next (struct it *it, int reseat_p)
6508 /* Reset flags indicating start and end of a sequence of characters
6509 with box. Reset them at the start of this function because
6510 moving the iterator to a new position might set them. */
6511 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6513 switch (it->method)
6515 case GET_FROM_BUFFER:
6516 /* The current display element of IT is a character from
6517 current_buffer. Advance in the buffer, and maybe skip over
6518 invisible lines that are so because of selective display. */
6519 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6520 reseat_at_next_visible_line_start (it, 0);
6521 else if (it->cmp_it.id >= 0)
6523 /* We are currently getting glyphs from a composition. */
6524 int i;
6526 if (! it->bidi_p)
6528 IT_CHARPOS (*it) += it->cmp_it.nchars;
6529 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6530 if (it->cmp_it.to < it->cmp_it.nglyphs)
6532 it->cmp_it.from = it->cmp_it.to;
6534 else
6536 it->cmp_it.id = -1;
6537 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6538 IT_BYTEPOS (*it),
6539 it->end_charpos, Qnil);
6542 else if (! it->cmp_it.reversed_p)
6544 /* Composition created while scanning forward. */
6545 /* Update IT's char/byte positions to point to the first
6546 character of the next grapheme cluster, or to the
6547 character visually after the current composition. */
6548 for (i = 0; i < it->cmp_it.nchars; i++)
6549 bidi_move_to_visually_next (&it->bidi_it);
6550 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6551 IT_CHARPOS (*it) = it->bidi_it.charpos;
6553 if (it->cmp_it.to < it->cmp_it.nglyphs)
6555 /* Proceed to the next grapheme cluster. */
6556 it->cmp_it.from = it->cmp_it.to;
6558 else
6560 /* No more grapheme clusters in this composition.
6561 Find the next stop position. */
6562 EMACS_INT stop = it->end_charpos;
6563 if (it->bidi_it.scan_dir < 0)
6564 /* Now we are scanning backward and don't know
6565 where to stop. */
6566 stop = -1;
6567 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6568 IT_BYTEPOS (*it), stop, Qnil);
6571 else
6573 /* Composition created while scanning backward. */
6574 /* Update IT's char/byte positions to point to the last
6575 character of the previous grapheme cluster, or the
6576 character visually after the current composition. */
6577 for (i = 0; i < it->cmp_it.nchars; i++)
6578 bidi_move_to_visually_next (&it->bidi_it);
6579 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6580 IT_CHARPOS (*it) = it->bidi_it.charpos;
6581 if (it->cmp_it.from > 0)
6583 /* Proceed to the previous grapheme cluster. */
6584 it->cmp_it.to = it->cmp_it.from;
6586 else
6588 /* No more grapheme clusters in this composition.
6589 Find the next stop position. */
6590 EMACS_INT stop = it->end_charpos;
6591 if (it->bidi_it.scan_dir < 0)
6592 /* Now we are scanning backward and don't know
6593 where to stop. */
6594 stop = -1;
6595 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6596 IT_BYTEPOS (*it), stop, Qnil);
6600 else
6602 xassert (it->len != 0);
6604 if (!it->bidi_p)
6606 IT_BYTEPOS (*it) += it->len;
6607 IT_CHARPOS (*it) += 1;
6609 else
6611 int prev_scan_dir = it->bidi_it.scan_dir;
6612 /* If this is a new paragraph, determine its base
6613 direction (a.k.a. its base embedding level). */
6614 if (it->bidi_it.new_paragraph)
6615 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6616 bidi_move_to_visually_next (&it->bidi_it);
6617 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6618 IT_CHARPOS (*it) = it->bidi_it.charpos;
6619 if (prev_scan_dir != it->bidi_it.scan_dir)
6621 /* As the scan direction was changed, we must
6622 re-compute the stop position for composition. */
6623 EMACS_INT stop = it->end_charpos;
6624 if (it->bidi_it.scan_dir < 0)
6625 stop = -1;
6626 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6627 IT_BYTEPOS (*it), stop, Qnil);
6630 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6632 break;
6634 case GET_FROM_C_STRING:
6635 /* Current display element of IT is from a C string. */
6636 if (!it->bidi_p
6637 /* If the string position is beyond string's end, it means
6638 next_element_from_c_string is padding the string with
6639 blanks, in which case we bypass the bidi iterator,
6640 because it cannot deal with such virtual characters. */
6641 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
6643 IT_BYTEPOS (*it) += it->len;
6644 IT_CHARPOS (*it) += 1;
6646 else
6648 bidi_move_to_visually_next (&it->bidi_it);
6649 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6650 IT_CHARPOS (*it) = it->bidi_it.charpos;
6652 break;
6654 case GET_FROM_DISPLAY_VECTOR:
6655 /* Current display element of IT is from a display table entry.
6656 Advance in the display table definition. Reset it to null if
6657 end reached, and continue with characters from buffers/
6658 strings. */
6659 ++it->current.dpvec_index;
6661 /* Restore face of the iterator to what they were before the
6662 display vector entry (these entries may contain faces). */
6663 it->face_id = it->saved_face_id;
6665 if (it->dpvec + it->current.dpvec_index == it->dpend)
6667 int recheck_faces = it->ellipsis_p;
6669 if (it->s)
6670 it->method = GET_FROM_C_STRING;
6671 else if (STRINGP (it->string))
6672 it->method = GET_FROM_STRING;
6673 else
6675 it->method = GET_FROM_BUFFER;
6676 it->object = it->w->buffer;
6679 it->dpvec = NULL;
6680 it->current.dpvec_index = -1;
6682 /* Skip over characters which were displayed via IT->dpvec. */
6683 if (it->dpvec_char_len < 0)
6684 reseat_at_next_visible_line_start (it, 1);
6685 else if (it->dpvec_char_len > 0)
6687 if (it->method == GET_FROM_STRING
6688 && it->n_overlay_strings > 0)
6689 it->ignore_overlay_strings_at_pos_p = 1;
6690 it->len = it->dpvec_char_len;
6691 set_iterator_to_next (it, reseat_p);
6694 /* Maybe recheck faces after display vector */
6695 if (recheck_faces)
6696 it->stop_charpos = IT_CHARPOS (*it);
6698 break;
6700 case GET_FROM_STRING:
6701 /* Current display element is a character from a Lisp string. */
6702 xassert (it->s == NULL && STRINGP (it->string));
6703 if (it->cmp_it.id >= 0)
6705 int i;
6707 if (! it->bidi_p)
6709 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6710 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6711 if (it->cmp_it.to < it->cmp_it.nglyphs)
6712 it->cmp_it.from = it->cmp_it.to;
6713 else
6715 it->cmp_it.id = -1;
6716 composition_compute_stop_pos (&it->cmp_it,
6717 IT_STRING_CHARPOS (*it),
6718 IT_STRING_BYTEPOS (*it),
6719 it->end_charpos, it->string);
6722 else if (! it->cmp_it.reversed_p)
6724 for (i = 0; i < it->cmp_it.nchars; i++)
6725 bidi_move_to_visually_next (&it->bidi_it);
6726 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6727 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6729 if (it->cmp_it.to < it->cmp_it.nglyphs)
6730 it->cmp_it.from = it->cmp_it.to;
6731 else
6733 EMACS_INT stop = it->end_charpos;
6734 if (it->bidi_it.scan_dir < 0)
6735 stop = -1;
6736 composition_compute_stop_pos (&it->cmp_it,
6737 IT_STRING_CHARPOS (*it),
6738 IT_STRING_BYTEPOS (*it), stop,
6739 it->string);
6742 else
6744 for (i = 0; i < it->cmp_it.nchars; i++)
6745 bidi_move_to_visually_next (&it->bidi_it);
6746 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6747 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6748 if (it->cmp_it.from > 0)
6749 it->cmp_it.to = it->cmp_it.from;
6750 else
6752 EMACS_INT stop = it->end_charpos;
6753 if (it->bidi_it.scan_dir < 0)
6754 stop = -1;
6755 composition_compute_stop_pos (&it->cmp_it,
6756 IT_STRING_CHARPOS (*it),
6757 IT_STRING_BYTEPOS (*it), stop,
6758 it->string);
6762 else
6764 if (!it->bidi_p
6765 /* If the string position is beyond string's end, it
6766 means next_element_from_string is padding the string
6767 with blanks, in which case we bypass the bidi
6768 iterator, because it cannot deal with such virtual
6769 characters. */
6770 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
6772 IT_STRING_BYTEPOS (*it) += it->len;
6773 IT_STRING_CHARPOS (*it) += 1;
6775 else
6777 int prev_scan_dir = it->bidi_it.scan_dir;
6779 bidi_move_to_visually_next (&it->bidi_it);
6780 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6781 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6782 if (prev_scan_dir != it->bidi_it.scan_dir)
6784 EMACS_INT stop = it->end_charpos;
6786 if (it->bidi_it.scan_dir < 0)
6787 stop = -1;
6788 composition_compute_stop_pos (&it->cmp_it,
6789 IT_STRING_CHARPOS (*it),
6790 IT_STRING_BYTEPOS (*it), stop,
6791 it->string);
6796 consider_string_end:
6798 if (it->current.overlay_string_index >= 0)
6800 /* IT->string is an overlay string. Advance to the
6801 next, if there is one. */
6802 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6804 it->ellipsis_p = 0;
6805 next_overlay_string (it);
6806 if (it->ellipsis_p)
6807 setup_for_ellipsis (it, 0);
6810 else
6812 /* IT->string is not an overlay string. If we reached
6813 its end, and there is something on IT->stack, proceed
6814 with what is on the stack. This can be either another
6815 string, this time an overlay string, or a buffer. */
6816 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6817 && it->sp > 0)
6819 pop_it (it);
6820 if (it->method == GET_FROM_STRING)
6821 goto consider_string_end;
6824 break;
6826 case GET_FROM_IMAGE:
6827 case GET_FROM_STRETCH:
6828 /* The position etc with which we have to proceed are on
6829 the stack. The position may be at the end of a string,
6830 if the `display' property takes up the whole string. */
6831 xassert (it->sp > 0);
6832 pop_it (it);
6833 if (it->method == GET_FROM_STRING)
6834 goto consider_string_end;
6835 break;
6837 default:
6838 /* There are no other methods defined, so this should be a bug. */
6839 abort ();
6842 xassert (it->method != GET_FROM_STRING
6843 || (STRINGP (it->string)
6844 && IT_STRING_CHARPOS (*it) >= 0));
6847 /* Load IT's display element fields with information about the next
6848 display element which comes from a display table entry or from the
6849 result of translating a control character to one of the forms `^C'
6850 or `\003'.
6852 IT->dpvec holds the glyphs to return as characters.
6853 IT->saved_face_id holds the face id before the display vector--it
6854 is restored into IT->face_id in set_iterator_to_next. */
6856 static int
6857 next_element_from_display_vector (struct it *it)
6859 Lisp_Object gc;
6861 /* Precondition. */
6862 xassert (it->dpvec && it->current.dpvec_index >= 0);
6864 it->face_id = it->saved_face_id;
6866 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6867 That seemed totally bogus - so I changed it... */
6868 gc = it->dpvec[it->current.dpvec_index];
6870 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6872 it->c = GLYPH_CODE_CHAR (gc);
6873 it->len = CHAR_BYTES (it->c);
6875 /* The entry may contain a face id to use. Such a face id is
6876 the id of a Lisp face, not a realized face. A face id of
6877 zero means no face is specified. */
6878 if (it->dpvec_face_id >= 0)
6879 it->face_id = it->dpvec_face_id;
6880 else
6882 EMACS_INT lface_id = GLYPH_CODE_FACE (gc);
6883 if (lface_id > 0)
6884 it->face_id = merge_faces (it->f, Qt, lface_id,
6885 it->saved_face_id);
6888 else
6889 /* Display table entry is invalid. Return a space. */
6890 it->c = ' ', it->len = 1;
6892 /* Don't change position and object of the iterator here. They are
6893 still the values of the character that had this display table
6894 entry or was translated, and that's what we want. */
6895 it->what = IT_CHARACTER;
6896 return 1;
6899 /* Get the first element of string/buffer in the visual order, after
6900 being reseated to a new position in a string or a buffer. */
6901 static void
6902 get_visually_first_element (struct it *it)
6904 int string_p = STRINGP (it->string) || it->s;
6905 EMACS_INT eob = (string_p ? it->bidi_it.string.schars : ZV);
6906 EMACS_INT bob = (string_p ? 0 : BEGV);
6908 if (STRINGP (it->string))
6910 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
6911 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
6913 else
6915 it->bidi_it.charpos = IT_CHARPOS (*it);
6916 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6919 if (it->bidi_it.charpos == eob)
6921 /* Nothing to do, but reset the FIRST_ELT flag, like
6922 bidi_paragraph_init does, because we are not going to
6923 call it. */
6924 it->bidi_it.first_elt = 0;
6926 else if (it->bidi_it.charpos == bob
6927 || (!string_p
6928 /* FIXME: Should support all Unicode line separators. */
6929 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6930 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
6932 /* If we are at the beginning of a line/string, we can produce
6933 the next element right away. */
6934 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6935 bidi_move_to_visually_next (&it->bidi_it);
6937 else
6939 EMACS_INT orig_bytepos = it->bidi_it.bytepos;
6941 /* We need to prime the bidi iterator starting at the line's or
6942 string's beginning, before we will be able to produce the
6943 next element. */
6944 if (string_p)
6945 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
6946 else
6948 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
6949 -1);
6950 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
6952 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6955 /* Now return to buffer/string position where we were asked
6956 to get the next display element, and produce that. */
6957 bidi_move_to_visually_next (&it->bidi_it);
6959 while (it->bidi_it.bytepos != orig_bytepos
6960 && it->bidi_it.charpos < eob);
6963 /* Adjust IT's position information to where we ended up. */
6964 if (STRINGP (it->string))
6966 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6967 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6969 else
6971 IT_CHARPOS (*it) = it->bidi_it.charpos;
6972 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6975 if (STRINGP (it->string) || !it->s)
6977 EMACS_INT stop, charpos, bytepos;
6979 if (STRINGP (it->string))
6981 xassert (!it->s);
6982 stop = SCHARS (it->string);
6983 if (stop > it->end_charpos)
6984 stop = it->end_charpos;
6985 charpos = IT_STRING_CHARPOS (*it);
6986 bytepos = IT_STRING_BYTEPOS (*it);
6988 else
6990 stop = it->end_charpos;
6991 charpos = IT_CHARPOS (*it);
6992 bytepos = IT_BYTEPOS (*it);
6994 if (it->bidi_it.scan_dir < 0)
6995 stop = -1;
6996 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
6997 it->string);
7001 /* Load IT with the next display element from Lisp string IT->string.
7002 IT->current.string_pos is the current position within the string.
7003 If IT->current.overlay_string_index >= 0, the Lisp string is an
7004 overlay string. */
7006 static int
7007 next_element_from_string (struct it *it)
7009 struct text_pos position;
7011 xassert (STRINGP (it->string));
7012 xassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7013 xassert (IT_STRING_CHARPOS (*it) >= 0);
7014 position = it->current.string_pos;
7016 /* With bidi reordering, the character to display might not be the
7017 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7018 that we were reseat()ed to a new string, whose paragraph
7019 direction is not known. */
7020 if (it->bidi_p && it->bidi_it.first_elt)
7022 get_visually_first_element (it);
7023 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7026 /* Time to check for invisible text? */
7027 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7029 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7031 if (!(!it->bidi_p
7032 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7033 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7035 /* With bidi non-linear iteration, we could find
7036 ourselves far beyond the last computed stop_charpos,
7037 with several other stop positions in between that we
7038 missed. Scan them all now, in buffer's logical
7039 order, until we find and handle the last stop_charpos
7040 that precedes our current position. */
7041 handle_stop_backwards (it, it->stop_charpos);
7042 return GET_NEXT_DISPLAY_ELEMENT (it);
7044 else
7046 if (it->bidi_p)
7048 /* Take note of the stop position we just moved
7049 across, for when we will move back across it. */
7050 it->prev_stop = it->stop_charpos;
7051 /* If we are at base paragraph embedding level, take
7052 note of the last stop position seen at this
7053 level. */
7054 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7055 it->base_level_stop = it->stop_charpos;
7057 handle_stop (it);
7059 /* Since a handler may have changed IT->method, we must
7060 recurse here. */
7061 return GET_NEXT_DISPLAY_ELEMENT (it);
7064 else if (it->bidi_p
7065 /* If we are before prev_stop, we may have overstepped
7066 on our way backwards a stop_pos, and if so, we need
7067 to handle that stop_pos. */
7068 && IT_STRING_CHARPOS (*it) < it->prev_stop
7069 /* We can sometimes back up for reasons that have nothing
7070 to do with bidi reordering. E.g., compositions. The
7071 code below is only needed when we are above the base
7072 embedding level, so test for that explicitly. */
7073 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7075 /* If we lost track of base_level_stop, we have no better
7076 place for handle_stop_backwards to start from than string
7077 beginning. This happens, e.g., when we were reseated to
7078 the previous screenful of text by vertical-motion. */
7079 if (it->base_level_stop <= 0
7080 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7081 it->base_level_stop = 0;
7082 handle_stop_backwards (it, it->base_level_stop);
7083 return GET_NEXT_DISPLAY_ELEMENT (it);
7087 if (it->current.overlay_string_index >= 0)
7089 /* Get the next character from an overlay string. In overlay
7090 strings, There is no field width or padding with spaces to
7091 do. */
7092 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7094 it->what = IT_EOB;
7095 return 0;
7097 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7098 IT_STRING_BYTEPOS (*it),
7099 it->bidi_it.scan_dir < 0
7100 ? -1
7101 : SCHARS (it->string))
7102 && next_element_from_composition (it))
7104 return 1;
7106 else if (STRING_MULTIBYTE (it->string))
7108 const unsigned char *s = (SDATA (it->string)
7109 + IT_STRING_BYTEPOS (*it));
7110 it->c = string_char_and_length (s, &it->len);
7112 else
7114 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7115 it->len = 1;
7118 else
7120 /* Get the next character from a Lisp string that is not an
7121 overlay string. Such strings come from the mode line, for
7122 example. We may have to pad with spaces, or truncate the
7123 string. See also next_element_from_c_string. */
7124 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7126 it->what = IT_EOB;
7127 return 0;
7129 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7131 /* Pad with spaces. */
7132 it->c = ' ', it->len = 1;
7133 CHARPOS (position) = BYTEPOS (position) = -1;
7135 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7136 IT_STRING_BYTEPOS (*it),
7137 it->bidi_it.scan_dir < 0
7138 ? -1
7139 : it->string_nchars)
7140 && next_element_from_composition (it))
7142 return 1;
7144 else if (STRING_MULTIBYTE (it->string))
7146 const unsigned char *s = (SDATA (it->string)
7147 + IT_STRING_BYTEPOS (*it));
7148 it->c = string_char_and_length (s, &it->len);
7150 else
7152 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7153 it->len = 1;
7157 /* Record what we have and where it came from. */
7158 it->what = IT_CHARACTER;
7159 it->object = it->string;
7160 it->position = position;
7161 return 1;
7165 /* Load IT with next display element from C string IT->s.
7166 IT->string_nchars is the maximum number of characters to return
7167 from the string. IT->end_charpos may be greater than
7168 IT->string_nchars when this function is called, in which case we
7169 may have to return padding spaces. Value is zero if end of string
7170 reached, including padding spaces. */
7172 static int
7173 next_element_from_c_string (struct it *it)
7175 int success_p = 1;
7177 xassert (it->s);
7178 xassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7179 it->what = IT_CHARACTER;
7180 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7181 it->object = Qnil;
7183 /* With bidi reordering, the character to display might not be the
7184 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7185 we were reseated to a new string, whose paragraph direction is
7186 not known. */
7187 if (it->bidi_p && it->bidi_it.first_elt)
7188 get_visually_first_element (it);
7190 /* IT's position can be greater than IT->string_nchars in case a
7191 field width or precision has been specified when the iterator was
7192 initialized. */
7193 if (IT_CHARPOS (*it) >= it->end_charpos)
7195 /* End of the game. */
7196 it->what = IT_EOB;
7197 success_p = 0;
7199 else if (IT_CHARPOS (*it) >= it->string_nchars)
7201 /* Pad with spaces. */
7202 it->c = ' ', it->len = 1;
7203 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7205 else if (it->multibyte_p)
7206 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7207 else
7208 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7210 return success_p;
7214 /* Set up IT to return characters from an ellipsis, if appropriate.
7215 The definition of the ellipsis glyphs may come from a display table
7216 entry. This function fills IT with the first glyph from the
7217 ellipsis if an ellipsis is to be displayed. */
7219 static int
7220 next_element_from_ellipsis (struct it *it)
7222 if (it->selective_display_ellipsis_p)
7223 setup_for_ellipsis (it, it->len);
7224 else
7226 /* The face at the current position may be different from the
7227 face we find after the invisible text. Remember what it
7228 was in IT->saved_face_id, and signal that it's there by
7229 setting face_before_selective_p. */
7230 it->saved_face_id = it->face_id;
7231 it->method = GET_FROM_BUFFER;
7232 it->object = it->w->buffer;
7233 reseat_at_next_visible_line_start (it, 1);
7234 it->face_before_selective_p = 1;
7237 return GET_NEXT_DISPLAY_ELEMENT (it);
7241 /* Deliver an image display element. The iterator IT is already
7242 filled with image information (done in handle_display_prop). Value
7243 is always 1. */
7246 static int
7247 next_element_from_image (struct it *it)
7249 it->what = IT_IMAGE;
7250 it->ignore_overlay_strings_at_pos_p = 0;
7251 return 1;
7255 /* Fill iterator IT with next display element from a stretch glyph
7256 property. IT->object is the value of the text property. Value is
7257 always 1. */
7259 static int
7260 next_element_from_stretch (struct it *it)
7262 it->what = IT_STRETCH;
7263 return 1;
7266 /* Scan backwards from IT's current position until we find a stop
7267 position, or until BEGV. This is called when we find ourself
7268 before both the last known prev_stop and base_level_stop while
7269 reordering bidirectional text. */
7271 static void
7272 compute_stop_pos_backwards (struct it *it)
7274 const int SCAN_BACK_LIMIT = 1000;
7275 struct text_pos pos;
7276 struct display_pos save_current = it->current;
7277 struct text_pos save_position = it->position;
7278 EMACS_INT charpos = IT_CHARPOS (*it);
7279 EMACS_INT where_we_are = charpos;
7280 EMACS_INT save_stop_pos = it->stop_charpos;
7281 EMACS_INT save_end_pos = it->end_charpos;
7283 xassert (NILP (it->string) && !it->s);
7284 xassert (it->bidi_p);
7285 it->bidi_p = 0;
7288 it->end_charpos = min (charpos + 1, ZV);
7289 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7290 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7291 reseat_1 (it, pos, 0);
7292 compute_stop_pos (it);
7293 /* We must advance forward, right? */
7294 if (it->stop_charpos <= charpos)
7295 abort ();
7297 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7299 if (it->stop_charpos <= where_we_are)
7300 it->prev_stop = it->stop_charpos;
7301 else
7302 it->prev_stop = BEGV;
7303 it->bidi_p = 1;
7304 it->current = save_current;
7305 it->position = save_position;
7306 it->stop_charpos = save_stop_pos;
7307 it->end_charpos = save_end_pos;
7310 /* Scan forward from CHARPOS in the current buffer/string, until we
7311 find a stop position > current IT's position. Then handle the stop
7312 position before that. This is called when we bump into a stop
7313 position while reordering bidirectional text. CHARPOS should be
7314 the last previously processed stop_pos (or BEGV/0, if none were
7315 processed yet) whose position is less that IT's current
7316 position. */
7318 static void
7319 handle_stop_backwards (struct it *it, EMACS_INT charpos)
7321 int bufp = !STRINGP (it->string);
7322 EMACS_INT where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7323 struct display_pos save_current = it->current;
7324 struct text_pos save_position = it->position;
7325 struct text_pos pos1;
7326 EMACS_INT next_stop;
7328 /* Scan in strict logical order. */
7329 xassert (it->bidi_p);
7330 it->bidi_p = 0;
7333 it->prev_stop = charpos;
7334 if (bufp)
7336 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7337 reseat_1 (it, pos1, 0);
7339 else
7340 it->current.string_pos = string_pos (charpos, it->string);
7341 compute_stop_pos (it);
7342 /* We must advance forward, right? */
7343 if (it->stop_charpos <= it->prev_stop)
7344 abort ();
7345 charpos = it->stop_charpos;
7347 while (charpos <= where_we_are);
7349 it->bidi_p = 1;
7350 it->current = save_current;
7351 it->position = save_position;
7352 next_stop = it->stop_charpos;
7353 it->stop_charpos = it->prev_stop;
7354 handle_stop (it);
7355 it->stop_charpos = next_stop;
7358 /* Load IT with the next display element from current_buffer. Value
7359 is zero if end of buffer reached. IT->stop_charpos is the next
7360 position at which to stop and check for text properties or buffer
7361 end. */
7363 static int
7364 next_element_from_buffer (struct it *it)
7366 int success_p = 1;
7368 xassert (IT_CHARPOS (*it) >= BEGV);
7369 xassert (NILP (it->string) && !it->s);
7370 xassert (!it->bidi_p
7371 || (EQ (it->bidi_it.string.lstring, Qnil)
7372 && it->bidi_it.string.s == NULL));
7374 /* With bidi reordering, the character to display might not be the
7375 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7376 we were reseat()ed to a new buffer position, which is potentially
7377 a different paragraph. */
7378 if (it->bidi_p && it->bidi_it.first_elt)
7380 get_visually_first_element (it);
7381 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7384 if (IT_CHARPOS (*it) >= it->stop_charpos)
7386 if (IT_CHARPOS (*it) >= it->end_charpos)
7388 int overlay_strings_follow_p;
7390 /* End of the game, except when overlay strings follow that
7391 haven't been returned yet. */
7392 if (it->overlay_strings_at_end_processed_p)
7393 overlay_strings_follow_p = 0;
7394 else
7396 it->overlay_strings_at_end_processed_p = 1;
7397 overlay_strings_follow_p = get_overlay_strings (it, 0);
7400 if (overlay_strings_follow_p)
7401 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7402 else
7404 it->what = IT_EOB;
7405 it->position = it->current.pos;
7406 success_p = 0;
7409 else if (!(!it->bidi_p
7410 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7411 || IT_CHARPOS (*it) == it->stop_charpos))
7413 /* With bidi non-linear iteration, we could find ourselves
7414 far beyond the last computed stop_charpos, with several
7415 other stop positions in between that we missed. Scan
7416 them all now, in buffer's logical order, until we find
7417 and handle the last stop_charpos that precedes our
7418 current position. */
7419 handle_stop_backwards (it, it->stop_charpos);
7420 return GET_NEXT_DISPLAY_ELEMENT (it);
7422 else
7424 if (it->bidi_p)
7426 /* Take note of the stop position we just moved across,
7427 for when we will move back across it. */
7428 it->prev_stop = it->stop_charpos;
7429 /* If we are at base paragraph embedding level, take
7430 note of the last stop position seen at this
7431 level. */
7432 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7433 it->base_level_stop = it->stop_charpos;
7435 handle_stop (it);
7436 return GET_NEXT_DISPLAY_ELEMENT (it);
7439 else if (it->bidi_p
7440 /* If we are before prev_stop, we may have overstepped on
7441 our way backwards a stop_pos, and if so, we need to
7442 handle that stop_pos. */
7443 && IT_CHARPOS (*it) < it->prev_stop
7444 /* We can sometimes back up for reasons that have nothing
7445 to do with bidi reordering. E.g., compositions. The
7446 code below is only needed when we are above the base
7447 embedding level, so test for that explicitly. */
7448 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7450 if (it->base_level_stop <= 0
7451 || IT_CHARPOS (*it) < it->base_level_stop)
7453 /* If we lost track of base_level_stop, we need to find
7454 prev_stop by looking backwards. This happens, e.g., when
7455 we were reseated to the previous screenful of text by
7456 vertical-motion. */
7457 it->base_level_stop = BEGV;
7458 compute_stop_pos_backwards (it);
7459 handle_stop_backwards (it, it->prev_stop);
7461 else
7462 handle_stop_backwards (it, it->base_level_stop);
7463 return GET_NEXT_DISPLAY_ELEMENT (it);
7465 else
7467 /* No face changes, overlays etc. in sight, so just return a
7468 character from current_buffer. */
7469 unsigned char *p;
7470 EMACS_INT stop;
7472 /* Maybe run the redisplay end trigger hook. Performance note:
7473 This doesn't seem to cost measurable time. */
7474 if (it->redisplay_end_trigger_charpos
7475 && it->glyph_row
7476 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7477 run_redisplay_end_trigger_hook (it);
7479 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7480 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7481 stop)
7482 && next_element_from_composition (it))
7484 return 1;
7487 /* Get the next character, maybe multibyte. */
7488 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7489 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7490 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7491 else
7492 it->c = *p, it->len = 1;
7494 /* Record what we have and where it came from. */
7495 it->what = IT_CHARACTER;
7496 it->object = it->w->buffer;
7497 it->position = it->current.pos;
7499 /* Normally we return the character found above, except when we
7500 really want to return an ellipsis for selective display. */
7501 if (it->selective)
7503 if (it->c == '\n')
7505 /* A value of selective > 0 means hide lines indented more
7506 than that number of columns. */
7507 if (it->selective > 0
7508 && IT_CHARPOS (*it) + 1 < ZV
7509 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7510 IT_BYTEPOS (*it) + 1,
7511 it->selective))
7513 success_p = next_element_from_ellipsis (it);
7514 it->dpvec_char_len = -1;
7517 else if (it->c == '\r' && it->selective == -1)
7519 /* A value of selective == -1 means that everything from the
7520 CR to the end of the line is invisible, with maybe an
7521 ellipsis displayed for it. */
7522 success_p = next_element_from_ellipsis (it);
7523 it->dpvec_char_len = -1;
7528 /* Value is zero if end of buffer reached. */
7529 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7530 return success_p;
7534 /* Run the redisplay end trigger hook for IT. */
7536 static void
7537 run_redisplay_end_trigger_hook (struct it *it)
7539 Lisp_Object args[3];
7541 /* IT->glyph_row should be non-null, i.e. we should be actually
7542 displaying something, or otherwise we should not run the hook. */
7543 xassert (it->glyph_row);
7545 /* Set up hook arguments. */
7546 args[0] = Qredisplay_end_trigger_functions;
7547 args[1] = it->window;
7548 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7549 it->redisplay_end_trigger_charpos = 0;
7551 /* Since we are *trying* to run these functions, don't try to run
7552 them again, even if they get an error. */
7553 it->w->redisplay_end_trigger = Qnil;
7554 Frun_hook_with_args (3, args);
7556 /* Notice if it changed the face of the character we are on. */
7557 handle_face_prop (it);
7561 /* Deliver a composition display element. Unlike the other
7562 next_element_from_XXX, this function is not registered in the array
7563 get_next_element[]. It is called from next_element_from_buffer and
7564 next_element_from_string when necessary. */
7566 static int
7567 next_element_from_composition (struct it *it)
7569 it->what = IT_COMPOSITION;
7570 it->len = it->cmp_it.nbytes;
7571 if (STRINGP (it->string))
7573 if (it->c < 0)
7575 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7576 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7577 return 0;
7579 it->position = it->current.string_pos;
7580 it->object = it->string;
7581 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7582 IT_STRING_BYTEPOS (*it), it->string);
7584 else
7586 if (it->c < 0)
7588 IT_CHARPOS (*it) += it->cmp_it.nchars;
7589 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7590 if (it->bidi_p)
7592 if (it->bidi_it.new_paragraph)
7593 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7594 /* Resync the bidi iterator with IT's new position.
7595 FIXME: this doesn't support bidirectional text. */
7596 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7597 bidi_move_to_visually_next (&it->bidi_it);
7599 return 0;
7601 it->position = it->current.pos;
7602 it->object = it->w->buffer;
7603 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7604 IT_BYTEPOS (*it), Qnil);
7606 return 1;
7611 /***********************************************************************
7612 Moving an iterator without producing glyphs
7613 ***********************************************************************/
7615 /* Check if iterator is at a position corresponding to a valid buffer
7616 position after some move_it_ call. */
7618 #define IT_POS_VALID_AFTER_MOVE_P(it) \
7619 ((it)->method == GET_FROM_STRING \
7620 ? IT_STRING_CHARPOS (*it) == 0 \
7621 : 1)
7624 /* Move iterator IT to a specified buffer or X position within one
7625 line on the display without producing glyphs.
7627 OP should be a bit mask including some or all of these bits:
7628 MOVE_TO_X: Stop upon reaching x-position TO_X.
7629 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
7630 Regardless of OP's value, stop upon reaching the end of the display line.
7632 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
7633 This means, in particular, that TO_X includes window's horizontal
7634 scroll amount.
7636 The return value has several possible values that
7637 say what condition caused the scan to stop:
7639 MOVE_POS_MATCH_OR_ZV
7640 - when TO_POS or ZV was reached.
7642 MOVE_X_REACHED
7643 -when TO_X was reached before TO_POS or ZV were reached.
7645 MOVE_LINE_CONTINUED
7646 - when we reached the end of the display area and the line must
7647 be continued.
7649 MOVE_LINE_TRUNCATED
7650 - when we reached the end of the display area and the line is
7651 truncated.
7653 MOVE_NEWLINE_OR_CR
7654 - when we stopped at a line end, i.e. a newline or a CR and selective
7655 display is on. */
7657 static enum move_it_result
7658 move_it_in_display_line_to (struct it *it,
7659 EMACS_INT to_charpos, int to_x,
7660 enum move_operation_enum op)
7662 enum move_it_result result = MOVE_UNDEFINED;
7663 struct glyph_row *saved_glyph_row;
7664 struct it wrap_it, atpos_it, atx_it, ppos_it;
7665 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
7666 void *ppos_data = NULL;
7667 int may_wrap = 0;
7668 enum it_method prev_method = it->method;
7669 EMACS_INT prev_pos = IT_CHARPOS (*it);
7670 int saw_smaller_pos = prev_pos < to_charpos;
7672 /* Don't produce glyphs in produce_glyphs. */
7673 saved_glyph_row = it->glyph_row;
7674 it->glyph_row = NULL;
7676 /* Use wrap_it to save a copy of IT wherever a word wrap could
7677 occur. Use atpos_it to save a copy of IT at the desired buffer
7678 position, if found, so that we can scan ahead and check if the
7679 word later overshoots the window edge. Use atx_it similarly, for
7680 pixel positions. */
7681 wrap_it.sp = -1;
7682 atpos_it.sp = -1;
7683 atx_it.sp = -1;
7685 /* Use ppos_it under bidi reordering to save a copy of IT for the
7686 position > CHARPOS that is the closest to CHARPOS. We restore
7687 that position in IT when we have scanned the entire display line
7688 without finding a match for CHARPOS and all the character
7689 positions are greater than CHARPOS. */
7690 if (it->bidi_p)
7692 SAVE_IT (ppos_it, *it, ppos_data);
7693 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
7694 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
7695 SAVE_IT (ppos_it, *it, ppos_data);
7698 #define BUFFER_POS_REACHED_P() \
7699 ((op & MOVE_TO_POS) != 0 \
7700 && BUFFERP (it->object) \
7701 && (IT_CHARPOS (*it) == to_charpos \
7702 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos) \
7703 || (it->what == IT_COMPOSITION \
7704 && ((IT_CHARPOS (*it) > to_charpos \
7705 && to_charpos >= it->cmp_it.charpos) \
7706 || (IT_CHARPOS (*it) < to_charpos \
7707 && to_charpos <= it->cmp_it.charpos)))) \
7708 && (it->method == GET_FROM_BUFFER \
7709 || (it->method == GET_FROM_DISPLAY_VECTOR \
7710 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
7712 /* If there's a line-/wrap-prefix, handle it. */
7713 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7714 && it->current_y < it->last_visible_y)
7715 handle_line_prefix (it);
7717 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7718 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7720 while (1)
7722 int x, i, ascent = 0, descent = 0;
7724 /* Utility macro to reset an iterator with x, ascent, and descent. */
7725 #define IT_RESET_X_ASCENT_DESCENT(IT) \
7726 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
7727 (IT)->max_descent = descent)
7729 /* Stop if we move beyond TO_CHARPOS (after an image or a
7730 display string or stretch glyph). */
7731 if ((op & MOVE_TO_POS) != 0
7732 && BUFFERP (it->object)
7733 && it->method == GET_FROM_BUFFER
7734 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
7735 || (it->bidi_p
7736 && (prev_method == GET_FROM_IMAGE
7737 || prev_method == GET_FROM_STRETCH
7738 || prev_method == GET_FROM_STRING)
7739 /* Passed TO_CHARPOS from left to right. */
7740 && ((prev_pos < to_charpos
7741 && IT_CHARPOS (*it) > to_charpos)
7742 /* Passed TO_CHARPOS from right to left. */
7743 || (prev_pos > to_charpos
7744 && IT_CHARPOS (*it) < to_charpos)))))
7746 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7748 result = MOVE_POS_MATCH_OR_ZV;
7749 break;
7751 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7752 /* If wrap_it is valid, the current position might be in a
7753 word that is wrapped. So, save the iterator in
7754 atpos_it and continue to see if wrapping happens. */
7755 SAVE_IT (atpos_it, *it, atpos_data);
7758 /* Stop when ZV reached.
7759 We used to stop here when TO_CHARPOS reached as well, but that is
7760 too soon if this glyph does not fit on this line. So we handle it
7761 explicitly below. */
7762 if (!get_next_display_element (it))
7764 result = MOVE_POS_MATCH_OR_ZV;
7765 break;
7768 if (it->line_wrap == TRUNCATE)
7770 if (BUFFER_POS_REACHED_P ())
7772 result = MOVE_POS_MATCH_OR_ZV;
7773 break;
7776 else
7778 if (it->line_wrap == WORD_WRAP)
7780 if (IT_DISPLAYING_WHITESPACE (it))
7781 may_wrap = 1;
7782 else if (may_wrap)
7784 /* We have reached a glyph that follows one or more
7785 whitespace characters. If the position is
7786 already found, we are done. */
7787 if (atpos_it.sp >= 0)
7789 RESTORE_IT (it, &atpos_it, atpos_data);
7790 result = MOVE_POS_MATCH_OR_ZV;
7791 goto done;
7793 if (atx_it.sp >= 0)
7795 RESTORE_IT (it, &atx_it, atx_data);
7796 result = MOVE_X_REACHED;
7797 goto done;
7799 /* Otherwise, we can wrap here. */
7800 SAVE_IT (wrap_it, *it, wrap_data);
7801 may_wrap = 0;
7806 /* Remember the line height for the current line, in case
7807 the next element doesn't fit on the line. */
7808 ascent = it->max_ascent;
7809 descent = it->max_descent;
7811 /* The call to produce_glyphs will get the metrics of the
7812 display element IT is loaded with. Record the x-position
7813 before this display element, in case it doesn't fit on the
7814 line. */
7815 x = it->current_x;
7817 PRODUCE_GLYPHS (it);
7819 if (it->area != TEXT_AREA)
7821 prev_method = it->method;
7822 if (it->method == GET_FROM_BUFFER)
7823 prev_pos = IT_CHARPOS (*it);
7824 set_iterator_to_next (it, 1);
7825 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7826 SET_TEXT_POS (this_line_min_pos,
7827 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7828 if (it->bidi_p
7829 && (op & MOVE_TO_POS)
7830 && IT_CHARPOS (*it) > to_charpos
7831 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
7832 SAVE_IT (ppos_it, *it, ppos_data);
7833 continue;
7836 /* The number of glyphs we get back in IT->nglyphs will normally
7837 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7838 character on a terminal frame, or (iii) a line end. For the
7839 second case, IT->nglyphs - 1 padding glyphs will be present.
7840 (On X frames, there is only one glyph produced for a
7841 composite character.)
7843 The behavior implemented below means, for continuation lines,
7844 that as many spaces of a TAB as fit on the current line are
7845 displayed there. For terminal frames, as many glyphs of a
7846 multi-glyph character are displayed in the current line, too.
7847 This is what the old redisplay code did, and we keep it that
7848 way. Under X, the whole shape of a complex character must
7849 fit on the line or it will be completely displayed in the
7850 next line.
7852 Note that both for tabs and padding glyphs, all glyphs have
7853 the same width. */
7854 if (it->nglyphs)
7856 /* More than one glyph or glyph doesn't fit on line. All
7857 glyphs have the same width. */
7858 int single_glyph_width = it->pixel_width / it->nglyphs;
7859 int new_x;
7860 int x_before_this_char = x;
7861 int hpos_before_this_char = it->hpos;
7863 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7865 new_x = x + single_glyph_width;
7867 /* We want to leave anything reaching TO_X to the caller. */
7868 if ((op & MOVE_TO_X) && new_x > to_x)
7870 if (BUFFER_POS_REACHED_P ())
7872 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7873 goto buffer_pos_reached;
7874 if (atpos_it.sp < 0)
7876 SAVE_IT (atpos_it, *it, atpos_data);
7877 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7880 else
7882 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7884 it->current_x = x;
7885 result = MOVE_X_REACHED;
7886 break;
7888 if (atx_it.sp < 0)
7890 SAVE_IT (atx_it, *it, atx_data);
7891 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7896 if (/* Lines are continued. */
7897 it->line_wrap != TRUNCATE
7898 && (/* And glyph doesn't fit on the line. */
7899 new_x > it->last_visible_x
7900 /* Or it fits exactly and we're on a window
7901 system frame. */
7902 || (new_x == it->last_visible_x
7903 && FRAME_WINDOW_P (it->f))))
7905 if (/* IT->hpos == 0 means the very first glyph
7906 doesn't fit on the line, e.g. a wide image. */
7907 it->hpos == 0
7908 || (new_x == it->last_visible_x
7909 && FRAME_WINDOW_P (it->f)))
7911 ++it->hpos;
7912 it->current_x = new_x;
7914 /* The character's last glyph just barely fits
7915 in this row. */
7916 if (i == it->nglyphs - 1)
7918 /* If this is the destination position,
7919 return a position *before* it in this row,
7920 now that we know it fits in this row. */
7921 if (BUFFER_POS_REACHED_P ())
7923 if (it->line_wrap != WORD_WRAP
7924 || wrap_it.sp < 0)
7926 it->hpos = hpos_before_this_char;
7927 it->current_x = x_before_this_char;
7928 result = MOVE_POS_MATCH_OR_ZV;
7929 break;
7931 if (it->line_wrap == WORD_WRAP
7932 && atpos_it.sp < 0)
7934 SAVE_IT (atpos_it, *it, atpos_data);
7935 atpos_it.current_x = x_before_this_char;
7936 atpos_it.hpos = hpos_before_this_char;
7940 prev_method = it->method;
7941 if (it->method == GET_FROM_BUFFER)
7942 prev_pos = IT_CHARPOS (*it);
7943 set_iterator_to_next (it, 1);
7944 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7945 SET_TEXT_POS (this_line_min_pos,
7946 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7947 /* On graphical terminals, newlines may
7948 "overflow" into the fringe if
7949 overflow-newline-into-fringe is non-nil.
7950 On text-only terminals, newlines may
7951 overflow into the last glyph on the
7952 display line.*/
7953 if (!FRAME_WINDOW_P (it->f)
7954 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7956 if (!get_next_display_element (it))
7958 result = MOVE_POS_MATCH_OR_ZV;
7959 break;
7961 if (BUFFER_POS_REACHED_P ())
7963 if (ITERATOR_AT_END_OF_LINE_P (it))
7964 result = MOVE_POS_MATCH_OR_ZV;
7965 else
7966 result = MOVE_LINE_CONTINUED;
7967 break;
7969 if (ITERATOR_AT_END_OF_LINE_P (it))
7971 result = MOVE_NEWLINE_OR_CR;
7972 break;
7977 else
7978 IT_RESET_X_ASCENT_DESCENT (it);
7980 if (wrap_it.sp >= 0)
7982 RESTORE_IT (it, &wrap_it, wrap_data);
7983 atpos_it.sp = -1;
7984 atx_it.sp = -1;
7987 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7988 IT_CHARPOS (*it)));
7989 result = MOVE_LINE_CONTINUED;
7990 break;
7993 if (BUFFER_POS_REACHED_P ())
7995 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7996 goto buffer_pos_reached;
7997 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7999 SAVE_IT (atpos_it, *it, atpos_data);
8000 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8004 if (new_x > it->first_visible_x)
8006 /* Glyph is visible. Increment number of glyphs that
8007 would be displayed. */
8008 ++it->hpos;
8012 if (result != MOVE_UNDEFINED)
8013 break;
8015 else if (BUFFER_POS_REACHED_P ())
8017 buffer_pos_reached:
8018 IT_RESET_X_ASCENT_DESCENT (it);
8019 result = MOVE_POS_MATCH_OR_ZV;
8020 break;
8022 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8024 /* Stop when TO_X specified and reached. This check is
8025 necessary here because of lines consisting of a line end,
8026 only. The line end will not produce any glyphs and we
8027 would never get MOVE_X_REACHED. */
8028 xassert (it->nglyphs == 0);
8029 result = MOVE_X_REACHED;
8030 break;
8033 /* Is this a line end? If yes, we're done. */
8034 if (ITERATOR_AT_END_OF_LINE_P (it))
8036 /* If we are past TO_CHARPOS, but never saw any character
8037 positions smaller than TO_CHARPOS, return
8038 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8039 did. */
8040 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8042 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8044 if (IT_CHARPOS (ppos_it) < ZV)
8046 RESTORE_IT (it, &ppos_it, ppos_data);
8047 result = MOVE_POS_MATCH_OR_ZV;
8049 else
8050 goto buffer_pos_reached;
8052 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8053 && IT_CHARPOS (*it) > to_charpos)
8054 goto buffer_pos_reached;
8055 else
8056 result = MOVE_NEWLINE_OR_CR;
8058 else
8059 result = MOVE_NEWLINE_OR_CR;
8060 break;
8063 prev_method = it->method;
8064 if (it->method == GET_FROM_BUFFER)
8065 prev_pos = IT_CHARPOS (*it);
8066 /* The current display element has been consumed. Advance
8067 to the next. */
8068 set_iterator_to_next (it, 1);
8069 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8070 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8071 if (IT_CHARPOS (*it) < to_charpos)
8072 saw_smaller_pos = 1;
8073 if (it->bidi_p
8074 && (op & MOVE_TO_POS)
8075 && IT_CHARPOS (*it) >= to_charpos
8076 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8077 SAVE_IT (ppos_it, *it, ppos_data);
8079 /* Stop if lines are truncated and IT's current x-position is
8080 past the right edge of the window now. */
8081 if (it->line_wrap == TRUNCATE
8082 && it->current_x >= it->last_visible_x)
8084 if (!FRAME_WINDOW_P (it->f)
8085 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8087 int at_eob_p = 0;
8089 if ((at_eob_p = !get_next_display_element (it))
8090 || BUFFER_POS_REACHED_P ()
8091 /* If we are past TO_CHARPOS, but never saw any
8092 character positions smaller than TO_CHARPOS,
8093 return MOVE_POS_MATCH_OR_ZV, like the
8094 unidirectional display did. */
8095 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8096 && !saw_smaller_pos
8097 && IT_CHARPOS (*it) > to_charpos))
8099 if (!at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8100 RESTORE_IT (it, &ppos_it, ppos_data);
8101 result = MOVE_POS_MATCH_OR_ZV;
8102 break;
8104 if (ITERATOR_AT_END_OF_LINE_P (it))
8106 result = MOVE_NEWLINE_OR_CR;
8107 break;
8110 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8111 && !saw_smaller_pos
8112 && IT_CHARPOS (*it) > to_charpos)
8114 if (IT_CHARPOS (ppos_it) < ZV)
8115 RESTORE_IT (it, &ppos_it, ppos_data);
8116 result = MOVE_POS_MATCH_OR_ZV;
8117 break;
8119 result = MOVE_LINE_TRUNCATED;
8120 break;
8122 #undef IT_RESET_X_ASCENT_DESCENT
8125 #undef BUFFER_POS_REACHED_P
8127 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8128 restore the saved iterator. */
8129 if (atpos_it.sp >= 0)
8130 RESTORE_IT (it, &atpos_it, atpos_data);
8131 else if (atx_it.sp >= 0)
8132 RESTORE_IT (it, &atx_it, atx_data);
8134 done:
8136 if (atpos_data)
8137 bidi_unshelve_cache (atpos_data, 1);
8138 if (atx_data)
8139 bidi_unshelve_cache (atx_data, 1);
8140 if (wrap_data)
8141 bidi_unshelve_cache (wrap_data, 1);
8142 if (ppos_data)
8143 bidi_unshelve_cache (ppos_data, 1);
8145 /* Restore the iterator settings altered at the beginning of this
8146 function. */
8147 it->glyph_row = saved_glyph_row;
8148 return result;
8151 /* For external use. */
8152 void
8153 move_it_in_display_line (struct it *it,
8154 EMACS_INT to_charpos, int to_x,
8155 enum move_operation_enum op)
8157 if (it->line_wrap == WORD_WRAP
8158 && (op & MOVE_TO_X))
8160 struct it save_it;
8161 void *save_data = NULL;
8162 int skip;
8164 SAVE_IT (save_it, *it, save_data);
8165 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8166 /* When word-wrap is on, TO_X may lie past the end
8167 of a wrapped line. Then it->current is the
8168 character on the next line, so backtrack to the
8169 space before the wrap point. */
8170 if (skip == MOVE_LINE_CONTINUED)
8172 int prev_x = max (it->current_x - 1, 0);
8173 RESTORE_IT (it, &save_it, save_data);
8174 move_it_in_display_line_to
8175 (it, -1, prev_x, MOVE_TO_X);
8177 else
8178 bidi_unshelve_cache (save_data, 1);
8180 else
8181 move_it_in_display_line_to (it, to_charpos, to_x, op);
8185 /* Move IT forward until it satisfies one or more of the criteria in
8186 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8188 OP is a bit-mask that specifies where to stop, and in particular,
8189 which of those four position arguments makes a difference. See the
8190 description of enum move_operation_enum.
8192 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8193 screen line, this function will set IT to the next position that is
8194 displayed to the right of TO_CHARPOS on the screen. */
8196 void
8197 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
8199 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8200 int line_height, line_start_x = 0, reached = 0;
8201 void *backup_data = NULL;
8203 for (;;)
8205 if (op & MOVE_TO_VPOS)
8207 /* If no TO_CHARPOS and no TO_X specified, stop at the
8208 start of the line TO_VPOS. */
8209 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8211 if (it->vpos == to_vpos)
8213 reached = 1;
8214 break;
8216 else
8217 skip = move_it_in_display_line_to (it, -1, -1, 0);
8219 else
8221 /* TO_VPOS >= 0 means stop at TO_X in the line at
8222 TO_VPOS, or at TO_POS, whichever comes first. */
8223 if (it->vpos == to_vpos)
8225 reached = 2;
8226 break;
8229 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8231 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8233 reached = 3;
8234 break;
8236 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8238 /* We have reached TO_X but not in the line we want. */
8239 skip = move_it_in_display_line_to (it, to_charpos,
8240 -1, MOVE_TO_POS);
8241 if (skip == MOVE_POS_MATCH_OR_ZV)
8243 reached = 4;
8244 break;
8249 else if (op & MOVE_TO_Y)
8251 struct it it_backup;
8253 if (it->line_wrap == WORD_WRAP)
8254 SAVE_IT (it_backup, *it, backup_data);
8256 /* TO_Y specified means stop at TO_X in the line containing
8257 TO_Y---or at TO_CHARPOS if this is reached first. The
8258 problem is that we can't really tell whether the line
8259 contains TO_Y before we have completely scanned it, and
8260 this may skip past TO_X. What we do is to first scan to
8261 TO_X.
8263 If TO_X is not specified, use a TO_X of zero. The reason
8264 is to make the outcome of this function more predictable.
8265 If we didn't use TO_X == 0, we would stop at the end of
8266 the line which is probably not what a caller would expect
8267 to happen. */
8268 skip = move_it_in_display_line_to
8269 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8270 (MOVE_TO_X | (op & MOVE_TO_POS)));
8272 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8273 if (skip == MOVE_POS_MATCH_OR_ZV)
8274 reached = 5;
8275 else if (skip == MOVE_X_REACHED)
8277 /* If TO_X was reached, we want to know whether TO_Y is
8278 in the line. We know this is the case if the already
8279 scanned glyphs make the line tall enough. Otherwise,
8280 we must check by scanning the rest of the line. */
8281 line_height = it->max_ascent + it->max_descent;
8282 if (to_y >= it->current_y
8283 && to_y < it->current_y + line_height)
8285 reached = 6;
8286 break;
8288 SAVE_IT (it_backup, *it, backup_data);
8289 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8290 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8291 op & MOVE_TO_POS);
8292 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8293 line_height = it->max_ascent + it->max_descent;
8294 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8296 if (to_y >= it->current_y
8297 && to_y < it->current_y + line_height)
8299 /* If TO_Y is in this line and TO_X was reached
8300 above, we scanned too far. We have to restore
8301 IT's settings to the ones before skipping. */
8302 RESTORE_IT (it, &it_backup, backup_data);
8303 reached = 6;
8305 else
8307 skip = skip2;
8308 if (skip == MOVE_POS_MATCH_OR_ZV)
8309 reached = 7;
8312 else
8314 /* Check whether TO_Y is in this line. */
8315 line_height = it->max_ascent + it->max_descent;
8316 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8318 if (to_y >= it->current_y
8319 && to_y < it->current_y + line_height)
8321 /* When word-wrap is on, TO_X may lie past the end
8322 of a wrapped line. Then it->current is the
8323 character on the next line, so backtrack to the
8324 space before the wrap point. */
8325 if (skip == MOVE_LINE_CONTINUED
8326 && it->line_wrap == WORD_WRAP)
8328 int prev_x = max (it->current_x - 1, 0);
8329 RESTORE_IT (it, &it_backup, backup_data);
8330 skip = move_it_in_display_line_to
8331 (it, -1, prev_x, MOVE_TO_X);
8333 reached = 6;
8337 if (reached)
8338 break;
8340 else if (BUFFERP (it->object)
8341 && (it->method == GET_FROM_BUFFER
8342 || it->method == GET_FROM_STRETCH)
8343 && IT_CHARPOS (*it) >= to_charpos)
8344 skip = MOVE_POS_MATCH_OR_ZV;
8345 else
8346 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8348 switch (skip)
8350 case MOVE_POS_MATCH_OR_ZV:
8351 reached = 8;
8352 goto out;
8354 case MOVE_NEWLINE_OR_CR:
8355 set_iterator_to_next (it, 1);
8356 it->continuation_lines_width = 0;
8357 break;
8359 case MOVE_LINE_TRUNCATED:
8360 it->continuation_lines_width = 0;
8361 reseat_at_next_visible_line_start (it, 0);
8362 if ((op & MOVE_TO_POS) != 0
8363 && IT_CHARPOS (*it) > to_charpos)
8365 reached = 9;
8366 goto out;
8368 break;
8370 case MOVE_LINE_CONTINUED:
8371 /* For continued lines ending in a tab, some of the glyphs
8372 associated with the tab are displayed on the current
8373 line. Since it->current_x does not include these glyphs,
8374 we use it->last_visible_x instead. */
8375 if (it->c == '\t')
8377 it->continuation_lines_width += it->last_visible_x;
8378 /* When moving by vpos, ensure that the iterator really
8379 advances to the next line (bug#847, bug#969). Fixme:
8380 do we need to do this in other circumstances? */
8381 if (it->current_x != it->last_visible_x
8382 && (op & MOVE_TO_VPOS)
8383 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8385 line_start_x = it->current_x + it->pixel_width
8386 - it->last_visible_x;
8387 set_iterator_to_next (it, 0);
8390 else
8391 it->continuation_lines_width += it->current_x;
8392 break;
8394 default:
8395 abort ();
8398 /* Reset/increment for the next run. */
8399 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8400 it->current_x = line_start_x;
8401 line_start_x = 0;
8402 it->hpos = 0;
8403 it->current_y += it->max_ascent + it->max_descent;
8404 ++it->vpos;
8405 last_height = it->max_ascent + it->max_descent;
8406 last_max_ascent = it->max_ascent;
8407 it->max_ascent = it->max_descent = 0;
8410 out:
8412 /* On text terminals, we may stop at the end of a line in the middle
8413 of a multi-character glyph. If the glyph itself is continued,
8414 i.e. it is actually displayed on the next line, don't treat this
8415 stopping point as valid; move to the next line instead (unless
8416 that brings us offscreen). */
8417 if (!FRAME_WINDOW_P (it->f)
8418 && op & MOVE_TO_POS
8419 && IT_CHARPOS (*it) == to_charpos
8420 && it->what == IT_CHARACTER
8421 && it->nglyphs > 1
8422 && it->line_wrap == WINDOW_WRAP
8423 && it->current_x == it->last_visible_x - 1
8424 && it->c != '\n'
8425 && it->c != '\t'
8426 && it->vpos < XFASTINT (it->w->window_end_vpos))
8428 it->continuation_lines_width += it->current_x;
8429 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8430 it->current_y += it->max_ascent + it->max_descent;
8431 ++it->vpos;
8432 last_height = it->max_ascent + it->max_descent;
8433 last_max_ascent = it->max_ascent;
8436 if (backup_data)
8437 bidi_unshelve_cache (backup_data, 1);
8439 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8443 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8445 If DY > 0, move IT backward at least that many pixels. DY = 0
8446 means move IT backward to the preceding line start or BEGV. This
8447 function may move over more than DY pixels if IT->current_y - DY
8448 ends up in the middle of a line; in this case IT->current_y will be
8449 set to the top of the line moved to. */
8451 void
8452 move_it_vertically_backward (struct it *it, int dy)
8454 int nlines, h;
8455 struct it it2, it3;
8456 void *it2data = NULL, *it3data = NULL;
8457 EMACS_INT start_pos;
8459 move_further_back:
8460 xassert (dy >= 0);
8462 start_pos = IT_CHARPOS (*it);
8464 /* Estimate how many newlines we must move back. */
8465 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8467 /* Set the iterator's position that many lines back. */
8468 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8469 back_to_previous_visible_line_start (it);
8471 /* Reseat the iterator here. When moving backward, we don't want
8472 reseat to skip forward over invisible text, set up the iterator
8473 to deliver from overlay strings at the new position etc. So,
8474 use reseat_1 here. */
8475 reseat_1 (it, it->current.pos, 1);
8477 /* We are now surely at a line start. */
8478 it->current_x = it->hpos = 0;
8479 it->continuation_lines_width = 0;
8481 /* Move forward and see what y-distance we moved. First move to the
8482 start of the next line so that we get its height. We need this
8483 height to be able to tell whether we reached the specified
8484 y-distance. */
8485 SAVE_IT (it2, *it, it2data);
8486 it2.max_ascent = it2.max_descent = 0;
8489 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8490 MOVE_TO_POS | MOVE_TO_VPOS);
8492 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
8493 xassert (IT_CHARPOS (*it) >= BEGV);
8494 SAVE_IT (it3, it2, it3data);
8496 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8497 xassert (IT_CHARPOS (*it) >= BEGV);
8498 /* H is the actual vertical distance from the position in *IT
8499 and the starting position. */
8500 h = it2.current_y - it->current_y;
8501 /* NLINES is the distance in number of lines. */
8502 nlines = it2.vpos - it->vpos;
8504 /* Correct IT's y and vpos position
8505 so that they are relative to the starting point. */
8506 it->vpos -= nlines;
8507 it->current_y -= h;
8509 if (dy == 0)
8511 /* DY == 0 means move to the start of the screen line. The
8512 value of nlines is > 0 if continuation lines were involved. */
8513 RESTORE_IT (it, it, it2data);
8514 if (nlines > 0)
8515 move_it_by_lines (it, nlines);
8516 bidi_unshelve_cache (it3data, 1);
8518 else
8520 /* The y-position we try to reach, relative to *IT.
8521 Note that H has been subtracted in front of the if-statement. */
8522 int target_y = it->current_y + h - dy;
8523 int y0 = it3.current_y;
8524 int y1;
8525 int line_height;
8527 RESTORE_IT (&it3, &it3, it3data);
8528 y1 = line_bottom_y (&it3);
8529 line_height = y1 - y0;
8530 RESTORE_IT (it, it, it2data);
8531 /* If we did not reach target_y, try to move further backward if
8532 we can. If we moved too far backward, try to move forward. */
8533 if (target_y < it->current_y
8534 /* This is heuristic. In a window that's 3 lines high, with
8535 a line height of 13 pixels each, recentering with point
8536 on the bottom line will try to move -39/2 = 19 pixels
8537 backward. Try to avoid moving into the first line. */
8538 && (it->current_y - target_y
8539 > min (window_box_height (it->w), line_height * 2 / 3))
8540 && IT_CHARPOS (*it) > BEGV)
8542 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8543 target_y - it->current_y));
8544 dy = it->current_y - target_y;
8545 goto move_further_back;
8547 else if (target_y >= it->current_y + line_height
8548 && IT_CHARPOS (*it) < ZV)
8550 /* Should move forward by at least one line, maybe more.
8552 Note: Calling move_it_by_lines can be expensive on
8553 terminal frames, where compute_motion is used (via
8554 vmotion) to do the job, when there are very long lines
8555 and truncate-lines is nil. That's the reason for
8556 treating terminal frames specially here. */
8558 if (!FRAME_WINDOW_P (it->f))
8559 move_it_vertically (it, target_y - (it->current_y + line_height));
8560 else
8564 move_it_by_lines (it, 1);
8566 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
8573 /* Move IT by a specified amount of pixel lines DY. DY negative means
8574 move backwards. DY = 0 means move to start of screen line. At the
8575 end, IT will be on the start of a screen line. */
8577 void
8578 move_it_vertically (struct it *it, int dy)
8580 if (dy <= 0)
8581 move_it_vertically_backward (it, -dy);
8582 else
8584 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
8585 move_it_to (it, ZV, -1, it->current_y + dy, -1,
8586 MOVE_TO_POS | MOVE_TO_Y);
8587 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
8589 /* If buffer ends in ZV without a newline, move to the start of
8590 the line to satisfy the post-condition. */
8591 if (IT_CHARPOS (*it) == ZV
8592 && ZV > BEGV
8593 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8594 move_it_by_lines (it, 0);
8599 /* Move iterator IT past the end of the text line it is in. */
8601 void
8602 move_it_past_eol (struct it *it)
8604 enum move_it_result rc;
8606 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
8607 if (rc == MOVE_NEWLINE_OR_CR)
8608 set_iterator_to_next (it, 0);
8612 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
8613 negative means move up. DVPOS == 0 means move to the start of the
8614 screen line.
8616 Optimization idea: If we would know that IT->f doesn't use
8617 a face with proportional font, we could be faster for
8618 truncate-lines nil. */
8620 void
8621 move_it_by_lines (struct it *it, int dvpos)
8624 /* The commented-out optimization uses vmotion on terminals. This
8625 gives bad results, because elements like it->what, on which
8626 callers such as pos_visible_p rely, aren't updated. */
8627 /* struct position pos;
8628 if (!FRAME_WINDOW_P (it->f))
8630 struct text_pos textpos;
8632 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
8633 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
8634 reseat (it, textpos, 1);
8635 it->vpos += pos.vpos;
8636 it->current_y += pos.vpos;
8638 else */
8640 if (dvpos == 0)
8642 /* DVPOS == 0 means move to the start of the screen line. */
8643 move_it_vertically_backward (it, 0);
8644 xassert (it->current_x == 0 && it->hpos == 0);
8645 /* Let next call to line_bottom_y calculate real line height */
8646 last_height = 0;
8648 else if (dvpos > 0)
8650 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
8651 if (!IT_POS_VALID_AFTER_MOVE_P (it))
8652 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
8654 else
8656 struct it it2;
8657 void *it2data = NULL;
8658 EMACS_INT start_charpos, i;
8660 /* Start at the beginning of the screen line containing IT's
8661 position. This may actually move vertically backwards,
8662 in case of overlays, so adjust dvpos accordingly. */
8663 dvpos += it->vpos;
8664 move_it_vertically_backward (it, 0);
8665 dvpos -= it->vpos;
8667 /* Go back -DVPOS visible lines and reseat the iterator there. */
8668 start_charpos = IT_CHARPOS (*it);
8669 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
8670 back_to_previous_visible_line_start (it);
8671 reseat (it, it->current.pos, 1);
8673 /* Move further back if we end up in a string or an image. */
8674 while (!IT_POS_VALID_AFTER_MOVE_P (it))
8676 /* First try to move to start of display line. */
8677 dvpos += it->vpos;
8678 move_it_vertically_backward (it, 0);
8679 dvpos -= it->vpos;
8680 if (IT_POS_VALID_AFTER_MOVE_P (it))
8681 break;
8682 /* If start of line is still in string or image,
8683 move further back. */
8684 back_to_previous_visible_line_start (it);
8685 reseat (it, it->current.pos, 1);
8686 dvpos--;
8689 it->current_x = it->hpos = 0;
8691 /* Above call may have moved too far if continuation lines
8692 are involved. Scan forward and see if it did. */
8693 SAVE_IT (it2, *it, it2data);
8694 it2.vpos = it2.current_y = 0;
8695 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
8696 it->vpos -= it2.vpos;
8697 it->current_y -= it2.current_y;
8698 it->current_x = it->hpos = 0;
8700 /* If we moved too far back, move IT some lines forward. */
8701 if (it2.vpos > -dvpos)
8703 int delta = it2.vpos + dvpos;
8705 RESTORE_IT (&it2, &it2, it2data);
8706 SAVE_IT (it2, *it, it2data);
8707 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
8708 /* Move back again if we got too far ahead. */
8709 if (IT_CHARPOS (*it) >= start_charpos)
8710 RESTORE_IT (it, &it2, it2data);
8711 else
8712 bidi_unshelve_cache (it2data, 1);
8714 else
8715 RESTORE_IT (it, it, it2data);
8719 /* Return 1 if IT points into the middle of a display vector. */
8722 in_display_vector_p (struct it *it)
8724 return (it->method == GET_FROM_DISPLAY_VECTOR
8725 && it->current.dpvec_index > 0
8726 && it->dpvec + it->current.dpvec_index != it->dpend);
8730 /***********************************************************************
8731 Messages
8732 ***********************************************************************/
8735 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
8736 to *Messages*. */
8738 void
8739 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
8741 Lisp_Object args[3];
8742 Lisp_Object msg, fmt;
8743 char *buffer;
8744 EMACS_INT len;
8745 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
8746 USE_SAFE_ALLOCA;
8748 /* Do nothing if called asynchronously. Inserting text into
8749 a buffer may call after-change-functions and alike and
8750 that would means running Lisp asynchronously. */
8751 if (handling_signal)
8752 return;
8754 fmt = msg = Qnil;
8755 GCPRO4 (fmt, msg, arg1, arg2);
8757 args[0] = fmt = build_string (format);
8758 args[1] = arg1;
8759 args[2] = arg2;
8760 msg = Fformat (3, args);
8762 len = SBYTES (msg) + 1;
8763 SAFE_ALLOCA (buffer, char *, len);
8764 memcpy (buffer, SDATA (msg), len);
8766 message_dolog (buffer, len - 1, 1, 0);
8767 SAFE_FREE ();
8769 UNGCPRO;
8773 /* Output a newline in the *Messages* buffer if "needs" one. */
8775 void
8776 message_log_maybe_newline (void)
8778 if (message_log_need_newline)
8779 message_dolog ("", 0, 1, 0);
8783 /* Add a string M of length NBYTES to the message log, optionally
8784 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
8785 nonzero, means interpret the contents of M as multibyte. This
8786 function calls low-level routines in order to bypass text property
8787 hooks, etc. which might not be safe to run.
8789 This may GC (insert may run before/after change hooks),
8790 so the buffer M must NOT point to a Lisp string. */
8792 void
8793 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
8795 const unsigned char *msg = (const unsigned char *) m;
8797 if (!NILP (Vmemory_full))
8798 return;
8800 if (!NILP (Vmessage_log_max))
8802 struct buffer *oldbuf;
8803 Lisp_Object oldpoint, oldbegv, oldzv;
8804 int old_windows_or_buffers_changed = windows_or_buffers_changed;
8805 EMACS_INT point_at_end = 0;
8806 EMACS_INT zv_at_end = 0;
8807 Lisp_Object old_deactivate_mark, tem;
8808 struct gcpro gcpro1;
8810 old_deactivate_mark = Vdeactivate_mark;
8811 oldbuf = current_buffer;
8812 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
8813 BVAR (current_buffer, undo_list) = Qt;
8815 oldpoint = message_dolog_marker1;
8816 set_marker_restricted (oldpoint, make_number (PT), Qnil);
8817 oldbegv = message_dolog_marker2;
8818 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
8819 oldzv = message_dolog_marker3;
8820 set_marker_restricted (oldzv, make_number (ZV), Qnil);
8821 GCPRO1 (old_deactivate_mark);
8823 if (PT == Z)
8824 point_at_end = 1;
8825 if (ZV == Z)
8826 zv_at_end = 1;
8828 BEGV = BEG;
8829 BEGV_BYTE = BEG_BYTE;
8830 ZV = Z;
8831 ZV_BYTE = Z_BYTE;
8832 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8834 /* Insert the string--maybe converting multibyte to single byte
8835 or vice versa, so that all the text fits the buffer. */
8836 if (multibyte
8837 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
8839 EMACS_INT i;
8840 int c, char_bytes;
8841 char work[1];
8843 /* Convert a multibyte string to single-byte
8844 for the *Message* buffer. */
8845 for (i = 0; i < nbytes; i += char_bytes)
8847 c = string_char_and_length (msg + i, &char_bytes);
8848 work[0] = (ASCII_CHAR_P (c)
8850 : multibyte_char_to_unibyte (c));
8851 insert_1_both (work, 1, 1, 1, 0, 0);
8854 else if (! multibyte
8855 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
8857 EMACS_INT i;
8858 int c, char_bytes;
8859 unsigned char str[MAX_MULTIBYTE_LENGTH];
8860 /* Convert a single-byte string to multibyte
8861 for the *Message* buffer. */
8862 for (i = 0; i < nbytes; i++)
8864 c = msg[i];
8865 MAKE_CHAR_MULTIBYTE (c);
8866 char_bytes = CHAR_STRING (c, str);
8867 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
8870 else if (nbytes)
8871 insert_1 (m, nbytes, 1, 0, 0);
8873 if (nlflag)
8875 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
8876 printmax_t dups;
8877 insert_1 ("\n", 1, 1, 0, 0);
8879 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8880 this_bol = PT;
8881 this_bol_byte = PT_BYTE;
8883 /* See if this line duplicates the previous one.
8884 If so, combine duplicates. */
8885 if (this_bol > BEG)
8887 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8888 prev_bol = PT;
8889 prev_bol_byte = PT_BYTE;
8891 dups = message_log_check_duplicate (prev_bol_byte,
8892 this_bol_byte);
8893 if (dups)
8895 del_range_both (prev_bol, prev_bol_byte,
8896 this_bol, this_bol_byte, 0);
8897 if (dups > 1)
8899 char dupstr[sizeof " [ times]"
8900 + INT_STRLEN_BOUND (printmax_t)];
8901 int duplen;
8903 /* If you change this format, don't forget to also
8904 change message_log_check_duplicate. */
8905 sprintf (dupstr, " [%"pMd" times]", dups);
8906 duplen = strlen (dupstr);
8907 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8908 insert_1 (dupstr, duplen, 1, 0, 1);
8913 /* If we have more than the desired maximum number of lines
8914 in the *Messages* buffer now, delete the oldest ones.
8915 This is safe because we don't have undo in this buffer. */
8917 if (NATNUMP (Vmessage_log_max))
8919 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8920 -XFASTINT (Vmessage_log_max) - 1, 0);
8921 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8924 BEGV = XMARKER (oldbegv)->charpos;
8925 BEGV_BYTE = marker_byte_position (oldbegv);
8927 if (zv_at_end)
8929 ZV = Z;
8930 ZV_BYTE = Z_BYTE;
8932 else
8934 ZV = XMARKER (oldzv)->charpos;
8935 ZV_BYTE = marker_byte_position (oldzv);
8938 if (point_at_end)
8939 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8940 else
8941 /* We can't do Fgoto_char (oldpoint) because it will run some
8942 Lisp code. */
8943 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8944 XMARKER (oldpoint)->bytepos);
8946 UNGCPRO;
8947 unchain_marker (XMARKER (oldpoint));
8948 unchain_marker (XMARKER (oldbegv));
8949 unchain_marker (XMARKER (oldzv));
8951 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8952 set_buffer_internal (oldbuf);
8953 if (NILP (tem))
8954 windows_or_buffers_changed = old_windows_or_buffers_changed;
8955 message_log_need_newline = !nlflag;
8956 Vdeactivate_mark = old_deactivate_mark;
8961 /* We are at the end of the buffer after just having inserted a newline.
8962 (Note: We depend on the fact we won't be crossing the gap.)
8963 Check to see if the most recent message looks a lot like the previous one.
8964 Return 0 if different, 1 if the new one should just replace it, or a
8965 value N > 1 if we should also append " [N times]". */
8967 static intmax_t
8968 message_log_check_duplicate (EMACS_INT prev_bol_byte, EMACS_INT this_bol_byte)
8970 EMACS_INT i;
8971 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
8972 int seen_dots = 0;
8973 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8974 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8976 for (i = 0; i < len; i++)
8978 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8979 seen_dots = 1;
8980 if (p1[i] != p2[i])
8981 return seen_dots;
8983 p1 += len;
8984 if (*p1 == '\n')
8985 return 2;
8986 if (*p1++ == ' ' && *p1++ == '[')
8988 char *pend;
8989 intmax_t n = strtoimax ((char *) p1, &pend, 10);
8990 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
8991 return n+1;
8993 return 0;
8997 /* Display an echo area message M with a specified length of NBYTES
8998 bytes. The string may include null characters. If M is 0, clear
8999 out any existing message, and let the mini-buffer text show
9000 through.
9002 This may GC, so the buffer M must NOT point to a Lisp string. */
9004 void
9005 message2 (const char *m, EMACS_INT nbytes, int multibyte)
9007 /* First flush out any partial line written with print. */
9008 message_log_maybe_newline ();
9009 if (m)
9010 message_dolog (m, nbytes, 1, multibyte);
9011 message2_nolog (m, nbytes, multibyte);
9015 /* The non-logging counterpart of message2. */
9017 void
9018 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
9020 struct frame *sf = SELECTED_FRAME ();
9021 message_enable_multibyte = multibyte;
9023 if (FRAME_INITIAL_P (sf))
9025 if (noninteractive_need_newline)
9026 putc ('\n', stderr);
9027 noninteractive_need_newline = 0;
9028 if (m)
9029 fwrite (m, nbytes, 1, stderr);
9030 if (cursor_in_echo_area == 0)
9031 fprintf (stderr, "\n");
9032 fflush (stderr);
9034 /* A null message buffer means that the frame hasn't really been
9035 initialized yet. Error messages get reported properly by
9036 cmd_error, so this must be just an informative message; toss it. */
9037 else if (INTERACTIVE
9038 && sf->glyphs_initialized_p
9039 && FRAME_MESSAGE_BUF (sf))
9041 Lisp_Object mini_window;
9042 struct frame *f;
9044 /* Get the frame containing the mini-buffer
9045 that the selected frame is using. */
9046 mini_window = FRAME_MINIBUF_WINDOW (sf);
9047 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9049 FRAME_SAMPLE_VISIBILITY (f);
9050 if (FRAME_VISIBLE_P (sf)
9051 && ! FRAME_VISIBLE_P (f))
9052 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9054 if (m)
9056 set_message (m, Qnil, nbytes, multibyte);
9057 if (minibuffer_auto_raise)
9058 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9060 else
9061 clear_message (1, 1);
9063 do_pending_window_change (0);
9064 echo_area_display (1);
9065 do_pending_window_change (0);
9066 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9067 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9072 /* Display an echo area message M with a specified length of NBYTES
9073 bytes. The string may include null characters. If M is not a
9074 string, clear out any existing message, and let the mini-buffer
9075 text show through.
9077 This function cancels echoing. */
9079 void
9080 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9082 struct gcpro gcpro1;
9084 GCPRO1 (m);
9085 clear_message (1,1);
9086 cancel_echoing ();
9088 /* First flush out any partial line written with print. */
9089 message_log_maybe_newline ();
9090 if (STRINGP (m))
9092 char *buffer;
9093 USE_SAFE_ALLOCA;
9095 SAFE_ALLOCA (buffer, char *, nbytes);
9096 memcpy (buffer, SDATA (m), nbytes);
9097 message_dolog (buffer, nbytes, 1, multibyte);
9098 SAFE_FREE ();
9100 message3_nolog (m, nbytes, multibyte);
9102 UNGCPRO;
9106 /* The non-logging version of message3.
9107 This does not cancel echoing, because it is used for echoing.
9108 Perhaps we need to make a separate function for echoing
9109 and make this cancel echoing. */
9111 void
9112 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9114 struct frame *sf = SELECTED_FRAME ();
9115 message_enable_multibyte = multibyte;
9117 if (FRAME_INITIAL_P (sf))
9119 if (noninteractive_need_newline)
9120 putc ('\n', stderr);
9121 noninteractive_need_newline = 0;
9122 if (STRINGP (m))
9123 fwrite (SDATA (m), nbytes, 1, stderr);
9124 if (cursor_in_echo_area == 0)
9125 fprintf (stderr, "\n");
9126 fflush (stderr);
9128 /* A null message buffer means that the frame hasn't really been
9129 initialized yet. Error messages get reported properly by
9130 cmd_error, so this must be just an informative message; toss it. */
9131 else if (INTERACTIVE
9132 && sf->glyphs_initialized_p
9133 && FRAME_MESSAGE_BUF (sf))
9135 Lisp_Object mini_window;
9136 Lisp_Object frame;
9137 struct frame *f;
9139 /* Get the frame containing the mini-buffer
9140 that the selected frame is using. */
9141 mini_window = FRAME_MINIBUF_WINDOW (sf);
9142 frame = XWINDOW (mini_window)->frame;
9143 f = XFRAME (frame);
9145 FRAME_SAMPLE_VISIBILITY (f);
9146 if (FRAME_VISIBLE_P (sf)
9147 && !FRAME_VISIBLE_P (f))
9148 Fmake_frame_visible (frame);
9150 if (STRINGP (m) && SCHARS (m) > 0)
9152 set_message (NULL, m, nbytes, multibyte);
9153 if (minibuffer_auto_raise)
9154 Fraise_frame (frame);
9155 /* Assume we are not echoing.
9156 (If we are, echo_now will override this.) */
9157 echo_message_buffer = Qnil;
9159 else
9160 clear_message (1, 1);
9162 do_pending_window_change (0);
9163 echo_area_display (1);
9164 do_pending_window_change (0);
9165 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9166 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9171 /* Display a null-terminated echo area message M. If M is 0, clear
9172 out any existing message, and let the mini-buffer text show through.
9174 The buffer M must continue to exist until after the echo area gets
9175 cleared or some other message gets displayed there. Do not pass
9176 text that is stored in a Lisp string. Do not pass text in a buffer
9177 that was alloca'd. */
9179 void
9180 message1 (const char *m)
9182 message2 (m, (m ? strlen (m) : 0), 0);
9186 /* The non-logging counterpart of message1. */
9188 void
9189 message1_nolog (const char *m)
9191 message2_nolog (m, (m ? strlen (m) : 0), 0);
9194 /* Display a message M which contains a single %s
9195 which gets replaced with STRING. */
9197 void
9198 message_with_string (const char *m, Lisp_Object string, int log)
9200 CHECK_STRING (string);
9202 if (noninteractive)
9204 if (m)
9206 if (noninteractive_need_newline)
9207 putc ('\n', stderr);
9208 noninteractive_need_newline = 0;
9209 fprintf (stderr, m, SDATA (string));
9210 if (!cursor_in_echo_area)
9211 fprintf (stderr, "\n");
9212 fflush (stderr);
9215 else if (INTERACTIVE)
9217 /* The frame whose minibuffer we're going to display the message on.
9218 It may be larger than the selected frame, so we need
9219 to use its buffer, not the selected frame's buffer. */
9220 Lisp_Object mini_window;
9221 struct frame *f, *sf = SELECTED_FRAME ();
9223 /* Get the frame containing the minibuffer
9224 that the selected frame is using. */
9225 mini_window = FRAME_MINIBUF_WINDOW (sf);
9226 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9228 /* A null message buffer means that the frame hasn't really been
9229 initialized yet. Error messages get reported properly by
9230 cmd_error, so this must be just an informative message; toss it. */
9231 if (FRAME_MESSAGE_BUF (f))
9233 Lisp_Object args[2], msg;
9234 struct gcpro gcpro1, gcpro2;
9236 args[0] = build_string (m);
9237 args[1] = msg = string;
9238 GCPRO2 (args[0], msg);
9239 gcpro1.nvars = 2;
9241 msg = Fformat (2, args);
9243 if (log)
9244 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9245 else
9246 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9248 UNGCPRO;
9250 /* Print should start at the beginning of the message
9251 buffer next time. */
9252 message_buf_print = 0;
9258 /* Dump an informative message to the minibuf. If M is 0, clear out
9259 any existing message, and let the mini-buffer text show through. */
9261 static void
9262 vmessage (const char *m, va_list ap)
9264 if (noninteractive)
9266 if (m)
9268 if (noninteractive_need_newline)
9269 putc ('\n', stderr);
9270 noninteractive_need_newline = 0;
9271 vfprintf (stderr, m, ap);
9272 if (cursor_in_echo_area == 0)
9273 fprintf (stderr, "\n");
9274 fflush (stderr);
9277 else if (INTERACTIVE)
9279 /* The frame whose mini-buffer we're going to display the message
9280 on. It may be larger than the selected frame, so we need to
9281 use its buffer, not the selected frame's buffer. */
9282 Lisp_Object mini_window;
9283 struct frame *f, *sf = SELECTED_FRAME ();
9285 /* Get the frame containing the mini-buffer
9286 that the selected frame is using. */
9287 mini_window = FRAME_MINIBUF_WINDOW (sf);
9288 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9290 /* A null message buffer means that the frame hasn't really been
9291 initialized yet. Error messages get reported properly by
9292 cmd_error, so this must be just an informative message; toss
9293 it. */
9294 if (FRAME_MESSAGE_BUF (f))
9296 if (m)
9298 ptrdiff_t len;
9300 len = doprnt (FRAME_MESSAGE_BUF (f),
9301 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9303 message2 (FRAME_MESSAGE_BUF (f), len, 0);
9305 else
9306 message1 (0);
9308 /* Print should start at the beginning of the message
9309 buffer next time. */
9310 message_buf_print = 0;
9315 void
9316 message (const char *m, ...)
9318 va_list ap;
9319 va_start (ap, m);
9320 vmessage (m, ap);
9321 va_end (ap);
9325 #if 0
9326 /* The non-logging version of message. */
9328 void
9329 message_nolog (const char *m, ...)
9331 Lisp_Object old_log_max;
9332 va_list ap;
9333 va_start (ap, m);
9334 old_log_max = Vmessage_log_max;
9335 Vmessage_log_max = Qnil;
9336 vmessage (m, ap);
9337 Vmessage_log_max = old_log_max;
9338 va_end (ap);
9340 #endif
9343 /* Display the current message in the current mini-buffer. This is
9344 only called from error handlers in process.c, and is not time
9345 critical. */
9347 void
9348 update_echo_area (void)
9350 if (!NILP (echo_area_buffer[0]))
9352 Lisp_Object string;
9353 string = Fcurrent_message ();
9354 message3 (string, SBYTES (string),
9355 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9360 /* Make sure echo area buffers in `echo_buffers' are live.
9361 If they aren't, make new ones. */
9363 static void
9364 ensure_echo_area_buffers (void)
9366 int i;
9368 for (i = 0; i < 2; ++i)
9369 if (!BUFFERP (echo_buffer[i])
9370 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9372 char name[30];
9373 Lisp_Object old_buffer;
9374 int j;
9376 old_buffer = echo_buffer[i];
9377 sprintf (name, " *Echo Area %d*", i);
9378 echo_buffer[i] = Fget_buffer_create (build_string (name));
9379 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9380 /* to force word wrap in echo area -
9381 it was decided to postpone this*/
9382 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9384 for (j = 0; j < 2; ++j)
9385 if (EQ (old_buffer, echo_area_buffer[j]))
9386 echo_area_buffer[j] = echo_buffer[i];
9391 /* Call FN with args A1..A4 with either the current or last displayed
9392 echo_area_buffer as current buffer.
9394 WHICH zero means use the current message buffer
9395 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9396 from echo_buffer[] and clear it.
9398 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9399 suitable buffer from echo_buffer[] and clear it.
9401 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9402 that the current message becomes the last displayed one, make
9403 choose a suitable buffer for echo_area_buffer[0], and clear it.
9405 Value is what FN returns. */
9407 static int
9408 with_echo_area_buffer (struct window *w, int which,
9409 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
9410 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9412 Lisp_Object buffer;
9413 int this_one, the_other, clear_buffer_p, rc;
9414 int count = SPECPDL_INDEX ();
9416 /* If buffers aren't live, make new ones. */
9417 ensure_echo_area_buffers ();
9419 clear_buffer_p = 0;
9421 if (which == 0)
9422 this_one = 0, the_other = 1;
9423 else if (which > 0)
9424 this_one = 1, the_other = 0;
9425 else
9427 this_one = 0, the_other = 1;
9428 clear_buffer_p = 1;
9430 /* We need a fresh one in case the current echo buffer equals
9431 the one containing the last displayed echo area message. */
9432 if (!NILP (echo_area_buffer[this_one])
9433 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9434 echo_area_buffer[this_one] = Qnil;
9437 /* Choose a suitable buffer from echo_buffer[] is we don't
9438 have one. */
9439 if (NILP (echo_area_buffer[this_one]))
9441 echo_area_buffer[this_one]
9442 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9443 ? echo_buffer[the_other]
9444 : echo_buffer[this_one]);
9445 clear_buffer_p = 1;
9448 buffer = echo_area_buffer[this_one];
9450 /* Don't get confused by reusing the buffer used for echoing
9451 for a different purpose. */
9452 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9453 cancel_echoing ();
9455 record_unwind_protect (unwind_with_echo_area_buffer,
9456 with_echo_area_buffer_unwind_data (w));
9458 /* Make the echo area buffer current. Note that for display
9459 purposes, it is not necessary that the displayed window's buffer
9460 == current_buffer, except for text property lookup. So, let's
9461 only set that buffer temporarily here without doing a full
9462 Fset_window_buffer. We must also change w->pointm, though,
9463 because otherwise an assertions in unshow_buffer fails, and Emacs
9464 aborts. */
9465 set_buffer_internal_1 (XBUFFER (buffer));
9466 if (w)
9468 w->buffer = buffer;
9469 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9472 BVAR (current_buffer, undo_list) = Qt;
9473 BVAR (current_buffer, read_only) = Qnil;
9474 specbind (Qinhibit_read_only, Qt);
9475 specbind (Qinhibit_modification_hooks, Qt);
9477 if (clear_buffer_p && Z > BEG)
9478 del_range (BEG, Z);
9480 xassert (BEGV >= BEG);
9481 xassert (ZV <= Z && ZV >= BEGV);
9483 rc = fn (a1, a2, a3, a4);
9485 xassert (BEGV >= BEG);
9486 xassert (ZV <= Z && ZV >= BEGV);
9488 unbind_to (count, Qnil);
9489 return rc;
9493 /* Save state that should be preserved around the call to the function
9494 FN called in with_echo_area_buffer. */
9496 static Lisp_Object
9497 with_echo_area_buffer_unwind_data (struct window *w)
9499 int i = 0;
9500 Lisp_Object vector, tmp;
9502 /* Reduce consing by keeping one vector in
9503 Vwith_echo_area_save_vector. */
9504 vector = Vwith_echo_area_save_vector;
9505 Vwith_echo_area_save_vector = Qnil;
9507 if (NILP (vector))
9508 vector = Fmake_vector (make_number (7), Qnil);
9510 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9511 ASET (vector, i, Vdeactivate_mark); ++i;
9512 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9514 if (w)
9516 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9517 ASET (vector, i, w->buffer); ++i;
9518 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9519 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9521 else
9523 int end = i + 4;
9524 for (; i < end; ++i)
9525 ASET (vector, i, Qnil);
9528 xassert (i == ASIZE (vector));
9529 return vector;
9533 /* Restore global state from VECTOR which was created by
9534 with_echo_area_buffer_unwind_data. */
9536 static Lisp_Object
9537 unwind_with_echo_area_buffer (Lisp_Object vector)
9539 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
9540 Vdeactivate_mark = AREF (vector, 1);
9541 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
9543 if (WINDOWP (AREF (vector, 3)))
9545 struct window *w;
9546 Lisp_Object buffer, charpos, bytepos;
9548 w = XWINDOW (AREF (vector, 3));
9549 buffer = AREF (vector, 4);
9550 charpos = AREF (vector, 5);
9551 bytepos = AREF (vector, 6);
9553 w->buffer = buffer;
9554 set_marker_both (w->pointm, buffer,
9555 XFASTINT (charpos), XFASTINT (bytepos));
9558 Vwith_echo_area_save_vector = vector;
9559 return Qnil;
9563 /* Set up the echo area for use by print functions. MULTIBYTE_P
9564 non-zero means we will print multibyte. */
9566 void
9567 setup_echo_area_for_printing (int multibyte_p)
9569 /* If we can't find an echo area any more, exit. */
9570 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
9571 Fkill_emacs (Qnil);
9573 ensure_echo_area_buffers ();
9575 if (!message_buf_print)
9577 /* A message has been output since the last time we printed.
9578 Choose a fresh echo area buffer. */
9579 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9580 echo_area_buffer[0] = echo_buffer[1];
9581 else
9582 echo_area_buffer[0] = echo_buffer[0];
9584 /* Switch to that buffer and clear it. */
9585 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9586 BVAR (current_buffer, truncate_lines) = Qnil;
9588 if (Z > BEG)
9590 int count = SPECPDL_INDEX ();
9591 specbind (Qinhibit_read_only, Qt);
9592 /* Note that undo recording is always disabled. */
9593 del_range (BEG, Z);
9594 unbind_to (count, Qnil);
9596 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9598 /* Set up the buffer for the multibyteness we need. */
9599 if (multibyte_p
9600 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9601 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
9603 /* Raise the frame containing the echo area. */
9604 if (minibuffer_auto_raise)
9606 struct frame *sf = SELECTED_FRAME ();
9607 Lisp_Object mini_window;
9608 mini_window = FRAME_MINIBUF_WINDOW (sf);
9609 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9612 message_log_maybe_newline ();
9613 message_buf_print = 1;
9615 else
9617 if (NILP (echo_area_buffer[0]))
9619 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9620 echo_area_buffer[0] = echo_buffer[1];
9621 else
9622 echo_area_buffer[0] = echo_buffer[0];
9625 if (current_buffer != XBUFFER (echo_area_buffer[0]))
9627 /* Someone switched buffers between print requests. */
9628 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9629 BVAR (current_buffer, truncate_lines) = Qnil;
9635 /* Display an echo area message in window W. Value is non-zero if W's
9636 height is changed. If display_last_displayed_message_p is
9637 non-zero, display the message that was last displayed, otherwise
9638 display the current message. */
9640 static int
9641 display_echo_area (struct window *w)
9643 int i, no_message_p, window_height_changed_p, count;
9645 /* Temporarily disable garbage collections while displaying the echo
9646 area. This is done because a GC can print a message itself.
9647 That message would modify the echo area buffer's contents while a
9648 redisplay of the buffer is going on, and seriously confuse
9649 redisplay. */
9650 count = inhibit_garbage_collection ();
9652 /* If there is no message, we must call display_echo_area_1
9653 nevertheless because it resizes the window. But we will have to
9654 reset the echo_area_buffer in question to nil at the end because
9655 with_echo_area_buffer will sets it to an empty buffer. */
9656 i = display_last_displayed_message_p ? 1 : 0;
9657 no_message_p = NILP (echo_area_buffer[i]);
9659 window_height_changed_p
9660 = with_echo_area_buffer (w, display_last_displayed_message_p,
9661 display_echo_area_1,
9662 (intptr_t) w, Qnil, 0, 0);
9664 if (no_message_p)
9665 echo_area_buffer[i] = Qnil;
9667 unbind_to (count, Qnil);
9668 return window_height_changed_p;
9672 /* Helper for display_echo_area. Display the current buffer which
9673 contains the current echo area message in window W, a mini-window,
9674 a pointer to which is passed in A1. A2..A4 are currently not used.
9675 Change the height of W so that all of the message is displayed.
9676 Value is non-zero if height of W was changed. */
9678 static int
9679 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9681 intptr_t i1 = a1;
9682 struct window *w = (struct window *) i1;
9683 Lisp_Object window;
9684 struct text_pos start;
9685 int window_height_changed_p = 0;
9687 /* Do this before displaying, so that we have a large enough glyph
9688 matrix for the display. If we can't get enough space for the
9689 whole text, display the last N lines. That works by setting w->start. */
9690 window_height_changed_p = resize_mini_window (w, 0);
9692 /* Use the starting position chosen by resize_mini_window. */
9693 SET_TEXT_POS_FROM_MARKER (start, w->start);
9695 /* Display. */
9696 clear_glyph_matrix (w->desired_matrix);
9697 XSETWINDOW (window, w);
9698 try_window (window, start, 0);
9700 return window_height_changed_p;
9704 /* Resize the echo area window to exactly the size needed for the
9705 currently displayed message, if there is one. If a mini-buffer
9706 is active, don't shrink it. */
9708 void
9709 resize_echo_area_exactly (void)
9711 if (BUFFERP (echo_area_buffer[0])
9712 && WINDOWP (echo_area_window))
9714 struct window *w = XWINDOW (echo_area_window);
9715 int resized_p;
9716 Lisp_Object resize_exactly;
9718 if (minibuf_level == 0)
9719 resize_exactly = Qt;
9720 else
9721 resize_exactly = Qnil;
9723 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
9724 (intptr_t) w, resize_exactly,
9725 0, 0);
9726 if (resized_p)
9728 ++windows_or_buffers_changed;
9729 ++update_mode_lines;
9730 redisplay_internal ();
9736 /* Callback function for with_echo_area_buffer, when used from
9737 resize_echo_area_exactly. A1 contains a pointer to the window to
9738 resize, EXACTLY non-nil means resize the mini-window exactly to the
9739 size of the text displayed. A3 and A4 are not used. Value is what
9740 resize_mini_window returns. */
9742 static int
9743 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
9745 intptr_t i1 = a1;
9746 return resize_mini_window ((struct window *) i1, !NILP (exactly));
9750 /* Resize mini-window W to fit the size of its contents. EXACT_P
9751 means size the window exactly to the size needed. Otherwise, it's
9752 only enlarged until W's buffer is empty.
9754 Set W->start to the right place to begin display. If the whole
9755 contents fit, start at the beginning. Otherwise, start so as
9756 to make the end of the contents appear. This is particularly
9757 important for y-or-n-p, but seems desirable generally.
9759 Value is non-zero if the window height has been changed. */
9762 resize_mini_window (struct window *w, int exact_p)
9764 struct frame *f = XFRAME (w->frame);
9765 int window_height_changed_p = 0;
9767 xassert (MINI_WINDOW_P (w));
9769 /* By default, start display at the beginning. */
9770 set_marker_both (w->start, w->buffer,
9771 BUF_BEGV (XBUFFER (w->buffer)),
9772 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
9774 /* Don't resize windows while redisplaying a window; it would
9775 confuse redisplay functions when the size of the window they are
9776 displaying changes from under them. Such a resizing can happen,
9777 for instance, when which-func prints a long message while
9778 we are running fontification-functions. We're running these
9779 functions with safe_call which binds inhibit-redisplay to t. */
9780 if (!NILP (Vinhibit_redisplay))
9781 return 0;
9783 /* Nil means don't try to resize. */
9784 if (NILP (Vresize_mini_windows)
9785 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
9786 return 0;
9788 if (!FRAME_MINIBUF_ONLY_P (f))
9790 struct it it;
9791 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
9792 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
9793 int height, max_height;
9794 int unit = FRAME_LINE_HEIGHT (f);
9795 struct text_pos start;
9796 struct buffer *old_current_buffer = NULL;
9798 if (current_buffer != XBUFFER (w->buffer))
9800 old_current_buffer = current_buffer;
9801 set_buffer_internal (XBUFFER (w->buffer));
9804 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
9806 /* Compute the max. number of lines specified by the user. */
9807 if (FLOATP (Vmax_mini_window_height))
9808 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
9809 else if (INTEGERP (Vmax_mini_window_height))
9810 max_height = XINT (Vmax_mini_window_height);
9811 else
9812 max_height = total_height / 4;
9814 /* Correct that max. height if it's bogus. */
9815 max_height = max (1, max_height);
9816 max_height = min (total_height, max_height);
9818 /* Find out the height of the text in the window. */
9819 if (it.line_wrap == TRUNCATE)
9820 height = 1;
9821 else
9823 last_height = 0;
9824 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
9825 if (it.max_ascent == 0 && it.max_descent == 0)
9826 height = it.current_y + last_height;
9827 else
9828 height = it.current_y + it.max_ascent + it.max_descent;
9829 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
9830 height = (height + unit - 1) / unit;
9833 /* Compute a suitable window start. */
9834 if (height > max_height)
9836 height = max_height;
9837 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
9838 move_it_vertically_backward (&it, (height - 1) * unit);
9839 start = it.current.pos;
9841 else
9842 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
9843 SET_MARKER_FROM_TEXT_POS (w->start, start);
9845 if (EQ (Vresize_mini_windows, Qgrow_only))
9847 /* Let it grow only, until we display an empty message, in which
9848 case the window shrinks again. */
9849 if (height > WINDOW_TOTAL_LINES (w))
9851 int old_height = WINDOW_TOTAL_LINES (w);
9852 freeze_window_starts (f, 1);
9853 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9854 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9856 else if (height < WINDOW_TOTAL_LINES (w)
9857 && (exact_p || BEGV == ZV))
9859 int old_height = WINDOW_TOTAL_LINES (w);
9860 freeze_window_starts (f, 0);
9861 shrink_mini_window (w);
9862 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9865 else
9867 /* Always resize to exact size needed. */
9868 if (height > WINDOW_TOTAL_LINES (w))
9870 int old_height = WINDOW_TOTAL_LINES (w);
9871 freeze_window_starts (f, 1);
9872 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9873 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9875 else if (height < WINDOW_TOTAL_LINES (w))
9877 int old_height = WINDOW_TOTAL_LINES (w);
9878 freeze_window_starts (f, 0);
9879 shrink_mini_window (w);
9881 if (height)
9883 freeze_window_starts (f, 1);
9884 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9887 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9891 if (old_current_buffer)
9892 set_buffer_internal (old_current_buffer);
9895 return window_height_changed_p;
9899 /* Value is the current message, a string, or nil if there is no
9900 current message. */
9902 Lisp_Object
9903 current_message (void)
9905 Lisp_Object msg;
9907 if (!BUFFERP (echo_area_buffer[0]))
9908 msg = Qnil;
9909 else
9911 with_echo_area_buffer (0, 0, current_message_1,
9912 (intptr_t) &msg, Qnil, 0, 0);
9913 if (NILP (msg))
9914 echo_area_buffer[0] = Qnil;
9917 return msg;
9921 static int
9922 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9924 intptr_t i1 = a1;
9925 Lisp_Object *msg = (Lisp_Object *) i1;
9927 if (Z > BEG)
9928 *msg = make_buffer_string (BEG, Z, 1);
9929 else
9930 *msg = Qnil;
9931 return 0;
9935 /* Push the current message on Vmessage_stack for later restauration
9936 by restore_message. Value is non-zero if the current message isn't
9937 empty. This is a relatively infrequent operation, so it's not
9938 worth optimizing. */
9941 push_message (void)
9943 Lisp_Object msg;
9944 msg = current_message ();
9945 Vmessage_stack = Fcons (msg, Vmessage_stack);
9946 return STRINGP (msg);
9950 /* Restore message display from the top of Vmessage_stack. */
9952 void
9953 restore_message (void)
9955 Lisp_Object msg;
9957 xassert (CONSP (Vmessage_stack));
9958 msg = XCAR (Vmessage_stack);
9959 if (STRINGP (msg))
9960 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9961 else
9962 message3_nolog (msg, 0, 0);
9966 /* Handler for record_unwind_protect calling pop_message. */
9968 Lisp_Object
9969 pop_message_unwind (Lisp_Object dummy)
9971 pop_message ();
9972 return Qnil;
9975 /* Pop the top-most entry off Vmessage_stack. */
9977 static void
9978 pop_message (void)
9980 xassert (CONSP (Vmessage_stack));
9981 Vmessage_stack = XCDR (Vmessage_stack);
9985 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9986 exits. If the stack is not empty, we have a missing pop_message
9987 somewhere. */
9989 void
9990 check_message_stack (void)
9992 if (!NILP (Vmessage_stack))
9993 abort ();
9997 /* Truncate to NCHARS what will be displayed in the echo area the next
9998 time we display it---but don't redisplay it now. */
10000 void
10001 truncate_echo_area (EMACS_INT nchars)
10003 if (nchars == 0)
10004 echo_area_buffer[0] = Qnil;
10005 /* A null message buffer means that the frame hasn't really been
10006 initialized yet. Error messages get reported properly by
10007 cmd_error, so this must be just an informative message; toss it. */
10008 else if (!noninteractive
10009 && INTERACTIVE
10010 && !NILP (echo_area_buffer[0]))
10012 struct frame *sf = SELECTED_FRAME ();
10013 if (FRAME_MESSAGE_BUF (sf))
10014 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10019 /* Helper function for truncate_echo_area. Truncate the current
10020 message to at most NCHARS characters. */
10022 static int
10023 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10025 if (BEG + nchars < Z)
10026 del_range (BEG + nchars, Z);
10027 if (Z == BEG)
10028 echo_area_buffer[0] = Qnil;
10029 return 0;
10033 /* Set the current message to a substring of S or STRING.
10035 If STRING is a Lisp string, set the message to the first NBYTES
10036 bytes from STRING. NBYTES zero means use the whole string. If
10037 STRING is multibyte, the message will be displayed multibyte.
10039 If S is not null, set the message to the first LEN bytes of S. LEN
10040 zero means use the whole string. MULTIBYTE_P non-zero means S is
10041 multibyte. Display the message multibyte in that case.
10043 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10044 to t before calling set_message_1 (which calls insert).
10047 static void
10048 set_message (const char *s, Lisp_Object string,
10049 EMACS_INT nbytes, int multibyte_p)
10051 message_enable_multibyte
10052 = ((s && multibyte_p)
10053 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10055 with_echo_area_buffer (0, -1, set_message_1,
10056 (intptr_t) s, string, nbytes, multibyte_p);
10057 message_buf_print = 0;
10058 help_echo_showing_p = 0;
10062 /* Helper function for set_message. Arguments have the same meaning
10063 as there, with A1 corresponding to S and A2 corresponding to STRING
10064 This function is called with the echo area buffer being
10065 current. */
10067 static int
10068 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
10070 intptr_t i1 = a1;
10071 const char *s = (const char *) i1;
10072 const unsigned char *msg = (const unsigned char *) s;
10073 Lisp_Object string = a2;
10075 /* Change multibyteness of the echo buffer appropriately. */
10076 if (message_enable_multibyte
10077 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10078 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10080 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10081 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10082 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10084 /* Insert new message at BEG. */
10085 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10087 if (STRINGP (string))
10089 EMACS_INT nchars;
10091 if (nbytes == 0)
10092 nbytes = SBYTES (string);
10093 nchars = string_byte_to_char (string, nbytes);
10095 /* This function takes care of single/multibyte conversion. We
10096 just have to ensure that the echo area buffer has the right
10097 setting of enable_multibyte_characters. */
10098 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10100 else if (s)
10102 if (nbytes == 0)
10103 nbytes = strlen (s);
10105 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10107 /* Convert from multi-byte to single-byte. */
10108 EMACS_INT i;
10109 int c, n;
10110 char work[1];
10112 /* Convert a multibyte string to single-byte. */
10113 for (i = 0; i < nbytes; i += n)
10115 c = string_char_and_length (msg + i, &n);
10116 work[0] = (ASCII_CHAR_P (c)
10118 : multibyte_char_to_unibyte (c));
10119 insert_1_both (work, 1, 1, 1, 0, 0);
10122 else if (!multibyte_p
10123 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10125 /* Convert from single-byte to multi-byte. */
10126 EMACS_INT i;
10127 int c, n;
10128 unsigned char str[MAX_MULTIBYTE_LENGTH];
10130 /* Convert a single-byte string to multibyte. */
10131 for (i = 0; i < nbytes; i++)
10133 c = msg[i];
10134 MAKE_CHAR_MULTIBYTE (c);
10135 n = CHAR_STRING (c, str);
10136 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10139 else
10140 insert_1 (s, nbytes, 1, 0, 0);
10143 return 0;
10147 /* Clear messages. CURRENT_P non-zero means clear the current
10148 message. LAST_DISPLAYED_P non-zero means clear the message
10149 last displayed. */
10151 void
10152 clear_message (int current_p, int last_displayed_p)
10154 if (current_p)
10156 echo_area_buffer[0] = Qnil;
10157 message_cleared_p = 1;
10160 if (last_displayed_p)
10161 echo_area_buffer[1] = Qnil;
10163 message_buf_print = 0;
10166 /* Clear garbaged frames.
10168 This function is used where the old redisplay called
10169 redraw_garbaged_frames which in turn called redraw_frame which in
10170 turn called clear_frame. The call to clear_frame was a source of
10171 flickering. I believe a clear_frame is not necessary. It should
10172 suffice in the new redisplay to invalidate all current matrices,
10173 and ensure a complete redisplay of all windows. */
10175 static void
10176 clear_garbaged_frames (void)
10178 if (frame_garbaged)
10180 Lisp_Object tail, frame;
10181 int changed_count = 0;
10183 FOR_EACH_FRAME (tail, frame)
10185 struct frame *f = XFRAME (frame);
10187 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10189 if (f->resized_p)
10191 Fredraw_frame (frame);
10192 f->force_flush_display_p = 1;
10194 clear_current_matrices (f);
10195 changed_count++;
10196 f->garbaged = 0;
10197 f->resized_p = 0;
10201 frame_garbaged = 0;
10202 if (changed_count)
10203 ++windows_or_buffers_changed;
10208 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10209 is non-zero update selected_frame. Value is non-zero if the
10210 mini-windows height has been changed. */
10212 static int
10213 echo_area_display (int update_frame_p)
10215 Lisp_Object mini_window;
10216 struct window *w;
10217 struct frame *f;
10218 int window_height_changed_p = 0;
10219 struct frame *sf = SELECTED_FRAME ();
10221 mini_window = FRAME_MINIBUF_WINDOW (sf);
10222 w = XWINDOW (mini_window);
10223 f = XFRAME (WINDOW_FRAME (w));
10225 /* Don't display if frame is invisible or not yet initialized. */
10226 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10227 return 0;
10229 #ifdef HAVE_WINDOW_SYSTEM
10230 /* When Emacs starts, selected_frame may be the initial terminal
10231 frame. If we let this through, a message would be displayed on
10232 the terminal. */
10233 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10234 return 0;
10235 #endif /* HAVE_WINDOW_SYSTEM */
10237 /* Redraw garbaged frames. */
10238 if (frame_garbaged)
10239 clear_garbaged_frames ();
10241 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10243 echo_area_window = mini_window;
10244 window_height_changed_p = display_echo_area (w);
10245 w->must_be_updated_p = 1;
10247 /* Update the display, unless called from redisplay_internal.
10248 Also don't update the screen during redisplay itself. The
10249 update will happen at the end of redisplay, and an update
10250 here could cause confusion. */
10251 if (update_frame_p && !redisplaying_p)
10253 int n = 0;
10255 /* If the display update has been interrupted by pending
10256 input, update mode lines in the frame. Due to the
10257 pending input, it might have been that redisplay hasn't
10258 been called, so that mode lines above the echo area are
10259 garbaged. This looks odd, so we prevent it here. */
10260 if (!display_completed)
10261 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10263 if (window_height_changed_p
10264 /* Don't do this if Emacs is shutting down. Redisplay
10265 needs to run hooks. */
10266 && !NILP (Vrun_hooks))
10268 /* Must update other windows. Likewise as in other
10269 cases, don't let this update be interrupted by
10270 pending input. */
10271 int count = SPECPDL_INDEX ();
10272 specbind (Qredisplay_dont_pause, Qt);
10273 windows_or_buffers_changed = 1;
10274 redisplay_internal ();
10275 unbind_to (count, Qnil);
10277 else if (FRAME_WINDOW_P (f) && n == 0)
10279 /* Window configuration is the same as before.
10280 Can do with a display update of the echo area,
10281 unless we displayed some mode lines. */
10282 update_single_window (w, 1);
10283 FRAME_RIF (f)->flush_display (f);
10285 else
10286 update_frame (f, 1, 1);
10288 /* If cursor is in the echo area, make sure that the next
10289 redisplay displays the minibuffer, so that the cursor will
10290 be replaced with what the minibuffer wants. */
10291 if (cursor_in_echo_area)
10292 ++windows_or_buffers_changed;
10295 else if (!EQ (mini_window, selected_window))
10296 windows_or_buffers_changed++;
10298 /* Last displayed message is now the current message. */
10299 echo_area_buffer[1] = echo_area_buffer[0];
10300 /* Inform read_char that we're not echoing. */
10301 echo_message_buffer = Qnil;
10303 /* Prevent redisplay optimization in redisplay_internal by resetting
10304 this_line_start_pos. This is done because the mini-buffer now
10305 displays the message instead of its buffer text. */
10306 if (EQ (mini_window, selected_window))
10307 CHARPOS (this_line_start_pos) = 0;
10309 return window_height_changed_p;
10314 /***********************************************************************
10315 Mode Lines and Frame Titles
10316 ***********************************************************************/
10318 /* A buffer for constructing non-propertized mode-line strings and
10319 frame titles in it; allocated from the heap in init_xdisp and
10320 resized as needed in store_mode_line_noprop_char. */
10322 static char *mode_line_noprop_buf;
10324 /* The buffer's end, and a current output position in it. */
10326 static char *mode_line_noprop_buf_end;
10327 static char *mode_line_noprop_ptr;
10329 #define MODE_LINE_NOPROP_LEN(start) \
10330 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10332 static enum {
10333 MODE_LINE_DISPLAY = 0,
10334 MODE_LINE_TITLE,
10335 MODE_LINE_NOPROP,
10336 MODE_LINE_STRING
10337 } mode_line_target;
10339 /* Alist that caches the results of :propertize.
10340 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10341 static Lisp_Object mode_line_proptrans_alist;
10343 /* List of strings making up the mode-line. */
10344 static Lisp_Object mode_line_string_list;
10346 /* Base face property when building propertized mode line string. */
10347 static Lisp_Object mode_line_string_face;
10348 static Lisp_Object mode_line_string_face_prop;
10351 /* Unwind data for mode line strings */
10353 static Lisp_Object Vmode_line_unwind_vector;
10355 static Lisp_Object
10356 format_mode_line_unwind_data (struct buffer *obuf,
10357 Lisp_Object owin,
10358 int save_proptrans)
10360 Lisp_Object vector, tmp;
10362 /* Reduce consing by keeping one vector in
10363 Vwith_echo_area_save_vector. */
10364 vector = Vmode_line_unwind_vector;
10365 Vmode_line_unwind_vector = Qnil;
10367 if (NILP (vector))
10368 vector = Fmake_vector (make_number (8), Qnil);
10370 ASET (vector, 0, make_number (mode_line_target));
10371 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10372 ASET (vector, 2, mode_line_string_list);
10373 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10374 ASET (vector, 4, mode_line_string_face);
10375 ASET (vector, 5, mode_line_string_face_prop);
10377 if (obuf)
10378 XSETBUFFER (tmp, obuf);
10379 else
10380 tmp = Qnil;
10381 ASET (vector, 6, tmp);
10382 ASET (vector, 7, owin);
10384 return vector;
10387 static Lisp_Object
10388 unwind_format_mode_line (Lisp_Object vector)
10390 mode_line_target = XINT (AREF (vector, 0));
10391 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10392 mode_line_string_list = AREF (vector, 2);
10393 if (! EQ (AREF (vector, 3), Qt))
10394 mode_line_proptrans_alist = AREF (vector, 3);
10395 mode_line_string_face = AREF (vector, 4);
10396 mode_line_string_face_prop = AREF (vector, 5);
10398 if (!NILP (AREF (vector, 7)))
10399 /* Select window before buffer, since it may change the buffer. */
10400 Fselect_window (AREF (vector, 7), Qt);
10402 if (!NILP (AREF (vector, 6)))
10404 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10405 ASET (vector, 6, Qnil);
10408 Vmode_line_unwind_vector = vector;
10409 return Qnil;
10413 /* Store a single character C for the frame title in mode_line_noprop_buf.
10414 Re-allocate mode_line_noprop_buf if necessary. */
10416 static void
10417 store_mode_line_noprop_char (char c)
10419 /* If output position has reached the end of the allocated buffer,
10420 double the buffer's size. */
10421 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10423 int len = MODE_LINE_NOPROP_LEN (0);
10424 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
10425 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
10426 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
10427 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10430 *mode_line_noprop_ptr++ = c;
10434 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10435 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10436 characters that yield more columns than PRECISION; PRECISION <= 0
10437 means copy the whole string. Pad with spaces until FIELD_WIDTH
10438 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10439 pad. Called from display_mode_element when it is used to build a
10440 frame title. */
10442 static int
10443 store_mode_line_noprop (const char *string, int field_width, int precision)
10445 const unsigned char *str = (const unsigned char *) string;
10446 int n = 0;
10447 EMACS_INT dummy, nbytes;
10449 /* Copy at most PRECISION chars from STR. */
10450 nbytes = strlen (string);
10451 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10452 while (nbytes--)
10453 store_mode_line_noprop_char (*str++);
10455 /* Fill up with spaces until FIELD_WIDTH reached. */
10456 while (field_width > 0
10457 && n < field_width)
10459 store_mode_line_noprop_char (' ');
10460 ++n;
10463 return n;
10466 /***********************************************************************
10467 Frame Titles
10468 ***********************************************************************/
10470 #ifdef HAVE_WINDOW_SYSTEM
10472 /* Set the title of FRAME, if it has changed. The title format is
10473 Vicon_title_format if FRAME is iconified, otherwise it is
10474 frame_title_format. */
10476 static void
10477 x_consider_frame_title (Lisp_Object frame)
10479 struct frame *f = XFRAME (frame);
10481 if (FRAME_WINDOW_P (f)
10482 || FRAME_MINIBUF_ONLY_P (f)
10483 || f->explicit_name)
10485 /* Do we have more than one visible frame on this X display? */
10486 Lisp_Object tail;
10487 Lisp_Object fmt;
10488 int title_start;
10489 char *title;
10490 int len;
10491 struct it it;
10492 int count = SPECPDL_INDEX ();
10494 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
10496 Lisp_Object other_frame = XCAR (tail);
10497 struct frame *tf = XFRAME (other_frame);
10499 if (tf != f
10500 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10501 && !FRAME_MINIBUF_ONLY_P (tf)
10502 && !EQ (other_frame, tip_frame)
10503 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10504 break;
10507 /* Set global variable indicating that multiple frames exist. */
10508 multiple_frames = CONSP (tail);
10510 /* Switch to the buffer of selected window of the frame. Set up
10511 mode_line_target so that display_mode_element will output into
10512 mode_line_noprop_buf; then display the title. */
10513 record_unwind_protect (unwind_format_mode_line,
10514 format_mode_line_unwind_data
10515 (current_buffer, selected_window, 0));
10517 Fselect_window (f->selected_window, Qt);
10518 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
10519 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
10521 mode_line_target = MODE_LINE_TITLE;
10522 title_start = MODE_LINE_NOPROP_LEN (0);
10523 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
10524 NULL, DEFAULT_FACE_ID);
10525 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
10526 len = MODE_LINE_NOPROP_LEN (title_start);
10527 title = mode_line_noprop_buf + title_start;
10528 unbind_to (count, Qnil);
10530 /* Set the title only if it's changed. This avoids consing in
10531 the common case where it hasn't. (If it turns out that we've
10532 already wasted too much time by walking through the list with
10533 display_mode_element, then we might need to optimize at a
10534 higher level than this.) */
10535 if (! STRINGP (f->name)
10536 || SBYTES (f->name) != len
10537 || memcmp (title, SDATA (f->name), len) != 0)
10538 x_implicitly_set_name (f, make_string (title, len), Qnil);
10542 #endif /* not HAVE_WINDOW_SYSTEM */
10547 /***********************************************************************
10548 Menu Bars
10549 ***********************************************************************/
10552 /* Prepare for redisplay by updating menu-bar item lists when
10553 appropriate. This can call eval. */
10555 void
10556 prepare_menu_bars (void)
10558 int all_windows;
10559 struct gcpro gcpro1, gcpro2;
10560 struct frame *f;
10561 Lisp_Object tooltip_frame;
10563 #ifdef HAVE_WINDOW_SYSTEM
10564 tooltip_frame = tip_frame;
10565 #else
10566 tooltip_frame = Qnil;
10567 #endif
10569 /* Update all frame titles based on their buffer names, etc. We do
10570 this before the menu bars so that the buffer-menu will show the
10571 up-to-date frame titles. */
10572 #ifdef HAVE_WINDOW_SYSTEM
10573 if (windows_or_buffers_changed || update_mode_lines)
10575 Lisp_Object tail, frame;
10577 FOR_EACH_FRAME (tail, frame)
10579 f = XFRAME (frame);
10580 if (!EQ (frame, tooltip_frame)
10581 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
10582 x_consider_frame_title (frame);
10585 #endif /* HAVE_WINDOW_SYSTEM */
10587 /* Update the menu bar item lists, if appropriate. This has to be
10588 done before any actual redisplay or generation of display lines. */
10589 all_windows = (update_mode_lines
10590 || buffer_shared > 1
10591 || windows_or_buffers_changed);
10592 if (all_windows)
10594 Lisp_Object tail, frame;
10595 int count = SPECPDL_INDEX ();
10596 /* 1 means that update_menu_bar has run its hooks
10597 so any further calls to update_menu_bar shouldn't do so again. */
10598 int menu_bar_hooks_run = 0;
10600 record_unwind_save_match_data ();
10602 FOR_EACH_FRAME (tail, frame)
10604 f = XFRAME (frame);
10606 /* Ignore tooltip frame. */
10607 if (EQ (frame, tooltip_frame))
10608 continue;
10610 /* If a window on this frame changed size, report that to
10611 the user and clear the size-change flag. */
10612 if (FRAME_WINDOW_SIZES_CHANGED (f))
10614 Lisp_Object functions;
10616 /* Clear flag first in case we get an error below. */
10617 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
10618 functions = Vwindow_size_change_functions;
10619 GCPRO2 (tail, functions);
10621 while (CONSP (functions))
10623 if (!EQ (XCAR (functions), Qt))
10624 call1 (XCAR (functions), frame);
10625 functions = XCDR (functions);
10627 UNGCPRO;
10630 GCPRO1 (tail);
10631 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
10632 #ifdef HAVE_WINDOW_SYSTEM
10633 update_tool_bar (f, 0);
10634 #endif
10635 #ifdef HAVE_NS
10636 if (windows_or_buffers_changed
10637 && FRAME_NS_P (f))
10638 ns_set_doc_edited (f, Fbuffer_modified_p
10639 (XWINDOW (f->selected_window)->buffer));
10640 #endif
10641 UNGCPRO;
10644 unbind_to (count, Qnil);
10646 else
10648 struct frame *sf = SELECTED_FRAME ();
10649 update_menu_bar (sf, 1, 0);
10650 #ifdef HAVE_WINDOW_SYSTEM
10651 update_tool_bar (sf, 1);
10652 #endif
10657 /* Update the menu bar item list for frame F. This has to be done
10658 before we start to fill in any display lines, because it can call
10659 eval.
10661 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
10663 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
10664 already ran the menu bar hooks for this redisplay, so there
10665 is no need to run them again. The return value is the
10666 updated value of this flag, to pass to the next call. */
10668 static int
10669 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
10671 Lisp_Object window;
10672 register struct window *w;
10674 /* If called recursively during a menu update, do nothing. This can
10675 happen when, for instance, an activate-menubar-hook causes a
10676 redisplay. */
10677 if (inhibit_menubar_update)
10678 return hooks_run;
10680 window = FRAME_SELECTED_WINDOW (f);
10681 w = XWINDOW (window);
10683 if (FRAME_WINDOW_P (f)
10685 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10686 || defined (HAVE_NS) || defined (USE_GTK)
10687 FRAME_EXTERNAL_MENU_BAR (f)
10688 #else
10689 FRAME_MENU_BAR_LINES (f) > 0
10690 #endif
10691 : FRAME_MENU_BAR_LINES (f) > 0)
10693 /* If the user has switched buffers or windows, we need to
10694 recompute to reflect the new bindings. But we'll
10695 recompute when update_mode_lines is set too; that means
10696 that people can use force-mode-line-update to request
10697 that the menu bar be recomputed. The adverse effect on
10698 the rest of the redisplay algorithm is about the same as
10699 windows_or_buffers_changed anyway. */
10700 if (windows_or_buffers_changed
10701 /* This used to test w->update_mode_line, but we believe
10702 there is no need to recompute the menu in that case. */
10703 || update_mode_lines
10704 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10705 < BUF_MODIFF (XBUFFER (w->buffer)))
10706 != !NILP (w->last_had_star))
10707 || ((!NILP (Vtransient_mark_mode)
10708 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
10709 != !NILP (w->region_showing)))
10711 struct buffer *prev = current_buffer;
10712 int count = SPECPDL_INDEX ();
10714 specbind (Qinhibit_menubar_update, Qt);
10716 set_buffer_internal_1 (XBUFFER (w->buffer));
10717 if (save_match_data)
10718 record_unwind_save_match_data ();
10719 if (NILP (Voverriding_local_map_menu_flag))
10721 specbind (Qoverriding_terminal_local_map, Qnil);
10722 specbind (Qoverriding_local_map, Qnil);
10725 if (!hooks_run)
10727 /* Run the Lucid hook. */
10728 safe_run_hooks (Qactivate_menubar_hook);
10730 /* If it has changed current-menubar from previous value,
10731 really recompute the menu-bar from the value. */
10732 if (! NILP (Vlucid_menu_bar_dirty_flag))
10733 call0 (Qrecompute_lucid_menubar);
10735 safe_run_hooks (Qmenu_bar_update_hook);
10737 hooks_run = 1;
10740 XSETFRAME (Vmenu_updating_frame, f);
10741 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
10743 /* Redisplay the menu bar in case we changed it. */
10744 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10745 || defined (HAVE_NS) || defined (USE_GTK)
10746 if (FRAME_WINDOW_P (f))
10748 #if defined (HAVE_NS)
10749 /* All frames on Mac OS share the same menubar. So only
10750 the selected frame should be allowed to set it. */
10751 if (f == SELECTED_FRAME ())
10752 #endif
10753 set_frame_menubar (f, 0, 0);
10755 else
10756 /* On a terminal screen, the menu bar is an ordinary screen
10757 line, and this makes it get updated. */
10758 w->update_mode_line = Qt;
10759 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10760 /* In the non-toolkit version, the menu bar is an ordinary screen
10761 line, and this makes it get updated. */
10762 w->update_mode_line = Qt;
10763 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10765 unbind_to (count, Qnil);
10766 set_buffer_internal_1 (prev);
10770 return hooks_run;
10775 /***********************************************************************
10776 Output Cursor
10777 ***********************************************************************/
10779 #ifdef HAVE_WINDOW_SYSTEM
10781 /* EXPORT:
10782 Nominal cursor position -- where to draw output.
10783 HPOS and VPOS are window relative glyph matrix coordinates.
10784 X and Y are window relative pixel coordinates. */
10786 struct cursor_pos output_cursor;
10789 /* EXPORT:
10790 Set the global variable output_cursor to CURSOR. All cursor
10791 positions are relative to updated_window. */
10793 void
10794 set_output_cursor (struct cursor_pos *cursor)
10796 output_cursor.hpos = cursor->hpos;
10797 output_cursor.vpos = cursor->vpos;
10798 output_cursor.x = cursor->x;
10799 output_cursor.y = cursor->y;
10803 /* EXPORT for RIF:
10804 Set a nominal cursor position.
10806 HPOS and VPOS are column/row positions in a window glyph matrix. X
10807 and Y are window text area relative pixel positions.
10809 If this is done during an update, updated_window will contain the
10810 window that is being updated and the position is the future output
10811 cursor position for that window. If updated_window is null, use
10812 selected_window and display the cursor at the given position. */
10814 void
10815 x_cursor_to (int vpos, int hpos, int y, int x)
10817 struct window *w;
10819 /* If updated_window is not set, work on selected_window. */
10820 if (updated_window)
10821 w = updated_window;
10822 else
10823 w = XWINDOW (selected_window);
10825 /* Set the output cursor. */
10826 output_cursor.hpos = hpos;
10827 output_cursor.vpos = vpos;
10828 output_cursor.x = x;
10829 output_cursor.y = y;
10831 /* If not called as part of an update, really display the cursor.
10832 This will also set the cursor position of W. */
10833 if (updated_window == NULL)
10835 BLOCK_INPUT;
10836 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10837 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10838 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10839 UNBLOCK_INPUT;
10843 #endif /* HAVE_WINDOW_SYSTEM */
10846 /***********************************************************************
10847 Tool-bars
10848 ***********************************************************************/
10850 #ifdef HAVE_WINDOW_SYSTEM
10852 /* Where the mouse was last time we reported a mouse event. */
10854 FRAME_PTR last_mouse_frame;
10856 /* Tool-bar item index of the item on which a mouse button was pressed
10857 or -1. */
10859 int last_tool_bar_item;
10862 static Lisp_Object
10863 update_tool_bar_unwind (Lisp_Object frame)
10865 selected_frame = frame;
10866 return Qnil;
10869 /* Update the tool-bar item list for frame F. This has to be done
10870 before we start to fill in any display lines. Called from
10871 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10872 and restore it here. */
10874 static void
10875 update_tool_bar (struct frame *f, int save_match_data)
10877 #if defined (USE_GTK) || defined (HAVE_NS)
10878 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10879 #else
10880 int do_update = WINDOWP (f->tool_bar_window)
10881 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10882 #endif
10884 if (do_update)
10886 Lisp_Object window;
10887 struct window *w;
10889 window = FRAME_SELECTED_WINDOW (f);
10890 w = XWINDOW (window);
10892 /* If the user has switched buffers or windows, we need to
10893 recompute to reflect the new bindings. But we'll
10894 recompute when update_mode_lines is set too; that means
10895 that people can use force-mode-line-update to request
10896 that the menu bar be recomputed. The adverse effect on
10897 the rest of the redisplay algorithm is about the same as
10898 windows_or_buffers_changed anyway. */
10899 if (windows_or_buffers_changed
10900 || !NILP (w->update_mode_line)
10901 || update_mode_lines
10902 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10903 < BUF_MODIFF (XBUFFER (w->buffer)))
10904 != !NILP (w->last_had_star))
10905 || ((!NILP (Vtransient_mark_mode)
10906 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
10907 != !NILP (w->region_showing)))
10909 struct buffer *prev = current_buffer;
10910 int count = SPECPDL_INDEX ();
10911 Lisp_Object frame, new_tool_bar;
10912 int new_n_tool_bar;
10913 struct gcpro gcpro1;
10915 /* Set current_buffer to the buffer of the selected
10916 window of the frame, so that we get the right local
10917 keymaps. */
10918 set_buffer_internal_1 (XBUFFER (w->buffer));
10920 /* Save match data, if we must. */
10921 if (save_match_data)
10922 record_unwind_save_match_data ();
10924 /* Make sure that we don't accidentally use bogus keymaps. */
10925 if (NILP (Voverriding_local_map_menu_flag))
10927 specbind (Qoverriding_terminal_local_map, Qnil);
10928 specbind (Qoverriding_local_map, Qnil);
10931 GCPRO1 (new_tool_bar);
10933 /* We must temporarily set the selected frame to this frame
10934 before calling tool_bar_items, because the calculation of
10935 the tool-bar keymap uses the selected frame (see
10936 `tool-bar-make-keymap' in tool-bar.el). */
10937 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10938 XSETFRAME (frame, f);
10939 selected_frame = frame;
10941 /* Build desired tool-bar items from keymaps. */
10942 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10943 &new_n_tool_bar);
10945 /* Redisplay the tool-bar if we changed it. */
10946 if (new_n_tool_bar != f->n_tool_bar_items
10947 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10949 /* Redisplay that happens asynchronously due to an expose event
10950 may access f->tool_bar_items. Make sure we update both
10951 variables within BLOCK_INPUT so no such event interrupts. */
10952 BLOCK_INPUT;
10953 f->tool_bar_items = new_tool_bar;
10954 f->n_tool_bar_items = new_n_tool_bar;
10955 w->update_mode_line = Qt;
10956 UNBLOCK_INPUT;
10959 UNGCPRO;
10961 unbind_to (count, Qnil);
10962 set_buffer_internal_1 (prev);
10968 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10969 F's desired tool-bar contents. F->tool_bar_items must have
10970 been set up previously by calling prepare_menu_bars. */
10972 static void
10973 build_desired_tool_bar_string (struct frame *f)
10975 int i, size, size_needed;
10976 struct gcpro gcpro1, gcpro2, gcpro3;
10977 Lisp_Object image, plist, props;
10979 image = plist = props = Qnil;
10980 GCPRO3 (image, plist, props);
10982 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10983 Otherwise, make a new string. */
10985 /* The size of the string we might be able to reuse. */
10986 size = (STRINGP (f->desired_tool_bar_string)
10987 ? SCHARS (f->desired_tool_bar_string)
10988 : 0);
10990 /* We need one space in the string for each image. */
10991 size_needed = f->n_tool_bar_items;
10993 /* Reuse f->desired_tool_bar_string, if possible. */
10994 if (size < size_needed || NILP (f->desired_tool_bar_string))
10995 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10996 make_number (' '));
10997 else
10999 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11000 Fremove_text_properties (make_number (0), make_number (size),
11001 props, f->desired_tool_bar_string);
11004 /* Put a `display' property on the string for the images to display,
11005 put a `menu_item' property on tool-bar items with a value that
11006 is the index of the item in F's tool-bar item vector. */
11007 for (i = 0; i < f->n_tool_bar_items; ++i)
11009 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11011 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11012 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11013 int hmargin, vmargin, relief, idx, end;
11015 /* If image is a vector, choose the image according to the
11016 button state. */
11017 image = PROP (TOOL_BAR_ITEM_IMAGES);
11018 if (VECTORP (image))
11020 if (enabled_p)
11021 idx = (selected_p
11022 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11023 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11024 else
11025 idx = (selected_p
11026 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11027 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11029 xassert (ASIZE (image) >= idx);
11030 image = AREF (image, idx);
11032 else
11033 idx = -1;
11035 /* Ignore invalid image specifications. */
11036 if (!valid_image_p (image))
11037 continue;
11039 /* Display the tool-bar button pressed, or depressed. */
11040 plist = Fcopy_sequence (XCDR (image));
11042 /* Compute margin and relief to draw. */
11043 relief = (tool_bar_button_relief >= 0
11044 ? tool_bar_button_relief
11045 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11046 hmargin = vmargin = relief;
11048 if (INTEGERP (Vtool_bar_button_margin)
11049 && XINT (Vtool_bar_button_margin) > 0)
11051 hmargin += XFASTINT (Vtool_bar_button_margin);
11052 vmargin += XFASTINT (Vtool_bar_button_margin);
11054 else if (CONSP (Vtool_bar_button_margin))
11056 if (INTEGERP (XCAR (Vtool_bar_button_margin))
11057 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
11058 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11060 if (INTEGERP (XCDR (Vtool_bar_button_margin))
11061 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
11062 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11065 if (auto_raise_tool_bar_buttons_p)
11067 /* Add a `:relief' property to the image spec if the item is
11068 selected. */
11069 if (selected_p)
11071 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11072 hmargin -= relief;
11073 vmargin -= relief;
11076 else
11078 /* If image is selected, display it pressed, i.e. with a
11079 negative relief. If it's not selected, display it with a
11080 raised relief. */
11081 plist = Fplist_put (plist, QCrelief,
11082 (selected_p
11083 ? make_number (-relief)
11084 : make_number (relief)));
11085 hmargin -= relief;
11086 vmargin -= relief;
11089 /* Put a margin around the image. */
11090 if (hmargin || vmargin)
11092 if (hmargin == vmargin)
11093 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11094 else
11095 plist = Fplist_put (plist, QCmargin,
11096 Fcons (make_number (hmargin),
11097 make_number (vmargin)));
11100 /* If button is not enabled, and we don't have special images
11101 for the disabled state, make the image appear disabled by
11102 applying an appropriate algorithm to it. */
11103 if (!enabled_p && idx < 0)
11104 plist = Fplist_put (plist, QCconversion, Qdisabled);
11106 /* Put a `display' text property on the string for the image to
11107 display. Put a `menu-item' property on the string that gives
11108 the start of this item's properties in the tool-bar items
11109 vector. */
11110 image = Fcons (Qimage, plist);
11111 props = list4 (Qdisplay, image,
11112 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11114 /* Let the last image hide all remaining spaces in the tool bar
11115 string. The string can be longer than needed when we reuse a
11116 previous string. */
11117 if (i + 1 == f->n_tool_bar_items)
11118 end = SCHARS (f->desired_tool_bar_string);
11119 else
11120 end = i + 1;
11121 Fadd_text_properties (make_number (i), make_number (end),
11122 props, f->desired_tool_bar_string);
11123 #undef PROP
11126 UNGCPRO;
11130 /* Display one line of the tool-bar of frame IT->f.
11132 HEIGHT specifies the desired height of the tool-bar line.
11133 If the actual height of the glyph row is less than HEIGHT, the
11134 row's height is increased to HEIGHT, and the icons are centered
11135 vertically in the new height.
11137 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11138 count a final empty row in case the tool-bar width exactly matches
11139 the window width.
11142 static void
11143 display_tool_bar_line (struct it *it, int height)
11145 struct glyph_row *row = it->glyph_row;
11146 int max_x = it->last_visible_x;
11147 struct glyph *last;
11149 prepare_desired_row (row);
11150 row->y = it->current_y;
11152 /* Note that this isn't made use of if the face hasn't a box,
11153 so there's no need to check the face here. */
11154 it->start_of_box_run_p = 1;
11156 while (it->current_x < max_x)
11158 int x, n_glyphs_before, i, nglyphs;
11159 struct it it_before;
11161 /* Get the next display element. */
11162 if (!get_next_display_element (it))
11164 /* Don't count empty row if we are counting needed tool-bar lines. */
11165 if (height < 0 && !it->hpos)
11166 return;
11167 break;
11170 /* Produce glyphs. */
11171 n_glyphs_before = row->used[TEXT_AREA];
11172 it_before = *it;
11174 PRODUCE_GLYPHS (it);
11176 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11177 i = 0;
11178 x = it_before.current_x;
11179 while (i < nglyphs)
11181 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11183 if (x + glyph->pixel_width > max_x)
11185 /* Glyph doesn't fit on line. Backtrack. */
11186 row->used[TEXT_AREA] = n_glyphs_before;
11187 *it = it_before;
11188 /* If this is the only glyph on this line, it will never fit on the
11189 tool-bar, so skip it. But ensure there is at least one glyph,
11190 so we don't accidentally disable the tool-bar. */
11191 if (n_glyphs_before == 0
11192 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11193 break;
11194 goto out;
11197 ++it->hpos;
11198 x += glyph->pixel_width;
11199 ++i;
11202 /* Stop at line end. */
11203 if (ITERATOR_AT_END_OF_LINE_P (it))
11204 break;
11206 set_iterator_to_next (it, 1);
11209 out:;
11211 row->displays_text_p = row->used[TEXT_AREA] != 0;
11213 /* Use default face for the border below the tool bar.
11215 FIXME: When auto-resize-tool-bars is grow-only, there is
11216 no additional border below the possibly empty tool-bar lines.
11217 So to make the extra empty lines look "normal", we have to
11218 use the tool-bar face for the border too. */
11219 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11220 it->face_id = DEFAULT_FACE_ID;
11222 extend_face_to_end_of_line (it);
11223 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11224 last->right_box_line_p = 1;
11225 if (last == row->glyphs[TEXT_AREA])
11226 last->left_box_line_p = 1;
11228 /* Make line the desired height and center it vertically. */
11229 if ((height -= it->max_ascent + it->max_descent) > 0)
11231 /* Don't add more than one line height. */
11232 height %= FRAME_LINE_HEIGHT (it->f);
11233 it->max_ascent += height / 2;
11234 it->max_descent += (height + 1) / 2;
11237 compute_line_metrics (it);
11239 /* If line is empty, make it occupy the rest of the tool-bar. */
11240 if (!row->displays_text_p)
11242 row->height = row->phys_height = it->last_visible_y - row->y;
11243 row->visible_height = row->height;
11244 row->ascent = row->phys_ascent = 0;
11245 row->extra_line_spacing = 0;
11248 row->full_width_p = 1;
11249 row->continued_p = 0;
11250 row->truncated_on_left_p = 0;
11251 row->truncated_on_right_p = 0;
11253 it->current_x = it->hpos = 0;
11254 it->current_y += row->height;
11255 ++it->vpos;
11256 ++it->glyph_row;
11260 /* Max tool-bar height. */
11262 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11263 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11265 /* Value is the number of screen lines needed to make all tool-bar
11266 items of frame F visible. The number of actual rows needed is
11267 returned in *N_ROWS if non-NULL. */
11269 static int
11270 tool_bar_lines_needed (struct frame *f, int *n_rows)
11272 struct window *w = XWINDOW (f->tool_bar_window);
11273 struct it it;
11274 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11275 the desired matrix, so use (unused) mode-line row as temporary row to
11276 avoid destroying the first tool-bar row. */
11277 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11279 /* Initialize an iterator for iteration over
11280 F->desired_tool_bar_string in the tool-bar window of frame F. */
11281 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11282 it.first_visible_x = 0;
11283 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11284 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11285 it.paragraph_embedding = L2R;
11287 while (!ITERATOR_AT_END_P (&it))
11289 clear_glyph_row (temp_row);
11290 it.glyph_row = temp_row;
11291 display_tool_bar_line (&it, -1);
11293 clear_glyph_row (temp_row);
11295 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11296 if (n_rows)
11297 *n_rows = it.vpos > 0 ? it.vpos : -1;
11299 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11303 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11304 0, 1, 0,
11305 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11306 (Lisp_Object frame)
11308 struct frame *f;
11309 struct window *w;
11310 int nlines = 0;
11312 if (NILP (frame))
11313 frame = selected_frame;
11314 else
11315 CHECK_FRAME (frame);
11316 f = XFRAME (frame);
11318 if (WINDOWP (f->tool_bar_window)
11319 && (w = XWINDOW (f->tool_bar_window),
11320 WINDOW_TOTAL_LINES (w) > 0))
11322 update_tool_bar (f, 1);
11323 if (f->n_tool_bar_items)
11325 build_desired_tool_bar_string (f);
11326 nlines = tool_bar_lines_needed (f, NULL);
11330 return make_number (nlines);
11334 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11335 height should be changed. */
11337 static int
11338 redisplay_tool_bar (struct frame *f)
11340 struct window *w;
11341 struct it it;
11342 struct glyph_row *row;
11344 #if defined (USE_GTK) || defined (HAVE_NS)
11345 if (FRAME_EXTERNAL_TOOL_BAR (f))
11346 update_frame_tool_bar (f);
11347 return 0;
11348 #endif
11350 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11351 do anything. This means you must start with tool-bar-lines
11352 non-zero to get the auto-sizing effect. Or in other words, you
11353 can turn off tool-bars by specifying tool-bar-lines zero. */
11354 if (!WINDOWP (f->tool_bar_window)
11355 || (w = XWINDOW (f->tool_bar_window),
11356 WINDOW_TOTAL_LINES (w) == 0))
11357 return 0;
11359 /* Set up an iterator for the tool-bar window. */
11360 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11361 it.first_visible_x = 0;
11362 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11363 row = it.glyph_row;
11365 /* Build a string that represents the contents of the tool-bar. */
11366 build_desired_tool_bar_string (f);
11367 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11368 /* FIXME: This should be controlled by a user option. But it
11369 doesn't make sense to have an R2L tool bar if the menu bar cannot
11370 be drawn also R2L, and making the menu bar R2L is tricky due
11371 toolkit-specific code that implements it. If an R2L tool bar is
11372 ever supported, display_tool_bar_line should also be augmented to
11373 call unproduce_glyphs like display_line and display_string
11374 do. */
11375 it.paragraph_embedding = L2R;
11377 if (f->n_tool_bar_rows == 0)
11379 int nlines;
11381 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11382 nlines != WINDOW_TOTAL_LINES (w)))
11384 Lisp_Object frame;
11385 int old_height = WINDOW_TOTAL_LINES (w);
11387 XSETFRAME (frame, f);
11388 Fmodify_frame_parameters (frame,
11389 Fcons (Fcons (Qtool_bar_lines,
11390 make_number (nlines)),
11391 Qnil));
11392 if (WINDOW_TOTAL_LINES (w) != old_height)
11394 clear_glyph_matrix (w->desired_matrix);
11395 fonts_changed_p = 1;
11396 return 1;
11401 /* Display as many lines as needed to display all tool-bar items. */
11403 if (f->n_tool_bar_rows > 0)
11405 int border, rows, height, extra;
11407 if (INTEGERP (Vtool_bar_border))
11408 border = XINT (Vtool_bar_border);
11409 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11410 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11411 else if (EQ (Vtool_bar_border, Qborder_width))
11412 border = f->border_width;
11413 else
11414 border = 0;
11415 if (border < 0)
11416 border = 0;
11418 rows = f->n_tool_bar_rows;
11419 height = max (1, (it.last_visible_y - border) / rows);
11420 extra = it.last_visible_y - border - height * rows;
11422 while (it.current_y < it.last_visible_y)
11424 int h = 0;
11425 if (extra > 0 && rows-- > 0)
11427 h = (extra + rows - 1) / rows;
11428 extra -= h;
11430 display_tool_bar_line (&it, height + h);
11433 else
11435 while (it.current_y < it.last_visible_y)
11436 display_tool_bar_line (&it, 0);
11439 /* It doesn't make much sense to try scrolling in the tool-bar
11440 window, so don't do it. */
11441 w->desired_matrix->no_scrolling_p = 1;
11442 w->must_be_updated_p = 1;
11444 if (!NILP (Vauto_resize_tool_bars))
11446 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11447 int change_height_p = 0;
11449 /* If we couldn't display everything, change the tool-bar's
11450 height if there is room for more. */
11451 if (IT_STRING_CHARPOS (it) < it.end_charpos
11452 && it.current_y < max_tool_bar_height)
11453 change_height_p = 1;
11455 row = it.glyph_row - 1;
11457 /* If there are blank lines at the end, except for a partially
11458 visible blank line at the end that is smaller than
11459 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11460 if (!row->displays_text_p
11461 && row->height >= FRAME_LINE_HEIGHT (f))
11462 change_height_p = 1;
11464 /* If row displays tool-bar items, but is partially visible,
11465 change the tool-bar's height. */
11466 if (row->displays_text_p
11467 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11468 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11469 change_height_p = 1;
11471 /* Resize windows as needed by changing the `tool-bar-lines'
11472 frame parameter. */
11473 if (change_height_p)
11475 Lisp_Object frame;
11476 int old_height = WINDOW_TOTAL_LINES (w);
11477 int nrows;
11478 int nlines = tool_bar_lines_needed (f, &nrows);
11480 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11481 && !f->minimize_tool_bar_window_p)
11482 ? (nlines > old_height)
11483 : (nlines != old_height));
11484 f->minimize_tool_bar_window_p = 0;
11486 if (change_height_p)
11488 XSETFRAME (frame, f);
11489 Fmodify_frame_parameters (frame,
11490 Fcons (Fcons (Qtool_bar_lines,
11491 make_number (nlines)),
11492 Qnil));
11493 if (WINDOW_TOTAL_LINES (w) != old_height)
11495 clear_glyph_matrix (w->desired_matrix);
11496 f->n_tool_bar_rows = nrows;
11497 fonts_changed_p = 1;
11498 return 1;
11504 f->minimize_tool_bar_window_p = 0;
11505 return 0;
11509 /* Get information about the tool-bar item which is displayed in GLYPH
11510 on frame F. Return in *PROP_IDX the index where tool-bar item
11511 properties start in F->tool_bar_items. Value is zero if
11512 GLYPH doesn't display a tool-bar item. */
11514 static int
11515 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11517 Lisp_Object prop;
11518 int success_p;
11519 int charpos;
11521 /* This function can be called asynchronously, which means we must
11522 exclude any possibility that Fget_text_property signals an
11523 error. */
11524 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
11525 charpos = max (0, charpos);
11527 /* Get the text property `menu-item' at pos. The value of that
11528 property is the start index of this item's properties in
11529 F->tool_bar_items. */
11530 prop = Fget_text_property (make_number (charpos),
11531 Qmenu_item, f->current_tool_bar_string);
11532 if (INTEGERP (prop))
11534 *prop_idx = XINT (prop);
11535 success_p = 1;
11537 else
11538 success_p = 0;
11540 return success_p;
11544 /* Get information about the tool-bar item at position X/Y on frame F.
11545 Return in *GLYPH a pointer to the glyph of the tool-bar item in
11546 the current matrix of the tool-bar window of F, or NULL if not
11547 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
11548 item in F->tool_bar_items. Value is
11550 -1 if X/Y is not on a tool-bar item
11551 0 if X/Y is on the same item that was highlighted before.
11552 1 otherwise. */
11554 static int
11555 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
11556 int *hpos, int *vpos, int *prop_idx)
11558 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11559 struct window *w = XWINDOW (f->tool_bar_window);
11560 int area;
11562 /* Find the glyph under X/Y. */
11563 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
11564 if (*glyph == NULL)
11565 return -1;
11567 /* Get the start of this tool-bar item's properties in
11568 f->tool_bar_items. */
11569 if (!tool_bar_item_info (f, *glyph, prop_idx))
11570 return -1;
11572 /* Is mouse on the highlighted item? */
11573 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
11574 && *vpos >= hlinfo->mouse_face_beg_row
11575 && *vpos <= hlinfo->mouse_face_end_row
11576 && (*vpos > hlinfo->mouse_face_beg_row
11577 || *hpos >= hlinfo->mouse_face_beg_col)
11578 && (*vpos < hlinfo->mouse_face_end_row
11579 || *hpos < hlinfo->mouse_face_end_col
11580 || hlinfo->mouse_face_past_end))
11581 return 0;
11583 return 1;
11587 /* EXPORT:
11588 Handle mouse button event on the tool-bar of frame F, at
11589 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
11590 0 for button release. MODIFIERS is event modifiers for button
11591 release. */
11593 void
11594 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
11595 unsigned int modifiers)
11597 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11598 struct window *w = XWINDOW (f->tool_bar_window);
11599 int hpos, vpos, prop_idx;
11600 struct glyph *glyph;
11601 Lisp_Object enabled_p;
11603 /* If not on the highlighted tool-bar item, return. */
11604 frame_to_window_pixel_xy (w, &x, &y);
11605 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
11606 return;
11608 /* If item is disabled, do nothing. */
11609 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11610 if (NILP (enabled_p))
11611 return;
11613 if (down_p)
11615 /* Show item in pressed state. */
11616 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
11617 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
11618 last_tool_bar_item = prop_idx;
11620 else
11622 Lisp_Object key, frame;
11623 struct input_event event;
11624 EVENT_INIT (event);
11626 /* Show item in released state. */
11627 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
11628 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
11630 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
11632 XSETFRAME (frame, f);
11633 event.kind = TOOL_BAR_EVENT;
11634 event.frame_or_window = frame;
11635 event.arg = frame;
11636 kbd_buffer_store_event (&event);
11638 event.kind = TOOL_BAR_EVENT;
11639 event.frame_or_window = frame;
11640 event.arg = key;
11641 event.modifiers = modifiers;
11642 kbd_buffer_store_event (&event);
11643 last_tool_bar_item = -1;
11648 /* Possibly highlight a tool-bar item on frame F when mouse moves to
11649 tool-bar window-relative coordinates X/Y. Called from
11650 note_mouse_highlight. */
11652 static void
11653 note_tool_bar_highlight (struct frame *f, int x, int y)
11655 Lisp_Object window = f->tool_bar_window;
11656 struct window *w = XWINDOW (window);
11657 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11658 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11659 int hpos, vpos;
11660 struct glyph *glyph;
11661 struct glyph_row *row;
11662 int i;
11663 Lisp_Object enabled_p;
11664 int prop_idx;
11665 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
11666 int mouse_down_p, rc;
11668 /* Function note_mouse_highlight is called with negative X/Y
11669 values when mouse moves outside of the frame. */
11670 if (x <= 0 || y <= 0)
11672 clear_mouse_face (hlinfo);
11673 return;
11676 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
11677 if (rc < 0)
11679 /* Not on tool-bar item. */
11680 clear_mouse_face (hlinfo);
11681 return;
11683 else if (rc == 0)
11684 /* On same tool-bar item as before. */
11685 goto set_help_echo;
11687 clear_mouse_face (hlinfo);
11689 /* Mouse is down, but on different tool-bar item? */
11690 mouse_down_p = (dpyinfo->grabbed
11691 && f == last_mouse_frame
11692 && FRAME_LIVE_P (f));
11693 if (mouse_down_p
11694 && last_tool_bar_item != prop_idx)
11695 return;
11697 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
11698 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
11700 /* If tool-bar item is not enabled, don't highlight it. */
11701 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11702 if (!NILP (enabled_p))
11704 /* Compute the x-position of the glyph. In front and past the
11705 image is a space. We include this in the highlighted area. */
11706 row = MATRIX_ROW (w->current_matrix, vpos);
11707 for (i = x = 0; i < hpos; ++i)
11708 x += row->glyphs[TEXT_AREA][i].pixel_width;
11710 /* Record this as the current active region. */
11711 hlinfo->mouse_face_beg_col = hpos;
11712 hlinfo->mouse_face_beg_row = vpos;
11713 hlinfo->mouse_face_beg_x = x;
11714 hlinfo->mouse_face_beg_y = row->y;
11715 hlinfo->mouse_face_past_end = 0;
11717 hlinfo->mouse_face_end_col = hpos + 1;
11718 hlinfo->mouse_face_end_row = vpos;
11719 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
11720 hlinfo->mouse_face_end_y = row->y;
11721 hlinfo->mouse_face_window = window;
11722 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
11724 /* Display it as active. */
11725 show_mouse_face (hlinfo, draw);
11726 hlinfo->mouse_face_image_state = draw;
11729 set_help_echo:
11731 /* Set help_echo_string to a help string to display for this tool-bar item.
11732 XTread_socket does the rest. */
11733 help_echo_object = help_echo_window = Qnil;
11734 help_echo_pos = -1;
11735 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
11736 if (NILP (help_echo_string))
11737 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
11740 #endif /* HAVE_WINDOW_SYSTEM */
11744 /************************************************************************
11745 Horizontal scrolling
11746 ************************************************************************/
11748 static int hscroll_window_tree (Lisp_Object);
11749 static int hscroll_windows (Lisp_Object);
11751 /* For all leaf windows in the window tree rooted at WINDOW, set their
11752 hscroll value so that PT is (i) visible in the window, and (ii) so
11753 that it is not within a certain margin at the window's left and
11754 right border. Value is non-zero if any window's hscroll has been
11755 changed. */
11757 static int
11758 hscroll_window_tree (Lisp_Object window)
11760 int hscrolled_p = 0;
11761 int hscroll_relative_p = FLOATP (Vhscroll_step);
11762 int hscroll_step_abs = 0;
11763 double hscroll_step_rel = 0;
11765 if (hscroll_relative_p)
11767 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
11768 if (hscroll_step_rel < 0)
11770 hscroll_relative_p = 0;
11771 hscroll_step_abs = 0;
11774 else if (INTEGERP (Vhscroll_step))
11776 hscroll_step_abs = XINT (Vhscroll_step);
11777 if (hscroll_step_abs < 0)
11778 hscroll_step_abs = 0;
11780 else
11781 hscroll_step_abs = 0;
11783 while (WINDOWP (window))
11785 struct window *w = XWINDOW (window);
11787 if (WINDOWP (w->hchild))
11788 hscrolled_p |= hscroll_window_tree (w->hchild);
11789 else if (WINDOWP (w->vchild))
11790 hscrolled_p |= hscroll_window_tree (w->vchild);
11791 else if (w->cursor.vpos >= 0)
11793 int h_margin;
11794 int text_area_width;
11795 struct glyph_row *current_cursor_row
11796 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
11797 struct glyph_row *desired_cursor_row
11798 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
11799 struct glyph_row *cursor_row
11800 = (desired_cursor_row->enabled_p
11801 ? desired_cursor_row
11802 : current_cursor_row);
11804 text_area_width = window_box_width (w, TEXT_AREA);
11806 /* Scroll when cursor is inside this scroll margin. */
11807 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
11809 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
11810 && ((XFASTINT (w->hscroll)
11811 && w->cursor.x <= h_margin)
11812 || (cursor_row->enabled_p
11813 && cursor_row->truncated_on_right_p
11814 && (w->cursor.x >= text_area_width - h_margin))))
11816 struct it it;
11817 int hscroll;
11818 struct buffer *saved_current_buffer;
11819 EMACS_INT pt;
11820 int wanted_x;
11822 /* Find point in a display of infinite width. */
11823 saved_current_buffer = current_buffer;
11824 current_buffer = XBUFFER (w->buffer);
11826 if (w == XWINDOW (selected_window))
11827 pt = PT;
11828 else
11830 pt = marker_position (w->pointm);
11831 pt = max (BEGV, pt);
11832 pt = min (ZV, pt);
11835 /* Move iterator to pt starting at cursor_row->start in
11836 a line with infinite width. */
11837 init_to_row_start (&it, w, cursor_row);
11838 it.last_visible_x = INFINITY;
11839 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11840 current_buffer = saved_current_buffer;
11842 /* Position cursor in window. */
11843 if (!hscroll_relative_p && hscroll_step_abs == 0)
11844 hscroll = max (0, (it.current_x
11845 - (ITERATOR_AT_END_OF_LINE_P (&it)
11846 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11847 : (text_area_width / 2))))
11848 / FRAME_COLUMN_WIDTH (it.f);
11849 else if (w->cursor.x >= text_area_width - h_margin)
11851 if (hscroll_relative_p)
11852 wanted_x = text_area_width * (1 - hscroll_step_rel)
11853 - h_margin;
11854 else
11855 wanted_x = text_area_width
11856 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11857 - h_margin;
11858 hscroll
11859 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11861 else
11863 if (hscroll_relative_p)
11864 wanted_x = text_area_width * hscroll_step_rel
11865 + h_margin;
11866 else
11867 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11868 + h_margin;
11869 hscroll
11870 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11872 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11874 /* Don't call Fset_window_hscroll if value hasn't
11875 changed because it will prevent redisplay
11876 optimizations. */
11877 if (XFASTINT (w->hscroll) != hscroll)
11879 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11880 w->hscroll = make_number (hscroll);
11881 hscrolled_p = 1;
11886 window = w->next;
11889 /* Value is non-zero if hscroll of any leaf window has been changed. */
11890 return hscrolled_p;
11894 /* Set hscroll so that cursor is visible and not inside horizontal
11895 scroll margins for all windows in the tree rooted at WINDOW. See
11896 also hscroll_window_tree above. Value is non-zero if any window's
11897 hscroll has been changed. If it has, desired matrices on the frame
11898 of WINDOW are cleared. */
11900 static int
11901 hscroll_windows (Lisp_Object window)
11903 int hscrolled_p = hscroll_window_tree (window);
11904 if (hscrolled_p)
11905 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11906 return hscrolled_p;
11911 /************************************************************************
11912 Redisplay
11913 ************************************************************************/
11915 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11916 to a non-zero value. This is sometimes handy to have in a debugger
11917 session. */
11919 #if GLYPH_DEBUG
11921 /* First and last unchanged row for try_window_id. */
11923 static int debug_first_unchanged_at_end_vpos;
11924 static int debug_last_unchanged_at_beg_vpos;
11926 /* Delta vpos and y. */
11928 static int debug_dvpos, debug_dy;
11930 /* Delta in characters and bytes for try_window_id. */
11932 static EMACS_INT debug_delta, debug_delta_bytes;
11934 /* Values of window_end_pos and window_end_vpos at the end of
11935 try_window_id. */
11937 static EMACS_INT debug_end_vpos;
11939 /* Append a string to W->desired_matrix->method. FMT is a printf
11940 format string. If trace_redisplay_p is non-zero also printf the
11941 resulting string to stderr. */
11943 static void debug_method_add (struct window *, char const *, ...)
11944 ATTRIBUTE_FORMAT_PRINTF (2, 3);
11946 static void
11947 debug_method_add (struct window *w, char const *fmt, ...)
11949 char buffer[512];
11950 char *method = w->desired_matrix->method;
11951 int len = strlen (method);
11952 int size = sizeof w->desired_matrix->method;
11953 int remaining = size - len - 1;
11954 va_list ap;
11956 va_start (ap, fmt);
11957 vsprintf (buffer, fmt, ap);
11958 va_end (ap);
11959 if (len && remaining)
11961 method[len] = '|';
11962 --remaining, ++len;
11965 strncpy (method + len, buffer, remaining);
11967 if (trace_redisplay_p)
11968 fprintf (stderr, "%p (%s): %s\n",
11970 ((BUFFERP (w->buffer)
11971 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
11972 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
11973 : "no buffer"),
11974 buffer);
11977 #endif /* GLYPH_DEBUG */
11980 /* Value is non-zero if all changes in window W, which displays
11981 current_buffer, are in the text between START and END. START is a
11982 buffer position, END is given as a distance from Z. Used in
11983 redisplay_internal for display optimization. */
11985 static inline int
11986 text_outside_line_unchanged_p (struct window *w,
11987 EMACS_INT start, EMACS_INT end)
11989 int unchanged_p = 1;
11991 /* If text or overlays have changed, see where. */
11992 if (XFASTINT (w->last_modified) < MODIFF
11993 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11995 /* Gap in the line? */
11996 if (GPT < start || Z - GPT < end)
11997 unchanged_p = 0;
11999 /* Changes start in front of the line, or end after it? */
12000 if (unchanged_p
12001 && (BEG_UNCHANGED < start - 1
12002 || END_UNCHANGED < end))
12003 unchanged_p = 0;
12005 /* If selective display, can't optimize if changes start at the
12006 beginning of the line. */
12007 if (unchanged_p
12008 && INTEGERP (BVAR (current_buffer, selective_display))
12009 && XINT (BVAR (current_buffer, selective_display)) > 0
12010 && (BEG_UNCHANGED < start || GPT <= start))
12011 unchanged_p = 0;
12013 /* If there are overlays at the start or end of the line, these
12014 may have overlay strings with newlines in them. A change at
12015 START, for instance, may actually concern the display of such
12016 overlay strings as well, and they are displayed on different
12017 lines. So, quickly rule out this case. (For the future, it
12018 might be desirable to implement something more telling than
12019 just BEG/END_UNCHANGED.) */
12020 if (unchanged_p)
12022 if (BEG + BEG_UNCHANGED == start
12023 && overlay_touches_p (start))
12024 unchanged_p = 0;
12025 if (END_UNCHANGED == end
12026 && overlay_touches_p (Z - end))
12027 unchanged_p = 0;
12030 /* Under bidi reordering, adding or deleting a character in the
12031 beginning of a paragraph, before the first strong directional
12032 character, can change the base direction of the paragraph (unless
12033 the buffer specifies a fixed paragraph direction), which will
12034 require to redisplay the whole paragraph. It might be worthwhile
12035 to find the paragraph limits and widen the range of redisplayed
12036 lines to that, but for now just give up this optimization. */
12037 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12038 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12039 unchanged_p = 0;
12042 return unchanged_p;
12046 /* Do a frame update, taking possible shortcuts into account. This is
12047 the main external entry point for redisplay.
12049 If the last redisplay displayed an echo area message and that message
12050 is no longer requested, we clear the echo area or bring back the
12051 mini-buffer if that is in use. */
12053 void
12054 redisplay (void)
12056 redisplay_internal ();
12060 static Lisp_Object
12061 overlay_arrow_string_or_property (Lisp_Object var)
12063 Lisp_Object val;
12065 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12066 return val;
12068 return Voverlay_arrow_string;
12071 /* Return 1 if there are any overlay-arrows in current_buffer. */
12072 static int
12073 overlay_arrow_in_current_buffer_p (void)
12075 Lisp_Object vlist;
12077 for (vlist = Voverlay_arrow_variable_list;
12078 CONSP (vlist);
12079 vlist = XCDR (vlist))
12081 Lisp_Object var = XCAR (vlist);
12082 Lisp_Object val;
12084 if (!SYMBOLP (var))
12085 continue;
12086 val = find_symbol_value (var);
12087 if (MARKERP (val)
12088 && current_buffer == XMARKER (val)->buffer)
12089 return 1;
12091 return 0;
12095 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12096 has changed. */
12098 static int
12099 overlay_arrows_changed_p (void)
12101 Lisp_Object vlist;
12103 for (vlist = Voverlay_arrow_variable_list;
12104 CONSP (vlist);
12105 vlist = XCDR (vlist))
12107 Lisp_Object var = XCAR (vlist);
12108 Lisp_Object val, pstr;
12110 if (!SYMBOLP (var))
12111 continue;
12112 val = find_symbol_value (var);
12113 if (!MARKERP (val))
12114 continue;
12115 if (! EQ (COERCE_MARKER (val),
12116 Fget (var, Qlast_arrow_position))
12117 || ! (pstr = overlay_arrow_string_or_property (var),
12118 EQ (pstr, Fget (var, Qlast_arrow_string))))
12119 return 1;
12121 return 0;
12124 /* Mark overlay arrows to be updated on next redisplay. */
12126 static void
12127 update_overlay_arrows (int up_to_date)
12129 Lisp_Object vlist;
12131 for (vlist = Voverlay_arrow_variable_list;
12132 CONSP (vlist);
12133 vlist = XCDR (vlist))
12135 Lisp_Object var = XCAR (vlist);
12137 if (!SYMBOLP (var))
12138 continue;
12140 if (up_to_date > 0)
12142 Lisp_Object val = find_symbol_value (var);
12143 Fput (var, Qlast_arrow_position,
12144 COERCE_MARKER (val));
12145 Fput (var, Qlast_arrow_string,
12146 overlay_arrow_string_or_property (var));
12148 else if (up_to_date < 0
12149 || !NILP (Fget (var, Qlast_arrow_position)))
12151 Fput (var, Qlast_arrow_position, Qt);
12152 Fput (var, Qlast_arrow_string, Qt);
12158 /* Return overlay arrow string to display at row.
12159 Return integer (bitmap number) for arrow bitmap in left fringe.
12160 Return nil if no overlay arrow. */
12162 static Lisp_Object
12163 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12165 Lisp_Object vlist;
12167 for (vlist = Voverlay_arrow_variable_list;
12168 CONSP (vlist);
12169 vlist = XCDR (vlist))
12171 Lisp_Object var = XCAR (vlist);
12172 Lisp_Object val;
12174 if (!SYMBOLP (var))
12175 continue;
12177 val = find_symbol_value (var);
12179 if (MARKERP (val)
12180 && current_buffer == XMARKER (val)->buffer
12181 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12183 if (FRAME_WINDOW_P (it->f)
12184 /* FIXME: if ROW->reversed_p is set, this should test
12185 the right fringe, not the left one. */
12186 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12188 #ifdef HAVE_WINDOW_SYSTEM
12189 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12191 int fringe_bitmap;
12192 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12193 return make_number (fringe_bitmap);
12195 #endif
12196 return make_number (-1); /* Use default arrow bitmap */
12198 return overlay_arrow_string_or_property (var);
12202 return Qnil;
12205 /* Return 1 if point moved out of or into a composition. Otherwise
12206 return 0. PREV_BUF and PREV_PT are the last point buffer and
12207 position. BUF and PT are the current point buffer and position. */
12209 static int
12210 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
12211 struct buffer *buf, EMACS_INT pt)
12213 EMACS_INT start, end;
12214 Lisp_Object prop;
12215 Lisp_Object buffer;
12217 XSETBUFFER (buffer, buf);
12218 /* Check a composition at the last point if point moved within the
12219 same buffer. */
12220 if (prev_buf == buf)
12222 if (prev_pt == pt)
12223 /* Point didn't move. */
12224 return 0;
12226 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12227 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12228 && COMPOSITION_VALID_P (start, end, prop)
12229 && start < prev_pt && end > prev_pt)
12230 /* The last point was within the composition. Return 1 iff
12231 point moved out of the composition. */
12232 return (pt <= start || pt >= end);
12235 /* Check a composition at the current point. */
12236 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12237 && find_composition (pt, -1, &start, &end, &prop, buffer)
12238 && COMPOSITION_VALID_P (start, end, prop)
12239 && start < pt && end > pt);
12243 /* Reconsider the setting of B->clip_changed which is displayed
12244 in window W. */
12246 static inline void
12247 reconsider_clip_changes (struct window *w, struct buffer *b)
12249 if (b->clip_changed
12250 && !NILP (w->window_end_valid)
12251 && w->current_matrix->buffer == b
12252 && w->current_matrix->zv == BUF_ZV (b)
12253 && w->current_matrix->begv == BUF_BEGV (b))
12254 b->clip_changed = 0;
12256 /* If display wasn't paused, and W is not a tool bar window, see if
12257 point has been moved into or out of a composition. In that case,
12258 we set b->clip_changed to 1 to force updating the screen. If
12259 b->clip_changed has already been set to 1, we can skip this
12260 check. */
12261 if (!b->clip_changed
12262 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12264 EMACS_INT pt;
12266 if (w == XWINDOW (selected_window))
12267 pt = PT;
12268 else
12269 pt = marker_position (w->pointm);
12271 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12272 || pt != XINT (w->last_point))
12273 && check_point_in_composition (w->current_matrix->buffer,
12274 XINT (w->last_point),
12275 XBUFFER (w->buffer), pt))
12276 b->clip_changed = 1;
12281 /* Select FRAME to forward the values of frame-local variables into C
12282 variables so that the redisplay routines can access those values
12283 directly. */
12285 static void
12286 select_frame_for_redisplay (Lisp_Object frame)
12288 Lisp_Object tail, tem;
12289 Lisp_Object old = selected_frame;
12290 struct Lisp_Symbol *sym;
12292 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12294 selected_frame = frame;
12296 do {
12297 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12298 if (CONSP (XCAR (tail))
12299 && (tem = XCAR (XCAR (tail)),
12300 SYMBOLP (tem))
12301 && (sym = indirect_variable (XSYMBOL (tem)),
12302 sym->redirect == SYMBOL_LOCALIZED)
12303 && sym->val.blv->frame_local)
12304 /* Use find_symbol_value rather than Fsymbol_value
12305 to avoid an error if it is void. */
12306 find_symbol_value (tem);
12307 } while (!EQ (frame, old) && (frame = old, 1));
12311 #define STOP_POLLING \
12312 do { if (! polling_stopped_here) stop_polling (); \
12313 polling_stopped_here = 1; } while (0)
12315 #define RESUME_POLLING \
12316 do { if (polling_stopped_here) start_polling (); \
12317 polling_stopped_here = 0; } while (0)
12320 /* Perhaps in the future avoid recentering windows if it
12321 is not necessary; currently that causes some problems. */
12323 static void
12324 redisplay_internal (void)
12326 struct window *w = XWINDOW (selected_window);
12327 struct window *sw;
12328 struct frame *fr;
12329 int pending;
12330 int must_finish = 0;
12331 struct text_pos tlbufpos, tlendpos;
12332 int number_of_visible_frames;
12333 int count, count1;
12334 struct frame *sf;
12335 int polling_stopped_here = 0;
12336 Lisp_Object old_frame = selected_frame;
12338 /* Non-zero means redisplay has to consider all windows on all
12339 frames. Zero means, only selected_window is considered. */
12340 int consider_all_windows_p;
12342 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12344 /* No redisplay if running in batch mode or frame is not yet fully
12345 initialized, or redisplay is explicitly turned off by setting
12346 Vinhibit_redisplay. */
12347 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12348 || !NILP (Vinhibit_redisplay))
12349 return;
12351 /* Don't examine these until after testing Vinhibit_redisplay.
12352 When Emacs is shutting down, perhaps because its connection to
12353 X has dropped, we should not look at them at all. */
12354 fr = XFRAME (w->frame);
12355 sf = SELECTED_FRAME ();
12357 if (!fr->glyphs_initialized_p)
12358 return;
12360 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12361 if (popup_activated ())
12362 return;
12363 #endif
12365 /* I don't think this happens but let's be paranoid. */
12366 if (redisplaying_p)
12367 return;
12369 /* Record a function that resets redisplaying_p to its old value
12370 when we leave this function. */
12371 count = SPECPDL_INDEX ();
12372 record_unwind_protect (unwind_redisplay,
12373 Fcons (make_number (redisplaying_p), selected_frame));
12374 ++redisplaying_p;
12375 specbind (Qinhibit_free_realized_faces, Qnil);
12378 Lisp_Object tail, frame;
12380 FOR_EACH_FRAME (tail, frame)
12382 struct frame *f = XFRAME (frame);
12383 f->already_hscrolled_p = 0;
12387 retry:
12388 /* Remember the currently selected window. */
12389 sw = w;
12391 if (!EQ (old_frame, selected_frame)
12392 && FRAME_LIVE_P (XFRAME (old_frame)))
12393 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12394 selected_frame and selected_window to be temporarily out-of-sync so
12395 when we come back here via `goto retry', we need to resync because we
12396 may need to run Elisp code (via prepare_menu_bars). */
12397 select_frame_for_redisplay (old_frame);
12399 pending = 0;
12400 reconsider_clip_changes (w, current_buffer);
12401 last_escape_glyph_frame = NULL;
12402 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12403 last_glyphless_glyph_frame = NULL;
12404 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12406 /* If new fonts have been loaded that make a glyph matrix adjustment
12407 necessary, do it. */
12408 if (fonts_changed_p)
12410 adjust_glyphs (NULL);
12411 ++windows_or_buffers_changed;
12412 fonts_changed_p = 0;
12415 /* If face_change_count is non-zero, init_iterator will free all
12416 realized faces, which includes the faces referenced from current
12417 matrices. So, we can't reuse current matrices in this case. */
12418 if (face_change_count)
12419 ++windows_or_buffers_changed;
12421 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12422 && FRAME_TTY (sf)->previous_frame != sf)
12424 /* Since frames on a single ASCII terminal share the same
12425 display area, displaying a different frame means redisplay
12426 the whole thing. */
12427 windows_or_buffers_changed++;
12428 SET_FRAME_GARBAGED (sf);
12429 #ifndef DOS_NT
12430 set_tty_color_mode (FRAME_TTY (sf), sf);
12431 #endif
12432 FRAME_TTY (sf)->previous_frame = sf;
12435 /* Set the visible flags for all frames. Do this before checking
12436 for resized or garbaged frames; they want to know if their frames
12437 are visible. See the comment in frame.h for
12438 FRAME_SAMPLE_VISIBILITY. */
12440 Lisp_Object tail, frame;
12442 number_of_visible_frames = 0;
12444 FOR_EACH_FRAME (tail, frame)
12446 struct frame *f = XFRAME (frame);
12448 FRAME_SAMPLE_VISIBILITY (f);
12449 if (FRAME_VISIBLE_P (f))
12450 ++number_of_visible_frames;
12451 clear_desired_matrices (f);
12455 /* Notice any pending interrupt request to change frame size. */
12456 do_pending_window_change (1);
12458 /* do_pending_window_change could change the selected_window due to
12459 frame resizing which makes the selected window too small. */
12460 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12462 sw = w;
12463 reconsider_clip_changes (w, current_buffer);
12466 /* Clear frames marked as garbaged. */
12467 if (frame_garbaged)
12468 clear_garbaged_frames ();
12470 /* Build menubar and tool-bar items. */
12471 if (NILP (Vmemory_full))
12472 prepare_menu_bars ();
12474 if (windows_or_buffers_changed)
12475 update_mode_lines++;
12477 /* Detect case that we need to write or remove a star in the mode line. */
12478 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
12480 w->update_mode_line = Qt;
12481 if (buffer_shared > 1)
12482 update_mode_lines++;
12485 /* Avoid invocation of point motion hooks by `current_column' below. */
12486 count1 = SPECPDL_INDEX ();
12487 specbind (Qinhibit_point_motion_hooks, Qt);
12489 /* If %c is in the mode line, update it if needed. */
12490 if (!NILP (w->column_number_displayed)
12491 /* This alternative quickly identifies a common case
12492 where no change is needed. */
12493 && !(PT == XFASTINT (w->last_point)
12494 && XFASTINT (w->last_modified) >= MODIFF
12495 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12496 && (XFASTINT (w->column_number_displayed) != current_column ()))
12497 w->update_mode_line = Qt;
12499 unbind_to (count1, Qnil);
12501 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12503 /* The variable buffer_shared is set in redisplay_window and
12504 indicates that we redisplay a buffer in different windows. See
12505 there. */
12506 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
12507 || cursor_type_changed);
12509 /* If specs for an arrow have changed, do thorough redisplay
12510 to ensure we remove any arrow that should no longer exist. */
12511 if (overlay_arrows_changed_p ())
12512 consider_all_windows_p = windows_or_buffers_changed = 1;
12514 /* Normally the message* functions will have already displayed and
12515 updated the echo area, but the frame may have been trashed, or
12516 the update may have been preempted, so display the echo area
12517 again here. Checking message_cleared_p captures the case that
12518 the echo area should be cleared. */
12519 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12520 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12521 || (message_cleared_p
12522 && minibuf_level == 0
12523 /* If the mini-window is currently selected, this means the
12524 echo-area doesn't show through. */
12525 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12527 int window_height_changed_p = echo_area_display (0);
12528 must_finish = 1;
12530 /* If we don't display the current message, don't clear the
12531 message_cleared_p flag, because, if we did, we wouldn't clear
12532 the echo area in the next redisplay which doesn't preserve
12533 the echo area. */
12534 if (!display_last_displayed_message_p)
12535 message_cleared_p = 0;
12537 if (fonts_changed_p)
12538 goto retry;
12539 else if (window_height_changed_p)
12541 consider_all_windows_p = 1;
12542 ++update_mode_lines;
12543 ++windows_or_buffers_changed;
12545 /* If window configuration was changed, frames may have been
12546 marked garbaged. Clear them or we will experience
12547 surprises wrt scrolling. */
12548 if (frame_garbaged)
12549 clear_garbaged_frames ();
12552 else if (EQ (selected_window, minibuf_window)
12553 && (current_buffer->clip_changed
12554 || XFASTINT (w->last_modified) < MODIFF
12555 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12556 && resize_mini_window (w, 0))
12558 /* Resized active mini-window to fit the size of what it is
12559 showing if its contents might have changed. */
12560 must_finish = 1;
12561 /* FIXME: this causes all frames to be updated, which seems unnecessary
12562 since only the current frame needs to be considered. This function needs
12563 to be rewritten with two variables, consider_all_windows and
12564 consider_all_frames. */
12565 consider_all_windows_p = 1;
12566 ++windows_or_buffers_changed;
12567 ++update_mode_lines;
12569 /* If window configuration was changed, frames may have been
12570 marked garbaged. Clear them or we will experience
12571 surprises wrt scrolling. */
12572 if (frame_garbaged)
12573 clear_garbaged_frames ();
12577 /* If showing the region, and mark has changed, we must redisplay
12578 the whole window. The assignment to this_line_start_pos prevents
12579 the optimization directly below this if-statement. */
12580 if (((!NILP (Vtransient_mark_mode)
12581 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
12582 != !NILP (w->region_showing))
12583 || (!NILP (w->region_showing)
12584 && !EQ (w->region_showing,
12585 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
12586 CHARPOS (this_line_start_pos) = 0;
12588 /* Optimize the case that only the line containing the cursor in the
12589 selected window has changed. Variables starting with this_ are
12590 set in display_line and record information about the line
12591 containing the cursor. */
12592 tlbufpos = this_line_start_pos;
12593 tlendpos = this_line_end_pos;
12594 if (!consider_all_windows_p
12595 && CHARPOS (tlbufpos) > 0
12596 && NILP (w->update_mode_line)
12597 && !current_buffer->clip_changed
12598 && !current_buffer->prevent_redisplay_optimizations_p
12599 && FRAME_VISIBLE_P (XFRAME (w->frame))
12600 && !FRAME_OBSCURED_P (XFRAME (w->frame))
12601 /* Make sure recorded data applies to current buffer, etc. */
12602 && this_line_buffer == current_buffer
12603 && current_buffer == XBUFFER (w->buffer)
12604 && NILP (w->force_start)
12605 && NILP (w->optional_new_start)
12606 /* Point must be on the line that we have info recorded about. */
12607 && PT >= CHARPOS (tlbufpos)
12608 && PT <= Z - CHARPOS (tlendpos)
12609 /* All text outside that line, including its final newline,
12610 must be unchanged. */
12611 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
12612 CHARPOS (tlendpos)))
12614 if (CHARPOS (tlbufpos) > BEGV
12615 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
12616 && (CHARPOS (tlbufpos) == ZV
12617 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
12618 /* Former continuation line has disappeared by becoming empty. */
12619 goto cancel;
12620 else if (XFASTINT (w->last_modified) < MODIFF
12621 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
12622 || MINI_WINDOW_P (w))
12624 /* We have to handle the case of continuation around a
12625 wide-column character (see the comment in indent.c around
12626 line 1340).
12628 For instance, in the following case:
12630 -------- Insert --------
12631 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
12632 J_I_ ==> J_I_ `^^' are cursors.
12633 ^^ ^^
12634 -------- --------
12636 As we have to redraw the line above, we cannot use this
12637 optimization. */
12639 struct it it;
12640 int line_height_before = this_line_pixel_height;
12642 /* Note that start_display will handle the case that the
12643 line starting at tlbufpos is a continuation line. */
12644 start_display (&it, w, tlbufpos);
12646 /* Implementation note: It this still necessary? */
12647 if (it.current_x != this_line_start_x)
12648 goto cancel;
12650 TRACE ((stderr, "trying display optimization 1\n"));
12651 w->cursor.vpos = -1;
12652 overlay_arrow_seen = 0;
12653 it.vpos = this_line_vpos;
12654 it.current_y = this_line_y;
12655 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
12656 display_line (&it);
12658 /* If line contains point, is not continued,
12659 and ends at same distance from eob as before, we win. */
12660 if (w->cursor.vpos >= 0
12661 /* Line is not continued, otherwise this_line_start_pos
12662 would have been set to 0 in display_line. */
12663 && CHARPOS (this_line_start_pos)
12664 /* Line ends as before. */
12665 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
12666 /* Line has same height as before. Otherwise other lines
12667 would have to be shifted up or down. */
12668 && this_line_pixel_height == line_height_before)
12670 /* If this is not the window's last line, we must adjust
12671 the charstarts of the lines below. */
12672 if (it.current_y < it.last_visible_y)
12674 struct glyph_row *row
12675 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
12676 EMACS_INT delta, delta_bytes;
12678 /* We used to distinguish between two cases here,
12679 conditioned by Z - CHARPOS (tlendpos) == ZV, for
12680 when the line ends in a newline or the end of the
12681 buffer's accessible portion. But both cases did
12682 the same, so they were collapsed. */
12683 delta = (Z
12684 - CHARPOS (tlendpos)
12685 - MATRIX_ROW_START_CHARPOS (row));
12686 delta_bytes = (Z_BYTE
12687 - BYTEPOS (tlendpos)
12688 - MATRIX_ROW_START_BYTEPOS (row));
12690 increment_matrix_positions (w->current_matrix,
12691 this_line_vpos + 1,
12692 w->current_matrix->nrows,
12693 delta, delta_bytes);
12696 /* If this row displays text now but previously didn't,
12697 or vice versa, w->window_end_vpos may have to be
12698 adjusted. */
12699 if ((it.glyph_row - 1)->displays_text_p)
12701 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
12702 XSETINT (w->window_end_vpos, this_line_vpos);
12704 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
12705 && this_line_vpos > 0)
12706 XSETINT (w->window_end_vpos, this_line_vpos - 1);
12707 w->window_end_valid = Qnil;
12709 /* Update hint: No need to try to scroll in update_window. */
12710 w->desired_matrix->no_scrolling_p = 1;
12712 #if GLYPH_DEBUG
12713 *w->desired_matrix->method = 0;
12714 debug_method_add (w, "optimization 1");
12715 #endif
12716 #ifdef HAVE_WINDOW_SYSTEM
12717 update_window_fringes (w, 0);
12718 #endif
12719 goto update;
12721 else
12722 goto cancel;
12724 else if (/* Cursor position hasn't changed. */
12725 PT == XFASTINT (w->last_point)
12726 /* Make sure the cursor was last displayed
12727 in this window. Otherwise we have to reposition it. */
12728 && 0 <= w->cursor.vpos
12729 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
12731 if (!must_finish)
12733 do_pending_window_change (1);
12734 /* If selected_window changed, redisplay again. */
12735 if (WINDOWP (selected_window)
12736 && (w = XWINDOW (selected_window)) != sw)
12737 goto retry;
12739 /* We used to always goto end_of_redisplay here, but this
12740 isn't enough if we have a blinking cursor. */
12741 if (w->cursor_off_p == w->last_cursor_off_p)
12742 goto end_of_redisplay;
12744 goto update;
12746 /* If highlighting the region, or if the cursor is in the echo area,
12747 then we can't just move the cursor. */
12748 else if (! (!NILP (Vtransient_mark_mode)
12749 && !NILP (BVAR (current_buffer, mark_active)))
12750 && (EQ (selected_window, BVAR (current_buffer, last_selected_window))
12751 || highlight_nonselected_windows)
12752 && NILP (w->region_showing)
12753 && NILP (Vshow_trailing_whitespace)
12754 && !cursor_in_echo_area)
12756 struct it it;
12757 struct glyph_row *row;
12759 /* Skip from tlbufpos to PT and see where it is. Note that
12760 PT may be in invisible text. If so, we will end at the
12761 next visible position. */
12762 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
12763 NULL, DEFAULT_FACE_ID);
12764 it.current_x = this_line_start_x;
12765 it.current_y = this_line_y;
12766 it.vpos = this_line_vpos;
12768 /* The call to move_it_to stops in front of PT, but
12769 moves over before-strings. */
12770 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
12772 if (it.vpos == this_line_vpos
12773 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
12774 row->enabled_p))
12776 xassert (this_line_vpos == it.vpos);
12777 xassert (this_line_y == it.current_y);
12778 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12779 #if GLYPH_DEBUG
12780 *w->desired_matrix->method = 0;
12781 debug_method_add (w, "optimization 3");
12782 #endif
12783 goto update;
12785 else
12786 goto cancel;
12789 cancel:
12790 /* Text changed drastically or point moved off of line. */
12791 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
12794 CHARPOS (this_line_start_pos) = 0;
12795 consider_all_windows_p |= buffer_shared > 1;
12796 ++clear_face_cache_count;
12797 #ifdef HAVE_WINDOW_SYSTEM
12798 ++clear_image_cache_count;
12799 #endif
12801 /* Build desired matrices, and update the display. If
12802 consider_all_windows_p is non-zero, do it for all windows on all
12803 frames. Otherwise do it for selected_window, only. */
12805 if (consider_all_windows_p)
12807 Lisp_Object tail, frame;
12809 FOR_EACH_FRAME (tail, frame)
12810 XFRAME (frame)->updated_p = 0;
12812 /* Recompute # windows showing selected buffer. This will be
12813 incremented each time such a window is displayed. */
12814 buffer_shared = 0;
12816 FOR_EACH_FRAME (tail, frame)
12818 struct frame *f = XFRAME (frame);
12820 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
12822 if (! EQ (frame, selected_frame))
12823 /* Select the frame, for the sake of frame-local
12824 variables. */
12825 select_frame_for_redisplay (frame);
12827 /* Mark all the scroll bars to be removed; we'll redeem
12828 the ones we want when we redisplay their windows. */
12829 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
12830 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
12832 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12833 redisplay_windows (FRAME_ROOT_WINDOW (f));
12835 /* The X error handler may have deleted that frame. */
12836 if (!FRAME_LIVE_P (f))
12837 continue;
12839 /* Any scroll bars which redisplay_windows should have
12840 nuked should now go away. */
12841 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12842 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12844 /* If fonts changed, display again. */
12845 /* ??? rms: I suspect it is a mistake to jump all the way
12846 back to retry here. It should just retry this frame. */
12847 if (fonts_changed_p)
12848 goto retry;
12850 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12852 /* See if we have to hscroll. */
12853 if (!f->already_hscrolled_p)
12855 f->already_hscrolled_p = 1;
12856 if (hscroll_windows (f->root_window))
12857 goto retry;
12860 /* Prevent various kinds of signals during display
12861 update. stdio is not robust about handling
12862 signals, which can cause an apparent I/O
12863 error. */
12864 if (interrupt_input)
12865 unrequest_sigio ();
12866 STOP_POLLING;
12868 /* Update the display. */
12869 set_window_update_flags (XWINDOW (f->root_window), 1);
12870 pending |= update_frame (f, 0, 0);
12871 f->updated_p = 1;
12876 if (!EQ (old_frame, selected_frame)
12877 && FRAME_LIVE_P (XFRAME (old_frame)))
12878 /* We played a bit fast-and-loose above and allowed selected_frame
12879 and selected_window to be temporarily out-of-sync but let's make
12880 sure this stays contained. */
12881 select_frame_for_redisplay (old_frame);
12882 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12884 if (!pending)
12886 /* Do the mark_window_display_accurate after all windows have
12887 been redisplayed because this call resets flags in buffers
12888 which are needed for proper redisplay. */
12889 FOR_EACH_FRAME (tail, frame)
12891 struct frame *f = XFRAME (frame);
12892 if (f->updated_p)
12894 mark_window_display_accurate (f->root_window, 1);
12895 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12896 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12901 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12903 Lisp_Object mini_window;
12904 struct frame *mini_frame;
12906 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12907 /* Use list_of_error, not Qerror, so that
12908 we catch only errors and don't run the debugger. */
12909 internal_condition_case_1 (redisplay_window_1, selected_window,
12910 list_of_error,
12911 redisplay_window_error);
12913 /* Compare desired and current matrices, perform output. */
12915 update:
12916 /* If fonts changed, display again. */
12917 if (fonts_changed_p)
12918 goto retry;
12920 /* Prevent various kinds of signals during display update.
12921 stdio is not robust about handling signals,
12922 which can cause an apparent I/O error. */
12923 if (interrupt_input)
12924 unrequest_sigio ();
12925 STOP_POLLING;
12927 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12929 if (hscroll_windows (selected_window))
12930 goto retry;
12932 XWINDOW (selected_window)->must_be_updated_p = 1;
12933 pending = update_frame (sf, 0, 0);
12936 /* We may have called echo_area_display at the top of this
12937 function. If the echo area is on another frame, that may
12938 have put text on a frame other than the selected one, so the
12939 above call to update_frame would not have caught it. Catch
12940 it here. */
12941 mini_window = FRAME_MINIBUF_WINDOW (sf);
12942 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12944 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12946 XWINDOW (mini_window)->must_be_updated_p = 1;
12947 pending |= update_frame (mini_frame, 0, 0);
12948 if (!pending && hscroll_windows (mini_window))
12949 goto retry;
12953 /* If display was paused because of pending input, make sure we do a
12954 thorough update the next time. */
12955 if (pending)
12957 /* Prevent the optimization at the beginning of
12958 redisplay_internal that tries a single-line update of the
12959 line containing the cursor in the selected window. */
12960 CHARPOS (this_line_start_pos) = 0;
12962 /* Let the overlay arrow be updated the next time. */
12963 update_overlay_arrows (0);
12965 /* If we pause after scrolling, some rows in the current
12966 matrices of some windows are not valid. */
12967 if (!WINDOW_FULL_WIDTH_P (w)
12968 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12969 update_mode_lines = 1;
12971 else
12973 if (!consider_all_windows_p)
12975 /* This has already been done above if
12976 consider_all_windows_p is set. */
12977 mark_window_display_accurate_1 (w, 1);
12979 /* Say overlay arrows are up to date. */
12980 update_overlay_arrows (1);
12982 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12983 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12986 update_mode_lines = 0;
12987 windows_or_buffers_changed = 0;
12988 cursor_type_changed = 0;
12991 /* Start SIGIO interrupts coming again. Having them off during the
12992 code above makes it less likely one will discard output, but not
12993 impossible, since there might be stuff in the system buffer here.
12994 But it is much hairier to try to do anything about that. */
12995 if (interrupt_input)
12996 request_sigio ();
12997 RESUME_POLLING;
12999 /* If a frame has become visible which was not before, redisplay
13000 again, so that we display it. Expose events for such a frame
13001 (which it gets when becoming visible) don't call the parts of
13002 redisplay constructing glyphs, so simply exposing a frame won't
13003 display anything in this case. So, we have to display these
13004 frames here explicitly. */
13005 if (!pending)
13007 Lisp_Object tail, frame;
13008 int new_count = 0;
13010 FOR_EACH_FRAME (tail, frame)
13012 int this_is_visible = 0;
13014 if (XFRAME (frame)->visible)
13015 this_is_visible = 1;
13016 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13017 if (XFRAME (frame)->visible)
13018 this_is_visible = 1;
13020 if (this_is_visible)
13021 new_count++;
13024 if (new_count != number_of_visible_frames)
13025 windows_or_buffers_changed++;
13028 /* Change frame size now if a change is pending. */
13029 do_pending_window_change (1);
13031 /* If we just did a pending size change, or have additional
13032 visible frames, or selected_window changed, redisplay again. */
13033 if ((windows_or_buffers_changed && !pending)
13034 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13035 goto retry;
13037 /* Clear the face and image caches.
13039 We used to do this only if consider_all_windows_p. But the cache
13040 needs to be cleared if a timer creates images in the current
13041 buffer (e.g. the test case in Bug#6230). */
13043 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13045 clear_face_cache (0);
13046 clear_face_cache_count = 0;
13049 #ifdef HAVE_WINDOW_SYSTEM
13050 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13052 clear_image_caches (Qnil);
13053 clear_image_cache_count = 0;
13055 #endif /* HAVE_WINDOW_SYSTEM */
13057 end_of_redisplay:
13058 unbind_to (count, Qnil);
13059 RESUME_POLLING;
13063 /* Redisplay, but leave alone any recent echo area message unless
13064 another message has been requested in its place.
13066 This is useful in situations where you need to redisplay but no
13067 user action has occurred, making it inappropriate for the message
13068 area to be cleared. See tracking_off and
13069 wait_reading_process_output for examples of these situations.
13071 FROM_WHERE is an integer saying from where this function was
13072 called. This is useful for debugging. */
13074 void
13075 redisplay_preserve_echo_area (int from_where)
13077 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13079 if (!NILP (echo_area_buffer[1]))
13081 /* We have a previously displayed message, but no current
13082 message. Redisplay the previous message. */
13083 display_last_displayed_message_p = 1;
13084 redisplay_internal ();
13085 display_last_displayed_message_p = 0;
13087 else
13088 redisplay_internal ();
13090 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13091 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13092 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13096 /* Function registered with record_unwind_protect in
13097 redisplay_internal. Reset redisplaying_p to the value it had
13098 before redisplay_internal was called, and clear
13099 prevent_freeing_realized_faces_p. It also selects the previously
13100 selected frame, unless it has been deleted (by an X connection
13101 failure during redisplay, for example). */
13103 static Lisp_Object
13104 unwind_redisplay (Lisp_Object val)
13106 Lisp_Object old_redisplaying_p, old_frame;
13108 old_redisplaying_p = XCAR (val);
13109 redisplaying_p = XFASTINT (old_redisplaying_p);
13110 old_frame = XCDR (val);
13111 if (! EQ (old_frame, selected_frame)
13112 && FRAME_LIVE_P (XFRAME (old_frame)))
13113 select_frame_for_redisplay (old_frame);
13114 return Qnil;
13118 /* Mark the display of window W as accurate or inaccurate. If
13119 ACCURATE_P is non-zero mark display of W as accurate. If
13120 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13121 redisplay_internal is called. */
13123 static void
13124 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13126 if (BUFFERP (w->buffer))
13128 struct buffer *b = XBUFFER (w->buffer);
13130 w->last_modified
13131 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
13132 w->last_overlay_modified
13133 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
13134 w->last_had_star
13135 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
13137 if (accurate_p)
13139 b->clip_changed = 0;
13140 b->prevent_redisplay_optimizations_p = 0;
13142 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13143 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13144 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13145 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13147 w->current_matrix->buffer = b;
13148 w->current_matrix->begv = BUF_BEGV (b);
13149 w->current_matrix->zv = BUF_ZV (b);
13151 w->last_cursor = w->cursor;
13152 w->last_cursor_off_p = w->cursor_off_p;
13154 if (w == XWINDOW (selected_window))
13155 w->last_point = make_number (BUF_PT (b));
13156 else
13157 w->last_point = make_number (XMARKER (w->pointm)->charpos);
13161 if (accurate_p)
13163 w->window_end_valid = w->buffer;
13164 w->update_mode_line = Qnil;
13169 /* Mark the display of windows in the window tree rooted at WINDOW as
13170 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13171 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13172 be redisplayed the next time redisplay_internal is called. */
13174 void
13175 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13177 struct window *w;
13179 for (; !NILP (window); window = w->next)
13181 w = XWINDOW (window);
13182 mark_window_display_accurate_1 (w, accurate_p);
13184 if (!NILP (w->vchild))
13185 mark_window_display_accurate (w->vchild, accurate_p);
13186 if (!NILP (w->hchild))
13187 mark_window_display_accurate (w->hchild, accurate_p);
13190 if (accurate_p)
13192 update_overlay_arrows (1);
13194 else
13196 /* Force a thorough redisplay the next time by setting
13197 last_arrow_position and last_arrow_string to t, which is
13198 unequal to any useful value of Voverlay_arrow_... */
13199 update_overlay_arrows (-1);
13204 /* Return value in display table DP (Lisp_Char_Table *) for character
13205 C. Since a display table doesn't have any parent, we don't have to
13206 follow parent. Do not call this function directly but use the
13207 macro DISP_CHAR_VECTOR. */
13209 Lisp_Object
13210 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13212 Lisp_Object val;
13214 if (ASCII_CHAR_P (c))
13216 val = dp->ascii;
13217 if (SUB_CHAR_TABLE_P (val))
13218 val = XSUB_CHAR_TABLE (val)->contents[c];
13220 else
13222 Lisp_Object table;
13224 XSETCHAR_TABLE (table, dp);
13225 val = char_table_ref (table, c);
13227 if (NILP (val))
13228 val = dp->defalt;
13229 return val;
13234 /***********************************************************************
13235 Window Redisplay
13236 ***********************************************************************/
13238 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13240 static void
13241 redisplay_windows (Lisp_Object window)
13243 while (!NILP (window))
13245 struct window *w = XWINDOW (window);
13247 if (!NILP (w->hchild))
13248 redisplay_windows (w->hchild);
13249 else if (!NILP (w->vchild))
13250 redisplay_windows (w->vchild);
13251 else if (!NILP (w->buffer))
13253 displayed_buffer = XBUFFER (w->buffer);
13254 /* Use list_of_error, not Qerror, so that
13255 we catch only errors and don't run the debugger. */
13256 internal_condition_case_1 (redisplay_window_0, window,
13257 list_of_error,
13258 redisplay_window_error);
13261 window = w->next;
13265 static Lisp_Object
13266 redisplay_window_error (Lisp_Object ignore)
13268 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13269 return Qnil;
13272 static Lisp_Object
13273 redisplay_window_0 (Lisp_Object window)
13275 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13276 redisplay_window (window, 0);
13277 return Qnil;
13280 static Lisp_Object
13281 redisplay_window_1 (Lisp_Object window)
13283 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13284 redisplay_window (window, 1);
13285 return Qnil;
13289 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13290 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13291 which positions recorded in ROW differ from current buffer
13292 positions.
13294 Return 0 if cursor is not on this row, 1 otherwise. */
13296 static int
13297 set_cursor_from_row (struct window *w, struct glyph_row *row,
13298 struct glyph_matrix *matrix,
13299 EMACS_INT delta, EMACS_INT delta_bytes,
13300 int dy, int dvpos)
13302 struct glyph *glyph = row->glyphs[TEXT_AREA];
13303 struct glyph *end = glyph + row->used[TEXT_AREA];
13304 struct glyph *cursor = NULL;
13305 /* The last known character position in row. */
13306 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13307 int x = row->x;
13308 EMACS_INT pt_old = PT - delta;
13309 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13310 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13311 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13312 /* A glyph beyond the edge of TEXT_AREA which we should never
13313 touch. */
13314 struct glyph *glyphs_end = end;
13315 /* Non-zero means we've found a match for cursor position, but that
13316 glyph has the avoid_cursor_p flag set. */
13317 int match_with_avoid_cursor = 0;
13318 /* Non-zero means we've seen at least one glyph that came from a
13319 display string. */
13320 int string_seen = 0;
13321 /* Largest and smalles buffer positions seen so far during scan of
13322 glyph row. */
13323 EMACS_INT bpos_max = pos_before;
13324 EMACS_INT bpos_min = pos_after;
13325 /* Last buffer position covered by an overlay string with an integer
13326 `cursor' property. */
13327 EMACS_INT bpos_covered = 0;
13328 /* Non-zero means the display string on which to display the cursor
13329 comes from a text property, not from an overlay. */
13330 int string_from_text_prop = 0;
13332 /* Skip over glyphs not having an object at the start and the end of
13333 the row. These are special glyphs like truncation marks on
13334 terminal frames. */
13335 if (row->displays_text_p)
13337 if (!row->reversed_p)
13339 while (glyph < end
13340 && INTEGERP (glyph->object)
13341 && glyph->charpos < 0)
13343 x += glyph->pixel_width;
13344 ++glyph;
13346 while (end > glyph
13347 && INTEGERP ((end - 1)->object)
13348 /* CHARPOS is zero for blanks and stretch glyphs
13349 inserted by extend_face_to_end_of_line. */
13350 && (end - 1)->charpos <= 0)
13351 --end;
13352 glyph_before = glyph - 1;
13353 glyph_after = end;
13355 else
13357 struct glyph *g;
13359 /* If the glyph row is reversed, we need to process it from back
13360 to front, so swap the edge pointers. */
13361 glyphs_end = end = glyph - 1;
13362 glyph += row->used[TEXT_AREA] - 1;
13364 while (glyph > end + 1
13365 && INTEGERP (glyph->object)
13366 && glyph->charpos < 0)
13368 --glyph;
13369 x -= glyph->pixel_width;
13371 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13372 --glyph;
13373 /* By default, in reversed rows we put the cursor on the
13374 rightmost (first in the reading order) glyph. */
13375 for (g = end + 1; g < glyph; g++)
13376 x += g->pixel_width;
13377 while (end < glyph
13378 && INTEGERP ((end + 1)->object)
13379 && (end + 1)->charpos <= 0)
13380 ++end;
13381 glyph_before = glyph + 1;
13382 glyph_after = end;
13385 else if (row->reversed_p)
13387 /* In R2L rows that don't display text, put the cursor on the
13388 rightmost glyph. Case in point: an empty last line that is
13389 part of an R2L paragraph. */
13390 cursor = end - 1;
13391 /* Avoid placing the cursor on the last glyph of the row, where
13392 on terminal frames we hold the vertical border between
13393 adjacent windows. */
13394 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13395 && !WINDOW_RIGHTMOST_P (w)
13396 && cursor == row->glyphs[LAST_AREA] - 1)
13397 cursor--;
13398 x = -1; /* will be computed below, at label compute_x */
13401 /* Step 1: Try to find the glyph whose character position
13402 corresponds to point. If that's not possible, find 2 glyphs
13403 whose character positions are the closest to point, one before
13404 point, the other after it. */
13405 if (!row->reversed_p)
13406 while (/* not marched to end of glyph row */
13407 glyph < end
13408 /* glyph was not inserted by redisplay for internal purposes */
13409 && !INTEGERP (glyph->object))
13411 if (BUFFERP (glyph->object))
13413 EMACS_INT dpos = glyph->charpos - pt_old;
13415 if (glyph->charpos > bpos_max)
13416 bpos_max = glyph->charpos;
13417 if (glyph->charpos < bpos_min)
13418 bpos_min = glyph->charpos;
13419 if (!glyph->avoid_cursor_p)
13421 /* If we hit point, we've found the glyph on which to
13422 display the cursor. */
13423 if (dpos == 0)
13425 match_with_avoid_cursor = 0;
13426 break;
13428 /* See if we've found a better approximation to
13429 POS_BEFORE or to POS_AFTER. Note that we want the
13430 first (leftmost) glyph of all those that are the
13431 closest from below, and the last (rightmost) of all
13432 those from above. */
13433 if (0 > dpos && dpos > pos_before - pt_old)
13435 pos_before = glyph->charpos;
13436 glyph_before = glyph;
13438 else if (0 < dpos && dpos <= pos_after - pt_old)
13440 pos_after = glyph->charpos;
13441 glyph_after = glyph;
13444 else if (dpos == 0)
13445 match_with_avoid_cursor = 1;
13447 else if (STRINGP (glyph->object))
13449 Lisp_Object chprop;
13450 EMACS_INT glyph_pos = glyph->charpos;
13452 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13453 glyph->object);
13454 if (INTEGERP (chprop))
13456 bpos_covered = bpos_max + XINT (chprop);
13457 /* If the `cursor' property covers buffer positions up
13458 to and including point, we should display cursor on
13459 this glyph. Note that overlays and text properties
13460 with string values stop bidi reordering, so every
13461 buffer position to the left of the string is always
13462 smaller than any position to the right of the
13463 string. Therefore, if a `cursor' property on one
13464 of the string's characters has an integer value, we
13465 will break out of the loop below _before_ we get to
13466 the position match above. IOW, integer values of
13467 the `cursor' property override the "exact match for
13468 point" strategy of positioning the cursor. */
13469 /* Implementation note: bpos_max == pt_old when, e.g.,
13470 we are in an empty line, where bpos_max is set to
13471 MATRIX_ROW_START_CHARPOS, see above. */
13472 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13474 cursor = glyph;
13475 break;
13479 string_seen = 1;
13481 x += glyph->pixel_width;
13482 ++glyph;
13484 else if (glyph > end) /* row is reversed */
13485 while (!INTEGERP (glyph->object))
13487 if (BUFFERP (glyph->object))
13489 EMACS_INT dpos = glyph->charpos - pt_old;
13491 if (glyph->charpos > bpos_max)
13492 bpos_max = glyph->charpos;
13493 if (glyph->charpos < bpos_min)
13494 bpos_min = glyph->charpos;
13495 if (!glyph->avoid_cursor_p)
13497 if (dpos == 0)
13499 match_with_avoid_cursor = 0;
13500 break;
13502 if (0 > dpos && dpos > pos_before - pt_old)
13504 pos_before = glyph->charpos;
13505 glyph_before = glyph;
13507 else if (0 < dpos && dpos <= pos_after - pt_old)
13509 pos_after = glyph->charpos;
13510 glyph_after = glyph;
13513 else if (dpos == 0)
13514 match_with_avoid_cursor = 1;
13516 else if (STRINGP (glyph->object))
13518 Lisp_Object chprop;
13519 EMACS_INT glyph_pos = glyph->charpos;
13521 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13522 glyph->object);
13523 if (INTEGERP (chprop))
13525 bpos_covered = bpos_max + XINT (chprop);
13526 /* If the `cursor' property covers buffer positions up
13527 to and including point, we should display cursor on
13528 this glyph. */
13529 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13531 cursor = glyph;
13532 break;
13535 string_seen = 1;
13537 --glyph;
13538 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
13540 x--; /* can't use any pixel_width */
13541 break;
13543 x -= glyph->pixel_width;
13546 /* Step 2: If we didn't find an exact match for point, we need to
13547 look for a proper place to put the cursor among glyphs between
13548 GLYPH_BEFORE and GLYPH_AFTER. */
13549 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13550 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
13551 && bpos_covered < pt_old)
13553 /* An empty line has a single glyph whose OBJECT is zero and
13554 whose CHARPOS is the position of a newline on that line.
13555 Note that on a TTY, there are more glyphs after that, which
13556 were produced by extend_face_to_end_of_line, but their
13557 CHARPOS is zero or negative. */
13558 int empty_line_p =
13559 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13560 && INTEGERP (glyph->object) && glyph->charpos > 0;
13562 if (row->ends_in_ellipsis_p && pos_after == last_pos)
13564 EMACS_INT ellipsis_pos;
13566 /* Scan back over the ellipsis glyphs. */
13567 if (!row->reversed_p)
13569 ellipsis_pos = (glyph - 1)->charpos;
13570 while (glyph > row->glyphs[TEXT_AREA]
13571 && (glyph - 1)->charpos == ellipsis_pos)
13572 glyph--, x -= glyph->pixel_width;
13573 /* That loop always goes one position too far, including
13574 the glyph before the ellipsis. So scan forward over
13575 that one. */
13576 x += glyph->pixel_width;
13577 glyph++;
13579 else /* row is reversed */
13581 ellipsis_pos = (glyph + 1)->charpos;
13582 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
13583 && (glyph + 1)->charpos == ellipsis_pos)
13584 glyph++, x += glyph->pixel_width;
13585 x -= glyph->pixel_width;
13586 glyph--;
13589 else if (match_with_avoid_cursor
13590 /* A truncated row may not include PT among its
13591 character positions. Setting the cursor inside the
13592 scroll margin will trigger recalculation of hscroll
13593 in hscroll_window_tree. */
13594 || (row->truncated_on_left_p && pt_old < bpos_min)
13595 || (row->truncated_on_right_p && pt_old > bpos_max)
13596 /* Zero-width characters produce no glyphs. */
13597 || (!string_seen
13598 && !empty_line_p
13599 && (row->reversed_p
13600 ? glyph_after > glyphs_end
13601 : glyph_after < glyphs_end)))
13603 cursor = glyph_after;
13604 x = -1;
13606 else if (string_seen)
13608 int incr = row->reversed_p ? -1 : +1;
13610 /* Need to find the glyph that came out of a string which is
13611 present at point. That glyph is somewhere between
13612 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
13613 positioned between POS_BEFORE and POS_AFTER in the
13614 buffer. */
13615 struct glyph *start, *stop;
13616 EMACS_INT pos = pos_before;
13618 x = -1;
13620 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
13621 correspond to POS_BEFORE and POS_AFTER, respectively. We
13622 need START and STOP in the order that corresponds to the
13623 row's direction as given by its reversed_p flag. If the
13624 directionality of characters between POS_BEFORE and
13625 POS_AFTER is the opposite of the row's base direction,
13626 these characters will have been reordered for display,
13627 and we need to reverse START and STOP. */
13628 if (!row->reversed_p)
13630 start = min (glyph_before, glyph_after);
13631 stop = max (glyph_before, glyph_after);
13633 else
13635 start = max (glyph_before, glyph_after);
13636 stop = min (glyph_before, glyph_after);
13638 for (glyph = start + incr;
13639 row->reversed_p ? glyph > stop : glyph < stop; )
13642 /* Any glyphs that come from the buffer are here because
13643 of bidi reordering. Skip them, and only pay
13644 attention to glyphs that came from some string. */
13645 if (STRINGP (glyph->object))
13647 Lisp_Object str;
13648 EMACS_INT tem;
13649 /* If the display property covers the newline, we
13650 need to search for it one position farther. */
13651 EMACS_INT lim = pos_after
13652 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
13654 string_from_text_prop = 0;
13655 str = glyph->object;
13656 tem = string_buffer_position_lim (str, pos, lim, 0);
13657 if (tem == 0 /* from overlay */
13658 || pos <= tem)
13660 /* If the string from which this glyph came is
13661 found in the buffer at point, then we've
13662 found the glyph we've been looking for. If
13663 it comes from an overlay (tem == 0), and it
13664 has the `cursor' property on one of its
13665 glyphs, record that glyph as a candidate for
13666 displaying the cursor. (As in the
13667 unidirectional version, we will display the
13668 cursor on the last candidate we find.) */
13669 if (tem == 0 || tem == pt_old)
13671 /* The glyphs from this string could have
13672 been reordered. Find the one with the
13673 smallest string position. Or there could
13674 be a character in the string with the
13675 `cursor' property, which means display
13676 cursor on that character's glyph. */
13677 EMACS_INT strpos = glyph->charpos;
13679 if (tem)
13681 cursor = glyph;
13682 string_from_text_prop = 1;
13684 for ( ;
13685 (row->reversed_p ? glyph > stop : glyph < stop)
13686 && EQ (glyph->object, str);
13687 glyph += incr)
13689 Lisp_Object cprop;
13690 EMACS_INT gpos = glyph->charpos;
13692 cprop = Fget_char_property (make_number (gpos),
13693 Qcursor,
13694 glyph->object);
13695 if (!NILP (cprop))
13697 cursor = glyph;
13698 break;
13700 if (tem && glyph->charpos < strpos)
13702 strpos = glyph->charpos;
13703 cursor = glyph;
13707 if (tem == pt_old)
13708 goto compute_x;
13710 if (tem)
13711 pos = tem + 1; /* don't find previous instances */
13713 /* This string is not what we want; skip all of the
13714 glyphs that came from it. */
13715 while ((row->reversed_p ? glyph > stop : glyph < stop)
13716 && EQ (glyph->object, str))
13717 glyph += incr;
13719 else
13720 glyph += incr;
13723 /* If we reached the end of the line, and END was from a string,
13724 the cursor is not on this line. */
13725 if (cursor == NULL
13726 && (row->reversed_p ? glyph <= end : glyph >= end)
13727 && STRINGP (end->object)
13728 && row->continued_p)
13729 return 0;
13733 compute_x:
13734 if (cursor != NULL)
13735 glyph = cursor;
13736 if (x < 0)
13738 struct glyph *g;
13740 /* Need to compute x that corresponds to GLYPH. */
13741 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
13743 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
13744 abort ();
13745 x += g->pixel_width;
13749 /* ROW could be part of a continued line, which, under bidi
13750 reordering, might have other rows whose start and end charpos
13751 occlude point. Only set w->cursor if we found a better
13752 approximation to the cursor position than we have from previously
13753 examined candidate rows belonging to the same continued line. */
13754 if (/* we already have a candidate row */
13755 w->cursor.vpos >= 0
13756 /* that candidate is not the row we are processing */
13757 && MATRIX_ROW (matrix, w->cursor.vpos) != row
13758 /* Make sure cursor.vpos specifies a row whose start and end
13759 charpos occlude point, and it is valid candidate for being a
13760 cursor-row. This is because some callers of this function
13761 leave cursor.vpos at the row where the cursor was displayed
13762 during the last redisplay cycle. */
13763 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
13764 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13765 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
13767 struct glyph *g1 =
13768 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
13770 /* Don't consider glyphs that are outside TEXT_AREA. */
13771 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
13772 return 0;
13773 /* Keep the candidate whose buffer position is the closest to
13774 point or has the `cursor' property. */
13775 if (/* previous candidate is a glyph in TEXT_AREA of that row */
13776 w->cursor.hpos >= 0
13777 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
13778 && ((BUFFERP (g1->object)
13779 && (g1->charpos == pt_old /* an exact match always wins */
13780 || (BUFFERP (glyph->object)
13781 && eabs (g1->charpos - pt_old)
13782 < eabs (glyph->charpos - pt_old))))
13783 /* previous candidate is a glyph from a string that has
13784 a non-nil `cursor' property */
13785 || (STRINGP (g1->object)
13786 && (!NILP (Fget_char_property (make_number (g1->charpos),
13787 Qcursor, g1->object))
13788 /* pevious candidate is from the same display
13789 string as this one, and the display string
13790 came from a text property */
13791 || (EQ (g1->object, glyph->object)
13792 && string_from_text_prop)
13793 /* this candidate is from newline and its
13794 position is not an exact match */
13795 || (INTEGERP (glyph->object)
13796 && glyph->charpos != pt_old)))))
13797 return 0;
13798 /* If this candidate gives an exact match, use that. */
13799 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
13800 /* Otherwise, keep the candidate that comes from a row
13801 spanning less buffer positions. This may win when one or
13802 both candidate positions are on glyphs that came from
13803 display strings, for which we cannot compare buffer
13804 positions. */
13805 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13806 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13807 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
13808 return 0;
13810 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
13811 w->cursor.x = x;
13812 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
13813 w->cursor.y = row->y + dy;
13815 if (w == XWINDOW (selected_window))
13817 if (!row->continued_p
13818 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
13819 && row->x == 0)
13821 this_line_buffer = XBUFFER (w->buffer);
13823 CHARPOS (this_line_start_pos)
13824 = MATRIX_ROW_START_CHARPOS (row) + delta;
13825 BYTEPOS (this_line_start_pos)
13826 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
13828 CHARPOS (this_line_end_pos)
13829 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
13830 BYTEPOS (this_line_end_pos)
13831 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
13833 this_line_y = w->cursor.y;
13834 this_line_pixel_height = row->height;
13835 this_line_vpos = w->cursor.vpos;
13836 this_line_start_x = row->x;
13838 else
13839 CHARPOS (this_line_start_pos) = 0;
13842 return 1;
13846 /* Run window scroll functions, if any, for WINDOW with new window
13847 start STARTP. Sets the window start of WINDOW to that position.
13849 We assume that the window's buffer is really current. */
13851 static inline struct text_pos
13852 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
13854 struct window *w = XWINDOW (window);
13855 SET_MARKER_FROM_TEXT_POS (w->start, startp);
13857 if (current_buffer != XBUFFER (w->buffer))
13858 abort ();
13860 if (!NILP (Vwindow_scroll_functions))
13862 run_hook_with_args_2 (Qwindow_scroll_functions, window,
13863 make_number (CHARPOS (startp)));
13864 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13865 /* In case the hook functions switch buffers. */
13866 if (current_buffer != XBUFFER (w->buffer))
13867 set_buffer_internal_1 (XBUFFER (w->buffer));
13870 return startp;
13874 /* Make sure the line containing the cursor is fully visible.
13875 A value of 1 means there is nothing to be done.
13876 (Either the line is fully visible, or it cannot be made so,
13877 or we cannot tell.)
13879 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13880 is higher than window.
13882 A value of 0 means the caller should do scrolling
13883 as if point had gone off the screen. */
13885 static int
13886 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
13888 struct glyph_matrix *matrix;
13889 struct glyph_row *row;
13890 int window_height;
13892 if (!make_cursor_line_fully_visible_p)
13893 return 1;
13895 /* It's not always possible to find the cursor, e.g, when a window
13896 is full of overlay strings. Don't do anything in that case. */
13897 if (w->cursor.vpos < 0)
13898 return 1;
13900 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
13901 row = MATRIX_ROW (matrix, w->cursor.vpos);
13903 /* If the cursor row is not partially visible, there's nothing to do. */
13904 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
13905 return 1;
13907 /* If the row the cursor is in is taller than the window's height,
13908 it's not clear what to do, so do nothing. */
13909 window_height = window_box_height (w);
13910 if (row->height >= window_height)
13912 if (!force_p || MINI_WINDOW_P (w)
13913 || w->vscroll || w->cursor.vpos == 0)
13914 return 1;
13916 return 0;
13920 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13921 non-zero means only WINDOW is redisplayed in redisplay_internal.
13922 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
13923 in redisplay_window to bring a partially visible line into view in
13924 the case that only the cursor has moved.
13926 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13927 last screen line's vertical height extends past the end of the screen.
13929 Value is
13931 1 if scrolling succeeded
13933 0 if scrolling didn't find point.
13935 -1 if new fonts have been loaded so that we must interrupt
13936 redisplay, adjust glyph matrices, and try again. */
13938 enum
13940 SCROLLING_SUCCESS,
13941 SCROLLING_FAILED,
13942 SCROLLING_NEED_LARGER_MATRICES
13945 /* If scroll-conservatively is more than this, never recenter.
13947 If you change this, don't forget to update the doc string of
13948 `scroll-conservatively' and the Emacs manual. */
13949 #define SCROLL_LIMIT 100
13951 static int
13952 try_scrolling (Lisp_Object window, int just_this_one_p,
13953 EMACS_INT arg_scroll_conservatively, EMACS_INT scroll_step,
13954 int temp_scroll_step, int last_line_misfit)
13956 struct window *w = XWINDOW (window);
13957 struct frame *f = XFRAME (w->frame);
13958 struct text_pos pos, startp;
13959 struct it it;
13960 int this_scroll_margin, scroll_max, rc, height;
13961 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13962 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13963 Lisp_Object aggressive;
13964 /* We will never try scrolling more than this number of lines. */
13965 int scroll_limit = SCROLL_LIMIT;
13967 #if GLYPH_DEBUG
13968 debug_method_add (w, "try_scrolling");
13969 #endif
13971 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13973 /* Compute scroll margin height in pixels. We scroll when point is
13974 within this distance from the top or bottom of the window. */
13975 if (scroll_margin > 0)
13976 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13977 * FRAME_LINE_HEIGHT (f);
13978 else
13979 this_scroll_margin = 0;
13981 /* Force arg_scroll_conservatively to have a reasonable value, to
13982 avoid scrolling too far away with slow move_it_* functions. Note
13983 that the user can supply scroll-conservatively equal to
13984 `most-positive-fixnum', which can be larger than INT_MAX. */
13985 if (arg_scroll_conservatively > scroll_limit)
13987 arg_scroll_conservatively = scroll_limit + 1;
13988 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
13990 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
13991 /* Compute how much we should try to scroll maximally to bring
13992 point into view. */
13993 scroll_max = (max (scroll_step,
13994 max (arg_scroll_conservatively, temp_scroll_step))
13995 * FRAME_LINE_HEIGHT (f));
13996 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
13997 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
13998 /* We're trying to scroll because of aggressive scrolling but no
13999 scroll_step is set. Choose an arbitrary one. */
14000 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14001 else
14002 scroll_max = 0;
14004 too_near_end:
14006 /* Decide whether to scroll down. */
14007 if (PT > CHARPOS (startp))
14009 int scroll_margin_y;
14011 /* Compute the pixel ypos of the scroll margin, then move it to
14012 either that ypos or PT, whichever comes first. */
14013 start_display (&it, w, startp);
14014 scroll_margin_y = it.last_visible_y - this_scroll_margin
14015 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14016 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14017 (MOVE_TO_POS | MOVE_TO_Y));
14019 if (PT > CHARPOS (it.current.pos))
14021 int y0 = line_bottom_y (&it);
14022 /* Compute how many pixels below window bottom to stop searching
14023 for PT. This avoids costly search for PT that is far away if
14024 the user limited scrolling by a small number of lines, but
14025 always finds PT if scroll_conservatively is set to a large
14026 number, such as most-positive-fixnum. */
14027 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14028 int y_to_move = it.last_visible_y + slack;
14030 /* Compute the distance from the scroll margin to PT or to
14031 the scroll limit, whichever comes first. This should
14032 include the height of the cursor line, to make that line
14033 fully visible. */
14034 move_it_to (&it, PT, -1, y_to_move,
14035 -1, MOVE_TO_POS | MOVE_TO_Y);
14036 dy = line_bottom_y (&it) - y0;
14038 if (dy > scroll_max)
14039 return SCROLLING_FAILED;
14041 scroll_down_p = 1;
14045 if (scroll_down_p)
14047 /* Point is in or below the bottom scroll margin, so move the
14048 window start down. If scrolling conservatively, move it just
14049 enough down to make point visible. If scroll_step is set,
14050 move it down by scroll_step. */
14051 if (arg_scroll_conservatively)
14052 amount_to_scroll
14053 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14054 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14055 else if (scroll_step || temp_scroll_step)
14056 amount_to_scroll = scroll_max;
14057 else
14059 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14060 height = WINDOW_BOX_TEXT_HEIGHT (w);
14061 if (NUMBERP (aggressive))
14063 double float_amount = XFLOATINT (aggressive) * height;
14064 amount_to_scroll = float_amount;
14065 if (amount_to_scroll == 0 && float_amount > 0)
14066 amount_to_scroll = 1;
14067 /* Don't let point enter the scroll margin near top of
14068 the window. */
14069 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14070 amount_to_scroll = height - 2*this_scroll_margin + dy;
14074 if (amount_to_scroll <= 0)
14075 return SCROLLING_FAILED;
14077 start_display (&it, w, startp);
14078 if (arg_scroll_conservatively <= scroll_limit)
14079 move_it_vertically (&it, amount_to_scroll);
14080 else
14082 /* Extra precision for users who set scroll-conservatively
14083 to a large number: make sure the amount we scroll
14084 the window start is never less than amount_to_scroll,
14085 which was computed as distance from window bottom to
14086 point. This matters when lines at window top and lines
14087 below window bottom have different height. */
14088 struct it it1;
14089 void *it1data = NULL;
14090 /* We use a temporary it1 because line_bottom_y can modify
14091 its argument, if it moves one line down; see there. */
14092 int start_y;
14094 SAVE_IT (it1, it, it1data);
14095 start_y = line_bottom_y (&it1);
14096 do {
14097 RESTORE_IT (&it, &it, it1data);
14098 move_it_by_lines (&it, 1);
14099 SAVE_IT (it1, it, it1data);
14100 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14103 /* If STARTP is unchanged, move it down another screen line. */
14104 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14105 move_it_by_lines (&it, 1);
14106 startp = it.current.pos;
14108 else
14110 struct text_pos scroll_margin_pos = startp;
14112 /* See if point is inside the scroll margin at the top of the
14113 window. */
14114 if (this_scroll_margin)
14116 start_display (&it, w, startp);
14117 move_it_vertically (&it, this_scroll_margin);
14118 scroll_margin_pos = it.current.pos;
14121 if (PT < CHARPOS (scroll_margin_pos))
14123 /* Point is in the scroll margin at the top of the window or
14124 above what is displayed in the window. */
14125 int y0, y_to_move;
14127 /* Compute the vertical distance from PT to the scroll
14128 margin position. Move as far as scroll_max allows, or
14129 one screenful, or 10 screen lines, whichever is largest.
14130 Give up if distance is greater than scroll_max. */
14131 SET_TEXT_POS (pos, PT, PT_BYTE);
14132 start_display (&it, w, pos);
14133 y0 = it.current_y;
14134 y_to_move = max (it.last_visible_y,
14135 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14136 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14137 y_to_move, -1,
14138 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14139 dy = it.current_y - y0;
14140 if (dy > scroll_max)
14141 return SCROLLING_FAILED;
14143 /* Compute new window start. */
14144 start_display (&it, w, startp);
14146 if (arg_scroll_conservatively)
14147 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14148 max (scroll_step, temp_scroll_step));
14149 else if (scroll_step || temp_scroll_step)
14150 amount_to_scroll = scroll_max;
14151 else
14153 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14154 height = WINDOW_BOX_TEXT_HEIGHT (w);
14155 if (NUMBERP (aggressive))
14157 double float_amount = XFLOATINT (aggressive) * height;
14158 amount_to_scroll = float_amount;
14159 if (amount_to_scroll == 0 && float_amount > 0)
14160 amount_to_scroll = 1;
14161 amount_to_scroll -=
14162 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14163 /* Don't let point enter the scroll margin near
14164 bottom of the window. */
14165 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14166 amount_to_scroll = height - 2*this_scroll_margin + dy;
14170 if (amount_to_scroll <= 0)
14171 return SCROLLING_FAILED;
14173 move_it_vertically_backward (&it, amount_to_scroll);
14174 startp = it.current.pos;
14178 /* Run window scroll functions. */
14179 startp = run_window_scroll_functions (window, startp);
14181 /* Display the window. Give up if new fonts are loaded, or if point
14182 doesn't appear. */
14183 if (!try_window (window, startp, 0))
14184 rc = SCROLLING_NEED_LARGER_MATRICES;
14185 else if (w->cursor.vpos < 0)
14187 clear_glyph_matrix (w->desired_matrix);
14188 rc = SCROLLING_FAILED;
14190 else
14192 /* Maybe forget recorded base line for line number display. */
14193 if (!just_this_one_p
14194 || current_buffer->clip_changed
14195 || BEG_UNCHANGED < CHARPOS (startp))
14196 w->base_line_number = Qnil;
14198 /* If cursor ends up on a partially visible line,
14199 treat that as being off the bottom of the screen. */
14200 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14201 /* It's possible that the cursor is on the first line of the
14202 buffer, which is partially obscured due to a vscroll
14203 (Bug#7537). In that case, avoid looping forever . */
14204 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14206 clear_glyph_matrix (w->desired_matrix);
14207 ++extra_scroll_margin_lines;
14208 goto too_near_end;
14210 rc = SCROLLING_SUCCESS;
14213 return rc;
14217 /* Compute a suitable window start for window W if display of W starts
14218 on a continuation line. Value is non-zero if a new window start
14219 was computed.
14221 The new window start will be computed, based on W's width, starting
14222 from the start of the continued line. It is the start of the
14223 screen line with the minimum distance from the old start W->start. */
14225 static int
14226 compute_window_start_on_continuation_line (struct window *w)
14228 struct text_pos pos, start_pos;
14229 int window_start_changed_p = 0;
14231 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14233 /* If window start is on a continuation line... Window start may be
14234 < BEGV in case there's invisible text at the start of the
14235 buffer (M-x rmail, for example). */
14236 if (CHARPOS (start_pos) > BEGV
14237 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14239 struct it it;
14240 struct glyph_row *row;
14242 /* Handle the case that the window start is out of range. */
14243 if (CHARPOS (start_pos) < BEGV)
14244 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14245 else if (CHARPOS (start_pos) > ZV)
14246 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14248 /* Find the start of the continued line. This should be fast
14249 because scan_buffer is fast (newline cache). */
14250 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14251 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14252 row, DEFAULT_FACE_ID);
14253 reseat_at_previous_visible_line_start (&it);
14255 /* If the line start is "too far" away from the window start,
14256 say it takes too much time to compute a new window start. */
14257 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14258 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14260 int min_distance, distance;
14262 /* Move forward by display lines to find the new window
14263 start. If window width was enlarged, the new start can
14264 be expected to be > the old start. If window width was
14265 decreased, the new window start will be < the old start.
14266 So, we're looking for the display line start with the
14267 minimum distance from the old window start. */
14268 pos = it.current.pos;
14269 min_distance = INFINITY;
14270 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14271 distance < min_distance)
14273 min_distance = distance;
14274 pos = it.current.pos;
14275 move_it_by_lines (&it, 1);
14278 /* Set the window start there. */
14279 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14280 window_start_changed_p = 1;
14284 return window_start_changed_p;
14288 /* Try cursor movement in case text has not changed in window WINDOW,
14289 with window start STARTP. Value is
14291 CURSOR_MOVEMENT_SUCCESS if successful
14293 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14295 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14296 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14297 we want to scroll as if scroll-step were set to 1. See the code.
14299 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14300 which case we have to abort this redisplay, and adjust matrices
14301 first. */
14303 enum
14305 CURSOR_MOVEMENT_SUCCESS,
14306 CURSOR_MOVEMENT_CANNOT_BE_USED,
14307 CURSOR_MOVEMENT_MUST_SCROLL,
14308 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14311 static int
14312 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14314 struct window *w = XWINDOW (window);
14315 struct frame *f = XFRAME (w->frame);
14316 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14318 #if GLYPH_DEBUG
14319 if (inhibit_try_cursor_movement)
14320 return rc;
14321 #endif
14323 /* Handle case where text has not changed, only point, and it has
14324 not moved off the frame. */
14325 if (/* Point may be in this window. */
14326 PT >= CHARPOS (startp)
14327 /* Selective display hasn't changed. */
14328 && !current_buffer->clip_changed
14329 /* Function force-mode-line-update is used to force a thorough
14330 redisplay. It sets either windows_or_buffers_changed or
14331 update_mode_lines. So don't take a shortcut here for these
14332 cases. */
14333 && !update_mode_lines
14334 && !windows_or_buffers_changed
14335 && !cursor_type_changed
14336 /* Can't use this case if highlighting a region. When a
14337 region exists, cursor movement has to do more than just
14338 set the cursor. */
14339 && !(!NILP (Vtransient_mark_mode)
14340 && !NILP (BVAR (current_buffer, mark_active)))
14341 && NILP (w->region_showing)
14342 && NILP (Vshow_trailing_whitespace)
14343 /* Right after splitting windows, last_point may be nil. */
14344 && INTEGERP (w->last_point)
14345 /* This code is not used for mini-buffer for the sake of the case
14346 of redisplaying to replace an echo area message; since in
14347 that case the mini-buffer contents per se are usually
14348 unchanged. This code is of no real use in the mini-buffer
14349 since the handling of this_line_start_pos, etc., in redisplay
14350 handles the same cases. */
14351 && !EQ (window, minibuf_window)
14352 /* When splitting windows or for new windows, it happens that
14353 redisplay is called with a nil window_end_vpos or one being
14354 larger than the window. This should really be fixed in
14355 window.c. I don't have this on my list, now, so we do
14356 approximately the same as the old redisplay code. --gerd. */
14357 && INTEGERP (w->window_end_vpos)
14358 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14359 && (FRAME_WINDOW_P (f)
14360 || !overlay_arrow_in_current_buffer_p ()))
14362 int this_scroll_margin, top_scroll_margin;
14363 struct glyph_row *row = NULL;
14365 #if GLYPH_DEBUG
14366 debug_method_add (w, "cursor movement");
14367 #endif
14369 /* Scroll if point within this distance from the top or bottom
14370 of the window. This is a pixel value. */
14371 if (scroll_margin > 0)
14373 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14374 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14376 else
14377 this_scroll_margin = 0;
14379 top_scroll_margin = this_scroll_margin;
14380 if (WINDOW_WANTS_HEADER_LINE_P (w))
14381 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14383 /* Start with the row the cursor was displayed during the last
14384 not paused redisplay. Give up if that row is not valid. */
14385 if (w->last_cursor.vpos < 0
14386 || w->last_cursor.vpos >= w->current_matrix->nrows)
14387 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14388 else
14390 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14391 if (row->mode_line_p)
14392 ++row;
14393 if (!row->enabled_p)
14394 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14397 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14399 int scroll_p = 0, must_scroll = 0;
14400 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14402 if (PT > XFASTINT (w->last_point))
14404 /* Point has moved forward. */
14405 while (MATRIX_ROW_END_CHARPOS (row) < PT
14406 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14408 xassert (row->enabled_p);
14409 ++row;
14412 /* If the end position of a row equals the start
14413 position of the next row, and PT is at that position,
14414 we would rather display cursor in the next line. */
14415 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14416 && MATRIX_ROW_END_CHARPOS (row) == PT
14417 && row < w->current_matrix->rows
14418 + w->current_matrix->nrows - 1
14419 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14420 && !cursor_row_p (row))
14421 ++row;
14423 /* If within the scroll margin, scroll. Note that
14424 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14425 the next line would be drawn, and that
14426 this_scroll_margin can be zero. */
14427 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14428 || PT > MATRIX_ROW_END_CHARPOS (row)
14429 /* Line is completely visible last line in window
14430 and PT is to be set in the next line. */
14431 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14432 && PT == MATRIX_ROW_END_CHARPOS (row)
14433 && !row->ends_at_zv_p
14434 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14435 scroll_p = 1;
14437 else if (PT < XFASTINT (w->last_point))
14439 /* Cursor has to be moved backward. Note that PT >=
14440 CHARPOS (startp) because of the outer if-statement. */
14441 while (!row->mode_line_p
14442 && (MATRIX_ROW_START_CHARPOS (row) > PT
14443 || (MATRIX_ROW_START_CHARPOS (row) == PT
14444 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14445 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14446 row > w->current_matrix->rows
14447 && (row-1)->ends_in_newline_from_string_p))))
14448 && (row->y > top_scroll_margin
14449 || CHARPOS (startp) == BEGV))
14451 xassert (row->enabled_p);
14452 --row;
14455 /* Consider the following case: Window starts at BEGV,
14456 there is invisible, intangible text at BEGV, so that
14457 display starts at some point START > BEGV. It can
14458 happen that we are called with PT somewhere between
14459 BEGV and START. Try to handle that case. */
14460 if (row < w->current_matrix->rows
14461 || row->mode_line_p)
14463 row = w->current_matrix->rows;
14464 if (row->mode_line_p)
14465 ++row;
14468 /* Due to newlines in overlay strings, we may have to
14469 skip forward over overlay strings. */
14470 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14471 && MATRIX_ROW_END_CHARPOS (row) == PT
14472 && !cursor_row_p (row))
14473 ++row;
14475 /* If within the scroll margin, scroll. */
14476 if (row->y < top_scroll_margin
14477 && CHARPOS (startp) != BEGV)
14478 scroll_p = 1;
14480 else
14482 /* Cursor did not move. So don't scroll even if cursor line
14483 is partially visible, as it was so before. */
14484 rc = CURSOR_MOVEMENT_SUCCESS;
14487 if (PT < MATRIX_ROW_START_CHARPOS (row)
14488 || PT > MATRIX_ROW_END_CHARPOS (row))
14490 /* if PT is not in the glyph row, give up. */
14491 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14492 must_scroll = 1;
14494 else if (rc != CURSOR_MOVEMENT_SUCCESS
14495 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14497 /* If rows are bidi-reordered and point moved, back up
14498 until we find a row that does not belong to a
14499 continuation line. This is because we must consider
14500 all rows of a continued line as candidates for the
14501 new cursor positioning, since row start and end
14502 positions change non-linearly with vertical position
14503 in such rows. */
14504 /* FIXME: Revisit this when glyph ``spilling'' in
14505 continuation lines' rows is implemented for
14506 bidi-reordered rows. */
14507 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
14509 xassert (row->enabled_p);
14510 --row;
14511 /* If we hit the beginning of the displayed portion
14512 without finding the first row of a continued
14513 line, give up. */
14514 if (row <= w->current_matrix->rows)
14516 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14517 break;
14522 if (must_scroll)
14524 else if (rc != CURSOR_MOVEMENT_SUCCESS
14525 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
14526 && make_cursor_line_fully_visible_p)
14528 if (PT == MATRIX_ROW_END_CHARPOS (row)
14529 && !row->ends_at_zv_p
14530 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14531 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14532 else if (row->height > window_box_height (w))
14534 /* If we end up in a partially visible line, let's
14535 make it fully visible, except when it's taller
14536 than the window, in which case we can't do much
14537 about it. */
14538 *scroll_step = 1;
14539 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14541 else
14543 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14544 if (!cursor_row_fully_visible_p (w, 0, 1))
14545 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14546 else
14547 rc = CURSOR_MOVEMENT_SUCCESS;
14550 else if (scroll_p)
14551 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14552 else if (rc != CURSOR_MOVEMENT_SUCCESS
14553 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14555 /* With bidi-reordered rows, there could be more than
14556 one candidate row whose start and end positions
14557 occlude point. We need to let set_cursor_from_row
14558 find the best candidate. */
14559 /* FIXME: Revisit this when glyph ``spilling'' in
14560 continuation lines' rows is implemented for
14561 bidi-reordered rows. */
14562 int rv = 0;
14566 if (MATRIX_ROW_START_CHARPOS (row) <= PT
14567 && PT <= MATRIX_ROW_END_CHARPOS (row)
14568 && cursor_row_p (row))
14569 rv |= set_cursor_from_row (w, row, w->current_matrix,
14570 0, 0, 0, 0);
14571 /* As soon as we've found the first suitable row
14572 whose ends_at_zv_p flag is set, we are done. */
14573 if (rv
14574 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
14576 rc = CURSOR_MOVEMENT_SUCCESS;
14577 break;
14579 ++row;
14581 while ((MATRIX_ROW_CONTINUATION_LINE_P (row)
14582 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
14583 || (MATRIX_ROW_START_CHARPOS (row) == PT
14584 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
14585 /* If we didn't find any candidate rows, or exited the
14586 loop before all the candidates were examined, signal
14587 to the caller that this method failed. */
14588 if (rc != CURSOR_MOVEMENT_SUCCESS
14589 && (!rv || MATRIX_ROW_CONTINUATION_LINE_P (row)))
14590 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14591 else if (rv)
14592 rc = CURSOR_MOVEMENT_SUCCESS;
14594 else
14598 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
14600 rc = CURSOR_MOVEMENT_SUCCESS;
14601 break;
14603 ++row;
14605 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14606 && MATRIX_ROW_START_CHARPOS (row) == PT
14607 && cursor_row_p (row));
14612 return rc;
14615 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
14616 static
14617 #endif
14618 void
14619 set_vertical_scroll_bar (struct window *w)
14621 EMACS_INT start, end, whole;
14623 /* Calculate the start and end positions for the current window.
14624 At some point, it would be nice to choose between scrollbars
14625 which reflect the whole buffer size, with special markers
14626 indicating narrowing, and scrollbars which reflect only the
14627 visible region.
14629 Note that mini-buffers sometimes aren't displaying any text. */
14630 if (!MINI_WINDOW_P (w)
14631 || (w == XWINDOW (minibuf_window)
14632 && NILP (echo_area_buffer[0])))
14634 struct buffer *buf = XBUFFER (w->buffer);
14635 whole = BUF_ZV (buf) - BUF_BEGV (buf);
14636 start = marker_position (w->start) - BUF_BEGV (buf);
14637 /* I don't think this is guaranteed to be right. For the
14638 moment, we'll pretend it is. */
14639 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
14641 if (end < start)
14642 end = start;
14643 if (whole < (end - start))
14644 whole = end - start;
14646 else
14647 start = end = whole = 0;
14649 /* Indicate what this scroll bar ought to be displaying now. */
14650 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
14651 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
14652 (w, end - start, whole, start);
14656 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
14657 selected_window is redisplayed.
14659 We can return without actually redisplaying the window if
14660 fonts_changed_p is nonzero. In that case, redisplay_internal will
14661 retry. */
14663 static void
14664 redisplay_window (Lisp_Object window, int just_this_one_p)
14666 struct window *w = XWINDOW (window);
14667 struct frame *f = XFRAME (w->frame);
14668 struct buffer *buffer = XBUFFER (w->buffer);
14669 struct buffer *old = current_buffer;
14670 struct text_pos lpoint, opoint, startp;
14671 int update_mode_line;
14672 int tem;
14673 struct it it;
14674 /* Record it now because it's overwritten. */
14675 int current_matrix_up_to_date_p = 0;
14676 int used_current_matrix_p = 0;
14677 /* This is less strict than current_matrix_up_to_date_p.
14678 It indictes that the buffer contents and narrowing are unchanged. */
14679 int buffer_unchanged_p = 0;
14680 int temp_scroll_step = 0;
14681 int count = SPECPDL_INDEX ();
14682 int rc;
14683 int centering_position = -1;
14684 int last_line_misfit = 0;
14685 EMACS_INT beg_unchanged, end_unchanged;
14687 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14688 opoint = lpoint;
14690 /* W must be a leaf window here. */
14691 xassert (!NILP (w->buffer));
14692 #if GLYPH_DEBUG
14693 *w->desired_matrix->method = 0;
14694 #endif
14696 restart:
14697 reconsider_clip_changes (w, buffer);
14699 /* Has the mode line to be updated? */
14700 update_mode_line = (!NILP (w->update_mode_line)
14701 || update_mode_lines
14702 || buffer->clip_changed
14703 || buffer->prevent_redisplay_optimizations_p);
14705 if (MINI_WINDOW_P (w))
14707 if (w == XWINDOW (echo_area_window)
14708 && !NILP (echo_area_buffer[0]))
14710 if (update_mode_line)
14711 /* We may have to update a tty frame's menu bar or a
14712 tool-bar. Example `M-x C-h C-h C-g'. */
14713 goto finish_menu_bars;
14714 else
14715 /* We've already displayed the echo area glyphs in this window. */
14716 goto finish_scroll_bars;
14718 else if ((w != XWINDOW (minibuf_window)
14719 || minibuf_level == 0)
14720 /* When buffer is nonempty, redisplay window normally. */
14721 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
14722 /* Quail displays non-mini buffers in minibuffer window.
14723 In that case, redisplay the window normally. */
14724 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
14726 /* W is a mini-buffer window, but it's not active, so clear
14727 it. */
14728 int yb = window_text_bottom_y (w);
14729 struct glyph_row *row;
14730 int y;
14732 for (y = 0, row = w->desired_matrix->rows;
14733 y < yb;
14734 y += row->height, ++row)
14735 blank_row (w, row, y);
14736 goto finish_scroll_bars;
14739 clear_glyph_matrix (w->desired_matrix);
14742 /* Otherwise set up data on this window; select its buffer and point
14743 value. */
14744 /* Really select the buffer, for the sake of buffer-local
14745 variables. */
14746 set_buffer_internal_1 (XBUFFER (w->buffer));
14748 current_matrix_up_to_date_p
14749 = (!NILP (w->window_end_valid)
14750 && !current_buffer->clip_changed
14751 && !current_buffer->prevent_redisplay_optimizations_p
14752 && XFASTINT (w->last_modified) >= MODIFF
14753 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
14755 /* Run the window-bottom-change-functions
14756 if it is possible that the text on the screen has changed
14757 (either due to modification of the text, or any other reason). */
14758 if (!current_matrix_up_to_date_p
14759 && !NILP (Vwindow_text_change_functions))
14761 safe_run_hooks (Qwindow_text_change_functions);
14762 goto restart;
14765 beg_unchanged = BEG_UNCHANGED;
14766 end_unchanged = END_UNCHANGED;
14768 SET_TEXT_POS (opoint, PT, PT_BYTE);
14770 specbind (Qinhibit_point_motion_hooks, Qt);
14772 buffer_unchanged_p
14773 = (!NILP (w->window_end_valid)
14774 && !current_buffer->clip_changed
14775 && XFASTINT (w->last_modified) >= MODIFF
14776 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
14778 /* When windows_or_buffers_changed is non-zero, we can't rely on
14779 the window end being valid, so set it to nil there. */
14780 if (windows_or_buffers_changed)
14782 /* If window starts on a continuation line, maybe adjust the
14783 window start in case the window's width changed. */
14784 if (XMARKER (w->start)->buffer == current_buffer)
14785 compute_window_start_on_continuation_line (w);
14787 w->window_end_valid = Qnil;
14790 /* Some sanity checks. */
14791 CHECK_WINDOW_END (w);
14792 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
14793 abort ();
14794 if (BYTEPOS (opoint) < CHARPOS (opoint))
14795 abort ();
14797 /* If %c is in mode line, update it if needed. */
14798 if (!NILP (w->column_number_displayed)
14799 /* This alternative quickly identifies a common case
14800 where no change is needed. */
14801 && !(PT == XFASTINT (w->last_point)
14802 && XFASTINT (w->last_modified) >= MODIFF
14803 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
14804 && (XFASTINT (w->column_number_displayed) != current_column ()))
14805 update_mode_line = 1;
14807 /* Count number of windows showing the selected buffer. An indirect
14808 buffer counts as its base buffer. */
14809 if (!just_this_one_p)
14811 struct buffer *current_base, *window_base;
14812 current_base = current_buffer;
14813 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
14814 if (current_base->base_buffer)
14815 current_base = current_base->base_buffer;
14816 if (window_base->base_buffer)
14817 window_base = window_base->base_buffer;
14818 if (current_base == window_base)
14819 buffer_shared++;
14822 /* Point refers normally to the selected window. For any other
14823 window, set up appropriate value. */
14824 if (!EQ (window, selected_window))
14826 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
14827 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
14828 if (new_pt < BEGV)
14830 new_pt = BEGV;
14831 new_pt_byte = BEGV_BYTE;
14832 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
14834 else if (new_pt > (ZV - 1))
14836 new_pt = ZV;
14837 new_pt_byte = ZV_BYTE;
14838 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
14841 /* We don't use SET_PT so that the point-motion hooks don't run. */
14842 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
14845 /* If any of the character widths specified in the display table
14846 have changed, invalidate the width run cache. It's true that
14847 this may be a bit late to catch such changes, but the rest of
14848 redisplay goes (non-fatally) haywire when the display table is
14849 changed, so why should we worry about doing any better? */
14850 if (current_buffer->width_run_cache)
14852 struct Lisp_Char_Table *disptab = buffer_display_table ();
14854 if (! disptab_matches_widthtab (disptab,
14855 XVECTOR (BVAR (current_buffer, width_table))))
14857 invalidate_region_cache (current_buffer,
14858 current_buffer->width_run_cache,
14859 BEG, Z);
14860 recompute_width_table (current_buffer, disptab);
14864 /* If window-start is screwed up, choose a new one. */
14865 if (XMARKER (w->start)->buffer != current_buffer)
14866 goto recenter;
14868 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14870 /* If someone specified a new starting point but did not insist,
14871 check whether it can be used. */
14872 if (!NILP (w->optional_new_start)
14873 && CHARPOS (startp) >= BEGV
14874 && CHARPOS (startp) <= ZV)
14876 w->optional_new_start = Qnil;
14877 start_display (&it, w, startp);
14878 move_it_to (&it, PT, 0, it.last_visible_y, -1,
14879 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14880 if (IT_CHARPOS (it) == PT)
14881 w->force_start = Qt;
14882 /* IT may overshoot PT if text at PT is invisible. */
14883 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
14884 w->force_start = Qt;
14887 force_start:
14889 /* Handle case where place to start displaying has been specified,
14890 unless the specified location is outside the accessible range. */
14891 if (!NILP (w->force_start)
14892 || w->frozen_window_start_p)
14894 /* We set this later on if we have to adjust point. */
14895 int new_vpos = -1;
14897 w->force_start = Qnil;
14898 w->vscroll = 0;
14899 w->window_end_valid = Qnil;
14901 /* Forget any recorded base line for line number display. */
14902 if (!buffer_unchanged_p)
14903 w->base_line_number = Qnil;
14905 /* Redisplay the mode line. Select the buffer properly for that.
14906 Also, run the hook window-scroll-functions
14907 because we have scrolled. */
14908 /* Note, we do this after clearing force_start because
14909 if there's an error, it is better to forget about force_start
14910 than to get into an infinite loop calling the hook functions
14911 and having them get more errors. */
14912 if (!update_mode_line
14913 || ! NILP (Vwindow_scroll_functions))
14915 update_mode_line = 1;
14916 w->update_mode_line = Qt;
14917 startp = run_window_scroll_functions (window, startp);
14920 w->last_modified = make_number (0);
14921 w->last_overlay_modified = make_number (0);
14922 if (CHARPOS (startp) < BEGV)
14923 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
14924 else if (CHARPOS (startp) > ZV)
14925 SET_TEXT_POS (startp, ZV, ZV_BYTE);
14927 /* Redisplay, then check if cursor has been set during the
14928 redisplay. Give up if new fonts were loaded. */
14929 /* We used to issue a CHECK_MARGINS argument to try_window here,
14930 but this causes scrolling to fail when point begins inside
14931 the scroll margin (bug#148) -- cyd */
14932 if (!try_window (window, startp, 0))
14934 w->force_start = Qt;
14935 clear_glyph_matrix (w->desired_matrix);
14936 goto need_larger_matrices;
14939 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
14941 /* If point does not appear, try to move point so it does
14942 appear. The desired matrix has been built above, so we
14943 can use it here. */
14944 new_vpos = window_box_height (w) / 2;
14947 if (!cursor_row_fully_visible_p (w, 0, 0))
14949 /* Point does appear, but on a line partly visible at end of window.
14950 Move it back to a fully-visible line. */
14951 new_vpos = window_box_height (w);
14954 /* If we need to move point for either of the above reasons,
14955 now actually do it. */
14956 if (new_vpos >= 0)
14958 struct glyph_row *row;
14960 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
14961 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
14962 ++row;
14964 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
14965 MATRIX_ROW_START_BYTEPOS (row));
14967 if (w != XWINDOW (selected_window))
14968 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
14969 else if (current_buffer == old)
14970 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14972 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
14974 /* If we are highlighting the region, then we just changed
14975 the region, so redisplay to show it. */
14976 if (!NILP (Vtransient_mark_mode)
14977 && !NILP (BVAR (current_buffer, mark_active)))
14979 clear_glyph_matrix (w->desired_matrix);
14980 if (!try_window (window, startp, 0))
14981 goto need_larger_matrices;
14985 #if GLYPH_DEBUG
14986 debug_method_add (w, "forced window start");
14987 #endif
14988 goto done;
14991 /* Handle case where text has not changed, only point, and it has
14992 not moved off the frame, and we are not retrying after hscroll.
14993 (current_matrix_up_to_date_p is nonzero when retrying.) */
14994 if (current_matrix_up_to_date_p
14995 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14996 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14998 switch (rc)
15000 case CURSOR_MOVEMENT_SUCCESS:
15001 used_current_matrix_p = 1;
15002 goto done;
15004 case CURSOR_MOVEMENT_MUST_SCROLL:
15005 goto try_to_scroll;
15007 default:
15008 abort ();
15011 /* If current starting point was originally the beginning of a line
15012 but no longer is, find a new starting point. */
15013 else if (!NILP (w->start_at_line_beg)
15014 && !(CHARPOS (startp) <= BEGV
15015 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15017 #if GLYPH_DEBUG
15018 debug_method_add (w, "recenter 1");
15019 #endif
15020 goto recenter;
15023 /* Try scrolling with try_window_id. Value is > 0 if update has
15024 been done, it is -1 if we know that the same window start will
15025 not work. It is 0 if unsuccessful for some other reason. */
15026 else if ((tem = try_window_id (w)) != 0)
15028 #if GLYPH_DEBUG
15029 debug_method_add (w, "try_window_id %d", tem);
15030 #endif
15032 if (fonts_changed_p)
15033 goto need_larger_matrices;
15034 if (tem > 0)
15035 goto done;
15037 /* Otherwise try_window_id has returned -1 which means that we
15038 don't want the alternative below this comment to execute. */
15040 else if (CHARPOS (startp) >= BEGV
15041 && CHARPOS (startp) <= ZV
15042 && PT >= CHARPOS (startp)
15043 && (CHARPOS (startp) < ZV
15044 /* Avoid starting at end of buffer. */
15045 || CHARPOS (startp) == BEGV
15046 || (XFASTINT (w->last_modified) >= MODIFF
15047 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
15050 /* If first window line is a continuation line, and window start
15051 is inside the modified region, but the first change is before
15052 current window start, we must select a new window start.
15054 However, if this is the result of a down-mouse event (e.g. by
15055 extending the mouse-drag-overlay), we don't want to select a
15056 new window start, since that would change the position under
15057 the mouse, resulting in an unwanted mouse-movement rather
15058 than a simple mouse-click. */
15059 if (NILP (w->start_at_line_beg)
15060 && NILP (do_mouse_tracking)
15061 && CHARPOS (startp) > BEGV
15062 && CHARPOS (startp) > BEG + beg_unchanged
15063 && CHARPOS (startp) <= Z - end_unchanged
15064 /* Even if w->start_at_line_beg is nil, a new window may
15065 start at a line_beg, since that's how set_buffer_window
15066 sets it. So, we need to check the return value of
15067 compute_window_start_on_continuation_line. (See also
15068 bug#197). */
15069 && XMARKER (w->start)->buffer == current_buffer
15070 && compute_window_start_on_continuation_line (w))
15072 w->force_start = Qt;
15073 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15074 goto force_start;
15077 #if GLYPH_DEBUG
15078 debug_method_add (w, "same window start");
15079 #endif
15081 /* Try to redisplay starting at same place as before.
15082 If point has not moved off frame, accept the results. */
15083 if (!current_matrix_up_to_date_p
15084 /* Don't use try_window_reusing_current_matrix in this case
15085 because a window scroll function can have changed the
15086 buffer. */
15087 || !NILP (Vwindow_scroll_functions)
15088 || MINI_WINDOW_P (w)
15089 || !(used_current_matrix_p
15090 = try_window_reusing_current_matrix (w)))
15092 IF_DEBUG (debug_method_add (w, "1"));
15093 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15094 /* -1 means we need to scroll.
15095 0 means we need new matrices, but fonts_changed_p
15096 is set in that case, so we will detect it below. */
15097 goto try_to_scroll;
15100 if (fonts_changed_p)
15101 goto need_larger_matrices;
15103 if (w->cursor.vpos >= 0)
15105 if (!just_this_one_p
15106 || current_buffer->clip_changed
15107 || BEG_UNCHANGED < CHARPOS (startp))
15108 /* Forget any recorded base line for line number display. */
15109 w->base_line_number = Qnil;
15111 if (!cursor_row_fully_visible_p (w, 1, 0))
15113 clear_glyph_matrix (w->desired_matrix);
15114 last_line_misfit = 1;
15116 /* Drop through and scroll. */
15117 else
15118 goto done;
15120 else
15121 clear_glyph_matrix (w->desired_matrix);
15124 try_to_scroll:
15126 w->last_modified = make_number (0);
15127 w->last_overlay_modified = make_number (0);
15129 /* Redisplay the mode line. Select the buffer properly for that. */
15130 if (!update_mode_line)
15132 update_mode_line = 1;
15133 w->update_mode_line = Qt;
15136 /* Try to scroll by specified few lines. */
15137 if ((scroll_conservatively
15138 || emacs_scroll_step
15139 || temp_scroll_step
15140 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15141 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15142 && CHARPOS (startp) >= BEGV
15143 && CHARPOS (startp) <= ZV)
15145 /* The function returns -1 if new fonts were loaded, 1 if
15146 successful, 0 if not successful. */
15147 int ss = try_scrolling (window, just_this_one_p,
15148 scroll_conservatively,
15149 emacs_scroll_step,
15150 temp_scroll_step, last_line_misfit);
15151 switch (ss)
15153 case SCROLLING_SUCCESS:
15154 goto done;
15156 case SCROLLING_NEED_LARGER_MATRICES:
15157 goto need_larger_matrices;
15159 case SCROLLING_FAILED:
15160 break;
15162 default:
15163 abort ();
15167 /* Finally, just choose a place to start which positions point
15168 according to user preferences. */
15170 recenter:
15172 #if GLYPH_DEBUG
15173 debug_method_add (w, "recenter");
15174 #endif
15176 /* w->vscroll = 0; */
15178 /* Forget any previously recorded base line for line number display. */
15179 if (!buffer_unchanged_p)
15180 w->base_line_number = Qnil;
15182 /* Determine the window start relative to point. */
15183 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15184 it.current_y = it.last_visible_y;
15185 if (centering_position < 0)
15187 int margin =
15188 scroll_margin > 0
15189 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15190 : 0;
15191 EMACS_INT margin_pos = CHARPOS (startp);
15192 int scrolling_up;
15193 Lisp_Object aggressive;
15195 /* If there is a scroll margin at the top of the window, find
15196 its character position. */
15197 if (margin
15198 /* Cannot call start_display if startp is not in the
15199 accessible region of the buffer. This can happen when we
15200 have just switched to a different buffer and/or changed
15201 its restriction. In that case, startp is initialized to
15202 the character position 1 (BEG) because we did not yet
15203 have chance to display the buffer even once. */
15204 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15206 struct it it1;
15207 void *it1data = NULL;
15209 SAVE_IT (it1, it, it1data);
15210 start_display (&it1, w, startp);
15211 move_it_vertically (&it1, margin);
15212 margin_pos = IT_CHARPOS (it1);
15213 RESTORE_IT (&it, &it, it1data);
15215 scrolling_up = PT > margin_pos;
15216 aggressive =
15217 scrolling_up
15218 ? BVAR (current_buffer, scroll_up_aggressively)
15219 : BVAR (current_buffer, scroll_down_aggressively);
15221 if (!MINI_WINDOW_P (w)
15222 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15224 int pt_offset = 0;
15226 /* Setting scroll-conservatively overrides
15227 scroll-*-aggressively. */
15228 if (!scroll_conservatively && NUMBERP (aggressive))
15230 double float_amount = XFLOATINT (aggressive);
15232 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15233 if (pt_offset == 0 && float_amount > 0)
15234 pt_offset = 1;
15235 if (pt_offset)
15236 margin -= 1;
15238 /* Compute how much to move the window start backward from
15239 point so that point will be displayed where the user
15240 wants it. */
15241 if (scrolling_up)
15243 centering_position = it.last_visible_y;
15244 if (pt_offset)
15245 centering_position -= pt_offset;
15246 centering_position -=
15247 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0));
15248 /* Don't let point enter the scroll margin near top of
15249 the window. */
15250 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15251 centering_position = margin * FRAME_LINE_HEIGHT (f);
15253 else
15254 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15256 else
15257 /* Set the window start half the height of the window backward
15258 from point. */
15259 centering_position = window_box_height (w) / 2;
15261 move_it_vertically_backward (&it, centering_position);
15263 xassert (IT_CHARPOS (it) >= BEGV);
15265 /* The function move_it_vertically_backward may move over more
15266 than the specified y-distance. If it->w is small, e.g. a
15267 mini-buffer window, we may end up in front of the window's
15268 display area. Start displaying at the start of the line
15269 containing PT in this case. */
15270 if (it.current_y <= 0)
15272 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15273 move_it_vertically_backward (&it, 0);
15274 it.current_y = 0;
15277 it.current_x = it.hpos = 0;
15279 /* Set the window start position here explicitly, to avoid an
15280 infinite loop in case the functions in window-scroll-functions
15281 get errors. */
15282 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15284 /* Run scroll hooks. */
15285 startp = run_window_scroll_functions (window, it.current.pos);
15287 /* Redisplay the window. */
15288 if (!current_matrix_up_to_date_p
15289 || windows_or_buffers_changed
15290 || cursor_type_changed
15291 /* Don't use try_window_reusing_current_matrix in this case
15292 because it can have changed the buffer. */
15293 || !NILP (Vwindow_scroll_functions)
15294 || !just_this_one_p
15295 || MINI_WINDOW_P (w)
15296 || !(used_current_matrix_p
15297 = try_window_reusing_current_matrix (w)))
15298 try_window (window, startp, 0);
15300 /* If new fonts have been loaded (due to fontsets), give up. We
15301 have to start a new redisplay since we need to re-adjust glyph
15302 matrices. */
15303 if (fonts_changed_p)
15304 goto need_larger_matrices;
15306 /* If cursor did not appear assume that the middle of the window is
15307 in the first line of the window. Do it again with the next line.
15308 (Imagine a window of height 100, displaying two lines of height
15309 60. Moving back 50 from it->last_visible_y will end in the first
15310 line.) */
15311 if (w->cursor.vpos < 0)
15313 if (!NILP (w->window_end_valid)
15314 && PT >= Z - XFASTINT (w->window_end_pos))
15316 clear_glyph_matrix (w->desired_matrix);
15317 move_it_by_lines (&it, 1);
15318 try_window (window, it.current.pos, 0);
15320 else if (PT < IT_CHARPOS (it))
15322 clear_glyph_matrix (w->desired_matrix);
15323 move_it_by_lines (&it, -1);
15324 try_window (window, it.current.pos, 0);
15326 else
15328 /* Not much we can do about it. */
15332 /* Consider the following case: Window starts at BEGV, there is
15333 invisible, intangible text at BEGV, so that display starts at
15334 some point START > BEGV. It can happen that we are called with
15335 PT somewhere between BEGV and START. Try to handle that case. */
15336 if (w->cursor.vpos < 0)
15338 struct glyph_row *row = w->current_matrix->rows;
15339 if (row->mode_line_p)
15340 ++row;
15341 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15344 if (!cursor_row_fully_visible_p (w, 0, 0))
15346 /* If vscroll is enabled, disable it and try again. */
15347 if (w->vscroll)
15349 w->vscroll = 0;
15350 clear_glyph_matrix (w->desired_matrix);
15351 goto recenter;
15354 /* If centering point failed to make the whole line visible,
15355 put point at the top instead. That has to make the whole line
15356 visible, if it can be done. */
15357 if (centering_position == 0)
15358 goto done;
15360 clear_glyph_matrix (w->desired_matrix);
15361 centering_position = 0;
15362 goto recenter;
15365 done:
15367 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15368 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
15369 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
15370 ? Qt : Qnil);
15372 /* Display the mode line, if we must. */
15373 if ((update_mode_line
15374 /* If window not full width, must redo its mode line
15375 if (a) the window to its side is being redone and
15376 (b) we do a frame-based redisplay. This is a consequence
15377 of how inverted lines are drawn in frame-based redisplay. */
15378 || (!just_this_one_p
15379 && !FRAME_WINDOW_P (f)
15380 && !WINDOW_FULL_WIDTH_P (w))
15381 /* Line number to display. */
15382 || INTEGERP (w->base_line_pos)
15383 /* Column number is displayed and different from the one displayed. */
15384 || (!NILP (w->column_number_displayed)
15385 && (XFASTINT (w->column_number_displayed) != current_column ())))
15386 /* This means that the window has a mode line. */
15387 && (WINDOW_WANTS_MODELINE_P (w)
15388 || WINDOW_WANTS_HEADER_LINE_P (w)))
15390 display_mode_lines (w);
15392 /* If mode line height has changed, arrange for a thorough
15393 immediate redisplay using the correct mode line height. */
15394 if (WINDOW_WANTS_MODELINE_P (w)
15395 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
15397 fonts_changed_p = 1;
15398 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
15399 = DESIRED_MODE_LINE_HEIGHT (w);
15402 /* If header line height has changed, arrange for a thorough
15403 immediate redisplay using the correct header line height. */
15404 if (WINDOW_WANTS_HEADER_LINE_P (w)
15405 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
15407 fonts_changed_p = 1;
15408 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
15409 = DESIRED_HEADER_LINE_HEIGHT (w);
15412 if (fonts_changed_p)
15413 goto need_larger_matrices;
15416 if (!line_number_displayed
15417 && !BUFFERP (w->base_line_pos))
15419 w->base_line_pos = Qnil;
15420 w->base_line_number = Qnil;
15423 finish_menu_bars:
15425 /* When we reach a frame's selected window, redo the frame's menu bar. */
15426 if (update_mode_line
15427 && EQ (FRAME_SELECTED_WINDOW (f), window))
15429 int redisplay_menu_p = 0;
15431 if (FRAME_WINDOW_P (f))
15433 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
15434 || defined (HAVE_NS) || defined (USE_GTK)
15435 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
15436 #else
15437 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15438 #endif
15440 else
15441 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15443 if (redisplay_menu_p)
15444 display_menu_bar (w);
15446 #ifdef HAVE_WINDOW_SYSTEM
15447 if (FRAME_WINDOW_P (f))
15449 #if defined (USE_GTK) || defined (HAVE_NS)
15450 if (FRAME_EXTERNAL_TOOL_BAR (f))
15451 redisplay_tool_bar (f);
15452 #else
15453 if (WINDOWP (f->tool_bar_window)
15454 && (FRAME_TOOL_BAR_LINES (f) > 0
15455 || !NILP (Vauto_resize_tool_bars))
15456 && redisplay_tool_bar (f))
15457 ignore_mouse_drag_p = 1;
15458 #endif
15460 #endif
15463 #ifdef HAVE_WINDOW_SYSTEM
15464 if (FRAME_WINDOW_P (f)
15465 && update_window_fringes (w, (just_this_one_p
15466 || (!used_current_matrix_p && !overlay_arrow_seen)
15467 || w->pseudo_window_p)))
15469 update_begin (f);
15470 BLOCK_INPUT;
15471 if (draw_window_fringes (w, 1))
15472 x_draw_vertical_border (w);
15473 UNBLOCK_INPUT;
15474 update_end (f);
15476 #endif /* HAVE_WINDOW_SYSTEM */
15478 /* We go to this label, with fonts_changed_p nonzero,
15479 if it is necessary to try again using larger glyph matrices.
15480 We have to redeem the scroll bar even in this case,
15481 because the loop in redisplay_internal expects that. */
15482 need_larger_matrices:
15484 finish_scroll_bars:
15486 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
15488 /* Set the thumb's position and size. */
15489 set_vertical_scroll_bar (w);
15491 /* Note that we actually used the scroll bar attached to this
15492 window, so it shouldn't be deleted at the end of redisplay. */
15493 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
15494 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
15497 /* Restore current_buffer and value of point in it. The window
15498 update may have changed the buffer, so first make sure `opoint'
15499 is still valid (Bug#6177). */
15500 if (CHARPOS (opoint) < BEGV)
15501 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15502 else if (CHARPOS (opoint) > ZV)
15503 TEMP_SET_PT_BOTH (Z, Z_BYTE);
15504 else
15505 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
15507 set_buffer_internal_1 (old);
15508 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
15509 shorter. This can be caused by log truncation in *Messages*. */
15510 if (CHARPOS (lpoint) <= ZV)
15511 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15513 unbind_to (count, Qnil);
15517 /* Build the complete desired matrix of WINDOW with a window start
15518 buffer position POS.
15520 Value is 1 if successful. It is zero if fonts were loaded during
15521 redisplay which makes re-adjusting glyph matrices necessary, and -1
15522 if point would appear in the scroll margins.
15523 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
15524 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
15525 set in FLAGS.) */
15528 try_window (Lisp_Object window, struct text_pos pos, int flags)
15530 struct window *w = XWINDOW (window);
15531 struct it it;
15532 struct glyph_row *last_text_row = NULL;
15533 struct frame *f = XFRAME (w->frame);
15535 /* Make POS the new window start. */
15536 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
15538 /* Mark cursor position as unknown. No overlay arrow seen. */
15539 w->cursor.vpos = -1;
15540 overlay_arrow_seen = 0;
15542 /* Initialize iterator and info to start at POS. */
15543 start_display (&it, w, pos);
15545 /* Display all lines of W. */
15546 while (it.current_y < it.last_visible_y)
15548 if (display_line (&it))
15549 last_text_row = it.glyph_row - 1;
15550 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
15551 return 0;
15554 /* Don't let the cursor end in the scroll margins. */
15555 if ((flags & TRY_WINDOW_CHECK_MARGINS)
15556 && !MINI_WINDOW_P (w))
15558 int this_scroll_margin;
15560 if (scroll_margin > 0)
15562 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15563 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15565 else
15566 this_scroll_margin = 0;
15568 if ((w->cursor.y >= 0 /* not vscrolled */
15569 && w->cursor.y < this_scroll_margin
15570 && CHARPOS (pos) > BEGV
15571 && IT_CHARPOS (it) < ZV)
15572 /* rms: considering make_cursor_line_fully_visible_p here
15573 seems to give wrong results. We don't want to recenter
15574 when the last line is partly visible, we want to allow
15575 that case to be handled in the usual way. */
15576 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
15578 w->cursor.vpos = -1;
15579 clear_glyph_matrix (w->desired_matrix);
15580 return -1;
15584 /* If bottom moved off end of frame, change mode line percentage. */
15585 if (XFASTINT (w->window_end_pos) <= 0
15586 && Z != IT_CHARPOS (it))
15587 w->update_mode_line = Qt;
15589 /* Set window_end_pos to the offset of the last character displayed
15590 on the window from the end of current_buffer. Set
15591 window_end_vpos to its row number. */
15592 if (last_text_row)
15594 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
15595 w->window_end_bytepos
15596 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15597 w->window_end_pos
15598 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15599 w->window_end_vpos
15600 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15601 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
15602 ->displays_text_p);
15604 else
15606 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
15607 w->window_end_pos = make_number (Z - ZV);
15608 w->window_end_vpos = make_number (0);
15611 /* But that is not valid info until redisplay finishes. */
15612 w->window_end_valid = Qnil;
15613 return 1;
15618 /************************************************************************
15619 Window redisplay reusing current matrix when buffer has not changed
15620 ************************************************************************/
15622 /* Try redisplay of window W showing an unchanged buffer with a
15623 different window start than the last time it was displayed by
15624 reusing its current matrix. Value is non-zero if successful.
15625 W->start is the new window start. */
15627 static int
15628 try_window_reusing_current_matrix (struct window *w)
15630 struct frame *f = XFRAME (w->frame);
15631 struct glyph_row *bottom_row;
15632 struct it it;
15633 struct run run;
15634 struct text_pos start, new_start;
15635 int nrows_scrolled, i;
15636 struct glyph_row *last_text_row;
15637 struct glyph_row *last_reused_text_row;
15638 struct glyph_row *start_row;
15639 int start_vpos, min_y, max_y;
15641 #if GLYPH_DEBUG
15642 if (inhibit_try_window_reusing)
15643 return 0;
15644 #endif
15646 if (/* This function doesn't handle terminal frames. */
15647 !FRAME_WINDOW_P (f)
15648 /* Don't try to reuse the display if windows have been split
15649 or such. */
15650 || windows_or_buffers_changed
15651 || cursor_type_changed)
15652 return 0;
15654 /* Can't do this if region may have changed. */
15655 if ((!NILP (Vtransient_mark_mode)
15656 && !NILP (BVAR (current_buffer, mark_active)))
15657 || !NILP (w->region_showing)
15658 || !NILP (Vshow_trailing_whitespace))
15659 return 0;
15661 /* If top-line visibility has changed, give up. */
15662 if (WINDOW_WANTS_HEADER_LINE_P (w)
15663 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
15664 return 0;
15666 /* Give up if old or new display is scrolled vertically. We could
15667 make this function handle this, but right now it doesn't. */
15668 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15669 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
15670 return 0;
15672 /* The variable new_start now holds the new window start. The old
15673 start `start' can be determined from the current matrix. */
15674 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
15675 start = start_row->minpos;
15676 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
15678 /* Clear the desired matrix for the display below. */
15679 clear_glyph_matrix (w->desired_matrix);
15681 if (CHARPOS (new_start) <= CHARPOS (start))
15683 /* Don't use this method if the display starts with an ellipsis
15684 displayed for invisible text. It's not easy to handle that case
15685 below, and it's certainly not worth the effort since this is
15686 not a frequent case. */
15687 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
15688 return 0;
15690 IF_DEBUG (debug_method_add (w, "twu1"));
15692 /* Display up to a row that can be reused. The variable
15693 last_text_row is set to the last row displayed that displays
15694 text. Note that it.vpos == 0 if or if not there is a
15695 header-line; it's not the same as the MATRIX_ROW_VPOS! */
15696 start_display (&it, w, new_start);
15697 w->cursor.vpos = -1;
15698 last_text_row = last_reused_text_row = NULL;
15700 while (it.current_y < it.last_visible_y
15701 && !fonts_changed_p)
15703 /* If we have reached into the characters in the START row,
15704 that means the line boundaries have changed. So we
15705 can't start copying with the row START. Maybe it will
15706 work to start copying with the following row. */
15707 while (IT_CHARPOS (it) > CHARPOS (start))
15709 /* Advance to the next row as the "start". */
15710 start_row++;
15711 start = start_row->minpos;
15712 /* If there are no more rows to try, or just one, give up. */
15713 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
15714 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
15715 || CHARPOS (start) == ZV)
15717 clear_glyph_matrix (w->desired_matrix);
15718 return 0;
15721 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
15723 /* If we have reached alignment,
15724 we can copy the rest of the rows. */
15725 if (IT_CHARPOS (it) == CHARPOS (start))
15726 break;
15728 if (display_line (&it))
15729 last_text_row = it.glyph_row - 1;
15732 /* A value of current_y < last_visible_y means that we stopped
15733 at the previous window start, which in turn means that we
15734 have at least one reusable row. */
15735 if (it.current_y < it.last_visible_y)
15737 struct glyph_row *row;
15739 /* IT.vpos always starts from 0; it counts text lines. */
15740 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
15742 /* Find PT if not already found in the lines displayed. */
15743 if (w->cursor.vpos < 0)
15745 int dy = it.current_y - start_row->y;
15747 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15748 row = row_containing_pos (w, PT, row, NULL, dy);
15749 if (row)
15750 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
15751 dy, nrows_scrolled);
15752 else
15754 clear_glyph_matrix (w->desired_matrix);
15755 return 0;
15759 /* Scroll the display. Do it before the current matrix is
15760 changed. The problem here is that update has not yet
15761 run, i.e. part of the current matrix is not up to date.
15762 scroll_run_hook will clear the cursor, and use the
15763 current matrix to get the height of the row the cursor is
15764 in. */
15765 run.current_y = start_row->y;
15766 run.desired_y = it.current_y;
15767 run.height = it.last_visible_y - it.current_y;
15769 if (run.height > 0 && run.current_y != run.desired_y)
15771 update_begin (f);
15772 FRAME_RIF (f)->update_window_begin_hook (w);
15773 FRAME_RIF (f)->clear_window_mouse_face (w);
15774 FRAME_RIF (f)->scroll_run_hook (w, &run);
15775 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15776 update_end (f);
15779 /* Shift current matrix down by nrows_scrolled lines. */
15780 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15781 rotate_matrix (w->current_matrix,
15782 start_vpos,
15783 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15784 nrows_scrolled);
15786 /* Disable lines that must be updated. */
15787 for (i = 0; i < nrows_scrolled; ++i)
15788 (start_row + i)->enabled_p = 0;
15790 /* Re-compute Y positions. */
15791 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15792 max_y = it.last_visible_y;
15793 for (row = start_row + nrows_scrolled;
15794 row < bottom_row;
15795 ++row)
15797 row->y = it.current_y;
15798 row->visible_height = row->height;
15800 if (row->y < min_y)
15801 row->visible_height -= min_y - row->y;
15802 if (row->y + row->height > max_y)
15803 row->visible_height -= row->y + row->height - max_y;
15804 if (row->fringe_bitmap_periodic_p)
15805 row->redraw_fringe_bitmaps_p = 1;
15807 it.current_y += row->height;
15809 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15810 last_reused_text_row = row;
15811 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
15812 break;
15815 /* Disable lines in the current matrix which are now
15816 below the window. */
15817 for (++row; row < bottom_row; ++row)
15818 row->enabled_p = row->mode_line_p = 0;
15821 /* Update window_end_pos etc.; last_reused_text_row is the last
15822 reused row from the current matrix containing text, if any.
15823 The value of last_text_row is the last displayed line
15824 containing text. */
15825 if (last_reused_text_row)
15827 w->window_end_bytepos
15828 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
15829 w->window_end_pos
15830 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
15831 w->window_end_vpos
15832 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
15833 w->current_matrix));
15835 else if (last_text_row)
15837 w->window_end_bytepos
15838 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15839 w->window_end_pos
15840 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15841 w->window_end_vpos
15842 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15844 else
15846 /* This window must be completely empty. */
15847 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
15848 w->window_end_pos = make_number (Z - ZV);
15849 w->window_end_vpos = make_number (0);
15851 w->window_end_valid = Qnil;
15853 /* Update hint: don't try scrolling again in update_window. */
15854 w->desired_matrix->no_scrolling_p = 1;
15856 #if GLYPH_DEBUG
15857 debug_method_add (w, "try_window_reusing_current_matrix 1");
15858 #endif
15859 return 1;
15861 else if (CHARPOS (new_start) > CHARPOS (start))
15863 struct glyph_row *pt_row, *row;
15864 struct glyph_row *first_reusable_row;
15865 struct glyph_row *first_row_to_display;
15866 int dy;
15867 int yb = window_text_bottom_y (w);
15869 /* Find the row starting at new_start, if there is one. Don't
15870 reuse a partially visible line at the end. */
15871 first_reusable_row = start_row;
15872 while (first_reusable_row->enabled_p
15873 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
15874 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15875 < CHARPOS (new_start)))
15876 ++first_reusable_row;
15878 /* Give up if there is no row to reuse. */
15879 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
15880 || !first_reusable_row->enabled_p
15881 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15882 != CHARPOS (new_start)))
15883 return 0;
15885 /* We can reuse fully visible rows beginning with
15886 first_reusable_row to the end of the window. Set
15887 first_row_to_display to the first row that cannot be reused.
15888 Set pt_row to the row containing point, if there is any. */
15889 pt_row = NULL;
15890 for (first_row_to_display = first_reusable_row;
15891 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
15892 ++first_row_to_display)
15894 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
15895 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
15896 pt_row = first_row_to_display;
15899 /* Start displaying at the start of first_row_to_display. */
15900 xassert (first_row_to_display->y < yb);
15901 init_to_row_start (&it, w, first_row_to_display);
15903 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
15904 - start_vpos);
15905 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
15906 - nrows_scrolled);
15907 it.current_y = (first_row_to_display->y - first_reusable_row->y
15908 + WINDOW_HEADER_LINE_HEIGHT (w));
15910 /* Display lines beginning with first_row_to_display in the
15911 desired matrix. Set last_text_row to the last row displayed
15912 that displays text. */
15913 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
15914 if (pt_row == NULL)
15915 w->cursor.vpos = -1;
15916 last_text_row = NULL;
15917 while (it.current_y < it.last_visible_y && !fonts_changed_p)
15918 if (display_line (&it))
15919 last_text_row = it.glyph_row - 1;
15921 /* If point is in a reused row, adjust y and vpos of the cursor
15922 position. */
15923 if (pt_row)
15925 w->cursor.vpos -= nrows_scrolled;
15926 w->cursor.y -= first_reusable_row->y - start_row->y;
15929 /* Give up if point isn't in a row displayed or reused. (This
15930 also handles the case where w->cursor.vpos < nrows_scrolled
15931 after the calls to display_line, which can happen with scroll
15932 margins. See bug#1295.) */
15933 if (w->cursor.vpos < 0)
15935 clear_glyph_matrix (w->desired_matrix);
15936 return 0;
15939 /* Scroll the display. */
15940 run.current_y = first_reusable_row->y;
15941 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
15942 run.height = it.last_visible_y - run.current_y;
15943 dy = run.current_y - run.desired_y;
15945 if (run.height)
15947 update_begin (f);
15948 FRAME_RIF (f)->update_window_begin_hook (w);
15949 FRAME_RIF (f)->clear_window_mouse_face (w);
15950 FRAME_RIF (f)->scroll_run_hook (w, &run);
15951 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15952 update_end (f);
15955 /* Adjust Y positions of reused rows. */
15956 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15957 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15958 max_y = it.last_visible_y;
15959 for (row = first_reusable_row; row < first_row_to_display; ++row)
15961 row->y -= dy;
15962 row->visible_height = row->height;
15963 if (row->y < min_y)
15964 row->visible_height -= min_y - row->y;
15965 if (row->y + row->height > max_y)
15966 row->visible_height -= row->y + row->height - max_y;
15967 if (row->fringe_bitmap_periodic_p)
15968 row->redraw_fringe_bitmaps_p = 1;
15971 /* Scroll the current matrix. */
15972 xassert (nrows_scrolled > 0);
15973 rotate_matrix (w->current_matrix,
15974 start_vpos,
15975 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15976 -nrows_scrolled);
15978 /* Disable rows not reused. */
15979 for (row -= nrows_scrolled; row < bottom_row; ++row)
15980 row->enabled_p = 0;
15982 /* Point may have moved to a different line, so we cannot assume that
15983 the previous cursor position is valid; locate the correct row. */
15984 if (pt_row)
15986 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15987 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
15988 row++)
15990 w->cursor.vpos++;
15991 w->cursor.y = row->y;
15993 if (row < bottom_row)
15995 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
15996 struct glyph *end = glyph + row->used[TEXT_AREA];
15998 /* Can't use this optimization with bidi-reordered glyph
15999 rows, unless cursor is already at point. */
16000 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16002 if (!(w->cursor.hpos >= 0
16003 && w->cursor.hpos < row->used[TEXT_AREA]
16004 && BUFFERP (glyph->object)
16005 && glyph->charpos == PT))
16006 return 0;
16008 else
16009 for (; glyph < end
16010 && (!BUFFERP (glyph->object)
16011 || glyph->charpos < PT);
16012 glyph++)
16014 w->cursor.hpos++;
16015 w->cursor.x += glyph->pixel_width;
16020 /* Adjust window end. A null value of last_text_row means that
16021 the window end is in reused rows which in turn means that
16022 only its vpos can have changed. */
16023 if (last_text_row)
16025 w->window_end_bytepos
16026 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16027 w->window_end_pos
16028 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16029 w->window_end_vpos
16030 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16032 else
16034 w->window_end_vpos
16035 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16038 w->window_end_valid = Qnil;
16039 w->desired_matrix->no_scrolling_p = 1;
16041 #if GLYPH_DEBUG
16042 debug_method_add (w, "try_window_reusing_current_matrix 2");
16043 #endif
16044 return 1;
16047 return 0;
16052 /************************************************************************
16053 Window redisplay reusing current matrix when buffer has changed
16054 ************************************************************************/
16056 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16057 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16058 EMACS_INT *, EMACS_INT *);
16059 static struct glyph_row *
16060 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16061 struct glyph_row *);
16064 /* Return the last row in MATRIX displaying text. If row START is
16065 non-null, start searching with that row. IT gives the dimensions
16066 of the display. Value is null if matrix is empty; otherwise it is
16067 a pointer to the row found. */
16069 static struct glyph_row *
16070 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16071 struct glyph_row *start)
16073 struct glyph_row *row, *row_found;
16075 /* Set row_found to the last row in IT->w's current matrix
16076 displaying text. The loop looks funny but think of partially
16077 visible lines. */
16078 row_found = NULL;
16079 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16080 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16082 xassert (row->enabled_p);
16083 row_found = row;
16084 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16085 break;
16086 ++row;
16089 return row_found;
16093 /* Return the last row in the current matrix of W that is not affected
16094 by changes at the start of current_buffer that occurred since W's
16095 current matrix was built. Value is null if no such row exists.
16097 BEG_UNCHANGED us the number of characters unchanged at the start of
16098 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16099 first changed character in current_buffer. Characters at positions <
16100 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16101 when the current matrix was built. */
16103 static struct glyph_row *
16104 find_last_unchanged_at_beg_row (struct window *w)
16106 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
16107 struct glyph_row *row;
16108 struct glyph_row *row_found = NULL;
16109 int yb = window_text_bottom_y (w);
16111 /* Find the last row displaying unchanged text. */
16112 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16113 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16114 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16115 ++row)
16117 if (/* If row ends before first_changed_pos, it is unchanged,
16118 except in some case. */
16119 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16120 /* When row ends in ZV and we write at ZV it is not
16121 unchanged. */
16122 && !row->ends_at_zv_p
16123 /* When first_changed_pos is the end of a continued line,
16124 row is not unchanged because it may be no longer
16125 continued. */
16126 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16127 && (row->continued_p
16128 || row->exact_window_width_line_p)))
16129 row_found = row;
16131 /* Stop if last visible row. */
16132 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16133 break;
16136 return row_found;
16140 /* Find the first glyph row in the current matrix of W that is not
16141 affected by changes at the end of current_buffer since the
16142 time W's current matrix was built.
16144 Return in *DELTA the number of chars by which buffer positions in
16145 unchanged text at the end of current_buffer must be adjusted.
16147 Return in *DELTA_BYTES the corresponding number of bytes.
16149 Value is null if no such row exists, i.e. all rows are affected by
16150 changes. */
16152 static struct glyph_row *
16153 find_first_unchanged_at_end_row (struct window *w,
16154 EMACS_INT *delta, EMACS_INT *delta_bytes)
16156 struct glyph_row *row;
16157 struct glyph_row *row_found = NULL;
16159 *delta = *delta_bytes = 0;
16161 /* Display must not have been paused, otherwise the current matrix
16162 is not up to date. */
16163 eassert (!NILP (w->window_end_valid));
16165 /* A value of window_end_pos >= END_UNCHANGED means that the window
16166 end is in the range of changed text. If so, there is no
16167 unchanged row at the end of W's current matrix. */
16168 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16169 return NULL;
16171 /* Set row to the last row in W's current matrix displaying text. */
16172 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16174 /* If matrix is entirely empty, no unchanged row exists. */
16175 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16177 /* The value of row is the last glyph row in the matrix having a
16178 meaningful buffer position in it. The end position of row
16179 corresponds to window_end_pos. This allows us to translate
16180 buffer positions in the current matrix to current buffer
16181 positions for characters not in changed text. */
16182 EMACS_INT Z_old =
16183 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16184 EMACS_INT Z_BYTE_old =
16185 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16186 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
16187 struct glyph_row *first_text_row
16188 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16190 *delta = Z - Z_old;
16191 *delta_bytes = Z_BYTE - Z_BYTE_old;
16193 /* Set last_unchanged_pos to the buffer position of the last
16194 character in the buffer that has not been changed. Z is the
16195 index + 1 of the last character in current_buffer, i.e. by
16196 subtracting END_UNCHANGED we get the index of the last
16197 unchanged character, and we have to add BEG to get its buffer
16198 position. */
16199 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16200 last_unchanged_pos_old = last_unchanged_pos - *delta;
16202 /* Search backward from ROW for a row displaying a line that
16203 starts at a minimum position >= last_unchanged_pos_old. */
16204 for (; row > first_text_row; --row)
16206 /* This used to abort, but it can happen.
16207 It is ok to just stop the search instead here. KFS. */
16208 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16209 break;
16211 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16212 row_found = row;
16216 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16218 return row_found;
16222 /* Make sure that glyph rows in the current matrix of window W
16223 reference the same glyph memory as corresponding rows in the
16224 frame's frame matrix. This function is called after scrolling W's
16225 current matrix on a terminal frame in try_window_id and
16226 try_window_reusing_current_matrix. */
16228 static void
16229 sync_frame_with_window_matrix_rows (struct window *w)
16231 struct frame *f = XFRAME (w->frame);
16232 struct glyph_row *window_row, *window_row_end, *frame_row;
16234 /* Preconditions: W must be a leaf window and full-width. Its frame
16235 must have a frame matrix. */
16236 xassert (NILP (w->hchild) && NILP (w->vchild));
16237 xassert (WINDOW_FULL_WIDTH_P (w));
16238 xassert (!FRAME_WINDOW_P (f));
16240 /* If W is a full-width window, glyph pointers in W's current matrix
16241 have, by definition, to be the same as glyph pointers in the
16242 corresponding frame matrix. Note that frame matrices have no
16243 marginal areas (see build_frame_matrix). */
16244 window_row = w->current_matrix->rows;
16245 window_row_end = window_row + w->current_matrix->nrows;
16246 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16247 while (window_row < window_row_end)
16249 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16250 struct glyph *end = window_row->glyphs[LAST_AREA];
16252 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16253 frame_row->glyphs[TEXT_AREA] = start;
16254 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16255 frame_row->glyphs[LAST_AREA] = end;
16257 /* Disable frame rows whose corresponding window rows have
16258 been disabled in try_window_id. */
16259 if (!window_row->enabled_p)
16260 frame_row->enabled_p = 0;
16262 ++window_row, ++frame_row;
16267 /* Find the glyph row in window W containing CHARPOS. Consider all
16268 rows between START and END (not inclusive). END null means search
16269 all rows to the end of the display area of W. Value is the row
16270 containing CHARPOS or null. */
16272 struct glyph_row *
16273 row_containing_pos (struct window *w, EMACS_INT charpos,
16274 struct glyph_row *start, struct glyph_row *end, int dy)
16276 struct glyph_row *row = start;
16277 struct glyph_row *best_row = NULL;
16278 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16279 int last_y;
16281 /* If we happen to start on a header-line, skip that. */
16282 if (row->mode_line_p)
16283 ++row;
16285 if ((end && row >= end) || !row->enabled_p)
16286 return NULL;
16288 last_y = window_text_bottom_y (w) - dy;
16290 while (1)
16292 /* Give up if we have gone too far. */
16293 if (end && row >= end)
16294 return NULL;
16295 /* This formerly returned if they were equal.
16296 I think that both quantities are of a "last plus one" type;
16297 if so, when they are equal, the row is within the screen. -- rms. */
16298 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16299 return NULL;
16301 /* If it is in this row, return this row. */
16302 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16303 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16304 /* The end position of a row equals the start
16305 position of the next row. If CHARPOS is there, we
16306 would rather display it in the next line, except
16307 when this line ends in ZV. */
16308 && !row->ends_at_zv_p
16309 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16310 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16312 struct glyph *g;
16314 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16315 || (!best_row && !row->continued_p))
16316 return row;
16317 /* In bidi-reordered rows, there could be several rows
16318 occluding point, all of them belonging to the same
16319 continued line. We need to find the row which fits
16320 CHARPOS the best. */
16321 for (g = row->glyphs[TEXT_AREA];
16322 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16323 g++)
16325 if (!STRINGP (g->object))
16327 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16329 mindif = eabs (g->charpos - charpos);
16330 best_row = row;
16331 /* Exact match always wins. */
16332 if (mindif == 0)
16333 return best_row;
16338 else if (best_row && !row->continued_p)
16339 return best_row;
16340 ++row;
16345 /* Try to redisplay window W by reusing its existing display. W's
16346 current matrix must be up to date when this function is called,
16347 i.e. window_end_valid must not be nil.
16349 Value is
16351 1 if display has been updated
16352 0 if otherwise unsuccessful
16353 -1 if redisplay with same window start is known not to succeed
16355 The following steps are performed:
16357 1. Find the last row in the current matrix of W that is not
16358 affected by changes at the start of current_buffer. If no such row
16359 is found, give up.
16361 2. Find the first row in W's current matrix that is not affected by
16362 changes at the end of current_buffer. Maybe there is no such row.
16364 3. Display lines beginning with the row + 1 found in step 1 to the
16365 row found in step 2 or, if step 2 didn't find a row, to the end of
16366 the window.
16368 4. If cursor is not known to appear on the window, give up.
16370 5. If display stopped at the row found in step 2, scroll the
16371 display and current matrix as needed.
16373 6. Maybe display some lines at the end of W, if we must. This can
16374 happen under various circumstances, like a partially visible line
16375 becoming fully visible, or because newly displayed lines are displayed
16376 in smaller font sizes.
16378 7. Update W's window end information. */
16380 static int
16381 try_window_id (struct window *w)
16383 struct frame *f = XFRAME (w->frame);
16384 struct glyph_matrix *current_matrix = w->current_matrix;
16385 struct glyph_matrix *desired_matrix = w->desired_matrix;
16386 struct glyph_row *last_unchanged_at_beg_row;
16387 struct glyph_row *first_unchanged_at_end_row;
16388 struct glyph_row *row;
16389 struct glyph_row *bottom_row;
16390 int bottom_vpos;
16391 struct it it;
16392 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
16393 int dvpos, dy;
16394 struct text_pos start_pos;
16395 struct run run;
16396 int first_unchanged_at_end_vpos = 0;
16397 struct glyph_row *last_text_row, *last_text_row_at_end;
16398 struct text_pos start;
16399 EMACS_INT first_changed_charpos, last_changed_charpos;
16401 #if GLYPH_DEBUG
16402 if (inhibit_try_window_id)
16403 return 0;
16404 #endif
16406 /* This is handy for debugging. */
16407 #if 0
16408 #define GIVE_UP(X) \
16409 do { \
16410 fprintf (stderr, "try_window_id give up %d\n", (X)); \
16411 return 0; \
16412 } while (0)
16413 #else
16414 #define GIVE_UP(X) return 0
16415 #endif
16417 SET_TEXT_POS_FROM_MARKER (start, w->start);
16419 /* Don't use this for mini-windows because these can show
16420 messages and mini-buffers, and we don't handle that here. */
16421 if (MINI_WINDOW_P (w))
16422 GIVE_UP (1);
16424 /* This flag is used to prevent redisplay optimizations. */
16425 if (windows_or_buffers_changed || cursor_type_changed)
16426 GIVE_UP (2);
16428 /* Verify that narrowing has not changed.
16429 Also verify that we were not told to prevent redisplay optimizations.
16430 It would be nice to further
16431 reduce the number of cases where this prevents try_window_id. */
16432 if (current_buffer->clip_changed
16433 || current_buffer->prevent_redisplay_optimizations_p)
16434 GIVE_UP (3);
16436 /* Window must either use window-based redisplay or be full width. */
16437 if (!FRAME_WINDOW_P (f)
16438 && (!FRAME_LINE_INS_DEL_OK (f)
16439 || !WINDOW_FULL_WIDTH_P (w)))
16440 GIVE_UP (4);
16442 /* Give up if point is known NOT to appear in W. */
16443 if (PT < CHARPOS (start))
16444 GIVE_UP (5);
16446 /* Another way to prevent redisplay optimizations. */
16447 if (XFASTINT (w->last_modified) == 0)
16448 GIVE_UP (6);
16450 /* Verify that window is not hscrolled. */
16451 if (XFASTINT (w->hscroll) != 0)
16452 GIVE_UP (7);
16454 /* Verify that display wasn't paused. */
16455 if (NILP (w->window_end_valid))
16456 GIVE_UP (8);
16458 /* Can't use this if highlighting a region because a cursor movement
16459 will do more than just set the cursor. */
16460 if (!NILP (Vtransient_mark_mode)
16461 && !NILP (BVAR (current_buffer, mark_active)))
16462 GIVE_UP (9);
16464 /* Likewise if highlighting trailing whitespace. */
16465 if (!NILP (Vshow_trailing_whitespace))
16466 GIVE_UP (11);
16468 /* Likewise if showing a region. */
16469 if (!NILP (w->region_showing))
16470 GIVE_UP (10);
16472 /* Can't use this if overlay arrow position and/or string have
16473 changed. */
16474 if (overlay_arrows_changed_p ())
16475 GIVE_UP (12);
16477 /* When word-wrap is on, adding a space to the first word of a
16478 wrapped line can change the wrap position, altering the line
16479 above it. It might be worthwhile to handle this more
16480 intelligently, but for now just redisplay from scratch. */
16481 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
16482 GIVE_UP (21);
16484 /* Under bidi reordering, adding or deleting a character in the
16485 beginning of a paragraph, before the first strong directional
16486 character, can change the base direction of the paragraph (unless
16487 the buffer specifies a fixed paragraph direction), which will
16488 require to redisplay the whole paragraph. It might be worthwhile
16489 to find the paragraph limits and widen the range of redisplayed
16490 lines to that, but for now just give up this optimization and
16491 redisplay from scratch. */
16492 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16493 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
16494 GIVE_UP (22);
16496 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
16497 only if buffer has really changed. The reason is that the gap is
16498 initially at Z for freshly visited files. The code below would
16499 set end_unchanged to 0 in that case. */
16500 if (MODIFF > SAVE_MODIFF
16501 /* This seems to happen sometimes after saving a buffer. */
16502 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
16504 if (GPT - BEG < BEG_UNCHANGED)
16505 BEG_UNCHANGED = GPT - BEG;
16506 if (Z - GPT < END_UNCHANGED)
16507 END_UNCHANGED = Z - GPT;
16510 /* The position of the first and last character that has been changed. */
16511 first_changed_charpos = BEG + BEG_UNCHANGED;
16512 last_changed_charpos = Z - END_UNCHANGED;
16514 /* If window starts after a line end, and the last change is in
16515 front of that newline, then changes don't affect the display.
16516 This case happens with stealth-fontification. Note that although
16517 the display is unchanged, glyph positions in the matrix have to
16518 be adjusted, of course. */
16519 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16520 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
16521 && ((last_changed_charpos < CHARPOS (start)
16522 && CHARPOS (start) == BEGV)
16523 || (last_changed_charpos < CHARPOS (start) - 1
16524 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
16526 EMACS_INT Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
16527 struct glyph_row *r0;
16529 /* Compute how many chars/bytes have been added to or removed
16530 from the buffer. */
16531 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16532 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16533 Z_delta = Z - Z_old;
16534 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
16536 /* Give up if PT is not in the window. Note that it already has
16537 been checked at the start of try_window_id that PT is not in
16538 front of the window start. */
16539 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
16540 GIVE_UP (13);
16542 /* If window start is unchanged, we can reuse the whole matrix
16543 as is, after adjusting glyph positions. No need to compute
16544 the window end again, since its offset from Z hasn't changed. */
16545 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16546 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
16547 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
16548 /* PT must not be in a partially visible line. */
16549 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
16550 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16552 /* Adjust positions in the glyph matrix. */
16553 if (Z_delta || Z_delta_bytes)
16555 struct glyph_row *r1
16556 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16557 increment_matrix_positions (w->current_matrix,
16558 MATRIX_ROW_VPOS (r0, current_matrix),
16559 MATRIX_ROW_VPOS (r1, current_matrix),
16560 Z_delta, Z_delta_bytes);
16563 /* Set the cursor. */
16564 row = row_containing_pos (w, PT, r0, NULL, 0);
16565 if (row)
16566 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
16567 else
16568 abort ();
16569 return 1;
16573 /* Handle the case that changes are all below what is displayed in
16574 the window, and that PT is in the window. This shortcut cannot
16575 be taken if ZV is visible in the window, and text has been added
16576 there that is visible in the window. */
16577 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
16578 /* ZV is not visible in the window, or there are no
16579 changes at ZV, actually. */
16580 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
16581 || first_changed_charpos == last_changed_charpos))
16583 struct glyph_row *r0;
16585 /* Give up if PT is not in the window. Note that it already has
16586 been checked at the start of try_window_id that PT is not in
16587 front of the window start. */
16588 if (PT >= MATRIX_ROW_END_CHARPOS (row))
16589 GIVE_UP (14);
16591 /* If window start is unchanged, we can reuse the whole matrix
16592 as is, without changing glyph positions since no text has
16593 been added/removed in front of the window end. */
16594 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16595 if (TEXT_POS_EQUAL_P (start, r0->minpos)
16596 /* PT must not be in a partially visible line. */
16597 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
16598 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16600 /* We have to compute the window end anew since text
16601 could have been added/removed after it. */
16602 w->window_end_pos
16603 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16604 w->window_end_bytepos
16605 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16607 /* Set the cursor. */
16608 row = row_containing_pos (w, PT, r0, NULL, 0);
16609 if (row)
16610 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
16611 else
16612 abort ();
16613 return 2;
16617 /* Give up if window start is in the changed area.
16619 The condition used to read
16621 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
16623 but why that was tested escapes me at the moment. */
16624 if (CHARPOS (start) >= first_changed_charpos
16625 && CHARPOS (start) <= last_changed_charpos)
16626 GIVE_UP (15);
16628 /* Check that window start agrees with the start of the first glyph
16629 row in its current matrix. Check this after we know the window
16630 start is not in changed text, otherwise positions would not be
16631 comparable. */
16632 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
16633 if (!TEXT_POS_EQUAL_P (start, row->minpos))
16634 GIVE_UP (16);
16636 /* Give up if the window ends in strings. Overlay strings
16637 at the end are difficult to handle, so don't try. */
16638 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
16639 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
16640 GIVE_UP (20);
16642 /* Compute the position at which we have to start displaying new
16643 lines. Some of the lines at the top of the window might be
16644 reusable because they are not displaying changed text. Find the
16645 last row in W's current matrix not affected by changes at the
16646 start of current_buffer. Value is null if changes start in the
16647 first line of window. */
16648 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
16649 if (last_unchanged_at_beg_row)
16651 /* Avoid starting to display in the moddle of a character, a TAB
16652 for instance. This is easier than to set up the iterator
16653 exactly, and it's not a frequent case, so the additional
16654 effort wouldn't really pay off. */
16655 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
16656 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
16657 && last_unchanged_at_beg_row > w->current_matrix->rows)
16658 --last_unchanged_at_beg_row;
16660 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
16661 GIVE_UP (17);
16663 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
16664 GIVE_UP (18);
16665 start_pos = it.current.pos;
16667 /* Start displaying new lines in the desired matrix at the same
16668 vpos we would use in the current matrix, i.e. below
16669 last_unchanged_at_beg_row. */
16670 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
16671 current_matrix);
16672 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16673 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
16675 xassert (it.hpos == 0 && it.current_x == 0);
16677 else
16679 /* There are no reusable lines at the start of the window.
16680 Start displaying in the first text line. */
16681 start_display (&it, w, start);
16682 it.vpos = it.first_vpos;
16683 start_pos = it.current.pos;
16686 /* Find the first row that is not affected by changes at the end of
16687 the buffer. Value will be null if there is no unchanged row, in
16688 which case we must redisplay to the end of the window. delta
16689 will be set to the value by which buffer positions beginning with
16690 first_unchanged_at_end_row have to be adjusted due to text
16691 changes. */
16692 first_unchanged_at_end_row
16693 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
16694 IF_DEBUG (debug_delta = delta);
16695 IF_DEBUG (debug_delta_bytes = delta_bytes);
16697 /* Set stop_pos to the buffer position up to which we will have to
16698 display new lines. If first_unchanged_at_end_row != NULL, this
16699 is the buffer position of the start of the line displayed in that
16700 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
16701 that we don't stop at a buffer position. */
16702 stop_pos = 0;
16703 if (first_unchanged_at_end_row)
16705 xassert (last_unchanged_at_beg_row == NULL
16706 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
16708 /* If this is a continuation line, move forward to the next one
16709 that isn't. Changes in lines above affect this line.
16710 Caution: this may move first_unchanged_at_end_row to a row
16711 not displaying text. */
16712 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
16713 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
16714 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
16715 < it.last_visible_y))
16716 ++first_unchanged_at_end_row;
16718 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
16719 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
16720 >= it.last_visible_y))
16721 first_unchanged_at_end_row = NULL;
16722 else
16724 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
16725 + delta);
16726 first_unchanged_at_end_vpos
16727 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
16728 xassert (stop_pos >= Z - END_UNCHANGED);
16731 else if (last_unchanged_at_beg_row == NULL)
16732 GIVE_UP (19);
16735 #if GLYPH_DEBUG
16737 /* Either there is no unchanged row at the end, or the one we have
16738 now displays text. This is a necessary condition for the window
16739 end pos calculation at the end of this function. */
16740 xassert (first_unchanged_at_end_row == NULL
16741 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
16743 debug_last_unchanged_at_beg_vpos
16744 = (last_unchanged_at_beg_row
16745 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
16746 : -1);
16747 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
16749 #endif /* GLYPH_DEBUG != 0 */
16752 /* Display new lines. Set last_text_row to the last new line
16753 displayed which has text on it, i.e. might end up as being the
16754 line where the window_end_vpos is. */
16755 w->cursor.vpos = -1;
16756 last_text_row = NULL;
16757 overlay_arrow_seen = 0;
16758 while (it.current_y < it.last_visible_y
16759 && !fonts_changed_p
16760 && (first_unchanged_at_end_row == NULL
16761 || IT_CHARPOS (it) < stop_pos))
16763 if (display_line (&it))
16764 last_text_row = it.glyph_row - 1;
16767 if (fonts_changed_p)
16768 return -1;
16771 /* Compute differences in buffer positions, y-positions etc. for
16772 lines reused at the bottom of the window. Compute what we can
16773 scroll. */
16774 if (first_unchanged_at_end_row
16775 /* No lines reused because we displayed everything up to the
16776 bottom of the window. */
16777 && it.current_y < it.last_visible_y)
16779 dvpos = (it.vpos
16780 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
16781 current_matrix));
16782 dy = it.current_y - first_unchanged_at_end_row->y;
16783 run.current_y = first_unchanged_at_end_row->y;
16784 run.desired_y = run.current_y + dy;
16785 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
16787 else
16789 delta = delta_bytes = dvpos = dy
16790 = run.current_y = run.desired_y = run.height = 0;
16791 first_unchanged_at_end_row = NULL;
16793 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
16796 /* Find the cursor if not already found. We have to decide whether
16797 PT will appear on this window (it sometimes doesn't, but this is
16798 not a very frequent case.) This decision has to be made before
16799 the current matrix is altered. A value of cursor.vpos < 0 means
16800 that PT is either in one of the lines beginning at
16801 first_unchanged_at_end_row or below the window. Don't care for
16802 lines that might be displayed later at the window end; as
16803 mentioned, this is not a frequent case. */
16804 if (w->cursor.vpos < 0)
16806 /* Cursor in unchanged rows at the top? */
16807 if (PT < CHARPOS (start_pos)
16808 && last_unchanged_at_beg_row)
16810 row = row_containing_pos (w, PT,
16811 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
16812 last_unchanged_at_beg_row + 1, 0);
16813 if (row)
16814 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16817 /* Start from first_unchanged_at_end_row looking for PT. */
16818 else if (first_unchanged_at_end_row)
16820 row = row_containing_pos (w, PT - delta,
16821 first_unchanged_at_end_row, NULL, 0);
16822 if (row)
16823 set_cursor_from_row (w, row, w->current_matrix, delta,
16824 delta_bytes, dy, dvpos);
16827 /* Give up if cursor was not found. */
16828 if (w->cursor.vpos < 0)
16830 clear_glyph_matrix (w->desired_matrix);
16831 return -1;
16835 /* Don't let the cursor end in the scroll margins. */
16837 int this_scroll_margin, cursor_height;
16839 this_scroll_margin = max (0, scroll_margin);
16840 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16841 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
16842 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
16844 if ((w->cursor.y < this_scroll_margin
16845 && CHARPOS (start) > BEGV)
16846 /* Old redisplay didn't take scroll margin into account at the bottom,
16847 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
16848 || (w->cursor.y + (make_cursor_line_fully_visible_p
16849 ? cursor_height + this_scroll_margin
16850 : 1)) > it.last_visible_y)
16852 w->cursor.vpos = -1;
16853 clear_glyph_matrix (w->desired_matrix);
16854 return -1;
16858 /* Scroll the display. Do it before changing the current matrix so
16859 that xterm.c doesn't get confused about where the cursor glyph is
16860 found. */
16861 if (dy && run.height)
16863 update_begin (f);
16865 if (FRAME_WINDOW_P (f))
16867 FRAME_RIF (f)->update_window_begin_hook (w);
16868 FRAME_RIF (f)->clear_window_mouse_face (w);
16869 FRAME_RIF (f)->scroll_run_hook (w, &run);
16870 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16872 else
16874 /* Terminal frame. In this case, dvpos gives the number of
16875 lines to scroll by; dvpos < 0 means scroll up. */
16876 int from_vpos
16877 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
16878 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
16879 int end = (WINDOW_TOP_EDGE_LINE (w)
16880 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
16881 + window_internal_height (w));
16883 #if defined (HAVE_GPM) || defined (MSDOS)
16884 x_clear_window_mouse_face (w);
16885 #endif
16886 /* Perform the operation on the screen. */
16887 if (dvpos > 0)
16889 /* Scroll last_unchanged_at_beg_row to the end of the
16890 window down dvpos lines. */
16891 set_terminal_window (f, end);
16893 /* On dumb terminals delete dvpos lines at the end
16894 before inserting dvpos empty lines. */
16895 if (!FRAME_SCROLL_REGION_OK (f))
16896 ins_del_lines (f, end - dvpos, -dvpos);
16898 /* Insert dvpos empty lines in front of
16899 last_unchanged_at_beg_row. */
16900 ins_del_lines (f, from, dvpos);
16902 else if (dvpos < 0)
16904 /* Scroll up last_unchanged_at_beg_vpos to the end of
16905 the window to last_unchanged_at_beg_vpos - |dvpos|. */
16906 set_terminal_window (f, end);
16908 /* Delete dvpos lines in front of
16909 last_unchanged_at_beg_vpos. ins_del_lines will set
16910 the cursor to the given vpos and emit |dvpos| delete
16911 line sequences. */
16912 ins_del_lines (f, from + dvpos, dvpos);
16914 /* On a dumb terminal insert dvpos empty lines at the
16915 end. */
16916 if (!FRAME_SCROLL_REGION_OK (f))
16917 ins_del_lines (f, end + dvpos, -dvpos);
16920 set_terminal_window (f, 0);
16923 update_end (f);
16926 /* Shift reused rows of the current matrix to the right position.
16927 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
16928 text. */
16929 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16930 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
16931 if (dvpos < 0)
16933 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
16934 bottom_vpos, dvpos);
16935 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
16936 bottom_vpos, 0);
16938 else if (dvpos > 0)
16940 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
16941 bottom_vpos, dvpos);
16942 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
16943 first_unchanged_at_end_vpos + dvpos, 0);
16946 /* For frame-based redisplay, make sure that current frame and window
16947 matrix are in sync with respect to glyph memory. */
16948 if (!FRAME_WINDOW_P (f))
16949 sync_frame_with_window_matrix_rows (w);
16951 /* Adjust buffer positions in reused rows. */
16952 if (delta || delta_bytes)
16953 increment_matrix_positions (current_matrix,
16954 first_unchanged_at_end_vpos + dvpos,
16955 bottom_vpos, delta, delta_bytes);
16957 /* Adjust Y positions. */
16958 if (dy)
16959 shift_glyph_matrix (w, current_matrix,
16960 first_unchanged_at_end_vpos + dvpos,
16961 bottom_vpos, dy);
16963 if (first_unchanged_at_end_row)
16965 first_unchanged_at_end_row += dvpos;
16966 if (first_unchanged_at_end_row->y >= it.last_visible_y
16967 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
16968 first_unchanged_at_end_row = NULL;
16971 /* If scrolling up, there may be some lines to display at the end of
16972 the window. */
16973 last_text_row_at_end = NULL;
16974 if (dy < 0)
16976 /* Scrolling up can leave for example a partially visible line
16977 at the end of the window to be redisplayed. */
16978 /* Set last_row to the glyph row in the current matrix where the
16979 window end line is found. It has been moved up or down in
16980 the matrix by dvpos. */
16981 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
16982 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
16984 /* If last_row is the window end line, it should display text. */
16985 xassert (last_row->displays_text_p);
16987 /* If window end line was partially visible before, begin
16988 displaying at that line. Otherwise begin displaying with the
16989 line following it. */
16990 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
16992 init_to_row_start (&it, w, last_row);
16993 it.vpos = last_vpos;
16994 it.current_y = last_row->y;
16996 else
16998 init_to_row_end (&it, w, last_row);
16999 it.vpos = 1 + last_vpos;
17000 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17001 ++last_row;
17004 /* We may start in a continuation line. If so, we have to
17005 get the right continuation_lines_width and current_x. */
17006 it.continuation_lines_width = last_row->continuation_lines_width;
17007 it.hpos = it.current_x = 0;
17009 /* Display the rest of the lines at the window end. */
17010 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17011 while (it.current_y < it.last_visible_y
17012 && !fonts_changed_p)
17014 /* Is it always sure that the display agrees with lines in
17015 the current matrix? I don't think so, so we mark rows
17016 displayed invalid in the current matrix by setting their
17017 enabled_p flag to zero. */
17018 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17019 if (display_line (&it))
17020 last_text_row_at_end = it.glyph_row - 1;
17024 /* Update window_end_pos and window_end_vpos. */
17025 if (first_unchanged_at_end_row
17026 && !last_text_row_at_end)
17028 /* Window end line if one of the preserved rows from the current
17029 matrix. Set row to the last row displaying text in current
17030 matrix starting at first_unchanged_at_end_row, after
17031 scrolling. */
17032 xassert (first_unchanged_at_end_row->displays_text_p);
17033 row = find_last_row_displaying_text (w->current_matrix, &it,
17034 first_unchanged_at_end_row);
17035 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17037 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17038 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17039 w->window_end_vpos
17040 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17041 xassert (w->window_end_bytepos >= 0);
17042 IF_DEBUG (debug_method_add (w, "A"));
17044 else if (last_text_row_at_end)
17046 w->window_end_pos
17047 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17048 w->window_end_bytepos
17049 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17050 w->window_end_vpos
17051 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17052 xassert (w->window_end_bytepos >= 0);
17053 IF_DEBUG (debug_method_add (w, "B"));
17055 else if (last_text_row)
17057 /* We have displayed either to the end of the window or at the
17058 end of the window, i.e. the last row with text is to be found
17059 in the desired matrix. */
17060 w->window_end_pos
17061 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17062 w->window_end_bytepos
17063 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17064 w->window_end_vpos
17065 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17066 xassert (w->window_end_bytepos >= 0);
17068 else if (first_unchanged_at_end_row == NULL
17069 && last_text_row == NULL
17070 && last_text_row_at_end == NULL)
17072 /* Displayed to end of window, but no line containing text was
17073 displayed. Lines were deleted at the end of the window. */
17074 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17075 int vpos = XFASTINT (w->window_end_vpos);
17076 struct glyph_row *current_row = current_matrix->rows + vpos;
17077 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17079 for (row = NULL;
17080 row == NULL && vpos >= first_vpos;
17081 --vpos, --current_row, --desired_row)
17083 if (desired_row->enabled_p)
17085 if (desired_row->displays_text_p)
17086 row = desired_row;
17088 else if (current_row->displays_text_p)
17089 row = current_row;
17092 xassert (row != NULL);
17093 w->window_end_vpos = make_number (vpos + 1);
17094 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17095 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17096 xassert (w->window_end_bytepos >= 0);
17097 IF_DEBUG (debug_method_add (w, "C"));
17099 else
17100 abort ();
17102 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17103 debug_end_vpos = XFASTINT (w->window_end_vpos));
17105 /* Record that display has not been completed. */
17106 w->window_end_valid = Qnil;
17107 w->desired_matrix->no_scrolling_p = 1;
17108 return 3;
17110 #undef GIVE_UP
17115 /***********************************************************************
17116 More debugging support
17117 ***********************************************************************/
17119 #if GLYPH_DEBUG
17121 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17122 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17123 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17126 /* Dump the contents of glyph matrix MATRIX on stderr.
17128 GLYPHS 0 means don't show glyph contents.
17129 GLYPHS 1 means show glyphs in short form
17130 GLYPHS > 1 means show glyphs in long form. */
17132 void
17133 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17135 int i;
17136 for (i = 0; i < matrix->nrows; ++i)
17137 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17141 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17142 the glyph row and area where the glyph comes from. */
17144 void
17145 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17147 if (glyph->type == CHAR_GLYPH)
17149 fprintf (stderr,
17150 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17151 glyph - row->glyphs[TEXT_AREA],
17152 'C',
17153 glyph->charpos,
17154 (BUFFERP (glyph->object)
17155 ? 'B'
17156 : (STRINGP (glyph->object)
17157 ? 'S'
17158 : '-')),
17159 glyph->pixel_width,
17160 glyph->u.ch,
17161 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17162 ? glyph->u.ch
17163 : '.'),
17164 glyph->face_id,
17165 glyph->left_box_line_p,
17166 glyph->right_box_line_p);
17168 else if (glyph->type == STRETCH_GLYPH)
17170 fprintf (stderr,
17171 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17172 glyph - row->glyphs[TEXT_AREA],
17173 'S',
17174 glyph->charpos,
17175 (BUFFERP (glyph->object)
17176 ? 'B'
17177 : (STRINGP (glyph->object)
17178 ? 'S'
17179 : '-')),
17180 glyph->pixel_width,
17182 '.',
17183 glyph->face_id,
17184 glyph->left_box_line_p,
17185 glyph->right_box_line_p);
17187 else if (glyph->type == IMAGE_GLYPH)
17189 fprintf (stderr,
17190 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17191 glyph - row->glyphs[TEXT_AREA],
17192 'I',
17193 glyph->charpos,
17194 (BUFFERP (glyph->object)
17195 ? 'B'
17196 : (STRINGP (glyph->object)
17197 ? 'S'
17198 : '-')),
17199 glyph->pixel_width,
17200 glyph->u.img_id,
17201 '.',
17202 glyph->face_id,
17203 glyph->left_box_line_p,
17204 glyph->right_box_line_p);
17206 else if (glyph->type == COMPOSITE_GLYPH)
17208 fprintf (stderr,
17209 " %5td %4c %6"pI"d %c %3d 0x%05x",
17210 glyph - row->glyphs[TEXT_AREA],
17211 '+',
17212 glyph->charpos,
17213 (BUFFERP (glyph->object)
17214 ? 'B'
17215 : (STRINGP (glyph->object)
17216 ? 'S'
17217 : '-')),
17218 glyph->pixel_width,
17219 glyph->u.cmp.id);
17220 if (glyph->u.cmp.automatic)
17221 fprintf (stderr,
17222 "[%d-%d]",
17223 glyph->slice.cmp.from, glyph->slice.cmp.to);
17224 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17225 glyph->face_id,
17226 glyph->left_box_line_p,
17227 glyph->right_box_line_p);
17232 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17233 GLYPHS 0 means don't show glyph contents.
17234 GLYPHS 1 means show glyphs in short form
17235 GLYPHS > 1 means show glyphs in long form. */
17237 void
17238 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17240 if (glyphs != 1)
17242 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17243 fprintf (stderr, "======================================================================\n");
17245 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17246 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17247 vpos,
17248 MATRIX_ROW_START_CHARPOS (row),
17249 MATRIX_ROW_END_CHARPOS (row),
17250 row->used[TEXT_AREA],
17251 row->contains_overlapping_glyphs_p,
17252 row->enabled_p,
17253 row->truncated_on_left_p,
17254 row->truncated_on_right_p,
17255 row->continued_p,
17256 MATRIX_ROW_CONTINUATION_LINE_P (row),
17257 row->displays_text_p,
17258 row->ends_at_zv_p,
17259 row->fill_line_p,
17260 row->ends_in_middle_of_char_p,
17261 row->starts_in_middle_of_char_p,
17262 row->mouse_face_p,
17263 row->x,
17264 row->y,
17265 row->pixel_width,
17266 row->height,
17267 row->visible_height,
17268 row->ascent,
17269 row->phys_ascent);
17270 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
17271 row->end.overlay_string_index,
17272 row->continuation_lines_width);
17273 fprintf (stderr, "%9"pI"d %5"pI"d\n",
17274 CHARPOS (row->start.string_pos),
17275 CHARPOS (row->end.string_pos));
17276 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17277 row->end.dpvec_index);
17280 if (glyphs > 1)
17282 int area;
17284 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17286 struct glyph *glyph = row->glyphs[area];
17287 struct glyph *glyph_end = glyph + row->used[area];
17289 /* Glyph for a line end in text. */
17290 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17291 ++glyph_end;
17293 if (glyph < glyph_end)
17294 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
17296 for (; glyph < glyph_end; ++glyph)
17297 dump_glyph (row, glyph, area);
17300 else if (glyphs == 1)
17302 int area;
17304 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17306 char *s = (char *) alloca (row->used[area] + 1);
17307 int i;
17309 for (i = 0; i < row->used[area]; ++i)
17311 struct glyph *glyph = row->glyphs[area] + i;
17312 if (glyph->type == CHAR_GLYPH
17313 && glyph->u.ch < 0x80
17314 && glyph->u.ch >= ' ')
17315 s[i] = glyph->u.ch;
17316 else
17317 s[i] = '.';
17320 s[i] = '\0';
17321 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17327 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17328 Sdump_glyph_matrix, 0, 1, "p",
17329 doc: /* Dump the current matrix of the selected window to stderr.
17330 Shows contents of glyph row structures. With non-nil
17331 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17332 glyphs in short form, otherwise show glyphs in long form. */)
17333 (Lisp_Object glyphs)
17335 struct window *w = XWINDOW (selected_window);
17336 struct buffer *buffer = XBUFFER (w->buffer);
17338 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17339 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17340 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17341 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17342 fprintf (stderr, "=============================================\n");
17343 dump_glyph_matrix (w->current_matrix,
17344 NILP (glyphs) ? 0 : XINT (glyphs));
17345 return Qnil;
17349 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17350 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17351 (void)
17353 struct frame *f = XFRAME (selected_frame);
17354 dump_glyph_matrix (f->current_matrix, 1);
17355 return Qnil;
17359 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17360 doc: /* Dump glyph row ROW to stderr.
17361 GLYPH 0 means don't dump glyphs.
17362 GLYPH 1 means dump glyphs in short form.
17363 GLYPH > 1 or omitted means dump glyphs in long form. */)
17364 (Lisp_Object row, Lisp_Object glyphs)
17366 struct glyph_matrix *matrix;
17367 int vpos;
17369 CHECK_NUMBER (row);
17370 matrix = XWINDOW (selected_window)->current_matrix;
17371 vpos = XINT (row);
17372 if (vpos >= 0 && vpos < matrix->nrows)
17373 dump_glyph_row (MATRIX_ROW (matrix, vpos),
17374 vpos,
17375 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17376 return Qnil;
17380 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
17381 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
17382 GLYPH 0 means don't dump glyphs.
17383 GLYPH 1 means dump glyphs in short form.
17384 GLYPH > 1 or omitted means dump glyphs in long form. */)
17385 (Lisp_Object row, Lisp_Object glyphs)
17387 struct frame *sf = SELECTED_FRAME ();
17388 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
17389 int vpos;
17391 CHECK_NUMBER (row);
17392 vpos = XINT (row);
17393 if (vpos >= 0 && vpos < m->nrows)
17394 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
17395 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17396 return Qnil;
17400 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
17401 doc: /* Toggle tracing of redisplay.
17402 With ARG, turn tracing on if and only if ARG is positive. */)
17403 (Lisp_Object arg)
17405 if (NILP (arg))
17406 trace_redisplay_p = !trace_redisplay_p;
17407 else
17409 arg = Fprefix_numeric_value (arg);
17410 trace_redisplay_p = XINT (arg) > 0;
17413 return Qnil;
17417 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
17418 doc: /* Like `format', but print result to stderr.
17419 usage: (trace-to-stderr STRING &rest OBJECTS) */)
17420 (ptrdiff_t nargs, Lisp_Object *args)
17422 Lisp_Object s = Fformat (nargs, args);
17423 fprintf (stderr, "%s", SDATA (s));
17424 return Qnil;
17427 #endif /* GLYPH_DEBUG */
17431 /***********************************************************************
17432 Building Desired Matrix Rows
17433 ***********************************************************************/
17435 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
17436 Used for non-window-redisplay windows, and for windows w/o left fringe. */
17438 static struct glyph_row *
17439 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
17441 struct frame *f = XFRAME (WINDOW_FRAME (w));
17442 struct buffer *buffer = XBUFFER (w->buffer);
17443 struct buffer *old = current_buffer;
17444 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
17445 int arrow_len = SCHARS (overlay_arrow_string);
17446 const unsigned char *arrow_end = arrow_string + arrow_len;
17447 const unsigned char *p;
17448 struct it it;
17449 int multibyte_p;
17450 int n_glyphs_before;
17452 set_buffer_temp (buffer);
17453 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
17454 it.glyph_row->used[TEXT_AREA] = 0;
17455 SET_TEXT_POS (it.position, 0, 0);
17457 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
17458 p = arrow_string;
17459 while (p < arrow_end)
17461 Lisp_Object face, ilisp;
17463 /* Get the next character. */
17464 if (multibyte_p)
17465 it.c = it.char_to_display = string_char_and_length (p, &it.len);
17466 else
17468 it.c = it.char_to_display = *p, it.len = 1;
17469 if (! ASCII_CHAR_P (it.c))
17470 it.char_to_display = BYTE8_TO_CHAR (it.c);
17472 p += it.len;
17474 /* Get its face. */
17475 ilisp = make_number (p - arrow_string);
17476 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
17477 it.face_id = compute_char_face (f, it.char_to_display, face);
17479 /* Compute its width, get its glyphs. */
17480 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
17481 SET_TEXT_POS (it.position, -1, -1);
17482 PRODUCE_GLYPHS (&it);
17484 /* If this character doesn't fit any more in the line, we have
17485 to remove some glyphs. */
17486 if (it.current_x > it.last_visible_x)
17488 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
17489 break;
17493 set_buffer_temp (old);
17494 return it.glyph_row;
17498 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
17499 glyphs are only inserted for terminal frames since we can't really
17500 win with truncation glyphs when partially visible glyphs are
17501 involved. Which glyphs to insert is determined by
17502 produce_special_glyphs. */
17504 static void
17505 insert_left_trunc_glyphs (struct it *it)
17507 struct it truncate_it;
17508 struct glyph *from, *end, *to, *toend;
17510 xassert (!FRAME_WINDOW_P (it->f));
17512 /* Get the truncation glyphs. */
17513 truncate_it = *it;
17514 truncate_it.current_x = 0;
17515 truncate_it.face_id = DEFAULT_FACE_ID;
17516 truncate_it.glyph_row = &scratch_glyph_row;
17517 truncate_it.glyph_row->used[TEXT_AREA] = 0;
17518 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
17519 truncate_it.object = make_number (0);
17520 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
17522 /* Overwrite glyphs from IT with truncation glyphs. */
17523 if (!it->glyph_row->reversed_p)
17525 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17526 end = from + truncate_it.glyph_row->used[TEXT_AREA];
17527 to = it->glyph_row->glyphs[TEXT_AREA];
17528 toend = to + it->glyph_row->used[TEXT_AREA];
17530 while (from < end)
17531 *to++ = *from++;
17533 /* There may be padding glyphs left over. Overwrite them too. */
17534 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
17536 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17537 while (from < end)
17538 *to++ = *from++;
17541 if (to > toend)
17542 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
17544 else
17546 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
17547 that back to front. */
17548 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
17549 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17550 toend = it->glyph_row->glyphs[TEXT_AREA];
17551 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
17553 while (from >= end && to >= toend)
17554 *to-- = *from--;
17555 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
17557 from =
17558 truncate_it.glyph_row->glyphs[TEXT_AREA]
17559 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17560 while (from >= end && to >= toend)
17561 *to-- = *from--;
17563 if (from >= end)
17565 /* Need to free some room before prepending additional
17566 glyphs. */
17567 int move_by = from - end + 1;
17568 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
17569 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
17571 for ( ; g >= g0; g--)
17572 g[move_by] = *g;
17573 while (from >= end)
17574 *to-- = *from--;
17575 it->glyph_row->used[TEXT_AREA] += move_by;
17581 /* Compute the pixel height and width of IT->glyph_row.
17583 Most of the time, ascent and height of a display line will be equal
17584 to the max_ascent and max_height values of the display iterator
17585 structure. This is not the case if
17587 1. We hit ZV without displaying anything. In this case, max_ascent
17588 and max_height will be zero.
17590 2. We have some glyphs that don't contribute to the line height.
17591 (The glyph row flag contributes_to_line_height_p is for future
17592 pixmap extensions).
17594 The first case is easily covered by using default values because in
17595 these cases, the line height does not really matter, except that it
17596 must not be zero. */
17598 static void
17599 compute_line_metrics (struct it *it)
17601 struct glyph_row *row = it->glyph_row;
17603 if (FRAME_WINDOW_P (it->f))
17605 int i, min_y, max_y;
17607 /* The line may consist of one space only, that was added to
17608 place the cursor on it. If so, the row's height hasn't been
17609 computed yet. */
17610 if (row->height == 0)
17612 if (it->max_ascent + it->max_descent == 0)
17613 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
17614 row->ascent = it->max_ascent;
17615 row->height = it->max_ascent + it->max_descent;
17616 row->phys_ascent = it->max_phys_ascent;
17617 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17618 row->extra_line_spacing = it->max_extra_line_spacing;
17621 /* Compute the width of this line. */
17622 row->pixel_width = row->x;
17623 for (i = 0; i < row->used[TEXT_AREA]; ++i)
17624 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
17626 xassert (row->pixel_width >= 0);
17627 xassert (row->ascent >= 0 && row->height > 0);
17629 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
17630 || MATRIX_ROW_OVERLAPS_PRED_P (row));
17632 /* If first line's physical ascent is larger than its logical
17633 ascent, use the physical ascent, and make the row taller.
17634 This makes accented characters fully visible. */
17635 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
17636 && row->phys_ascent > row->ascent)
17638 row->height += row->phys_ascent - row->ascent;
17639 row->ascent = row->phys_ascent;
17642 /* Compute how much of the line is visible. */
17643 row->visible_height = row->height;
17645 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
17646 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
17648 if (row->y < min_y)
17649 row->visible_height -= min_y - row->y;
17650 if (row->y + row->height > max_y)
17651 row->visible_height -= row->y + row->height - max_y;
17653 else
17655 row->pixel_width = row->used[TEXT_AREA];
17656 if (row->continued_p)
17657 row->pixel_width -= it->continuation_pixel_width;
17658 else if (row->truncated_on_right_p)
17659 row->pixel_width -= it->truncation_pixel_width;
17660 row->ascent = row->phys_ascent = 0;
17661 row->height = row->phys_height = row->visible_height = 1;
17662 row->extra_line_spacing = 0;
17665 /* Compute a hash code for this row. */
17667 int area, i;
17668 row->hash = 0;
17669 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17670 for (i = 0; i < row->used[area]; ++i)
17671 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
17672 + row->glyphs[area][i].u.val
17673 + row->glyphs[area][i].face_id
17674 + row->glyphs[area][i].padding_p
17675 + (row->glyphs[area][i].type << 2));
17678 it->max_ascent = it->max_descent = 0;
17679 it->max_phys_ascent = it->max_phys_descent = 0;
17683 /* Append one space to the glyph row of iterator IT if doing a
17684 window-based redisplay. The space has the same face as
17685 IT->face_id. Value is non-zero if a space was added.
17687 This function is called to make sure that there is always one glyph
17688 at the end of a glyph row that the cursor can be set on under
17689 window-systems. (If there weren't such a glyph we would not know
17690 how wide and tall a box cursor should be displayed).
17692 At the same time this space let's a nicely handle clearing to the
17693 end of the line if the row ends in italic text. */
17695 static int
17696 append_space_for_newline (struct it *it, int default_face_p)
17698 if (FRAME_WINDOW_P (it->f))
17700 int n = it->glyph_row->used[TEXT_AREA];
17702 if (it->glyph_row->glyphs[TEXT_AREA] + n
17703 < it->glyph_row->glyphs[1 + TEXT_AREA])
17705 /* Save some values that must not be changed.
17706 Must save IT->c and IT->len because otherwise
17707 ITERATOR_AT_END_P wouldn't work anymore after
17708 append_space_for_newline has been called. */
17709 enum display_element_type saved_what = it->what;
17710 int saved_c = it->c, saved_len = it->len;
17711 int saved_char_to_display = it->char_to_display;
17712 int saved_x = it->current_x;
17713 int saved_face_id = it->face_id;
17714 struct text_pos saved_pos;
17715 Lisp_Object saved_object;
17716 struct face *face;
17718 saved_object = it->object;
17719 saved_pos = it->position;
17721 it->what = IT_CHARACTER;
17722 memset (&it->position, 0, sizeof it->position);
17723 it->object = make_number (0);
17724 it->c = it->char_to_display = ' ';
17725 it->len = 1;
17727 if (default_face_p)
17728 it->face_id = DEFAULT_FACE_ID;
17729 else if (it->face_before_selective_p)
17730 it->face_id = it->saved_face_id;
17731 face = FACE_FROM_ID (it->f, it->face_id);
17732 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
17734 PRODUCE_GLYPHS (it);
17736 it->override_ascent = -1;
17737 it->constrain_row_ascent_descent_p = 0;
17738 it->current_x = saved_x;
17739 it->object = saved_object;
17740 it->position = saved_pos;
17741 it->what = saved_what;
17742 it->face_id = saved_face_id;
17743 it->len = saved_len;
17744 it->c = saved_c;
17745 it->char_to_display = saved_char_to_display;
17746 return 1;
17750 return 0;
17754 /* Extend the face of the last glyph in the text area of IT->glyph_row
17755 to the end of the display line. Called from display_line. If the
17756 glyph row is empty, add a space glyph to it so that we know the
17757 face to draw. Set the glyph row flag fill_line_p. If the glyph
17758 row is R2L, prepend a stretch glyph to cover the empty space to the
17759 left of the leftmost glyph. */
17761 static void
17762 extend_face_to_end_of_line (struct it *it)
17764 struct face *face;
17765 struct frame *f = it->f;
17767 /* If line is already filled, do nothing. Non window-system frames
17768 get a grace of one more ``pixel'' because their characters are
17769 1-``pixel'' wide, so they hit the equality too early. This grace
17770 is needed only for R2L rows that are not continued, to produce
17771 one extra blank where we could display the cursor. */
17772 if (it->current_x >= it->last_visible_x
17773 + (!FRAME_WINDOW_P (f)
17774 && it->glyph_row->reversed_p
17775 && !it->glyph_row->continued_p))
17776 return;
17778 /* Face extension extends the background and box of IT->face_id
17779 to the end of the line. If the background equals the background
17780 of the frame, we don't have to do anything. */
17781 if (it->face_before_selective_p)
17782 face = FACE_FROM_ID (f, it->saved_face_id);
17783 else
17784 face = FACE_FROM_ID (f, it->face_id);
17786 if (FRAME_WINDOW_P (f)
17787 && it->glyph_row->displays_text_p
17788 && face->box == FACE_NO_BOX
17789 && face->background == FRAME_BACKGROUND_PIXEL (f)
17790 && !face->stipple
17791 && !it->glyph_row->reversed_p)
17792 return;
17794 /* Set the glyph row flag indicating that the face of the last glyph
17795 in the text area has to be drawn to the end of the text area. */
17796 it->glyph_row->fill_line_p = 1;
17798 /* If current character of IT is not ASCII, make sure we have the
17799 ASCII face. This will be automatically undone the next time
17800 get_next_display_element returns a multibyte character. Note
17801 that the character will always be single byte in unibyte
17802 text. */
17803 if (!ASCII_CHAR_P (it->c))
17805 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
17808 if (FRAME_WINDOW_P (f))
17810 /* If the row is empty, add a space with the current face of IT,
17811 so that we know which face to draw. */
17812 if (it->glyph_row->used[TEXT_AREA] == 0)
17814 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
17815 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
17816 it->glyph_row->used[TEXT_AREA] = 1;
17818 #ifdef HAVE_WINDOW_SYSTEM
17819 if (it->glyph_row->reversed_p)
17821 /* Prepend a stretch glyph to the row, such that the
17822 rightmost glyph will be drawn flushed all the way to the
17823 right margin of the window. The stretch glyph that will
17824 occupy the empty space, if any, to the left of the
17825 glyphs. */
17826 struct font *font = face->font ? face->font : FRAME_FONT (f);
17827 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
17828 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
17829 struct glyph *g;
17830 int row_width, stretch_ascent, stretch_width;
17831 struct text_pos saved_pos;
17832 int saved_face_id, saved_avoid_cursor;
17834 for (row_width = 0, g = row_start; g < row_end; g++)
17835 row_width += g->pixel_width;
17836 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
17837 if (stretch_width > 0)
17839 stretch_ascent =
17840 (((it->ascent + it->descent)
17841 * FONT_BASE (font)) / FONT_HEIGHT (font));
17842 saved_pos = it->position;
17843 memset (&it->position, 0, sizeof it->position);
17844 saved_avoid_cursor = it->avoid_cursor_p;
17845 it->avoid_cursor_p = 1;
17846 saved_face_id = it->face_id;
17847 /* The last row's stretch glyph should get the default
17848 face, to avoid painting the rest of the window with
17849 the region face, if the region ends at ZV. */
17850 if (it->glyph_row->ends_at_zv_p)
17851 it->face_id = DEFAULT_FACE_ID;
17852 else
17853 it->face_id = face->id;
17854 append_stretch_glyph (it, make_number (0), stretch_width,
17855 it->ascent + it->descent, stretch_ascent);
17856 it->position = saved_pos;
17857 it->avoid_cursor_p = saved_avoid_cursor;
17858 it->face_id = saved_face_id;
17861 #endif /* HAVE_WINDOW_SYSTEM */
17863 else
17865 /* Save some values that must not be changed. */
17866 int saved_x = it->current_x;
17867 struct text_pos saved_pos;
17868 Lisp_Object saved_object;
17869 enum display_element_type saved_what = it->what;
17870 int saved_face_id = it->face_id;
17872 saved_object = it->object;
17873 saved_pos = it->position;
17875 it->what = IT_CHARACTER;
17876 memset (&it->position, 0, sizeof it->position);
17877 it->object = make_number (0);
17878 it->c = it->char_to_display = ' ';
17879 it->len = 1;
17880 /* The last row's blank glyphs should get the default face, to
17881 avoid painting the rest of the window with the region face,
17882 if the region ends at ZV. */
17883 if (it->glyph_row->ends_at_zv_p)
17884 it->face_id = DEFAULT_FACE_ID;
17885 else
17886 it->face_id = face->id;
17888 PRODUCE_GLYPHS (it);
17890 while (it->current_x <= it->last_visible_x)
17891 PRODUCE_GLYPHS (it);
17893 /* Don't count these blanks really. It would let us insert a left
17894 truncation glyph below and make us set the cursor on them, maybe. */
17895 it->current_x = saved_x;
17896 it->object = saved_object;
17897 it->position = saved_pos;
17898 it->what = saved_what;
17899 it->face_id = saved_face_id;
17904 /* Value is non-zero if text starting at CHARPOS in current_buffer is
17905 trailing whitespace. */
17907 static int
17908 trailing_whitespace_p (EMACS_INT charpos)
17910 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
17911 int c = 0;
17913 while (bytepos < ZV_BYTE
17914 && (c = FETCH_CHAR (bytepos),
17915 c == ' ' || c == '\t'))
17916 ++bytepos;
17918 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
17920 if (bytepos != PT_BYTE)
17921 return 1;
17923 return 0;
17927 /* Highlight trailing whitespace, if any, in ROW. */
17929 static void
17930 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
17932 int used = row->used[TEXT_AREA];
17934 if (used)
17936 struct glyph *start = row->glyphs[TEXT_AREA];
17937 struct glyph *glyph = start + used - 1;
17939 if (row->reversed_p)
17941 /* Right-to-left rows need to be processed in the opposite
17942 direction, so swap the edge pointers. */
17943 glyph = start;
17944 start = row->glyphs[TEXT_AREA] + used - 1;
17947 /* Skip over glyphs inserted to display the cursor at the
17948 end of a line, for extending the face of the last glyph
17949 to the end of the line on terminals, and for truncation
17950 and continuation glyphs. */
17951 if (!row->reversed_p)
17953 while (glyph >= start
17954 && glyph->type == CHAR_GLYPH
17955 && INTEGERP (glyph->object))
17956 --glyph;
17958 else
17960 while (glyph <= start
17961 && glyph->type == CHAR_GLYPH
17962 && INTEGERP (glyph->object))
17963 ++glyph;
17966 /* If last glyph is a space or stretch, and it's trailing
17967 whitespace, set the face of all trailing whitespace glyphs in
17968 IT->glyph_row to `trailing-whitespace'. */
17969 if ((row->reversed_p ? glyph <= start : glyph >= start)
17970 && BUFFERP (glyph->object)
17971 && (glyph->type == STRETCH_GLYPH
17972 || (glyph->type == CHAR_GLYPH
17973 && glyph->u.ch == ' '))
17974 && trailing_whitespace_p (glyph->charpos))
17976 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
17977 if (face_id < 0)
17978 return;
17980 if (!row->reversed_p)
17982 while (glyph >= start
17983 && BUFFERP (glyph->object)
17984 && (glyph->type == STRETCH_GLYPH
17985 || (glyph->type == CHAR_GLYPH
17986 && glyph->u.ch == ' ')))
17987 (glyph--)->face_id = face_id;
17989 else
17991 while (glyph <= start
17992 && BUFFERP (glyph->object)
17993 && (glyph->type == STRETCH_GLYPH
17994 || (glyph->type == CHAR_GLYPH
17995 && glyph->u.ch == ' ')))
17996 (glyph++)->face_id = face_id;
18003 /* Value is non-zero if glyph row ROW should be
18004 used to hold the cursor. */
18006 static int
18007 cursor_row_p (struct glyph_row *row)
18009 int result = 1;
18011 if (PT == CHARPOS (row->end.pos))
18013 /* Suppose the row ends on a string.
18014 Unless the row is continued, that means it ends on a newline
18015 in the string. If it's anything other than a display string
18016 (e.g. a before-string from an overlay), we don't want the
18017 cursor there. (This heuristic seems to give the optimal
18018 behavior for the various types of multi-line strings.) */
18019 if (CHARPOS (row->end.string_pos) >= 0)
18021 if (row->continued_p)
18022 result = 1;
18023 else
18025 /* Check for `display' property. */
18026 struct glyph *beg = row->glyphs[TEXT_AREA];
18027 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18028 struct glyph *glyph;
18030 result = 0;
18031 for (glyph = end; glyph >= beg; --glyph)
18032 if (STRINGP (glyph->object))
18034 Lisp_Object prop
18035 = Fget_char_property (make_number (PT),
18036 Qdisplay, Qnil);
18037 result =
18038 (!NILP (prop)
18039 && display_prop_string_p (prop, glyph->object));
18040 break;
18044 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18046 /* If the row ends in middle of a real character,
18047 and the line is continued, we want the cursor here.
18048 That's because CHARPOS (ROW->end.pos) would equal
18049 PT if PT is before the character. */
18050 if (!row->ends_in_ellipsis_p)
18051 result = row->continued_p;
18052 else
18053 /* If the row ends in an ellipsis, then
18054 CHARPOS (ROW->end.pos) will equal point after the
18055 invisible text. We want that position to be displayed
18056 after the ellipsis. */
18057 result = 0;
18059 /* If the row ends at ZV, display the cursor at the end of that
18060 row instead of at the start of the row below. */
18061 else if (row->ends_at_zv_p)
18062 result = 1;
18063 else
18064 result = 0;
18067 return result;
18072 /* Push the property PROP so that it will be rendered at the current
18073 position in IT. Return 1 if PROP was successfully pushed, 0
18074 otherwise. Called from handle_line_prefix to handle the
18075 `line-prefix' and `wrap-prefix' properties. */
18077 static int
18078 push_display_prop (struct it *it, Lisp_Object prop)
18080 struct text_pos pos =
18081 (it->method == GET_FROM_STRING) ? it->current.string_pos : it->current.pos;
18083 xassert (it->method == GET_FROM_BUFFER
18084 || it->method == GET_FROM_STRING);
18086 /* We need to save the current buffer/string position, so it will be
18087 restored by pop_it, because iterate_out_of_display_property
18088 depends on that being set correctly, but some situations leave
18089 it->position not yet set when this function is called. */
18090 push_it (it, &pos);
18092 if (STRINGP (prop))
18094 if (SCHARS (prop) == 0)
18096 pop_it (it);
18097 return 0;
18100 it->string = prop;
18101 it->multibyte_p = STRING_MULTIBYTE (it->string);
18102 it->current.overlay_string_index = -1;
18103 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18104 it->end_charpos = it->string_nchars = SCHARS (it->string);
18105 it->method = GET_FROM_STRING;
18106 it->stop_charpos = 0;
18107 it->prev_stop = 0;
18108 it->base_level_stop = 0;
18110 /* Force paragraph direction to be that of the parent
18111 buffer/string. */
18112 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18113 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18114 else
18115 it->paragraph_embedding = L2R;
18117 /* Set up the bidi iterator for this display string. */
18118 if (it->bidi_p)
18120 it->bidi_it.string.lstring = it->string;
18121 it->bidi_it.string.s = NULL;
18122 it->bidi_it.string.schars = it->end_charpos;
18123 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18124 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18125 it->bidi_it.string.unibyte = !it->multibyte_p;
18126 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18129 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18131 it->method = GET_FROM_STRETCH;
18132 it->object = prop;
18134 #ifdef HAVE_WINDOW_SYSTEM
18135 else if (IMAGEP (prop))
18137 it->what = IT_IMAGE;
18138 it->image_id = lookup_image (it->f, prop);
18139 it->method = GET_FROM_IMAGE;
18141 #endif /* HAVE_WINDOW_SYSTEM */
18142 else
18144 pop_it (it); /* bogus display property, give up */
18145 return 0;
18148 return 1;
18151 /* Return the character-property PROP at the current position in IT. */
18153 static Lisp_Object
18154 get_it_property (struct it *it, Lisp_Object prop)
18156 Lisp_Object position;
18158 if (STRINGP (it->object))
18159 position = make_number (IT_STRING_CHARPOS (*it));
18160 else if (BUFFERP (it->object))
18161 position = make_number (IT_CHARPOS (*it));
18162 else
18163 return Qnil;
18165 return Fget_char_property (position, prop, it->object);
18168 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18170 static void
18171 handle_line_prefix (struct it *it)
18173 Lisp_Object prefix;
18175 if (it->continuation_lines_width > 0)
18177 prefix = get_it_property (it, Qwrap_prefix);
18178 if (NILP (prefix))
18179 prefix = Vwrap_prefix;
18181 else
18183 prefix = get_it_property (it, Qline_prefix);
18184 if (NILP (prefix))
18185 prefix = Vline_prefix;
18187 if (! NILP (prefix) && push_display_prop (it, prefix))
18189 /* If the prefix is wider than the window, and we try to wrap
18190 it, it would acquire its own wrap prefix, and so on till the
18191 iterator stack overflows. So, don't wrap the prefix. */
18192 it->line_wrap = TRUNCATE;
18193 it->avoid_cursor_p = 1;
18199 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18200 only for R2L lines from display_line and display_string, when they
18201 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18202 the line/string needs to be continued on the next glyph row. */
18203 static void
18204 unproduce_glyphs (struct it *it, int n)
18206 struct glyph *glyph, *end;
18208 xassert (it->glyph_row);
18209 xassert (it->glyph_row->reversed_p);
18210 xassert (it->area == TEXT_AREA);
18211 xassert (n <= it->glyph_row->used[TEXT_AREA]);
18213 if (n > it->glyph_row->used[TEXT_AREA])
18214 n = it->glyph_row->used[TEXT_AREA];
18215 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18216 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18217 for ( ; glyph < end; glyph++)
18218 glyph[-n] = *glyph;
18221 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18222 and ROW->maxpos. */
18223 static void
18224 find_row_edges (struct it *it, struct glyph_row *row,
18225 EMACS_INT min_pos, EMACS_INT min_bpos,
18226 EMACS_INT max_pos, EMACS_INT max_bpos)
18228 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18229 lines' rows is implemented for bidi-reordered rows. */
18231 /* ROW->minpos is the value of min_pos, the minimal buffer position
18232 we have in ROW, or ROW->start.pos if that is smaller. */
18233 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18234 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18235 else
18236 /* We didn't find buffer positions smaller than ROW->start, or
18237 didn't find _any_ valid buffer positions in any of the glyphs,
18238 so we must trust the iterator's computed positions. */
18239 row->minpos = row->start.pos;
18240 if (max_pos <= 0)
18242 max_pos = CHARPOS (it->current.pos);
18243 max_bpos = BYTEPOS (it->current.pos);
18246 /* Here are the various use-cases for ending the row, and the
18247 corresponding values for ROW->maxpos:
18249 Line ends in a newline from buffer eol_pos + 1
18250 Line is continued from buffer max_pos + 1
18251 Line is truncated on right it->current.pos
18252 Line ends in a newline from string max_pos
18253 Line is continued from string max_pos
18254 Line is continued from display vector max_pos
18255 Line is entirely from a string min_pos == max_pos
18256 Line is entirely from a display vector min_pos == max_pos
18257 Line that ends at ZV ZV
18259 If you discover other use-cases, please add them here as
18260 appropriate. */
18261 if (row->ends_at_zv_p)
18262 row->maxpos = it->current.pos;
18263 else if (row->used[TEXT_AREA])
18265 if (row->ends_in_newline_from_string_p)
18266 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18267 else if (CHARPOS (it->eol_pos) > 0)
18268 SET_TEXT_POS (row->maxpos,
18269 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
18270 else if (row->continued_p)
18272 /* If max_pos is different from IT's current position, it
18273 means IT->method does not belong to the display element
18274 at max_pos. However, it also means that the display
18275 element at max_pos was displayed in its entirety on this
18276 line, which is equivalent to saying that the next line
18277 starts at the next buffer position. */
18278 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
18279 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18280 else
18282 INC_BOTH (max_pos, max_bpos);
18283 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18286 else if (row->truncated_on_right_p)
18287 /* display_line already called reseat_at_next_visible_line_start,
18288 which puts the iterator at the beginning of the next line, in
18289 the logical order. */
18290 row->maxpos = it->current.pos;
18291 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
18292 /* A line that is entirely from a string/image/stretch... */
18293 row->maxpos = row->minpos;
18294 else
18295 abort ();
18297 else
18298 row->maxpos = it->current.pos;
18301 /* Construct the glyph row IT->glyph_row in the desired matrix of
18302 IT->w from text at the current position of IT. See dispextern.h
18303 for an overview of struct it. Value is non-zero if
18304 IT->glyph_row displays text, as opposed to a line displaying ZV
18305 only. */
18307 static int
18308 display_line (struct it *it)
18310 struct glyph_row *row = it->glyph_row;
18311 Lisp_Object overlay_arrow_string;
18312 struct it wrap_it;
18313 void *wrap_data = NULL;
18314 int may_wrap = 0, wrap_x IF_LINT (= 0);
18315 int wrap_row_used = -1;
18316 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
18317 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
18318 int wrap_row_extra_line_spacing IF_LINT (= 0);
18319 EMACS_INT wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
18320 EMACS_INT wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
18321 int cvpos;
18322 EMACS_INT min_pos = ZV + 1, max_pos = 0;
18323 EMACS_INT min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
18325 /* We always start displaying at hpos zero even if hscrolled. */
18326 xassert (it->hpos == 0 && it->current_x == 0);
18328 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
18329 >= it->w->desired_matrix->nrows)
18331 it->w->nrows_scale_factor++;
18332 fonts_changed_p = 1;
18333 return 0;
18336 /* Is IT->w showing the region? */
18337 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
18339 /* Clear the result glyph row and enable it. */
18340 prepare_desired_row (row);
18342 row->y = it->current_y;
18343 row->start = it->start;
18344 row->continuation_lines_width = it->continuation_lines_width;
18345 row->displays_text_p = 1;
18346 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
18347 it->starts_in_middle_of_char_p = 0;
18349 /* Arrange the overlays nicely for our purposes. Usually, we call
18350 display_line on only one line at a time, in which case this
18351 can't really hurt too much, or we call it on lines which appear
18352 one after another in the buffer, in which case all calls to
18353 recenter_overlay_lists but the first will be pretty cheap. */
18354 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
18356 /* Move over display elements that are not visible because we are
18357 hscrolled. This may stop at an x-position < IT->first_visible_x
18358 if the first glyph is partially visible or if we hit a line end. */
18359 if (it->current_x < it->first_visible_x)
18361 this_line_min_pos = row->start.pos;
18362 move_it_in_display_line_to (it, ZV, it->first_visible_x,
18363 MOVE_TO_POS | MOVE_TO_X);
18364 /* Record the smallest positions seen while we moved over
18365 display elements that are not visible. This is needed by
18366 redisplay_internal for optimizing the case where the cursor
18367 stays inside the same line. The rest of this function only
18368 considers positions that are actually displayed, so
18369 RECORD_MAX_MIN_POS will not otherwise record positions that
18370 are hscrolled to the left of the left edge of the window. */
18371 min_pos = CHARPOS (this_line_min_pos);
18372 min_bpos = BYTEPOS (this_line_min_pos);
18374 else
18376 /* We only do this when not calling `move_it_in_display_line_to'
18377 above, because move_it_in_display_line_to calls
18378 handle_line_prefix itself. */
18379 handle_line_prefix (it);
18382 /* Get the initial row height. This is either the height of the
18383 text hscrolled, if there is any, or zero. */
18384 row->ascent = it->max_ascent;
18385 row->height = it->max_ascent + it->max_descent;
18386 row->phys_ascent = it->max_phys_ascent;
18387 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18388 row->extra_line_spacing = it->max_extra_line_spacing;
18390 /* Utility macro to record max and min buffer positions seen until now. */
18391 #define RECORD_MAX_MIN_POS(IT) \
18392 do \
18394 int composition_p = (IT)->what == IT_COMPOSITION; \
18395 EMACS_INT current_pos = \
18396 composition_p ? (IT)->cmp_it.charpos \
18397 : IT_CHARPOS (*(IT)); \
18398 EMACS_INT current_bpos = \
18399 composition_p ? CHAR_TO_BYTE (current_pos) \
18400 : IT_BYTEPOS (*(IT)); \
18401 if (current_pos < min_pos) \
18403 min_pos = current_pos; \
18404 min_bpos = current_bpos; \
18406 if (current_pos > max_pos) \
18408 max_pos = current_pos; \
18409 max_bpos = current_bpos; \
18412 while (0)
18414 /* Loop generating characters. The loop is left with IT on the next
18415 character to display. */
18416 while (1)
18418 int n_glyphs_before, hpos_before, x_before;
18419 int x, nglyphs;
18420 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
18422 /* Retrieve the next thing to display. Value is zero if end of
18423 buffer reached. */
18424 if (!get_next_display_element (it))
18426 /* Maybe add a space at the end of this line that is used to
18427 display the cursor there under X. Set the charpos of the
18428 first glyph of blank lines not corresponding to any text
18429 to -1. */
18430 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18431 row->exact_window_width_line_p = 1;
18432 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
18433 || row->used[TEXT_AREA] == 0)
18435 row->glyphs[TEXT_AREA]->charpos = -1;
18436 row->displays_text_p = 0;
18438 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
18439 && (!MINI_WINDOW_P (it->w)
18440 || (minibuf_level && EQ (it->window, minibuf_window))))
18441 row->indicate_empty_line_p = 1;
18444 it->continuation_lines_width = 0;
18445 row->ends_at_zv_p = 1;
18446 /* A row that displays right-to-left text must always have
18447 its last face extended all the way to the end of line,
18448 even if this row ends in ZV, because we still write to
18449 the screen left to right. */
18450 if (row->reversed_p)
18451 extend_face_to_end_of_line (it);
18452 break;
18455 /* Now, get the metrics of what we want to display. This also
18456 generates glyphs in `row' (which is IT->glyph_row). */
18457 n_glyphs_before = row->used[TEXT_AREA];
18458 x = it->current_x;
18460 /* Remember the line height so far in case the next element doesn't
18461 fit on the line. */
18462 if (it->line_wrap != TRUNCATE)
18464 ascent = it->max_ascent;
18465 descent = it->max_descent;
18466 phys_ascent = it->max_phys_ascent;
18467 phys_descent = it->max_phys_descent;
18469 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
18471 if (IT_DISPLAYING_WHITESPACE (it))
18472 may_wrap = 1;
18473 else if (may_wrap)
18475 SAVE_IT (wrap_it, *it, wrap_data);
18476 wrap_x = x;
18477 wrap_row_used = row->used[TEXT_AREA];
18478 wrap_row_ascent = row->ascent;
18479 wrap_row_height = row->height;
18480 wrap_row_phys_ascent = row->phys_ascent;
18481 wrap_row_phys_height = row->phys_height;
18482 wrap_row_extra_line_spacing = row->extra_line_spacing;
18483 wrap_row_min_pos = min_pos;
18484 wrap_row_min_bpos = min_bpos;
18485 wrap_row_max_pos = max_pos;
18486 wrap_row_max_bpos = max_bpos;
18487 may_wrap = 0;
18492 PRODUCE_GLYPHS (it);
18494 /* If this display element was in marginal areas, continue with
18495 the next one. */
18496 if (it->area != TEXT_AREA)
18498 row->ascent = max (row->ascent, it->max_ascent);
18499 row->height = max (row->height, it->max_ascent + it->max_descent);
18500 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18501 row->phys_height = max (row->phys_height,
18502 it->max_phys_ascent + it->max_phys_descent);
18503 row->extra_line_spacing = max (row->extra_line_spacing,
18504 it->max_extra_line_spacing);
18505 set_iterator_to_next (it, 1);
18506 continue;
18509 /* Does the display element fit on the line? If we truncate
18510 lines, we should draw past the right edge of the window. If
18511 we don't truncate, we want to stop so that we can display the
18512 continuation glyph before the right margin. If lines are
18513 continued, there are two possible strategies for characters
18514 resulting in more than 1 glyph (e.g. tabs): Display as many
18515 glyphs as possible in this line and leave the rest for the
18516 continuation line, or display the whole element in the next
18517 line. Original redisplay did the former, so we do it also. */
18518 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
18519 hpos_before = it->hpos;
18520 x_before = x;
18522 if (/* Not a newline. */
18523 nglyphs > 0
18524 /* Glyphs produced fit entirely in the line. */
18525 && it->current_x < it->last_visible_x)
18527 it->hpos += nglyphs;
18528 row->ascent = max (row->ascent, it->max_ascent);
18529 row->height = max (row->height, it->max_ascent + it->max_descent);
18530 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18531 row->phys_height = max (row->phys_height,
18532 it->max_phys_ascent + it->max_phys_descent);
18533 row->extra_line_spacing = max (row->extra_line_spacing,
18534 it->max_extra_line_spacing);
18535 if (it->current_x - it->pixel_width < it->first_visible_x)
18536 row->x = x - it->first_visible_x;
18537 /* Record the maximum and minimum buffer positions seen so
18538 far in glyphs that will be displayed by this row. */
18539 if (it->bidi_p)
18540 RECORD_MAX_MIN_POS (it);
18542 else
18544 int i, new_x;
18545 struct glyph *glyph;
18547 for (i = 0; i < nglyphs; ++i, x = new_x)
18549 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18550 new_x = x + glyph->pixel_width;
18552 if (/* Lines are continued. */
18553 it->line_wrap != TRUNCATE
18554 && (/* Glyph doesn't fit on the line. */
18555 new_x > it->last_visible_x
18556 /* Or it fits exactly on a window system frame. */
18557 || (new_x == it->last_visible_x
18558 && FRAME_WINDOW_P (it->f))))
18560 /* End of a continued line. */
18562 if (it->hpos == 0
18563 || (new_x == it->last_visible_x
18564 && FRAME_WINDOW_P (it->f)))
18566 /* Current glyph is the only one on the line or
18567 fits exactly on the line. We must continue
18568 the line because we can't draw the cursor
18569 after the glyph. */
18570 row->continued_p = 1;
18571 it->current_x = new_x;
18572 it->continuation_lines_width += new_x;
18573 ++it->hpos;
18574 /* Record the maximum and minimum buffer
18575 positions seen so far in glyphs that will be
18576 displayed by this row. */
18577 if (it->bidi_p)
18578 RECORD_MAX_MIN_POS (it);
18579 if (i == nglyphs - 1)
18581 /* If line-wrap is on, check if a previous
18582 wrap point was found. */
18583 if (wrap_row_used > 0
18584 /* Even if there is a previous wrap
18585 point, continue the line here as
18586 usual, if (i) the previous character
18587 was a space or tab AND (ii) the
18588 current character is not. */
18589 && (!may_wrap
18590 || IT_DISPLAYING_WHITESPACE (it)))
18591 goto back_to_wrap;
18593 set_iterator_to_next (it, 1);
18594 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18596 if (!get_next_display_element (it))
18598 row->exact_window_width_line_p = 1;
18599 it->continuation_lines_width = 0;
18600 row->continued_p = 0;
18601 row->ends_at_zv_p = 1;
18603 else if (ITERATOR_AT_END_OF_LINE_P (it))
18605 row->continued_p = 0;
18606 row->exact_window_width_line_p = 1;
18611 else if (CHAR_GLYPH_PADDING_P (*glyph)
18612 && !FRAME_WINDOW_P (it->f))
18614 /* A padding glyph that doesn't fit on this line.
18615 This means the whole character doesn't fit
18616 on the line. */
18617 if (row->reversed_p)
18618 unproduce_glyphs (it, row->used[TEXT_AREA]
18619 - n_glyphs_before);
18620 row->used[TEXT_AREA] = n_glyphs_before;
18622 /* Fill the rest of the row with continuation
18623 glyphs like in 20.x. */
18624 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
18625 < row->glyphs[1 + TEXT_AREA])
18626 produce_special_glyphs (it, IT_CONTINUATION);
18628 row->continued_p = 1;
18629 it->current_x = x_before;
18630 it->continuation_lines_width += x_before;
18632 /* Restore the height to what it was before the
18633 element not fitting on the line. */
18634 it->max_ascent = ascent;
18635 it->max_descent = descent;
18636 it->max_phys_ascent = phys_ascent;
18637 it->max_phys_descent = phys_descent;
18639 else if (wrap_row_used > 0)
18641 back_to_wrap:
18642 if (row->reversed_p)
18643 unproduce_glyphs (it,
18644 row->used[TEXT_AREA] - wrap_row_used);
18645 RESTORE_IT (it, &wrap_it, wrap_data);
18646 it->continuation_lines_width += wrap_x;
18647 row->used[TEXT_AREA] = wrap_row_used;
18648 row->ascent = wrap_row_ascent;
18649 row->height = wrap_row_height;
18650 row->phys_ascent = wrap_row_phys_ascent;
18651 row->phys_height = wrap_row_phys_height;
18652 row->extra_line_spacing = wrap_row_extra_line_spacing;
18653 min_pos = wrap_row_min_pos;
18654 min_bpos = wrap_row_min_bpos;
18655 max_pos = wrap_row_max_pos;
18656 max_bpos = wrap_row_max_bpos;
18657 row->continued_p = 1;
18658 row->ends_at_zv_p = 0;
18659 row->exact_window_width_line_p = 0;
18660 it->continuation_lines_width += x;
18662 /* Make sure that a non-default face is extended
18663 up to the right margin of the window. */
18664 extend_face_to_end_of_line (it);
18666 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
18668 /* A TAB that extends past the right edge of the
18669 window. This produces a single glyph on
18670 window system frames. We leave the glyph in
18671 this row and let it fill the row, but don't
18672 consume the TAB. */
18673 it->continuation_lines_width += it->last_visible_x;
18674 row->ends_in_middle_of_char_p = 1;
18675 row->continued_p = 1;
18676 glyph->pixel_width = it->last_visible_x - x;
18677 it->starts_in_middle_of_char_p = 1;
18679 else
18681 /* Something other than a TAB that draws past
18682 the right edge of the window. Restore
18683 positions to values before the element. */
18684 if (row->reversed_p)
18685 unproduce_glyphs (it, row->used[TEXT_AREA]
18686 - (n_glyphs_before + i));
18687 row->used[TEXT_AREA] = n_glyphs_before + i;
18689 /* Display continuation glyphs. */
18690 if (!FRAME_WINDOW_P (it->f))
18691 produce_special_glyphs (it, IT_CONTINUATION);
18692 row->continued_p = 1;
18694 it->current_x = x_before;
18695 it->continuation_lines_width += x;
18696 extend_face_to_end_of_line (it);
18698 if (nglyphs > 1 && i > 0)
18700 row->ends_in_middle_of_char_p = 1;
18701 it->starts_in_middle_of_char_p = 1;
18704 /* Restore the height to what it was before the
18705 element not fitting on the line. */
18706 it->max_ascent = ascent;
18707 it->max_descent = descent;
18708 it->max_phys_ascent = phys_ascent;
18709 it->max_phys_descent = phys_descent;
18712 break;
18714 else if (new_x > it->first_visible_x)
18716 /* Increment number of glyphs actually displayed. */
18717 ++it->hpos;
18719 /* Record the maximum and minimum buffer positions
18720 seen so far in glyphs that will be displayed by
18721 this row. */
18722 if (it->bidi_p)
18723 RECORD_MAX_MIN_POS (it);
18725 if (x < it->first_visible_x)
18726 /* Glyph is partially visible, i.e. row starts at
18727 negative X position. */
18728 row->x = x - it->first_visible_x;
18730 else
18732 /* Glyph is completely off the left margin of the
18733 window. This should not happen because of the
18734 move_it_in_display_line at the start of this
18735 function, unless the text display area of the
18736 window is empty. */
18737 xassert (it->first_visible_x <= it->last_visible_x);
18741 row->ascent = max (row->ascent, it->max_ascent);
18742 row->height = max (row->height, it->max_ascent + it->max_descent);
18743 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18744 row->phys_height = max (row->phys_height,
18745 it->max_phys_ascent + it->max_phys_descent);
18746 row->extra_line_spacing = max (row->extra_line_spacing,
18747 it->max_extra_line_spacing);
18749 /* End of this display line if row is continued. */
18750 if (row->continued_p || row->ends_at_zv_p)
18751 break;
18754 at_end_of_line:
18755 /* Is this a line end? If yes, we're also done, after making
18756 sure that a non-default face is extended up to the right
18757 margin of the window. */
18758 if (ITERATOR_AT_END_OF_LINE_P (it))
18760 int used_before = row->used[TEXT_AREA];
18762 row->ends_in_newline_from_string_p = STRINGP (it->object);
18764 /* Add a space at the end of the line that is used to
18765 display the cursor there. */
18766 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18767 append_space_for_newline (it, 0);
18769 /* Extend the face to the end of the line. */
18770 extend_face_to_end_of_line (it);
18772 /* Make sure we have the position. */
18773 if (used_before == 0)
18774 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
18776 /* Record the position of the newline, for use in
18777 find_row_edges. */
18778 it->eol_pos = it->current.pos;
18780 /* Consume the line end. This skips over invisible lines. */
18781 set_iterator_to_next (it, 1);
18782 it->continuation_lines_width = 0;
18783 break;
18786 /* Proceed with next display element. Note that this skips
18787 over lines invisible because of selective display. */
18788 set_iterator_to_next (it, 1);
18790 /* If we truncate lines, we are done when the last displayed
18791 glyphs reach past the right margin of the window. */
18792 if (it->line_wrap == TRUNCATE
18793 && (FRAME_WINDOW_P (it->f)
18794 ? (it->current_x >= it->last_visible_x)
18795 : (it->current_x > it->last_visible_x)))
18797 /* Maybe add truncation glyphs. */
18798 if (!FRAME_WINDOW_P (it->f))
18800 int i, n;
18802 if (!row->reversed_p)
18804 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18805 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18806 break;
18808 else
18810 for (i = 0; i < row->used[TEXT_AREA]; i++)
18811 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18812 break;
18813 /* Remove any padding glyphs at the front of ROW, to
18814 make room for the truncation glyphs we will be
18815 adding below. The loop below always inserts at
18816 least one truncation glyph, so also remove the
18817 last glyph added to ROW. */
18818 unproduce_glyphs (it, i + 1);
18819 /* Adjust i for the loop below. */
18820 i = row->used[TEXT_AREA] - (i + 1);
18823 for (n = row->used[TEXT_AREA]; i < n; ++i)
18825 row->used[TEXT_AREA] = i;
18826 produce_special_glyphs (it, IT_TRUNCATION);
18829 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18831 /* Don't truncate if we can overflow newline into fringe. */
18832 if (!get_next_display_element (it))
18834 it->continuation_lines_width = 0;
18835 row->ends_at_zv_p = 1;
18836 row->exact_window_width_line_p = 1;
18837 break;
18839 if (ITERATOR_AT_END_OF_LINE_P (it))
18841 row->exact_window_width_line_p = 1;
18842 goto at_end_of_line;
18846 row->truncated_on_right_p = 1;
18847 it->continuation_lines_width = 0;
18848 reseat_at_next_visible_line_start (it, 0);
18849 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
18850 it->hpos = hpos_before;
18851 it->current_x = x_before;
18852 break;
18856 if (wrap_data)
18857 bidi_unshelve_cache (wrap_data, 1);
18859 /* If line is not empty and hscrolled, maybe insert truncation glyphs
18860 at the left window margin. */
18861 if (it->first_visible_x
18862 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
18864 if (!FRAME_WINDOW_P (it->f))
18865 insert_left_trunc_glyphs (it);
18866 row->truncated_on_left_p = 1;
18869 /* Remember the position at which this line ends.
18871 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
18872 cannot be before the call to find_row_edges below, since that is
18873 where these positions are determined. */
18874 row->end = it->current;
18875 if (!it->bidi_p)
18877 row->minpos = row->start.pos;
18878 row->maxpos = row->end.pos;
18880 else
18882 /* ROW->minpos and ROW->maxpos must be the smallest and
18883 `1 + the largest' buffer positions in ROW. But if ROW was
18884 bidi-reordered, these two positions can be anywhere in the
18885 row, so we must determine them now. */
18886 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
18889 /* If the start of this line is the overlay arrow-position, then
18890 mark this glyph row as the one containing the overlay arrow.
18891 This is clearly a mess with variable size fonts. It would be
18892 better to let it be displayed like cursors under X. */
18893 if ((row->displays_text_p || !overlay_arrow_seen)
18894 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
18895 !NILP (overlay_arrow_string)))
18897 /* Overlay arrow in window redisplay is a fringe bitmap. */
18898 if (STRINGP (overlay_arrow_string))
18900 struct glyph_row *arrow_row
18901 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
18902 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
18903 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
18904 struct glyph *p = row->glyphs[TEXT_AREA];
18905 struct glyph *p2, *end;
18907 /* Copy the arrow glyphs. */
18908 while (glyph < arrow_end)
18909 *p++ = *glyph++;
18911 /* Throw away padding glyphs. */
18912 p2 = p;
18913 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18914 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
18915 ++p2;
18916 if (p2 > p)
18918 while (p2 < end)
18919 *p++ = *p2++;
18920 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
18923 else
18925 xassert (INTEGERP (overlay_arrow_string));
18926 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
18928 overlay_arrow_seen = 1;
18931 /* Compute pixel dimensions of this line. */
18932 compute_line_metrics (it);
18934 /* Record whether this row ends inside an ellipsis. */
18935 row->ends_in_ellipsis_p
18936 = (it->method == GET_FROM_DISPLAY_VECTOR
18937 && it->ellipsis_p);
18939 /* Save fringe bitmaps in this row. */
18940 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
18941 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
18942 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
18943 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
18945 it->left_user_fringe_bitmap = 0;
18946 it->left_user_fringe_face_id = 0;
18947 it->right_user_fringe_bitmap = 0;
18948 it->right_user_fringe_face_id = 0;
18950 /* Maybe set the cursor. */
18951 cvpos = it->w->cursor.vpos;
18952 if ((cvpos < 0
18953 /* In bidi-reordered rows, keep checking for proper cursor
18954 position even if one has been found already, because buffer
18955 positions in such rows change non-linearly with ROW->VPOS,
18956 when a line is continued. One exception: when we are at ZV,
18957 display cursor on the first suitable glyph row, since all
18958 the empty rows after that also have their position set to ZV. */
18959 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18960 lines' rows is implemented for bidi-reordered rows. */
18961 || (it->bidi_p
18962 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
18963 && PT >= MATRIX_ROW_START_CHARPOS (row)
18964 && PT <= MATRIX_ROW_END_CHARPOS (row)
18965 && cursor_row_p (row))
18966 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
18968 /* Highlight trailing whitespace. */
18969 if (!NILP (Vshow_trailing_whitespace))
18970 highlight_trailing_whitespace (it->f, it->glyph_row);
18972 /* Prepare for the next line. This line starts horizontally at (X
18973 HPOS) = (0 0). Vertical positions are incremented. As a
18974 convenience for the caller, IT->glyph_row is set to the next
18975 row to be used. */
18976 it->current_x = it->hpos = 0;
18977 it->current_y += row->height;
18978 SET_TEXT_POS (it->eol_pos, 0, 0);
18979 ++it->vpos;
18980 ++it->glyph_row;
18981 /* The next row should by default use the same value of the
18982 reversed_p flag as this one. set_iterator_to_next decides when
18983 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
18984 the flag accordingly. */
18985 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
18986 it->glyph_row->reversed_p = row->reversed_p;
18987 it->start = row->end;
18988 return row->displays_text_p;
18990 #undef RECORD_MAX_MIN_POS
18993 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
18994 Scurrent_bidi_paragraph_direction, 0, 1, 0,
18995 doc: /* Return paragraph direction at point in BUFFER.
18996 Value is either `left-to-right' or `right-to-left'.
18997 If BUFFER is omitted or nil, it defaults to the current buffer.
18999 Paragraph direction determines how the text in the paragraph is displayed.
19000 In left-to-right paragraphs, text begins at the left margin of the window
19001 and the reading direction is generally left to right. In right-to-left
19002 paragraphs, text begins at the right margin and is read from right to left.
19004 See also `bidi-paragraph-direction'. */)
19005 (Lisp_Object buffer)
19007 struct buffer *buf = current_buffer;
19008 struct buffer *old = buf;
19010 if (! NILP (buffer))
19012 CHECK_BUFFER (buffer);
19013 buf = XBUFFER (buffer);
19016 if (NILP (BVAR (buf, bidi_display_reordering)))
19017 return Qleft_to_right;
19018 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19019 return BVAR (buf, bidi_paragraph_direction);
19020 else
19022 /* Determine the direction from buffer text. We could try to
19023 use current_matrix if it is up to date, but this seems fast
19024 enough as it is. */
19025 struct bidi_it itb;
19026 EMACS_INT pos = BUF_PT (buf);
19027 EMACS_INT bytepos = BUF_PT_BYTE (buf);
19028 int c;
19030 set_buffer_temp (buf);
19031 /* bidi_paragraph_init finds the base direction of the paragraph
19032 by searching forward from paragraph start. We need the base
19033 direction of the current or _previous_ paragraph, so we need
19034 to make sure we are within that paragraph. To that end, find
19035 the previous non-empty line. */
19036 if (pos >= ZV && pos > BEGV)
19038 pos--;
19039 bytepos = CHAR_TO_BYTE (pos);
19041 while ((c = FETCH_BYTE (bytepos)) == '\n'
19042 || c == ' ' || c == '\t' || c == '\f')
19044 if (bytepos <= BEGV_BYTE)
19045 break;
19046 bytepos--;
19047 pos--;
19049 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19050 bytepos--;
19051 itb.charpos = pos;
19052 itb.bytepos = bytepos;
19053 itb.nchars = -1;
19054 itb.string.s = NULL;
19055 itb.string.lstring = Qnil;
19056 itb.frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ()); /* guesswork */
19057 itb.first_elt = 1;
19058 itb.separator_limit = -1;
19059 itb.paragraph_dir = NEUTRAL_DIR;
19061 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19062 set_buffer_temp (old);
19063 switch (itb.paragraph_dir)
19065 case L2R:
19066 return Qleft_to_right;
19067 break;
19068 case R2L:
19069 return Qright_to_left;
19070 break;
19071 default:
19072 abort ();
19079 /***********************************************************************
19080 Menu Bar
19081 ***********************************************************************/
19083 /* Redisplay the menu bar in the frame for window W.
19085 The menu bar of X frames that don't have X toolkit support is
19086 displayed in a special window W->frame->menu_bar_window.
19088 The menu bar of terminal frames is treated specially as far as
19089 glyph matrices are concerned. Menu bar lines are not part of
19090 windows, so the update is done directly on the frame matrix rows
19091 for the menu bar. */
19093 static void
19094 display_menu_bar (struct window *w)
19096 struct frame *f = XFRAME (WINDOW_FRAME (w));
19097 struct it it;
19098 Lisp_Object items;
19099 int i;
19101 /* Don't do all this for graphical frames. */
19102 #ifdef HAVE_NTGUI
19103 if (FRAME_W32_P (f))
19104 return;
19105 #endif
19106 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
19107 if (FRAME_X_P (f))
19108 return;
19109 #endif
19111 #ifdef HAVE_NS
19112 if (FRAME_NS_P (f))
19113 return;
19114 #endif /* HAVE_NS */
19116 #ifdef USE_X_TOOLKIT
19117 xassert (!FRAME_WINDOW_P (f));
19118 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
19119 it.first_visible_x = 0;
19120 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19121 #else /* not USE_X_TOOLKIT */
19122 if (FRAME_WINDOW_P (f))
19124 /* Menu bar lines are displayed in the desired matrix of the
19125 dummy window menu_bar_window. */
19126 struct window *menu_w;
19127 xassert (WINDOWP (f->menu_bar_window));
19128 menu_w = XWINDOW (f->menu_bar_window);
19129 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
19130 MENU_FACE_ID);
19131 it.first_visible_x = 0;
19132 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19134 else
19136 /* This is a TTY frame, i.e. character hpos/vpos are used as
19137 pixel x/y. */
19138 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
19139 MENU_FACE_ID);
19140 it.first_visible_x = 0;
19141 it.last_visible_x = FRAME_COLS (f);
19143 #endif /* not USE_X_TOOLKIT */
19145 /* FIXME: This should be controlled by a user option. See the
19146 comments in redisplay_tool_bar and display_mode_line about
19147 this. */
19148 it.paragraph_embedding = L2R;
19150 if (! mode_line_inverse_video)
19151 /* Force the menu-bar to be displayed in the default face. */
19152 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19154 /* Clear all rows of the menu bar. */
19155 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
19157 struct glyph_row *row = it.glyph_row + i;
19158 clear_glyph_row (row);
19159 row->enabled_p = 1;
19160 row->full_width_p = 1;
19163 /* Display all items of the menu bar. */
19164 items = FRAME_MENU_BAR_ITEMS (it.f);
19165 for (i = 0; i < ASIZE (items); i += 4)
19167 Lisp_Object string;
19169 /* Stop at nil string. */
19170 string = AREF (items, i + 1);
19171 if (NILP (string))
19172 break;
19174 /* Remember where item was displayed. */
19175 ASET (items, i + 3, make_number (it.hpos));
19177 /* Display the item, pad with one space. */
19178 if (it.current_x < it.last_visible_x)
19179 display_string (NULL, string, Qnil, 0, 0, &it,
19180 SCHARS (string) + 1, 0, 0, -1);
19183 /* Fill out the line with spaces. */
19184 if (it.current_x < it.last_visible_x)
19185 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
19187 /* Compute the total height of the lines. */
19188 compute_line_metrics (&it);
19193 /***********************************************************************
19194 Mode Line
19195 ***********************************************************************/
19197 /* Redisplay mode lines in the window tree whose root is WINDOW. If
19198 FORCE is non-zero, redisplay mode lines unconditionally.
19199 Otherwise, redisplay only mode lines that are garbaged. Value is
19200 the number of windows whose mode lines were redisplayed. */
19202 static int
19203 redisplay_mode_lines (Lisp_Object window, int force)
19205 int nwindows = 0;
19207 while (!NILP (window))
19209 struct window *w = XWINDOW (window);
19211 if (WINDOWP (w->hchild))
19212 nwindows += redisplay_mode_lines (w->hchild, force);
19213 else if (WINDOWP (w->vchild))
19214 nwindows += redisplay_mode_lines (w->vchild, force);
19215 else if (force
19216 || FRAME_GARBAGED_P (XFRAME (w->frame))
19217 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
19219 struct text_pos lpoint;
19220 struct buffer *old = current_buffer;
19222 /* Set the window's buffer for the mode line display. */
19223 SET_TEXT_POS (lpoint, PT, PT_BYTE);
19224 set_buffer_internal_1 (XBUFFER (w->buffer));
19226 /* Point refers normally to the selected window. For any
19227 other window, set up appropriate value. */
19228 if (!EQ (window, selected_window))
19230 struct text_pos pt;
19232 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
19233 if (CHARPOS (pt) < BEGV)
19234 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
19235 else if (CHARPOS (pt) > (ZV - 1))
19236 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
19237 else
19238 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
19241 /* Display mode lines. */
19242 clear_glyph_matrix (w->desired_matrix);
19243 if (display_mode_lines (w))
19245 ++nwindows;
19246 w->must_be_updated_p = 1;
19249 /* Restore old settings. */
19250 set_buffer_internal_1 (old);
19251 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
19254 window = w->next;
19257 return nwindows;
19261 /* Display the mode and/or header line of window W. Value is the
19262 sum number of mode lines and header lines displayed. */
19264 static int
19265 display_mode_lines (struct window *w)
19267 Lisp_Object old_selected_window, old_selected_frame;
19268 int n = 0;
19270 old_selected_frame = selected_frame;
19271 selected_frame = w->frame;
19272 old_selected_window = selected_window;
19273 XSETWINDOW (selected_window, w);
19275 /* These will be set while the mode line specs are processed. */
19276 line_number_displayed = 0;
19277 w->column_number_displayed = Qnil;
19279 if (WINDOW_WANTS_MODELINE_P (w))
19281 struct window *sel_w = XWINDOW (old_selected_window);
19283 /* Select mode line face based on the real selected window. */
19284 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
19285 BVAR (current_buffer, mode_line_format));
19286 ++n;
19289 if (WINDOW_WANTS_HEADER_LINE_P (w))
19291 display_mode_line (w, HEADER_LINE_FACE_ID,
19292 BVAR (current_buffer, header_line_format));
19293 ++n;
19296 selected_frame = old_selected_frame;
19297 selected_window = old_selected_window;
19298 return n;
19302 /* Display mode or header line of window W. FACE_ID specifies which
19303 line to display; it is either MODE_LINE_FACE_ID or
19304 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
19305 display. Value is the pixel height of the mode/header line
19306 displayed. */
19308 static int
19309 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
19311 struct it it;
19312 struct face *face;
19313 int count = SPECPDL_INDEX ();
19315 init_iterator (&it, w, -1, -1, NULL, face_id);
19316 /* Don't extend on a previously drawn mode-line.
19317 This may happen if called from pos_visible_p. */
19318 it.glyph_row->enabled_p = 0;
19319 prepare_desired_row (it.glyph_row);
19321 it.glyph_row->mode_line_p = 1;
19323 if (! mode_line_inverse_video)
19324 /* Force the mode-line to be displayed in the default face. */
19325 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19327 /* FIXME: This should be controlled by a user option. But
19328 supporting such an option is not trivial, since the mode line is
19329 made up of many separate strings. */
19330 it.paragraph_embedding = L2R;
19332 record_unwind_protect (unwind_format_mode_line,
19333 format_mode_line_unwind_data (NULL, Qnil, 0));
19335 mode_line_target = MODE_LINE_DISPLAY;
19337 /* Temporarily make frame's keyboard the current kboard so that
19338 kboard-local variables in the mode_line_format will get the right
19339 values. */
19340 push_kboard (FRAME_KBOARD (it.f));
19341 record_unwind_save_match_data ();
19342 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19343 pop_kboard ();
19345 unbind_to (count, Qnil);
19347 /* Fill up with spaces. */
19348 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
19350 compute_line_metrics (&it);
19351 it.glyph_row->full_width_p = 1;
19352 it.glyph_row->continued_p = 0;
19353 it.glyph_row->truncated_on_left_p = 0;
19354 it.glyph_row->truncated_on_right_p = 0;
19356 /* Make a 3D mode-line have a shadow at its right end. */
19357 face = FACE_FROM_ID (it.f, face_id);
19358 extend_face_to_end_of_line (&it);
19359 if (face->box != FACE_NO_BOX)
19361 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
19362 + it.glyph_row->used[TEXT_AREA] - 1);
19363 last->right_box_line_p = 1;
19366 return it.glyph_row->height;
19369 /* Move element ELT in LIST to the front of LIST.
19370 Return the updated list. */
19372 static Lisp_Object
19373 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
19375 register Lisp_Object tail, prev;
19376 register Lisp_Object tem;
19378 tail = list;
19379 prev = Qnil;
19380 while (CONSP (tail))
19382 tem = XCAR (tail);
19384 if (EQ (elt, tem))
19386 /* Splice out the link TAIL. */
19387 if (NILP (prev))
19388 list = XCDR (tail);
19389 else
19390 Fsetcdr (prev, XCDR (tail));
19392 /* Now make it the first. */
19393 Fsetcdr (tail, list);
19394 return tail;
19396 else
19397 prev = tail;
19398 tail = XCDR (tail);
19399 QUIT;
19402 /* Not found--return unchanged LIST. */
19403 return list;
19406 /* Contribute ELT to the mode line for window IT->w. How it
19407 translates into text depends on its data type.
19409 IT describes the display environment in which we display, as usual.
19411 DEPTH is the depth in recursion. It is used to prevent
19412 infinite recursion here.
19414 FIELD_WIDTH is the number of characters the display of ELT should
19415 occupy in the mode line, and PRECISION is the maximum number of
19416 characters to display from ELT's representation. See
19417 display_string for details.
19419 Returns the hpos of the end of the text generated by ELT.
19421 PROPS is a property list to add to any string we encounter.
19423 If RISKY is nonzero, remove (disregard) any properties in any string
19424 we encounter, and ignore :eval and :propertize.
19426 The global variable `mode_line_target' determines whether the
19427 output is passed to `store_mode_line_noprop',
19428 `store_mode_line_string', or `display_string'. */
19430 static int
19431 display_mode_element (struct it *it, int depth, int field_width, int precision,
19432 Lisp_Object elt, Lisp_Object props, int risky)
19434 int n = 0, field, prec;
19435 int literal = 0;
19437 tail_recurse:
19438 if (depth > 100)
19439 elt = build_string ("*too-deep*");
19441 depth++;
19443 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
19445 case Lisp_String:
19447 /* A string: output it and check for %-constructs within it. */
19448 unsigned char c;
19449 EMACS_INT offset = 0;
19451 if (SCHARS (elt) > 0
19452 && (!NILP (props) || risky))
19454 Lisp_Object oprops, aelt;
19455 oprops = Ftext_properties_at (make_number (0), elt);
19457 /* If the starting string's properties are not what
19458 we want, translate the string. Also, if the string
19459 is risky, do that anyway. */
19461 if (NILP (Fequal (props, oprops)) || risky)
19463 /* If the starting string has properties,
19464 merge the specified ones onto the existing ones. */
19465 if (! NILP (oprops) && !risky)
19467 Lisp_Object tem;
19469 oprops = Fcopy_sequence (oprops);
19470 tem = props;
19471 while (CONSP (tem))
19473 oprops = Fplist_put (oprops, XCAR (tem),
19474 XCAR (XCDR (tem)));
19475 tem = XCDR (XCDR (tem));
19477 props = oprops;
19480 aelt = Fassoc (elt, mode_line_proptrans_alist);
19481 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
19483 /* AELT is what we want. Move it to the front
19484 without consing. */
19485 elt = XCAR (aelt);
19486 mode_line_proptrans_alist
19487 = move_elt_to_front (aelt, mode_line_proptrans_alist);
19489 else
19491 Lisp_Object tem;
19493 /* If AELT has the wrong props, it is useless.
19494 so get rid of it. */
19495 if (! NILP (aelt))
19496 mode_line_proptrans_alist
19497 = Fdelq (aelt, mode_line_proptrans_alist);
19499 elt = Fcopy_sequence (elt);
19500 Fset_text_properties (make_number (0), Flength (elt),
19501 props, elt);
19502 /* Add this item to mode_line_proptrans_alist. */
19503 mode_line_proptrans_alist
19504 = Fcons (Fcons (elt, props),
19505 mode_line_proptrans_alist);
19506 /* Truncate mode_line_proptrans_alist
19507 to at most 50 elements. */
19508 tem = Fnthcdr (make_number (50),
19509 mode_line_proptrans_alist);
19510 if (! NILP (tem))
19511 XSETCDR (tem, Qnil);
19516 offset = 0;
19518 if (literal)
19520 prec = precision - n;
19521 switch (mode_line_target)
19523 case MODE_LINE_NOPROP:
19524 case MODE_LINE_TITLE:
19525 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
19526 break;
19527 case MODE_LINE_STRING:
19528 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
19529 break;
19530 case MODE_LINE_DISPLAY:
19531 n += display_string (NULL, elt, Qnil, 0, 0, it,
19532 0, prec, 0, STRING_MULTIBYTE (elt));
19533 break;
19536 break;
19539 /* Handle the non-literal case. */
19541 while ((precision <= 0 || n < precision)
19542 && SREF (elt, offset) != 0
19543 && (mode_line_target != MODE_LINE_DISPLAY
19544 || it->current_x < it->last_visible_x))
19546 EMACS_INT last_offset = offset;
19548 /* Advance to end of string or next format specifier. */
19549 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
19552 if (offset - 1 != last_offset)
19554 EMACS_INT nchars, nbytes;
19556 /* Output to end of string or up to '%'. Field width
19557 is length of string. Don't output more than
19558 PRECISION allows us. */
19559 offset--;
19561 prec = c_string_width (SDATA (elt) + last_offset,
19562 offset - last_offset, precision - n,
19563 &nchars, &nbytes);
19565 switch (mode_line_target)
19567 case MODE_LINE_NOPROP:
19568 case MODE_LINE_TITLE:
19569 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
19570 break;
19571 case MODE_LINE_STRING:
19573 EMACS_INT bytepos = last_offset;
19574 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
19575 EMACS_INT endpos = (precision <= 0
19576 ? string_byte_to_char (elt, offset)
19577 : charpos + nchars);
19579 n += store_mode_line_string (NULL,
19580 Fsubstring (elt, make_number (charpos),
19581 make_number (endpos)),
19582 0, 0, 0, Qnil);
19584 break;
19585 case MODE_LINE_DISPLAY:
19587 EMACS_INT bytepos = last_offset;
19588 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
19590 if (precision <= 0)
19591 nchars = string_byte_to_char (elt, offset) - charpos;
19592 n += display_string (NULL, elt, Qnil, 0, charpos,
19593 it, 0, nchars, 0,
19594 STRING_MULTIBYTE (elt));
19596 break;
19599 else /* c == '%' */
19601 EMACS_INT percent_position = offset;
19603 /* Get the specified minimum width. Zero means
19604 don't pad. */
19605 field = 0;
19606 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
19607 field = field * 10 + c - '0';
19609 /* Don't pad beyond the total padding allowed. */
19610 if (field_width - n > 0 && field > field_width - n)
19611 field = field_width - n;
19613 /* Note that either PRECISION <= 0 or N < PRECISION. */
19614 prec = precision - n;
19616 if (c == 'M')
19617 n += display_mode_element (it, depth, field, prec,
19618 Vglobal_mode_string, props,
19619 risky);
19620 else if (c != 0)
19622 int multibyte;
19623 EMACS_INT bytepos, charpos;
19624 const char *spec;
19625 Lisp_Object string;
19627 bytepos = percent_position;
19628 charpos = (STRING_MULTIBYTE (elt)
19629 ? string_byte_to_char (elt, bytepos)
19630 : bytepos);
19631 spec = decode_mode_spec (it->w, c, field, &string);
19632 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
19634 switch (mode_line_target)
19636 case MODE_LINE_NOPROP:
19637 case MODE_LINE_TITLE:
19638 n += store_mode_line_noprop (spec, field, prec);
19639 break;
19640 case MODE_LINE_STRING:
19642 Lisp_Object tem = build_string (spec);
19643 props = Ftext_properties_at (make_number (charpos), elt);
19644 /* Should only keep face property in props */
19645 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
19647 break;
19648 case MODE_LINE_DISPLAY:
19650 int nglyphs_before, nwritten;
19652 nglyphs_before = it->glyph_row->used[TEXT_AREA];
19653 nwritten = display_string (spec, string, elt,
19654 charpos, 0, it,
19655 field, prec, 0,
19656 multibyte);
19658 /* Assign to the glyphs written above the
19659 string where the `%x' came from, position
19660 of the `%'. */
19661 if (nwritten > 0)
19663 struct glyph *glyph
19664 = (it->glyph_row->glyphs[TEXT_AREA]
19665 + nglyphs_before);
19666 int i;
19668 for (i = 0; i < nwritten; ++i)
19670 glyph[i].object = elt;
19671 glyph[i].charpos = charpos;
19674 n += nwritten;
19677 break;
19680 else /* c == 0 */
19681 break;
19685 break;
19687 case Lisp_Symbol:
19688 /* A symbol: process the value of the symbol recursively
19689 as if it appeared here directly. Avoid error if symbol void.
19690 Special case: if value of symbol is a string, output the string
19691 literally. */
19693 register Lisp_Object tem;
19695 /* If the variable is not marked as risky to set
19696 then its contents are risky to use. */
19697 if (NILP (Fget (elt, Qrisky_local_variable)))
19698 risky = 1;
19700 tem = Fboundp (elt);
19701 if (!NILP (tem))
19703 tem = Fsymbol_value (elt);
19704 /* If value is a string, output that string literally:
19705 don't check for % within it. */
19706 if (STRINGP (tem))
19707 literal = 1;
19709 if (!EQ (tem, elt))
19711 /* Give up right away for nil or t. */
19712 elt = tem;
19713 goto tail_recurse;
19717 break;
19719 case Lisp_Cons:
19721 register Lisp_Object car, tem;
19723 /* A cons cell: five distinct cases.
19724 If first element is :eval or :propertize, do something special.
19725 If first element is a string or a cons, process all the elements
19726 and effectively concatenate them.
19727 If first element is a negative number, truncate displaying cdr to
19728 at most that many characters. If positive, pad (with spaces)
19729 to at least that many characters.
19730 If first element is a symbol, process the cadr or caddr recursively
19731 according to whether the symbol's value is non-nil or nil. */
19732 car = XCAR (elt);
19733 if (EQ (car, QCeval))
19735 /* An element of the form (:eval FORM) means evaluate FORM
19736 and use the result as mode line elements. */
19738 if (risky)
19739 break;
19741 if (CONSP (XCDR (elt)))
19743 Lisp_Object spec;
19744 spec = safe_eval (XCAR (XCDR (elt)));
19745 n += display_mode_element (it, depth, field_width - n,
19746 precision - n, spec, props,
19747 risky);
19750 else if (EQ (car, QCpropertize))
19752 /* An element of the form (:propertize ELT PROPS...)
19753 means display ELT but applying properties PROPS. */
19755 if (risky)
19756 break;
19758 if (CONSP (XCDR (elt)))
19759 n += display_mode_element (it, depth, field_width - n,
19760 precision - n, XCAR (XCDR (elt)),
19761 XCDR (XCDR (elt)), risky);
19763 else if (SYMBOLP (car))
19765 tem = Fboundp (car);
19766 elt = XCDR (elt);
19767 if (!CONSP (elt))
19768 goto invalid;
19769 /* elt is now the cdr, and we know it is a cons cell.
19770 Use its car if CAR has a non-nil value. */
19771 if (!NILP (tem))
19773 tem = Fsymbol_value (car);
19774 if (!NILP (tem))
19776 elt = XCAR (elt);
19777 goto tail_recurse;
19780 /* Symbol's value is nil (or symbol is unbound)
19781 Get the cddr of the original list
19782 and if possible find the caddr and use that. */
19783 elt = XCDR (elt);
19784 if (NILP (elt))
19785 break;
19786 else if (!CONSP (elt))
19787 goto invalid;
19788 elt = XCAR (elt);
19789 goto tail_recurse;
19791 else if (INTEGERP (car))
19793 register int lim = XINT (car);
19794 elt = XCDR (elt);
19795 if (lim < 0)
19797 /* Negative int means reduce maximum width. */
19798 if (precision <= 0)
19799 precision = -lim;
19800 else
19801 precision = min (precision, -lim);
19803 else if (lim > 0)
19805 /* Padding specified. Don't let it be more than
19806 current maximum. */
19807 if (precision > 0)
19808 lim = min (precision, lim);
19810 /* If that's more padding than already wanted, queue it.
19811 But don't reduce padding already specified even if
19812 that is beyond the current truncation point. */
19813 field_width = max (lim, field_width);
19815 goto tail_recurse;
19817 else if (STRINGP (car) || CONSP (car))
19819 Lisp_Object halftail = elt;
19820 int len = 0;
19822 while (CONSP (elt)
19823 && (precision <= 0 || n < precision))
19825 n += display_mode_element (it, depth,
19826 /* Do padding only after the last
19827 element in the list. */
19828 (! CONSP (XCDR (elt))
19829 ? field_width - n
19830 : 0),
19831 precision - n, XCAR (elt),
19832 props, risky);
19833 elt = XCDR (elt);
19834 len++;
19835 if ((len & 1) == 0)
19836 halftail = XCDR (halftail);
19837 /* Check for cycle. */
19838 if (EQ (halftail, elt))
19839 break;
19843 break;
19845 default:
19846 invalid:
19847 elt = build_string ("*invalid*");
19848 goto tail_recurse;
19851 /* Pad to FIELD_WIDTH. */
19852 if (field_width > 0 && n < field_width)
19854 switch (mode_line_target)
19856 case MODE_LINE_NOPROP:
19857 case MODE_LINE_TITLE:
19858 n += store_mode_line_noprop ("", field_width - n, 0);
19859 break;
19860 case MODE_LINE_STRING:
19861 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
19862 break;
19863 case MODE_LINE_DISPLAY:
19864 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
19865 0, 0, 0);
19866 break;
19870 return n;
19873 /* Store a mode-line string element in mode_line_string_list.
19875 If STRING is non-null, display that C string. Otherwise, the Lisp
19876 string LISP_STRING is displayed.
19878 FIELD_WIDTH is the minimum number of output glyphs to produce.
19879 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19880 with spaces. FIELD_WIDTH <= 0 means don't pad.
19882 PRECISION is the maximum number of characters to output from
19883 STRING. PRECISION <= 0 means don't truncate the string.
19885 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
19886 properties to the string.
19888 PROPS are the properties to add to the string.
19889 The mode_line_string_face face property is always added to the string.
19892 static int
19893 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
19894 int field_width, int precision, Lisp_Object props)
19896 EMACS_INT len;
19897 int n = 0;
19899 if (string != NULL)
19901 len = strlen (string);
19902 if (precision > 0 && len > precision)
19903 len = precision;
19904 lisp_string = make_string (string, len);
19905 if (NILP (props))
19906 props = mode_line_string_face_prop;
19907 else if (!NILP (mode_line_string_face))
19909 Lisp_Object face = Fplist_get (props, Qface);
19910 props = Fcopy_sequence (props);
19911 if (NILP (face))
19912 face = mode_line_string_face;
19913 else
19914 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19915 props = Fplist_put (props, Qface, face);
19917 Fadd_text_properties (make_number (0), make_number (len),
19918 props, lisp_string);
19920 else
19922 len = XFASTINT (Flength (lisp_string));
19923 if (precision > 0 && len > precision)
19925 len = precision;
19926 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
19927 precision = -1;
19929 if (!NILP (mode_line_string_face))
19931 Lisp_Object face;
19932 if (NILP (props))
19933 props = Ftext_properties_at (make_number (0), lisp_string);
19934 face = Fplist_get (props, Qface);
19935 if (NILP (face))
19936 face = mode_line_string_face;
19937 else
19938 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19939 props = Fcons (Qface, Fcons (face, Qnil));
19940 if (copy_string)
19941 lisp_string = Fcopy_sequence (lisp_string);
19943 if (!NILP (props))
19944 Fadd_text_properties (make_number (0), make_number (len),
19945 props, lisp_string);
19948 if (len > 0)
19950 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19951 n += len;
19954 if (field_width > len)
19956 field_width -= len;
19957 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
19958 if (!NILP (props))
19959 Fadd_text_properties (make_number (0), make_number (field_width),
19960 props, lisp_string);
19961 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19962 n += field_width;
19965 return n;
19969 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
19970 1, 4, 0,
19971 doc: /* Format a string out of a mode line format specification.
19972 First arg FORMAT specifies the mode line format (see `mode-line-format'
19973 for details) to use.
19975 By default, the format is evaluated for the currently selected window.
19977 Optional second arg FACE specifies the face property to put on all
19978 characters for which no face is specified. The value nil means the
19979 default face. The value t means whatever face the window's mode line
19980 currently uses (either `mode-line' or `mode-line-inactive',
19981 depending on whether the window is the selected window or not).
19982 An integer value means the value string has no text
19983 properties.
19985 Optional third and fourth args WINDOW and BUFFER specify the window
19986 and buffer to use as the context for the formatting (defaults
19987 are the selected window and the WINDOW's buffer). */)
19988 (Lisp_Object format, Lisp_Object face,
19989 Lisp_Object window, Lisp_Object buffer)
19991 struct it it;
19992 int len;
19993 struct window *w;
19994 struct buffer *old_buffer = NULL;
19995 int face_id;
19996 int no_props = INTEGERP (face);
19997 int count = SPECPDL_INDEX ();
19998 Lisp_Object str;
19999 int string_start = 0;
20001 if (NILP (window))
20002 window = selected_window;
20003 CHECK_WINDOW (window);
20004 w = XWINDOW (window);
20006 if (NILP (buffer))
20007 buffer = w->buffer;
20008 CHECK_BUFFER (buffer);
20010 /* Make formatting the modeline a non-op when noninteractive, otherwise
20011 there will be problems later caused by a partially initialized frame. */
20012 if (NILP (format) || noninteractive)
20013 return empty_unibyte_string;
20015 if (no_props)
20016 face = Qnil;
20018 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20019 : EQ (face, Qt) ? (EQ (window, selected_window)
20020 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20021 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20022 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20023 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20024 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20025 : DEFAULT_FACE_ID;
20027 if (XBUFFER (buffer) != current_buffer)
20028 old_buffer = current_buffer;
20030 /* Save things including mode_line_proptrans_alist,
20031 and set that to nil so that we don't alter the outer value. */
20032 record_unwind_protect (unwind_format_mode_line,
20033 format_mode_line_unwind_data
20034 (old_buffer, selected_window, 1));
20035 mode_line_proptrans_alist = Qnil;
20037 Fselect_window (window, Qt);
20038 if (old_buffer)
20039 set_buffer_internal_1 (XBUFFER (buffer));
20041 init_iterator (&it, w, -1, -1, NULL, face_id);
20043 if (no_props)
20045 mode_line_target = MODE_LINE_NOPROP;
20046 mode_line_string_face_prop = Qnil;
20047 mode_line_string_list = Qnil;
20048 string_start = MODE_LINE_NOPROP_LEN (0);
20050 else
20052 mode_line_target = MODE_LINE_STRING;
20053 mode_line_string_list = Qnil;
20054 mode_line_string_face = face;
20055 mode_line_string_face_prop
20056 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20059 push_kboard (FRAME_KBOARD (it.f));
20060 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20061 pop_kboard ();
20063 if (no_props)
20065 len = MODE_LINE_NOPROP_LEN (string_start);
20066 str = make_string (mode_line_noprop_buf + string_start, len);
20068 else
20070 mode_line_string_list = Fnreverse (mode_line_string_list);
20071 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20072 empty_unibyte_string);
20075 unbind_to (count, Qnil);
20076 return str;
20079 /* Write a null-terminated, right justified decimal representation of
20080 the positive integer D to BUF using a minimal field width WIDTH. */
20082 static void
20083 pint2str (register char *buf, register int width, register EMACS_INT d)
20085 register char *p = buf;
20087 if (d <= 0)
20088 *p++ = '0';
20089 else
20091 while (d > 0)
20093 *p++ = d % 10 + '0';
20094 d /= 10;
20098 for (width -= (int) (p - buf); width > 0; --width)
20099 *p++ = ' ';
20100 *p-- = '\0';
20101 while (p > buf)
20103 d = *buf;
20104 *buf++ = *p;
20105 *p-- = d;
20109 /* Write a null-terminated, right justified decimal and "human
20110 readable" representation of the nonnegative integer D to BUF using
20111 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20113 static const char power_letter[] =
20115 0, /* no letter */
20116 'k', /* kilo */
20117 'M', /* mega */
20118 'G', /* giga */
20119 'T', /* tera */
20120 'P', /* peta */
20121 'E', /* exa */
20122 'Z', /* zetta */
20123 'Y' /* yotta */
20126 static void
20127 pint2hrstr (char *buf, int width, EMACS_INT d)
20129 /* We aim to represent the nonnegative integer D as
20130 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
20131 EMACS_INT quotient = d;
20132 int remainder = 0;
20133 /* -1 means: do not use TENTHS. */
20134 int tenths = -1;
20135 int exponent = 0;
20137 /* Length of QUOTIENT.TENTHS as a string. */
20138 int length;
20140 char * psuffix;
20141 char * p;
20143 if (1000 <= quotient)
20145 /* Scale to the appropriate EXPONENT. */
20148 remainder = quotient % 1000;
20149 quotient /= 1000;
20150 exponent++;
20152 while (1000 <= quotient);
20154 /* Round to nearest and decide whether to use TENTHS or not. */
20155 if (quotient <= 9)
20157 tenths = remainder / 100;
20158 if (50 <= remainder % 100)
20160 if (tenths < 9)
20161 tenths++;
20162 else
20164 quotient++;
20165 if (quotient == 10)
20166 tenths = -1;
20167 else
20168 tenths = 0;
20172 else
20173 if (500 <= remainder)
20175 if (quotient < 999)
20176 quotient++;
20177 else
20179 quotient = 1;
20180 exponent++;
20181 tenths = 0;
20186 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
20187 if (tenths == -1 && quotient <= 99)
20188 if (quotient <= 9)
20189 length = 1;
20190 else
20191 length = 2;
20192 else
20193 length = 3;
20194 p = psuffix = buf + max (width, length);
20196 /* Print EXPONENT. */
20197 *psuffix++ = power_letter[exponent];
20198 *psuffix = '\0';
20200 /* Print TENTHS. */
20201 if (tenths >= 0)
20203 *--p = '0' + tenths;
20204 *--p = '.';
20207 /* Print QUOTIENT. */
20210 int digit = quotient % 10;
20211 *--p = '0' + digit;
20213 while ((quotient /= 10) != 0);
20215 /* Print leading spaces. */
20216 while (buf < p)
20217 *--p = ' ';
20220 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
20221 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
20222 type of CODING_SYSTEM. Return updated pointer into BUF. */
20224 static unsigned char invalid_eol_type[] = "(*invalid*)";
20226 static char *
20227 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
20229 Lisp_Object val;
20230 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
20231 const unsigned char *eol_str;
20232 int eol_str_len;
20233 /* The EOL conversion we are using. */
20234 Lisp_Object eoltype;
20236 val = CODING_SYSTEM_SPEC (coding_system);
20237 eoltype = Qnil;
20239 if (!VECTORP (val)) /* Not yet decided. */
20241 if (multibyte)
20242 *buf++ = '-';
20243 if (eol_flag)
20244 eoltype = eol_mnemonic_undecided;
20245 /* Don't mention EOL conversion if it isn't decided. */
20247 else
20249 Lisp_Object attrs;
20250 Lisp_Object eolvalue;
20252 attrs = AREF (val, 0);
20253 eolvalue = AREF (val, 2);
20255 if (multibyte)
20256 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
20258 if (eol_flag)
20260 /* The EOL conversion that is normal on this system. */
20262 if (NILP (eolvalue)) /* Not yet decided. */
20263 eoltype = eol_mnemonic_undecided;
20264 else if (VECTORP (eolvalue)) /* Not yet decided. */
20265 eoltype = eol_mnemonic_undecided;
20266 else /* eolvalue is Qunix, Qdos, or Qmac. */
20267 eoltype = (EQ (eolvalue, Qunix)
20268 ? eol_mnemonic_unix
20269 : (EQ (eolvalue, Qdos) == 1
20270 ? eol_mnemonic_dos : eol_mnemonic_mac));
20274 if (eol_flag)
20276 /* Mention the EOL conversion if it is not the usual one. */
20277 if (STRINGP (eoltype))
20279 eol_str = SDATA (eoltype);
20280 eol_str_len = SBYTES (eoltype);
20282 else if (CHARACTERP (eoltype))
20284 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
20285 int c = XFASTINT (eoltype);
20286 eol_str_len = CHAR_STRING (c, tmp);
20287 eol_str = tmp;
20289 else
20291 eol_str = invalid_eol_type;
20292 eol_str_len = sizeof (invalid_eol_type) - 1;
20294 memcpy (buf, eol_str, eol_str_len);
20295 buf += eol_str_len;
20298 return buf;
20301 /* Return a string for the output of a mode line %-spec for window W,
20302 generated by character C. FIELD_WIDTH > 0 means pad the string
20303 returned with spaces to that value. Return a Lisp string in
20304 *STRING if the resulting string is taken from that Lisp string.
20306 Note we operate on the current buffer for most purposes,
20307 the exception being w->base_line_pos. */
20309 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
20311 static const char *
20312 decode_mode_spec (struct window *w, register int c, int field_width,
20313 Lisp_Object *string)
20315 Lisp_Object obj;
20316 struct frame *f = XFRAME (WINDOW_FRAME (w));
20317 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
20318 struct buffer *b = current_buffer;
20320 obj = Qnil;
20321 *string = Qnil;
20323 switch (c)
20325 case '*':
20326 if (!NILP (BVAR (b, read_only)))
20327 return "%";
20328 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20329 return "*";
20330 return "-";
20332 case '+':
20333 /* This differs from %* only for a modified read-only buffer. */
20334 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20335 return "*";
20336 if (!NILP (BVAR (b, read_only)))
20337 return "%";
20338 return "-";
20340 case '&':
20341 /* This differs from %* in ignoring read-only-ness. */
20342 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20343 return "*";
20344 return "-";
20346 case '%':
20347 return "%";
20349 case '[':
20351 int i;
20352 char *p;
20354 if (command_loop_level > 5)
20355 return "[[[... ";
20356 p = decode_mode_spec_buf;
20357 for (i = 0; i < command_loop_level; i++)
20358 *p++ = '[';
20359 *p = 0;
20360 return decode_mode_spec_buf;
20363 case ']':
20365 int i;
20366 char *p;
20368 if (command_loop_level > 5)
20369 return " ...]]]";
20370 p = decode_mode_spec_buf;
20371 for (i = 0; i < command_loop_level; i++)
20372 *p++ = ']';
20373 *p = 0;
20374 return decode_mode_spec_buf;
20377 case '-':
20379 register int i;
20381 /* Let lots_of_dashes be a string of infinite length. */
20382 if (mode_line_target == MODE_LINE_NOPROP ||
20383 mode_line_target == MODE_LINE_STRING)
20384 return "--";
20385 if (field_width <= 0
20386 || field_width > sizeof (lots_of_dashes))
20388 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
20389 decode_mode_spec_buf[i] = '-';
20390 decode_mode_spec_buf[i] = '\0';
20391 return decode_mode_spec_buf;
20393 else
20394 return lots_of_dashes;
20397 case 'b':
20398 obj = BVAR (b, name);
20399 break;
20401 case 'c':
20402 /* %c and %l are ignored in `frame-title-format'.
20403 (In redisplay_internal, the frame title is drawn _before_ the
20404 windows are updated, so the stuff which depends on actual
20405 window contents (such as %l) may fail to render properly, or
20406 even crash emacs.) */
20407 if (mode_line_target == MODE_LINE_TITLE)
20408 return "";
20409 else
20411 EMACS_INT col = current_column ();
20412 w->column_number_displayed = make_number (col);
20413 pint2str (decode_mode_spec_buf, field_width, col);
20414 return decode_mode_spec_buf;
20417 case 'e':
20418 #ifndef SYSTEM_MALLOC
20420 if (NILP (Vmemory_full))
20421 return "";
20422 else
20423 return "!MEM FULL! ";
20425 #else
20426 return "";
20427 #endif
20429 case 'F':
20430 /* %F displays the frame name. */
20431 if (!NILP (f->title))
20432 return SSDATA (f->title);
20433 if (f->explicit_name || ! FRAME_WINDOW_P (f))
20434 return SSDATA (f->name);
20435 return "Emacs";
20437 case 'f':
20438 obj = BVAR (b, filename);
20439 break;
20441 case 'i':
20443 EMACS_INT size = ZV - BEGV;
20444 pint2str (decode_mode_spec_buf, field_width, size);
20445 return decode_mode_spec_buf;
20448 case 'I':
20450 EMACS_INT size = ZV - BEGV;
20451 pint2hrstr (decode_mode_spec_buf, field_width, size);
20452 return decode_mode_spec_buf;
20455 case 'l':
20457 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
20458 EMACS_INT topline, nlines, height;
20459 EMACS_INT junk;
20461 /* %c and %l are ignored in `frame-title-format'. */
20462 if (mode_line_target == MODE_LINE_TITLE)
20463 return "";
20465 startpos = XMARKER (w->start)->charpos;
20466 startpos_byte = marker_byte_position (w->start);
20467 height = WINDOW_TOTAL_LINES (w);
20469 /* If we decided that this buffer isn't suitable for line numbers,
20470 don't forget that too fast. */
20471 if (EQ (w->base_line_pos, w->buffer))
20472 goto no_value;
20473 /* But do forget it, if the window shows a different buffer now. */
20474 else if (BUFFERP (w->base_line_pos))
20475 w->base_line_pos = Qnil;
20477 /* If the buffer is very big, don't waste time. */
20478 if (INTEGERP (Vline_number_display_limit)
20479 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
20481 w->base_line_pos = Qnil;
20482 w->base_line_number = Qnil;
20483 goto no_value;
20486 if (INTEGERP (w->base_line_number)
20487 && INTEGERP (w->base_line_pos)
20488 && XFASTINT (w->base_line_pos) <= startpos)
20490 line = XFASTINT (w->base_line_number);
20491 linepos = XFASTINT (w->base_line_pos);
20492 linepos_byte = buf_charpos_to_bytepos (b, linepos);
20494 else
20496 line = 1;
20497 linepos = BUF_BEGV (b);
20498 linepos_byte = BUF_BEGV_BYTE (b);
20501 /* Count lines from base line to window start position. */
20502 nlines = display_count_lines (linepos_byte,
20503 startpos_byte,
20504 startpos, &junk);
20506 topline = nlines + line;
20508 /* Determine a new base line, if the old one is too close
20509 or too far away, or if we did not have one.
20510 "Too close" means it's plausible a scroll-down would
20511 go back past it. */
20512 if (startpos == BUF_BEGV (b))
20514 w->base_line_number = make_number (topline);
20515 w->base_line_pos = make_number (BUF_BEGV (b));
20517 else if (nlines < height + 25 || nlines > height * 3 + 50
20518 || linepos == BUF_BEGV (b))
20520 EMACS_INT limit = BUF_BEGV (b);
20521 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
20522 EMACS_INT position;
20523 EMACS_INT distance =
20524 (height * 2 + 30) * line_number_display_limit_width;
20526 if (startpos - distance > limit)
20528 limit = startpos - distance;
20529 limit_byte = CHAR_TO_BYTE (limit);
20532 nlines = display_count_lines (startpos_byte,
20533 limit_byte,
20534 - (height * 2 + 30),
20535 &position);
20536 /* If we couldn't find the lines we wanted within
20537 line_number_display_limit_width chars per line,
20538 give up on line numbers for this window. */
20539 if (position == limit_byte && limit == startpos - distance)
20541 w->base_line_pos = w->buffer;
20542 w->base_line_number = Qnil;
20543 goto no_value;
20546 w->base_line_number = make_number (topline - nlines);
20547 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
20550 /* Now count lines from the start pos to point. */
20551 nlines = display_count_lines (startpos_byte,
20552 PT_BYTE, PT, &junk);
20554 /* Record that we did display the line number. */
20555 line_number_displayed = 1;
20557 /* Make the string to show. */
20558 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
20559 return decode_mode_spec_buf;
20560 no_value:
20562 char* p = decode_mode_spec_buf;
20563 int pad = field_width - 2;
20564 while (pad-- > 0)
20565 *p++ = ' ';
20566 *p++ = '?';
20567 *p++ = '?';
20568 *p = '\0';
20569 return decode_mode_spec_buf;
20572 break;
20574 case 'm':
20575 obj = BVAR (b, mode_name);
20576 break;
20578 case 'n':
20579 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
20580 return " Narrow";
20581 break;
20583 case 'p':
20585 EMACS_INT pos = marker_position (w->start);
20586 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
20588 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
20590 if (pos <= BUF_BEGV (b))
20591 return "All";
20592 else
20593 return "Bottom";
20595 else if (pos <= BUF_BEGV (b))
20596 return "Top";
20597 else
20599 if (total > 1000000)
20600 /* Do it differently for a large value, to avoid overflow. */
20601 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
20602 else
20603 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
20604 /* We can't normally display a 3-digit number,
20605 so get us a 2-digit number that is close. */
20606 if (total == 100)
20607 total = 99;
20608 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
20609 return decode_mode_spec_buf;
20613 /* Display percentage of size above the bottom of the screen. */
20614 case 'P':
20616 EMACS_INT toppos = marker_position (w->start);
20617 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
20618 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
20620 if (botpos >= BUF_ZV (b))
20622 if (toppos <= BUF_BEGV (b))
20623 return "All";
20624 else
20625 return "Bottom";
20627 else
20629 if (total > 1000000)
20630 /* Do it differently for a large value, to avoid overflow. */
20631 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
20632 else
20633 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
20634 /* We can't normally display a 3-digit number,
20635 so get us a 2-digit number that is close. */
20636 if (total == 100)
20637 total = 99;
20638 if (toppos <= BUF_BEGV (b))
20639 sprintf (decode_mode_spec_buf, "Top%2"pI"d%%", total);
20640 else
20641 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
20642 return decode_mode_spec_buf;
20646 case 's':
20647 /* status of process */
20648 obj = Fget_buffer_process (Fcurrent_buffer ());
20649 if (NILP (obj))
20650 return "no process";
20651 #ifndef MSDOS
20652 obj = Fsymbol_name (Fprocess_status (obj));
20653 #endif
20654 break;
20656 case '@':
20658 int count = inhibit_garbage_collection ();
20659 Lisp_Object val = call1 (intern ("file-remote-p"),
20660 BVAR (current_buffer, directory));
20661 unbind_to (count, Qnil);
20663 if (NILP (val))
20664 return "-";
20665 else
20666 return "@";
20669 case 't': /* indicate TEXT or BINARY */
20670 return "T";
20672 case 'z':
20673 /* coding-system (not including end-of-line format) */
20674 case 'Z':
20675 /* coding-system (including end-of-line type) */
20677 int eol_flag = (c == 'Z');
20678 char *p = decode_mode_spec_buf;
20680 if (! FRAME_WINDOW_P (f))
20682 /* No need to mention EOL here--the terminal never needs
20683 to do EOL conversion. */
20684 p = decode_mode_spec_coding (CODING_ID_NAME
20685 (FRAME_KEYBOARD_CODING (f)->id),
20686 p, 0);
20687 p = decode_mode_spec_coding (CODING_ID_NAME
20688 (FRAME_TERMINAL_CODING (f)->id),
20689 p, 0);
20691 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
20692 p, eol_flag);
20694 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
20695 #ifdef subprocesses
20696 obj = Fget_buffer_process (Fcurrent_buffer ());
20697 if (PROCESSP (obj))
20699 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
20700 p, eol_flag);
20701 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
20702 p, eol_flag);
20704 #endif /* subprocesses */
20705 #endif /* 0 */
20706 *p = 0;
20707 return decode_mode_spec_buf;
20711 if (STRINGP (obj))
20713 *string = obj;
20714 return SSDATA (obj);
20716 else
20717 return "";
20721 /* Count up to COUNT lines starting from START_BYTE.
20722 But don't go beyond LIMIT_BYTE.
20723 Return the number of lines thus found (always nonnegative).
20725 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
20727 static EMACS_INT
20728 display_count_lines (EMACS_INT start_byte,
20729 EMACS_INT limit_byte, EMACS_INT count,
20730 EMACS_INT *byte_pos_ptr)
20732 register unsigned char *cursor;
20733 unsigned char *base;
20735 register EMACS_INT ceiling;
20736 register unsigned char *ceiling_addr;
20737 EMACS_INT orig_count = count;
20739 /* If we are not in selective display mode,
20740 check only for newlines. */
20741 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
20742 && !INTEGERP (BVAR (current_buffer, selective_display)));
20744 if (count > 0)
20746 while (start_byte < limit_byte)
20748 ceiling = BUFFER_CEILING_OF (start_byte);
20749 ceiling = min (limit_byte - 1, ceiling);
20750 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
20751 base = (cursor = BYTE_POS_ADDR (start_byte));
20752 while (1)
20754 if (selective_display)
20755 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
20757 else
20758 while (*cursor != '\n' && ++cursor != ceiling_addr)
20761 if (cursor != ceiling_addr)
20763 if (--count == 0)
20765 start_byte += cursor - base + 1;
20766 *byte_pos_ptr = start_byte;
20767 return orig_count;
20769 else
20770 if (++cursor == ceiling_addr)
20771 break;
20773 else
20774 break;
20776 start_byte += cursor - base;
20779 else
20781 while (start_byte > limit_byte)
20783 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
20784 ceiling = max (limit_byte, ceiling);
20785 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
20786 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
20787 while (1)
20789 if (selective_display)
20790 while (--cursor != ceiling_addr
20791 && *cursor != '\n' && *cursor != 015)
20793 else
20794 while (--cursor != ceiling_addr && *cursor != '\n')
20797 if (cursor != ceiling_addr)
20799 if (++count == 0)
20801 start_byte += cursor - base + 1;
20802 *byte_pos_ptr = start_byte;
20803 /* When scanning backwards, we should
20804 not count the newline posterior to which we stop. */
20805 return - orig_count - 1;
20808 else
20809 break;
20811 /* Here we add 1 to compensate for the last decrement
20812 of CURSOR, which took it past the valid range. */
20813 start_byte += cursor - base + 1;
20817 *byte_pos_ptr = limit_byte;
20819 if (count < 0)
20820 return - orig_count + count;
20821 return orig_count - count;
20827 /***********************************************************************
20828 Displaying strings
20829 ***********************************************************************/
20831 /* Display a NUL-terminated string, starting with index START.
20833 If STRING is non-null, display that C string. Otherwise, the Lisp
20834 string LISP_STRING is displayed. There's a case that STRING is
20835 non-null and LISP_STRING is not nil. It means STRING is a string
20836 data of LISP_STRING. In that case, we display LISP_STRING while
20837 ignoring its text properties.
20839 If FACE_STRING is not nil, FACE_STRING_POS is a position in
20840 FACE_STRING. Display STRING or LISP_STRING with the face at
20841 FACE_STRING_POS in FACE_STRING:
20843 Display the string in the environment given by IT, but use the
20844 standard display table, temporarily.
20846 FIELD_WIDTH is the minimum number of output glyphs to produce.
20847 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20848 with spaces. If STRING has more characters, more than FIELD_WIDTH
20849 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
20851 PRECISION is the maximum number of characters to output from
20852 STRING. PRECISION < 0 means don't truncate the string.
20854 This is roughly equivalent to printf format specifiers:
20856 FIELD_WIDTH PRECISION PRINTF
20857 ----------------------------------------
20858 -1 -1 %s
20859 -1 10 %.10s
20860 10 -1 %10s
20861 20 10 %20.10s
20863 MULTIBYTE zero means do not display multibyte chars, > 0 means do
20864 display them, and < 0 means obey the current buffer's value of
20865 enable_multibyte_characters.
20867 Value is the number of columns displayed. */
20869 static int
20870 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
20871 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
20872 int field_width, int precision, int max_x, int multibyte)
20874 int hpos_at_start = it->hpos;
20875 int saved_face_id = it->face_id;
20876 struct glyph_row *row = it->glyph_row;
20877 EMACS_INT it_charpos;
20879 /* Initialize the iterator IT for iteration over STRING beginning
20880 with index START. */
20881 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
20882 precision, field_width, multibyte);
20883 if (string && STRINGP (lisp_string))
20884 /* LISP_STRING is the one returned by decode_mode_spec. We should
20885 ignore its text properties. */
20886 it->stop_charpos = it->end_charpos;
20888 /* If displaying STRING, set up the face of the iterator from
20889 FACE_STRING, if that's given. */
20890 if (STRINGP (face_string))
20892 EMACS_INT endptr;
20893 struct face *face;
20895 it->face_id
20896 = face_at_string_position (it->w, face_string, face_string_pos,
20897 0, it->region_beg_charpos,
20898 it->region_end_charpos,
20899 &endptr, it->base_face_id, 0);
20900 face = FACE_FROM_ID (it->f, it->face_id);
20901 it->face_box_p = face->box != FACE_NO_BOX;
20904 /* Set max_x to the maximum allowed X position. Don't let it go
20905 beyond the right edge of the window. */
20906 if (max_x <= 0)
20907 max_x = it->last_visible_x;
20908 else
20909 max_x = min (max_x, it->last_visible_x);
20911 /* Skip over display elements that are not visible. because IT->w is
20912 hscrolled. */
20913 if (it->current_x < it->first_visible_x)
20914 move_it_in_display_line_to (it, 100000, it->first_visible_x,
20915 MOVE_TO_POS | MOVE_TO_X);
20917 row->ascent = it->max_ascent;
20918 row->height = it->max_ascent + it->max_descent;
20919 row->phys_ascent = it->max_phys_ascent;
20920 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20921 row->extra_line_spacing = it->max_extra_line_spacing;
20923 if (STRINGP (it->string))
20924 it_charpos = IT_STRING_CHARPOS (*it);
20925 else
20926 it_charpos = IT_CHARPOS (*it);
20928 /* This condition is for the case that we are called with current_x
20929 past last_visible_x. */
20930 while (it->current_x < max_x)
20932 int x_before, x, n_glyphs_before, i, nglyphs;
20934 /* Get the next display element. */
20935 if (!get_next_display_element (it))
20936 break;
20938 /* Produce glyphs. */
20939 x_before = it->current_x;
20940 n_glyphs_before = row->used[TEXT_AREA];
20941 PRODUCE_GLYPHS (it);
20943 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20944 i = 0;
20945 x = x_before;
20946 while (i < nglyphs)
20948 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20950 if (it->line_wrap != TRUNCATE
20951 && x + glyph->pixel_width > max_x)
20953 /* End of continued line or max_x reached. */
20954 if (CHAR_GLYPH_PADDING_P (*glyph))
20956 /* A wide character is unbreakable. */
20957 if (row->reversed_p)
20958 unproduce_glyphs (it, row->used[TEXT_AREA]
20959 - n_glyphs_before);
20960 row->used[TEXT_AREA] = n_glyphs_before;
20961 it->current_x = x_before;
20963 else
20965 if (row->reversed_p)
20966 unproduce_glyphs (it, row->used[TEXT_AREA]
20967 - (n_glyphs_before + i));
20968 row->used[TEXT_AREA] = n_glyphs_before + i;
20969 it->current_x = x;
20971 break;
20973 else if (x + glyph->pixel_width >= it->first_visible_x)
20975 /* Glyph is at least partially visible. */
20976 ++it->hpos;
20977 if (x < it->first_visible_x)
20978 row->x = x - it->first_visible_x;
20980 else
20982 /* Glyph is off the left margin of the display area.
20983 Should not happen. */
20984 abort ();
20987 row->ascent = max (row->ascent, it->max_ascent);
20988 row->height = max (row->height, it->max_ascent + it->max_descent);
20989 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20990 row->phys_height = max (row->phys_height,
20991 it->max_phys_ascent + it->max_phys_descent);
20992 row->extra_line_spacing = max (row->extra_line_spacing,
20993 it->max_extra_line_spacing);
20994 x += glyph->pixel_width;
20995 ++i;
20998 /* Stop if max_x reached. */
20999 if (i < nglyphs)
21000 break;
21002 /* Stop at line ends. */
21003 if (ITERATOR_AT_END_OF_LINE_P (it))
21005 it->continuation_lines_width = 0;
21006 break;
21009 set_iterator_to_next (it, 1);
21010 if (STRINGP (it->string))
21011 it_charpos = IT_STRING_CHARPOS (*it);
21012 else
21013 it_charpos = IT_CHARPOS (*it);
21015 /* Stop if truncating at the right edge. */
21016 if (it->line_wrap == TRUNCATE
21017 && it->current_x >= it->last_visible_x)
21019 /* Add truncation mark, but don't do it if the line is
21020 truncated at a padding space. */
21021 if (it_charpos < it->string_nchars)
21023 if (!FRAME_WINDOW_P (it->f))
21025 int ii, n;
21027 if (it->current_x > it->last_visible_x)
21029 if (!row->reversed_p)
21031 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21032 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21033 break;
21035 else
21037 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21038 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21039 break;
21040 unproduce_glyphs (it, ii + 1);
21041 ii = row->used[TEXT_AREA] - (ii + 1);
21043 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21045 row->used[TEXT_AREA] = ii;
21046 produce_special_glyphs (it, IT_TRUNCATION);
21049 produce_special_glyphs (it, IT_TRUNCATION);
21051 row->truncated_on_right_p = 1;
21053 break;
21057 /* Maybe insert a truncation at the left. */
21058 if (it->first_visible_x
21059 && it_charpos > 0)
21061 if (!FRAME_WINDOW_P (it->f))
21062 insert_left_trunc_glyphs (it);
21063 row->truncated_on_left_p = 1;
21066 it->face_id = saved_face_id;
21068 /* Value is number of columns displayed. */
21069 return it->hpos - hpos_at_start;
21074 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21075 appears as an element of LIST or as the car of an element of LIST.
21076 If PROPVAL is a list, compare each element against LIST in that
21077 way, and return 1/2 if any element of PROPVAL is found in LIST.
21078 Otherwise return 0. This function cannot quit.
21079 The return value is 2 if the text is invisible but with an ellipsis
21080 and 1 if it's invisible and without an ellipsis. */
21083 invisible_p (register Lisp_Object propval, Lisp_Object list)
21085 register Lisp_Object tail, proptail;
21087 for (tail = list; CONSP (tail); tail = XCDR (tail))
21089 register Lisp_Object tem;
21090 tem = XCAR (tail);
21091 if (EQ (propval, tem))
21092 return 1;
21093 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21094 return NILP (XCDR (tem)) ? 1 : 2;
21097 if (CONSP (propval))
21099 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
21101 Lisp_Object propelt;
21102 propelt = XCAR (proptail);
21103 for (tail = list; CONSP (tail); tail = XCDR (tail))
21105 register Lisp_Object tem;
21106 tem = XCAR (tail);
21107 if (EQ (propelt, tem))
21108 return 1;
21109 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
21110 return NILP (XCDR (tem)) ? 1 : 2;
21115 return 0;
21118 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
21119 doc: /* Non-nil if the property makes the text invisible.
21120 POS-OR-PROP can be a marker or number, in which case it is taken to be
21121 a position in the current buffer and the value of the `invisible' property
21122 is checked; or it can be some other value, which is then presumed to be the
21123 value of the `invisible' property of the text of interest.
21124 The non-nil value returned can be t for truly invisible text or something
21125 else if the text is replaced by an ellipsis. */)
21126 (Lisp_Object pos_or_prop)
21128 Lisp_Object prop
21129 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
21130 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
21131 : pos_or_prop);
21132 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
21133 return (invis == 0 ? Qnil
21134 : invis == 1 ? Qt
21135 : make_number (invis));
21138 /* Calculate a width or height in pixels from a specification using
21139 the following elements:
21141 SPEC ::=
21142 NUM - a (fractional) multiple of the default font width/height
21143 (NUM) - specifies exactly NUM pixels
21144 UNIT - a fixed number of pixels, see below.
21145 ELEMENT - size of a display element in pixels, see below.
21146 (NUM . SPEC) - equals NUM * SPEC
21147 (+ SPEC SPEC ...) - add pixel values
21148 (- SPEC SPEC ...) - subtract pixel values
21149 (- SPEC) - negate pixel value
21151 NUM ::=
21152 INT or FLOAT - a number constant
21153 SYMBOL - use symbol's (buffer local) variable binding.
21155 UNIT ::=
21156 in - pixels per inch *)
21157 mm - pixels per 1/1000 meter *)
21158 cm - pixels per 1/100 meter *)
21159 width - width of current font in pixels.
21160 height - height of current font in pixels.
21162 *) using the ratio(s) defined in display-pixels-per-inch.
21164 ELEMENT ::=
21166 left-fringe - left fringe width in pixels
21167 right-fringe - right fringe width in pixels
21169 left-margin - left margin width in pixels
21170 right-margin - right margin width in pixels
21172 scroll-bar - scroll-bar area width in pixels
21174 Examples:
21176 Pixels corresponding to 5 inches:
21177 (5 . in)
21179 Total width of non-text areas on left side of window (if scroll-bar is on left):
21180 '(space :width (+ left-fringe left-margin scroll-bar))
21182 Align to first text column (in header line):
21183 '(space :align-to 0)
21185 Align to middle of text area minus half the width of variable `my-image'
21186 containing a loaded image:
21187 '(space :align-to (0.5 . (- text my-image)))
21189 Width of left margin minus width of 1 character in the default font:
21190 '(space :width (- left-margin 1))
21192 Width of left margin minus width of 2 characters in the current font:
21193 '(space :width (- left-margin (2 . width)))
21195 Center 1 character over left-margin (in header line):
21196 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
21198 Different ways to express width of left fringe plus left margin minus one pixel:
21199 '(space :width (- (+ left-fringe left-margin) (1)))
21200 '(space :width (+ left-fringe left-margin (- (1))))
21201 '(space :width (+ left-fringe left-margin (-1)))
21205 #define NUMVAL(X) \
21206 ((INTEGERP (X) || FLOATP (X)) \
21207 ? XFLOATINT (X) \
21208 : - 1)
21211 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
21212 struct font *font, int width_p, int *align_to)
21214 double pixels;
21216 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
21217 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
21219 if (NILP (prop))
21220 return OK_PIXELS (0);
21222 xassert (FRAME_LIVE_P (it->f));
21224 if (SYMBOLP (prop))
21226 if (SCHARS (SYMBOL_NAME (prop)) == 2)
21228 char *unit = SSDATA (SYMBOL_NAME (prop));
21230 if (unit[0] == 'i' && unit[1] == 'n')
21231 pixels = 1.0;
21232 else if (unit[0] == 'm' && unit[1] == 'm')
21233 pixels = 25.4;
21234 else if (unit[0] == 'c' && unit[1] == 'm')
21235 pixels = 2.54;
21236 else
21237 pixels = 0;
21238 if (pixels > 0)
21240 double ppi;
21241 #ifdef HAVE_WINDOW_SYSTEM
21242 if (FRAME_WINDOW_P (it->f)
21243 && (ppi = (width_p
21244 ? FRAME_X_DISPLAY_INFO (it->f)->resx
21245 : FRAME_X_DISPLAY_INFO (it->f)->resy),
21246 ppi > 0))
21247 return OK_PIXELS (ppi / pixels);
21248 #endif
21250 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
21251 || (CONSP (Vdisplay_pixels_per_inch)
21252 && (ppi = (width_p
21253 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
21254 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
21255 ppi > 0)))
21256 return OK_PIXELS (ppi / pixels);
21258 return 0;
21262 #ifdef HAVE_WINDOW_SYSTEM
21263 if (EQ (prop, Qheight))
21264 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
21265 if (EQ (prop, Qwidth))
21266 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
21267 #else
21268 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
21269 return OK_PIXELS (1);
21270 #endif
21272 if (EQ (prop, Qtext))
21273 return OK_PIXELS (width_p
21274 ? window_box_width (it->w, TEXT_AREA)
21275 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
21277 if (align_to && *align_to < 0)
21279 *res = 0;
21280 if (EQ (prop, Qleft))
21281 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
21282 if (EQ (prop, Qright))
21283 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
21284 if (EQ (prop, Qcenter))
21285 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
21286 + window_box_width (it->w, TEXT_AREA) / 2);
21287 if (EQ (prop, Qleft_fringe))
21288 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21289 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
21290 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
21291 if (EQ (prop, Qright_fringe))
21292 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21293 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21294 : window_box_right_offset (it->w, TEXT_AREA));
21295 if (EQ (prop, Qleft_margin))
21296 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
21297 if (EQ (prop, Qright_margin))
21298 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
21299 if (EQ (prop, Qscroll_bar))
21300 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
21302 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21303 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21304 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21305 : 0)));
21307 else
21309 if (EQ (prop, Qleft_fringe))
21310 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
21311 if (EQ (prop, Qright_fringe))
21312 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
21313 if (EQ (prop, Qleft_margin))
21314 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
21315 if (EQ (prop, Qright_margin))
21316 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
21317 if (EQ (prop, Qscroll_bar))
21318 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
21321 prop = Fbuffer_local_value (prop, it->w->buffer);
21324 if (INTEGERP (prop) || FLOATP (prop))
21326 int base_unit = (width_p
21327 ? FRAME_COLUMN_WIDTH (it->f)
21328 : FRAME_LINE_HEIGHT (it->f));
21329 return OK_PIXELS (XFLOATINT (prop) * base_unit);
21332 if (CONSP (prop))
21334 Lisp_Object car = XCAR (prop);
21335 Lisp_Object cdr = XCDR (prop);
21337 if (SYMBOLP (car))
21339 #ifdef HAVE_WINDOW_SYSTEM
21340 if (FRAME_WINDOW_P (it->f)
21341 && valid_image_p (prop))
21343 int id = lookup_image (it->f, prop);
21344 struct image *img = IMAGE_FROM_ID (it->f, id);
21346 return OK_PIXELS (width_p ? img->width : img->height);
21348 #endif
21349 if (EQ (car, Qplus) || EQ (car, Qminus))
21351 int first = 1;
21352 double px;
21354 pixels = 0;
21355 while (CONSP (cdr))
21357 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
21358 font, width_p, align_to))
21359 return 0;
21360 if (first)
21361 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
21362 else
21363 pixels += px;
21364 cdr = XCDR (cdr);
21366 if (EQ (car, Qminus))
21367 pixels = -pixels;
21368 return OK_PIXELS (pixels);
21371 car = Fbuffer_local_value (car, it->w->buffer);
21374 if (INTEGERP (car) || FLOATP (car))
21376 double fact;
21377 pixels = XFLOATINT (car);
21378 if (NILP (cdr))
21379 return OK_PIXELS (pixels);
21380 if (calc_pixel_width_or_height (&fact, it, cdr,
21381 font, width_p, align_to))
21382 return OK_PIXELS (pixels * fact);
21383 return 0;
21386 return 0;
21389 return 0;
21393 /***********************************************************************
21394 Glyph Display
21395 ***********************************************************************/
21397 #ifdef HAVE_WINDOW_SYSTEM
21399 #if GLYPH_DEBUG
21401 void
21402 dump_glyph_string (struct glyph_string *s)
21404 fprintf (stderr, "glyph string\n");
21405 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
21406 s->x, s->y, s->width, s->height);
21407 fprintf (stderr, " ybase = %d\n", s->ybase);
21408 fprintf (stderr, " hl = %d\n", s->hl);
21409 fprintf (stderr, " left overhang = %d, right = %d\n",
21410 s->left_overhang, s->right_overhang);
21411 fprintf (stderr, " nchars = %d\n", s->nchars);
21412 fprintf (stderr, " extends to end of line = %d\n",
21413 s->extends_to_end_of_line_p);
21414 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
21415 fprintf (stderr, " bg width = %d\n", s->background_width);
21418 #endif /* GLYPH_DEBUG */
21420 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
21421 of XChar2b structures for S; it can't be allocated in
21422 init_glyph_string because it must be allocated via `alloca'. W
21423 is the window on which S is drawn. ROW and AREA are the glyph row
21424 and area within the row from which S is constructed. START is the
21425 index of the first glyph structure covered by S. HL is a
21426 face-override for drawing S. */
21428 #ifdef HAVE_NTGUI
21429 #define OPTIONAL_HDC(hdc) HDC hdc,
21430 #define DECLARE_HDC(hdc) HDC hdc;
21431 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
21432 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
21433 #endif
21435 #ifndef OPTIONAL_HDC
21436 #define OPTIONAL_HDC(hdc)
21437 #define DECLARE_HDC(hdc)
21438 #define ALLOCATE_HDC(hdc, f)
21439 #define RELEASE_HDC(hdc, f)
21440 #endif
21442 static void
21443 init_glyph_string (struct glyph_string *s,
21444 OPTIONAL_HDC (hdc)
21445 XChar2b *char2b, struct window *w, struct glyph_row *row,
21446 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
21448 memset (s, 0, sizeof *s);
21449 s->w = w;
21450 s->f = XFRAME (w->frame);
21451 #ifdef HAVE_NTGUI
21452 s->hdc = hdc;
21453 #endif
21454 s->display = FRAME_X_DISPLAY (s->f);
21455 s->window = FRAME_X_WINDOW (s->f);
21456 s->char2b = char2b;
21457 s->hl = hl;
21458 s->row = row;
21459 s->area = area;
21460 s->first_glyph = row->glyphs[area] + start;
21461 s->height = row->height;
21462 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
21463 s->ybase = s->y + row->ascent;
21467 /* Append the list of glyph strings with head H and tail T to the list
21468 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
21470 static inline void
21471 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
21472 struct glyph_string *h, struct glyph_string *t)
21474 if (h)
21476 if (*head)
21477 (*tail)->next = h;
21478 else
21479 *head = h;
21480 h->prev = *tail;
21481 *tail = t;
21486 /* Prepend the list of glyph strings with head H and tail T to the
21487 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
21488 result. */
21490 static inline void
21491 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
21492 struct glyph_string *h, struct glyph_string *t)
21494 if (h)
21496 if (*head)
21497 (*head)->prev = t;
21498 else
21499 *tail = t;
21500 t->next = *head;
21501 *head = h;
21506 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
21507 Set *HEAD and *TAIL to the resulting list. */
21509 static inline void
21510 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
21511 struct glyph_string *s)
21513 s->next = s->prev = NULL;
21514 append_glyph_string_lists (head, tail, s, s);
21518 /* Get face and two-byte form of character C in face FACE_ID on frame F.
21519 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
21520 make sure that X resources for the face returned are allocated.
21521 Value is a pointer to a realized face that is ready for display if
21522 DISPLAY_P is non-zero. */
21524 static inline struct face *
21525 get_char_face_and_encoding (struct frame *f, int c, int face_id,
21526 XChar2b *char2b, int display_p)
21528 struct face *face = FACE_FROM_ID (f, face_id);
21530 if (face->font)
21532 unsigned code = face->font->driver->encode_char (face->font, c);
21534 if (code != FONT_INVALID_CODE)
21535 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21536 else
21537 STORE_XCHAR2B (char2b, 0, 0);
21540 /* Make sure X resources of the face are allocated. */
21541 #ifdef HAVE_X_WINDOWS
21542 if (display_p)
21543 #endif
21545 xassert (face != NULL);
21546 PREPARE_FACE_FOR_DISPLAY (f, face);
21549 return face;
21553 /* Get face and two-byte form of character glyph GLYPH on frame F.
21554 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
21555 a pointer to a realized face that is ready for display. */
21557 static inline struct face *
21558 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
21559 XChar2b *char2b, int *two_byte_p)
21561 struct face *face;
21563 xassert (glyph->type == CHAR_GLYPH);
21564 face = FACE_FROM_ID (f, glyph->face_id);
21566 if (two_byte_p)
21567 *two_byte_p = 0;
21569 if (face->font)
21571 unsigned code;
21573 if (CHAR_BYTE8_P (glyph->u.ch))
21574 code = CHAR_TO_BYTE8 (glyph->u.ch);
21575 else
21576 code = face->font->driver->encode_char (face->font, glyph->u.ch);
21578 if (code != FONT_INVALID_CODE)
21579 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21580 else
21581 STORE_XCHAR2B (char2b, 0, 0);
21584 /* Make sure X resources of the face are allocated. */
21585 xassert (face != NULL);
21586 PREPARE_FACE_FOR_DISPLAY (f, face);
21587 return face;
21591 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
21592 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
21594 static inline int
21595 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
21597 unsigned code;
21599 if (CHAR_BYTE8_P (c))
21600 code = CHAR_TO_BYTE8 (c);
21601 else
21602 code = font->driver->encode_char (font, c);
21604 if (code == FONT_INVALID_CODE)
21605 return 0;
21606 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21607 return 1;
21611 /* Fill glyph string S with composition components specified by S->cmp.
21613 BASE_FACE is the base face of the composition.
21614 S->cmp_from is the index of the first component for S.
21616 OVERLAPS non-zero means S should draw the foreground only, and use
21617 its physical height for clipping. See also draw_glyphs.
21619 Value is the index of a component not in S. */
21621 static int
21622 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
21623 int overlaps)
21625 int i;
21626 /* For all glyphs of this composition, starting at the offset
21627 S->cmp_from, until we reach the end of the definition or encounter a
21628 glyph that requires the different face, add it to S. */
21629 struct face *face;
21631 xassert (s);
21633 s->for_overlaps = overlaps;
21634 s->face = NULL;
21635 s->font = NULL;
21636 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
21638 int c = COMPOSITION_GLYPH (s->cmp, i);
21640 if (c != '\t')
21642 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
21643 -1, Qnil);
21645 face = get_char_face_and_encoding (s->f, c, face_id,
21646 s->char2b + i, 1);
21647 if (face)
21649 if (! s->face)
21651 s->face = face;
21652 s->font = s->face->font;
21654 else if (s->face != face)
21655 break;
21658 ++s->nchars;
21660 s->cmp_to = i;
21662 /* All glyph strings for the same composition has the same width,
21663 i.e. the width set for the first component of the composition. */
21664 s->width = s->first_glyph->pixel_width;
21666 /* If the specified font could not be loaded, use the frame's
21667 default font, but record the fact that we couldn't load it in
21668 the glyph string so that we can draw rectangles for the
21669 characters of the glyph string. */
21670 if (s->font == NULL)
21672 s->font_not_found_p = 1;
21673 s->font = FRAME_FONT (s->f);
21676 /* Adjust base line for subscript/superscript text. */
21677 s->ybase += s->first_glyph->voffset;
21679 /* This glyph string must always be drawn with 16-bit functions. */
21680 s->two_byte_p = 1;
21682 return s->cmp_to;
21685 static int
21686 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
21687 int start, int end, int overlaps)
21689 struct glyph *glyph, *last;
21690 Lisp_Object lgstring;
21691 int i;
21693 s->for_overlaps = overlaps;
21694 glyph = s->row->glyphs[s->area] + start;
21695 last = s->row->glyphs[s->area] + end;
21696 s->cmp_id = glyph->u.cmp.id;
21697 s->cmp_from = glyph->slice.cmp.from;
21698 s->cmp_to = glyph->slice.cmp.to + 1;
21699 s->face = FACE_FROM_ID (s->f, face_id);
21700 lgstring = composition_gstring_from_id (s->cmp_id);
21701 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
21702 glyph++;
21703 while (glyph < last
21704 && glyph->u.cmp.automatic
21705 && glyph->u.cmp.id == s->cmp_id
21706 && s->cmp_to == glyph->slice.cmp.from)
21707 s->cmp_to = (glyph++)->slice.cmp.to + 1;
21709 for (i = s->cmp_from; i < s->cmp_to; i++)
21711 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
21712 unsigned code = LGLYPH_CODE (lglyph);
21714 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
21716 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
21717 return glyph - s->row->glyphs[s->area];
21721 /* Fill glyph string S from a sequence glyphs for glyphless characters.
21722 See the comment of fill_glyph_string for arguments.
21723 Value is the index of the first glyph not in S. */
21726 static int
21727 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
21728 int start, int end, int overlaps)
21730 struct glyph *glyph, *last;
21731 int voffset;
21733 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
21734 s->for_overlaps = overlaps;
21735 glyph = s->row->glyphs[s->area] + start;
21736 last = s->row->glyphs[s->area] + end;
21737 voffset = glyph->voffset;
21738 s->face = FACE_FROM_ID (s->f, face_id);
21739 s->font = s->face->font;
21740 s->nchars = 1;
21741 s->width = glyph->pixel_width;
21742 glyph++;
21743 while (glyph < last
21744 && glyph->type == GLYPHLESS_GLYPH
21745 && glyph->voffset == voffset
21746 && glyph->face_id == face_id)
21748 s->nchars++;
21749 s->width += glyph->pixel_width;
21750 glyph++;
21752 s->ybase += voffset;
21753 return glyph - s->row->glyphs[s->area];
21757 /* Fill glyph string S from a sequence of character glyphs.
21759 FACE_ID is the face id of the string. START is the index of the
21760 first glyph to consider, END is the index of the last + 1.
21761 OVERLAPS non-zero means S should draw the foreground only, and use
21762 its physical height for clipping. See also draw_glyphs.
21764 Value is the index of the first glyph not in S. */
21766 static int
21767 fill_glyph_string (struct glyph_string *s, int face_id,
21768 int start, int end, int overlaps)
21770 struct glyph *glyph, *last;
21771 int voffset;
21772 int glyph_not_available_p;
21774 xassert (s->f == XFRAME (s->w->frame));
21775 xassert (s->nchars == 0);
21776 xassert (start >= 0 && end > start);
21778 s->for_overlaps = overlaps;
21779 glyph = s->row->glyphs[s->area] + start;
21780 last = s->row->glyphs[s->area] + end;
21781 voffset = glyph->voffset;
21782 s->padding_p = glyph->padding_p;
21783 glyph_not_available_p = glyph->glyph_not_available_p;
21785 while (glyph < last
21786 && glyph->type == CHAR_GLYPH
21787 && glyph->voffset == voffset
21788 /* Same face id implies same font, nowadays. */
21789 && glyph->face_id == face_id
21790 && glyph->glyph_not_available_p == glyph_not_available_p)
21792 int two_byte_p;
21794 s->face = get_glyph_face_and_encoding (s->f, glyph,
21795 s->char2b + s->nchars,
21796 &two_byte_p);
21797 s->two_byte_p = two_byte_p;
21798 ++s->nchars;
21799 xassert (s->nchars <= end - start);
21800 s->width += glyph->pixel_width;
21801 if (glyph++->padding_p != s->padding_p)
21802 break;
21805 s->font = s->face->font;
21807 /* If the specified font could not be loaded, use the frame's font,
21808 but record the fact that we couldn't load it in
21809 S->font_not_found_p so that we can draw rectangles for the
21810 characters of the glyph string. */
21811 if (s->font == NULL || glyph_not_available_p)
21813 s->font_not_found_p = 1;
21814 s->font = FRAME_FONT (s->f);
21817 /* Adjust base line for subscript/superscript text. */
21818 s->ybase += voffset;
21820 xassert (s->face && s->face->gc);
21821 return glyph - s->row->glyphs[s->area];
21825 /* Fill glyph string S from image glyph S->first_glyph. */
21827 static void
21828 fill_image_glyph_string (struct glyph_string *s)
21830 xassert (s->first_glyph->type == IMAGE_GLYPH);
21831 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
21832 xassert (s->img);
21833 s->slice = s->first_glyph->slice.img;
21834 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
21835 s->font = s->face->font;
21836 s->width = s->first_glyph->pixel_width;
21838 /* Adjust base line for subscript/superscript text. */
21839 s->ybase += s->first_glyph->voffset;
21843 /* Fill glyph string S from a sequence of stretch glyphs.
21845 START is the index of the first glyph to consider,
21846 END is the index of the last + 1.
21848 Value is the index of the first glyph not in S. */
21850 static int
21851 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
21853 struct glyph *glyph, *last;
21854 int voffset, face_id;
21856 xassert (s->first_glyph->type == STRETCH_GLYPH);
21858 glyph = s->row->glyphs[s->area] + start;
21859 last = s->row->glyphs[s->area] + end;
21860 face_id = glyph->face_id;
21861 s->face = FACE_FROM_ID (s->f, face_id);
21862 s->font = s->face->font;
21863 s->width = glyph->pixel_width;
21864 s->nchars = 1;
21865 voffset = glyph->voffset;
21867 for (++glyph;
21868 (glyph < last
21869 && glyph->type == STRETCH_GLYPH
21870 && glyph->voffset == voffset
21871 && glyph->face_id == face_id);
21872 ++glyph)
21873 s->width += glyph->pixel_width;
21875 /* Adjust base line for subscript/superscript text. */
21876 s->ybase += voffset;
21878 /* The case that face->gc == 0 is handled when drawing the glyph
21879 string by calling PREPARE_FACE_FOR_DISPLAY. */
21880 xassert (s->face);
21881 return glyph - s->row->glyphs[s->area];
21884 static struct font_metrics *
21885 get_per_char_metric (struct font *font, XChar2b *char2b)
21887 static struct font_metrics metrics;
21888 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
21890 if (! font || code == FONT_INVALID_CODE)
21891 return NULL;
21892 font->driver->text_extents (font, &code, 1, &metrics);
21893 return &metrics;
21896 /* EXPORT for RIF:
21897 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
21898 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
21899 assumed to be zero. */
21901 void
21902 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
21904 *left = *right = 0;
21906 if (glyph->type == CHAR_GLYPH)
21908 struct face *face;
21909 XChar2b char2b;
21910 struct font_metrics *pcm;
21912 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
21913 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
21915 if (pcm->rbearing > pcm->width)
21916 *right = pcm->rbearing - pcm->width;
21917 if (pcm->lbearing < 0)
21918 *left = -pcm->lbearing;
21921 else if (glyph->type == COMPOSITE_GLYPH)
21923 if (! glyph->u.cmp.automatic)
21925 struct composition *cmp = composition_table[glyph->u.cmp.id];
21927 if (cmp->rbearing > cmp->pixel_width)
21928 *right = cmp->rbearing - cmp->pixel_width;
21929 if (cmp->lbearing < 0)
21930 *left = - cmp->lbearing;
21932 else
21934 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
21935 struct font_metrics metrics;
21937 composition_gstring_width (gstring, glyph->slice.cmp.from,
21938 glyph->slice.cmp.to + 1, &metrics);
21939 if (metrics.rbearing > metrics.width)
21940 *right = metrics.rbearing - metrics.width;
21941 if (metrics.lbearing < 0)
21942 *left = - metrics.lbearing;
21948 /* Return the index of the first glyph preceding glyph string S that
21949 is overwritten by S because of S's left overhang. Value is -1
21950 if no glyphs are overwritten. */
21952 static int
21953 left_overwritten (struct glyph_string *s)
21955 int k;
21957 if (s->left_overhang)
21959 int x = 0, i;
21960 struct glyph *glyphs = s->row->glyphs[s->area];
21961 int first = s->first_glyph - glyphs;
21963 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
21964 x -= glyphs[i].pixel_width;
21966 k = i + 1;
21968 else
21969 k = -1;
21971 return k;
21975 /* Return the index of the first glyph preceding glyph string S that
21976 is overwriting S because of its right overhang. Value is -1 if no
21977 glyph in front of S overwrites S. */
21979 static int
21980 left_overwriting (struct glyph_string *s)
21982 int i, k, x;
21983 struct glyph *glyphs = s->row->glyphs[s->area];
21984 int first = s->first_glyph - glyphs;
21986 k = -1;
21987 x = 0;
21988 for (i = first - 1; i >= 0; --i)
21990 int left, right;
21991 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21992 if (x + right > 0)
21993 k = i;
21994 x -= glyphs[i].pixel_width;
21997 return k;
22001 /* Return the index of the last glyph following glyph string S that is
22002 overwritten by S because of S's right overhang. Value is -1 if
22003 no such glyph is found. */
22005 static int
22006 right_overwritten (struct glyph_string *s)
22008 int k = -1;
22010 if (s->right_overhang)
22012 int x = 0, i;
22013 struct glyph *glyphs = s->row->glyphs[s->area];
22014 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22015 int end = s->row->used[s->area];
22017 for (i = first; i < end && s->right_overhang > x; ++i)
22018 x += glyphs[i].pixel_width;
22020 k = i;
22023 return k;
22027 /* Return the index of the last glyph following glyph string S that
22028 overwrites S because of its left overhang. Value is negative
22029 if no such glyph is found. */
22031 static int
22032 right_overwriting (struct glyph_string *s)
22034 int i, k, x;
22035 int end = s->row->used[s->area];
22036 struct glyph *glyphs = s->row->glyphs[s->area];
22037 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22039 k = -1;
22040 x = 0;
22041 for (i = first; i < end; ++i)
22043 int left, right;
22044 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22045 if (x - left < 0)
22046 k = i;
22047 x += glyphs[i].pixel_width;
22050 return k;
22054 /* Set background width of glyph string S. START is the index of the
22055 first glyph following S. LAST_X is the right-most x-position + 1
22056 in the drawing area. */
22058 static inline void
22059 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22061 /* If the face of this glyph string has to be drawn to the end of
22062 the drawing area, set S->extends_to_end_of_line_p. */
22064 if (start == s->row->used[s->area]
22065 && s->area == TEXT_AREA
22066 && ((s->row->fill_line_p
22067 && (s->hl == DRAW_NORMAL_TEXT
22068 || s->hl == DRAW_IMAGE_RAISED
22069 || s->hl == DRAW_IMAGE_SUNKEN))
22070 || s->hl == DRAW_MOUSE_FACE))
22071 s->extends_to_end_of_line_p = 1;
22073 /* If S extends its face to the end of the line, set its
22074 background_width to the distance to the right edge of the drawing
22075 area. */
22076 if (s->extends_to_end_of_line_p)
22077 s->background_width = last_x - s->x + 1;
22078 else
22079 s->background_width = s->width;
22083 /* Compute overhangs and x-positions for glyph string S and its
22084 predecessors, or successors. X is the starting x-position for S.
22085 BACKWARD_P non-zero means process predecessors. */
22087 static void
22088 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22090 if (backward_p)
22092 while (s)
22094 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22095 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22096 x -= s->width;
22097 s->x = x;
22098 s = s->prev;
22101 else
22103 while (s)
22105 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22106 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22107 s->x = x;
22108 x += s->width;
22109 s = s->next;
22116 /* The following macros are only called from draw_glyphs below.
22117 They reference the following parameters of that function directly:
22118 `w', `row', `area', and `overlap_p'
22119 as well as the following local variables:
22120 `s', `f', and `hdc' (in W32) */
22122 #ifdef HAVE_NTGUI
22123 /* On W32, silently add local `hdc' variable to argument list of
22124 init_glyph_string. */
22125 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22126 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
22127 #else
22128 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22129 init_glyph_string (s, char2b, w, row, area, start, hl)
22130 #endif
22132 /* Add a glyph string for a stretch glyph to the list of strings
22133 between HEAD and TAIL. START is the index of the stretch glyph in
22134 row area AREA of glyph row ROW. END is the index of the last glyph
22135 in that glyph row area. X is the current output position assigned
22136 to the new glyph string constructed. HL overrides that face of the
22137 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22138 is the right-most x-position of the drawing area. */
22140 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
22141 and below -- keep them on one line. */
22142 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22143 do \
22145 s = (struct glyph_string *) alloca (sizeof *s); \
22146 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22147 START = fill_stretch_glyph_string (s, START, END); \
22148 append_glyph_string (&HEAD, &TAIL, s); \
22149 s->x = (X); \
22151 while (0)
22154 /* Add a glyph string for an image glyph to the list of strings
22155 between HEAD and TAIL. START is the index of the image glyph in
22156 row area AREA of glyph row ROW. END is the index of the last glyph
22157 in that glyph row area. X is the current output position assigned
22158 to the new glyph string constructed. HL overrides that face of the
22159 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22160 is the right-most x-position of the drawing area. */
22162 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22163 do \
22165 s = (struct glyph_string *) alloca (sizeof *s); \
22166 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22167 fill_image_glyph_string (s); \
22168 append_glyph_string (&HEAD, &TAIL, s); \
22169 ++START; \
22170 s->x = (X); \
22172 while (0)
22175 /* Add a glyph string for a sequence of character glyphs to the list
22176 of strings between HEAD and TAIL. START is the index of the first
22177 glyph in row area AREA of glyph row ROW that is part of the new
22178 glyph string. END is the index of the last glyph in that glyph row
22179 area. X is the current output position assigned to the new glyph
22180 string constructed. HL overrides that face of the glyph; e.g. it
22181 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
22182 right-most x-position of the drawing area. */
22184 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22185 do \
22187 int face_id; \
22188 XChar2b *char2b; \
22190 face_id = (row)->glyphs[area][START].face_id; \
22192 s = (struct glyph_string *) alloca (sizeof *s); \
22193 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
22194 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22195 append_glyph_string (&HEAD, &TAIL, s); \
22196 s->x = (X); \
22197 START = fill_glyph_string (s, face_id, START, END, overlaps); \
22199 while (0)
22202 /* Add a glyph string for a composite sequence to the list of strings
22203 between HEAD and TAIL. START is the index of the first glyph in
22204 row area AREA of glyph row ROW that is part of the new glyph
22205 string. END is the index of the last glyph in that glyph row area.
22206 X is the current output position assigned to the new glyph string
22207 constructed. HL overrides that face of the glyph; e.g. it is
22208 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
22209 x-position of the drawing area. */
22211 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22212 do { \
22213 int face_id = (row)->glyphs[area][START].face_id; \
22214 struct face *base_face = FACE_FROM_ID (f, face_id); \
22215 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
22216 struct composition *cmp = composition_table[cmp_id]; \
22217 XChar2b *char2b; \
22218 struct glyph_string *first_s IF_LINT (= NULL); \
22219 int n; \
22221 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
22223 /* Make glyph_strings for each glyph sequence that is drawable by \
22224 the same face, and append them to HEAD/TAIL. */ \
22225 for (n = 0; n < cmp->glyph_len;) \
22227 s = (struct glyph_string *) alloca (sizeof *s); \
22228 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22229 append_glyph_string (&(HEAD), &(TAIL), s); \
22230 s->cmp = cmp; \
22231 s->cmp_from = n; \
22232 s->x = (X); \
22233 if (n == 0) \
22234 first_s = s; \
22235 n = fill_composite_glyph_string (s, base_face, overlaps); \
22238 ++START; \
22239 s = first_s; \
22240 } while (0)
22243 /* Add a glyph string for a glyph-string sequence to the list of strings
22244 between HEAD and TAIL. */
22246 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22247 do { \
22248 int face_id; \
22249 XChar2b *char2b; \
22250 Lisp_Object gstring; \
22252 face_id = (row)->glyphs[area][START].face_id; \
22253 gstring = (composition_gstring_from_id \
22254 ((row)->glyphs[area][START].u.cmp.id)); \
22255 s = (struct glyph_string *) alloca (sizeof *s); \
22256 char2b = (XChar2b *) alloca ((sizeof *char2b) \
22257 * LGSTRING_GLYPH_LEN (gstring)); \
22258 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22259 append_glyph_string (&(HEAD), &(TAIL), s); \
22260 s->x = (X); \
22261 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
22262 } while (0)
22265 /* Add a glyph string for a sequence of glyphless character's glyphs
22266 to the list of strings between HEAD and TAIL. The meanings of
22267 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
22269 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22270 do \
22272 int face_id; \
22274 face_id = (row)->glyphs[area][START].face_id; \
22276 s = (struct glyph_string *) alloca (sizeof *s); \
22277 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22278 append_glyph_string (&HEAD, &TAIL, s); \
22279 s->x = (X); \
22280 START = fill_glyphless_glyph_string (s, face_id, START, END, \
22281 overlaps); \
22283 while (0)
22286 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
22287 of AREA of glyph row ROW on window W between indices START and END.
22288 HL overrides the face for drawing glyph strings, e.g. it is
22289 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
22290 x-positions of the drawing area.
22292 This is an ugly monster macro construct because we must use alloca
22293 to allocate glyph strings (because draw_glyphs can be called
22294 asynchronously). */
22296 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22297 do \
22299 HEAD = TAIL = NULL; \
22300 while (START < END) \
22302 struct glyph *first_glyph = (row)->glyphs[area] + START; \
22303 switch (first_glyph->type) \
22305 case CHAR_GLYPH: \
22306 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
22307 HL, X, LAST_X); \
22308 break; \
22310 case COMPOSITE_GLYPH: \
22311 if (first_glyph->u.cmp.automatic) \
22312 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
22313 HL, X, LAST_X); \
22314 else \
22315 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
22316 HL, X, LAST_X); \
22317 break; \
22319 case STRETCH_GLYPH: \
22320 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
22321 HL, X, LAST_X); \
22322 break; \
22324 case IMAGE_GLYPH: \
22325 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
22326 HL, X, LAST_X); \
22327 break; \
22329 case GLYPHLESS_GLYPH: \
22330 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
22331 HL, X, LAST_X); \
22332 break; \
22334 default: \
22335 abort (); \
22338 if (s) \
22340 set_glyph_string_background_width (s, START, LAST_X); \
22341 (X) += s->width; \
22344 } while (0)
22347 /* Draw glyphs between START and END in AREA of ROW on window W,
22348 starting at x-position X. X is relative to AREA in W. HL is a
22349 face-override with the following meaning:
22351 DRAW_NORMAL_TEXT draw normally
22352 DRAW_CURSOR draw in cursor face
22353 DRAW_MOUSE_FACE draw in mouse face.
22354 DRAW_INVERSE_VIDEO draw in mode line face
22355 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
22356 DRAW_IMAGE_RAISED draw an image with a raised relief around it
22358 If OVERLAPS is non-zero, draw only the foreground of characters and
22359 clip to the physical height of ROW. Non-zero value also defines
22360 the overlapping part to be drawn:
22362 OVERLAPS_PRED overlap with preceding rows
22363 OVERLAPS_SUCC overlap with succeeding rows
22364 OVERLAPS_BOTH overlap with both preceding/succeeding rows
22365 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
22367 Value is the x-position reached, relative to AREA of W. */
22369 static int
22370 draw_glyphs (struct window *w, int x, struct glyph_row *row,
22371 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
22372 enum draw_glyphs_face hl, int overlaps)
22374 struct glyph_string *head, *tail;
22375 struct glyph_string *s;
22376 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
22377 int i, j, x_reached, last_x, area_left = 0;
22378 struct frame *f = XFRAME (WINDOW_FRAME (w));
22379 DECLARE_HDC (hdc);
22381 ALLOCATE_HDC (hdc, f);
22383 /* Let's rather be paranoid than getting a SEGV. */
22384 end = min (end, row->used[area]);
22385 start = max (0, start);
22386 start = min (end, start);
22388 /* Translate X to frame coordinates. Set last_x to the right
22389 end of the drawing area. */
22390 if (row->full_width_p)
22392 /* X is relative to the left edge of W, without scroll bars
22393 or fringes. */
22394 area_left = WINDOW_LEFT_EDGE_X (w);
22395 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
22397 else
22399 area_left = window_box_left (w, area);
22400 last_x = area_left + window_box_width (w, area);
22402 x += area_left;
22404 /* Build a doubly-linked list of glyph_string structures between
22405 head and tail from what we have to draw. Note that the macro
22406 BUILD_GLYPH_STRINGS will modify its start parameter. That's
22407 the reason we use a separate variable `i'. */
22408 i = start;
22409 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
22410 if (tail)
22411 x_reached = tail->x + tail->background_width;
22412 else
22413 x_reached = x;
22415 /* If there are any glyphs with lbearing < 0 or rbearing > width in
22416 the row, redraw some glyphs in front or following the glyph
22417 strings built above. */
22418 if (head && !overlaps && row->contains_overlapping_glyphs_p)
22420 struct glyph_string *h, *t;
22421 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
22422 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
22423 int check_mouse_face = 0;
22424 int dummy_x = 0;
22426 /* If mouse highlighting is on, we may need to draw adjacent
22427 glyphs using mouse-face highlighting. */
22428 if (area == TEXT_AREA && row->mouse_face_p)
22430 struct glyph_row *mouse_beg_row, *mouse_end_row;
22432 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
22433 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
22435 if (row >= mouse_beg_row && row <= mouse_end_row)
22437 check_mouse_face = 1;
22438 mouse_beg_col = (row == mouse_beg_row)
22439 ? hlinfo->mouse_face_beg_col : 0;
22440 mouse_end_col = (row == mouse_end_row)
22441 ? hlinfo->mouse_face_end_col
22442 : row->used[TEXT_AREA];
22446 /* Compute overhangs for all glyph strings. */
22447 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
22448 for (s = head; s; s = s->next)
22449 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
22451 /* Prepend glyph strings for glyphs in front of the first glyph
22452 string that are overwritten because of the first glyph
22453 string's left overhang. The background of all strings
22454 prepended must be drawn because the first glyph string
22455 draws over it. */
22456 i = left_overwritten (head);
22457 if (i >= 0)
22459 enum draw_glyphs_face overlap_hl;
22461 /* If this row contains mouse highlighting, attempt to draw
22462 the overlapped glyphs with the correct highlight. This
22463 code fails if the overlap encompasses more than one glyph
22464 and mouse-highlight spans only some of these glyphs.
22465 However, making it work perfectly involves a lot more
22466 code, and I don't know if the pathological case occurs in
22467 practice, so we'll stick to this for now. --- cyd */
22468 if (check_mouse_face
22469 && mouse_beg_col < start && mouse_end_col > i)
22470 overlap_hl = DRAW_MOUSE_FACE;
22471 else
22472 overlap_hl = DRAW_NORMAL_TEXT;
22474 j = i;
22475 BUILD_GLYPH_STRINGS (j, start, h, t,
22476 overlap_hl, dummy_x, last_x);
22477 start = i;
22478 compute_overhangs_and_x (t, head->x, 1);
22479 prepend_glyph_string_lists (&head, &tail, h, t);
22480 clip_head = head;
22483 /* Prepend glyph strings for glyphs in front of the first glyph
22484 string that overwrite that glyph string because of their
22485 right overhang. For these strings, only the foreground must
22486 be drawn, because it draws over the glyph string at `head'.
22487 The background must not be drawn because this would overwrite
22488 right overhangs of preceding glyphs for which no glyph
22489 strings exist. */
22490 i = left_overwriting (head);
22491 if (i >= 0)
22493 enum draw_glyphs_face overlap_hl;
22495 if (check_mouse_face
22496 && mouse_beg_col < start && mouse_end_col > i)
22497 overlap_hl = DRAW_MOUSE_FACE;
22498 else
22499 overlap_hl = DRAW_NORMAL_TEXT;
22501 clip_head = head;
22502 BUILD_GLYPH_STRINGS (i, start, h, t,
22503 overlap_hl, dummy_x, last_x);
22504 for (s = h; s; s = s->next)
22505 s->background_filled_p = 1;
22506 compute_overhangs_and_x (t, head->x, 1);
22507 prepend_glyph_string_lists (&head, &tail, h, t);
22510 /* Append glyphs strings for glyphs following the last glyph
22511 string tail that are overwritten by tail. The background of
22512 these strings has to be drawn because tail's foreground draws
22513 over it. */
22514 i = right_overwritten (tail);
22515 if (i >= 0)
22517 enum draw_glyphs_face overlap_hl;
22519 if (check_mouse_face
22520 && mouse_beg_col < i && mouse_end_col > end)
22521 overlap_hl = DRAW_MOUSE_FACE;
22522 else
22523 overlap_hl = DRAW_NORMAL_TEXT;
22525 BUILD_GLYPH_STRINGS (end, i, h, t,
22526 overlap_hl, x, last_x);
22527 /* Because BUILD_GLYPH_STRINGS updates the first argument,
22528 we don't have `end = i;' here. */
22529 compute_overhangs_and_x (h, tail->x + tail->width, 0);
22530 append_glyph_string_lists (&head, &tail, h, t);
22531 clip_tail = tail;
22534 /* Append glyph strings for glyphs following the last glyph
22535 string tail that overwrite tail. The foreground of such
22536 glyphs has to be drawn because it writes into the background
22537 of tail. The background must not be drawn because it could
22538 paint over the foreground of following glyphs. */
22539 i = right_overwriting (tail);
22540 if (i >= 0)
22542 enum draw_glyphs_face overlap_hl;
22543 if (check_mouse_face
22544 && mouse_beg_col < i && mouse_end_col > end)
22545 overlap_hl = DRAW_MOUSE_FACE;
22546 else
22547 overlap_hl = DRAW_NORMAL_TEXT;
22549 clip_tail = tail;
22550 i++; /* We must include the Ith glyph. */
22551 BUILD_GLYPH_STRINGS (end, i, h, t,
22552 overlap_hl, x, last_x);
22553 for (s = h; s; s = s->next)
22554 s->background_filled_p = 1;
22555 compute_overhangs_and_x (h, tail->x + tail->width, 0);
22556 append_glyph_string_lists (&head, &tail, h, t);
22558 if (clip_head || clip_tail)
22559 for (s = head; s; s = s->next)
22561 s->clip_head = clip_head;
22562 s->clip_tail = clip_tail;
22566 /* Draw all strings. */
22567 for (s = head; s; s = s->next)
22568 FRAME_RIF (f)->draw_glyph_string (s);
22570 #ifndef HAVE_NS
22571 /* When focus a sole frame and move horizontally, this sets on_p to 0
22572 causing a failure to erase prev cursor position. */
22573 if (area == TEXT_AREA
22574 && !row->full_width_p
22575 /* When drawing overlapping rows, only the glyph strings'
22576 foreground is drawn, which doesn't erase a cursor
22577 completely. */
22578 && !overlaps)
22580 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
22581 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
22582 : (tail ? tail->x + tail->background_width : x));
22583 x0 -= area_left;
22584 x1 -= area_left;
22586 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
22587 row->y, MATRIX_ROW_BOTTOM_Y (row));
22589 #endif
22591 /* Value is the x-position up to which drawn, relative to AREA of W.
22592 This doesn't include parts drawn because of overhangs. */
22593 if (row->full_width_p)
22594 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
22595 else
22596 x_reached -= area_left;
22598 RELEASE_HDC (hdc, f);
22600 return x_reached;
22603 /* Expand row matrix if too narrow. Don't expand if area
22604 is not present. */
22606 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
22608 if (!fonts_changed_p \
22609 && (it->glyph_row->glyphs[area] \
22610 < it->glyph_row->glyphs[area + 1])) \
22612 it->w->ncols_scale_factor++; \
22613 fonts_changed_p = 1; \
22617 /* Store one glyph for IT->char_to_display in IT->glyph_row.
22618 Called from x_produce_glyphs when IT->glyph_row is non-null. */
22620 static inline void
22621 append_glyph (struct it *it)
22623 struct glyph *glyph;
22624 enum glyph_row_area area = it->area;
22626 xassert (it->glyph_row);
22627 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
22629 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22630 if (glyph < it->glyph_row->glyphs[area + 1])
22632 /* If the glyph row is reversed, we need to prepend the glyph
22633 rather than append it. */
22634 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22636 struct glyph *g;
22638 /* Make room for the additional glyph. */
22639 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22640 g[1] = *g;
22641 glyph = it->glyph_row->glyphs[area];
22643 glyph->charpos = CHARPOS (it->position);
22644 glyph->object = it->object;
22645 if (it->pixel_width > 0)
22647 glyph->pixel_width = it->pixel_width;
22648 glyph->padding_p = 0;
22650 else
22652 /* Assure at least 1-pixel width. Otherwise, cursor can't
22653 be displayed correctly. */
22654 glyph->pixel_width = 1;
22655 glyph->padding_p = 1;
22657 glyph->ascent = it->ascent;
22658 glyph->descent = it->descent;
22659 glyph->voffset = it->voffset;
22660 glyph->type = CHAR_GLYPH;
22661 glyph->avoid_cursor_p = it->avoid_cursor_p;
22662 glyph->multibyte_p = it->multibyte_p;
22663 glyph->left_box_line_p = it->start_of_box_run_p;
22664 glyph->right_box_line_p = it->end_of_box_run_p;
22665 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22666 || it->phys_descent > it->descent);
22667 glyph->glyph_not_available_p = it->glyph_not_available_p;
22668 glyph->face_id = it->face_id;
22669 glyph->u.ch = it->char_to_display;
22670 glyph->slice.img = null_glyph_slice;
22671 glyph->font_type = FONT_TYPE_UNKNOWN;
22672 if (it->bidi_p)
22674 glyph->resolved_level = it->bidi_it.resolved_level;
22675 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22676 abort ();
22677 glyph->bidi_type = it->bidi_it.type;
22679 else
22681 glyph->resolved_level = 0;
22682 glyph->bidi_type = UNKNOWN_BT;
22684 ++it->glyph_row->used[area];
22686 else
22687 IT_EXPAND_MATRIX_WIDTH (it, area);
22690 /* Store one glyph for the composition IT->cmp_it.id in
22691 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
22692 non-null. */
22694 static inline void
22695 append_composite_glyph (struct it *it)
22697 struct glyph *glyph;
22698 enum glyph_row_area area = it->area;
22700 xassert (it->glyph_row);
22702 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22703 if (glyph < it->glyph_row->glyphs[area + 1])
22705 /* If the glyph row is reversed, we need to prepend the glyph
22706 rather than append it. */
22707 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
22709 struct glyph *g;
22711 /* Make room for the new glyph. */
22712 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
22713 g[1] = *g;
22714 glyph = it->glyph_row->glyphs[it->area];
22716 glyph->charpos = it->cmp_it.charpos;
22717 glyph->object = it->object;
22718 glyph->pixel_width = it->pixel_width;
22719 glyph->ascent = it->ascent;
22720 glyph->descent = it->descent;
22721 glyph->voffset = it->voffset;
22722 glyph->type = COMPOSITE_GLYPH;
22723 if (it->cmp_it.ch < 0)
22725 glyph->u.cmp.automatic = 0;
22726 glyph->u.cmp.id = it->cmp_it.id;
22727 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
22729 else
22731 glyph->u.cmp.automatic = 1;
22732 glyph->u.cmp.id = it->cmp_it.id;
22733 glyph->slice.cmp.from = it->cmp_it.from;
22734 glyph->slice.cmp.to = it->cmp_it.to - 1;
22736 glyph->avoid_cursor_p = it->avoid_cursor_p;
22737 glyph->multibyte_p = it->multibyte_p;
22738 glyph->left_box_line_p = it->start_of_box_run_p;
22739 glyph->right_box_line_p = it->end_of_box_run_p;
22740 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22741 || it->phys_descent > it->descent);
22742 glyph->padding_p = 0;
22743 glyph->glyph_not_available_p = 0;
22744 glyph->face_id = it->face_id;
22745 glyph->font_type = FONT_TYPE_UNKNOWN;
22746 if (it->bidi_p)
22748 glyph->resolved_level = it->bidi_it.resolved_level;
22749 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22750 abort ();
22751 glyph->bidi_type = it->bidi_it.type;
22753 ++it->glyph_row->used[area];
22755 else
22756 IT_EXPAND_MATRIX_WIDTH (it, area);
22760 /* Change IT->ascent and IT->height according to the setting of
22761 IT->voffset. */
22763 static inline void
22764 take_vertical_position_into_account (struct it *it)
22766 if (it->voffset)
22768 if (it->voffset < 0)
22769 /* Increase the ascent so that we can display the text higher
22770 in the line. */
22771 it->ascent -= it->voffset;
22772 else
22773 /* Increase the descent so that we can display the text lower
22774 in the line. */
22775 it->descent += it->voffset;
22780 /* Produce glyphs/get display metrics for the image IT is loaded with.
22781 See the description of struct display_iterator in dispextern.h for
22782 an overview of struct display_iterator. */
22784 static void
22785 produce_image_glyph (struct it *it)
22787 struct image *img;
22788 struct face *face;
22789 int glyph_ascent, crop;
22790 struct glyph_slice slice;
22792 xassert (it->what == IT_IMAGE);
22794 face = FACE_FROM_ID (it->f, it->face_id);
22795 xassert (face);
22796 /* Make sure X resources of the face is loaded. */
22797 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22799 if (it->image_id < 0)
22801 /* Fringe bitmap. */
22802 it->ascent = it->phys_ascent = 0;
22803 it->descent = it->phys_descent = 0;
22804 it->pixel_width = 0;
22805 it->nglyphs = 0;
22806 return;
22809 img = IMAGE_FROM_ID (it->f, it->image_id);
22810 xassert (img);
22811 /* Make sure X resources of the image is loaded. */
22812 prepare_image_for_display (it->f, img);
22814 slice.x = slice.y = 0;
22815 slice.width = img->width;
22816 slice.height = img->height;
22818 if (INTEGERP (it->slice.x))
22819 slice.x = XINT (it->slice.x);
22820 else if (FLOATP (it->slice.x))
22821 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
22823 if (INTEGERP (it->slice.y))
22824 slice.y = XINT (it->slice.y);
22825 else if (FLOATP (it->slice.y))
22826 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
22828 if (INTEGERP (it->slice.width))
22829 slice.width = XINT (it->slice.width);
22830 else if (FLOATP (it->slice.width))
22831 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
22833 if (INTEGERP (it->slice.height))
22834 slice.height = XINT (it->slice.height);
22835 else if (FLOATP (it->slice.height))
22836 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
22838 if (slice.x >= img->width)
22839 slice.x = img->width;
22840 if (slice.y >= img->height)
22841 slice.y = img->height;
22842 if (slice.x + slice.width >= img->width)
22843 slice.width = img->width - slice.x;
22844 if (slice.y + slice.height > img->height)
22845 slice.height = img->height - slice.y;
22847 if (slice.width == 0 || slice.height == 0)
22848 return;
22850 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
22852 it->descent = slice.height - glyph_ascent;
22853 if (slice.y == 0)
22854 it->descent += img->vmargin;
22855 if (slice.y + slice.height == img->height)
22856 it->descent += img->vmargin;
22857 it->phys_descent = it->descent;
22859 it->pixel_width = slice.width;
22860 if (slice.x == 0)
22861 it->pixel_width += img->hmargin;
22862 if (slice.x + slice.width == img->width)
22863 it->pixel_width += img->hmargin;
22865 /* It's quite possible for images to have an ascent greater than
22866 their height, so don't get confused in that case. */
22867 if (it->descent < 0)
22868 it->descent = 0;
22870 it->nglyphs = 1;
22872 if (face->box != FACE_NO_BOX)
22874 if (face->box_line_width > 0)
22876 if (slice.y == 0)
22877 it->ascent += face->box_line_width;
22878 if (slice.y + slice.height == img->height)
22879 it->descent += face->box_line_width;
22882 if (it->start_of_box_run_p && slice.x == 0)
22883 it->pixel_width += eabs (face->box_line_width);
22884 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
22885 it->pixel_width += eabs (face->box_line_width);
22888 take_vertical_position_into_account (it);
22890 /* Automatically crop wide image glyphs at right edge so we can
22891 draw the cursor on same display row. */
22892 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
22893 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
22895 it->pixel_width -= crop;
22896 slice.width -= crop;
22899 if (it->glyph_row)
22901 struct glyph *glyph;
22902 enum glyph_row_area area = it->area;
22904 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22905 if (glyph < it->glyph_row->glyphs[area + 1])
22907 glyph->charpos = CHARPOS (it->position);
22908 glyph->object = it->object;
22909 glyph->pixel_width = it->pixel_width;
22910 glyph->ascent = glyph_ascent;
22911 glyph->descent = it->descent;
22912 glyph->voffset = it->voffset;
22913 glyph->type = IMAGE_GLYPH;
22914 glyph->avoid_cursor_p = it->avoid_cursor_p;
22915 glyph->multibyte_p = it->multibyte_p;
22916 glyph->left_box_line_p = it->start_of_box_run_p;
22917 glyph->right_box_line_p = it->end_of_box_run_p;
22918 glyph->overlaps_vertically_p = 0;
22919 glyph->padding_p = 0;
22920 glyph->glyph_not_available_p = 0;
22921 glyph->face_id = it->face_id;
22922 glyph->u.img_id = img->id;
22923 glyph->slice.img = slice;
22924 glyph->font_type = FONT_TYPE_UNKNOWN;
22925 if (it->bidi_p)
22927 glyph->resolved_level = it->bidi_it.resolved_level;
22928 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22929 abort ();
22930 glyph->bidi_type = it->bidi_it.type;
22932 ++it->glyph_row->used[area];
22934 else
22935 IT_EXPAND_MATRIX_WIDTH (it, area);
22940 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
22941 of the glyph, WIDTH and HEIGHT are the width and height of the
22942 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
22944 static void
22945 append_stretch_glyph (struct it *it, Lisp_Object object,
22946 int width, int height, int ascent)
22948 struct glyph *glyph;
22949 enum glyph_row_area area = it->area;
22951 xassert (ascent >= 0 && ascent <= height);
22953 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22954 if (glyph < it->glyph_row->glyphs[area + 1])
22956 /* If the glyph row is reversed, we need to prepend the glyph
22957 rather than append it. */
22958 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22960 struct glyph *g;
22962 /* Make room for the additional glyph. */
22963 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22964 g[1] = *g;
22965 glyph = it->glyph_row->glyphs[area];
22967 glyph->charpos = CHARPOS (it->position);
22968 glyph->object = object;
22969 glyph->pixel_width = width;
22970 glyph->ascent = ascent;
22971 glyph->descent = height - ascent;
22972 glyph->voffset = it->voffset;
22973 glyph->type = STRETCH_GLYPH;
22974 glyph->avoid_cursor_p = it->avoid_cursor_p;
22975 glyph->multibyte_p = it->multibyte_p;
22976 glyph->left_box_line_p = it->start_of_box_run_p;
22977 glyph->right_box_line_p = it->end_of_box_run_p;
22978 glyph->overlaps_vertically_p = 0;
22979 glyph->padding_p = 0;
22980 glyph->glyph_not_available_p = 0;
22981 glyph->face_id = it->face_id;
22982 glyph->u.stretch.ascent = ascent;
22983 glyph->u.stretch.height = height;
22984 glyph->slice.img = null_glyph_slice;
22985 glyph->font_type = FONT_TYPE_UNKNOWN;
22986 if (it->bidi_p)
22988 glyph->resolved_level = it->bidi_it.resolved_level;
22989 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22990 abort ();
22991 glyph->bidi_type = it->bidi_it.type;
22993 else
22995 glyph->resolved_level = 0;
22996 glyph->bidi_type = UNKNOWN_BT;
22998 ++it->glyph_row->used[area];
23000 else
23001 IT_EXPAND_MATRIX_WIDTH (it, area);
23005 /* Produce a stretch glyph for iterator IT. IT->object is the value
23006 of the glyph property displayed. The value must be a list
23007 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23008 being recognized:
23010 1. `:width WIDTH' specifies that the space should be WIDTH *
23011 canonical char width wide. WIDTH may be an integer or floating
23012 point number.
23014 2. `:relative-width FACTOR' specifies that the width of the stretch
23015 should be computed from the width of the first character having the
23016 `glyph' property, and should be FACTOR times that width.
23018 3. `:align-to HPOS' specifies that the space should be wide enough
23019 to reach HPOS, a value in canonical character units.
23021 Exactly one of the above pairs must be present.
23023 4. `:height HEIGHT' specifies that the height of the stretch produced
23024 should be HEIGHT, measured in canonical character units.
23026 5. `:relative-height FACTOR' specifies that the height of the
23027 stretch should be FACTOR times the height of the characters having
23028 the glyph property.
23030 Either none or exactly one of 4 or 5 must be present.
23032 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23033 of the stretch should be used for the ascent of the stretch.
23034 ASCENT must be in the range 0 <= ASCENT <= 100. */
23036 static void
23037 produce_stretch_glyph (struct it *it)
23039 /* (space :width WIDTH :height HEIGHT ...) */
23040 Lisp_Object prop, plist;
23041 int width = 0, height = 0, align_to = -1;
23042 int zero_width_ok_p = 0, zero_height_ok_p = 0;
23043 int ascent = 0;
23044 double tem;
23045 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23046 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
23048 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23050 /* List should start with `space'. */
23051 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
23052 plist = XCDR (it->object);
23054 /* Compute the width of the stretch. */
23055 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
23056 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
23058 /* Absolute width `:width WIDTH' specified and valid. */
23059 zero_width_ok_p = 1;
23060 width = (int)tem;
23062 else if (prop = Fplist_get (plist, QCrelative_width),
23063 NUMVAL (prop) > 0)
23065 /* Relative width `:relative-width FACTOR' specified and valid.
23066 Compute the width of the characters having the `glyph'
23067 property. */
23068 struct it it2;
23069 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
23071 it2 = *it;
23072 if (it->multibyte_p)
23073 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
23074 else
23076 it2.c = it2.char_to_display = *p, it2.len = 1;
23077 if (! ASCII_CHAR_P (it2.c))
23078 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
23081 it2.glyph_row = NULL;
23082 it2.what = IT_CHARACTER;
23083 x_produce_glyphs (&it2);
23084 width = NUMVAL (prop) * it2.pixel_width;
23086 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
23087 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
23089 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
23090 align_to = (align_to < 0
23092 : align_to - window_box_left_offset (it->w, TEXT_AREA));
23093 else if (align_to < 0)
23094 align_to = window_box_left_offset (it->w, TEXT_AREA);
23095 width = max (0, (int)tem + align_to - it->current_x);
23096 zero_width_ok_p = 1;
23098 else
23099 /* Nothing specified -> width defaults to canonical char width. */
23100 width = FRAME_COLUMN_WIDTH (it->f);
23102 if (width <= 0 && (width < 0 || !zero_width_ok_p))
23103 width = 1;
23105 /* Compute height. */
23106 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
23107 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23109 height = (int)tem;
23110 zero_height_ok_p = 1;
23112 else if (prop = Fplist_get (plist, QCrelative_height),
23113 NUMVAL (prop) > 0)
23114 height = FONT_HEIGHT (font) * NUMVAL (prop);
23115 else
23116 height = FONT_HEIGHT (font);
23118 if (height <= 0 && (height < 0 || !zero_height_ok_p))
23119 height = 1;
23121 /* Compute percentage of height used for ascent. If
23122 `:ascent ASCENT' is present and valid, use that. Otherwise,
23123 derive the ascent from the font in use. */
23124 if (prop = Fplist_get (plist, QCascent),
23125 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
23126 ascent = height * NUMVAL (prop) / 100.0;
23127 else if (!NILP (prop)
23128 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23129 ascent = min (max (0, (int)tem), height);
23130 else
23131 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
23133 if (width > 0 && it->line_wrap != TRUNCATE
23134 && it->current_x + width > it->last_visible_x)
23135 width = it->last_visible_x - it->current_x - 1;
23137 if (width > 0 && height > 0 && it->glyph_row)
23139 Lisp_Object object = it->stack[it->sp - 1].string;
23140 if (!STRINGP (object))
23141 object = it->w->buffer;
23142 append_stretch_glyph (it, object, width, height, ascent);
23145 it->pixel_width = width;
23146 it->ascent = it->phys_ascent = ascent;
23147 it->descent = it->phys_descent = height - it->ascent;
23148 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
23150 take_vertical_position_into_account (it);
23153 /* Calculate line-height and line-spacing properties.
23154 An integer value specifies explicit pixel value.
23155 A float value specifies relative value to current face height.
23156 A cons (float . face-name) specifies relative value to
23157 height of specified face font.
23159 Returns height in pixels, or nil. */
23162 static Lisp_Object
23163 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
23164 int boff, int override)
23166 Lisp_Object face_name = Qnil;
23167 int ascent, descent, height;
23169 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
23170 return val;
23172 if (CONSP (val))
23174 face_name = XCAR (val);
23175 val = XCDR (val);
23176 if (!NUMBERP (val))
23177 val = make_number (1);
23178 if (NILP (face_name))
23180 height = it->ascent + it->descent;
23181 goto scale;
23185 if (NILP (face_name))
23187 font = FRAME_FONT (it->f);
23188 boff = FRAME_BASELINE_OFFSET (it->f);
23190 else if (EQ (face_name, Qt))
23192 override = 0;
23194 else
23196 int face_id;
23197 struct face *face;
23199 face_id = lookup_named_face (it->f, face_name, 0);
23200 if (face_id < 0)
23201 return make_number (-1);
23203 face = FACE_FROM_ID (it->f, face_id);
23204 font = face->font;
23205 if (font == NULL)
23206 return make_number (-1);
23207 boff = font->baseline_offset;
23208 if (font->vertical_centering)
23209 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23212 ascent = FONT_BASE (font) + boff;
23213 descent = FONT_DESCENT (font) - boff;
23215 if (override)
23217 it->override_ascent = ascent;
23218 it->override_descent = descent;
23219 it->override_boff = boff;
23222 height = ascent + descent;
23224 scale:
23225 if (FLOATP (val))
23226 height = (int)(XFLOAT_DATA (val) * height);
23227 else if (INTEGERP (val))
23228 height *= XINT (val);
23230 return make_number (height);
23234 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
23235 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
23236 and only if this is for a character for which no font was found.
23238 If the display method (it->glyphless_method) is
23239 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
23240 length of the acronym or the hexadecimal string, UPPER_XOFF and
23241 UPPER_YOFF are pixel offsets for the upper part of the string,
23242 LOWER_XOFF and LOWER_YOFF are for the lower part.
23244 For the other display methods, LEN through LOWER_YOFF are zero. */
23246 static void
23247 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
23248 short upper_xoff, short upper_yoff,
23249 short lower_xoff, short lower_yoff)
23251 struct glyph *glyph;
23252 enum glyph_row_area area = it->area;
23254 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23255 if (glyph < it->glyph_row->glyphs[area + 1])
23257 /* If the glyph row is reversed, we need to prepend the glyph
23258 rather than append it. */
23259 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23261 struct glyph *g;
23263 /* Make room for the additional glyph. */
23264 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23265 g[1] = *g;
23266 glyph = it->glyph_row->glyphs[area];
23268 glyph->charpos = CHARPOS (it->position);
23269 glyph->object = it->object;
23270 glyph->pixel_width = it->pixel_width;
23271 glyph->ascent = it->ascent;
23272 glyph->descent = it->descent;
23273 glyph->voffset = it->voffset;
23274 glyph->type = GLYPHLESS_GLYPH;
23275 glyph->u.glyphless.method = it->glyphless_method;
23276 glyph->u.glyphless.for_no_font = for_no_font;
23277 glyph->u.glyphless.len = len;
23278 glyph->u.glyphless.ch = it->c;
23279 glyph->slice.glyphless.upper_xoff = upper_xoff;
23280 glyph->slice.glyphless.upper_yoff = upper_yoff;
23281 glyph->slice.glyphless.lower_xoff = lower_xoff;
23282 glyph->slice.glyphless.lower_yoff = lower_yoff;
23283 glyph->avoid_cursor_p = it->avoid_cursor_p;
23284 glyph->multibyte_p = it->multibyte_p;
23285 glyph->left_box_line_p = it->start_of_box_run_p;
23286 glyph->right_box_line_p = it->end_of_box_run_p;
23287 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23288 || it->phys_descent > it->descent);
23289 glyph->padding_p = 0;
23290 glyph->glyph_not_available_p = 0;
23291 glyph->face_id = face_id;
23292 glyph->font_type = FONT_TYPE_UNKNOWN;
23293 if (it->bidi_p)
23295 glyph->resolved_level = it->bidi_it.resolved_level;
23296 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23297 abort ();
23298 glyph->bidi_type = it->bidi_it.type;
23300 ++it->glyph_row->used[area];
23302 else
23303 IT_EXPAND_MATRIX_WIDTH (it, area);
23307 /* Produce a glyph for a glyphless character for iterator IT.
23308 IT->glyphless_method specifies which method to use for displaying
23309 the character. See the description of enum
23310 glyphless_display_method in dispextern.h for the detail.
23312 FOR_NO_FONT is nonzero if and only if this is for a character for
23313 which no font was found. ACRONYM, if non-nil, is an acronym string
23314 for the character. */
23316 static void
23317 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
23319 int face_id;
23320 struct face *face;
23321 struct font *font;
23322 int base_width, base_height, width, height;
23323 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
23324 int len;
23326 /* Get the metrics of the base font. We always refer to the current
23327 ASCII face. */
23328 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
23329 font = face->font ? face->font : FRAME_FONT (it->f);
23330 it->ascent = FONT_BASE (font) + font->baseline_offset;
23331 it->descent = FONT_DESCENT (font) - font->baseline_offset;
23332 base_height = it->ascent + it->descent;
23333 base_width = font->average_width;
23335 /* Get a face ID for the glyph by utilizing a cache (the same way as
23336 done for `escape-glyph' in get_next_display_element). */
23337 if (it->f == last_glyphless_glyph_frame
23338 && it->face_id == last_glyphless_glyph_face_id)
23340 face_id = last_glyphless_glyph_merged_face_id;
23342 else
23344 /* Merge the `glyphless-char' face into the current face. */
23345 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
23346 last_glyphless_glyph_frame = it->f;
23347 last_glyphless_glyph_face_id = it->face_id;
23348 last_glyphless_glyph_merged_face_id = face_id;
23351 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
23353 it->pixel_width = THIN_SPACE_WIDTH;
23354 len = 0;
23355 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23357 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
23359 width = CHAR_WIDTH (it->c);
23360 if (width == 0)
23361 width = 1;
23362 else if (width > 4)
23363 width = 4;
23364 it->pixel_width = base_width * width;
23365 len = 0;
23366 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23368 else
23370 char buf[7];
23371 const char *str;
23372 unsigned int code[6];
23373 int upper_len;
23374 int ascent, descent;
23375 struct font_metrics metrics_upper, metrics_lower;
23377 face = FACE_FROM_ID (it->f, face_id);
23378 font = face->font ? face->font : FRAME_FONT (it->f);
23379 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23381 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
23383 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
23384 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
23385 if (CONSP (acronym))
23386 acronym = XCAR (acronym);
23387 str = STRINGP (acronym) ? SSDATA (acronym) : "";
23389 else
23391 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
23392 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
23393 str = buf;
23395 for (len = 0; str[len] && ASCII_BYTE_P (str[len]); len++)
23396 code[len] = font->driver->encode_char (font, str[len]);
23397 upper_len = (len + 1) / 2;
23398 font->driver->text_extents (font, code, upper_len,
23399 &metrics_upper);
23400 font->driver->text_extents (font, code + upper_len, len - upper_len,
23401 &metrics_lower);
23405 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
23406 width = max (metrics_upper.width, metrics_lower.width) + 4;
23407 upper_xoff = upper_yoff = 2; /* the typical case */
23408 if (base_width >= width)
23410 /* Align the upper to the left, the lower to the right. */
23411 it->pixel_width = base_width;
23412 lower_xoff = base_width - 2 - metrics_lower.width;
23414 else
23416 /* Center the shorter one. */
23417 it->pixel_width = width;
23418 if (metrics_upper.width >= metrics_lower.width)
23419 lower_xoff = (width - metrics_lower.width) / 2;
23420 else
23422 /* FIXME: This code doesn't look right. It formerly was
23423 missing the "lower_xoff = 0;", which couldn't have
23424 been right since it left lower_xoff uninitialized. */
23425 lower_xoff = 0;
23426 upper_xoff = (width - metrics_upper.width) / 2;
23430 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
23431 top, bottom, and between upper and lower strings. */
23432 height = (metrics_upper.ascent + metrics_upper.descent
23433 + metrics_lower.ascent + metrics_lower.descent) + 5;
23434 /* Center vertically.
23435 H:base_height, D:base_descent
23436 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
23438 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
23439 descent = D - H/2 + h/2;
23440 lower_yoff = descent - 2 - ld;
23441 upper_yoff = lower_yoff - la - 1 - ud; */
23442 ascent = - (it->descent - (base_height + height + 1) / 2);
23443 descent = it->descent - (base_height - height) / 2;
23444 lower_yoff = descent - 2 - metrics_lower.descent;
23445 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
23446 - metrics_upper.descent);
23447 /* Don't make the height shorter than the base height. */
23448 if (height > base_height)
23450 it->ascent = ascent;
23451 it->descent = descent;
23455 it->phys_ascent = it->ascent;
23456 it->phys_descent = it->descent;
23457 if (it->glyph_row)
23458 append_glyphless_glyph (it, face_id, for_no_font, len,
23459 upper_xoff, upper_yoff,
23460 lower_xoff, lower_yoff);
23461 it->nglyphs = 1;
23462 take_vertical_position_into_account (it);
23466 /* RIF:
23467 Produce glyphs/get display metrics for the display element IT is
23468 loaded with. See the description of struct it in dispextern.h
23469 for an overview of struct it. */
23471 void
23472 x_produce_glyphs (struct it *it)
23474 int extra_line_spacing = it->extra_line_spacing;
23476 it->glyph_not_available_p = 0;
23478 if (it->what == IT_CHARACTER)
23480 XChar2b char2b;
23481 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23482 struct font *font = face->font;
23483 struct font_metrics *pcm = NULL;
23484 int boff; /* baseline offset */
23486 if (font == NULL)
23488 /* When no suitable font is found, display this character by
23489 the method specified in the first extra slot of
23490 Vglyphless_char_display. */
23491 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
23493 xassert (it->what == IT_GLYPHLESS);
23494 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
23495 goto done;
23498 boff = font->baseline_offset;
23499 if (font->vertical_centering)
23500 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23502 if (it->char_to_display != '\n' && it->char_to_display != '\t')
23504 int stretched_p;
23506 it->nglyphs = 1;
23508 if (it->override_ascent >= 0)
23510 it->ascent = it->override_ascent;
23511 it->descent = it->override_descent;
23512 boff = it->override_boff;
23514 else
23516 it->ascent = FONT_BASE (font) + boff;
23517 it->descent = FONT_DESCENT (font) - boff;
23520 if (get_char_glyph_code (it->char_to_display, font, &char2b))
23522 pcm = get_per_char_metric (font, &char2b);
23523 if (pcm->width == 0
23524 && pcm->rbearing == 0 && pcm->lbearing == 0)
23525 pcm = NULL;
23528 if (pcm)
23530 it->phys_ascent = pcm->ascent + boff;
23531 it->phys_descent = pcm->descent - boff;
23532 it->pixel_width = pcm->width;
23534 else
23536 it->glyph_not_available_p = 1;
23537 it->phys_ascent = it->ascent;
23538 it->phys_descent = it->descent;
23539 it->pixel_width = font->space_width;
23542 if (it->constrain_row_ascent_descent_p)
23544 if (it->descent > it->max_descent)
23546 it->ascent += it->descent - it->max_descent;
23547 it->descent = it->max_descent;
23549 if (it->ascent > it->max_ascent)
23551 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
23552 it->ascent = it->max_ascent;
23554 it->phys_ascent = min (it->phys_ascent, it->ascent);
23555 it->phys_descent = min (it->phys_descent, it->descent);
23556 extra_line_spacing = 0;
23559 /* If this is a space inside a region of text with
23560 `space-width' property, change its width. */
23561 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
23562 if (stretched_p)
23563 it->pixel_width *= XFLOATINT (it->space_width);
23565 /* If face has a box, add the box thickness to the character
23566 height. If character has a box line to the left and/or
23567 right, add the box line width to the character's width. */
23568 if (face->box != FACE_NO_BOX)
23570 int thick = face->box_line_width;
23572 if (thick > 0)
23574 it->ascent += thick;
23575 it->descent += thick;
23577 else
23578 thick = -thick;
23580 if (it->start_of_box_run_p)
23581 it->pixel_width += thick;
23582 if (it->end_of_box_run_p)
23583 it->pixel_width += thick;
23586 /* If face has an overline, add the height of the overline
23587 (1 pixel) and a 1 pixel margin to the character height. */
23588 if (face->overline_p)
23589 it->ascent += overline_margin;
23591 if (it->constrain_row_ascent_descent_p)
23593 if (it->ascent > it->max_ascent)
23594 it->ascent = it->max_ascent;
23595 if (it->descent > it->max_descent)
23596 it->descent = it->max_descent;
23599 take_vertical_position_into_account (it);
23601 /* If we have to actually produce glyphs, do it. */
23602 if (it->glyph_row)
23604 if (stretched_p)
23606 /* Translate a space with a `space-width' property
23607 into a stretch glyph. */
23608 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
23609 / FONT_HEIGHT (font));
23610 append_stretch_glyph (it, it->object, it->pixel_width,
23611 it->ascent + it->descent, ascent);
23613 else
23614 append_glyph (it);
23616 /* If characters with lbearing or rbearing are displayed
23617 in this line, record that fact in a flag of the
23618 glyph row. This is used to optimize X output code. */
23619 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
23620 it->glyph_row->contains_overlapping_glyphs_p = 1;
23622 if (! stretched_p && it->pixel_width == 0)
23623 /* We assure that all visible glyphs have at least 1-pixel
23624 width. */
23625 it->pixel_width = 1;
23627 else if (it->char_to_display == '\n')
23629 /* A newline has no width, but we need the height of the
23630 line. But if previous part of the line sets a height,
23631 don't increase that height */
23633 Lisp_Object height;
23634 Lisp_Object total_height = Qnil;
23636 it->override_ascent = -1;
23637 it->pixel_width = 0;
23638 it->nglyphs = 0;
23640 height = get_it_property (it, Qline_height);
23641 /* Split (line-height total-height) list */
23642 if (CONSP (height)
23643 && CONSP (XCDR (height))
23644 && NILP (XCDR (XCDR (height))))
23646 total_height = XCAR (XCDR (height));
23647 height = XCAR (height);
23649 height = calc_line_height_property (it, height, font, boff, 1);
23651 if (it->override_ascent >= 0)
23653 it->ascent = it->override_ascent;
23654 it->descent = it->override_descent;
23655 boff = it->override_boff;
23657 else
23659 it->ascent = FONT_BASE (font) + boff;
23660 it->descent = FONT_DESCENT (font) - boff;
23663 if (EQ (height, Qt))
23665 if (it->descent > it->max_descent)
23667 it->ascent += it->descent - it->max_descent;
23668 it->descent = it->max_descent;
23670 if (it->ascent > it->max_ascent)
23672 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
23673 it->ascent = it->max_ascent;
23675 it->phys_ascent = min (it->phys_ascent, it->ascent);
23676 it->phys_descent = min (it->phys_descent, it->descent);
23677 it->constrain_row_ascent_descent_p = 1;
23678 extra_line_spacing = 0;
23680 else
23682 Lisp_Object spacing;
23684 it->phys_ascent = it->ascent;
23685 it->phys_descent = it->descent;
23687 if ((it->max_ascent > 0 || it->max_descent > 0)
23688 && face->box != FACE_NO_BOX
23689 && face->box_line_width > 0)
23691 it->ascent += face->box_line_width;
23692 it->descent += face->box_line_width;
23694 if (!NILP (height)
23695 && XINT (height) > it->ascent + it->descent)
23696 it->ascent = XINT (height) - it->descent;
23698 if (!NILP (total_height))
23699 spacing = calc_line_height_property (it, total_height, font, boff, 0);
23700 else
23702 spacing = get_it_property (it, Qline_spacing);
23703 spacing = calc_line_height_property (it, spacing, font, boff, 0);
23705 if (INTEGERP (spacing))
23707 extra_line_spacing = XINT (spacing);
23708 if (!NILP (total_height))
23709 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
23713 else /* i.e. (it->char_to_display == '\t') */
23715 if (font->space_width > 0)
23717 int tab_width = it->tab_width * font->space_width;
23718 int x = it->current_x + it->continuation_lines_width;
23719 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
23721 /* If the distance from the current position to the next tab
23722 stop is less than a space character width, use the
23723 tab stop after that. */
23724 if (next_tab_x - x < font->space_width)
23725 next_tab_x += tab_width;
23727 it->pixel_width = next_tab_x - x;
23728 it->nglyphs = 1;
23729 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
23730 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
23732 if (it->glyph_row)
23734 append_stretch_glyph (it, it->object, it->pixel_width,
23735 it->ascent + it->descent, it->ascent);
23738 else
23740 it->pixel_width = 0;
23741 it->nglyphs = 1;
23745 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
23747 /* A static composition.
23749 Note: A composition is represented as one glyph in the
23750 glyph matrix. There are no padding glyphs.
23752 Important note: pixel_width, ascent, and descent are the
23753 values of what is drawn by draw_glyphs (i.e. the values of
23754 the overall glyphs composed). */
23755 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23756 int boff; /* baseline offset */
23757 struct composition *cmp = composition_table[it->cmp_it.id];
23758 int glyph_len = cmp->glyph_len;
23759 struct font *font = face->font;
23761 it->nglyphs = 1;
23763 /* If we have not yet calculated pixel size data of glyphs of
23764 the composition for the current face font, calculate them
23765 now. Theoretically, we have to check all fonts for the
23766 glyphs, but that requires much time and memory space. So,
23767 here we check only the font of the first glyph. This may
23768 lead to incorrect display, but it's very rare, and C-l
23769 (recenter-top-bottom) can correct the display anyway. */
23770 if (! cmp->font || cmp->font != font)
23772 /* Ascent and descent of the font of the first character
23773 of this composition (adjusted by baseline offset).
23774 Ascent and descent of overall glyphs should not be less
23775 than these, respectively. */
23776 int font_ascent, font_descent, font_height;
23777 /* Bounding box of the overall glyphs. */
23778 int leftmost, rightmost, lowest, highest;
23779 int lbearing, rbearing;
23780 int i, width, ascent, descent;
23781 int left_padded = 0, right_padded = 0;
23782 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
23783 XChar2b char2b;
23784 struct font_metrics *pcm;
23785 int font_not_found_p;
23786 EMACS_INT pos;
23788 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
23789 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
23790 break;
23791 if (glyph_len < cmp->glyph_len)
23792 right_padded = 1;
23793 for (i = 0; i < glyph_len; i++)
23795 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
23796 break;
23797 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
23799 if (i > 0)
23800 left_padded = 1;
23802 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
23803 : IT_CHARPOS (*it));
23804 /* If no suitable font is found, use the default font. */
23805 font_not_found_p = font == NULL;
23806 if (font_not_found_p)
23808 face = face->ascii_face;
23809 font = face->font;
23811 boff = font->baseline_offset;
23812 if (font->vertical_centering)
23813 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23814 font_ascent = FONT_BASE (font) + boff;
23815 font_descent = FONT_DESCENT (font) - boff;
23816 font_height = FONT_HEIGHT (font);
23818 cmp->font = (void *) font;
23820 pcm = NULL;
23821 if (! font_not_found_p)
23823 get_char_face_and_encoding (it->f, c, it->face_id,
23824 &char2b, 0);
23825 pcm = get_per_char_metric (font, &char2b);
23828 /* Initialize the bounding box. */
23829 if (pcm)
23831 width = pcm->width;
23832 ascent = pcm->ascent;
23833 descent = pcm->descent;
23834 lbearing = pcm->lbearing;
23835 rbearing = pcm->rbearing;
23837 else
23839 width = font->space_width;
23840 ascent = FONT_BASE (font);
23841 descent = FONT_DESCENT (font);
23842 lbearing = 0;
23843 rbearing = width;
23846 rightmost = width;
23847 leftmost = 0;
23848 lowest = - descent + boff;
23849 highest = ascent + boff;
23851 if (! font_not_found_p
23852 && font->default_ascent
23853 && CHAR_TABLE_P (Vuse_default_ascent)
23854 && !NILP (Faref (Vuse_default_ascent,
23855 make_number (it->char_to_display))))
23856 highest = font->default_ascent + boff;
23858 /* Draw the first glyph at the normal position. It may be
23859 shifted to right later if some other glyphs are drawn
23860 at the left. */
23861 cmp->offsets[i * 2] = 0;
23862 cmp->offsets[i * 2 + 1] = boff;
23863 cmp->lbearing = lbearing;
23864 cmp->rbearing = rbearing;
23866 /* Set cmp->offsets for the remaining glyphs. */
23867 for (i++; i < glyph_len; i++)
23869 int left, right, btm, top;
23870 int ch = COMPOSITION_GLYPH (cmp, i);
23871 int face_id;
23872 struct face *this_face;
23874 if (ch == '\t')
23875 ch = ' ';
23876 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
23877 this_face = FACE_FROM_ID (it->f, face_id);
23878 font = this_face->font;
23880 if (font == NULL)
23881 pcm = NULL;
23882 else
23884 get_char_face_and_encoding (it->f, ch, face_id,
23885 &char2b, 0);
23886 pcm = get_per_char_metric (font, &char2b);
23888 if (! pcm)
23889 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
23890 else
23892 width = pcm->width;
23893 ascent = pcm->ascent;
23894 descent = pcm->descent;
23895 lbearing = pcm->lbearing;
23896 rbearing = pcm->rbearing;
23897 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
23899 /* Relative composition with or without
23900 alternate chars. */
23901 left = (leftmost + rightmost - width) / 2;
23902 btm = - descent + boff;
23903 if (font->relative_compose
23904 && (! CHAR_TABLE_P (Vignore_relative_composition)
23905 || NILP (Faref (Vignore_relative_composition,
23906 make_number (ch)))))
23909 if (- descent >= font->relative_compose)
23910 /* One extra pixel between two glyphs. */
23911 btm = highest + 1;
23912 else if (ascent <= 0)
23913 /* One extra pixel between two glyphs. */
23914 btm = lowest - 1 - ascent - descent;
23917 else
23919 /* A composition rule is specified by an integer
23920 value that encodes global and new reference
23921 points (GREF and NREF). GREF and NREF are
23922 specified by numbers as below:
23924 0---1---2 -- ascent
23928 9--10--11 -- center
23930 ---3---4---5--- baseline
23932 6---7---8 -- descent
23934 int rule = COMPOSITION_RULE (cmp, i);
23935 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
23937 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
23938 grefx = gref % 3, nrefx = nref % 3;
23939 grefy = gref / 3, nrefy = nref / 3;
23940 if (xoff)
23941 xoff = font_height * (xoff - 128) / 256;
23942 if (yoff)
23943 yoff = font_height * (yoff - 128) / 256;
23945 left = (leftmost
23946 + grefx * (rightmost - leftmost) / 2
23947 - nrefx * width / 2
23948 + xoff);
23950 btm = ((grefy == 0 ? highest
23951 : grefy == 1 ? 0
23952 : grefy == 2 ? lowest
23953 : (highest + lowest) / 2)
23954 - (nrefy == 0 ? ascent + descent
23955 : nrefy == 1 ? descent - boff
23956 : nrefy == 2 ? 0
23957 : (ascent + descent) / 2)
23958 + yoff);
23961 cmp->offsets[i * 2] = left;
23962 cmp->offsets[i * 2 + 1] = btm + descent;
23964 /* Update the bounding box of the overall glyphs. */
23965 if (width > 0)
23967 right = left + width;
23968 if (left < leftmost)
23969 leftmost = left;
23970 if (right > rightmost)
23971 rightmost = right;
23973 top = btm + descent + ascent;
23974 if (top > highest)
23975 highest = top;
23976 if (btm < lowest)
23977 lowest = btm;
23979 if (cmp->lbearing > left + lbearing)
23980 cmp->lbearing = left + lbearing;
23981 if (cmp->rbearing < left + rbearing)
23982 cmp->rbearing = left + rbearing;
23986 /* If there are glyphs whose x-offsets are negative,
23987 shift all glyphs to the right and make all x-offsets
23988 non-negative. */
23989 if (leftmost < 0)
23991 for (i = 0; i < cmp->glyph_len; i++)
23992 cmp->offsets[i * 2] -= leftmost;
23993 rightmost -= leftmost;
23994 cmp->lbearing -= leftmost;
23995 cmp->rbearing -= leftmost;
23998 if (left_padded && cmp->lbearing < 0)
24000 for (i = 0; i < cmp->glyph_len; i++)
24001 cmp->offsets[i * 2] -= cmp->lbearing;
24002 rightmost -= cmp->lbearing;
24003 cmp->rbearing -= cmp->lbearing;
24004 cmp->lbearing = 0;
24006 if (right_padded && rightmost < cmp->rbearing)
24008 rightmost = cmp->rbearing;
24011 cmp->pixel_width = rightmost;
24012 cmp->ascent = highest;
24013 cmp->descent = - lowest;
24014 if (cmp->ascent < font_ascent)
24015 cmp->ascent = font_ascent;
24016 if (cmp->descent < font_descent)
24017 cmp->descent = font_descent;
24020 if (it->glyph_row
24021 && (cmp->lbearing < 0
24022 || cmp->rbearing > cmp->pixel_width))
24023 it->glyph_row->contains_overlapping_glyphs_p = 1;
24025 it->pixel_width = cmp->pixel_width;
24026 it->ascent = it->phys_ascent = cmp->ascent;
24027 it->descent = it->phys_descent = cmp->descent;
24028 if (face->box != FACE_NO_BOX)
24030 int thick = face->box_line_width;
24032 if (thick > 0)
24034 it->ascent += thick;
24035 it->descent += thick;
24037 else
24038 thick = - thick;
24040 if (it->start_of_box_run_p)
24041 it->pixel_width += thick;
24042 if (it->end_of_box_run_p)
24043 it->pixel_width += thick;
24046 /* If face has an overline, add the height of the overline
24047 (1 pixel) and a 1 pixel margin to the character height. */
24048 if (face->overline_p)
24049 it->ascent += overline_margin;
24051 take_vertical_position_into_account (it);
24052 if (it->ascent < 0)
24053 it->ascent = 0;
24054 if (it->descent < 0)
24055 it->descent = 0;
24057 if (it->glyph_row)
24058 append_composite_glyph (it);
24060 else if (it->what == IT_COMPOSITION)
24062 /* A dynamic (automatic) composition. */
24063 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24064 Lisp_Object gstring;
24065 struct font_metrics metrics;
24067 it->nglyphs = 1;
24069 gstring = composition_gstring_from_id (it->cmp_it.id);
24070 it->pixel_width
24071 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
24072 &metrics);
24073 if (it->glyph_row
24074 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
24075 it->glyph_row->contains_overlapping_glyphs_p = 1;
24076 it->ascent = it->phys_ascent = metrics.ascent;
24077 it->descent = it->phys_descent = metrics.descent;
24078 if (face->box != FACE_NO_BOX)
24080 int thick = face->box_line_width;
24082 if (thick > 0)
24084 it->ascent += thick;
24085 it->descent += thick;
24087 else
24088 thick = - thick;
24090 if (it->start_of_box_run_p)
24091 it->pixel_width += thick;
24092 if (it->end_of_box_run_p)
24093 it->pixel_width += thick;
24095 /* If face has an overline, add the height of the overline
24096 (1 pixel) and a 1 pixel margin to the character height. */
24097 if (face->overline_p)
24098 it->ascent += overline_margin;
24099 take_vertical_position_into_account (it);
24100 if (it->ascent < 0)
24101 it->ascent = 0;
24102 if (it->descent < 0)
24103 it->descent = 0;
24105 if (it->glyph_row)
24106 append_composite_glyph (it);
24108 else if (it->what == IT_GLYPHLESS)
24109 produce_glyphless_glyph (it, 0, Qnil);
24110 else if (it->what == IT_IMAGE)
24111 produce_image_glyph (it);
24112 else if (it->what == IT_STRETCH)
24113 produce_stretch_glyph (it);
24115 done:
24116 /* Accumulate dimensions. Note: can't assume that it->descent > 0
24117 because this isn't true for images with `:ascent 100'. */
24118 xassert (it->ascent >= 0 && it->descent >= 0);
24119 if (it->area == TEXT_AREA)
24120 it->current_x += it->pixel_width;
24122 if (extra_line_spacing > 0)
24124 it->descent += extra_line_spacing;
24125 if (extra_line_spacing > it->max_extra_line_spacing)
24126 it->max_extra_line_spacing = extra_line_spacing;
24129 it->max_ascent = max (it->max_ascent, it->ascent);
24130 it->max_descent = max (it->max_descent, it->descent);
24131 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
24132 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
24135 /* EXPORT for RIF:
24136 Output LEN glyphs starting at START at the nominal cursor position.
24137 Advance the nominal cursor over the text. The global variable
24138 updated_window contains the window being updated, updated_row is
24139 the glyph row being updated, and updated_area is the area of that
24140 row being updated. */
24142 void
24143 x_write_glyphs (struct glyph *start, int len)
24145 int x, hpos;
24147 xassert (updated_window && updated_row);
24148 BLOCK_INPUT;
24150 /* Write glyphs. */
24152 hpos = start - updated_row->glyphs[updated_area];
24153 x = draw_glyphs (updated_window, output_cursor.x,
24154 updated_row, updated_area,
24155 hpos, hpos + len,
24156 DRAW_NORMAL_TEXT, 0);
24158 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
24159 if (updated_area == TEXT_AREA
24160 && updated_window->phys_cursor_on_p
24161 && updated_window->phys_cursor.vpos == output_cursor.vpos
24162 && updated_window->phys_cursor.hpos >= hpos
24163 && updated_window->phys_cursor.hpos < hpos + len)
24164 updated_window->phys_cursor_on_p = 0;
24166 UNBLOCK_INPUT;
24168 /* Advance the output cursor. */
24169 output_cursor.hpos += len;
24170 output_cursor.x = x;
24174 /* EXPORT for RIF:
24175 Insert LEN glyphs from START at the nominal cursor position. */
24177 void
24178 x_insert_glyphs (struct glyph *start, int len)
24180 struct frame *f;
24181 struct window *w;
24182 int line_height, shift_by_width, shifted_region_width;
24183 struct glyph_row *row;
24184 struct glyph *glyph;
24185 int frame_x, frame_y;
24186 EMACS_INT hpos;
24188 xassert (updated_window && updated_row);
24189 BLOCK_INPUT;
24190 w = updated_window;
24191 f = XFRAME (WINDOW_FRAME (w));
24193 /* Get the height of the line we are in. */
24194 row = updated_row;
24195 line_height = row->height;
24197 /* Get the width of the glyphs to insert. */
24198 shift_by_width = 0;
24199 for (glyph = start; glyph < start + len; ++glyph)
24200 shift_by_width += glyph->pixel_width;
24202 /* Get the width of the region to shift right. */
24203 shifted_region_width = (window_box_width (w, updated_area)
24204 - output_cursor.x
24205 - shift_by_width);
24207 /* Shift right. */
24208 frame_x = window_box_left (w, updated_area) + output_cursor.x;
24209 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
24211 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
24212 line_height, shift_by_width);
24214 /* Write the glyphs. */
24215 hpos = start - row->glyphs[updated_area];
24216 draw_glyphs (w, output_cursor.x, row, updated_area,
24217 hpos, hpos + len,
24218 DRAW_NORMAL_TEXT, 0);
24220 /* Advance the output cursor. */
24221 output_cursor.hpos += len;
24222 output_cursor.x += shift_by_width;
24223 UNBLOCK_INPUT;
24227 /* EXPORT for RIF:
24228 Erase the current text line from the nominal cursor position
24229 (inclusive) to pixel column TO_X (exclusive). The idea is that
24230 everything from TO_X onward is already erased.
24232 TO_X is a pixel position relative to updated_area of
24233 updated_window. TO_X == -1 means clear to the end of this area. */
24235 void
24236 x_clear_end_of_line (int to_x)
24238 struct frame *f;
24239 struct window *w = updated_window;
24240 int max_x, min_y, max_y;
24241 int from_x, from_y, to_y;
24243 xassert (updated_window && updated_row);
24244 f = XFRAME (w->frame);
24246 if (updated_row->full_width_p)
24247 max_x = WINDOW_TOTAL_WIDTH (w);
24248 else
24249 max_x = window_box_width (w, updated_area);
24250 max_y = window_text_bottom_y (w);
24252 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
24253 of window. For TO_X > 0, truncate to end of drawing area. */
24254 if (to_x == 0)
24255 return;
24256 else if (to_x < 0)
24257 to_x = max_x;
24258 else
24259 to_x = min (to_x, max_x);
24261 to_y = min (max_y, output_cursor.y + updated_row->height);
24263 /* Notice if the cursor will be cleared by this operation. */
24264 if (!updated_row->full_width_p)
24265 notice_overwritten_cursor (w, updated_area,
24266 output_cursor.x, -1,
24267 updated_row->y,
24268 MATRIX_ROW_BOTTOM_Y (updated_row));
24270 from_x = output_cursor.x;
24272 /* Translate to frame coordinates. */
24273 if (updated_row->full_width_p)
24275 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
24276 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
24278 else
24280 int area_left = window_box_left (w, updated_area);
24281 from_x += area_left;
24282 to_x += area_left;
24285 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
24286 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
24287 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
24289 /* Prevent inadvertently clearing to end of the X window. */
24290 if (to_x > from_x && to_y > from_y)
24292 BLOCK_INPUT;
24293 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
24294 to_x - from_x, to_y - from_y);
24295 UNBLOCK_INPUT;
24299 #endif /* HAVE_WINDOW_SYSTEM */
24303 /***********************************************************************
24304 Cursor types
24305 ***********************************************************************/
24307 /* Value is the internal representation of the specified cursor type
24308 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
24309 of the bar cursor. */
24311 static enum text_cursor_kinds
24312 get_specified_cursor_type (Lisp_Object arg, int *width)
24314 enum text_cursor_kinds type;
24316 if (NILP (arg))
24317 return NO_CURSOR;
24319 if (EQ (arg, Qbox))
24320 return FILLED_BOX_CURSOR;
24322 if (EQ (arg, Qhollow))
24323 return HOLLOW_BOX_CURSOR;
24325 if (EQ (arg, Qbar))
24327 *width = 2;
24328 return BAR_CURSOR;
24331 if (CONSP (arg)
24332 && EQ (XCAR (arg), Qbar)
24333 && INTEGERP (XCDR (arg))
24334 && XINT (XCDR (arg)) >= 0)
24336 *width = XINT (XCDR (arg));
24337 return BAR_CURSOR;
24340 if (EQ (arg, Qhbar))
24342 *width = 2;
24343 return HBAR_CURSOR;
24346 if (CONSP (arg)
24347 && EQ (XCAR (arg), Qhbar)
24348 && INTEGERP (XCDR (arg))
24349 && XINT (XCDR (arg)) >= 0)
24351 *width = XINT (XCDR (arg));
24352 return HBAR_CURSOR;
24355 /* Treat anything unknown as "hollow box cursor".
24356 It was bad to signal an error; people have trouble fixing
24357 .Xdefaults with Emacs, when it has something bad in it. */
24358 type = HOLLOW_BOX_CURSOR;
24360 return type;
24363 /* Set the default cursor types for specified frame. */
24364 void
24365 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
24367 int width = 1;
24368 Lisp_Object tem;
24370 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
24371 FRAME_CURSOR_WIDTH (f) = width;
24373 /* By default, set up the blink-off state depending on the on-state. */
24375 tem = Fassoc (arg, Vblink_cursor_alist);
24376 if (!NILP (tem))
24378 FRAME_BLINK_OFF_CURSOR (f)
24379 = get_specified_cursor_type (XCDR (tem), &width);
24380 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
24382 else
24383 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
24387 #ifdef HAVE_WINDOW_SYSTEM
24389 /* Return the cursor we want to be displayed in window W. Return
24390 width of bar/hbar cursor through WIDTH arg. Return with
24391 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
24392 (i.e. if the `system caret' should track this cursor).
24394 In a mini-buffer window, we want the cursor only to appear if we
24395 are reading input from this window. For the selected window, we
24396 want the cursor type given by the frame parameter or buffer local
24397 setting of cursor-type. If explicitly marked off, draw no cursor.
24398 In all other cases, we want a hollow box cursor. */
24400 static enum text_cursor_kinds
24401 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
24402 int *active_cursor)
24404 struct frame *f = XFRAME (w->frame);
24405 struct buffer *b = XBUFFER (w->buffer);
24406 int cursor_type = DEFAULT_CURSOR;
24407 Lisp_Object alt_cursor;
24408 int non_selected = 0;
24410 *active_cursor = 1;
24412 /* Echo area */
24413 if (cursor_in_echo_area
24414 && FRAME_HAS_MINIBUF_P (f)
24415 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
24417 if (w == XWINDOW (echo_area_window))
24419 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
24421 *width = FRAME_CURSOR_WIDTH (f);
24422 return FRAME_DESIRED_CURSOR (f);
24424 else
24425 return get_specified_cursor_type (BVAR (b, cursor_type), width);
24428 *active_cursor = 0;
24429 non_selected = 1;
24432 /* Detect a nonselected window or nonselected frame. */
24433 else if (w != XWINDOW (f->selected_window)
24434 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
24436 *active_cursor = 0;
24438 if (MINI_WINDOW_P (w) && minibuf_level == 0)
24439 return NO_CURSOR;
24441 non_selected = 1;
24444 /* Never display a cursor in a window in which cursor-type is nil. */
24445 if (NILP (BVAR (b, cursor_type)))
24446 return NO_CURSOR;
24448 /* Get the normal cursor type for this window. */
24449 if (EQ (BVAR (b, cursor_type), Qt))
24451 cursor_type = FRAME_DESIRED_CURSOR (f);
24452 *width = FRAME_CURSOR_WIDTH (f);
24454 else
24455 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
24457 /* Use cursor-in-non-selected-windows instead
24458 for non-selected window or frame. */
24459 if (non_selected)
24461 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
24462 if (!EQ (Qt, alt_cursor))
24463 return get_specified_cursor_type (alt_cursor, width);
24464 /* t means modify the normal cursor type. */
24465 if (cursor_type == FILLED_BOX_CURSOR)
24466 cursor_type = HOLLOW_BOX_CURSOR;
24467 else if (cursor_type == BAR_CURSOR && *width > 1)
24468 --*width;
24469 return cursor_type;
24472 /* Use normal cursor if not blinked off. */
24473 if (!w->cursor_off_p)
24475 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
24477 if (cursor_type == FILLED_BOX_CURSOR)
24479 /* Using a block cursor on large images can be very annoying.
24480 So use a hollow cursor for "large" images.
24481 If image is not transparent (no mask), also use hollow cursor. */
24482 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
24483 if (img != NULL && IMAGEP (img->spec))
24485 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
24486 where N = size of default frame font size.
24487 This should cover most of the "tiny" icons people may use. */
24488 if (!img->mask
24489 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
24490 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
24491 cursor_type = HOLLOW_BOX_CURSOR;
24494 else if (cursor_type != NO_CURSOR)
24496 /* Display current only supports BOX and HOLLOW cursors for images.
24497 So for now, unconditionally use a HOLLOW cursor when cursor is
24498 not a solid box cursor. */
24499 cursor_type = HOLLOW_BOX_CURSOR;
24502 return cursor_type;
24505 /* Cursor is blinked off, so determine how to "toggle" it. */
24507 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
24508 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
24509 return get_specified_cursor_type (XCDR (alt_cursor), width);
24511 /* Then see if frame has specified a specific blink off cursor type. */
24512 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
24514 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
24515 return FRAME_BLINK_OFF_CURSOR (f);
24518 #if 0
24519 /* Some people liked having a permanently visible blinking cursor,
24520 while others had very strong opinions against it. So it was
24521 decided to remove it. KFS 2003-09-03 */
24523 /* Finally perform built-in cursor blinking:
24524 filled box <-> hollow box
24525 wide [h]bar <-> narrow [h]bar
24526 narrow [h]bar <-> no cursor
24527 other type <-> no cursor */
24529 if (cursor_type == FILLED_BOX_CURSOR)
24530 return HOLLOW_BOX_CURSOR;
24532 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
24534 *width = 1;
24535 return cursor_type;
24537 #endif
24539 return NO_CURSOR;
24543 /* Notice when the text cursor of window W has been completely
24544 overwritten by a drawing operation that outputs glyphs in AREA
24545 starting at X0 and ending at X1 in the line starting at Y0 and
24546 ending at Y1. X coordinates are area-relative. X1 < 0 means all
24547 the rest of the line after X0 has been written. Y coordinates
24548 are window-relative. */
24550 static void
24551 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
24552 int x0, int x1, int y0, int y1)
24554 int cx0, cx1, cy0, cy1;
24555 struct glyph_row *row;
24557 if (!w->phys_cursor_on_p)
24558 return;
24559 if (area != TEXT_AREA)
24560 return;
24562 if (w->phys_cursor.vpos < 0
24563 || w->phys_cursor.vpos >= w->current_matrix->nrows
24564 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
24565 !(row->enabled_p && row->displays_text_p)))
24566 return;
24568 if (row->cursor_in_fringe_p)
24570 row->cursor_in_fringe_p = 0;
24571 draw_fringe_bitmap (w, row, row->reversed_p);
24572 w->phys_cursor_on_p = 0;
24573 return;
24576 cx0 = w->phys_cursor.x;
24577 cx1 = cx0 + w->phys_cursor_width;
24578 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
24579 return;
24581 /* The cursor image will be completely removed from the
24582 screen if the output area intersects the cursor area in
24583 y-direction. When we draw in [y0 y1[, and some part of
24584 the cursor is at y < y0, that part must have been drawn
24585 before. When scrolling, the cursor is erased before
24586 actually scrolling, so we don't come here. When not
24587 scrolling, the rows above the old cursor row must have
24588 changed, and in this case these rows must have written
24589 over the cursor image.
24591 Likewise if part of the cursor is below y1, with the
24592 exception of the cursor being in the first blank row at
24593 the buffer and window end because update_text_area
24594 doesn't draw that row. (Except when it does, but
24595 that's handled in update_text_area.) */
24597 cy0 = w->phys_cursor.y;
24598 cy1 = cy0 + w->phys_cursor_height;
24599 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
24600 return;
24602 w->phys_cursor_on_p = 0;
24605 #endif /* HAVE_WINDOW_SYSTEM */
24608 /************************************************************************
24609 Mouse Face
24610 ************************************************************************/
24612 #ifdef HAVE_WINDOW_SYSTEM
24614 /* EXPORT for RIF:
24615 Fix the display of area AREA of overlapping row ROW in window W
24616 with respect to the overlapping part OVERLAPS. */
24618 void
24619 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
24620 enum glyph_row_area area, int overlaps)
24622 int i, x;
24624 BLOCK_INPUT;
24626 x = 0;
24627 for (i = 0; i < row->used[area];)
24629 if (row->glyphs[area][i].overlaps_vertically_p)
24631 int start = i, start_x = x;
24635 x += row->glyphs[area][i].pixel_width;
24636 ++i;
24638 while (i < row->used[area]
24639 && row->glyphs[area][i].overlaps_vertically_p);
24641 draw_glyphs (w, start_x, row, area,
24642 start, i,
24643 DRAW_NORMAL_TEXT, overlaps);
24645 else
24647 x += row->glyphs[area][i].pixel_width;
24648 ++i;
24652 UNBLOCK_INPUT;
24656 /* EXPORT:
24657 Draw the cursor glyph of window W in glyph row ROW. See the
24658 comment of draw_glyphs for the meaning of HL. */
24660 void
24661 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
24662 enum draw_glyphs_face hl)
24664 /* If cursor hpos is out of bounds, don't draw garbage. This can
24665 happen in mini-buffer windows when switching between echo area
24666 glyphs and mini-buffer. */
24667 if ((row->reversed_p
24668 ? (w->phys_cursor.hpos >= 0)
24669 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
24671 int on_p = w->phys_cursor_on_p;
24672 int x1;
24673 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
24674 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
24675 hl, 0);
24676 w->phys_cursor_on_p = on_p;
24678 if (hl == DRAW_CURSOR)
24679 w->phys_cursor_width = x1 - w->phys_cursor.x;
24680 /* When we erase the cursor, and ROW is overlapped by other
24681 rows, make sure that these overlapping parts of other rows
24682 are redrawn. */
24683 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
24685 w->phys_cursor_width = x1 - w->phys_cursor.x;
24687 if (row > w->current_matrix->rows
24688 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
24689 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
24690 OVERLAPS_ERASED_CURSOR);
24692 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
24693 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
24694 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
24695 OVERLAPS_ERASED_CURSOR);
24701 /* EXPORT:
24702 Erase the image of a cursor of window W from the screen. */
24704 void
24705 erase_phys_cursor (struct window *w)
24707 struct frame *f = XFRAME (w->frame);
24708 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24709 int hpos = w->phys_cursor.hpos;
24710 int vpos = w->phys_cursor.vpos;
24711 int mouse_face_here_p = 0;
24712 struct glyph_matrix *active_glyphs = w->current_matrix;
24713 struct glyph_row *cursor_row;
24714 struct glyph *cursor_glyph;
24715 enum draw_glyphs_face hl;
24717 /* No cursor displayed or row invalidated => nothing to do on the
24718 screen. */
24719 if (w->phys_cursor_type == NO_CURSOR)
24720 goto mark_cursor_off;
24722 /* VPOS >= active_glyphs->nrows means that window has been resized.
24723 Don't bother to erase the cursor. */
24724 if (vpos >= active_glyphs->nrows)
24725 goto mark_cursor_off;
24727 /* If row containing cursor is marked invalid, there is nothing we
24728 can do. */
24729 cursor_row = MATRIX_ROW (active_glyphs, vpos);
24730 if (!cursor_row->enabled_p)
24731 goto mark_cursor_off;
24733 /* If line spacing is > 0, old cursor may only be partially visible in
24734 window after split-window. So adjust visible height. */
24735 cursor_row->visible_height = min (cursor_row->visible_height,
24736 window_text_bottom_y (w) - cursor_row->y);
24738 /* If row is completely invisible, don't attempt to delete a cursor which
24739 isn't there. This can happen if cursor is at top of a window, and
24740 we switch to a buffer with a header line in that window. */
24741 if (cursor_row->visible_height <= 0)
24742 goto mark_cursor_off;
24744 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
24745 if (cursor_row->cursor_in_fringe_p)
24747 cursor_row->cursor_in_fringe_p = 0;
24748 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
24749 goto mark_cursor_off;
24752 /* This can happen when the new row is shorter than the old one.
24753 In this case, either draw_glyphs or clear_end_of_line
24754 should have cleared the cursor. Note that we wouldn't be
24755 able to erase the cursor in this case because we don't have a
24756 cursor glyph at hand. */
24757 if ((cursor_row->reversed_p
24758 ? (w->phys_cursor.hpos < 0)
24759 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
24760 goto mark_cursor_off;
24762 /* If the cursor is in the mouse face area, redisplay that when
24763 we clear the cursor. */
24764 if (! NILP (hlinfo->mouse_face_window)
24765 && coords_in_mouse_face_p (w, hpos, vpos)
24766 /* Don't redraw the cursor's spot in mouse face if it is at the
24767 end of a line (on a newline). The cursor appears there, but
24768 mouse highlighting does not. */
24769 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
24770 mouse_face_here_p = 1;
24772 /* Maybe clear the display under the cursor. */
24773 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
24775 int x, y, left_x;
24776 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
24777 int width;
24779 cursor_glyph = get_phys_cursor_glyph (w);
24780 if (cursor_glyph == NULL)
24781 goto mark_cursor_off;
24783 width = cursor_glyph->pixel_width;
24784 left_x = window_box_left_offset (w, TEXT_AREA);
24785 x = w->phys_cursor.x;
24786 if (x < left_x)
24787 width -= left_x - x;
24788 width = min (width, window_box_width (w, TEXT_AREA) - x);
24789 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
24790 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
24792 if (width > 0)
24793 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
24796 /* Erase the cursor by redrawing the character underneath it. */
24797 if (mouse_face_here_p)
24798 hl = DRAW_MOUSE_FACE;
24799 else
24800 hl = DRAW_NORMAL_TEXT;
24801 draw_phys_cursor_glyph (w, cursor_row, hl);
24803 mark_cursor_off:
24804 w->phys_cursor_on_p = 0;
24805 w->phys_cursor_type = NO_CURSOR;
24809 /* EXPORT:
24810 Display or clear cursor of window W. If ON is zero, clear the
24811 cursor. If it is non-zero, display the cursor. If ON is nonzero,
24812 where to put the cursor is specified by HPOS, VPOS, X and Y. */
24814 void
24815 display_and_set_cursor (struct window *w, int on,
24816 int hpos, int vpos, int x, int y)
24818 struct frame *f = XFRAME (w->frame);
24819 int new_cursor_type;
24820 int new_cursor_width;
24821 int active_cursor;
24822 struct glyph_row *glyph_row;
24823 struct glyph *glyph;
24825 /* This is pointless on invisible frames, and dangerous on garbaged
24826 windows and frames; in the latter case, the frame or window may
24827 be in the midst of changing its size, and x and y may be off the
24828 window. */
24829 if (! FRAME_VISIBLE_P (f)
24830 || FRAME_GARBAGED_P (f)
24831 || vpos >= w->current_matrix->nrows
24832 || hpos >= w->current_matrix->matrix_w)
24833 return;
24835 /* If cursor is off and we want it off, return quickly. */
24836 if (!on && !w->phys_cursor_on_p)
24837 return;
24839 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
24840 /* If cursor row is not enabled, we don't really know where to
24841 display the cursor. */
24842 if (!glyph_row->enabled_p)
24844 w->phys_cursor_on_p = 0;
24845 return;
24848 glyph = NULL;
24849 if (!glyph_row->exact_window_width_line_p
24850 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
24851 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
24853 xassert (interrupt_input_blocked);
24855 /* Set new_cursor_type to the cursor we want to be displayed. */
24856 new_cursor_type = get_window_cursor_type (w, glyph,
24857 &new_cursor_width, &active_cursor);
24859 /* If cursor is currently being shown and we don't want it to be or
24860 it is in the wrong place, or the cursor type is not what we want,
24861 erase it. */
24862 if (w->phys_cursor_on_p
24863 && (!on
24864 || w->phys_cursor.x != x
24865 || w->phys_cursor.y != y
24866 || new_cursor_type != w->phys_cursor_type
24867 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
24868 && new_cursor_width != w->phys_cursor_width)))
24869 erase_phys_cursor (w);
24871 /* Don't check phys_cursor_on_p here because that flag is only set
24872 to zero in some cases where we know that the cursor has been
24873 completely erased, to avoid the extra work of erasing the cursor
24874 twice. In other words, phys_cursor_on_p can be 1 and the cursor
24875 still not be visible, or it has only been partly erased. */
24876 if (on)
24878 w->phys_cursor_ascent = glyph_row->ascent;
24879 w->phys_cursor_height = glyph_row->height;
24881 /* Set phys_cursor_.* before x_draw_.* is called because some
24882 of them may need the information. */
24883 w->phys_cursor.x = x;
24884 w->phys_cursor.y = glyph_row->y;
24885 w->phys_cursor.hpos = hpos;
24886 w->phys_cursor.vpos = vpos;
24889 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
24890 new_cursor_type, new_cursor_width,
24891 on, active_cursor);
24895 /* Switch the display of W's cursor on or off, according to the value
24896 of ON. */
24898 static void
24899 update_window_cursor (struct window *w, int on)
24901 /* Don't update cursor in windows whose frame is in the process
24902 of being deleted. */
24903 if (w->current_matrix)
24905 BLOCK_INPUT;
24906 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
24907 w->phys_cursor.x, w->phys_cursor.y);
24908 UNBLOCK_INPUT;
24913 /* Call update_window_cursor with parameter ON_P on all leaf windows
24914 in the window tree rooted at W. */
24916 static void
24917 update_cursor_in_window_tree (struct window *w, int on_p)
24919 while (w)
24921 if (!NILP (w->hchild))
24922 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
24923 else if (!NILP (w->vchild))
24924 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
24925 else
24926 update_window_cursor (w, on_p);
24928 w = NILP (w->next) ? 0 : XWINDOW (w->next);
24933 /* EXPORT:
24934 Display the cursor on window W, or clear it, according to ON_P.
24935 Don't change the cursor's position. */
24937 void
24938 x_update_cursor (struct frame *f, int on_p)
24940 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
24944 /* EXPORT:
24945 Clear the cursor of window W to background color, and mark the
24946 cursor as not shown. This is used when the text where the cursor
24947 is about to be rewritten. */
24949 void
24950 x_clear_cursor (struct window *w)
24952 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
24953 update_window_cursor (w, 0);
24956 #endif /* HAVE_WINDOW_SYSTEM */
24958 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
24959 and MSDOS. */
24960 static void
24961 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
24962 int start_hpos, int end_hpos,
24963 enum draw_glyphs_face draw)
24965 #ifdef HAVE_WINDOW_SYSTEM
24966 if (FRAME_WINDOW_P (XFRAME (w->frame)))
24968 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
24969 return;
24971 #endif
24972 #if defined (HAVE_GPM) || defined (MSDOS)
24973 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
24974 #endif
24977 /* Display the active region described by mouse_face_* according to DRAW. */
24979 static void
24980 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
24982 struct window *w = XWINDOW (hlinfo->mouse_face_window);
24983 struct frame *f = XFRAME (WINDOW_FRAME (w));
24985 if (/* If window is in the process of being destroyed, don't bother
24986 to do anything. */
24987 w->current_matrix != NULL
24988 /* Don't update mouse highlight if hidden */
24989 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
24990 /* Recognize when we are called to operate on rows that don't exist
24991 anymore. This can happen when a window is split. */
24992 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
24994 int phys_cursor_on_p = w->phys_cursor_on_p;
24995 struct glyph_row *row, *first, *last;
24997 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
24998 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
25000 for (row = first; row <= last && row->enabled_p; ++row)
25002 int start_hpos, end_hpos, start_x;
25004 /* For all but the first row, the highlight starts at column 0. */
25005 if (row == first)
25007 /* R2L rows have BEG and END in reversed order, but the
25008 screen drawing geometry is always left to right. So
25009 we need to mirror the beginning and end of the
25010 highlighted area in R2L rows. */
25011 if (!row->reversed_p)
25013 start_hpos = hlinfo->mouse_face_beg_col;
25014 start_x = hlinfo->mouse_face_beg_x;
25016 else if (row == last)
25018 start_hpos = hlinfo->mouse_face_end_col;
25019 start_x = hlinfo->mouse_face_end_x;
25021 else
25023 start_hpos = 0;
25024 start_x = 0;
25027 else if (row->reversed_p && row == last)
25029 start_hpos = hlinfo->mouse_face_end_col;
25030 start_x = hlinfo->mouse_face_end_x;
25032 else
25034 start_hpos = 0;
25035 start_x = 0;
25038 if (row == last)
25040 if (!row->reversed_p)
25041 end_hpos = hlinfo->mouse_face_end_col;
25042 else if (row == first)
25043 end_hpos = hlinfo->mouse_face_beg_col;
25044 else
25046 end_hpos = row->used[TEXT_AREA];
25047 if (draw == DRAW_NORMAL_TEXT)
25048 row->fill_line_p = 1; /* Clear to end of line */
25051 else if (row->reversed_p && row == first)
25052 end_hpos = hlinfo->mouse_face_beg_col;
25053 else
25055 end_hpos = row->used[TEXT_AREA];
25056 if (draw == DRAW_NORMAL_TEXT)
25057 row->fill_line_p = 1; /* Clear to end of line */
25060 if (end_hpos > start_hpos)
25062 draw_row_with_mouse_face (w, start_x, row,
25063 start_hpos, end_hpos, draw);
25065 row->mouse_face_p
25066 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
25070 #ifdef HAVE_WINDOW_SYSTEM
25071 /* When we've written over the cursor, arrange for it to
25072 be displayed again. */
25073 if (FRAME_WINDOW_P (f)
25074 && phys_cursor_on_p && !w->phys_cursor_on_p)
25076 BLOCK_INPUT;
25077 display_and_set_cursor (w, 1,
25078 w->phys_cursor.hpos, w->phys_cursor.vpos,
25079 w->phys_cursor.x, w->phys_cursor.y);
25080 UNBLOCK_INPUT;
25082 #endif /* HAVE_WINDOW_SYSTEM */
25085 #ifdef HAVE_WINDOW_SYSTEM
25086 /* Change the mouse cursor. */
25087 if (FRAME_WINDOW_P (f))
25089 if (draw == DRAW_NORMAL_TEXT
25090 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
25091 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
25092 else if (draw == DRAW_MOUSE_FACE)
25093 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
25094 else
25095 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
25097 #endif /* HAVE_WINDOW_SYSTEM */
25100 /* EXPORT:
25101 Clear out the mouse-highlighted active region.
25102 Redraw it un-highlighted first. Value is non-zero if mouse
25103 face was actually drawn unhighlighted. */
25106 clear_mouse_face (Mouse_HLInfo *hlinfo)
25108 int cleared = 0;
25110 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
25112 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
25113 cleared = 1;
25116 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25117 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25118 hlinfo->mouse_face_window = Qnil;
25119 hlinfo->mouse_face_overlay = Qnil;
25120 return cleared;
25123 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
25124 within the mouse face on that window. */
25125 static int
25126 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
25128 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25130 /* Quickly resolve the easy cases. */
25131 if (!(WINDOWP (hlinfo->mouse_face_window)
25132 && XWINDOW (hlinfo->mouse_face_window) == w))
25133 return 0;
25134 if (vpos < hlinfo->mouse_face_beg_row
25135 || vpos > hlinfo->mouse_face_end_row)
25136 return 0;
25137 if (vpos > hlinfo->mouse_face_beg_row
25138 && vpos < hlinfo->mouse_face_end_row)
25139 return 1;
25141 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
25143 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25145 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
25146 return 1;
25148 else if ((vpos == hlinfo->mouse_face_beg_row
25149 && hpos >= hlinfo->mouse_face_beg_col)
25150 || (vpos == hlinfo->mouse_face_end_row
25151 && hpos < hlinfo->mouse_face_end_col))
25152 return 1;
25154 else
25156 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25158 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
25159 return 1;
25161 else if ((vpos == hlinfo->mouse_face_beg_row
25162 && hpos <= hlinfo->mouse_face_beg_col)
25163 || (vpos == hlinfo->mouse_face_end_row
25164 && hpos > hlinfo->mouse_face_end_col))
25165 return 1;
25167 return 0;
25171 /* EXPORT:
25172 Non-zero if physical cursor of window W is within mouse face. */
25175 cursor_in_mouse_face_p (struct window *w)
25177 return coords_in_mouse_face_p (w, w->phys_cursor.hpos, w->phys_cursor.vpos);
25182 /* Find the glyph rows START_ROW and END_ROW of window W that display
25183 characters between buffer positions START_CHARPOS and END_CHARPOS
25184 (excluding END_CHARPOS). This is similar to row_containing_pos,
25185 but is more accurate when bidi reordering makes buffer positions
25186 change non-linearly with glyph rows. */
25187 static void
25188 rows_from_pos_range (struct window *w,
25189 EMACS_INT start_charpos, EMACS_INT end_charpos,
25190 struct glyph_row **start, struct glyph_row **end)
25192 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25193 int last_y = window_text_bottom_y (w);
25194 struct glyph_row *row;
25196 *start = NULL;
25197 *end = NULL;
25199 while (!first->enabled_p
25200 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
25201 first++;
25203 /* Find the START row. */
25204 for (row = first;
25205 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
25206 row++)
25208 /* A row can potentially be the START row if the range of the
25209 characters it displays intersects the range
25210 [START_CHARPOS..END_CHARPOS). */
25211 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
25212 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
25213 /* See the commentary in row_containing_pos, for the
25214 explanation of the complicated way to check whether
25215 some position is beyond the end of the characters
25216 displayed by a row. */
25217 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
25218 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
25219 && !row->ends_at_zv_p
25220 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
25221 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
25222 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
25223 && !row->ends_at_zv_p
25224 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
25226 /* Found a candidate row. Now make sure at least one of the
25227 glyphs it displays has a charpos from the range
25228 [START_CHARPOS..END_CHARPOS).
25230 This is not obvious because bidi reordering could make
25231 buffer positions of a row be 1,2,3,102,101,100, and if we
25232 want to highlight characters in [50..60), we don't want
25233 this row, even though [50..60) does intersect [1..103),
25234 the range of character positions given by the row's start
25235 and end positions. */
25236 struct glyph *g = row->glyphs[TEXT_AREA];
25237 struct glyph *e = g + row->used[TEXT_AREA];
25239 while (g < e)
25241 if ((BUFFERP (g->object) || INTEGERP (g->object))
25242 && start_charpos <= g->charpos && g->charpos < end_charpos)
25243 *start = row;
25244 g++;
25246 if (*start)
25247 break;
25251 /* Find the END row. */
25252 if (!*start
25253 /* If the last row is partially visible, start looking for END
25254 from that row, instead of starting from FIRST. */
25255 && !(row->enabled_p
25256 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
25257 row = first;
25258 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
25260 struct glyph_row *next = row + 1;
25262 if (!next->enabled_p
25263 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
25264 /* The first row >= START whose range of displayed characters
25265 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
25266 is the row END + 1. */
25267 || (start_charpos < MATRIX_ROW_START_CHARPOS (next)
25268 && end_charpos < MATRIX_ROW_START_CHARPOS (next))
25269 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
25270 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
25271 && !next->ends_at_zv_p
25272 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
25273 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
25274 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
25275 && !next->ends_at_zv_p
25276 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
25278 *end = row;
25279 break;
25281 else
25283 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
25284 but none of the characters it displays are in the range, it is
25285 also END + 1. */
25286 struct glyph *g = next->glyphs[TEXT_AREA];
25287 struct glyph *e = g + next->used[TEXT_AREA];
25289 while (g < e)
25291 if ((BUFFERP (g->object) || INTEGERP (g->object))
25292 && start_charpos <= g->charpos && g->charpos < end_charpos)
25293 break;
25294 g++;
25296 if (g == e)
25298 *end = row;
25299 break;
25305 /* This function sets the mouse_face_* elements of HLINFO, assuming
25306 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
25307 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
25308 for the overlay or run of text properties specifying the mouse
25309 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
25310 before-string and after-string that must also be highlighted.
25311 COVER_STRING, if non-nil, is a display string that may cover some
25312 or all of the highlighted text. */
25314 static void
25315 mouse_face_from_buffer_pos (Lisp_Object window,
25316 Mouse_HLInfo *hlinfo,
25317 EMACS_INT mouse_charpos,
25318 EMACS_INT start_charpos,
25319 EMACS_INT end_charpos,
25320 Lisp_Object before_string,
25321 Lisp_Object after_string,
25322 Lisp_Object cover_string)
25324 struct window *w = XWINDOW (window);
25325 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25326 struct glyph_row *r1, *r2;
25327 struct glyph *glyph, *end;
25328 EMACS_INT ignore, pos;
25329 int x;
25331 xassert (NILP (cover_string) || STRINGP (cover_string));
25332 xassert (NILP (before_string) || STRINGP (before_string));
25333 xassert (NILP (after_string) || STRINGP (after_string));
25335 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
25336 rows_from_pos_range (w, start_charpos, end_charpos, &r1, &r2);
25337 if (r1 == NULL)
25338 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25339 /* If the before-string or display-string contains newlines,
25340 rows_from_pos_range skips to its last row. Move back. */
25341 if (!NILP (before_string) || !NILP (cover_string))
25343 struct glyph_row *prev;
25344 while ((prev = r1 - 1, prev >= first)
25345 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
25346 && prev->used[TEXT_AREA] > 0)
25348 struct glyph *beg = prev->glyphs[TEXT_AREA];
25349 glyph = beg + prev->used[TEXT_AREA];
25350 while (--glyph >= beg && INTEGERP (glyph->object));
25351 if (glyph < beg
25352 || !(EQ (glyph->object, before_string)
25353 || EQ (glyph->object, cover_string)))
25354 break;
25355 r1 = prev;
25358 if (r2 == NULL)
25360 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25361 hlinfo->mouse_face_past_end = 1;
25363 else if (!NILP (after_string))
25365 /* If the after-string has newlines, advance to its last row. */
25366 struct glyph_row *next;
25367 struct glyph_row *last
25368 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25370 for (next = r2 + 1;
25371 next <= last
25372 && next->used[TEXT_AREA] > 0
25373 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
25374 ++next)
25375 r2 = next;
25377 /* The rest of the display engine assumes that mouse_face_beg_row is
25378 either above below mouse_face_end_row or identical to it. But
25379 with bidi-reordered continued lines, the row for START_CHARPOS
25380 could be below the row for END_CHARPOS. If so, swap the rows and
25381 store them in correct order. */
25382 if (r1->y > r2->y)
25384 struct glyph_row *tem = r2;
25386 r2 = r1;
25387 r1 = tem;
25390 hlinfo->mouse_face_beg_y = r1->y;
25391 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
25392 hlinfo->mouse_face_end_y = r2->y;
25393 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
25395 /* For a bidi-reordered row, the positions of BEFORE_STRING,
25396 AFTER_STRING, COVER_STRING, START_CHARPOS, and END_CHARPOS
25397 could be anywhere in the row and in any order. The strategy
25398 below is to find the leftmost and the rightmost glyph that
25399 belongs to either of these 3 strings, or whose position is
25400 between START_CHARPOS and END_CHARPOS, and highlight all the
25401 glyphs between those two. This may cover more than just the text
25402 between START_CHARPOS and END_CHARPOS if the range of characters
25403 strides the bidi level boundary, e.g. if the beginning is in R2L
25404 text while the end is in L2R text or vice versa. */
25405 if (!r1->reversed_p)
25407 /* This row is in a left to right paragraph. Scan it left to
25408 right. */
25409 glyph = r1->glyphs[TEXT_AREA];
25410 end = glyph + r1->used[TEXT_AREA];
25411 x = r1->x;
25413 /* Skip truncation glyphs at the start of the glyph row. */
25414 if (r1->displays_text_p)
25415 for (; glyph < end
25416 && INTEGERP (glyph->object)
25417 && glyph->charpos < 0;
25418 ++glyph)
25419 x += glyph->pixel_width;
25421 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
25422 or COVER_STRING, and the first glyph from buffer whose
25423 position is between START_CHARPOS and END_CHARPOS. */
25424 for (; glyph < end
25425 && !INTEGERP (glyph->object)
25426 && !EQ (glyph->object, cover_string)
25427 && !(BUFFERP (glyph->object)
25428 && (glyph->charpos >= start_charpos
25429 && glyph->charpos < end_charpos));
25430 ++glyph)
25432 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25433 are present at buffer positions between START_CHARPOS and
25434 END_CHARPOS, or if they come from an overlay. */
25435 if (EQ (glyph->object, before_string))
25437 pos = string_buffer_position (before_string,
25438 start_charpos);
25439 /* If pos == 0, it means before_string came from an
25440 overlay, not from a buffer position. */
25441 if (!pos || (pos >= start_charpos && pos < end_charpos))
25442 break;
25444 else if (EQ (glyph->object, after_string))
25446 pos = string_buffer_position (after_string, end_charpos);
25447 if (!pos || (pos >= start_charpos && pos < end_charpos))
25448 break;
25450 x += glyph->pixel_width;
25452 hlinfo->mouse_face_beg_x = x;
25453 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
25455 else
25457 /* This row is in a right to left paragraph. Scan it right to
25458 left. */
25459 struct glyph *g;
25461 end = r1->glyphs[TEXT_AREA] - 1;
25462 glyph = end + r1->used[TEXT_AREA];
25464 /* Skip truncation glyphs at the start of the glyph row. */
25465 if (r1->displays_text_p)
25466 for (; glyph > end
25467 && INTEGERP (glyph->object)
25468 && glyph->charpos < 0;
25469 --glyph)
25472 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
25473 or COVER_STRING, and the first glyph from buffer whose
25474 position is between START_CHARPOS and END_CHARPOS. */
25475 for (; glyph > end
25476 && !INTEGERP (glyph->object)
25477 && !EQ (glyph->object, cover_string)
25478 && !(BUFFERP (glyph->object)
25479 && (glyph->charpos >= start_charpos
25480 && glyph->charpos < end_charpos));
25481 --glyph)
25483 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25484 are present at buffer positions between START_CHARPOS and
25485 END_CHARPOS, or if they come from an overlay. */
25486 if (EQ (glyph->object, before_string))
25488 pos = string_buffer_position (before_string, start_charpos);
25489 /* If pos == 0, it means before_string came from an
25490 overlay, not from a buffer position. */
25491 if (!pos || (pos >= start_charpos && pos < end_charpos))
25492 break;
25494 else if (EQ (glyph->object, after_string))
25496 pos = string_buffer_position (after_string, end_charpos);
25497 if (!pos || (pos >= start_charpos && pos < end_charpos))
25498 break;
25502 glyph++; /* first glyph to the right of the highlighted area */
25503 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
25504 x += g->pixel_width;
25505 hlinfo->mouse_face_beg_x = x;
25506 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
25509 /* If the highlight ends in a different row, compute GLYPH and END
25510 for the end row. Otherwise, reuse the values computed above for
25511 the row where the highlight begins. */
25512 if (r2 != r1)
25514 if (!r2->reversed_p)
25516 glyph = r2->glyphs[TEXT_AREA];
25517 end = glyph + r2->used[TEXT_AREA];
25518 x = r2->x;
25520 else
25522 end = r2->glyphs[TEXT_AREA] - 1;
25523 glyph = end + r2->used[TEXT_AREA];
25527 if (!r2->reversed_p)
25529 /* Skip truncation and continuation glyphs near the end of the
25530 row, and also blanks and stretch glyphs inserted by
25531 extend_face_to_end_of_line. */
25532 while (end > glyph
25533 && INTEGERP ((end - 1)->object)
25534 && (end - 1)->charpos <= 0)
25535 --end;
25536 /* Scan the rest of the glyph row from the end, looking for the
25537 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
25538 COVER_STRING, or whose position is between START_CHARPOS
25539 and END_CHARPOS */
25540 for (--end;
25541 end > glyph
25542 && !INTEGERP (end->object)
25543 && !EQ (end->object, cover_string)
25544 && !(BUFFERP (end->object)
25545 && (end->charpos >= start_charpos
25546 && end->charpos < end_charpos));
25547 --end)
25549 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25550 are present at buffer positions between START_CHARPOS and
25551 END_CHARPOS, or if they come from an overlay. */
25552 if (EQ (end->object, before_string))
25554 pos = string_buffer_position (before_string, start_charpos);
25555 if (!pos || (pos >= start_charpos && pos < end_charpos))
25556 break;
25558 else if (EQ (end->object, after_string))
25560 pos = string_buffer_position (after_string, end_charpos);
25561 if (!pos || (pos >= start_charpos && pos < end_charpos))
25562 break;
25565 /* Find the X coordinate of the last glyph to be highlighted. */
25566 for (; glyph <= end; ++glyph)
25567 x += glyph->pixel_width;
25569 hlinfo->mouse_face_end_x = x;
25570 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
25572 else
25574 /* Skip truncation and continuation glyphs near the end of the
25575 row, and also blanks and stretch glyphs inserted by
25576 extend_face_to_end_of_line. */
25577 x = r2->x;
25578 end++;
25579 while (end < glyph
25580 && INTEGERP (end->object)
25581 && end->charpos <= 0)
25583 x += end->pixel_width;
25584 ++end;
25586 /* Scan the rest of the glyph row from the end, looking for the
25587 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
25588 COVER_STRING, or whose position is between START_CHARPOS
25589 and END_CHARPOS */
25590 for ( ;
25591 end < glyph
25592 && !INTEGERP (end->object)
25593 && !EQ (end->object, cover_string)
25594 && !(BUFFERP (end->object)
25595 && (end->charpos >= start_charpos
25596 && end->charpos < end_charpos));
25597 ++end)
25599 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25600 are present at buffer positions between START_CHARPOS and
25601 END_CHARPOS, or if they come from an overlay. */
25602 if (EQ (end->object, before_string))
25604 pos = string_buffer_position (before_string, start_charpos);
25605 if (!pos || (pos >= start_charpos && pos < end_charpos))
25606 break;
25608 else if (EQ (end->object, after_string))
25610 pos = string_buffer_position (after_string, end_charpos);
25611 if (!pos || (pos >= start_charpos && pos < end_charpos))
25612 break;
25614 x += end->pixel_width;
25616 hlinfo->mouse_face_end_x = x;
25617 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
25620 hlinfo->mouse_face_window = window;
25621 hlinfo->mouse_face_face_id
25622 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
25623 mouse_charpos + 1,
25624 !hlinfo->mouse_face_hidden, -1);
25625 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25628 /* The following function is not used anymore (replaced with
25629 mouse_face_from_string_pos), but I leave it here for the time
25630 being, in case someone would. */
25632 #if 0 /* not used */
25634 /* Find the position of the glyph for position POS in OBJECT in
25635 window W's current matrix, and return in *X, *Y the pixel
25636 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
25638 RIGHT_P non-zero means return the position of the right edge of the
25639 glyph, RIGHT_P zero means return the left edge position.
25641 If no glyph for POS exists in the matrix, return the position of
25642 the glyph with the next smaller position that is in the matrix, if
25643 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
25644 exists in the matrix, return the position of the glyph with the
25645 next larger position in OBJECT.
25647 Value is non-zero if a glyph was found. */
25649 static int
25650 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
25651 int *hpos, int *vpos, int *x, int *y, int right_p)
25653 int yb = window_text_bottom_y (w);
25654 struct glyph_row *r;
25655 struct glyph *best_glyph = NULL;
25656 struct glyph_row *best_row = NULL;
25657 int best_x = 0;
25659 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25660 r->enabled_p && r->y < yb;
25661 ++r)
25663 struct glyph *g = r->glyphs[TEXT_AREA];
25664 struct glyph *e = g + r->used[TEXT_AREA];
25665 int gx;
25667 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
25668 if (EQ (g->object, object))
25670 if (g->charpos == pos)
25672 best_glyph = g;
25673 best_x = gx;
25674 best_row = r;
25675 goto found;
25677 else if (best_glyph == NULL
25678 || ((eabs (g->charpos - pos)
25679 < eabs (best_glyph->charpos - pos))
25680 && (right_p
25681 ? g->charpos < pos
25682 : g->charpos > pos)))
25684 best_glyph = g;
25685 best_x = gx;
25686 best_row = r;
25691 found:
25693 if (best_glyph)
25695 *x = best_x;
25696 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
25698 if (right_p)
25700 *x += best_glyph->pixel_width;
25701 ++*hpos;
25704 *y = best_row->y;
25705 *vpos = best_row - w->current_matrix->rows;
25708 return best_glyph != NULL;
25710 #endif /* not used */
25712 /* Find the positions of the first and the last glyphs in window W's
25713 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
25714 (assumed to be a string), and return in HLINFO's mouse_face_*
25715 members the pixel and column/row coordinates of those glyphs. */
25717 static void
25718 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
25719 Lisp_Object object,
25720 EMACS_INT startpos, EMACS_INT endpos)
25722 int yb = window_text_bottom_y (w);
25723 struct glyph_row *r;
25724 struct glyph *g, *e;
25725 int gx;
25726 int found = 0;
25728 /* Find the glyph row with at least one position in the range
25729 [STARTPOS..ENDPOS], and the first glyph in that row whose
25730 position belongs to that range. */
25731 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25732 r->enabled_p && r->y < yb;
25733 ++r)
25735 if (!r->reversed_p)
25737 g = r->glyphs[TEXT_AREA];
25738 e = g + r->used[TEXT_AREA];
25739 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
25740 if (EQ (g->object, object)
25741 && startpos <= g->charpos && g->charpos <= endpos)
25743 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
25744 hlinfo->mouse_face_beg_y = r->y;
25745 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
25746 hlinfo->mouse_face_beg_x = gx;
25747 found = 1;
25748 break;
25751 else
25753 struct glyph *g1;
25755 e = r->glyphs[TEXT_AREA];
25756 g = e + r->used[TEXT_AREA];
25757 for ( ; g > e; --g)
25758 if (EQ ((g-1)->object, object)
25759 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
25761 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
25762 hlinfo->mouse_face_beg_y = r->y;
25763 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
25764 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
25765 gx += g1->pixel_width;
25766 hlinfo->mouse_face_beg_x = gx;
25767 found = 1;
25768 break;
25771 if (found)
25772 break;
25775 if (!found)
25776 return;
25778 /* Starting with the next row, look for the first row which does NOT
25779 include any glyphs whose positions are in the range. */
25780 for (++r; r->enabled_p && r->y < yb; ++r)
25782 g = r->glyphs[TEXT_AREA];
25783 e = g + r->used[TEXT_AREA];
25784 found = 0;
25785 for ( ; g < e; ++g)
25786 if (EQ (g->object, object)
25787 && startpos <= g->charpos && g->charpos <= endpos)
25789 found = 1;
25790 break;
25792 if (!found)
25793 break;
25796 /* The highlighted region ends on the previous row. */
25797 r--;
25799 /* Set the end row and its vertical pixel coordinate. */
25800 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
25801 hlinfo->mouse_face_end_y = r->y;
25803 /* Compute and set the end column and the end column's horizontal
25804 pixel coordinate. */
25805 if (!r->reversed_p)
25807 g = r->glyphs[TEXT_AREA];
25808 e = g + r->used[TEXT_AREA];
25809 for ( ; e > g; --e)
25810 if (EQ ((e-1)->object, object)
25811 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
25812 break;
25813 hlinfo->mouse_face_end_col = e - g;
25815 for (gx = r->x; g < e; ++g)
25816 gx += g->pixel_width;
25817 hlinfo->mouse_face_end_x = gx;
25819 else
25821 e = r->glyphs[TEXT_AREA];
25822 g = e + r->used[TEXT_AREA];
25823 for (gx = r->x ; e < g; ++e)
25825 if (EQ (e->object, object)
25826 && startpos <= e->charpos && e->charpos <= endpos)
25827 break;
25828 gx += e->pixel_width;
25830 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
25831 hlinfo->mouse_face_end_x = gx;
25835 #ifdef HAVE_WINDOW_SYSTEM
25837 /* See if position X, Y is within a hot-spot of an image. */
25839 static int
25840 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
25842 if (!CONSP (hot_spot))
25843 return 0;
25845 if (EQ (XCAR (hot_spot), Qrect))
25847 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
25848 Lisp_Object rect = XCDR (hot_spot);
25849 Lisp_Object tem;
25850 if (!CONSP (rect))
25851 return 0;
25852 if (!CONSP (XCAR (rect)))
25853 return 0;
25854 if (!CONSP (XCDR (rect)))
25855 return 0;
25856 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
25857 return 0;
25858 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
25859 return 0;
25860 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
25861 return 0;
25862 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
25863 return 0;
25864 return 1;
25866 else if (EQ (XCAR (hot_spot), Qcircle))
25868 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
25869 Lisp_Object circ = XCDR (hot_spot);
25870 Lisp_Object lr, lx0, ly0;
25871 if (CONSP (circ)
25872 && CONSP (XCAR (circ))
25873 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
25874 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
25875 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
25877 double r = XFLOATINT (lr);
25878 double dx = XINT (lx0) - x;
25879 double dy = XINT (ly0) - y;
25880 return (dx * dx + dy * dy <= r * r);
25883 else if (EQ (XCAR (hot_spot), Qpoly))
25885 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
25886 if (VECTORP (XCDR (hot_spot)))
25888 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
25889 Lisp_Object *poly = v->contents;
25890 int n = v->header.size;
25891 int i;
25892 int inside = 0;
25893 Lisp_Object lx, ly;
25894 int x0, y0;
25896 /* Need an even number of coordinates, and at least 3 edges. */
25897 if (n < 6 || n & 1)
25898 return 0;
25900 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
25901 If count is odd, we are inside polygon. Pixels on edges
25902 may or may not be included depending on actual geometry of the
25903 polygon. */
25904 if ((lx = poly[n-2], !INTEGERP (lx))
25905 || (ly = poly[n-1], !INTEGERP (lx)))
25906 return 0;
25907 x0 = XINT (lx), y0 = XINT (ly);
25908 for (i = 0; i < n; i += 2)
25910 int x1 = x0, y1 = y0;
25911 if ((lx = poly[i], !INTEGERP (lx))
25912 || (ly = poly[i+1], !INTEGERP (ly)))
25913 return 0;
25914 x0 = XINT (lx), y0 = XINT (ly);
25916 /* Does this segment cross the X line? */
25917 if (x0 >= x)
25919 if (x1 >= x)
25920 continue;
25922 else if (x1 < x)
25923 continue;
25924 if (y > y0 && y > y1)
25925 continue;
25926 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
25927 inside = !inside;
25929 return inside;
25932 return 0;
25935 Lisp_Object
25936 find_hot_spot (Lisp_Object map, int x, int y)
25938 while (CONSP (map))
25940 if (CONSP (XCAR (map))
25941 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
25942 return XCAR (map);
25943 map = XCDR (map);
25946 return Qnil;
25949 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
25950 3, 3, 0,
25951 doc: /* Lookup in image map MAP coordinates X and Y.
25952 An image map is an alist where each element has the format (AREA ID PLIST).
25953 An AREA is specified as either a rectangle, a circle, or a polygon:
25954 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
25955 pixel coordinates of the upper left and bottom right corners.
25956 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
25957 and the radius of the circle; r may be a float or integer.
25958 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
25959 vector describes one corner in the polygon.
25960 Returns the alist element for the first matching AREA in MAP. */)
25961 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
25963 if (NILP (map))
25964 return Qnil;
25966 CHECK_NUMBER (x);
25967 CHECK_NUMBER (y);
25969 return find_hot_spot (map, XINT (x), XINT (y));
25973 /* Display frame CURSOR, optionally using shape defined by POINTER. */
25974 static void
25975 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
25977 /* Do not change cursor shape while dragging mouse. */
25978 if (!NILP (do_mouse_tracking))
25979 return;
25981 if (!NILP (pointer))
25983 if (EQ (pointer, Qarrow))
25984 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25985 else if (EQ (pointer, Qhand))
25986 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
25987 else if (EQ (pointer, Qtext))
25988 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25989 else if (EQ (pointer, intern ("hdrag")))
25990 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25991 #ifdef HAVE_X_WINDOWS
25992 else if (EQ (pointer, intern ("vdrag")))
25993 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
25994 #endif
25995 else if (EQ (pointer, intern ("hourglass")))
25996 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
25997 else if (EQ (pointer, Qmodeline))
25998 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
25999 else
26000 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26003 if (cursor != No_Cursor)
26004 FRAME_RIF (f)->define_frame_cursor (f, cursor);
26007 #endif /* HAVE_WINDOW_SYSTEM */
26009 /* Take proper action when mouse has moved to the mode or header line
26010 or marginal area AREA of window W, x-position X and y-position Y.
26011 X is relative to the start of the text display area of W, so the
26012 width of bitmap areas and scroll bars must be subtracted to get a
26013 position relative to the start of the mode line. */
26015 static void
26016 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
26017 enum window_part area)
26019 struct window *w = XWINDOW (window);
26020 struct frame *f = XFRAME (w->frame);
26021 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26022 #ifdef HAVE_WINDOW_SYSTEM
26023 Display_Info *dpyinfo;
26024 #endif
26025 Cursor cursor = No_Cursor;
26026 Lisp_Object pointer = Qnil;
26027 int dx, dy, width, height;
26028 EMACS_INT charpos;
26029 Lisp_Object string, object = Qnil;
26030 Lisp_Object pos, help;
26032 Lisp_Object mouse_face;
26033 int original_x_pixel = x;
26034 struct glyph * glyph = NULL, * row_start_glyph = NULL;
26035 struct glyph_row *row;
26037 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
26039 int x0;
26040 struct glyph *end;
26042 /* Kludge alert: mode_line_string takes X/Y in pixels, but
26043 returns them in row/column units! */
26044 string = mode_line_string (w, area, &x, &y, &charpos,
26045 &object, &dx, &dy, &width, &height);
26047 row = (area == ON_MODE_LINE
26048 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
26049 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
26051 /* Find the glyph under the mouse pointer. */
26052 if (row->mode_line_p && row->enabled_p)
26054 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
26055 end = glyph + row->used[TEXT_AREA];
26057 for (x0 = original_x_pixel;
26058 glyph < end && x0 >= glyph->pixel_width;
26059 ++glyph)
26060 x0 -= glyph->pixel_width;
26062 if (glyph >= end)
26063 glyph = NULL;
26066 else
26068 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
26069 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
26070 returns them in row/column units! */
26071 string = marginal_area_string (w, area, &x, &y, &charpos,
26072 &object, &dx, &dy, &width, &height);
26075 help = Qnil;
26077 #ifdef HAVE_WINDOW_SYSTEM
26078 if (IMAGEP (object))
26080 Lisp_Object image_map, hotspot;
26081 if ((image_map = Fplist_get (XCDR (object), QCmap),
26082 !NILP (image_map))
26083 && (hotspot = find_hot_spot (image_map, dx, dy),
26084 CONSP (hotspot))
26085 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
26087 Lisp_Object plist;
26089 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
26090 If so, we could look for mouse-enter, mouse-leave
26091 properties in PLIST (and do something...). */
26092 hotspot = XCDR (hotspot);
26093 if (CONSP (hotspot)
26094 && (plist = XCAR (hotspot), CONSP (plist)))
26096 pointer = Fplist_get (plist, Qpointer);
26097 if (NILP (pointer))
26098 pointer = Qhand;
26099 help = Fplist_get (plist, Qhelp_echo);
26100 if (!NILP (help))
26102 help_echo_string = help;
26103 /* Is this correct? ++kfs */
26104 XSETWINDOW (help_echo_window, w);
26105 help_echo_object = w->buffer;
26106 help_echo_pos = charpos;
26110 if (NILP (pointer))
26111 pointer = Fplist_get (XCDR (object), QCpointer);
26113 #endif /* HAVE_WINDOW_SYSTEM */
26115 if (STRINGP (string))
26117 pos = make_number (charpos);
26118 /* If we're on a string with `help-echo' text property, arrange
26119 for the help to be displayed. This is done by setting the
26120 global variable help_echo_string to the help string. */
26121 if (NILP (help))
26123 help = Fget_text_property (pos, Qhelp_echo, string);
26124 if (!NILP (help))
26126 help_echo_string = help;
26127 XSETWINDOW (help_echo_window, w);
26128 help_echo_object = string;
26129 help_echo_pos = charpos;
26133 #ifdef HAVE_WINDOW_SYSTEM
26134 if (FRAME_WINDOW_P (f))
26136 dpyinfo = FRAME_X_DISPLAY_INFO (f);
26137 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26138 if (NILP (pointer))
26139 pointer = Fget_text_property (pos, Qpointer, string);
26141 /* Change the mouse pointer according to what is under X/Y. */
26142 if (NILP (pointer)
26143 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
26145 Lisp_Object map;
26146 map = Fget_text_property (pos, Qlocal_map, string);
26147 if (!KEYMAPP (map))
26148 map = Fget_text_property (pos, Qkeymap, string);
26149 if (!KEYMAPP (map))
26150 cursor = dpyinfo->vertical_scroll_bar_cursor;
26153 #endif
26155 /* Change the mouse face according to what is under X/Y. */
26156 mouse_face = Fget_text_property (pos, Qmouse_face, string);
26157 if (!NILP (mouse_face)
26158 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
26159 && glyph)
26161 Lisp_Object b, e;
26163 struct glyph * tmp_glyph;
26165 int gpos;
26166 int gseq_length;
26167 int total_pixel_width;
26168 EMACS_INT begpos, endpos, ignore;
26170 int vpos, hpos;
26172 b = Fprevious_single_property_change (make_number (charpos + 1),
26173 Qmouse_face, string, Qnil);
26174 if (NILP (b))
26175 begpos = 0;
26176 else
26177 begpos = XINT (b);
26179 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
26180 if (NILP (e))
26181 endpos = SCHARS (string);
26182 else
26183 endpos = XINT (e);
26185 /* Calculate the glyph position GPOS of GLYPH in the
26186 displayed string, relative to the beginning of the
26187 highlighted part of the string.
26189 Note: GPOS is different from CHARPOS. CHARPOS is the
26190 position of GLYPH in the internal string object. A mode
26191 line string format has structures which are converted to
26192 a flattened string by the Emacs Lisp interpreter. The
26193 internal string is an element of those structures. The
26194 displayed string is the flattened string. */
26195 tmp_glyph = row_start_glyph;
26196 while (tmp_glyph < glyph
26197 && (!(EQ (tmp_glyph->object, glyph->object)
26198 && begpos <= tmp_glyph->charpos
26199 && tmp_glyph->charpos < endpos)))
26200 tmp_glyph++;
26201 gpos = glyph - tmp_glyph;
26203 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
26204 the highlighted part of the displayed string to which
26205 GLYPH belongs. Note: GSEQ_LENGTH is different from
26206 SCHARS (STRING), because the latter returns the length of
26207 the internal string. */
26208 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
26209 tmp_glyph > glyph
26210 && (!(EQ (tmp_glyph->object, glyph->object)
26211 && begpos <= tmp_glyph->charpos
26212 && tmp_glyph->charpos < endpos));
26213 tmp_glyph--)
26215 gseq_length = gpos + (tmp_glyph - glyph) + 1;
26217 /* Calculate the total pixel width of all the glyphs between
26218 the beginning of the highlighted area and GLYPH. */
26219 total_pixel_width = 0;
26220 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
26221 total_pixel_width += tmp_glyph->pixel_width;
26223 /* Pre calculation of re-rendering position. Note: X is in
26224 column units here, after the call to mode_line_string or
26225 marginal_area_string. */
26226 hpos = x - gpos;
26227 vpos = (area == ON_MODE_LINE
26228 ? (w->current_matrix)->nrows - 1
26229 : 0);
26231 /* If GLYPH's position is included in the region that is
26232 already drawn in mouse face, we have nothing to do. */
26233 if ( EQ (window, hlinfo->mouse_face_window)
26234 && (!row->reversed_p
26235 ? (hlinfo->mouse_face_beg_col <= hpos
26236 && hpos < hlinfo->mouse_face_end_col)
26237 /* In R2L rows we swap BEG and END, see below. */
26238 : (hlinfo->mouse_face_end_col <= hpos
26239 && hpos < hlinfo->mouse_face_beg_col))
26240 && hlinfo->mouse_face_beg_row == vpos )
26241 return;
26243 if (clear_mouse_face (hlinfo))
26244 cursor = No_Cursor;
26246 if (!row->reversed_p)
26248 hlinfo->mouse_face_beg_col = hpos;
26249 hlinfo->mouse_face_beg_x = original_x_pixel
26250 - (total_pixel_width + dx);
26251 hlinfo->mouse_face_end_col = hpos + gseq_length;
26252 hlinfo->mouse_face_end_x = 0;
26254 else
26256 /* In R2L rows, show_mouse_face expects BEG and END
26257 coordinates to be swapped. */
26258 hlinfo->mouse_face_end_col = hpos;
26259 hlinfo->mouse_face_end_x = original_x_pixel
26260 - (total_pixel_width + dx);
26261 hlinfo->mouse_face_beg_col = hpos + gseq_length;
26262 hlinfo->mouse_face_beg_x = 0;
26265 hlinfo->mouse_face_beg_row = vpos;
26266 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
26267 hlinfo->mouse_face_beg_y = 0;
26268 hlinfo->mouse_face_end_y = 0;
26269 hlinfo->mouse_face_past_end = 0;
26270 hlinfo->mouse_face_window = window;
26272 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
26273 charpos,
26274 0, 0, 0,
26275 &ignore,
26276 glyph->face_id,
26278 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26280 if (NILP (pointer))
26281 pointer = Qhand;
26283 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
26284 clear_mouse_face (hlinfo);
26286 #ifdef HAVE_WINDOW_SYSTEM
26287 if (FRAME_WINDOW_P (f))
26288 define_frame_cursor1 (f, cursor, pointer);
26289 #endif
26293 /* EXPORT:
26294 Take proper action when the mouse has moved to position X, Y on
26295 frame F as regards highlighting characters that have mouse-face
26296 properties. Also de-highlighting chars where the mouse was before.
26297 X and Y can be negative or out of range. */
26299 void
26300 note_mouse_highlight (struct frame *f, int x, int y)
26302 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26303 enum window_part part;
26304 Lisp_Object window;
26305 struct window *w;
26306 Cursor cursor = No_Cursor;
26307 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
26308 struct buffer *b;
26310 /* When a menu is active, don't highlight because this looks odd. */
26311 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
26312 if (popup_activated ())
26313 return;
26314 #endif
26316 if (NILP (Vmouse_highlight)
26317 || !f->glyphs_initialized_p
26318 || f->pointer_invisible)
26319 return;
26321 hlinfo->mouse_face_mouse_x = x;
26322 hlinfo->mouse_face_mouse_y = y;
26323 hlinfo->mouse_face_mouse_frame = f;
26325 if (hlinfo->mouse_face_defer)
26326 return;
26328 if (gc_in_progress)
26330 hlinfo->mouse_face_deferred_gc = 1;
26331 return;
26334 /* Which window is that in? */
26335 window = window_from_coordinates (f, x, y, &part, 1);
26337 /* If we were displaying active text in another window, clear that.
26338 Also clear if we move out of text area in same window. */
26339 if (! EQ (window, hlinfo->mouse_face_window)
26340 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
26341 && !NILP (hlinfo->mouse_face_window)))
26342 clear_mouse_face (hlinfo);
26344 /* Not on a window -> return. */
26345 if (!WINDOWP (window))
26346 return;
26348 /* Reset help_echo_string. It will get recomputed below. */
26349 help_echo_string = Qnil;
26351 /* Convert to window-relative pixel coordinates. */
26352 w = XWINDOW (window);
26353 frame_to_window_pixel_xy (w, &x, &y);
26355 #ifdef HAVE_WINDOW_SYSTEM
26356 /* Handle tool-bar window differently since it doesn't display a
26357 buffer. */
26358 if (EQ (window, f->tool_bar_window))
26360 note_tool_bar_highlight (f, x, y);
26361 return;
26363 #endif
26365 /* Mouse is on the mode, header line or margin? */
26366 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
26367 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
26369 note_mode_line_or_margin_highlight (window, x, y, part);
26370 return;
26373 #ifdef HAVE_WINDOW_SYSTEM
26374 if (part == ON_VERTICAL_BORDER)
26376 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26377 help_echo_string = build_string ("drag-mouse-1: resize");
26379 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
26380 || part == ON_SCROLL_BAR)
26381 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26382 else
26383 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26384 #endif
26386 /* Are we in a window whose display is up to date?
26387 And verify the buffer's text has not changed. */
26388 b = XBUFFER (w->buffer);
26389 if (part == ON_TEXT
26390 && EQ (w->window_end_valid, w->buffer)
26391 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
26392 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
26394 int hpos, vpos, dx, dy, area;
26395 EMACS_INT pos;
26396 struct glyph *glyph;
26397 Lisp_Object object;
26398 Lisp_Object mouse_face = Qnil, position;
26399 Lisp_Object *overlay_vec = NULL;
26400 ptrdiff_t i, noverlays;
26401 struct buffer *obuf;
26402 EMACS_INT obegv, ozv;
26403 int same_region;
26405 /* Find the glyph under X/Y. */
26406 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
26408 #ifdef HAVE_WINDOW_SYSTEM
26409 /* Look for :pointer property on image. */
26410 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26412 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26413 if (img != NULL && IMAGEP (img->spec))
26415 Lisp_Object image_map, hotspot;
26416 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
26417 !NILP (image_map))
26418 && (hotspot = find_hot_spot (image_map,
26419 glyph->slice.img.x + dx,
26420 glyph->slice.img.y + dy),
26421 CONSP (hotspot))
26422 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
26424 Lisp_Object plist;
26426 /* Could check XCAR (hotspot) to see if we enter/leave
26427 this hot-spot.
26428 If so, we could look for mouse-enter, mouse-leave
26429 properties in PLIST (and do something...). */
26430 hotspot = XCDR (hotspot);
26431 if (CONSP (hotspot)
26432 && (plist = XCAR (hotspot), CONSP (plist)))
26434 pointer = Fplist_get (plist, Qpointer);
26435 if (NILP (pointer))
26436 pointer = Qhand;
26437 help_echo_string = Fplist_get (plist, Qhelp_echo);
26438 if (!NILP (help_echo_string))
26440 help_echo_window = window;
26441 help_echo_object = glyph->object;
26442 help_echo_pos = glyph->charpos;
26446 if (NILP (pointer))
26447 pointer = Fplist_get (XCDR (img->spec), QCpointer);
26450 #endif /* HAVE_WINDOW_SYSTEM */
26452 /* Clear mouse face if X/Y not over text. */
26453 if (glyph == NULL
26454 || area != TEXT_AREA
26455 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
26456 /* Glyph's OBJECT is an integer for glyphs inserted by the
26457 display engine for its internal purposes, like truncation
26458 and continuation glyphs and blanks beyond the end of
26459 line's text on text terminals. If we are over such a
26460 glyph, we are not over any text. */
26461 || INTEGERP (glyph->object)
26462 /* R2L rows have a stretch glyph at their front, which
26463 stands for no text, whereas L2R rows have no glyphs at
26464 all beyond the end of text. Treat such stretch glyphs
26465 like we do with NULL glyphs in L2R rows. */
26466 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
26467 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
26468 && glyph->type == STRETCH_GLYPH
26469 && glyph->avoid_cursor_p))
26471 if (clear_mouse_face (hlinfo))
26472 cursor = No_Cursor;
26473 #ifdef HAVE_WINDOW_SYSTEM
26474 if (FRAME_WINDOW_P (f) && NILP (pointer))
26476 if (area != TEXT_AREA)
26477 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26478 else
26479 pointer = Vvoid_text_area_pointer;
26481 #endif
26482 goto set_cursor;
26485 pos = glyph->charpos;
26486 object = glyph->object;
26487 if (!STRINGP (object) && !BUFFERP (object))
26488 goto set_cursor;
26490 /* If we get an out-of-range value, return now; avoid an error. */
26491 if (BUFFERP (object) && pos > BUF_Z (b))
26492 goto set_cursor;
26494 /* Make the window's buffer temporarily current for
26495 overlays_at and compute_char_face. */
26496 obuf = current_buffer;
26497 current_buffer = b;
26498 obegv = BEGV;
26499 ozv = ZV;
26500 BEGV = BEG;
26501 ZV = Z;
26503 /* Is this char mouse-active or does it have help-echo? */
26504 position = make_number (pos);
26506 if (BUFFERP (object))
26508 /* Put all the overlays we want in a vector in overlay_vec. */
26509 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
26510 /* Sort overlays into increasing priority order. */
26511 noverlays = sort_overlays (overlay_vec, noverlays, w);
26513 else
26514 noverlays = 0;
26516 same_region = coords_in_mouse_face_p (w, hpos, vpos);
26518 if (same_region)
26519 cursor = No_Cursor;
26521 /* Check mouse-face highlighting. */
26522 if (! same_region
26523 /* If there exists an overlay with mouse-face overlapping
26524 the one we are currently highlighting, we have to
26525 check if we enter the overlapping overlay, and then
26526 highlight only that. */
26527 || (OVERLAYP (hlinfo->mouse_face_overlay)
26528 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
26530 /* Find the highest priority overlay with a mouse-face. */
26531 Lisp_Object overlay = Qnil;
26532 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
26534 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
26535 if (!NILP (mouse_face))
26536 overlay = overlay_vec[i];
26539 /* If we're highlighting the same overlay as before, there's
26540 no need to do that again. */
26541 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
26542 goto check_help_echo;
26543 hlinfo->mouse_face_overlay = overlay;
26545 /* Clear the display of the old active region, if any. */
26546 if (clear_mouse_face (hlinfo))
26547 cursor = No_Cursor;
26549 /* If no overlay applies, get a text property. */
26550 if (NILP (overlay))
26551 mouse_face = Fget_text_property (position, Qmouse_face, object);
26553 /* Next, compute the bounds of the mouse highlighting and
26554 display it. */
26555 if (!NILP (mouse_face) && STRINGP (object))
26557 /* The mouse-highlighting comes from a display string
26558 with a mouse-face. */
26559 Lisp_Object s, e;
26560 EMACS_INT ignore;
26562 s = Fprevious_single_property_change
26563 (make_number (pos + 1), Qmouse_face, object, Qnil);
26564 e = Fnext_single_property_change
26565 (position, Qmouse_face, object, Qnil);
26566 if (NILP (s))
26567 s = make_number (0);
26568 if (NILP (e))
26569 e = make_number (SCHARS (object) - 1);
26570 mouse_face_from_string_pos (w, hlinfo, object,
26571 XINT (s), XINT (e));
26572 hlinfo->mouse_face_past_end = 0;
26573 hlinfo->mouse_face_window = window;
26574 hlinfo->mouse_face_face_id
26575 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
26576 glyph->face_id, 1);
26577 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26578 cursor = No_Cursor;
26580 else
26582 /* The mouse-highlighting, if any, comes from an overlay
26583 or text property in the buffer. */
26584 Lisp_Object buffer IF_LINT (= Qnil);
26585 Lisp_Object cover_string IF_LINT (= Qnil);
26587 if (STRINGP (object))
26589 /* If we are on a display string with no mouse-face,
26590 check if the text under it has one. */
26591 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
26592 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26593 pos = string_buffer_position (object, start);
26594 if (pos > 0)
26596 mouse_face = get_char_property_and_overlay
26597 (make_number (pos), Qmouse_face, w->buffer, &overlay);
26598 buffer = w->buffer;
26599 cover_string = object;
26602 else
26604 buffer = object;
26605 cover_string = Qnil;
26608 if (!NILP (mouse_face))
26610 Lisp_Object before, after;
26611 Lisp_Object before_string, after_string;
26612 /* To correctly find the limits of mouse highlight
26613 in a bidi-reordered buffer, we must not use the
26614 optimization of limiting the search in
26615 previous-single-property-change and
26616 next-single-property-change, because
26617 rows_from_pos_range needs the real start and end
26618 positions to DTRT in this case. That's because
26619 the first row visible in a window does not
26620 necessarily display the character whose position
26621 is the smallest. */
26622 Lisp_Object lim1 =
26623 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
26624 ? Fmarker_position (w->start)
26625 : Qnil;
26626 Lisp_Object lim2 =
26627 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
26628 ? make_number (BUF_Z (XBUFFER (buffer))
26629 - XFASTINT (w->window_end_pos))
26630 : Qnil;
26632 if (NILP (overlay))
26634 /* Handle the text property case. */
26635 before = Fprevious_single_property_change
26636 (make_number (pos + 1), Qmouse_face, buffer, lim1);
26637 after = Fnext_single_property_change
26638 (make_number (pos), Qmouse_face, buffer, lim2);
26639 before_string = after_string = Qnil;
26641 else
26643 /* Handle the overlay case. */
26644 before = Foverlay_start (overlay);
26645 after = Foverlay_end (overlay);
26646 before_string = Foverlay_get (overlay, Qbefore_string);
26647 after_string = Foverlay_get (overlay, Qafter_string);
26649 if (!STRINGP (before_string)) before_string = Qnil;
26650 if (!STRINGP (after_string)) after_string = Qnil;
26653 mouse_face_from_buffer_pos (window, hlinfo, pos,
26654 XFASTINT (before),
26655 XFASTINT (after),
26656 before_string, after_string,
26657 cover_string);
26658 cursor = No_Cursor;
26663 check_help_echo:
26665 /* Look for a `help-echo' property. */
26666 if (NILP (help_echo_string)) {
26667 Lisp_Object help, overlay;
26669 /* Check overlays first. */
26670 help = overlay = Qnil;
26671 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
26673 overlay = overlay_vec[i];
26674 help = Foverlay_get (overlay, Qhelp_echo);
26677 if (!NILP (help))
26679 help_echo_string = help;
26680 help_echo_window = window;
26681 help_echo_object = overlay;
26682 help_echo_pos = pos;
26684 else
26686 Lisp_Object obj = glyph->object;
26687 EMACS_INT charpos = glyph->charpos;
26689 /* Try text properties. */
26690 if (STRINGP (obj)
26691 && charpos >= 0
26692 && charpos < SCHARS (obj))
26694 help = Fget_text_property (make_number (charpos),
26695 Qhelp_echo, obj);
26696 if (NILP (help))
26698 /* If the string itself doesn't specify a help-echo,
26699 see if the buffer text ``under'' it does. */
26700 struct glyph_row *r
26701 = MATRIX_ROW (w->current_matrix, vpos);
26702 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26703 EMACS_INT p = string_buffer_position (obj, start);
26704 if (p > 0)
26706 help = Fget_char_property (make_number (p),
26707 Qhelp_echo, w->buffer);
26708 if (!NILP (help))
26710 charpos = p;
26711 obj = w->buffer;
26716 else if (BUFFERP (obj)
26717 && charpos >= BEGV
26718 && charpos < ZV)
26719 help = Fget_text_property (make_number (charpos), Qhelp_echo,
26720 obj);
26722 if (!NILP (help))
26724 help_echo_string = help;
26725 help_echo_window = window;
26726 help_echo_object = obj;
26727 help_echo_pos = charpos;
26732 #ifdef HAVE_WINDOW_SYSTEM
26733 /* Look for a `pointer' property. */
26734 if (FRAME_WINDOW_P (f) && NILP (pointer))
26736 /* Check overlays first. */
26737 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
26738 pointer = Foverlay_get (overlay_vec[i], Qpointer);
26740 if (NILP (pointer))
26742 Lisp_Object obj = glyph->object;
26743 EMACS_INT charpos = glyph->charpos;
26745 /* Try text properties. */
26746 if (STRINGP (obj)
26747 && charpos >= 0
26748 && charpos < SCHARS (obj))
26750 pointer = Fget_text_property (make_number (charpos),
26751 Qpointer, obj);
26752 if (NILP (pointer))
26754 /* If the string itself doesn't specify a pointer,
26755 see if the buffer text ``under'' it does. */
26756 struct glyph_row *r
26757 = MATRIX_ROW (w->current_matrix, vpos);
26758 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26759 EMACS_INT p = string_buffer_position (obj, start);
26760 if (p > 0)
26761 pointer = Fget_char_property (make_number (p),
26762 Qpointer, w->buffer);
26765 else if (BUFFERP (obj)
26766 && charpos >= BEGV
26767 && charpos < ZV)
26768 pointer = Fget_text_property (make_number (charpos),
26769 Qpointer, obj);
26772 #endif /* HAVE_WINDOW_SYSTEM */
26774 BEGV = obegv;
26775 ZV = ozv;
26776 current_buffer = obuf;
26779 set_cursor:
26781 #ifdef HAVE_WINDOW_SYSTEM
26782 if (FRAME_WINDOW_P (f))
26783 define_frame_cursor1 (f, cursor, pointer);
26784 #else
26785 /* This is here to prevent a compiler error, about "label at end of
26786 compound statement". */
26787 return;
26788 #endif
26792 /* EXPORT for RIF:
26793 Clear any mouse-face on window W. This function is part of the
26794 redisplay interface, and is called from try_window_id and similar
26795 functions to ensure the mouse-highlight is off. */
26797 void
26798 x_clear_window_mouse_face (struct window *w)
26800 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26801 Lisp_Object window;
26803 BLOCK_INPUT;
26804 XSETWINDOW (window, w);
26805 if (EQ (window, hlinfo->mouse_face_window))
26806 clear_mouse_face (hlinfo);
26807 UNBLOCK_INPUT;
26811 /* EXPORT:
26812 Just discard the mouse face information for frame F, if any.
26813 This is used when the size of F is changed. */
26815 void
26816 cancel_mouse_face (struct frame *f)
26818 Lisp_Object window;
26819 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26821 window = hlinfo->mouse_face_window;
26822 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
26824 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26825 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26826 hlinfo->mouse_face_window = Qnil;
26832 /***********************************************************************
26833 Exposure Events
26834 ***********************************************************************/
26836 #ifdef HAVE_WINDOW_SYSTEM
26838 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
26839 which intersects rectangle R. R is in window-relative coordinates. */
26841 static void
26842 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
26843 enum glyph_row_area area)
26845 struct glyph *first = row->glyphs[area];
26846 struct glyph *end = row->glyphs[area] + row->used[area];
26847 struct glyph *last;
26848 int first_x, start_x, x;
26850 if (area == TEXT_AREA && row->fill_line_p)
26851 /* If row extends face to end of line write the whole line. */
26852 draw_glyphs (w, 0, row, area,
26853 0, row->used[area],
26854 DRAW_NORMAL_TEXT, 0);
26855 else
26857 /* Set START_X to the window-relative start position for drawing glyphs of
26858 AREA. The first glyph of the text area can be partially visible.
26859 The first glyphs of other areas cannot. */
26860 start_x = window_box_left_offset (w, area);
26861 x = start_x;
26862 if (area == TEXT_AREA)
26863 x += row->x;
26865 /* Find the first glyph that must be redrawn. */
26866 while (first < end
26867 && x + first->pixel_width < r->x)
26869 x += first->pixel_width;
26870 ++first;
26873 /* Find the last one. */
26874 last = first;
26875 first_x = x;
26876 while (last < end
26877 && x < r->x + r->width)
26879 x += last->pixel_width;
26880 ++last;
26883 /* Repaint. */
26884 if (last > first)
26885 draw_glyphs (w, first_x - start_x, row, area,
26886 first - row->glyphs[area], last - row->glyphs[area],
26887 DRAW_NORMAL_TEXT, 0);
26892 /* Redraw the parts of the glyph row ROW on window W intersecting
26893 rectangle R. R is in window-relative coordinates. Value is
26894 non-zero if mouse-face was overwritten. */
26896 static int
26897 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
26899 xassert (row->enabled_p);
26901 if (row->mode_line_p || w->pseudo_window_p)
26902 draw_glyphs (w, 0, row, TEXT_AREA,
26903 0, row->used[TEXT_AREA],
26904 DRAW_NORMAL_TEXT, 0);
26905 else
26907 if (row->used[LEFT_MARGIN_AREA])
26908 expose_area (w, row, r, LEFT_MARGIN_AREA);
26909 if (row->used[TEXT_AREA])
26910 expose_area (w, row, r, TEXT_AREA);
26911 if (row->used[RIGHT_MARGIN_AREA])
26912 expose_area (w, row, r, RIGHT_MARGIN_AREA);
26913 draw_row_fringe_bitmaps (w, row);
26916 return row->mouse_face_p;
26920 /* Redraw those parts of glyphs rows during expose event handling that
26921 overlap other rows. Redrawing of an exposed line writes over parts
26922 of lines overlapping that exposed line; this function fixes that.
26924 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
26925 row in W's current matrix that is exposed and overlaps other rows.
26926 LAST_OVERLAPPING_ROW is the last such row. */
26928 static void
26929 expose_overlaps (struct window *w,
26930 struct glyph_row *first_overlapping_row,
26931 struct glyph_row *last_overlapping_row,
26932 XRectangle *r)
26934 struct glyph_row *row;
26936 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
26937 if (row->overlapping_p)
26939 xassert (row->enabled_p && !row->mode_line_p);
26941 row->clip = r;
26942 if (row->used[LEFT_MARGIN_AREA])
26943 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
26945 if (row->used[TEXT_AREA])
26946 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
26948 if (row->used[RIGHT_MARGIN_AREA])
26949 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
26950 row->clip = NULL;
26955 /* Return non-zero if W's cursor intersects rectangle R. */
26957 static int
26958 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
26960 XRectangle cr, result;
26961 struct glyph *cursor_glyph;
26962 struct glyph_row *row;
26964 if (w->phys_cursor.vpos >= 0
26965 && w->phys_cursor.vpos < w->current_matrix->nrows
26966 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
26967 row->enabled_p)
26968 && row->cursor_in_fringe_p)
26970 /* Cursor is in the fringe. */
26971 cr.x = window_box_right_offset (w,
26972 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
26973 ? RIGHT_MARGIN_AREA
26974 : TEXT_AREA));
26975 cr.y = row->y;
26976 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
26977 cr.height = row->height;
26978 return x_intersect_rectangles (&cr, r, &result);
26981 cursor_glyph = get_phys_cursor_glyph (w);
26982 if (cursor_glyph)
26984 /* r is relative to W's box, but w->phys_cursor.x is relative
26985 to left edge of W's TEXT area. Adjust it. */
26986 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
26987 cr.y = w->phys_cursor.y;
26988 cr.width = cursor_glyph->pixel_width;
26989 cr.height = w->phys_cursor_height;
26990 /* ++KFS: W32 version used W32-specific IntersectRect here, but
26991 I assume the effect is the same -- and this is portable. */
26992 return x_intersect_rectangles (&cr, r, &result);
26994 /* If we don't understand the format, pretend we're not in the hot-spot. */
26995 return 0;
26999 /* EXPORT:
27000 Draw a vertical window border to the right of window W if W doesn't
27001 have vertical scroll bars. */
27003 void
27004 x_draw_vertical_border (struct window *w)
27006 struct frame *f = XFRAME (WINDOW_FRAME (w));
27008 /* We could do better, if we knew what type of scroll-bar the adjacent
27009 windows (on either side) have... But we don't :-(
27010 However, I think this works ok. ++KFS 2003-04-25 */
27012 /* Redraw borders between horizontally adjacent windows. Don't
27013 do it for frames with vertical scroll bars because either the
27014 right scroll bar of a window, or the left scroll bar of its
27015 neighbor will suffice as a border. */
27016 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
27017 return;
27019 if (!WINDOW_RIGHTMOST_P (w)
27020 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
27022 int x0, x1, y0, y1;
27024 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27025 y1 -= 1;
27027 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27028 x1 -= 1;
27030 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
27032 else if (!WINDOW_LEFTMOST_P (w)
27033 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
27035 int x0, x1, y0, y1;
27037 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27038 y1 -= 1;
27040 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27041 x0 -= 1;
27043 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
27048 /* Redraw the part of window W intersection rectangle FR. Pixel
27049 coordinates in FR are frame-relative. Call this function with
27050 input blocked. Value is non-zero if the exposure overwrites
27051 mouse-face. */
27053 static int
27054 expose_window (struct window *w, XRectangle *fr)
27056 struct frame *f = XFRAME (w->frame);
27057 XRectangle wr, r;
27058 int mouse_face_overwritten_p = 0;
27060 /* If window is not yet fully initialized, do nothing. This can
27061 happen when toolkit scroll bars are used and a window is split.
27062 Reconfiguring the scroll bar will generate an expose for a newly
27063 created window. */
27064 if (w->current_matrix == NULL)
27065 return 0;
27067 /* When we're currently updating the window, display and current
27068 matrix usually don't agree. Arrange for a thorough display
27069 later. */
27070 if (w == updated_window)
27072 SET_FRAME_GARBAGED (f);
27073 return 0;
27076 /* Frame-relative pixel rectangle of W. */
27077 wr.x = WINDOW_LEFT_EDGE_X (w);
27078 wr.y = WINDOW_TOP_EDGE_Y (w);
27079 wr.width = WINDOW_TOTAL_WIDTH (w);
27080 wr.height = WINDOW_TOTAL_HEIGHT (w);
27082 if (x_intersect_rectangles (fr, &wr, &r))
27084 int yb = window_text_bottom_y (w);
27085 struct glyph_row *row;
27086 int cursor_cleared_p;
27087 struct glyph_row *first_overlapping_row, *last_overlapping_row;
27089 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
27090 r.x, r.y, r.width, r.height));
27092 /* Convert to window coordinates. */
27093 r.x -= WINDOW_LEFT_EDGE_X (w);
27094 r.y -= WINDOW_TOP_EDGE_Y (w);
27096 /* Turn off the cursor. */
27097 if (!w->pseudo_window_p
27098 && phys_cursor_in_rect_p (w, &r))
27100 x_clear_cursor (w);
27101 cursor_cleared_p = 1;
27103 else
27104 cursor_cleared_p = 0;
27106 /* Update lines intersecting rectangle R. */
27107 first_overlapping_row = last_overlapping_row = NULL;
27108 for (row = w->current_matrix->rows;
27109 row->enabled_p;
27110 ++row)
27112 int y0 = row->y;
27113 int y1 = MATRIX_ROW_BOTTOM_Y (row);
27115 if ((y0 >= r.y && y0 < r.y + r.height)
27116 || (y1 > r.y && y1 < r.y + r.height)
27117 || (r.y >= y0 && r.y < y1)
27118 || (r.y + r.height > y0 && r.y + r.height < y1))
27120 /* A header line may be overlapping, but there is no need
27121 to fix overlapping areas for them. KFS 2005-02-12 */
27122 if (row->overlapping_p && !row->mode_line_p)
27124 if (first_overlapping_row == NULL)
27125 first_overlapping_row = row;
27126 last_overlapping_row = row;
27129 row->clip = fr;
27130 if (expose_line (w, row, &r))
27131 mouse_face_overwritten_p = 1;
27132 row->clip = NULL;
27134 else if (row->overlapping_p)
27136 /* We must redraw a row overlapping the exposed area. */
27137 if (y0 < r.y
27138 ? y0 + row->phys_height > r.y
27139 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
27141 if (first_overlapping_row == NULL)
27142 first_overlapping_row = row;
27143 last_overlapping_row = row;
27147 if (y1 >= yb)
27148 break;
27151 /* Display the mode line if there is one. */
27152 if (WINDOW_WANTS_MODELINE_P (w)
27153 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
27154 row->enabled_p)
27155 && row->y < r.y + r.height)
27157 if (expose_line (w, row, &r))
27158 mouse_face_overwritten_p = 1;
27161 if (!w->pseudo_window_p)
27163 /* Fix the display of overlapping rows. */
27164 if (first_overlapping_row)
27165 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
27166 fr);
27168 /* Draw border between windows. */
27169 x_draw_vertical_border (w);
27171 /* Turn the cursor on again. */
27172 if (cursor_cleared_p)
27173 update_window_cursor (w, 1);
27177 return mouse_face_overwritten_p;
27182 /* Redraw (parts) of all windows in the window tree rooted at W that
27183 intersect R. R contains frame pixel coordinates. Value is
27184 non-zero if the exposure overwrites mouse-face. */
27186 static int
27187 expose_window_tree (struct window *w, XRectangle *r)
27189 struct frame *f = XFRAME (w->frame);
27190 int mouse_face_overwritten_p = 0;
27192 while (w && !FRAME_GARBAGED_P (f))
27194 if (!NILP (w->hchild))
27195 mouse_face_overwritten_p
27196 |= expose_window_tree (XWINDOW (w->hchild), r);
27197 else if (!NILP (w->vchild))
27198 mouse_face_overwritten_p
27199 |= expose_window_tree (XWINDOW (w->vchild), r);
27200 else
27201 mouse_face_overwritten_p |= expose_window (w, r);
27203 w = NILP (w->next) ? NULL : XWINDOW (w->next);
27206 return mouse_face_overwritten_p;
27210 /* EXPORT:
27211 Redisplay an exposed area of frame F. X and Y are the upper-left
27212 corner of the exposed rectangle. W and H are width and height of
27213 the exposed area. All are pixel values. W or H zero means redraw
27214 the entire frame. */
27216 void
27217 expose_frame (struct frame *f, int x, int y, int w, int h)
27219 XRectangle r;
27220 int mouse_face_overwritten_p = 0;
27222 TRACE ((stderr, "expose_frame "));
27224 /* No need to redraw if frame will be redrawn soon. */
27225 if (FRAME_GARBAGED_P (f))
27227 TRACE ((stderr, " garbaged\n"));
27228 return;
27231 /* If basic faces haven't been realized yet, there is no point in
27232 trying to redraw anything. This can happen when we get an expose
27233 event while Emacs is starting, e.g. by moving another window. */
27234 if (FRAME_FACE_CACHE (f) == NULL
27235 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
27237 TRACE ((stderr, " no faces\n"));
27238 return;
27241 if (w == 0 || h == 0)
27243 r.x = r.y = 0;
27244 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
27245 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
27247 else
27249 r.x = x;
27250 r.y = y;
27251 r.width = w;
27252 r.height = h;
27255 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
27256 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
27258 if (WINDOWP (f->tool_bar_window))
27259 mouse_face_overwritten_p
27260 |= expose_window (XWINDOW (f->tool_bar_window), &r);
27262 #ifdef HAVE_X_WINDOWS
27263 #ifndef MSDOS
27264 #ifndef USE_X_TOOLKIT
27265 if (WINDOWP (f->menu_bar_window))
27266 mouse_face_overwritten_p
27267 |= expose_window (XWINDOW (f->menu_bar_window), &r);
27268 #endif /* not USE_X_TOOLKIT */
27269 #endif
27270 #endif
27272 /* Some window managers support a focus-follows-mouse style with
27273 delayed raising of frames. Imagine a partially obscured frame,
27274 and moving the mouse into partially obscured mouse-face on that
27275 frame. The visible part of the mouse-face will be highlighted,
27276 then the WM raises the obscured frame. With at least one WM, KDE
27277 2.1, Emacs is not getting any event for the raising of the frame
27278 (even tried with SubstructureRedirectMask), only Expose events.
27279 These expose events will draw text normally, i.e. not
27280 highlighted. Which means we must redo the highlight here.
27281 Subsume it under ``we love X''. --gerd 2001-08-15 */
27282 /* Included in Windows version because Windows most likely does not
27283 do the right thing if any third party tool offers
27284 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
27285 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
27287 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27288 if (f == hlinfo->mouse_face_mouse_frame)
27290 int mouse_x = hlinfo->mouse_face_mouse_x;
27291 int mouse_y = hlinfo->mouse_face_mouse_y;
27292 clear_mouse_face (hlinfo);
27293 note_mouse_highlight (f, mouse_x, mouse_y);
27299 /* EXPORT:
27300 Determine the intersection of two rectangles R1 and R2. Return
27301 the intersection in *RESULT. Value is non-zero if RESULT is not
27302 empty. */
27305 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
27307 XRectangle *left, *right;
27308 XRectangle *upper, *lower;
27309 int intersection_p = 0;
27311 /* Rearrange so that R1 is the left-most rectangle. */
27312 if (r1->x < r2->x)
27313 left = r1, right = r2;
27314 else
27315 left = r2, right = r1;
27317 /* X0 of the intersection is right.x0, if this is inside R1,
27318 otherwise there is no intersection. */
27319 if (right->x <= left->x + left->width)
27321 result->x = right->x;
27323 /* The right end of the intersection is the minimum of
27324 the right ends of left and right. */
27325 result->width = (min (left->x + left->width, right->x + right->width)
27326 - result->x);
27328 /* Same game for Y. */
27329 if (r1->y < r2->y)
27330 upper = r1, lower = r2;
27331 else
27332 upper = r2, lower = r1;
27334 /* The upper end of the intersection is lower.y0, if this is inside
27335 of upper. Otherwise, there is no intersection. */
27336 if (lower->y <= upper->y + upper->height)
27338 result->y = lower->y;
27340 /* The lower end of the intersection is the minimum of the lower
27341 ends of upper and lower. */
27342 result->height = (min (lower->y + lower->height,
27343 upper->y + upper->height)
27344 - result->y);
27345 intersection_p = 1;
27349 return intersection_p;
27352 #endif /* HAVE_WINDOW_SYSTEM */
27355 /***********************************************************************
27356 Initialization
27357 ***********************************************************************/
27359 void
27360 syms_of_xdisp (void)
27362 Vwith_echo_area_save_vector = Qnil;
27363 staticpro (&Vwith_echo_area_save_vector);
27365 Vmessage_stack = Qnil;
27366 staticpro (&Vmessage_stack);
27368 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
27370 message_dolog_marker1 = Fmake_marker ();
27371 staticpro (&message_dolog_marker1);
27372 message_dolog_marker2 = Fmake_marker ();
27373 staticpro (&message_dolog_marker2);
27374 message_dolog_marker3 = Fmake_marker ();
27375 staticpro (&message_dolog_marker3);
27377 #if GLYPH_DEBUG
27378 defsubr (&Sdump_frame_glyph_matrix);
27379 defsubr (&Sdump_glyph_matrix);
27380 defsubr (&Sdump_glyph_row);
27381 defsubr (&Sdump_tool_bar_row);
27382 defsubr (&Strace_redisplay);
27383 defsubr (&Strace_to_stderr);
27384 #endif
27385 #ifdef HAVE_WINDOW_SYSTEM
27386 defsubr (&Stool_bar_lines_needed);
27387 defsubr (&Slookup_image_map);
27388 #endif
27389 defsubr (&Sformat_mode_line);
27390 defsubr (&Sinvisible_p);
27391 defsubr (&Scurrent_bidi_paragraph_direction);
27393 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
27394 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
27395 DEFSYM (Qoverriding_local_map, "overriding-local-map");
27396 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
27397 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
27398 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
27399 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
27400 DEFSYM (Qeval, "eval");
27401 DEFSYM (QCdata, ":data");
27402 DEFSYM (Qdisplay, "display");
27403 DEFSYM (Qspace_width, "space-width");
27404 DEFSYM (Qraise, "raise");
27405 DEFSYM (Qslice, "slice");
27406 DEFSYM (Qspace, "space");
27407 DEFSYM (Qmargin, "margin");
27408 DEFSYM (Qpointer, "pointer");
27409 DEFSYM (Qleft_margin, "left-margin");
27410 DEFSYM (Qright_margin, "right-margin");
27411 DEFSYM (Qcenter, "center");
27412 DEFSYM (Qline_height, "line-height");
27413 DEFSYM (QCalign_to, ":align-to");
27414 DEFSYM (QCrelative_width, ":relative-width");
27415 DEFSYM (QCrelative_height, ":relative-height");
27416 DEFSYM (QCeval, ":eval");
27417 DEFSYM (QCpropertize, ":propertize");
27418 DEFSYM (QCfile, ":file");
27419 DEFSYM (Qfontified, "fontified");
27420 DEFSYM (Qfontification_functions, "fontification-functions");
27421 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
27422 DEFSYM (Qescape_glyph, "escape-glyph");
27423 DEFSYM (Qnobreak_space, "nobreak-space");
27424 DEFSYM (Qimage, "image");
27425 DEFSYM (Qtext, "text");
27426 DEFSYM (Qboth, "both");
27427 DEFSYM (Qboth_horiz, "both-horiz");
27428 DEFSYM (Qtext_image_horiz, "text-image-horiz");
27429 DEFSYM (QCmap, ":map");
27430 DEFSYM (QCpointer, ":pointer");
27431 DEFSYM (Qrect, "rect");
27432 DEFSYM (Qcircle, "circle");
27433 DEFSYM (Qpoly, "poly");
27434 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
27435 DEFSYM (Qgrow_only, "grow-only");
27436 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
27437 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
27438 DEFSYM (Qposition, "position");
27439 DEFSYM (Qbuffer_position, "buffer-position");
27440 DEFSYM (Qobject, "object");
27441 DEFSYM (Qbar, "bar");
27442 DEFSYM (Qhbar, "hbar");
27443 DEFSYM (Qbox, "box");
27444 DEFSYM (Qhollow, "hollow");
27445 DEFSYM (Qhand, "hand");
27446 DEFSYM (Qarrow, "arrow");
27447 DEFSYM (Qtext, "text");
27448 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
27450 list_of_error = Fcons (Fcons (intern_c_string ("error"),
27451 Fcons (intern_c_string ("void-variable"), Qnil)),
27452 Qnil);
27453 staticpro (&list_of_error);
27455 DEFSYM (Qlast_arrow_position, "last-arrow-position");
27456 DEFSYM (Qlast_arrow_string, "last-arrow-string");
27457 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
27458 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
27460 echo_buffer[0] = echo_buffer[1] = Qnil;
27461 staticpro (&echo_buffer[0]);
27462 staticpro (&echo_buffer[1]);
27464 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
27465 staticpro (&echo_area_buffer[0]);
27466 staticpro (&echo_area_buffer[1]);
27468 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
27469 staticpro (&Vmessages_buffer_name);
27471 mode_line_proptrans_alist = Qnil;
27472 staticpro (&mode_line_proptrans_alist);
27473 mode_line_string_list = Qnil;
27474 staticpro (&mode_line_string_list);
27475 mode_line_string_face = Qnil;
27476 staticpro (&mode_line_string_face);
27477 mode_line_string_face_prop = Qnil;
27478 staticpro (&mode_line_string_face_prop);
27479 Vmode_line_unwind_vector = Qnil;
27480 staticpro (&Vmode_line_unwind_vector);
27482 help_echo_string = Qnil;
27483 staticpro (&help_echo_string);
27484 help_echo_object = Qnil;
27485 staticpro (&help_echo_object);
27486 help_echo_window = Qnil;
27487 staticpro (&help_echo_window);
27488 previous_help_echo_string = Qnil;
27489 staticpro (&previous_help_echo_string);
27490 help_echo_pos = -1;
27492 DEFSYM (Qright_to_left, "right-to-left");
27493 DEFSYM (Qleft_to_right, "left-to-right");
27495 #ifdef HAVE_WINDOW_SYSTEM
27496 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
27497 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
27498 For example, if a block cursor is over a tab, it will be drawn as
27499 wide as that tab on the display. */);
27500 x_stretch_cursor_p = 0;
27501 #endif
27503 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
27504 doc: /* *Non-nil means highlight trailing whitespace.
27505 The face used for trailing whitespace is `trailing-whitespace'. */);
27506 Vshow_trailing_whitespace = Qnil;
27508 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
27509 doc: /* *Control highlighting of nobreak space and soft hyphen.
27510 A value of t means highlight the character itself (for nobreak space,
27511 use face `nobreak-space').
27512 A value of nil means no highlighting.
27513 Other values mean display the escape glyph followed by an ordinary
27514 space or ordinary hyphen. */);
27515 Vnobreak_char_display = Qt;
27517 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
27518 doc: /* *The pointer shape to show in void text areas.
27519 A value of nil means to show the text pointer. Other options are `arrow',
27520 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
27521 Vvoid_text_area_pointer = Qarrow;
27523 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
27524 doc: /* Non-nil means don't actually do any redisplay.
27525 This is used for internal purposes. */);
27526 Vinhibit_redisplay = Qnil;
27528 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
27529 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
27530 Vglobal_mode_string = Qnil;
27532 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
27533 doc: /* Marker for where to display an arrow on top of the buffer text.
27534 This must be the beginning of a line in order to work.
27535 See also `overlay-arrow-string'. */);
27536 Voverlay_arrow_position = Qnil;
27538 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
27539 doc: /* String to display as an arrow in non-window frames.
27540 See also `overlay-arrow-position'. */);
27541 Voverlay_arrow_string = make_pure_c_string ("=>");
27543 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
27544 doc: /* List of variables (symbols) which hold markers for overlay arrows.
27545 The symbols on this list are examined during redisplay to determine
27546 where to display overlay arrows. */);
27547 Voverlay_arrow_variable_list
27548 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
27550 DEFVAR_INT ("scroll-step", emacs_scroll_step,
27551 doc: /* *The number of lines to try scrolling a window by when point moves out.
27552 If that fails to bring point back on frame, point is centered instead.
27553 If this is zero, point is always centered after it moves off frame.
27554 If you want scrolling to always be a line at a time, you should set
27555 `scroll-conservatively' to a large value rather than set this to 1. */);
27557 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
27558 doc: /* *Scroll up to this many lines, to bring point back on screen.
27559 If point moves off-screen, redisplay will scroll by up to
27560 `scroll-conservatively' lines in order to bring point just barely
27561 onto the screen again. If that cannot be done, then redisplay
27562 recenters point as usual.
27564 If the value is greater than 100, redisplay will never recenter point,
27565 but will always scroll just enough text to bring point into view, even
27566 if you move far away.
27568 A value of zero means always recenter point if it moves off screen. */);
27569 scroll_conservatively = 0;
27571 DEFVAR_INT ("scroll-margin", scroll_margin,
27572 doc: /* *Number of lines of margin at the top and bottom of a window.
27573 Recenter the window whenever point gets within this many lines
27574 of the top or bottom of the window. */);
27575 scroll_margin = 0;
27577 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
27578 doc: /* Pixels per inch value for non-window system displays.
27579 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
27580 Vdisplay_pixels_per_inch = make_float (72.0);
27582 #if GLYPH_DEBUG
27583 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
27584 #endif
27586 DEFVAR_LISP ("truncate-partial-width-windows",
27587 Vtruncate_partial_width_windows,
27588 doc: /* Non-nil means truncate lines in windows narrower than the frame.
27589 For an integer value, truncate lines in each window narrower than the
27590 full frame width, provided the window width is less than that integer;
27591 otherwise, respect the value of `truncate-lines'.
27593 For any other non-nil value, truncate lines in all windows that do
27594 not span the full frame width.
27596 A value of nil means to respect the value of `truncate-lines'.
27598 If `word-wrap' is enabled, you might want to reduce this. */);
27599 Vtruncate_partial_width_windows = make_number (50);
27601 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
27602 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
27603 Any other value means to use the appropriate face, `mode-line',
27604 `header-line', or `menu' respectively. */);
27605 mode_line_inverse_video = 1;
27607 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
27608 doc: /* *Maximum buffer size for which line number should be displayed.
27609 If the buffer is bigger than this, the line number does not appear
27610 in the mode line. A value of nil means no limit. */);
27611 Vline_number_display_limit = Qnil;
27613 DEFVAR_INT ("line-number-display-limit-width",
27614 line_number_display_limit_width,
27615 doc: /* *Maximum line width (in characters) for line number display.
27616 If the average length of the lines near point is bigger than this, then the
27617 line number may be omitted from the mode line. */);
27618 line_number_display_limit_width = 200;
27620 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
27621 doc: /* *Non-nil means highlight region even in nonselected windows. */);
27622 highlight_nonselected_windows = 0;
27624 DEFVAR_BOOL ("multiple-frames", multiple_frames,
27625 doc: /* Non-nil if more than one frame is visible on this display.
27626 Minibuffer-only frames don't count, but iconified frames do.
27627 This variable is not guaranteed to be accurate except while processing
27628 `frame-title-format' and `icon-title-format'. */);
27630 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
27631 doc: /* Template for displaying the title bar of visible frames.
27632 \(Assuming the window manager supports this feature.)
27634 This variable has the same structure as `mode-line-format', except that
27635 the %c and %l constructs are ignored. It is used only on frames for
27636 which no explicit name has been set \(see `modify-frame-parameters'). */);
27638 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
27639 doc: /* Template for displaying the title bar of an iconified frame.
27640 \(Assuming the window manager supports this feature.)
27641 This variable has the same structure as `mode-line-format' (which see),
27642 and is used only on frames for which no explicit name has been set
27643 \(see `modify-frame-parameters'). */);
27644 Vicon_title_format
27645 = Vframe_title_format
27646 = pure_cons (intern_c_string ("multiple-frames"),
27647 pure_cons (make_pure_c_string ("%b"),
27648 pure_cons (pure_cons (empty_unibyte_string,
27649 pure_cons (intern_c_string ("invocation-name"),
27650 pure_cons (make_pure_c_string ("@"),
27651 pure_cons (intern_c_string ("system-name"),
27652 Qnil)))),
27653 Qnil)));
27655 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
27656 doc: /* Maximum number of lines to keep in the message log buffer.
27657 If nil, disable message logging. If t, log messages but don't truncate
27658 the buffer when it becomes large. */);
27659 Vmessage_log_max = make_number (100);
27661 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
27662 doc: /* Functions called before redisplay, if window sizes have changed.
27663 The value should be a list of functions that take one argument.
27664 Just before redisplay, for each frame, if any of its windows have changed
27665 size since the last redisplay, or have been split or deleted,
27666 all the functions in the list are called, with the frame as argument. */);
27667 Vwindow_size_change_functions = Qnil;
27669 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
27670 doc: /* List of functions to call before redisplaying a window with scrolling.
27671 Each function is called with two arguments, the window and its new
27672 display-start position. Note that these functions are also called by
27673 `set-window-buffer'. Also note that the value of `window-end' is not
27674 valid when these functions are called. */);
27675 Vwindow_scroll_functions = Qnil;
27677 DEFVAR_LISP ("window-text-change-functions",
27678 Vwindow_text_change_functions,
27679 doc: /* Functions to call in redisplay when text in the window might change. */);
27680 Vwindow_text_change_functions = Qnil;
27682 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
27683 doc: /* Functions called when redisplay of a window reaches the end trigger.
27684 Each function is called with two arguments, the window and the end trigger value.
27685 See `set-window-redisplay-end-trigger'. */);
27686 Vredisplay_end_trigger_functions = Qnil;
27688 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
27689 doc: /* *Non-nil means autoselect window with mouse pointer.
27690 If nil, do not autoselect windows.
27691 A positive number means delay autoselection by that many seconds: a
27692 window is autoselected only after the mouse has remained in that
27693 window for the duration of the delay.
27694 A negative number has a similar effect, but causes windows to be
27695 autoselected only after the mouse has stopped moving. \(Because of
27696 the way Emacs compares mouse events, you will occasionally wait twice
27697 that time before the window gets selected.\)
27698 Any other value means to autoselect window instantaneously when the
27699 mouse pointer enters it.
27701 Autoselection selects the minibuffer only if it is active, and never
27702 unselects the minibuffer if it is active.
27704 When customizing this variable make sure that the actual value of
27705 `focus-follows-mouse' matches the behavior of your window manager. */);
27706 Vmouse_autoselect_window = Qnil;
27708 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
27709 doc: /* *Non-nil means automatically resize tool-bars.
27710 This dynamically changes the tool-bar's height to the minimum height
27711 that is needed to make all tool-bar items visible.
27712 If value is `grow-only', the tool-bar's height is only increased
27713 automatically; to decrease the tool-bar height, use \\[recenter]. */);
27714 Vauto_resize_tool_bars = Qt;
27716 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
27717 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
27718 auto_raise_tool_bar_buttons_p = 1;
27720 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
27721 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
27722 make_cursor_line_fully_visible_p = 1;
27724 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
27725 doc: /* *Border below tool-bar in pixels.
27726 If an integer, use it as the height of the border.
27727 If it is one of `internal-border-width' or `border-width', use the
27728 value of the corresponding frame parameter.
27729 Otherwise, no border is added below the tool-bar. */);
27730 Vtool_bar_border = Qinternal_border_width;
27732 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
27733 doc: /* *Margin around tool-bar buttons in pixels.
27734 If an integer, use that for both horizontal and vertical margins.
27735 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
27736 HORZ specifying the horizontal margin, and VERT specifying the
27737 vertical margin. */);
27738 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
27740 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
27741 doc: /* *Relief thickness of tool-bar buttons. */);
27742 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
27744 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
27745 doc: /* Tool bar style to use.
27746 It can be one of
27747 image - show images only
27748 text - show text only
27749 both - show both, text below image
27750 both-horiz - show text to the right of the image
27751 text-image-horiz - show text to the left of the image
27752 any other - use system default or image if no system default. */);
27753 Vtool_bar_style = Qnil;
27755 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
27756 doc: /* *Maximum number of characters a label can have to be shown.
27757 The tool bar style must also show labels for this to have any effect, see
27758 `tool-bar-style'. */);
27759 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
27761 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
27762 doc: /* List of functions to call to fontify regions of text.
27763 Each function is called with one argument POS. Functions must
27764 fontify a region starting at POS in the current buffer, and give
27765 fontified regions the property `fontified'. */);
27766 Vfontification_functions = Qnil;
27767 Fmake_variable_buffer_local (Qfontification_functions);
27769 DEFVAR_BOOL ("unibyte-display-via-language-environment",
27770 unibyte_display_via_language_environment,
27771 doc: /* *Non-nil means display unibyte text according to language environment.
27772 Specifically, this means that raw bytes in the range 160-255 decimal
27773 are displayed by converting them to the equivalent multibyte characters
27774 according to the current language environment. As a result, they are
27775 displayed according to the current fontset.
27777 Note that this variable affects only how these bytes are displayed,
27778 but does not change the fact they are interpreted as raw bytes. */);
27779 unibyte_display_via_language_environment = 0;
27781 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
27782 doc: /* *Maximum height for resizing mini-windows (the minibuffer and the echo area).
27783 If a float, it specifies a fraction of the mini-window frame's height.
27784 If an integer, it specifies a number of lines. */);
27785 Vmax_mini_window_height = make_float (0.25);
27787 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
27788 doc: /* How to resize mini-windows (the minibuffer and the echo area).
27789 A value of nil means don't automatically resize mini-windows.
27790 A value of t means resize them to fit the text displayed in them.
27791 A value of `grow-only', the default, means let mini-windows grow only;
27792 they return to their normal size when the minibuffer is closed, or the
27793 echo area becomes empty. */);
27794 Vresize_mini_windows = Qgrow_only;
27796 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
27797 doc: /* Alist specifying how to blink the cursor off.
27798 Each element has the form (ON-STATE . OFF-STATE). Whenever the
27799 `cursor-type' frame-parameter or variable equals ON-STATE,
27800 comparing using `equal', Emacs uses OFF-STATE to specify
27801 how to blink it off. ON-STATE and OFF-STATE are values for
27802 the `cursor-type' frame parameter.
27804 If a frame's ON-STATE has no entry in this list,
27805 the frame's other specifications determine how to blink the cursor off. */);
27806 Vblink_cursor_alist = Qnil;
27808 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
27809 doc: /* Allow or disallow automatic horizontal scrolling of windows.
27810 If non-nil, windows are automatically scrolled horizontally to make
27811 point visible. */);
27812 automatic_hscrolling_p = 1;
27813 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
27815 DEFVAR_INT ("hscroll-margin", hscroll_margin,
27816 doc: /* *How many columns away from the window edge point is allowed to get
27817 before automatic hscrolling will horizontally scroll the window. */);
27818 hscroll_margin = 5;
27820 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
27821 doc: /* *How many columns to scroll the window when point gets too close to the edge.
27822 When point is less than `hscroll-margin' columns from the window
27823 edge, automatic hscrolling will scroll the window by the amount of columns
27824 determined by this variable. If its value is a positive integer, scroll that
27825 many columns. If it's a positive floating-point number, it specifies the
27826 fraction of the window's width to scroll. If it's nil or zero, point will be
27827 centered horizontally after the scroll. Any other value, including negative
27828 numbers, are treated as if the value were zero.
27830 Automatic hscrolling always moves point outside the scroll margin, so if
27831 point was more than scroll step columns inside the margin, the window will
27832 scroll more than the value given by the scroll step.
27834 Note that the lower bound for automatic hscrolling specified by `scroll-left'
27835 and `scroll-right' overrides this variable's effect. */);
27836 Vhscroll_step = make_number (0);
27838 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
27839 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
27840 Bind this around calls to `message' to let it take effect. */);
27841 message_truncate_lines = 0;
27843 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
27844 doc: /* Normal hook run to update the menu bar definitions.
27845 Redisplay runs this hook before it redisplays the menu bar.
27846 This is used to update submenus such as Buffers,
27847 whose contents depend on various data. */);
27848 Vmenu_bar_update_hook = Qnil;
27850 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
27851 doc: /* Frame for which we are updating a menu.
27852 The enable predicate for a menu binding should check this variable. */);
27853 Vmenu_updating_frame = Qnil;
27855 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
27856 doc: /* Non-nil means don't update menu bars. Internal use only. */);
27857 inhibit_menubar_update = 0;
27859 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
27860 doc: /* Prefix prepended to all continuation lines at display time.
27861 The value may be a string, an image, or a stretch-glyph; it is
27862 interpreted in the same way as the value of a `display' text property.
27864 This variable is overridden by any `wrap-prefix' text or overlay
27865 property.
27867 To add a prefix to non-continuation lines, use `line-prefix'. */);
27868 Vwrap_prefix = Qnil;
27869 DEFSYM (Qwrap_prefix, "wrap-prefix");
27870 Fmake_variable_buffer_local (Qwrap_prefix);
27872 DEFVAR_LISP ("line-prefix", Vline_prefix,
27873 doc: /* Prefix prepended to all non-continuation lines at display time.
27874 The value may be a string, an image, or a stretch-glyph; it is
27875 interpreted in the same way as the value of a `display' text property.
27877 This variable is overridden by any `line-prefix' text or overlay
27878 property.
27880 To add a prefix to continuation lines, use `wrap-prefix'. */);
27881 Vline_prefix = Qnil;
27882 DEFSYM (Qline_prefix, "line-prefix");
27883 Fmake_variable_buffer_local (Qline_prefix);
27885 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
27886 doc: /* Non-nil means don't eval Lisp during redisplay. */);
27887 inhibit_eval_during_redisplay = 0;
27889 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
27890 doc: /* Non-nil means don't free realized faces. Internal use only. */);
27891 inhibit_free_realized_faces = 0;
27893 #if GLYPH_DEBUG
27894 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
27895 doc: /* Inhibit try_window_id display optimization. */);
27896 inhibit_try_window_id = 0;
27898 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
27899 doc: /* Inhibit try_window_reusing display optimization. */);
27900 inhibit_try_window_reusing = 0;
27902 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
27903 doc: /* Inhibit try_cursor_movement display optimization. */);
27904 inhibit_try_cursor_movement = 0;
27905 #endif /* GLYPH_DEBUG */
27907 DEFVAR_INT ("overline-margin", overline_margin,
27908 doc: /* *Space between overline and text, in pixels.
27909 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
27910 margin to the caracter height. */);
27911 overline_margin = 2;
27913 DEFVAR_INT ("underline-minimum-offset",
27914 underline_minimum_offset,
27915 doc: /* Minimum distance between baseline and underline.
27916 This can improve legibility of underlined text at small font sizes,
27917 particularly when using variable `x-use-underline-position-properties'
27918 with fonts that specify an UNDERLINE_POSITION relatively close to the
27919 baseline. The default value is 1. */);
27920 underline_minimum_offset = 1;
27922 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
27923 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
27924 This feature only works when on a window system that can change
27925 cursor shapes. */);
27926 display_hourglass_p = 1;
27928 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
27929 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
27930 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
27932 hourglass_atimer = NULL;
27933 hourglass_shown_p = 0;
27935 DEFSYM (Qglyphless_char, "glyphless-char");
27936 DEFSYM (Qhex_code, "hex-code");
27937 DEFSYM (Qempty_box, "empty-box");
27938 DEFSYM (Qthin_space, "thin-space");
27939 DEFSYM (Qzero_width, "zero-width");
27941 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
27942 /* Intern this now in case it isn't already done.
27943 Setting this variable twice is harmless.
27944 But don't staticpro it here--that is done in alloc.c. */
27945 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
27946 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
27948 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
27949 doc: /* Char-table defining glyphless characters.
27950 Each element, if non-nil, should be one of the following:
27951 an ASCII acronym string: display this string in a box
27952 `hex-code': display the hexadecimal code of a character in a box
27953 `empty-box': display as an empty box
27954 `thin-space': display as 1-pixel width space
27955 `zero-width': don't display
27956 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
27957 display method for graphical terminals and text terminals respectively.
27958 GRAPHICAL and TEXT should each have one of the values listed above.
27960 The char-table has one extra slot to control the display of a character for
27961 which no font is found. This slot only takes effect on graphical terminals.
27962 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
27963 `thin-space'. The default is `empty-box'. */);
27964 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
27965 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
27966 Qempty_box);
27970 /* Initialize this module when Emacs starts. */
27972 void
27973 init_xdisp (void)
27975 current_header_line_height = current_mode_line_height = -1;
27977 CHARPOS (this_line_start_pos) = 0;
27979 if (!noninteractive)
27981 struct window *m = XWINDOW (minibuf_window);
27982 Lisp_Object frame = m->frame;
27983 struct frame *f = XFRAME (frame);
27984 Lisp_Object root = FRAME_ROOT_WINDOW (f);
27985 struct window *r = XWINDOW (root);
27986 int i;
27988 echo_area_window = minibuf_window;
27990 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
27991 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
27992 XSETFASTINT (r->total_cols, FRAME_COLS (f));
27993 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
27994 XSETFASTINT (m->total_lines, 1);
27995 XSETFASTINT (m->total_cols, FRAME_COLS (f));
27997 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
27998 scratch_glyph_row.glyphs[TEXT_AREA + 1]
27999 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
28001 /* The default ellipsis glyphs `...'. */
28002 for (i = 0; i < 3; ++i)
28003 default_invis_vector[i] = make_number ('.');
28007 /* Allocate the buffer for frame titles.
28008 Also used for `format-mode-line'. */
28009 int size = 100;
28010 mode_line_noprop_buf = (char *) xmalloc (size);
28011 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
28012 mode_line_noprop_ptr = mode_line_noprop_buf;
28013 mode_line_target = MODE_LINE_DISPLAY;
28016 help_echo_showing_p = 0;
28019 /* Since w32 does not support atimers, it defines its own implementation of
28020 the following three functions in w32fns.c. */
28021 #ifndef WINDOWSNT
28023 /* Platform-independent portion of hourglass implementation. */
28025 /* Return non-zero if houglass timer has been started or hourglass is shown. */
28027 hourglass_started (void)
28029 return hourglass_shown_p || hourglass_atimer != NULL;
28032 /* Cancel a currently active hourglass timer, and start a new one. */
28033 void
28034 start_hourglass (void)
28036 #if defined (HAVE_WINDOW_SYSTEM)
28037 EMACS_TIME delay;
28038 int secs, usecs = 0;
28040 cancel_hourglass ();
28042 if (INTEGERP (Vhourglass_delay)
28043 && XINT (Vhourglass_delay) > 0)
28044 secs = XFASTINT (Vhourglass_delay);
28045 else if (FLOATP (Vhourglass_delay)
28046 && XFLOAT_DATA (Vhourglass_delay) > 0)
28048 Lisp_Object tem;
28049 tem = Ftruncate (Vhourglass_delay, Qnil);
28050 secs = XFASTINT (tem);
28051 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
28053 else
28054 secs = DEFAULT_HOURGLASS_DELAY;
28056 EMACS_SET_SECS_USECS (delay, secs, usecs);
28057 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
28058 show_hourglass, NULL);
28059 #endif
28063 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
28064 shown. */
28065 void
28066 cancel_hourglass (void)
28068 #if defined (HAVE_WINDOW_SYSTEM)
28069 if (hourglass_atimer)
28071 cancel_atimer (hourglass_atimer);
28072 hourglass_atimer = NULL;
28075 if (hourglass_shown_p)
28076 hide_hourglass ();
28077 #endif
28079 #endif /* ! WINDOWSNT */