Adjust NEWS entry for recent change
[emacs.git] / src / xdisp.c
blob62d0d187a19ed2256afb75553d0c6b895653d3dd
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2012 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. */
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. */
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. */
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. */
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. */
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. */
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. */
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 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;
1213 /* Subroutine of pos_visible_p below. Extracts a display string, if
1214 any, from the display spec given as its argument. */
1215 static Lisp_Object
1216 string_from_display_spec (Lisp_Object spec)
1218 if (CONSP (spec))
1220 while (CONSP (spec))
1222 if (STRINGP (XCAR (spec)))
1223 return XCAR (spec);
1224 spec = XCDR (spec);
1227 else if (VECTORP (spec))
1229 ptrdiff_t i;
1231 for (i = 0; i < ASIZE (spec); i++)
1233 if (STRINGP (AREF (spec, i)))
1234 return AREF (spec, i);
1236 return Qnil;
1239 return spec;
1242 /* Return 1 if position CHARPOS is visible in window W.
1243 CHARPOS < 0 means return info about WINDOW_END position.
1244 If visible, set *X and *Y to pixel coordinates of top left corner.
1245 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1246 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1249 pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1250 int *rtop, int *rbot, int *rowh, int *vpos)
1252 struct it it;
1253 void *itdata = bidi_shelve_cache ();
1254 struct text_pos top;
1255 int visible_p = 0;
1256 struct buffer *old_buffer = NULL;
1258 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1259 return visible_p;
1261 if (XBUFFER (w->buffer) != current_buffer)
1263 old_buffer = current_buffer;
1264 set_buffer_internal_1 (XBUFFER (w->buffer));
1267 SET_TEXT_POS_FROM_MARKER (top, w->start);
1269 /* Compute exact mode line heights. */
1270 if (WINDOW_WANTS_MODELINE_P (w))
1271 current_mode_line_height
1272 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1273 BVAR (current_buffer, mode_line_format));
1275 if (WINDOW_WANTS_HEADER_LINE_P (w))
1276 current_header_line_height
1277 = display_mode_line (w, HEADER_LINE_FACE_ID,
1278 BVAR (current_buffer, header_line_format));
1280 start_display (&it, w, top);
1281 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1282 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1284 if (charpos >= 0
1285 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1286 && IT_CHARPOS (it) >= charpos)
1287 /* When scanning backwards under bidi iteration, move_it_to
1288 stops at or _before_ CHARPOS, because it stops at or to
1289 the _right_ of the character at CHARPOS. */
1290 || (it.bidi_p && it.bidi_it.scan_dir == -1
1291 && IT_CHARPOS (it) <= charpos)))
1293 /* We have reached CHARPOS, or passed it. How the call to
1294 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1295 or covered by a display property, move_it_to stops at the end
1296 of the invisible text, to the right of CHARPOS. (ii) If
1297 CHARPOS is in a display vector, move_it_to stops on its last
1298 glyph. */
1299 int top_x = it.current_x;
1300 int top_y = it.current_y;
1301 enum it_method it_method = it.method;
1302 /* Calling line_bottom_y may change it.method, it.position, etc. */
1303 int bottom_y = (last_height = 0, line_bottom_y (&it));
1304 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1306 if (top_y < window_top_y)
1307 visible_p = bottom_y > window_top_y;
1308 else if (top_y < it.last_visible_y)
1309 visible_p = 1;
1310 if (visible_p)
1312 if (it_method == GET_FROM_DISPLAY_VECTOR)
1314 /* We stopped on the last glyph of a display vector.
1315 Try and recompute. Hack alert! */
1316 if (charpos < 2 || top.charpos >= charpos)
1317 top_x = it.glyph_row->x;
1318 else
1320 struct it it2;
1321 start_display (&it2, w, top);
1322 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1323 get_next_display_element (&it2);
1324 PRODUCE_GLYPHS (&it2);
1325 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1326 || it2.current_x > it2.last_visible_x)
1327 top_x = it.glyph_row->x;
1328 else
1330 top_x = it2.current_x;
1331 top_y = it2.current_y;
1335 else if (IT_CHARPOS (it) != charpos)
1337 Lisp_Object cpos = make_number (charpos);
1338 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1339 Lisp_Object string = string_from_display_spec (spec);
1340 int newline_in_string = 0;
1342 if (STRINGP (string))
1344 const char *s = SSDATA (string);
1345 const char *e = s + SBYTES (string);
1346 while (s < e)
1348 if (*s++ == '\n')
1350 newline_in_string = 1;
1351 break;
1355 /* The tricky code below is needed because there's a
1356 discrepancy between move_it_to and how we set cursor
1357 when the display line ends in a newline from a
1358 display string. move_it_to will stop _after_ such
1359 display strings, whereas set_cursor_from_row
1360 conspires with cursor_row_p to place the cursor on
1361 the first glyph produced from the display string. */
1363 /* We have overshoot PT because it is covered by a
1364 display property whose value is a string. If the
1365 string includes embedded newlines, we are also in the
1366 wrong display line. Backtrack to the correct line,
1367 where the display string begins. */
1368 if (newline_in_string)
1370 Lisp_Object startpos, endpos;
1371 EMACS_INT start, end;
1372 struct it it3;
1374 /* Find the first and the last buffer positions
1375 covered by the display string. */
1376 endpos =
1377 Fnext_single_char_property_change (cpos, Qdisplay,
1378 Qnil, Qnil);
1379 startpos =
1380 Fprevious_single_char_property_change (endpos, Qdisplay,
1381 Qnil, Qnil);
1382 start = XFASTINT (startpos);
1383 end = XFASTINT (endpos);
1384 /* Move to the last buffer position before the
1385 display property. */
1386 start_display (&it3, w, top);
1387 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1388 /* Move forward one more line if the position before
1389 the display string is a newline or if it is the
1390 rightmost character on a line that is
1391 continued or word-wrapped. */
1392 if (it3.method == GET_FROM_BUFFER
1393 && it3.c == '\n')
1394 move_it_by_lines (&it3, 1);
1395 else if (move_it_in_display_line_to (&it3, -1,
1396 it3.current_x
1397 + it3.pixel_width,
1398 MOVE_TO_X)
1399 == MOVE_LINE_CONTINUED)
1401 move_it_by_lines (&it3, 1);
1402 /* When we are under word-wrap, the #$@%!
1403 move_it_by_lines moves 2 lines, so we need to
1404 fix that up. */
1405 if (it3.line_wrap == WORD_WRAP)
1406 move_it_by_lines (&it3, -1);
1409 /* Record the vertical coordinate of the display
1410 line where we wound up. */
1411 top_y = it3.current_y;
1412 if (it3.bidi_p)
1414 /* When characters are reordered for display,
1415 the character displayed to the left of the
1416 display string could be _after_ the display
1417 property in the logical order. Use the
1418 smallest vertical position of these two. */
1419 start_display (&it3, w, top);
1420 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1421 if (it3.current_y < top_y)
1422 top_y = it3.current_y;
1424 /* Move from the top of the window to the beginning
1425 of the display line where the display string
1426 begins. */
1427 start_display (&it3, w, top);
1428 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1429 /* Finally, advance the iterator until we hit the
1430 first display element whose character position is
1431 CHARPOS, or until the first newline from the
1432 display string, which signals the end of the
1433 display line. */
1434 while (get_next_display_element (&it3))
1436 PRODUCE_GLYPHS (&it3);
1437 if (IT_CHARPOS (it3) == charpos
1438 || ITERATOR_AT_END_OF_LINE_P (&it3))
1439 break;
1440 set_iterator_to_next (&it3, 0);
1442 top_x = it3.current_x - it3.pixel_width;
1443 /* Normally, we would exit the above loop because we
1444 found the display element whose character
1445 position is CHARPOS. For the contingency that we
1446 didn't, and stopped at the first newline from the
1447 display string, move back over the glyphs
1448 produced from the string, until we find the
1449 rightmost glyph not from the string. */
1450 if (IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1452 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1453 + it3.glyph_row->used[TEXT_AREA];
1455 while (EQ ((g - 1)->object, string))
1457 --g;
1458 top_x -= g->pixel_width;
1460 xassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1461 + it3.glyph_row->used[TEXT_AREA]);
1466 *x = top_x;
1467 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1468 *rtop = max (0, window_top_y - top_y);
1469 *rbot = max (0, bottom_y - it.last_visible_y);
1470 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1471 - max (top_y, window_top_y)));
1472 *vpos = it.vpos;
1475 else
1477 /* We were asked to provide info about WINDOW_END. */
1478 struct it it2;
1479 void *it2data = NULL;
1481 SAVE_IT (it2, it, it2data);
1482 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1483 move_it_by_lines (&it, 1);
1484 if (charpos < IT_CHARPOS (it)
1485 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1487 visible_p = 1;
1488 RESTORE_IT (&it2, &it2, it2data);
1489 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1490 *x = it2.current_x;
1491 *y = it2.current_y + it2.max_ascent - it2.ascent;
1492 *rtop = max (0, -it2.current_y);
1493 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1494 - it.last_visible_y));
1495 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1496 it.last_visible_y)
1497 - max (it2.current_y,
1498 WINDOW_HEADER_LINE_HEIGHT (w))));
1499 *vpos = it2.vpos;
1501 else
1502 bidi_unshelve_cache (it2data, 1);
1504 bidi_unshelve_cache (itdata, 0);
1506 if (old_buffer)
1507 set_buffer_internal_1 (old_buffer);
1509 current_header_line_height = current_mode_line_height = -1;
1511 if (visible_p && XFASTINT (w->hscroll) > 0)
1512 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1514 #if 0
1515 /* Debugging code. */
1516 if (visible_p)
1517 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1518 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1519 else
1520 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1521 #endif
1523 return visible_p;
1527 /* Return the next character from STR. Return in *LEN the length of
1528 the character. This is like STRING_CHAR_AND_LENGTH but never
1529 returns an invalid character. If we find one, we return a `?', but
1530 with the length of the invalid character. */
1532 static inline int
1533 string_char_and_length (const unsigned char *str, int *len)
1535 int c;
1537 c = STRING_CHAR_AND_LENGTH (str, *len);
1538 if (!CHAR_VALID_P (c))
1539 /* We may not change the length here because other places in Emacs
1540 don't use this function, i.e. they silently accept invalid
1541 characters. */
1542 c = '?';
1544 return c;
1549 /* Given a position POS containing a valid character and byte position
1550 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1552 static struct text_pos
1553 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1555 xassert (STRINGP (string) && nchars >= 0);
1557 if (STRING_MULTIBYTE (string))
1559 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1560 int len;
1562 while (nchars--)
1564 string_char_and_length (p, &len);
1565 p += len;
1566 CHARPOS (pos) += 1;
1567 BYTEPOS (pos) += len;
1570 else
1571 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1573 return pos;
1577 /* Value is the text position, i.e. character and byte position,
1578 for character position CHARPOS in STRING. */
1580 static inline struct text_pos
1581 string_pos (EMACS_INT charpos, Lisp_Object string)
1583 struct text_pos pos;
1584 xassert (STRINGP (string));
1585 xassert (charpos >= 0);
1586 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1587 return pos;
1591 /* Value is a text position, i.e. character and byte position, for
1592 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1593 means recognize multibyte characters. */
1595 static struct text_pos
1596 c_string_pos (EMACS_INT charpos, const char *s, int multibyte_p)
1598 struct text_pos pos;
1600 xassert (s != NULL);
1601 xassert (charpos >= 0);
1603 if (multibyte_p)
1605 int len;
1607 SET_TEXT_POS (pos, 0, 0);
1608 while (charpos--)
1610 string_char_and_length ((const unsigned char *) s, &len);
1611 s += len;
1612 CHARPOS (pos) += 1;
1613 BYTEPOS (pos) += len;
1616 else
1617 SET_TEXT_POS (pos, charpos, charpos);
1619 return pos;
1623 /* Value is the number of characters in C string S. MULTIBYTE_P
1624 non-zero means recognize multibyte characters. */
1626 static EMACS_INT
1627 number_of_chars (const char *s, int multibyte_p)
1629 EMACS_INT nchars;
1631 if (multibyte_p)
1633 EMACS_INT rest = strlen (s);
1634 int len;
1635 const unsigned char *p = (const unsigned char *) s;
1637 for (nchars = 0; rest > 0; ++nchars)
1639 string_char_and_length (p, &len);
1640 rest -= len, p += len;
1643 else
1644 nchars = strlen (s);
1646 return nchars;
1650 /* Compute byte position NEWPOS->bytepos corresponding to
1651 NEWPOS->charpos. POS is a known position in string STRING.
1652 NEWPOS->charpos must be >= POS.charpos. */
1654 static void
1655 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1657 xassert (STRINGP (string));
1658 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1660 if (STRING_MULTIBYTE (string))
1661 *newpos = string_pos_nchars_ahead (pos, string,
1662 CHARPOS (*newpos) - CHARPOS (pos));
1663 else
1664 BYTEPOS (*newpos) = CHARPOS (*newpos);
1667 /* EXPORT:
1668 Return an estimation of the pixel height of mode or header lines on
1669 frame F. FACE_ID specifies what line's height to estimate. */
1672 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1674 #ifdef HAVE_WINDOW_SYSTEM
1675 if (FRAME_WINDOW_P (f))
1677 int height = FONT_HEIGHT (FRAME_FONT (f));
1679 /* This function is called so early when Emacs starts that the face
1680 cache and mode line face are not yet initialized. */
1681 if (FRAME_FACE_CACHE (f))
1683 struct face *face = FACE_FROM_ID (f, face_id);
1684 if (face)
1686 if (face->font)
1687 height = FONT_HEIGHT (face->font);
1688 if (face->box_line_width > 0)
1689 height += 2 * face->box_line_width;
1693 return height;
1695 #endif
1697 return 1;
1700 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1701 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1702 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1703 not force the value into range. */
1705 void
1706 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1707 int *x, int *y, NativeRectangle *bounds, int noclip)
1710 #ifdef HAVE_WINDOW_SYSTEM
1711 if (FRAME_WINDOW_P (f))
1713 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1714 even for negative values. */
1715 if (pix_x < 0)
1716 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1717 if (pix_y < 0)
1718 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1720 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1721 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1723 if (bounds)
1724 STORE_NATIVE_RECT (*bounds,
1725 FRAME_COL_TO_PIXEL_X (f, pix_x),
1726 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1727 FRAME_COLUMN_WIDTH (f) - 1,
1728 FRAME_LINE_HEIGHT (f) - 1);
1730 if (!noclip)
1732 if (pix_x < 0)
1733 pix_x = 0;
1734 else if (pix_x > FRAME_TOTAL_COLS (f))
1735 pix_x = FRAME_TOTAL_COLS (f);
1737 if (pix_y < 0)
1738 pix_y = 0;
1739 else if (pix_y > FRAME_LINES (f))
1740 pix_y = FRAME_LINES (f);
1743 #endif
1745 *x = pix_x;
1746 *y = pix_y;
1750 /* Find the glyph under window-relative coordinates X/Y in window W.
1751 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1752 strings. Return in *HPOS and *VPOS the row and column number of
1753 the glyph found. Return in *AREA the glyph area containing X.
1754 Value is a pointer to the glyph found or null if X/Y is not on
1755 text, or we can't tell because W's current matrix is not up to
1756 date. */
1758 static
1759 struct glyph *
1760 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1761 int *dx, int *dy, int *area)
1763 struct glyph *glyph, *end;
1764 struct glyph_row *row = NULL;
1765 int x0, i;
1767 /* Find row containing Y. Give up if some row is not enabled. */
1768 for (i = 0; i < w->current_matrix->nrows; ++i)
1770 row = MATRIX_ROW (w->current_matrix, i);
1771 if (!row->enabled_p)
1772 return NULL;
1773 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1774 break;
1777 *vpos = i;
1778 *hpos = 0;
1780 /* Give up if Y is not in the window. */
1781 if (i == w->current_matrix->nrows)
1782 return NULL;
1784 /* Get the glyph area containing X. */
1785 if (w->pseudo_window_p)
1787 *area = TEXT_AREA;
1788 x0 = 0;
1790 else
1792 if (x < window_box_left_offset (w, TEXT_AREA))
1794 *area = LEFT_MARGIN_AREA;
1795 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1797 else if (x < window_box_right_offset (w, TEXT_AREA))
1799 *area = TEXT_AREA;
1800 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1802 else
1804 *area = RIGHT_MARGIN_AREA;
1805 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1809 /* Find glyph containing X. */
1810 glyph = row->glyphs[*area];
1811 end = glyph + row->used[*area];
1812 x -= x0;
1813 while (glyph < end && x >= glyph->pixel_width)
1815 x -= glyph->pixel_width;
1816 ++glyph;
1819 if (glyph == end)
1820 return NULL;
1822 if (dx)
1824 *dx = x;
1825 *dy = y - (row->y + row->ascent - glyph->ascent);
1828 *hpos = glyph - row->glyphs[*area];
1829 return glyph;
1832 /* Convert frame-relative x/y to coordinates relative to window W.
1833 Takes pseudo-windows into account. */
1835 static void
1836 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1838 if (w->pseudo_window_p)
1840 /* A pseudo-window is always full-width, and starts at the
1841 left edge of the frame, plus a frame border. */
1842 struct frame *f = XFRAME (w->frame);
1843 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1844 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1846 else
1848 *x -= WINDOW_LEFT_EDGE_X (w);
1849 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1853 #ifdef HAVE_WINDOW_SYSTEM
1855 /* EXPORT:
1856 Return in RECTS[] at most N clipping rectangles for glyph string S.
1857 Return the number of stored rectangles. */
1860 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1862 XRectangle r;
1864 if (n <= 0)
1865 return 0;
1867 if (s->row->full_width_p)
1869 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1870 r.x = WINDOW_LEFT_EDGE_X (s->w);
1871 r.width = WINDOW_TOTAL_WIDTH (s->w);
1873 /* Unless displaying a mode or menu bar line, which are always
1874 fully visible, clip to the visible part of the row. */
1875 if (s->w->pseudo_window_p)
1876 r.height = s->row->visible_height;
1877 else
1878 r.height = s->height;
1880 else
1882 /* This is a text line that may be partially visible. */
1883 r.x = window_box_left (s->w, s->area);
1884 r.width = window_box_width (s->w, s->area);
1885 r.height = s->row->visible_height;
1888 if (s->clip_head)
1889 if (r.x < s->clip_head->x)
1891 if (r.width >= s->clip_head->x - r.x)
1892 r.width -= s->clip_head->x - r.x;
1893 else
1894 r.width = 0;
1895 r.x = s->clip_head->x;
1897 if (s->clip_tail)
1898 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1900 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1901 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1902 else
1903 r.width = 0;
1906 /* If S draws overlapping rows, it's sufficient to use the top and
1907 bottom of the window for clipping because this glyph string
1908 intentionally draws over other lines. */
1909 if (s->for_overlaps)
1911 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1912 r.height = window_text_bottom_y (s->w) - r.y;
1914 /* Alas, the above simple strategy does not work for the
1915 environments with anti-aliased text: if the same text is
1916 drawn onto the same place multiple times, it gets thicker.
1917 If the overlap we are processing is for the erased cursor, we
1918 take the intersection with the rectangle of the cursor. */
1919 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1921 XRectangle rc, r_save = r;
1923 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1924 rc.y = s->w->phys_cursor.y;
1925 rc.width = s->w->phys_cursor_width;
1926 rc.height = s->w->phys_cursor_height;
1928 x_intersect_rectangles (&r_save, &rc, &r);
1931 else
1933 /* Don't use S->y for clipping because it doesn't take partially
1934 visible lines into account. For example, it can be negative for
1935 partially visible lines at the top of a window. */
1936 if (!s->row->full_width_p
1937 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1938 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1939 else
1940 r.y = max (0, s->row->y);
1943 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1945 /* If drawing the cursor, don't let glyph draw outside its
1946 advertised boundaries. Cleartype does this under some circumstances. */
1947 if (s->hl == DRAW_CURSOR)
1949 struct glyph *glyph = s->first_glyph;
1950 int height, max_y;
1952 if (s->x > r.x)
1954 r.width -= s->x - r.x;
1955 r.x = s->x;
1957 r.width = min (r.width, glyph->pixel_width);
1959 /* If r.y is below window bottom, ensure that we still see a cursor. */
1960 height = min (glyph->ascent + glyph->descent,
1961 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1962 max_y = window_text_bottom_y (s->w) - height;
1963 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1964 if (s->ybase - glyph->ascent > max_y)
1966 r.y = max_y;
1967 r.height = height;
1969 else
1971 /* Don't draw cursor glyph taller than our actual glyph. */
1972 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1973 if (height < r.height)
1975 max_y = r.y + r.height;
1976 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1977 r.height = min (max_y - r.y, height);
1982 if (s->row->clip)
1984 XRectangle r_save = r;
1986 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
1987 r.width = 0;
1990 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1991 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1993 #ifdef CONVERT_FROM_XRECT
1994 CONVERT_FROM_XRECT (r, *rects);
1995 #else
1996 *rects = r;
1997 #endif
1998 return 1;
2000 else
2002 /* If we are processing overlapping and allowed to return
2003 multiple clipping rectangles, we exclude the row of the glyph
2004 string from the clipping rectangle. This is to avoid drawing
2005 the same text on the environment with anti-aliasing. */
2006 #ifdef CONVERT_FROM_XRECT
2007 XRectangle rs[2];
2008 #else
2009 XRectangle *rs = rects;
2010 #endif
2011 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2013 if (s->for_overlaps & OVERLAPS_PRED)
2015 rs[i] = r;
2016 if (r.y + r.height > row_y)
2018 if (r.y < row_y)
2019 rs[i].height = row_y - r.y;
2020 else
2021 rs[i].height = 0;
2023 i++;
2025 if (s->for_overlaps & OVERLAPS_SUCC)
2027 rs[i] = r;
2028 if (r.y < row_y + s->row->visible_height)
2030 if (r.y + r.height > row_y + s->row->visible_height)
2032 rs[i].y = row_y + s->row->visible_height;
2033 rs[i].height = r.y + r.height - rs[i].y;
2035 else
2036 rs[i].height = 0;
2038 i++;
2041 n = i;
2042 #ifdef CONVERT_FROM_XRECT
2043 for (i = 0; i < n; i++)
2044 CONVERT_FROM_XRECT (rs[i], rects[i]);
2045 #endif
2046 return n;
2050 /* EXPORT:
2051 Return in *NR the clipping rectangle for glyph string S. */
2053 void
2054 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2056 get_glyph_string_clip_rects (s, nr, 1);
2060 /* EXPORT:
2061 Return the position and height of the phys cursor in window W.
2062 Set w->phys_cursor_width to width of phys cursor.
2065 void
2066 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2067 struct glyph *glyph, int *xp, int *yp, int *heightp)
2069 struct frame *f = XFRAME (WINDOW_FRAME (w));
2070 int x, y, wd, h, h0, y0;
2072 /* Compute the width of the rectangle to draw. If on a stretch
2073 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2074 rectangle as wide as the glyph, but use a canonical character
2075 width instead. */
2076 wd = glyph->pixel_width - 1;
2077 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2078 wd++; /* Why? */
2079 #endif
2081 x = w->phys_cursor.x;
2082 if (x < 0)
2084 wd += x;
2085 x = 0;
2088 if (glyph->type == STRETCH_GLYPH
2089 && !x_stretch_cursor_p)
2090 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2091 w->phys_cursor_width = wd;
2093 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2095 /* If y is below window bottom, ensure that we still see a cursor. */
2096 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2098 h = max (h0, glyph->ascent + glyph->descent);
2099 h0 = min (h0, glyph->ascent + glyph->descent);
2101 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2102 if (y < y0)
2104 h = max (h - (y0 - y) + 1, h0);
2105 y = y0 - 1;
2107 else
2109 y0 = window_text_bottom_y (w) - h0;
2110 if (y > y0)
2112 h += y - y0;
2113 y = y0;
2117 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2118 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2119 *heightp = h;
2123 * Remember which glyph the mouse is over.
2126 void
2127 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2129 Lisp_Object window;
2130 struct window *w;
2131 struct glyph_row *r, *gr, *end_row;
2132 enum window_part part;
2133 enum glyph_row_area area;
2134 int x, y, width, height;
2136 /* Try to determine frame pixel position and size of the glyph under
2137 frame pixel coordinates X/Y on frame F. */
2139 if (!f->glyphs_initialized_p
2140 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2141 NILP (window)))
2143 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2144 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2145 goto virtual_glyph;
2148 w = XWINDOW (window);
2149 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2150 height = WINDOW_FRAME_LINE_HEIGHT (w);
2152 x = window_relative_x_coord (w, part, gx);
2153 y = gy - WINDOW_TOP_EDGE_Y (w);
2155 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2156 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2158 if (w->pseudo_window_p)
2160 area = TEXT_AREA;
2161 part = ON_MODE_LINE; /* Don't adjust margin. */
2162 goto text_glyph;
2165 switch (part)
2167 case ON_LEFT_MARGIN:
2168 area = LEFT_MARGIN_AREA;
2169 goto text_glyph;
2171 case ON_RIGHT_MARGIN:
2172 area = RIGHT_MARGIN_AREA;
2173 goto text_glyph;
2175 case ON_HEADER_LINE:
2176 case ON_MODE_LINE:
2177 gr = (part == ON_HEADER_LINE
2178 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2179 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2180 gy = gr->y;
2181 area = TEXT_AREA;
2182 goto text_glyph_row_found;
2184 case ON_TEXT:
2185 area = TEXT_AREA;
2187 text_glyph:
2188 gr = 0; gy = 0;
2189 for (; r <= end_row && r->enabled_p; ++r)
2190 if (r->y + r->height > y)
2192 gr = r; gy = r->y;
2193 break;
2196 text_glyph_row_found:
2197 if (gr && gy <= y)
2199 struct glyph *g = gr->glyphs[area];
2200 struct glyph *end = g + gr->used[area];
2202 height = gr->height;
2203 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2204 if (gx + g->pixel_width > x)
2205 break;
2207 if (g < end)
2209 if (g->type == IMAGE_GLYPH)
2211 /* Don't remember when mouse is over image, as
2212 image may have hot-spots. */
2213 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2214 return;
2216 width = g->pixel_width;
2218 else
2220 /* Use nominal char spacing at end of line. */
2221 x -= gx;
2222 gx += (x / width) * width;
2225 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2226 gx += window_box_left_offset (w, area);
2228 else
2230 /* Use nominal line height at end of window. */
2231 gx = (x / width) * width;
2232 y -= gy;
2233 gy += (y / height) * height;
2235 break;
2237 case ON_LEFT_FRINGE:
2238 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2239 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2240 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2241 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2242 goto row_glyph;
2244 case ON_RIGHT_FRINGE:
2245 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2246 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2247 : window_box_right_offset (w, TEXT_AREA));
2248 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2249 goto row_glyph;
2251 case ON_SCROLL_BAR:
2252 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2254 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2255 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2256 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2257 : 0)));
2258 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2260 row_glyph:
2261 gr = 0, gy = 0;
2262 for (; r <= end_row && r->enabled_p; ++r)
2263 if (r->y + r->height > y)
2265 gr = r; gy = r->y;
2266 break;
2269 if (gr && gy <= y)
2270 height = gr->height;
2271 else
2273 /* Use nominal line height at end of window. */
2274 y -= gy;
2275 gy += (y / height) * height;
2277 break;
2279 default:
2281 virtual_glyph:
2282 /* If there is no glyph under the mouse, then we divide the screen
2283 into a grid of the smallest glyph in the frame, and use that
2284 as our "glyph". */
2286 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2287 round down even for negative values. */
2288 if (gx < 0)
2289 gx -= width - 1;
2290 if (gy < 0)
2291 gy -= height - 1;
2293 gx = (gx / width) * width;
2294 gy = (gy / height) * height;
2296 goto store_rect;
2299 gx += WINDOW_LEFT_EDGE_X (w);
2300 gy += WINDOW_TOP_EDGE_Y (w);
2302 store_rect:
2303 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2305 /* Visible feedback for debugging. */
2306 #if 0
2307 #if HAVE_X_WINDOWS
2308 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2309 f->output_data.x->normal_gc,
2310 gx, gy, width, height);
2311 #endif
2312 #endif
2316 #endif /* HAVE_WINDOW_SYSTEM */
2319 /***********************************************************************
2320 Lisp form evaluation
2321 ***********************************************************************/
2323 /* Error handler for safe_eval and safe_call. */
2325 static Lisp_Object
2326 safe_eval_handler (Lisp_Object arg)
2328 add_to_log ("Error during redisplay: %S", arg, Qnil);
2329 return Qnil;
2333 /* Evaluate SEXPR and return the result, or nil if something went
2334 wrong. Prevent redisplay during the evaluation. */
2336 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2337 Return the result, or nil if something went wrong. Prevent
2338 redisplay during the evaluation. */
2340 Lisp_Object
2341 safe_call (ptrdiff_t nargs, Lisp_Object *args)
2343 Lisp_Object val;
2345 if (inhibit_eval_during_redisplay)
2346 val = Qnil;
2347 else
2349 int count = SPECPDL_INDEX ();
2350 struct gcpro gcpro1;
2352 GCPRO1 (args[0]);
2353 gcpro1.nvars = nargs;
2354 specbind (Qinhibit_redisplay, Qt);
2355 /* Use Qt to ensure debugger does not run,
2356 so there is no possibility of wanting to redisplay. */
2357 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2358 safe_eval_handler);
2359 UNGCPRO;
2360 val = unbind_to (count, val);
2363 return val;
2367 /* Call function FN with one argument ARG.
2368 Return the result, or nil if something went wrong. */
2370 Lisp_Object
2371 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2373 Lisp_Object args[2];
2374 args[0] = fn;
2375 args[1] = arg;
2376 return safe_call (2, args);
2379 static Lisp_Object Qeval;
2381 Lisp_Object
2382 safe_eval (Lisp_Object sexpr)
2384 return safe_call1 (Qeval, sexpr);
2387 /* Call function FN with one argument ARG.
2388 Return the result, or nil if something went wrong. */
2390 Lisp_Object
2391 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2393 Lisp_Object args[3];
2394 args[0] = fn;
2395 args[1] = arg1;
2396 args[2] = arg2;
2397 return safe_call (3, args);
2402 /***********************************************************************
2403 Debugging
2404 ***********************************************************************/
2406 #if 0
2408 /* Define CHECK_IT to perform sanity checks on iterators.
2409 This is for debugging. It is too slow to do unconditionally. */
2411 static void
2412 check_it (struct it *it)
2414 if (it->method == GET_FROM_STRING)
2416 xassert (STRINGP (it->string));
2417 xassert (IT_STRING_CHARPOS (*it) >= 0);
2419 else
2421 xassert (IT_STRING_CHARPOS (*it) < 0);
2422 if (it->method == GET_FROM_BUFFER)
2424 /* Check that character and byte positions agree. */
2425 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2429 if (it->dpvec)
2430 xassert (it->current.dpvec_index >= 0);
2431 else
2432 xassert (it->current.dpvec_index < 0);
2435 #define CHECK_IT(IT) check_it ((IT))
2437 #else /* not 0 */
2439 #define CHECK_IT(IT) (void) 0
2441 #endif /* not 0 */
2444 #if GLYPH_DEBUG && XASSERTS
2446 /* Check that the window end of window W is what we expect it
2447 to be---the last row in the current matrix displaying text. */
2449 static void
2450 check_window_end (struct window *w)
2452 if (!MINI_WINDOW_P (w)
2453 && !NILP (w->window_end_valid))
2455 struct glyph_row *row;
2456 xassert ((row = MATRIX_ROW (w->current_matrix,
2457 XFASTINT (w->window_end_vpos)),
2458 !row->enabled_p
2459 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2460 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2464 #define CHECK_WINDOW_END(W) check_window_end ((W))
2466 #else
2468 #define CHECK_WINDOW_END(W) (void) 0
2470 #endif
2474 /***********************************************************************
2475 Iterator initialization
2476 ***********************************************************************/
2478 /* Initialize IT for displaying current_buffer in window W, starting
2479 at character position CHARPOS. CHARPOS < 0 means that no buffer
2480 position is specified which is useful when the iterator is assigned
2481 a position later. BYTEPOS is the byte position corresponding to
2482 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2484 If ROW is not null, calls to produce_glyphs with IT as parameter
2485 will produce glyphs in that row.
2487 BASE_FACE_ID is the id of a base face to use. It must be one of
2488 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2489 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2490 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2492 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2493 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2494 will be initialized to use the corresponding mode line glyph row of
2495 the desired matrix of W. */
2497 void
2498 init_iterator (struct it *it, struct window *w,
2499 EMACS_INT charpos, EMACS_INT bytepos,
2500 struct glyph_row *row, enum face_id base_face_id)
2502 int highlight_region_p;
2503 enum face_id remapped_base_face_id = base_face_id;
2505 /* Some precondition checks. */
2506 xassert (w != NULL && it != NULL);
2507 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2508 && charpos <= ZV));
2510 /* If face attributes have been changed since the last redisplay,
2511 free realized faces now because they depend on face definitions
2512 that might have changed. Don't free faces while there might be
2513 desired matrices pending which reference these faces. */
2514 if (face_change_count && !inhibit_free_realized_faces)
2516 face_change_count = 0;
2517 free_all_realized_faces (Qnil);
2520 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2521 if (! NILP (Vface_remapping_alist))
2522 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2524 /* Use one of the mode line rows of W's desired matrix if
2525 appropriate. */
2526 if (row == NULL)
2528 if (base_face_id == MODE_LINE_FACE_ID
2529 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2530 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2531 else if (base_face_id == HEADER_LINE_FACE_ID)
2532 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2535 /* Clear IT. */
2536 memset (it, 0, sizeof *it);
2537 it->current.overlay_string_index = -1;
2538 it->current.dpvec_index = -1;
2539 it->base_face_id = remapped_base_face_id;
2540 it->string = Qnil;
2541 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2542 it->paragraph_embedding = L2R;
2543 it->bidi_it.string.lstring = Qnil;
2544 it->bidi_it.string.s = NULL;
2545 it->bidi_it.string.bufpos = 0;
2547 /* The window in which we iterate over current_buffer: */
2548 XSETWINDOW (it->window, w);
2549 it->w = w;
2550 it->f = XFRAME (w->frame);
2552 it->cmp_it.id = -1;
2554 /* Extra space between lines (on window systems only). */
2555 if (base_face_id == DEFAULT_FACE_ID
2556 && FRAME_WINDOW_P (it->f))
2558 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2559 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2560 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2561 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2562 * FRAME_LINE_HEIGHT (it->f));
2563 else if (it->f->extra_line_spacing > 0)
2564 it->extra_line_spacing = it->f->extra_line_spacing;
2565 it->max_extra_line_spacing = 0;
2568 /* If realized faces have been removed, e.g. because of face
2569 attribute changes of named faces, recompute them. When running
2570 in batch mode, the face cache of the initial frame is null. If
2571 we happen to get called, make a dummy face cache. */
2572 if (FRAME_FACE_CACHE (it->f) == NULL)
2573 init_frame_faces (it->f);
2574 if (FRAME_FACE_CACHE (it->f)->used == 0)
2575 recompute_basic_faces (it->f);
2577 /* Current value of the `slice', `space-width', and 'height' properties. */
2578 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2579 it->space_width = Qnil;
2580 it->font_height = Qnil;
2581 it->override_ascent = -1;
2583 /* Are control characters displayed as `^C'? */
2584 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2586 /* -1 means everything between a CR and the following line end
2587 is invisible. >0 means lines indented more than this value are
2588 invisible. */
2589 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2590 ? XINT (BVAR (current_buffer, selective_display))
2591 : (!NILP (BVAR (current_buffer, selective_display))
2592 ? -1 : 0));
2593 it->selective_display_ellipsis_p
2594 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2596 /* Display table to use. */
2597 it->dp = window_display_table (w);
2599 /* Are multibyte characters enabled in current_buffer? */
2600 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2602 /* Non-zero if we should highlight the region. */
2603 highlight_region_p
2604 = (!NILP (Vtransient_mark_mode)
2605 && !NILP (BVAR (current_buffer, mark_active))
2606 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2608 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2609 start and end of a visible region in window IT->w. Set both to
2610 -1 to indicate no region. */
2611 if (highlight_region_p
2612 /* Maybe highlight only in selected window. */
2613 && (/* Either show region everywhere. */
2614 highlight_nonselected_windows
2615 /* Or show region in the selected window. */
2616 || w == XWINDOW (selected_window)
2617 /* Or show the region if we are in the mini-buffer and W is
2618 the window the mini-buffer refers to. */
2619 || (MINI_WINDOW_P (XWINDOW (selected_window))
2620 && WINDOWP (minibuf_selected_window)
2621 && w == XWINDOW (minibuf_selected_window))))
2623 EMACS_INT markpos = marker_position (BVAR (current_buffer, mark));
2624 it->region_beg_charpos = min (PT, markpos);
2625 it->region_end_charpos = max (PT, markpos);
2627 else
2628 it->region_beg_charpos = it->region_end_charpos = -1;
2630 /* Get the position at which the redisplay_end_trigger hook should
2631 be run, if it is to be run at all. */
2632 if (MARKERP (w->redisplay_end_trigger)
2633 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2634 it->redisplay_end_trigger_charpos
2635 = marker_position (w->redisplay_end_trigger);
2636 else if (INTEGERP (w->redisplay_end_trigger))
2637 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2639 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2641 /* Are lines in the display truncated? */
2642 if (base_face_id != DEFAULT_FACE_ID
2643 || XINT (it->w->hscroll)
2644 || (! WINDOW_FULL_WIDTH_P (it->w)
2645 && ((!NILP (Vtruncate_partial_width_windows)
2646 && !INTEGERP (Vtruncate_partial_width_windows))
2647 || (INTEGERP (Vtruncate_partial_width_windows)
2648 && (WINDOW_TOTAL_COLS (it->w)
2649 < XINT (Vtruncate_partial_width_windows))))))
2650 it->line_wrap = TRUNCATE;
2651 else if (NILP (BVAR (current_buffer, truncate_lines)))
2652 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2653 ? WINDOW_WRAP : WORD_WRAP;
2654 else
2655 it->line_wrap = TRUNCATE;
2657 /* Get dimensions of truncation and continuation glyphs. These are
2658 displayed as fringe bitmaps under X, so we don't need them for such
2659 frames. */
2660 if (!FRAME_WINDOW_P (it->f))
2662 if (it->line_wrap == TRUNCATE)
2664 /* We will need the truncation glyph. */
2665 xassert (it->glyph_row == NULL);
2666 produce_special_glyphs (it, IT_TRUNCATION);
2667 it->truncation_pixel_width = it->pixel_width;
2669 else
2671 /* We will need the continuation glyph. */
2672 xassert (it->glyph_row == NULL);
2673 produce_special_glyphs (it, IT_CONTINUATION);
2674 it->continuation_pixel_width = it->pixel_width;
2677 /* Reset these values to zero because the produce_special_glyphs
2678 above has changed them. */
2679 it->pixel_width = it->ascent = it->descent = 0;
2680 it->phys_ascent = it->phys_descent = 0;
2683 /* Set this after getting the dimensions of truncation and
2684 continuation glyphs, so that we don't produce glyphs when calling
2685 produce_special_glyphs, above. */
2686 it->glyph_row = row;
2687 it->area = TEXT_AREA;
2689 /* Forget any previous info about this row being reversed. */
2690 if (it->glyph_row)
2691 it->glyph_row->reversed_p = 0;
2693 /* Get the dimensions of the display area. The display area
2694 consists of the visible window area plus a horizontally scrolled
2695 part to the left of the window. All x-values are relative to the
2696 start of this total display area. */
2697 if (base_face_id != DEFAULT_FACE_ID)
2699 /* Mode lines, menu bar in terminal frames. */
2700 it->first_visible_x = 0;
2701 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2703 else
2705 it->first_visible_x
2706 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2707 it->last_visible_x = (it->first_visible_x
2708 + window_box_width (w, TEXT_AREA));
2710 /* If we truncate lines, leave room for the truncator glyph(s) at
2711 the right margin. Otherwise, leave room for the continuation
2712 glyph(s). Truncation and continuation glyphs are not inserted
2713 for window-based redisplay. */
2714 if (!FRAME_WINDOW_P (it->f))
2716 if (it->line_wrap == TRUNCATE)
2717 it->last_visible_x -= it->truncation_pixel_width;
2718 else
2719 it->last_visible_x -= it->continuation_pixel_width;
2722 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2723 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2726 /* Leave room for a border glyph. */
2727 if (!FRAME_WINDOW_P (it->f)
2728 && !WINDOW_RIGHTMOST_P (it->w))
2729 it->last_visible_x -= 1;
2731 it->last_visible_y = window_text_bottom_y (w);
2733 /* For mode lines and alike, arrange for the first glyph having a
2734 left box line if the face specifies a box. */
2735 if (base_face_id != DEFAULT_FACE_ID)
2737 struct face *face;
2739 it->face_id = remapped_base_face_id;
2741 /* If we have a boxed mode line, make the first character appear
2742 with a left box line. */
2743 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2744 if (face->box != FACE_NO_BOX)
2745 it->start_of_box_run_p = 1;
2748 /* If a buffer position was specified, set the iterator there,
2749 getting overlays and face properties from that position. */
2750 if (charpos >= BUF_BEG (current_buffer))
2752 it->end_charpos = ZV;
2753 IT_CHARPOS (*it) = charpos;
2755 /* We will rely on `reseat' to set this up properly, via
2756 handle_face_prop. */
2757 it->face_id = it->base_face_id;
2759 /* Compute byte position if not specified. */
2760 if (bytepos < charpos)
2761 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2762 else
2763 IT_BYTEPOS (*it) = bytepos;
2765 it->start = it->current;
2766 /* Do we need to reorder bidirectional text? Not if this is a
2767 unibyte buffer: by definition, none of the single-byte
2768 characters are strong R2L, so no reordering is needed. And
2769 bidi.c doesn't support unibyte buffers anyway. Also, don't
2770 reorder while we are loading loadup.el, since the tables of
2771 character properties needed for reordering are not yet
2772 available. */
2773 it->bidi_p =
2774 NILP (Vpurify_flag)
2775 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2776 && it->multibyte_p;
2778 /* If we are to reorder bidirectional text, init the bidi
2779 iterator. */
2780 if (it->bidi_p)
2782 /* Note the paragraph direction that this buffer wants to
2783 use. */
2784 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2785 Qleft_to_right))
2786 it->paragraph_embedding = L2R;
2787 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2788 Qright_to_left))
2789 it->paragraph_embedding = R2L;
2790 else
2791 it->paragraph_embedding = NEUTRAL_DIR;
2792 bidi_unshelve_cache (NULL, 0);
2793 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2794 &it->bidi_it);
2797 /* Compute faces etc. */
2798 reseat (it, it->current.pos, 1);
2801 CHECK_IT (it);
2805 /* Initialize IT for the display of window W with window start POS. */
2807 void
2808 start_display (struct it *it, struct window *w, struct text_pos pos)
2810 struct glyph_row *row;
2811 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2813 row = w->desired_matrix->rows + first_vpos;
2814 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2815 it->first_vpos = first_vpos;
2817 /* Don't reseat to previous visible line start if current start
2818 position is in a string or image. */
2819 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2821 int start_at_line_beg_p;
2822 int first_y = it->current_y;
2824 /* If window start is not at a line start, skip forward to POS to
2825 get the correct continuation lines width. */
2826 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2827 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2828 if (!start_at_line_beg_p)
2830 int new_x;
2832 reseat_at_previous_visible_line_start (it);
2833 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2835 new_x = it->current_x + it->pixel_width;
2837 /* If lines are continued, this line may end in the middle
2838 of a multi-glyph character (e.g. a control character
2839 displayed as \003, or in the middle of an overlay
2840 string). In this case move_it_to above will not have
2841 taken us to the start of the continuation line but to the
2842 end of the continued line. */
2843 if (it->current_x > 0
2844 && it->line_wrap != TRUNCATE /* Lines are continued. */
2845 && (/* And glyph doesn't fit on the line. */
2846 new_x > it->last_visible_x
2847 /* Or it fits exactly and we're on a window
2848 system frame. */
2849 || (new_x == it->last_visible_x
2850 && FRAME_WINDOW_P (it->f))))
2852 if ((it->current.dpvec_index >= 0
2853 || it->current.overlay_string_index >= 0)
2854 /* If we are on a newline from a display vector or
2855 overlay string, then we are already at the end of
2856 a screen line; no need to go to the next line in
2857 that case, as this line is not really continued.
2858 (If we do go to the next line, C-e will not DTRT.) */
2859 && it->c != '\n')
2861 set_iterator_to_next (it, 1);
2862 move_it_in_display_line_to (it, -1, -1, 0);
2865 it->continuation_lines_width += it->current_x;
2867 /* If the character at POS is displayed via a display
2868 vector, move_it_to above stops at the final glyph of
2869 IT->dpvec. To make the caller redisplay that character
2870 again (a.k.a. start at POS), we need to reset the
2871 dpvec_index to the beginning of IT->dpvec. */
2872 else if (it->current.dpvec_index >= 0)
2873 it->current.dpvec_index = 0;
2875 /* We're starting a new display line, not affected by the
2876 height of the continued line, so clear the appropriate
2877 fields in the iterator structure. */
2878 it->max_ascent = it->max_descent = 0;
2879 it->max_phys_ascent = it->max_phys_descent = 0;
2881 it->current_y = first_y;
2882 it->vpos = 0;
2883 it->current_x = it->hpos = 0;
2889 /* Return 1 if POS is a position in ellipses displayed for invisible
2890 text. W is the window we display, for text property lookup. */
2892 static int
2893 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2895 Lisp_Object prop, window;
2896 int ellipses_p = 0;
2897 EMACS_INT charpos = CHARPOS (pos->pos);
2899 /* If POS specifies a position in a display vector, this might
2900 be for an ellipsis displayed for invisible text. We won't
2901 get the iterator set up for delivering that ellipsis unless
2902 we make sure that it gets aware of the invisible text. */
2903 if (pos->dpvec_index >= 0
2904 && pos->overlay_string_index < 0
2905 && CHARPOS (pos->string_pos) < 0
2906 && charpos > BEGV
2907 && (XSETWINDOW (window, w),
2908 prop = Fget_char_property (make_number (charpos),
2909 Qinvisible, window),
2910 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2912 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2913 window);
2914 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2917 return ellipses_p;
2921 /* Initialize IT for stepping through current_buffer in window W,
2922 starting at position POS that includes overlay string and display
2923 vector/ control character translation position information. Value
2924 is zero if there are overlay strings with newlines at POS. */
2926 static int
2927 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2929 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2930 int i, overlay_strings_with_newlines = 0;
2932 /* If POS specifies a position in a display vector, this might
2933 be for an ellipsis displayed for invisible text. We won't
2934 get the iterator set up for delivering that ellipsis unless
2935 we make sure that it gets aware of the invisible text. */
2936 if (in_ellipses_for_invisible_text_p (pos, w))
2938 --charpos;
2939 bytepos = 0;
2942 /* Keep in mind: the call to reseat in init_iterator skips invisible
2943 text, so we might end up at a position different from POS. This
2944 is only a problem when POS is a row start after a newline and an
2945 overlay starts there with an after-string, and the overlay has an
2946 invisible property. Since we don't skip invisible text in
2947 display_line and elsewhere immediately after consuming the
2948 newline before the row start, such a POS will not be in a string,
2949 but the call to init_iterator below will move us to the
2950 after-string. */
2951 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2953 /* This only scans the current chunk -- it should scan all chunks.
2954 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2955 to 16 in 22.1 to make this a lesser problem. */
2956 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2958 const char *s = SSDATA (it->overlay_strings[i]);
2959 const char *e = s + SBYTES (it->overlay_strings[i]);
2961 while (s < e && *s != '\n')
2962 ++s;
2964 if (s < e)
2966 overlay_strings_with_newlines = 1;
2967 break;
2971 /* If position is within an overlay string, set up IT to the right
2972 overlay string. */
2973 if (pos->overlay_string_index >= 0)
2975 int relative_index;
2977 /* If the first overlay string happens to have a `display'
2978 property for an image, the iterator will be set up for that
2979 image, and we have to undo that setup first before we can
2980 correct the overlay string index. */
2981 if (it->method == GET_FROM_IMAGE)
2982 pop_it (it);
2984 /* We already have the first chunk of overlay strings in
2985 IT->overlay_strings. Load more until the one for
2986 pos->overlay_string_index is in IT->overlay_strings. */
2987 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2989 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2990 it->current.overlay_string_index = 0;
2991 while (n--)
2993 load_overlay_strings (it, 0);
2994 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2998 it->current.overlay_string_index = pos->overlay_string_index;
2999 relative_index = (it->current.overlay_string_index
3000 % OVERLAY_STRING_CHUNK_SIZE);
3001 it->string = it->overlay_strings[relative_index];
3002 xassert (STRINGP (it->string));
3003 it->current.string_pos = pos->string_pos;
3004 it->method = GET_FROM_STRING;
3007 if (CHARPOS (pos->string_pos) >= 0)
3009 /* Recorded position is not in an overlay string, but in another
3010 string. This can only be a string from a `display' property.
3011 IT should already be filled with that string. */
3012 it->current.string_pos = pos->string_pos;
3013 xassert (STRINGP (it->string));
3016 /* Restore position in display vector translations, control
3017 character translations or ellipses. */
3018 if (pos->dpvec_index >= 0)
3020 if (it->dpvec == NULL)
3021 get_next_display_element (it);
3022 xassert (it->dpvec && it->current.dpvec_index == 0);
3023 it->current.dpvec_index = pos->dpvec_index;
3026 CHECK_IT (it);
3027 return !overlay_strings_with_newlines;
3031 /* Initialize IT for stepping through current_buffer in window W
3032 starting at ROW->start. */
3034 static void
3035 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3037 init_from_display_pos (it, w, &row->start);
3038 it->start = row->start;
3039 it->continuation_lines_width = row->continuation_lines_width;
3040 CHECK_IT (it);
3044 /* Initialize IT for stepping through current_buffer in window W
3045 starting in the line following ROW, i.e. starting at ROW->end.
3046 Value is zero if there are overlay strings with newlines at ROW's
3047 end position. */
3049 static int
3050 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3052 int success = 0;
3054 if (init_from_display_pos (it, w, &row->end))
3056 if (row->continued_p)
3057 it->continuation_lines_width
3058 = row->continuation_lines_width + row->pixel_width;
3059 CHECK_IT (it);
3060 success = 1;
3063 return success;
3069 /***********************************************************************
3070 Text properties
3071 ***********************************************************************/
3073 /* Called when IT reaches IT->stop_charpos. Handle text property and
3074 overlay changes. Set IT->stop_charpos to the next position where
3075 to stop. */
3077 static void
3078 handle_stop (struct it *it)
3080 enum prop_handled handled;
3081 int handle_overlay_change_p;
3082 struct props *p;
3084 it->dpvec = NULL;
3085 it->current.dpvec_index = -1;
3086 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3087 it->ignore_overlay_strings_at_pos_p = 0;
3088 it->ellipsis_p = 0;
3090 /* Use face of preceding text for ellipsis (if invisible) */
3091 if (it->selective_display_ellipsis_p)
3092 it->saved_face_id = it->face_id;
3096 handled = HANDLED_NORMALLY;
3098 /* Call text property handlers. */
3099 for (p = it_props; p->handler; ++p)
3101 handled = p->handler (it);
3103 if (handled == HANDLED_RECOMPUTE_PROPS)
3104 break;
3105 else if (handled == HANDLED_RETURN)
3107 /* We still want to show before and after strings from
3108 overlays even if the actual buffer text is replaced. */
3109 if (!handle_overlay_change_p
3110 || it->sp > 1
3111 || !get_overlay_strings_1 (it, 0, 0))
3113 if (it->ellipsis_p)
3114 setup_for_ellipsis (it, 0);
3115 /* When handling a display spec, we might load an
3116 empty string. In that case, discard it here. We
3117 used to discard it in handle_single_display_spec,
3118 but that causes get_overlay_strings_1, above, to
3119 ignore overlay strings that we must check. */
3120 if (STRINGP (it->string) && !SCHARS (it->string))
3121 pop_it (it);
3122 return;
3124 else if (STRINGP (it->string) && !SCHARS (it->string))
3125 pop_it (it);
3126 else
3128 it->ignore_overlay_strings_at_pos_p = 1;
3129 it->string_from_display_prop_p = 0;
3130 it->from_disp_prop_p = 0;
3131 handle_overlay_change_p = 0;
3133 handled = HANDLED_RECOMPUTE_PROPS;
3134 break;
3136 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3137 handle_overlay_change_p = 0;
3140 if (handled != HANDLED_RECOMPUTE_PROPS)
3142 /* Don't check for overlay strings below when set to deliver
3143 characters from a display vector. */
3144 if (it->method == GET_FROM_DISPLAY_VECTOR)
3145 handle_overlay_change_p = 0;
3147 /* Handle overlay changes.
3148 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3149 if it finds overlays. */
3150 if (handle_overlay_change_p)
3151 handled = handle_overlay_change (it);
3154 if (it->ellipsis_p)
3156 setup_for_ellipsis (it, 0);
3157 break;
3160 while (handled == HANDLED_RECOMPUTE_PROPS);
3162 /* Determine where to stop next. */
3163 if (handled == HANDLED_NORMALLY)
3164 compute_stop_pos (it);
3168 /* Compute IT->stop_charpos from text property and overlay change
3169 information for IT's current position. */
3171 static void
3172 compute_stop_pos (struct it *it)
3174 register INTERVAL iv, next_iv;
3175 Lisp_Object object, limit, position;
3176 EMACS_INT charpos, bytepos;
3178 if (STRINGP (it->string))
3180 /* Strings are usually short, so don't limit the search for
3181 properties. */
3182 it->stop_charpos = it->end_charpos;
3183 object = it->string;
3184 limit = Qnil;
3185 charpos = IT_STRING_CHARPOS (*it);
3186 bytepos = IT_STRING_BYTEPOS (*it);
3188 else
3190 EMACS_INT pos;
3192 /* If end_charpos is out of range for some reason, such as a
3193 misbehaving display function, rationalize it (Bug#5984). */
3194 if (it->end_charpos > ZV)
3195 it->end_charpos = ZV;
3196 it->stop_charpos = it->end_charpos;
3198 /* If next overlay change is in front of the current stop pos
3199 (which is IT->end_charpos), stop there. Note: value of
3200 next_overlay_change is point-max if no overlay change
3201 follows. */
3202 charpos = IT_CHARPOS (*it);
3203 bytepos = IT_BYTEPOS (*it);
3204 pos = next_overlay_change (charpos);
3205 if (pos < it->stop_charpos)
3206 it->stop_charpos = pos;
3208 /* If showing the region, we have to stop at the region
3209 start or end because the face might change there. */
3210 if (it->region_beg_charpos > 0)
3212 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3213 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3214 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3215 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3218 /* Set up variables for computing the stop position from text
3219 property changes. */
3220 XSETBUFFER (object, current_buffer);
3221 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3224 /* Get the interval containing IT's position. Value is a null
3225 interval if there isn't such an interval. */
3226 position = make_number (charpos);
3227 iv = validate_interval_range (object, &position, &position, 0);
3228 if (!NULL_INTERVAL_P (iv))
3230 Lisp_Object values_here[LAST_PROP_IDX];
3231 struct props *p;
3233 /* Get properties here. */
3234 for (p = it_props; p->handler; ++p)
3235 values_here[p->idx] = textget (iv->plist, *p->name);
3237 /* Look for an interval following iv that has different
3238 properties. */
3239 for (next_iv = next_interval (iv);
3240 (!NULL_INTERVAL_P (next_iv)
3241 && (NILP (limit)
3242 || XFASTINT (limit) > next_iv->position));
3243 next_iv = next_interval (next_iv))
3245 for (p = it_props; p->handler; ++p)
3247 Lisp_Object new_value;
3249 new_value = textget (next_iv->plist, *p->name);
3250 if (!EQ (values_here[p->idx], new_value))
3251 break;
3254 if (p->handler)
3255 break;
3258 if (!NULL_INTERVAL_P (next_iv))
3260 if (INTEGERP (limit)
3261 && next_iv->position >= XFASTINT (limit))
3262 /* No text property change up to limit. */
3263 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3264 else
3265 /* Text properties change in next_iv. */
3266 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3270 if (it->cmp_it.id < 0)
3272 EMACS_INT stoppos = it->end_charpos;
3274 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3275 stoppos = -1;
3276 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3277 stoppos, it->string);
3280 xassert (STRINGP (it->string)
3281 || (it->stop_charpos >= BEGV
3282 && it->stop_charpos >= IT_CHARPOS (*it)));
3286 /* Return the position of the next overlay change after POS in
3287 current_buffer. Value is point-max if no overlay change
3288 follows. This is like `next-overlay-change' but doesn't use
3289 xmalloc. */
3291 static EMACS_INT
3292 next_overlay_change (EMACS_INT pos)
3294 ptrdiff_t i, noverlays;
3295 EMACS_INT endpos;
3296 Lisp_Object *overlays;
3298 /* Get all overlays at the given position. */
3299 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3301 /* If any of these overlays ends before endpos,
3302 use its ending point instead. */
3303 for (i = 0; i < noverlays; ++i)
3305 Lisp_Object oend;
3306 EMACS_INT oendpos;
3308 oend = OVERLAY_END (overlays[i]);
3309 oendpos = OVERLAY_POSITION (oend);
3310 endpos = min (endpos, oendpos);
3313 return endpos;
3316 /* How many characters forward to search for a display property or
3317 display string. Searching too far forward makes the bidi display
3318 sluggish, especially in small windows. */
3319 #define MAX_DISP_SCAN 250
3321 /* Return the character position of a display string at or after
3322 position specified by POSITION. If no display string exists at or
3323 after POSITION, return ZV. A display string is either an overlay
3324 with `display' property whose value is a string, or a `display'
3325 text property whose value is a string. STRING is data about the
3326 string to iterate; if STRING->lstring is nil, we are iterating a
3327 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3328 on a GUI frame. DISP_PROP is set to zero if we searched
3329 MAX_DISP_SCAN characters forward without finding any display
3330 strings, non-zero otherwise. It is set to 2 if the display string
3331 uses any kind of `(space ...)' spec that will produce a stretch of
3332 white space in the text area. */
3333 EMACS_INT
3334 compute_display_string_pos (struct text_pos *position,
3335 struct bidi_string_data *string,
3336 int frame_window_p, int *disp_prop)
3338 /* OBJECT = nil means current buffer. */
3339 Lisp_Object object =
3340 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3341 Lisp_Object pos, spec, limpos;
3342 int string_p = (string && (STRINGP (string->lstring) || string->s));
3343 EMACS_INT eob = string_p ? string->schars : ZV;
3344 EMACS_INT begb = string_p ? 0 : BEGV;
3345 EMACS_INT bufpos, charpos = CHARPOS (*position);
3346 EMACS_INT lim =
3347 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3348 struct text_pos tpos;
3349 int rv = 0;
3351 *disp_prop = 1;
3353 if (charpos >= eob
3354 /* We don't support display properties whose values are strings
3355 that have display string properties. */
3356 || string->from_disp_str
3357 /* C strings cannot have display properties. */
3358 || (string->s && !STRINGP (object)))
3360 *disp_prop = 0;
3361 return eob;
3364 /* If the character at CHARPOS is where the display string begins,
3365 return CHARPOS. */
3366 pos = make_number (charpos);
3367 if (STRINGP (object))
3368 bufpos = string->bufpos;
3369 else
3370 bufpos = charpos;
3371 tpos = *position;
3372 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3373 && (charpos <= begb
3374 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3375 object),
3376 spec))
3377 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3378 frame_window_p)))
3380 if (rv == 2)
3381 *disp_prop = 2;
3382 return charpos;
3385 /* Look forward for the first character with a `display' property
3386 that will replace the underlying text when displayed. */
3387 limpos = make_number (lim);
3388 do {
3389 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3390 CHARPOS (tpos) = XFASTINT (pos);
3391 if (CHARPOS (tpos) >= lim)
3393 *disp_prop = 0;
3394 break;
3396 if (STRINGP (object))
3397 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3398 else
3399 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3400 spec = Fget_char_property (pos, Qdisplay, object);
3401 if (!STRINGP (object))
3402 bufpos = CHARPOS (tpos);
3403 } while (NILP (spec)
3404 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3405 bufpos, frame_window_p)));
3406 if (rv == 2)
3407 *disp_prop = 2;
3409 return CHARPOS (tpos);
3412 /* Return the character position of the end of the display string that
3413 started at CHARPOS. If there's no display string at CHARPOS,
3414 return -1. A display string is either an overlay with `display'
3415 property whose value is a string or a `display' text property whose
3416 value is a string. */
3417 EMACS_INT
3418 compute_display_string_end (EMACS_INT charpos, struct bidi_string_data *string)
3420 /* OBJECT = nil means current buffer. */
3421 Lisp_Object object =
3422 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3423 Lisp_Object pos = make_number (charpos);
3424 EMACS_INT eob =
3425 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3427 if (charpos >= eob || (string->s && !STRINGP (object)))
3428 return eob;
3430 /* It could happen that the display property or overlay was removed
3431 since we found it in compute_display_string_pos above. One way
3432 this can happen is if JIT font-lock was called (through
3433 handle_fontified_prop), and jit-lock-functions remove text
3434 properties or overlays from the portion of buffer that includes
3435 CHARPOS. Muse mode is known to do that, for example. In this
3436 case, we return -1 to the caller, to signal that no display
3437 string is actually present at CHARPOS. See bidi_fetch_char for
3438 how this is handled.
3440 An alternative would be to never look for display properties past
3441 it->stop_charpos. But neither compute_display_string_pos nor
3442 bidi_fetch_char that calls it know or care where the next
3443 stop_charpos is. */
3444 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3445 return -1;
3447 /* Look forward for the first character where the `display' property
3448 changes. */
3449 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3451 return XFASTINT (pos);
3456 /***********************************************************************
3457 Fontification
3458 ***********************************************************************/
3460 /* Handle changes in the `fontified' property of the current buffer by
3461 calling hook functions from Qfontification_functions to fontify
3462 regions of text. */
3464 static enum prop_handled
3465 handle_fontified_prop (struct it *it)
3467 Lisp_Object prop, pos;
3468 enum prop_handled handled = HANDLED_NORMALLY;
3470 if (!NILP (Vmemory_full))
3471 return handled;
3473 /* Get the value of the `fontified' property at IT's current buffer
3474 position. (The `fontified' property doesn't have a special
3475 meaning in strings.) If the value is nil, call functions from
3476 Qfontification_functions. */
3477 if (!STRINGP (it->string)
3478 && it->s == NULL
3479 && !NILP (Vfontification_functions)
3480 && !NILP (Vrun_hooks)
3481 && (pos = make_number (IT_CHARPOS (*it)),
3482 prop = Fget_char_property (pos, Qfontified, Qnil),
3483 /* Ignore the special cased nil value always present at EOB since
3484 no amount of fontifying will be able to change it. */
3485 NILP (prop) && IT_CHARPOS (*it) < Z))
3487 int count = SPECPDL_INDEX ();
3488 Lisp_Object val;
3489 struct buffer *obuf = current_buffer;
3490 int begv = BEGV, zv = ZV;
3491 int old_clip_changed = current_buffer->clip_changed;
3493 val = Vfontification_functions;
3494 specbind (Qfontification_functions, Qnil);
3496 xassert (it->end_charpos == ZV);
3498 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3499 safe_call1 (val, pos);
3500 else
3502 Lisp_Object fns, fn;
3503 struct gcpro gcpro1, gcpro2;
3505 fns = Qnil;
3506 GCPRO2 (val, fns);
3508 for (; CONSP (val); val = XCDR (val))
3510 fn = XCAR (val);
3512 if (EQ (fn, Qt))
3514 /* A value of t indicates this hook has a local
3515 binding; it means to run the global binding too.
3516 In a global value, t should not occur. If it
3517 does, we must ignore it to avoid an endless
3518 loop. */
3519 for (fns = Fdefault_value (Qfontification_functions);
3520 CONSP (fns);
3521 fns = XCDR (fns))
3523 fn = XCAR (fns);
3524 if (!EQ (fn, Qt))
3525 safe_call1 (fn, pos);
3528 else
3529 safe_call1 (fn, pos);
3532 UNGCPRO;
3535 unbind_to (count, Qnil);
3537 /* Fontification functions routinely call `save-restriction'.
3538 Normally, this tags clip_changed, which can confuse redisplay
3539 (see discussion in Bug#6671). Since we don't perform any
3540 special handling of fontification changes in the case where
3541 `save-restriction' isn't called, there's no point doing so in
3542 this case either. So, if the buffer's restrictions are
3543 actually left unchanged, reset clip_changed. */
3544 if (obuf == current_buffer)
3546 if (begv == BEGV && zv == ZV)
3547 current_buffer->clip_changed = old_clip_changed;
3549 /* There isn't much we can reasonably do to protect against
3550 misbehaving fontification, but here's a fig leaf. */
3551 else if (!NILP (BVAR (obuf, name)))
3552 set_buffer_internal_1 (obuf);
3554 /* The fontification code may have added/removed text.
3555 It could do even a lot worse, but let's at least protect against
3556 the most obvious case where only the text past `pos' gets changed',
3557 as is/was done in grep.el where some escapes sequences are turned
3558 into face properties (bug#7876). */
3559 it->end_charpos = ZV;
3561 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3562 something. This avoids an endless loop if they failed to
3563 fontify the text for which reason ever. */
3564 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3565 handled = HANDLED_RECOMPUTE_PROPS;
3568 return handled;
3573 /***********************************************************************
3574 Faces
3575 ***********************************************************************/
3577 /* Set up iterator IT from face properties at its current position.
3578 Called from handle_stop. */
3580 static enum prop_handled
3581 handle_face_prop (struct it *it)
3583 int new_face_id;
3584 EMACS_INT next_stop;
3586 if (!STRINGP (it->string))
3588 new_face_id
3589 = face_at_buffer_position (it->w,
3590 IT_CHARPOS (*it),
3591 it->region_beg_charpos,
3592 it->region_end_charpos,
3593 &next_stop,
3594 (IT_CHARPOS (*it)
3595 + TEXT_PROP_DISTANCE_LIMIT),
3596 0, it->base_face_id);
3598 /* Is this a start of a run of characters with box face?
3599 Caveat: this can be called for a freshly initialized
3600 iterator; face_id is -1 in this case. We know that the new
3601 face will not change until limit, i.e. if the new face has a
3602 box, all characters up to limit will have one. But, as
3603 usual, we don't know whether limit is really the end. */
3604 if (new_face_id != it->face_id)
3606 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3608 /* If new face has a box but old face has not, this is
3609 the start of a run of characters with box, i.e. it has
3610 a shadow on the left side. The value of face_id of the
3611 iterator will be -1 if this is the initial call that gets
3612 the face. In this case, we have to look in front of IT's
3613 position and see whether there is a face != new_face_id. */
3614 it->start_of_box_run_p
3615 = (new_face->box != FACE_NO_BOX
3616 && (it->face_id >= 0
3617 || IT_CHARPOS (*it) == BEG
3618 || new_face_id != face_before_it_pos (it)));
3619 it->face_box_p = new_face->box != FACE_NO_BOX;
3622 else
3624 int base_face_id;
3625 EMACS_INT bufpos;
3626 int i;
3627 Lisp_Object from_overlay
3628 = (it->current.overlay_string_index >= 0
3629 ? it->string_overlays[it->current.overlay_string_index]
3630 : Qnil);
3632 /* See if we got to this string directly or indirectly from
3633 an overlay property. That includes the before-string or
3634 after-string of an overlay, strings in display properties
3635 provided by an overlay, their text properties, etc.
3637 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3638 if (! NILP (from_overlay))
3639 for (i = it->sp - 1; i >= 0; i--)
3641 if (it->stack[i].current.overlay_string_index >= 0)
3642 from_overlay
3643 = it->string_overlays[it->stack[i].current.overlay_string_index];
3644 else if (! NILP (it->stack[i].from_overlay))
3645 from_overlay = it->stack[i].from_overlay;
3647 if (!NILP (from_overlay))
3648 break;
3651 if (! NILP (from_overlay))
3653 bufpos = IT_CHARPOS (*it);
3654 /* For a string from an overlay, the base face depends
3655 only on text properties and ignores overlays. */
3656 base_face_id
3657 = face_for_overlay_string (it->w,
3658 IT_CHARPOS (*it),
3659 it->region_beg_charpos,
3660 it->region_end_charpos,
3661 &next_stop,
3662 (IT_CHARPOS (*it)
3663 + TEXT_PROP_DISTANCE_LIMIT),
3665 from_overlay);
3667 else
3669 bufpos = 0;
3671 /* For strings from a `display' property, use the face at
3672 IT's current buffer position as the base face to merge
3673 with, so that overlay strings appear in the same face as
3674 surrounding text, unless they specify their own
3675 faces. */
3676 base_face_id = underlying_face_id (it);
3679 new_face_id = face_at_string_position (it->w,
3680 it->string,
3681 IT_STRING_CHARPOS (*it),
3682 bufpos,
3683 it->region_beg_charpos,
3684 it->region_end_charpos,
3685 &next_stop,
3686 base_face_id, 0);
3688 /* Is this a start of a run of characters with box? Caveat:
3689 this can be called for a freshly allocated iterator; face_id
3690 is -1 is this case. We know that the new face will not
3691 change until the next check pos, i.e. if the new face has a
3692 box, all characters up to that position will have a
3693 box. But, as usual, we don't know whether that position
3694 is really the end. */
3695 if (new_face_id != it->face_id)
3697 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3698 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3700 /* If new face has a box but old face hasn't, this is the
3701 start of a run of characters with box, i.e. it has a
3702 shadow on the left side. */
3703 it->start_of_box_run_p
3704 = new_face->box && (old_face == NULL || !old_face->box);
3705 it->face_box_p = new_face->box != FACE_NO_BOX;
3709 it->face_id = new_face_id;
3710 return HANDLED_NORMALLY;
3714 /* Return the ID of the face ``underlying'' IT's current position,
3715 which is in a string. If the iterator is associated with a
3716 buffer, return the face at IT's current buffer position.
3717 Otherwise, use the iterator's base_face_id. */
3719 static int
3720 underlying_face_id (struct it *it)
3722 int face_id = it->base_face_id, i;
3724 xassert (STRINGP (it->string));
3726 for (i = it->sp - 1; i >= 0; --i)
3727 if (NILP (it->stack[i].string))
3728 face_id = it->stack[i].face_id;
3730 return face_id;
3734 /* Compute the face one character before or after the current position
3735 of IT, in the visual order. BEFORE_P non-zero means get the face
3736 in front (to the left in L2R paragraphs, to the right in R2L
3737 paragraphs) of IT's screen position. Value is the ID of the face. */
3739 static int
3740 face_before_or_after_it_pos (struct it *it, int before_p)
3742 int face_id, limit;
3743 EMACS_INT next_check_charpos;
3744 struct it it_copy;
3745 void *it_copy_data = NULL;
3747 xassert (it->s == NULL);
3749 if (STRINGP (it->string))
3751 EMACS_INT bufpos, charpos;
3752 int base_face_id;
3754 /* No face change past the end of the string (for the case
3755 we are padding with spaces). No face change before the
3756 string start. */
3757 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3758 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3759 return it->face_id;
3761 if (!it->bidi_p)
3763 /* Set charpos to the position before or after IT's current
3764 position, in the logical order, which in the non-bidi
3765 case is the same as the visual order. */
3766 if (before_p)
3767 charpos = IT_STRING_CHARPOS (*it) - 1;
3768 else if (it->what == IT_COMPOSITION)
3769 /* For composition, we must check the character after the
3770 composition. */
3771 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3772 else
3773 charpos = IT_STRING_CHARPOS (*it) + 1;
3775 else
3777 if (before_p)
3779 /* With bidi iteration, the character before the current
3780 in the visual order cannot be found by simple
3781 iteration, because "reverse" reordering is not
3782 supported. Instead, we need to use the move_it_*
3783 family of functions. */
3784 /* Ignore face changes before the first visible
3785 character on this display line. */
3786 if (it->current_x <= it->first_visible_x)
3787 return it->face_id;
3788 SAVE_IT (it_copy, *it, it_copy_data);
3789 /* Implementation note: Since move_it_in_display_line
3790 works in the iterator geometry, and thinks the first
3791 character is always the leftmost, even in R2L lines,
3792 we don't need to distinguish between the R2L and L2R
3793 cases here. */
3794 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3795 it_copy.current_x - 1, MOVE_TO_X);
3796 charpos = IT_STRING_CHARPOS (it_copy);
3797 RESTORE_IT (it, it, it_copy_data);
3799 else
3801 /* Set charpos to the string position of the character
3802 that comes after IT's current position in the visual
3803 order. */
3804 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3806 it_copy = *it;
3807 while (n--)
3808 bidi_move_to_visually_next (&it_copy.bidi_it);
3810 charpos = it_copy.bidi_it.charpos;
3813 xassert (0 <= charpos && charpos <= SCHARS (it->string));
3815 if (it->current.overlay_string_index >= 0)
3816 bufpos = IT_CHARPOS (*it);
3817 else
3818 bufpos = 0;
3820 base_face_id = underlying_face_id (it);
3822 /* Get the face for ASCII, or unibyte. */
3823 face_id = face_at_string_position (it->w,
3824 it->string,
3825 charpos,
3826 bufpos,
3827 it->region_beg_charpos,
3828 it->region_end_charpos,
3829 &next_check_charpos,
3830 base_face_id, 0);
3832 /* Correct the face for charsets different from ASCII. Do it
3833 for the multibyte case only. The face returned above is
3834 suitable for unibyte text if IT->string is unibyte. */
3835 if (STRING_MULTIBYTE (it->string))
3837 struct text_pos pos1 = string_pos (charpos, it->string);
3838 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3839 int c, len;
3840 struct face *face = FACE_FROM_ID (it->f, face_id);
3842 c = string_char_and_length (p, &len);
3843 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3846 else
3848 struct text_pos pos;
3850 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3851 || (IT_CHARPOS (*it) <= BEGV && before_p))
3852 return it->face_id;
3854 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3855 pos = it->current.pos;
3857 if (!it->bidi_p)
3859 if (before_p)
3860 DEC_TEXT_POS (pos, it->multibyte_p);
3861 else
3863 if (it->what == IT_COMPOSITION)
3865 /* For composition, we must check the position after
3866 the composition. */
3867 pos.charpos += it->cmp_it.nchars;
3868 pos.bytepos += it->len;
3870 else
3871 INC_TEXT_POS (pos, it->multibyte_p);
3874 else
3876 if (before_p)
3878 /* With bidi iteration, the character before the current
3879 in the visual order cannot be found by simple
3880 iteration, because "reverse" reordering is not
3881 supported. Instead, we need to use the move_it_*
3882 family of functions. */
3883 /* Ignore face changes before the first visible
3884 character on this display line. */
3885 if (it->current_x <= it->first_visible_x)
3886 return it->face_id;
3887 SAVE_IT (it_copy, *it, it_copy_data);
3888 /* Implementation note: Since move_it_in_display_line
3889 works in the iterator geometry, and thinks the first
3890 character is always the leftmost, even in R2L lines,
3891 we don't need to distinguish between the R2L and L2R
3892 cases here. */
3893 move_it_in_display_line (&it_copy, ZV,
3894 it_copy.current_x - 1, MOVE_TO_X);
3895 pos = it_copy.current.pos;
3896 RESTORE_IT (it, it, it_copy_data);
3898 else
3900 /* Set charpos to the buffer position of the character
3901 that comes after IT's current position in the visual
3902 order. */
3903 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3905 it_copy = *it;
3906 while (n--)
3907 bidi_move_to_visually_next (&it_copy.bidi_it);
3909 SET_TEXT_POS (pos,
3910 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3913 xassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
3915 /* Determine face for CHARSET_ASCII, or unibyte. */
3916 face_id = face_at_buffer_position (it->w,
3917 CHARPOS (pos),
3918 it->region_beg_charpos,
3919 it->region_end_charpos,
3920 &next_check_charpos,
3921 limit, 0, -1);
3923 /* Correct the face for charsets different from ASCII. Do it
3924 for the multibyte case only. The face returned above is
3925 suitable for unibyte text if current_buffer is unibyte. */
3926 if (it->multibyte_p)
3928 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3929 struct face *face = FACE_FROM_ID (it->f, face_id);
3930 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3934 return face_id;
3939 /***********************************************************************
3940 Invisible text
3941 ***********************************************************************/
3943 /* Set up iterator IT from invisible properties at its current
3944 position. Called from handle_stop. */
3946 static enum prop_handled
3947 handle_invisible_prop (struct it *it)
3949 enum prop_handled handled = HANDLED_NORMALLY;
3951 if (STRINGP (it->string))
3953 Lisp_Object prop, end_charpos, limit, charpos;
3955 /* Get the value of the invisible text property at the
3956 current position. Value will be nil if there is no such
3957 property. */
3958 charpos = make_number (IT_STRING_CHARPOS (*it));
3959 prop = Fget_text_property (charpos, Qinvisible, it->string);
3961 if (!NILP (prop)
3962 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3964 EMACS_INT endpos;
3966 handled = HANDLED_RECOMPUTE_PROPS;
3968 /* Get the position at which the next change of the
3969 invisible text property can be found in IT->string.
3970 Value will be nil if the property value is the same for
3971 all the rest of IT->string. */
3972 XSETINT (limit, SCHARS (it->string));
3973 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3974 it->string, limit);
3976 /* Text at current position is invisible. The next
3977 change in the property is at position end_charpos.
3978 Move IT's current position to that position. */
3979 if (INTEGERP (end_charpos)
3980 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
3982 struct text_pos old;
3983 EMACS_INT oldpos;
3985 old = it->current.string_pos;
3986 oldpos = CHARPOS (old);
3987 if (it->bidi_p)
3989 if (it->bidi_it.first_elt
3990 && it->bidi_it.charpos < SCHARS (it->string))
3991 bidi_paragraph_init (it->paragraph_embedding,
3992 &it->bidi_it, 1);
3993 /* Bidi-iterate out of the invisible text. */
3996 bidi_move_to_visually_next (&it->bidi_it);
3998 while (oldpos <= it->bidi_it.charpos
3999 && it->bidi_it.charpos < endpos);
4001 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4002 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4003 if (IT_CHARPOS (*it) >= endpos)
4004 it->prev_stop = endpos;
4006 else
4008 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4009 compute_string_pos (&it->current.string_pos, old, it->string);
4012 else
4014 /* The rest of the string is invisible. If this is an
4015 overlay string, proceed with the next overlay string
4016 or whatever comes and return a character from there. */
4017 if (it->current.overlay_string_index >= 0)
4019 next_overlay_string (it);
4020 /* Don't check for overlay strings when we just
4021 finished processing them. */
4022 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4024 else
4026 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4027 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4032 else
4034 int invis_p;
4035 EMACS_INT newpos, next_stop, start_charpos, tem;
4036 Lisp_Object pos, prop, overlay;
4038 /* First of all, is there invisible text at this position? */
4039 tem = start_charpos = IT_CHARPOS (*it);
4040 pos = make_number (tem);
4041 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4042 &overlay);
4043 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4045 /* If we are on invisible text, skip over it. */
4046 if (invis_p && start_charpos < it->end_charpos)
4048 /* Record whether we have to display an ellipsis for the
4049 invisible text. */
4050 int display_ellipsis_p = invis_p == 2;
4052 handled = HANDLED_RECOMPUTE_PROPS;
4054 /* Loop skipping over invisible text. The loop is left at
4055 ZV or with IT on the first char being visible again. */
4058 /* Try to skip some invisible text. Return value is the
4059 position reached which can be equal to where we start
4060 if there is nothing invisible there. This skips both
4061 over invisible text properties and overlays with
4062 invisible property. */
4063 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4065 /* If we skipped nothing at all we weren't at invisible
4066 text in the first place. If everything to the end of
4067 the buffer was skipped, end the loop. */
4068 if (newpos == tem || newpos >= ZV)
4069 invis_p = 0;
4070 else
4072 /* We skipped some characters but not necessarily
4073 all there are. Check if we ended up on visible
4074 text. Fget_char_property returns the property of
4075 the char before the given position, i.e. if we
4076 get invis_p = 0, this means that the char at
4077 newpos is visible. */
4078 pos = make_number (newpos);
4079 prop = Fget_char_property (pos, Qinvisible, it->window);
4080 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4083 /* If we ended up on invisible text, proceed to
4084 skip starting with next_stop. */
4085 if (invis_p)
4086 tem = next_stop;
4088 /* If there are adjacent invisible texts, don't lose the
4089 second one's ellipsis. */
4090 if (invis_p == 2)
4091 display_ellipsis_p = 1;
4093 while (invis_p);
4095 /* The position newpos is now either ZV or on visible text. */
4096 if (it->bidi_p)
4098 EMACS_INT bpos = CHAR_TO_BYTE (newpos);
4099 int on_newline =
4100 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4101 int after_newline =
4102 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4104 /* If the invisible text ends on a newline or on a
4105 character after a newline, we can avoid the costly,
4106 character by character, bidi iteration to NEWPOS, and
4107 instead simply reseat the iterator there. That's
4108 because all bidi reordering information is tossed at
4109 the newline. This is a big win for modes that hide
4110 complete lines, like Outline, Org, etc. */
4111 if (on_newline || after_newline)
4113 struct text_pos tpos;
4114 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4116 SET_TEXT_POS (tpos, newpos, bpos);
4117 reseat_1 (it, tpos, 0);
4118 /* If we reseat on a newline/ZV, we need to prep the
4119 bidi iterator for advancing to the next character
4120 after the newline/EOB, keeping the current paragraph
4121 direction (so that PRODUCE_GLYPHS does TRT wrt
4122 prepending/appending glyphs to a glyph row). */
4123 if (on_newline)
4125 it->bidi_it.first_elt = 0;
4126 it->bidi_it.paragraph_dir = pdir;
4127 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4128 it->bidi_it.nchars = 1;
4129 it->bidi_it.ch_len = 1;
4132 else /* Must use the slow method. */
4134 /* With bidi iteration, the region of invisible text
4135 could start and/or end in the middle of a
4136 non-base embedding level. Therefore, we need to
4137 skip invisible text using the bidi iterator,
4138 starting at IT's current position, until we find
4139 ourselves outside of the invisible text.
4140 Skipping invisible text _after_ bidi iteration
4141 avoids affecting the visual order of the
4142 displayed text when invisible properties are
4143 added or removed. */
4144 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4146 /* If we were `reseat'ed to a new paragraph,
4147 determine the paragraph base direction. We
4148 need to do it now because
4149 next_element_from_buffer may not have a
4150 chance to do it, if we are going to skip any
4151 text at the beginning, which resets the
4152 FIRST_ELT flag. */
4153 bidi_paragraph_init (it->paragraph_embedding,
4154 &it->bidi_it, 1);
4158 bidi_move_to_visually_next (&it->bidi_it);
4160 while (it->stop_charpos <= it->bidi_it.charpos
4161 && it->bidi_it.charpos < newpos);
4162 IT_CHARPOS (*it) = it->bidi_it.charpos;
4163 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4164 /* If we overstepped NEWPOS, record its position in
4165 the iterator, so that we skip invisible text if
4166 later the bidi iteration lands us in the
4167 invisible region again. */
4168 if (IT_CHARPOS (*it) >= newpos)
4169 it->prev_stop = newpos;
4172 else
4174 IT_CHARPOS (*it) = newpos;
4175 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4178 /* If there are before-strings at the start of invisible
4179 text, and the text is invisible because of a text
4180 property, arrange to show before-strings because 20.x did
4181 it that way. (If the text is invisible because of an
4182 overlay property instead of a text property, this is
4183 already handled in the overlay code.) */
4184 if (NILP (overlay)
4185 && get_overlay_strings (it, it->stop_charpos))
4187 handled = HANDLED_RECOMPUTE_PROPS;
4188 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4190 else if (display_ellipsis_p)
4192 /* Make sure that the glyphs of the ellipsis will get
4193 correct `charpos' values. If we would not update
4194 it->position here, the glyphs would belong to the
4195 last visible character _before_ the invisible
4196 text, which confuses `set_cursor_from_row'.
4198 We use the last invisible position instead of the
4199 first because this way the cursor is always drawn on
4200 the first "." of the ellipsis, whenever PT is inside
4201 the invisible text. Otherwise the cursor would be
4202 placed _after_ the ellipsis when the point is after the
4203 first invisible character. */
4204 if (!STRINGP (it->object))
4206 it->position.charpos = newpos - 1;
4207 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4209 it->ellipsis_p = 1;
4210 /* Let the ellipsis display before
4211 considering any properties of the following char.
4212 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4213 handled = HANDLED_RETURN;
4218 return handled;
4222 /* Make iterator IT return `...' next.
4223 Replaces LEN characters from buffer. */
4225 static void
4226 setup_for_ellipsis (struct it *it, int len)
4228 /* Use the display table definition for `...'. Invalid glyphs
4229 will be handled by the method returning elements from dpvec. */
4230 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4232 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4233 it->dpvec = v->contents;
4234 it->dpend = v->contents + v->header.size;
4236 else
4238 /* Default `...'. */
4239 it->dpvec = default_invis_vector;
4240 it->dpend = default_invis_vector + 3;
4243 it->dpvec_char_len = len;
4244 it->current.dpvec_index = 0;
4245 it->dpvec_face_id = -1;
4247 /* Remember the current face id in case glyphs specify faces.
4248 IT's face is restored in set_iterator_to_next.
4249 saved_face_id was set to preceding char's face in handle_stop. */
4250 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4251 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4253 it->method = GET_FROM_DISPLAY_VECTOR;
4254 it->ellipsis_p = 1;
4259 /***********************************************************************
4260 'display' property
4261 ***********************************************************************/
4263 /* Set up iterator IT from `display' property at its current position.
4264 Called from handle_stop.
4265 We return HANDLED_RETURN if some part of the display property
4266 overrides the display of the buffer text itself.
4267 Otherwise we return HANDLED_NORMALLY. */
4269 static enum prop_handled
4270 handle_display_prop (struct it *it)
4272 Lisp_Object propval, object, overlay;
4273 struct text_pos *position;
4274 EMACS_INT bufpos;
4275 /* Nonzero if some property replaces the display of the text itself. */
4276 int display_replaced_p = 0;
4278 if (STRINGP (it->string))
4280 object = it->string;
4281 position = &it->current.string_pos;
4282 bufpos = CHARPOS (it->current.pos);
4284 else
4286 XSETWINDOW (object, it->w);
4287 position = &it->current.pos;
4288 bufpos = CHARPOS (*position);
4291 /* Reset those iterator values set from display property values. */
4292 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4293 it->space_width = Qnil;
4294 it->font_height = Qnil;
4295 it->voffset = 0;
4297 /* We don't support recursive `display' properties, i.e. string
4298 values that have a string `display' property, that have a string
4299 `display' property etc. */
4300 if (!it->string_from_display_prop_p)
4301 it->area = TEXT_AREA;
4303 propval = get_char_property_and_overlay (make_number (position->charpos),
4304 Qdisplay, object, &overlay);
4305 if (NILP (propval))
4306 return HANDLED_NORMALLY;
4307 /* Now OVERLAY is the overlay that gave us this property, or nil
4308 if it was a text property. */
4310 if (!STRINGP (it->string))
4311 object = it->w->buffer;
4313 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4314 position, bufpos,
4315 FRAME_WINDOW_P (it->f));
4317 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4320 /* Subroutine of handle_display_prop. Returns non-zero if the display
4321 specification in SPEC is a replacing specification, i.e. it would
4322 replace the text covered by `display' property with something else,
4323 such as an image or a display string. If SPEC includes any kind or
4324 `(space ...) specification, the value is 2; this is used by
4325 compute_display_string_pos, which see.
4327 See handle_single_display_spec for documentation of arguments.
4328 frame_window_p is non-zero if the window being redisplayed is on a
4329 GUI frame; this argument is used only if IT is NULL, see below.
4331 IT can be NULL, if this is called by the bidi reordering code
4332 through compute_display_string_pos, which see. In that case, this
4333 function only examines SPEC, but does not otherwise "handle" it, in
4334 the sense that it doesn't set up members of IT from the display
4335 spec. */
4336 static int
4337 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4338 Lisp_Object overlay, struct text_pos *position,
4339 EMACS_INT bufpos, int frame_window_p)
4341 int replacing_p = 0;
4342 int rv;
4344 if (CONSP (spec)
4345 /* Simple specifications. */
4346 && !EQ (XCAR (spec), Qimage)
4347 && !EQ (XCAR (spec), Qspace)
4348 && !EQ (XCAR (spec), Qwhen)
4349 && !EQ (XCAR (spec), Qslice)
4350 && !EQ (XCAR (spec), Qspace_width)
4351 && !EQ (XCAR (spec), Qheight)
4352 && !EQ (XCAR (spec), Qraise)
4353 /* Marginal area specifications. */
4354 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4355 && !EQ (XCAR (spec), Qleft_fringe)
4356 && !EQ (XCAR (spec), Qright_fringe)
4357 && !NILP (XCAR (spec)))
4359 for (; CONSP (spec); spec = XCDR (spec))
4361 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4362 overlay, position, bufpos,
4363 replacing_p, frame_window_p)))
4365 replacing_p = rv;
4366 /* If some text in a string is replaced, `position' no
4367 longer points to the position of `object'. */
4368 if (!it || STRINGP (object))
4369 break;
4373 else if (VECTORP (spec))
4375 int i;
4376 for (i = 0; i < ASIZE (spec); ++i)
4377 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4378 overlay, position, bufpos,
4379 replacing_p, frame_window_p)))
4381 replacing_p = rv;
4382 /* If some text in a string is replaced, `position' no
4383 longer points to the position of `object'. */
4384 if (!it || STRINGP (object))
4385 break;
4388 else
4390 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4391 position, bufpos, 0,
4392 frame_window_p)))
4393 replacing_p = rv;
4396 return replacing_p;
4399 /* Value is the position of the end of the `display' property starting
4400 at START_POS in OBJECT. */
4402 static struct text_pos
4403 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4405 Lisp_Object end;
4406 struct text_pos end_pos;
4408 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4409 Qdisplay, object, Qnil);
4410 CHARPOS (end_pos) = XFASTINT (end);
4411 if (STRINGP (object))
4412 compute_string_pos (&end_pos, start_pos, it->string);
4413 else
4414 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4416 return end_pos;
4420 /* Set up IT from a single `display' property specification SPEC. OBJECT
4421 is the object in which the `display' property was found. *POSITION
4422 is the position in OBJECT at which the `display' property was found.
4423 BUFPOS is the buffer position of OBJECT (different from POSITION if
4424 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4425 previously saw a display specification which already replaced text
4426 display with something else, for example an image; we ignore such
4427 properties after the first one has been processed.
4429 OVERLAY is the overlay this `display' property came from,
4430 or nil if it was a text property.
4432 If SPEC is a `space' or `image' specification, and in some other
4433 cases too, set *POSITION to the position where the `display'
4434 property ends.
4436 If IT is NULL, only examine the property specification in SPEC, but
4437 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4438 is intended to be displayed in a window on a GUI frame.
4440 Value is non-zero if something was found which replaces the display
4441 of buffer or string text. */
4443 static int
4444 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4445 Lisp_Object overlay, struct text_pos *position,
4446 EMACS_INT bufpos, int display_replaced_p,
4447 int frame_window_p)
4449 Lisp_Object form;
4450 Lisp_Object location, value;
4451 struct text_pos start_pos = *position;
4452 int valid_p;
4454 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4455 If the result is non-nil, use VALUE instead of SPEC. */
4456 form = Qt;
4457 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4459 spec = XCDR (spec);
4460 if (!CONSP (spec))
4461 return 0;
4462 form = XCAR (spec);
4463 spec = XCDR (spec);
4466 if (!NILP (form) && !EQ (form, Qt))
4468 int count = SPECPDL_INDEX ();
4469 struct gcpro gcpro1;
4471 /* Bind `object' to the object having the `display' property, a
4472 buffer or string. Bind `position' to the position in the
4473 object where the property was found, and `buffer-position'
4474 to the current position in the buffer. */
4476 if (NILP (object))
4477 XSETBUFFER (object, current_buffer);
4478 specbind (Qobject, object);
4479 specbind (Qposition, make_number (CHARPOS (*position)));
4480 specbind (Qbuffer_position, make_number (bufpos));
4481 GCPRO1 (form);
4482 form = safe_eval (form);
4483 UNGCPRO;
4484 unbind_to (count, Qnil);
4487 if (NILP (form))
4488 return 0;
4490 /* Handle `(height HEIGHT)' specifications. */
4491 if (CONSP (spec)
4492 && EQ (XCAR (spec), Qheight)
4493 && CONSP (XCDR (spec)))
4495 if (it)
4497 if (!FRAME_WINDOW_P (it->f))
4498 return 0;
4500 it->font_height = XCAR (XCDR (spec));
4501 if (!NILP (it->font_height))
4503 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4504 int new_height = -1;
4506 if (CONSP (it->font_height)
4507 && (EQ (XCAR (it->font_height), Qplus)
4508 || EQ (XCAR (it->font_height), Qminus))
4509 && CONSP (XCDR (it->font_height))
4510 && INTEGERP (XCAR (XCDR (it->font_height))))
4512 /* `(+ N)' or `(- N)' where N is an integer. */
4513 int steps = XINT (XCAR (XCDR (it->font_height)));
4514 if (EQ (XCAR (it->font_height), Qplus))
4515 steps = - steps;
4516 it->face_id = smaller_face (it->f, it->face_id, steps);
4518 else if (FUNCTIONP (it->font_height))
4520 /* Call function with current height as argument.
4521 Value is the new height. */
4522 Lisp_Object height;
4523 height = safe_call1 (it->font_height,
4524 face->lface[LFACE_HEIGHT_INDEX]);
4525 if (NUMBERP (height))
4526 new_height = XFLOATINT (height);
4528 else if (NUMBERP (it->font_height))
4530 /* Value is a multiple of the canonical char height. */
4531 struct face *f;
4533 f = FACE_FROM_ID (it->f,
4534 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4535 new_height = (XFLOATINT (it->font_height)
4536 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4538 else
4540 /* Evaluate IT->font_height with `height' bound to the
4541 current specified height to get the new height. */
4542 int count = SPECPDL_INDEX ();
4544 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4545 value = safe_eval (it->font_height);
4546 unbind_to (count, Qnil);
4548 if (NUMBERP (value))
4549 new_height = XFLOATINT (value);
4552 if (new_height > 0)
4553 it->face_id = face_with_height (it->f, it->face_id, new_height);
4557 return 0;
4560 /* Handle `(space-width WIDTH)'. */
4561 if (CONSP (spec)
4562 && EQ (XCAR (spec), Qspace_width)
4563 && CONSP (XCDR (spec)))
4565 if (it)
4567 if (!FRAME_WINDOW_P (it->f))
4568 return 0;
4570 value = XCAR (XCDR (spec));
4571 if (NUMBERP (value) && XFLOATINT (value) > 0)
4572 it->space_width = value;
4575 return 0;
4578 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4579 if (CONSP (spec)
4580 && EQ (XCAR (spec), Qslice))
4582 Lisp_Object tem;
4584 if (it)
4586 if (!FRAME_WINDOW_P (it->f))
4587 return 0;
4589 if (tem = XCDR (spec), CONSP (tem))
4591 it->slice.x = XCAR (tem);
4592 if (tem = XCDR (tem), CONSP (tem))
4594 it->slice.y = XCAR (tem);
4595 if (tem = XCDR (tem), CONSP (tem))
4597 it->slice.width = XCAR (tem);
4598 if (tem = XCDR (tem), CONSP (tem))
4599 it->slice.height = XCAR (tem);
4605 return 0;
4608 /* Handle `(raise FACTOR)'. */
4609 if (CONSP (spec)
4610 && EQ (XCAR (spec), Qraise)
4611 && CONSP (XCDR (spec)))
4613 if (it)
4615 if (!FRAME_WINDOW_P (it->f))
4616 return 0;
4618 #ifdef HAVE_WINDOW_SYSTEM
4619 value = XCAR (XCDR (spec));
4620 if (NUMBERP (value))
4622 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4623 it->voffset = - (XFLOATINT (value)
4624 * (FONT_HEIGHT (face->font)));
4626 #endif /* HAVE_WINDOW_SYSTEM */
4629 return 0;
4632 /* Don't handle the other kinds of display specifications
4633 inside a string that we got from a `display' property. */
4634 if (it && it->string_from_display_prop_p)
4635 return 0;
4637 /* Characters having this form of property are not displayed, so
4638 we have to find the end of the property. */
4639 if (it)
4641 start_pos = *position;
4642 *position = display_prop_end (it, object, start_pos);
4644 value = Qnil;
4646 /* Stop the scan at that end position--we assume that all
4647 text properties change there. */
4648 if (it)
4649 it->stop_charpos = position->charpos;
4651 /* Handle `(left-fringe BITMAP [FACE])'
4652 and `(right-fringe BITMAP [FACE])'. */
4653 if (CONSP (spec)
4654 && (EQ (XCAR (spec), Qleft_fringe)
4655 || EQ (XCAR (spec), Qright_fringe))
4656 && CONSP (XCDR (spec)))
4658 int fringe_bitmap;
4660 if (it)
4662 if (!FRAME_WINDOW_P (it->f))
4663 /* If we return here, POSITION has been advanced
4664 across the text with this property. */
4665 return 0;
4667 else if (!frame_window_p)
4668 return 0;
4670 #ifdef HAVE_WINDOW_SYSTEM
4671 value = XCAR (XCDR (spec));
4672 if (!SYMBOLP (value)
4673 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4674 /* If we return here, POSITION has been advanced
4675 across the text with this property. */
4676 return 0;
4678 if (it)
4680 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4682 if (CONSP (XCDR (XCDR (spec))))
4684 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4685 int face_id2 = lookup_derived_face (it->f, face_name,
4686 FRINGE_FACE_ID, 0);
4687 if (face_id2 >= 0)
4688 face_id = face_id2;
4691 /* Save current settings of IT so that we can restore them
4692 when we are finished with the glyph property value. */
4693 push_it (it, position);
4695 it->area = TEXT_AREA;
4696 it->what = IT_IMAGE;
4697 it->image_id = -1; /* no image */
4698 it->position = start_pos;
4699 it->object = NILP (object) ? it->w->buffer : object;
4700 it->method = GET_FROM_IMAGE;
4701 it->from_overlay = Qnil;
4702 it->face_id = face_id;
4703 it->from_disp_prop_p = 1;
4705 /* Say that we haven't consumed the characters with
4706 `display' property yet. The call to pop_it in
4707 set_iterator_to_next will clean this up. */
4708 *position = start_pos;
4710 if (EQ (XCAR (spec), Qleft_fringe))
4712 it->left_user_fringe_bitmap = fringe_bitmap;
4713 it->left_user_fringe_face_id = face_id;
4715 else
4717 it->right_user_fringe_bitmap = fringe_bitmap;
4718 it->right_user_fringe_face_id = face_id;
4721 #endif /* HAVE_WINDOW_SYSTEM */
4722 return 1;
4725 /* Prepare to handle `((margin left-margin) ...)',
4726 `((margin right-margin) ...)' and `((margin nil) ...)'
4727 prefixes for display specifications. */
4728 location = Qunbound;
4729 if (CONSP (spec) && CONSP (XCAR (spec)))
4731 Lisp_Object tem;
4733 value = XCDR (spec);
4734 if (CONSP (value))
4735 value = XCAR (value);
4737 tem = XCAR (spec);
4738 if (EQ (XCAR (tem), Qmargin)
4739 && (tem = XCDR (tem),
4740 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4741 (NILP (tem)
4742 || EQ (tem, Qleft_margin)
4743 || EQ (tem, Qright_margin))))
4744 location = tem;
4747 if (EQ (location, Qunbound))
4749 location = Qnil;
4750 value = spec;
4753 /* After this point, VALUE is the property after any
4754 margin prefix has been stripped. It must be a string,
4755 an image specification, or `(space ...)'.
4757 LOCATION specifies where to display: `left-margin',
4758 `right-margin' or nil. */
4760 valid_p = (STRINGP (value)
4761 #ifdef HAVE_WINDOW_SYSTEM
4762 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4763 && valid_image_p (value))
4764 #endif /* not HAVE_WINDOW_SYSTEM */
4765 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4767 if (valid_p && !display_replaced_p)
4769 int retval = 1;
4771 if (!it)
4773 /* Callers need to know whether the display spec is any kind
4774 of `(space ...)' spec that is about to affect text-area
4775 display. */
4776 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4777 retval = 2;
4778 return retval;
4781 /* Save current settings of IT so that we can restore them
4782 when we are finished with the glyph property value. */
4783 push_it (it, position);
4784 it->from_overlay = overlay;
4785 it->from_disp_prop_p = 1;
4787 if (NILP (location))
4788 it->area = TEXT_AREA;
4789 else if (EQ (location, Qleft_margin))
4790 it->area = LEFT_MARGIN_AREA;
4791 else
4792 it->area = RIGHT_MARGIN_AREA;
4794 if (STRINGP (value))
4796 it->string = value;
4797 it->multibyte_p = STRING_MULTIBYTE (it->string);
4798 it->current.overlay_string_index = -1;
4799 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4800 it->end_charpos = it->string_nchars = SCHARS (it->string);
4801 it->method = GET_FROM_STRING;
4802 it->stop_charpos = 0;
4803 it->prev_stop = 0;
4804 it->base_level_stop = 0;
4805 it->string_from_display_prop_p = 1;
4806 /* Say that we haven't consumed the characters with
4807 `display' property yet. The call to pop_it in
4808 set_iterator_to_next will clean this up. */
4809 if (BUFFERP (object))
4810 *position = start_pos;
4812 /* Force paragraph direction to be that of the parent
4813 object. If the parent object's paragraph direction is
4814 not yet determined, default to L2R. */
4815 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4816 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4817 else
4818 it->paragraph_embedding = L2R;
4820 /* Set up the bidi iterator for this display string. */
4821 if (it->bidi_p)
4823 it->bidi_it.string.lstring = it->string;
4824 it->bidi_it.string.s = NULL;
4825 it->bidi_it.string.schars = it->end_charpos;
4826 it->bidi_it.string.bufpos = bufpos;
4827 it->bidi_it.string.from_disp_str = 1;
4828 it->bidi_it.string.unibyte = !it->multibyte_p;
4829 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4832 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4834 it->method = GET_FROM_STRETCH;
4835 it->object = value;
4836 *position = it->position = start_pos;
4837 retval = 1 + (it->area == TEXT_AREA);
4839 #ifdef HAVE_WINDOW_SYSTEM
4840 else
4842 it->what = IT_IMAGE;
4843 it->image_id = lookup_image (it->f, value);
4844 it->position = start_pos;
4845 it->object = NILP (object) ? it->w->buffer : object;
4846 it->method = GET_FROM_IMAGE;
4848 /* Say that we haven't consumed the characters with
4849 `display' property yet. The call to pop_it in
4850 set_iterator_to_next will clean this up. */
4851 *position = start_pos;
4853 #endif /* HAVE_WINDOW_SYSTEM */
4855 return retval;
4858 /* Invalid property or property not supported. Restore
4859 POSITION to what it was before. */
4860 *position = start_pos;
4861 return 0;
4864 /* Check if PROP is a display property value whose text should be
4865 treated as intangible. OVERLAY is the overlay from which PROP
4866 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4867 specify the buffer position covered by PROP. */
4870 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4871 EMACS_INT charpos, EMACS_INT bytepos)
4873 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4874 struct text_pos position;
4876 SET_TEXT_POS (position, charpos, bytepos);
4877 return handle_display_spec (NULL, prop, Qnil, overlay,
4878 &position, charpos, frame_window_p);
4882 /* Return 1 if PROP is a display sub-property value containing STRING.
4884 Implementation note: this and the following function are really
4885 special cases of handle_display_spec and
4886 handle_single_display_spec, and should ideally use the same code.
4887 Until they do, these two pairs must be consistent and must be
4888 modified in sync. */
4890 static int
4891 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4893 if (EQ (string, prop))
4894 return 1;
4896 /* Skip over `when FORM'. */
4897 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4899 prop = XCDR (prop);
4900 if (!CONSP (prop))
4901 return 0;
4902 /* Actually, the condition following `when' should be eval'ed,
4903 like handle_single_display_spec does, and we should return
4904 zero if it evaluates to nil. However, this function is
4905 called only when the buffer was already displayed and some
4906 glyph in the glyph matrix was found to come from a display
4907 string. Therefore, the condition was already evaluated, and
4908 the result was non-nil, otherwise the display string wouldn't
4909 have been displayed and we would have never been called for
4910 this property. Thus, we can skip the evaluation and assume
4911 its result is non-nil. */
4912 prop = XCDR (prop);
4915 if (CONSP (prop))
4916 /* Skip over `margin LOCATION'. */
4917 if (EQ (XCAR (prop), Qmargin))
4919 prop = XCDR (prop);
4920 if (!CONSP (prop))
4921 return 0;
4923 prop = XCDR (prop);
4924 if (!CONSP (prop))
4925 return 0;
4928 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
4932 /* Return 1 if STRING appears in the `display' property PROP. */
4934 static int
4935 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4937 if (CONSP (prop)
4938 && !EQ (XCAR (prop), Qwhen)
4939 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
4941 /* A list of sub-properties. */
4942 while (CONSP (prop))
4944 if (single_display_spec_string_p (XCAR (prop), string))
4945 return 1;
4946 prop = XCDR (prop);
4949 else if (VECTORP (prop))
4951 /* A vector of sub-properties. */
4952 int i;
4953 for (i = 0; i < ASIZE (prop); ++i)
4954 if (single_display_spec_string_p (AREF (prop, i), string))
4955 return 1;
4957 else
4958 return single_display_spec_string_p (prop, string);
4960 return 0;
4963 /* Look for STRING in overlays and text properties in the current
4964 buffer, between character positions FROM and TO (excluding TO).
4965 BACK_P non-zero means look back (in this case, TO is supposed to be
4966 less than FROM).
4967 Value is the first character position where STRING was found, or
4968 zero if it wasn't found before hitting TO.
4970 This function may only use code that doesn't eval because it is
4971 called asynchronously from note_mouse_highlight. */
4973 static EMACS_INT
4974 string_buffer_position_lim (Lisp_Object string,
4975 EMACS_INT from, EMACS_INT to, int back_p)
4977 Lisp_Object limit, prop, pos;
4978 int found = 0;
4980 pos = make_number (from);
4982 if (!back_p) /* looking forward */
4984 limit = make_number (min (to, ZV));
4985 while (!found && !EQ (pos, limit))
4987 prop = Fget_char_property (pos, Qdisplay, Qnil);
4988 if (!NILP (prop) && display_prop_string_p (prop, string))
4989 found = 1;
4990 else
4991 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4992 limit);
4995 else /* looking back */
4997 limit = make_number (max (to, BEGV));
4998 while (!found && !EQ (pos, limit))
5000 prop = Fget_char_property (pos, Qdisplay, Qnil);
5001 if (!NILP (prop) && display_prop_string_p (prop, string))
5002 found = 1;
5003 else
5004 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5005 limit);
5009 return found ? XINT (pos) : 0;
5012 /* Determine which buffer position in current buffer STRING comes from.
5013 AROUND_CHARPOS is an approximate position where it could come from.
5014 Value is the buffer position or 0 if it couldn't be determined.
5016 This function is necessary because we don't record buffer positions
5017 in glyphs generated from strings (to keep struct glyph small).
5018 This function may only use code that doesn't eval because it is
5019 called asynchronously from note_mouse_highlight. */
5021 static EMACS_INT
5022 string_buffer_position (Lisp_Object string, EMACS_INT around_charpos)
5024 const int MAX_DISTANCE = 1000;
5025 EMACS_INT found = string_buffer_position_lim (string, around_charpos,
5026 around_charpos + MAX_DISTANCE,
5029 if (!found)
5030 found = string_buffer_position_lim (string, around_charpos,
5031 around_charpos - MAX_DISTANCE, 1);
5032 return found;
5037 /***********************************************************************
5038 `composition' property
5039 ***********************************************************************/
5041 /* Set up iterator IT from `composition' property at its current
5042 position. Called from handle_stop. */
5044 static enum prop_handled
5045 handle_composition_prop (struct it *it)
5047 Lisp_Object prop, string;
5048 EMACS_INT pos, pos_byte, start, end;
5050 if (STRINGP (it->string))
5052 unsigned char *s;
5054 pos = IT_STRING_CHARPOS (*it);
5055 pos_byte = IT_STRING_BYTEPOS (*it);
5056 string = it->string;
5057 s = SDATA (string) + pos_byte;
5058 it->c = STRING_CHAR (s);
5060 else
5062 pos = IT_CHARPOS (*it);
5063 pos_byte = IT_BYTEPOS (*it);
5064 string = Qnil;
5065 it->c = FETCH_CHAR (pos_byte);
5068 /* If there's a valid composition and point is not inside of the
5069 composition (in the case that the composition is from the current
5070 buffer), draw a glyph composed from the composition components. */
5071 if (find_composition (pos, -1, &start, &end, &prop, string)
5072 && COMPOSITION_VALID_P (start, end, prop)
5073 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5075 if (start < pos)
5076 /* As we can't handle this situation (perhaps font-lock added
5077 a new composition), we just return here hoping that next
5078 redisplay will detect this composition much earlier. */
5079 return HANDLED_NORMALLY;
5080 if (start != pos)
5082 if (STRINGP (it->string))
5083 pos_byte = string_char_to_byte (it->string, start);
5084 else
5085 pos_byte = CHAR_TO_BYTE (start);
5087 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5088 prop, string);
5090 if (it->cmp_it.id >= 0)
5092 it->cmp_it.ch = -1;
5093 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5094 it->cmp_it.nglyphs = -1;
5098 return HANDLED_NORMALLY;
5103 /***********************************************************************
5104 Overlay strings
5105 ***********************************************************************/
5107 /* The following structure is used to record overlay strings for
5108 later sorting in load_overlay_strings. */
5110 struct overlay_entry
5112 Lisp_Object overlay;
5113 Lisp_Object string;
5114 int priority;
5115 int after_string_p;
5119 /* Set up iterator IT from overlay strings at its current position.
5120 Called from handle_stop. */
5122 static enum prop_handled
5123 handle_overlay_change (struct it *it)
5125 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5126 return HANDLED_RECOMPUTE_PROPS;
5127 else
5128 return HANDLED_NORMALLY;
5132 /* Set up the next overlay string for delivery by IT, if there is an
5133 overlay string to deliver. Called by set_iterator_to_next when the
5134 end of the current overlay string is reached. If there are more
5135 overlay strings to display, IT->string and
5136 IT->current.overlay_string_index are set appropriately here.
5137 Otherwise IT->string is set to nil. */
5139 static void
5140 next_overlay_string (struct it *it)
5142 ++it->current.overlay_string_index;
5143 if (it->current.overlay_string_index == it->n_overlay_strings)
5145 /* No more overlay strings. Restore IT's settings to what
5146 they were before overlay strings were processed, and
5147 continue to deliver from current_buffer. */
5149 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5150 pop_it (it);
5151 xassert (it->sp > 0
5152 || (NILP (it->string)
5153 && it->method == GET_FROM_BUFFER
5154 && it->stop_charpos >= BEGV
5155 && it->stop_charpos <= it->end_charpos));
5156 it->current.overlay_string_index = -1;
5157 it->n_overlay_strings = 0;
5158 it->overlay_strings_charpos = -1;
5159 /* If there's an empty display string on the stack, pop the
5160 stack, to resync the bidi iterator with IT's position. Such
5161 empty strings are pushed onto the stack in
5162 get_overlay_strings_1. */
5163 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5164 pop_it (it);
5166 /* If we're at the end of the buffer, record that we have
5167 processed the overlay strings there already, so that
5168 next_element_from_buffer doesn't try it again. */
5169 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5170 it->overlay_strings_at_end_processed_p = 1;
5172 else
5174 /* There are more overlay strings to process. If
5175 IT->current.overlay_string_index has advanced to a position
5176 where we must load IT->overlay_strings with more strings, do
5177 it. We must load at the IT->overlay_strings_charpos where
5178 IT->n_overlay_strings was originally computed; when invisible
5179 text is present, this might not be IT_CHARPOS (Bug#7016). */
5180 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5182 if (it->current.overlay_string_index && i == 0)
5183 load_overlay_strings (it, it->overlay_strings_charpos);
5185 /* Initialize IT to deliver display elements from the overlay
5186 string. */
5187 it->string = it->overlay_strings[i];
5188 it->multibyte_p = STRING_MULTIBYTE (it->string);
5189 SET_TEXT_POS (it->current.string_pos, 0, 0);
5190 it->method = GET_FROM_STRING;
5191 it->stop_charpos = 0;
5192 if (it->cmp_it.stop_pos >= 0)
5193 it->cmp_it.stop_pos = 0;
5194 it->prev_stop = 0;
5195 it->base_level_stop = 0;
5197 /* Set up the bidi iterator for this overlay string. */
5198 if (it->bidi_p)
5200 it->bidi_it.string.lstring = it->string;
5201 it->bidi_it.string.s = NULL;
5202 it->bidi_it.string.schars = SCHARS (it->string);
5203 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5204 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5205 it->bidi_it.string.unibyte = !it->multibyte_p;
5206 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5210 CHECK_IT (it);
5214 /* Compare two overlay_entry structures E1 and E2. Used as a
5215 comparison function for qsort in load_overlay_strings. Overlay
5216 strings for the same position are sorted so that
5218 1. All after-strings come in front of before-strings, except
5219 when they come from the same overlay.
5221 2. Within after-strings, strings are sorted so that overlay strings
5222 from overlays with higher priorities come first.
5224 2. Within before-strings, strings are sorted so that overlay
5225 strings from overlays with higher priorities come last.
5227 Value is analogous to strcmp. */
5230 static int
5231 compare_overlay_entries (const void *e1, const void *e2)
5233 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5234 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5235 int result;
5237 if (entry1->after_string_p != entry2->after_string_p)
5239 /* Let after-strings appear in front of before-strings if
5240 they come from different overlays. */
5241 if (EQ (entry1->overlay, entry2->overlay))
5242 result = entry1->after_string_p ? 1 : -1;
5243 else
5244 result = entry1->after_string_p ? -1 : 1;
5246 else if (entry1->after_string_p)
5247 /* After-strings sorted in order of decreasing priority. */
5248 result = entry2->priority - entry1->priority;
5249 else
5250 /* Before-strings sorted in order of increasing priority. */
5251 result = entry1->priority - entry2->priority;
5253 return result;
5257 /* Load the vector IT->overlay_strings with overlay strings from IT's
5258 current buffer position, or from CHARPOS if that is > 0. Set
5259 IT->n_overlays to the total number of overlay strings found.
5261 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5262 a time. On entry into load_overlay_strings,
5263 IT->current.overlay_string_index gives the number of overlay
5264 strings that have already been loaded by previous calls to this
5265 function.
5267 IT->add_overlay_start contains an additional overlay start
5268 position to consider for taking overlay strings from, if non-zero.
5269 This position comes into play when the overlay has an `invisible'
5270 property, and both before and after-strings. When we've skipped to
5271 the end of the overlay, because of its `invisible' property, we
5272 nevertheless want its before-string to appear.
5273 IT->add_overlay_start will contain the overlay start position
5274 in this case.
5276 Overlay strings are sorted so that after-string strings come in
5277 front of before-string strings. Within before and after-strings,
5278 strings are sorted by overlay priority. See also function
5279 compare_overlay_entries. */
5281 static void
5282 load_overlay_strings (struct it *it, EMACS_INT charpos)
5284 Lisp_Object overlay, window, str, invisible;
5285 struct Lisp_Overlay *ov;
5286 EMACS_INT start, end;
5287 int size = 20;
5288 int n = 0, i, j, invis_p;
5289 struct overlay_entry *entries
5290 = (struct overlay_entry *) alloca (size * sizeof *entries);
5292 if (charpos <= 0)
5293 charpos = IT_CHARPOS (*it);
5295 /* Append the overlay string STRING of overlay OVERLAY to vector
5296 `entries' which has size `size' and currently contains `n'
5297 elements. AFTER_P non-zero means STRING is an after-string of
5298 OVERLAY. */
5299 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5300 do \
5302 Lisp_Object priority; \
5304 if (n == size) \
5306 int new_size = 2 * size; \
5307 struct overlay_entry *old = entries; \
5308 entries = \
5309 (struct overlay_entry *) alloca (new_size \
5310 * sizeof *entries); \
5311 memcpy (entries, old, size * sizeof *entries); \
5312 size = new_size; \
5315 entries[n].string = (STRING); \
5316 entries[n].overlay = (OVERLAY); \
5317 priority = Foverlay_get ((OVERLAY), Qpriority); \
5318 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5319 entries[n].after_string_p = (AFTER_P); \
5320 ++n; \
5322 while (0)
5324 /* Process overlay before the overlay center. */
5325 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5327 XSETMISC (overlay, ov);
5328 xassert (OVERLAYP (overlay));
5329 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5330 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5332 if (end < charpos)
5333 break;
5335 /* Skip this overlay if it doesn't start or end at IT's current
5336 position. */
5337 if (end != charpos && start != charpos)
5338 continue;
5340 /* Skip this overlay if it doesn't apply to IT->w. */
5341 window = Foverlay_get (overlay, Qwindow);
5342 if (WINDOWP (window) && XWINDOW (window) != it->w)
5343 continue;
5345 /* If the text ``under'' the overlay is invisible, both before-
5346 and after-strings from this overlay are visible; start and
5347 end position are indistinguishable. */
5348 invisible = Foverlay_get (overlay, Qinvisible);
5349 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5351 /* If overlay has a non-empty before-string, record it. */
5352 if ((start == charpos || (end == charpos && invis_p))
5353 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5354 && SCHARS (str))
5355 RECORD_OVERLAY_STRING (overlay, str, 0);
5357 /* If overlay has a non-empty after-string, record it. */
5358 if ((end == charpos || (start == charpos && invis_p))
5359 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5360 && SCHARS (str))
5361 RECORD_OVERLAY_STRING (overlay, str, 1);
5364 /* Process overlays after the overlay center. */
5365 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5367 XSETMISC (overlay, ov);
5368 xassert (OVERLAYP (overlay));
5369 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5370 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5372 if (start > charpos)
5373 break;
5375 /* Skip this overlay if it doesn't start or end at IT's current
5376 position. */
5377 if (end != charpos && start != charpos)
5378 continue;
5380 /* Skip this overlay if it doesn't apply to IT->w. */
5381 window = Foverlay_get (overlay, Qwindow);
5382 if (WINDOWP (window) && XWINDOW (window) != it->w)
5383 continue;
5385 /* If the text ``under'' the overlay is invisible, it has a zero
5386 dimension, and both before- and after-strings apply. */
5387 invisible = Foverlay_get (overlay, Qinvisible);
5388 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5390 /* If overlay has a non-empty before-string, record it. */
5391 if ((start == charpos || (end == charpos && invis_p))
5392 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5393 && SCHARS (str))
5394 RECORD_OVERLAY_STRING (overlay, str, 0);
5396 /* If overlay has a non-empty after-string, record it. */
5397 if ((end == charpos || (start == charpos && invis_p))
5398 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5399 && SCHARS (str))
5400 RECORD_OVERLAY_STRING (overlay, str, 1);
5403 #undef RECORD_OVERLAY_STRING
5405 /* Sort entries. */
5406 if (n > 1)
5407 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5409 /* Record number of overlay strings, and where we computed it. */
5410 it->n_overlay_strings = n;
5411 it->overlay_strings_charpos = charpos;
5413 /* IT->current.overlay_string_index is the number of overlay strings
5414 that have already been consumed by IT. Copy some of the
5415 remaining overlay strings to IT->overlay_strings. */
5416 i = 0;
5417 j = it->current.overlay_string_index;
5418 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5420 it->overlay_strings[i] = entries[j].string;
5421 it->string_overlays[i++] = entries[j++].overlay;
5424 CHECK_IT (it);
5428 /* Get the first chunk of overlay strings at IT's current buffer
5429 position, or at CHARPOS if that is > 0. Value is non-zero if at
5430 least one overlay string was found. */
5432 static int
5433 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
5435 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5436 process. This fills IT->overlay_strings with strings, and sets
5437 IT->n_overlay_strings to the total number of strings to process.
5438 IT->pos.overlay_string_index has to be set temporarily to zero
5439 because load_overlay_strings needs this; it must be set to -1
5440 when no overlay strings are found because a zero value would
5441 indicate a position in the first overlay string. */
5442 it->current.overlay_string_index = 0;
5443 load_overlay_strings (it, charpos);
5445 /* If we found overlay strings, set up IT to deliver display
5446 elements from the first one. Otherwise set up IT to deliver
5447 from current_buffer. */
5448 if (it->n_overlay_strings)
5450 /* Make sure we know settings in current_buffer, so that we can
5451 restore meaningful values when we're done with the overlay
5452 strings. */
5453 if (compute_stop_p)
5454 compute_stop_pos (it);
5455 xassert (it->face_id >= 0);
5457 /* Save IT's settings. They are restored after all overlay
5458 strings have been processed. */
5459 xassert (!compute_stop_p || it->sp == 0);
5461 /* When called from handle_stop, there might be an empty display
5462 string loaded. In that case, don't bother saving it. But
5463 don't use this optimization with the bidi iterator, since we
5464 need the corresponding pop_it call to resync the bidi
5465 iterator's position with IT's position, after we are done
5466 with the overlay strings. (The corresponding call to pop_it
5467 in case of an empty display string is in
5468 next_overlay_string.) */
5469 if (!(!it->bidi_p
5470 && STRINGP (it->string) && !SCHARS (it->string)))
5471 push_it (it, NULL);
5473 /* Set up IT to deliver display elements from the first overlay
5474 string. */
5475 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5476 it->string = it->overlay_strings[0];
5477 it->from_overlay = Qnil;
5478 it->stop_charpos = 0;
5479 xassert (STRINGP (it->string));
5480 it->end_charpos = SCHARS (it->string);
5481 it->prev_stop = 0;
5482 it->base_level_stop = 0;
5483 it->multibyte_p = STRING_MULTIBYTE (it->string);
5484 it->method = GET_FROM_STRING;
5485 it->from_disp_prop_p = 0;
5487 /* Force paragraph direction to be that of the parent
5488 buffer. */
5489 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5490 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5491 else
5492 it->paragraph_embedding = L2R;
5494 /* Set up the bidi iterator for this overlay string. */
5495 if (it->bidi_p)
5497 EMACS_INT pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5499 it->bidi_it.string.lstring = it->string;
5500 it->bidi_it.string.s = NULL;
5501 it->bidi_it.string.schars = SCHARS (it->string);
5502 it->bidi_it.string.bufpos = pos;
5503 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5504 it->bidi_it.string.unibyte = !it->multibyte_p;
5505 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5507 return 1;
5510 it->current.overlay_string_index = -1;
5511 return 0;
5514 static int
5515 get_overlay_strings (struct it *it, EMACS_INT charpos)
5517 it->string = Qnil;
5518 it->method = GET_FROM_BUFFER;
5520 (void) get_overlay_strings_1 (it, charpos, 1);
5522 CHECK_IT (it);
5524 /* Value is non-zero if we found at least one overlay string. */
5525 return STRINGP (it->string);
5530 /***********************************************************************
5531 Saving and restoring state
5532 ***********************************************************************/
5534 /* Save current settings of IT on IT->stack. Called, for example,
5535 before setting up IT for an overlay string, to be able to restore
5536 IT's settings to what they were after the overlay string has been
5537 processed. If POSITION is non-NULL, it is the position to save on
5538 the stack instead of IT->position. */
5540 static void
5541 push_it (struct it *it, struct text_pos *position)
5543 struct iterator_stack_entry *p;
5545 xassert (it->sp < IT_STACK_SIZE);
5546 p = it->stack + it->sp;
5548 p->stop_charpos = it->stop_charpos;
5549 p->prev_stop = it->prev_stop;
5550 p->base_level_stop = it->base_level_stop;
5551 p->cmp_it = it->cmp_it;
5552 xassert (it->face_id >= 0);
5553 p->face_id = it->face_id;
5554 p->string = it->string;
5555 p->method = it->method;
5556 p->from_overlay = it->from_overlay;
5557 switch (p->method)
5559 case GET_FROM_IMAGE:
5560 p->u.image.object = it->object;
5561 p->u.image.image_id = it->image_id;
5562 p->u.image.slice = it->slice;
5563 break;
5564 case GET_FROM_STRETCH:
5565 p->u.stretch.object = it->object;
5566 break;
5568 p->position = position ? *position : it->position;
5569 p->current = it->current;
5570 p->end_charpos = it->end_charpos;
5571 p->string_nchars = it->string_nchars;
5572 p->area = it->area;
5573 p->multibyte_p = it->multibyte_p;
5574 p->avoid_cursor_p = it->avoid_cursor_p;
5575 p->space_width = it->space_width;
5576 p->font_height = it->font_height;
5577 p->voffset = it->voffset;
5578 p->string_from_display_prop_p = it->string_from_display_prop_p;
5579 p->display_ellipsis_p = 0;
5580 p->line_wrap = it->line_wrap;
5581 p->bidi_p = it->bidi_p;
5582 p->paragraph_embedding = it->paragraph_embedding;
5583 p->from_disp_prop_p = it->from_disp_prop_p;
5584 ++it->sp;
5586 /* Save the state of the bidi iterator as well. */
5587 if (it->bidi_p)
5588 bidi_push_it (&it->bidi_it);
5591 static void
5592 iterate_out_of_display_property (struct it *it)
5594 int buffer_p = BUFFERP (it->object);
5595 EMACS_INT eob = (buffer_p ? ZV : it->end_charpos);
5596 EMACS_INT bob = (buffer_p ? BEGV : 0);
5598 xassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5600 /* Maybe initialize paragraph direction. If we are at the beginning
5601 of a new paragraph, next_element_from_buffer may not have a
5602 chance to do that. */
5603 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5604 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5605 /* prev_stop can be zero, so check against BEGV as well. */
5606 while (it->bidi_it.charpos >= bob
5607 && it->prev_stop <= it->bidi_it.charpos
5608 && it->bidi_it.charpos < CHARPOS (it->position)
5609 && it->bidi_it.charpos < eob)
5610 bidi_move_to_visually_next (&it->bidi_it);
5611 /* Record the stop_pos we just crossed, for when we cross it
5612 back, maybe. */
5613 if (it->bidi_it.charpos > CHARPOS (it->position))
5614 it->prev_stop = CHARPOS (it->position);
5615 /* If we ended up not where pop_it put us, resync IT's
5616 positional members with the bidi iterator. */
5617 if (it->bidi_it.charpos != CHARPOS (it->position))
5618 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5619 if (buffer_p)
5620 it->current.pos = it->position;
5621 else
5622 it->current.string_pos = it->position;
5625 /* Restore IT's settings from IT->stack. Called, for example, when no
5626 more overlay strings must be processed, and we return to delivering
5627 display elements from a buffer, or when the end of a string from a
5628 `display' property is reached and we return to delivering display
5629 elements from an overlay string, or from a buffer. */
5631 static void
5632 pop_it (struct it *it)
5634 struct iterator_stack_entry *p;
5635 int from_display_prop = it->from_disp_prop_p;
5637 xassert (it->sp > 0);
5638 --it->sp;
5639 p = it->stack + it->sp;
5640 it->stop_charpos = p->stop_charpos;
5641 it->prev_stop = p->prev_stop;
5642 it->base_level_stop = p->base_level_stop;
5643 it->cmp_it = p->cmp_it;
5644 it->face_id = p->face_id;
5645 it->current = p->current;
5646 it->position = p->position;
5647 it->string = p->string;
5648 it->from_overlay = p->from_overlay;
5649 if (NILP (it->string))
5650 SET_TEXT_POS (it->current.string_pos, -1, -1);
5651 it->method = p->method;
5652 switch (it->method)
5654 case GET_FROM_IMAGE:
5655 it->image_id = p->u.image.image_id;
5656 it->object = p->u.image.object;
5657 it->slice = p->u.image.slice;
5658 break;
5659 case GET_FROM_STRETCH:
5660 it->object = p->u.stretch.object;
5661 break;
5662 case GET_FROM_BUFFER:
5663 it->object = it->w->buffer;
5664 break;
5665 case GET_FROM_STRING:
5666 it->object = it->string;
5667 break;
5668 case GET_FROM_DISPLAY_VECTOR:
5669 if (it->s)
5670 it->method = GET_FROM_C_STRING;
5671 else if (STRINGP (it->string))
5672 it->method = GET_FROM_STRING;
5673 else
5675 it->method = GET_FROM_BUFFER;
5676 it->object = it->w->buffer;
5679 it->end_charpos = p->end_charpos;
5680 it->string_nchars = p->string_nchars;
5681 it->area = p->area;
5682 it->multibyte_p = p->multibyte_p;
5683 it->avoid_cursor_p = p->avoid_cursor_p;
5684 it->space_width = p->space_width;
5685 it->font_height = p->font_height;
5686 it->voffset = p->voffset;
5687 it->string_from_display_prop_p = p->string_from_display_prop_p;
5688 it->line_wrap = p->line_wrap;
5689 it->bidi_p = p->bidi_p;
5690 it->paragraph_embedding = p->paragraph_embedding;
5691 it->from_disp_prop_p = p->from_disp_prop_p;
5692 if (it->bidi_p)
5694 bidi_pop_it (&it->bidi_it);
5695 /* Bidi-iterate until we get out of the portion of text, if any,
5696 covered by a `display' text property or by an overlay with
5697 `display' property. (We cannot just jump there, because the
5698 internal coherency of the bidi iterator state can not be
5699 preserved across such jumps.) We also must determine the
5700 paragraph base direction if the overlay we just processed is
5701 at the beginning of a new paragraph. */
5702 if (from_display_prop
5703 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5704 iterate_out_of_display_property (it);
5706 xassert ((BUFFERP (it->object)
5707 && IT_CHARPOS (*it) == it->bidi_it.charpos
5708 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5709 || (STRINGP (it->object)
5710 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5711 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5712 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5718 /***********************************************************************
5719 Moving over lines
5720 ***********************************************************************/
5722 /* Set IT's current position to the previous line start. */
5724 static void
5725 back_to_previous_line_start (struct it *it)
5727 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5728 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5732 /* Move IT to the next line start.
5734 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5735 we skipped over part of the text (as opposed to moving the iterator
5736 continuously over the text). Otherwise, don't change the value
5737 of *SKIPPED_P.
5739 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5740 iterator on the newline, if it was found.
5742 Newlines may come from buffer text, overlay strings, or strings
5743 displayed via the `display' property. That's the reason we can't
5744 simply use find_next_newline_no_quit.
5746 Note that this function may not skip over invisible text that is so
5747 because of text properties and immediately follows a newline. If
5748 it would, function reseat_at_next_visible_line_start, when called
5749 from set_iterator_to_next, would effectively make invisible
5750 characters following a newline part of the wrong glyph row, which
5751 leads to wrong cursor motion. */
5753 static int
5754 forward_to_next_line_start (struct it *it, int *skipped_p,
5755 struct bidi_it *bidi_it_prev)
5757 EMACS_INT old_selective;
5758 int newline_found_p, n;
5759 const int MAX_NEWLINE_DISTANCE = 500;
5761 /* If already on a newline, just consume it to avoid unintended
5762 skipping over invisible text below. */
5763 if (it->what == IT_CHARACTER
5764 && it->c == '\n'
5765 && CHARPOS (it->position) == IT_CHARPOS (*it))
5767 if (it->bidi_p && bidi_it_prev)
5768 *bidi_it_prev = it->bidi_it;
5769 set_iterator_to_next (it, 0);
5770 it->c = 0;
5771 return 1;
5774 /* Don't handle selective display in the following. It's (a)
5775 unnecessary because it's done by the caller, and (b) leads to an
5776 infinite recursion because next_element_from_ellipsis indirectly
5777 calls this function. */
5778 old_selective = it->selective;
5779 it->selective = 0;
5781 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5782 from buffer text. */
5783 for (n = newline_found_p = 0;
5784 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5785 n += STRINGP (it->string) ? 0 : 1)
5787 if (!get_next_display_element (it))
5788 return 0;
5789 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5790 if (newline_found_p && it->bidi_p && bidi_it_prev)
5791 *bidi_it_prev = it->bidi_it;
5792 set_iterator_to_next (it, 0);
5795 /* If we didn't find a newline near enough, see if we can use a
5796 short-cut. */
5797 if (!newline_found_p)
5799 EMACS_INT start = IT_CHARPOS (*it);
5800 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5801 Lisp_Object pos;
5803 xassert (!STRINGP (it->string));
5805 /* If there isn't any `display' property in sight, and no
5806 overlays, we can just use the position of the newline in
5807 buffer text. */
5808 if (it->stop_charpos >= limit
5809 || ((pos = Fnext_single_property_change (make_number (start),
5810 Qdisplay, Qnil,
5811 make_number (limit)),
5812 NILP (pos))
5813 && next_overlay_change (start) == ZV))
5815 if (!it->bidi_p)
5817 IT_CHARPOS (*it) = limit;
5818 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5820 else
5822 struct bidi_it bprev;
5824 /* Help bidi.c avoid expensive searches for display
5825 properties and overlays, by telling it that there are
5826 none up to `limit'. */
5827 if (it->bidi_it.disp_pos < limit)
5829 it->bidi_it.disp_pos = limit;
5830 it->bidi_it.disp_prop = 0;
5832 do {
5833 bprev = it->bidi_it;
5834 bidi_move_to_visually_next (&it->bidi_it);
5835 } while (it->bidi_it.charpos != limit);
5836 IT_CHARPOS (*it) = limit;
5837 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5838 if (bidi_it_prev)
5839 *bidi_it_prev = bprev;
5841 *skipped_p = newline_found_p = 1;
5843 else
5845 while (get_next_display_element (it)
5846 && !newline_found_p)
5848 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5849 if (newline_found_p && it->bidi_p && bidi_it_prev)
5850 *bidi_it_prev = it->bidi_it;
5851 set_iterator_to_next (it, 0);
5856 it->selective = old_selective;
5857 return newline_found_p;
5861 /* Set IT's current position to the previous visible line start. Skip
5862 invisible text that is so either due to text properties or due to
5863 selective display. Caution: this does not change IT->current_x and
5864 IT->hpos. */
5866 static void
5867 back_to_previous_visible_line_start (struct it *it)
5869 while (IT_CHARPOS (*it) > BEGV)
5871 back_to_previous_line_start (it);
5873 if (IT_CHARPOS (*it) <= BEGV)
5874 break;
5876 /* If selective > 0, then lines indented more than its value are
5877 invisible. */
5878 if (it->selective > 0
5879 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5880 it->selective))
5881 continue;
5883 /* Check the newline before point for invisibility. */
5885 Lisp_Object prop;
5886 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5887 Qinvisible, it->window);
5888 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5889 continue;
5892 if (IT_CHARPOS (*it) <= BEGV)
5893 break;
5896 struct it it2;
5897 void *it2data = NULL;
5898 EMACS_INT pos;
5899 EMACS_INT beg, end;
5900 Lisp_Object val, overlay;
5902 SAVE_IT (it2, *it, it2data);
5904 /* If newline is part of a composition, continue from start of composition */
5905 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5906 && beg < IT_CHARPOS (*it))
5907 goto replaced;
5909 /* If newline is replaced by a display property, find start of overlay
5910 or interval and continue search from that point. */
5911 pos = --IT_CHARPOS (it2);
5912 --IT_BYTEPOS (it2);
5913 it2.sp = 0;
5914 bidi_unshelve_cache (NULL, 0);
5915 it2.string_from_display_prop_p = 0;
5916 it2.from_disp_prop_p = 0;
5917 if (handle_display_prop (&it2) == HANDLED_RETURN
5918 && !NILP (val = get_char_property_and_overlay
5919 (make_number (pos), Qdisplay, Qnil, &overlay))
5920 && (OVERLAYP (overlay)
5921 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5922 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5924 RESTORE_IT (it, it, it2data);
5925 goto replaced;
5928 /* Newline is not replaced by anything -- so we are done. */
5929 RESTORE_IT (it, it, it2data);
5930 break;
5932 replaced:
5933 if (beg < BEGV)
5934 beg = BEGV;
5935 IT_CHARPOS (*it) = beg;
5936 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5940 it->continuation_lines_width = 0;
5942 xassert (IT_CHARPOS (*it) >= BEGV);
5943 xassert (IT_CHARPOS (*it) == BEGV
5944 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5945 CHECK_IT (it);
5949 /* Reseat iterator IT at the previous visible line start. Skip
5950 invisible text that is so either due to text properties or due to
5951 selective display. At the end, update IT's overlay information,
5952 face information etc. */
5954 void
5955 reseat_at_previous_visible_line_start (struct it *it)
5957 back_to_previous_visible_line_start (it);
5958 reseat (it, it->current.pos, 1);
5959 CHECK_IT (it);
5963 /* Reseat iterator IT on the next visible line start in the current
5964 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5965 preceding the line start. Skip over invisible text that is so
5966 because of selective display. Compute faces, overlays etc at the
5967 new position. Note that this function does not skip over text that
5968 is invisible because of text properties. */
5970 static void
5971 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5973 int newline_found_p, skipped_p = 0;
5974 struct bidi_it bidi_it_prev;
5976 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
5978 /* Skip over lines that are invisible because they are indented
5979 more than the value of IT->selective. */
5980 if (it->selective > 0)
5981 while (IT_CHARPOS (*it) < ZV
5982 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5983 it->selective))
5985 xassert (IT_BYTEPOS (*it) == BEGV
5986 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5987 newline_found_p =
5988 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
5991 /* Position on the newline if that's what's requested. */
5992 if (on_newline_p && newline_found_p)
5994 if (STRINGP (it->string))
5996 if (IT_STRING_CHARPOS (*it) > 0)
5998 if (!it->bidi_p)
6000 --IT_STRING_CHARPOS (*it);
6001 --IT_STRING_BYTEPOS (*it);
6003 else
6005 /* We need to restore the bidi iterator to the state
6006 it had on the newline, and resync the IT's
6007 position with that. */
6008 it->bidi_it = bidi_it_prev;
6009 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6010 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6014 else if (IT_CHARPOS (*it) > BEGV)
6016 if (!it->bidi_p)
6018 --IT_CHARPOS (*it);
6019 --IT_BYTEPOS (*it);
6021 else
6023 /* We need to restore the bidi iterator to the state it
6024 had on the newline and resync IT with that. */
6025 it->bidi_it = bidi_it_prev;
6026 IT_CHARPOS (*it) = it->bidi_it.charpos;
6027 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6029 reseat (it, it->current.pos, 0);
6032 else if (skipped_p)
6033 reseat (it, it->current.pos, 0);
6035 CHECK_IT (it);
6040 /***********************************************************************
6041 Changing an iterator's position
6042 ***********************************************************************/
6044 /* Change IT's current position to POS in current_buffer. If FORCE_P
6045 is non-zero, always check for text properties at the new position.
6046 Otherwise, text properties are only looked up if POS >=
6047 IT->check_charpos of a property. */
6049 static void
6050 reseat (struct it *it, struct text_pos pos, int force_p)
6052 EMACS_INT original_pos = IT_CHARPOS (*it);
6054 reseat_1 (it, pos, 0);
6056 /* Determine where to check text properties. Avoid doing it
6057 where possible because text property lookup is very expensive. */
6058 if (force_p
6059 || CHARPOS (pos) > it->stop_charpos
6060 || CHARPOS (pos) < original_pos)
6062 if (it->bidi_p)
6064 /* For bidi iteration, we need to prime prev_stop and
6065 base_level_stop with our best estimations. */
6066 /* Implementation note: Of course, POS is not necessarily a
6067 stop position, so assigning prev_pos to it is a lie; we
6068 should have called compute_stop_backwards. However, if
6069 the current buffer does not include any R2L characters,
6070 that call would be a waste of cycles, because the
6071 iterator will never move back, and thus never cross this
6072 "fake" stop position. So we delay that backward search
6073 until the time we really need it, in next_element_from_buffer. */
6074 if (CHARPOS (pos) != it->prev_stop)
6075 it->prev_stop = CHARPOS (pos);
6076 if (CHARPOS (pos) < it->base_level_stop)
6077 it->base_level_stop = 0; /* meaning it's unknown */
6078 handle_stop (it);
6080 else
6082 handle_stop (it);
6083 it->prev_stop = it->base_level_stop = 0;
6088 CHECK_IT (it);
6092 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6093 IT->stop_pos to POS, also. */
6095 static void
6096 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6098 /* Don't call this function when scanning a C string. */
6099 xassert (it->s == NULL);
6101 /* POS must be a reasonable value. */
6102 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6104 it->current.pos = it->position = pos;
6105 it->end_charpos = ZV;
6106 it->dpvec = NULL;
6107 it->current.dpvec_index = -1;
6108 it->current.overlay_string_index = -1;
6109 IT_STRING_CHARPOS (*it) = -1;
6110 IT_STRING_BYTEPOS (*it) = -1;
6111 it->string = Qnil;
6112 it->method = GET_FROM_BUFFER;
6113 it->object = it->w->buffer;
6114 it->area = TEXT_AREA;
6115 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6116 it->sp = 0;
6117 it->string_from_display_prop_p = 0;
6118 it->from_disp_prop_p = 0;
6119 it->face_before_selective_p = 0;
6120 if (it->bidi_p)
6122 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6123 &it->bidi_it);
6124 bidi_unshelve_cache (NULL, 0);
6125 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6126 it->bidi_it.string.s = NULL;
6127 it->bidi_it.string.lstring = Qnil;
6128 it->bidi_it.string.bufpos = 0;
6129 it->bidi_it.string.unibyte = 0;
6132 if (set_stop_p)
6134 it->stop_charpos = CHARPOS (pos);
6135 it->base_level_stop = CHARPOS (pos);
6140 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6141 If S is non-null, it is a C string to iterate over. Otherwise,
6142 STRING gives a Lisp string to iterate over.
6144 If PRECISION > 0, don't return more then PRECISION number of
6145 characters from the string.
6147 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6148 characters have been returned. FIELD_WIDTH < 0 means an infinite
6149 field width.
6151 MULTIBYTE = 0 means disable processing of multibyte characters,
6152 MULTIBYTE > 0 means enable it,
6153 MULTIBYTE < 0 means use IT->multibyte_p.
6155 IT must be initialized via a prior call to init_iterator before
6156 calling this function. */
6158 static void
6159 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6160 EMACS_INT charpos, EMACS_INT precision, int field_width,
6161 int multibyte)
6163 /* No region in strings. */
6164 it->region_beg_charpos = it->region_end_charpos = -1;
6166 /* No text property checks performed by default, but see below. */
6167 it->stop_charpos = -1;
6169 /* Set iterator position and end position. */
6170 memset (&it->current, 0, sizeof it->current);
6171 it->current.overlay_string_index = -1;
6172 it->current.dpvec_index = -1;
6173 xassert (charpos >= 0);
6175 /* If STRING is specified, use its multibyteness, otherwise use the
6176 setting of MULTIBYTE, if specified. */
6177 if (multibyte >= 0)
6178 it->multibyte_p = multibyte > 0;
6180 /* Bidirectional reordering of strings is controlled by the default
6181 value of bidi-display-reordering. Don't try to reorder while
6182 loading loadup.el, as the necessary character property tables are
6183 not yet available. */
6184 it->bidi_p =
6185 NILP (Vpurify_flag)
6186 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6188 if (s == NULL)
6190 xassert (STRINGP (string));
6191 it->string = string;
6192 it->s = NULL;
6193 it->end_charpos = it->string_nchars = SCHARS (string);
6194 it->method = GET_FROM_STRING;
6195 it->current.string_pos = string_pos (charpos, string);
6197 if (it->bidi_p)
6199 it->bidi_it.string.lstring = string;
6200 it->bidi_it.string.s = NULL;
6201 it->bidi_it.string.schars = it->end_charpos;
6202 it->bidi_it.string.bufpos = 0;
6203 it->bidi_it.string.from_disp_str = 0;
6204 it->bidi_it.string.unibyte = !it->multibyte_p;
6205 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6206 FRAME_WINDOW_P (it->f), &it->bidi_it);
6209 else
6211 it->s = (const unsigned char *) s;
6212 it->string = Qnil;
6214 /* Note that we use IT->current.pos, not it->current.string_pos,
6215 for displaying C strings. */
6216 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6217 if (it->multibyte_p)
6219 it->current.pos = c_string_pos (charpos, s, 1);
6220 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6222 else
6224 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6225 it->end_charpos = it->string_nchars = strlen (s);
6228 if (it->bidi_p)
6230 it->bidi_it.string.lstring = Qnil;
6231 it->bidi_it.string.s = (const unsigned char *) s;
6232 it->bidi_it.string.schars = it->end_charpos;
6233 it->bidi_it.string.bufpos = 0;
6234 it->bidi_it.string.from_disp_str = 0;
6235 it->bidi_it.string.unibyte = !it->multibyte_p;
6236 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6237 &it->bidi_it);
6239 it->method = GET_FROM_C_STRING;
6242 /* PRECISION > 0 means don't return more than PRECISION characters
6243 from the string. */
6244 if (precision > 0 && it->end_charpos - charpos > precision)
6246 it->end_charpos = it->string_nchars = charpos + precision;
6247 if (it->bidi_p)
6248 it->bidi_it.string.schars = it->end_charpos;
6251 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6252 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6253 FIELD_WIDTH < 0 means infinite field width. This is useful for
6254 padding with `-' at the end of a mode line. */
6255 if (field_width < 0)
6256 field_width = INFINITY;
6257 /* Implementation note: We deliberately don't enlarge
6258 it->bidi_it.string.schars here to fit it->end_charpos, because
6259 the bidi iterator cannot produce characters out of thin air. */
6260 if (field_width > it->end_charpos - charpos)
6261 it->end_charpos = charpos + field_width;
6263 /* Use the standard display table for displaying strings. */
6264 if (DISP_TABLE_P (Vstandard_display_table))
6265 it->dp = XCHAR_TABLE (Vstandard_display_table);
6267 it->stop_charpos = charpos;
6268 it->prev_stop = charpos;
6269 it->base_level_stop = 0;
6270 if (it->bidi_p)
6272 it->bidi_it.first_elt = 1;
6273 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6274 it->bidi_it.disp_pos = -1;
6276 if (s == NULL && it->multibyte_p)
6278 EMACS_INT endpos = SCHARS (it->string);
6279 if (endpos > it->end_charpos)
6280 endpos = it->end_charpos;
6281 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6282 it->string);
6284 CHECK_IT (it);
6289 /***********************************************************************
6290 Iteration
6291 ***********************************************************************/
6293 /* Map enum it_method value to corresponding next_element_from_* function. */
6295 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6297 next_element_from_buffer,
6298 next_element_from_display_vector,
6299 next_element_from_string,
6300 next_element_from_c_string,
6301 next_element_from_image,
6302 next_element_from_stretch
6305 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6308 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6309 (possibly with the following characters). */
6311 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6312 ((IT)->cmp_it.id >= 0 \
6313 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6314 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6315 END_CHARPOS, (IT)->w, \
6316 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6317 (IT)->string)))
6320 /* Lookup the char-table Vglyphless_char_display for character C (-1
6321 if we want information for no-font case), and return the display
6322 method symbol. By side-effect, update it->what and
6323 it->glyphless_method. This function is called from
6324 get_next_display_element for each character element, and from
6325 x_produce_glyphs when no suitable font was found. */
6327 Lisp_Object
6328 lookup_glyphless_char_display (int c, struct it *it)
6330 Lisp_Object glyphless_method = Qnil;
6332 if (CHAR_TABLE_P (Vglyphless_char_display)
6333 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6335 if (c >= 0)
6337 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6338 if (CONSP (glyphless_method))
6339 glyphless_method = FRAME_WINDOW_P (it->f)
6340 ? XCAR (glyphless_method)
6341 : XCDR (glyphless_method);
6343 else
6344 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6347 retry:
6348 if (NILP (glyphless_method))
6350 if (c >= 0)
6351 /* The default is to display the character by a proper font. */
6352 return Qnil;
6353 /* The default for the no-font case is to display an empty box. */
6354 glyphless_method = Qempty_box;
6356 if (EQ (glyphless_method, Qzero_width))
6358 if (c >= 0)
6359 return glyphless_method;
6360 /* This method can't be used for the no-font case. */
6361 glyphless_method = Qempty_box;
6363 if (EQ (glyphless_method, Qthin_space))
6364 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6365 else if (EQ (glyphless_method, Qempty_box))
6366 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6367 else if (EQ (glyphless_method, Qhex_code))
6368 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6369 else if (STRINGP (glyphless_method))
6370 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6371 else
6373 /* Invalid value. We use the default method. */
6374 glyphless_method = Qnil;
6375 goto retry;
6377 it->what = IT_GLYPHLESS;
6378 return glyphless_method;
6381 /* Load IT's display element fields with information about the next
6382 display element from the current position of IT. Value is zero if
6383 end of buffer (or C string) is reached. */
6385 static struct frame *last_escape_glyph_frame = NULL;
6386 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6387 static int last_escape_glyph_merged_face_id = 0;
6389 struct frame *last_glyphless_glyph_frame = NULL;
6390 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6391 int last_glyphless_glyph_merged_face_id = 0;
6393 static int
6394 get_next_display_element (struct it *it)
6396 /* Non-zero means that we found a display element. Zero means that
6397 we hit the end of what we iterate over. Performance note: the
6398 function pointer `method' used here turns out to be faster than
6399 using a sequence of if-statements. */
6400 int success_p;
6402 get_next:
6403 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6405 if (it->what == IT_CHARACTER)
6407 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6408 and only if (a) the resolved directionality of that character
6409 is R..." */
6410 /* FIXME: Do we need an exception for characters from display
6411 tables? */
6412 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6413 it->c = bidi_mirror_char (it->c);
6414 /* Map via display table or translate control characters.
6415 IT->c, IT->len etc. have been set to the next character by
6416 the function call above. If we have a display table, and it
6417 contains an entry for IT->c, translate it. Don't do this if
6418 IT->c itself comes from a display table, otherwise we could
6419 end up in an infinite recursion. (An alternative could be to
6420 count the recursion depth of this function and signal an
6421 error when a certain maximum depth is reached.) Is it worth
6422 it? */
6423 if (success_p && it->dpvec == NULL)
6425 Lisp_Object dv;
6426 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6427 int nonascii_space_p = 0;
6428 int nonascii_hyphen_p = 0;
6429 int c = it->c; /* This is the character to display. */
6431 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6433 xassert (SINGLE_BYTE_CHAR_P (c));
6434 if (unibyte_display_via_language_environment)
6436 c = DECODE_CHAR (unibyte, c);
6437 if (c < 0)
6438 c = BYTE8_TO_CHAR (it->c);
6440 else
6441 c = BYTE8_TO_CHAR (it->c);
6444 if (it->dp
6445 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6446 VECTORP (dv)))
6448 struct Lisp_Vector *v = XVECTOR (dv);
6450 /* Return the first character from the display table
6451 entry, if not empty. If empty, don't display the
6452 current character. */
6453 if (v->header.size)
6455 it->dpvec_char_len = it->len;
6456 it->dpvec = v->contents;
6457 it->dpend = v->contents + v->header.size;
6458 it->current.dpvec_index = 0;
6459 it->dpvec_face_id = -1;
6460 it->saved_face_id = it->face_id;
6461 it->method = GET_FROM_DISPLAY_VECTOR;
6462 it->ellipsis_p = 0;
6464 else
6466 set_iterator_to_next (it, 0);
6468 goto get_next;
6471 if (! NILP (lookup_glyphless_char_display (c, it)))
6473 if (it->what == IT_GLYPHLESS)
6474 goto done;
6475 /* Don't display this character. */
6476 set_iterator_to_next (it, 0);
6477 goto get_next;
6480 /* If `nobreak-char-display' is non-nil, we display
6481 non-ASCII spaces and hyphens specially. */
6482 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6484 if (c == 0xA0)
6485 nonascii_space_p = 1;
6486 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6487 nonascii_hyphen_p = 1;
6490 /* Translate control characters into `\003' or `^C' form.
6491 Control characters coming from a display table entry are
6492 currently not translated because we use IT->dpvec to hold
6493 the translation. This could easily be changed but I
6494 don't believe that it is worth doing.
6496 The characters handled by `nobreak-char-display' must be
6497 translated too.
6499 Non-printable characters and raw-byte characters are also
6500 translated to octal form. */
6501 if (((c < ' ' || c == 127) /* ASCII control chars */
6502 ? (it->area != TEXT_AREA
6503 /* In mode line, treat \n, \t like other crl chars. */
6504 || (c != '\t'
6505 && it->glyph_row
6506 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6507 || (c != '\n' && c != '\t'))
6508 : (nonascii_space_p
6509 || nonascii_hyphen_p
6510 || CHAR_BYTE8_P (c)
6511 || ! CHAR_PRINTABLE_P (c))))
6513 /* C is a control character, non-ASCII space/hyphen,
6514 raw-byte, or a non-printable character which must be
6515 displayed either as '\003' or as `^C' where the '\\'
6516 and '^' can be defined in the display table. Fill
6517 IT->ctl_chars with glyphs for what we have to
6518 display. Then, set IT->dpvec to these glyphs. */
6519 Lisp_Object gc;
6520 int ctl_len;
6521 int face_id;
6522 EMACS_INT lface_id = 0;
6523 int escape_glyph;
6525 /* Handle control characters with ^. */
6527 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6529 int g;
6531 g = '^'; /* default glyph for Control */
6532 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6533 if (it->dp
6534 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
6535 && GLYPH_CODE_CHAR_VALID_P (gc))
6537 g = GLYPH_CODE_CHAR (gc);
6538 lface_id = GLYPH_CODE_FACE (gc);
6540 if (lface_id)
6542 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6544 else if (it->f == last_escape_glyph_frame
6545 && it->face_id == last_escape_glyph_face_id)
6547 face_id = last_escape_glyph_merged_face_id;
6549 else
6551 /* Merge the escape-glyph face into the current face. */
6552 face_id = merge_faces (it->f, Qescape_glyph, 0,
6553 it->face_id);
6554 last_escape_glyph_frame = it->f;
6555 last_escape_glyph_face_id = it->face_id;
6556 last_escape_glyph_merged_face_id = face_id;
6559 XSETINT (it->ctl_chars[0], g);
6560 XSETINT (it->ctl_chars[1], c ^ 0100);
6561 ctl_len = 2;
6562 goto display_control;
6565 /* Handle non-ascii space in the mode where it only gets
6566 highlighting. */
6568 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6570 /* Merge `nobreak-space' into the current face. */
6571 face_id = merge_faces (it->f, Qnobreak_space, 0,
6572 it->face_id);
6573 XSETINT (it->ctl_chars[0], ' ');
6574 ctl_len = 1;
6575 goto display_control;
6578 /* Handle sequences that start with the "escape glyph". */
6580 /* the default escape glyph is \. */
6581 escape_glyph = '\\';
6583 if (it->dp
6584 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
6585 && GLYPH_CODE_CHAR_VALID_P (gc))
6587 escape_glyph = GLYPH_CODE_CHAR (gc);
6588 lface_id = GLYPH_CODE_FACE (gc);
6590 if (lface_id)
6592 /* The display table specified a face.
6593 Merge it into face_id and also into escape_glyph. */
6594 face_id = merge_faces (it->f, Qt, lface_id,
6595 it->face_id);
6597 else if (it->f == last_escape_glyph_frame
6598 && it->face_id == last_escape_glyph_face_id)
6600 face_id = last_escape_glyph_merged_face_id;
6602 else
6604 /* Merge the escape-glyph face into the current face. */
6605 face_id = merge_faces (it->f, Qescape_glyph, 0,
6606 it->face_id);
6607 last_escape_glyph_frame = it->f;
6608 last_escape_glyph_face_id = it->face_id;
6609 last_escape_glyph_merged_face_id = face_id;
6612 /* Draw non-ASCII hyphen with just highlighting: */
6614 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6616 XSETINT (it->ctl_chars[0], '-');
6617 ctl_len = 1;
6618 goto display_control;
6621 /* Draw non-ASCII space/hyphen with escape glyph: */
6623 if (nonascii_space_p || nonascii_hyphen_p)
6625 XSETINT (it->ctl_chars[0], escape_glyph);
6626 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6627 ctl_len = 2;
6628 goto display_control;
6632 char str[10];
6633 int len, i;
6635 if (CHAR_BYTE8_P (c))
6636 /* Display \200 instead of \17777600. */
6637 c = CHAR_TO_BYTE8 (c);
6638 len = sprintf (str, "%03o", c);
6640 XSETINT (it->ctl_chars[0], escape_glyph);
6641 for (i = 0; i < len; i++)
6642 XSETINT (it->ctl_chars[i + 1], str[i]);
6643 ctl_len = len + 1;
6646 display_control:
6647 /* Set up IT->dpvec and return first character from it. */
6648 it->dpvec_char_len = it->len;
6649 it->dpvec = it->ctl_chars;
6650 it->dpend = it->dpvec + ctl_len;
6651 it->current.dpvec_index = 0;
6652 it->dpvec_face_id = face_id;
6653 it->saved_face_id = it->face_id;
6654 it->method = GET_FROM_DISPLAY_VECTOR;
6655 it->ellipsis_p = 0;
6656 goto get_next;
6658 it->char_to_display = c;
6660 else if (success_p)
6662 it->char_to_display = it->c;
6666 /* Adjust face id for a multibyte character. There are no multibyte
6667 character in unibyte text. */
6668 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6669 && it->multibyte_p
6670 && success_p
6671 && FRAME_WINDOW_P (it->f))
6673 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6675 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6677 /* Automatic composition with glyph-string. */
6678 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6680 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6682 else
6684 EMACS_INT pos = (it->s ? -1
6685 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6686 : IT_CHARPOS (*it));
6687 int c;
6689 if (it->what == IT_CHARACTER)
6690 c = it->char_to_display;
6691 else
6693 struct composition *cmp = composition_table[it->cmp_it.id];
6694 int i;
6696 c = ' ';
6697 for (i = 0; i < cmp->glyph_len; i++)
6698 /* TAB in a composition means display glyphs with
6699 padding space on the left or right. */
6700 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6701 break;
6703 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6707 done:
6708 /* Is this character the last one of a run of characters with
6709 box? If yes, set IT->end_of_box_run_p to 1. */
6710 if (it->face_box_p
6711 && it->s == NULL)
6713 if (it->method == GET_FROM_STRING && it->sp)
6715 int face_id = underlying_face_id (it);
6716 struct face *face = FACE_FROM_ID (it->f, face_id);
6718 if (face)
6720 if (face->box == FACE_NO_BOX)
6722 /* If the box comes from face properties in a
6723 display string, check faces in that string. */
6724 int string_face_id = face_after_it_pos (it);
6725 it->end_of_box_run_p
6726 = (FACE_FROM_ID (it->f, string_face_id)->box
6727 == FACE_NO_BOX);
6729 /* Otherwise, the box comes from the underlying face.
6730 If this is the last string character displayed, check
6731 the next buffer location. */
6732 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6733 && (it->current.overlay_string_index
6734 == it->n_overlay_strings - 1))
6736 EMACS_INT ignore;
6737 int next_face_id;
6738 struct text_pos pos = it->current.pos;
6739 INC_TEXT_POS (pos, it->multibyte_p);
6741 next_face_id = face_at_buffer_position
6742 (it->w, CHARPOS (pos), it->region_beg_charpos,
6743 it->region_end_charpos, &ignore,
6744 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6745 -1);
6746 it->end_of_box_run_p
6747 = (FACE_FROM_ID (it->f, next_face_id)->box
6748 == FACE_NO_BOX);
6752 else
6754 int face_id = face_after_it_pos (it);
6755 it->end_of_box_run_p
6756 = (face_id != it->face_id
6757 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6761 /* Value is 0 if end of buffer or string reached. */
6762 return success_p;
6766 /* Move IT to the next display element.
6768 RESEAT_P non-zero means if called on a newline in buffer text,
6769 skip to the next visible line start.
6771 Functions get_next_display_element and set_iterator_to_next are
6772 separate because I find this arrangement easier to handle than a
6773 get_next_display_element function that also increments IT's
6774 position. The way it is we can first look at an iterator's current
6775 display element, decide whether it fits on a line, and if it does,
6776 increment the iterator position. The other way around we probably
6777 would either need a flag indicating whether the iterator has to be
6778 incremented the next time, or we would have to implement a
6779 decrement position function which would not be easy to write. */
6781 void
6782 set_iterator_to_next (struct it *it, int reseat_p)
6784 /* Reset flags indicating start and end of a sequence of characters
6785 with box. Reset them at the start of this function because
6786 moving the iterator to a new position might set them. */
6787 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6789 switch (it->method)
6791 case GET_FROM_BUFFER:
6792 /* The current display element of IT is a character from
6793 current_buffer. Advance in the buffer, and maybe skip over
6794 invisible lines that are so because of selective display. */
6795 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6796 reseat_at_next_visible_line_start (it, 0);
6797 else if (it->cmp_it.id >= 0)
6799 /* We are currently getting glyphs from a composition. */
6800 int i;
6802 if (! it->bidi_p)
6804 IT_CHARPOS (*it) += it->cmp_it.nchars;
6805 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6806 if (it->cmp_it.to < it->cmp_it.nglyphs)
6808 it->cmp_it.from = it->cmp_it.to;
6810 else
6812 it->cmp_it.id = -1;
6813 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6814 IT_BYTEPOS (*it),
6815 it->end_charpos, Qnil);
6818 else if (! it->cmp_it.reversed_p)
6820 /* Composition created while scanning forward. */
6821 /* Update IT's char/byte positions to point to the first
6822 character of the next grapheme cluster, or to the
6823 character visually after the current composition. */
6824 for (i = 0; i < it->cmp_it.nchars; i++)
6825 bidi_move_to_visually_next (&it->bidi_it);
6826 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6827 IT_CHARPOS (*it) = it->bidi_it.charpos;
6829 if (it->cmp_it.to < it->cmp_it.nglyphs)
6831 /* Proceed to the next grapheme cluster. */
6832 it->cmp_it.from = it->cmp_it.to;
6834 else
6836 /* No more grapheme clusters in this composition.
6837 Find the next stop position. */
6838 EMACS_INT stop = it->end_charpos;
6839 if (it->bidi_it.scan_dir < 0)
6840 /* Now we are scanning backward and don't know
6841 where to stop. */
6842 stop = -1;
6843 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6844 IT_BYTEPOS (*it), stop, Qnil);
6847 else
6849 /* Composition created while scanning backward. */
6850 /* Update IT's char/byte positions to point to the last
6851 character of the previous grapheme cluster, or the
6852 character visually after the current composition. */
6853 for (i = 0; i < it->cmp_it.nchars; i++)
6854 bidi_move_to_visually_next (&it->bidi_it);
6855 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6856 IT_CHARPOS (*it) = it->bidi_it.charpos;
6857 if (it->cmp_it.from > 0)
6859 /* Proceed to the previous grapheme cluster. */
6860 it->cmp_it.to = it->cmp_it.from;
6862 else
6864 /* No more grapheme clusters in this composition.
6865 Find the next stop position. */
6866 EMACS_INT stop = it->end_charpos;
6867 if (it->bidi_it.scan_dir < 0)
6868 /* Now we are scanning backward and don't know
6869 where to stop. */
6870 stop = -1;
6871 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6872 IT_BYTEPOS (*it), stop, Qnil);
6876 else
6878 xassert (it->len != 0);
6880 if (!it->bidi_p)
6882 IT_BYTEPOS (*it) += it->len;
6883 IT_CHARPOS (*it) += 1;
6885 else
6887 int prev_scan_dir = it->bidi_it.scan_dir;
6888 /* If this is a new paragraph, determine its base
6889 direction (a.k.a. its base embedding level). */
6890 if (it->bidi_it.new_paragraph)
6891 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6892 bidi_move_to_visually_next (&it->bidi_it);
6893 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6894 IT_CHARPOS (*it) = it->bidi_it.charpos;
6895 if (prev_scan_dir != it->bidi_it.scan_dir)
6897 /* As the scan direction was changed, we must
6898 re-compute the stop position for composition. */
6899 EMACS_INT stop = it->end_charpos;
6900 if (it->bidi_it.scan_dir < 0)
6901 stop = -1;
6902 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6903 IT_BYTEPOS (*it), stop, Qnil);
6906 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6908 break;
6910 case GET_FROM_C_STRING:
6911 /* Current display element of IT is from a C string. */
6912 if (!it->bidi_p
6913 /* If the string position is beyond string's end, it means
6914 next_element_from_c_string is padding the string with
6915 blanks, in which case we bypass the bidi iterator,
6916 because it cannot deal with such virtual characters. */
6917 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
6919 IT_BYTEPOS (*it) += it->len;
6920 IT_CHARPOS (*it) += 1;
6922 else
6924 bidi_move_to_visually_next (&it->bidi_it);
6925 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6926 IT_CHARPOS (*it) = it->bidi_it.charpos;
6928 break;
6930 case GET_FROM_DISPLAY_VECTOR:
6931 /* Current display element of IT is from a display table entry.
6932 Advance in the display table definition. Reset it to null if
6933 end reached, and continue with characters from buffers/
6934 strings. */
6935 ++it->current.dpvec_index;
6937 /* Restore face of the iterator to what they were before the
6938 display vector entry (these entries may contain faces). */
6939 it->face_id = it->saved_face_id;
6941 if (it->dpvec + it->current.dpvec_index == it->dpend)
6943 int recheck_faces = it->ellipsis_p;
6945 if (it->s)
6946 it->method = GET_FROM_C_STRING;
6947 else if (STRINGP (it->string))
6948 it->method = GET_FROM_STRING;
6949 else
6951 it->method = GET_FROM_BUFFER;
6952 it->object = it->w->buffer;
6955 it->dpvec = NULL;
6956 it->current.dpvec_index = -1;
6958 /* Skip over characters which were displayed via IT->dpvec. */
6959 if (it->dpvec_char_len < 0)
6960 reseat_at_next_visible_line_start (it, 1);
6961 else if (it->dpvec_char_len > 0)
6963 if (it->method == GET_FROM_STRING
6964 && it->n_overlay_strings > 0)
6965 it->ignore_overlay_strings_at_pos_p = 1;
6966 it->len = it->dpvec_char_len;
6967 set_iterator_to_next (it, reseat_p);
6970 /* Maybe recheck faces after display vector */
6971 if (recheck_faces)
6972 it->stop_charpos = IT_CHARPOS (*it);
6974 break;
6976 case GET_FROM_STRING:
6977 /* Current display element is a character from a Lisp string. */
6978 xassert (it->s == NULL && STRINGP (it->string));
6979 if (it->cmp_it.id >= 0)
6981 int i;
6983 if (! it->bidi_p)
6985 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6986 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6987 if (it->cmp_it.to < it->cmp_it.nglyphs)
6988 it->cmp_it.from = it->cmp_it.to;
6989 else
6991 it->cmp_it.id = -1;
6992 composition_compute_stop_pos (&it->cmp_it,
6993 IT_STRING_CHARPOS (*it),
6994 IT_STRING_BYTEPOS (*it),
6995 it->end_charpos, it->string);
6998 else if (! it->cmp_it.reversed_p)
7000 for (i = 0; i < it->cmp_it.nchars; i++)
7001 bidi_move_to_visually_next (&it->bidi_it);
7002 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7003 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7005 if (it->cmp_it.to < it->cmp_it.nglyphs)
7006 it->cmp_it.from = it->cmp_it.to;
7007 else
7009 EMACS_INT stop = it->end_charpos;
7010 if (it->bidi_it.scan_dir < 0)
7011 stop = -1;
7012 composition_compute_stop_pos (&it->cmp_it,
7013 IT_STRING_CHARPOS (*it),
7014 IT_STRING_BYTEPOS (*it), stop,
7015 it->string);
7018 else
7020 for (i = 0; i < it->cmp_it.nchars; i++)
7021 bidi_move_to_visually_next (&it->bidi_it);
7022 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7023 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7024 if (it->cmp_it.from > 0)
7025 it->cmp_it.to = it->cmp_it.from;
7026 else
7028 EMACS_INT stop = it->end_charpos;
7029 if (it->bidi_it.scan_dir < 0)
7030 stop = -1;
7031 composition_compute_stop_pos (&it->cmp_it,
7032 IT_STRING_CHARPOS (*it),
7033 IT_STRING_BYTEPOS (*it), stop,
7034 it->string);
7038 else
7040 if (!it->bidi_p
7041 /* If the string position is beyond string's end, it
7042 means next_element_from_string is padding the string
7043 with blanks, in which case we bypass the bidi
7044 iterator, because it cannot deal with such virtual
7045 characters. */
7046 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7048 IT_STRING_BYTEPOS (*it) += it->len;
7049 IT_STRING_CHARPOS (*it) += 1;
7051 else
7053 int prev_scan_dir = it->bidi_it.scan_dir;
7055 bidi_move_to_visually_next (&it->bidi_it);
7056 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7057 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7058 if (prev_scan_dir != it->bidi_it.scan_dir)
7060 EMACS_INT stop = it->end_charpos;
7062 if (it->bidi_it.scan_dir < 0)
7063 stop = -1;
7064 composition_compute_stop_pos (&it->cmp_it,
7065 IT_STRING_CHARPOS (*it),
7066 IT_STRING_BYTEPOS (*it), stop,
7067 it->string);
7072 consider_string_end:
7074 if (it->current.overlay_string_index >= 0)
7076 /* IT->string is an overlay string. Advance to the
7077 next, if there is one. */
7078 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7080 it->ellipsis_p = 0;
7081 next_overlay_string (it);
7082 if (it->ellipsis_p)
7083 setup_for_ellipsis (it, 0);
7086 else
7088 /* IT->string is not an overlay string. If we reached
7089 its end, and there is something on IT->stack, proceed
7090 with what is on the stack. This can be either another
7091 string, this time an overlay string, or a buffer. */
7092 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7093 && it->sp > 0)
7095 pop_it (it);
7096 if (it->method == GET_FROM_STRING)
7097 goto consider_string_end;
7100 break;
7102 case GET_FROM_IMAGE:
7103 case GET_FROM_STRETCH:
7104 /* The position etc with which we have to proceed are on
7105 the stack. The position may be at the end of a string,
7106 if the `display' property takes up the whole string. */
7107 xassert (it->sp > 0);
7108 pop_it (it);
7109 if (it->method == GET_FROM_STRING)
7110 goto consider_string_end;
7111 break;
7113 default:
7114 /* There are no other methods defined, so this should be a bug. */
7115 abort ();
7118 xassert (it->method != GET_FROM_STRING
7119 || (STRINGP (it->string)
7120 && IT_STRING_CHARPOS (*it) >= 0));
7123 /* Load IT's display element fields with information about the next
7124 display element which comes from a display table entry or from the
7125 result of translating a control character to one of the forms `^C'
7126 or `\003'.
7128 IT->dpvec holds the glyphs to return as characters.
7129 IT->saved_face_id holds the face id before the display vector--it
7130 is restored into IT->face_id in set_iterator_to_next. */
7132 static int
7133 next_element_from_display_vector (struct it *it)
7135 Lisp_Object gc;
7137 /* Precondition. */
7138 xassert (it->dpvec && it->current.dpvec_index >= 0);
7140 it->face_id = it->saved_face_id;
7142 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7143 That seemed totally bogus - so I changed it... */
7144 gc = it->dpvec[it->current.dpvec_index];
7146 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
7148 it->c = GLYPH_CODE_CHAR (gc);
7149 it->len = CHAR_BYTES (it->c);
7151 /* The entry may contain a face id to use. Such a face id is
7152 the id of a Lisp face, not a realized face. A face id of
7153 zero means no face is specified. */
7154 if (it->dpvec_face_id >= 0)
7155 it->face_id = it->dpvec_face_id;
7156 else
7158 EMACS_INT lface_id = GLYPH_CODE_FACE (gc);
7159 if (lface_id > 0)
7160 it->face_id = merge_faces (it->f, Qt, lface_id,
7161 it->saved_face_id);
7164 else
7165 /* Display table entry is invalid. Return a space. */
7166 it->c = ' ', it->len = 1;
7168 /* Don't change position and object of the iterator here. They are
7169 still the values of the character that had this display table
7170 entry or was translated, and that's what we want. */
7171 it->what = IT_CHARACTER;
7172 return 1;
7175 /* Get the first element of string/buffer in the visual order, after
7176 being reseated to a new position in a string or a buffer. */
7177 static void
7178 get_visually_first_element (struct it *it)
7180 int string_p = STRINGP (it->string) || it->s;
7181 EMACS_INT eob = (string_p ? it->bidi_it.string.schars : ZV);
7182 EMACS_INT bob = (string_p ? 0 : BEGV);
7184 if (STRINGP (it->string))
7186 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7187 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7189 else
7191 it->bidi_it.charpos = IT_CHARPOS (*it);
7192 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7195 if (it->bidi_it.charpos == eob)
7197 /* Nothing to do, but reset the FIRST_ELT flag, like
7198 bidi_paragraph_init does, because we are not going to
7199 call it. */
7200 it->bidi_it.first_elt = 0;
7202 else if (it->bidi_it.charpos == bob
7203 || (!string_p
7204 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7205 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7207 /* If we are at the beginning of a line/string, we can produce
7208 the next element right away. */
7209 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7210 bidi_move_to_visually_next (&it->bidi_it);
7212 else
7214 EMACS_INT orig_bytepos = it->bidi_it.bytepos;
7216 /* We need to prime the bidi iterator starting at the line's or
7217 string's beginning, before we will be able to produce the
7218 next element. */
7219 if (string_p)
7220 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7221 else
7223 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7224 -1);
7225 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7227 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7230 /* Now return to buffer/string position where we were asked
7231 to get the next display element, and produce that. */
7232 bidi_move_to_visually_next (&it->bidi_it);
7234 while (it->bidi_it.bytepos != orig_bytepos
7235 && it->bidi_it.charpos < eob);
7238 /* Adjust IT's position information to where we ended up. */
7239 if (STRINGP (it->string))
7241 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7242 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7244 else
7246 IT_CHARPOS (*it) = it->bidi_it.charpos;
7247 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7250 if (STRINGP (it->string) || !it->s)
7252 EMACS_INT stop, charpos, bytepos;
7254 if (STRINGP (it->string))
7256 xassert (!it->s);
7257 stop = SCHARS (it->string);
7258 if (stop > it->end_charpos)
7259 stop = it->end_charpos;
7260 charpos = IT_STRING_CHARPOS (*it);
7261 bytepos = IT_STRING_BYTEPOS (*it);
7263 else
7265 stop = it->end_charpos;
7266 charpos = IT_CHARPOS (*it);
7267 bytepos = IT_BYTEPOS (*it);
7269 if (it->bidi_it.scan_dir < 0)
7270 stop = -1;
7271 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7272 it->string);
7276 /* Load IT with the next display element from Lisp string IT->string.
7277 IT->current.string_pos is the current position within the string.
7278 If IT->current.overlay_string_index >= 0, the Lisp string is an
7279 overlay string. */
7281 static int
7282 next_element_from_string (struct it *it)
7284 struct text_pos position;
7286 xassert (STRINGP (it->string));
7287 xassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7288 xassert (IT_STRING_CHARPOS (*it) >= 0);
7289 position = it->current.string_pos;
7291 /* With bidi reordering, the character to display might not be the
7292 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7293 that we were reseat()ed to a new string, whose paragraph
7294 direction is not known. */
7295 if (it->bidi_p && it->bidi_it.first_elt)
7297 get_visually_first_element (it);
7298 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7301 /* Time to check for invisible text? */
7302 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7304 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7306 if (!(!it->bidi_p
7307 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7308 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7310 /* With bidi non-linear iteration, we could find
7311 ourselves far beyond the last computed stop_charpos,
7312 with several other stop positions in between that we
7313 missed. Scan them all now, in buffer's logical
7314 order, until we find and handle the last stop_charpos
7315 that precedes our current position. */
7316 handle_stop_backwards (it, it->stop_charpos);
7317 return GET_NEXT_DISPLAY_ELEMENT (it);
7319 else
7321 if (it->bidi_p)
7323 /* Take note of the stop position we just moved
7324 across, for when we will move back across it. */
7325 it->prev_stop = it->stop_charpos;
7326 /* If we are at base paragraph embedding level, take
7327 note of the last stop position seen at this
7328 level. */
7329 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7330 it->base_level_stop = it->stop_charpos;
7332 handle_stop (it);
7334 /* Since a handler may have changed IT->method, we must
7335 recurse here. */
7336 return GET_NEXT_DISPLAY_ELEMENT (it);
7339 else if (it->bidi_p
7340 /* If we are before prev_stop, we may have overstepped
7341 on our way backwards a stop_pos, and if so, we need
7342 to handle that stop_pos. */
7343 && IT_STRING_CHARPOS (*it) < it->prev_stop
7344 /* We can sometimes back up for reasons that have nothing
7345 to do with bidi reordering. E.g., compositions. The
7346 code below is only needed when we are above the base
7347 embedding level, so test for that explicitly. */
7348 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7350 /* If we lost track of base_level_stop, we have no better
7351 place for handle_stop_backwards to start from than string
7352 beginning. This happens, e.g., when we were reseated to
7353 the previous screenful of text by vertical-motion. */
7354 if (it->base_level_stop <= 0
7355 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7356 it->base_level_stop = 0;
7357 handle_stop_backwards (it, it->base_level_stop);
7358 return GET_NEXT_DISPLAY_ELEMENT (it);
7362 if (it->current.overlay_string_index >= 0)
7364 /* Get the next character from an overlay string. In overlay
7365 strings, there is no field width or padding with spaces to
7366 do. */
7367 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7369 it->what = IT_EOB;
7370 return 0;
7372 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7373 IT_STRING_BYTEPOS (*it),
7374 it->bidi_it.scan_dir < 0
7375 ? -1
7376 : SCHARS (it->string))
7377 && next_element_from_composition (it))
7379 return 1;
7381 else if (STRING_MULTIBYTE (it->string))
7383 const unsigned char *s = (SDATA (it->string)
7384 + IT_STRING_BYTEPOS (*it));
7385 it->c = string_char_and_length (s, &it->len);
7387 else
7389 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7390 it->len = 1;
7393 else
7395 /* Get the next character from a Lisp string that is not an
7396 overlay string. Such strings come from the mode line, for
7397 example. We may have to pad with spaces, or truncate the
7398 string. See also next_element_from_c_string. */
7399 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7401 it->what = IT_EOB;
7402 return 0;
7404 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7406 /* Pad with spaces. */
7407 it->c = ' ', it->len = 1;
7408 CHARPOS (position) = BYTEPOS (position) = -1;
7410 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7411 IT_STRING_BYTEPOS (*it),
7412 it->bidi_it.scan_dir < 0
7413 ? -1
7414 : it->string_nchars)
7415 && next_element_from_composition (it))
7417 return 1;
7419 else if (STRING_MULTIBYTE (it->string))
7421 const unsigned char *s = (SDATA (it->string)
7422 + IT_STRING_BYTEPOS (*it));
7423 it->c = string_char_and_length (s, &it->len);
7425 else
7427 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7428 it->len = 1;
7432 /* Record what we have and where it came from. */
7433 it->what = IT_CHARACTER;
7434 it->object = it->string;
7435 it->position = position;
7436 return 1;
7440 /* Load IT with next display element from C string IT->s.
7441 IT->string_nchars is the maximum number of characters to return
7442 from the string. IT->end_charpos may be greater than
7443 IT->string_nchars when this function is called, in which case we
7444 may have to return padding spaces. Value is zero if end of string
7445 reached, including padding spaces. */
7447 static int
7448 next_element_from_c_string (struct it *it)
7450 int success_p = 1;
7452 xassert (it->s);
7453 xassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7454 it->what = IT_CHARACTER;
7455 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7456 it->object = Qnil;
7458 /* With bidi reordering, the character to display might not be the
7459 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7460 we were reseated to a new string, whose paragraph direction is
7461 not known. */
7462 if (it->bidi_p && it->bidi_it.first_elt)
7463 get_visually_first_element (it);
7465 /* IT's position can be greater than IT->string_nchars in case a
7466 field width or precision has been specified when the iterator was
7467 initialized. */
7468 if (IT_CHARPOS (*it) >= it->end_charpos)
7470 /* End of the game. */
7471 it->what = IT_EOB;
7472 success_p = 0;
7474 else if (IT_CHARPOS (*it) >= it->string_nchars)
7476 /* Pad with spaces. */
7477 it->c = ' ', it->len = 1;
7478 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7480 else if (it->multibyte_p)
7481 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7482 else
7483 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7485 return success_p;
7489 /* Set up IT to return characters from an ellipsis, if appropriate.
7490 The definition of the ellipsis glyphs may come from a display table
7491 entry. This function fills IT with the first glyph from the
7492 ellipsis if an ellipsis is to be displayed. */
7494 static int
7495 next_element_from_ellipsis (struct it *it)
7497 if (it->selective_display_ellipsis_p)
7498 setup_for_ellipsis (it, it->len);
7499 else
7501 /* The face at the current position may be different from the
7502 face we find after the invisible text. Remember what it
7503 was in IT->saved_face_id, and signal that it's there by
7504 setting face_before_selective_p. */
7505 it->saved_face_id = it->face_id;
7506 it->method = GET_FROM_BUFFER;
7507 it->object = it->w->buffer;
7508 reseat_at_next_visible_line_start (it, 1);
7509 it->face_before_selective_p = 1;
7512 return GET_NEXT_DISPLAY_ELEMENT (it);
7516 /* Deliver an image display element. The iterator IT is already
7517 filled with image information (done in handle_display_prop). Value
7518 is always 1. */
7521 static int
7522 next_element_from_image (struct it *it)
7524 it->what = IT_IMAGE;
7525 it->ignore_overlay_strings_at_pos_p = 0;
7526 return 1;
7530 /* Fill iterator IT with next display element from a stretch glyph
7531 property. IT->object is the value of the text property. Value is
7532 always 1. */
7534 static int
7535 next_element_from_stretch (struct it *it)
7537 it->what = IT_STRETCH;
7538 return 1;
7541 /* Scan backwards from IT's current position until we find a stop
7542 position, or until BEGV. This is called when we find ourself
7543 before both the last known prev_stop and base_level_stop while
7544 reordering bidirectional text. */
7546 static void
7547 compute_stop_pos_backwards (struct it *it)
7549 const int SCAN_BACK_LIMIT = 1000;
7550 struct text_pos pos;
7551 struct display_pos save_current = it->current;
7552 struct text_pos save_position = it->position;
7553 EMACS_INT charpos = IT_CHARPOS (*it);
7554 EMACS_INT where_we_are = charpos;
7555 EMACS_INT save_stop_pos = it->stop_charpos;
7556 EMACS_INT save_end_pos = it->end_charpos;
7558 xassert (NILP (it->string) && !it->s);
7559 xassert (it->bidi_p);
7560 it->bidi_p = 0;
7563 it->end_charpos = min (charpos + 1, ZV);
7564 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7565 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7566 reseat_1 (it, pos, 0);
7567 compute_stop_pos (it);
7568 /* We must advance forward, right? */
7569 if (it->stop_charpos <= charpos)
7570 abort ();
7572 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7574 if (it->stop_charpos <= where_we_are)
7575 it->prev_stop = it->stop_charpos;
7576 else
7577 it->prev_stop = BEGV;
7578 it->bidi_p = 1;
7579 it->current = save_current;
7580 it->position = save_position;
7581 it->stop_charpos = save_stop_pos;
7582 it->end_charpos = save_end_pos;
7585 /* Scan forward from CHARPOS in the current buffer/string, until we
7586 find a stop position > current IT's position. Then handle the stop
7587 position before that. This is called when we bump into a stop
7588 position while reordering bidirectional text. CHARPOS should be
7589 the last previously processed stop_pos (or BEGV/0, if none were
7590 processed yet) whose position is less that IT's current
7591 position. */
7593 static void
7594 handle_stop_backwards (struct it *it, EMACS_INT charpos)
7596 int bufp = !STRINGP (it->string);
7597 EMACS_INT where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7598 struct display_pos save_current = it->current;
7599 struct text_pos save_position = it->position;
7600 struct text_pos pos1;
7601 EMACS_INT next_stop;
7603 /* Scan in strict logical order. */
7604 xassert (it->bidi_p);
7605 it->bidi_p = 0;
7608 it->prev_stop = charpos;
7609 if (bufp)
7611 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7612 reseat_1 (it, pos1, 0);
7614 else
7615 it->current.string_pos = string_pos (charpos, it->string);
7616 compute_stop_pos (it);
7617 /* We must advance forward, right? */
7618 if (it->stop_charpos <= it->prev_stop)
7619 abort ();
7620 charpos = it->stop_charpos;
7622 while (charpos <= where_we_are);
7624 it->bidi_p = 1;
7625 it->current = save_current;
7626 it->position = save_position;
7627 next_stop = it->stop_charpos;
7628 it->stop_charpos = it->prev_stop;
7629 handle_stop (it);
7630 it->stop_charpos = next_stop;
7633 /* Load IT with the next display element from current_buffer. Value
7634 is zero if end of buffer reached. IT->stop_charpos is the next
7635 position at which to stop and check for text properties or buffer
7636 end. */
7638 static int
7639 next_element_from_buffer (struct it *it)
7641 int success_p = 1;
7643 xassert (IT_CHARPOS (*it) >= BEGV);
7644 xassert (NILP (it->string) && !it->s);
7645 xassert (!it->bidi_p
7646 || (EQ (it->bidi_it.string.lstring, Qnil)
7647 && it->bidi_it.string.s == NULL));
7649 /* With bidi reordering, the character to display might not be the
7650 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7651 we were reseat()ed to a new buffer position, which is potentially
7652 a different paragraph. */
7653 if (it->bidi_p && it->bidi_it.first_elt)
7655 get_visually_first_element (it);
7656 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7659 if (IT_CHARPOS (*it) >= it->stop_charpos)
7661 if (IT_CHARPOS (*it) >= it->end_charpos)
7663 int overlay_strings_follow_p;
7665 /* End of the game, except when overlay strings follow that
7666 haven't been returned yet. */
7667 if (it->overlay_strings_at_end_processed_p)
7668 overlay_strings_follow_p = 0;
7669 else
7671 it->overlay_strings_at_end_processed_p = 1;
7672 overlay_strings_follow_p = get_overlay_strings (it, 0);
7675 if (overlay_strings_follow_p)
7676 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7677 else
7679 it->what = IT_EOB;
7680 it->position = it->current.pos;
7681 success_p = 0;
7684 else if (!(!it->bidi_p
7685 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7686 || IT_CHARPOS (*it) == it->stop_charpos))
7688 /* With bidi non-linear iteration, we could find ourselves
7689 far beyond the last computed stop_charpos, with several
7690 other stop positions in between that we missed. Scan
7691 them all now, in buffer's logical order, until we find
7692 and handle the last stop_charpos that precedes our
7693 current position. */
7694 handle_stop_backwards (it, it->stop_charpos);
7695 return GET_NEXT_DISPLAY_ELEMENT (it);
7697 else
7699 if (it->bidi_p)
7701 /* Take note of the stop position we just moved across,
7702 for when we will move back across it. */
7703 it->prev_stop = it->stop_charpos;
7704 /* If we are at base paragraph embedding level, take
7705 note of the last stop position seen at this
7706 level. */
7707 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7708 it->base_level_stop = it->stop_charpos;
7710 handle_stop (it);
7711 return GET_NEXT_DISPLAY_ELEMENT (it);
7714 else if (it->bidi_p
7715 /* If we are before prev_stop, we may have overstepped on
7716 our way backwards a stop_pos, and if so, we need to
7717 handle that stop_pos. */
7718 && IT_CHARPOS (*it) < it->prev_stop
7719 /* We can sometimes back up for reasons that have nothing
7720 to do with bidi reordering. E.g., compositions. The
7721 code below is only needed when we are above the base
7722 embedding level, so test for that explicitly. */
7723 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7725 if (it->base_level_stop <= 0
7726 || IT_CHARPOS (*it) < it->base_level_stop)
7728 /* If we lost track of base_level_stop, we need to find
7729 prev_stop by looking backwards. This happens, e.g., when
7730 we were reseated to the previous screenful of text by
7731 vertical-motion. */
7732 it->base_level_stop = BEGV;
7733 compute_stop_pos_backwards (it);
7734 handle_stop_backwards (it, it->prev_stop);
7736 else
7737 handle_stop_backwards (it, it->base_level_stop);
7738 return GET_NEXT_DISPLAY_ELEMENT (it);
7740 else
7742 /* No face changes, overlays etc. in sight, so just return a
7743 character from current_buffer. */
7744 unsigned char *p;
7745 EMACS_INT stop;
7747 /* Maybe run the redisplay end trigger hook. Performance note:
7748 This doesn't seem to cost measurable time. */
7749 if (it->redisplay_end_trigger_charpos
7750 && it->glyph_row
7751 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7752 run_redisplay_end_trigger_hook (it);
7754 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7755 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7756 stop)
7757 && next_element_from_composition (it))
7759 return 1;
7762 /* Get the next character, maybe multibyte. */
7763 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7764 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7765 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7766 else
7767 it->c = *p, it->len = 1;
7769 /* Record what we have and where it came from. */
7770 it->what = IT_CHARACTER;
7771 it->object = it->w->buffer;
7772 it->position = it->current.pos;
7774 /* Normally we return the character found above, except when we
7775 really want to return an ellipsis for selective display. */
7776 if (it->selective)
7778 if (it->c == '\n')
7780 /* A value of selective > 0 means hide lines indented more
7781 than that number of columns. */
7782 if (it->selective > 0
7783 && IT_CHARPOS (*it) + 1 < ZV
7784 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7785 IT_BYTEPOS (*it) + 1,
7786 it->selective))
7788 success_p = next_element_from_ellipsis (it);
7789 it->dpvec_char_len = -1;
7792 else if (it->c == '\r' && it->selective == -1)
7794 /* A value of selective == -1 means that everything from the
7795 CR to the end of the line is invisible, with maybe an
7796 ellipsis displayed for it. */
7797 success_p = next_element_from_ellipsis (it);
7798 it->dpvec_char_len = -1;
7803 /* Value is zero if end of buffer reached. */
7804 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7805 return success_p;
7809 /* Run the redisplay end trigger hook for IT. */
7811 static void
7812 run_redisplay_end_trigger_hook (struct it *it)
7814 Lisp_Object args[3];
7816 /* IT->glyph_row should be non-null, i.e. we should be actually
7817 displaying something, or otherwise we should not run the hook. */
7818 xassert (it->glyph_row);
7820 /* Set up hook arguments. */
7821 args[0] = Qredisplay_end_trigger_functions;
7822 args[1] = it->window;
7823 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7824 it->redisplay_end_trigger_charpos = 0;
7826 /* Since we are *trying* to run these functions, don't try to run
7827 them again, even if they get an error. */
7828 it->w->redisplay_end_trigger = Qnil;
7829 Frun_hook_with_args (3, args);
7831 /* Notice if it changed the face of the character we are on. */
7832 handle_face_prop (it);
7836 /* Deliver a composition display element. Unlike the other
7837 next_element_from_XXX, this function is not registered in the array
7838 get_next_element[]. It is called from next_element_from_buffer and
7839 next_element_from_string when necessary. */
7841 static int
7842 next_element_from_composition (struct it *it)
7844 it->what = IT_COMPOSITION;
7845 it->len = it->cmp_it.nbytes;
7846 if (STRINGP (it->string))
7848 if (it->c < 0)
7850 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7851 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7852 return 0;
7854 it->position = it->current.string_pos;
7855 it->object = it->string;
7856 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7857 IT_STRING_BYTEPOS (*it), it->string);
7859 else
7861 if (it->c < 0)
7863 IT_CHARPOS (*it) += it->cmp_it.nchars;
7864 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7865 if (it->bidi_p)
7867 if (it->bidi_it.new_paragraph)
7868 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7869 /* Resync the bidi iterator with IT's new position.
7870 FIXME: this doesn't support bidirectional text. */
7871 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7872 bidi_move_to_visually_next (&it->bidi_it);
7874 return 0;
7876 it->position = it->current.pos;
7877 it->object = it->w->buffer;
7878 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7879 IT_BYTEPOS (*it), Qnil);
7881 return 1;
7886 /***********************************************************************
7887 Moving an iterator without producing glyphs
7888 ***********************************************************************/
7890 /* Check if iterator is at a position corresponding to a valid buffer
7891 position after some move_it_ call. */
7893 #define IT_POS_VALID_AFTER_MOVE_P(it) \
7894 ((it)->method == GET_FROM_STRING \
7895 ? IT_STRING_CHARPOS (*it) == 0 \
7896 : 1)
7899 /* Move iterator IT to a specified buffer or X position within one
7900 line on the display without producing glyphs.
7902 OP should be a bit mask including some or all of these bits:
7903 MOVE_TO_X: Stop upon reaching x-position TO_X.
7904 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
7905 Regardless of OP's value, stop upon reaching the end of the display line.
7907 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
7908 This means, in particular, that TO_X includes window's horizontal
7909 scroll amount.
7911 The return value has several possible values that
7912 say what condition caused the scan to stop:
7914 MOVE_POS_MATCH_OR_ZV
7915 - when TO_POS or ZV was reached.
7917 MOVE_X_REACHED
7918 -when TO_X was reached before TO_POS or ZV were reached.
7920 MOVE_LINE_CONTINUED
7921 - when we reached the end of the display area and the line must
7922 be continued.
7924 MOVE_LINE_TRUNCATED
7925 - when we reached the end of the display area and the line is
7926 truncated.
7928 MOVE_NEWLINE_OR_CR
7929 - when we stopped at a line end, i.e. a newline or a CR and selective
7930 display is on. */
7932 static enum move_it_result
7933 move_it_in_display_line_to (struct it *it,
7934 EMACS_INT to_charpos, int to_x,
7935 enum move_operation_enum op)
7937 enum move_it_result result = MOVE_UNDEFINED;
7938 struct glyph_row *saved_glyph_row;
7939 struct it wrap_it, atpos_it, atx_it, ppos_it;
7940 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
7941 void *ppos_data = NULL;
7942 int may_wrap = 0;
7943 enum it_method prev_method = it->method;
7944 EMACS_INT prev_pos = IT_CHARPOS (*it);
7945 int saw_smaller_pos = prev_pos < to_charpos;
7947 /* Don't produce glyphs in produce_glyphs. */
7948 saved_glyph_row = it->glyph_row;
7949 it->glyph_row = NULL;
7951 /* Use wrap_it to save a copy of IT wherever a word wrap could
7952 occur. Use atpos_it to save a copy of IT at the desired buffer
7953 position, if found, so that we can scan ahead and check if the
7954 word later overshoots the window edge. Use atx_it similarly, for
7955 pixel positions. */
7956 wrap_it.sp = -1;
7957 atpos_it.sp = -1;
7958 atx_it.sp = -1;
7960 /* Use ppos_it under bidi reordering to save a copy of IT for the
7961 position > CHARPOS that is the closest to CHARPOS. We restore
7962 that position in IT when we have scanned the entire display line
7963 without finding a match for CHARPOS and all the character
7964 positions are greater than CHARPOS. */
7965 if (it->bidi_p)
7967 SAVE_IT (ppos_it, *it, ppos_data);
7968 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
7969 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
7970 SAVE_IT (ppos_it, *it, ppos_data);
7973 #define BUFFER_POS_REACHED_P() \
7974 ((op & MOVE_TO_POS) != 0 \
7975 && BUFFERP (it->object) \
7976 && (IT_CHARPOS (*it) == to_charpos \
7977 || ((!it->bidi_p \
7978 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
7979 && IT_CHARPOS (*it) > to_charpos) \
7980 || (it->what == IT_COMPOSITION \
7981 && ((IT_CHARPOS (*it) > to_charpos \
7982 && to_charpos >= it->cmp_it.charpos) \
7983 || (IT_CHARPOS (*it) < to_charpos \
7984 && to_charpos <= it->cmp_it.charpos)))) \
7985 && (it->method == GET_FROM_BUFFER \
7986 || (it->method == GET_FROM_DISPLAY_VECTOR \
7987 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
7989 /* If there's a line-/wrap-prefix, handle it. */
7990 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7991 && it->current_y < it->last_visible_y)
7992 handle_line_prefix (it);
7994 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7995 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7997 while (1)
7999 int x, i, ascent = 0, descent = 0;
8001 /* Utility macro to reset an iterator with x, ascent, and descent. */
8002 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8003 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8004 (IT)->max_descent = descent)
8006 /* Stop if we move beyond TO_CHARPOS (after an image or a
8007 display string or stretch glyph). */
8008 if ((op & MOVE_TO_POS) != 0
8009 && BUFFERP (it->object)
8010 && it->method == GET_FROM_BUFFER
8011 && (((!it->bidi_p
8012 /* When the iterator is at base embedding level, we
8013 are guaranteed that characters are delivered for
8014 display in strictly increasing order of their
8015 buffer positions. */
8016 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8017 && IT_CHARPOS (*it) > to_charpos)
8018 || (it->bidi_p
8019 && (prev_method == GET_FROM_IMAGE
8020 || prev_method == GET_FROM_STRETCH
8021 || prev_method == GET_FROM_STRING)
8022 /* Passed TO_CHARPOS from left to right. */
8023 && ((prev_pos < to_charpos
8024 && IT_CHARPOS (*it) > to_charpos)
8025 /* Passed TO_CHARPOS from right to left. */
8026 || (prev_pos > to_charpos
8027 && IT_CHARPOS (*it) < to_charpos)))))
8029 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8031 result = MOVE_POS_MATCH_OR_ZV;
8032 break;
8034 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8035 /* If wrap_it is valid, the current position might be in a
8036 word that is wrapped. So, save the iterator in
8037 atpos_it and continue to see if wrapping happens. */
8038 SAVE_IT (atpos_it, *it, atpos_data);
8041 /* Stop when ZV reached.
8042 We used to stop here when TO_CHARPOS reached as well, but that is
8043 too soon if this glyph does not fit on this line. So we handle it
8044 explicitly below. */
8045 if (!get_next_display_element (it))
8047 result = MOVE_POS_MATCH_OR_ZV;
8048 break;
8051 if (it->line_wrap == TRUNCATE)
8053 if (BUFFER_POS_REACHED_P ())
8055 result = MOVE_POS_MATCH_OR_ZV;
8056 break;
8059 else
8061 if (it->line_wrap == WORD_WRAP)
8063 if (IT_DISPLAYING_WHITESPACE (it))
8064 may_wrap = 1;
8065 else if (may_wrap)
8067 /* We have reached a glyph that follows one or more
8068 whitespace characters. If the position is
8069 already found, we are done. */
8070 if (atpos_it.sp >= 0)
8072 RESTORE_IT (it, &atpos_it, atpos_data);
8073 result = MOVE_POS_MATCH_OR_ZV;
8074 goto done;
8076 if (atx_it.sp >= 0)
8078 RESTORE_IT (it, &atx_it, atx_data);
8079 result = MOVE_X_REACHED;
8080 goto done;
8082 /* Otherwise, we can wrap here. */
8083 SAVE_IT (wrap_it, *it, wrap_data);
8084 may_wrap = 0;
8089 /* Remember the line height for the current line, in case
8090 the next element doesn't fit on the line. */
8091 ascent = it->max_ascent;
8092 descent = it->max_descent;
8094 /* The call to produce_glyphs will get the metrics of the
8095 display element IT is loaded with. Record the x-position
8096 before this display element, in case it doesn't fit on the
8097 line. */
8098 x = it->current_x;
8100 PRODUCE_GLYPHS (it);
8102 if (it->area != TEXT_AREA)
8104 prev_method = it->method;
8105 if (it->method == GET_FROM_BUFFER)
8106 prev_pos = IT_CHARPOS (*it);
8107 set_iterator_to_next (it, 1);
8108 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8109 SET_TEXT_POS (this_line_min_pos,
8110 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8111 if (it->bidi_p
8112 && (op & MOVE_TO_POS)
8113 && IT_CHARPOS (*it) > to_charpos
8114 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8115 SAVE_IT (ppos_it, *it, ppos_data);
8116 continue;
8119 /* The number of glyphs we get back in IT->nglyphs will normally
8120 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8121 character on a terminal frame, or (iii) a line end. For the
8122 second case, IT->nglyphs - 1 padding glyphs will be present.
8123 (On X frames, there is only one glyph produced for a
8124 composite character.)
8126 The behavior implemented below means, for continuation lines,
8127 that as many spaces of a TAB as fit on the current line are
8128 displayed there. For terminal frames, as many glyphs of a
8129 multi-glyph character are displayed in the current line, too.
8130 This is what the old redisplay code did, and we keep it that
8131 way. Under X, the whole shape of a complex character must
8132 fit on the line or it will be completely displayed in the
8133 next line.
8135 Note that both for tabs and padding glyphs, all glyphs have
8136 the same width. */
8137 if (it->nglyphs)
8139 /* More than one glyph or glyph doesn't fit on line. All
8140 glyphs have the same width. */
8141 int single_glyph_width = it->pixel_width / it->nglyphs;
8142 int new_x;
8143 int x_before_this_char = x;
8144 int hpos_before_this_char = it->hpos;
8146 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8148 new_x = x + single_glyph_width;
8150 /* We want to leave anything reaching TO_X to the caller. */
8151 if ((op & MOVE_TO_X) && new_x > to_x)
8153 if (BUFFER_POS_REACHED_P ())
8155 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8156 goto buffer_pos_reached;
8157 if (atpos_it.sp < 0)
8159 SAVE_IT (atpos_it, *it, atpos_data);
8160 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8163 else
8165 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8167 it->current_x = x;
8168 result = MOVE_X_REACHED;
8169 break;
8171 if (atx_it.sp < 0)
8173 SAVE_IT (atx_it, *it, atx_data);
8174 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8179 if (/* Lines are continued. */
8180 it->line_wrap != TRUNCATE
8181 && (/* And glyph doesn't fit on the line. */
8182 new_x > it->last_visible_x
8183 /* Or it fits exactly and we're on a window
8184 system frame. */
8185 || (new_x == it->last_visible_x
8186 && FRAME_WINDOW_P (it->f))))
8188 if (/* IT->hpos == 0 means the very first glyph
8189 doesn't fit on the line, e.g. a wide image. */
8190 it->hpos == 0
8191 || (new_x == it->last_visible_x
8192 && FRAME_WINDOW_P (it->f)))
8194 ++it->hpos;
8195 it->current_x = new_x;
8197 /* The character's last glyph just barely fits
8198 in this row. */
8199 if (i == it->nglyphs - 1)
8201 /* If this is the destination position,
8202 return a position *before* it in this row,
8203 now that we know it fits in this row. */
8204 if (BUFFER_POS_REACHED_P ())
8206 if (it->line_wrap != WORD_WRAP
8207 || wrap_it.sp < 0)
8209 it->hpos = hpos_before_this_char;
8210 it->current_x = x_before_this_char;
8211 result = MOVE_POS_MATCH_OR_ZV;
8212 break;
8214 if (it->line_wrap == WORD_WRAP
8215 && atpos_it.sp < 0)
8217 SAVE_IT (atpos_it, *it, atpos_data);
8218 atpos_it.current_x = x_before_this_char;
8219 atpos_it.hpos = hpos_before_this_char;
8223 prev_method = it->method;
8224 if (it->method == GET_FROM_BUFFER)
8225 prev_pos = IT_CHARPOS (*it);
8226 set_iterator_to_next (it, 1);
8227 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8228 SET_TEXT_POS (this_line_min_pos,
8229 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8230 /* On graphical terminals, newlines may
8231 "overflow" into the fringe if
8232 overflow-newline-into-fringe is non-nil.
8233 On text-only terminals, newlines may
8234 overflow into the last glyph on the
8235 display line.*/
8236 if (!FRAME_WINDOW_P (it->f)
8237 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8239 if (!get_next_display_element (it))
8241 result = MOVE_POS_MATCH_OR_ZV;
8242 break;
8244 if (BUFFER_POS_REACHED_P ())
8246 if (ITERATOR_AT_END_OF_LINE_P (it))
8247 result = MOVE_POS_MATCH_OR_ZV;
8248 else
8249 result = MOVE_LINE_CONTINUED;
8250 break;
8252 if (ITERATOR_AT_END_OF_LINE_P (it))
8254 result = MOVE_NEWLINE_OR_CR;
8255 break;
8260 else
8261 IT_RESET_X_ASCENT_DESCENT (it);
8263 if (wrap_it.sp >= 0)
8265 RESTORE_IT (it, &wrap_it, wrap_data);
8266 atpos_it.sp = -1;
8267 atx_it.sp = -1;
8270 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8271 IT_CHARPOS (*it)));
8272 result = MOVE_LINE_CONTINUED;
8273 break;
8276 if (BUFFER_POS_REACHED_P ())
8278 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8279 goto buffer_pos_reached;
8280 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8282 SAVE_IT (atpos_it, *it, atpos_data);
8283 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8287 if (new_x > it->first_visible_x)
8289 /* Glyph is visible. Increment number of glyphs that
8290 would be displayed. */
8291 ++it->hpos;
8295 if (result != MOVE_UNDEFINED)
8296 break;
8298 else if (BUFFER_POS_REACHED_P ())
8300 buffer_pos_reached:
8301 IT_RESET_X_ASCENT_DESCENT (it);
8302 result = MOVE_POS_MATCH_OR_ZV;
8303 break;
8305 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8307 /* Stop when TO_X specified and reached. This check is
8308 necessary here because of lines consisting of a line end,
8309 only. The line end will not produce any glyphs and we
8310 would never get MOVE_X_REACHED. */
8311 xassert (it->nglyphs == 0);
8312 result = MOVE_X_REACHED;
8313 break;
8316 /* Is this a line end? If yes, we're done. */
8317 if (ITERATOR_AT_END_OF_LINE_P (it))
8319 /* If we are past TO_CHARPOS, but never saw any character
8320 positions smaller than TO_CHARPOS, return
8321 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8322 did. */
8323 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8325 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8327 if (IT_CHARPOS (ppos_it) < ZV)
8329 RESTORE_IT (it, &ppos_it, ppos_data);
8330 result = MOVE_POS_MATCH_OR_ZV;
8332 else
8333 goto buffer_pos_reached;
8335 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8336 && IT_CHARPOS (*it) > to_charpos)
8337 goto buffer_pos_reached;
8338 else
8339 result = MOVE_NEWLINE_OR_CR;
8341 else
8342 result = MOVE_NEWLINE_OR_CR;
8343 break;
8346 prev_method = it->method;
8347 if (it->method == GET_FROM_BUFFER)
8348 prev_pos = IT_CHARPOS (*it);
8349 /* The current display element has been consumed. Advance
8350 to the next. */
8351 set_iterator_to_next (it, 1);
8352 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8353 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8354 if (IT_CHARPOS (*it) < to_charpos)
8355 saw_smaller_pos = 1;
8356 if (it->bidi_p
8357 && (op & MOVE_TO_POS)
8358 && IT_CHARPOS (*it) >= to_charpos
8359 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8360 SAVE_IT (ppos_it, *it, ppos_data);
8362 /* Stop if lines are truncated and IT's current x-position is
8363 past the right edge of the window now. */
8364 if (it->line_wrap == TRUNCATE
8365 && it->current_x >= it->last_visible_x)
8367 if (!FRAME_WINDOW_P (it->f)
8368 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8370 int at_eob_p = 0;
8372 if ((at_eob_p = !get_next_display_element (it))
8373 || BUFFER_POS_REACHED_P ()
8374 /* If we are past TO_CHARPOS, but never saw any
8375 character positions smaller than TO_CHARPOS,
8376 return MOVE_POS_MATCH_OR_ZV, like the
8377 unidirectional display did. */
8378 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8379 && !saw_smaller_pos
8380 && IT_CHARPOS (*it) > to_charpos))
8382 if (it->bidi_p
8383 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8384 RESTORE_IT (it, &ppos_it, ppos_data);
8385 result = MOVE_POS_MATCH_OR_ZV;
8386 break;
8388 if (ITERATOR_AT_END_OF_LINE_P (it))
8390 result = MOVE_NEWLINE_OR_CR;
8391 break;
8394 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8395 && !saw_smaller_pos
8396 && IT_CHARPOS (*it) > to_charpos)
8398 if (IT_CHARPOS (ppos_it) < ZV)
8399 RESTORE_IT (it, &ppos_it, ppos_data);
8400 result = MOVE_POS_MATCH_OR_ZV;
8401 break;
8403 result = MOVE_LINE_TRUNCATED;
8404 break;
8406 #undef IT_RESET_X_ASCENT_DESCENT
8409 #undef BUFFER_POS_REACHED_P
8411 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8412 restore the saved iterator. */
8413 if (atpos_it.sp >= 0)
8414 RESTORE_IT (it, &atpos_it, atpos_data);
8415 else if (atx_it.sp >= 0)
8416 RESTORE_IT (it, &atx_it, atx_data);
8418 done:
8420 if (atpos_data)
8421 bidi_unshelve_cache (atpos_data, 1);
8422 if (atx_data)
8423 bidi_unshelve_cache (atx_data, 1);
8424 if (wrap_data)
8425 bidi_unshelve_cache (wrap_data, 1);
8426 if (ppos_data)
8427 bidi_unshelve_cache (ppos_data, 1);
8429 /* Restore the iterator settings altered at the beginning of this
8430 function. */
8431 it->glyph_row = saved_glyph_row;
8432 return result;
8435 /* For external use. */
8436 void
8437 move_it_in_display_line (struct it *it,
8438 EMACS_INT to_charpos, int to_x,
8439 enum move_operation_enum op)
8441 if (it->line_wrap == WORD_WRAP
8442 && (op & MOVE_TO_X))
8444 struct it save_it;
8445 void *save_data = NULL;
8446 int skip;
8448 SAVE_IT (save_it, *it, save_data);
8449 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8450 /* When word-wrap is on, TO_X may lie past the end
8451 of a wrapped line. Then it->current is the
8452 character on the next line, so backtrack to the
8453 space before the wrap point. */
8454 if (skip == MOVE_LINE_CONTINUED)
8456 int prev_x = max (it->current_x - 1, 0);
8457 RESTORE_IT (it, &save_it, save_data);
8458 move_it_in_display_line_to
8459 (it, -1, prev_x, MOVE_TO_X);
8461 else
8462 bidi_unshelve_cache (save_data, 1);
8464 else
8465 move_it_in_display_line_to (it, to_charpos, to_x, op);
8469 /* Move IT forward until it satisfies one or more of the criteria in
8470 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8472 OP is a bit-mask that specifies where to stop, and in particular,
8473 which of those four position arguments makes a difference. See the
8474 description of enum move_operation_enum.
8476 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8477 screen line, this function will set IT to the next position that is
8478 displayed to the right of TO_CHARPOS on the screen. */
8480 void
8481 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
8483 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8484 int line_height, line_start_x = 0, reached = 0;
8485 void *backup_data = NULL;
8487 for (;;)
8489 if (op & MOVE_TO_VPOS)
8491 /* If no TO_CHARPOS and no TO_X specified, stop at the
8492 start of the line TO_VPOS. */
8493 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8495 if (it->vpos == to_vpos)
8497 reached = 1;
8498 break;
8500 else
8501 skip = move_it_in_display_line_to (it, -1, -1, 0);
8503 else
8505 /* TO_VPOS >= 0 means stop at TO_X in the line at
8506 TO_VPOS, or at TO_POS, whichever comes first. */
8507 if (it->vpos == to_vpos)
8509 reached = 2;
8510 break;
8513 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8515 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8517 reached = 3;
8518 break;
8520 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8522 /* We have reached TO_X but not in the line we want. */
8523 skip = move_it_in_display_line_to (it, to_charpos,
8524 -1, MOVE_TO_POS);
8525 if (skip == MOVE_POS_MATCH_OR_ZV)
8527 reached = 4;
8528 break;
8533 else if (op & MOVE_TO_Y)
8535 struct it it_backup;
8537 if (it->line_wrap == WORD_WRAP)
8538 SAVE_IT (it_backup, *it, backup_data);
8540 /* TO_Y specified means stop at TO_X in the line containing
8541 TO_Y---or at TO_CHARPOS if this is reached first. The
8542 problem is that we can't really tell whether the line
8543 contains TO_Y before we have completely scanned it, and
8544 this may skip past TO_X. What we do is to first scan to
8545 TO_X.
8547 If TO_X is not specified, use a TO_X of zero. The reason
8548 is to make the outcome of this function more predictable.
8549 If we didn't use TO_X == 0, we would stop at the end of
8550 the line which is probably not what a caller would expect
8551 to happen. */
8552 skip = move_it_in_display_line_to
8553 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8554 (MOVE_TO_X | (op & MOVE_TO_POS)));
8556 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8557 if (skip == MOVE_POS_MATCH_OR_ZV)
8558 reached = 5;
8559 else if (skip == MOVE_X_REACHED)
8561 /* If TO_X was reached, we want to know whether TO_Y is
8562 in the line. We know this is the case if the already
8563 scanned glyphs make the line tall enough. Otherwise,
8564 we must check by scanning the rest of the line. */
8565 line_height = it->max_ascent + it->max_descent;
8566 if (to_y >= it->current_y
8567 && to_y < it->current_y + line_height)
8569 reached = 6;
8570 break;
8572 SAVE_IT (it_backup, *it, backup_data);
8573 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8574 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8575 op & MOVE_TO_POS);
8576 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8577 line_height = it->max_ascent + it->max_descent;
8578 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8580 if (to_y >= it->current_y
8581 && to_y < it->current_y + line_height)
8583 /* If TO_Y is in this line and TO_X was reached
8584 above, we scanned too far. We have to restore
8585 IT's settings to the ones before skipping. */
8586 RESTORE_IT (it, &it_backup, backup_data);
8587 reached = 6;
8589 else
8591 skip = skip2;
8592 if (skip == MOVE_POS_MATCH_OR_ZV)
8593 reached = 7;
8596 else
8598 /* Check whether TO_Y is in this line. */
8599 line_height = it->max_ascent + it->max_descent;
8600 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8602 if (to_y >= it->current_y
8603 && to_y < it->current_y + line_height)
8605 /* When word-wrap is on, TO_X may lie past the end
8606 of a wrapped line. Then it->current is the
8607 character on the next line, so backtrack to the
8608 space before the wrap point. */
8609 if (skip == MOVE_LINE_CONTINUED
8610 && it->line_wrap == WORD_WRAP)
8612 int prev_x = max (it->current_x - 1, 0);
8613 RESTORE_IT (it, &it_backup, backup_data);
8614 skip = move_it_in_display_line_to
8615 (it, -1, prev_x, MOVE_TO_X);
8617 reached = 6;
8621 if (reached)
8622 break;
8624 else if (BUFFERP (it->object)
8625 && (it->method == GET_FROM_BUFFER
8626 || it->method == GET_FROM_STRETCH)
8627 && IT_CHARPOS (*it) >= to_charpos
8628 /* Under bidi iteration, a call to set_iterator_to_next
8629 can scan far beyond to_charpos if the initial
8630 portion of the next line needs to be reordered. In
8631 that case, give move_it_in_display_line_to another
8632 chance below. */
8633 && !(it->bidi_p
8634 && it->bidi_it.scan_dir == -1))
8635 skip = MOVE_POS_MATCH_OR_ZV;
8636 else
8637 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8639 switch (skip)
8641 case MOVE_POS_MATCH_OR_ZV:
8642 reached = 8;
8643 goto out;
8645 case MOVE_NEWLINE_OR_CR:
8646 set_iterator_to_next (it, 1);
8647 it->continuation_lines_width = 0;
8648 break;
8650 case MOVE_LINE_TRUNCATED:
8651 it->continuation_lines_width = 0;
8652 reseat_at_next_visible_line_start (it, 0);
8653 if ((op & MOVE_TO_POS) != 0
8654 && IT_CHARPOS (*it) > to_charpos)
8656 reached = 9;
8657 goto out;
8659 break;
8661 case MOVE_LINE_CONTINUED:
8662 /* For continued lines ending in a tab, some of the glyphs
8663 associated with the tab are displayed on the current
8664 line. Since it->current_x does not include these glyphs,
8665 we use it->last_visible_x instead. */
8666 if (it->c == '\t')
8668 it->continuation_lines_width += it->last_visible_x;
8669 /* When moving by vpos, ensure that the iterator really
8670 advances to the next line (bug#847, bug#969). Fixme:
8671 do we need to do this in other circumstances? */
8672 if (it->current_x != it->last_visible_x
8673 && (op & MOVE_TO_VPOS)
8674 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8676 line_start_x = it->current_x + it->pixel_width
8677 - it->last_visible_x;
8678 set_iterator_to_next (it, 0);
8681 else
8682 it->continuation_lines_width += it->current_x;
8683 break;
8685 default:
8686 abort ();
8689 /* Reset/increment for the next run. */
8690 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8691 it->current_x = line_start_x;
8692 line_start_x = 0;
8693 it->hpos = 0;
8694 it->current_y += it->max_ascent + it->max_descent;
8695 ++it->vpos;
8696 last_height = it->max_ascent + it->max_descent;
8697 last_max_ascent = it->max_ascent;
8698 it->max_ascent = it->max_descent = 0;
8701 out:
8703 /* On text terminals, we may stop at the end of a line in the middle
8704 of a multi-character glyph. If the glyph itself is continued,
8705 i.e. it is actually displayed on the next line, don't treat this
8706 stopping point as valid; move to the next line instead (unless
8707 that brings us offscreen). */
8708 if (!FRAME_WINDOW_P (it->f)
8709 && op & MOVE_TO_POS
8710 && IT_CHARPOS (*it) == to_charpos
8711 && it->what == IT_CHARACTER
8712 && it->nglyphs > 1
8713 && it->line_wrap == WINDOW_WRAP
8714 && it->current_x == it->last_visible_x - 1
8715 && it->c != '\n'
8716 && it->c != '\t'
8717 && it->vpos < XFASTINT (it->w->window_end_vpos))
8719 it->continuation_lines_width += it->current_x;
8720 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8721 it->current_y += it->max_ascent + it->max_descent;
8722 ++it->vpos;
8723 last_height = it->max_ascent + it->max_descent;
8724 last_max_ascent = it->max_ascent;
8727 if (backup_data)
8728 bidi_unshelve_cache (backup_data, 1);
8730 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8734 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8736 If DY > 0, move IT backward at least that many pixels. DY = 0
8737 means move IT backward to the preceding line start or BEGV. This
8738 function may move over more than DY pixels if IT->current_y - DY
8739 ends up in the middle of a line; in this case IT->current_y will be
8740 set to the top of the line moved to. */
8742 void
8743 move_it_vertically_backward (struct it *it, int dy)
8745 int nlines, h;
8746 struct it it2, it3;
8747 void *it2data = NULL, *it3data = NULL;
8748 EMACS_INT start_pos;
8750 move_further_back:
8751 xassert (dy >= 0);
8753 start_pos = IT_CHARPOS (*it);
8755 /* Estimate how many newlines we must move back. */
8756 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8758 /* Set the iterator's position that many lines back. */
8759 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8760 back_to_previous_visible_line_start (it);
8762 /* Reseat the iterator here. When moving backward, we don't want
8763 reseat to skip forward over invisible text, set up the iterator
8764 to deliver from overlay strings at the new position etc. So,
8765 use reseat_1 here. */
8766 reseat_1 (it, it->current.pos, 1);
8768 /* We are now surely at a line start. */
8769 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8770 reordering is in effect. */
8771 it->continuation_lines_width = 0;
8773 /* Move forward and see what y-distance we moved. First move to the
8774 start of the next line so that we get its height. We need this
8775 height to be able to tell whether we reached the specified
8776 y-distance. */
8777 SAVE_IT (it2, *it, it2data);
8778 it2.max_ascent = it2.max_descent = 0;
8781 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8782 MOVE_TO_POS | MOVE_TO_VPOS);
8784 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8785 /* If we are in a display string which starts at START_POS,
8786 and that display string includes a newline, and we are
8787 right after that newline (i.e. at the beginning of a
8788 display line), exit the loop, because otherwise we will
8789 infloop, since move_it_to will see that it is already at
8790 START_POS and will not move. */
8791 || (it2.method == GET_FROM_STRING
8792 && IT_CHARPOS (it2) == start_pos
8793 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
8794 xassert (IT_CHARPOS (*it) >= BEGV);
8795 SAVE_IT (it3, it2, it3data);
8797 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8798 xassert (IT_CHARPOS (*it) >= BEGV);
8799 /* H is the actual vertical distance from the position in *IT
8800 and the starting position. */
8801 h = it2.current_y - it->current_y;
8802 /* NLINES is the distance in number of lines. */
8803 nlines = it2.vpos - it->vpos;
8805 /* Correct IT's y and vpos position
8806 so that they are relative to the starting point. */
8807 it->vpos -= nlines;
8808 it->current_y -= h;
8810 if (dy == 0)
8812 /* DY == 0 means move to the start of the screen line. The
8813 value of nlines is > 0 if continuation lines were involved,
8814 or if the original IT position was at start of a line. */
8815 RESTORE_IT (it, it, it2data);
8816 if (nlines > 0)
8817 move_it_by_lines (it, nlines);
8818 /* The above code moves us to some position NLINES down,
8819 usually to its first glyph (leftmost in an L2R line), but
8820 that's not necessarily the start of the line, under bidi
8821 reordering. We want to get to the character position
8822 that is immediately after the newline of the previous
8823 line. */
8824 if (it->bidi_p
8825 && !it->continuation_lines_width
8826 && !STRINGP (it->string)
8827 && IT_CHARPOS (*it) > BEGV
8828 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8830 EMACS_INT nl_pos =
8831 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
8833 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
8835 bidi_unshelve_cache (it3data, 1);
8837 else
8839 /* The y-position we try to reach, relative to *IT.
8840 Note that H has been subtracted in front of the if-statement. */
8841 int target_y = it->current_y + h - dy;
8842 int y0 = it3.current_y;
8843 int y1;
8844 int line_height;
8846 RESTORE_IT (&it3, &it3, it3data);
8847 y1 = line_bottom_y (&it3);
8848 line_height = y1 - y0;
8849 RESTORE_IT (it, it, it2data);
8850 /* If we did not reach target_y, try to move further backward if
8851 we can. If we moved too far backward, try to move forward. */
8852 if (target_y < it->current_y
8853 /* This is heuristic. In a window that's 3 lines high, with
8854 a line height of 13 pixels each, recentering with point
8855 on the bottom line will try to move -39/2 = 19 pixels
8856 backward. Try to avoid moving into the first line. */
8857 && (it->current_y - target_y
8858 > min (window_box_height (it->w), line_height * 2 / 3))
8859 && IT_CHARPOS (*it) > BEGV)
8861 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8862 target_y - it->current_y));
8863 dy = it->current_y - target_y;
8864 goto move_further_back;
8866 else if (target_y >= it->current_y + line_height
8867 && IT_CHARPOS (*it) < ZV)
8869 /* Should move forward by at least one line, maybe more.
8871 Note: Calling move_it_by_lines can be expensive on
8872 terminal frames, where compute_motion is used (via
8873 vmotion) to do the job, when there are very long lines
8874 and truncate-lines is nil. That's the reason for
8875 treating terminal frames specially here. */
8877 if (!FRAME_WINDOW_P (it->f))
8878 move_it_vertically (it, target_y - (it->current_y + line_height));
8879 else
8883 move_it_by_lines (it, 1);
8885 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
8892 /* Move IT by a specified amount of pixel lines DY. DY negative means
8893 move backwards. DY = 0 means move to start of screen line. At the
8894 end, IT will be on the start of a screen line. */
8896 void
8897 move_it_vertically (struct it *it, int dy)
8899 if (dy <= 0)
8900 move_it_vertically_backward (it, -dy);
8901 else
8903 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
8904 move_it_to (it, ZV, -1, it->current_y + dy, -1,
8905 MOVE_TO_POS | MOVE_TO_Y);
8906 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
8908 /* If buffer ends in ZV without a newline, move to the start of
8909 the line to satisfy the post-condition. */
8910 if (IT_CHARPOS (*it) == ZV
8911 && ZV > BEGV
8912 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8913 move_it_by_lines (it, 0);
8918 /* Move iterator IT past the end of the text line it is in. */
8920 void
8921 move_it_past_eol (struct it *it)
8923 enum move_it_result rc;
8925 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
8926 if (rc == MOVE_NEWLINE_OR_CR)
8927 set_iterator_to_next (it, 0);
8931 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
8932 negative means move up. DVPOS == 0 means move to the start of the
8933 screen line.
8935 Optimization idea: If we would know that IT->f doesn't use
8936 a face with proportional font, we could be faster for
8937 truncate-lines nil. */
8939 void
8940 move_it_by_lines (struct it *it, int dvpos)
8943 /* The commented-out optimization uses vmotion on terminals. This
8944 gives bad results, because elements like it->what, on which
8945 callers such as pos_visible_p rely, aren't updated. */
8946 /* struct position pos;
8947 if (!FRAME_WINDOW_P (it->f))
8949 struct text_pos textpos;
8951 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
8952 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
8953 reseat (it, textpos, 1);
8954 it->vpos += pos.vpos;
8955 it->current_y += pos.vpos;
8957 else */
8959 if (dvpos == 0)
8961 /* DVPOS == 0 means move to the start of the screen line. */
8962 move_it_vertically_backward (it, 0);
8963 xassert (it->current_x == 0 && it->hpos == 0);
8964 /* Let next call to line_bottom_y calculate real line height */
8965 last_height = 0;
8967 else if (dvpos > 0)
8969 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
8970 if (!IT_POS_VALID_AFTER_MOVE_P (it))
8971 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
8973 else
8975 struct it it2;
8976 void *it2data = NULL;
8977 EMACS_INT start_charpos, i;
8979 /* Start at the beginning of the screen line containing IT's
8980 position. This may actually move vertically backwards,
8981 in case of overlays, so adjust dvpos accordingly. */
8982 dvpos += it->vpos;
8983 move_it_vertically_backward (it, 0);
8984 dvpos -= it->vpos;
8986 /* Go back -DVPOS visible lines and reseat the iterator there. */
8987 start_charpos = IT_CHARPOS (*it);
8988 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
8989 back_to_previous_visible_line_start (it);
8990 reseat (it, it->current.pos, 1);
8992 /* Move further back if we end up in a string or an image. */
8993 while (!IT_POS_VALID_AFTER_MOVE_P (it))
8995 /* First try to move to start of display line. */
8996 dvpos += it->vpos;
8997 move_it_vertically_backward (it, 0);
8998 dvpos -= it->vpos;
8999 if (IT_POS_VALID_AFTER_MOVE_P (it))
9000 break;
9001 /* If start of line is still in string or image,
9002 move further back. */
9003 back_to_previous_visible_line_start (it);
9004 reseat (it, it->current.pos, 1);
9005 dvpos--;
9008 it->current_x = it->hpos = 0;
9010 /* Above call may have moved too far if continuation lines
9011 are involved. Scan forward and see if it did. */
9012 SAVE_IT (it2, *it, it2data);
9013 it2.vpos = it2.current_y = 0;
9014 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9015 it->vpos -= it2.vpos;
9016 it->current_y -= it2.current_y;
9017 it->current_x = it->hpos = 0;
9019 /* If we moved too far back, move IT some lines forward. */
9020 if (it2.vpos > -dvpos)
9022 int delta = it2.vpos + dvpos;
9024 RESTORE_IT (&it2, &it2, it2data);
9025 SAVE_IT (it2, *it, it2data);
9026 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9027 /* Move back again if we got too far ahead. */
9028 if (IT_CHARPOS (*it) >= start_charpos)
9029 RESTORE_IT (it, &it2, it2data);
9030 else
9031 bidi_unshelve_cache (it2data, 1);
9033 else
9034 RESTORE_IT (it, it, it2data);
9038 /* Return 1 if IT points into the middle of a display vector. */
9041 in_display_vector_p (struct it *it)
9043 return (it->method == GET_FROM_DISPLAY_VECTOR
9044 && it->current.dpvec_index > 0
9045 && it->dpvec + it->current.dpvec_index != it->dpend);
9049 /***********************************************************************
9050 Messages
9051 ***********************************************************************/
9054 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9055 to *Messages*. */
9057 void
9058 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9060 Lisp_Object args[3];
9061 Lisp_Object msg, fmt;
9062 char *buffer;
9063 EMACS_INT len;
9064 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9065 USE_SAFE_ALLOCA;
9067 /* Do nothing if called asynchronously. Inserting text into
9068 a buffer may call after-change-functions and alike and
9069 that would means running Lisp asynchronously. */
9070 if (handling_signal)
9071 return;
9073 fmt = msg = Qnil;
9074 GCPRO4 (fmt, msg, arg1, arg2);
9076 args[0] = fmt = build_string (format);
9077 args[1] = arg1;
9078 args[2] = arg2;
9079 msg = Fformat (3, args);
9081 len = SBYTES (msg) + 1;
9082 SAFE_ALLOCA (buffer, char *, len);
9083 memcpy (buffer, SDATA (msg), len);
9085 message_dolog (buffer, len - 1, 1, 0);
9086 SAFE_FREE ();
9088 UNGCPRO;
9092 /* Output a newline in the *Messages* buffer if "needs" one. */
9094 void
9095 message_log_maybe_newline (void)
9097 if (message_log_need_newline)
9098 message_dolog ("", 0, 1, 0);
9102 /* Add a string M of length NBYTES to the message log, optionally
9103 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9104 nonzero, means interpret the contents of M as multibyte. This
9105 function calls low-level routines in order to bypass text property
9106 hooks, etc. which might not be safe to run.
9108 This may GC (insert may run before/after change hooks),
9109 so the buffer M must NOT point to a Lisp string. */
9111 void
9112 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
9114 const unsigned char *msg = (const unsigned char *) m;
9116 if (!NILP (Vmemory_full))
9117 return;
9119 if (!NILP (Vmessage_log_max))
9121 struct buffer *oldbuf;
9122 Lisp_Object oldpoint, oldbegv, oldzv;
9123 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9124 EMACS_INT point_at_end = 0;
9125 EMACS_INT zv_at_end = 0;
9126 Lisp_Object old_deactivate_mark, tem;
9127 struct gcpro gcpro1;
9129 old_deactivate_mark = Vdeactivate_mark;
9130 oldbuf = current_buffer;
9131 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9132 BVAR (current_buffer, undo_list) = Qt;
9134 oldpoint = message_dolog_marker1;
9135 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9136 oldbegv = message_dolog_marker2;
9137 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9138 oldzv = message_dolog_marker3;
9139 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9140 GCPRO1 (old_deactivate_mark);
9142 if (PT == Z)
9143 point_at_end = 1;
9144 if (ZV == Z)
9145 zv_at_end = 1;
9147 BEGV = BEG;
9148 BEGV_BYTE = BEG_BYTE;
9149 ZV = Z;
9150 ZV_BYTE = Z_BYTE;
9151 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9153 /* Insert the string--maybe converting multibyte to single byte
9154 or vice versa, so that all the text fits the buffer. */
9155 if (multibyte
9156 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9158 EMACS_INT i;
9159 int c, char_bytes;
9160 char work[1];
9162 /* Convert a multibyte string to single-byte
9163 for the *Message* buffer. */
9164 for (i = 0; i < nbytes; i += char_bytes)
9166 c = string_char_and_length (msg + i, &char_bytes);
9167 work[0] = (ASCII_CHAR_P (c)
9169 : multibyte_char_to_unibyte (c));
9170 insert_1_both (work, 1, 1, 1, 0, 0);
9173 else if (! multibyte
9174 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9176 EMACS_INT i;
9177 int c, char_bytes;
9178 unsigned char str[MAX_MULTIBYTE_LENGTH];
9179 /* Convert a single-byte string to multibyte
9180 for the *Message* buffer. */
9181 for (i = 0; i < nbytes; i++)
9183 c = msg[i];
9184 MAKE_CHAR_MULTIBYTE (c);
9185 char_bytes = CHAR_STRING (c, str);
9186 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9189 else if (nbytes)
9190 insert_1 (m, nbytes, 1, 0, 0);
9192 if (nlflag)
9194 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9195 printmax_t dups;
9196 insert_1 ("\n", 1, 1, 0, 0);
9198 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9199 this_bol = PT;
9200 this_bol_byte = PT_BYTE;
9202 /* See if this line duplicates the previous one.
9203 If so, combine duplicates. */
9204 if (this_bol > BEG)
9206 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9207 prev_bol = PT;
9208 prev_bol_byte = PT_BYTE;
9210 dups = message_log_check_duplicate (prev_bol_byte,
9211 this_bol_byte);
9212 if (dups)
9214 del_range_both (prev_bol, prev_bol_byte,
9215 this_bol, this_bol_byte, 0);
9216 if (dups > 1)
9218 char dupstr[sizeof " [ times]"
9219 + INT_STRLEN_BOUND (printmax_t)];
9220 int duplen;
9222 /* If you change this format, don't forget to also
9223 change message_log_check_duplicate. */
9224 sprintf (dupstr, " [%"pMd" times]", dups);
9225 duplen = strlen (dupstr);
9226 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9227 insert_1 (dupstr, duplen, 1, 0, 1);
9232 /* If we have more than the desired maximum number of lines
9233 in the *Messages* buffer now, delete the oldest ones.
9234 This is safe because we don't have undo in this buffer. */
9236 if (NATNUMP (Vmessage_log_max))
9238 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9239 -XFASTINT (Vmessage_log_max) - 1, 0);
9240 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9243 BEGV = XMARKER (oldbegv)->charpos;
9244 BEGV_BYTE = marker_byte_position (oldbegv);
9246 if (zv_at_end)
9248 ZV = Z;
9249 ZV_BYTE = Z_BYTE;
9251 else
9253 ZV = XMARKER (oldzv)->charpos;
9254 ZV_BYTE = marker_byte_position (oldzv);
9257 if (point_at_end)
9258 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9259 else
9260 /* We can't do Fgoto_char (oldpoint) because it will run some
9261 Lisp code. */
9262 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9263 XMARKER (oldpoint)->bytepos);
9265 UNGCPRO;
9266 unchain_marker (XMARKER (oldpoint));
9267 unchain_marker (XMARKER (oldbegv));
9268 unchain_marker (XMARKER (oldzv));
9270 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9271 set_buffer_internal (oldbuf);
9272 if (NILP (tem))
9273 windows_or_buffers_changed = old_windows_or_buffers_changed;
9274 message_log_need_newline = !nlflag;
9275 Vdeactivate_mark = old_deactivate_mark;
9280 /* We are at the end of the buffer after just having inserted a newline.
9281 (Note: We depend on the fact we won't be crossing the gap.)
9282 Check to see if the most recent message looks a lot like the previous one.
9283 Return 0 if different, 1 if the new one should just replace it, or a
9284 value N > 1 if we should also append " [N times]". */
9286 static intmax_t
9287 message_log_check_duplicate (EMACS_INT prev_bol_byte, EMACS_INT this_bol_byte)
9289 EMACS_INT i;
9290 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
9291 int seen_dots = 0;
9292 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9293 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9295 for (i = 0; i < len; i++)
9297 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9298 seen_dots = 1;
9299 if (p1[i] != p2[i])
9300 return seen_dots;
9302 p1 += len;
9303 if (*p1 == '\n')
9304 return 2;
9305 if (*p1++ == ' ' && *p1++ == '[')
9307 char *pend;
9308 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9309 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9310 return n+1;
9312 return 0;
9316 /* Display an echo area message M with a specified length of NBYTES
9317 bytes. The string may include null characters. If M is 0, clear
9318 out any existing message, and let the mini-buffer text show
9319 through.
9321 This may GC, so the buffer M must NOT point to a Lisp string. */
9323 void
9324 message2 (const char *m, EMACS_INT nbytes, int multibyte)
9326 /* First flush out any partial line written with print. */
9327 message_log_maybe_newline ();
9328 if (m)
9329 message_dolog (m, nbytes, 1, multibyte);
9330 message2_nolog (m, nbytes, multibyte);
9334 /* The non-logging counterpart of message2. */
9336 void
9337 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
9339 struct frame *sf = SELECTED_FRAME ();
9340 message_enable_multibyte = multibyte;
9342 if (FRAME_INITIAL_P (sf))
9344 if (noninteractive_need_newline)
9345 putc ('\n', stderr);
9346 noninteractive_need_newline = 0;
9347 if (m)
9348 fwrite (m, nbytes, 1, stderr);
9349 if (cursor_in_echo_area == 0)
9350 fprintf (stderr, "\n");
9351 fflush (stderr);
9353 /* A null message buffer means that the frame hasn't really been
9354 initialized yet. Error messages get reported properly by
9355 cmd_error, so this must be just an informative message; toss it. */
9356 else if (INTERACTIVE
9357 && sf->glyphs_initialized_p
9358 && FRAME_MESSAGE_BUF (sf))
9360 Lisp_Object mini_window;
9361 struct frame *f;
9363 /* Get the frame containing the mini-buffer
9364 that the selected frame is using. */
9365 mini_window = FRAME_MINIBUF_WINDOW (sf);
9366 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9368 FRAME_SAMPLE_VISIBILITY (f);
9369 if (FRAME_VISIBLE_P (sf)
9370 && ! FRAME_VISIBLE_P (f))
9371 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9373 if (m)
9375 set_message (m, Qnil, nbytes, multibyte);
9376 if (minibuffer_auto_raise)
9377 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9379 else
9380 clear_message (1, 1);
9382 do_pending_window_change (0);
9383 echo_area_display (1);
9384 do_pending_window_change (0);
9385 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9386 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9391 /* Display an echo area message M with a specified length of NBYTES
9392 bytes. The string may include null characters. If M is not a
9393 string, clear out any existing message, and let the mini-buffer
9394 text show through.
9396 This function cancels echoing. */
9398 void
9399 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9401 struct gcpro gcpro1;
9403 GCPRO1 (m);
9404 clear_message (1,1);
9405 cancel_echoing ();
9407 /* First flush out any partial line written with print. */
9408 message_log_maybe_newline ();
9409 if (STRINGP (m))
9411 char *buffer;
9412 USE_SAFE_ALLOCA;
9414 SAFE_ALLOCA (buffer, char *, nbytes);
9415 memcpy (buffer, SDATA (m), nbytes);
9416 message_dolog (buffer, nbytes, 1, multibyte);
9417 SAFE_FREE ();
9419 message3_nolog (m, nbytes, multibyte);
9421 UNGCPRO;
9425 /* The non-logging version of message3.
9426 This does not cancel echoing, because it is used for echoing.
9427 Perhaps we need to make a separate function for echoing
9428 and make this cancel echoing. */
9430 void
9431 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9433 struct frame *sf = SELECTED_FRAME ();
9434 message_enable_multibyte = multibyte;
9436 if (FRAME_INITIAL_P (sf))
9438 if (noninteractive_need_newline)
9439 putc ('\n', stderr);
9440 noninteractive_need_newline = 0;
9441 if (STRINGP (m))
9442 fwrite (SDATA (m), nbytes, 1, stderr);
9443 if (cursor_in_echo_area == 0)
9444 fprintf (stderr, "\n");
9445 fflush (stderr);
9447 /* A null message buffer means that the frame hasn't really been
9448 initialized yet. Error messages get reported properly by
9449 cmd_error, so this must be just an informative message; toss it. */
9450 else if (INTERACTIVE
9451 && sf->glyphs_initialized_p
9452 && FRAME_MESSAGE_BUF (sf))
9454 Lisp_Object mini_window;
9455 Lisp_Object frame;
9456 struct frame *f;
9458 /* Get the frame containing the mini-buffer
9459 that the selected frame is using. */
9460 mini_window = FRAME_MINIBUF_WINDOW (sf);
9461 frame = XWINDOW (mini_window)->frame;
9462 f = XFRAME (frame);
9464 FRAME_SAMPLE_VISIBILITY (f);
9465 if (FRAME_VISIBLE_P (sf)
9466 && !FRAME_VISIBLE_P (f))
9467 Fmake_frame_visible (frame);
9469 if (STRINGP (m) && SCHARS (m) > 0)
9471 set_message (NULL, m, nbytes, multibyte);
9472 if (minibuffer_auto_raise)
9473 Fraise_frame (frame);
9474 /* Assume we are not echoing.
9475 (If we are, echo_now will override this.) */
9476 echo_message_buffer = Qnil;
9478 else
9479 clear_message (1, 1);
9481 do_pending_window_change (0);
9482 echo_area_display (1);
9483 do_pending_window_change (0);
9484 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9485 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9490 /* Display a null-terminated echo area message M. If M is 0, clear
9491 out any existing message, and let the mini-buffer text show through.
9493 The buffer M must continue to exist until after the echo area gets
9494 cleared or some other message gets displayed there. Do not pass
9495 text that is stored in a Lisp string. Do not pass text in a buffer
9496 that was alloca'd. */
9498 void
9499 message1 (const char *m)
9501 message2 (m, (m ? strlen (m) : 0), 0);
9505 /* The non-logging counterpart of message1. */
9507 void
9508 message1_nolog (const char *m)
9510 message2_nolog (m, (m ? strlen (m) : 0), 0);
9513 /* Display a message M which contains a single %s
9514 which gets replaced with STRING. */
9516 void
9517 message_with_string (const char *m, Lisp_Object string, int log)
9519 CHECK_STRING (string);
9521 if (noninteractive)
9523 if (m)
9525 if (noninteractive_need_newline)
9526 putc ('\n', stderr);
9527 noninteractive_need_newline = 0;
9528 fprintf (stderr, m, SDATA (string));
9529 if (!cursor_in_echo_area)
9530 fprintf (stderr, "\n");
9531 fflush (stderr);
9534 else if (INTERACTIVE)
9536 /* The frame whose minibuffer we're going to display the message on.
9537 It may be larger than the selected frame, so we need
9538 to use its buffer, not the selected frame's buffer. */
9539 Lisp_Object mini_window;
9540 struct frame *f, *sf = SELECTED_FRAME ();
9542 /* Get the frame containing the minibuffer
9543 that the selected frame is using. */
9544 mini_window = FRAME_MINIBUF_WINDOW (sf);
9545 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9547 /* A null message buffer means that the frame hasn't really been
9548 initialized yet. Error messages get reported properly by
9549 cmd_error, so this must be just an informative message; toss it. */
9550 if (FRAME_MESSAGE_BUF (f))
9552 Lisp_Object args[2], msg;
9553 struct gcpro gcpro1, gcpro2;
9555 args[0] = build_string (m);
9556 args[1] = msg = string;
9557 GCPRO2 (args[0], msg);
9558 gcpro1.nvars = 2;
9560 msg = Fformat (2, args);
9562 if (log)
9563 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9564 else
9565 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9567 UNGCPRO;
9569 /* Print should start at the beginning of the message
9570 buffer next time. */
9571 message_buf_print = 0;
9577 /* Dump an informative message to the minibuf. If M is 0, clear out
9578 any existing message, and let the mini-buffer text show through. */
9580 static void
9581 vmessage (const char *m, va_list ap)
9583 if (noninteractive)
9585 if (m)
9587 if (noninteractive_need_newline)
9588 putc ('\n', stderr);
9589 noninteractive_need_newline = 0;
9590 vfprintf (stderr, m, ap);
9591 if (cursor_in_echo_area == 0)
9592 fprintf (stderr, "\n");
9593 fflush (stderr);
9596 else if (INTERACTIVE)
9598 /* The frame whose mini-buffer we're going to display the message
9599 on. It may be larger than the selected frame, so we need to
9600 use its buffer, not the selected frame's buffer. */
9601 Lisp_Object mini_window;
9602 struct frame *f, *sf = SELECTED_FRAME ();
9604 /* Get the frame containing the mini-buffer
9605 that the selected frame is using. */
9606 mini_window = FRAME_MINIBUF_WINDOW (sf);
9607 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9609 /* A null message buffer means that the frame hasn't really been
9610 initialized yet. Error messages get reported properly by
9611 cmd_error, so this must be just an informative message; toss
9612 it. */
9613 if (FRAME_MESSAGE_BUF (f))
9615 if (m)
9617 ptrdiff_t len;
9619 len = doprnt (FRAME_MESSAGE_BUF (f),
9620 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9622 message2 (FRAME_MESSAGE_BUF (f), len, 0);
9624 else
9625 message1 (0);
9627 /* Print should start at the beginning of the message
9628 buffer next time. */
9629 message_buf_print = 0;
9634 void
9635 message (const char *m, ...)
9637 va_list ap;
9638 va_start (ap, m);
9639 vmessage (m, ap);
9640 va_end (ap);
9644 #if 0
9645 /* The non-logging version of message. */
9647 void
9648 message_nolog (const char *m, ...)
9650 Lisp_Object old_log_max;
9651 va_list ap;
9652 va_start (ap, m);
9653 old_log_max = Vmessage_log_max;
9654 Vmessage_log_max = Qnil;
9655 vmessage (m, ap);
9656 Vmessage_log_max = old_log_max;
9657 va_end (ap);
9659 #endif
9662 /* Display the current message in the current mini-buffer. This is
9663 only called from error handlers in process.c, and is not time
9664 critical. */
9666 void
9667 update_echo_area (void)
9669 if (!NILP (echo_area_buffer[0]))
9671 Lisp_Object string;
9672 string = Fcurrent_message ();
9673 message3 (string, SBYTES (string),
9674 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9679 /* Make sure echo area buffers in `echo_buffers' are live.
9680 If they aren't, make new ones. */
9682 static void
9683 ensure_echo_area_buffers (void)
9685 int i;
9687 for (i = 0; i < 2; ++i)
9688 if (!BUFFERP (echo_buffer[i])
9689 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9691 char name[30];
9692 Lisp_Object old_buffer;
9693 int j;
9695 old_buffer = echo_buffer[i];
9696 sprintf (name, " *Echo Area %d*", i);
9697 echo_buffer[i] = Fget_buffer_create (build_string (name));
9698 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9699 /* to force word wrap in echo area -
9700 it was decided to postpone this*/
9701 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9703 for (j = 0; j < 2; ++j)
9704 if (EQ (old_buffer, echo_area_buffer[j]))
9705 echo_area_buffer[j] = echo_buffer[i];
9710 /* Call FN with args A1..A4 with either the current or last displayed
9711 echo_area_buffer as current buffer.
9713 WHICH zero means use the current message buffer
9714 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9715 from echo_buffer[] and clear it.
9717 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9718 suitable buffer from echo_buffer[] and clear it.
9720 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9721 that the current message becomes the last displayed one, make
9722 choose a suitable buffer for echo_area_buffer[0], and clear it.
9724 Value is what FN returns. */
9726 static int
9727 with_echo_area_buffer (struct window *w, int which,
9728 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
9729 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9731 Lisp_Object buffer;
9732 int this_one, the_other, clear_buffer_p, rc;
9733 int count = SPECPDL_INDEX ();
9735 /* If buffers aren't live, make new ones. */
9736 ensure_echo_area_buffers ();
9738 clear_buffer_p = 0;
9740 if (which == 0)
9741 this_one = 0, the_other = 1;
9742 else if (which > 0)
9743 this_one = 1, the_other = 0;
9744 else
9746 this_one = 0, the_other = 1;
9747 clear_buffer_p = 1;
9749 /* We need a fresh one in case the current echo buffer equals
9750 the one containing the last displayed echo area message. */
9751 if (!NILP (echo_area_buffer[this_one])
9752 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9753 echo_area_buffer[this_one] = Qnil;
9756 /* Choose a suitable buffer from echo_buffer[] is we don't
9757 have one. */
9758 if (NILP (echo_area_buffer[this_one]))
9760 echo_area_buffer[this_one]
9761 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9762 ? echo_buffer[the_other]
9763 : echo_buffer[this_one]);
9764 clear_buffer_p = 1;
9767 buffer = echo_area_buffer[this_one];
9769 /* Don't get confused by reusing the buffer used for echoing
9770 for a different purpose. */
9771 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9772 cancel_echoing ();
9774 record_unwind_protect (unwind_with_echo_area_buffer,
9775 with_echo_area_buffer_unwind_data (w));
9777 /* Make the echo area buffer current. Note that for display
9778 purposes, it is not necessary that the displayed window's buffer
9779 == current_buffer, except for text property lookup. So, let's
9780 only set that buffer temporarily here without doing a full
9781 Fset_window_buffer. We must also change w->pointm, though,
9782 because otherwise an assertions in unshow_buffer fails, and Emacs
9783 aborts. */
9784 set_buffer_internal_1 (XBUFFER (buffer));
9785 if (w)
9787 w->buffer = buffer;
9788 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9791 BVAR (current_buffer, undo_list) = Qt;
9792 BVAR (current_buffer, read_only) = Qnil;
9793 specbind (Qinhibit_read_only, Qt);
9794 specbind (Qinhibit_modification_hooks, Qt);
9796 if (clear_buffer_p && Z > BEG)
9797 del_range (BEG, Z);
9799 xassert (BEGV >= BEG);
9800 xassert (ZV <= Z && ZV >= BEGV);
9802 rc = fn (a1, a2, a3, a4);
9804 xassert (BEGV >= BEG);
9805 xassert (ZV <= Z && ZV >= BEGV);
9807 unbind_to (count, Qnil);
9808 return rc;
9812 /* Save state that should be preserved around the call to the function
9813 FN called in with_echo_area_buffer. */
9815 static Lisp_Object
9816 with_echo_area_buffer_unwind_data (struct window *w)
9818 int i = 0;
9819 Lisp_Object vector, tmp;
9821 /* Reduce consing by keeping one vector in
9822 Vwith_echo_area_save_vector. */
9823 vector = Vwith_echo_area_save_vector;
9824 Vwith_echo_area_save_vector = Qnil;
9826 if (NILP (vector))
9827 vector = Fmake_vector (make_number (7), Qnil);
9829 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9830 ASET (vector, i, Vdeactivate_mark); ++i;
9831 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9833 if (w)
9835 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9836 ASET (vector, i, w->buffer); ++i;
9837 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9838 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9840 else
9842 int end = i + 4;
9843 for (; i < end; ++i)
9844 ASET (vector, i, Qnil);
9847 xassert (i == ASIZE (vector));
9848 return vector;
9852 /* Restore global state from VECTOR which was created by
9853 with_echo_area_buffer_unwind_data. */
9855 static Lisp_Object
9856 unwind_with_echo_area_buffer (Lisp_Object vector)
9858 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
9859 Vdeactivate_mark = AREF (vector, 1);
9860 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
9862 if (WINDOWP (AREF (vector, 3)))
9864 struct window *w;
9865 Lisp_Object buffer, charpos, bytepos;
9867 w = XWINDOW (AREF (vector, 3));
9868 buffer = AREF (vector, 4);
9869 charpos = AREF (vector, 5);
9870 bytepos = AREF (vector, 6);
9872 w->buffer = buffer;
9873 set_marker_both (w->pointm, buffer,
9874 XFASTINT (charpos), XFASTINT (bytepos));
9877 Vwith_echo_area_save_vector = vector;
9878 return Qnil;
9882 /* Set up the echo area for use by print functions. MULTIBYTE_P
9883 non-zero means we will print multibyte. */
9885 void
9886 setup_echo_area_for_printing (int multibyte_p)
9888 /* If we can't find an echo area any more, exit. */
9889 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
9890 Fkill_emacs (Qnil);
9892 ensure_echo_area_buffers ();
9894 if (!message_buf_print)
9896 /* A message has been output since the last time we printed.
9897 Choose a fresh echo area buffer. */
9898 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9899 echo_area_buffer[0] = echo_buffer[1];
9900 else
9901 echo_area_buffer[0] = echo_buffer[0];
9903 /* Switch to that buffer and clear it. */
9904 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9905 BVAR (current_buffer, truncate_lines) = Qnil;
9907 if (Z > BEG)
9909 int count = SPECPDL_INDEX ();
9910 specbind (Qinhibit_read_only, Qt);
9911 /* Note that undo recording is always disabled. */
9912 del_range (BEG, Z);
9913 unbind_to (count, Qnil);
9915 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9917 /* Set up the buffer for the multibyteness we need. */
9918 if (multibyte_p
9919 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9920 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
9922 /* Raise the frame containing the echo area. */
9923 if (minibuffer_auto_raise)
9925 struct frame *sf = SELECTED_FRAME ();
9926 Lisp_Object mini_window;
9927 mini_window = FRAME_MINIBUF_WINDOW (sf);
9928 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9931 message_log_maybe_newline ();
9932 message_buf_print = 1;
9934 else
9936 if (NILP (echo_area_buffer[0]))
9938 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9939 echo_area_buffer[0] = echo_buffer[1];
9940 else
9941 echo_area_buffer[0] = echo_buffer[0];
9944 if (current_buffer != XBUFFER (echo_area_buffer[0]))
9946 /* Someone switched buffers between print requests. */
9947 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9948 BVAR (current_buffer, truncate_lines) = Qnil;
9954 /* Display an echo area message in window W. Value is non-zero if W's
9955 height is changed. If display_last_displayed_message_p is
9956 non-zero, display the message that was last displayed, otherwise
9957 display the current message. */
9959 static int
9960 display_echo_area (struct window *w)
9962 int i, no_message_p, window_height_changed_p, count;
9964 /* Temporarily disable garbage collections while displaying the echo
9965 area. This is done because a GC can print a message itself.
9966 That message would modify the echo area buffer's contents while a
9967 redisplay of the buffer is going on, and seriously confuse
9968 redisplay. */
9969 count = inhibit_garbage_collection ();
9971 /* If there is no message, we must call display_echo_area_1
9972 nevertheless because it resizes the window. But we will have to
9973 reset the echo_area_buffer in question to nil at the end because
9974 with_echo_area_buffer will sets it to an empty buffer. */
9975 i = display_last_displayed_message_p ? 1 : 0;
9976 no_message_p = NILP (echo_area_buffer[i]);
9978 window_height_changed_p
9979 = with_echo_area_buffer (w, display_last_displayed_message_p,
9980 display_echo_area_1,
9981 (intptr_t) w, Qnil, 0, 0);
9983 if (no_message_p)
9984 echo_area_buffer[i] = Qnil;
9986 unbind_to (count, Qnil);
9987 return window_height_changed_p;
9991 /* Helper for display_echo_area. Display the current buffer which
9992 contains the current echo area message in window W, a mini-window,
9993 a pointer to which is passed in A1. A2..A4 are currently not used.
9994 Change the height of W so that all of the message is displayed.
9995 Value is non-zero if height of W was changed. */
9997 static int
9998 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10000 intptr_t i1 = a1;
10001 struct window *w = (struct window *) i1;
10002 Lisp_Object window;
10003 struct text_pos start;
10004 int window_height_changed_p = 0;
10006 /* Do this before displaying, so that we have a large enough glyph
10007 matrix for the display. If we can't get enough space for the
10008 whole text, display the last N lines. That works by setting w->start. */
10009 window_height_changed_p = resize_mini_window (w, 0);
10011 /* Use the starting position chosen by resize_mini_window. */
10012 SET_TEXT_POS_FROM_MARKER (start, w->start);
10014 /* Display. */
10015 clear_glyph_matrix (w->desired_matrix);
10016 XSETWINDOW (window, w);
10017 try_window (window, start, 0);
10019 return window_height_changed_p;
10023 /* Resize the echo area window to exactly the size needed for the
10024 currently displayed message, if there is one. If a mini-buffer
10025 is active, don't shrink it. */
10027 void
10028 resize_echo_area_exactly (void)
10030 if (BUFFERP (echo_area_buffer[0])
10031 && WINDOWP (echo_area_window))
10033 struct window *w = XWINDOW (echo_area_window);
10034 int resized_p;
10035 Lisp_Object resize_exactly;
10037 if (minibuf_level == 0)
10038 resize_exactly = Qt;
10039 else
10040 resize_exactly = Qnil;
10042 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10043 (intptr_t) w, resize_exactly,
10044 0, 0);
10045 if (resized_p)
10047 ++windows_or_buffers_changed;
10048 ++update_mode_lines;
10049 redisplay_internal ();
10055 /* Callback function for with_echo_area_buffer, when used from
10056 resize_echo_area_exactly. A1 contains a pointer to the window to
10057 resize, EXACTLY non-nil means resize the mini-window exactly to the
10058 size of the text displayed. A3 and A4 are not used. Value is what
10059 resize_mini_window returns. */
10061 static int
10062 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
10064 intptr_t i1 = a1;
10065 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10069 /* Resize mini-window W to fit the size of its contents. EXACT_P
10070 means size the window exactly to the size needed. Otherwise, it's
10071 only enlarged until W's buffer is empty.
10073 Set W->start to the right place to begin display. If the whole
10074 contents fit, start at the beginning. Otherwise, start so as
10075 to make the end of the contents appear. This is particularly
10076 important for y-or-n-p, but seems desirable generally.
10078 Value is non-zero if the window height has been changed. */
10081 resize_mini_window (struct window *w, int exact_p)
10083 struct frame *f = XFRAME (w->frame);
10084 int window_height_changed_p = 0;
10086 xassert (MINI_WINDOW_P (w));
10088 /* By default, start display at the beginning. */
10089 set_marker_both (w->start, w->buffer,
10090 BUF_BEGV (XBUFFER (w->buffer)),
10091 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10093 /* Don't resize windows while redisplaying a window; it would
10094 confuse redisplay functions when the size of the window they are
10095 displaying changes from under them. Such a resizing can happen,
10096 for instance, when which-func prints a long message while
10097 we are running fontification-functions. We're running these
10098 functions with safe_call which binds inhibit-redisplay to t. */
10099 if (!NILP (Vinhibit_redisplay))
10100 return 0;
10102 /* Nil means don't try to resize. */
10103 if (NILP (Vresize_mini_windows)
10104 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10105 return 0;
10107 if (!FRAME_MINIBUF_ONLY_P (f))
10109 struct it it;
10110 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10111 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10112 int height, max_height;
10113 int unit = FRAME_LINE_HEIGHT (f);
10114 struct text_pos start;
10115 struct buffer *old_current_buffer = NULL;
10117 if (current_buffer != XBUFFER (w->buffer))
10119 old_current_buffer = current_buffer;
10120 set_buffer_internal (XBUFFER (w->buffer));
10123 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10125 /* Compute the max. number of lines specified by the user. */
10126 if (FLOATP (Vmax_mini_window_height))
10127 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10128 else if (INTEGERP (Vmax_mini_window_height))
10129 max_height = XINT (Vmax_mini_window_height);
10130 else
10131 max_height = total_height / 4;
10133 /* Correct that max. height if it's bogus. */
10134 max_height = max (1, max_height);
10135 max_height = min (total_height, max_height);
10137 /* Find out the height of the text in the window. */
10138 if (it.line_wrap == TRUNCATE)
10139 height = 1;
10140 else
10142 last_height = 0;
10143 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10144 if (it.max_ascent == 0 && it.max_descent == 0)
10145 height = it.current_y + last_height;
10146 else
10147 height = it.current_y + it.max_ascent + it.max_descent;
10148 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10149 height = (height + unit - 1) / unit;
10152 /* Compute a suitable window start. */
10153 if (height > max_height)
10155 height = max_height;
10156 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10157 move_it_vertically_backward (&it, (height - 1) * unit);
10158 start = it.current.pos;
10160 else
10161 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10162 SET_MARKER_FROM_TEXT_POS (w->start, start);
10164 if (EQ (Vresize_mini_windows, Qgrow_only))
10166 /* Let it grow only, until we display an empty message, in which
10167 case the window shrinks again. */
10168 if (height > WINDOW_TOTAL_LINES (w))
10170 int old_height = WINDOW_TOTAL_LINES (w);
10171 freeze_window_starts (f, 1);
10172 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10173 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10175 else if (height < WINDOW_TOTAL_LINES (w)
10176 && (exact_p || BEGV == ZV))
10178 int old_height = WINDOW_TOTAL_LINES (w);
10179 freeze_window_starts (f, 0);
10180 shrink_mini_window (w);
10181 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10184 else
10186 /* Always resize to exact size needed. */
10187 if (height > WINDOW_TOTAL_LINES (w))
10189 int old_height = WINDOW_TOTAL_LINES (w);
10190 freeze_window_starts (f, 1);
10191 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10192 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10194 else if (height < WINDOW_TOTAL_LINES (w))
10196 int old_height = WINDOW_TOTAL_LINES (w);
10197 freeze_window_starts (f, 0);
10198 shrink_mini_window (w);
10200 if (height)
10202 freeze_window_starts (f, 1);
10203 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10206 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10210 if (old_current_buffer)
10211 set_buffer_internal (old_current_buffer);
10214 return window_height_changed_p;
10218 /* Value is the current message, a string, or nil if there is no
10219 current message. */
10221 Lisp_Object
10222 current_message (void)
10224 Lisp_Object msg;
10226 if (!BUFFERP (echo_area_buffer[0]))
10227 msg = Qnil;
10228 else
10230 with_echo_area_buffer (0, 0, current_message_1,
10231 (intptr_t) &msg, Qnil, 0, 0);
10232 if (NILP (msg))
10233 echo_area_buffer[0] = Qnil;
10236 return msg;
10240 static int
10241 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10243 intptr_t i1 = a1;
10244 Lisp_Object *msg = (Lisp_Object *) i1;
10246 if (Z > BEG)
10247 *msg = make_buffer_string (BEG, Z, 1);
10248 else
10249 *msg = Qnil;
10250 return 0;
10254 /* Push the current message on Vmessage_stack for later restoration
10255 by restore_message. Value is non-zero if the current message isn't
10256 empty. This is a relatively infrequent operation, so it's not
10257 worth optimizing. */
10260 push_message (void)
10262 Lisp_Object msg;
10263 msg = current_message ();
10264 Vmessage_stack = Fcons (msg, Vmessage_stack);
10265 return STRINGP (msg);
10269 /* Restore message display from the top of Vmessage_stack. */
10271 void
10272 restore_message (void)
10274 Lisp_Object msg;
10276 xassert (CONSP (Vmessage_stack));
10277 msg = XCAR (Vmessage_stack);
10278 if (STRINGP (msg))
10279 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10280 else
10281 message3_nolog (msg, 0, 0);
10285 /* Handler for record_unwind_protect calling pop_message. */
10287 Lisp_Object
10288 pop_message_unwind (Lisp_Object dummy)
10290 pop_message ();
10291 return Qnil;
10294 /* Pop the top-most entry off Vmessage_stack. */
10296 static void
10297 pop_message (void)
10299 xassert (CONSP (Vmessage_stack));
10300 Vmessage_stack = XCDR (Vmessage_stack);
10304 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10305 exits. If the stack is not empty, we have a missing pop_message
10306 somewhere. */
10308 void
10309 check_message_stack (void)
10311 if (!NILP (Vmessage_stack))
10312 abort ();
10316 /* Truncate to NCHARS what will be displayed in the echo area the next
10317 time we display it---but don't redisplay it now. */
10319 void
10320 truncate_echo_area (EMACS_INT nchars)
10322 if (nchars == 0)
10323 echo_area_buffer[0] = Qnil;
10324 /* A null message buffer means that the frame hasn't really been
10325 initialized yet. Error messages get reported properly by
10326 cmd_error, so this must be just an informative message; toss it. */
10327 else if (!noninteractive
10328 && INTERACTIVE
10329 && !NILP (echo_area_buffer[0]))
10331 struct frame *sf = SELECTED_FRAME ();
10332 if (FRAME_MESSAGE_BUF (sf))
10333 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10338 /* Helper function for truncate_echo_area. Truncate the current
10339 message to at most NCHARS characters. */
10341 static int
10342 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10344 if (BEG + nchars < Z)
10345 del_range (BEG + nchars, Z);
10346 if (Z == BEG)
10347 echo_area_buffer[0] = Qnil;
10348 return 0;
10352 /* Set the current message to a substring of S or STRING.
10354 If STRING is a Lisp string, set the message to the first NBYTES
10355 bytes from STRING. NBYTES zero means use the whole string. If
10356 STRING is multibyte, the message will be displayed multibyte.
10358 If S is not null, set the message to the first LEN bytes of S. LEN
10359 zero means use the whole string. MULTIBYTE_P non-zero means S is
10360 multibyte. Display the message multibyte in that case.
10362 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10363 to t before calling set_message_1 (which calls insert).
10366 static void
10367 set_message (const char *s, Lisp_Object string,
10368 EMACS_INT nbytes, int multibyte_p)
10370 message_enable_multibyte
10371 = ((s && multibyte_p)
10372 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10374 with_echo_area_buffer (0, -1, set_message_1,
10375 (intptr_t) s, string, nbytes, multibyte_p);
10376 message_buf_print = 0;
10377 help_echo_showing_p = 0;
10381 /* Helper function for set_message. Arguments have the same meaning
10382 as there, with A1 corresponding to S and A2 corresponding to STRING
10383 This function is called with the echo area buffer being
10384 current. */
10386 static int
10387 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
10389 intptr_t i1 = a1;
10390 const char *s = (const char *) i1;
10391 const unsigned char *msg = (const unsigned char *) s;
10392 Lisp_Object string = a2;
10394 /* Change multibyteness of the echo buffer appropriately. */
10395 if (message_enable_multibyte
10396 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10397 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10399 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10400 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10401 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10403 /* Insert new message at BEG. */
10404 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10406 if (STRINGP (string))
10408 EMACS_INT nchars;
10410 if (nbytes == 0)
10411 nbytes = SBYTES (string);
10412 nchars = string_byte_to_char (string, nbytes);
10414 /* This function takes care of single/multibyte conversion. We
10415 just have to ensure that the echo area buffer has the right
10416 setting of enable_multibyte_characters. */
10417 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10419 else if (s)
10421 if (nbytes == 0)
10422 nbytes = strlen (s);
10424 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10426 /* Convert from multi-byte to single-byte. */
10427 EMACS_INT i;
10428 int c, n;
10429 char work[1];
10431 /* Convert a multibyte string to single-byte. */
10432 for (i = 0; i < nbytes; i += n)
10434 c = string_char_and_length (msg + i, &n);
10435 work[0] = (ASCII_CHAR_P (c)
10437 : multibyte_char_to_unibyte (c));
10438 insert_1_both (work, 1, 1, 1, 0, 0);
10441 else if (!multibyte_p
10442 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10444 /* Convert from single-byte to multi-byte. */
10445 EMACS_INT i;
10446 int c, n;
10447 unsigned char str[MAX_MULTIBYTE_LENGTH];
10449 /* Convert a single-byte string to multibyte. */
10450 for (i = 0; i < nbytes; i++)
10452 c = msg[i];
10453 MAKE_CHAR_MULTIBYTE (c);
10454 n = CHAR_STRING (c, str);
10455 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10458 else
10459 insert_1 (s, nbytes, 1, 0, 0);
10462 return 0;
10466 /* Clear messages. CURRENT_P non-zero means clear the current
10467 message. LAST_DISPLAYED_P non-zero means clear the message
10468 last displayed. */
10470 void
10471 clear_message (int current_p, int last_displayed_p)
10473 if (current_p)
10475 echo_area_buffer[0] = Qnil;
10476 message_cleared_p = 1;
10479 if (last_displayed_p)
10480 echo_area_buffer[1] = Qnil;
10482 message_buf_print = 0;
10485 /* Clear garbaged frames.
10487 This function is used where the old redisplay called
10488 redraw_garbaged_frames which in turn called redraw_frame which in
10489 turn called clear_frame. The call to clear_frame was a source of
10490 flickering. I believe a clear_frame is not necessary. It should
10491 suffice in the new redisplay to invalidate all current matrices,
10492 and ensure a complete redisplay of all windows. */
10494 static void
10495 clear_garbaged_frames (void)
10497 if (frame_garbaged)
10499 Lisp_Object tail, frame;
10500 int changed_count = 0;
10502 FOR_EACH_FRAME (tail, frame)
10504 struct frame *f = XFRAME (frame);
10506 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10508 if (f->resized_p)
10510 Fredraw_frame (frame);
10511 f->force_flush_display_p = 1;
10513 clear_current_matrices (f);
10514 changed_count++;
10515 f->garbaged = 0;
10516 f->resized_p = 0;
10520 frame_garbaged = 0;
10521 if (changed_count)
10522 ++windows_or_buffers_changed;
10527 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10528 is non-zero update selected_frame. Value is non-zero if the
10529 mini-windows height has been changed. */
10531 static int
10532 echo_area_display (int update_frame_p)
10534 Lisp_Object mini_window;
10535 struct window *w;
10536 struct frame *f;
10537 int window_height_changed_p = 0;
10538 struct frame *sf = SELECTED_FRAME ();
10540 mini_window = FRAME_MINIBUF_WINDOW (sf);
10541 w = XWINDOW (mini_window);
10542 f = XFRAME (WINDOW_FRAME (w));
10544 /* Don't display if frame is invisible or not yet initialized. */
10545 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10546 return 0;
10548 #ifdef HAVE_WINDOW_SYSTEM
10549 /* When Emacs starts, selected_frame may be the initial terminal
10550 frame. If we let this through, a message would be displayed on
10551 the terminal. */
10552 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10553 return 0;
10554 #endif /* HAVE_WINDOW_SYSTEM */
10556 /* Redraw garbaged frames. */
10557 if (frame_garbaged)
10558 clear_garbaged_frames ();
10560 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10562 echo_area_window = mini_window;
10563 window_height_changed_p = display_echo_area (w);
10564 w->must_be_updated_p = 1;
10566 /* Update the display, unless called from redisplay_internal.
10567 Also don't update the screen during redisplay itself. The
10568 update will happen at the end of redisplay, and an update
10569 here could cause confusion. */
10570 if (update_frame_p && !redisplaying_p)
10572 int n = 0;
10574 /* If the display update has been interrupted by pending
10575 input, update mode lines in the frame. Due to the
10576 pending input, it might have been that redisplay hasn't
10577 been called, so that mode lines above the echo area are
10578 garbaged. This looks odd, so we prevent it here. */
10579 if (!display_completed)
10580 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10582 if (window_height_changed_p
10583 /* Don't do this if Emacs is shutting down. Redisplay
10584 needs to run hooks. */
10585 && !NILP (Vrun_hooks))
10587 /* Must update other windows. Likewise as in other
10588 cases, don't let this update be interrupted by
10589 pending input. */
10590 int count = SPECPDL_INDEX ();
10591 specbind (Qredisplay_dont_pause, Qt);
10592 windows_or_buffers_changed = 1;
10593 redisplay_internal ();
10594 unbind_to (count, Qnil);
10596 else if (FRAME_WINDOW_P (f) && n == 0)
10598 /* Window configuration is the same as before.
10599 Can do with a display update of the echo area,
10600 unless we displayed some mode lines. */
10601 update_single_window (w, 1);
10602 FRAME_RIF (f)->flush_display (f);
10604 else
10605 update_frame (f, 1, 1);
10607 /* If cursor is in the echo area, make sure that the next
10608 redisplay displays the minibuffer, so that the cursor will
10609 be replaced with what the minibuffer wants. */
10610 if (cursor_in_echo_area)
10611 ++windows_or_buffers_changed;
10614 else if (!EQ (mini_window, selected_window))
10615 windows_or_buffers_changed++;
10617 /* Last displayed message is now the current message. */
10618 echo_area_buffer[1] = echo_area_buffer[0];
10619 /* Inform read_char that we're not echoing. */
10620 echo_message_buffer = Qnil;
10622 /* Prevent redisplay optimization in redisplay_internal by resetting
10623 this_line_start_pos. This is done because the mini-buffer now
10624 displays the message instead of its buffer text. */
10625 if (EQ (mini_window, selected_window))
10626 CHARPOS (this_line_start_pos) = 0;
10628 return window_height_changed_p;
10633 /***********************************************************************
10634 Mode Lines and Frame Titles
10635 ***********************************************************************/
10637 /* A buffer for constructing non-propertized mode-line strings and
10638 frame titles in it; allocated from the heap in init_xdisp and
10639 resized as needed in store_mode_line_noprop_char. */
10641 static char *mode_line_noprop_buf;
10643 /* The buffer's end, and a current output position in it. */
10645 static char *mode_line_noprop_buf_end;
10646 static char *mode_line_noprop_ptr;
10648 #define MODE_LINE_NOPROP_LEN(start) \
10649 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10651 static enum {
10652 MODE_LINE_DISPLAY = 0,
10653 MODE_LINE_TITLE,
10654 MODE_LINE_NOPROP,
10655 MODE_LINE_STRING
10656 } mode_line_target;
10658 /* Alist that caches the results of :propertize.
10659 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10660 static Lisp_Object mode_line_proptrans_alist;
10662 /* List of strings making up the mode-line. */
10663 static Lisp_Object mode_line_string_list;
10665 /* Base face property when building propertized mode line string. */
10666 static Lisp_Object mode_line_string_face;
10667 static Lisp_Object mode_line_string_face_prop;
10670 /* Unwind data for mode line strings */
10672 static Lisp_Object Vmode_line_unwind_vector;
10674 static Lisp_Object
10675 format_mode_line_unwind_data (struct buffer *obuf,
10676 Lisp_Object owin,
10677 int save_proptrans)
10679 Lisp_Object vector, tmp;
10681 /* Reduce consing by keeping one vector in
10682 Vwith_echo_area_save_vector. */
10683 vector = Vmode_line_unwind_vector;
10684 Vmode_line_unwind_vector = Qnil;
10686 if (NILP (vector))
10687 vector = Fmake_vector (make_number (8), Qnil);
10689 ASET (vector, 0, make_number (mode_line_target));
10690 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10691 ASET (vector, 2, mode_line_string_list);
10692 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10693 ASET (vector, 4, mode_line_string_face);
10694 ASET (vector, 5, mode_line_string_face_prop);
10696 if (obuf)
10697 XSETBUFFER (tmp, obuf);
10698 else
10699 tmp = Qnil;
10700 ASET (vector, 6, tmp);
10701 ASET (vector, 7, owin);
10703 return vector;
10706 static Lisp_Object
10707 unwind_format_mode_line (Lisp_Object vector)
10709 mode_line_target = XINT (AREF (vector, 0));
10710 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10711 mode_line_string_list = AREF (vector, 2);
10712 if (! EQ (AREF (vector, 3), Qt))
10713 mode_line_proptrans_alist = AREF (vector, 3);
10714 mode_line_string_face = AREF (vector, 4);
10715 mode_line_string_face_prop = AREF (vector, 5);
10717 if (!NILP (AREF (vector, 7)))
10718 /* Select window before buffer, since it may change the buffer. */
10719 Fselect_window (AREF (vector, 7), Qt);
10721 if (!NILP (AREF (vector, 6)))
10723 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10724 ASET (vector, 6, Qnil);
10727 Vmode_line_unwind_vector = vector;
10728 return Qnil;
10732 /* Store a single character C for the frame title in mode_line_noprop_buf.
10733 Re-allocate mode_line_noprop_buf if necessary. */
10735 static void
10736 store_mode_line_noprop_char (char c)
10738 /* If output position has reached the end of the allocated buffer,
10739 increase the buffer's size. */
10740 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10742 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10743 ptrdiff_t size = len;
10744 mode_line_noprop_buf =
10745 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10746 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10747 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10750 *mode_line_noprop_ptr++ = c;
10754 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10755 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10756 characters that yield more columns than PRECISION; PRECISION <= 0
10757 means copy the whole string. Pad with spaces until FIELD_WIDTH
10758 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10759 pad. Called from display_mode_element when it is used to build a
10760 frame title. */
10762 static int
10763 store_mode_line_noprop (const char *string, int field_width, int precision)
10765 const unsigned char *str = (const unsigned char *) string;
10766 int n = 0;
10767 EMACS_INT dummy, nbytes;
10769 /* Copy at most PRECISION chars from STR. */
10770 nbytes = strlen (string);
10771 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10772 while (nbytes--)
10773 store_mode_line_noprop_char (*str++);
10775 /* Fill up with spaces until FIELD_WIDTH reached. */
10776 while (field_width > 0
10777 && n < field_width)
10779 store_mode_line_noprop_char (' ');
10780 ++n;
10783 return n;
10786 /***********************************************************************
10787 Frame Titles
10788 ***********************************************************************/
10790 #ifdef HAVE_WINDOW_SYSTEM
10792 /* Set the title of FRAME, if it has changed. The title format is
10793 Vicon_title_format if FRAME is iconified, otherwise it is
10794 frame_title_format. */
10796 static void
10797 x_consider_frame_title (Lisp_Object frame)
10799 struct frame *f = XFRAME (frame);
10801 if (FRAME_WINDOW_P (f)
10802 || FRAME_MINIBUF_ONLY_P (f)
10803 || f->explicit_name)
10805 /* Do we have more than one visible frame on this X display? */
10806 Lisp_Object tail;
10807 Lisp_Object fmt;
10808 ptrdiff_t title_start;
10809 char *title;
10810 ptrdiff_t len;
10811 struct it it;
10812 int count = SPECPDL_INDEX ();
10814 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
10816 Lisp_Object other_frame = XCAR (tail);
10817 struct frame *tf = XFRAME (other_frame);
10819 if (tf != f
10820 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10821 && !FRAME_MINIBUF_ONLY_P (tf)
10822 && !EQ (other_frame, tip_frame)
10823 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10824 break;
10827 /* Set global variable indicating that multiple frames exist. */
10828 multiple_frames = CONSP (tail);
10830 /* Switch to the buffer of selected window of the frame. Set up
10831 mode_line_target so that display_mode_element will output into
10832 mode_line_noprop_buf; then display the title. */
10833 record_unwind_protect (unwind_format_mode_line,
10834 format_mode_line_unwind_data
10835 (current_buffer, selected_window, 0));
10837 Fselect_window (f->selected_window, Qt);
10838 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
10839 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
10841 mode_line_target = MODE_LINE_TITLE;
10842 title_start = MODE_LINE_NOPROP_LEN (0);
10843 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
10844 NULL, DEFAULT_FACE_ID);
10845 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
10846 len = MODE_LINE_NOPROP_LEN (title_start);
10847 title = mode_line_noprop_buf + title_start;
10848 unbind_to (count, Qnil);
10850 /* Set the title only if it's changed. This avoids consing in
10851 the common case where it hasn't. (If it turns out that we've
10852 already wasted too much time by walking through the list with
10853 display_mode_element, then we might need to optimize at a
10854 higher level than this.) */
10855 if (! STRINGP (f->name)
10856 || SBYTES (f->name) != len
10857 || memcmp (title, SDATA (f->name), len) != 0)
10858 x_implicitly_set_name (f, make_string (title, len), Qnil);
10862 #endif /* not HAVE_WINDOW_SYSTEM */
10867 /***********************************************************************
10868 Menu Bars
10869 ***********************************************************************/
10872 /* Prepare for redisplay by updating menu-bar item lists when
10873 appropriate. This can call eval. */
10875 void
10876 prepare_menu_bars (void)
10878 int all_windows;
10879 struct gcpro gcpro1, gcpro2;
10880 struct frame *f;
10881 Lisp_Object tooltip_frame;
10883 #ifdef HAVE_WINDOW_SYSTEM
10884 tooltip_frame = tip_frame;
10885 #else
10886 tooltip_frame = Qnil;
10887 #endif
10889 /* Update all frame titles based on their buffer names, etc. We do
10890 this before the menu bars so that the buffer-menu will show the
10891 up-to-date frame titles. */
10892 #ifdef HAVE_WINDOW_SYSTEM
10893 if (windows_or_buffers_changed || update_mode_lines)
10895 Lisp_Object tail, frame;
10897 FOR_EACH_FRAME (tail, frame)
10899 f = XFRAME (frame);
10900 if (!EQ (frame, tooltip_frame)
10901 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
10902 x_consider_frame_title (frame);
10905 #endif /* HAVE_WINDOW_SYSTEM */
10907 /* Update the menu bar item lists, if appropriate. This has to be
10908 done before any actual redisplay or generation of display lines. */
10909 all_windows = (update_mode_lines
10910 || buffer_shared > 1
10911 || windows_or_buffers_changed);
10912 if (all_windows)
10914 Lisp_Object tail, frame;
10915 int count = SPECPDL_INDEX ();
10916 /* 1 means that update_menu_bar has run its hooks
10917 so any further calls to update_menu_bar shouldn't do so again. */
10918 int menu_bar_hooks_run = 0;
10920 record_unwind_save_match_data ();
10922 FOR_EACH_FRAME (tail, frame)
10924 f = XFRAME (frame);
10926 /* Ignore tooltip frame. */
10927 if (EQ (frame, tooltip_frame))
10928 continue;
10930 /* If a window on this frame changed size, report that to
10931 the user and clear the size-change flag. */
10932 if (FRAME_WINDOW_SIZES_CHANGED (f))
10934 Lisp_Object functions;
10936 /* Clear flag first in case we get an error below. */
10937 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
10938 functions = Vwindow_size_change_functions;
10939 GCPRO2 (tail, functions);
10941 while (CONSP (functions))
10943 if (!EQ (XCAR (functions), Qt))
10944 call1 (XCAR (functions), frame);
10945 functions = XCDR (functions);
10947 UNGCPRO;
10950 GCPRO1 (tail);
10951 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
10952 #ifdef HAVE_WINDOW_SYSTEM
10953 update_tool_bar (f, 0);
10954 #endif
10955 #ifdef HAVE_NS
10956 if (windows_or_buffers_changed
10957 && FRAME_NS_P (f))
10958 ns_set_doc_edited (f, Fbuffer_modified_p
10959 (XWINDOW (f->selected_window)->buffer));
10960 #endif
10961 UNGCPRO;
10964 unbind_to (count, Qnil);
10966 else
10968 struct frame *sf = SELECTED_FRAME ();
10969 update_menu_bar (sf, 1, 0);
10970 #ifdef HAVE_WINDOW_SYSTEM
10971 update_tool_bar (sf, 1);
10972 #endif
10977 /* Update the menu bar item list for frame F. This has to be done
10978 before we start to fill in any display lines, because it can call
10979 eval.
10981 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
10983 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
10984 already ran the menu bar hooks for this redisplay, so there
10985 is no need to run them again. The return value is the
10986 updated value of this flag, to pass to the next call. */
10988 static int
10989 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
10991 Lisp_Object window;
10992 register struct window *w;
10994 /* If called recursively during a menu update, do nothing. This can
10995 happen when, for instance, an activate-menubar-hook causes a
10996 redisplay. */
10997 if (inhibit_menubar_update)
10998 return hooks_run;
11000 window = FRAME_SELECTED_WINDOW (f);
11001 w = XWINDOW (window);
11003 if (FRAME_WINDOW_P (f)
11005 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11006 || defined (HAVE_NS) || defined (USE_GTK)
11007 FRAME_EXTERNAL_MENU_BAR (f)
11008 #else
11009 FRAME_MENU_BAR_LINES (f) > 0
11010 #endif
11011 : FRAME_MENU_BAR_LINES (f) > 0)
11013 /* If the user has switched buffers or windows, we need to
11014 recompute to reflect the new bindings. But we'll
11015 recompute when update_mode_lines is set too; that means
11016 that people can use force-mode-line-update to request
11017 that the menu bar be recomputed. The adverse effect on
11018 the rest of the redisplay algorithm is about the same as
11019 windows_or_buffers_changed anyway. */
11020 if (windows_or_buffers_changed
11021 /* This used to test w->update_mode_line, but we believe
11022 there is no need to recompute the menu in that case. */
11023 || update_mode_lines
11024 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11025 < BUF_MODIFF (XBUFFER (w->buffer)))
11026 != !NILP (w->last_had_star))
11027 || ((!NILP (Vtransient_mark_mode)
11028 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11029 != !NILP (w->region_showing)))
11031 struct buffer *prev = current_buffer;
11032 int count = SPECPDL_INDEX ();
11034 specbind (Qinhibit_menubar_update, Qt);
11036 set_buffer_internal_1 (XBUFFER (w->buffer));
11037 if (save_match_data)
11038 record_unwind_save_match_data ();
11039 if (NILP (Voverriding_local_map_menu_flag))
11041 specbind (Qoverriding_terminal_local_map, Qnil);
11042 specbind (Qoverriding_local_map, Qnil);
11045 if (!hooks_run)
11047 /* Run the Lucid hook. */
11048 safe_run_hooks (Qactivate_menubar_hook);
11050 /* If it has changed current-menubar from previous value,
11051 really recompute the menu-bar from the value. */
11052 if (! NILP (Vlucid_menu_bar_dirty_flag))
11053 call0 (Qrecompute_lucid_menubar);
11055 safe_run_hooks (Qmenu_bar_update_hook);
11057 hooks_run = 1;
11060 XSETFRAME (Vmenu_updating_frame, f);
11061 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
11063 /* Redisplay the menu bar in case we changed it. */
11064 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11065 || defined (HAVE_NS) || defined (USE_GTK)
11066 if (FRAME_WINDOW_P (f))
11068 #if defined (HAVE_NS)
11069 /* All frames on Mac OS share the same menubar. So only
11070 the selected frame should be allowed to set it. */
11071 if (f == SELECTED_FRAME ())
11072 #endif
11073 set_frame_menubar (f, 0, 0);
11075 else
11076 /* On a terminal screen, the menu bar is an ordinary screen
11077 line, and this makes it get updated. */
11078 w->update_mode_line = Qt;
11079 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11080 /* In the non-toolkit version, the menu bar is an ordinary screen
11081 line, and this makes it get updated. */
11082 w->update_mode_line = Qt;
11083 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11085 unbind_to (count, Qnil);
11086 set_buffer_internal_1 (prev);
11090 return hooks_run;
11095 /***********************************************************************
11096 Output Cursor
11097 ***********************************************************************/
11099 #ifdef HAVE_WINDOW_SYSTEM
11101 /* EXPORT:
11102 Nominal cursor position -- where to draw output.
11103 HPOS and VPOS are window relative glyph matrix coordinates.
11104 X and Y are window relative pixel coordinates. */
11106 struct cursor_pos output_cursor;
11109 /* EXPORT:
11110 Set the global variable output_cursor to CURSOR. All cursor
11111 positions are relative to updated_window. */
11113 void
11114 set_output_cursor (struct cursor_pos *cursor)
11116 output_cursor.hpos = cursor->hpos;
11117 output_cursor.vpos = cursor->vpos;
11118 output_cursor.x = cursor->x;
11119 output_cursor.y = cursor->y;
11123 /* EXPORT for RIF:
11124 Set a nominal cursor position.
11126 HPOS and VPOS are column/row positions in a window glyph matrix. X
11127 and Y are window text area relative pixel positions.
11129 If this is done during an update, updated_window will contain the
11130 window that is being updated and the position is the future output
11131 cursor position for that window. If updated_window is null, use
11132 selected_window and display the cursor at the given position. */
11134 void
11135 x_cursor_to (int vpos, int hpos, int y, int x)
11137 struct window *w;
11139 /* If updated_window is not set, work on selected_window. */
11140 if (updated_window)
11141 w = updated_window;
11142 else
11143 w = XWINDOW (selected_window);
11145 /* Set the output cursor. */
11146 output_cursor.hpos = hpos;
11147 output_cursor.vpos = vpos;
11148 output_cursor.x = x;
11149 output_cursor.y = y;
11151 /* If not called as part of an update, really display the cursor.
11152 This will also set the cursor position of W. */
11153 if (updated_window == NULL)
11155 BLOCK_INPUT;
11156 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11157 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11158 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11159 UNBLOCK_INPUT;
11163 #endif /* HAVE_WINDOW_SYSTEM */
11166 /***********************************************************************
11167 Tool-bars
11168 ***********************************************************************/
11170 #ifdef HAVE_WINDOW_SYSTEM
11172 /* Where the mouse was last time we reported a mouse event. */
11174 FRAME_PTR last_mouse_frame;
11176 /* Tool-bar item index of the item on which a mouse button was pressed
11177 or -1. */
11179 int last_tool_bar_item;
11182 static Lisp_Object
11183 update_tool_bar_unwind (Lisp_Object frame)
11185 selected_frame = frame;
11186 return Qnil;
11189 /* Update the tool-bar item list for frame F. This has to be done
11190 before we start to fill in any display lines. Called from
11191 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11192 and restore it here. */
11194 static void
11195 update_tool_bar (struct frame *f, int save_match_data)
11197 #if defined (USE_GTK) || defined (HAVE_NS)
11198 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11199 #else
11200 int do_update = WINDOWP (f->tool_bar_window)
11201 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11202 #endif
11204 if (do_update)
11206 Lisp_Object window;
11207 struct window *w;
11209 window = FRAME_SELECTED_WINDOW (f);
11210 w = XWINDOW (window);
11212 /* If the user has switched buffers or windows, we need to
11213 recompute to reflect the new bindings. But we'll
11214 recompute when update_mode_lines is set too; that means
11215 that people can use force-mode-line-update to request
11216 that the menu bar be recomputed. The adverse effect on
11217 the rest of the redisplay algorithm is about the same as
11218 windows_or_buffers_changed anyway. */
11219 if (windows_or_buffers_changed
11220 || !NILP (w->update_mode_line)
11221 || update_mode_lines
11222 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11223 < BUF_MODIFF (XBUFFER (w->buffer)))
11224 != !NILP (w->last_had_star))
11225 || ((!NILP (Vtransient_mark_mode)
11226 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11227 != !NILP (w->region_showing)))
11229 struct buffer *prev = current_buffer;
11230 int count = SPECPDL_INDEX ();
11231 Lisp_Object frame, new_tool_bar;
11232 int new_n_tool_bar;
11233 struct gcpro gcpro1;
11235 /* Set current_buffer to the buffer of the selected
11236 window of the frame, so that we get the right local
11237 keymaps. */
11238 set_buffer_internal_1 (XBUFFER (w->buffer));
11240 /* Save match data, if we must. */
11241 if (save_match_data)
11242 record_unwind_save_match_data ();
11244 /* Make sure that we don't accidentally use bogus keymaps. */
11245 if (NILP (Voverriding_local_map_menu_flag))
11247 specbind (Qoverriding_terminal_local_map, Qnil);
11248 specbind (Qoverriding_local_map, Qnil);
11251 GCPRO1 (new_tool_bar);
11253 /* We must temporarily set the selected frame to this frame
11254 before calling tool_bar_items, because the calculation of
11255 the tool-bar keymap uses the selected frame (see
11256 `tool-bar-make-keymap' in tool-bar.el). */
11257 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11258 XSETFRAME (frame, f);
11259 selected_frame = frame;
11261 /* Build desired tool-bar items from keymaps. */
11262 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11263 &new_n_tool_bar);
11265 /* Redisplay the tool-bar if we changed it. */
11266 if (new_n_tool_bar != f->n_tool_bar_items
11267 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11269 /* Redisplay that happens asynchronously due to an expose event
11270 may access f->tool_bar_items. Make sure we update both
11271 variables within BLOCK_INPUT so no such event interrupts. */
11272 BLOCK_INPUT;
11273 f->tool_bar_items = new_tool_bar;
11274 f->n_tool_bar_items = new_n_tool_bar;
11275 w->update_mode_line = Qt;
11276 UNBLOCK_INPUT;
11279 UNGCPRO;
11281 unbind_to (count, Qnil);
11282 set_buffer_internal_1 (prev);
11288 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11289 F's desired tool-bar contents. F->tool_bar_items must have
11290 been set up previously by calling prepare_menu_bars. */
11292 static void
11293 build_desired_tool_bar_string (struct frame *f)
11295 int i, size, size_needed;
11296 struct gcpro gcpro1, gcpro2, gcpro3;
11297 Lisp_Object image, plist, props;
11299 image = plist = props = Qnil;
11300 GCPRO3 (image, plist, props);
11302 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11303 Otherwise, make a new string. */
11305 /* The size of the string we might be able to reuse. */
11306 size = (STRINGP (f->desired_tool_bar_string)
11307 ? SCHARS (f->desired_tool_bar_string)
11308 : 0);
11310 /* We need one space in the string for each image. */
11311 size_needed = f->n_tool_bar_items;
11313 /* Reuse f->desired_tool_bar_string, if possible. */
11314 if (size < size_needed || NILP (f->desired_tool_bar_string))
11315 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
11316 make_number (' '));
11317 else
11319 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11320 Fremove_text_properties (make_number (0), make_number (size),
11321 props, f->desired_tool_bar_string);
11324 /* Put a `display' property on the string for the images to display,
11325 put a `menu_item' property on tool-bar items with a value that
11326 is the index of the item in F's tool-bar item vector. */
11327 for (i = 0; i < f->n_tool_bar_items; ++i)
11329 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11331 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11332 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11333 int hmargin, vmargin, relief, idx, end;
11335 /* If image is a vector, choose the image according to the
11336 button state. */
11337 image = PROP (TOOL_BAR_ITEM_IMAGES);
11338 if (VECTORP (image))
11340 if (enabled_p)
11341 idx = (selected_p
11342 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11343 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11344 else
11345 idx = (selected_p
11346 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11347 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11349 xassert (ASIZE (image) >= idx);
11350 image = AREF (image, idx);
11352 else
11353 idx = -1;
11355 /* Ignore invalid image specifications. */
11356 if (!valid_image_p (image))
11357 continue;
11359 /* Display the tool-bar button pressed, or depressed. */
11360 plist = Fcopy_sequence (XCDR (image));
11362 /* Compute margin and relief to draw. */
11363 relief = (tool_bar_button_relief >= 0
11364 ? tool_bar_button_relief
11365 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11366 hmargin = vmargin = relief;
11368 if (INTEGERP (Vtool_bar_button_margin)
11369 && XINT (Vtool_bar_button_margin) > 0)
11371 hmargin += XFASTINT (Vtool_bar_button_margin);
11372 vmargin += XFASTINT (Vtool_bar_button_margin);
11374 else if (CONSP (Vtool_bar_button_margin))
11376 if (INTEGERP (XCAR (Vtool_bar_button_margin))
11377 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
11378 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11380 if (INTEGERP (XCDR (Vtool_bar_button_margin))
11381 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
11382 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11385 if (auto_raise_tool_bar_buttons_p)
11387 /* Add a `:relief' property to the image spec if the item is
11388 selected. */
11389 if (selected_p)
11391 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11392 hmargin -= relief;
11393 vmargin -= relief;
11396 else
11398 /* If image is selected, display it pressed, i.e. with a
11399 negative relief. If it's not selected, display it with a
11400 raised relief. */
11401 plist = Fplist_put (plist, QCrelief,
11402 (selected_p
11403 ? make_number (-relief)
11404 : make_number (relief)));
11405 hmargin -= relief;
11406 vmargin -= relief;
11409 /* Put a margin around the image. */
11410 if (hmargin || vmargin)
11412 if (hmargin == vmargin)
11413 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11414 else
11415 plist = Fplist_put (plist, QCmargin,
11416 Fcons (make_number (hmargin),
11417 make_number (vmargin)));
11420 /* If button is not enabled, and we don't have special images
11421 for the disabled state, make the image appear disabled by
11422 applying an appropriate algorithm to it. */
11423 if (!enabled_p && idx < 0)
11424 plist = Fplist_put (plist, QCconversion, Qdisabled);
11426 /* Put a `display' text property on the string for the image to
11427 display. Put a `menu-item' property on the string that gives
11428 the start of this item's properties in the tool-bar items
11429 vector. */
11430 image = Fcons (Qimage, plist);
11431 props = list4 (Qdisplay, image,
11432 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11434 /* Let the last image hide all remaining spaces in the tool bar
11435 string. The string can be longer than needed when we reuse a
11436 previous string. */
11437 if (i + 1 == f->n_tool_bar_items)
11438 end = SCHARS (f->desired_tool_bar_string);
11439 else
11440 end = i + 1;
11441 Fadd_text_properties (make_number (i), make_number (end),
11442 props, f->desired_tool_bar_string);
11443 #undef PROP
11446 UNGCPRO;
11450 /* Display one line of the tool-bar of frame IT->f.
11452 HEIGHT specifies the desired height of the tool-bar line.
11453 If the actual height of the glyph row is less than HEIGHT, the
11454 row's height is increased to HEIGHT, and the icons are centered
11455 vertically in the new height.
11457 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11458 count a final empty row in case the tool-bar width exactly matches
11459 the window width.
11462 static void
11463 display_tool_bar_line (struct it *it, int height)
11465 struct glyph_row *row = it->glyph_row;
11466 int max_x = it->last_visible_x;
11467 struct glyph *last;
11469 prepare_desired_row (row);
11470 row->y = it->current_y;
11472 /* Note that this isn't made use of if the face hasn't a box,
11473 so there's no need to check the face here. */
11474 it->start_of_box_run_p = 1;
11476 while (it->current_x < max_x)
11478 int x, n_glyphs_before, i, nglyphs;
11479 struct it it_before;
11481 /* Get the next display element. */
11482 if (!get_next_display_element (it))
11484 /* Don't count empty row if we are counting needed tool-bar lines. */
11485 if (height < 0 && !it->hpos)
11486 return;
11487 break;
11490 /* Produce glyphs. */
11491 n_glyphs_before = row->used[TEXT_AREA];
11492 it_before = *it;
11494 PRODUCE_GLYPHS (it);
11496 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11497 i = 0;
11498 x = it_before.current_x;
11499 while (i < nglyphs)
11501 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11503 if (x + glyph->pixel_width > max_x)
11505 /* Glyph doesn't fit on line. Backtrack. */
11506 row->used[TEXT_AREA] = n_glyphs_before;
11507 *it = it_before;
11508 /* If this is the only glyph on this line, it will never fit on the
11509 tool-bar, so skip it. But ensure there is at least one glyph,
11510 so we don't accidentally disable the tool-bar. */
11511 if (n_glyphs_before == 0
11512 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11513 break;
11514 goto out;
11517 ++it->hpos;
11518 x += glyph->pixel_width;
11519 ++i;
11522 /* Stop at line end. */
11523 if (ITERATOR_AT_END_OF_LINE_P (it))
11524 break;
11526 set_iterator_to_next (it, 1);
11529 out:;
11531 row->displays_text_p = row->used[TEXT_AREA] != 0;
11533 /* Use default face for the border below the tool bar.
11535 FIXME: When auto-resize-tool-bars is grow-only, there is
11536 no additional border below the possibly empty tool-bar lines.
11537 So to make the extra empty lines look "normal", we have to
11538 use the tool-bar face for the border too. */
11539 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11540 it->face_id = DEFAULT_FACE_ID;
11542 extend_face_to_end_of_line (it);
11543 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11544 last->right_box_line_p = 1;
11545 if (last == row->glyphs[TEXT_AREA])
11546 last->left_box_line_p = 1;
11548 /* Make line the desired height and center it vertically. */
11549 if ((height -= it->max_ascent + it->max_descent) > 0)
11551 /* Don't add more than one line height. */
11552 height %= FRAME_LINE_HEIGHT (it->f);
11553 it->max_ascent += height / 2;
11554 it->max_descent += (height + 1) / 2;
11557 compute_line_metrics (it);
11559 /* If line is empty, make it occupy the rest of the tool-bar. */
11560 if (!row->displays_text_p)
11562 row->height = row->phys_height = it->last_visible_y - row->y;
11563 row->visible_height = row->height;
11564 row->ascent = row->phys_ascent = 0;
11565 row->extra_line_spacing = 0;
11568 row->full_width_p = 1;
11569 row->continued_p = 0;
11570 row->truncated_on_left_p = 0;
11571 row->truncated_on_right_p = 0;
11573 it->current_x = it->hpos = 0;
11574 it->current_y += row->height;
11575 ++it->vpos;
11576 ++it->glyph_row;
11580 /* Max tool-bar height. */
11582 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11583 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11585 /* Value is the number of screen lines needed to make all tool-bar
11586 items of frame F visible. The number of actual rows needed is
11587 returned in *N_ROWS if non-NULL. */
11589 static int
11590 tool_bar_lines_needed (struct frame *f, int *n_rows)
11592 struct window *w = XWINDOW (f->tool_bar_window);
11593 struct it it;
11594 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11595 the desired matrix, so use (unused) mode-line row as temporary row to
11596 avoid destroying the first tool-bar row. */
11597 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11599 /* Initialize an iterator for iteration over
11600 F->desired_tool_bar_string in the tool-bar window of frame F. */
11601 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11602 it.first_visible_x = 0;
11603 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11604 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11605 it.paragraph_embedding = L2R;
11607 while (!ITERATOR_AT_END_P (&it))
11609 clear_glyph_row (temp_row);
11610 it.glyph_row = temp_row;
11611 display_tool_bar_line (&it, -1);
11613 clear_glyph_row (temp_row);
11615 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11616 if (n_rows)
11617 *n_rows = it.vpos > 0 ? it.vpos : -1;
11619 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11623 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11624 0, 1, 0,
11625 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11626 (Lisp_Object frame)
11628 struct frame *f;
11629 struct window *w;
11630 int nlines = 0;
11632 if (NILP (frame))
11633 frame = selected_frame;
11634 else
11635 CHECK_FRAME (frame);
11636 f = XFRAME (frame);
11638 if (WINDOWP (f->tool_bar_window)
11639 && (w = XWINDOW (f->tool_bar_window),
11640 WINDOW_TOTAL_LINES (w) > 0))
11642 update_tool_bar (f, 1);
11643 if (f->n_tool_bar_items)
11645 build_desired_tool_bar_string (f);
11646 nlines = tool_bar_lines_needed (f, NULL);
11650 return make_number (nlines);
11654 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11655 height should be changed. */
11657 static int
11658 redisplay_tool_bar (struct frame *f)
11660 struct window *w;
11661 struct it it;
11662 struct glyph_row *row;
11664 #if defined (USE_GTK) || defined (HAVE_NS)
11665 if (FRAME_EXTERNAL_TOOL_BAR (f))
11666 update_frame_tool_bar (f);
11667 return 0;
11668 #endif
11670 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11671 do anything. This means you must start with tool-bar-lines
11672 non-zero to get the auto-sizing effect. Or in other words, you
11673 can turn off tool-bars by specifying tool-bar-lines zero. */
11674 if (!WINDOWP (f->tool_bar_window)
11675 || (w = XWINDOW (f->tool_bar_window),
11676 WINDOW_TOTAL_LINES (w) == 0))
11677 return 0;
11679 /* Set up an iterator for the tool-bar window. */
11680 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11681 it.first_visible_x = 0;
11682 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11683 row = it.glyph_row;
11685 /* Build a string that represents the contents of the tool-bar. */
11686 build_desired_tool_bar_string (f);
11687 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11688 /* FIXME: This should be controlled by a user option. But it
11689 doesn't make sense to have an R2L tool bar if the menu bar cannot
11690 be drawn also R2L, and making the menu bar R2L is tricky due
11691 toolkit-specific code that implements it. If an R2L tool bar is
11692 ever supported, display_tool_bar_line should also be augmented to
11693 call unproduce_glyphs like display_line and display_string
11694 do. */
11695 it.paragraph_embedding = L2R;
11697 if (f->n_tool_bar_rows == 0)
11699 int nlines;
11701 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11702 nlines != WINDOW_TOTAL_LINES (w)))
11704 Lisp_Object frame;
11705 int old_height = WINDOW_TOTAL_LINES (w);
11707 XSETFRAME (frame, f);
11708 Fmodify_frame_parameters (frame,
11709 Fcons (Fcons (Qtool_bar_lines,
11710 make_number (nlines)),
11711 Qnil));
11712 if (WINDOW_TOTAL_LINES (w) != old_height)
11714 clear_glyph_matrix (w->desired_matrix);
11715 fonts_changed_p = 1;
11716 return 1;
11721 /* Display as many lines as needed to display all tool-bar items. */
11723 if (f->n_tool_bar_rows > 0)
11725 int border, rows, height, extra;
11727 if (INTEGERP (Vtool_bar_border))
11728 border = XINT (Vtool_bar_border);
11729 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11730 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11731 else if (EQ (Vtool_bar_border, Qborder_width))
11732 border = f->border_width;
11733 else
11734 border = 0;
11735 if (border < 0)
11736 border = 0;
11738 rows = f->n_tool_bar_rows;
11739 height = max (1, (it.last_visible_y - border) / rows);
11740 extra = it.last_visible_y - border - height * rows;
11742 while (it.current_y < it.last_visible_y)
11744 int h = 0;
11745 if (extra > 0 && rows-- > 0)
11747 h = (extra + rows - 1) / rows;
11748 extra -= h;
11750 display_tool_bar_line (&it, height + h);
11753 else
11755 while (it.current_y < it.last_visible_y)
11756 display_tool_bar_line (&it, 0);
11759 /* It doesn't make much sense to try scrolling in the tool-bar
11760 window, so don't do it. */
11761 w->desired_matrix->no_scrolling_p = 1;
11762 w->must_be_updated_p = 1;
11764 if (!NILP (Vauto_resize_tool_bars))
11766 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11767 int change_height_p = 0;
11769 /* If we couldn't display everything, change the tool-bar's
11770 height if there is room for more. */
11771 if (IT_STRING_CHARPOS (it) < it.end_charpos
11772 && it.current_y < max_tool_bar_height)
11773 change_height_p = 1;
11775 row = it.glyph_row - 1;
11777 /* If there are blank lines at the end, except for a partially
11778 visible blank line at the end that is smaller than
11779 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11780 if (!row->displays_text_p
11781 && row->height >= FRAME_LINE_HEIGHT (f))
11782 change_height_p = 1;
11784 /* If row displays tool-bar items, but is partially visible,
11785 change the tool-bar's height. */
11786 if (row->displays_text_p
11787 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11788 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11789 change_height_p = 1;
11791 /* Resize windows as needed by changing the `tool-bar-lines'
11792 frame parameter. */
11793 if (change_height_p)
11795 Lisp_Object frame;
11796 int old_height = WINDOW_TOTAL_LINES (w);
11797 int nrows;
11798 int nlines = tool_bar_lines_needed (f, &nrows);
11800 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11801 && !f->minimize_tool_bar_window_p)
11802 ? (nlines > old_height)
11803 : (nlines != old_height));
11804 f->minimize_tool_bar_window_p = 0;
11806 if (change_height_p)
11808 XSETFRAME (frame, f);
11809 Fmodify_frame_parameters (frame,
11810 Fcons (Fcons (Qtool_bar_lines,
11811 make_number (nlines)),
11812 Qnil));
11813 if (WINDOW_TOTAL_LINES (w) != old_height)
11815 clear_glyph_matrix (w->desired_matrix);
11816 f->n_tool_bar_rows = nrows;
11817 fonts_changed_p = 1;
11818 return 1;
11824 f->minimize_tool_bar_window_p = 0;
11825 return 0;
11829 /* Get information about the tool-bar item which is displayed in GLYPH
11830 on frame F. Return in *PROP_IDX the index where tool-bar item
11831 properties start in F->tool_bar_items. Value is zero if
11832 GLYPH doesn't display a tool-bar item. */
11834 static int
11835 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11837 Lisp_Object prop;
11838 int success_p;
11839 int charpos;
11841 /* This function can be called asynchronously, which means we must
11842 exclude any possibility that Fget_text_property signals an
11843 error. */
11844 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
11845 charpos = max (0, charpos);
11847 /* Get the text property `menu-item' at pos. The value of that
11848 property is the start index of this item's properties in
11849 F->tool_bar_items. */
11850 prop = Fget_text_property (make_number (charpos),
11851 Qmenu_item, f->current_tool_bar_string);
11852 if (INTEGERP (prop))
11854 *prop_idx = XINT (prop);
11855 success_p = 1;
11857 else
11858 success_p = 0;
11860 return success_p;
11864 /* Get information about the tool-bar item at position X/Y on frame F.
11865 Return in *GLYPH a pointer to the glyph of the tool-bar item in
11866 the current matrix of the tool-bar window of F, or NULL if not
11867 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
11868 item in F->tool_bar_items. Value is
11870 -1 if X/Y is not on a tool-bar item
11871 0 if X/Y is on the same item that was highlighted before.
11872 1 otherwise. */
11874 static int
11875 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
11876 int *hpos, int *vpos, int *prop_idx)
11878 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11879 struct window *w = XWINDOW (f->tool_bar_window);
11880 int area;
11882 /* Find the glyph under X/Y. */
11883 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
11884 if (*glyph == NULL)
11885 return -1;
11887 /* Get the start of this tool-bar item's properties in
11888 f->tool_bar_items. */
11889 if (!tool_bar_item_info (f, *glyph, prop_idx))
11890 return -1;
11892 /* Is mouse on the highlighted item? */
11893 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
11894 && *vpos >= hlinfo->mouse_face_beg_row
11895 && *vpos <= hlinfo->mouse_face_end_row
11896 && (*vpos > hlinfo->mouse_face_beg_row
11897 || *hpos >= hlinfo->mouse_face_beg_col)
11898 && (*vpos < hlinfo->mouse_face_end_row
11899 || *hpos < hlinfo->mouse_face_end_col
11900 || hlinfo->mouse_face_past_end))
11901 return 0;
11903 return 1;
11907 /* EXPORT:
11908 Handle mouse button event on the tool-bar of frame F, at
11909 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
11910 0 for button release. MODIFIERS is event modifiers for button
11911 release. */
11913 void
11914 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
11915 unsigned int modifiers)
11917 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11918 struct window *w = XWINDOW (f->tool_bar_window);
11919 int hpos, vpos, prop_idx;
11920 struct glyph *glyph;
11921 Lisp_Object enabled_p;
11923 /* If not on the highlighted tool-bar item, return. */
11924 frame_to_window_pixel_xy (w, &x, &y);
11925 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
11926 return;
11928 /* If item is disabled, do nothing. */
11929 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11930 if (NILP (enabled_p))
11931 return;
11933 if (down_p)
11935 /* Show item in pressed state. */
11936 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
11937 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
11938 last_tool_bar_item = prop_idx;
11940 else
11942 Lisp_Object key, frame;
11943 struct input_event event;
11944 EVENT_INIT (event);
11946 /* Show item in released state. */
11947 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
11948 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
11950 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
11952 XSETFRAME (frame, f);
11953 event.kind = TOOL_BAR_EVENT;
11954 event.frame_or_window = frame;
11955 event.arg = frame;
11956 kbd_buffer_store_event (&event);
11958 event.kind = TOOL_BAR_EVENT;
11959 event.frame_or_window = frame;
11960 event.arg = key;
11961 event.modifiers = modifiers;
11962 kbd_buffer_store_event (&event);
11963 last_tool_bar_item = -1;
11968 /* Possibly highlight a tool-bar item on frame F when mouse moves to
11969 tool-bar window-relative coordinates X/Y. Called from
11970 note_mouse_highlight. */
11972 static void
11973 note_tool_bar_highlight (struct frame *f, int x, int y)
11975 Lisp_Object window = f->tool_bar_window;
11976 struct window *w = XWINDOW (window);
11977 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11978 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11979 int hpos, vpos;
11980 struct glyph *glyph;
11981 struct glyph_row *row;
11982 int i;
11983 Lisp_Object enabled_p;
11984 int prop_idx;
11985 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
11986 int mouse_down_p, rc;
11988 /* Function note_mouse_highlight is called with negative X/Y
11989 values when mouse moves outside of the frame. */
11990 if (x <= 0 || y <= 0)
11992 clear_mouse_face (hlinfo);
11993 return;
11996 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
11997 if (rc < 0)
11999 /* Not on tool-bar item. */
12000 clear_mouse_face (hlinfo);
12001 return;
12003 else if (rc == 0)
12004 /* On same tool-bar item as before. */
12005 goto set_help_echo;
12007 clear_mouse_face (hlinfo);
12009 /* Mouse is down, but on different tool-bar item? */
12010 mouse_down_p = (dpyinfo->grabbed
12011 && f == last_mouse_frame
12012 && FRAME_LIVE_P (f));
12013 if (mouse_down_p
12014 && last_tool_bar_item != prop_idx)
12015 return;
12017 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12018 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12020 /* If tool-bar item is not enabled, don't highlight it. */
12021 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12022 if (!NILP (enabled_p))
12024 /* Compute the x-position of the glyph. In front and past the
12025 image is a space. We include this in the highlighted area. */
12026 row = MATRIX_ROW (w->current_matrix, vpos);
12027 for (i = x = 0; i < hpos; ++i)
12028 x += row->glyphs[TEXT_AREA][i].pixel_width;
12030 /* Record this as the current active region. */
12031 hlinfo->mouse_face_beg_col = hpos;
12032 hlinfo->mouse_face_beg_row = vpos;
12033 hlinfo->mouse_face_beg_x = x;
12034 hlinfo->mouse_face_beg_y = row->y;
12035 hlinfo->mouse_face_past_end = 0;
12037 hlinfo->mouse_face_end_col = hpos + 1;
12038 hlinfo->mouse_face_end_row = vpos;
12039 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12040 hlinfo->mouse_face_end_y = row->y;
12041 hlinfo->mouse_face_window = window;
12042 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12044 /* Display it as active. */
12045 show_mouse_face (hlinfo, draw);
12046 hlinfo->mouse_face_image_state = draw;
12049 set_help_echo:
12051 /* Set help_echo_string to a help string to display for this tool-bar item.
12052 XTread_socket does the rest. */
12053 help_echo_object = help_echo_window = Qnil;
12054 help_echo_pos = -1;
12055 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12056 if (NILP (help_echo_string))
12057 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12060 #endif /* HAVE_WINDOW_SYSTEM */
12064 /************************************************************************
12065 Horizontal scrolling
12066 ************************************************************************/
12068 static int hscroll_window_tree (Lisp_Object);
12069 static int hscroll_windows (Lisp_Object);
12071 /* For all leaf windows in the window tree rooted at WINDOW, set their
12072 hscroll value so that PT is (i) visible in the window, and (ii) so
12073 that it is not within a certain margin at the window's left and
12074 right border. Value is non-zero if any window's hscroll has been
12075 changed. */
12077 static int
12078 hscroll_window_tree (Lisp_Object window)
12080 int hscrolled_p = 0;
12081 int hscroll_relative_p = FLOATP (Vhscroll_step);
12082 int hscroll_step_abs = 0;
12083 double hscroll_step_rel = 0;
12085 if (hscroll_relative_p)
12087 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12088 if (hscroll_step_rel < 0)
12090 hscroll_relative_p = 0;
12091 hscroll_step_abs = 0;
12094 else if (INTEGERP (Vhscroll_step))
12096 hscroll_step_abs = XINT (Vhscroll_step);
12097 if (hscroll_step_abs < 0)
12098 hscroll_step_abs = 0;
12100 else
12101 hscroll_step_abs = 0;
12103 while (WINDOWP (window))
12105 struct window *w = XWINDOW (window);
12107 if (WINDOWP (w->hchild))
12108 hscrolled_p |= hscroll_window_tree (w->hchild);
12109 else if (WINDOWP (w->vchild))
12110 hscrolled_p |= hscroll_window_tree (w->vchild);
12111 else if (w->cursor.vpos >= 0)
12113 int h_margin;
12114 int text_area_width;
12115 struct glyph_row *current_cursor_row
12116 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12117 struct glyph_row *desired_cursor_row
12118 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12119 struct glyph_row *cursor_row
12120 = (desired_cursor_row->enabled_p
12121 ? desired_cursor_row
12122 : current_cursor_row);
12123 int row_r2l_p = cursor_row->reversed_p;
12125 text_area_width = window_box_width (w, TEXT_AREA);
12127 /* Scroll when cursor is inside this scroll margin. */
12128 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12130 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12131 /* For left-to-right rows, hscroll when cursor is either
12132 (i) inside the right hscroll margin, or (ii) if it is
12133 inside the left margin and the window is already
12134 hscrolled. */
12135 && ((!row_r2l_p
12136 && ((XFASTINT (w->hscroll)
12137 && w->cursor.x <= h_margin)
12138 || (cursor_row->enabled_p
12139 && cursor_row->truncated_on_right_p
12140 && (w->cursor.x >= text_area_width - h_margin))))
12141 /* For right-to-left rows, the logic is similar,
12142 except that rules for scrolling to left and right
12143 are reversed. E.g., if cursor.x <= h_margin, we
12144 need to hscroll "to the right" unconditionally,
12145 and that will scroll the screen to the left so as
12146 to reveal the next portion of the row. */
12147 || (row_r2l_p
12148 && ((cursor_row->enabled_p
12149 /* FIXME: It is confusing to set the
12150 truncated_on_right_p flag when R2L rows
12151 are actually truncated on the left. */
12152 && cursor_row->truncated_on_right_p
12153 && w->cursor.x <= h_margin)
12154 || (XFASTINT (w->hscroll)
12155 && (w->cursor.x >= text_area_width - h_margin))))))
12157 struct it it;
12158 int hscroll;
12159 struct buffer *saved_current_buffer;
12160 EMACS_INT pt;
12161 int wanted_x;
12163 /* Find point in a display of infinite width. */
12164 saved_current_buffer = current_buffer;
12165 current_buffer = XBUFFER (w->buffer);
12167 if (w == XWINDOW (selected_window))
12168 pt = PT;
12169 else
12171 pt = marker_position (w->pointm);
12172 pt = max (BEGV, pt);
12173 pt = min (ZV, pt);
12176 /* Move iterator to pt starting at cursor_row->start in
12177 a line with infinite width. */
12178 init_to_row_start (&it, w, cursor_row);
12179 it.last_visible_x = INFINITY;
12180 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12181 current_buffer = saved_current_buffer;
12183 /* Position cursor in window. */
12184 if (!hscroll_relative_p && hscroll_step_abs == 0)
12185 hscroll = max (0, (it.current_x
12186 - (ITERATOR_AT_END_OF_LINE_P (&it)
12187 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12188 : (text_area_width / 2))))
12189 / FRAME_COLUMN_WIDTH (it.f);
12190 else if ((!row_r2l_p
12191 && w->cursor.x >= text_area_width - h_margin)
12192 || (row_r2l_p && w->cursor.x <= h_margin))
12194 if (hscroll_relative_p)
12195 wanted_x = text_area_width * (1 - hscroll_step_rel)
12196 - h_margin;
12197 else
12198 wanted_x = text_area_width
12199 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12200 - h_margin;
12201 hscroll
12202 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12204 else
12206 if (hscroll_relative_p)
12207 wanted_x = text_area_width * hscroll_step_rel
12208 + h_margin;
12209 else
12210 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12211 + h_margin;
12212 hscroll
12213 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12215 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
12217 /* Don't prevent redisplay optimizations if hscroll
12218 hasn't changed, as it will unnecessarily slow down
12219 redisplay. */
12220 if (XFASTINT (w->hscroll) != hscroll)
12222 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12223 w->hscroll = make_number (hscroll);
12224 hscrolled_p = 1;
12229 window = w->next;
12232 /* Value is non-zero if hscroll of any leaf window has been changed. */
12233 return hscrolled_p;
12237 /* Set hscroll so that cursor is visible and not inside horizontal
12238 scroll margins for all windows in the tree rooted at WINDOW. See
12239 also hscroll_window_tree above. Value is non-zero if any window's
12240 hscroll has been changed. If it has, desired matrices on the frame
12241 of WINDOW are cleared. */
12243 static int
12244 hscroll_windows (Lisp_Object window)
12246 int hscrolled_p = hscroll_window_tree (window);
12247 if (hscrolled_p)
12248 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12249 return hscrolled_p;
12254 /************************************************************************
12255 Redisplay
12256 ************************************************************************/
12258 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12259 to a non-zero value. This is sometimes handy to have in a debugger
12260 session. */
12262 #if GLYPH_DEBUG
12264 /* First and last unchanged row for try_window_id. */
12266 static int debug_first_unchanged_at_end_vpos;
12267 static int debug_last_unchanged_at_beg_vpos;
12269 /* Delta vpos and y. */
12271 static int debug_dvpos, debug_dy;
12273 /* Delta in characters and bytes for try_window_id. */
12275 static EMACS_INT debug_delta, debug_delta_bytes;
12277 /* Values of window_end_pos and window_end_vpos at the end of
12278 try_window_id. */
12280 static EMACS_INT debug_end_vpos;
12282 /* Append a string to W->desired_matrix->method. FMT is a printf
12283 format string. If trace_redisplay_p is non-zero also printf the
12284 resulting string to stderr. */
12286 static void debug_method_add (struct window *, char const *, ...)
12287 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12289 static void
12290 debug_method_add (struct window *w, char const *fmt, ...)
12292 char buffer[512];
12293 char *method = w->desired_matrix->method;
12294 int len = strlen (method);
12295 int size = sizeof w->desired_matrix->method;
12296 int remaining = size - len - 1;
12297 va_list ap;
12299 va_start (ap, fmt);
12300 vsprintf (buffer, fmt, ap);
12301 va_end (ap);
12302 if (len && remaining)
12304 method[len] = '|';
12305 --remaining, ++len;
12308 strncpy (method + len, buffer, remaining);
12310 if (trace_redisplay_p)
12311 fprintf (stderr, "%p (%s): %s\n",
12313 ((BUFFERP (w->buffer)
12314 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12315 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12316 : "no buffer"),
12317 buffer);
12320 #endif /* GLYPH_DEBUG */
12323 /* Value is non-zero if all changes in window W, which displays
12324 current_buffer, are in the text between START and END. START is a
12325 buffer position, END is given as a distance from Z. Used in
12326 redisplay_internal for display optimization. */
12328 static inline int
12329 text_outside_line_unchanged_p (struct window *w,
12330 EMACS_INT start, EMACS_INT end)
12332 int unchanged_p = 1;
12334 /* If text or overlays have changed, see where. */
12335 if (XFASTINT (w->last_modified) < MODIFF
12336 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12338 /* Gap in the line? */
12339 if (GPT < start || Z - GPT < end)
12340 unchanged_p = 0;
12342 /* Changes start in front of the line, or end after it? */
12343 if (unchanged_p
12344 && (BEG_UNCHANGED < start - 1
12345 || END_UNCHANGED < end))
12346 unchanged_p = 0;
12348 /* If selective display, can't optimize if changes start at the
12349 beginning of the line. */
12350 if (unchanged_p
12351 && INTEGERP (BVAR (current_buffer, selective_display))
12352 && XINT (BVAR (current_buffer, selective_display)) > 0
12353 && (BEG_UNCHANGED < start || GPT <= start))
12354 unchanged_p = 0;
12356 /* If there are overlays at the start or end of the line, these
12357 may have overlay strings with newlines in them. A change at
12358 START, for instance, may actually concern the display of such
12359 overlay strings as well, and they are displayed on different
12360 lines. So, quickly rule out this case. (For the future, it
12361 might be desirable to implement something more telling than
12362 just BEG/END_UNCHANGED.) */
12363 if (unchanged_p)
12365 if (BEG + BEG_UNCHANGED == start
12366 && overlay_touches_p (start))
12367 unchanged_p = 0;
12368 if (END_UNCHANGED == end
12369 && overlay_touches_p (Z - end))
12370 unchanged_p = 0;
12373 /* Under bidi reordering, adding or deleting a character in the
12374 beginning of a paragraph, before the first strong directional
12375 character, can change the base direction of the paragraph (unless
12376 the buffer specifies a fixed paragraph direction), which will
12377 require to redisplay the whole paragraph. It might be worthwhile
12378 to find the paragraph limits and widen the range of redisplayed
12379 lines to that, but for now just give up this optimization. */
12380 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12381 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12382 unchanged_p = 0;
12385 return unchanged_p;
12389 /* Do a frame update, taking possible shortcuts into account. This is
12390 the main external entry point for redisplay.
12392 If the last redisplay displayed an echo area message and that message
12393 is no longer requested, we clear the echo area or bring back the
12394 mini-buffer if that is in use. */
12396 void
12397 redisplay (void)
12399 redisplay_internal ();
12403 static Lisp_Object
12404 overlay_arrow_string_or_property (Lisp_Object var)
12406 Lisp_Object val;
12408 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12409 return val;
12411 return Voverlay_arrow_string;
12414 /* Return 1 if there are any overlay-arrows in current_buffer. */
12415 static int
12416 overlay_arrow_in_current_buffer_p (void)
12418 Lisp_Object vlist;
12420 for (vlist = Voverlay_arrow_variable_list;
12421 CONSP (vlist);
12422 vlist = XCDR (vlist))
12424 Lisp_Object var = XCAR (vlist);
12425 Lisp_Object val;
12427 if (!SYMBOLP (var))
12428 continue;
12429 val = find_symbol_value (var);
12430 if (MARKERP (val)
12431 && current_buffer == XMARKER (val)->buffer)
12432 return 1;
12434 return 0;
12438 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12439 has changed. */
12441 static int
12442 overlay_arrows_changed_p (void)
12444 Lisp_Object vlist;
12446 for (vlist = Voverlay_arrow_variable_list;
12447 CONSP (vlist);
12448 vlist = XCDR (vlist))
12450 Lisp_Object var = XCAR (vlist);
12451 Lisp_Object val, pstr;
12453 if (!SYMBOLP (var))
12454 continue;
12455 val = find_symbol_value (var);
12456 if (!MARKERP (val))
12457 continue;
12458 if (! EQ (COERCE_MARKER (val),
12459 Fget (var, Qlast_arrow_position))
12460 || ! (pstr = overlay_arrow_string_or_property (var),
12461 EQ (pstr, Fget (var, Qlast_arrow_string))))
12462 return 1;
12464 return 0;
12467 /* Mark overlay arrows to be updated on next redisplay. */
12469 static void
12470 update_overlay_arrows (int up_to_date)
12472 Lisp_Object vlist;
12474 for (vlist = Voverlay_arrow_variable_list;
12475 CONSP (vlist);
12476 vlist = XCDR (vlist))
12478 Lisp_Object var = XCAR (vlist);
12480 if (!SYMBOLP (var))
12481 continue;
12483 if (up_to_date > 0)
12485 Lisp_Object val = find_symbol_value (var);
12486 Fput (var, Qlast_arrow_position,
12487 COERCE_MARKER (val));
12488 Fput (var, Qlast_arrow_string,
12489 overlay_arrow_string_or_property (var));
12491 else if (up_to_date < 0
12492 || !NILP (Fget (var, Qlast_arrow_position)))
12494 Fput (var, Qlast_arrow_position, Qt);
12495 Fput (var, Qlast_arrow_string, Qt);
12501 /* Return overlay arrow string to display at row.
12502 Return integer (bitmap number) for arrow bitmap in left fringe.
12503 Return nil if no overlay arrow. */
12505 static Lisp_Object
12506 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12508 Lisp_Object vlist;
12510 for (vlist = Voverlay_arrow_variable_list;
12511 CONSP (vlist);
12512 vlist = XCDR (vlist))
12514 Lisp_Object var = XCAR (vlist);
12515 Lisp_Object val;
12517 if (!SYMBOLP (var))
12518 continue;
12520 val = find_symbol_value (var);
12522 if (MARKERP (val)
12523 && current_buffer == XMARKER (val)->buffer
12524 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12526 if (FRAME_WINDOW_P (it->f)
12527 /* FIXME: if ROW->reversed_p is set, this should test
12528 the right fringe, not the left one. */
12529 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12531 #ifdef HAVE_WINDOW_SYSTEM
12532 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12534 int fringe_bitmap;
12535 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12536 return make_number (fringe_bitmap);
12538 #endif
12539 return make_number (-1); /* Use default arrow bitmap */
12541 return overlay_arrow_string_or_property (var);
12545 return Qnil;
12548 /* Return 1 if point moved out of or into a composition. Otherwise
12549 return 0. PREV_BUF and PREV_PT are the last point buffer and
12550 position. BUF and PT are the current point buffer and position. */
12552 static int
12553 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
12554 struct buffer *buf, EMACS_INT pt)
12556 EMACS_INT start, end;
12557 Lisp_Object prop;
12558 Lisp_Object buffer;
12560 XSETBUFFER (buffer, buf);
12561 /* Check a composition at the last point if point moved within the
12562 same buffer. */
12563 if (prev_buf == buf)
12565 if (prev_pt == pt)
12566 /* Point didn't move. */
12567 return 0;
12569 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12570 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12571 && COMPOSITION_VALID_P (start, end, prop)
12572 && start < prev_pt && end > prev_pt)
12573 /* The last point was within the composition. Return 1 iff
12574 point moved out of the composition. */
12575 return (pt <= start || pt >= end);
12578 /* Check a composition at the current point. */
12579 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12580 && find_composition (pt, -1, &start, &end, &prop, buffer)
12581 && COMPOSITION_VALID_P (start, end, prop)
12582 && start < pt && end > pt);
12586 /* Reconsider the setting of B->clip_changed which is displayed
12587 in window W. */
12589 static inline void
12590 reconsider_clip_changes (struct window *w, struct buffer *b)
12592 if (b->clip_changed
12593 && !NILP (w->window_end_valid)
12594 && w->current_matrix->buffer == b
12595 && w->current_matrix->zv == BUF_ZV (b)
12596 && w->current_matrix->begv == BUF_BEGV (b))
12597 b->clip_changed = 0;
12599 /* If display wasn't paused, and W is not a tool bar window, see if
12600 point has been moved into or out of a composition. In that case,
12601 we set b->clip_changed to 1 to force updating the screen. If
12602 b->clip_changed has already been set to 1, we can skip this
12603 check. */
12604 if (!b->clip_changed
12605 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12607 EMACS_INT pt;
12609 if (w == XWINDOW (selected_window))
12610 pt = PT;
12611 else
12612 pt = marker_position (w->pointm);
12614 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12615 || pt != XINT (w->last_point))
12616 && check_point_in_composition (w->current_matrix->buffer,
12617 XINT (w->last_point),
12618 XBUFFER (w->buffer), pt))
12619 b->clip_changed = 1;
12624 /* Select FRAME to forward the values of frame-local variables into C
12625 variables so that the redisplay routines can access those values
12626 directly. */
12628 static void
12629 select_frame_for_redisplay (Lisp_Object frame)
12631 Lisp_Object tail, tem;
12632 Lisp_Object old = selected_frame;
12633 struct Lisp_Symbol *sym;
12635 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12637 selected_frame = frame;
12639 do {
12640 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12641 if (CONSP (XCAR (tail))
12642 && (tem = XCAR (XCAR (tail)),
12643 SYMBOLP (tem))
12644 && (sym = indirect_variable (XSYMBOL (tem)),
12645 sym->redirect == SYMBOL_LOCALIZED)
12646 && sym->val.blv->frame_local)
12647 /* Use find_symbol_value rather than Fsymbol_value
12648 to avoid an error if it is void. */
12649 find_symbol_value (tem);
12650 } while (!EQ (frame, old) && (frame = old, 1));
12654 #define STOP_POLLING \
12655 do { if (! polling_stopped_here) stop_polling (); \
12656 polling_stopped_here = 1; } while (0)
12658 #define RESUME_POLLING \
12659 do { if (polling_stopped_here) start_polling (); \
12660 polling_stopped_here = 0; } while (0)
12663 /* Perhaps in the future avoid recentering windows if it
12664 is not necessary; currently that causes some problems. */
12666 static void
12667 redisplay_internal (void)
12669 struct window *w = XWINDOW (selected_window);
12670 struct window *sw;
12671 struct frame *fr;
12672 int pending;
12673 int must_finish = 0;
12674 struct text_pos tlbufpos, tlendpos;
12675 int number_of_visible_frames;
12676 int count, count1;
12677 struct frame *sf;
12678 int polling_stopped_here = 0;
12679 Lisp_Object old_frame = selected_frame;
12681 /* Non-zero means redisplay has to consider all windows on all
12682 frames. Zero means, only selected_window is considered. */
12683 int consider_all_windows_p;
12685 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12687 /* No redisplay if running in batch mode or frame is not yet fully
12688 initialized, or redisplay is explicitly turned off by setting
12689 Vinhibit_redisplay. */
12690 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12691 || !NILP (Vinhibit_redisplay))
12692 return;
12694 /* Don't examine these until after testing Vinhibit_redisplay.
12695 When Emacs is shutting down, perhaps because its connection to
12696 X has dropped, we should not look at them at all. */
12697 fr = XFRAME (w->frame);
12698 sf = SELECTED_FRAME ();
12700 if (!fr->glyphs_initialized_p)
12701 return;
12703 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12704 if (popup_activated ())
12705 return;
12706 #endif
12708 /* I don't think this happens but let's be paranoid. */
12709 if (redisplaying_p)
12710 return;
12712 /* Record a function that resets redisplaying_p to its old value
12713 when we leave this function. */
12714 count = SPECPDL_INDEX ();
12715 record_unwind_protect (unwind_redisplay,
12716 Fcons (make_number (redisplaying_p), selected_frame));
12717 ++redisplaying_p;
12718 specbind (Qinhibit_free_realized_faces, Qnil);
12721 Lisp_Object tail, frame;
12723 FOR_EACH_FRAME (tail, frame)
12725 struct frame *f = XFRAME (frame);
12726 f->already_hscrolled_p = 0;
12730 retry:
12731 /* Remember the currently selected window. */
12732 sw = w;
12734 if (!EQ (old_frame, selected_frame)
12735 && FRAME_LIVE_P (XFRAME (old_frame)))
12736 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12737 selected_frame and selected_window to be temporarily out-of-sync so
12738 when we come back here via `goto retry', we need to resync because we
12739 may need to run Elisp code (via prepare_menu_bars). */
12740 select_frame_for_redisplay (old_frame);
12742 pending = 0;
12743 reconsider_clip_changes (w, current_buffer);
12744 last_escape_glyph_frame = NULL;
12745 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12746 last_glyphless_glyph_frame = NULL;
12747 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12749 /* If new fonts have been loaded that make a glyph matrix adjustment
12750 necessary, do it. */
12751 if (fonts_changed_p)
12753 adjust_glyphs (NULL);
12754 ++windows_or_buffers_changed;
12755 fonts_changed_p = 0;
12758 /* If face_change_count is non-zero, init_iterator will free all
12759 realized faces, which includes the faces referenced from current
12760 matrices. So, we can't reuse current matrices in this case. */
12761 if (face_change_count)
12762 ++windows_or_buffers_changed;
12764 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12765 && FRAME_TTY (sf)->previous_frame != sf)
12767 /* Since frames on a single ASCII terminal share the same
12768 display area, displaying a different frame means redisplay
12769 the whole thing. */
12770 windows_or_buffers_changed++;
12771 SET_FRAME_GARBAGED (sf);
12772 #ifndef DOS_NT
12773 set_tty_color_mode (FRAME_TTY (sf), sf);
12774 #endif
12775 FRAME_TTY (sf)->previous_frame = sf;
12778 /* Set the visible flags for all frames. Do this before checking
12779 for resized or garbaged frames; they want to know if their frames
12780 are visible. See the comment in frame.h for
12781 FRAME_SAMPLE_VISIBILITY. */
12783 Lisp_Object tail, frame;
12785 number_of_visible_frames = 0;
12787 FOR_EACH_FRAME (tail, frame)
12789 struct frame *f = XFRAME (frame);
12791 FRAME_SAMPLE_VISIBILITY (f);
12792 if (FRAME_VISIBLE_P (f))
12793 ++number_of_visible_frames;
12794 clear_desired_matrices (f);
12798 /* Notice any pending interrupt request to change frame size. */
12799 do_pending_window_change (1);
12801 /* do_pending_window_change could change the selected_window due to
12802 frame resizing which makes the selected window too small. */
12803 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12805 sw = w;
12806 reconsider_clip_changes (w, current_buffer);
12809 /* Clear frames marked as garbaged. */
12810 if (frame_garbaged)
12811 clear_garbaged_frames ();
12813 /* Build menubar and tool-bar items. */
12814 if (NILP (Vmemory_full))
12815 prepare_menu_bars ();
12817 if (windows_or_buffers_changed)
12818 update_mode_lines++;
12820 /* Detect case that we need to write or remove a star in the mode line. */
12821 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
12823 w->update_mode_line = Qt;
12824 if (buffer_shared > 1)
12825 update_mode_lines++;
12828 /* Avoid invocation of point motion hooks by `current_column' below. */
12829 count1 = SPECPDL_INDEX ();
12830 specbind (Qinhibit_point_motion_hooks, Qt);
12832 /* If %c is in the mode line, update it if needed. */
12833 if (!NILP (w->column_number_displayed)
12834 /* This alternative quickly identifies a common case
12835 where no change is needed. */
12836 && !(PT == XFASTINT (w->last_point)
12837 && XFASTINT (w->last_modified) >= MODIFF
12838 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12839 && (XFASTINT (w->column_number_displayed) != current_column ()))
12840 w->update_mode_line = Qt;
12842 unbind_to (count1, Qnil);
12844 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12846 /* The variable buffer_shared is set in redisplay_window and
12847 indicates that we redisplay a buffer in different windows. See
12848 there. */
12849 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
12850 || cursor_type_changed);
12852 /* If specs for an arrow have changed, do thorough redisplay
12853 to ensure we remove any arrow that should no longer exist. */
12854 if (overlay_arrows_changed_p ())
12855 consider_all_windows_p = windows_or_buffers_changed = 1;
12857 /* Normally the message* functions will have already displayed and
12858 updated the echo area, but the frame may have been trashed, or
12859 the update may have been preempted, so display the echo area
12860 again here. Checking message_cleared_p captures the case that
12861 the echo area should be cleared. */
12862 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12863 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12864 || (message_cleared_p
12865 && minibuf_level == 0
12866 /* If the mini-window is currently selected, this means the
12867 echo-area doesn't show through. */
12868 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12870 int window_height_changed_p = echo_area_display (0);
12871 must_finish = 1;
12873 /* If we don't display the current message, don't clear the
12874 message_cleared_p flag, because, if we did, we wouldn't clear
12875 the echo area in the next redisplay which doesn't preserve
12876 the echo area. */
12877 if (!display_last_displayed_message_p)
12878 message_cleared_p = 0;
12880 if (fonts_changed_p)
12881 goto retry;
12882 else if (window_height_changed_p)
12884 consider_all_windows_p = 1;
12885 ++update_mode_lines;
12886 ++windows_or_buffers_changed;
12888 /* If window configuration was changed, frames may have been
12889 marked garbaged. Clear them or we will experience
12890 surprises wrt scrolling. */
12891 if (frame_garbaged)
12892 clear_garbaged_frames ();
12895 else if (EQ (selected_window, minibuf_window)
12896 && (current_buffer->clip_changed
12897 || XFASTINT (w->last_modified) < MODIFF
12898 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12899 && resize_mini_window (w, 0))
12901 /* Resized active mini-window to fit the size of what it is
12902 showing if its contents might have changed. */
12903 must_finish = 1;
12904 /* FIXME: this causes all frames to be updated, which seems unnecessary
12905 since only the current frame needs to be considered. This function needs
12906 to be rewritten with two variables, consider_all_windows and
12907 consider_all_frames. */
12908 consider_all_windows_p = 1;
12909 ++windows_or_buffers_changed;
12910 ++update_mode_lines;
12912 /* If window configuration was changed, frames may have been
12913 marked garbaged. Clear them or we will experience
12914 surprises wrt scrolling. */
12915 if (frame_garbaged)
12916 clear_garbaged_frames ();
12920 /* If showing the region, and mark has changed, we must redisplay
12921 the whole window. The assignment to this_line_start_pos prevents
12922 the optimization directly below this if-statement. */
12923 if (((!NILP (Vtransient_mark_mode)
12924 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
12925 != !NILP (w->region_showing))
12926 || (!NILP (w->region_showing)
12927 && !EQ (w->region_showing,
12928 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
12929 CHARPOS (this_line_start_pos) = 0;
12931 /* Optimize the case that only the line containing the cursor in the
12932 selected window has changed. Variables starting with this_ are
12933 set in display_line and record information about the line
12934 containing the cursor. */
12935 tlbufpos = this_line_start_pos;
12936 tlendpos = this_line_end_pos;
12937 if (!consider_all_windows_p
12938 && CHARPOS (tlbufpos) > 0
12939 && NILP (w->update_mode_line)
12940 && !current_buffer->clip_changed
12941 && !current_buffer->prevent_redisplay_optimizations_p
12942 && FRAME_VISIBLE_P (XFRAME (w->frame))
12943 && !FRAME_OBSCURED_P (XFRAME (w->frame))
12944 /* Make sure recorded data applies to current buffer, etc. */
12945 && this_line_buffer == current_buffer
12946 && current_buffer == XBUFFER (w->buffer)
12947 && NILP (w->force_start)
12948 && NILP (w->optional_new_start)
12949 /* Point must be on the line that we have info recorded about. */
12950 && PT >= CHARPOS (tlbufpos)
12951 && PT <= Z - CHARPOS (tlendpos)
12952 /* All text outside that line, including its final newline,
12953 must be unchanged. */
12954 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
12955 CHARPOS (tlendpos)))
12957 if (CHARPOS (tlbufpos) > BEGV
12958 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
12959 && (CHARPOS (tlbufpos) == ZV
12960 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
12961 /* Former continuation line has disappeared by becoming empty. */
12962 goto cancel;
12963 else if (XFASTINT (w->last_modified) < MODIFF
12964 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
12965 || MINI_WINDOW_P (w))
12967 /* We have to handle the case of continuation around a
12968 wide-column character (see the comment in indent.c around
12969 line 1340).
12971 For instance, in the following case:
12973 -------- Insert --------
12974 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
12975 J_I_ ==> J_I_ `^^' are cursors.
12976 ^^ ^^
12977 -------- --------
12979 As we have to redraw the line above, we cannot use this
12980 optimization. */
12982 struct it it;
12983 int line_height_before = this_line_pixel_height;
12985 /* Note that start_display will handle the case that the
12986 line starting at tlbufpos is a continuation line. */
12987 start_display (&it, w, tlbufpos);
12989 /* Implementation note: It this still necessary? */
12990 if (it.current_x != this_line_start_x)
12991 goto cancel;
12993 TRACE ((stderr, "trying display optimization 1\n"));
12994 w->cursor.vpos = -1;
12995 overlay_arrow_seen = 0;
12996 it.vpos = this_line_vpos;
12997 it.current_y = this_line_y;
12998 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
12999 display_line (&it);
13001 /* If line contains point, is not continued,
13002 and ends at same distance from eob as before, we win. */
13003 if (w->cursor.vpos >= 0
13004 /* Line is not continued, otherwise this_line_start_pos
13005 would have been set to 0 in display_line. */
13006 && CHARPOS (this_line_start_pos)
13007 /* Line ends as before. */
13008 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13009 /* Line has same height as before. Otherwise other lines
13010 would have to be shifted up or down. */
13011 && this_line_pixel_height == line_height_before)
13013 /* If this is not the window's last line, we must adjust
13014 the charstarts of the lines below. */
13015 if (it.current_y < it.last_visible_y)
13017 struct glyph_row *row
13018 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13019 EMACS_INT delta, delta_bytes;
13021 /* We used to distinguish between two cases here,
13022 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13023 when the line ends in a newline or the end of the
13024 buffer's accessible portion. But both cases did
13025 the same, so they were collapsed. */
13026 delta = (Z
13027 - CHARPOS (tlendpos)
13028 - MATRIX_ROW_START_CHARPOS (row));
13029 delta_bytes = (Z_BYTE
13030 - BYTEPOS (tlendpos)
13031 - MATRIX_ROW_START_BYTEPOS (row));
13033 increment_matrix_positions (w->current_matrix,
13034 this_line_vpos + 1,
13035 w->current_matrix->nrows,
13036 delta, delta_bytes);
13039 /* If this row displays text now but previously didn't,
13040 or vice versa, w->window_end_vpos may have to be
13041 adjusted. */
13042 if ((it.glyph_row - 1)->displays_text_p)
13044 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13045 XSETINT (w->window_end_vpos, this_line_vpos);
13047 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13048 && this_line_vpos > 0)
13049 XSETINT (w->window_end_vpos, this_line_vpos - 1);
13050 w->window_end_valid = Qnil;
13052 /* Update hint: No need to try to scroll in update_window. */
13053 w->desired_matrix->no_scrolling_p = 1;
13055 #if GLYPH_DEBUG
13056 *w->desired_matrix->method = 0;
13057 debug_method_add (w, "optimization 1");
13058 #endif
13059 #ifdef HAVE_WINDOW_SYSTEM
13060 update_window_fringes (w, 0);
13061 #endif
13062 goto update;
13064 else
13065 goto cancel;
13067 else if (/* Cursor position hasn't changed. */
13068 PT == XFASTINT (w->last_point)
13069 /* Make sure the cursor was last displayed
13070 in this window. Otherwise we have to reposition it. */
13071 && 0 <= w->cursor.vpos
13072 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13074 if (!must_finish)
13076 do_pending_window_change (1);
13077 /* If selected_window changed, redisplay again. */
13078 if (WINDOWP (selected_window)
13079 && (w = XWINDOW (selected_window)) != sw)
13080 goto retry;
13082 /* We used to always goto end_of_redisplay here, but this
13083 isn't enough if we have a blinking cursor. */
13084 if (w->cursor_off_p == w->last_cursor_off_p)
13085 goto end_of_redisplay;
13087 goto update;
13089 /* If highlighting the region, or if the cursor is in the echo area,
13090 then we can't just move the cursor. */
13091 else if (! (!NILP (Vtransient_mark_mode)
13092 && !NILP (BVAR (current_buffer, mark_active)))
13093 && (EQ (selected_window, BVAR (current_buffer, last_selected_window))
13094 || highlight_nonselected_windows)
13095 && NILP (w->region_showing)
13096 && NILP (Vshow_trailing_whitespace)
13097 && !cursor_in_echo_area)
13099 struct it it;
13100 struct glyph_row *row;
13102 /* Skip from tlbufpos to PT and see where it is. Note that
13103 PT may be in invisible text. If so, we will end at the
13104 next visible position. */
13105 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13106 NULL, DEFAULT_FACE_ID);
13107 it.current_x = this_line_start_x;
13108 it.current_y = this_line_y;
13109 it.vpos = this_line_vpos;
13111 /* The call to move_it_to stops in front of PT, but
13112 moves over before-strings. */
13113 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13115 if (it.vpos == this_line_vpos
13116 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13117 row->enabled_p))
13119 xassert (this_line_vpos == it.vpos);
13120 xassert (this_line_y == it.current_y);
13121 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13122 #if GLYPH_DEBUG
13123 *w->desired_matrix->method = 0;
13124 debug_method_add (w, "optimization 3");
13125 #endif
13126 goto update;
13128 else
13129 goto cancel;
13132 cancel:
13133 /* Text changed drastically or point moved off of line. */
13134 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13137 CHARPOS (this_line_start_pos) = 0;
13138 consider_all_windows_p |= buffer_shared > 1;
13139 ++clear_face_cache_count;
13140 #ifdef HAVE_WINDOW_SYSTEM
13141 ++clear_image_cache_count;
13142 #endif
13144 /* Build desired matrices, and update the display. If
13145 consider_all_windows_p is non-zero, do it for all windows on all
13146 frames. Otherwise do it for selected_window, only. */
13148 if (consider_all_windows_p)
13150 Lisp_Object tail, frame;
13152 FOR_EACH_FRAME (tail, frame)
13153 XFRAME (frame)->updated_p = 0;
13155 /* Recompute # windows showing selected buffer. This will be
13156 incremented each time such a window is displayed. */
13157 buffer_shared = 0;
13159 FOR_EACH_FRAME (tail, frame)
13161 struct frame *f = XFRAME (frame);
13163 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13165 if (! EQ (frame, selected_frame))
13166 /* Select the frame, for the sake of frame-local
13167 variables. */
13168 select_frame_for_redisplay (frame);
13170 /* Mark all the scroll bars to be removed; we'll redeem
13171 the ones we want when we redisplay their windows. */
13172 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13173 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13175 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13176 redisplay_windows (FRAME_ROOT_WINDOW (f));
13178 /* The X error handler may have deleted that frame. */
13179 if (!FRAME_LIVE_P (f))
13180 continue;
13182 /* Any scroll bars which redisplay_windows should have
13183 nuked should now go away. */
13184 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13185 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13187 /* If fonts changed, display again. */
13188 /* ??? rms: I suspect it is a mistake to jump all the way
13189 back to retry here. It should just retry this frame. */
13190 if (fonts_changed_p)
13191 goto retry;
13193 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13195 /* See if we have to hscroll. */
13196 if (!f->already_hscrolled_p)
13198 f->already_hscrolled_p = 1;
13199 if (hscroll_windows (f->root_window))
13200 goto retry;
13203 /* Prevent various kinds of signals during display
13204 update. stdio is not robust about handling
13205 signals, which can cause an apparent I/O
13206 error. */
13207 if (interrupt_input)
13208 unrequest_sigio ();
13209 STOP_POLLING;
13211 /* Update the display. */
13212 set_window_update_flags (XWINDOW (f->root_window), 1);
13213 pending |= update_frame (f, 0, 0);
13214 f->updated_p = 1;
13219 if (!EQ (old_frame, selected_frame)
13220 && FRAME_LIVE_P (XFRAME (old_frame)))
13221 /* We played a bit fast-and-loose above and allowed selected_frame
13222 and selected_window to be temporarily out-of-sync but let's make
13223 sure this stays contained. */
13224 select_frame_for_redisplay (old_frame);
13225 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13227 if (!pending)
13229 /* Do the mark_window_display_accurate after all windows have
13230 been redisplayed because this call resets flags in buffers
13231 which are needed for proper redisplay. */
13232 FOR_EACH_FRAME (tail, frame)
13234 struct frame *f = XFRAME (frame);
13235 if (f->updated_p)
13237 mark_window_display_accurate (f->root_window, 1);
13238 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13239 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13244 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13246 Lisp_Object mini_window;
13247 struct frame *mini_frame;
13249 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13250 /* Use list_of_error, not Qerror, so that
13251 we catch only errors and don't run the debugger. */
13252 internal_condition_case_1 (redisplay_window_1, selected_window,
13253 list_of_error,
13254 redisplay_window_error);
13256 /* Compare desired and current matrices, perform output. */
13258 update:
13259 /* If fonts changed, display again. */
13260 if (fonts_changed_p)
13261 goto retry;
13263 /* Prevent various kinds of signals during display update.
13264 stdio is not robust about handling signals,
13265 which can cause an apparent I/O error. */
13266 if (interrupt_input)
13267 unrequest_sigio ();
13268 STOP_POLLING;
13270 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13272 if (hscroll_windows (selected_window))
13273 goto retry;
13275 XWINDOW (selected_window)->must_be_updated_p = 1;
13276 pending = update_frame (sf, 0, 0);
13279 /* We may have called echo_area_display at the top of this
13280 function. If the echo area is on another frame, that may
13281 have put text on a frame other than the selected one, so the
13282 above call to update_frame would not have caught it. Catch
13283 it here. */
13284 mini_window = FRAME_MINIBUF_WINDOW (sf);
13285 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13287 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13289 XWINDOW (mini_window)->must_be_updated_p = 1;
13290 pending |= update_frame (mini_frame, 0, 0);
13291 if (!pending && hscroll_windows (mini_window))
13292 goto retry;
13296 /* If display was paused because of pending input, make sure we do a
13297 thorough update the next time. */
13298 if (pending)
13300 /* Prevent the optimization at the beginning of
13301 redisplay_internal that tries a single-line update of the
13302 line containing the cursor in the selected window. */
13303 CHARPOS (this_line_start_pos) = 0;
13305 /* Let the overlay arrow be updated the next time. */
13306 update_overlay_arrows (0);
13308 /* If we pause after scrolling, some rows in the current
13309 matrices of some windows are not valid. */
13310 if (!WINDOW_FULL_WIDTH_P (w)
13311 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13312 update_mode_lines = 1;
13314 else
13316 if (!consider_all_windows_p)
13318 /* This has already been done above if
13319 consider_all_windows_p is set. */
13320 mark_window_display_accurate_1 (w, 1);
13322 /* Say overlay arrows are up to date. */
13323 update_overlay_arrows (1);
13325 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13326 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13329 update_mode_lines = 0;
13330 windows_or_buffers_changed = 0;
13331 cursor_type_changed = 0;
13334 /* Start SIGIO interrupts coming again. Having them off during the
13335 code above makes it less likely one will discard output, but not
13336 impossible, since there might be stuff in the system buffer here.
13337 But it is much hairier to try to do anything about that. */
13338 if (interrupt_input)
13339 request_sigio ();
13340 RESUME_POLLING;
13342 /* If a frame has become visible which was not before, redisplay
13343 again, so that we display it. Expose events for such a frame
13344 (which it gets when becoming visible) don't call the parts of
13345 redisplay constructing glyphs, so simply exposing a frame won't
13346 display anything in this case. So, we have to display these
13347 frames here explicitly. */
13348 if (!pending)
13350 Lisp_Object tail, frame;
13351 int new_count = 0;
13353 FOR_EACH_FRAME (tail, frame)
13355 int this_is_visible = 0;
13357 if (XFRAME (frame)->visible)
13358 this_is_visible = 1;
13359 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13360 if (XFRAME (frame)->visible)
13361 this_is_visible = 1;
13363 if (this_is_visible)
13364 new_count++;
13367 if (new_count != number_of_visible_frames)
13368 windows_or_buffers_changed++;
13371 /* Change frame size now if a change is pending. */
13372 do_pending_window_change (1);
13374 /* If we just did a pending size change, or have additional
13375 visible frames, or selected_window changed, redisplay again. */
13376 if ((windows_or_buffers_changed && !pending)
13377 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13378 goto retry;
13380 /* Clear the face and image caches.
13382 We used to do this only if consider_all_windows_p. But the cache
13383 needs to be cleared if a timer creates images in the current
13384 buffer (e.g. the test case in Bug#6230). */
13386 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13388 clear_face_cache (0);
13389 clear_face_cache_count = 0;
13392 #ifdef HAVE_WINDOW_SYSTEM
13393 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13395 clear_image_caches (Qnil);
13396 clear_image_cache_count = 0;
13398 #endif /* HAVE_WINDOW_SYSTEM */
13400 end_of_redisplay:
13401 unbind_to (count, Qnil);
13402 RESUME_POLLING;
13406 /* Redisplay, but leave alone any recent echo area message unless
13407 another message has been requested in its place.
13409 This is useful in situations where you need to redisplay but no
13410 user action has occurred, making it inappropriate for the message
13411 area to be cleared. See tracking_off and
13412 wait_reading_process_output for examples of these situations.
13414 FROM_WHERE is an integer saying from where this function was
13415 called. This is useful for debugging. */
13417 void
13418 redisplay_preserve_echo_area (int from_where)
13420 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13422 if (!NILP (echo_area_buffer[1]))
13424 /* We have a previously displayed message, but no current
13425 message. Redisplay the previous message. */
13426 display_last_displayed_message_p = 1;
13427 redisplay_internal ();
13428 display_last_displayed_message_p = 0;
13430 else
13431 redisplay_internal ();
13433 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13434 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13435 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13439 /* Function registered with record_unwind_protect in
13440 redisplay_internal. Reset redisplaying_p to the value it had
13441 before redisplay_internal was called, and clear
13442 prevent_freeing_realized_faces_p. It also selects the previously
13443 selected frame, unless it has been deleted (by an X connection
13444 failure during redisplay, for example). */
13446 static Lisp_Object
13447 unwind_redisplay (Lisp_Object val)
13449 Lisp_Object old_redisplaying_p, old_frame;
13451 old_redisplaying_p = XCAR (val);
13452 redisplaying_p = XFASTINT (old_redisplaying_p);
13453 old_frame = XCDR (val);
13454 if (! EQ (old_frame, selected_frame)
13455 && FRAME_LIVE_P (XFRAME (old_frame)))
13456 select_frame_for_redisplay (old_frame);
13457 return Qnil;
13461 /* Mark the display of window W as accurate or inaccurate. If
13462 ACCURATE_P is non-zero mark display of W as accurate. If
13463 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13464 redisplay_internal is called. */
13466 static void
13467 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13469 if (BUFFERP (w->buffer))
13471 struct buffer *b = XBUFFER (w->buffer);
13473 w->last_modified
13474 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
13475 w->last_overlay_modified
13476 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
13477 w->last_had_star
13478 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
13480 if (accurate_p)
13482 b->clip_changed = 0;
13483 b->prevent_redisplay_optimizations_p = 0;
13485 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13486 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13487 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13488 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13490 w->current_matrix->buffer = b;
13491 w->current_matrix->begv = BUF_BEGV (b);
13492 w->current_matrix->zv = BUF_ZV (b);
13494 w->last_cursor = w->cursor;
13495 w->last_cursor_off_p = w->cursor_off_p;
13497 if (w == XWINDOW (selected_window))
13498 w->last_point = make_number (BUF_PT (b));
13499 else
13500 w->last_point = make_number (XMARKER (w->pointm)->charpos);
13504 if (accurate_p)
13506 w->window_end_valid = w->buffer;
13507 w->update_mode_line = Qnil;
13512 /* Mark the display of windows in the window tree rooted at WINDOW as
13513 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13514 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13515 be redisplayed the next time redisplay_internal is called. */
13517 void
13518 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13520 struct window *w;
13522 for (; !NILP (window); window = w->next)
13524 w = XWINDOW (window);
13525 mark_window_display_accurate_1 (w, accurate_p);
13527 if (!NILP (w->vchild))
13528 mark_window_display_accurate (w->vchild, accurate_p);
13529 if (!NILP (w->hchild))
13530 mark_window_display_accurate (w->hchild, accurate_p);
13533 if (accurate_p)
13535 update_overlay_arrows (1);
13537 else
13539 /* Force a thorough redisplay the next time by setting
13540 last_arrow_position and last_arrow_string to t, which is
13541 unequal to any useful value of Voverlay_arrow_... */
13542 update_overlay_arrows (-1);
13547 /* Return value in display table DP (Lisp_Char_Table *) for character
13548 C. Since a display table doesn't have any parent, we don't have to
13549 follow parent. Do not call this function directly but use the
13550 macro DISP_CHAR_VECTOR. */
13552 Lisp_Object
13553 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13555 Lisp_Object val;
13557 if (ASCII_CHAR_P (c))
13559 val = dp->ascii;
13560 if (SUB_CHAR_TABLE_P (val))
13561 val = XSUB_CHAR_TABLE (val)->contents[c];
13563 else
13565 Lisp_Object table;
13567 XSETCHAR_TABLE (table, dp);
13568 val = char_table_ref (table, c);
13570 if (NILP (val))
13571 val = dp->defalt;
13572 return val;
13577 /***********************************************************************
13578 Window Redisplay
13579 ***********************************************************************/
13581 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13583 static void
13584 redisplay_windows (Lisp_Object window)
13586 while (!NILP (window))
13588 struct window *w = XWINDOW (window);
13590 if (!NILP (w->hchild))
13591 redisplay_windows (w->hchild);
13592 else if (!NILP (w->vchild))
13593 redisplay_windows (w->vchild);
13594 else if (!NILP (w->buffer))
13596 displayed_buffer = XBUFFER (w->buffer);
13597 /* Use list_of_error, not Qerror, so that
13598 we catch only errors and don't run the debugger. */
13599 internal_condition_case_1 (redisplay_window_0, window,
13600 list_of_error,
13601 redisplay_window_error);
13604 window = w->next;
13608 static Lisp_Object
13609 redisplay_window_error (Lisp_Object ignore)
13611 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13612 return Qnil;
13615 static Lisp_Object
13616 redisplay_window_0 (Lisp_Object window)
13618 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13619 redisplay_window (window, 0);
13620 return Qnil;
13623 static Lisp_Object
13624 redisplay_window_1 (Lisp_Object window)
13626 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13627 redisplay_window (window, 1);
13628 return Qnil;
13632 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13633 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13634 which positions recorded in ROW differ from current buffer
13635 positions.
13637 Return 0 if cursor is not on this row, 1 otherwise. */
13639 static int
13640 set_cursor_from_row (struct window *w, struct glyph_row *row,
13641 struct glyph_matrix *matrix,
13642 EMACS_INT delta, EMACS_INT delta_bytes,
13643 int dy, int dvpos)
13645 struct glyph *glyph = row->glyphs[TEXT_AREA];
13646 struct glyph *end = glyph + row->used[TEXT_AREA];
13647 struct glyph *cursor = NULL;
13648 /* The last known character position in row. */
13649 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13650 int x = row->x;
13651 EMACS_INT pt_old = PT - delta;
13652 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13653 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13654 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13655 /* A glyph beyond the edge of TEXT_AREA which we should never
13656 touch. */
13657 struct glyph *glyphs_end = end;
13658 /* Non-zero means we've found a match for cursor position, but that
13659 glyph has the avoid_cursor_p flag set. */
13660 int match_with_avoid_cursor = 0;
13661 /* Non-zero means we've seen at least one glyph that came from a
13662 display string. */
13663 int string_seen = 0;
13664 /* Largest and smallest buffer positions seen so far during scan of
13665 glyph row. */
13666 EMACS_INT bpos_max = pos_before;
13667 EMACS_INT bpos_min = pos_after;
13668 /* Last buffer position covered by an overlay string with an integer
13669 `cursor' property. */
13670 EMACS_INT bpos_covered = 0;
13671 /* Non-zero means the display string on which to display the cursor
13672 comes from a text property, not from an overlay. */
13673 int string_from_text_prop = 0;
13675 /* Skip over glyphs not having an object at the start and the end of
13676 the row. These are special glyphs like truncation marks on
13677 terminal frames. */
13678 if (row->displays_text_p)
13680 if (!row->reversed_p)
13682 while (glyph < end
13683 && INTEGERP (glyph->object)
13684 && glyph->charpos < 0)
13686 x += glyph->pixel_width;
13687 ++glyph;
13689 while (end > glyph
13690 && INTEGERP ((end - 1)->object)
13691 /* CHARPOS is zero for blanks and stretch glyphs
13692 inserted by extend_face_to_end_of_line. */
13693 && (end - 1)->charpos <= 0)
13694 --end;
13695 glyph_before = glyph - 1;
13696 glyph_after = end;
13698 else
13700 struct glyph *g;
13702 /* If the glyph row is reversed, we need to process it from back
13703 to front, so swap the edge pointers. */
13704 glyphs_end = end = glyph - 1;
13705 glyph += row->used[TEXT_AREA] - 1;
13707 while (glyph > end + 1
13708 && INTEGERP (glyph->object)
13709 && glyph->charpos < 0)
13711 --glyph;
13712 x -= glyph->pixel_width;
13714 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13715 --glyph;
13716 /* By default, in reversed rows we put the cursor on the
13717 rightmost (first in the reading order) glyph. */
13718 for (g = end + 1; g < glyph; g++)
13719 x += g->pixel_width;
13720 while (end < glyph
13721 && INTEGERP ((end + 1)->object)
13722 && (end + 1)->charpos <= 0)
13723 ++end;
13724 glyph_before = glyph + 1;
13725 glyph_after = end;
13728 else if (row->reversed_p)
13730 /* In R2L rows that don't display text, put the cursor on the
13731 rightmost glyph. Case in point: an empty last line that is
13732 part of an R2L paragraph. */
13733 cursor = end - 1;
13734 /* Avoid placing the cursor on the last glyph of the row, where
13735 on terminal frames we hold the vertical border between
13736 adjacent windows. */
13737 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13738 && !WINDOW_RIGHTMOST_P (w)
13739 && cursor == row->glyphs[LAST_AREA] - 1)
13740 cursor--;
13741 x = -1; /* will be computed below, at label compute_x */
13744 /* Step 1: Try to find the glyph whose character position
13745 corresponds to point. If that's not possible, find 2 glyphs
13746 whose character positions are the closest to point, one before
13747 point, the other after it. */
13748 if (!row->reversed_p)
13749 while (/* not marched to end of glyph row */
13750 glyph < end
13751 /* glyph was not inserted by redisplay for internal purposes */
13752 && !INTEGERP (glyph->object))
13754 if (BUFFERP (glyph->object))
13756 EMACS_INT dpos = glyph->charpos - pt_old;
13758 if (glyph->charpos > bpos_max)
13759 bpos_max = glyph->charpos;
13760 if (glyph->charpos < bpos_min)
13761 bpos_min = glyph->charpos;
13762 if (!glyph->avoid_cursor_p)
13764 /* If we hit point, we've found the glyph on which to
13765 display the cursor. */
13766 if (dpos == 0)
13768 match_with_avoid_cursor = 0;
13769 break;
13771 /* See if we've found a better approximation to
13772 POS_BEFORE or to POS_AFTER. Note that we want the
13773 first (leftmost) glyph of all those that are the
13774 closest from below, and the last (rightmost) of all
13775 those from above. */
13776 if (0 > dpos && dpos > pos_before - pt_old)
13778 pos_before = glyph->charpos;
13779 glyph_before = glyph;
13781 else if (0 < dpos && dpos <= pos_after - pt_old)
13783 pos_after = glyph->charpos;
13784 glyph_after = glyph;
13787 else if (dpos == 0)
13788 match_with_avoid_cursor = 1;
13790 else if (STRINGP (glyph->object))
13792 Lisp_Object chprop;
13793 EMACS_INT glyph_pos = glyph->charpos;
13795 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13796 glyph->object);
13797 if (INTEGERP (chprop))
13799 bpos_covered = bpos_max + XINT (chprop);
13800 /* If the `cursor' property covers buffer positions up
13801 to and including point, we should display cursor on
13802 this glyph. Note that overlays and text properties
13803 with string values stop bidi reordering, so every
13804 buffer position to the left of the string is always
13805 smaller than any position to the right of the
13806 string. Therefore, if a `cursor' property on one
13807 of the string's characters has an integer value, we
13808 will break out of the loop below _before_ we get to
13809 the position match above. IOW, integer values of
13810 the `cursor' property override the "exact match for
13811 point" strategy of positioning the cursor. */
13812 /* Implementation note: bpos_max == pt_old when, e.g.,
13813 we are in an empty line, where bpos_max is set to
13814 MATRIX_ROW_START_CHARPOS, see above. */
13815 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13817 cursor = glyph;
13818 break;
13822 string_seen = 1;
13824 x += glyph->pixel_width;
13825 ++glyph;
13827 else if (glyph > end) /* row is reversed */
13828 while (!INTEGERP (glyph->object))
13830 if (BUFFERP (glyph->object))
13832 EMACS_INT dpos = glyph->charpos - pt_old;
13834 if (glyph->charpos > bpos_max)
13835 bpos_max = glyph->charpos;
13836 if (glyph->charpos < bpos_min)
13837 bpos_min = glyph->charpos;
13838 if (!glyph->avoid_cursor_p)
13840 if (dpos == 0)
13842 match_with_avoid_cursor = 0;
13843 break;
13845 if (0 > dpos && dpos > pos_before - pt_old)
13847 pos_before = glyph->charpos;
13848 glyph_before = glyph;
13850 else if (0 < dpos && dpos <= pos_after - pt_old)
13852 pos_after = glyph->charpos;
13853 glyph_after = glyph;
13856 else if (dpos == 0)
13857 match_with_avoid_cursor = 1;
13859 else if (STRINGP (glyph->object))
13861 Lisp_Object chprop;
13862 EMACS_INT glyph_pos = glyph->charpos;
13864 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13865 glyph->object);
13866 if (INTEGERP (chprop))
13868 bpos_covered = bpos_max + XINT (chprop);
13869 /* If the `cursor' property covers buffer positions up
13870 to and including point, we should display cursor on
13871 this glyph. */
13872 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13874 cursor = glyph;
13875 break;
13878 string_seen = 1;
13880 --glyph;
13881 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
13883 x--; /* can't use any pixel_width */
13884 break;
13886 x -= glyph->pixel_width;
13889 /* Step 2: If we didn't find an exact match for point, we need to
13890 look for a proper place to put the cursor among glyphs between
13891 GLYPH_BEFORE and GLYPH_AFTER. */
13892 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13893 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
13894 && bpos_covered < pt_old)
13896 /* An empty line has a single glyph whose OBJECT is zero and
13897 whose CHARPOS is the position of a newline on that line.
13898 Note that on a TTY, there are more glyphs after that, which
13899 were produced by extend_face_to_end_of_line, but their
13900 CHARPOS is zero or negative. */
13901 int empty_line_p =
13902 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13903 && INTEGERP (glyph->object) && glyph->charpos > 0;
13905 if (row->ends_in_ellipsis_p && pos_after == last_pos)
13907 EMACS_INT ellipsis_pos;
13909 /* Scan back over the ellipsis glyphs. */
13910 if (!row->reversed_p)
13912 ellipsis_pos = (glyph - 1)->charpos;
13913 while (glyph > row->glyphs[TEXT_AREA]
13914 && (glyph - 1)->charpos == ellipsis_pos)
13915 glyph--, x -= glyph->pixel_width;
13916 /* That loop always goes one position too far, including
13917 the glyph before the ellipsis. So scan forward over
13918 that one. */
13919 x += glyph->pixel_width;
13920 glyph++;
13922 else /* row is reversed */
13924 ellipsis_pos = (glyph + 1)->charpos;
13925 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
13926 && (glyph + 1)->charpos == ellipsis_pos)
13927 glyph++, x += glyph->pixel_width;
13928 x -= glyph->pixel_width;
13929 glyph--;
13932 else if (match_with_avoid_cursor)
13934 cursor = glyph_after;
13935 x = -1;
13937 else if (string_seen)
13939 int incr = row->reversed_p ? -1 : +1;
13941 /* Need to find the glyph that came out of a string which is
13942 present at point. That glyph is somewhere between
13943 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
13944 positioned between POS_BEFORE and POS_AFTER in the
13945 buffer. */
13946 struct glyph *start, *stop;
13947 EMACS_INT pos = pos_before;
13949 x = -1;
13951 /* If the row ends in a newline from a display string,
13952 reordering could have moved the glyphs belonging to the
13953 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
13954 in this case we extend the search to the last glyph in
13955 the row that was not inserted by redisplay. */
13956 if (row->ends_in_newline_from_string_p)
13958 glyph_after = end;
13959 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13962 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
13963 correspond to POS_BEFORE and POS_AFTER, respectively. We
13964 need START and STOP in the order that corresponds to the
13965 row's direction as given by its reversed_p flag. If the
13966 directionality of characters between POS_BEFORE and
13967 POS_AFTER is the opposite of the row's base direction,
13968 these characters will have been reordered for display,
13969 and we need to reverse START and STOP. */
13970 if (!row->reversed_p)
13972 start = min (glyph_before, glyph_after);
13973 stop = max (glyph_before, glyph_after);
13975 else
13977 start = max (glyph_before, glyph_after);
13978 stop = min (glyph_before, glyph_after);
13980 for (glyph = start + incr;
13981 row->reversed_p ? glyph > stop : glyph < stop; )
13984 /* Any glyphs that come from the buffer are here because
13985 of bidi reordering. Skip them, and only pay
13986 attention to glyphs that came from some string. */
13987 if (STRINGP (glyph->object))
13989 Lisp_Object str;
13990 EMACS_INT tem;
13991 /* If the display property covers the newline, we
13992 need to search for it one position farther. */
13993 EMACS_INT lim = pos_after
13994 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
13996 string_from_text_prop = 0;
13997 str = glyph->object;
13998 tem = string_buffer_position_lim (str, pos, lim, 0);
13999 if (tem == 0 /* from overlay */
14000 || pos <= tem)
14002 /* If the string from which this glyph came is
14003 found in the buffer at point, then we've
14004 found the glyph we've been looking for. If
14005 it comes from an overlay (tem == 0), and it
14006 has the `cursor' property on one of its
14007 glyphs, record that glyph as a candidate for
14008 displaying the cursor. (As in the
14009 unidirectional version, we will display the
14010 cursor on the last candidate we find.) */
14011 if (tem == 0 || tem == pt_old)
14013 /* The glyphs from this string could have
14014 been reordered. Find the one with the
14015 smallest string position. Or there could
14016 be a character in the string with the
14017 `cursor' property, which means display
14018 cursor on that character's glyph. */
14019 EMACS_INT strpos = glyph->charpos;
14021 if (tem)
14023 cursor = glyph;
14024 string_from_text_prop = 1;
14026 for ( ;
14027 (row->reversed_p ? glyph > stop : glyph < stop)
14028 && EQ (glyph->object, str);
14029 glyph += incr)
14031 Lisp_Object cprop;
14032 EMACS_INT gpos = glyph->charpos;
14034 cprop = Fget_char_property (make_number (gpos),
14035 Qcursor,
14036 glyph->object);
14037 if (!NILP (cprop))
14039 cursor = glyph;
14040 break;
14042 if (tem && glyph->charpos < strpos)
14044 strpos = glyph->charpos;
14045 cursor = glyph;
14049 if (tem == pt_old)
14050 goto compute_x;
14052 if (tem)
14053 pos = tem + 1; /* don't find previous instances */
14055 /* This string is not what we want; skip all of the
14056 glyphs that came from it. */
14057 while ((row->reversed_p ? glyph > stop : glyph < stop)
14058 && EQ (glyph->object, str))
14059 glyph += incr;
14061 else
14062 glyph += incr;
14065 /* If we reached the end of the line, and END was from a string,
14066 the cursor is not on this line. */
14067 if (cursor == NULL
14068 && (row->reversed_p ? glyph <= end : glyph >= end)
14069 && STRINGP (end->object)
14070 && row->continued_p)
14071 return 0;
14073 /* A truncated row may not include PT among its character positions.
14074 Setting the cursor inside the scroll margin will trigger
14075 recalculation of hscroll in hscroll_window_tree. But if a
14076 display string covers point, defer to the string-handling
14077 code below to figure this out. */
14078 else if (row->truncated_on_left_p && pt_old < bpos_min)
14080 cursor = glyph_before;
14081 x = -1;
14083 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14084 /* Zero-width characters produce no glyphs. */
14085 || (!empty_line_p
14086 && (row->reversed_p
14087 ? glyph_after > glyphs_end
14088 : glyph_after < glyphs_end)))
14090 cursor = glyph_after;
14091 x = -1;
14095 compute_x:
14096 if (cursor != NULL)
14097 glyph = cursor;
14098 if (x < 0)
14100 struct glyph *g;
14102 /* Need to compute x that corresponds to GLYPH. */
14103 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14105 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14106 abort ();
14107 x += g->pixel_width;
14111 /* ROW could be part of a continued line, which, under bidi
14112 reordering, might have other rows whose start and end charpos
14113 occlude point. Only set w->cursor if we found a better
14114 approximation to the cursor position than we have from previously
14115 examined candidate rows belonging to the same continued line. */
14116 if (/* we already have a candidate row */
14117 w->cursor.vpos >= 0
14118 /* that candidate is not the row we are processing */
14119 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14120 /* Make sure cursor.vpos specifies a row whose start and end
14121 charpos occlude point, and it is valid candidate for being a
14122 cursor-row. This is because some callers of this function
14123 leave cursor.vpos at the row where the cursor was displayed
14124 during the last redisplay cycle. */
14125 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14126 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14127 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14129 struct glyph *g1 =
14130 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14132 /* Don't consider glyphs that are outside TEXT_AREA. */
14133 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14134 return 0;
14135 /* Keep the candidate whose buffer position is the closest to
14136 point or has the `cursor' property. */
14137 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14138 w->cursor.hpos >= 0
14139 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14140 && ((BUFFERP (g1->object)
14141 && (g1->charpos == pt_old /* an exact match always wins */
14142 || (BUFFERP (glyph->object)
14143 && eabs (g1->charpos - pt_old)
14144 < eabs (glyph->charpos - pt_old))))
14145 /* previous candidate is a glyph from a string that has
14146 a non-nil `cursor' property */
14147 || (STRINGP (g1->object)
14148 && (!NILP (Fget_char_property (make_number (g1->charpos),
14149 Qcursor, g1->object))
14150 /* previous candidate is from the same display
14151 string as this one, and the display string
14152 came from a text property */
14153 || (EQ (g1->object, glyph->object)
14154 && string_from_text_prop)
14155 /* this candidate is from newline and its
14156 position is not an exact match */
14157 || (INTEGERP (glyph->object)
14158 && glyph->charpos != pt_old)))))
14159 return 0;
14160 /* If this candidate gives an exact match, use that. */
14161 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14162 /* If this candidate is a glyph created for the
14163 terminating newline of a line, and point is on that
14164 newline, it wins because it's an exact match. */
14165 || (!row->continued_p
14166 && INTEGERP (glyph->object)
14167 && glyph->charpos == 0
14168 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14169 /* Otherwise, keep the candidate that comes from a row
14170 spanning less buffer positions. This may win when one or
14171 both candidate positions are on glyphs that came from
14172 display strings, for which we cannot compare buffer
14173 positions. */
14174 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14175 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14176 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14177 return 0;
14179 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14180 w->cursor.x = x;
14181 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14182 w->cursor.y = row->y + dy;
14184 if (w == XWINDOW (selected_window))
14186 if (!row->continued_p
14187 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14188 && row->x == 0)
14190 this_line_buffer = XBUFFER (w->buffer);
14192 CHARPOS (this_line_start_pos)
14193 = MATRIX_ROW_START_CHARPOS (row) + delta;
14194 BYTEPOS (this_line_start_pos)
14195 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14197 CHARPOS (this_line_end_pos)
14198 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14199 BYTEPOS (this_line_end_pos)
14200 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14202 this_line_y = w->cursor.y;
14203 this_line_pixel_height = row->height;
14204 this_line_vpos = w->cursor.vpos;
14205 this_line_start_x = row->x;
14207 else
14208 CHARPOS (this_line_start_pos) = 0;
14211 return 1;
14215 /* Run window scroll functions, if any, for WINDOW with new window
14216 start STARTP. Sets the window start of WINDOW to that position.
14218 We assume that the window's buffer is really current. */
14220 static inline struct text_pos
14221 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14223 struct window *w = XWINDOW (window);
14224 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14226 if (current_buffer != XBUFFER (w->buffer))
14227 abort ();
14229 if (!NILP (Vwindow_scroll_functions))
14231 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14232 make_number (CHARPOS (startp)));
14233 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14234 /* In case the hook functions switch buffers. */
14235 if (current_buffer != XBUFFER (w->buffer))
14236 set_buffer_internal_1 (XBUFFER (w->buffer));
14239 return startp;
14243 /* Make sure the line containing the cursor is fully visible.
14244 A value of 1 means there is nothing to be done.
14245 (Either the line is fully visible, or it cannot be made so,
14246 or we cannot tell.)
14248 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14249 is higher than window.
14251 A value of 0 means the caller should do scrolling
14252 as if point had gone off the screen. */
14254 static int
14255 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14257 struct glyph_matrix *matrix;
14258 struct glyph_row *row;
14259 int window_height;
14261 if (!make_cursor_line_fully_visible_p)
14262 return 1;
14264 /* It's not always possible to find the cursor, e.g, when a window
14265 is full of overlay strings. Don't do anything in that case. */
14266 if (w->cursor.vpos < 0)
14267 return 1;
14269 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14270 row = MATRIX_ROW (matrix, w->cursor.vpos);
14272 /* If the cursor row is not partially visible, there's nothing to do. */
14273 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14274 return 1;
14276 /* If the row the cursor is in is taller than the window's height,
14277 it's not clear what to do, so do nothing. */
14278 window_height = window_box_height (w);
14279 if (row->height >= window_height)
14281 if (!force_p || MINI_WINDOW_P (w)
14282 || w->vscroll || w->cursor.vpos == 0)
14283 return 1;
14285 return 0;
14289 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14290 non-zero means only WINDOW is redisplayed in redisplay_internal.
14291 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14292 in redisplay_window to bring a partially visible line into view in
14293 the case that only the cursor has moved.
14295 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14296 last screen line's vertical height extends past the end of the screen.
14298 Value is
14300 1 if scrolling succeeded
14302 0 if scrolling didn't find point.
14304 -1 if new fonts have been loaded so that we must interrupt
14305 redisplay, adjust glyph matrices, and try again. */
14307 enum
14309 SCROLLING_SUCCESS,
14310 SCROLLING_FAILED,
14311 SCROLLING_NEED_LARGER_MATRICES
14314 /* If scroll-conservatively is more than this, never recenter.
14316 If you change this, don't forget to update the doc string of
14317 `scroll-conservatively' and the Emacs manual. */
14318 #define SCROLL_LIMIT 100
14320 static int
14321 try_scrolling (Lisp_Object window, int just_this_one_p,
14322 EMACS_INT arg_scroll_conservatively, EMACS_INT scroll_step,
14323 int temp_scroll_step, int last_line_misfit)
14325 struct window *w = XWINDOW (window);
14326 struct frame *f = XFRAME (w->frame);
14327 struct text_pos pos, startp;
14328 struct it it;
14329 int this_scroll_margin, scroll_max, rc, height;
14330 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14331 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14332 Lisp_Object aggressive;
14333 /* We will never try scrolling more than this number of lines. */
14334 int scroll_limit = SCROLL_LIMIT;
14336 #if GLYPH_DEBUG
14337 debug_method_add (w, "try_scrolling");
14338 #endif
14340 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14342 /* Compute scroll margin height in pixels. We scroll when point is
14343 within this distance from the top or bottom of the window. */
14344 if (scroll_margin > 0)
14345 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14346 * FRAME_LINE_HEIGHT (f);
14347 else
14348 this_scroll_margin = 0;
14350 /* Force arg_scroll_conservatively to have a reasonable value, to
14351 avoid scrolling too far away with slow move_it_* functions. Note
14352 that the user can supply scroll-conservatively equal to
14353 `most-positive-fixnum', which can be larger than INT_MAX. */
14354 if (arg_scroll_conservatively > scroll_limit)
14356 arg_scroll_conservatively = scroll_limit + 1;
14357 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14359 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14360 /* Compute how much we should try to scroll maximally to bring
14361 point into view. */
14362 scroll_max = (max (scroll_step,
14363 max (arg_scroll_conservatively, temp_scroll_step))
14364 * FRAME_LINE_HEIGHT (f));
14365 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14366 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14367 /* We're trying to scroll because of aggressive scrolling but no
14368 scroll_step is set. Choose an arbitrary one. */
14369 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14370 else
14371 scroll_max = 0;
14373 too_near_end:
14375 /* Decide whether to scroll down. */
14376 if (PT > CHARPOS (startp))
14378 int scroll_margin_y;
14380 /* Compute the pixel ypos of the scroll margin, then move IT to
14381 either that ypos or PT, whichever comes first. */
14382 start_display (&it, w, startp);
14383 scroll_margin_y = it.last_visible_y - this_scroll_margin
14384 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14385 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14386 (MOVE_TO_POS | MOVE_TO_Y));
14388 if (PT > CHARPOS (it.current.pos))
14390 int y0 = line_bottom_y (&it);
14391 /* Compute how many pixels below window bottom to stop searching
14392 for PT. This avoids costly search for PT that is far away if
14393 the user limited scrolling by a small number of lines, but
14394 always finds PT if scroll_conservatively is set to a large
14395 number, such as most-positive-fixnum. */
14396 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14397 int y_to_move = it.last_visible_y + slack;
14399 /* Compute the distance from the scroll margin to PT or to
14400 the scroll limit, whichever comes first. This should
14401 include the height of the cursor line, to make that line
14402 fully visible. */
14403 move_it_to (&it, PT, -1, y_to_move,
14404 -1, MOVE_TO_POS | MOVE_TO_Y);
14405 dy = line_bottom_y (&it) - y0;
14407 if (dy > scroll_max)
14408 return SCROLLING_FAILED;
14410 if (dy > 0)
14411 scroll_down_p = 1;
14415 if (scroll_down_p)
14417 /* Point is in or below the bottom scroll margin, so move the
14418 window start down. If scrolling conservatively, move it just
14419 enough down to make point visible. If scroll_step is set,
14420 move it down by scroll_step. */
14421 if (arg_scroll_conservatively)
14422 amount_to_scroll
14423 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14424 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14425 else if (scroll_step || temp_scroll_step)
14426 amount_to_scroll = scroll_max;
14427 else
14429 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14430 height = WINDOW_BOX_TEXT_HEIGHT (w);
14431 if (NUMBERP (aggressive))
14433 double float_amount = XFLOATINT (aggressive) * height;
14434 amount_to_scroll = float_amount;
14435 if (amount_to_scroll == 0 && float_amount > 0)
14436 amount_to_scroll = 1;
14437 /* Don't let point enter the scroll margin near top of
14438 the window. */
14439 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14440 amount_to_scroll = height - 2*this_scroll_margin + dy;
14444 if (amount_to_scroll <= 0)
14445 return SCROLLING_FAILED;
14447 start_display (&it, w, startp);
14448 if (arg_scroll_conservatively <= scroll_limit)
14449 move_it_vertically (&it, amount_to_scroll);
14450 else
14452 /* Extra precision for users who set scroll-conservatively
14453 to a large number: make sure the amount we scroll
14454 the window start is never less than amount_to_scroll,
14455 which was computed as distance from window bottom to
14456 point. This matters when lines at window top and lines
14457 below window bottom have different height. */
14458 struct it it1;
14459 void *it1data = NULL;
14460 /* We use a temporary it1 because line_bottom_y can modify
14461 its argument, if it moves one line down; see there. */
14462 int start_y;
14464 SAVE_IT (it1, it, it1data);
14465 start_y = line_bottom_y (&it1);
14466 do {
14467 RESTORE_IT (&it, &it, it1data);
14468 move_it_by_lines (&it, 1);
14469 SAVE_IT (it1, it, it1data);
14470 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14473 /* If STARTP is unchanged, move it down another screen line. */
14474 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14475 move_it_by_lines (&it, 1);
14476 startp = it.current.pos;
14478 else
14480 struct text_pos scroll_margin_pos = startp;
14482 /* See if point is inside the scroll margin at the top of the
14483 window. */
14484 if (this_scroll_margin)
14486 start_display (&it, w, startp);
14487 move_it_vertically (&it, this_scroll_margin);
14488 scroll_margin_pos = it.current.pos;
14491 if (PT < CHARPOS (scroll_margin_pos))
14493 /* Point is in the scroll margin at the top of the window or
14494 above what is displayed in the window. */
14495 int y0, y_to_move;
14497 /* Compute the vertical distance from PT to the scroll
14498 margin position. Move as far as scroll_max allows, or
14499 one screenful, or 10 screen lines, whichever is largest.
14500 Give up if distance is greater than scroll_max. */
14501 SET_TEXT_POS (pos, PT, PT_BYTE);
14502 start_display (&it, w, pos);
14503 y0 = it.current_y;
14504 y_to_move = max (it.last_visible_y,
14505 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14506 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14507 y_to_move, -1,
14508 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14509 dy = it.current_y - y0;
14510 if (dy > scroll_max)
14511 return SCROLLING_FAILED;
14513 /* Compute new window start. */
14514 start_display (&it, w, startp);
14516 if (arg_scroll_conservatively)
14517 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14518 max (scroll_step, temp_scroll_step));
14519 else if (scroll_step || temp_scroll_step)
14520 amount_to_scroll = scroll_max;
14521 else
14523 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14524 height = WINDOW_BOX_TEXT_HEIGHT (w);
14525 if (NUMBERP (aggressive))
14527 double float_amount = XFLOATINT (aggressive) * height;
14528 amount_to_scroll = float_amount;
14529 if (amount_to_scroll == 0 && float_amount > 0)
14530 amount_to_scroll = 1;
14531 amount_to_scroll -=
14532 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14533 /* Don't let point enter the scroll margin near
14534 bottom of the window. */
14535 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14536 amount_to_scroll = height - 2*this_scroll_margin + dy;
14540 if (amount_to_scroll <= 0)
14541 return SCROLLING_FAILED;
14543 move_it_vertically_backward (&it, amount_to_scroll);
14544 startp = it.current.pos;
14548 /* Run window scroll functions. */
14549 startp = run_window_scroll_functions (window, startp);
14551 /* Display the window. Give up if new fonts are loaded, or if point
14552 doesn't appear. */
14553 if (!try_window (window, startp, 0))
14554 rc = SCROLLING_NEED_LARGER_MATRICES;
14555 else if (w->cursor.vpos < 0)
14557 clear_glyph_matrix (w->desired_matrix);
14558 rc = SCROLLING_FAILED;
14560 else
14562 /* Maybe forget recorded base line for line number display. */
14563 if (!just_this_one_p
14564 || current_buffer->clip_changed
14565 || BEG_UNCHANGED < CHARPOS (startp))
14566 w->base_line_number = Qnil;
14568 /* If cursor ends up on a partially visible line,
14569 treat that as being off the bottom of the screen. */
14570 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14571 /* It's possible that the cursor is on the first line of the
14572 buffer, which is partially obscured due to a vscroll
14573 (Bug#7537). In that case, avoid looping forever . */
14574 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14576 clear_glyph_matrix (w->desired_matrix);
14577 ++extra_scroll_margin_lines;
14578 goto too_near_end;
14580 rc = SCROLLING_SUCCESS;
14583 return rc;
14587 /* Compute a suitable window start for window W if display of W starts
14588 on a continuation line. Value is non-zero if a new window start
14589 was computed.
14591 The new window start will be computed, based on W's width, starting
14592 from the start of the continued line. It is the start of the
14593 screen line with the minimum distance from the old start W->start. */
14595 static int
14596 compute_window_start_on_continuation_line (struct window *w)
14598 struct text_pos pos, start_pos;
14599 int window_start_changed_p = 0;
14601 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14603 /* If window start is on a continuation line... Window start may be
14604 < BEGV in case there's invisible text at the start of the
14605 buffer (M-x rmail, for example). */
14606 if (CHARPOS (start_pos) > BEGV
14607 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14609 struct it it;
14610 struct glyph_row *row;
14612 /* Handle the case that the window start is out of range. */
14613 if (CHARPOS (start_pos) < BEGV)
14614 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14615 else if (CHARPOS (start_pos) > ZV)
14616 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14618 /* Find the start of the continued line. This should be fast
14619 because scan_buffer is fast (newline cache). */
14620 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14621 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14622 row, DEFAULT_FACE_ID);
14623 reseat_at_previous_visible_line_start (&it);
14625 /* If the line start is "too far" away from the window start,
14626 say it takes too much time to compute a new window start. */
14627 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14628 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14630 int min_distance, distance;
14632 /* Move forward by display lines to find the new window
14633 start. If window width was enlarged, the new start can
14634 be expected to be > the old start. If window width was
14635 decreased, the new window start will be < the old start.
14636 So, we're looking for the display line start with the
14637 minimum distance from the old window start. */
14638 pos = it.current.pos;
14639 min_distance = INFINITY;
14640 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14641 distance < min_distance)
14643 min_distance = distance;
14644 pos = it.current.pos;
14645 move_it_by_lines (&it, 1);
14648 /* Set the window start there. */
14649 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14650 window_start_changed_p = 1;
14654 return window_start_changed_p;
14658 /* Try cursor movement in case text has not changed in window WINDOW,
14659 with window start STARTP. Value is
14661 CURSOR_MOVEMENT_SUCCESS if successful
14663 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14665 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14666 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14667 we want to scroll as if scroll-step were set to 1. See the code.
14669 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14670 which case we have to abort this redisplay, and adjust matrices
14671 first. */
14673 enum
14675 CURSOR_MOVEMENT_SUCCESS,
14676 CURSOR_MOVEMENT_CANNOT_BE_USED,
14677 CURSOR_MOVEMENT_MUST_SCROLL,
14678 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14681 static int
14682 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14684 struct window *w = XWINDOW (window);
14685 struct frame *f = XFRAME (w->frame);
14686 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14688 #if GLYPH_DEBUG
14689 if (inhibit_try_cursor_movement)
14690 return rc;
14691 #endif
14693 /* Handle case where text has not changed, only point, and it has
14694 not moved off the frame. */
14695 if (/* Point may be in this window. */
14696 PT >= CHARPOS (startp)
14697 /* Selective display hasn't changed. */
14698 && !current_buffer->clip_changed
14699 /* Function force-mode-line-update is used to force a thorough
14700 redisplay. It sets either windows_or_buffers_changed or
14701 update_mode_lines. So don't take a shortcut here for these
14702 cases. */
14703 && !update_mode_lines
14704 && !windows_or_buffers_changed
14705 && !cursor_type_changed
14706 /* Can't use this case if highlighting a region. When a
14707 region exists, cursor movement has to do more than just
14708 set the cursor. */
14709 && !(!NILP (Vtransient_mark_mode)
14710 && !NILP (BVAR (current_buffer, mark_active)))
14711 && NILP (w->region_showing)
14712 && NILP (Vshow_trailing_whitespace)
14713 /* Right after splitting windows, last_point may be nil. */
14714 && INTEGERP (w->last_point)
14715 /* This code is not used for mini-buffer for the sake of the case
14716 of redisplaying to replace an echo area message; since in
14717 that case the mini-buffer contents per se are usually
14718 unchanged. This code is of no real use in the mini-buffer
14719 since the handling of this_line_start_pos, etc., in redisplay
14720 handles the same cases. */
14721 && !EQ (window, minibuf_window)
14722 /* When splitting windows or for new windows, it happens that
14723 redisplay is called with a nil window_end_vpos or one being
14724 larger than the window. This should really be fixed in
14725 window.c. I don't have this on my list, now, so we do
14726 approximately the same as the old redisplay code. --gerd. */
14727 && INTEGERP (w->window_end_vpos)
14728 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14729 && (FRAME_WINDOW_P (f)
14730 || !overlay_arrow_in_current_buffer_p ()))
14732 int this_scroll_margin, top_scroll_margin;
14733 struct glyph_row *row = NULL;
14735 #if GLYPH_DEBUG
14736 debug_method_add (w, "cursor movement");
14737 #endif
14739 /* Scroll if point within this distance from the top or bottom
14740 of the window. This is a pixel value. */
14741 if (scroll_margin > 0)
14743 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14744 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14746 else
14747 this_scroll_margin = 0;
14749 top_scroll_margin = this_scroll_margin;
14750 if (WINDOW_WANTS_HEADER_LINE_P (w))
14751 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14753 /* Start with the row the cursor was displayed during the last
14754 not paused redisplay. Give up if that row is not valid. */
14755 if (w->last_cursor.vpos < 0
14756 || w->last_cursor.vpos >= w->current_matrix->nrows)
14757 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14758 else
14760 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14761 if (row->mode_line_p)
14762 ++row;
14763 if (!row->enabled_p)
14764 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14767 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14769 int scroll_p = 0, must_scroll = 0;
14770 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14772 if (PT > XFASTINT (w->last_point))
14774 /* Point has moved forward. */
14775 while (MATRIX_ROW_END_CHARPOS (row) < PT
14776 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14778 xassert (row->enabled_p);
14779 ++row;
14782 /* If the end position of a row equals the start
14783 position of the next row, and PT is at that position,
14784 we would rather display cursor in the next line. */
14785 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14786 && MATRIX_ROW_END_CHARPOS (row) == PT
14787 && row < w->current_matrix->rows
14788 + w->current_matrix->nrows - 1
14789 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14790 && !cursor_row_p (row))
14791 ++row;
14793 /* If within the scroll margin, scroll. Note that
14794 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14795 the next line would be drawn, and that
14796 this_scroll_margin can be zero. */
14797 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14798 || PT > MATRIX_ROW_END_CHARPOS (row)
14799 /* Line is completely visible last line in window
14800 and PT is to be set in the next line. */
14801 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14802 && PT == MATRIX_ROW_END_CHARPOS (row)
14803 && !row->ends_at_zv_p
14804 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14805 scroll_p = 1;
14807 else if (PT < XFASTINT (w->last_point))
14809 /* Cursor has to be moved backward. Note that PT >=
14810 CHARPOS (startp) because of the outer if-statement. */
14811 while (!row->mode_line_p
14812 && (MATRIX_ROW_START_CHARPOS (row) > PT
14813 || (MATRIX_ROW_START_CHARPOS (row) == PT
14814 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14815 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14816 row > w->current_matrix->rows
14817 && (row-1)->ends_in_newline_from_string_p))))
14818 && (row->y > top_scroll_margin
14819 || CHARPOS (startp) == BEGV))
14821 xassert (row->enabled_p);
14822 --row;
14825 /* Consider the following case: Window starts at BEGV,
14826 there is invisible, intangible text at BEGV, so that
14827 display starts at some point START > BEGV. It can
14828 happen that we are called with PT somewhere between
14829 BEGV and START. Try to handle that case. */
14830 if (row < w->current_matrix->rows
14831 || row->mode_line_p)
14833 row = w->current_matrix->rows;
14834 if (row->mode_line_p)
14835 ++row;
14838 /* Due to newlines in overlay strings, we may have to
14839 skip forward over overlay strings. */
14840 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14841 && MATRIX_ROW_END_CHARPOS (row) == PT
14842 && !cursor_row_p (row))
14843 ++row;
14845 /* If within the scroll margin, scroll. */
14846 if (row->y < top_scroll_margin
14847 && CHARPOS (startp) != BEGV)
14848 scroll_p = 1;
14850 else
14852 /* Cursor did not move. So don't scroll even if cursor line
14853 is partially visible, as it was so before. */
14854 rc = CURSOR_MOVEMENT_SUCCESS;
14857 if (PT < MATRIX_ROW_START_CHARPOS (row)
14858 || PT > MATRIX_ROW_END_CHARPOS (row))
14860 /* if PT is not in the glyph row, give up. */
14861 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14862 must_scroll = 1;
14864 else if (rc != CURSOR_MOVEMENT_SUCCESS
14865 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14867 /* If rows are bidi-reordered and point moved, back up
14868 until we find a row that does not belong to a
14869 continuation line. This is because we must consider
14870 all rows of a continued line as candidates for the
14871 new cursor positioning, since row start and end
14872 positions change non-linearly with vertical position
14873 in such rows. */
14874 /* FIXME: Revisit this when glyph ``spilling'' in
14875 continuation lines' rows is implemented for
14876 bidi-reordered rows. */
14877 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
14879 /* If we hit the beginning of the displayed portion
14880 without finding the first row of a continued
14881 line, give up. */
14882 if (row <= w->current_matrix->rows)
14884 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14885 break;
14887 xassert (row->enabled_p);
14888 --row;
14891 if (must_scroll)
14893 else if (rc != CURSOR_MOVEMENT_SUCCESS
14894 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
14895 && make_cursor_line_fully_visible_p)
14897 if (PT == MATRIX_ROW_END_CHARPOS (row)
14898 && !row->ends_at_zv_p
14899 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14900 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14901 else if (row->height > window_box_height (w))
14903 /* If we end up in a partially visible line, let's
14904 make it fully visible, except when it's taller
14905 than the window, in which case we can't do much
14906 about it. */
14907 *scroll_step = 1;
14908 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14910 else
14912 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14913 if (!cursor_row_fully_visible_p (w, 0, 1))
14914 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14915 else
14916 rc = CURSOR_MOVEMENT_SUCCESS;
14919 else if (scroll_p)
14920 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14921 else if (rc != CURSOR_MOVEMENT_SUCCESS
14922 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14924 /* With bidi-reordered rows, there could be more than
14925 one candidate row whose start and end positions
14926 occlude point. We need to let set_cursor_from_row
14927 find the best candidate. */
14928 /* FIXME: Revisit this when glyph ``spilling'' in
14929 continuation lines' rows is implemented for
14930 bidi-reordered rows. */
14931 int rv = 0;
14935 int at_zv_p = 0, exact_match_p = 0;
14937 if (MATRIX_ROW_START_CHARPOS (row) <= PT
14938 && PT <= MATRIX_ROW_END_CHARPOS (row)
14939 && cursor_row_p (row))
14940 rv |= set_cursor_from_row (w, row, w->current_matrix,
14941 0, 0, 0, 0);
14942 /* As soon as we've found the exact match for point,
14943 or the first suitable row whose ends_at_zv_p flag
14944 is set, we are done. */
14945 at_zv_p =
14946 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
14947 if (rv && !at_zv_p
14948 && w->cursor.hpos >= 0
14949 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
14950 w->cursor.vpos))
14952 struct glyph_row *candidate =
14953 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14954 struct glyph *g =
14955 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
14956 EMACS_INT endpos = MATRIX_ROW_END_CHARPOS (candidate);
14958 exact_match_p =
14959 (BUFFERP (g->object) && g->charpos == PT)
14960 || (INTEGERP (g->object)
14961 && (g->charpos == PT
14962 || (g->charpos == 0 && endpos - 1 == PT)));
14964 if (rv && (at_zv_p || exact_match_p))
14966 rc = CURSOR_MOVEMENT_SUCCESS;
14967 break;
14969 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
14970 break;
14971 ++row;
14973 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
14974 || row->continued_p)
14975 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
14976 || (MATRIX_ROW_START_CHARPOS (row) == PT
14977 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
14978 /* If we didn't find any candidate rows, or exited the
14979 loop before all the candidates were examined, signal
14980 to the caller that this method failed. */
14981 if (rc != CURSOR_MOVEMENT_SUCCESS
14982 && !(rv
14983 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14984 && !row->continued_p))
14985 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14986 else if (rv)
14987 rc = CURSOR_MOVEMENT_SUCCESS;
14989 else
14993 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
14995 rc = CURSOR_MOVEMENT_SUCCESS;
14996 break;
14998 ++row;
15000 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15001 && MATRIX_ROW_START_CHARPOS (row) == PT
15002 && cursor_row_p (row));
15007 return rc;
15010 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15011 static
15012 #endif
15013 void
15014 set_vertical_scroll_bar (struct window *w)
15016 EMACS_INT start, end, whole;
15018 /* Calculate the start and end positions for the current window.
15019 At some point, it would be nice to choose between scrollbars
15020 which reflect the whole buffer size, with special markers
15021 indicating narrowing, and scrollbars which reflect only the
15022 visible region.
15024 Note that mini-buffers sometimes aren't displaying any text. */
15025 if (!MINI_WINDOW_P (w)
15026 || (w == XWINDOW (minibuf_window)
15027 && NILP (echo_area_buffer[0])))
15029 struct buffer *buf = XBUFFER (w->buffer);
15030 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15031 start = marker_position (w->start) - BUF_BEGV (buf);
15032 /* I don't think this is guaranteed to be right. For the
15033 moment, we'll pretend it is. */
15034 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15036 if (end < start)
15037 end = start;
15038 if (whole < (end - start))
15039 whole = end - start;
15041 else
15042 start = end = whole = 0;
15044 /* Indicate what this scroll bar ought to be displaying now. */
15045 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15046 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15047 (w, end - start, whole, start);
15051 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15052 selected_window is redisplayed.
15054 We can return without actually redisplaying the window if
15055 fonts_changed_p is nonzero. In that case, redisplay_internal will
15056 retry. */
15058 static void
15059 redisplay_window (Lisp_Object window, int just_this_one_p)
15061 struct window *w = XWINDOW (window);
15062 struct frame *f = XFRAME (w->frame);
15063 struct buffer *buffer = XBUFFER (w->buffer);
15064 struct buffer *old = current_buffer;
15065 struct text_pos lpoint, opoint, startp;
15066 int update_mode_line;
15067 int tem;
15068 struct it it;
15069 /* Record it now because it's overwritten. */
15070 int current_matrix_up_to_date_p = 0;
15071 int used_current_matrix_p = 0;
15072 /* This is less strict than current_matrix_up_to_date_p.
15073 It indicates that the buffer contents and narrowing are unchanged. */
15074 int buffer_unchanged_p = 0;
15075 int temp_scroll_step = 0;
15076 int count = SPECPDL_INDEX ();
15077 int rc;
15078 int centering_position = -1;
15079 int last_line_misfit = 0;
15080 EMACS_INT beg_unchanged, end_unchanged;
15082 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15083 opoint = lpoint;
15085 /* W must be a leaf window here. */
15086 xassert (!NILP (w->buffer));
15087 #if GLYPH_DEBUG
15088 *w->desired_matrix->method = 0;
15089 #endif
15091 restart:
15092 reconsider_clip_changes (w, buffer);
15094 /* Has the mode line to be updated? */
15095 update_mode_line = (!NILP (w->update_mode_line)
15096 || update_mode_lines
15097 || buffer->clip_changed
15098 || buffer->prevent_redisplay_optimizations_p);
15100 if (MINI_WINDOW_P (w))
15102 if (w == XWINDOW (echo_area_window)
15103 && !NILP (echo_area_buffer[0]))
15105 if (update_mode_line)
15106 /* We may have to update a tty frame's menu bar or a
15107 tool-bar. Example `M-x C-h C-h C-g'. */
15108 goto finish_menu_bars;
15109 else
15110 /* We've already displayed the echo area glyphs in this window. */
15111 goto finish_scroll_bars;
15113 else if ((w != XWINDOW (minibuf_window)
15114 || minibuf_level == 0)
15115 /* When buffer is nonempty, redisplay window normally. */
15116 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15117 /* Quail displays non-mini buffers in minibuffer window.
15118 In that case, redisplay the window normally. */
15119 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15121 /* W is a mini-buffer window, but it's not active, so clear
15122 it. */
15123 int yb = window_text_bottom_y (w);
15124 struct glyph_row *row;
15125 int y;
15127 for (y = 0, row = w->desired_matrix->rows;
15128 y < yb;
15129 y += row->height, ++row)
15130 blank_row (w, row, y);
15131 goto finish_scroll_bars;
15134 clear_glyph_matrix (w->desired_matrix);
15137 /* Otherwise set up data on this window; select its buffer and point
15138 value. */
15139 /* Really select the buffer, for the sake of buffer-local
15140 variables. */
15141 set_buffer_internal_1 (XBUFFER (w->buffer));
15143 current_matrix_up_to_date_p
15144 = (!NILP (w->window_end_valid)
15145 && !current_buffer->clip_changed
15146 && !current_buffer->prevent_redisplay_optimizations_p
15147 && XFASTINT (w->last_modified) >= MODIFF
15148 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15150 /* Run the window-bottom-change-functions
15151 if it is possible that the text on the screen has changed
15152 (either due to modification of the text, or any other reason). */
15153 if (!current_matrix_up_to_date_p
15154 && !NILP (Vwindow_text_change_functions))
15156 safe_run_hooks (Qwindow_text_change_functions);
15157 goto restart;
15160 beg_unchanged = BEG_UNCHANGED;
15161 end_unchanged = END_UNCHANGED;
15163 SET_TEXT_POS (opoint, PT, PT_BYTE);
15165 specbind (Qinhibit_point_motion_hooks, Qt);
15167 buffer_unchanged_p
15168 = (!NILP (w->window_end_valid)
15169 && !current_buffer->clip_changed
15170 && XFASTINT (w->last_modified) >= MODIFF
15171 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15173 /* When windows_or_buffers_changed is non-zero, we can't rely on
15174 the window end being valid, so set it to nil there. */
15175 if (windows_or_buffers_changed)
15177 /* If window starts on a continuation line, maybe adjust the
15178 window start in case the window's width changed. */
15179 if (XMARKER (w->start)->buffer == current_buffer)
15180 compute_window_start_on_continuation_line (w);
15182 w->window_end_valid = Qnil;
15185 /* Some sanity checks. */
15186 CHECK_WINDOW_END (w);
15187 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15188 abort ();
15189 if (BYTEPOS (opoint) < CHARPOS (opoint))
15190 abort ();
15192 /* If %c is in mode line, update it if needed. */
15193 if (!NILP (w->column_number_displayed)
15194 /* This alternative quickly identifies a common case
15195 where no change is needed. */
15196 && !(PT == XFASTINT (w->last_point)
15197 && XFASTINT (w->last_modified) >= MODIFF
15198 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
15199 && (XFASTINT (w->column_number_displayed) != current_column ()))
15200 update_mode_line = 1;
15202 /* Count number of windows showing the selected buffer. An indirect
15203 buffer counts as its base buffer. */
15204 if (!just_this_one_p)
15206 struct buffer *current_base, *window_base;
15207 current_base = current_buffer;
15208 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15209 if (current_base->base_buffer)
15210 current_base = current_base->base_buffer;
15211 if (window_base->base_buffer)
15212 window_base = window_base->base_buffer;
15213 if (current_base == window_base)
15214 buffer_shared++;
15217 /* Point refers normally to the selected window. For any other
15218 window, set up appropriate value. */
15219 if (!EQ (window, selected_window))
15221 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
15222 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
15223 if (new_pt < BEGV)
15225 new_pt = BEGV;
15226 new_pt_byte = BEGV_BYTE;
15227 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15229 else if (new_pt > (ZV - 1))
15231 new_pt = ZV;
15232 new_pt_byte = ZV_BYTE;
15233 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15236 /* We don't use SET_PT so that the point-motion hooks don't run. */
15237 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15240 /* If any of the character widths specified in the display table
15241 have changed, invalidate the width run cache. It's true that
15242 this may be a bit late to catch such changes, but the rest of
15243 redisplay goes (non-fatally) haywire when the display table is
15244 changed, so why should we worry about doing any better? */
15245 if (current_buffer->width_run_cache)
15247 struct Lisp_Char_Table *disptab = buffer_display_table ();
15249 if (! disptab_matches_widthtab (disptab,
15250 XVECTOR (BVAR (current_buffer, width_table))))
15252 invalidate_region_cache (current_buffer,
15253 current_buffer->width_run_cache,
15254 BEG, Z);
15255 recompute_width_table (current_buffer, disptab);
15259 /* If window-start is screwed up, choose a new one. */
15260 if (XMARKER (w->start)->buffer != current_buffer)
15261 goto recenter;
15263 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15265 /* If someone specified a new starting point but did not insist,
15266 check whether it can be used. */
15267 if (!NILP (w->optional_new_start)
15268 && CHARPOS (startp) >= BEGV
15269 && CHARPOS (startp) <= ZV)
15271 w->optional_new_start = Qnil;
15272 start_display (&it, w, startp);
15273 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15274 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15275 if (IT_CHARPOS (it) == PT)
15276 w->force_start = Qt;
15277 /* IT may overshoot PT if text at PT is invisible. */
15278 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15279 w->force_start = Qt;
15282 force_start:
15284 /* Handle case where place to start displaying has been specified,
15285 unless the specified location is outside the accessible range. */
15286 if (!NILP (w->force_start)
15287 || w->frozen_window_start_p)
15289 /* We set this later on if we have to adjust point. */
15290 int new_vpos = -1;
15292 w->force_start = Qnil;
15293 w->vscroll = 0;
15294 w->window_end_valid = Qnil;
15296 /* Forget any recorded base line for line number display. */
15297 if (!buffer_unchanged_p)
15298 w->base_line_number = Qnil;
15300 /* Redisplay the mode line. Select the buffer properly for that.
15301 Also, run the hook window-scroll-functions
15302 because we have scrolled. */
15303 /* Note, we do this after clearing force_start because
15304 if there's an error, it is better to forget about force_start
15305 than to get into an infinite loop calling the hook functions
15306 and having them get more errors. */
15307 if (!update_mode_line
15308 || ! NILP (Vwindow_scroll_functions))
15310 update_mode_line = 1;
15311 w->update_mode_line = Qt;
15312 startp = run_window_scroll_functions (window, startp);
15315 w->last_modified = make_number (0);
15316 w->last_overlay_modified = make_number (0);
15317 if (CHARPOS (startp) < BEGV)
15318 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15319 else if (CHARPOS (startp) > ZV)
15320 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15322 /* Redisplay, then check if cursor has been set during the
15323 redisplay. Give up if new fonts were loaded. */
15324 /* We used to issue a CHECK_MARGINS argument to try_window here,
15325 but this causes scrolling to fail when point begins inside
15326 the scroll margin (bug#148) -- cyd */
15327 if (!try_window (window, startp, 0))
15329 w->force_start = Qt;
15330 clear_glyph_matrix (w->desired_matrix);
15331 goto need_larger_matrices;
15334 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15336 /* If point does not appear, try to move point so it does
15337 appear. The desired matrix has been built above, so we
15338 can use it here. */
15339 new_vpos = window_box_height (w) / 2;
15342 if (!cursor_row_fully_visible_p (w, 0, 0))
15344 /* Point does appear, but on a line partly visible at end of window.
15345 Move it back to a fully-visible line. */
15346 new_vpos = window_box_height (w);
15349 /* If we need to move point for either of the above reasons,
15350 now actually do it. */
15351 if (new_vpos >= 0)
15353 struct glyph_row *row;
15355 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15356 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15357 ++row;
15359 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15360 MATRIX_ROW_START_BYTEPOS (row));
15362 if (w != XWINDOW (selected_window))
15363 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15364 else if (current_buffer == old)
15365 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15367 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15369 /* If we are highlighting the region, then we just changed
15370 the region, so redisplay to show it. */
15371 if (!NILP (Vtransient_mark_mode)
15372 && !NILP (BVAR (current_buffer, mark_active)))
15374 clear_glyph_matrix (w->desired_matrix);
15375 if (!try_window (window, startp, 0))
15376 goto need_larger_matrices;
15380 #if GLYPH_DEBUG
15381 debug_method_add (w, "forced window start");
15382 #endif
15383 goto done;
15386 /* Handle case where text has not changed, only point, and it has
15387 not moved off the frame, and we are not retrying after hscroll.
15388 (current_matrix_up_to_date_p is nonzero when retrying.) */
15389 if (current_matrix_up_to_date_p
15390 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15391 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15393 switch (rc)
15395 case CURSOR_MOVEMENT_SUCCESS:
15396 used_current_matrix_p = 1;
15397 goto done;
15399 case CURSOR_MOVEMENT_MUST_SCROLL:
15400 goto try_to_scroll;
15402 default:
15403 abort ();
15406 /* If current starting point was originally the beginning of a line
15407 but no longer is, find a new starting point. */
15408 else if (!NILP (w->start_at_line_beg)
15409 && !(CHARPOS (startp) <= BEGV
15410 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15412 #if GLYPH_DEBUG
15413 debug_method_add (w, "recenter 1");
15414 #endif
15415 goto recenter;
15418 /* Try scrolling with try_window_id. Value is > 0 if update has
15419 been done, it is -1 if we know that the same window start will
15420 not work. It is 0 if unsuccessful for some other reason. */
15421 else if ((tem = try_window_id (w)) != 0)
15423 #if GLYPH_DEBUG
15424 debug_method_add (w, "try_window_id %d", tem);
15425 #endif
15427 if (fonts_changed_p)
15428 goto need_larger_matrices;
15429 if (tem > 0)
15430 goto done;
15432 /* Otherwise try_window_id has returned -1 which means that we
15433 don't want the alternative below this comment to execute. */
15435 else if (CHARPOS (startp) >= BEGV
15436 && CHARPOS (startp) <= ZV
15437 && PT >= CHARPOS (startp)
15438 && (CHARPOS (startp) < ZV
15439 /* Avoid starting at end of buffer. */
15440 || CHARPOS (startp) == BEGV
15441 || (XFASTINT (w->last_modified) >= MODIFF
15442 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
15444 int d1, d2, d3, d4, d5, d6;
15446 /* If first window line is a continuation line, and window start
15447 is inside the modified region, but the first change is before
15448 current window start, we must select a new window start.
15450 However, if this is the result of a down-mouse event (e.g. by
15451 extending the mouse-drag-overlay), we don't want to select a
15452 new window start, since that would change the position under
15453 the mouse, resulting in an unwanted mouse-movement rather
15454 than a simple mouse-click. */
15455 if (NILP (w->start_at_line_beg)
15456 && NILP (do_mouse_tracking)
15457 && CHARPOS (startp) > BEGV
15458 && CHARPOS (startp) > BEG + beg_unchanged
15459 && CHARPOS (startp) <= Z - end_unchanged
15460 /* Even if w->start_at_line_beg is nil, a new window may
15461 start at a line_beg, since that's how set_buffer_window
15462 sets it. So, we need to check the return value of
15463 compute_window_start_on_continuation_line. (See also
15464 bug#197). */
15465 && XMARKER (w->start)->buffer == current_buffer
15466 && compute_window_start_on_continuation_line (w)
15467 /* It doesn't make sense to force the window start like we
15468 do at label force_start if it is already known that point
15469 will not be visible in the resulting window, because
15470 doing so will move point from its correct position
15471 instead of scrolling the window to bring point into view.
15472 See bug#9324. */
15473 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15475 w->force_start = Qt;
15476 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15477 goto force_start;
15480 #if GLYPH_DEBUG
15481 debug_method_add (w, "same window start");
15482 #endif
15484 /* Try to redisplay starting at same place as before.
15485 If point has not moved off frame, accept the results. */
15486 if (!current_matrix_up_to_date_p
15487 /* Don't use try_window_reusing_current_matrix in this case
15488 because a window scroll function can have changed the
15489 buffer. */
15490 || !NILP (Vwindow_scroll_functions)
15491 || MINI_WINDOW_P (w)
15492 || !(used_current_matrix_p
15493 = try_window_reusing_current_matrix (w)))
15495 IF_DEBUG (debug_method_add (w, "1"));
15496 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15497 /* -1 means we need to scroll.
15498 0 means we need new matrices, but fonts_changed_p
15499 is set in that case, so we will detect it below. */
15500 goto try_to_scroll;
15503 if (fonts_changed_p)
15504 goto need_larger_matrices;
15506 if (w->cursor.vpos >= 0)
15508 if (!just_this_one_p
15509 || current_buffer->clip_changed
15510 || BEG_UNCHANGED < CHARPOS (startp))
15511 /* Forget any recorded base line for line number display. */
15512 w->base_line_number = Qnil;
15514 if (!cursor_row_fully_visible_p (w, 1, 0))
15516 clear_glyph_matrix (w->desired_matrix);
15517 last_line_misfit = 1;
15519 /* Drop through and scroll. */
15520 else
15521 goto done;
15523 else
15524 clear_glyph_matrix (w->desired_matrix);
15527 try_to_scroll:
15529 w->last_modified = make_number (0);
15530 w->last_overlay_modified = make_number (0);
15532 /* Redisplay the mode line. Select the buffer properly for that. */
15533 if (!update_mode_line)
15535 update_mode_line = 1;
15536 w->update_mode_line = Qt;
15539 /* Try to scroll by specified few lines. */
15540 if ((scroll_conservatively
15541 || emacs_scroll_step
15542 || temp_scroll_step
15543 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15544 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15545 && CHARPOS (startp) >= BEGV
15546 && CHARPOS (startp) <= ZV)
15548 /* The function returns -1 if new fonts were loaded, 1 if
15549 successful, 0 if not successful. */
15550 int ss = try_scrolling (window, just_this_one_p,
15551 scroll_conservatively,
15552 emacs_scroll_step,
15553 temp_scroll_step, last_line_misfit);
15554 switch (ss)
15556 case SCROLLING_SUCCESS:
15557 goto done;
15559 case SCROLLING_NEED_LARGER_MATRICES:
15560 goto need_larger_matrices;
15562 case SCROLLING_FAILED:
15563 break;
15565 default:
15566 abort ();
15570 /* Finally, just choose a place to start which positions point
15571 according to user preferences. */
15573 recenter:
15575 #if GLYPH_DEBUG
15576 debug_method_add (w, "recenter");
15577 #endif
15579 /* w->vscroll = 0; */
15581 /* Forget any previously recorded base line for line number display. */
15582 if (!buffer_unchanged_p)
15583 w->base_line_number = Qnil;
15585 /* Determine the window start relative to point. */
15586 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15587 it.current_y = it.last_visible_y;
15588 if (centering_position < 0)
15590 int margin =
15591 scroll_margin > 0
15592 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15593 : 0;
15594 EMACS_INT margin_pos = CHARPOS (startp);
15595 Lisp_Object aggressive;
15596 int scrolling_up;
15598 /* If there is a scroll margin at the top of the window, find
15599 its character position. */
15600 if (margin
15601 /* Cannot call start_display if startp is not in the
15602 accessible region of the buffer. This can happen when we
15603 have just switched to a different buffer and/or changed
15604 its restriction. In that case, startp is initialized to
15605 the character position 1 (BEGV) because we did not yet
15606 have chance to display the buffer even once. */
15607 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15609 struct it it1;
15610 void *it1data = NULL;
15612 SAVE_IT (it1, it, it1data);
15613 start_display (&it1, w, startp);
15614 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15615 margin_pos = IT_CHARPOS (it1);
15616 RESTORE_IT (&it, &it, it1data);
15618 scrolling_up = PT > margin_pos;
15619 aggressive =
15620 scrolling_up
15621 ? BVAR (current_buffer, scroll_up_aggressively)
15622 : BVAR (current_buffer, scroll_down_aggressively);
15624 if (!MINI_WINDOW_P (w)
15625 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15627 int pt_offset = 0;
15629 /* Setting scroll-conservatively overrides
15630 scroll-*-aggressively. */
15631 if (!scroll_conservatively && NUMBERP (aggressive))
15633 double float_amount = XFLOATINT (aggressive);
15635 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15636 if (pt_offset == 0 && float_amount > 0)
15637 pt_offset = 1;
15638 if (pt_offset && margin > 0)
15639 margin -= 1;
15641 /* Compute how much to move the window start backward from
15642 point so that point will be displayed where the user
15643 wants it. */
15644 if (scrolling_up)
15646 centering_position = it.last_visible_y;
15647 if (pt_offset)
15648 centering_position -= pt_offset;
15649 centering_position -=
15650 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15651 + WINDOW_HEADER_LINE_HEIGHT (w);
15652 /* Don't let point enter the scroll margin near top of
15653 the window. */
15654 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15655 centering_position = margin * FRAME_LINE_HEIGHT (f);
15657 else
15658 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15660 else
15661 /* Set the window start half the height of the window backward
15662 from point. */
15663 centering_position = window_box_height (w) / 2;
15665 move_it_vertically_backward (&it, centering_position);
15667 xassert (IT_CHARPOS (it) >= BEGV);
15669 /* The function move_it_vertically_backward may move over more
15670 than the specified y-distance. If it->w is small, e.g. a
15671 mini-buffer window, we may end up in front of the window's
15672 display area. Start displaying at the start of the line
15673 containing PT in this case. */
15674 if (it.current_y <= 0)
15676 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15677 move_it_vertically_backward (&it, 0);
15678 it.current_y = 0;
15681 it.current_x = it.hpos = 0;
15683 /* Set the window start position here explicitly, to avoid an
15684 infinite loop in case the functions in window-scroll-functions
15685 get errors. */
15686 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15688 /* Run scroll hooks. */
15689 startp = run_window_scroll_functions (window, it.current.pos);
15691 /* Redisplay the window. */
15692 if (!current_matrix_up_to_date_p
15693 || windows_or_buffers_changed
15694 || cursor_type_changed
15695 /* Don't use try_window_reusing_current_matrix in this case
15696 because it can have changed the buffer. */
15697 || !NILP (Vwindow_scroll_functions)
15698 || !just_this_one_p
15699 || MINI_WINDOW_P (w)
15700 || !(used_current_matrix_p
15701 = try_window_reusing_current_matrix (w)))
15702 try_window (window, startp, 0);
15704 /* If new fonts have been loaded (due to fontsets), give up. We
15705 have to start a new redisplay since we need to re-adjust glyph
15706 matrices. */
15707 if (fonts_changed_p)
15708 goto need_larger_matrices;
15710 /* If cursor did not appear assume that the middle of the window is
15711 in the first line of the window. Do it again with the next line.
15712 (Imagine a window of height 100, displaying two lines of height
15713 60. Moving back 50 from it->last_visible_y will end in the first
15714 line.) */
15715 if (w->cursor.vpos < 0)
15717 if (!NILP (w->window_end_valid)
15718 && PT >= Z - XFASTINT (w->window_end_pos))
15720 clear_glyph_matrix (w->desired_matrix);
15721 move_it_by_lines (&it, 1);
15722 try_window (window, it.current.pos, 0);
15724 else if (PT < IT_CHARPOS (it))
15726 clear_glyph_matrix (w->desired_matrix);
15727 move_it_by_lines (&it, -1);
15728 try_window (window, it.current.pos, 0);
15730 else
15732 /* Not much we can do about it. */
15736 /* Consider the following case: Window starts at BEGV, there is
15737 invisible, intangible text at BEGV, so that display starts at
15738 some point START > BEGV. It can happen that we are called with
15739 PT somewhere between BEGV and START. Try to handle that case. */
15740 if (w->cursor.vpos < 0)
15742 struct glyph_row *row = w->current_matrix->rows;
15743 if (row->mode_line_p)
15744 ++row;
15745 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15748 if (!cursor_row_fully_visible_p (w, 0, 0))
15750 /* If vscroll is enabled, disable it and try again. */
15751 if (w->vscroll)
15753 w->vscroll = 0;
15754 clear_glyph_matrix (w->desired_matrix);
15755 goto recenter;
15758 /* Users who set scroll-conservatively to a large number want
15759 point just above/below the scroll margin. If we ended up
15760 with point's row partially visible, move the window start to
15761 make that row fully visible and out of the margin. */
15762 if (scroll_conservatively > SCROLL_LIMIT)
15764 int margin =
15765 scroll_margin > 0
15766 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15767 : 0;
15768 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
15770 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
15771 clear_glyph_matrix (w->desired_matrix);
15772 if (1 == try_window (window, it.current.pos,
15773 TRY_WINDOW_CHECK_MARGINS))
15774 goto done;
15777 /* If centering point failed to make the whole line visible,
15778 put point at the top instead. That has to make the whole line
15779 visible, if it can be done. */
15780 if (centering_position == 0)
15781 goto done;
15783 clear_glyph_matrix (w->desired_matrix);
15784 centering_position = 0;
15785 goto recenter;
15788 done:
15790 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15791 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
15792 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
15793 ? Qt : Qnil);
15795 /* Display the mode line, if we must. */
15796 if ((update_mode_line
15797 /* If window not full width, must redo its mode line
15798 if (a) the window to its side is being redone and
15799 (b) we do a frame-based redisplay. This is a consequence
15800 of how inverted lines are drawn in frame-based redisplay. */
15801 || (!just_this_one_p
15802 && !FRAME_WINDOW_P (f)
15803 && !WINDOW_FULL_WIDTH_P (w))
15804 /* Line number to display. */
15805 || INTEGERP (w->base_line_pos)
15806 /* Column number is displayed and different from the one displayed. */
15807 || (!NILP (w->column_number_displayed)
15808 && (XFASTINT (w->column_number_displayed) != current_column ())))
15809 /* This means that the window has a mode line. */
15810 && (WINDOW_WANTS_MODELINE_P (w)
15811 || WINDOW_WANTS_HEADER_LINE_P (w)))
15813 display_mode_lines (w);
15815 /* If mode line height has changed, arrange for a thorough
15816 immediate redisplay using the correct mode line height. */
15817 if (WINDOW_WANTS_MODELINE_P (w)
15818 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
15820 fonts_changed_p = 1;
15821 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
15822 = DESIRED_MODE_LINE_HEIGHT (w);
15825 /* If header line height has changed, arrange for a thorough
15826 immediate redisplay using the correct header line height. */
15827 if (WINDOW_WANTS_HEADER_LINE_P (w)
15828 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
15830 fonts_changed_p = 1;
15831 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
15832 = DESIRED_HEADER_LINE_HEIGHT (w);
15835 if (fonts_changed_p)
15836 goto need_larger_matrices;
15839 if (!line_number_displayed
15840 && !BUFFERP (w->base_line_pos))
15842 w->base_line_pos = Qnil;
15843 w->base_line_number = Qnil;
15846 finish_menu_bars:
15848 /* When we reach a frame's selected window, redo the frame's menu bar. */
15849 if (update_mode_line
15850 && EQ (FRAME_SELECTED_WINDOW (f), window))
15852 int redisplay_menu_p = 0;
15854 if (FRAME_WINDOW_P (f))
15856 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
15857 || defined (HAVE_NS) || defined (USE_GTK)
15858 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
15859 #else
15860 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15861 #endif
15863 else
15864 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15866 if (redisplay_menu_p)
15867 display_menu_bar (w);
15869 #ifdef HAVE_WINDOW_SYSTEM
15870 if (FRAME_WINDOW_P (f))
15872 #if defined (USE_GTK) || defined (HAVE_NS)
15873 if (FRAME_EXTERNAL_TOOL_BAR (f))
15874 redisplay_tool_bar (f);
15875 #else
15876 if (WINDOWP (f->tool_bar_window)
15877 && (FRAME_TOOL_BAR_LINES (f) > 0
15878 || !NILP (Vauto_resize_tool_bars))
15879 && redisplay_tool_bar (f))
15880 ignore_mouse_drag_p = 1;
15881 #endif
15883 #endif
15886 #ifdef HAVE_WINDOW_SYSTEM
15887 if (FRAME_WINDOW_P (f)
15888 && update_window_fringes (w, (just_this_one_p
15889 || (!used_current_matrix_p && !overlay_arrow_seen)
15890 || w->pseudo_window_p)))
15892 update_begin (f);
15893 BLOCK_INPUT;
15894 if (draw_window_fringes (w, 1))
15895 x_draw_vertical_border (w);
15896 UNBLOCK_INPUT;
15897 update_end (f);
15899 #endif /* HAVE_WINDOW_SYSTEM */
15901 /* We go to this label, with fonts_changed_p nonzero,
15902 if it is necessary to try again using larger glyph matrices.
15903 We have to redeem the scroll bar even in this case,
15904 because the loop in redisplay_internal expects that. */
15905 need_larger_matrices:
15907 finish_scroll_bars:
15909 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
15911 /* Set the thumb's position and size. */
15912 set_vertical_scroll_bar (w);
15914 /* Note that we actually used the scroll bar attached to this
15915 window, so it shouldn't be deleted at the end of redisplay. */
15916 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
15917 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
15920 /* Restore current_buffer and value of point in it. The window
15921 update may have changed the buffer, so first make sure `opoint'
15922 is still valid (Bug#6177). */
15923 if (CHARPOS (opoint) < BEGV)
15924 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15925 else if (CHARPOS (opoint) > ZV)
15926 TEMP_SET_PT_BOTH (Z, Z_BYTE);
15927 else
15928 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
15930 set_buffer_internal_1 (old);
15931 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
15932 shorter. This can be caused by log truncation in *Messages*. */
15933 if (CHARPOS (lpoint) <= ZV)
15934 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15936 unbind_to (count, Qnil);
15940 /* Build the complete desired matrix of WINDOW with a window start
15941 buffer position POS.
15943 Value is 1 if successful. It is zero if fonts were loaded during
15944 redisplay which makes re-adjusting glyph matrices necessary, and -1
15945 if point would appear in the scroll margins.
15946 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
15947 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
15948 set in FLAGS.) */
15951 try_window (Lisp_Object window, struct text_pos pos, int flags)
15953 struct window *w = XWINDOW (window);
15954 struct it it;
15955 struct glyph_row *last_text_row = NULL;
15956 struct frame *f = XFRAME (w->frame);
15958 /* Make POS the new window start. */
15959 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
15961 /* Mark cursor position as unknown. No overlay arrow seen. */
15962 w->cursor.vpos = -1;
15963 overlay_arrow_seen = 0;
15965 /* Initialize iterator and info to start at POS. */
15966 start_display (&it, w, pos);
15968 /* Display all lines of W. */
15969 while (it.current_y < it.last_visible_y)
15971 if (display_line (&it))
15972 last_text_row = it.glyph_row - 1;
15973 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
15974 return 0;
15977 /* Don't let the cursor end in the scroll margins. */
15978 if ((flags & TRY_WINDOW_CHECK_MARGINS)
15979 && !MINI_WINDOW_P (w))
15981 int this_scroll_margin;
15983 if (scroll_margin > 0)
15985 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15986 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15988 else
15989 this_scroll_margin = 0;
15991 if ((w->cursor.y >= 0 /* not vscrolled */
15992 && w->cursor.y < this_scroll_margin
15993 && CHARPOS (pos) > BEGV
15994 && IT_CHARPOS (it) < ZV)
15995 /* rms: considering make_cursor_line_fully_visible_p here
15996 seems to give wrong results. We don't want to recenter
15997 when the last line is partly visible, we want to allow
15998 that case to be handled in the usual way. */
15999 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16001 w->cursor.vpos = -1;
16002 clear_glyph_matrix (w->desired_matrix);
16003 return -1;
16007 /* If bottom moved off end of frame, change mode line percentage. */
16008 if (XFASTINT (w->window_end_pos) <= 0
16009 && Z != IT_CHARPOS (it))
16010 w->update_mode_line = Qt;
16012 /* Set window_end_pos to the offset of the last character displayed
16013 on the window from the end of current_buffer. Set
16014 window_end_vpos to its row number. */
16015 if (last_text_row)
16017 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16018 w->window_end_bytepos
16019 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16020 w->window_end_pos
16021 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16022 w->window_end_vpos
16023 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16024 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
16025 ->displays_text_p);
16027 else
16029 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16030 w->window_end_pos = make_number (Z - ZV);
16031 w->window_end_vpos = make_number (0);
16034 /* But that is not valid info until redisplay finishes. */
16035 w->window_end_valid = Qnil;
16036 return 1;
16041 /************************************************************************
16042 Window redisplay reusing current matrix when buffer has not changed
16043 ************************************************************************/
16045 /* Try redisplay of window W showing an unchanged buffer with a
16046 different window start than the last time it was displayed by
16047 reusing its current matrix. Value is non-zero if successful.
16048 W->start is the new window start. */
16050 static int
16051 try_window_reusing_current_matrix (struct window *w)
16053 struct frame *f = XFRAME (w->frame);
16054 struct glyph_row *bottom_row;
16055 struct it it;
16056 struct run run;
16057 struct text_pos start, new_start;
16058 int nrows_scrolled, i;
16059 struct glyph_row *last_text_row;
16060 struct glyph_row *last_reused_text_row;
16061 struct glyph_row *start_row;
16062 int start_vpos, min_y, max_y;
16064 #if GLYPH_DEBUG
16065 if (inhibit_try_window_reusing)
16066 return 0;
16067 #endif
16069 if (/* This function doesn't handle terminal frames. */
16070 !FRAME_WINDOW_P (f)
16071 /* Don't try to reuse the display if windows have been split
16072 or such. */
16073 || windows_or_buffers_changed
16074 || cursor_type_changed)
16075 return 0;
16077 /* Can't do this if region may have changed. */
16078 if ((!NILP (Vtransient_mark_mode)
16079 && !NILP (BVAR (current_buffer, mark_active)))
16080 || !NILP (w->region_showing)
16081 || !NILP (Vshow_trailing_whitespace))
16082 return 0;
16084 /* If top-line visibility has changed, give up. */
16085 if (WINDOW_WANTS_HEADER_LINE_P (w)
16086 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16087 return 0;
16089 /* Give up if old or new display is scrolled vertically. We could
16090 make this function handle this, but right now it doesn't. */
16091 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16092 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16093 return 0;
16095 /* The variable new_start now holds the new window start. The old
16096 start `start' can be determined from the current matrix. */
16097 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16098 start = start_row->minpos;
16099 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16101 /* Clear the desired matrix for the display below. */
16102 clear_glyph_matrix (w->desired_matrix);
16104 if (CHARPOS (new_start) <= CHARPOS (start))
16106 /* Don't use this method if the display starts with an ellipsis
16107 displayed for invisible text. It's not easy to handle that case
16108 below, and it's certainly not worth the effort since this is
16109 not a frequent case. */
16110 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16111 return 0;
16113 IF_DEBUG (debug_method_add (w, "twu1"));
16115 /* Display up to a row that can be reused. The variable
16116 last_text_row is set to the last row displayed that displays
16117 text. Note that it.vpos == 0 if or if not there is a
16118 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16119 start_display (&it, w, new_start);
16120 w->cursor.vpos = -1;
16121 last_text_row = last_reused_text_row = NULL;
16123 while (it.current_y < it.last_visible_y
16124 && !fonts_changed_p)
16126 /* If we have reached into the characters in the START row,
16127 that means the line boundaries have changed. So we
16128 can't start copying with the row START. Maybe it will
16129 work to start copying with the following row. */
16130 while (IT_CHARPOS (it) > CHARPOS (start))
16132 /* Advance to the next row as the "start". */
16133 start_row++;
16134 start = start_row->minpos;
16135 /* If there are no more rows to try, or just one, give up. */
16136 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16137 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16138 || CHARPOS (start) == ZV)
16140 clear_glyph_matrix (w->desired_matrix);
16141 return 0;
16144 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16146 /* If we have reached alignment, we can copy the rest of the
16147 rows. */
16148 if (IT_CHARPOS (it) == CHARPOS (start)
16149 /* Don't accept "alignment" inside a display vector,
16150 since start_row could have started in the middle of
16151 that same display vector (thus their character
16152 positions match), and we have no way of telling if
16153 that is the case. */
16154 && it.current.dpvec_index < 0)
16155 break;
16157 if (display_line (&it))
16158 last_text_row = it.glyph_row - 1;
16162 /* A value of current_y < last_visible_y means that we stopped
16163 at the previous window start, which in turn means that we
16164 have at least one reusable row. */
16165 if (it.current_y < it.last_visible_y)
16167 struct glyph_row *row;
16169 /* IT.vpos always starts from 0; it counts text lines. */
16170 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16172 /* Find PT if not already found in the lines displayed. */
16173 if (w->cursor.vpos < 0)
16175 int dy = it.current_y - start_row->y;
16177 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16178 row = row_containing_pos (w, PT, row, NULL, dy);
16179 if (row)
16180 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16181 dy, nrows_scrolled);
16182 else
16184 clear_glyph_matrix (w->desired_matrix);
16185 return 0;
16189 /* Scroll the display. Do it before the current matrix is
16190 changed. The problem here is that update has not yet
16191 run, i.e. part of the current matrix is not up to date.
16192 scroll_run_hook will clear the cursor, and use the
16193 current matrix to get the height of the row the cursor is
16194 in. */
16195 run.current_y = start_row->y;
16196 run.desired_y = it.current_y;
16197 run.height = it.last_visible_y - it.current_y;
16199 if (run.height > 0 && run.current_y != run.desired_y)
16201 update_begin (f);
16202 FRAME_RIF (f)->update_window_begin_hook (w);
16203 FRAME_RIF (f)->clear_window_mouse_face (w);
16204 FRAME_RIF (f)->scroll_run_hook (w, &run);
16205 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16206 update_end (f);
16209 /* Shift current matrix down by nrows_scrolled lines. */
16210 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16211 rotate_matrix (w->current_matrix,
16212 start_vpos,
16213 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16214 nrows_scrolled);
16216 /* Disable lines that must be updated. */
16217 for (i = 0; i < nrows_scrolled; ++i)
16218 (start_row + i)->enabled_p = 0;
16220 /* Re-compute Y positions. */
16221 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16222 max_y = it.last_visible_y;
16223 for (row = start_row + nrows_scrolled;
16224 row < bottom_row;
16225 ++row)
16227 row->y = it.current_y;
16228 row->visible_height = row->height;
16230 if (row->y < min_y)
16231 row->visible_height -= min_y - row->y;
16232 if (row->y + row->height > max_y)
16233 row->visible_height -= row->y + row->height - max_y;
16234 if (row->fringe_bitmap_periodic_p)
16235 row->redraw_fringe_bitmaps_p = 1;
16237 it.current_y += row->height;
16239 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16240 last_reused_text_row = row;
16241 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16242 break;
16245 /* Disable lines in the current matrix which are now
16246 below the window. */
16247 for (++row; row < bottom_row; ++row)
16248 row->enabled_p = row->mode_line_p = 0;
16251 /* Update window_end_pos etc.; last_reused_text_row is the last
16252 reused row from the current matrix containing text, if any.
16253 The value of last_text_row is the last displayed line
16254 containing text. */
16255 if (last_reused_text_row)
16257 w->window_end_bytepos
16258 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16259 w->window_end_pos
16260 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
16261 w->window_end_vpos
16262 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16263 w->current_matrix));
16265 else if (last_text_row)
16267 w->window_end_bytepos
16268 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16269 w->window_end_pos
16270 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16271 w->window_end_vpos
16272 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16274 else
16276 /* This window must be completely empty. */
16277 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16278 w->window_end_pos = make_number (Z - ZV);
16279 w->window_end_vpos = make_number (0);
16281 w->window_end_valid = Qnil;
16283 /* Update hint: don't try scrolling again in update_window. */
16284 w->desired_matrix->no_scrolling_p = 1;
16286 #if GLYPH_DEBUG
16287 debug_method_add (w, "try_window_reusing_current_matrix 1");
16288 #endif
16289 return 1;
16291 else if (CHARPOS (new_start) > CHARPOS (start))
16293 struct glyph_row *pt_row, *row;
16294 struct glyph_row *first_reusable_row;
16295 struct glyph_row *first_row_to_display;
16296 int dy;
16297 int yb = window_text_bottom_y (w);
16299 /* Find the row starting at new_start, if there is one. Don't
16300 reuse a partially visible line at the end. */
16301 first_reusable_row = start_row;
16302 while (first_reusable_row->enabled_p
16303 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16304 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16305 < CHARPOS (new_start)))
16306 ++first_reusable_row;
16308 /* Give up if there is no row to reuse. */
16309 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16310 || !first_reusable_row->enabled_p
16311 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16312 != CHARPOS (new_start)))
16313 return 0;
16315 /* We can reuse fully visible rows beginning with
16316 first_reusable_row to the end of the window. Set
16317 first_row_to_display to the first row that cannot be reused.
16318 Set pt_row to the row containing point, if there is any. */
16319 pt_row = NULL;
16320 for (first_row_to_display = first_reusable_row;
16321 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16322 ++first_row_to_display)
16324 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16325 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16326 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16327 && first_row_to_display->ends_at_zv_p
16328 && pt_row == NULL)))
16329 pt_row = first_row_to_display;
16332 /* Start displaying at the start of first_row_to_display. */
16333 xassert (first_row_to_display->y < yb);
16334 init_to_row_start (&it, w, first_row_to_display);
16336 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16337 - start_vpos);
16338 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16339 - nrows_scrolled);
16340 it.current_y = (first_row_to_display->y - first_reusable_row->y
16341 + WINDOW_HEADER_LINE_HEIGHT (w));
16343 /* Display lines beginning with first_row_to_display in the
16344 desired matrix. Set last_text_row to the last row displayed
16345 that displays text. */
16346 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16347 if (pt_row == NULL)
16348 w->cursor.vpos = -1;
16349 last_text_row = NULL;
16350 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16351 if (display_line (&it))
16352 last_text_row = it.glyph_row - 1;
16354 /* If point is in a reused row, adjust y and vpos of the cursor
16355 position. */
16356 if (pt_row)
16358 w->cursor.vpos -= nrows_scrolled;
16359 w->cursor.y -= first_reusable_row->y - start_row->y;
16362 /* Give up if point isn't in a row displayed or reused. (This
16363 also handles the case where w->cursor.vpos < nrows_scrolled
16364 after the calls to display_line, which can happen with scroll
16365 margins. See bug#1295.) */
16366 if (w->cursor.vpos < 0)
16368 clear_glyph_matrix (w->desired_matrix);
16369 return 0;
16372 /* Scroll the display. */
16373 run.current_y = first_reusable_row->y;
16374 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16375 run.height = it.last_visible_y - run.current_y;
16376 dy = run.current_y - run.desired_y;
16378 if (run.height)
16380 update_begin (f);
16381 FRAME_RIF (f)->update_window_begin_hook (w);
16382 FRAME_RIF (f)->clear_window_mouse_face (w);
16383 FRAME_RIF (f)->scroll_run_hook (w, &run);
16384 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16385 update_end (f);
16388 /* Adjust Y positions of reused rows. */
16389 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16390 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16391 max_y = it.last_visible_y;
16392 for (row = first_reusable_row; row < first_row_to_display; ++row)
16394 row->y -= dy;
16395 row->visible_height = row->height;
16396 if (row->y < min_y)
16397 row->visible_height -= min_y - row->y;
16398 if (row->y + row->height > max_y)
16399 row->visible_height -= row->y + row->height - max_y;
16400 if (row->fringe_bitmap_periodic_p)
16401 row->redraw_fringe_bitmaps_p = 1;
16404 /* Scroll the current matrix. */
16405 xassert (nrows_scrolled > 0);
16406 rotate_matrix (w->current_matrix,
16407 start_vpos,
16408 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16409 -nrows_scrolled);
16411 /* Disable rows not reused. */
16412 for (row -= nrows_scrolled; row < bottom_row; ++row)
16413 row->enabled_p = 0;
16415 /* Point may have moved to a different line, so we cannot assume that
16416 the previous cursor position is valid; locate the correct row. */
16417 if (pt_row)
16419 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16420 row < bottom_row
16421 && PT >= MATRIX_ROW_END_CHARPOS (row)
16422 && !row->ends_at_zv_p;
16423 row++)
16425 w->cursor.vpos++;
16426 w->cursor.y = row->y;
16428 if (row < bottom_row)
16430 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16431 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16433 /* Can't use this optimization with bidi-reordered glyph
16434 rows, unless cursor is already at point. */
16435 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16437 if (!(w->cursor.hpos >= 0
16438 && w->cursor.hpos < row->used[TEXT_AREA]
16439 && BUFFERP (glyph->object)
16440 && glyph->charpos == PT))
16441 return 0;
16443 else
16444 for (; glyph < end
16445 && (!BUFFERP (glyph->object)
16446 || glyph->charpos < PT);
16447 glyph++)
16449 w->cursor.hpos++;
16450 w->cursor.x += glyph->pixel_width;
16455 /* Adjust window end. A null value of last_text_row means that
16456 the window end is in reused rows which in turn means that
16457 only its vpos can have changed. */
16458 if (last_text_row)
16460 w->window_end_bytepos
16461 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16462 w->window_end_pos
16463 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16464 w->window_end_vpos
16465 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16467 else
16469 w->window_end_vpos
16470 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16473 w->window_end_valid = Qnil;
16474 w->desired_matrix->no_scrolling_p = 1;
16476 #if GLYPH_DEBUG
16477 debug_method_add (w, "try_window_reusing_current_matrix 2");
16478 #endif
16479 return 1;
16482 return 0;
16487 /************************************************************************
16488 Window redisplay reusing current matrix when buffer has changed
16489 ************************************************************************/
16491 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16492 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16493 EMACS_INT *, EMACS_INT *);
16494 static struct glyph_row *
16495 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16496 struct glyph_row *);
16499 /* Return the last row in MATRIX displaying text. If row START is
16500 non-null, start searching with that row. IT gives the dimensions
16501 of the display. Value is null if matrix is empty; otherwise it is
16502 a pointer to the row found. */
16504 static struct glyph_row *
16505 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16506 struct glyph_row *start)
16508 struct glyph_row *row, *row_found;
16510 /* Set row_found to the last row in IT->w's current matrix
16511 displaying text. The loop looks funny but think of partially
16512 visible lines. */
16513 row_found = NULL;
16514 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16515 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16517 xassert (row->enabled_p);
16518 row_found = row;
16519 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16520 break;
16521 ++row;
16524 return row_found;
16528 /* Return the last row in the current matrix of W that is not affected
16529 by changes at the start of current_buffer that occurred since W's
16530 current matrix was built. Value is null if no such row exists.
16532 BEG_UNCHANGED us the number of characters unchanged at the start of
16533 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16534 first changed character in current_buffer. Characters at positions <
16535 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16536 when the current matrix was built. */
16538 static struct glyph_row *
16539 find_last_unchanged_at_beg_row (struct window *w)
16541 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
16542 struct glyph_row *row;
16543 struct glyph_row *row_found = NULL;
16544 int yb = window_text_bottom_y (w);
16546 /* Find the last row displaying unchanged text. */
16547 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16548 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16549 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16550 ++row)
16552 if (/* If row ends before first_changed_pos, it is unchanged,
16553 except in some case. */
16554 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16555 /* When row ends in ZV and we write at ZV it is not
16556 unchanged. */
16557 && !row->ends_at_zv_p
16558 /* When first_changed_pos is the end of a continued line,
16559 row is not unchanged because it may be no longer
16560 continued. */
16561 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16562 && (row->continued_p
16563 || row->exact_window_width_line_p)))
16564 row_found = row;
16566 /* Stop if last visible row. */
16567 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16568 break;
16571 return row_found;
16575 /* Find the first glyph row in the current matrix of W that is not
16576 affected by changes at the end of current_buffer since the
16577 time W's current matrix was built.
16579 Return in *DELTA the number of chars by which buffer positions in
16580 unchanged text at the end of current_buffer must be adjusted.
16582 Return in *DELTA_BYTES the corresponding number of bytes.
16584 Value is null if no such row exists, i.e. all rows are affected by
16585 changes. */
16587 static struct glyph_row *
16588 find_first_unchanged_at_end_row (struct window *w,
16589 EMACS_INT *delta, EMACS_INT *delta_bytes)
16591 struct glyph_row *row;
16592 struct glyph_row *row_found = NULL;
16594 *delta = *delta_bytes = 0;
16596 /* Display must not have been paused, otherwise the current matrix
16597 is not up to date. */
16598 eassert (!NILP (w->window_end_valid));
16600 /* A value of window_end_pos >= END_UNCHANGED means that the window
16601 end is in the range of changed text. If so, there is no
16602 unchanged row at the end of W's current matrix. */
16603 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16604 return NULL;
16606 /* Set row to the last row in W's current matrix displaying text. */
16607 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16609 /* If matrix is entirely empty, no unchanged row exists. */
16610 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16612 /* The value of row is the last glyph row in the matrix having a
16613 meaningful buffer position in it. The end position of row
16614 corresponds to window_end_pos. This allows us to translate
16615 buffer positions in the current matrix to current buffer
16616 positions for characters not in changed text. */
16617 EMACS_INT Z_old =
16618 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16619 EMACS_INT Z_BYTE_old =
16620 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16621 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
16622 struct glyph_row *first_text_row
16623 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16625 *delta = Z - Z_old;
16626 *delta_bytes = Z_BYTE - Z_BYTE_old;
16628 /* Set last_unchanged_pos to the buffer position of the last
16629 character in the buffer that has not been changed. Z is the
16630 index + 1 of the last character in current_buffer, i.e. by
16631 subtracting END_UNCHANGED we get the index of the last
16632 unchanged character, and we have to add BEG to get its buffer
16633 position. */
16634 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16635 last_unchanged_pos_old = last_unchanged_pos - *delta;
16637 /* Search backward from ROW for a row displaying a line that
16638 starts at a minimum position >= last_unchanged_pos_old. */
16639 for (; row > first_text_row; --row)
16641 /* This used to abort, but it can happen.
16642 It is ok to just stop the search instead here. KFS. */
16643 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16644 break;
16646 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16647 row_found = row;
16651 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16653 return row_found;
16657 /* Make sure that glyph rows in the current matrix of window W
16658 reference the same glyph memory as corresponding rows in the
16659 frame's frame matrix. This function is called after scrolling W's
16660 current matrix on a terminal frame in try_window_id and
16661 try_window_reusing_current_matrix. */
16663 static void
16664 sync_frame_with_window_matrix_rows (struct window *w)
16666 struct frame *f = XFRAME (w->frame);
16667 struct glyph_row *window_row, *window_row_end, *frame_row;
16669 /* Preconditions: W must be a leaf window and full-width. Its frame
16670 must have a frame matrix. */
16671 xassert (NILP (w->hchild) && NILP (w->vchild));
16672 xassert (WINDOW_FULL_WIDTH_P (w));
16673 xassert (!FRAME_WINDOW_P (f));
16675 /* If W is a full-width window, glyph pointers in W's current matrix
16676 have, by definition, to be the same as glyph pointers in the
16677 corresponding frame matrix. Note that frame matrices have no
16678 marginal areas (see build_frame_matrix). */
16679 window_row = w->current_matrix->rows;
16680 window_row_end = window_row + w->current_matrix->nrows;
16681 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16682 while (window_row < window_row_end)
16684 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16685 struct glyph *end = window_row->glyphs[LAST_AREA];
16687 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16688 frame_row->glyphs[TEXT_AREA] = start;
16689 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16690 frame_row->glyphs[LAST_AREA] = end;
16692 /* Disable frame rows whose corresponding window rows have
16693 been disabled in try_window_id. */
16694 if (!window_row->enabled_p)
16695 frame_row->enabled_p = 0;
16697 ++window_row, ++frame_row;
16702 /* Find the glyph row in window W containing CHARPOS. Consider all
16703 rows between START and END (not inclusive). END null means search
16704 all rows to the end of the display area of W. Value is the row
16705 containing CHARPOS or null. */
16707 struct glyph_row *
16708 row_containing_pos (struct window *w, EMACS_INT charpos,
16709 struct glyph_row *start, struct glyph_row *end, int dy)
16711 struct glyph_row *row = start;
16712 struct glyph_row *best_row = NULL;
16713 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16714 int last_y;
16716 /* If we happen to start on a header-line, skip that. */
16717 if (row->mode_line_p)
16718 ++row;
16720 if ((end && row >= end) || !row->enabled_p)
16721 return NULL;
16723 last_y = window_text_bottom_y (w) - dy;
16725 while (1)
16727 /* Give up if we have gone too far. */
16728 if (end && row >= end)
16729 return NULL;
16730 /* This formerly returned if they were equal.
16731 I think that both quantities are of a "last plus one" type;
16732 if so, when they are equal, the row is within the screen. -- rms. */
16733 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16734 return NULL;
16736 /* If it is in this row, return this row. */
16737 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16738 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16739 /* The end position of a row equals the start
16740 position of the next row. If CHARPOS is there, we
16741 would rather display it in the next line, except
16742 when this line ends in ZV. */
16743 && !row->ends_at_zv_p
16744 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16745 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16747 struct glyph *g;
16749 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16750 || (!best_row && !row->continued_p))
16751 return row;
16752 /* In bidi-reordered rows, there could be several rows
16753 occluding point, all of them belonging to the same
16754 continued line. We need to find the row which fits
16755 CHARPOS the best. */
16756 for (g = row->glyphs[TEXT_AREA];
16757 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16758 g++)
16760 if (!STRINGP (g->object))
16762 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16764 mindif = eabs (g->charpos - charpos);
16765 best_row = row;
16766 /* Exact match always wins. */
16767 if (mindif == 0)
16768 return best_row;
16773 else if (best_row && !row->continued_p)
16774 return best_row;
16775 ++row;
16780 /* Try to redisplay window W by reusing its existing display. W's
16781 current matrix must be up to date when this function is called,
16782 i.e. window_end_valid must not be nil.
16784 Value is
16786 1 if display has been updated
16787 0 if otherwise unsuccessful
16788 -1 if redisplay with same window start is known not to succeed
16790 The following steps are performed:
16792 1. Find the last row in the current matrix of W that is not
16793 affected by changes at the start of current_buffer. If no such row
16794 is found, give up.
16796 2. Find the first row in W's current matrix that is not affected by
16797 changes at the end of current_buffer. Maybe there is no such row.
16799 3. Display lines beginning with the row + 1 found in step 1 to the
16800 row found in step 2 or, if step 2 didn't find a row, to the end of
16801 the window.
16803 4. If cursor is not known to appear on the window, give up.
16805 5. If display stopped at the row found in step 2, scroll the
16806 display and current matrix as needed.
16808 6. Maybe display some lines at the end of W, if we must. This can
16809 happen under various circumstances, like a partially visible line
16810 becoming fully visible, or because newly displayed lines are displayed
16811 in smaller font sizes.
16813 7. Update W's window end information. */
16815 static int
16816 try_window_id (struct window *w)
16818 struct frame *f = XFRAME (w->frame);
16819 struct glyph_matrix *current_matrix = w->current_matrix;
16820 struct glyph_matrix *desired_matrix = w->desired_matrix;
16821 struct glyph_row *last_unchanged_at_beg_row;
16822 struct glyph_row *first_unchanged_at_end_row;
16823 struct glyph_row *row;
16824 struct glyph_row *bottom_row;
16825 int bottom_vpos;
16826 struct it it;
16827 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
16828 int dvpos, dy;
16829 struct text_pos start_pos;
16830 struct run run;
16831 int first_unchanged_at_end_vpos = 0;
16832 struct glyph_row *last_text_row, *last_text_row_at_end;
16833 struct text_pos start;
16834 EMACS_INT first_changed_charpos, last_changed_charpos;
16836 #if GLYPH_DEBUG
16837 if (inhibit_try_window_id)
16838 return 0;
16839 #endif
16841 /* This is handy for debugging. */
16842 #if 0
16843 #define GIVE_UP(X) \
16844 do { \
16845 fprintf (stderr, "try_window_id give up %d\n", (X)); \
16846 return 0; \
16847 } while (0)
16848 #else
16849 #define GIVE_UP(X) return 0
16850 #endif
16852 SET_TEXT_POS_FROM_MARKER (start, w->start);
16854 /* Don't use this for mini-windows because these can show
16855 messages and mini-buffers, and we don't handle that here. */
16856 if (MINI_WINDOW_P (w))
16857 GIVE_UP (1);
16859 /* This flag is used to prevent redisplay optimizations. */
16860 if (windows_or_buffers_changed || cursor_type_changed)
16861 GIVE_UP (2);
16863 /* Verify that narrowing has not changed.
16864 Also verify that we were not told to prevent redisplay optimizations.
16865 It would be nice to further
16866 reduce the number of cases where this prevents try_window_id. */
16867 if (current_buffer->clip_changed
16868 || current_buffer->prevent_redisplay_optimizations_p)
16869 GIVE_UP (3);
16871 /* Window must either use window-based redisplay or be full width. */
16872 if (!FRAME_WINDOW_P (f)
16873 && (!FRAME_LINE_INS_DEL_OK (f)
16874 || !WINDOW_FULL_WIDTH_P (w)))
16875 GIVE_UP (4);
16877 /* Give up if point is known NOT to appear in W. */
16878 if (PT < CHARPOS (start))
16879 GIVE_UP (5);
16881 /* Another way to prevent redisplay optimizations. */
16882 if (XFASTINT (w->last_modified) == 0)
16883 GIVE_UP (6);
16885 /* Verify that window is not hscrolled. */
16886 if (XFASTINT (w->hscroll) != 0)
16887 GIVE_UP (7);
16889 /* Verify that display wasn't paused. */
16890 if (NILP (w->window_end_valid))
16891 GIVE_UP (8);
16893 /* Can't use this if highlighting a region because a cursor movement
16894 will do more than just set the cursor. */
16895 if (!NILP (Vtransient_mark_mode)
16896 && !NILP (BVAR (current_buffer, mark_active)))
16897 GIVE_UP (9);
16899 /* Likewise if highlighting trailing whitespace. */
16900 if (!NILP (Vshow_trailing_whitespace))
16901 GIVE_UP (11);
16903 /* Likewise if showing a region. */
16904 if (!NILP (w->region_showing))
16905 GIVE_UP (10);
16907 /* Can't use this if overlay arrow position and/or string have
16908 changed. */
16909 if (overlay_arrows_changed_p ())
16910 GIVE_UP (12);
16912 /* When word-wrap is on, adding a space to the first word of a
16913 wrapped line can change the wrap position, altering the line
16914 above it. It might be worthwhile to handle this more
16915 intelligently, but for now just redisplay from scratch. */
16916 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
16917 GIVE_UP (21);
16919 /* Under bidi reordering, adding or deleting a character in the
16920 beginning of a paragraph, before the first strong directional
16921 character, can change the base direction of the paragraph (unless
16922 the buffer specifies a fixed paragraph direction), which will
16923 require to redisplay the whole paragraph. It might be worthwhile
16924 to find the paragraph limits and widen the range of redisplayed
16925 lines to that, but for now just give up this optimization and
16926 redisplay from scratch. */
16927 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16928 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
16929 GIVE_UP (22);
16931 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
16932 only if buffer has really changed. The reason is that the gap is
16933 initially at Z for freshly visited files. The code below would
16934 set end_unchanged to 0 in that case. */
16935 if (MODIFF > SAVE_MODIFF
16936 /* This seems to happen sometimes after saving a buffer. */
16937 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
16939 if (GPT - BEG < BEG_UNCHANGED)
16940 BEG_UNCHANGED = GPT - BEG;
16941 if (Z - GPT < END_UNCHANGED)
16942 END_UNCHANGED = Z - GPT;
16945 /* The position of the first and last character that has been changed. */
16946 first_changed_charpos = BEG + BEG_UNCHANGED;
16947 last_changed_charpos = Z - END_UNCHANGED;
16949 /* If window starts after a line end, and the last change is in
16950 front of that newline, then changes don't affect the display.
16951 This case happens with stealth-fontification. Note that although
16952 the display is unchanged, glyph positions in the matrix have to
16953 be adjusted, of course. */
16954 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16955 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
16956 && ((last_changed_charpos < CHARPOS (start)
16957 && CHARPOS (start) == BEGV)
16958 || (last_changed_charpos < CHARPOS (start) - 1
16959 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
16961 EMACS_INT Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
16962 struct glyph_row *r0;
16964 /* Compute how many chars/bytes have been added to or removed
16965 from the buffer. */
16966 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16967 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16968 Z_delta = Z - Z_old;
16969 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
16971 /* Give up if PT is not in the window. Note that it already has
16972 been checked at the start of try_window_id that PT is not in
16973 front of the window start. */
16974 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
16975 GIVE_UP (13);
16977 /* If window start is unchanged, we can reuse the whole matrix
16978 as is, after adjusting glyph positions. No need to compute
16979 the window end again, since its offset from Z hasn't changed. */
16980 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16981 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
16982 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
16983 /* PT must not be in a partially visible line. */
16984 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
16985 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16987 /* Adjust positions in the glyph matrix. */
16988 if (Z_delta || Z_delta_bytes)
16990 struct glyph_row *r1
16991 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16992 increment_matrix_positions (w->current_matrix,
16993 MATRIX_ROW_VPOS (r0, current_matrix),
16994 MATRIX_ROW_VPOS (r1, current_matrix),
16995 Z_delta, Z_delta_bytes);
16998 /* Set the cursor. */
16999 row = row_containing_pos (w, PT, r0, NULL, 0);
17000 if (row)
17001 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17002 else
17003 abort ();
17004 return 1;
17008 /* Handle the case that changes are all below what is displayed in
17009 the window, and that PT is in the window. This shortcut cannot
17010 be taken if ZV is visible in the window, and text has been added
17011 there that is visible in the window. */
17012 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17013 /* ZV is not visible in the window, or there are no
17014 changes at ZV, actually. */
17015 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17016 || first_changed_charpos == last_changed_charpos))
17018 struct glyph_row *r0;
17020 /* Give up if PT is not in the window. Note that it already has
17021 been checked at the start of try_window_id that PT is not in
17022 front of the window start. */
17023 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17024 GIVE_UP (14);
17026 /* If window start is unchanged, we can reuse the whole matrix
17027 as is, without changing glyph positions since no text has
17028 been added/removed in front of the window end. */
17029 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17030 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17031 /* PT must not be in a partially visible line. */
17032 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17033 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17035 /* We have to compute the window end anew since text
17036 could have been added/removed after it. */
17037 w->window_end_pos
17038 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17039 w->window_end_bytepos
17040 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17042 /* Set the cursor. */
17043 row = row_containing_pos (w, PT, r0, NULL, 0);
17044 if (row)
17045 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17046 else
17047 abort ();
17048 return 2;
17052 /* Give up if window start is in the changed area.
17054 The condition used to read
17056 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17058 but why that was tested escapes me at the moment. */
17059 if (CHARPOS (start) >= first_changed_charpos
17060 && CHARPOS (start) <= last_changed_charpos)
17061 GIVE_UP (15);
17063 /* Check that window start agrees with the start of the first glyph
17064 row in its current matrix. Check this after we know the window
17065 start is not in changed text, otherwise positions would not be
17066 comparable. */
17067 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17068 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17069 GIVE_UP (16);
17071 /* Give up if the window ends in strings. Overlay strings
17072 at the end are difficult to handle, so don't try. */
17073 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17074 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17075 GIVE_UP (20);
17077 /* Compute the position at which we have to start displaying new
17078 lines. Some of the lines at the top of the window might be
17079 reusable because they are not displaying changed text. Find the
17080 last row in W's current matrix not affected by changes at the
17081 start of current_buffer. Value is null if changes start in the
17082 first line of window. */
17083 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17084 if (last_unchanged_at_beg_row)
17086 /* Avoid starting to display in the middle of a character, a TAB
17087 for instance. This is easier than to set up the iterator
17088 exactly, and it's not a frequent case, so the additional
17089 effort wouldn't really pay off. */
17090 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17091 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17092 && last_unchanged_at_beg_row > w->current_matrix->rows)
17093 --last_unchanged_at_beg_row;
17095 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17096 GIVE_UP (17);
17098 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17099 GIVE_UP (18);
17100 start_pos = it.current.pos;
17102 /* Start displaying new lines in the desired matrix at the same
17103 vpos we would use in the current matrix, i.e. below
17104 last_unchanged_at_beg_row. */
17105 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17106 current_matrix);
17107 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17108 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17110 xassert (it.hpos == 0 && it.current_x == 0);
17112 else
17114 /* There are no reusable lines at the start of the window.
17115 Start displaying in the first text line. */
17116 start_display (&it, w, start);
17117 it.vpos = it.first_vpos;
17118 start_pos = it.current.pos;
17121 /* Find the first row that is not affected by changes at the end of
17122 the buffer. Value will be null if there is no unchanged row, in
17123 which case we must redisplay to the end of the window. delta
17124 will be set to the value by which buffer positions beginning with
17125 first_unchanged_at_end_row have to be adjusted due to text
17126 changes. */
17127 first_unchanged_at_end_row
17128 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17129 IF_DEBUG (debug_delta = delta);
17130 IF_DEBUG (debug_delta_bytes = delta_bytes);
17132 /* Set stop_pos to the buffer position up to which we will have to
17133 display new lines. If first_unchanged_at_end_row != NULL, this
17134 is the buffer position of the start of the line displayed in that
17135 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17136 that we don't stop at a buffer position. */
17137 stop_pos = 0;
17138 if (first_unchanged_at_end_row)
17140 xassert (last_unchanged_at_beg_row == NULL
17141 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17143 /* If this is a continuation line, move forward to the next one
17144 that isn't. Changes in lines above affect this line.
17145 Caution: this may move first_unchanged_at_end_row to a row
17146 not displaying text. */
17147 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17148 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17149 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17150 < it.last_visible_y))
17151 ++first_unchanged_at_end_row;
17153 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17154 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17155 >= it.last_visible_y))
17156 first_unchanged_at_end_row = NULL;
17157 else
17159 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17160 + delta);
17161 first_unchanged_at_end_vpos
17162 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17163 xassert (stop_pos >= Z - END_UNCHANGED);
17166 else if (last_unchanged_at_beg_row == NULL)
17167 GIVE_UP (19);
17170 #if GLYPH_DEBUG
17172 /* Either there is no unchanged row at the end, or the one we have
17173 now displays text. This is a necessary condition for the window
17174 end pos calculation at the end of this function. */
17175 xassert (first_unchanged_at_end_row == NULL
17176 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17178 debug_last_unchanged_at_beg_vpos
17179 = (last_unchanged_at_beg_row
17180 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17181 : -1);
17182 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17184 #endif /* GLYPH_DEBUG != 0 */
17187 /* Display new lines. Set last_text_row to the last new line
17188 displayed which has text on it, i.e. might end up as being the
17189 line where the window_end_vpos is. */
17190 w->cursor.vpos = -1;
17191 last_text_row = NULL;
17192 overlay_arrow_seen = 0;
17193 while (it.current_y < it.last_visible_y
17194 && !fonts_changed_p
17195 && (first_unchanged_at_end_row == NULL
17196 || IT_CHARPOS (it) < stop_pos))
17198 if (display_line (&it))
17199 last_text_row = it.glyph_row - 1;
17202 if (fonts_changed_p)
17203 return -1;
17206 /* Compute differences in buffer positions, y-positions etc. for
17207 lines reused at the bottom of the window. Compute what we can
17208 scroll. */
17209 if (first_unchanged_at_end_row
17210 /* No lines reused because we displayed everything up to the
17211 bottom of the window. */
17212 && it.current_y < it.last_visible_y)
17214 dvpos = (it.vpos
17215 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17216 current_matrix));
17217 dy = it.current_y - first_unchanged_at_end_row->y;
17218 run.current_y = first_unchanged_at_end_row->y;
17219 run.desired_y = run.current_y + dy;
17220 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17222 else
17224 delta = delta_bytes = dvpos = dy
17225 = run.current_y = run.desired_y = run.height = 0;
17226 first_unchanged_at_end_row = NULL;
17228 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17231 /* Find the cursor if not already found. We have to decide whether
17232 PT will appear on this window (it sometimes doesn't, but this is
17233 not a very frequent case.) This decision has to be made before
17234 the current matrix is altered. A value of cursor.vpos < 0 means
17235 that PT is either in one of the lines beginning at
17236 first_unchanged_at_end_row or below the window. Don't care for
17237 lines that might be displayed later at the window end; as
17238 mentioned, this is not a frequent case. */
17239 if (w->cursor.vpos < 0)
17241 /* Cursor in unchanged rows at the top? */
17242 if (PT < CHARPOS (start_pos)
17243 && last_unchanged_at_beg_row)
17245 row = row_containing_pos (w, PT,
17246 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17247 last_unchanged_at_beg_row + 1, 0);
17248 if (row)
17249 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17252 /* Start from first_unchanged_at_end_row looking for PT. */
17253 else if (first_unchanged_at_end_row)
17255 row = row_containing_pos (w, PT - delta,
17256 first_unchanged_at_end_row, NULL, 0);
17257 if (row)
17258 set_cursor_from_row (w, row, w->current_matrix, delta,
17259 delta_bytes, dy, dvpos);
17262 /* Give up if cursor was not found. */
17263 if (w->cursor.vpos < 0)
17265 clear_glyph_matrix (w->desired_matrix);
17266 return -1;
17270 /* Don't let the cursor end in the scroll margins. */
17272 int this_scroll_margin, cursor_height;
17274 this_scroll_margin =
17275 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17276 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17277 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17279 if ((w->cursor.y < this_scroll_margin
17280 && CHARPOS (start) > BEGV)
17281 /* Old redisplay didn't take scroll margin into account at the bottom,
17282 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17283 || (w->cursor.y + (make_cursor_line_fully_visible_p
17284 ? cursor_height + this_scroll_margin
17285 : 1)) > it.last_visible_y)
17287 w->cursor.vpos = -1;
17288 clear_glyph_matrix (w->desired_matrix);
17289 return -1;
17293 /* Scroll the display. Do it before changing the current matrix so
17294 that xterm.c doesn't get confused about where the cursor glyph is
17295 found. */
17296 if (dy && run.height)
17298 update_begin (f);
17300 if (FRAME_WINDOW_P (f))
17302 FRAME_RIF (f)->update_window_begin_hook (w);
17303 FRAME_RIF (f)->clear_window_mouse_face (w);
17304 FRAME_RIF (f)->scroll_run_hook (w, &run);
17305 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17307 else
17309 /* Terminal frame. In this case, dvpos gives the number of
17310 lines to scroll by; dvpos < 0 means scroll up. */
17311 int from_vpos
17312 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17313 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17314 int end = (WINDOW_TOP_EDGE_LINE (w)
17315 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17316 + window_internal_height (w));
17318 #if defined (HAVE_GPM) || defined (MSDOS)
17319 x_clear_window_mouse_face (w);
17320 #endif
17321 /* Perform the operation on the screen. */
17322 if (dvpos > 0)
17324 /* Scroll last_unchanged_at_beg_row to the end of the
17325 window down dvpos lines. */
17326 set_terminal_window (f, end);
17328 /* On dumb terminals delete dvpos lines at the end
17329 before inserting dvpos empty lines. */
17330 if (!FRAME_SCROLL_REGION_OK (f))
17331 ins_del_lines (f, end - dvpos, -dvpos);
17333 /* Insert dvpos empty lines in front of
17334 last_unchanged_at_beg_row. */
17335 ins_del_lines (f, from, dvpos);
17337 else if (dvpos < 0)
17339 /* Scroll up last_unchanged_at_beg_vpos to the end of
17340 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17341 set_terminal_window (f, end);
17343 /* Delete dvpos lines in front of
17344 last_unchanged_at_beg_vpos. ins_del_lines will set
17345 the cursor to the given vpos and emit |dvpos| delete
17346 line sequences. */
17347 ins_del_lines (f, from + dvpos, dvpos);
17349 /* On a dumb terminal insert dvpos empty lines at the
17350 end. */
17351 if (!FRAME_SCROLL_REGION_OK (f))
17352 ins_del_lines (f, end + dvpos, -dvpos);
17355 set_terminal_window (f, 0);
17358 update_end (f);
17361 /* Shift reused rows of the current matrix to the right position.
17362 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17363 text. */
17364 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17365 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17366 if (dvpos < 0)
17368 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17369 bottom_vpos, dvpos);
17370 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17371 bottom_vpos, 0);
17373 else if (dvpos > 0)
17375 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17376 bottom_vpos, dvpos);
17377 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17378 first_unchanged_at_end_vpos + dvpos, 0);
17381 /* For frame-based redisplay, make sure that current frame and window
17382 matrix are in sync with respect to glyph memory. */
17383 if (!FRAME_WINDOW_P (f))
17384 sync_frame_with_window_matrix_rows (w);
17386 /* Adjust buffer positions in reused rows. */
17387 if (delta || delta_bytes)
17388 increment_matrix_positions (current_matrix,
17389 first_unchanged_at_end_vpos + dvpos,
17390 bottom_vpos, delta, delta_bytes);
17392 /* Adjust Y positions. */
17393 if (dy)
17394 shift_glyph_matrix (w, current_matrix,
17395 first_unchanged_at_end_vpos + dvpos,
17396 bottom_vpos, dy);
17398 if (first_unchanged_at_end_row)
17400 first_unchanged_at_end_row += dvpos;
17401 if (first_unchanged_at_end_row->y >= it.last_visible_y
17402 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17403 first_unchanged_at_end_row = NULL;
17406 /* If scrolling up, there may be some lines to display at the end of
17407 the window. */
17408 last_text_row_at_end = NULL;
17409 if (dy < 0)
17411 /* Scrolling up can leave for example a partially visible line
17412 at the end of the window to be redisplayed. */
17413 /* Set last_row to the glyph row in the current matrix where the
17414 window end line is found. It has been moved up or down in
17415 the matrix by dvpos. */
17416 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17417 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17419 /* If last_row is the window end line, it should display text. */
17420 xassert (last_row->displays_text_p);
17422 /* If window end line was partially visible before, begin
17423 displaying at that line. Otherwise begin displaying with the
17424 line following it. */
17425 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17427 init_to_row_start (&it, w, last_row);
17428 it.vpos = last_vpos;
17429 it.current_y = last_row->y;
17431 else
17433 init_to_row_end (&it, w, last_row);
17434 it.vpos = 1 + last_vpos;
17435 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17436 ++last_row;
17439 /* We may start in a continuation line. If so, we have to
17440 get the right continuation_lines_width and current_x. */
17441 it.continuation_lines_width = last_row->continuation_lines_width;
17442 it.hpos = it.current_x = 0;
17444 /* Display the rest of the lines at the window end. */
17445 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17446 while (it.current_y < it.last_visible_y
17447 && !fonts_changed_p)
17449 /* Is it always sure that the display agrees with lines in
17450 the current matrix? I don't think so, so we mark rows
17451 displayed invalid in the current matrix by setting their
17452 enabled_p flag to zero. */
17453 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17454 if (display_line (&it))
17455 last_text_row_at_end = it.glyph_row - 1;
17459 /* Update window_end_pos and window_end_vpos. */
17460 if (first_unchanged_at_end_row
17461 && !last_text_row_at_end)
17463 /* Window end line if one of the preserved rows from the current
17464 matrix. Set row to the last row displaying text in current
17465 matrix starting at first_unchanged_at_end_row, after
17466 scrolling. */
17467 xassert (first_unchanged_at_end_row->displays_text_p);
17468 row = find_last_row_displaying_text (w->current_matrix, &it,
17469 first_unchanged_at_end_row);
17470 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17472 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17473 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17474 w->window_end_vpos
17475 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17476 xassert (w->window_end_bytepos >= 0);
17477 IF_DEBUG (debug_method_add (w, "A"));
17479 else if (last_text_row_at_end)
17481 w->window_end_pos
17482 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17483 w->window_end_bytepos
17484 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17485 w->window_end_vpos
17486 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17487 xassert (w->window_end_bytepos >= 0);
17488 IF_DEBUG (debug_method_add (w, "B"));
17490 else if (last_text_row)
17492 /* We have displayed either to the end of the window or at the
17493 end of the window, i.e. the last row with text is to be found
17494 in the desired matrix. */
17495 w->window_end_pos
17496 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17497 w->window_end_bytepos
17498 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17499 w->window_end_vpos
17500 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17501 xassert (w->window_end_bytepos >= 0);
17503 else if (first_unchanged_at_end_row == NULL
17504 && last_text_row == NULL
17505 && last_text_row_at_end == NULL)
17507 /* Displayed to end of window, but no line containing text was
17508 displayed. Lines were deleted at the end of the window. */
17509 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17510 int vpos = XFASTINT (w->window_end_vpos);
17511 struct glyph_row *current_row = current_matrix->rows + vpos;
17512 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17514 for (row = NULL;
17515 row == NULL && vpos >= first_vpos;
17516 --vpos, --current_row, --desired_row)
17518 if (desired_row->enabled_p)
17520 if (desired_row->displays_text_p)
17521 row = desired_row;
17523 else if (current_row->displays_text_p)
17524 row = current_row;
17527 xassert (row != NULL);
17528 w->window_end_vpos = make_number (vpos + 1);
17529 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17530 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17531 xassert (w->window_end_bytepos >= 0);
17532 IF_DEBUG (debug_method_add (w, "C"));
17534 else
17535 abort ();
17537 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17538 debug_end_vpos = XFASTINT (w->window_end_vpos));
17540 /* Record that display has not been completed. */
17541 w->window_end_valid = Qnil;
17542 w->desired_matrix->no_scrolling_p = 1;
17543 return 3;
17545 #undef GIVE_UP
17550 /***********************************************************************
17551 More debugging support
17552 ***********************************************************************/
17554 #if GLYPH_DEBUG
17556 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17557 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17558 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17561 /* Dump the contents of glyph matrix MATRIX on stderr.
17563 GLYPHS 0 means don't show glyph contents.
17564 GLYPHS 1 means show glyphs in short form
17565 GLYPHS > 1 means show glyphs in long form. */
17567 void
17568 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17570 int i;
17571 for (i = 0; i < matrix->nrows; ++i)
17572 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17576 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17577 the glyph row and area where the glyph comes from. */
17579 void
17580 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17582 if (glyph->type == CHAR_GLYPH)
17584 fprintf (stderr,
17585 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17586 glyph - row->glyphs[TEXT_AREA],
17587 'C',
17588 glyph->charpos,
17589 (BUFFERP (glyph->object)
17590 ? 'B'
17591 : (STRINGP (glyph->object)
17592 ? 'S'
17593 : '-')),
17594 glyph->pixel_width,
17595 glyph->u.ch,
17596 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17597 ? glyph->u.ch
17598 : '.'),
17599 glyph->face_id,
17600 glyph->left_box_line_p,
17601 glyph->right_box_line_p);
17603 else if (glyph->type == STRETCH_GLYPH)
17605 fprintf (stderr,
17606 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17607 glyph - row->glyphs[TEXT_AREA],
17608 'S',
17609 glyph->charpos,
17610 (BUFFERP (glyph->object)
17611 ? 'B'
17612 : (STRINGP (glyph->object)
17613 ? 'S'
17614 : '-')),
17615 glyph->pixel_width,
17617 '.',
17618 glyph->face_id,
17619 glyph->left_box_line_p,
17620 glyph->right_box_line_p);
17622 else if (glyph->type == IMAGE_GLYPH)
17624 fprintf (stderr,
17625 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17626 glyph - row->glyphs[TEXT_AREA],
17627 'I',
17628 glyph->charpos,
17629 (BUFFERP (glyph->object)
17630 ? 'B'
17631 : (STRINGP (glyph->object)
17632 ? 'S'
17633 : '-')),
17634 glyph->pixel_width,
17635 glyph->u.img_id,
17636 '.',
17637 glyph->face_id,
17638 glyph->left_box_line_p,
17639 glyph->right_box_line_p);
17641 else if (glyph->type == COMPOSITE_GLYPH)
17643 fprintf (stderr,
17644 " %5td %4c %6"pI"d %c %3d 0x%05x",
17645 glyph - row->glyphs[TEXT_AREA],
17646 '+',
17647 glyph->charpos,
17648 (BUFFERP (glyph->object)
17649 ? 'B'
17650 : (STRINGP (glyph->object)
17651 ? 'S'
17652 : '-')),
17653 glyph->pixel_width,
17654 glyph->u.cmp.id);
17655 if (glyph->u.cmp.automatic)
17656 fprintf (stderr,
17657 "[%d-%d]",
17658 glyph->slice.cmp.from, glyph->slice.cmp.to);
17659 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17660 glyph->face_id,
17661 glyph->left_box_line_p,
17662 glyph->right_box_line_p);
17667 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17668 GLYPHS 0 means don't show glyph contents.
17669 GLYPHS 1 means show glyphs in short form
17670 GLYPHS > 1 means show glyphs in long form. */
17672 void
17673 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17675 if (glyphs != 1)
17677 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17678 fprintf (stderr, "======================================================================\n");
17680 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17681 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17682 vpos,
17683 MATRIX_ROW_START_CHARPOS (row),
17684 MATRIX_ROW_END_CHARPOS (row),
17685 row->used[TEXT_AREA],
17686 row->contains_overlapping_glyphs_p,
17687 row->enabled_p,
17688 row->truncated_on_left_p,
17689 row->truncated_on_right_p,
17690 row->continued_p,
17691 MATRIX_ROW_CONTINUATION_LINE_P (row),
17692 row->displays_text_p,
17693 row->ends_at_zv_p,
17694 row->fill_line_p,
17695 row->ends_in_middle_of_char_p,
17696 row->starts_in_middle_of_char_p,
17697 row->mouse_face_p,
17698 row->x,
17699 row->y,
17700 row->pixel_width,
17701 row->height,
17702 row->visible_height,
17703 row->ascent,
17704 row->phys_ascent);
17705 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
17706 row->end.overlay_string_index,
17707 row->continuation_lines_width);
17708 fprintf (stderr, "%9"pI"d %5"pI"d\n",
17709 CHARPOS (row->start.string_pos),
17710 CHARPOS (row->end.string_pos));
17711 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17712 row->end.dpvec_index);
17715 if (glyphs > 1)
17717 int area;
17719 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17721 struct glyph *glyph = row->glyphs[area];
17722 struct glyph *glyph_end = glyph + row->used[area];
17724 /* Glyph for a line end in text. */
17725 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17726 ++glyph_end;
17728 if (glyph < glyph_end)
17729 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
17731 for (; glyph < glyph_end; ++glyph)
17732 dump_glyph (row, glyph, area);
17735 else if (glyphs == 1)
17737 int area;
17739 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17741 char *s = (char *) alloca (row->used[area] + 1);
17742 int i;
17744 for (i = 0; i < row->used[area]; ++i)
17746 struct glyph *glyph = row->glyphs[area] + i;
17747 if (glyph->type == CHAR_GLYPH
17748 && glyph->u.ch < 0x80
17749 && glyph->u.ch >= ' ')
17750 s[i] = glyph->u.ch;
17751 else
17752 s[i] = '.';
17755 s[i] = '\0';
17756 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17762 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17763 Sdump_glyph_matrix, 0, 1, "p",
17764 doc: /* Dump the current matrix of the selected window to stderr.
17765 Shows contents of glyph row structures. With non-nil
17766 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17767 glyphs in short form, otherwise show glyphs in long form. */)
17768 (Lisp_Object glyphs)
17770 struct window *w = XWINDOW (selected_window);
17771 struct buffer *buffer = XBUFFER (w->buffer);
17773 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17774 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17775 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17776 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17777 fprintf (stderr, "=============================================\n");
17778 dump_glyph_matrix (w->current_matrix,
17779 NILP (glyphs) ? 0 : XINT (glyphs));
17780 return Qnil;
17784 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17785 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17786 (void)
17788 struct frame *f = XFRAME (selected_frame);
17789 dump_glyph_matrix (f->current_matrix, 1);
17790 return Qnil;
17794 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17795 doc: /* Dump glyph row ROW to stderr.
17796 GLYPH 0 means don't dump glyphs.
17797 GLYPH 1 means dump glyphs in short form.
17798 GLYPH > 1 or omitted means dump glyphs in long form. */)
17799 (Lisp_Object row, Lisp_Object glyphs)
17801 struct glyph_matrix *matrix;
17802 int vpos;
17804 CHECK_NUMBER (row);
17805 matrix = XWINDOW (selected_window)->current_matrix;
17806 vpos = XINT (row);
17807 if (vpos >= 0 && vpos < matrix->nrows)
17808 dump_glyph_row (MATRIX_ROW (matrix, vpos),
17809 vpos,
17810 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17811 return Qnil;
17815 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
17816 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
17817 GLYPH 0 means don't dump glyphs.
17818 GLYPH 1 means dump glyphs in short form.
17819 GLYPH > 1 or omitted means dump glyphs in long form. */)
17820 (Lisp_Object row, Lisp_Object glyphs)
17822 struct frame *sf = SELECTED_FRAME ();
17823 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
17824 int vpos;
17826 CHECK_NUMBER (row);
17827 vpos = XINT (row);
17828 if (vpos >= 0 && vpos < m->nrows)
17829 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
17830 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17831 return Qnil;
17835 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
17836 doc: /* Toggle tracing of redisplay.
17837 With ARG, turn tracing on if and only if ARG is positive. */)
17838 (Lisp_Object arg)
17840 if (NILP (arg))
17841 trace_redisplay_p = !trace_redisplay_p;
17842 else
17844 arg = Fprefix_numeric_value (arg);
17845 trace_redisplay_p = XINT (arg) > 0;
17848 return Qnil;
17852 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
17853 doc: /* Like `format', but print result to stderr.
17854 usage: (trace-to-stderr STRING &rest OBJECTS) */)
17855 (ptrdiff_t nargs, Lisp_Object *args)
17857 Lisp_Object s = Fformat (nargs, args);
17858 fprintf (stderr, "%s", SDATA (s));
17859 return Qnil;
17862 #endif /* GLYPH_DEBUG */
17866 /***********************************************************************
17867 Building Desired Matrix Rows
17868 ***********************************************************************/
17870 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
17871 Used for non-window-redisplay windows, and for windows w/o left fringe. */
17873 static struct glyph_row *
17874 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
17876 struct frame *f = XFRAME (WINDOW_FRAME (w));
17877 struct buffer *buffer = XBUFFER (w->buffer);
17878 struct buffer *old = current_buffer;
17879 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
17880 int arrow_len = SCHARS (overlay_arrow_string);
17881 const unsigned char *arrow_end = arrow_string + arrow_len;
17882 const unsigned char *p;
17883 struct it it;
17884 int multibyte_p;
17885 int n_glyphs_before;
17887 set_buffer_temp (buffer);
17888 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
17889 it.glyph_row->used[TEXT_AREA] = 0;
17890 SET_TEXT_POS (it.position, 0, 0);
17892 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
17893 p = arrow_string;
17894 while (p < arrow_end)
17896 Lisp_Object face, ilisp;
17898 /* Get the next character. */
17899 if (multibyte_p)
17900 it.c = it.char_to_display = string_char_and_length (p, &it.len);
17901 else
17903 it.c = it.char_to_display = *p, it.len = 1;
17904 if (! ASCII_CHAR_P (it.c))
17905 it.char_to_display = BYTE8_TO_CHAR (it.c);
17907 p += it.len;
17909 /* Get its face. */
17910 ilisp = make_number (p - arrow_string);
17911 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
17912 it.face_id = compute_char_face (f, it.char_to_display, face);
17914 /* Compute its width, get its glyphs. */
17915 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
17916 SET_TEXT_POS (it.position, -1, -1);
17917 PRODUCE_GLYPHS (&it);
17919 /* If this character doesn't fit any more in the line, we have
17920 to remove some glyphs. */
17921 if (it.current_x > it.last_visible_x)
17923 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
17924 break;
17928 set_buffer_temp (old);
17929 return it.glyph_row;
17933 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
17934 glyphs are only inserted for terminal frames since we can't really
17935 win with truncation glyphs when partially visible glyphs are
17936 involved. Which glyphs to insert is determined by
17937 produce_special_glyphs. */
17939 static void
17940 insert_left_trunc_glyphs (struct it *it)
17942 struct it truncate_it;
17943 struct glyph *from, *end, *to, *toend;
17945 xassert (!FRAME_WINDOW_P (it->f));
17947 /* Get the truncation glyphs. */
17948 truncate_it = *it;
17949 truncate_it.current_x = 0;
17950 truncate_it.face_id = DEFAULT_FACE_ID;
17951 truncate_it.glyph_row = &scratch_glyph_row;
17952 truncate_it.glyph_row->used[TEXT_AREA] = 0;
17953 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
17954 truncate_it.object = make_number (0);
17955 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
17957 /* Overwrite glyphs from IT with truncation glyphs. */
17958 if (!it->glyph_row->reversed_p)
17960 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17961 end = from + truncate_it.glyph_row->used[TEXT_AREA];
17962 to = it->glyph_row->glyphs[TEXT_AREA];
17963 toend = to + it->glyph_row->used[TEXT_AREA];
17965 while (from < end)
17966 *to++ = *from++;
17968 /* There may be padding glyphs left over. Overwrite them too. */
17969 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
17971 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17972 while (from < end)
17973 *to++ = *from++;
17976 if (to > toend)
17977 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
17979 else
17981 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
17982 that back to front. */
17983 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
17984 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17985 toend = it->glyph_row->glyphs[TEXT_AREA];
17986 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
17988 while (from >= end && to >= toend)
17989 *to-- = *from--;
17990 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
17992 from =
17993 truncate_it.glyph_row->glyphs[TEXT_AREA]
17994 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17995 while (from >= end && to >= toend)
17996 *to-- = *from--;
17998 if (from >= end)
18000 /* Need to free some room before prepending additional
18001 glyphs. */
18002 int move_by = from - end + 1;
18003 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18004 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18006 for ( ; g >= g0; g--)
18007 g[move_by] = *g;
18008 while (from >= end)
18009 *to-- = *from--;
18010 it->glyph_row->used[TEXT_AREA] += move_by;
18015 /* Compute the hash code for ROW. */
18016 unsigned
18017 row_hash (struct glyph_row *row)
18019 int area, k;
18020 unsigned hashval = 0;
18022 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18023 for (k = 0; k < row->used[area]; ++k)
18024 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18025 + row->glyphs[area][k].u.val
18026 + row->glyphs[area][k].face_id
18027 + row->glyphs[area][k].padding_p
18028 + (row->glyphs[area][k].type << 2));
18030 return hashval;
18033 /* Compute the pixel height and width of IT->glyph_row.
18035 Most of the time, ascent and height of a display line will be equal
18036 to the max_ascent and max_height values of the display iterator
18037 structure. This is not the case if
18039 1. We hit ZV without displaying anything. In this case, max_ascent
18040 and max_height will be zero.
18042 2. We have some glyphs that don't contribute to the line height.
18043 (The glyph row flag contributes_to_line_height_p is for future
18044 pixmap extensions).
18046 The first case is easily covered by using default values because in
18047 these cases, the line height does not really matter, except that it
18048 must not be zero. */
18050 static void
18051 compute_line_metrics (struct it *it)
18053 struct glyph_row *row = it->glyph_row;
18055 if (FRAME_WINDOW_P (it->f))
18057 int i, min_y, max_y;
18059 /* The line may consist of one space only, that was added to
18060 place the cursor on it. If so, the row's height hasn't been
18061 computed yet. */
18062 if (row->height == 0)
18064 if (it->max_ascent + it->max_descent == 0)
18065 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18066 row->ascent = it->max_ascent;
18067 row->height = it->max_ascent + it->max_descent;
18068 row->phys_ascent = it->max_phys_ascent;
18069 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18070 row->extra_line_spacing = it->max_extra_line_spacing;
18073 /* Compute the width of this line. */
18074 row->pixel_width = row->x;
18075 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18076 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18078 xassert (row->pixel_width >= 0);
18079 xassert (row->ascent >= 0 && row->height > 0);
18081 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18082 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18084 /* If first line's physical ascent is larger than its logical
18085 ascent, use the physical ascent, and make the row taller.
18086 This makes accented characters fully visible. */
18087 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18088 && row->phys_ascent > row->ascent)
18090 row->height += row->phys_ascent - row->ascent;
18091 row->ascent = row->phys_ascent;
18094 /* Compute how much of the line is visible. */
18095 row->visible_height = row->height;
18097 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18098 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18100 if (row->y < min_y)
18101 row->visible_height -= min_y - row->y;
18102 if (row->y + row->height > max_y)
18103 row->visible_height -= row->y + row->height - max_y;
18105 else
18107 row->pixel_width = row->used[TEXT_AREA];
18108 if (row->continued_p)
18109 row->pixel_width -= it->continuation_pixel_width;
18110 else if (row->truncated_on_right_p)
18111 row->pixel_width -= it->truncation_pixel_width;
18112 row->ascent = row->phys_ascent = 0;
18113 row->height = row->phys_height = row->visible_height = 1;
18114 row->extra_line_spacing = 0;
18117 /* Compute a hash code for this row. */
18118 row->hash = row_hash (row);
18120 it->max_ascent = it->max_descent = 0;
18121 it->max_phys_ascent = it->max_phys_descent = 0;
18125 /* Append one space to the glyph row of iterator IT if doing a
18126 window-based redisplay. The space has the same face as
18127 IT->face_id. Value is non-zero if a space was added.
18129 This function is called to make sure that there is always one glyph
18130 at the end of a glyph row that the cursor can be set on under
18131 window-systems. (If there weren't such a glyph we would not know
18132 how wide and tall a box cursor should be displayed).
18134 At the same time this space let's a nicely handle clearing to the
18135 end of the line if the row ends in italic text. */
18137 static int
18138 append_space_for_newline (struct it *it, int default_face_p)
18140 if (FRAME_WINDOW_P (it->f))
18142 int n = it->glyph_row->used[TEXT_AREA];
18144 if (it->glyph_row->glyphs[TEXT_AREA] + n
18145 < it->glyph_row->glyphs[1 + TEXT_AREA])
18147 /* Save some values that must not be changed.
18148 Must save IT->c and IT->len because otherwise
18149 ITERATOR_AT_END_P wouldn't work anymore after
18150 append_space_for_newline has been called. */
18151 enum display_element_type saved_what = it->what;
18152 int saved_c = it->c, saved_len = it->len;
18153 int saved_char_to_display = it->char_to_display;
18154 int saved_x = it->current_x;
18155 int saved_face_id = it->face_id;
18156 struct text_pos saved_pos;
18157 Lisp_Object saved_object;
18158 struct face *face;
18160 saved_object = it->object;
18161 saved_pos = it->position;
18163 it->what = IT_CHARACTER;
18164 memset (&it->position, 0, sizeof it->position);
18165 it->object = make_number (0);
18166 it->c = it->char_to_display = ' ';
18167 it->len = 1;
18169 if (default_face_p)
18170 it->face_id = DEFAULT_FACE_ID;
18171 else if (it->face_before_selective_p)
18172 it->face_id = it->saved_face_id;
18173 face = FACE_FROM_ID (it->f, it->face_id);
18174 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18176 PRODUCE_GLYPHS (it);
18178 it->override_ascent = -1;
18179 it->constrain_row_ascent_descent_p = 0;
18180 it->current_x = saved_x;
18181 it->object = saved_object;
18182 it->position = saved_pos;
18183 it->what = saved_what;
18184 it->face_id = saved_face_id;
18185 it->len = saved_len;
18186 it->c = saved_c;
18187 it->char_to_display = saved_char_to_display;
18188 return 1;
18192 return 0;
18196 /* Extend the face of the last glyph in the text area of IT->glyph_row
18197 to the end of the display line. Called from display_line. If the
18198 glyph row is empty, add a space glyph to it so that we know the
18199 face to draw. Set the glyph row flag fill_line_p. If the glyph
18200 row is R2L, prepend a stretch glyph to cover the empty space to the
18201 left of the leftmost glyph. */
18203 static void
18204 extend_face_to_end_of_line (struct it *it)
18206 struct face *face;
18207 struct frame *f = it->f;
18209 /* If line is already filled, do nothing. Non window-system frames
18210 get a grace of one more ``pixel'' because their characters are
18211 1-``pixel'' wide, so they hit the equality too early. This grace
18212 is needed only for R2L rows that are not continued, to produce
18213 one extra blank where we could display the cursor. */
18214 if (it->current_x >= it->last_visible_x
18215 + (!FRAME_WINDOW_P (f)
18216 && it->glyph_row->reversed_p
18217 && !it->glyph_row->continued_p))
18218 return;
18220 /* Face extension extends the background and box of IT->face_id
18221 to the end of the line. If the background equals the background
18222 of the frame, we don't have to do anything. */
18223 if (it->face_before_selective_p)
18224 face = FACE_FROM_ID (f, it->saved_face_id);
18225 else
18226 face = FACE_FROM_ID (f, it->face_id);
18228 if (FRAME_WINDOW_P (f)
18229 && it->glyph_row->displays_text_p
18230 && face->box == FACE_NO_BOX
18231 && face->background == FRAME_BACKGROUND_PIXEL (f)
18232 && !face->stipple
18233 && !it->glyph_row->reversed_p)
18234 return;
18236 /* Set the glyph row flag indicating that the face of the last glyph
18237 in the text area has to be drawn to the end of the text area. */
18238 it->glyph_row->fill_line_p = 1;
18240 /* If current character of IT is not ASCII, make sure we have the
18241 ASCII face. This will be automatically undone the next time
18242 get_next_display_element returns a multibyte character. Note
18243 that the character will always be single byte in unibyte
18244 text. */
18245 if (!ASCII_CHAR_P (it->c))
18247 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18250 if (FRAME_WINDOW_P (f))
18252 /* If the row is empty, add a space with the current face of IT,
18253 so that we know which face to draw. */
18254 if (it->glyph_row->used[TEXT_AREA] == 0)
18256 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18257 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
18258 it->glyph_row->used[TEXT_AREA] = 1;
18260 #ifdef HAVE_WINDOW_SYSTEM
18261 if (it->glyph_row->reversed_p)
18263 /* Prepend a stretch glyph to the row, such that the
18264 rightmost glyph will be drawn flushed all the way to the
18265 right margin of the window. The stretch glyph that will
18266 occupy the empty space, if any, to the left of the
18267 glyphs. */
18268 struct font *font = face->font ? face->font : FRAME_FONT (f);
18269 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18270 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18271 struct glyph *g;
18272 int row_width, stretch_ascent, stretch_width;
18273 struct text_pos saved_pos;
18274 int saved_face_id, saved_avoid_cursor;
18276 for (row_width = 0, g = row_start; g < row_end; g++)
18277 row_width += g->pixel_width;
18278 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18279 if (stretch_width > 0)
18281 stretch_ascent =
18282 (((it->ascent + it->descent)
18283 * FONT_BASE (font)) / FONT_HEIGHT (font));
18284 saved_pos = it->position;
18285 memset (&it->position, 0, sizeof it->position);
18286 saved_avoid_cursor = it->avoid_cursor_p;
18287 it->avoid_cursor_p = 1;
18288 saved_face_id = it->face_id;
18289 /* The last row's stretch glyph should get the default
18290 face, to avoid painting the rest of the window with
18291 the region face, if the region ends at ZV. */
18292 if (it->glyph_row->ends_at_zv_p)
18293 it->face_id = DEFAULT_FACE_ID;
18294 else
18295 it->face_id = face->id;
18296 append_stretch_glyph (it, make_number (0), stretch_width,
18297 it->ascent + it->descent, stretch_ascent);
18298 it->position = saved_pos;
18299 it->avoid_cursor_p = saved_avoid_cursor;
18300 it->face_id = saved_face_id;
18303 #endif /* HAVE_WINDOW_SYSTEM */
18305 else
18307 /* Save some values that must not be changed. */
18308 int saved_x = it->current_x;
18309 struct text_pos saved_pos;
18310 Lisp_Object saved_object;
18311 enum display_element_type saved_what = it->what;
18312 int saved_face_id = it->face_id;
18314 saved_object = it->object;
18315 saved_pos = it->position;
18317 it->what = IT_CHARACTER;
18318 memset (&it->position, 0, sizeof it->position);
18319 it->object = make_number (0);
18320 it->c = it->char_to_display = ' ';
18321 it->len = 1;
18322 /* The last row's blank glyphs should get the default face, to
18323 avoid painting the rest of the window with the region face,
18324 if the region ends at ZV. */
18325 if (it->glyph_row->ends_at_zv_p)
18326 it->face_id = DEFAULT_FACE_ID;
18327 else
18328 it->face_id = face->id;
18330 PRODUCE_GLYPHS (it);
18332 while (it->current_x <= it->last_visible_x)
18333 PRODUCE_GLYPHS (it);
18335 /* Don't count these blanks really. It would let us insert a left
18336 truncation glyph below and make us set the cursor on them, maybe. */
18337 it->current_x = saved_x;
18338 it->object = saved_object;
18339 it->position = saved_pos;
18340 it->what = saved_what;
18341 it->face_id = saved_face_id;
18346 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18347 trailing whitespace. */
18349 static int
18350 trailing_whitespace_p (EMACS_INT charpos)
18352 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
18353 int c = 0;
18355 while (bytepos < ZV_BYTE
18356 && (c = FETCH_CHAR (bytepos),
18357 c == ' ' || c == '\t'))
18358 ++bytepos;
18360 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18362 if (bytepos != PT_BYTE)
18363 return 1;
18365 return 0;
18369 /* Highlight trailing whitespace, if any, in ROW. */
18371 static void
18372 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18374 int used = row->used[TEXT_AREA];
18376 if (used)
18378 struct glyph *start = row->glyphs[TEXT_AREA];
18379 struct glyph *glyph = start + used - 1;
18381 if (row->reversed_p)
18383 /* Right-to-left rows need to be processed in the opposite
18384 direction, so swap the edge pointers. */
18385 glyph = start;
18386 start = row->glyphs[TEXT_AREA] + used - 1;
18389 /* Skip over glyphs inserted to display the cursor at the
18390 end of a line, for extending the face of the last glyph
18391 to the end of the line on terminals, and for truncation
18392 and continuation glyphs. */
18393 if (!row->reversed_p)
18395 while (glyph >= start
18396 && glyph->type == CHAR_GLYPH
18397 && INTEGERP (glyph->object))
18398 --glyph;
18400 else
18402 while (glyph <= start
18403 && glyph->type == CHAR_GLYPH
18404 && INTEGERP (glyph->object))
18405 ++glyph;
18408 /* If last glyph is a space or stretch, and it's trailing
18409 whitespace, set the face of all trailing whitespace glyphs in
18410 IT->glyph_row to `trailing-whitespace'. */
18411 if ((row->reversed_p ? glyph <= start : glyph >= start)
18412 && BUFFERP (glyph->object)
18413 && (glyph->type == STRETCH_GLYPH
18414 || (glyph->type == CHAR_GLYPH
18415 && glyph->u.ch == ' '))
18416 && trailing_whitespace_p (glyph->charpos))
18418 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18419 if (face_id < 0)
18420 return;
18422 if (!row->reversed_p)
18424 while (glyph >= start
18425 && BUFFERP (glyph->object)
18426 && (glyph->type == STRETCH_GLYPH
18427 || (glyph->type == CHAR_GLYPH
18428 && glyph->u.ch == ' ')))
18429 (glyph--)->face_id = face_id;
18431 else
18433 while (glyph <= start
18434 && BUFFERP (glyph->object)
18435 && (glyph->type == STRETCH_GLYPH
18436 || (glyph->type == CHAR_GLYPH
18437 && glyph->u.ch == ' ')))
18438 (glyph++)->face_id = face_id;
18445 /* Value is non-zero if glyph row ROW should be
18446 used to hold the cursor. */
18448 static int
18449 cursor_row_p (struct glyph_row *row)
18451 int result = 1;
18453 if (PT == CHARPOS (row->end.pos)
18454 || PT == MATRIX_ROW_END_CHARPOS (row))
18456 /* Suppose the row ends on a string.
18457 Unless the row is continued, that means it ends on a newline
18458 in the string. If it's anything other than a display string
18459 (e.g. a before-string from an overlay), we don't want the
18460 cursor there. (This heuristic seems to give the optimal
18461 behavior for the various types of multi-line strings.) */
18462 if (CHARPOS (row->end.string_pos) >= 0)
18464 if (row->continued_p)
18465 result = 1;
18466 else
18468 /* Check for `display' property. */
18469 struct glyph *beg = row->glyphs[TEXT_AREA];
18470 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18471 struct glyph *glyph;
18473 result = 0;
18474 for (glyph = end; glyph >= beg; --glyph)
18475 if (STRINGP (glyph->object))
18477 Lisp_Object prop
18478 = Fget_char_property (make_number (PT),
18479 Qdisplay, Qnil);
18480 result =
18481 (!NILP (prop)
18482 && display_prop_string_p (prop, glyph->object));
18483 break;
18487 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18489 /* If the row ends in middle of a real character,
18490 and the line is continued, we want the cursor here.
18491 That's because CHARPOS (ROW->end.pos) would equal
18492 PT if PT is before the character. */
18493 if (!row->ends_in_ellipsis_p)
18494 result = row->continued_p;
18495 else
18496 /* If the row ends in an ellipsis, then
18497 CHARPOS (ROW->end.pos) will equal point after the
18498 invisible text. We want that position to be displayed
18499 after the ellipsis. */
18500 result = 0;
18502 /* If the row ends at ZV, display the cursor at the end of that
18503 row instead of at the start of the row below. */
18504 else if (row->ends_at_zv_p)
18505 result = 1;
18506 else
18507 result = 0;
18510 return result;
18515 /* Push the property PROP so that it will be rendered at the current
18516 position in IT. Return 1 if PROP was successfully pushed, 0
18517 otherwise. Called from handle_line_prefix to handle the
18518 `line-prefix' and `wrap-prefix' properties. */
18520 static int
18521 push_display_prop (struct it *it, Lisp_Object prop)
18523 struct text_pos pos =
18524 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18526 xassert (it->method == GET_FROM_BUFFER
18527 || it->method == GET_FROM_DISPLAY_VECTOR
18528 || it->method == GET_FROM_STRING);
18530 /* We need to save the current buffer/string position, so it will be
18531 restored by pop_it, because iterate_out_of_display_property
18532 depends on that being set correctly, but some situations leave
18533 it->position not yet set when this function is called. */
18534 push_it (it, &pos);
18536 if (STRINGP (prop))
18538 if (SCHARS (prop) == 0)
18540 pop_it (it);
18541 return 0;
18544 it->string = prop;
18545 it->multibyte_p = STRING_MULTIBYTE (it->string);
18546 it->current.overlay_string_index = -1;
18547 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18548 it->end_charpos = it->string_nchars = SCHARS (it->string);
18549 it->method = GET_FROM_STRING;
18550 it->stop_charpos = 0;
18551 it->prev_stop = 0;
18552 it->base_level_stop = 0;
18554 /* Force paragraph direction to be that of the parent
18555 buffer/string. */
18556 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18557 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18558 else
18559 it->paragraph_embedding = L2R;
18561 /* Set up the bidi iterator for this display string. */
18562 if (it->bidi_p)
18564 it->bidi_it.string.lstring = it->string;
18565 it->bidi_it.string.s = NULL;
18566 it->bidi_it.string.schars = it->end_charpos;
18567 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18568 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18569 it->bidi_it.string.unibyte = !it->multibyte_p;
18570 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18573 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18575 it->method = GET_FROM_STRETCH;
18576 it->object = prop;
18578 #ifdef HAVE_WINDOW_SYSTEM
18579 else if (IMAGEP (prop))
18581 it->what = IT_IMAGE;
18582 it->image_id = lookup_image (it->f, prop);
18583 it->method = GET_FROM_IMAGE;
18585 #endif /* HAVE_WINDOW_SYSTEM */
18586 else
18588 pop_it (it); /* bogus display property, give up */
18589 return 0;
18592 return 1;
18595 /* Return the character-property PROP at the current position in IT. */
18597 static Lisp_Object
18598 get_it_property (struct it *it, Lisp_Object prop)
18600 Lisp_Object position;
18602 if (STRINGP (it->object))
18603 position = make_number (IT_STRING_CHARPOS (*it));
18604 else if (BUFFERP (it->object))
18605 position = make_number (IT_CHARPOS (*it));
18606 else
18607 return Qnil;
18609 return Fget_char_property (position, prop, it->object);
18612 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18614 static void
18615 handle_line_prefix (struct it *it)
18617 Lisp_Object prefix;
18619 if (it->continuation_lines_width > 0)
18621 prefix = get_it_property (it, Qwrap_prefix);
18622 if (NILP (prefix))
18623 prefix = Vwrap_prefix;
18625 else
18627 prefix = get_it_property (it, Qline_prefix);
18628 if (NILP (prefix))
18629 prefix = Vline_prefix;
18631 if (! NILP (prefix) && push_display_prop (it, prefix))
18633 /* If the prefix is wider than the window, and we try to wrap
18634 it, it would acquire its own wrap prefix, and so on till the
18635 iterator stack overflows. So, don't wrap the prefix. */
18636 it->line_wrap = TRUNCATE;
18637 it->avoid_cursor_p = 1;
18643 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18644 only for R2L lines from display_line and display_string, when they
18645 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18646 the line/string needs to be continued on the next glyph row. */
18647 static void
18648 unproduce_glyphs (struct it *it, int n)
18650 struct glyph *glyph, *end;
18652 xassert (it->glyph_row);
18653 xassert (it->glyph_row->reversed_p);
18654 xassert (it->area == TEXT_AREA);
18655 xassert (n <= it->glyph_row->used[TEXT_AREA]);
18657 if (n > it->glyph_row->used[TEXT_AREA])
18658 n = it->glyph_row->used[TEXT_AREA];
18659 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18660 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18661 for ( ; glyph < end; glyph++)
18662 glyph[-n] = *glyph;
18665 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18666 and ROW->maxpos. */
18667 static void
18668 find_row_edges (struct it *it, struct glyph_row *row,
18669 EMACS_INT min_pos, EMACS_INT min_bpos,
18670 EMACS_INT max_pos, EMACS_INT max_bpos)
18672 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18673 lines' rows is implemented for bidi-reordered rows. */
18675 /* ROW->minpos is the value of min_pos, the minimal buffer position
18676 we have in ROW, or ROW->start.pos if that is smaller. */
18677 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18678 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18679 else
18680 /* We didn't find buffer positions smaller than ROW->start, or
18681 didn't find _any_ valid buffer positions in any of the glyphs,
18682 so we must trust the iterator's computed positions. */
18683 row->minpos = row->start.pos;
18684 if (max_pos <= 0)
18686 max_pos = CHARPOS (it->current.pos);
18687 max_bpos = BYTEPOS (it->current.pos);
18690 /* Here are the various use-cases for ending the row, and the
18691 corresponding values for ROW->maxpos:
18693 Line ends in a newline from buffer eol_pos + 1
18694 Line is continued from buffer max_pos + 1
18695 Line is truncated on right it->current.pos
18696 Line ends in a newline from string max_pos + 1(*)
18697 (*) + 1 only when line ends in a forward scan
18698 Line is continued from string max_pos
18699 Line is continued from display vector max_pos
18700 Line is entirely from a string min_pos == max_pos
18701 Line is entirely from a display vector min_pos == max_pos
18702 Line that ends at ZV ZV
18704 If you discover other use-cases, please add them here as
18705 appropriate. */
18706 if (row->ends_at_zv_p)
18707 row->maxpos = it->current.pos;
18708 else if (row->used[TEXT_AREA])
18710 int seen_this_string = 0;
18711 struct glyph_row *r1 = row - 1;
18713 /* Did we see the same display string on the previous row? */
18714 if (STRINGP (it->object)
18715 /* this is not the first row */
18716 && row > it->w->desired_matrix->rows
18717 /* previous row is not the header line */
18718 && !r1->mode_line_p
18719 /* previous row also ends in a newline from a string */
18720 && r1->ends_in_newline_from_string_p)
18722 struct glyph *start, *end;
18724 /* Search for the last glyph of the previous row that came
18725 from buffer or string. Depending on whether the row is
18726 L2R or R2L, we need to process it front to back or the
18727 other way round. */
18728 if (!r1->reversed_p)
18730 start = r1->glyphs[TEXT_AREA];
18731 end = start + r1->used[TEXT_AREA];
18732 /* Glyphs inserted by redisplay have an integer (zero)
18733 as their object. */
18734 while (end > start
18735 && INTEGERP ((end - 1)->object)
18736 && (end - 1)->charpos <= 0)
18737 --end;
18738 if (end > start)
18740 if (EQ ((end - 1)->object, it->object))
18741 seen_this_string = 1;
18743 else
18744 /* If all the glyphs of the previous row were inserted
18745 by redisplay, it means the previous row was
18746 produced from a single newline, which is only
18747 possible if that newline came from the same string
18748 as the one which produced this ROW. */
18749 seen_this_string = 1;
18751 else
18753 end = r1->glyphs[TEXT_AREA] - 1;
18754 start = end + r1->used[TEXT_AREA];
18755 while (end < start
18756 && INTEGERP ((end + 1)->object)
18757 && (end + 1)->charpos <= 0)
18758 ++end;
18759 if (end < start)
18761 if (EQ ((end + 1)->object, it->object))
18762 seen_this_string = 1;
18764 else
18765 seen_this_string = 1;
18768 /* Take note of each display string that covers a newline only
18769 once, the first time we see it. This is for when a display
18770 string includes more than one newline in it. */
18771 if (row->ends_in_newline_from_string_p && !seen_this_string)
18773 /* If we were scanning the buffer forward when we displayed
18774 the string, we want to account for at least one buffer
18775 position that belongs to this row (position covered by
18776 the display string), so that cursor positioning will
18777 consider this row as a candidate when point is at the end
18778 of the visual line represented by this row. This is not
18779 required when scanning back, because max_pos will already
18780 have a much larger value. */
18781 if (CHARPOS (row->end.pos) > max_pos)
18782 INC_BOTH (max_pos, max_bpos);
18783 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18785 else if (CHARPOS (it->eol_pos) > 0)
18786 SET_TEXT_POS (row->maxpos,
18787 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
18788 else if (row->continued_p)
18790 /* If max_pos is different from IT's current position, it
18791 means IT->method does not belong to the display element
18792 at max_pos. However, it also means that the display
18793 element at max_pos was displayed in its entirety on this
18794 line, which is equivalent to saying that the next line
18795 starts at the next buffer position. */
18796 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
18797 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18798 else
18800 INC_BOTH (max_pos, max_bpos);
18801 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18804 else if (row->truncated_on_right_p)
18805 /* display_line already called reseat_at_next_visible_line_start,
18806 which puts the iterator at the beginning of the next line, in
18807 the logical order. */
18808 row->maxpos = it->current.pos;
18809 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
18810 /* A line that is entirely from a string/image/stretch... */
18811 row->maxpos = row->minpos;
18812 else
18813 abort ();
18815 else
18816 row->maxpos = it->current.pos;
18819 /* Construct the glyph row IT->glyph_row in the desired matrix of
18820 IT->w from text at the current position of IT. See dispextern.h
18821 for an overview of struct it. Value is non-zero if
18822 IT->glyph_row displays text, as opposed to a line displaying ZV
18823 only. */
18825 static int
18826 display_line (struct it *it)
18828 struct glyph_row *row = it->glyph_row;
18829 Lisp_Object overlay_arrow_string;
18830 struct it wrap_it;
18831 void *wrap_data = NULL;
18832 int may_wrap = 0, wrap_x IF_LINT (= 0);
18833 int wrap_row_used = -1;
18834 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
18835 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
18836 int wrap_row_extra_line_spacing IF_LINT (= 0);
18837 EMACS_INT wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
18838 EMACS_INT wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
18839 int cvpos;
18840 EMACS_INT min_pos = ZV + 1, max_pos = 0;
18841 EMACS_INT min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
18843 /* We always start displaying at hpos zero even if hscrolled. */
18844 xassert (it->hpos == 0 && it->current_x == 0);
18846 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
18847 >= it->w->desired_matrix->nrows)
18849 it->w->nrows_scale_factor++;
18850 fonts_changed_p = 1;
18851 return 0;
18854 /* Is IT->w showing the region? */
18855 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
18857 /* Clear the result glyph row and enable it. */
18858 prepare_desired_row (row);
18860 row->y = it->current_y;
18861 row->start = it->start;
18862 row->continuation_lines_width = it->continuation_lines_width;
18863 row->displays_text_p = 1;
18864 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
18865 it->starts_in_middle_of_char_p = 0;
18867 /* Arrange the overlays nicely for our purposes. Usually, we call
18868 display_line on only one line at a time, in which case this
18869 can't really hurt too much, or we call it on lines which appear
18870 one after another in the buffer, in which case all calls to
18871 recenter_overlay_lists but the first will be pretty cheap. */
18872 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
18874 /* Move over display elements that are not visible because we are
18875 hscrolled. This may stop at an x-position < IT->first_visible_x
18876 if the first glyph is partially visible or if we hit a line end. */
18877 if (it->current_x < it->first_visible_x)
18879 this_line_min_pos = row->start.pos;
18880 move_it_in_display_line_to (it, ZV, it->first_visible_x,
18881 MOVE_TO_POS | MOVE_TO_X);
18882 /* Record the smallest positions seen while we moved over
18883 display elements that are not visible. This is needed by
18884 redisplay_internal for optimizing the case where the cursor
18885 stays inside the same line. The rest of this function only
18886 considers positions that are actually displayed, so
18887 RECORD_MAX_MIN_POS will not otherwise record positions that
18888 are hscrolled to the left of the left edge of the window. */
18889 min_pos = CHARPOS (this_line_min_pos);
18890 min_bpos = BYTEPOS (this_line_min_pos);
18892 else
18894 /* We only do this when not calling `move_it_in_display_line_to'
18895 above, because move_it_in_display_line_to calls
18896 handle_line_prefix itself. */
18897 handle_line_prefix (it);
18900 /* Get the initial row height. This is either the height of the
18901 text hscrolled, if there is any, or zero. */
18902 row->ascent = it->max_ascent;
18903 row->height = it->max_ascent + it->max_descent;
18904 row->phys_ascent = it->max_phys_ascent;
18905 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18906 row->extra_line_spacing = it->max_extra_line_spacing;
18908 /* Utility macro to record max and min buffer positions seen until now. */
18909 #define RECORD_MAX_MIN_POS(IT) \
18910 do \
18912 int composition_p = !STRINGP ((IT)->string) \
18913 && ((IT)->what == IT_COMPOSITION); \
18914 EMACS_INT current_pos = \
18915 composition_p ? (IT)->cmp_it.charpos \
18916 : IT_CHARPOS (*(IT)); \
18917 EMACS_INT current_bpos = \
18918 composition_p ? CHAR_TO_BYTE (current_pos) \
18919 : IT_BYTEPOS (*(IT)); \
18920 if (current_pos < min_pos) \
18922 min_pos = current_pos; \
18923 min_bpos = current_bpos; \
18925 if (IT_CHARPOS (*it) > max_pos) \
18927 max_pos = IT_CHARPOS (*it); \
18928 max_bpos = IT_BYTEPOS (*it); \
18931 while (0)
18933 /* Loop generating characters. The loop is left with IT on the next
18934 character to display. */
18935 while (1)
18937 int n_glyphs_before, hpos_before, x_before;
18938 int x, nglyphs;
18939 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
18941 /* Retrieve the next thing to display. Value is zero if end of
18942 buffer reached. */
18943 if (!get_next_display_element (it))
18945 /* Maybe add a space at the end of this line that is used to
18946 display the cursor there under X. Set the charpos of the
18947 first glyph of blank lines not corresponding to any text
18948 to -1. */
18949 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18950 row->exact_window_width_line_p = 1;
18951 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
18952 || row->used[TEXT_AREA] == 0)
18954 row->glyphs[TEXT_AREA]->charpos = -1;
18955 row->displays_text_p = 0;
18957 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
18958 && (!MINI_WINDOW_P (it->w)
18959 || (minibuf_level && EQ (it->window, minibuf_window))))
18960 row->indicate_empty_line_p = 1;
18963 it->continuation_lines_width = 0;
18964 row->ends_at_zv_p = 1;
18965 /* A row that displays right-to-left text must always have
18966 its last face extended all the way to the end of line,
18967 even if this row ends in ZV, because we still write to
18968 the screen left to right. */
18969 if (row->reversed_p)
18970 extend_face_to_end_of_line (it);
18971 break;
18974 /* Now, get the metrics of what we want to display. This also
18975 generates glyphs in `row' (which is IT->glyph_row). */
18976 n_glyphs_before = row->used[TEXT_AREA];
18977 x = it->current_x;
18979 /* Remember the line height so far in case the next element doesn't
18980 fit on the line. */
18981 if (it->line_wrap != TRUNCATE)
18983 ascent = it->max_ascent;
18984 descent = it->max_descent;
18985 phys_ascent = it->max_phys_ascent;
18986 phys_descent = it->max_phys_descent;
18988 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
18990 if (IT_DISPLAYING_WHITESPACE (it))
18991 may_wrap = 1;
18992 else if (may_wrap)
18994 SAVE_IT (wrap_it, *it, wrap_data);
18995 wrap_x = x;
18996 wrap_row_used = row->used[TEXT_AREA];
18997 wrap_row_ascent = row->ascent;
18998 wrap_row_height = row->height;
18999 wrap_row_phys_ascent = row->phys_ascent;
19000 wrap_row_phys_height = row->phys_height;
19001 wrap_row_extra_line_spacing = row->extra_line_spacing;
19002 wrap_row_min_pos = min_pos;
19003 wrap_row_min_bpos = min_bpos;
19004 wrap_row_max_pos = max_pos;
19005 wrap_row_max_bpos = max_bpos;
19006 may_wrap = 0;
19011 PRODUCE_GLYPHS (it);
19013 /* If this display element was in marginal areas, continue with
19014 the next one. */
19015 if (it->area != TEXT_AREA)
19017 row->ascent = max (row->ascent, it->max_ascent);
19018 row->height = max (row->height, it->max_ascent + it->max_descent);
19019 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19020 row->phys_height = max (row->phys_height,
19021 it->max_phys_ascent + it->max_phys_descent);
19022 row->extra_line_spacing = max (row->extra_line_spacing,
19023 it->max_extra_line_spacing);
19024 set_iterator_to_next (it, 1);
19025 continue;
19028 /* Does the display element fit on the line? If we truncate
19029 lines, we should draw past the right edge of the window. If
19030 we don't truncate, we want to stop so that we can display the
19031 continuation glyph before the right margin. If lines are
19032 continued, there are two possible strategies for characters
19033 resulting in more than 1 glyph (e.g. tabs): Display as many
19034 glyphs as possible in this line and leave the rest for the
19035 continuation line, or display the whole element in the next
19036 line. Original redisplay did the former, so we do it also. */
19037 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19038 hpos_before = it->hpos;
19039 x_before = x;
19041 if (/* Not a newline. */
19042 nglyphs > 0
19043 /* Glyphs produced fit entirely in the line. */
19044 && it->current_x < it->last_visible_x)
19046 it->hpos += nglyphs;
19047 row->ascent = max (row->ascent, it->max_ascent);
19048 row->height = max (row->height, it->max_ascent + it->max_descent);
19049 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19050 row->phys_height = max (row->phys_height,
19051 it->max_phys_ascent + it->max_phys_descent);
19052 row->extra_line_spacing = max (row->extra_line_spacing,
19053 it->max_extra_line_spacing);
19054 if (it->current_x - it->pixel_width < it->first_visible_x)
19055 row->x = x - it->first_visible_x;
19056 /* Record the maximum and minimum buffer positions seen so
19057 far in glyphs that will be displayed by this row. */
19058 if (it->bidi_p)
19059 RECORD_MAX_MIN_POS (it);
19061 else
19063 int i, new_x;
19064 struct glyph *glyph;
19066 for (i = 0; i < nglyphs; ++i, x = new_x)
19068 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19069 new_x = x + glyph->pixel_width;
19071 if (/* Lines are continued. */
19072 it->line_wrap != TRUNCATE
19073 && (/* Glyph doesn't fit on the line. */
19074 new_x > it->last_visible_x
19075 /* Or it fits exactly on a window system frame. */
19076 || (new_x == it->last_visible_x
19077 && FRAME_WINDOW_P (it->f))))
19079 /* End of a continued line. */
19081 if (it->hpos == 0
19082 || (new_x == it->last_visible_x
19083 && FRAME_WINDOW_P (it->f)))
19085 /* Current glyph is the only one on the line or
19086 fits exactly on the line. We must continue
19087 the line because we can't draw the cursor
19088 after the glyph. */
19089 row->continued_p = 1;
19090 it->current_x = new_x;
19091 it->continuation_lines_width += new_x;
19092 ++it->hpos;
19093 if (i == nglyphs - 1)
19095 /* If line-wrap is on, check if a previous
19096 wrap point was found. */
19097 if (wrap_row_used > 0
19098 /* Even if there is a previous wrap
19099 point, continue the line here as
19100 usual, if (i) the previous character
19101 was a space or tab AND (ii) the
19102 current character is not. */
19103 && (!may_wrap
19104 || IT_DISPLAYING_WHITESPACE (it)))
19105 goto back_to_wrap;
19107 /* Record the maximum and minimum buffer
19108 positions seen so far in glyphs that will be
19109 displayed by this row. */
19110 if (it->bidi_p)
19111 RECORD_MAX_MIN_POS (it);
19112 set_iterator_to_next (it, 1);
19113 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19115 if (!get_next_display_element (it))
19117 row->exact_window_width_line_p = 1;
19118 it->continuation_lines_width = 0;
19119 row->continued_p = 0;
19120 row->ends_at_zv_p = 1;
19122 else if (ITERATOR_AT_END_OF_LINE_P (it))
19124 row->continued_p = 0;
19125 row->exact_window_width_line_p = 1;
19129 else if (it->bidi_p)
19130 RECORD_MAX_MIN_POS (it);
19132 else if (CHAR_GLYPH_PADDING_P (*glyph)
19133 && !FRAME_WINDOW_P (it->f))
19135 /* A padding glyph that doesn't fit on this line.
19136 This means the whole character doesn't fit
19137 on the line. */
19138 if (row->reversed_p)
19139 unproduce_glyphs (it, row->used[TEXT_AREA]
19140 - n_glyphs_before);
19141 row->used[TEXT_AREA] = n_glyphs_before;
19143 /* Fill the rest of the row with continuation
19144 glyphs like in 20.x. */
19145 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19146 < row->glyphs[1 + TEXT_AREA])
19147 produce_special_glyphs (it, IT_CONTINUATION);
19149 row->continued_p = 1;
19150 it->current_x = x_before;
19151 it->continuation_lines_width += x_before;
19153 /* Restore the height to what it was before the
19154 element not fitting on the line. */
19155 it->max_ascent = ascent;
19156 it->max_descent = descent;
19157 it->max_phys_ascent = phys_ascent;
19158 it->max_phys_descent = phys_descent;
19160 else if (wrap_row_used > 0)
19162 back_to_wrap:
19163 if (row->reversed_p)
19164 unproduce_glyphs (it,
19165 row->used[TEXT_AREA] - wrap_row_used);
19166 RESTORE_IT (it, &wrap_it, wrap_data);
19167 it->continuation_lines_width += wrap_x;
19168 row->used[TEXT_AREA] = wrap_row_used;
19169 row->ascent = wrap_row_ascent;
19170 row->height = wrap_row_height;
19171 row->phys_ascent = wrap_row_phys_ascent;
19172 row->phys_height = wrap_row_phys_height;
19173 row->extra_line_spacing = wrap_row_extra_line_spacing;
19174 min_pos = wrap_row_min_pos;
19175 min_bpos = wrap_row_min_bpos;
19176 max_pos = wrap_row_max_pos;
19177 max_bpos = wrap_row_max_bpos;
19178 row->continued_p = 1;
19179 row->ends_at_zv_p = 0;
19180 row->exact_window_width_line_p = 0;
19181 it->continuation_lines_width += x;
19183 /* Make sure that a non-default face is extended
19184 up to the right margin of the window. */
19185 extend_face_to_end_of_line (it);
19187 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19189 /* A TAB that extends past the right edge of the
19190 window. This produces a single glyph on
19191 window system frames. We leave the glyph in
19192 this row and let it fill the row, but don't
19193 consume the TAB. */
19194 it->continuation_lines_width += it->last_visible_x;
19195 row->ends_in_middle_of_char_p = 1;
19196 row->continued_p = 1;
19197 glyph->pixel_width = it->last_visible_x - x;
19198 it->starts_in_middle_of_char_p = 1;
19200 else
19202 /* Something other than a TAB that draws past
19203 the right edge of the window. Restore
19204 positions to values before the element. */
19205 if (row->reversed_p)
19206 unproduce_glyphs (it, row->used[TEXT_AREA]
19207 - (n_glyphs_before + i));
19208 row->used[TEXT_AREA] = n_glyphs_before + i;
19210 /* Display continuation glyphs. */
19211 if (!FRAME_WINDOW_P (it->f))
19212 produce_special_glyphs (it, IT_CONTINUATION);
19213 row->continued_p = 1;
19215 it->current_x = x_before;
19216 it->continuation_lines_width += x;
19217 extend_face_to_end_of_line (it);
19219 if (nglyphs > 1 && i > 0)
19221 row->ends_in_middle_of_char_p = 1;
19222 it->starts_in_middle_of_char_p = 1;
19225 /* Restore the height to what it was before the
19226 element not fitting on the line. */
19227 it->max_ascent = ascent;
19228 it->max_descent = descent;
19229 it->max_phys_ascent = phys_ascent;
19230 it->max_phys_descent = phys_descent;
19233 break;
19235 else if (new_x > it->first_visible_x)
19237 /* Increment number of glyphs actually displayed. */
19238 ++it->hpos;
19240 /* Record the maximum and minimum buffer positions
19241 seen so far in glyphs that will be displayed by
19242 this row. */
19243 if (it->bidi_p)
19244 RECORD_MAX_MIN_POS (it);
19246 if (x < it->first_visible_x)
19247 /* Glyph is partially visible, i.e. row starts at
19248 negative X position. */
19249 row->x = x - it->first_visible_x;
19251 else
19253 /* Glyph is completely off the left margin of the
19254 window. This should not happen because of the
19255 move_it_in_display_line at the start of this
19256 function, unless the text display area of the
19257 window is empty. */
19258 xassert (it->first_visible_x <= it->last_visible_x);
19261 /* Even if this display element produced no glyphs at all,
19262 we want to record its position. */
19263 if (it->bidi_p && nglyphs == 0)
19264 RECORD_MAX_MIN_POS (it);
19266 row->ascent = max (row->ascent, it->max_ascent);
19267 row->height = max (row->height, it->max_ascent + it->max_descent);
19268 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19269 row->phys_height = max (row->phys_height,
19270 it->max_phys_ascent + it->max_phys_descent);
19271 row->extra_line_spacing = max (row->extra_line_spacing,
19272 it->max_extra_line_spacing);
19274 /* End of this display line if row is continued. */
19275 if (row->continued_p || row->ends_at_zv_p)
19276 break;
19279 at_end_of_line:
19280 /* Is this a line end? If yes, we're also done, after making
19281 sure that a non-default face is extended up to the right
19282 margin of the window. */
19283 if (ITERATOR_AT_END_OF_LINE_P (it))
19285 int used_before = row->used[TEXT_AREA];
19287 row->ends_in_newline_from_string_p = STRINGP (it->object);
19289 /* Add a space at the end of the line that is used to
19290 display the cursor there. */
19291 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19292 append_space_for_newline (it, 0);
19294 /* Extend the face to the end of the line. */
19295 extend_face_to_end_of_line (it);
19297 /* Make sure we have the position. */
19298 if (used_before == 0)
19299 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19301 /* Record the position of the newline, for use in
19302 find_row_edges. */
19303 it->eol_pos = it->current.pos;
19305 /* Consume the line end. This skips over invisible lines. */
19306 set_iterator_to_next (it, 1);
19307 it->continuation_lines_width = 0;
19308 break;
19311 /* Proceed with next display element. Note that this skips
19312 over lines invisible because of selective display. */
19313 set_iterator_to_next (it, 1);
19315 /* If we truncate lines, we are done when the last displayed
19316 glyphs reach past the right margin of the window. */
19317 if (it->line_wrap == TRUNCATE
19318 && (FRAME_WINDOW_P (it->f)
19319 ? (it->current_x >= it->last_visible_x)
19320 : (it->current_x > it->last_visible_x)))
19322 /* Maybe add truncation glyphs. */
19323 if (!FRAME_WINDOW_P (it->f))
19325 int i, n;
19327 if (!row->reversed_p)
19329 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19330 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19331 break;
19333 else
19335 for (i = 0; i < row->used[TEXT_AREA]; i++)
19336 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19337 break;
19338 /* Remove any padding glyphs at the front of ROW, to
19339 make room for the truncation glyphs we will be
19340 adding below. The loop below always inserts at
19341 least one truncation glyph, so also remove the
19342 last glyph added to ROW. */
19343 unproduce_glyphs (it, i + 1);
19344 /* Adjust i for the loop below. */
19345 i = row->used[TEXT_AREA] - (i + 1);
19348 for (n = row->used[TEXT_AREA]; i < n; ++i)
19350 row->used[TEXT_AREA] = i;
19351 produce_special_glyphs (it, IT_TRUNCATION);
19354 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19356 /* Don't truncate if we can overflow newline into fringe. */
19357 if (!get_next_display_element (it))
19359 it->continuation_lines_width = 0;
19360 row->ends_at_zv_p = 1;
19361 row->exact_window_width_line_p = 1;
19362 break;
19364 if (ITERATOR_AT_END_OF_LINE_P (it))
19366 row->exact_window_width_line_p = 1;
19367 goto at_end_of_line;
19371 row->truncated_on_right_p = 1;
19372 it->continuation_lines_width = 0;
19373 reseat_at_next_visible_line_start (it, 0);
19374 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19375 it->hpos = hpos_before;
19376 it->current_x = x_before;
19377 break;
19381 if (wrap_data)
19382 bidi_unshelve_cache (wrap_data, 1);
19384 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19385 at the left window margin. */
19386 if (it->first_visible_x
19387 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19389 if (!FRAME_WINDOW_P (it->f))
19390 insert_left_trunc_glyphs (it);
19391 row->truncated_on_left_p = 1;
19394 /* Remember the position at which this line ends.
19396 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19397 cannot be before the call to find_row_edges below, since that is
19398 where these positions are determined. */
19399 row->end = it->current;
19400 if (!it->bidi_p)
19402 row->minpos = row->start.pos;
19403 row->maxpos = row->end.pos;
19405 else
19407 /* ROW->minpos and ROW->maxpos must be the smallest and
19408 `1 + the largest' buffer positions in ROW. But if ROW was
19409 bidi-reordered, these two positions can be anywhere in the
19410 row, so we must determine them now. */
19411 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19414 /* If the start of this line is the overlay arrow-position, then
19415 mark this glyph row as the one containing the overlay arrow.
19416 This is clearly a mess with variable size fonts. It would be
19417 better to let it be displayed like cursors under X. */
19418 if ((row->displays_text_p || !overlay_arrow_seen)
19419 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19420 !NILP (overlay_arrow_string)))
19422 /* Overlay arrow in window redisplay is a fringe bitmap. */
19423 if (STRINGP (overlay_arrow_string))
19425 struct glyph_row *arrow_row
19426 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19427 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19428 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19429 struct glyph *p = row->glyphs[TEXT_AREA];
19430 struct glyph *p2, *end;
19432 /* Copy the arrow glyphs. */
19433 while (glyph < arrow_end)
19434 *p++ = *glyph++;
19436 /* Throw away padding glyphs. */
19437 p2 = p;
19438 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19439 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19440 ++p2;
19441 if (p2 > p)
19443 while (p2 < end)
19444 *p++ = *p2++;
19445 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19448 else
19450 xassert (INTEGERP (overlay_arrow_string));
19451 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19453 overlay_arrow_seen = 1;
19456 /* Highlight trailing whitespace. */
19457 if (!NILP (Vshow_trailing_whitespace))
19458 highlight_trailing_whitespace (it->f, it->glyph_row);
19460 /* Compute pixel dimensions of this line. */
19461 compute_line_metrics (it);
19463 /* Implementation note: No changes in the glyphs of ROW or in their
19464 faces can be done past this point, because compute_line_metrics
19465 computes ROW's hash value and stores it within the glyph_row
19466 structure. */
19468 /* Record whether this row ends inside an ellipsis. */
19469 row->ends_in_ellipsis_p
19470 = (it->method == GET_FROM_DISPLAY_VECTOR
19471 && it->ellipsis_p);
19473 /* Save fringe bitmaps in this row. */
19474 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19475 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19476 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19477 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19479 it->left_user_fringe_bitmap = 0;
19480 it->left_user_fringe_face_id = 0;
19481 it->right_user_fringe_bitmap = 0;
19482 it->right_user_fringe_face_id = 0;
19484 /* Maybe set the cursor. */
19485 cvpos = it->w->cursor.vpos;
19486 if ((cvpos < 0
19487 /* In bidi-reordered rows, keep checking for proper cursor
19488 position even if one has been found already, because buffer
19489 positions in such rows change non-linearly with ROW->VPOS,
19490 when a line is continued. One exception: when we are at ZV,
19491 display cursor on the first suitable glyph row, since all
19492 the empty rows after that also have their position set to ZV. */
19493 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19494 lines' rows is implemented for bidi-reordered rows. */
19495 || (it->bidi_p
19496 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19497 && PT >= MATRIX_ROW_START_CHARPOS (row)
19498 && PT <= MATRIX_ROW_END_CHARPOS (row)
19499 && cursor_row_p (row))
19500 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19502 /* Prepare for the next line. This line starts horizontally at (X
19503 HPOS) = (0 0). Vertical positions are incremented. As a
19504 convenience for the caller, IT->glyph_row is set to the next
19505 row to be used. */
19506 it->current_x = it->hpos = 0;
19507 it->current_y += row->height;
19508 SET_TEXT_POS (it->eol_pos, 0, 0);
19509 ++it->vpos;
19510 ++it->glyph_row;
19511 /* The next row should by default use the same value of the
19512 reversed_p flag as this one. set_iterator_to_next decides when
19513 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19514 the flag accordingly. */
19515 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19516 it->glyph_row->reversed_p = row->reversed_p;
19517 it->start = row->end;
19518 return row->displays_text_p;
19520 #undef RECORD_MAX_MIN_POS
19523 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19524 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19525 doc: /* Return paragraph direction at point in BUFFER.
19526 Value is either `left-to-right' or `right-to-left'.
19527 If BUFFER is omitted or nil, it defaults to the current buffer.
19529 Paragraph direction determines how the text in the paragraph is displayed.
19530 In left-to-right paragraphs, text begins at the left margin of the window
19531 and the reading direction is generally left to right. In right-to-left
19532 paragraphs, text begins at the right margin and is read from right to left.
19534 See also `bidi-paragraph-direction'. */)
19535 (Lisp_Object buffer)
19537 struct buffer *buf = current_buffer;
19538 struct buffer *old = buf;
19540 if (! NILP (buffer))
19542 CHECK_BUFFER (buffer);
19543 buf = XBUFFER (buffer);
19546 if (NILP (BVAR (buf, bidi_display_reordering))
19547 || NILP (BVAR (buf, enable_multibyte_characters))
19548 /* When we are loading loadup.el, the character property tables
19549 needed for bidi iteration are not yet available. */
19550 || !NILP (Vpurify_flag))
19551 return Qleft_to_right;
19552 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19553 return BVAR (buf, bidi_paragraph_direction);
19554 else
19556 /* Determine the direction from buffer text. We could try to
19557 use current_matrix if it is up to date, but this seems fast
19558 enough as it is. */
19559 struct bidi_it itb;
19560 EMACS_INT pos = BUF_PT (buf);
19561 EMACS_INT bytepos = BUF_PT_BYTE (buf);
19562 int c;
19563 void *itb_data = bidi_shelve_cache ();
19565 set_buffer_temp (buf);
19566 /* bidi_paragraph_init finds the base direction of the paragraph
19567 by searching forward from paragraph start. We need the base
19568 direction of the current or _previous_ paragraph, so we need
19569 to make sure we are within that paragraph. To that end, find
19570 the previous non-empty line. */
19571 if (pos >= ZV && pos > BEGV)
19573 pos--;
19574 bytepos = CHAR_TO_BYTE (pos);
19576 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19577 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19579 while ((c = FETCH_BYTE (bytepos)) == '\n'
19580 || c == ' ' || c == '\t' || c == '\f')
19582 if (bytepos <= BEGV_BYTE)
19583 break;
19584 bytepos--;
19585 pos--;
19587 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19588 bytepos--;
19590 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19591 itb.paragraph_dir = NEUTRAL_DIR;
19592 itb.string.s = NULL;
19593 itb.string.lstring = Qnil;
19594 itb.string.bufpos = 0;
19595 itb.string.unibyte = 0;
19596 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19597 bidi_unshelve_cache (itb_data, 0);
19598 set_buffer_temp (old);
19599 switch (itb.paragraph_dir)
19601 case L2R:
19602 return Qleft_to_right;
19603 break;
19604 case R2L:
19605 return Qright_to_left;
19606 break;
19607 default:
19608 abort ();
19615 /***********************************************************************
19616 Menu Bar
19617 ***********************************************************************/
19619 /* Redisplay the menu bar in the frame for window W.
19621 The menu bar of X frames that don't have X toolkit support is
19622 displayed in a special window W->frame->menu_bar_window.
19624 The menu bar of terminal frames is treated specially as far as
19625 glyph matrices are concerned. Menu bar lines are not part of
19626 windows, so the update is done directly on the frame matrix rows
19627 for the menu bar. */
19629 static void
19630 display_menu_bar (struct window *w)
19632 struct frame *f = XFRAME (WINDOW_FRAME (w));
19633 struct it it;
19634 Lisp_Object items;
19635 int i;
19637 /* Don't do all this for graphical frames. */
19638 #ifdef HAVE_NTGUI
19639 if (FRAME_W32_P (f))
19640 return;
19641 #endif
19642 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
19643 if (FRAME_X_P (f))
19644 return;
19645 #endif
19647 #ifdef HAVE_NS
19648 if (FRAME_NS_P (f))
19649 return;
19650 #endif /* HAVE_NS */
19652 #ifdef USE_X_TOOLKIT
19653 xassert (!FRAME_WINDOW_P (f));
19654 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
19655 it.first_visible_x = 0;
19656 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19657 #else /* not USE_X_TOOLKIT */
19658 if (FRAME_WINDOW_P (f))
19660 /* Menu bar lines are displayed in the desired matrix of the
19661 dummy window menu_bar_window. */
19662 struct window *menu_w;
19663 xassert (WINDOWP (f->menu_bar_window));
19664 menu_w = XWINDOW (f->menu_bar_window);
19665 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
19666 MENU_FACE_ID);
19667 it.first_visible_x = 0;
19668 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19670 else
19672 /* This is a TTY frame, i.e. character hpos/vpos are used as
19673 pixel x/y. */
19674 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
19675 MENU_FACE_ID);
19676 it.first_visible_x = 0;
19677 it.last_visible_x = FRAME_COLS (f);
19679 #endif /* not USE_X_TOOLKIT */
19681 /* FIXME: This should be controlled by a user option. See the
19682 comments in redisplay_tool_bar and display_mode_line about
19683 this. */
19684 it.paragraph_embedding = L2R;
19686 if (! mode_line_inverse_video)
19687 /* Force the menu-bar to be displayed in the default face. */
19688 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19690 /* Clear all rows of the menu bar. */
19691 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
19693 struct glyph_row *row = it.glyph_row + i;
19694 clear_glyph_row (row);
19695 row->enabled_p = 1;
19696 row->full_width_p = 1;
19699 /* Display all items of the menu bar. */
19700 items = FRAME_MENU_BAR_ITEMS (it.f);
19701 for (i = 0; i < ASIZE (items); i += 4)
19703 Lisp_Object string;
19705 /* Stop at nil string. */
19706 string = AREF (items, i + 1);
19707 if (NILP (string))
19708 break;
19710 /* Remember where item was displayed. */
19711 ASET (items, i + 3, make_number (it.hpos));
19713 /* Display the item, pad with one space. */
19714 if (it.current_x < it.last_visible_x)
19715 display_string (NULL, string, Qnil, 0, 0, &it,
19716 SCHARS (string) + 1, 0, 0, -1);
19719 /* Fill out the line with spaces. */
19720 if (it.current_x < it.last_visible_x)
19721 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
19723 /* Compute the total height of the lines. */
19724 compute_line_metrics (&it);
19729 /***********************************************************************
19730 Mode Line
19731 ***********************************************************************/
19733 /* Redisplay mode lines in the window tree whose root is WINDOW. If
19734 FORCE is non-zero, redisplay mode lines unconditionally.
19735 Otherwise, redisplay only mode lines that are garbaged. Value is
19736 the number of windows whose mode lines were redisplayed. */
19738 static int
19739 redisplay_mode_lines (Lisp_Object window, int force)
19741 int nwindows = 0;
19743 while (!NILP (window))
19745 struct window *w = XWINDOW (window);
19747 if (WINDOWP (w->hchild))
19748 nwindows += redisplay_mode_lines (w->hchild, force);
19749 else if (WINDOWP (w->vchild))
19750 nwindows += redisplay_mode_lines (w->vchild, force);
19751 else if (force
19752 || FRAME_GARBAGED_P (XFRAME (w->frame))
19753 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
19755 struct text_pos lpoint;
19756 struct buffer *old = current_buffer;
19758 /* Set the window's buffer for the mode line display. */
19759 SET_TEXT_POS (lpoint, PT, PT_BYTE);
19760 set_buffer_internal_1 (XBUFFER (w->buffer));
19762 /* Point refers normally to the selected window. For any
19763 other window, set up appropriate value. */
19764 if (!EQ (window, selected_window))
19766 struct text_pos pt;
19768 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
19769 if (CHARPOS (pt) < BEGV)
19770 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
19771 else if (CHARPOS (pt) > (ZV - 1))
19772 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
19773 else
19774 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
19777 /* Display mode lines. */
19778 clear_glyph_matrix (w->desired_matrix);
19779 if (display_mode_lines (w))
19781 ++nwindows;
19782 w->must_be_updated_p = 1;
19785 /* Restore old settings. */
19786 set_buffer_internal_1 (old);
19787 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
19790 window = w->next;
19793 return nwindows;
19797 /* Display the mode and/or header line of window W. Value is the
19798 sum number of mode lines and header lines displayed. */
19800 static int
19801 display_mode_lines (struct window *w)
19803 Lisp_Object old_selected_window, old_selected_frame;
19804 int n = 0;
19806 old_selected_frame = selected_frame;
19807 selected_frame = w->frame;
19808 old_selected_window = selected_window;
19809 XSETWINDOW (selected_window, w);
19811 /* These will be set while the mode line specs are processed. */
19812 line_number_displayed = 0;
19813 w->column_number_displayed = Qnil;
19815 if (WINDOW_WANTS_MODELINE_P (w))
19817 struct window *sel_w = XWINDOW (old_selected_window);
19819 /* Select mode line face based on the real selected window. */
19820 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
19821 BVAR (current_buffer, mode_line_format));
19822 ++n;
19825 if (WINDOW_WANTS_HEADER_LINE_P (w))
19827 display_mode_line (w, HEADER_LINE_FACE_ID,
19828 BVAR (current_buffer, header_line_format));
19829 ++n;
19832 selected_frame = old_selected_frame;
19833 selected_window = old_selected_window;
19834 return n;
19838 /* Display mode or header line of window W. FACE_ID specifies which
19839 line to display; it is either MODE_LINE_FACE_ID or
19840 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
19841 display. Value is the pixel height of the mode/header line
19842 displayed. */
19844 static int
19845 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
19847 struct it it;
19848 struct face *face;
19849 int count = SPECPDL_INDEX ();
19851 init_iterator (&it, w, -1, -1, NULL, face_id);
19852 /* Don't extend on a previously drawn mode-line.
19853 This may happen if called from pos_visible_p. */
19854 it.glyph_row->enabled_p = 0;
19855 prepare_desired_row (it.glyph_row);
19857 it.glyph_row->mode_line_p = 1;
19859 if (! mode_line_inverse_video)
19860 /* Force the mode-line to be displayed in the default face. */
19861 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19863 /* FIXME: This should be controlled by a user option. But
19864 supporting such an option is not trivial, since the mode line is
19865 made up of many separate strings. */
19866 it.paragraph_embedding = L2R;
19868 record_unwind_protect (unwind_format_mode_line,
19869 format_mode_line_unwind_data (NULL, Qnil, 0));
19871 mode_line_target = MODE_LINE_DISPLAY;
19873 /* Temporarily make frame's keyboard the current kboard so that
19874 kboard-local variables in the mode_line_format will get the right
19875 values. */
19876 push_kboard (FRAME_KBOARD (it.f));
19877 record_unwind_save_match_data ();
19878 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19879 pop_kboard ();
19881 unbind_to (count, Qnil);
19883 /* Fill up with spaces. */
19884 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
19886 compute_line_metrics (&it);
19887 it.glyph_row->full_width_p = 1;
19888 it.glyph_row->continued_p = 0;
19889 it.glyph_row->truncated_on_left_p = 0;
19890 it.glyph_row->truncated_on_right_p = 0;
19892 /* Make a 3D mode-line have a shadow at its right end. */
19893 face = FACE_FROM_ID (it.f, face_id);
19894 extend_face_to_end_of_line (&it);
19895 if (face->box != FACE_NO_BOX)
19897 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
19898 + it.glyph_row->used[TEXT_AREA] - 1);
19899 last->right_box_line_p = 1;
19902 return it.glyph_row->height;
19905 /* Move element ELT in LIST to the front of LIST.
19906 Return the updated list. */
19908 static Lisp_Object
19909 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
19911 register Lisp_Object tail, prev;
19912 register Lisp_Object tem;
19914 tail = list;
19915 prev = Qnil;
19916 while (CONSP (tail))
19918 tem = XCAR (tail);
19920 if (EQ (elt, tem))
19922 /* Splice out the link TAIL. */
19923 if (NILP (prev))
19924 list = XCDR (tail);
19925 else
19926 Fsetcdr (prev, XCDR (tail));
19928 /* Now make it the first. */
19929 Fsetcdr (tail, list);
19930 return tail;
19932 else
19933 prev = tail;
19934 tail = XCDR (tail);
19935 QUIT;
19938 /* Not found--return unchanged LIST. */
19939 return list;
19942 /* Contribute ELT to the mode line for window IT->w. How it
19943 translates into text depends on its data type.
19945 IT describes the display environment in which we display, as usual.
19947 DEPTH is the depth in recursion. It is used to prevent
19948 infinite recursion here.
19950 FIELD_WIDTH is the number of characters the display of ELT should
19951 occupy in the mode line, and PRECISION is the maximum number of
19952 characters to display from ELT's representation. See
19953 display_string for details.
19955 Returns the hpos of the end of the text generated by ELT.
19957 PROPS is a property list to add to any string we encounter.
19959 If RISKY is nonzero, remove (disregard) any properties in any string
19960 we encounter, and ignore :eval and :propertize.
19962 The global variable `mode_line_target' determines whether the
19963 output is passed to `store_mode_line_noprop',
19964 `store_mode_line_string', or `display_string'. */
19966 static int
19967 display_mode_element (struct it *it, int depth, int field_width, int precision,
19968 Lisp_Object elt, Lisp_Object props, int risky)
19970 int n = 0, field, prec;
19971 int literal = 0;
19973 tail_recurse:
19974 if (depth > 100)
19975 elt = build_string ("*too-deep*");
19977 depth++;
19979 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
19981 case Lisp_String:
19983 /* A string: output it and check for %-constructs within it. */
19984 unsigned char c;
19985 EMACS_INT offset = 0;
19987 if (SCHARS (elt) > 0
19988 && (!NILP (props) || risky))
19990 Lisp_Object oprops, aelt;
19991 oprops = Ftext_properties_at (make_number (0), elt);
19993 /* If the starting string's properties are not what
19994 we want, translate the string. Also, if the string
19995 is risky, do that anyway. */
19997 if (NILP (Fequal (props, oprops)) || risky)
19999 /* If the starting string has properties,
20000 merge the specified ones onto the existing ones. */
20001 if (! NILP (oprops) && !risky)
20003 Lisp_Object tem;
20005 oprops = Fcopy_sequence (oprops);
20006 tem = props;
20007 while (CONSP (tem))
20009 oprops = Fplist_put (oprops, XCAR (tem),
20010 XCAR (XCDR (tem)));
20011 tem = XCDR (XCDR (tem));
20013 props = oprops;
20016 aelt = Fassoc (elt, mode_line_proptrans_alist);
20017 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20019 /* AELT is what we want. Move it to the front
20020 without consing. */
20021 elt = XCAR (aelt);
20022 mode_line_proptrans_alist
20023 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20025 else
20027 Lisp_Object tem;
20029 /* If AELT has the wrong props, it is useless.
20030 so get rid of it. */
20031 if (! NILP (aelt))
20032 mode_line_proptrans_alist
20033 = Fdelq (aelt, mode_line_proptrans_alist);
20035 elt = Fcopy_sequence (elt);
20036 Fset_text_properties (make_number (0), Flength (elt),
20037 props, elt);
20038 /* Add this item to mode_line_proptrans_alist. */
20039 mode_line_proptrans_alist
20040 = Fcons (Fcons (elt, props),
20041 mode_line_proptrans_alist);
20042 /* Truncate mode_line_proptrans_alist
20043 to at most 50 elements. */
20044 tem = Fnthcdr (make_number (50),
20045 mode_line_proptrans_alist);
20046 if (! NILP (tem))
20047 XSETCDR (tem, Qnil);
20052 offset = 0;
20054 if (literal)
20056 prec = precision - n;
20057 switch (mode_line_target)
20059 case MODE_LINE_NOPROP:
20060 case MODE_LINE_TITLE:
20061 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20062 break;
20063 case MODE_LINE_STRING:
20064 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20065 break;
20066 case MODE_LINE_DISPLAY:
20067 n += display_string (NULL, elt, Qnil, 0, 0, it,
20068 0, prec, 0, STRING_MULTIBYTE (elt));
20069 break;
20072 break;
20075 /* Handle the non-literal case. */
20077 while ((precision <= 0 || n < precision)
20078 && SREF (elt, offset) != 0
20079 && (mode_line_target != MODE_LINE_DISPLAY
20080 || it->current_x < it->last_visible_x))
20082 EMACS_INT last_offset = offset;
20084 /* Advance to end of string or next format specifier. */
20085 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20088 if (offset - 1 != last_offset)
20090 EMACS_INT nchars, nbytes;
20092 /* Output to end of string or up to '%'. Field width
20093 is length of string. Don't output more than
20094 PRECISION allows us. */
20095 offset--;
20097 prec = c_string_width (SDATA (elt) + last_offset,
20098 offset - last_offset, precision - n,
20099 &nchars, &nbytes);
20101 switch (mode_line_target)
20103 case MODE_LINE_NOPROP:
20104 case MODE_LINE_TITLE:
20105 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20106 break;
20107 case MODE_LINE_STRING:
20109 EMACS_INT bytepos = last_offset;
20110 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
20111 EMACS_INT endpos = (precision <= 0
20112 ? string_byte_to_char (elt, offset)
20113 : charpos + nchars);
20115 n += store_mode_line_string (NULL,
20116 Fsubstring (elt, make_number (charpos),
20117 make_number (endpos)),
20118 0, 0, 0, Qnil);
20120 break;
20121 case MODE_LINE_DISPLAY:
20123 EMACS_INT bytepos = last_offset;
20124 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
20126 if (precision <= 0)
20127 nchars = string_byte_to_char (elt, offset) - charpos;
20128 n += display_string (NULL, elt, Qnil, 0, charpos,
20129 it, 0, nchars, 0,
20130 STRING_MULTIBYTE (elt));
20132 break;
20135 else /* c == '%' */
20137 EMACS_INT percent_position = offset;
20139 /* Get the specified minimum width. Zero means
20140 don't pad. */
20141 field = 0;
20142 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20143 field = field * 10 + c - '0';
20145 /* Don't pad beyond the total padding allowed. */
20146 if (field_width - n > 0 && field > field_width - n)
20147 field = field_width - n;
20149 /* Note that either PRECISION <= 0 or N < PRECISION. */
20150 prec = precision - n;
20152 if (c == 'M')
20153 n += display_mode_element (it, depth, field, prec,
20154 Vglobal_mode_string, props,
20155 risky);
20156 else if (c != 0)
20158 int multibyte;
20159 EMACS_INT bytepos, charpos;
20160 const char *spec;
20161 Lisp_Object string;
20163 bytepos = percent_position;
20164 charpos = (STRING_MULTIBYTE (elt)
20165 ? string_byte_to_char (elt, bytepos)
20166 : bytepos);
20167 spec = decode_mode_spec (it->w, c, field, &string);
20168 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20170 switch (mode_line_target)
20172 case MODE_LINE_NOPROP:
20173 case MODE_LINE_TITLE:
20174 n += store_mode_line_noprop (spec, field, prec);
20175 break;
20176 case MODE_LINE_STRING:
20178 Lisp_Object tem = build_string (spec);
20179 props = Ftext_properties_at (make_number (charpos), elt);
20180 /* Should only keep face property in props */
20181 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20183 break;
20184 case MODE_LINE_DISPLAY:
20186 int nglyphs_before, nwritten;
20188 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20189 nwritten = display_string (spec, string, elt,
20190 charpos, 0, it,
20191 field, prec, 0,
20192 multibyte);
20194 /* Assign to the glyphs written above the
20195 string where the `%x' came from, position
20196 of the `%'. */
20197 if (nwritten > 0)
20199 struct glyph *glyph
20200 = (it->glyph_row->glyphs[TEXT_AREA]
20201 + nglyphs_before);
20202 int i;
20204 for (i = 0; i < nwritten; ++i)
20206 glyph[i].object = elt;
20207 glyph[i].charpos = charpos;
20210 n += nwritten;
20213 break;
20216 else /* c == 0 */
20217 break;
20221 break;
20223 case Lisp_Symbol:
20224 /* A symbol: process the value of the symbol recursively
20225 as if it appeared here directly. Avoid error if symbol void.
20226 Special case: if value of symbol is a string, output the string
20227 literally. */
20229 register Lisp_Object tem;
20231 /* If the variable is not marked as risky to set
20232 then its contents are risky to use. */
20233 if (NILP (Fget (elt, Qrisky_local_variable)))
20234 risky = 1;
20236 tem = Fboundp (elt);
20237 if (!NILP (tem))
20239 tem = Fsymbol_value (elt);
20240 /* If value is a string, output that string literally:
20241 don't check for % within it. */
20242 if (STRINGP (tem))
20243 literal = 1;
20245 if (!EQ (tem, elt))
20247 /* Give up right away for nil or t. */
20248 elt = tem;
20249 goto tail_recurse;
20253 break;
20255 case Lisp_Cons:
20257 register Lisp_Object car, tem;
20259 /* A cons cell: five distinct cases.
20260 If first element is :eval or :propertize, do something special.
20261 If first element is a string or a cons, process all the elements
20262 and effectively concatenate them.
20263 If first element is a negative number, truncate displaying cdr to
20264 at most that many characters. If positive, pad (with spaces)
20265 to at least that many characters.
20266 If first element is a symbol, process the cadr or caddr recursively
20267 according to whether the symbol's value is non-nil or nil. */
20268 car = XCAR (elt);
20269 if (EQ (car, QCeval))
20271 /* An element of the form (:eval FORM) means evaluate FORM
20272 and use the result as mode line elements. */
20274 if (risky)
20275 break;
20277 if (CONSP (XCDR (elt)))
20279 Lisp_Object spec;
20280 spec = safe_eval (XCAR (XCDR (elt)));
20281 n += display_mode_element (it, depth, field_width - n,
20282 precision - n, spec, props,
20283 risky);
20286 else if (EQ (car, QCpropertize))
20288 /* An element of the form (:propertize ELT PROPS...)
20289 means display ELT but applying properties PROPS. */
20291 if (risky)
20292 break;
20294 if (CONSP (XCDR (elt)))
20295 n += display_mode_element (it, depth, field_width - n,
20296 precision - n, XCAR (XCDR (elt)),
20297 XCDR (XCDR (elt)), risky);
20299 else if (SYMBOLP (car))
20301 tem = Fboundp (car);
20302 elt = XCDR (elt);
20303 if (!CONSP (elt))
20304 goto invalid;
20305 /* elt is now the cdr, and we know it is a cons cell.
20306 Use its car if CAR has a non-nil value. */
20307 if (!NILP (tem))
20309 tem = Fsymbol_value (car);
20310 if (!NILP (tem))
20312 elt = XCAR (elt);
20313 goto tail_recurse;
20316 /* Symbol's value is nil (or symbol is unbound)
20317 Get the cddr of the original list
20318 and if possible find the caddr and use that. */
20319 elt = XCDR (elt);
20320 if (NILP (elt))
20321 break;
20322 else if (!CONSP (elt))
20323 goto invalid;
20324 elt = XCAR (elt);
20325 goto tail_recurse;
20327 else if (INTEGERP (car))
20329 register int lim = XINT (car);
20330 elt = XCDR (elt);
20331 if (lim < 0)
20333 /* Negative int means reduce maximum width. */
20334 if (precision <= 0)
20335 precision = -lim;
20336 else
20337 precision = min (precision, -lim);
20339 else if (lim > 0)
20341 /* Padding specified. Don't let it be more than
20342 current maximum. */
20343 if (precision > 0)
20344 lim = min (precision, lim);
20346 /* If that's more padding than already wanted, queue it.
20347 But don't reduce padding already specified even if
20348 that is beyond the current truncation point. */
20349 field_width = max (lim, field_width);
20351 goto tail_recurse;
20353 else if (STRINGP (car) || CONSP (car))
20355 Lisp_Object halftail = elt;
20356 int len = 0;
20358 while (CONSP (elt)
20359 && (precision <= 0 || n < precision))
20361 n += display_mode_element (it, depth,
20362 /* Do padding only after the last
20363 element in the list. */
20364 (! CONSP (XCDR (elt))
20365 ? field_width - n
20366 : 0),
20367 precision - n, XCAR (elt),
20368 props, risky);
20369 elt = XCDR (elt);
20370 len++;
20371 if ((len & 1) == 0)
20372 halftail = XCDR (halftail);
20373 /* Check for cycle. */
20374 if (EQ (halftail, elt))
20375 break;
20379 break;
20381 default:
20382 invalid:
20383 elt = build_string ("*invalid*");
20384 goto tail_recurse;
20387 /* Pad to FIELD_WIDTH. */
20388 if (field_width > 0 && n < field_width)
20390 switch (mode_line_target)
20392 case MODE_LINE_NOPROP:
20393 case MODE_LINE_TITLE:
20394 n += store_mode_line_noprop ("", field_width - n, 0);
20395 break;
20396 case MODE_LINE_STRING:
20397 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20398 break;
20399 case MODE_LINE_DISPLAY:
20400 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20401 0, 0, 0);
20402 break;
20406 return n;
20409 /* Store a mode-line string element in mode_line_string_list.
20411 If STRING is non-null, display that C string. Otherwise, the Lisp
20412 string LISP_STRING is displayed.
20414 FIELD_WIDTH is the minimum number of output glyphs to produce.
20415 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20416 with spaces. FIELD_WIDTH <= 0 means don't pad.
20418 PRECISION is the maximum number of characters to output from
20419 STRING. PRECISION <= 0 means don't truncate the string.
20421 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20422 properties to the string.
20424 PROPS are the properties to add to the string.
20425 The mode_line_string_face face property is always added to the string.
20428 static int
20429 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20430 int field_width, int precision, Lisp_Object props)
20432 EMACS_INT len;
20433 int n = 0;
20435 if (string != NULL)
20437 len = strlen (string);
20438 if (precision > 0 && len > precision)
20439 len = precision;
20440 lisp_string = make_string (string, len);
20441 if (NILP (props))
20442 props = mode_line_string_face_prop;
20443 else if (!NILP (mode_line_string_face))
20445 Lisp_Object face = Fplist_get (props, Qface);
20446 props = Fcopy_sequence (props);
20447 if (NILP (face))
20448 face = mode_line_string_face;
20449 else
20450 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20451 props = Fplist_put (props, Qface, face);
20453 Fadd_text_properties (make_number (0), make_number (len),
20454 props, lisp_string);
20456 else
20458 len = XFASTINT (Flength (lisp_string));
20459 if (precision > 0 && len > precision)
20461 len = precision;
20462 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20463 precision = -1;
20465 if (!NILP (mode_line_string_face))
20467 Lisp_Object face;
20468 if (NILP (props))
20469 props = Ftext_properties_at (make_number (0), lisp_string);
20470 face = Fplist_get (props, Qface);
20471 if (NILP (face))
20472 face = mode_line_string_face;
20473 else
20474 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20475 props = Fcons (Qface, Fcons (face, Qnil));
20476 if (copy_string)
20477 lisp_string = Fcopy_sequence (lisp_string);
20479 if (!NILP (props))
20480 Fadd_text_properties (make_number (0), make_number (len),
20481 props, lisp_string);
20484 if (len > 0)
20486 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20487 n += len;
20490 if (field_width > len)
20492 field_width -= len;
20493 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20494 if (!NILP (props))
20495 Fadd_text_properties (make_number (0), make_number (field_width),
20496 props, lisp_string);
20497 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20498 n += field_width;
20501 return n;
20505 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20506 1, 4, 0,
20507 doc: /* Format a string out of a mode line format specification.
20508 First arg FORMAT specifies the mode line format (see `mode-line-format'
20509 for details) to use.
20511 By default, the format is evaluated for the currently selected window.
20513 Optional second arg FACE specifies the face property to put on all
20514 characters for which no face is specified. The value nil means the
20515 default face. The value t means whatever face the window's mode line
20516 currently uses (either `mode-line' or `mode-line-inactive',
20517 depending on whether the window is the selected window or not).
20518 An integer value means the value string has no text
20519 properties.
20521 Optional third and fourth args WINDOW and BUFFER specify the window
20522 and buffer to use as the context for the formatting (defaults
20523 are the selected window and the WINDOW's buffer). */)
20524 (Lisp_Object format, Lisp_Object face,
20525 Lisp_Object window, Lisp_Object buffer)
20527 struct it it;
20528 int len;
20529 struct window *w;
20530 struct buffer *old_buffer = NULL;
20531 int face_id;
20532 int no_props = INTEGERP (face);
20533 int count = SPECPDL_INDEX ();
20534 Lisp_Object str;
20535 int string_start = 0;
20537 if (NILP (window))
20538 window = selected_window;
20539 CHECK_WINDOW (window);
20540 w = XWINDOW (window);
20542 if (NILP (buffer))
20543 buffer = w->buffer;
20544 CHECK_BUFFER (buffer);
20546 /* Make formatting the modeline a non-op when noninteractive, otherwise
20547 there will be problems later caused by a partially initialized frame. */
20548 if (NILP (format) || noninteractive)
20549 return empty_unibyte_string;
20551 if (no_props)
20552 face = Qnil;
20554 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20555 : EQ (face, Qt) ? (EQ (window, selected_window)
20556 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20557 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20558 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20559 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20560 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20561 : DEFAULT_FACE_ID;
20563 if (XBUFFER (buffer) != current_buffer)
20564 old_buffer = current_buffer;
20566 /* Save things including mode_line_proptrans_alist,
20567 and set that to nil so that we don't alter the outer value. */
20568 record_unwind_protect (unwind_format_mode_line,
20569 format_mode_line_unwind_data
20570 (old_buffer, selected_window, 1));
20571 mode_line_proptrans_alist = Qnil;
20573 Fselect_window (window, Qt);
20574 if (old_buffer)
20575 set_buffer_internal_1 (XBUFFER (buffer));
20577 init_iterator (&it, w, -1, -1, NULL, face_id);
20579 if (no_props)
20581 mode_line_target = MODE_LINE_NOPROP;
20582 mode_line_string_face_prop = Qnil;
20583 mode_line_string_list = Qnil;
20584 string_start = MODE_LINE_NOPROP_LEN (0);
20586 else
20588 mode_line_target = MODE_LINE_STRING;
20589 mode_line_string_list = Qnil;
20590 mode_line_string_face = face;
20591 mode_line_string_face_prop
20592 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20595 push_kboard (FRAME_KBOARD (it.f));
20596 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20597 pop_kboard ();
20599 if (no_props)
20601 len = MODE_LINE_NOPROP_LEN (string_start);
20602 str = make_string (mode_line_noprop_buf + string_start, len);
20604 else
20606 mode_line_string_list = Fnreverse (mode_line_string_list);
20607 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20608 empty_unibyte_string);
20611 unbind_to (count, Qnil);
20612 return str;
20615 /* Write a null-terminated, right justified decimal representation of
20616 the positive integer D to BUF using a minimal field width WIDTH. */
20618 static void
20619 pint2str (register char *buf, register int width, register EMACS_INT d)
20621 register char *p = buf;
20623 if (d <= 0)
20624 *p++ = '0';
20625 else
20627 while (d > 0)
20629 *p++ = d % 10 + '0';
20630 d /= 10;
20634 for (width -= (int) (p - buf); width > 0; --width)
20635 *p++ = ' ';
20636 *p-- = '\0';
20637 while (p > buf)
20639 d = *buf;
20640 *buf++ = *p;
20641 *p-- = d;
20645 /* Write a null-terminated, right justified decimal and "human
20646 readable" representation of the nonnegative integer D to BUF using
20647 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20649 static const char power_letter[] =
20651 0, /* no letter */
20652 'k', /* kilo */
20653 'M', /* mega */
20654 'G', /* giga */
20655 'T', /* tera */
20656 'P', /* peta */
20657 'E', /* exa */
20658 'Z', /* zetta */
20659 'Y' /* yotta */
20662 static void
20663 pint2hrstr (char *buf, int width, EMACS_INT d)
20665 /* We aim to represent the nonnegative integer D as
20666 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
20667 EMACS_INT quotient = d;
20668 int remainder = 0;
20669 /* -1 means: do not use TENTHS. */
20670 int tenths = -1;
20671 int exponent = 0;
20673 /* Length of QUOTIENT.TENTHS as a string. */
20674 int length;
20676 char * psuffix;
20677 char * p;
20679 if (1000 <= quotient)
20681 /* Scale to the appropriate EXPONENT. */
20684 remainder = quotient % 1000;
20685 quotient /= 1000;
20686 exponent++;
20688 while (1000 <= quotient);
20690 /* Round to nearest and decide whether to use TENTHS or not. */
20691 if (quotient <= 9)
20693 tenths = remainder / 100;
20694 if (50 <= remainder % 100)
20696 if (tenths < 9)
20697 tenths++;
20698 else
20700 quotient++;
20701 if (quotient == 10)
20702 tenths = -1;
20703 else
20704 tenths = 0;
20708 else
20709 if (500 <= remainder)
20711 if (quotient < 999)
20712 quotient++;
20713 else
20715 quotient = 1;
20716 exponent++;
20717 tenths = 0;
20722 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
20723 if (tenths == -1 && quotient <= 99)
20724 if (quotient <= 9)
20725 length = 1;
20726 else
20727 length = 2;
20728 else
20729 length = 3;
20730 p = psuffix = buf + max (width, length);
20732 /* Print EXPONENT. */
20733 *psuffix++ = power_letter[exponent];
20734 *psuffix = '\0';
20736 /* Print TENTHS. */
20737 if (tenths >= 0)
20739 *--p = '0' + tenths;
20740 *--p = '.';
20743 /* Print QUOTIENT. */
20746 int digit = quotient % 10;
20747 *--p = '0' + digit;
20749 while ((quotient /= 10) != 0);
20751 /* Print leading spaces. */
20752 while (buf < p)
20753 *--p = ' ';
20756 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
20757 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
20758 type of CODING_SYSTEM. Return updated pointer into BUF. */
20760 static unsigned char invalid_eol_type[] = "(*invalid*)";
20762 static char *
20763 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
20765 Lisp_Object val;
20766 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
20767 const unsigned char *eol_str;
20768 int eol_str_len;
20769 /* The EOL conversion we are using. */
20770 Lisp_Object eoltype;
20772 val = CODING_SYSTEM_SPEC (coding_system);
20773 eoltype = Qnil;
20775 if (!VECTORP (val)) /* Not yet decided. */
20777 if (multibyte)
20778 *buf++ = '-';
20779 if (eol_flag)
20780 eoltype = eol_mnemonic_undecided;
20781 /* Don't mention EOL conversion if it isn't decided. */
20783 else
20785 Lisp_Object attrs;
20786 Lisp_Object eolvalue;
20788 attrs = AREF (val, 0);
20789 eolvalue = AREF (val, 2);
20791 if (multibyte)
20792 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
20794 if (eol_flag)
20796 /* The EOL conversion that is normal on this system. */
20798 if (NILP (eolvalue)) /* Not yet decided. */
20799 eoltype = eol_mnemonic_undecided;
20800 else if (VECTORP (eolvalue)) /* Not yet decided. */
20801 eoltype = eol_mnemonic_undecided;
20802 else /* eolvalue is Qunix, Qdos, or Qmac. */
20803 eoltype = (EQ (eolvalue, Qunix)
20804 ? eol_mnemonic_unix
20805 : (EQ (eolvalue, Qdos) == 1
20806 ? eol_mnemonic_dos : eol_mnemonic_mac));
20810 if (eol_flag)
20812 /* Mention the EOL conversion if it is not the usual one. */
20813 if (STRINGP (eoltype))
20815 eol_str = SDATA (eoltype);
20816 eol_str_len = SBYTES (eoltype);
20818 else if (CHARACTERP (eoltype))
20820 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
20821 int c = XFASTINT (eoltype);
20822 eol_str_len = CHAR_STRING (c, tmp);
20823 eol_str = tmp;
20825 else
20827 eol_str = invalid_eol_type;
20828 eol_str_len = sizeof (invalid_eol_type) - 1;
20830 memcpy (buf, eol_str, eol_str_len);
20831 buf += eol_str_len;
20834 return buf;
20837 /* Return a string for the output of a mode line %-spec for window W,
20838 generated by character C. FIELD_WIDTH > 0 means pad the string
20839 returned with spaces to that value. Return a Lisp string in
20840 *STRING if the resulting string is taken from that Lisp string.
20842 Note we operate on the current buffer for most purposes,
20843 the exception being w->base_line_pos. */
20845 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
20847 static const char *
20848 decode_mode_spec (struct window *w, register int c, int field_width,
20849 Lisp_Object *string)
20851 Lisp_Object obj;
20852 struct frame *f = XFRAME (WINDOW_FRAME (w));
20853 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
20854 struct buffer *b = current_buffer;
20856 obj = Qnil;
20857 *string = Qnil;
20859 switch (c)
20861 case '*':
20862 if (!NILP (BVAR (b, read_only)))
20863 return "%";
20864 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20865 return "*";
20866 return "-";
20868 case '+':
20869 /* This differs from %* only for a modified read-only buffer. */
20870 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20871 return "*";
20872 if (!NILP (BVAR (b, read_only)))
20873 return "%";
20874 return "-";
20876 case '&':
20877 /* This differs from %* in ignoring read-only-ness. */
20878 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20879 return "*";
20880 return "-";
20882 case '%':
20883 return "%";
20885 case '[':
20887 int i;
20888 char *p;
20890 if (command_loop_level > 5)
20891 return "[[[... ";
20892 p = decode_mode_spec_buf;
20893 for (i = 0; i < command_loop_level; i++)
20894 *p++ = '[';
20895 *p = 0;
20896 return decode_mode_spec_buf;
20899 case ']':
20901 int i;
20902 char *p;
20904 if (command_loop_level > 5)
20905 return " ...]]]";
20906 p = decode_mode_spec_buf;
20907 for (i = 0; i < command_loop_level; i++)
20908 *p++ = ']';
20909 *p = 0;
20910 return decode_mode_spec_buf;
20913 case '-':
20915 register int i;
20917 /* Let lots_of_dashes be a string of infinite length. */
20918 if (mode_line_target == MODE_LINE_NOPROP ||
20919 mode_line_target == MODE_LINE_STRING)
20920 return "--";
20921 if (field_width <= 0
20922 || field_width > sizeof (lots_of_dashes))
20924 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
20925 decode_mode_spec_buf[i] = '-';
20926 decode_mode_spec_buf[i] = '\0';
20927 return decode_mode_spec_buf;
20929 else
20930 return lots_of_dashes;
20933 case 'b':
20934 obj = BVAR (b, name);
20935 break;
20937 case 'c':
20938 /* %c and %l are ignored in `frame-title-format'.
20939 (In redisplay_internal, the frame title is drawn _before_ the
20940 windows are updated, so the stuff which depends on actual
20941 window contents (such as %l) may fail to render properly, or
20942 even crash emacs.) */
20943 if (mode_line_target == MODE_LINE_TITLE)
20944 return "";
20945 else
20947 EMACS_INT col = current_column ();
20948 w->column_number_displayed = make_number (col);
20949 pint2str (decode_mode_spec_buf, field_width, col);
20950 return decode_mode_spec_buf;
20953 case 'e':
20954 #ifndef SYSTEM_MALLOC
20956 if (NILP (Vmemory_full))
20957 return "";
20958 else
20959 return "!MEM FULL! ";
20961 #else
20962 return "";
20963 #endif
20965 case 'F':
20966 /* %F displays the frame name. */
20967 if (!NILP (f->title))
20968 return SSDATA (f->title);
20969 if (f->explicit_name || ! FRAME_WINDOW_P (f))
20970 return SSDATA (f->name);
20971 return "Emacs";
20973 case 'f':
20974 obj = BVAR (b, filename);
20975 break;
20977 case 'i':
20979 EMACS_INT size = ZV - BEGV;
20980 pint2str (decode_mode_spec_buf, field_width, size);
20981 return decode_mode_spec_buf;
20984 case 'I':
20986 EMACS_INT size = ZV - BEGV;
20987 pint2hrstr (decode_mode_spec_buf, field_width, size);
20988 return decode_mode_spec_buf;
20991 case 'l':
20993 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
20994 EMACS_INT topline, nlines, height;
20995 EMACS_INT junk;
20997 /* %c and %l are ignored in `frame-title-format'. */
20998 if (mode_line_target == MODE_LINE_TITLE)
20999 return "";
21001 startpos = XMARKER (w->start)->charpos;
21002 startpos_byte = marker_byte_position (w->start);
21003 height = WINDOW_TOTAL_LINES (w);
21005 /* If we decided that this buffer isn't suitable for line numbers,
21006 don't forget that too fast. */
21007 if (EQ (w->base_line_pos, w->buffer))
21008 goto no_value;
21009 /* But do forget it, if the window shows a different buffer now. */
21010 else if (BUFFERP (w->base_line_pos))
21011 w->base_line_pos = Qnil;
21013 /* If the buffer is very big, don't waste time. */
21014 if (INTEGERP (Vline_number_display_limit)
21015 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21017 w->base_line_pos = Qnil;
21018 w->base_line_number = Qnil;
21019 goto no_value;
21022 if (INTEGERP (w->base_line_number)
21023 && INTEGERP (w->base_line_pos)
21024 && XFASTINT (w->base_line_pos) <= startpos)
21026 line = XFASTINT (w->base_line_number);
21027 linepos = XFASTINT (w->base_line_pos);
21028 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21030 else
21032 line = 1;
21033 linepos = BUF_BEGV (b);
21034 linepos_byte = BUF_BEGV_BYTE (b);
21037 /* Count lines from base line to window start position. */
21038 nlines = display_count_lines (linepos_byte,
21039 startpos_byte,
21040 startpos, &junk);
21042 topline = nlines + line;
21044 /* Determine a new base line, if the old one is too close
21045 or too far away, or if we did not have one.
21046 "Too close" means it's plausible a scroll-down would
21047 go back past it. */
21048 if (startpos == BUF_BEGV (b))
21050 w->base_line_number = make_number (topline);
21051 w->base_line_pos = make_number (BUF_BEGV (b));
21053 else if (nlines < height + 25 || nlines > height * 3 + 50
21054 || linepos == BUF_BEGV (b))
21056 EMACS_INT limit = BUF_BEGV (b);
21057 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
21058 EMACS_INT position;
21059 EMACS_INT distance =
21060 (height * 2 + 30) * line_number_display_limit_width;
21062 if (startpos - distance > limit)
21064 limit = startpos - distance;
21065 limit_byte = CHAR_TO_BYTE (limit);
21068 nlines = display_count_lines (startpos_byte,
21069 limit_byte,
21070 - (height * 2 + 30),
21071 &position);
21072 /* If we couldn't find the lines we wanted within
21073 line_number_display_limit_width chars per line,
21074 give up on line numbers for this window. */
21075 if (position == limit_byte && limit == startpos - distance)
21077 w->base_line_pos = w->buffer;
21078 w->base_line_number = Qnil;
21079 goto no_value;
21082 w->base_line_number = make_number (topline - nlines);
21083 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
21086 /* Now count lines from the start pos to point. */
21087 nlines = display_count_lines (startpos_byte,
21088 PT_BYTE, PT, &junk);
21090 /* Record that we did display the line number. */
21091 line_number_displayed = 1;
21093 /* Make the string to show. */
21094 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21095 return decode_mode_spec_buf;
21096 no_value:
21098 char* p = decode_mode_spec_buf;
21099 int pad = field_width - 2;
21100 while (pad-- > 0)
21101 *p++ = ' ';
21102 *p++ = '?';
21103 *p++ = '?';
21104 *p = '\0';
21105 return decode_mode_spec_buf;
21108 break;
21110 case 'm':
21111 obj = BVAR (b, mode_name);
21112 break;
21114 case 'n':
21115 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21116 return " Narrow";
21117 break;
21119 case 'p':
21121 EMACS_INT pos = marker_position (w->start);
21122 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
21124 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21126 if (pos <= BUF_BEGV (b))
21127 return "All";
21128 else
21129 return "Bottom";
21131 else if (pos <= BUF_BEGV (b))
21132 return "Top";
21133 else
21135 if (total > 1000000)
21136 /* Do it differently for a large value, to avoid overflow. */
21137 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21138 else
21139 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21140 /* We can't normally display a 3-digit number,
21141 so get us a 2-digit number that is close. */
21142 if (total == 100)
21143 total = 99;
21144 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
21145 return decode_mode_spec_buf;
21149 /* Display percentage of size above the bottom of the screen. */
21150 case 'P':
21152 EMACS_INT toppos = marker_position (w->start);
21153 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21154 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
21156 if (botpos >= BUF_ZV (b))
21158 if (toppos <= BUF_BEGV (b))
21159 return "All";
21160 else
21161 return "Bottom";
21163 else
21165 if (total > 1000000)
21166 /* Do it differently for a large value, to avoid overflow. */
21167 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21168 else
21169 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21170 /* We can't normally display a 3-digit number,
21171 so get us a 2-digit number that is close. */
21172 if (total == 100)
21173 total = 99;
21174 if (toppos <= BUF_BEGV (b))
21175 sprintf (decode_mode_spec_buf, "Top%2"pI"d%%", total);
21176 else
21177 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
21178 return decode_mode_spec_buf;
21182 case 's':
21183 /* status of process */
21184 obj = Fget_buffer_process (Fcurrent_buffer ());
21185 if (NILP (obj))
21186 return "no process";
21187 #ifndef MSDOS
21188 obj = Fsymbol_name (Fprocess_status (obj));
21189 #endif
21190 break;
21192 case '@':
21194 int count = inhibit_garbage_collection ();
21195 Lisp_Object val = call1 (intern ("file-remote-p"),
21196 BVAR (current_buffer, directory));
21197 unbind_to (count, Qnil);
21199 if (NILP (val))
21200 return "-";
21201 else
21202 return "@";
21205 case 't': /* indicate TEXT or BINARY */
21206 return "T";
21208 case 'z':
21209 /* coding-system (not including end-of-line format) */
21210 case 'Z':
21211 /* coding-system (including end-of-line type) */
21213 int eol_flag = (c == 'Z');
21214 char *p = decode_mode_spec_buf;
21216 if (! FRAME_WINDOW_P (f))
21218 /* No need to mention EOL here--the terminal never needs
21219 to do EOL conversion. */
21220 p = decode_mode_spec_coding (CODING_ID_NAME
21221 (FRAME_KEYBOARD_CODING (f)->id),
21222 p, 0);
21223 p = decode_mode_spec_coding (CODING_ID_NAME
21224 (FRAME_TERMINAL_CODING (f)->id),
21225 p, 0);
21227 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21228 p, eol_flag);
21230 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21231 #ifdef subprocesses
21232 obj = Fget_buffer_process (Fcurrent_buffer ());
21233 if (PROCESSP (obj))
21235 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
21236 p, eol_flag);
21237 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
21238 p, eol_flag);
21240 #endif /* subprocesses */
21241 #endif /* 0 */
21242 *p = 0;
21243 return decode_mode_spec_buf;
21247 if (STRINGP (obj))
21249 *string = obj;
21250 return SSDATA (obj);
21252 else
21253 return "";
21257 /* Count up to COUNT lines starting from START_BYTE.
21258 But don't go beyond LIMIT_BYTE.
21259 Return the number of lines thus found (always nonnegative).
21261 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21263 static EMACS_INT
21264 display_count_lines (EMACS_INT start_byte,
21265 EMACS_INT limit_byte, EMACS_INT count,
21266 EMACS_INT *byte_pos_ptr)
21268 register unsigned char *cursor;
21269 unsigned char *base;
21271 register EMACS_INT ceiling;
21272 register unsigned char *ceiling_addr;
21273 EMACS_INT orig_count = count;
21275 /* If we are not in selective display mode,
21276 check only for newlines. */
21277 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21278 && !INTEGERP (BVAR (current_buffer, selective_display)));
21280 if (count > 0)
21282 while (start_byte < limit_byte)
21284 ceiling = BUFFER_CEILING_OF (start_byte);
21285 ceiling = min (limit_byte - 1, ceiling);
21286 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21287 base = (cursor = BYTE_POS_ADDR (start_byte));
21288 while (1)
21290 if (selective_display)
21291 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21293 else
21294 while (*cursor != '\n' && ++cursor != ceiling_addr)
21297 if (cursor != ceiling_addr)
21299 if (--count == 0)
21301 start_byte += cursor - base + 1;
21302 *byte_pos_ptr = start_byte;
21303 return orig_count;
21305 else
21306 if (++cursor == ceiling_addr)
21307 break;
21309 else
21310 break;
21312 start_byte += cursor - base;
21315 else
21317 while (start_byte > limit_byte)
21319 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21320 ceiling = max (limit_byte, ceiling);
21321 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21322 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21323 while (1)
21325 if (selective_display)
21326 while (--cursor != ceiling_addr
21327 && *cursor != '\n' && *cursor != 015)
21329 else
21330 while (--cursor != ceiling_addr && *cursor != '\n')
21333 if (cursor != ceiling_addr)
21335 if (++count == 0)
21337 start_byte += cursor - base + 1;
21338 *byte_pos_ptr = start_byte;
21339 /* When scanning backwards, we should
21340 not count the newline posterior to which we stop. */
21341 return - orig_count - 1;
21344 else
21345 break;
21347 /* Here we add 1 to compensate for the last decrement
21348 of CURSOR, which took it past the valid range. */
21349 start_byte += cursor - base + 1;
21353 *byte_pos_ptr = limit_byte;
21355 if (count < 0)
21356 return - orig_count + count;
21357 return orig_count - count;
21363 /***********************************************************************
21364 Displaying strings
21365 ***********************************************************************/
21367 /* Display a NUL-terminated string, starting with index START.
21369 If STRING is non-null, display that C string. Otherwise, the Lisp
21370 string LISP_STRING is displayed. There's a case that STRING is
21371 non-null and LISP_STRING is not nil. It means STRING is a string
21372 data of LISP_STRING. In that case, we display LISP_STRING while
21373 ignoring its text properties.
21375 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21376 FACE_STRING. Display STRING or LISP_STRING with the face at
21377 FACE_STRING_POS in FACE_STRING:
21379 Display the string in the environment given by IT, but use the
21380 standard display table, temporarily.
21382 FIELD_WIDTH is the minimum number of output glyphs to produce.
21383 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21384 with spaces. If STRING has more characters, more than FIELD_WIDTH
21385 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21387 PRECISION is the maximum number of characters to output from
21388 STRING. PRECISION < 0 means don't truncate the string.
21390 This is roughly equivalent to printf format specifiers:
21392 FIELD_WIDTH PRECISION PRINTF
21393 ----------------------------------------
21394 -1 -1 %s
21395 -1 10 %.10s
21396 10 -1 %10s
21397 20 10 %20.10s
21399 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21400 display them, and < 0 means obey the current buffer's value of
21401 enable_multibyte_characters.
21403 Value is the number of columns displayed. */
21405 static int
21406 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21407 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
21408 int field_width, int precision, int max_x, int multibyte)
21410 int hpos_at_start = it->hpos;
21411 int saved_face_id = it->face_id;
21412 struct glyph_row *row = it->glyph_row;
21413 EMACS_INT it_charpos;
21415 /* Initialize the iterator IT for iteration over STRING beginning
21416 with index START. */
21417 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21418 precision, field_width, multibyte);
21419 if (string && STRINGP (lisp_string))
21420 /* LISP_STRING is the one returned by decode_mode_spec. We should
21421 ignore its text properties. */
21422 it->stop_charpos = it->end_charpos;
21424 /* If displaying STRING, set up the face of the iterator from
21425 FACE_STRING, if that's given. */
21426 if (STRINGP (face_string))
21428 EMACS_INT endptr;
21429 struct face *face;
21431 it->face_id
21432 = face_at_string_position (it->w, face_string, face_string_pos,
21433 0, it->region_beg_charpos,
21434 it->region_end_charpos,
21435 &endptr, it->base_face_id, 0);
21436 face = FACE_FROM_ID (it->f, it->face_id);
21437 it->face_box_p = face->box != FACE_NO_BOX;
21440 /* Set max_x to the maximum allowed X position. Don't let it go
21441 beyond the right edge of the window. */
21442 if (max_x <= 0)
21443 max_x = it->last_visible_x;
21444 else
21445 max_x = min (max_x, it->last_visible_x);
21447 /* Skip over display elements that are not visible. because IT->w is
21448 hscrolled. */
21449 if (it->current_x < it->first_visible_x)
21450 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21451 MOVE_TO_POS | MOVE_TO_X);
21453 row->ascent = it->max_ascent;
21454 row->height = it->max_ascent + it->max_descent;
21455 row->phys_ascent = it->max_phys_ascent;
21456 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21457 row->extra_line_spacing = it->max_extra_line_spacing;
21459 if (STRINGP (it->string))
21460 it_charpos = IT_STRING_CHARPOS (*it);
21461 else
21462 it_charpos = IT_CHARPOS (*it);
21464 /* This condition is for the case that we are called with current_x
21465 past last_visible_x. */
21466 while (it->current_x < max_x)
21468 int x_before, x, n_glyphs_before, i, nglyphs;
21470 /* Get the next display element. */
21471 if (!get_next_display_element (it))
21472 break;
21474 /* Produce glyphs. */
21475 x_before = it->current_x;
21476 n_glyphs_before = row->used[TEXT_AREA];
21477 PRODUCE_GLYPHS (it);
21479 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21480 i = 0;
21481 x = x_before;
21482 while (i < nglyphs)
21484 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21486 if (it->line_wrap != TRUNCATE
21487 && x + glyph->pixel_width > max_x)
21489 /* End of continued line or max_x reached. */
21490 if (CHAR_GLYPH_PADDING_P (*glyph))
21492 /* A wide character is unbreakable. */
21493 if (row->reversed_p)
21494 unproduce_glyphs (it, row->used[TEXT_AREA]
21495 - n_glyphs_before);
21496 row->used[TEXT_AREA] = n_glyphs_before;
21497 it->current_x = x_before;
21499 else
21501 if (row->reversed_p)
21502 unproduce_glyphs (it, row->used[TEXT_AREA]
21503 - (n_glyphs_before + i));
21504 row->used[TEXT_AREA] = n_glyphs_before + i;
21505 it->current_x = x;
21507 break;
21509 else if (x + glyph->pixel_width >= it->first_visible_x)
21511 /* Glyph is at least partially visible. */
21512 ++it->hpos;
21513 if (x < it->first_visible_x)
21514 row->x = x - it->first_visible_x;
21516 else
21518 /* Glyph is off the left margin of the display area.
21519 Should not happen. */
21520 abort ();
21523 row->ascent = max (row->ascent, it->max_ascent);
21524 row->height = max (row->height, it->max_ascent + it->max_descent);
21525 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21526 row->phys_height = max (row->phys_height,
21527 it->max_phys_ascent + it->max_phys_descent);
21528 row->extra_line_spacing = max (row->extra_line_spacing,
21529 it->max_extra_line_spacing);
21530 x += glyph->pixel_width;
21531 ++i;
21534 /* Stop if max_x reached. */
21535 if (i < nglyphs)
21536 break;
21538 /* Stop at line ends. */
21539 if (ITERATOR_AT_END_OF_LINE_P (it))
21541 it->continuation_lines_width = 0;
21542 break;
21545 set_iterator_to_next (it, 1);
21546 if (STRINGP (it->string))
21547 it_charpos = IT_STRING_CHARPOS (*it);
21548 else
21549 it_charpos = IT_CHARPOS (*it);
21551 /* Stop if truncating at the right edge. */
21552 if (it->line_wrap == TRUNCATE
21553 && it->current_x >= it->last_visible_x)
21555 /* Add truncation mark, but don't do it if the line is
21556 truncated at a padding space. */
21557 if (it_charpos < it->string_nchars)
21559 if (!FRAME_WINDOW_P (it->f))
21561 int ii, n;
21563 if (it->current_x > it->last_visible_x)
21565 if (!row->reversed_p)
21567 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21568 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21569 break;
21571 else
21573 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21574 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21575 break;
21576 unproduce_glyphs (it, ii + 1);
21577 ii = row->used[TEXT_AREA] - (ii + 1);
21579 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21581 row->used[TEXT_AREA] = ii;
21582 produce_special_glyphs (it, IT_TRUNCATION);
21585 produce_special_glyphs (it, IT_TRUNCATION);
21587 row->truncated_on_right_p = 1;
21589 break;
21593 /* Maybe insert a truncation at the left. */
21594 if (it->first_visible_x
21595 && it_charpos > 0)
21597 if (!FRAME_WINDOW_P (it->f))
21598 insert_left_trunc_glyphs (it);
21599 row->truncated_on_left_p = 1;
21602 it->face_id = saved_face_id;
21604 /* Value is number of columns displayed. */
21605 return it->hpos - hpos_at_start;
21610 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21611 appears as an element of LIST or as the car of an element of LIST.
21612 If PROPVAL is a list, compare each element against LIST in that
21613 way, and return 1/2 if any element of PROPVAL is found in LIST.
21614 Otherwise return 0. This function cannot quit.
21615 The return value is 2 if the text is invisible but with an ellipsis
21616 and 1 if it's invisible and without an ellipsis. */
21619 invisible_p (register Lisp_Object propval, Lisp_Object list)
21621 register Lisp_Object tail, proptail;
21623 for (tail = list; CONSP (tail); tail = XCDR (tail))
21625 register Lisp_Object tem;
21626 tem = XCAR (tail);
21627 if (EQ (propval, tem))
21628 return 1;
21629 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21630 return NILP (XCDR (tem)) ? 1 : 2;
21633 if (CONSP (propval))
21635 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
21637 Lisp_Object propelt;
21638 propelt = XCAR (proptail);
21639 for (tail = list; CONSP (tail); tail = XCDR (tail))
21641 register Lisp_Object tem;
21642 tem = XCAR (tail);
21643 if (EQ (propelt, tem))
21644 return 1;
21645 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
21646 return NILP (XCDR (tem)) ? 1 : 2;
21651 return 0;
21654 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
21655 doc: /* Non-nil if the property makes the text invisible.
21656 POS-OR-PROP can be a marker or number, in which case it is taken to be
21657 a position in the current buffer and the value of the `invisible' property
21658 is checked; or it can be some other value, which is then presumed to be the
21659 value of the `invisible' property of the text of interest.
21660 The non-nil value returned can be t for truly invisible text or something
21661 else if the text is replaced by an ellipsis. */)
21662 (Lisp_Object pos_or_prop)
21664 Lisp_Object prop
21665 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
21666 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
21667 : pos_or_prop);
21668 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
21669 return (invis == 0 ? Qnil
21670 : invis == 1 ? Qt
21671 : make_number (invis));
21674 /* Calculate a width or height in pixels from a specification using
21675 the following elements:
21677 SPEC ::=
21678 NUM - a (fractional) multiple of the default font width/height
21679 (NUM) - specifies exactly NUM pixels
21680 UNIT - a fixed number of pixels, see below.
21681 ELEMENT - size of a display element in pixels, see below.
21682 (NUM . SPEC) - equals NUM * SPEC
21683 (+ SPEC SPEC ...) - add pixel values
21684 (- SPEC SPEC ...) - subtract pixel values
21685 (- SPEC) - negate pixel value
21687 NUM ::=
21688 INT or FLOAT - a number constant
21689 SYMBOL - use symbol's (buffer local) variable binding.
21691 UNIT ::=
21692 in - pixels per inch *)
21693 mm - pixels per 1/1000 meter *)
21694 cm - pixels per 1/100 meter *)
21695 width - width of current font in pixels.
21696 height - height of current font in pixels.
21698 *) using the ratio(s) defined in display-pixels-per-inch.
21700 ELEMENT ::=
21702 left-fringe - left fringe width in pixels
21703 right-fringe - right fringe width in pixels
21705 left-margin - left margin width in pixels
21706 right-margin - right margin width in pixels
21708 scroll-bar - scroll-bar area width in pixels
21710 Examples:
21712 Pixels corresponding to 5 inches:
21713 (5 . in)
21715 Total width of non-text areas on left side of window (if scroll-bar is on left):
21716 '(space :width (+ left-fringe left-margin scroll-bar))
21718 Align to first text column (in header line):
21719 '(space :align-to 0)
21721 Align to middle of text area minus half the width of variable `my-image'
21722 containing a loaded image:
21723 '(space :align-to (0.5 . (- text my-image)))
21725 Width of left margin minus width of 1 character in the default font:
21726 '(space :width (- left-margin 1))
21728 Width of left margin minus width of 2 characters in the current font:
21729 '(space :width (- left-margin (2 . width)))
21731 Center 1 character over left-margin (in header line):
21732 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
21734 Different ways to express width of left fringe plus left margin minus one pixel:
21735 '(space :width (- (+ left-fringe left-margin) (1)))
21736 '(space :width (+ left-fringe left-margin (- (1))))
21737 '(space :width (+ left-fringe left-margin (-1)))
21741 #define NUMVAL(X) \
21742 ((INTEGERP (X) || FLOATP (X)) \
21743 ? XFLOATINT (X) \
21744 : - 1)
21746 static int
21747 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
21748 struct font *font, int width_p, int *align_to)
21750 double pixels;
21752 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
21753 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
21755 if (NILP (prop))
21756 return OK_PIXELS (0);
21758 xassert (FRAME_LIVE_P (it->f));
21760 if (SYMBOLP (prop))
21762 if (SCHARS (SYMBOL_NAME (prop)) == 2)
21764 char *unit = SSDATA (SYMBOL_NAME (prop));
21766 if (unit[0] == 'i' && unit[1] == 'n')
21767 pixels = 1.0;
21768 else if (unit[0] == 'm' && unit[1] == 'm')
21769 pixels = 25.4;
21770 else if (unit[0] == 'c' && unit[1] == 'm')
21771 pixels = 2.54;
21772 else
21773 pixels = 0;
21774 if (pixels > 0)
21776 double ppi;
21777 #ifdef HAVE_WINDOW_SYSTEM
21778 if (FRAME_WINDOW_P (it->f)
21779 && (ppi = (width_p
21780 ? FRAME_X_DISPLAY_INFO (it->f)->resx
21781 : FRAME_X_DISPLAY_INFO (it->f)->resy),
21782 ppi > 0))
21783 return OK_PIXELS (ppi / pixels);
21784 #endif
21786 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
21787 || (CONSP (Vdisplay_pixels_per_inch)
21788 && (ppi = (width_p
21789 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
21790 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
21791 ppi > 0)))
21792 return OK_PIXELS (ppi / pixels);
21794 return 0;
21798 #ifdef HAVE_WINDOW_SYSTEM
21799 if (EQ (prop, Qheight))
21800 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
21801 if (EQ (prop, Qwidth))
21802 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
21803 #else
21804 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
21805 return OK_PIXELS (1);
21806 #endif
21808 if (EQ (prop, Qtext))
21809 return OK_PIXELS (width_p
21810 ? window_box_width (it->w, TEXT_AREA)
21811 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
21813 if (align_to && *align_to < 0)
21815 *res = 0;
21816 if (EQ (prop, Qleft))
21817 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
21818 if (EQ (prop, Qright))
21819 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
21820 if (EQ (prop, Qcenter))
21821 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
21822 + window_box_width (it->w, TEXT_AREA) / 2);
21823 if (EQ (prop, Qleft_fringe))
21824 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21825 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
21826 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
21827 if (EQ (prop, Qright_fringe))
21828 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21829 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21830 : window_box_right_offset (it->w, TEXT_AREA));
21831 if (EQ (prop, Qleft_margin))
21832 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
21833 if (EQ (prop, Qright_margin))
21834 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
21835 if (EQ (prop, Qscroll_bar))
21836 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
21838 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21839 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21840 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21841 : 0)));
21843 else
21845 if (EQ (prop, Qleft_fringe))
21846 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
21847 if (EQ (prop, Qright_fringe))
21848 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
21849 if (EQ (prop, Qleft_margin))
21850 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
21851 if (EQ (prop, Qright_margin))
21852 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
21853 if (EQ (prop, Qscroll_bar))
21854 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
21857 prop = Fbuffer_local_value (prop, it->w->buffer);
21860 if (INTEGERP (prop) || FLOATP (prop))
21862 int base_unit = (width_p
21863 ? FRAME_COLUMN_WIDTH (it->f)
21864 : FRAME_LINE_HEIGHT (it->f));
21865 return OK_PIXELS (XFLOATINT (prop) * base_unit);
21868 if (CONSP (prop))
21870 Lisp_Object car = XCAR (prop);
21871 Lisp_Object cdr = XCDR (prop);
21873 if (SYMBOLP (car))
21875 #ifdef HAVE_WINDOW_SYSTEM
21876 if (FRAME_WINDOW_P (it->f)
21877 && valid_image_p (prop))
21879 ptrdiff_t id = lookup_image (it->f, prop);
21880 struct image *img = IMAGE_FROM_ID (it->f, id);
21882 return OK_PIXELS (width_p ? img->width : img->height);
21884 #endif
21885 if (EQ (car, Qplus) || EQ (car, Qminus))
21887 int first = 1;
21888 double px;
21890 pixels = 0;
21891 while (CONSP (cdr))
21893 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
21894 font, width_p, align_to))
21895 return 0;
21896 if (first)
21897 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
21898 else
21899 pixels += px;
21900 cdr = XCDR (cdr);
21902 if (EQ (car, Qminus))
21903 pixels = -pixels;
21904 return OK_PIXELS (pixels);
21907 car = Fbuffer_local_value (car, it->w->buffer);
21910 if (INTEGERP (car) || FLOATP (car))
21912 double fact;
21913 pixels = XFLOATINT (car);
21914 if (NILP (cdr))
21915 return OK_PIXELS (pixels);
21916 if (calc_pixel_width_or_height (&fact, it, cdr,
21917 font, width_p, align_to))
21918 return OK_PIXELS (pixels * fact);
21919 return 0;
21922 return 0;
21925 return 0;
21929 /***********************************************************************
21930 Glyph Display
21931 ***********************************************************************/
21933 #ifdef HAVE_WINDOW_SYSTEM
21935 #if GLYPH_DEBUG
21937 void
21938 dump_glyph_string (struct glyph_string *s)
21940 fprintf (stderr, "glyph string\n");
21941 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
21942 s->x, s->y, s->width, s->height);
21943 fprintf (stderr, " ybase = %d\n", s->ybase);
21944 fprintf (stderr, " hl = %d\n", s->hl);
21945 fprintf (stderr, " left overhang = %d, right = %d\n",
21946 s->left_overhang, s->right_overhang);
21947 fprintf (stderr, " nchars = %d\n", s->nchars);
21948 fprintf (stderr, " extends to end of line = %d\n",
21949 s->extends_to_end_of_line_p);
21950 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
21951 fprintf (stderr, " bg width = %d\n", s->background_width);
21954 #endif /* GLYPH_DEBUG */
21956 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
21957 of XChar2b structures for S; it can't be allocated in
21958 init_glyph_string because it must be allocated via `alloca'. W
21959 is the window on which S is drawn. ROW and AREA are the glyph row
21960 and area within the row from which S is constructed. START is the
21961 index of the first glyph structure covered by S. HL is a
21962 face-override for drawing S. */
21964 #ifdef HAVE_NTGUI
21965 #define OPTIONAL_HDC(hdc) HDC hdc,
21966 #define DECLARE_HDC(hdc) HDC hdc;
21967 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
21968 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
21969 #endif
21971 #ifndef OPTIONAL_HDC
21972 #define OPTIONAL_HDC(hdc)
21973 #define DECLARE_HDC(hdc)
21974 #define ALLOCATE_HDC(hdc, f)
21975 #define RELEASE_HDC(hdc, f)
21976 #endif
21978 static void
21979 init_glyph_string (struct glyph_string *s,
21980 OPTIONAL_HDC (hdc)
21981 XChar2b *char2b, struct window *w, struct glyph_row *row,
21982 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
21984 memset (s, 0, sizeof *s);
21985 s->w = w;
21986 s->f = XFRAME (w->frame);
21987 #ifdef HAVE_NTGUI
21988 s->hdc = hdc;
21989 #endif
21990 s->display = FRAME_X_DISPLAY (s->f);
21991 s->window = FRAME_X_WINDOW (s->f);
21992 s->char2b = char2b;
21993 s->hl = hl;
21994 s->row = row;
21995 s->area = area;
21996 s->first_glyph = row->glyphs[area] + start;
21997 s->height = row->height;
21998 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
21999 s->ybase = s->y + row->ascent;
22003 /* Append the list of glyph strings with head H and tail T to the list
22004 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22006 static inline void
22007 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22008 struct glyph_string *h, struct glyph_string *t)
22010 if (h)
22012 if (*head)
22013 (*tail)->next = h;
22014 else
22015 *head = h;
22016 h->prev = *tail;
22017 *tail = t;
22022 /* Prepend the list of glyph strings with head H and tail T to the
22023 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22024 result. */
22026 static inline void
22027 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22028 struct glyph_string *h, struct glyph_string *t)
22030 if (h)
22032 if (*head)
22033 (*head)->prev = t;
22034 else
22035 *tail = t;
22036 t->next = *head;
22037 *head = h;
22042 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22043 Set *HEAD and *TAIL to the resulting list. */
22045 static inline void
22046 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22047 struct glyph_string *s)
22049 s->next = s->prev = NULL;
22050 append_glyph_string_lists (head, tail, s, s);
22054 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22055 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22056 make sure that X resources for the face returned are allocated.
22057 Value is a pointer to a realized face that is ready for display if
22058 DISPLAY_P is non-zero. */
22060 static inline struct face *
22061 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22062 XChar2b *char2b, int display_p)
22064 struct face *face = FACE_FROM_ID (f, face_id);
22066 if (face->font)
22068 unsigned code = face->font->driver->encode_char (face->font, c);
22070 if (code != FONT_INVALID_CODE)
22071 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22072 else
22073 STORE_XCHAR2B (char2b, 0, 0);
22076 /* Make sure X resources of the face are allocated. */
22077 #ifdef HAVE_X_WINDOWS
22078 if (display_p)
22079 #endif
22081 xassert (face != NULL);
22082 PREPARE_FACE_FOR_DISPLAY (f, face);
22085 return face;
22089 /* Get face and two-byte form of character glyph GLYPH on frame F.
22090 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22091 a pointer to a realized face that is ready for display. */
22093 static inline struct face *
22094 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22095 XChar2b *char2b, int *two_byte_p)
22097 struct face *face;
22099 xassert (glyph->type == CHAR_GLYPH);
22100 face = FACE_FROM_ID (f, glyph->face_id);
22102 if (two_byte_p)
22103 *two_byte_p = 0;
22105 if (face->font)
22107 unsigned code;
22109 if (CHAR_BYTE8_P (glyph->u.ch))
22110 code = CHAR_TO_BYTE8 (glyph->u.ch);
22111 else
22112 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22114 if (code != FONT_INVALID_CODE)
22115 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22116 else
22117 STORE_XCHAR2B (char2b, 0, 0);
22120 /* Make sure X resources of the face are allocated. */
22121 xassert (face != NULL);
22122 PREPARE_FACE_FOR_DISPLAY (f, face);
22123 return face;
22127 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22128 Return 1 if FONT has a glyph for C, otherwise return 0. */
22130 static inline int
22131 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22133 unsigned code;
22135 if (CHAR_BYTE8_P (c))
22136 code = CHAR_TO_BYTE8 (c);
22137 else
22138 code = font->driver->encode_char (font, c);
22140 if (code == FONT_INVALID_CODE)
22141 return 0;
22142 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22143 return 1;
22147 /* Fill glyph string S with composition components specified by S->cmp.
22149 BASE_FACE is the base face of the composition.
22150 S->cmp_from is the index of the first component for S.
22152 OVERLAPS non-zero means S should draw the foreground only, and use
22153 its physical height for clipping. See also draw_glyphs.
22155 Value is the index of a component not in S. */
22157 static int
22158 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22159 int overlaps)
22161 int i;
22162 /* For all glyphs of this composition, starting at the offset
22163 S->cmp_from, until we reach the end of the definition or encounter a
22164 glyph that requires the different face, add it to S. */
22165 struct face *face;
22167 xassert (s);
22169 s->for_overlaps = overlaps;
22170 s->face = NULL;
22171 s->font = NULL;
22172 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22174 int c = COMPOSITION_GLYPH (s->cmp, i);
22176 /* TAB in a composition means display glyphs with padding space
22177 on the left or right. */
22178 if (c != '\t')
22180 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22181 -1, Qnil);
22183 face = get_char_face_and_encoding (s->f, c, face_id,
22184 s->char2b + i, 1);
22185 if (face)
22187 if (! s->face)
22189 s->face = face;
22190 s->font = s->face->font;
22192 else if (s->face != face)
22193 break;
22196 ++s->nchars;
22198 s->cmp_to = i;
22200 if (s->face == NULL)
22202 s->face = base_face->ascii_face;
22203 s->font = s->face->font;
22206 /* All glyph strings for the same composition has the same width,
22207 i.e. the width set for the first component of the composition. */
22208 s->width = s->first_glyph->pixel_width;
22210 /* If the specified font could not be loaded, use the frame's
22211 default font, but record the fact that we couldn't load it in
22212 the glyph string so that we can draw rectangles for the
22213 characters of the glyph string. */
22214 if (s->font == NULL)
22216 s->font_not_found_p = 1;
22217 s->font = FRAME_FONT (s->f);
22220 /* Adjust base line for subscript/superscript text. */
22221 s->ybase += s->first_glyph->voffset;
22223 /* This glyph string must always be drawn with 16-bit functions. */
22224 s->two_byte_p = 1;
22226 return s->cmp_to;
22229 static int
22230 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22231 int start, int end, int overlaps)
22233 struct glyph *glyph, *last;
22234 Lisp_Object lgstring;
22235 int i;
22237 s->for_overlaps = overlaps;
22238 glyph = s->row->glyphs[s->area] + start;
22239 last = s->row->glyphs[s->area] + end;
22240 s->cmp_id = glyph->u.cmp.id;
22241 s->cmp_from = glyph->slice.cmp.from;
22242 s->cmp_to = glyph->slice.cmp.to + 1;
22243 s->face = FACE_FROM_ID (s->f, face_id);
22244 lgstring = composition_gstring_from_id (s->cmp_id);
22245 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22246 glyph++;
22247 while (glyph < last
22248 && glyph->u.cmp.automatic
22249 && glyph->u.cmp.id == s->cmp_id
22250 && s->cmp_to == glyph->slice.cmp.from)
22251 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22253 for (i = s->cmp_from; i < s->cmp_to; i++)
22255 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22256 unsigned code = LGLYPH_CODE (lglyph);
22258 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22260 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22261 return glyph - s->row->glyphs[s->area];
22265 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22266 See the comment of fill_glyph_string for arguments.
22267 Value is the index of the first glyph not in S. */
22270 static int
22271 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22272 int start, int end, int overlaps)
22274 struct glyph *glyph, *last;
22275 int voffset;
22277 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22278 s->for_overlaps = overlaps;
22279 glyph = s->row->glyphs[s->area] + start;
22280 last = s->row->glyphs[s->area] + end;
22281 voffset = glyph->voffset;
22282 s->face = FACE_FROM_ID (s->f, face_id);
22283 s->font = s->face->font;
22284 s->nchars = 1;
22285 s->width = glyph->pixel_width;
22286 glyph++;
22287 while (glyph < last
22288 && glyph->type == GLYPHLESS_GLYPH
22289 && glyph->voffset == voffset
22290 && glyph->face_id == face_id)
22292 s->nchars++;
22293 s->width += glyph->pixel_width;
22294 glyph++;
22296 s->ybase += voffset;
22297 return glyph - s->row->glyphs[s->area];
22301 /* Fill glyph string S from a sequence of character glyphs.
22303 FACE_ID is the face id of the string. START is the index of the
22304 first glyph to consider, END is the index of the last + 1.
22305 OVERLAPS non-zero means S should draw the foreground only, and use
22306 its physical height for clipping. See also draw_glyphs.
22308 Value is the index of the first glyph not in S. */
22310 static int
22311 fill_glyph_string (struct glyph_string *s, int face_id,
22312 int start, int end, int overlaps)
22314 struct glyph *glyph, *last;
22315 int voffset;
22316 int glyph_not_available_p;
22318 xassert (s->f == XFRAME (s->w->frame));
22319 xassert (s->nchars == 0);
22320 xassert (start >= 0 && end > start);
22322 s->for_overlaps = overlaps;
22323 glyph = s->row->glyphs[s->area] + start;
22324 last = s->row->glyphs[s->area] + end;
22325 voffset = glyph->voffset;
22326 s->padding_p = glyph->padding_p;
22327 glyph_not_available_p = glyph->glyph_not_available_p;
22329 while (glyph < last
22330 && glyph->type == CHAR_GLYPH
22331 && glyph->voffset == voffset
22332 /* Same face id implies same font, nowadays. */
22333 && glyph->face_id == face_id
22334 && glyph->glyph_not_available_p == glyph_not_available_p)
22336 int two_byte_p;
22338 s->face = get_glyph_face_and_encoding (s->f, glyph,
22339 s->char2b + s->nchars,
22340 &two_byte_p);
22341 s->two_byte_p = two_byte_p;
22342 ++s->nchars;
22343 xassert (s->nchars <= end - start);
22344 s->width += glyph->pixel_width;
22345 if (glyph++->padding_p != s->padding_p)
22346 break;
22349 s->font = s->face->font;
22351 /* If the specified font could not be loaded, use the frame's font,
22352 but record the fact that we couldn't load it in
22353 S->font_not_found_p so that we can draw rectangles for the
22354 characters of the glyph string. */
22355 if (s->font == NULL || glyph_not_available_p)
22357 s->font_not_found_p = 1;
22358 s->font = FRAME_FONT (s->f);
22361 /* Adjust base line for subscript/superscript text. */
22362 s->ybase += voffset;
22364 xassert (s->face && s->face->gc);
22365 return glyph - s->row->glyphs[s->area];
22369 /* Fill glyph string S from image glyph S->first_glyph. */
22371 static void
22372 fill_image_glyph_string (struct glyph_string *s)
22374 xassert (s->first_glyph->type == IMAGE_GLYPH);
22375 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22376 xassert (s->img);
22377 s->slice = s->first_glyph->slice.img;
22378 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22379 s->font = s->face->font;
22380 s->width = s->first_glyph->pixel_width;
22382 /* Adjust base line for subscript/superscript text. */
22383 s->ybase += s->first_glyph->voffset;
22387 /* Fill glyph string S from a sequence of stretch glyphs.
22389 START is the index of the first glyph to consider,
22390 END is the index of the last + 1.
22392 Value is the index of the first glyph not in S. */
22394 static int
22395 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22397 struct glyph *glyph, *last;
22398 int voffset, face_id;
22400 xassert (s->first_glyph->type == STRETCH_GLYPH);
22402 glyph = s->row->glyphs[s->area] + start;
22403 last = s->row->glyphs[s->area] + end;
22404 face_id = glyph->face_id;
22405 s->face = FACE_FROM_ID (s->f, face_id);
22406 s->font = s->face->font;
22407 s->width = glyph->pixel_width;
22408 s->nchars = 1;
22409 voffset = glyph->voffset;
22411 for (++glyph;
22412 (glyph < last
22413 && glyph->type == STRETCH_GLYPH
22414 && glyph->voffset == voffset
22415 && glyph->face_id == face_id);
22416 ++glyph)
22417 s->width += glyph->pixel_width;
22419 /* Adjust base line for subscript/superscript text. */
22420 s->ybase += voffset;
22422 /* The case that face->gc == 0 is handled when drawing the glyph
22423 string by calling PREPARE_FACE_FOR_DISPLAY. */
22424 xassert (s->face);
22425 return glyph - s->row->glyphs[s->area];
22428 static struct font_metrics *
22429 get_per_char_metric (struct font *font, XChar2b *char2b)
22431 static struct font_metrics metrics;
22432 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22434 if (! font || code == FONT_INVALID_CODE)
22435 return NULL;
22436 font->driver->text_extents (font, &code, 1, &metrics);
22437 return &metrics;
22440 /* EXPORT for RIF:
22441 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22442 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22443 assumed to be zero. */
22445 void
22446 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22448 *left = *right = 0;
22450 if (glyph->type == CHAR_GLYPH)
22452 struct face *face;
22453 XChar2b char2b;
22454 struct font_metrics *pcm;
22456 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22457 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22459 if (pcm->rbearing > pcm->width)
22460 *right = pcm->rbearing - pcm->width;
22461 if (pcm->lbearing < 0)
22462 *left = -pcm->lbearing;
22465 else if (glyph->type == COMPOSITE_GLYPH)
22467 if (! glyph->u.cmp.automatic)
22469 struct composition *cmp = composition_table[glyph->u.cmp.id];
22471 if (cmp->rbearing > cmp->pixel_width)
22472 *right = cmp->rbearing - cmp->pixel_width;
22473 if (cmp->lbearing < 0)
22474 *left = - cmp->lbearing;
22476 else
22478 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22479 struct font_metrics metrics;
22481 composition_gstring_width (gstring, glyph->slice.cmp.from,
22482 glyph->slice.cmp.to + 1, &metrics);
22483 if (metrics.rbearing > metrics.width)
22484 *right = metrics.rbearing - metrics.width;
22485 if (metrics.lbearing < 0)
22486 *left = - metrics.lbearing;
22492 /* Return the index of the first glyph preceding glyph string S that
22493 is overwritten by S because of S's left overhang. Value is -1
22494 if no glyphs are overwritten. */
22496 static int
22497 left_overwritten (struct glyph_string *s)
22499 int k;
22501 if (s->left_overhang)
22503 int x = 0, i;
22504 struct glyph *glyphs = s->row->glyphs[s->area];
22505 int first = s->first_glyph - glyphs;
22507 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22508 x -= glyphs[i].pixel_width;
22510 k = i + 1;
22512 else
22513 k = -1;
22515 return k;
22519 /* Return the index of the first glyph preceding glyph string S that
22520 is overwriting S because of its right overhang. Value is -1 if no
22521 glyph in front of S overwrites S. */
22523 static int
22524 left_overwriting (struct glyph_string *s)
22526 int i, k, x;
22527 struct glyph *glyphs = s->row->glyphs[s->area];
22528 int first = s->first_glyph - glyphs;
22530 k = -1;
22531 x = 0;
22532 for (i = first - 1; i >= 0; --i)
22534 int left, right;
22535 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22536 if (x + right > 0)
22537 k = i;
22538 x -= glyphs[i].pixel_width;
22541 return k;
22545 /* Return the index of the last glyph following glyph string S that is
22546 overwritten by S because of S's right overhang. Value is -1 if
22547 no such glyph is found. */
22549 static int
22550 right_overwritten (struct glyph_string *s)
22552 int k = -1;
22554 if (s->right_overhang)
22556 int x = 0, i;
22557 struct glyph *glyphs = s->row->glyphs[s->area];
22558 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22559 int end = s->row->used[s->area];
22561 for (i = first; i < end && s->right_overhang > x; ++i)
22562 x += glyphs[i].pixel_width;
22564 k = i;
22567 return k;
22571 /* Return the index of the last glyph following glyph string S that
22572 overwrites S because of its left overhang. Value is negative
22573 if no such glyph is found. */
22575 static int
22576 right_overwriting (struct glyph_string *s)
22578 int i, k, x;
22579 int end = s->row->used[s->area];
22580 struct glyph *glyphs = s->row->glyphs[s->area];
22581 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22583 k = -1;
22584 x = 0;
22585 for (i = first; i < end; ++i)
22587 int left, right;
22588 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22589 if (x - left < 0)
22590 k = i;
22591 x += glyphs[i].pixel_width;
22594 return k;
22598 /* Set background width of glyph string S. START is the index of the
22599 first glyph following S. LAST_X is the right-most x-position + 1
22600 in the drawing area. */
22602 static inline void
22603 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22605 /* If the face of this glyph string has to be drawn to the end of
22606 the drawing area, set S->extends_to_end_of_line_p. */
22608 if (start == s->row->used[s->area]
22609 && s->area == TEXT_AREA
22610 && ((s->row->fill_line_p
22611 && (s->hl == DRAW_NORMAL_TEXT
22612 || s->hl == DRAW_IMAGE_RAISED
22613 || s->hl == DRAW_IMAGE_SUNKEN))
22614 || s->hl == DRAW_MOUSE_FACE))
22615 s->extends_to_end_of_line_p = 1;
22617 /* If S extends its face to the end of the line, set its
22618 background_width to the distance to the right edge of the drawing
22619 area. */
22620 if (s->extends_to_end_of_line_p)
22621 s->background_width = last_x - s->x + 1;
22622 else
22623 s->background_width = s->width;
22627 /* Compute overhangs and x-positions for glyph string S and its
22628 predecessors, or successors. X is the starting x-position for S.
22629 BACKWARD_P non-zero means process predecessors. */
22631 static void
22632 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22634 if (backward_p)
22636 while (s)
22638 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22639 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22640 x -= s->width;
22641 s->x = x;
22642 s = s->prev;
22645 else
22647 while (s)
22649 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22650 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22651 s->x = x;
22652 x += s->width;
22653 s = s->next;
22660 /* The following macros are only called from draw_glyphs below.
22661 They reference the following parameters of that function directly:
22662 `w', `row', `area', and `overlap_p'
22663 as well as the following local variables:
22664 `s', `f', and `hdc' (in W32) */
22666 #ifdef HAVE_NTGUI
22667 /* On W32, silently add local `hdc' variable to argument list of
22668 init_glyph_string. */
22669 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22670 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
22671 #else
22672 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22673 init_glyph_string (s, char2b, w, row, area, start, hl)
22674 #endif
22676 /* Add a glyph string for a stretch glyph to the list of strings
22677 between HEAD and TAIL. START is the index of the stretch glyph in
22678 row area AREA of glyph row ROW. END is the index of the last glyph
22679 in that glyph row area. X is the current output position assigned
22680 to the new glyph string constructed. HL overrides that face of the
22681 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22682 is the right-most x-position of the drawing area. */
22684 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
22685 and below -- keep them on one line. */
22686 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22687 do \
22689 s = (struct glyph_string *) alloca (sizeof *s); \
22690 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22691 START = fill_stretch_glyph_string (s, START, END); \
22692 append_glyph_string (&HEAD, &TAIL, s); \
22693 s->x = (X); \
22695 while (0)
22698 /* Add a glyph string for an image glyph to the list of strings
22699 between HEAD and TAIL. START is the index of the image glyph in
22700 row area AREA of glyph row ROW. END is the index of the last glyph
22701 in that glyph row area. X is the current output position assigned
22702 to the new glyph string constructed. HL overrides that face of the
22703 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22704 is the right-most x-position of the drawing area. */
22706 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22707 do \
22709 s = (struct glyph_string *) alloca (sizeof *s); \
22710 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22711 fill_image_glyph_string (s); \
22712 append_glyph_string (&HEAD, &TAIL, s); \
22713 ++START; \
22714 s->x = (X); \
22716 while (0)
22719 /* Add a glyph string for a sequence of character glyphs to the list
22720 of strings between HEAD and TAIL. START is the index of the first
22721 glyph in row area AREA of glyph row ROW that is part of the new
22722 glyph string. END is the index of the last glyph in that glyph row
22723 area. X is the current output position assigned to the new glyph
22724 string constructed. HL overrides that face of the glyph; e.g. it
22725 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
22726 right-most x-position of the drawing area. */
22728 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22729 do \
22731 int face_id; \
22732 XChar2b *char2b; \
22734 face_id = (row)->glyphs[area][START].face_id; \
22736 s = (struct glyph_string *) alloca (sizeof *s); \
22737 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
22738 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22739 append_glyph_string (&HEAD, &TAIL, s); \
22740 s->x = (X); \
22741 START = fill_glyph_string (s, face_id, START, END, overlaps); \
22743 while (0)
22746 /* Add a glyph string for a composite sequence to the list of strings
22747 between HEAD and TAIL. START is the index of the first glyph in
22748 row area AREA of glyph row ROW that is part of the new glyph
22749 string. END is the index of the last glyph in that glyph row area.
22750 X is the current output position assigned to the new glyph string
22751 constructed. HL overrides that face of the glyph; e.g. it is
22752 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
22753 x-position of the drawing area. */
22755 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22756 do { \
22757 int face_id = (row)->glyphs[area][START].face_id; \
22758 struct face *base_face = FACE_FROM_ID (f, face_id); \
22759 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
22760 struct composition *cmp = composition_table[cmp_id]; \
22761 XChar2b *char2b; \
22762 struct glyph_string *first_s = NULL; \
22763 int n; \
22765 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
22767 /* Make glyph_strings for each glyph sequence that is drawable by \
22768 the same face, and append them to HEAD/TAIL. */ \
22769 for (n = 0; n < cmp->glyph_len;) \
22771 s = (struct glyph_string *) alloca (sizeof *s); \
22772 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22773 append_glyph_string (&(HEAD), &(TAIL), s); \
22774 s->cmp = cmp; \
22775 s->cmp_from = n; \
22776 s->x = (X); \
22777 if (n == 0) \
22778 first_s = s; \
22779 n = fill_composite_glyph_string (s, base_face, overlaps); \
22782 ++START; \
22783 s = first_s; \
22784 } while (0)
22787 /* Add a glyph string for a glyph-string sequence to the list of strings
22788 between HEAD and TAIL. */
22790 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22791 do { \
22792 int face_id; \
22793 XChar2b *char2b; \
22794 Lisp_Object gstring; \
22796 face_id = (row)->glyphs[area][START].face_id; \
22797 gstring = (composition_gstring_from_id \
22798 ((row)->glyphs[area][START].u.cmp.id)); \
22799 s = (struct glyph_string *) alloca (sizeof *s); \
22800 char2b = (XChar2b *) alloca ((sizeof *char2b) \
22801 * LGSTRING_GLYPH_LEN (gstring)); \
22802 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22803 append_glyph_string (&(HEAD), &(TAIL), s); \
22804 s->x = (X); \
22805 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
22806 } while (0)
22809 /* Add a glyph string for a sequence of glyphless character's glyphs
22810 to the list of strings between HEAD and TAIL. The meanings of
22811 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
22813 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22814 do \
22816 int face_id; \
22818 face_id = (row)->glyphs[area][START].face_id; \
22820 s = (struct glyph_string *) alloca (sizeof *s); \
22821 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22822 append_glyph_string (&HEAD, &TAIL, s); \
22823 s->x = (X); \
22824 START = fill_glyphless_glyph_string (s, face_id, START, END, \
22825 overlaps); \
22827 while (0)
22830 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
22831 of AREA of glyph row ROW on window W between indices START and END.
22832 HL overrides the face for drawing glyph strings, e.g. it is
22833 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
22834 x-positions of the drawing area.
22836 This is an ugly monster macro construct because we must use alloca
22837 to allocate glyph strings (because draw_glyphs can be called
22838 asynchronously). */
22840 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22841 do \
22843 HEAD = TAIL = NULL; \
22844 while (START < END) \
22846 struct glyph *first_glyph = (row)->glyphs[area] + START; \
22847 switch (first_glyph->type) \
22849 case CHAR_GLYPH: \
22850 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
22851 HL, X, LAST_X); \
22852 break; \
22854 case COMPOSITE_GLYPH: \
22855 if (first_glyph->u.cmp.automatic) \
22856 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
22857 HL, X, LAST_X); \
22858 else \
22859 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
22860 HL, X, LAST_X); \
22861 break; \
22863 case STRETCH_GLYPH: \
22864 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
22865 HL, X, LAST_X); \
22866 break; \
22868 case IMAGE_GLYPH: \
22869 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
22870 HL, X, LAST_X); \
22871 break; \
22873 case GLYPHLESS_GLYPH: \
22874 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
22875 HL, X, LAST_X); \
22876 break; \
22878 default: \
22879 abort (); \
22882 if (s) \
22884 set_glyph_string_background_width (s, START, LAST_X); \
22885 (X) += s->width; \
22888 } while (0)
22891 /* Draw glyphs between START and END in AREA of ROW on window W,
22892 starting at x-position X. X is relative to AREA in W. HL is a
22893 face-override with the following meaning:
22895 DRAW_NORMAL_TEXT draw normally
22896 DRAW_CURSOR draw in cursor face
22897 DRAW_MOUSE_FACE draw in mouse face.
22898 DRAW_INVERSE_VIDEO draw in mode line face
22899 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
22900 DRAW_IMAGE_RAISED draw an image with a raised relief around it
22902 If OVERLAPS is non-zero, draw only the foreground of characters and
22903 clip to the physical height of ROW. Non-zero value also defines
22904 the overlapping part to be drawn:
22906 OVERLAPS_PRED overlap with preceding rows
22907 OVERLAPS_SUCC overlap with succeeding rows
22908 OVERLAPS_BOTH overlap with both preceding/succeeding rows
22909 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
22911 Value is the x-position reached, relative to AREA of W. */
22913 static int
22914 draw_glyphs (struct window *w, int x, struct glyph_row *row,
22915 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
22916 enum draw_glyphs_face hl, int overlaps)
22918 struct glyph_string *head, *tail;
22919 struct glyph_string *s;
22920 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
22921 int i, j, x_reached, last_x, area_left = 0;
22922 struct frame *f = XFRAME (WINDOW_FRAME (w));
22923 DECLARE_HDC (hdc);
22925 ALLOCATE_HDC (hdc, f);
22927 /* Let's rather be paranoid than getting a SEGV. */
22928 end = min (end, row->used[area]);
22929 start = max (0, start);
22930 start = min (end, start);
22932 /* Translate X to frame coordinates. Set last_x to the right
22933 end of the drawing area. */
22934 if (row->full_width_p)
22936 /* X is relative to the left edge of W, without scroll bars
22937 or fringes. */
22938 area_left = WINDOW_LEFT_EDGE_X (w);
22939 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
22941 else
22943 area_left = window_box_left (w, area);
22944 last_x = area_left + window_box_width (w, area);
22946 x += area_left;
22948 /* Build a doubly-linked list of glyph_string structures between
22949 head and tail from what we have to draw. Note that the macro
22950 BUILD_GLYPH_STRINGS will modify its start parameter. That's
22951 the reason we use a separate variable `i'. */
22952 i = start;
22953 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
22954 if (tail)
22955 x_reached = tail->x + tail->background_width;
22956 else
22957 x_reached = x;
22959 /* If there are any glyphs with lbearing < 0 or rbearing > width in
22960 the row, redraw some glyphs in front or following the glyph
22961 strings built above. */
22962 if (head && !overlaps && row->contains_overlapping_glyphs_p)
22964 struct glyph_string *h, *t;
22965 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
22966 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
22967 int check_mouse_face = 0;
22968 int dummy_x = 0;
22970 /* If mouse highlighting is on, we may need to draw adjacent
22971 glyphs using mouse-face highlighting. */
22972 if (area == TEXT_AREA && row->mouse_face_p)
22974 struct glyph_row *mouse_beg_row, *mouse_end_row;
22976 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
22977 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
22979 if (row >= mouse_beg_row && row <= mouse_end_row)
22981 check_mouse_face = 1;
22982 mouse_beg_col = (row == mouse_beg_row)
22983 ? hlinfo->mouse_face_beg_col : 0;
22984 mouse_end_col = (row == mouse_end_row)
22985 ? hlinfo->mouse_face_end_col
22986 : row->used[TEXT_AREA];
22990 /* Compute overhangs for all glyph strings. */
22991 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
22992 for (s = head; s; s = s->next)
22993 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
22995 /* Prepend glyph strings for glyphs in front of the first glyph
22996 string that are overwritten because of the first glyph
22997 string's left overhang. The background of all strings
22998 prepended must be drawn because the first glyph string
22999 draws over it. */
23000 i = left_overwritten (head);
23001 if (i >= 0)
23003 enum draw_glyphs_face overlap_hl;
23005 /* If this row contains mouse highlighting, attempt to draw
23006 the overlapped glyphs with the correct highlight. This
23007 code fails if the overlap encompasses more than one glyph
23008 and mouse-highlight spans only some of these glyphs.
23009 However, making it work perfectly involves a lot more
23010 code, and I don't know if the pathological case occurs in
23011 practice, so we'll stick to this for now. --- cyd */
23012 if (check_mouse_face
23013 && mouse_beg_col < start && mouse_end_col > i)
23014 overlap_hl = DRAW_MOUSE_FACE;
23015 else
23016 overlap_hl = DRAW_NORMAL_TEXT;
23018 j = i;
23019 BUILD_GLYPH_STRINGS (j, start, h, t,
23020 overlap_hl, dummy_x, last_x);
23021 start = i;
23022 compute_overhangs_and_x (t, head->x, 1);
23023 prepend_glyph_string_lists (&head, &tail, h, t);
23024 clip_head = head;
23027 /* Prepend glyph strings for glyphs in front of the first glyph
23028 string that overwrite that glyph string because of their
23029 right overhang. For these strings, only the foreground must
23030 be drawn, because it draws over the glyph string at `head'.
23031 The background must not be drawn because this would overwrite
23032 right overhangs of preceding glyphs for which no glyph
23033 strings exist. */
23034 i = left_overwriting (head);
23035 if (i >= 0)
23037 enum draw_glyphs_face overlap_hl;
23039 if (check_mouse_face
23040 && mouse_beg_col < start && mouse_end_col > i)
23041 overlap_hl = DRAW_MOUSE_FACE;
23042 else
23043 overlap_hl = DRAW_NORMAL_TEXT;
23045 clip_head = head;
23046 BUILD_GLYPH_STRINGS (i, start, h, t,
23047 overlap_hl, dummy_x, last_x);
23048 for (s = h; s; s = s->next)
23049 s->background_filled_p = 1;
23050 compute_overhangs_and_x (t, head->x, 1);
23051 prepend_glyph_string_lists (&head, &tail, h, t);
23054 /* Append glyphs strings for glyphs following the last glyph
23055 string tail that are overwritten by tail. The background of
23056 these strings has to be drawn because tail's foreground draws
23057 over it. */
23058 i = right_overwritten (tail);
23059 if (i >= 0)
23061 enum draw_glyphs_face overlap_hl;
23063 if (check_mouse_face
23064 && mouse_beg_col < i && mouse_end_col > end)
23065 overlap_hl = DRAW_MOUSE_FACE;
23066 else
23067 overlap_hl = DRAW_NORMAL_TEXT;
23069 BUILD_GLYPH_STRINGS (end, i, h, t,
23070 overlap_hl, x, last_x);
23071 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23072 we don't have `end = i;' here. */
23073 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23074 append_glyph_string_lists (&head, &tail, h, t);
23075 clip_tail = tail;
23078 /* Append glyph strings for glyphs following the last glyph
23079 string tail that overwrite tail. The foreground of such
23080 glyphs has to be drawn because it writes into the background
23081 of tail. The background must not be drawn because it could
23082 paint over the foreground of following glyphs. */
23083 i = right_overwriting (tail);
23084 if (i >= 0)
23086 enum draw_glyphs_face overlap_hl;
23087 if (check_mouse_face
23088 && mouse_beg_col < i && mouse_end_col > end)
23089 overlap_hl = DRAW_MOUSE_FACE;
23090 else
23091 overlap_hl = DRAW_NORMAL_TEXT;
23093 clip_tail = tail;
23094 i++; /* We must include the Ith glyph. */
23095 BUILD_GLYPH_STRINGS (end, i, h, t,
23096 overlap_hl, x, last_x);
23097 for (s = h; s; s = s->next)
23098 s->background_filled_p = 1;
23099 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23100 append_glyph_string_lists (&head, &tail, h, t);
23102 if (clip_head || clip_tail)
23103 for (s = head; s; s = s->next)
23105 s->clip_head = clip_head;
23106 s->clip_tail = clip_tail;
23110 /* Draw all strings. */
23111 for (s = head; s; s = s->next)
23112 FRAME_RIF (f)->draw_glyph_string (s);
23114 #ifndef HAVE_NS
23115 /* When focus a sole frame and move horizontally, this sets on_p to 0
23116 causing a failure to erase prev cursor position. */
23117 if (area == TEXT_AREA
23118 && !row->full_width_p
23119 /* When drawing overlapping rows, only the glyph strings'
23120 foreground is drawn, which doesn't erase a cursor
23121 completely. */
23122 && !overlaps)
23124 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23125 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23126 : (tail ? tail->x + tail->background_width : x));
23127 x0 -= area_left;
23128 x1 -= area_left;
23130 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23131 row->y, MATRIX_ROW_BOTTOM_Y (row));
23133 #endif
23135 /* Value is the x-position up to which drawn, relative to AREA of W.
23136 This doesn't include parts drawn because of overhangs. */
23137 if (row->full_width_p)
23138 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23139 else
23140 x_reached -= area_left;
23142 RELEASE_HDC (hdc, f);
23144 return x_reached;
23147 /* Expand row matrix if too narrow. Don't expand if area
23148 is not present. */
23150 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23152 if (!fonts_changed_p \
23153 && (it->glyph_row->glyphs[area] \
23154 < it->glyph_row->glyphs[area + 1])) \
23156 it->w->ncols_scale_factor++; \
23157 fonts_changed_p = 1; \
23161 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23162 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23164 static inline void
23165 append_glyph (struct it *it)
23167 struct glyph *glyph;
23168 enum glyph_row_area area = it->area;
23170 xassert (it->glyph_row);
23171 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23173 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23174 if (glyph < it->glyph_row->glyphs[area + 1])
23176 /* If the glyph row is reversed, we need to prepend the glyph
23177 rather than append it. */
23178 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23180 struct glyph *g;
23182 /* Make room for the additional glyph. */
23183 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23184 g[1] = *g;
23185 glyph = it->glyph_row->glyphs[area];
23187 glyph->charpos = CHARPOS (it->position);
23188 glyph->object = it->object;
23189 if (it->pixel_width > 0)
23191 glyph->pixel_width = it->pixel_width;
23192 glyph->padding_p = 0;
23194 else
23196 /* Assure at least 1-pixel width. Otherwise, cursor can't
23197 be displayed correctly. */
23198 glyph->pixel_width = 1;
23199 glyph->padding_p = 1;
23201 glyph->ascent = it->ascent;
23202 glyph->descent = it->descent;
23203 glyph->voffset = it->voffset;
23204 glyph->type = CHAR_GLYPH;
23205 glyph->avoid_cursor_p = it->avoid_cursor_p;
23206 glyph->multibyte_p = it->multibyte_p;
23207 glyph->left_box_line_p = it->start_of_box_run_p;
23208 glyph->right_box_line_p = it->end_of_box_run_p;
23209 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23210 || it->phys_descent > it->descent);
23211 glyph->glyph_not_available_p = it->glyph_not_available_p;
23212 glyph->face_id = it->face_id;
23213 glyph->u.ch = it->char_to_display;
23214 glyph->slice.img = null_glyph_slice;
23215 glyph->font_type = FONT_TYPE_UNKNOWN;
23216 if (it->bidi_p)
23218 glyph->resolved_level = it->bidi_it.resolved_level;
23219 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23220 abort ();
23221 glyph->bidi_type = it->bidi_it.type;
23223 else
23225 glyph->resolved_level = 0;
23226 glyph->bidi_type = UNKNOWN_BT;
23228 ++it->glyph_row->used[area];
23230 else
23231 IT_EXPAND_MATRIX_WIDTH (it, area);
23234 /* Store one glyph for the composition IT->cmp_it.id in
23235 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23236 non-null. */
23238 static inline void
23239 append_composite_glyph (struct it *it)
23241 struct glyph *glyph;
23242 enum glyph_row_area area = it->area;
23244 xassert (it->glyph_row);
23246 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23247 if (glyph < it->glyph_row->glyphs[area + 1])
23249 /* If the glyph row is reversed, we need to prepend the glyph
23250 rather than append it. */
23251 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23253 struct glyph *g;
23255 /* Make room for the new glyph. */
23256 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23257 g[1] = *g;
23258 glyph = it->glyph_row->glyphs[it->area];
23260 glyph->charpos = it->cmp_it.charpos;
23261 glyph->object = it->object;
23262 glyph->pixel_width = it->pixel_width;
23263 glyph->ascent = it->ascent;
23264 glyph->descent = it->descent;
23265 glyph->voffset = it->voffset;
23266 glyph->type = COMPOSITE_GLYPH;
23267 if (it->cmp_it.ch < 0)
23269 glyph->u.cmp.automatic = 0;
23270 glyph->u.cmp.id = it->cmp_it.id;
23271 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23273 else
23275 glyph->u.cmp.automatic = 1;
23276 glyph->u.cmp.id = it->cmp_it.id;
23277 glyph->slice.cmp.from = it->cmp_it.from;
23278 glyph->slice.cmp.to = it->cmp_it.to - 1;
23280 glyph->avoid_cursor_p = it->avoid_cursor_p;
23281 glyph->multibyte_p = it->multibyte_p;
23282 glyph->left_box_line_p = it->start_of_box_run_p;
23283 glyph->right_box_line_p = it->end_of_box_run_p;
23284 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23285 || it->phys_descent > it->descent);
23286 glyph->padding_p = 0;
23287 glyph->glyph_not_available_p = 0;
23288 glyph->face_id = it->face_id;
23289 glyph->font_type = FONT_TYPE_UNKNOWN;
23290 if (it->bidi_p)
23292 glyph->resolved_level = it->bidi_it.resolved_level;
23293 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23294 abort ();
23295 glyph->bidi_type = it->bidi_it.type;
23297 ++it->glyph_row->used[area];
23299 else
23300 IT_EXPAND_MATRIX_WIDTH (it, area);
23304 /* Change IT->ascent and IT->height according to the setting of
23305 IT->voffset. */
23307 static inline void
23308 take_vertical_position_into_account (struct it *it)
23310 if (it->voffset)
23312 if (it->voffset < 0)
23313 /* Increase the ascent so that we can display the text higher
23314 in the line. */
23315 it->ascent -= it->voffset;
23316 else
23317 /* Increase the descent so that we can display the text lower
23318 in the line. */
23319 it->descent += it->voffset;
23324 /* Produce glyphs/get display metrics for the image IT is loaded with.
23325 See the description of struct display_iterator in dispextern.h for
23326 an overview of struct display_iterator. */
23328 static void
23329 produce_image_glyph (struct it *it)
23331 struct image *img;
23332 struct face *face;
23333 int glyph_ascent, crop;
23334 struct glyph_slice slice;
23336 xassert (it->what == IT_IMAGE);
23338 face = FACE_FROM_ID (it->f, it->face_id);
23339 xassert (face);
23340 /* Make sure X resources of the face is loaded. */
23341 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23343 if (it->image_id < 0)
23345 /* Fringe bitmap. */
23346 it->ascent = it->phys_ascent = 0;
23347 it->descent = it->phys_descent = 0;
23348 it->pixel_width = 0;
23349 it->nglyphs = 0;
23350 return;
23353 img = IMAGE_FROM_ID (it->f, it->image_id);
23354 xassert (img);
23355 /* Make sure X resources of the image is loaded. */
23356 prepare_image_for_display (it->f, img);
23358 slice.x = slice.y = 0;
23359 slice.width = img->width;
23360 slice.height = img->height;
23362 if (INTEGERP (it->slice.x))
23363 slice.x = XINT (it->slice.x);
23364 else if (FLOATP (it->slice.x))
23365 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23367 if (INTEGERP (it->slice.y))
23368 slice.y = XINT (it->slice.y);
23369 else if (FLOATP (it->slice.y))
23370 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23372 if (INTEGERP (it->slice.width))
23373 slice.width = XINT (it->slice.width);
23374 else if (FLOATP (it->slice.width))
23375 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23377 if (INTEGERP (it->slice.height))
23378 slice.height = XINT (it->slice.height);
23379 else if (FLOATP (it->slice.height))
23380 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23382 if (slice.x >= img->width)
23383 slice.x = img->width;
23384 if (slice.y >= img->height)
23385 slice.y = img->height;
23386 if (slice.x + slice.width >= img->width)
23387 slice.width = img->width - slice.x;
23388 if (slice.y + slice.height > img->height)
23389 slice.height = img->height - slice.y;
23391 if (slice.width == 0 || slice.height == 0)
23392 return;
23394 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23396 it->descent = slice.height - glyph_ascent;
23397 if (slice.y == 0)
23398 it->descent += img->vmargin;
23399 if (slice.y + slice.height == img->height)
23400 it->descent += img->vmargin;
23401 it->phys_descent = it->descent;
23403 it->pixel_width = slice.width;
23404 if (slice.x == 0)
23405 it->pixel_width += img->hmargin;
23406 if (slice.x + slice.width == img->width)
23407 it->pixel_width += img->hmargin;
23409 /* It's quite possible for images to have an ascent greater than
23410 their height, so don't get confused in that case. */
23411 if (it->descent < 0)
23412 it->descent = 0;
23414 it->nglyphs = 1;
23416 if (face->box != FACE_NO_BOX)
23418 if (face->box_line_width > 0)
23420 if (slice.y == 0)
23421 it->ascent += face->box_line_width;
23422 if (slice.y + slice.height == img->height)
23423 it->descent += face->box_line_width;
23426 if (it->start_of_box_run_p && slice.x == 0)
23427 it->pixel_width += eabs (face->box_line_width);
23428 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23429 it->pixel_width += eabs (face->box_line_width);
23432 take_vertical_position_into_account (it);
23434 /* Automatically crop wide image glyphs at right edge so we can
23435 draw the cursor on same display row. */
23436 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23437 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23439 it->pixel_width -= crop;
23440 slice.width -= crop;
23443 if (it->glyph_row)
23445 struct glyph *glyph;
23446 enum glyph_row_area area = it->area;
23448 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23449 if (glyph < it->glyph_row->glyphs[area + 1])
23451 glyph->charpos = CHARPOS (it->position);
23452 glyph->object = it->object;
23453 glyph->pixel_width = it->pixel_width;
23454 glyph->ascent = glyph_ascent;
23455 glyph->descent = it->descent;
23456 glyph->voffset = it->voffset;
23457 glyph->type = IMAGE_GLYPH;
23458 glyph->avoid_cursor_p = it->avoid_cursor_p;
23459 glyph->multibyte_p = it->multibyte_p;
23460 glyph->left_box_line_p = it->start_of_box_run_p;
23461 glyph->right_box_line_p = it->end_of_box_run_p;
23462 glyph->overlaps_vertically_p = 0;
23463 glyph->padding_p = 0;
23464 glyph->glyph_not_available_p = 0;
23465 glyph->face_id = it->face_id;
23466 glyph->u.img_id = img->id;
23467 glyph->slice.img = slice;
23468 glyph->font_type = FONT_TYPE_UNKNOWN;
23469 if (it->bidi_p)
23471 glyph->resolved_level = it->bidi_it.resolved_level;
23472 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23473 abort ();
23474 glyph->bidi_type = it->bidi_it.type;
23476 ++it->glyph_row->used[area];
23478 else
23479 IT_EXPAND_MATRIX_WIDTH (it, area);
23484 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23485 of the glyph, WIDTH and HEIGHT are the width and height of the
23486 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23488 static void
23489 append_stretch_glyph (struct it *it, Lisp_Object object,
23490 int width, int height, int ascent)
23492 struct glyph *glyph;
23493 enum glyph_row_area area = it->area;
23495 xassert (ascent >= 0 && ascent <= height);
23497 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23498 if (glyph < it->glyph_row->glyphs[area + 1])
23500 /* If the glyph row is reversed, we need to prepend the glyph
23501 rather than append it. */
23502 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23504 struct glyph *g;
23506 /* Make room for the additional glyph. */
23507 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23508 g[1] = *g;
23509 glyph = it->glyph_row->glyphs[area];
23511 glyph->charpos = CHARPOS (it->position);
23512 glyph->object = object;
23513 glyph->pixel_width = width;
23514 glyph->ascent = ascent;
23515 glyph->descent = height - ascent;
23516 glyph->voffset = it->voffset;
23517 glyph->type = STRETCH_GLYPH;
23518 glyph->avoid_cursor_p = it->avoid_cursor_p;
23519 glyph->multibyte_p = it->multibyte_p;
23520 glyph->left_box_line_p = it->start_of_box_run_p;
23521 glyph->right_box_line_p = it->end_of_box_run_p;
23522 glyph->overlaps_vertically_p = 0;
23523 glyph->padding_p = 0;
23524 glyph->glyph_not_available_p = 0;
23525 glyph->face_id = it->face_id;
23526 glyph->u.stretch.ascent = ascent;
23527 glyph->u.stretch.height = height;
23528 glyph->slice.img = null_glyph_slice;
23529 glyph->font_type = FONT_TYPE_UNKNOWN;
23530 if (it->bidi_p)
23532 glyph->resolved_level = it->bidi_it.resolved_level;
23533 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23534 abort ();
23535 glyph->bidi_type = it->bidi_it.type;
23537 else
23539 glyph->resolved_level = 0;
23540 glyph->bidi_type = UNKNOWN_BT;
23542 ++it->glyph_row->used[area];
23544 else
23545 IT_EXPAND_MATRIX_WIDTH (it, area);
23548 #endif /* HAVE_WINDOW_SYSTEM */
23550 /* Produce a stretch glyph for iterator IT. IT->object is the value
23551 of the glyph property displayed. The value must be a list
23552 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23553 being recognized:
23555 1. `:width WIDTH' specifies that the space should be WIDTH *
23556 canonical char width wide. WIDTH may be an integer or floating
23557 point number.
23559 2. `:relative-width FACTOR' specifies that the width of the stretch
23560 should be computed from the width of the first character having the
23561 `glyph' property, and should be FACTOR times that width.
23563 3. `:align-to HPOS' specifies that the space should be wide enough
23564 to reach HPOS, a value in canonical character units.
23566 Exactly one of the above pairs must be present.
23568 4. `:height HEIGHT' specifies that the height of the stretch produced
23569 should be HEIGHT, measured in canonical character units.
23571 5. `:relative-height FACTOR' specifies that the height of the
23572 stretch should be FACTOR times the height of the characters having
23573 the glyph property.
23575 Either none or exactly one of 4 or 5 must be present.
23577 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23578 of the stretch should be used for the ascent of the stretch.
23579 ASCENT must be in the range 0 <= ASCENT <= 100. */
23581 void
23582 produce_stretch_glyph (struct it *it)
23584 /* (space :width WIDTH :height HEIGHT ...) */
23585 Lisp_Object prop, plist;
23586 int width = 0, height = 0, align_to = -1;
23587 int zero_width_ok_p = 0;
23588 int ascent = 0;
23589 double tem;
23590 struct face *face = NULL;
23591 struct font *font = NULL;
23593 #ifdef HAVE_WINDOW_SYSTEM
23594 int zero_height_ok_p = 0;
23596 if (FRAME_WINDOW_P (it->f))
23598 face = FACE_FROM_ID (it->f, it->face_id);
23599 font = face->font ? face->font : FRAME_FONT (it->f);
23600 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23602 #endif
23604 /* List should start with `space'. */
23605 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
23606 plist = XCDR (it->object);
23608 /* Compute the width of the stretch. */
23609 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
23610 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
23612 /* Absolute width `:width WIDTH' specified and valid. */
23613 zero_width_ok_p = 1;
23614 width = (int)tem;
23616 #ifdef HAVE_WINDOW_SYSTEM
23617 else if (FRAME_WINDOW_P (it->f)
23618 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
23620 /* Relative width `:relative-width FACTOR' specified and valid.
23621 Compute the width of the characters having the `glyph'
23622 property. */
23623 struct it it2;
23624 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
23626 it2 = *it;
23627 if (it->multibyte_p)
23628 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
23629 else
23631 it2.c = it2.char_to_display = *p, it2.len = 1;
23632 if (! ASCII_CHAR_P (it2.c))
23633 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
23636 it2.glyph_row = NULL;
23637 it2.what = IT_CHARACTER;
23638 x_produce_glyphs (&it2);
23639 width = NUMVAL (prop) * it2.pixel_width;
23641 #endif /* HAVE_WINDOW_SYSTEM */
23642 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
23643 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
23645 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
23646 align_to = (align_to < 0
23648 : align_to - window_box_left_offset (it->w, TEXT_AREA));
23649 else if (align_to < 0)
23650 align_to = window_box_left_offset (it->w, TEXT_AREA);
23651 width = max (0, (int)tem + align_to - it->current_x);
23652 zero_width_ok_p = 1;
23654 else
23655 /* Nothing specified -> width defaults to canonical char width. */
23656 width = FRAME_COLUMN_WIDTH (it->f);
23658 if (width <= 0 && (width < 0 || !zero_width_ok_p))
23659 width = 1;
23661 #ifdef HAVE_WINDOW_SYSTEM
23662 /* Compute height. */
23663 if (FRAME_WINDOW_P (it->f))
23665 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
23666 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23668 height = (int)tem;
23669 zero_height_ok_p = 1;
23671 else if (prop = Fplist_get (plist, QCrelative_height),
23672 NUMVAL (prop) > 0)
23673 height = FONT_HEIGHT (font) * NUMVAL (prop);
23674 else
23675 height = FONT_HEIGHT (font);
23677 if (height <= 0 && (height < 0 || !zero_height_ok_p))
23678 height = 1;
23680 /* Compute percentage of height used for ascent. If
23681 `:ascent ASCENT' is present and valid, use that. Otherwise,
23682 derive the ascent from the font in use. */
23683 if (prop = Fplist_get (plist, QCascent),
23684 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
23685 ascent = height * NUMVAL (prop) / 100.0;
23686 else if (!NILP (prop)
23687 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23688 ascent = min (max (0, (int)tem), height);
23689 else
23690 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
23692 else
23693 #endif /* HAVE_WINDOW_SYSTEM */
23694 height = 1;
23696 if (width > 0 && it->line_wrap != TRUNCATE
23697 && it->current_x + width > it->last_visible_x)
23699 width = it->last_visible_x - it->current_x;
23700 #ifdef HAVE_WINDOW_SYSTEM
23701 /* Subtract one more pixel from the stretch width, but only on
23702 GUI frames, since on a TTY each glyph is one "pixel" wide. */
23703 width -= FRAME_WINDOW_P (it->f);
23704 #endif
23707 if (width > 0 && height > 0 && it->glyph_row)
23709 Lisp_Object o_object = it->object;
23710 Lisp_Object object = it->stack[it->sp - 1].string;
23711 int n = width;
23713 if (!STRINGP (object))
23714 object = it->w->buffer;
23715 #ifdef HAVE_WINDOW_SYSTEM
23716 if (FRAME_WINDOW_P (it->f))
23717 append_stretch_glyph (it, object, width, height, ascent);
23718 else
23719 #endif
23721 it->object = object;
23722 it->char_to_display = ' ';
23723 it->pixel_width = it->len = 1;
23724 while (n--)
23725 tty_append_glyph (it);
23726 it->object = o_object;
23730 it->pixel_width = width;
23731 #ifdef HAVE_WINDOW_SYSTEM
23732 if (FRAME_WINDOW_P (it->f))
23734 it->ascent = it->phys_ascent = ascent;
23735 it->descent = it->phys_descent = height - it->ascent;
23736 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
23737 take_vertical_position_into_account (it);
23739 else
23740 #endif
23741 it->nglyphs = width;
23744 #ifdef HAVE_WINDOW_SYSTEM
23746 /* Calculate line-height and line-spacing properties.
23747 An integer value specifies explicit pixel value.
23748 A float value specifies relative value to current face height.
23749 A cons (float . face-name) specifies relative value to
23750 height of specified face font.
23752 Returns height in pixels, or nil. */
23755 static Lisp_Object
23756 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
23757 int boff, int override)
23759 Lisp_Object face_name = Qnil;
23760 int ascent, descent, height;
23762 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
23763 return val;
23765 if (CONSP (val))
23767 face_name = XCAR (val);
23768 val = XCDR (val);
23769 if (!NUMBERP (val))
23770 val = make_number (1);
23771 if (NILP (face_name))
23773 height = it->ascent + it->descent;
23774 goto scale;
23778 if (NILP (face_name))
23780 font = FRAME_FONT (it->f);
23781 boff = FRAME_BASELINE_OFFSET (it->f);
23783 else if (EQ (face_name, Qt))
23785 override = 0;
23787 else
23789 int face_id;
23790 struct face *face;
23792 face_id = lookup_named_face (it->f, face_name, 0);
23793 if (face_id < 0)
23794 return make_number (-1);
23796 face = FACE_FROM_ID (it->f, face_id);
23797 font = face->font;
23798 if (font == NULL)
23799 return make_number (-1);
23800 boff = font->baseline_offset;
23801 if (font->vertical_centering)
23802 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23805 ascent = FONT_BASE (font) + boff;
23806 descent = FONT_DESCENT (font) - boff;
23808 if (override)
23810 it->override_ascent = ascent;
23811 it->override_descent = descent;
23812 it->override_boff = boff;
23815 height = ascent + descent;
23817 scale:
23818 if (FLOATP (val))
23819 height = (int)(XFLOAT_DATA (val) * height);
23820 else if (INTEGERP (val))
23821 height *= XINT (val);
23823 return make_number (height);
23827 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
23828 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
23829 and only if this is for a character for which no font was found.
23831 If the display method (it->glyphless_method) is
23832 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
23833 length of the acronym or the hexadecimal string, UPPER_XOFF and
23834 UPPER_YOFF are pixel offsets for the upper part of the string,
23835 LOWER_XOFF and LOWER_YOFF are for the lower part.
23837 For the other display methods, LEN through LOWER_YOFF are zero. */
23839 static void
23840 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
23841 short upper_xoff, short upper_yoff,
23842 short lower_xoff, short lower_yoff)
23844 struct glyph *glyph;
23845 enum glyph_row_area area = it->area;
23847 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23848 if (glyph < it->glyph_row->glyphs[area + 1])
23850 /* If the glyph row is reversed, we need to prepend the glyph
23851 rather than append it. */
23852 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23854 struct glyph *g;
23856 /* Make room for the additional glyph. */
23857 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23858 g[1] = *g;
23859 glyph = it->glyph_row->glyphs[area];
23861 glyph->charpos = CHARPOS (it->position);
23862 glyph->object = it->object;
23863 glyph->pixel_width = it->pixel_width;
23864 glyph->ascent = it->ascent;
23865 glyph->descent = it->descent;
23866 glyph->voffset = it->voffset;
23867 glyph->type = GLYPHLESS_GLYPH;
23868 glyph->u.glyphless.method = it->glyphless_method;
23869 glyph->u.glyphless.for_no_font = for_no_font;
23870 glyph->u.glyphless.len = len;
23871 glyph->u.glyphless.ch = it->c;
23872 glyph->slice.glyphless.upper_xoff = upper_xoff;
23873 glyph->slice.glyphless.upper_yoff = upper_yoff;
23874 glyph->slice.glyphless.lower_xoff = lower_xoff;
23875 glyph->slice.glyphless.lower_yoff = lower_yoff;
23876 glyph->avoid_cursor_p = it->avoid_cursor_p;
23877 glyph->multibyte_p = it->multibyte_p;
23878 glyph->left_box_line_p = it->start_of_box_run_p;
23879 glyph->right_box_line_p = it->end_of_box_run_p;
23880 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23881 || it->phys_descent > it->descent);
23882 glyph->padding_p = 0;
23883 glyph->glyph_not_available_p = 0;
23884 glyph->face_id = face_id;
23885 glyph->font_type = FONT_TYPE_UNKNOWN;
23886 if (it->bidi_p)
23888 glyph->resolved_level = it->bidi_it.resolved_level;
23889 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23890 abort ();
23891 glyph->bidi_type = it->bidi_it.type;
23893 ++it->glyph_row->used[area];
23895 else
23896 IT_EXPAND_MATRIX_WIDTH (it, area);
23900 /* Produce a glyph for a glyphless character for iterator IT.
23901 IT->glyphless_method specifies which method to use for displaying
23902 the character. See the description of enum
23903 glyphless_display_method in dispextern.h for the detail.
23905 FOR_NO_FONT is nonzero if and only if this is for a character for
23906 which no font was found. ACRONYM, if non-nil, is an acronym string
23907 for the character. */
23909 static void
23910 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
23912 int face_id;
23913 struct face *face;
23914 struct font *font;
23915 int base_width, base_height, width, height;
23916 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
23917 int len;
23919 /* Get the metrics of the base font. We always refer to the current
23920 ASCII face. */
23921 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
23922 font = face->font ? face->font : FRAME_FONT (it->f);
23923 it->ascent = FONT_BASE (font) + font->baseline_offset;
23924 it->descent = FONT_DESCENT (font) - font->baseline_offset;
23925 base_height = it->ascent + it->descent;
23926 base_width = font->average_width;
23928 /* Get a face ID for the glyph by utilizing a cache (the same way as
23929 done for `escape-glyph' in get_next_display_element). */
23930 if (it->f == last_glyphless_glyph_frame
23931 && it->face_id == last_glyphless_glyph_face_id)
23933 face_id = last_glyphless_glyph_merged_face_id;
23935 else
23937 /* Merge the `glyphless-char' face into the current face. */
23938 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
23939 last_glyphless_glyph_frame = it->f;
23940 last_glyphless_glyph_face_id = it->face_id;
23941 last_glyphless_glyph_merged_face_id = face_id;
23944 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
23946 it->pixel_width = THIN_SPACE_WIDTH;
23947 len = 0;
23948 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23950 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
23952 width = CHAR_WIDTH (it->c);
23953 if (width == 0)
23954 width = 1;
23955 else if (width > 4)
23956 width = 4;
23957 it->pixel_width = base_width * width;
23958 len = 0;
23959 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23961 else
23963 char buf[7];
23964 const char *str;
23965 unsigned int code[6];
23966 int upper_len;
23967 int ascent, descent;
23968 struct font_metrics metrics_upper, metrics_lower;
23970 face = FACE_FROM_ID (it->f, face_id);
23971 font = face->font ? face->font : FRAME_FONT (it->f);
23972 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23974 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
23976 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
23977 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
23978 if (CONSP (acronym))
23979 acronym = XCAR (acronym);
23980 str = STRINGP (acronym) ? SSDATA (acronym) : "";
23982 else
23984 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
23985 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
23986 str = buf;
23988 for (len = 0; str[len] && ASCII_BYTE_P (str[len]); len++)
23989 code[len] = font->driver->encode_char (font, str[len]);
23990 upper_len = (len + 1) / 2;
23991 font->driver->text_extents (font, code, upper_len,
23992 &metrics_upper);
23993 font->driver->text_extents (font, code + upper_len, len - upper_len,
23994 &metrics_lower);
23998 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
23999 width = max (metrics_upper.width, metrics_lower.width) + 4;
24000 upper_xoff = upper_yoff = 2; /* the typical case */
24001 if (base_width >= width)
24003 /* Align the upper to the left, the lower to the right. */
24004 it->pixel_width = base_width;
24005 lower_xoff = base_width - 2 - metrics_lower.width;
24007 else
24009 /* Center the shorter one. */
24010 it->pixel_width = width;
24011 if (metrics_upper.width >= metrics_lower.width)
24012 lower_xoff = (width - metrics_lower.width) / 2;
24013 else
24015 /* FIXME: This code doesn't look right. It formerly was
24016 missing the "lower_xoff = 0;", which couldn't have
24017 been right since it left lower_xoff uninitialized. */
24018 lower_xoff = 0;
24019 upper_xoff = (width - metrics_upper.width) / 2;
24023 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24024 top, bottom, and between upper and lower strings. */
24025 height = (metrics_upper.ascent + metrics_upper.descent
24026 + metrics_lower.ascent + metrics_lower.descent) + 5;
24027 /* Center vertically.
24028 H:base_height, D:base_descent
24029 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24031 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24032 descent = D - H/2 + h/2;
24033 lower_yoff = descent - 2 - ld;
24034 upper_yoff = lower_yoff - la - 1 - ud; */
24035 ascent = - (it->descent - (base_height + height + 1) / 2);
24036 descent = it->descent - (base_height - height) / 2;
24037 lower_yoff = descent - 2 - metrics_lower.descent;
24038 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24039 - metrics_upper.descent);
24040 /* Don't make the height shorter than the base height. */
24041 if (height > base_height)
24043 it->ascent = ascent;
24044 it->descent = descent;
24048 it->phys_ascent = it->ascent;
24049 it->phys_descent = it->descent;
24050 if (it->glyph_row)
24051 append_glyphless_glyph (it, face_id, for_no_font, len,
24052 upper_xoff, upper_yoff,
24053 lower_xoff, lower_yoff);
24054 it->nglyphs = 1;
24055 take_vertical_position_into_account (it);
24059 /* RIF:
24060 Produce glyphs/get display metrics for the display element IT is
24061 loaded with. See the description of struct it in dispextern.h
24062 for an overview of struct it. */
24064 void
24065 x_produce_glyphs (struct it *it)
24067 int extra_line_spacing = it->extra_line_spacing;
24069 it->glyph_not_available_p = 0;
24071 if (it->what == IT_CHARACTER)
24073 XChar2b char2b;
24074 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24075 struct font *font = face->font;
24076 struct font_metrics *pcm = NULL;
24077 int boff; /* baseline offset */
24079 if (font == NULL)
24081 /* When no suitable font is found, display this character by
24082 the method specified in the first extra slot of
24083 Vglyphless_char_display. */
24084 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24086 xassert (it->what == IT_GLYPHLESS);
24087 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24088 goto done;
24091 boff = font->baseline_offset;
24092 if (font->vertical_centering)
24093 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24095 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24097 int stretched_p;
24099 it->nglyphs = 1;
24101 if (it->override_ascent >= 0)
24103 it->ascent = it->override_ascent;
24104 it->descent = it->override_descent;
24105 boff = it->override_boff;
24107 else
24109 it->ascent = FONT_BASE (font) + boff;
24110 it->descent = FONT_DESCENT (font) - boff;
24113 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24115 pcm = get_per_char_metric (font, &char2b);
24116 if (pcm->width == 0
24117 && pcm->rbearing == 0 && pcm->lbearing == 0)
24118 pcm = NULL;
24121 if (pcm)
24123 it->phys_ascent = pcm->ascent + boff;
24124 it->phys_descent = pcm->descent - boff;
24125 it->pixel_width = pcm->width;
24127 else
24129 it->glyph_not_available_p = 1;
24130 it->phys_ascent = it->ascent;
24131 it->phys_descent = it->descent;
24132 it->pixel_width = font->space_width;
24135 if (it->constrain_row_ascent_descent_p)
24137 if (it->descent > it->max_descent)
24139 it->ascent += it->descent - it->max_descent;
24140 it->descent = it->max_descent;
24142 if (it->ascent > it->max_ascent)
24144 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24145 it->ascent = it->max_ascent;
24147 it->phys_ascent = min (it->phys_ascent, it->ascent);
24148 it->phys_descent = min (it->phys_descent, it->descent);
24149 extra_line_spacing = 0;
24152 /* If this is a space inside a region of text with
24153 `space-width' property, change its width. */
24154 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24155 if (stretched_p)
24156 it->pixel_width *= XFLOATINT (it->space_width);
24158 /* If face has a box, add the box thickness to the character
24159 height. If character has a box line to the left and/or
24160 right, add the box line width to the character's width. */
24161 if (face->box != FACE_NO_BOX)
24163 int thick = face->box_line_width;
24165 if (thick > 0)
24167 it->ascent += thick;
24168 it->descent += thick;
24170 else
24171 thick = -thick;
24173 if (it->start_of_box_run_p)
24174 it->pixel_width += thick;
24175 if (it->end_of_box_run_p)
24176 it->pixel_width += thick;
24179 /* If face has an overline, add the height of the overline
24180 (1 pixel) and a 1 pixel margin to the character height. */
24181 if (face->overline_p)
24182 it->ascent += overline_margin;
24184 if (it->constrain_row_ascent_descent_p)
24186 if (it->ascent > it->max_ascent)
24187 it->ascent = it->max_ascent;
24188 if (it->descent > it->max_descent)
24189 it->descent = it->max_descent;
24192 take_vertical_position_into_account (it);
24194 /* If we have to actually produce glyphs, do it. */
24195 if (it->glyph_row)
24197 if (stretched_p)
24199 /* Translate a space with a `space-width' property
24200 into a stretch glyph. */
24201 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24202 / FONT_HEIGHT (font));
24203 append_stretch_glyph (it, it->object, it->pixel_width,
24204 it->ascent + it->descent, ascent);
24206 else
24207 append_glyph (it);
24209 /* If characters with lbearing or rbearing are displayed
24210 in this line, record that fact in a flag of the
24211 glyph row. This is used to optimize X output code. */
24212 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24213 it->glyph_row->contains_overlapping_glyphs_p = 1;
24215 if (! stretched_p && it->pixel_width == 0)
24216 /* We assure that all visible glyphs have at least 1-pixel
24217 width. */
24218 it->pixel_width = 1;
24220 else if (it->char_to_display == '\n')
24222 /* A newline has no width, but we need the height of the
24223 line. But if previous part of the line sets a height,
24224 don't increase that height */
24226 Lisp_Object height;
24227 Lisp_Object total_height = Qnil;
24229 it->override_ascent = -1;
24230 it->pixel_width = 0;
24231 it->nglyphs = 0;
24233 height = get_it_property (it, Qline_height);
24234 /* Split (line-height total-height) list */
24235 if (CONSP (height)
24236 && CONSP (XCDR (height))
24237 && NILP (XCDR (XCDR (height))))
24239 total_height = XCAR (XCDR (height));
24240 height = XCAR (height);
24242 height = calc_line_height_property (it, height, font, boff, 1);
24244 if (it->override_ascent >= 0)
24246 it->ascent = it->override_ascent;
24247 it->descent = it->override_descent;
24248 boff = it->override_boff;
24250 else
24252 it->ascent = FONT_BASE (font) + boff;
24253 it->descent = FONT_DESCENT (font) - boff;
24256 if (EQ (height, Qt))
24258 if (it->descent > it->max_descent)
24260 it->ascent += it->descent - it->max_descent;
24261 it->descent = it->max_descent;
24263 if (it->ascent > it->max_ascent)
24265 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24266 it->ascent = it->max_ascent;
24268 it->phys_ascent = min (it->phys_ascent, it->ascent);
24269 it->phys_descent = min (it->phys_descent, it->descent);
24270 it->constrain_row_ascent_descent_p = 1;
24271 extra_line_spacing = 0;
24273 else
24275 Lisp_Object spacing;
24277 it->phys_ascent = it->ascent;
24278 it->phys_descent = it->descent;
24280 if ((it->max_ascent > 0 || it->max_descent > 0)
24281 && face->box != FACE_NO_BOX
24282 && face->box_line_width > 0)
24284 it->ascent += face->box_line_width;
24285 it->descent += face->box_line_width;
24287 if (!NILP (height)
24288 && XINT (height) > it->ascent + it->descent)
24289 it->ascent = XINT (height) - it->descent;
24291 if (!NILP (total_height))
24292 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24293 else
24295 spacing = get_it_property (it, Qline_spacing);
24296 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24298 if (INTEGERP (spacing))
24300 extra_line_spacing = XINT (spacing);
24301 if (!NILP (total_height))
24302 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24306 else /* i.e. (it->char_to_display == '\t') */
24308 if (font->space_width > 0)
24310 int tab_width = it->tab_width * font->space_width;
24311 int x = it->current_x + it->continuation_lines_width;
24312 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24314 /* If the distance from the current position to the next tab
24315 stop is less than a space character width, use the
24316 tab stop after that. */
24317 if (next_tab_x - x < font->space_width)
24318 next_tab_x += tab_width;
24320 it->pixel_width = next_tab_x - x;
24321 it->nglyphs = 1;
24322 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24323 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24325 if (it->glyph_row)
24327 append_stretch_glyph (it, it->object, it->pixel_width,
24328 it->ascent + it->descent, it->ascent);
24331 else
24333 it->pixel_width = 0;
24334 it->nglyphs = 1;
24338 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24340 /* A static composition.
24342 Note: A composition is represented as one glyph in the
24343 glyph matrix. There are no padding glyphs.
24345 Important note: pixel_width, ascent, and descent are the
24346 values of what is drawn by draw_glyphs (i.e. the values of
24347 the overall glyphs composed). */
24348 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24349 int boff; /* baseline offset */
24350 struct composition *cmp = composition_table[it->cmp_it.id];
24351 int glyph_len = cmp->glyph_len;
24352 struct font *font = face->font;
24354 it->nglyphs = 1;
24356 /* If we have not yet calculated pixel size data of glyphs of
24357 the composition for the current face font, calculate them
24358 now. Theoretically, we have to check all fonts for the
24359 glyphs, but that requires much time and memory space. So,
24360 here we check only the font of the first glyph. This may
24361 lead to incorrect display, but it's very rare, and C-l
24362 (recenter-top-bottom) can correct the display anyway. */
24363 if (! cmp->font || cmp->font != font)
24365 /* Ascent and descent of the font of the first character
24366 of this composition (adjusted by baseline offset).
24367 Ascent and descent of overall glyphs should not be less
24368 than these, respectively. */
24369 int font_ascent, font_descent, font_height;
24370 /* Bounding box of the overall glyphs. */
24371 int leftmost, rightmost, lowest, highest;
24372 int lbearing, rbearing;
24373 int i, width, ascent, descent;
24374 int left_padded = 0, right_padded = 0;
24375 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24376 XChar2b char2b;
24377 struct font_metrics *pcm;
24378 int font_not_found_p;
24379 EMACS_INT pos;
24381 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24382 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24383 break;
24384 if (glyph_len < cmp->glyph_len)
24385 right_padded = 1;
24386 for (i = 0; i < glyph_len; i++)
24388 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24389 break;
24390 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24392 if (i > 0)
24393 left_padded = 1;
24395 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24396 : IT_CHARPOS (*it));
24397 /* If no suitable font is found, use the default font. */
24398 font_not_found_p = font == NULL;
24399 if (font_not_found_p)
24401 face = face->ascii_face;
24402 font = face->font;
24404 boff = font->baseline_offset;
24405 if (font->vertical_centering)
24406 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24407 font_ascent = FONT_BASE (font) + boff;
24408 font_descent = FONT_DESCENT (font) - boff;
24409 font_height = FONT_HEIGHT (font);
24411 cmp->font = (void *) font;
24413 pcm = NULL;
24414 if (! font_not_found_p)
24416 get_char_face_and_encoding (it->f, c, it->face_id,
24417 &char2b, 0);
24418 pcm = get_per_char_metric (font, &char2b);
24421 /* Initialize the bounding box. */
24422 if (pcm)
24424 width = cmp->glyph_len > 0 ? pcm->width : 0;
24425 ascent = pcm->ascent;
24426 descent = pcm->descent;
24427 lbearing = pcm->lbearing;
24428 rbearing = pcm->rbearing;
24430 else
24432 width = cmp->glyph_len > 0 ? font->space_width : 0;
24433 ascent = FONT_BASE (font);
24434 descent = FONT_DESCENT (font);
24435 lbearing = 0;
24436 rbearing = width;
24439 rightmost = width;
24440 leftmost = 0;
24441 lowest = - descent + boff;
24442 highest = ascent + boff;
24444 if (! font_not_found_p
24445 && font->default_ascent
24446 && CHAR_TABLE_P (Vuse_default_ascent)
24447 && !NILP (Faref (Vuse_default_ascent,
24448 make_number (it->char_to_display))))
24449 highest = font->default_ascent + boff;
24451 /* Draw the first glyph at the normal position. It may be
24452 shifted to right later if some other glyphs are drawn
24453 at the left. */
24454 cmp->offsets[i * 2] = 0;
24455 cmp->offsets[i * 2 + 1] = boff;
24456 cmp->lbearing = lbearing;
24457 cmp->rbearing = rbearing;
24459 /* Set cmp->offsets for the remaining glyphs. */
24460 for (i++; i < glyph_len; i++)
24462 int left, right, btm, top;
24463 int ch = COMPOSITION_GLYPH (cmp, i);
24464 int face_id;
24465 struct face *this_face;
24467 if (ch == '\t')
24468 ch = ' ';
24469 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24470 this_face = FACE_FROM_ID (it->f, face_id);
24471 font = this_face->font;
24473 if (font == NULL)
24474 pcm = NULL;
24475 else
24477 get_char_face_and_encoding (it->f, ch, face_id,
24478 &char2b, 0);
24479 pcm = get_per_char_metric (font, &char2b);
24481 if (! pcm)
24482 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24483 else
24485 width = pcm->width;
24486 ascent = pcm->ascent;
24487 descent = pcm->descent;
24488 lbearing = pcm->lbearing;
24489 rbearing = pcm->rbearing;
24490 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
24492 /* Relative composition with or without
24493 alternate chars. */
24494 left = (leftmost + rightmost - width) / 2;
24495 btm = - descent + boff;
24496 if (font->relative_compose
24497 && (! CHAR_TABLE_P (Vignore_relative_composition)
24498 || NILP (Faref (Vignore_relative_composition,
24499 make_number (ch)))))
24502 if (- descent >= font->relative_compose)
24503 /* One extra pixel between two glyphs. */
24504 btm = highest + 1;
24505 else if (ascent <= 0)
24506 /* One extra pixel between two glyphs. */
24507 btm = lowest - 1 - ascent - descent;
24510 else
24512 /* A composition rule is specified by an integer
24513 value that encodes global and new reference
24514 points (GREF and NREF). GREF and NREF are
24515 specified by numbers as below:
24517 0---1---2 -- ascent
24521 9--10--11 -- center
24523 ---3---4---5--- baseline
24525 6---7---8 -- descent
24527 int rule = COMPOSITION_RULE (cmp, i);
24528 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
24530 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
24531 grefx = gref % 3, nrefx = nref % 3;
24532 grefy = gref / 3, nrefy = nref / 3;
24533 if (xoff)
24534 xoff = font_height * (xoff - 128) / 256;
24535 if (yoff)
24536 yoff = font_height * (yoff - 128) / 256;
24538 left = (leftmost
24539 + grefx * (rightmost - leftmost) / 2
24540 - nrefx * width / 2
24541 + xoff);
24543 btm = ((grefy == 0 ? highest
24544 : grefy == 1 ? 0
24545 : grefy == 2 ? lowest
24546 : (highest + lowest) / 2)
24547 - (nrefy == 0 ? ascent + descent
24548 : nrefy == 1 ? descent - boff
24549 : nrefy == 2 ? 0
24550 : (ascent + descent) / 2)
24551 + yoff);
24554 cmp->offsets[i * 2] = left;
24555 cmp->offsets[i * 2 + 1] = btm + descent;
24557 /* Update the bounding box of the overall glyphs. */
24558 if (width > 0)
24560 right = left + width;
24561 if (left < leftmost)
24562 leftmost = left;
24563 if (right > rightmost)
24564 rightmost = right;
24566 top = btm + descent + ascent;
24567 if (top > highest)
24568 highest = top;
24569 if (btm < lowest)
24570 lowest = btm;
24572 if (cmp->lbearing > left + lbearing)
24573 cmp->lbearing = left + lbearing;
24574 if (cmp->rbearing < left + rbearing)
24575 cmp->rbearing = left + rbearing;
24579 /* If there are glyphs whose x-offsets are negative,
24580 shift all glyphs to the right and make all x-offsets
24581 non-negative. */
24582 if (leftmost < 0)
24584 for (i = 0; i < cmp->glyph_len; i++)
24585 cmp->offsets[i * 2] -= leftmost;
24586 rightmost -= leftmost;
24587 cmp->lbearing -= leftmost;
24588 cmp->rbearing -= leftmost;
24591 if (left_padded && cmp->lbearing < 0)
24593 for (i = 0; i < cmp->glyph_len; i++)
24594 cmp->offsets[i * 2] -= cmp->lbearing;
24595 rightmost -= cmp->lbearing;
24596 cmp->rbearing -= cmp->lbearing;
24597 cmp->lbearing = 0;
24599 if (right_padded && rightmost < cmp->rbearing)
24601 rightmost = cmp->rbearing;
24604 cmp->pixel_width = rightmost;
24605 cmp->ascent = highest;
24606 cmp->descent = - lowest;
24607 if (cmp->ascent < font_ascent)
24608 cmp->ascent = font_ascent;
24609 if (cmp->descent < font_descent)
24610 cmp->descent = font_descent;
24613 if (it->glyph_row
24614 && (cmp->lbearing < 0
24615 || cmp->rbearing > cmp->pixel_width))
24616 it->glyph_row->contains_overlapping_glyphs_p = 1;
24618 it->pixel_width = cmp->pixel_width;
24619 it->ascent = it->phys_ascent = cmp->ascent;
24620 it->descent = it->phys_descent = cmp->descent;
24621 if (face->box != FACE_NO_BOX)
24623 int thick = face->box_line_width;
24625 if (thick > 0)
24627 it->ascent += thick;
24628 it->descent += thick;
24630 else
24631 thick = - thick;
24633 if (it->start_of_box_run_p)
24634 it->pixel_width += thick;
24635 if (it->end_of_box_run_p)
24636 it->pixel_width += thick;
24639 /* If face has an overline, add the height of the overline
24640 (1 pixel) and a 1 pixel margin to the character height. */
24641 if (face->overline_p)
24642 it->ascent += overline_margin;
24644 take_vertical_position_into_account (it);
24645 if (it->ascent < 0)
24646 it->ascent = 0;
24647 if (it->descent < 0)
24648 it->descent = 0;
24650 if (it->glyph_row && cmp->glyph_len > 0)
24651 append_composite_glyph (it);
24653 else if (it->what == IT_COMPOSITION)
24655 /* A dynamic (automatic) composition. */
24656 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24657 Lisp_Object gstring;
24658 struct font_metrics metrics;
24660 it->nglyphs = 1;
24662 gstring = composition_gstring_from_id (it->cmp_it.id);
24663 it->pixel_width
24664 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
24665 &metrics);
24666 if (it->glyph_row
24667 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
24668 it->glyph_row->contains_overlapping_glyphs_p = 1;
24669 it->ascent = it->phys_ascent = metrics.ascent;
24670 it->descent = it->phys_descent = metrics.descent;
24671 if (face->box != FACE_NO_BOX)
24673 int thick = face->box_line_width;
24675 if (thick > 0)
24677 it->ascent += thick;
24678 it->descent += thick;
24680 else
24681 thick = - thick;
24683 if (it->start_of_box_run_p)
24684 it->pixel_width += thick;
24685 if (it->end_of_box_run_p)
24686 it->pixel_width += thick;
24688 /* If face has an overline, add the height of the overline
24689 (1 pixel) and a 1 pixel margin to the character height. */
24690 if (face->overline_p)
24691 it->ascent += overline_margin;
24692 take_vertical_position_into_account (it);
24693 if (it->ascent < 0)
24694 it->ascent = 0;
24695 if (it->descent < 0)
24696 it->descent = 0;
24698 if (it->glyph_row)
24699 append_composite_glyph (it);
24701 else if (it->what == IT_GLYPHLESS)
24702 produce_glyphless_glyph (it, 0, Qnil);
24703 else if (it->what == IT_IMAGE)
24704 produce_image_glyph (it);
24705 else if (it->what == IT_STRETCH)
24706 produce_stretch_glyph (it);
24708 done:
24709 /* Accumulate dimensions. Note: can't assume that it->descent > 0
24710 because this isn't true for images with `:ascent 100'. */
24711 xassert (it->ascent >= 0 && it->descent >= 0);
24712 if (it->area == TEXT_AREA)
24713 it->current_x += it->pixel_width;
24715 if (extra_line_spacing > 0)
24717 it->descent += extra_line_spacing;
24718 if (extra_line_spacing > it->max_extra_line_spacing)
24719 it->max_extra_line_spacing = extra_line_spacing;
24722 it->max_ascent = max (it->max_ascent, it->ascent);
24723 it->max_descent = max (it->max_descent, it->descent);
24724 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
24725 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
24728 /* EXPORT for RIF:
24729 Output LEN glyphs starting at START at the nominal cursor position.
24730 Advance the nominal cursor over the text. The global variable
24731 updated_window contains the window being updated, updated_row is
24732 the glyph row being updated, and updated_area is the area of that
24733 row being updated. */
24735 void
24736 x_write_glyphs (struct glyph *start, int len)
24738 int x, hpos, chpos = updated_window->phys_cursor.hpos;
24740 xassert (updated_window && updated_row);
24741 /* When the window is hscrolled, cursor hpos can legitimately be out
24742 of bounds, but we draw the cursor at the corresponding window
24743 margin in that case. */
24744 if (!updated_row->reversed_p && chpos < 0)
24745 chpos = 0;
24746 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
24747 chpos = updated_row->used[TEXT_AREA] - 1;
24749 BLOCK_INPUT;
24751 /* Write glyphs. */
24753 hpos = start - updated_row->glyphs[updated_area];
24754 x = draw_glyphs (updated_window, output_cursor.x,
24755 updated_row, updated_area,
24756 hpos, hpos + len,
24757 DRAW_NORMAL_TEXT, 0);
24759 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
24760 if (updated_area == TEXT_AREA
24761 && updated_window->phys_cursor_on_p
24762 && updated_window->phys_cursor.vpos == output_cursor.vpos
24763 && chpos >= hpos
24764 && chpos < hpos + len)
24765 updated_window->phys_cursor_on_p = 0;
24767 UNBLOCK_INPUT;
24769 /* Advance the output cursor. */
24770 output_cursor.hpos += len;
24771 output_cursor.x = x;
24775 /* EXPORT for RIF:
24776 Insert LEN glyphs from START at the nominal cursor position. */
24778 void
24779 x_insert_glyphs (struct glyph *start, int len)
24781 struct frame *f;
24782 struct window *w;
24783 int line_height, shift_by_width, shifted_region_width;
24784 struct glyph_row *row;
24785 struct glyph *glyph;
24786 int frame_x, frame_y;
24787 EMACS_INT hpos;
24789 xassert (updated_window && updated_row);
24790 BLOCK_INPUT;
24791 w = updated_window;
24792 f = XFRAME (WINDOW_FRAME (w));
24794 /* Get the height of the line we are in. */
24795 row = updated_row;
24796 line_height = row->height;
24798 /* Get the width of the glyphs to insert. */
24799 shift_by_width = 0;
24800 for (glyph = start; glyph < start + len; ++glyph)
24801 shift_by_width += glyph->pixel_width;
24803 /* Get the width of the region to shift right. */
24804 shifted_region_width = (window_box_width (w, updated_area)
24805 - output_cursor.x
24806 - shift_by_width);
24808 /* Shift right. */
24809 frame_x = window_box_left (w, updated_area) + output_cursor.x;
24810 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
24812 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
24813 line_height, shift_by_width);
24815 /* Write the glyphs. */
24816 hpos = start - row->glyphs[updated_area];
24817 draw_glyphs (w, output_cursor.x, row, updated_area,
24818 hpos, hpos + len,
24819 DRAW_NORMAL_TEXT, 0);
24821 /* Advance the output cursor. */
24822 output_cursor.hpos += len;
24823 output_cursor.x += shift_by_width;
24824 UNBLOCK_INPUT;
24828 /* EXPORT for RIF:
24829 Erase the current text line from the nominal cursor position
24830 (inclusive) to pixel column TO_X (exclusive). The idea is that
24831 everything from TO_X onward is already erased.
24833 TO_X is a pixel position relative to updated_area of
24834 updated_window. TO_X == -1 means clear to the end of this area. */
24836 void
24837 x_clear_end_of_line (int to_x)
24839 struct frame *f;
24840 struct window *w = updated_window;
24841 int max_x, min_y, max_y;
24842 int from_x, from_y, to_y;
24844 xassert (updated_window && updated_row);
24845 f = XFRAME (w->frame);
24847 if (updated_row->full_width_p)
24848 max_x = WINDOW_TOTAL_WIDTH (w);
24849 else
24850 max_x = window_box_width (w, updated_area);
24851 max_y = window_text_bottom_y (w);
24853 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
24854 of window. For TO_X > 0, truncate to end of drawing area. */
24855 if (to_x == 0)
24856 return;
24857 else if (to_x < 0)
24858 to_x = max_x;
24859 else
24860 to_x = min (to_x, max_x);
24862 to_y = min (max_y, output_cursor.y + updated_row->height);
24864 /* Notice if the cursor will be cleared by this operation. */
24865 if (!updated_row->full_width_p)
24866 notice_overwritten_cursor (w, updated_area,
24867 output_cursor.x, -1,
24868 updated_row->y,
24869 MATRIX_ROW_BOTTOM_Y (updated_row));
24871 from_x = output_cursor.x;
24873 /* Translate to frame coordinates. */
24874 if (updated_row->full_width_p)
24876 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
24877 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
24879 else
24881 int area_left = window_box_left (w, updated_area);
24882 from_x += area_left;
24883 to_x += area_left;
24886 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
24887 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
24888 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
24890 /* Prevent inadvertently clearing to end of the X window. */
24891 if (to_x > from_x && to_y > from_y)
24893 BLOCK_INPUT;
24894 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
24895 to_x - from_x, to_y - from_y);
24896 UNBLOCK_INPUT;
24900 #endif /* HAVE_WINDOW_SYSTEM */
24904 /***********************************************************************
24905 Cursor types
24906 ***********************************************************************/
24908 /* Value is the internal representation of the specified cursor type
24909 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
24910 of the bar cursor. */
24912 static enum text_cursor_kinds
24913 get_specified_cursor_type (Lisp_Object arg, int *width)
24915 enum text_cursor_kinds type;
24917 if (NILP (arg))
24918 return NO_CURSOR;
24920 if (EQ (arg, Qbox))
24921 return FILLED_BOX_CURSOR;
24923 if (EQ (arg, Qhollow))
24924 return HOLLOW_BOX_CURSOR;
24926 if (EQ (arg, Qbar))
24928 *width = 2;
24929 return BAR_CURSOR;
24932 if (CONSP (arg)
24933 && EQ (XCAR (arg), Qbar)
24934 && INTEGERP (XCDR (arg))
24935 && XINT (XCDR (arg)) >= 0)
24937 *width = XINT (XCDR (arg));
24938 return BAR_CURSOR;
24941 if (EQ (arg, Qhbar))
24943 *width = 2;
24944 return HBAR_CURSOR;
24947 if (CONSP (arg)
24948 && EQ (XCAR (arg), Qhbar)
24949 && INTEGERP (XCDR (arg))
24950 && XINT (XCDR (arg)) >= 0)
24952 *width = XINT (XCDR (arg));
24953 return HBAR_CURSOR;
24956 /* Treat anything unknown as "hollow box cursor".
24957 It was bad to signal an error; people have trouble fixing
24958 .Xdefaults with Emacs, when it has something bad in it. */
24959 type = HOLLOW_BOX_CURSOR;
24961 return type;
24964 /* Set the default cursor types for specified frame. */
24965 void
24966 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
24968 int width = 1;
24969 Lisp_Object tem;
24971 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
24972 FRAME_CURSOR_WIDTH (f) = width;
24974 /* By default, set up the blink-off state depending on the on-state. */
24976 tem = Fassoc (arg, Vblink_cursor_alist);
24977 if (!NILP (tem))
24979 FRAME_BLINK_OFF_CURSOR (f)
24980 = get_specified_cursor_type (XCDR (tem), &width);
24981 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
24983 else
24984 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
24988 #ifdef HAVE_WINDOW_SYSTEM
24990 /* Return the cursor we want to be displayed in window W. Return
24991 width of bar/hbar cursor through WIDTH arg. Return with
24992 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
24993 (i.e. if the `system caret' should track this cursor).
24995 In a mini-buffer window, we want the cursor only to appear if we
24996 are reading input from this window. For the selected window, we
24997 want the cursor type given by the frame parameter or buffer local
24998 setting of cursor-type. If explicitly marked off, draw no cursor.
24999 In all other cases, we want a hollow box cursor. */
25001 static enum text_cursor_kinds
25002 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25003 int *active_cursor)
25005 struct frame *f = XFRAME (w->frame);
25006 struct buffer *b = XBUFFER (w->buffer);
25007 int cursor_type = DEFAULT_CURSOR;
25008 Lisp_Object alt_cursor;
25009 int non_selected = 0;
25011 *active_cursor = 1;
25013 /* Echo area */
25014 if (cursor_in_echo_area
25015 && FRAME_HAS_MINIBUF_P (f)
25016 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25018 if (w == XWINDOW (echo_area_window))
25020 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25022 *width = FRAME_CURSOR_WIDTH (f);
25023 return FRAME_DESIRED_CURSOR (f);
25025 else
25026 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25029 *active_cursor = 0;
25030 non_selected = 1;
25033 /* Detect a nonselected window or nonselected frame. */
25034 else if (w != XWINDOW (f->selected_window)
25035 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25037 *active_cursor = 0;
25039 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25040 return NO_CURSOR;
25042 non_selected = 1;
25045 /* Never display a cursor in a window in which cursor-type is nil. */
25046 if (NILP (BVAR (b, cursor_type)))
25047 return NO_CURSOR;
25049 /* Get the normal cursor type for this window. */
25050 if (EQ (BVAR (b, cursor_type), Qt))
25052 cursor_type = FRAME_DESIRED_CURSOR (f);
25053 *width = FRAME_CURSOR_WIDTH (f);
25055 else
25056 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25058 /* Use cursor-in-non-selected-windows instead
25059 for non-selected window or frame. */
25060 if (non_selected)
25062 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25063 if (!EQ (Qt, alt_cursor))
25064 return get_specified_cursor_type (alt_cursor, width);
25065 /* t means modify the normal cursor type. */
25066 if (cursor_type == FILLED_BOX_CURSOR)
25067 cursor_type = HOLLOW_BOX_CURSOR;
25068 else if (cursor_type == BAR_CURSOR && *width > 1)
25069 --*width;
25070 return cursor_type;
25073 /* Use normal cursor if not blinked off. */
25074 if (!w->cursor_off_p)
25076 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25078 if (cursor_type == FILLED_BOX_CURSOR)
25080 /* Using a block cursor on large images can be very annoying.
25081 So use a hollow cursor for "large" images.
25082 If image is not transparent (no mask), also use hollow cursor. */
25083 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25084 if (img != NULL && IMAGEP (img->spec))
25086 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25087 where N = size of default frame font size.
25088 This should cover most of the "tiny" icons people may use. */
25089 if (!img->mask
25090 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25091 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25092 cursor_type = HOLLOW_BOX_CURSOR;
25095 else if (cursor_type != NO_CURSOR)
25097 /* Display current only supports BOX and HOLLOW cursors for images.
25098 So for now, unconditionally use a HOLLOW cursor when cursor is
25099 not a solid box cursor. */
25100 cursor_type = HOLLOW_BOX_CURSOR;
25103 return cursor_type;
25106 /* Cursor is blinked off, so determine how to "toggle" it. */
25108 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25109 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25110 return get_specified_cursor_type (XCDR (alt_cursor), width);
25112 /* Then see if frame has specified a specific blink off cursor type. */
25113 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25115 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25116 return FRAME_BLINK_OFF_CURSOR (f);
25119 #if 0
25120 /* Some people liked having a permanently visible blinking cursor,
25121 while others had very strong opinions against it. So it was
25122 decided to remove it. KFS 2003-09-03 */
25124 /* Finally perform built-in cursor blinking:
25125 filled box <-> hollow box
25126 wide [h]bar <-> narrow [h]bar
25127 narrow [h]bar <-> no cursor
25128 other type <-> no cursor */
25130 if (cursor_type == FILLED_BOX_CURSOR)
25131 return HOLLOW_BOX_CURSOR;
25133 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25135 *width = 1;
25136 return cursor_type;
25138 #endif
25140 return NO_CURSOR;
25144 /* Notice when the text cursor of window W has been completely
25145 overwritten by a drawing operation that outputs glyphs in AREA
25146 starting at X0 and ending at X1 in the line starting at Y0 and
25147 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25148 the rest of the line after X0 has been written. Y coordinates
25149 are window-relative. */
25151 static void
25152 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25153 int x0, int x1, int y0, int y1)
25155 int cx0, cx1, cy0, cy1;
25156 struct glyph_row *row;
25158 if (!w->phys_cursor_on_p)
25159 return;
25160 if (area != TEXT_AREA)
25161 return;
25163 if (w->phys_cursor.vpos < 0
25164 || w->phys_cursor.vpos >= w->current_matrix->nrows
25165 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25166 !(row->enabled_p && row->displays_text_p)))
25167 return;
25169 if (row->cursor_in_fringe_p)
25171 row->cursor_in_fringe_p = 0;
25172 draw_fringe_bitmap (w, row, row->reversed_p);
25173 w->phys_cursor_on_p = 0;
25174 return;
25177 cx0 = w->phys_cursor.x;
25178 cx1 = cx0 + w->phys_cursor_width;
25179 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25180 return;
25182 /* The cursor image will be completely removed from the
25183 screen if the output area intersects the cursor area in
25184 y-direction. When we draw in [y0 y1[, and some part of
25185 the cursor is at y < y0, that part must have been drawn
25186 before. When scrolling, the cursor is erased before
25187 actually scrolling, so we don't come here. When not
25188 scrolling, the rows above the old cursor row must have
25189 changed, and in this case these rows must have written
25190 over the cursor image.
25192 Likewise if part of the cursor is below y1, with the
25193 exception of the cursor being in the first blank row at
25194 the buffer and window end because update_text_area
25195 doesn't draw that row. (Except when it does, but
25196 that's handled in update_text_area.) */
25198 cy0 = w->phys_cursor.y;
25199 cy1 = cy0 + w->phys_cursor_height;
25200 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25201 return;
25203 w->phys_cursor_on_p = 0;
25206 #endif /* HAVE_WINDOW_SYSTEM */
25209 /************************************************************************
25210 Mouse Face
25211 ************************************************************************/
25213 #ifdef HAVE_WINDOW_SYSTEM
25215 /* EXPORT for RIF:
25216 Fix the display of area AREA of overlapping row ROW in window W
25217 with respect to the overlapping part OVERLAPS. */
25219 void
25220 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25221 enum glyph_row_area area, int overlaps)
25223 int i, x;
25225 BLOCK_INPUT;
25227 x = 0;
25228 for (i = 0; i < row->used[area];)
25230 if (row->glyphs[area][i].overlaps_vertically_p)
25232 int start = i, start_x = x;
25236 x += row->glyphs[area][i].pixel_width;
25237 ++i;
25239 while (i < row->used[area]
25240 && row->glyphs[area][i].overlaps_vertically_p);
25242 draw_glyphs (w, start_x, row, area,
25243 start, i,
25244 DRAW_NORMAL_TEXT, overlaps);
25246 else
25248 x += row->glyphs[area][i].pixel_width;
25249 ++i;
25253 UNBLOCK_INPUT;
25257 /* EXPORT:
25258 Draw the cursor glyph of window W in glyph row ROW. See the
25259 comment of draw_glyphs for the meaning of HL. */
25261 void
25262 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25263 enum draw_glyphs_face hl)
25265 /* If cursor hpos is out of bounds, don't draw garbage. This can
25266 happen in mini-buffer windows when switching between echo area
25267 glyphs and mini-buffer. */
25268 if ((row->reversed_p
25269 ? (w->phys_cursor.hpos >= 0)
25270 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25272 int on_p = w->phys_cursor_on_p;
25273 int x1;
25274 int hpos = w->phys_cursor.hpos;
25276 /* When the window is hscrolled, cursor hpos can legitimately be
25277 out of bounds, but we draw the cursor at the corresponding
25278 window margin in that case. */
25279 if (!row->reversed_p && hpos < 0)
25280 hpos = 0;
25281 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25282 hpos = row->used[TEXT_AREA] - 1;
25284 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25285 hl, 0);
25286 w->phys_cursor_on_p = on_p;
25288 if (hl == DRAW_CURSOR)
25289 w->phys_cursor_width = x1 - w->phys_cursor.x;
25290 /* When we erase the cursor, and ROW is overlapped by other
25291 rows, make sure that these overlapping parts of other rows
25292 are redrawn. */
25293 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25295 w->phys_cursor_width = x1 - w->phys_cursor.x;
25297 if (row > w->current_matrix->rows
25298 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25299 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25300 OVERLAPS_ERASED_CURSOR);
25302 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25303 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25304 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25305 OVERLAPS_ERASED_CURSOR);
25311 /* EXPORT:
25312 Erase the image of a cursor of window W from the screen. */
25314 void
25315 erase_phys_cursor (struct window *w)
25317 struct frame *f = XFRAME (w->frame);
25318 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25319 int hpos = w->phys_cursor.hpos;
25320 int vpos = w->phys_cursor.vpos;
25321 int mouse_face_here_p = 0;
25322 struct glyph_matrix *active_glyphs = w->current_matrix;
25323 struct glyph_row *cursor_row;
25324 struct glyph *cursor_glyph;
25325 enum draw_glyphs_face hl;
25327 /* No cursor displayed or row invalidated => nothing to do on the
25328 screen. */
25329 if (w->phys_cursor_type == NO_CURSOR)
25330 goto mark_cursor_off;
25332 /* VPOS >= active_glyphs->nrows means that window has been resized.
25333 Don't bother to erase the cursor. */
25334 if (vpos >= active_glyphs->nrows)
25335 goto mark_cursor_off;
25337 /* If row containing cursor is marked invalid, there is nothing we
25338 can do. */
25339 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25340 if (!cursor_row->enabled_p)
25341 goto mark_cursor_off;
25343 /* If line spacing is > 0, old cursor may only be partially visible in
25344 window after split-window. So adjust visible height. */
25345 cursor_row->visible_height = min (cursor_row->visible_height,
25346 window_text_bottom_y (w) - cursor_row->y);
25348 /* If row is completely invisible, don't attempt to delete a cursor which
25349 isn't there. This can happen if cursor is at top of a window, and
25350 we switch to a buffer with a header line in that window. */
25351 if (cursor_row->visible_height <= 0)
25352 goto mark_cursor_off;
25354 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25355 if (cursor_row->cursor_in_fringe_p)
25357 cursor_row->cursor_in_fringe_p = 0;
25358 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25359 goto mark_cursor_off;
25362 /* This can happen when the new row is shorter than the old one.
25363 In this case, either draw_glyphs or clear_end_of_line
25364 should have cleared the cursor. Note that we wouldn't be
25365 able to erase the cursor in this case because we don't have a
25366 cursor glyph at hand. */
25367 if ((cursor_row->reversed_p
25368 ? (w->phys_cursor.hpos < 0)
25369 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25370 goto mark_cursor_off;
25372 /* When the window is hscrolled, cursor hpos can legitimately be out
25373 of bounds, but we draw the cursor at the corresponding window
25374 margin in that case. */
25375 if (!cursor_row->reversed_p && hpos < 0)
25376 hpos = 0;
25377 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25378 hpos = cursor_row->used[TEXT_AREA] - 1;
25380 /* If the cursor is in the mouse face area, redisplay that when
25381 we clear the cursor. */
25382 if (! NILP (hlinfo->mouse_face_window)
25383 && coords_in_mouse_face_p (w, hpos, vpos)
25384 /* Don't redraw the cursor's spot in mouse face if it is at the
25385 end of a line (on a newline). The cursor appears there, but
25386 mouse highlighting does not. */
25387 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25388 mouse_face_here_p = 1;
25390 /* Maybe clear the display under the cursor. */
25391 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25393 int x, y, left_x;
25394 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25395 int width;
25397 cursor_glyph = get_phys_cursor_glyph (w);
25398 if (cursor_glyph == NULL)
25399 goto mark_cursor_off;
25401 width = cursor_glyph->pixel_width;
25402 left_x = window_box_left_offset (w, TEXT_AREA);
25403 x = w->phys_cursor.x;
25404 if (x < left_x)
25405 width -= left_x - x;
25406 width = min (width, window_box_width (w, TEXT_AREA) - x);
25407 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25408 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25410 if (width > 0)
25411 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25414 /* Erase the cursor by redrawing the character underneath it. */
25415 if (mouse_face_here_p)
25416 hl = DRAW_MOUSE_FACE;
25417 else
25418 hl = DRAW_NORMAL_TEXT;
25419 draw_phys_cursor_glyph (w, cursor_row, hl);
25421 mark_cursor_off:
25422 w->phys_cursor_on_p = 0;
25423 w->phys_cursor_type = NO_CURSOR;
25427 /* EXPORT:
25428 Display or clear cursor of window W. If ON is zero, clear the
25429 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25430 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25432 void
25433 display_and_set_cursor (struct window *w, int on,
25434 int hpos, int vpos, int x, int y)
25436 struct frame *f = XFRAME (w->frame);
25437 int new_cursor_type;
25438 int new_cursor_width;
25439 int active_cursor;
25440 struct glyph_row *glyph_row;
25441 struct glyph *glyph;
25443 /* This is pointless on invisible frames, and dangerous on garbaged
25444 windows and frames; in the latter case, the frame or window may
25445 be in the midst of changing its size, and x and y may be off the
25446 window. */
25447 if (! FRAME_VISIBLE_P (f)
25448 || FRAME_GARBAGED_P (f)
25449 || vpos >= w->current_matrix->nrows
25450 || hpos >= w->current_matrix->matrix_w)
25451 return;
25453 /* If cursor is off and we want it off, return quickly. */
25454 if (!on && !w->phys_cursor_on_p)
25455 return;
25457 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25458 /* If cursor row is not enabled, we don't really know where to
25459 display the cursor. */
25460 if (!glyph_row->enabled_p)
25462 w->phys_cursor_on_p = 0;
25463 return;
25466 glyph = NULL;
25467 if (!glyph_row->exact_window_width_line_p
25468 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25469 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25471 xassert (interrupt_input_blocked);
25473 /* Set new_cursor_type to the cursor we want to be displayed. */
25474 new_cursor_type = get_window_cursor_type (w, glyph,
25475 &new_cursor_width, &active_cursor);
25477 /* If cursor is currently being shown and we don't want it to be or
25478 it is in the wrong place, or the cursor type is not what we want,
25479 erase it. */
25480 if (w->phys_cursor_on_p
25481 && (!on
25482 || w->phys_cursor.x != x
25483 || w->phys_cursor.y != y
25484 || new_cursor_type != w->phys_cursor_type
25485 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
25486 && new_cursor_width != w->phys_cursor_width)))
25487 erase_phys_cursor (w);
25489 /* Don't check phys_cursor_on_p here because that flag is only set
25490 to zero in some cases where we know that the cursor has been
25491 completely erased, to avoid the extra work of erasing the cursor
25492 twice. In other words, phys_cursor_on_p can be 1 and the cursor
25493 still not be visible, or it has only been partly erased. */
25494 if (on)
25496 w->phys_cursor_ascent = glyph_row->ascent;
25497 w->phys_cursor_height = glyph_row->height;
25499 /* Set phys_cursor_.* before x_draw_.* is called because some
25500 of them may need the information. */
25501 w->phys_cursor.x = x;
25502 w->phys_cursor.y = glyph_row->y;
25503 w->phys_cursor.hpos = hpos;
25504 w->phys_cursor.vpos = vpos;
25507 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
25508 new_cursor_type, new_cursor_width,
25509 on, active_cursor);
25513 /* Switch the display of W's cursor on or off, according to the value
25514 of ON. */
25516 static void
25517 update_window_cursor (struct window *w, int on)
25519 /* Don't update cursor in windows whose frame is in the process
25520 of being deleted. */
25521 if (w->current_matrix)
25523 int hpos = w->phys_cursor.hpos;
25524 int vpos = w->phys_cursor.vpos;
25525 struct glyph_row *row;
25527 if (vpos >= w->current_matrix->nrows
25528 || hpos >= w->current_matrix->matrix_w)
25529 return;
25531 row = MATRIX_ROW (w->current_matrix, vpos);
25533 /* When the window is hscrolled, cursor hpos can legitimately be
25534 out of bounds, but we draw the cursor at the corresponding
25535 window margin in that case. */
25536 if (!row->reversed_p && hpos < 0)
25537 hpos = 0;
25538 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25539 hpos = row->used[TEXT_AREA] - 1;
25541 BLOCK_INPUT;
25542 display_and_set_cursor (w, on, hpos, vpos,
25543 w->phys_cursor.x, w->phys_cursor.y);
25544 UNBLOCK_INPUT;
25549 /* Call update_window_cursor with parameter ON_P on all leaf windows
25550 in the window tree rooted at W. */
25552 static void
25553 update_cursor_in_window_tree (struct window *w, int on_p)
25555 while (w)
25557 if (!NILP (w->hchild))
25558 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
25559 else if (!NILP (w->vchild))
25560 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
25561 else
25562 update_window_cursor (w, on_p);
25564 w = NILP (w->next) ? 0 : XWINDOW (w->next);
25569 /* EXPORT:
25570 Display the cursor on window W, or clear it, according to ON_P.
25571 Don't change the cursor's position. */
25573 void
25574 x_update_cursor (struct frame *f, int on_p)
25576 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
25580 /* EXPORT:
25581 Clear the cursor of window W to background color, and mark the
25582 cursor as not shown. This is used when the text where the cursor
25583 is about to be rewritten. */
25585 void
25586 x_clear_cursor (struct window *w)
25588 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
25589 update_window_cursor (w, 0);
25592 #endif /* HAVE_WINDOW_SYSTEM */
25594 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
25595 and MSDOS. */
25596 static void
25597 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
25598 int start_hpos, int end_hpos,
25599 enum draw_glyphs_face draw)
25601 #ifdef HAVE_WINDOW_SYSTEM
25602 if (FRAME_WINDOW_P (XFRAME (w->frame)))
25604 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
25605 return;
25607 #endif
25608 #if defined (HAVE_GPM) || defined (MSDOS)
25609 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
25610 #endif
25613 /* Display the active region described by mouse_face_* according to DRAW. */
25615 static void
25616 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
25618 struct window *w = XWINDOW (hlinfo->mouse_face_window);
25619 struct frame *f = XFRAME (WINDOW_FRAME (w));
25621 if (/* If window is in the process of being destroyed, don't bother
25622 to do anything. */
25623 w->current_matrix != NULL
25624 /* Don't update mouse highlight if hidden */
25625 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
25626 /* Recognize when we are called to operate on rows that don't exist
25627 anymore. This can happen when a window is split. */
25628 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
25630 int phys_cursor_on_p = w->phys_cursor_on_p;
25631 struct glyph_row *row, *first, *last;
25633 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
25634 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
25636 for (row = first; row <= last && row->enabled_p; ++row)
25638 int start_hpos, end_hpos, start_x;
25640 /* For all but the first row, the highlight starts at column 0. */
25641 if (row == first)
25643 /* R2L rows have BEG and END in reversed order, but the
25644 screen drawing geometry is always left to right. So
25645 we need to mirror the beginning and end of the
25646 highlighted area in R2L rows. */
25647 if (!row->reversed_p)
25649 start_hpos = hlinfo->mouse_face_beg_col;
25650 start_x = hlinfo->mouse_face_beg_x;
25652 else if (row == last)
25654 start_hpos = hlinfo->mouse_face_end_col;
25655 start_x = hlinfo->mouse_face_end_x;
25657 else
25659 start_hpos = 0;
25660 start_x = 0;
25663 else if (row->reversed_p && row == last)
25665 start_hpos = hlinfo->mouse_face_end_col;
25666 start_x = hlinfo->mouse_face_end_x;
25668 else
25670 start_hpos = 0;
25671 start_x = 0;
25674 if (row == last)
25676 if (!row->reversed_p)
25677 end_hpos = hlinfo->mouse_face_end_col;
25678 else if (row == first)
25679 end_hpos = hlinfo->mouse_face_beg_col;
25680 else
25682 end_hpos = row->used[TEXT_AREA];
25683 if (draw == DRAW_NORMAL_TEXT)
25684 row->fill_line_p = 1; /* Clear to end of line */
25687 else if (row->reversed_p && row == first)
25688 end_hpos = hlinfo->mouse_face_beg_col;
25689 else
25691 end_hpos = row->used[TEXT_AREA];
25692 if (draw == DRAW_NORMAL_TEXT)
25693 row->fill_line_p = 1; /* Clear to end of line */
25696 if (end_hpos > start_hpos)
25698 draw_row_with_mouse_face (w, start_x, row,
25699 start_hpos, end_hpos, draw);
25701 row->mouse_face_p
25702 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
25706 #ifdef HAVE_WINDOW_SYSTEM
25707 /* When we've written over the cursor, arrange for it to
25708 be displayed again. */
25709 if (FRAME_WINDOW_P (f)
25710 && phys_cursor_on_p && !w->phys_cursor_on_p)
25712 int hpos = w->phys_cursor.hpos;
25714 /* When the window is hscrolled, cursor hpos can legitimately be
25715 out of bounds, but we draw the cursor at the corresponding
25716 window margin in that case. */
25717 if (!row->reversed_p && hpos < 0)
25718 hpos = 0;
25719 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25720 hpos = row->used[TEXT_AREA] - 1;
25722 BLOCK_INPUT;
25723 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
25724 w->phys_cursor.x, w->phys_cursor.y);
25725 UNBLOCK_INPUT;
25727 #endif /* HAVE_WINDOW_SYSTEM */
25730 #ifdef HAVE_WINDOW_SYSTEM
25731 /* Change the mouse cursor. */
25732 if (FRAME_WINDOW_P (f))
25734 if (draw == DRAW_NORMAL_TEXT
25735 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
25736 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
25737 else if (draw == DRAW_MOUSE_FACE)
25738 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
25739 else
25740 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
25742 #endif /* HAVE_WINDOW_SYSTEM */
25745 /* EXPORT:
25746 Clear out the mouse-highlighted active region.
25747 Redraw it un-highlighted first. Value is non-zero if mouse
25748 face was actually drawn unhighlighted. */
25751 clear_mouse_face (Mouse_HLInfo *hlinfo)
25753 int cleared = 0;
25755 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
25757 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
25758 cleared = 1;
25761 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25762 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25763 hlinfo->mouse_face_window = Qnil;
25764 hlinfo->mouse_face_overlay = Qnil;
25765 return cleared;
25768 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
25769 within the mouse face on that window. */
25770 static int
25771 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
25773 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25775 /* Quickly resolve the easy cases. */
25776 if (!(WINDOWP (hlinfo->mouse_face_window)
25777 && XWINDOW (hlinfo->mouse_face_window) == w))
25778 return 0;
25779 if (vpos < hlinfo->mouse_face_beg_row
25780 || vpos > hlinfo->mouse_face_end_row)
25781 return 0;
25782 if (vpos > hlinfo->mouse_face_beg_row
25783 && vpos < hlinfo->mouse_face_end_row)
25784 return 1;
25786 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
25788 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25790 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
25791 return 1;
25793 else if ((vpos == hlinfo->mouse_face_beg_row
25794 && hpos >= hlinfo->mouse_face_beg_col)
25795 || (vpos == hlinfo->mouse_face_end_row
25796 && hpos < hlinfo->mouse_face_end_col))
25797 return 1;
25799 else
25801 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25803 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
25804 return 1;
25806 else if ((vpos == hlinfo->mouse_face_beg_row
25807 && hpos <= hlinfo->mouse_face_beg_col)
25808 || (vpos == hlinfo->mouse_face_end_row
25809 && hpos > hlinfo->mouse_face_end_col))
25810 return 1;
25812 return 0;
25816 /* EXPORT:
25817 Non-zero if physical cursor of window W is within mouse face. */
25820 cursor_in_mouse_face_p (struct window *w)
25822 int hpos = w->phys_cursor.hpos;
25823 int vpos = w->phys_cursor.vpos;
25824 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
25826 /* When the window is hscrolled, cursor hpos can legitimately be out
25827 of bounds, but we draw the cursor at the corresponding window
25828 margin in that case. */
25829 if (!row->reversed_p && hpos < 0)
25830 hpos = 0;
25831 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25832 hpos = row->used[TEXT_AREA] - 1;
25834 return coords_in_mouse_face_p (w, hpos, vpos);
25839 /* Find the glyph rows START_ROW and END_ROW of window W that display
25840 characters between buffer positions START_CHARPOS and END_CHARPOS
25841 (excluding END_CHARPOS). DISP_STRING is a display string that
25842 covers these buffer positions. This is similar to
25843 row_containing_pos, but is more accurate when bidi reordering makes
25844 buffer positions change non-linearly with glyph rows. */
25845 static void
25846 rows_from_pos_range (struct window *w,
25847 EMACS_INT start_charpos, EMACS_INT end_charpos,
25848 Lisp_Object disp_string,
25849 struct glyph_row **start, struct glyph_row **end)
25851 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25852 int last_y = window_text_bottom_y (w);
25853 struct glyph_row *row;
25855 *start = NULL;
25856 *end = NULL;
25858 while (!first->enabled_p
25859 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
25860 first++;
25862 /* Find the START row. */
25863 for (row = first;
25864 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
25865 row++)
25867 /* A row can potentially be the START row if the range of the
25868 characters it displays intersects the range
25869 [START_CHARPOS..END_CHARPOS). */
25870 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
25871 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
25872 /* See the commentary in row_containing_pos, for the
25873 explanation of the complicated way to check whether
25874 some position is beyond the end of the characters
25875 displayed by a row. */
25876 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
25877 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
25878 && !row->ends_at_zv_p
25879 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
25880 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
25881 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
25882 && !row->ends_at_zv_p
25883 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
25885 /* Found a candidate row. Now make sure at least one of the
25886 glyphs it displays has a charpos from the range
25887 [START_CHARPOS..END_CHARPOS).
25889 This is not obvious because bidi reordering could make
25890 buffer positions of a row be 1,2,3,102,101,100, and if we
25891 want to highlight characters in [50..60), we don't want
25892 this row, even though [50..60) does intersect [1..103),
25893 the range of character positions given by the row's start
25894 and end positions. */
25895 struct glyph *g = row->glyphs[TEXT_AREA];
25896 struct glyph *e = g + row->used[TEXT_AREA];
25898 while (g < e)
25900 if (((BUFFERP (g->object) || INTEGERP (g->object))
25901 && start_charpos <= g->charpos && g->charpos < end_charpos)
25902 /* A glyph that comes from DISP_STRING is by
25903 definition to be highlighted. */
25904 || EQ (g->object, disp_string))
25905 *start = row;
25906 g++;
25908 if (*start)
25909 break;
25913 /* Find the END row. */
25914 if (!*start
25915 /* If the last row is partially visible, start looking for END
25916 from that row, instead of starting from FIRST. */
25917 && !(row->enabled_p
25918 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
25919 row = first;
25920 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
25922 struct glyph_row *next = row + 1;
25923 EMACS_INT next_start = MATRIX_ROW_START_CHARPOS (next);
25925 if (!next->enabled_p
25926 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
25927 /* The first row >= START whose range of displayed characters
25928 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
25929 is the row END + 1. */
25930 || (start_charpos < next_start
25931 && end_charpos < next_start)
25932 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
25933 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
25934 && !next->ends_at_zv_p
25935 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
25936 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
25937 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
25938 && !next->ends_at_zv_p
25939 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
25941 *end = row;
25942 break;
25944 else
25946 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
25947 but none of the characters it displays are in the range, it is
25948 also END + 1. */
25949 struct glyph *g = next->glyphs[TEXT_AREA];
25950 struct glyph *s = g;
25951 struct glyph *e = g + next->used[TEXT_AREA];
25953 while (g < e)
25955 if (((BUFFERP (g->object) || INTEGERP (g->object))
25956 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
25957 /* If the buffer position of the first glyph in
25958 the row is equal to END_CHARPOS, it means
25959 the last character to be highlighted is the
25960 newline of ROW, and we must consider NEXT as
25961 END, not END+1. */
25962 || (((!next->reversed_p && g == s)
25963 || (next->reversed_p && g == e - 1))
25964 && (g->charpos == end_charpos
25965 /* Special case for when NEXT is an
25966 empty line at ZV. */
25967 || (g->charpos == -1
25968 && !row->ends_at_zv_p
25969 && next_start == end_charpos)))))
25970 /* A glyph that comes from DISP_STRING is by
25971 definition to be highlighted. */
25972 || EQ (g->object, disp_string))
25973 break;
25974 g++;
25976 if (g == e)
25978 *end = row;
25979 break;
25981 /* The first row that ends at ZV must be the last to be
25982 highlighted. */
25983 else if (next->ends_at_zv_p)
25985 *end = next;
25986 break;
25992 /* This function sets the mouse_face_* elements of HLINFO, assuming
25993 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
25994 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
25995 for the overlay or run of text properties specifying the mouse
25996 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
25997 before-string and after-string that must also be highlighted.
25998 DISP_STRING, if non-nil, is a display string that may cover some
25999 or all of the highlighted text. */
26001 static void
26002 mouse_face_from_buffer_pos (Lisp_Object window,
26003 Mouse_HLInfo *hlinfo,
26004 EMACS_INT mouse_charpos,
26005 EMACS_INT start_charpos,
26006 EMACS_INT end_charpos,
26007 Lisp_Object before_string,
26008 Lisp_Object after_string,
26009 Lisp_Object disp_string)
26011 struct window *w = XWINDOW (window);
26012 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26013 struct glyph_row *r1, *r2;
26014 struct glyph *glyph, *end;
26015 EMACS_INT ignore, pos;
26016 int x;
26018 xassert (NILP (disp_string) || STRINGP (disp_string));
26019 xassert (NILP (before_string) || STRINGP (before_string));
26020 xassert (NILP (after_string) || STRINGP (after_string));
26022 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26023 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26024 if (r1 == NULL)
26025 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26026 /* If the before-string or display-string contains newlines,
26027 rows_from_pos_range skips to its last row. Move back. */
26028 if (!NILP (before_string) || !NILP (disp_string))
26030 struct glyph_row *prev;
26031 while ((prev = r1 - 1, prev >= first)
26032 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26033 && prev->used[TEXT_AREA] > 0)
26035 struct glyph *beg = prev->glyphs[TEXT_AREA];
26036 glyph = beg + prev->used[TEXT_AREA];
26037 while (--glyph >= beg && INTEGERP (glyph->object));
26038 if (glyph < beg
26039 || !(EQ (glyph->object, before_string)
26040 || EQ (glyph->object, disp_string)))
26041 break;
26042 r1 = prev;
26045 if (r2 == NULL)
26047 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26048 hlinfo->mouse_face_past_end = 1;
26050 else if (!NILP (after_string))
26052 /* If the after-string has newlines, advance to its last row. */
26053 struct glyph_row *next;
26054 struct glyph_row *last
26055 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26057 for (next = r2 + 1;
26058 next <= last
26059 && next->used[TEXT_AREA] > 0
26060 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26061 ++next)
26062 r2 = next;
26064 /* The rest of the display engine assumes that mouse_face_beg_row is
26065 either above mouse_face_end_row or identical to it. But with
26066 bidi-reordered continued lines, the row for START_CHARPOS could
26067 be below the row for END_CHARPOS. If so, swap the rows and store
26068 them in correct order. */
26069 if (r1->y > r2->y)
26071 struct glyph_row *tem = r2;
26073 r2 = r1;
26074 r1 = tem;
26077 hlinfo->mouse_face_beg_y = r1->y;
26078 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26079 hlinfo->mouse_face_end_y = r2->y;
26080 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26082 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26083 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26084 could be anywhere in the row and in any order. The strategy
26085 below is to find the leftmost and the rightmost glyph that
26086 belongs to either of these 3 strings, or whose position is
26087 between START_CHARPOS and END_CHARPOS, and highlight all the
26088 glyphs between those two. This may cover more than just the text
26089 between START_CHARPOS and END_CHARPOS if the range of characters
26090 strides the bidi level boundary, e.g. if the beginning is in R2L
26091 text while the end is in L2R text or vice versa. */
26092 if (!r1->reversed_p)
26094 /* This row is in a left to right paragraph. Scan it left to
26095 right. */
26096 glyph = r1->glyphs[TEXT_AREA];
26097 end = glyph + r1->used[TEXT_AREA];
26098 x = r1->x;
26100 /* Skip truncation glyphs at the start of the glyph row. */
26101 if (r1->displays_text_p)
26102 for (; glyph < end
26103 && INTEGERP (glyph->object)
26104 && glyph->charpos < 0;
26105 ++glyph)
26106 x += glyph->pixel_width;
26108 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26109 or DISP_STRING, and the first glyph from buffer whose
26110 position is between START_CHARPOS and END_CHARPOS. */
26111 for (; glyph < end
26112 && !INTEGERP (glyph->object)
26113 && !EQ (glyph->object, disp_string)
26114 && !(BUFFERP (glyph->object)
26115 && (glyph->charpos >= start_charpos
26116 && glyph->charpos < end_charpos));
26117 ++glyph)
26119 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26120 are present at buffer positions between START_CHARPOS and
26121 END_CHARPOS, or if they come from an overlay. */
26122 if (EQ (glyph->object, before_string))
26124 pos = string_buffer_position (before_string,
26125 start_charpos);
26126 /* If pos == 0, it means before_string came from an
26127 overlay, not from a buffer position. */
26128 if (!pos || (pos >= start_charpos && pos < end_charpos))
26129 break;
26131 else if (EQ (glyph->object, after_string))
26133 pos = string_buffer_position (after_string, end_charpos);
26134 if (!pos || (pos >= start_charpos && pos < end_charpos))
26135 break;
26137 x += glyph->pixel_width;
26139 hlinfo->mouse_face_beg_x = x;
26140 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26142 else
26144 /* This row is in a right to left paragraph. Scan it right to
26145 left. */
26146 struct glyph *g;
26148 end = r1->glyphs[TEXT_AREA] - 1;
26149 glyph = end + r1->used[TEXT_AREA];
26151 /* Skip truncation glyphs at the start of the glyph row. */
26152 if (r1->displays_text_p)
26153 for (; glyph > end
26154 && INTEGERP (glyph->object)
26155 && glyph->charpos < 0;
26156 --glyph)
26159 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26160 or DISP_STRING, and the first glyph from buffer whose
26161 position is between START_CHARPOS and END_CHARPOS. */
26162 for (; glyph > end
26163 && !INTEGERP (glyph->object)
26164 && !EQ (glyph->object, disp_string)
26165 && !(BUFFERP (glyph->object)
26166 && (glyph->charpos >= start_charpos
26167 && glyph->charpos < end_charpos));
26168 --glyph)
26170 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26171 are present at buffer positions between START_CHARPOS and
26172 END_CHARPOS, or if they come from an overlay. */
26173 if (EQ (glyph->object, before_string))
26175 pos = string_buffer_position (before_string, start_charpos);
26176 /* If pos == 0, it means before_string came from an
26177 overlay, not from a buffer position. */
26178 if (!pos || (pos >= start_charpos && pos < end_charpos))
26179 break;
26181 else if (EQ (glyph->object, after_string))
26183 pos = string_buffer_position (after_string, end_charpos);
26184 if (!pos || (pos >= start_charpos && pos < end_charpos))
26185 break;
26189 glyph++; /* first glyph to the right of the highlighted area */
26190 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26191 x += g->pixel_width;
26192 hlinfo->mouse_face_beg_x = x;
26193 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26196 /* If the highlight ends in a different row, compute GLYPH and END
26197 for the end row. Otherwise, reuse the values computed above for
26198 the row where the highlight begins. */
26199 if (r2 != r1)
26201 if (!r2->reversed_p)
26203 glyph = r2->glyphs[TEXT_AREA];
26204 end = glyph + r2->used[TEXT_AREA];
26205 x = r2->x;
26207 else
26209 end = r2->glyphs[TEXT_AREA] - 1;
26210 glyph = end + r2->used[TEXT_AREA];
26214 if (!r2->reversed_p)
26216 /* Skip truncation and continuation glyphs near the end of the
26217 row, and also blanks and stretch glyphs inserted by
26218 extend_face_to_end_of_line. */
26219 while (end > glyph
26220 && INTEGERP ((end - 1)->object))
26221 --end;
26222 /* Scan the rest of the glyph row from the end, looking for the
26223 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26224 DISP_STRING, or whose position is between START_CHARPOS
26225 and END_CHARPOS */
26226 for (--end;
26227 end > glyph
26228 && !INTEGERP (end->object)
26229 && !EQ (end->object, disp_string)
26230 && !(BUFFERP (end->object)
26231 && (end->charpos >= start_charpos
26232 && end->charpos < end_charpos));
26233 --end)
26235 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26236 are present at buffer positions between START_CHARPOS and
26237 END_CHARPOS, or if they come from an overlay. */
26238 if (EQ (end->object, before_string))
26240 pos = string_buffer_position (before_string, start_charpos);
26241 if (!pos || (pos >= start_charpos && pos < end_charpos))
26242 break;
26244 else if (EQ (end->object, after_string))
26246 pos = string_buffer_position (after_string, end_charpos);
26247 if (!pos || (pos >= start_charpos && pos < end_charpos))
26248 break;
26251 /* Find the X coordinate of the last glyph to be highlighted. */
26252 for (; glyph <= end; ++glyph)
26253 x += glyph->pixel_width;
26255 hlinfo->mouse_face_end_x = x;
26256 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26258 else
26260 /* Skip truncation and continuation glyphs near the end of the
26261 row, and also blanks and stretch glyphs inserted by
26262 extend_face_to_end_of_line. */
26263 x = r2->x;
26264 end++;
26265 while (end < glyph
26266 && INTEGERP (end->object))
26268 x += end->pixel_width;
26269 ++end;
26271 /* Scan the rest of the glyph row from the end, looking for the
26272 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26273 DISP_STRING, or whose position is between START_CHARPOS
26274 and END_CHARPOS */
26275 for ( ;
26276 end < glyph
26277 && !INTEGERP (end->object)
26278 && !EQ (end->object, disp_string)
26279 && !(BUFFERP (end->object)
26280 && (end->charpos >= start_charpos
26281 && end->charpos < end_charpos));
26282 ++end)
26284 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26285 are present at buffer positions between START_CHARPOS and
26286 END_CHARPOS, or if they come from an overlay. */
26287 if (EQ (end->object, before_string))
26289 pos = string_buffer_position (before_string, start_charpos);
26290 if (!pos || (pos >= start_charpos && pos < end_charpos))
26291 break;
26293 else if (EQ (end->object, after_string))
26295 pos = string_buffer_position (after_string, end_charpos);
26296 if (!pos || (pos >= start_charpos && pos < end_charpos))
26297 break;
26299 x += end->pixel_width;
26301 /* If we exited the above loop because we arrived at the last
26302 glyph of the row, and its buffer position is still not in
26303 range, it means the last character in range is the preceding
26304 newline. Bump the end column and x values to get past the
26305 last glyph. */
26306 if (end == glyph
26307 && BUFFERP (end->object)
26308 && (end->charpos < start_charpos
26309 || end->charpos >= end_charpos))
26311 x += end->pixel_width;
26312 ++end;
26314 hlinfo->mouse_face_end_x = x;
26315 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26318 hlinfo->mouse_face_window = window;
26319 hlinfo->mouse_face_face_id
26320 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26321 mouse_charpos + 1,
26322 !hlinfo->mouse_face_hidden, -1);
26323 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26326 /* The following function is not used anymore (replaced with
26327 mouse_face_from_string_pos), but I leave it here for the time
26328 being, in case someone would. */
26330 #if 0 /* not used */
26332 /* Find the position of the glyph for position POS in OBJECT in
26333 window W's current matrix, and return in *X, *Y the pixel
26334 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26336 RIGHT_P non-zero means return the position of the right edge of the
26337 glyph, RIGHT_P zero means return the left edge position.
26339 If no glyph for POS exists in the matrix, return the position of
26340 the glyph with the next smaller position that is in the matrix, if
26341 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26342 exists in the matrix, return the position of the glyph with the
26343 next larger position in OBJECT.
26345 Value is non-zero if a glyph was found. */
26347 static int
26348 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
26349 int *hpos, int *vpos, int *x, int *y, int right_p)
26351 int yb = window_text_bottom_y (w);
26352 struct glyph_row *r;
26353 struct glyph *best_glyph = NULL;
26354 struct glyph_row *best_row = NULL;
26355 int best_x = 0;
26357 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26358 r->enabled_p && r->y < yb;
26359 ++r)
26361 struct glyph *g = r->glyphs[TEXT_AREA];
26362 struct glyph *e = g + r->used[TEXT_AREA];
26363 int gx;
26365 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26366 if (EQ (g->object, object))
26368 if (g->charpos == pos)
26370 best_glyph = g;
26371 best_x = gx;
26372 best_row = r;
26373 goto found;
26375 else if (best_glyph == NULL
26376 || ((eabs (g->charpos - pos)
26377 < eabs (best_glyph->charpos - pos))
26378 && (right_p
26379 ? g->charpos < pos
26380 : g->charpos > pos)))
26382 best_glyph = g;
26383 best_x = gx;
26384 best_row = r;
26389 found:
26391 if (best_glyph)
26393 *x = best_x;
26394 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26396 if (right_p)
26398 *x += best_glyph->pixel_width;
26399 ++*hpos;
26402 *y = best_row->y;
26403 *vpos = best_row - w->current_matrix->rows;
26406 return best_glyph != NULL;
26408 #endif /* not used */
26410 /* Find the positions of the first and the last glyphs in window W's
26411 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26412 (assumed to be a string), and return in HLINFO's mouse_face_*
26413 members the pixel and column/row coordinates of those glyphs. */
26415 static void
26416 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26417 Lisp_Object object,
26418 EMACS_INT startpos, EMACS_INT endpos)
26420 int yb = window_text_bottom_y (w);
26421 struct glyph_row *r;
26422 struct glyph *g, *e;
26423 int gx;
26424 int found = 0;
26426 /* Find the glyph row with at least one position in the range
26427 [STARTPOS..ENDPOS], and the first glyph in that row whose
26428 position belongs to that range. */
26429 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26430 r->enabled_p && r->y < yb;
26431 ++r)
26433 if (!r->reversed_p)
26435 g = r->glyphs[TEXT_AREA];
26436 e = g + r->used[TEXT_AREA];
26437 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26438 if (EQ (g->object, object)
26439 && startpos <= g->charpos && g->charpos <= endpos)
26441 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26442 hlinfo->mouse_face_beg_y = r->y;
26443 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26444 hlinfo->mouse_face_beg_x = gx;
26445 found = 1;
26446 break;
26449 else
26451 struct glyph *g1;
26453 e = r->glyphs[TEXT_AREA];
26454 g = e + r->used[TEXT_AREA];
26455 for ( ; g > e; --g)
26456 if (EQ ((g-1)->object, object)
26457 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26459 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26460 hlinfo->mouse_face_beg_y = r->y;
26461 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26462 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26463 gx += g1->pixel_width;
26464 hlinfo->mouse_face_beg_x = gx;
26465 found = 1;
26466 break;
26469 if (found)
26470 break;
26473 if (!found)
26474 return;
26476 /* Starting with the next row, look for the first row which does NOT
26477 include any glyphs whose positions are in the range. */
26478 for (++r; r->enabled_p && r->y < yb; ++r)
26480 g = r->glyphs[TEXT_AREA];
26481 e = g + r->used[TEXT_AREA];
26482 found = 0;
26483 for ( ; g < e; ++g)
26484 if (EQ (g->object, object)
26485 && startpos <= g->charpos && g->charpos <= endpos)
26487 found = 1;
26488 break;
26490 if (!found)
26491 break;
26494 /* The highlighted region ends on the previous row. */
26495 r--;
26497 /* Set the end row and its vertical pixel coordinate. */
26498 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
26499 hlinfo->mouse_face_end_y = r->y;
26501 /* Compute and set the end column and the end column's horizontal
26502 pixel coordinate. */
26503 if (!r->reversed_p)
26505 g = r->glyphs[TEXT_AREA];
26506 e = g + r->used[TEXT_AREA];
26507 for ( ; e > g; --e)
26508 if (EQ ((e-1)->object, object)
26509 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
26510 break;
26511 hlinfo->mouse_face_end_col = e - g;
26513 for (gx = r->x; g < e; ++g)
26514 gx += g->pixel_width;
26515 hlinfo->mouse_face_end_x = gx;
26517 else
26519 e = r->glyphs[TEXT_AREA];
26520 g = e + r->used[TEXT_AREA];
26521 for (gx = r->x ; e < g; ++e)
26523 if (EQ (e->object, object)
26524 && startpos <= e->charpos && e->charpos <= endpos)
26525 break;
26526 gx += e->pixel_width;
26528 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
26529 hlinfo->mouse_face_end_x = gx;
26533 #ifdef HAVE_WINDOW_SYSTEM
26535 /* See if position X, Y is within a hot-spot of an image. */
26537 static int
26538 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
26540 if (!CONSP (hot_spot))
26541 return 0;
26543 if (EQ (XCAR (hot_spot), Qrect))
26545 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
26546 Lisp_Object rect = XCDR (hot_spot);
26547 Lisp_Object tem;
26548 if (!CONSP (rect))
26549 return 0;
26550 if (!CONSP (XCAR (rect)))
26551 return 0;
26552 if (!CONSP (XCDR (rect)))
26553 return 0;
26554 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
26555 return 0;
26556 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
26557 return 0;
26558 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
26559 return 0;
26560 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
26561 return 0;
26562 return 1;
26564 else if (EQ (XCAR (hot_spot), Qcircle))
26566 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
26567 Lisp_Object circ = XCDR (hot_spot);
26568 Lisp_Object lr, lx0, ly0;
26569 if (CONSP (circ)
26570 && CONSP (XCAR (circ))
26571 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
26572 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
26573 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
26575 double r = XFLOATINT (lr);
26576 double dx = XINT (lx0) - x;
26577 double dy = XINT (ly0) - y;
26578 return (dx * dx + dy * dy <= r * r);
26581 else if (EQ (XCAR (hot_spot), Qpoly))
26583 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
26584 if (VECTORP (XCDR (hot_spot)))
26586 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
26587 Lisp_Object *poly = v->contents;
26588 int n = v->header.size;
26589 int i;
26590 int inside = 0;
26591 Lisp_Object lx, ly;
26592 int x0, y0;
26594 /* Need an even number of coordinates, and at least 3 edges. */
26595 if (n < 6 || n & 1)
26596 return 0;
26598 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
26599 If count is odd, we are inside polygon. Pixels on edges
26600 may or may not be included depending on actual geometry of the
26601 polygon. */
26602 if ((lx = poly[n-2], !INTEGERP (lx))
26603 || (ly = poly[n-1], !INTEGERP (lx)))
26604 return 0;
26605 x0 = XINT (lx), y0 = XINT (ly);
26606 for (i = 0; i < n; i += 2)
26608 int x1 = x0, y1 = y0;
26609 if ((lx = poly[i], !INTEGERP (lx))
26610 || (ly = poly[i+1], !INTEGERP (ly)))
26611 return 0;
26612 x0 = XINT (lx), y0 = XINT (ly);
26614 /* Does this segment cross the X line? */
26615 if (x0 >= x)
26617 if (x1 >= x)
26618 continue;
26620 else if (x1 < x)
26621 continue;
26622 if (y > y0 && y > y1)
26623 continue;
26624 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
26625 inside = !inside;
26627 return inside;
26630 return 0;
26633 Lisp_Object
26634 find_hot_spot (Lisp_Object map, int x, int y)
26636 while (CONSP (map))
26638 if (CONSP (XCAR (map))
26639 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
26640 return XCAR (map);
26641 map = XCDR (map);
26644 return Qnil;
26647 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
26648 3, 3, 0,
26649 doc: /* Lookup in image map MAP coordinates X and Y.
26650 An image map is an alist where each element has the format (AREA ID PLIST).
26651 An AREA is specified as either a rectangle, a circle, or a polygon:
26652 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
26653 pixel coordinates of the upper left and bottom right corners.
26654 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
26655 and the radius of the circle; r may be a float or integer.
26656 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
26657 vector describes one corner in the polygon.
26658 Returns the alist element for the first matching AREA in MAP. */)
26659 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
26661 if (NILP (map))
26662 return Qnil;
26664 CHECK_NUMBER (x);
26665 CHECK_NUMBER (y);
26667 return find_hot_spot (map, XINT (x), XINT (y));
26671 /* Display frame CURSOR, optionally using shape defined by POINTER. */
26672 static void
26673 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
26675 /* Do not change cursor shape while dragging mouse. */
26676 if (!NILP (do_mouse_tracking))
26677 return;
26679 if (!NILP (pointer))
26681 if (EQ (pointer, Qarrow))
26682 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26683 else if (EQ (pointer, Qhand))
26684 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
26685 else if (EQ (pointer, Qtext))
26686 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26687 else if (EQ (pointer, intern ("hdrag")))
26688 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26689 #ifdef HAVE_X_WINDOWS
26690 else if (EQ (pointer, intern ("vdrag")))
26691 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
26692 #endif
26693 else if (EQ (pointer, intern ("hourglass")))
26694 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
26695 else if (EQ (pointer, Qmodeline))
26696 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
26697 else
26698 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26701 if (cursor != No_Cursor)
26702 FRAME_RIF (f)->define_frame_cursor (f, cursor);
26705 #endif /* HAVE_WINDOW_SYSTEM */
26707 /* Take proper action when mouse has moved to the mode or header line
26708 or marginal area AREA of window W, x-position X and y-position Y.
26709 X is relative to the start of the text display area of W, so the
26710 width of bitmap areas and scroll bars must be subtracted to get a
26711 position relative to the start of the mode line. */
26713 static void
26714 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
26715 enum window_part area)
26717 struct window *w = XWINDOW (window);
26718 struct frame *f = XFRAME (w->frame);
26719 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26720 #ifdef HAVE_WINDOW_SYSTEM
26721 Display_Info *dpyinfo;
26722 #endif
26723 Cursor cursor = No_Cursor;
26724 Lisp_Object pointer = Qnil;
26725 int dx, dy, width, height;
26726 EMACS_INT charpos;
26727 Lisp_Object string, object = Qnil;
26728 Lisp_Object pos, help;
26730 Lisp_Object mouse_face;
26731 int original_x_pixel = x;
26732 struct glyph * glyph = NULL, * row_start_glyph = NULL;
26733 struct glyph_row *row;
26735 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
26737 int x0;
26738 struct glyph *end;
26740 /* Kludge alert: mode_line_string takes X/Y in pixels, but
26741 returns them in row/column units! */
26742 string = mode_line_string (w, area, &x, &y, &charpos,
26743 &object, &dx, &dy, &width, &height);
26745 row = (area == ON_MODE_LINE
26746 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
26747 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
26749 /* Find the glyph under the mouse pointer. */
26750 if (row->mode_line_p && row->enabled_p)
26752 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
26753 end = glyph + row->used[TEXT_AREA];
26755 for (x0 = original_x_pixel;
26756 glyph < end && x0 >= glyph->pixel_width;
26757 ++glyph)
26758 x0 -= glyph->pixel_width;
26760 if (glyph >= end)
26761 glyph = NULL;
26764 else
26766 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
26767 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
26768 returns them in row/column units! */
26769 string = marginal_area_string (w, area, &x, &y, &charpos,
26770 &object, &dx, &dy, &width, &height);
26773 help = Qnil;
26775 #ifdef HAVE_WINDOW_SYSTEM
26776 if (IMAGEP (object))
26778 Lisp_Object image_map, hotspot;
26779 if ((image_map = Fplist_get (XCDR (object), QCmap),
26780 !NILP (image_map))
26781 && (hotspot = find_hot_spot (image_map, dx, dy),
26782 CONSP (hotspot))
26783 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
26785 Lisp_Object plist;
26787 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
26788 If so, we could look for mouse-enter, mouse-leave
26789 properties in PLIST (and do something...). */
26790 hotspot = XCDR (hotspot);
26791 if (CONSP (hotspot)
26792 && (plist = XCAR (hotspot), CONSP (plist)))
26794 pointer = Fplist_get (plist, Qpointer);
26795 if (NILP (pointer))
26796 pointer = Qhand;
26797 help = Fplist_get (plist, Qhelp_echo);
26798 if (!NILP (help))
26800 help_echo_string = help;
26801 /* Is this correct? ++kfs */
26802 XSETWINDOW (help_echo_window, w);
26803 help_echo_object = w->buffer;
26804 help_echo_pos = charpos;
26808 if (NILP (pointer))
26809 pointer = Fplist_get (XCDR (object), QCpointer);
26811 #endif /* HAVE_WINDOW_SYSTEM */
26813 if (STRINGP (string))
26815 pos = make_number (charpos);
26816 /* If we're on a string with `help-echo' text property, arrange
26817 for the help to be displayed. This is done by setting the
26818 global variable help_echo_string to the help string. */
26819 if (NILP (help))
26821 help = Fget_text_property (pos, Qhelp_echo, string);
26822 if (!NILP (help))
26824 help_echo_string = help;
26825 XSETWINDOW (help_echo_window, w);
26826 help_echo_object = string;
26827 help_echo_pos = charpos;
26831 #ifdef HAVE_WINDOW_SYSTEM
26832 if (FRAME_WINDOW_P (f))
26834 dpyinfo = FRAME_X_DISPLAY_INFO (f);
26835 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26836 if (NILP (pointer))
26837 pointer = Fget_text_property (pos, Qpointer, string);
26839 /* Change the mouse pointer according to what is under X/Y. */
26840 if (NILP (pointer)
26841 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
26843 Lisp_Object map;
26844 map = Fget_text_property (pos, Qlocal_map, string);
26845 if (!KEYMAPP (map))
26846 map = Fget_text_property (pos, Qkeymap, string);
26847 if (!KEYMAPP (map))
26848 cursor = dpyinfo->vertical_scroll_bar_cursor;
26851 #endif
26853 /* Change the mouse face according to what is under X/Y. */
26854 mouse_face = Fget_text_property (pos, Qmouse_face, string);
26855 if (!NILP (mouse_face)
26856 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
26857 && glyph)
26859 Lisp_Object b, e;
26861 struct glyph * tmp_glyph;
26863 int gpos;
26864 int gseq_length;
26865 int total_pixel_width;
26866 EMACS_INT begpos, endpos, ignore;
26868 int vpos, hpos;
26870 b = Fprevious_single_property_change (make_number (charpos + 1),
26871 Qmouse_face, string, Qnil);
26872 if (NILP (b))
26873 begpos = 0;
26874 else
26875 begpos = XINT (b);
26877 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
26878 if (NILP (e))
26879 endpos = SCHARS (string);
26880 else
26881 endpos = XINT (e);
26883 /* Calculate the glyph position GPOS of GLYPH in the
26884 displayed string, relative to the beginning of the
26885 highlighted part of the string.
26887 Note: GPOS is different from CHARPOS. CHARPOS is the
26888 position of GLYPH in the internal string object. A mode
26889 line string format has structures which are converted to
26890 a flattened string by the Emacs Lisp interpreter. The
26891 internal string is an element of those structures. The
26892 displayed string is the flattened string. */
26893 tmp_glyph = row_start_glyph;
26894 while (tmp_glyph < glyph
26895 && (!(EQ (tmp_glyph->object, glyph->object)
26896 && begpos <= tmp_glyph->charpos
26897 && tmp_glyph->charpos < endpos)))
26898 tmp_glyph++;
26899 gpos = glyph - tmp_glyph;
26901 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
26902 the highlighted part of the displayed string to which
26903 GLYPH belongs. Note: GSEQ_LENGTH is different from
26904 SCHARS (STRING), because the latter returns the length of
26905 the internal string. */
26906 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
26907 tmp_glyph > glyph
26908 && (!(EQ (tmp_glyph->object, glyph->object)
26909 && begpos <= tmp_glyph->charpos
26910 && tmp_glyph->charpos < endpos));
26911 tmp_glyph--)
26913 gseq_length = gpos + (tmp_glyph - glyph) + 1;
26915 /* Calculate the total pixel width of all the glyphs between
26916 the beginning of the highlighted area and GLYPH. */
26917 total_pixel_width = 0;
26918 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
26919 total_pixel_width += tmp_glyph->pixel_width;
26921 /* Pre calculation of re-rendering position. Note: X is in
26922 column units here, after the call to mode_line_string or
26923 marginal_area_string. */
26924 hpos = x - gpos;
26925 vpos = (area == ON_MODE_LINE
26926 ? (w->current_matrix)->nrows - 1
26927 : 0);
26929 /* If GLYPH's position is included in the region that is
26930 already drawn in mouse face, we have nothing to do. */
26931 if ( EQ (window, hlinfo->mouse_face_window)
26932 && (!row->reversed_p
26933 ? (hlinfo->mouse_face_beg_col <= hpos
26934 && hpos < hlinfo->mouse_face_end_col)
26935 /* In R2L rows we swap BEG and END, see below. */
26936 : (hlinfo->mouse_face_end_col <= hpos
26937 && hpos < hlinfo->mouse_face_beg_col))
26938 && hlinfo->mouse_face_beg_row == vpos )
26939 return;
26941 if (clear_mouse_face (hlinfo))
26942 cursor = No_Cursor;
26944 if (!row->reversed_p)
26946 hlinfo->mouse_face_beg_col = hpos;
26947 hlinfo->mouse_face_beg_x = original_x_pixel
26948 - (total_pixel_width + dx);
26949 hlinfo->mouse_face_end_col = hpos + gseq_length;
26950 hlinfo->mouse_face_end_x = 0;
26952 else
26954 /* In R2L rows, show_mouse_face expects BEG and END
26955 coordinates to be swapped. */
26956 hlinfo->mouse_face_end_col = hpos;
26957 hlinfo->mouse_face_end_x = original_x_pixel
26958 - (total_pixel_width + dx);
26959 hlinfo->mouse_face_beg_col = hpos + gseq_length;
26960 hlinfo->mouse_face_beg_x = 0;
26963 hlinfo->mouse_face_beg_row = vpos;
26964 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
26965 hlinfo->mouse_face_beg_y = 0;
26966 hlinfo->mouse_face_end_y = 0;
26967 hlinfo->mouse_face_past_end = 0;
26968 hlinfo->mouse_face_window = window;
26970 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
26971 charpos,
26972 0, 0, 0,
26973 &ignore,
26974 glyph->face_id,
26976 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26978 if (NILP (pointer))
26979 pointer = Qhand;
26981 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
26982 clear_mouse_face (hlinfo);
26984 #ifdef HAVE_WINDOW_SYSTEM
26985 if (FRAME_WINDOW_P (f))
26986 define_frame_cursor1 (f, cursor, pointer);
26987 #endif
26991 /* EXPORT:
26992 Take proper action when the mouse has moved to position X, Y on
26993 frame F as regards highlighting characters that have mouse-face
26994 properties. Also de-highlighting chars where the mouse was before.
26995 X and Y can be negative or out of range. */
26997 void
26998 note_mouse_highlight (struct frame *f, int x, int y)
27000 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27001 enum window_part part = ON_NOTHING;
27002 Lisp_Object window;
27003 struct window *w;
27004 Cursor cursor = No_Cursor;
27005 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27006 struct buffer *b;
27008 /* When a menu is active, don't highlight because this looks odd. */
27009 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27010 if (popup_activated ())
27011 return;
27012 #endif
27014 if (NILP (Vmouse_highlight)
27015 || !f->glyphs_initialized_p
27016 || f->pointer_invisible)
27017 return;
27019 hlinfo->mouse_face_mouse_x = x;
27020 hlinfo->mouse_face_mouse_y = y;
27021 hlinfo->mouse_face_mouse_frame = f;
27023 if (hlinfo->mouse_face_defer)
27024 return;
27026 if (gc_in_progress)
27028 hlinfo->mouse_face_deferred_gc = 1;
27029 return;
27032 /* Which window is that in? */
27033 window = window_from_coordinates (f, x, y, &part, 1);
27035 /* If displaying active text in another window, clear that. */
27036 if (! EQ (window, hlinfo->mouse_face_window)
27037 /* Also clear if we move out of text area in same window. */
27038 || (!NILP (hlinfo->mouse_face_window)
27039 && !NILP (window)
27040 && part != ON_TEXT
27041 && part != ON_MODE_LINE
27042 && part != ON_HEADER_LINE))
27043 clear_mouse_face (hlinfo);
27045 /* Not on a window -> return. */
27046 if (!WINDOWP (window))
27047 return;
27049 /* Reset help_echo_string. It will get recomputed below. */
27050 help_echo_string = Qnil;
27052 /* Convert to window-relative pixel coordinates. */
27053 w = XWINDOW (window);
27054 frame_to_window_pixel_xy (w, &x, &y);
27056 #ifdef HAVE_WINDOW_SYSTEM
27057 /* Handle tool-bar window differently since it doesn't display a
27058 buffer. */
27059 if (EQ (window, f->tool_bar_window))
27061 note_tool_bar_highlight (f, x, y);
27062 return;
27064 #endif
27066 /* Mouse is on the mode, header line or margin? */
27067 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27068 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27070 note_mode_line_or_margin_highlight (window, x, y, part);
27071 return;
27074 #ifdef HAVE_WINDOW_SYSTEM
27075 if (part == ON_VERTICAL_BORDER)
27077 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27078 help_echo_string = build_string ("drag-mouse-1: resize");
27080 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27081 || part == ON_SCROLL_BAR)
27082 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27083 else
27084 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27085 #endif
27087 /* Are we in a window whose display is up to date?
27088 And verify the buffer's text has not changed. */
27089 b = XBUFFER (w->buffer);
27090 if (part == ON_TEXT
27091 && EQ (w->window_end_valid, w->buffer)
27092 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
27093 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
27095 int hpos, vpos, dx, dy, area = LAST_AREA;
27096 EMACS_INT pos;
27097 struct glyph *glyph;
27098 Lisp_Object object;
27099 Lisp_Object mouse_face = Qnil, position;
27100 Lisp_Object *overlay_vec = NULL;
27101 ptrdiff_t i, noverlays;
27102 struct buffer *obuf;
27103 EMACS_INT obegv, ozv;
27104 int same_region;
27106 /* Find the glyph under X/Y. */
27107 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27109 #ifdef HAVE_WINDOW_SYSTEM
27110 /* Look for :pointer property on image. */
27111 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27113 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27114 if (img != NULL && IMAGEP (img->spec))
27116 Lisp_Object image_map, hotspot;
27117 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27118 !NILP (image_map))
27119 && (hotspot = find_hot_spot (image_map,
27120 glyph->slice.img.x + dx,
27121 glyph->slice.img.y + dy),
27122 CONSP (hotspot))
27123 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27125 Lisp_Object plist;
27127 /* Could check XCAR (hotspot) to see if we enter/leave
27128 this hot-spot.
27129 If so, we could look for mouse-enter, mouse-leave
27130 properties in PLIST (and do something...). */
27131 hotspot = XCDR (hotspot);
27132 if (CONSP (hotspot)
27133 && (plist = XCAR (hotspot), CONSP (plist)))
27135 pointer = Fplist_get (plist, Qpointer);
27136 if (NILP (pointer))
27137 pointer = Qhand;
27138 help_echo_string = Fplist_get (plist, Qhelp_echo);
27139 if (!NILP (help_echo_string))
27141 help_echo_window = window;
27142 help_echo_object = glyph->object;
27143 help_echo_pos = glyph->charpos;
27147 if (NILP (pointer))
27148 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27151 #endif /* HAVE_WINDOW_SYSTEM */
27153 /* Clear mouse face if X/Y not over text. */
27154 if (glyph == NULL
27155 || area != TEXT_AREA
27156 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27157 /* Glyph's OBJECT is an integer for glyphs inserted by the
27158 display engine for its internal purposes, like truncation
27159 and continuation glyphs and blanks beyond the end of
27160 line's text on text terminals. If we are over such a
27161 glyph, we are not over any text. */
27162 || INTEGERP (glyph->object)
27163 /* R2L rows have a stretch glyph at their front, which
27164 stands for no text, whereas L2R rows have no glyphs at
27165 all beyond the end of text. Treat such stretch glyphs
27166 like we do with NULL glyphs in L2R rows. */
27167 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27168 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27169 && glyph->type == STRETCH_GLYPH
27170 && glyph->avoid_cursor_p))
27172 if (clear_mouse_face (hlinfo))
27173 cursor = No_Cursor;
27174 #ifdef HAVE_WINDOW_SYSTEM
27175 if (FRAME_WINDOW_P (f) && NILP (pointer))
27177 if (area != TEXT_AREA)
27178 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27179 else
27180 pointer = Vvoid_text_area_pointer;
27182 #endif
27183 goto set_cursor;
27186 pos = glyph->charpos;
27187 object = glyph->object;
27188 if (!STRINGP (object) && !BUFFERP (object))
27189 goto set_cursor;
27191 /* If we get an out-of-range value, return now; avoid an error. */
27192 if (BUFFERP (object) && pos > BUF_Z (b))
27193 goto set_cursor;
27195 /* Make the window's buffer temporarily current for
27196 overlays_at and compute_char_face. */
27197 obuf = current_buffer;
27198 current_buffer = b;
27199 obegv = BEGV;
27200 ozv = ZV;
27201 BEGV = BEG;
27202 ZV = Z;
27204 /* Is this char mouse-active or does it have help-echo? */
27205 position = make_number (pos);
27207 if (BUFFERP (object))
27209 /* Put all the overlays we want in a vector in overlay_vec. */
27210 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27211 /* Sort overlays into increasing priority order. */
27212 noverlays = sort_overlays (overlay_vec, noverlays, w);
27214 else
27215 noverlays = 0;
27217 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27219 if (same_region)
27220 cursor = No_Cursor;
27222 /* Check mouse-face highlighting. */
27223 if (! same_region
27224 /* If there exists an overlay with mouse-face overlapping
27225 the one we are currently highlighting, we have to
27226 check if we enter the overlapping overlay, and then
27227 highlight only that. */
27228 || (OVERLAYP (hlinfo->mouse_face_overlay)
27229 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27231 /* Find the highest priority overlay with a mouse-face. */
27232 Lisp_Object overlay = Qnil;
27233 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27235 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27236 if (!NILP (mouse_face))
27237 overlay = overlay_vec[i];
27240 /* If we're highlighting the same overlay as before, there's
27241 no need to do that again. */
27242 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27243 goto check_help_echo;
27244 hlinfo->mouse_face_overlay = overlay;
27246 /* Clear the display of the old active region, if any. */
27247 if (clear_mouse_face (hlinfo))
27248 cursor = No_Cursor;
27250 /* If no overlay applies, get a text property. */
27251 if (NILP (overlay))
27252 mouse_face = Fget_text_property (position, Qmouse_face, object);
27254 /* Next, compute the bounds of the mouse highlighting and
27255 display it. */
27256 if (!NILP (mouse_face) && STRINGP (object))
27258 /* The mouse-highlighting comes from a display string
27259 with a mouse-face. */
27260 Lisp_Object s, e;
27261 EMACS_INT ignore;
27263 s = Fprevious_single_property_change
27264 (make_number (pos + 1), Qmouse_face, object, Qnil);
27265 e = Fnext_single_property_change
27266 (position, Qmouse_face, object, Qnil);
27267 if (NILP (s))
27268 s = make_number (0);
27269 if (NILP (e))
27270 e = make_number (SCHARS (object) - 1);
27271 mouse_face_from_string_pos (w, hlinfo, object,
27272 XINT (s), XINT (e));
27273 hlinfo->mouse_face_past_end = 0;
27274 hlinfo->mouse_face_window = window;
27275 hlinfo->mouse_face_face_id
27276 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27277 glyph->face_id, 1);
27278 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27279 cursor = No_Cursor;
27281 else
27283 /* The mouse-highlighting, if any, comes from an overlay
27284 or text property in the buffer. */
27285 Lisp_Object buffer IF_LINT (= Qnil);
27286 Lisp_Object disp_string IF_LINT (= Qnil);
27288 if (STRINGP (object))
27290 /* If we are on a display string with no mouse-face,
27291 check if the text under it has one. */
27292 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27293 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27294 pos = string_buffer_position (object, start);
27295 if (pos > 0)
27297 mouse_face = get_char_property_and_overlay
27298 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27299 buffer = w->buffer;
27300 disp_string = object;
27303 else
27305 buffer = object;
27306 disp_string = Qnil;
27309 if (!NILP (mouse_face))
27311 Lisp_Object before, after;
27312 Lisp_Object before_string, after_string;
27313 /* To correctly find the limits of mouse highlight
27314 in a bidi-reordered buffer, we must not use the
27315 optimization of limiting the search in
27316 previous-single-property-change and
27317 next-single-property-change, because
27318 rows_from_pos_range needs the real start and end
27319 positions to DTRT in this case. That's because
27320 the first row visible in a window does not
27321 necessarily display the character whose position
27322 is the smallest. */
27323 Lisp_Object lim1 =
27324 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27325 ? Fmarker_position (w->start)
27326 : Qnil;
27327 Lisp_Object lim2 =
27328 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27329 ? make_number (BUF_Z (XBUFFER (buffer))
27330 - XFASTINT (w->window_end_pos))
27331 : Qnil;
27333 if (NILP (overlay))
27335 /* Handle the text property case. */
27336 before = Fprevious_single_property_change
27337 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27338 after = Fnext_single_property_change
27339 (make_number (pos), Qmouse_face, buffer, lim2);
27340 before_string = after_string = Qnil;
27342 else
27344 /* Handle the overlay case. */
27345 before = Foverlay_start (overlay);
27346 after = Foverlay_end (overlay);
27347 before_string = Foverlay_get (overlay, Qbefore_string);
27348 after_string = Foverlay_get (overlay, Qafter_string);
27350 if (!STRINGP (before_string)) before_string = Qnil;
27351 if (!STRINGP (after_string)) after_string = Qnil;
27354 mouse_face_from_buffer_pos (window, hlinfo, pos,
27355 NILP (before)
27357 : XFASTINT (before),
27358 NILP (after)
27359 ? BUF_Z (XBUFFER (buffer))
27360 : XFASTINT (after),
27361 before_string, after_string,
27362 disp_string);
27363 cursor = No_Cursor;
27368 check_help_echo:
27370 /* Look for a `help-echo' property. */
27371 if (NILP (help_echo_string)) {
27372 Lisp_Object help, overlay;
27374 /* Check overlays first. */
27375 help = overlay = Qnil;
27376 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27378 overlay = overlay_vec[i];
27379 help = Foverlay_get (overlay, Qhelp_echo);
27382 if (!NILP (help))
27384 help_echo_string = help;
27385 help_echo_window = window;
27386 help_echo_object = overlay;
27387 help_echo_pos = pos;
27389 else
27391 Lisp_Object obj = glyph->object;
27392 EMACS_INT charpos = glyph->charpos;
27394 /* Try text properties. */
27395 if (STRINGP (obj)
27396 && charpos >= 0
27397 && charpos < SCHARS (obj))
27399 help = Fget_text_property (make_number (charpos),
27400 Qhelp_echo, obj);
27401 if (NILP (help))
27403 /* If the string itself doesn't specify a help-echo,
27404 see if the buffer text ``under'' it does. */
27405 struct glyph_row *r
27406 = MATRIX_ROW (w->current_matrix, vpos);
27407 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27408 EMACS_INT p = string_buffer_position (obj, start);
27409 if (p > 0)
27411 help = Fget_char_property (make_number (p),
27412 Qhelp_echo, w->buffer);
27413 if (!NILP (help))
27415 charpos = p;
27416 obj = w->buffer;
27421 else if (BUFFERP (obj)
27422 && charpos >= BEGV
27423 && charpos < ZV)
27424 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27425 obj);
27427 if (!NILP (help))
27429 help_echo_string = help;
27430 help_echo_window = window;
27431 help_echo_object = obj;
27432 help_echo_pos = charpos;
27437 #ifdef HAVE_WINDOW_SYSTEM
27438 /* Look for a `pointer' property. */
27439 if (FRAME_WINDOW_P (f) && NILP (pointer))
27441 /* Check overlays first. */
27442 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27443 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27445 if (NILP (pointer))
27447 Lisp_Object obj = glyph->object;
27448 EMACS_INT charpos = glyph->charpos;
27450 /* Try text properties. */
27451 if (STRINGP (obj)
27452 && charpos >= 0
27453 && charpos < SCHARS (obj))
27455 pointer = Fget_text_property (make_number (charpos),
27456 Qpointer, obj);
27457 if (NILP (pointer))
27459 /* If the string itself doesn't specify a pointer,
27460 see if the buffer text ``under'' it does. */
27461 struct glyph_row *r
27462 = MATRIX_ROW (w->current_matrix, vpos);
27463 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27464 EMACS_INT p = string_buffer_position (obj, start);
27465 if (p > 0)
27466 pointer = Fget_char_property (make_number (p),
27467 Qpointer, w->buffer);
27470 else if (BUFFERP (obj)
27471 && charpos >= BEGV
27472 && charpos < ZV)
27473 pointer = Fget_text_property (make_number (charpos),
27474 Qpointer, obj);
27477 #endif /* HAVE_WINDOW_SYSTEM */
27479 BEGV = obegv;
27480 ZV = ozv;
27481 current_buffer = obuf;
27484 set_cursor:
27486 #ifdef HAVE_WINDOW_SYSTEM
27487 if (FRAME_WINDOW_P (f))
27488 define_frame_cursor1 (f, cursor, pointer);
27489 #else
27490 /* This is here to prevent a compiler error, about "label at end of
27491 compound statement". */
27492 return;
27493 #endif
27497 /* EXPORT for RIF:
27498 Clear any mouse-face on window W. This function is part of the
27499 redisplay interface, and is called from try_window_id and similar
27500 functions to ensure the mouse-highlight is off. */
27502 void
27503 x_clear_window_mouse_face (struct window *w)
27505 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27506 Lisp_Object window;
27508 BLOCK_INPUT;
27509 XSETWINDOW (window, w);
27510 if (EQ (window, hlinfo->mouse_face_window))
27511 clear_mouse_face (hlinfo);
27512 UNBLOCK_INPUT;
27516 /* EXPORT:
27517 Just discard the mouse face information for frame F, if any.
27518 This is used when the size of F is changed. */
27520 void
27521 cancel_mouse_face (struct frame *f)
27523 Lisp_Object window;
27524 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27526 window = hlinfo->mouse_face_window;
27527 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
27529 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27530 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27531 hlinfo->mouse_face_window = Qnil;
27537 /***********************************************************************
27538 Exposure Events
27539 ***********************************************************************/
27541 #ifdef HAVE_WINDOW_SYSTEM
27543 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
27544 which intersects rectangle R. R is in window-relative coordinates. */
27546 static void
27547 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
27548 enum glyph_row_area area)
27550 struct glyph *first = row->glyphs[area];
27551 struct glyph *end = row->glyphs[area] + row->used[area];
27552 struct glyph *last;
27553 int first_x, start_x, x;
27555 if (area == TEXT_AREA && row->fill_line_p)
27556 /* If row extends face to end of line write the whole line. */
27557 draw_glyphs (w, 0, row, area,
27558 0, row->used[area],
27559 DRAW_NORMAL_TEXT, 0);
27560 else
27562 /* Set START_X to the window-relative start position for drawing glyphs of
27563 AREA. The first glyph of the text area can be partially visible.
27564 The first glyphs of other areas cannot. */
27565 start_x = window_box_left_offset (w, area);
27566 x = start_x;
27567 if (area == TEXT_AREA)
27568 x += row->x;
27570 /* Find the first glyph that must be redrawn. */
27571 while (first < end
27572 && x + first->pixel_width < r->x)
27574 x += first->pixel_width;
27575 ++first;
27578 /* Find the last one. */
27579 last = first;
27580 first_x = x;
27581 while (last < end
27582 && x < r->x + r->width)
27584 x += last->pixel_width;
27585 ++last;
27588 /* Repaint. */
27589 if (last > first)
27590 draw_glyphs (w, first_x - start_x, row, area,
27591 first - row->glyphs[area], last - row->glyphs[area],
27592 DRAW_NORMAL_TEXT, 0);
27597 /* Redraw the parts of the glyph row ROW on window W intersecting
27598 rectangle R. R is in window-relative coordinates. Value is
27599 non-zero if mouse-face was overwritten. */
27601 static int
27602 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
27604 xassert (row->enabled_p);
27606 if (row->mode_line_p || w->pseudo_window_p)
27607 draw_glyphs (w, 0, row, TEXT_AREA,
27608 0, row->used[TEXT_AREA],
27609 DRAW_NORMAL_TEXT, 0);
27610 else
27612 if (row->used[LEFT_MARGIN_AREA])
27613 expose_area (w, row, r, LEFT_MARGIN_AREA);
27614 if (row->used[TEXT_AREA])
27615 expose_area (w, row, r, TEXT_AREA);
27616 if (row->used[RIGHT_MARGIN_AREA])
27617 expose_area (w, row, r, RIGHT_MARGIN_AREA);
27618 draw_row_fringe_bitmaps (w, row);
27621 return row->mouse_face_p;
27625 /* Redraw those parts of glyphs rows during expose event handling that
27626 overlap other rows. Redrawing of an exposed line writes over parts
27627 of lines overlapping that exposed line; this function fixes that.
27629 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
27630 row in W's current matrix that is exposed and overlaps other rows.
27631 LAST_OVERLAPPING_ROW is the last such row. */
27633 static void
27634 expose_overlaps (struct window *w,
27635 struct glyph_row *first_overlapping_row,
27636 struct glyph_row *last_overlapping_row,
27637 XRectangle *r)
27639 struct glyph_row *row;
27641 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
27642 if (row->overlapping_p)
27644 xassert (row->enabled_p && !row->mode_line_p);
27646 row->clip = r;
27647 if (row->used[LEFT_MARGIN_AREA])
27648 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
27650 if (row->used[TEXT_AREA])
27651 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
27653 if (row->used[RIGHT_MARGIN_AREA])
27654 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
27655 row->clip = NULL;
27660 /* Return non-zero if W's cursor intersects rectangle R. */
27662 static int
27663 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
27665 XRectangle cr, result;
27666 struct glyph *cursor_glyph;
27667 struct glyph_row *row;
27669 if (w->phys_cursor.vpos >= 0
27670 && w->phys_cursor.vpos < w->current_matrix->nrows
27671 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
27672 row->enabled_p)
27673 && row->cursor_in_fringe_p)
27675 /* Cursor is in the fringe. */
27676 cr.x = window_box_right_offset (w,
27677 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
27678 ? RIGHT_MARGIN_AREA
27679 : TEXT_AREA));
27680 cr.y = row->y;
27681 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
27682 cr.height = row->height;
27683 return x_intersect_rectangles (&cr, r, &result);
27686 cursor_glyph = get_phys_cursor_glyph (w);
27687 if (cursor_glyph)
27689 /* r is relative to W's box, but w->phys_cursor.x is relative
27690 to left edge of W's TEXT area. Adjust it. */
27691 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
27692 cr.y = w->phys_cursor.y;
27693 cr.width = cursor_glyph->pixel_width;
27694 cr.height = w->phys_cursor_height;
27695 /* ++KFS: W32 version used W32-specific IntersectRect here, but
27696 I assume the effect is the same -- and this is portable. */
27697 return x_intersect_rectangles (&cr, r, &result);
27699 /* If we don't understand the format, pretend we're not in the hot-spot. */
27700 return 0;
27704 /* EXPORT:
27705 Draw a vertical window border to the right of window W if W doesn't
27706 have vertical scroll bars. */
27708 void
27709 x_draw_vertical_border (struct window *w)
27711 struct frame *f = XFRAME (WINDOW_FRAME (w));
27713 /* We could do better, if we knew what type of scroll-bar the adjacent
27714 windows (on either side) have... But we don't :-(
27715 However, I think this works ok. ++KFS 2003-04-25 */
27717 /* Redraw borders between horizontally adjacent windows. Don't
27718 do it for frames with vertical scroll bars because either the
27719 right scroll bar of a window, or the left scroll bar of its
27720 neighbor will suffice as a border. */
27721 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
27722 return;
27724 if (!WINDOW_RIGHTMOST_P (w)
27725 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
27727 int x0, x1, y0, y1;
27729 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27730 y1 -= 1;
27732 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27733 x1 -= 1;
27735 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
27737 else if (!WINDOW_LEFTMOST_P (w)
27738 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
27740 int x0, x1, y0, y1;
27742 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27743 y1 -= 1;
27745 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27746 x0 -= 1;
27748 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
27753 /* Redraw the part of window W intersection rectangle FR. Pixel
27754 coordinates in FR are frame-relative. Call this function with
27755 input blocked. Value is non-zero if the exposure overwrites
27756 mouse-face. */
27758 static int
27759 expose_window (struct window *w, XRectangle *fr)
27761 struct frame *f = XFRAME (w->frame);
27762 XRectangle wr, r;
27763 int mouse_face_overwritten_p = 0;
27765 /* If window is not yet fully initialized, do nothing. This can
27766 happen when toolkit scroll bars are used and a window is split.
27767 Reconfiguring the scroll bar will generate an expose for a newly
27768 created window. */
27769 if (w->current_matrix == NULL)
27770 return 0;
27772 /* When we're currently updating the window, display and current
27773 matrix usually don't agree. Arrange for a thorough display
27774 later. */
27775 if (w == updated_window)
27777 SET_FRAME_GARBAGED (f);
27778 return 0;
27781 /* Frame-relative pixel rectangle of W. */
27782 wr.x = WINDOW_LEFT_EDGE_X (w);
27783 wr.y = WINDOW_TOP_EDGE_Y (w);
27784 wr.width = WINDOW_TOTAL_WIDTH (w);
27785 wr.height = WINDOW_TOTAL_HEIGHT (w);
27787 if (x_intersect_rectangles (fr, &wr, &r))
27789 int yb = window_text_bottom_y (w);
27790 struct glyph_row *row;
27791 int cursor_cleared_p, phys_cursor_on_p;
27792 struct glyph_row *first_overlapping_row, *last_overlapping_row;
27794 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
27795 r.x, r.y, r.width, r.height));
27797 /* Convert to window coordinates. */
27798 r.x -= WINDOW_LEFT_EDGE_X (w);
27799 r.y -= WINDOW_TOP_EDGE_Y (w);
27801 /* Turn off the cursor. */
27802 if (!w->pseudo_window_p
27803 && phys_cursor_in_rect_p (w, &r))
27805 x_clear_cursor (w);
27806 cursor_cleared_p = 1;
27808 else
27809 cursor_cleared_p = 0;
27811 /* If the row containing the cursor extends face to end of line,
27812 then expose_area might overwrite the cursor outside the
27813 rectangle and thus notice_overwritten_cursor might clear
27814 w->phys_cursor_on_p. We remember the original value and
27815 check later if it is changed. */
27816 phys_cursor_on_p = w->phys_cursor_on_p;
27818 /* Update lines intersecting rectangle R. */
27819 first_overlapping_row = last_overlapping_row = NULL;
27820 for (row = w->current_matrix->rows;
27821 row->enabled_p;
27822 ++row)
27824 int y0 = row->y;
27825 int y1 = MATRIX_ROW_BOTTOM_Y (row);
27827 if ((y0 >= r.y && y0 < r.y + r.height)
27828 || (y1 > r.y && y1 < r.y + r.height)
27829 || (r.y >= y0 && r.y < y1)
27830 || (r.y + r.height > y0 && r.y + r.height < y1))
27832 /* A header line may be overlapping, but there is no need
27833 to fix overlapping areas for them. KFS 2005-02-12 */
27834 if (row->overlapping_p && !row->mode_line_p)
27836 if (first_overlapping_row == NULL)
27837 first_overlapping_row = row;
27838 last_overlapping_row = row;
27841 row->clip = fr;
27842 if (expose_line (w, row, &r))
27843 mouse_face_overwritten_p = 1;
27844 row->clip = NULL;
27846 else if (row->overlapping_p)
27848 /* We must redraw a row overlapping the exposed area. */
27849 if (y0 < r.y
27850 ? y0 + row->phys_height > r.y
27851 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
27853 if (first_overlapping_row == NULL)
27854 first_overlapping_row = row;
27855 last_overlapping_row = row;
27859 if (y1 >= yb)
27860 break;
27863 /* Display the mode line if there is one. */
27864 if (WINDOW_WANTS_MODELINE_P (w)
27865 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
27866 row->enabled_p)
27867 && row->y < r.y + r.height)
27869 if (expose_line (w, row, &r))
27870 mouse_face_overwritten_p = 1;
27873 if (!w->pseudo_window_p)
27875 /* Fix the display of overlapping rows. */
27876 if (first_overlapping_row)
27877 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
27878 fr);
27880 /* Draw border between windows. */
27881 x_draw_vertical_border (w);
27883 /* Turn the cursor on again. */
27884 if (cursor_cleared_p
27885 || (phys_cursor_on_p && !w->phys_cursor_on_p))
27886 update_window_cursor (w, 1);
27890 return mouse_face_overwritten_p;
27895 /* Redraw (parts) of all windows in the window tree rooted at W that
27896 intersect R. R contains frame pixel coordinates. Value is
27897 non-zero if the exposure overwrites mouse-face. */
27899 static int
27900 expose_window_tree (struct window *w, XRectangle *r)
27902 struct frame *f = XFRAME (w->frame);
27903 int mouse_face_overwritten_p = 0;
27905 while (w && !FRAME_GARBAGED_P (f))
27907 if (!NILP (w->hchild))
27908 mouse_face_overwritten_p
27909 |= expose_window_tree (XWINDOW (w->hchild), r);
27910 else if (!NILP (w->vchild))
27911 mouse_face_overwritten_p
27912 |= expose_window_tree (XWINDOW (w->vchild), r);
27913 else
27914 mouse_face_overwritten_p |= expose_window (w, r);
27916 w = NILP (w->next) ? NULL : XWINDOW (w->next);
27919 return mouse_face_overwritten_p;
27923 /* EXPORT:
27924 Redisplay an exposed area of frame F. X and Y are the upper-left
27925 corner of the exposed rectangle. W and H are width and height of
27926 the exposed area. All are pixel values. W or H zero means redraw
27927 the entire frame. */
27929 void
27930 expose_frame (struct frame *f, int x, int y, int w, int h)
27932 XRectangle r;
27933 int mouse_face_overwritten_p = 0;
27935 TRACE ((stderr, "expose_frame "));
27937 /* No need to redraw if frame will be redrawn soon. */
27938 if (FRAME_GARBAGED_P (f))
27940 TRACE ((stderr, " garbaged\n"));
27941 return;
27944 /* If basic faces haven't been realized yet, there is no point in
27945 trying to redraw anything. This can happen when we get an expose
27946 event while Emacs is starting, e.g. by moving another window. */
27947 if (FRAME_FACE_CACHE (f) == NULL
27948 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
27950 TRACE ((stderr, " no faces\n"));
27951 return;
27954 if (w == 0 || h == 0)
27956 r.x = r.y = 0;
27957 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
27958 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
27960 else
27962 r.x = x;
27963 r.y = y;
27964 r.width = w;
27965 r.height = h;
27968 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
27969 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
27971 if (WINDOWP (f->tool_bar_window))
27972 mouse_face_overwritten_p
27973 |= expose_window (XWINDOW (f->tool_bar_window), &r);
27975 #ifdef HAVE_X_WINDOWS
27976 #ifndef MSDOS
27977 #ifndef USE_X_TOOLKIT
27978 if (WINDOWP (f->menu_bar_window))
27979 mouse_face_overwritten_p
27980 |= expose_window (XWINDOW (f->menu_bar_window), &r);
27981 #endif /* not USE_X_TOOLKIT */
27982 #endif
27983 #endif
27985 /* Some window managers support a focus-follows-mouse style with
27986 delayed raising of frames. Imagine a partially obscured frame,
27987 and moving the mouse into partially obscured mouse-face on that
27988 frame. The visible part of the mouse-face will be highlighted,
27989 then the WM raises the obscured frame. With at least one WM, KDE
27990 2.1, Emacs is not getting any event for the raising of the frame
27991 (even tried with SubstructureRedirectMask), only Expose events.
27992 These expose events will draw text normally, i.e. not
27993 highlighted. Which means we must redo the highlight here.
27994 Subsume it under ``we love X''. --gerd 2001-08-15 */
27995 /* Included in Windows version because Windows most likely does not
27996 do the right thing if any third party tool offers
27997 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
27998 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28000 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28001 if (f == hlinfo->mouse_face_mouse_frame)
28003 int mouse_x = hlinfo->mouse_face_mouse_x;
28004 int mouse_y = hlinfo->mouse_face_mouse_y;
28005 clear_mouse_face (hlinfo);
28006 note_mouse_highlight (f, mouse_x, mouse_y);
28012 /* EXPORT:
28013 Determine the intersection of two rectangles R1 and R2. Return
28014 the intersection in *RESULT. Value is non-zero if RESULT is not
28015 empty. */
28018 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28020 XRectangle *left, *right;
28021 XRectangle *upper, *lower;
28022 int intersection_p = 0;
28024 /* Rearrange so that R1 is the left-most rectangle. */
28025 if (r1->x < r2->x)
28026 left = r1, right = r2;
28027 else
28028 left = r2, right = r1;
28030 /* X0 of the intersection is right.x0, if this is inside R1,
28031 otherwise there is no intersection. */
28032 if (right->x <= left->x + left->width)
28034 result->x = right->x;
28036 /* The right end of the intersection is the minimum of
28037 the right ends of left and right. */
28038 result->width = (min (left->x + left->width, right->x + right->width)
28039 - result->x);
28041 /* Same game for Y. */
28042 if (r1->y < r2->y)
28043 upper = r1, lower = r2;
28044 else
28045 upper = r2, lower = r1;
28047 /* The upper end of the intersection is lower.y0, if this is inside
28048 of upper. Otherwise, there is no intersection. */
28049 if (lower->y <= upper->y + upper->height)
28051 result->y = lower->y;
28053 /* The lower end of the intersection is the minimum of the lower
28054 ends of upper and lower. */
28055 result->height = (min (lower->y + lower->height,
28056 upper->y + upper->height)
28057 - result->y);
28058 intersection_p = 1;
28062 return intersection_p;
28065 #endif /* HAVE_WINDOW_SYSTEM */
28068 /***********************************************************************
28069 Initialization
28070 ***********************************************************************/
28072 void
28073 syms_of_xdisp (void)
28075 Vwith_echo_area_save_vector = Qnil;
28076 staticpro (&Vwith_echo_area_save_vector);
28078 Vmessage_stack = Qnil;
28079 staticpro (&Vmessage_stack);
28081 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28083 message_dolog_marker1 = Fmake_marker ();
28084 staticpro (&message_dolog_marker1);
28085 message_dolog_marker2 = Fmake_marker ();
28086 staticpro (&message_dolog_marker2);
28087 message_dolog_marker3 = Fmake_marker ();
28088 staticpro (&message_dolog_marker3);
28090 #if GLYPH_DEBUG
28091 defsubr (&Sdump_frame_glyph_matrix);
28092 defsubr (&Sdump_glyph_matrix);
28093 defsubr (&Sdump_glyph_row);
28094 defsubr (&Sdump_tool_bar_row);
28095 defsubr (&Strace_redisplay);
28096 defsubr (&Strace_to_stderr);
28097 #endif
28098 #ifdef HAVE_WINDOW_SYSTEM
28099 defsubr (&Stool_bar_lines_needed);
28100 defsubr (&Slookup_image_map);
28101 #endif
28102 defsubr (&Sformat_mode_line);
28103 defsubr (&Sinvisible_p);
28104 defsubr (&Scurrent_bidi_paragraph_direction);
28106 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28107 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28108 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28109 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28110 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28111 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28112 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28113 DEFSYM (Qeval, "eval");
28114 DEFSYM (QCdata, ":data");
28115 DEFSYM (Qdisplay, "display");
28116 DEFSYM (Qspace_width, "space-width");
28117 DEFSYM (Qraise, "raise");
28118 DEFSYM (Qslice, "slice");
28119 DEFSYM (Qspace, "space");
28120 DEFSYM (Qmargin, "margin");
28121 DEFSYM (Qpointer, "pointer");
28122 DEFSYM (Qleft_margin, "left-margin");
28123 DEFSYM (Qright_margin, "right-margin");
28124 DEFSYM (Qcenter, "center");
28125 DEFSYM (Qline_height, "line-height");
28126 DEFSYM (QCalign_to, ":align-to");
28127 DEFSYM (QCrelative_width, ":relative-width");
28128 DEFSYM (QCrelative_height, ":relative-height");
28129 DEFSYM (QCeval, ":eval");
28130 DEFSYM (QCpropertize, ":propertize");
28131 DEFSYM (QCfile, ":file");
28132 DEFSYM (Qfontified, "fontified");
28133 DEFSYM (Qfontification_functions, "fontification-functions");
28134 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28135 DEFSYM (Qescape_glyph, "escape-glyph");
28136 DEFSYM (Qnobreak_space, "nobreak-space");
28137 DEFSYM (Qimage, "image");
28138 DEFSYM (Qtext, "text");
28139 DEFSYM (Qboth, "both");
28140 DEFSYM (Qboth_horiz, "both-horiz");
28141 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28142 DEFSYM (QCmap, ":map");
28143 DEFSYM (QCpointer, ":pointer");
28144 DEFSYM (Qrect, "rect");
28145 DEFSYM (Qcircle, "circle");
28146 DEFSYM (Qpoly, "poly");
28147 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28148 DEFSYM (Qgrow_only, "grow-only");
28149 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28150 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28151 DEFSYM (Qposition, "position");
28152 DEFSYM (Qbuffer_position, "buffer-position");
28153 DEFSYM (Qobject, "object");
28154 DEFSYM (Qbar, "bar");
28155 DEFSYM (Qhbar, "hbar");
28156 DEFSYM (Qbox, "box");
28157 DEFSYM (Qhollow, "hollow");
28158 DEFSYM (Qhand, "hand");
28159 DEFSYM (Qarrow, "arrow");
28160 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28162 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28163 Fcons (intern_c_string ("void-variable"), Qnil)),
28164 Qnil);
28165 staticpro (&list_of_error);
28167 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28168 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28169 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28170 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28172 echo_buffer[0] = echo_buffer[1] = Qnil;
28173 staticpro (&echo_buffer[0]);
28174 staticpro (&echo_buffer[1]);
28176 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28177 staticpro (&echo_area_buffer[0]);
28178 staticpro (&echo_area_buffer[1]);
28180 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
28181 staticpro (&Vmessages_buffer_name);
28183 mode_line_proptrans_alist = Qnil;
28184 staticpro (&mode_line_proptrans_alist);
28185 mode_line_string_list = Qnil;
28186 staticpro (&mode_line_string_list);
28187 mode_line_string_face = Qnil;
28188 staticpro (&mode_line_string_face);
28189 mode_line_string_face_prop = Qnil;
28190 staticpro (&mode_line_string_face_prop);
28191 Vmode_line_unwind_vector = Qnil;
28192 staticpro (&Vmode_line_unwind_vector);
28194 help_echo_string = Qnil;
28195 staticpro (&help_echo_string);
28196 help_echo_object = Qnil;
28197 staticpro (&help_echo_object);
28198 help_echo_window = Qnil;
28199 staticpro (&help_echo_window);
28200 previous_help_echo_string = Qnil;
28201 staticpro (&previous_help_echo_string);
28202 help_echo_pos = -1;
28204 DEFSYM (Qright_to_left, "right-to-left");
28205 DEFSYM (Qleft_to_right, "left-to-right");
28207 #ifdef HAVE_WINDOW_SYSTEM
28208 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28209 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
28210 For example, if a block cursor is over a tab, it will be drawn as
28211 wide as that tab on the display. */);
28212 x_stretch_cursor_p = 0;
28213 #endif
28215 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28216 doc: /* *Non-nil means highlight trailing whitespace.
28217 The face used for trailing whitespace is `trailing-whitespace'. */);
28218 Vshow_trailing_whitespace = Qnil;
28220 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28221 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28222 If the value is t, Emacs highlights non-ASCII chars which have the
28223 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28224 or `escape-glyph' face respectively.
28226 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28227 U+2011 (non-breaking hyphen) are affected.
28229 Any other non-nil value means to display these characters as a escape
28230 glyph followed by an ordinary space or hyphen.
28232 A value of nil means no special handling of these characters. */);
28233 Vnobreak_char_display = Qt;
28235 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28236 doc: /* *The pointer shape to show in void text areas.
28237 A value of nil means to show the text pointer. Other options are `arrow',
28238 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28239 Vvoid_text_area_pointer = Qarrow;
28241 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28242 doc: /* Non-nil means don't actually do any redisplay.
28243 This is used for internal purposes. */);
28244 Vinhibit_redisplay = Qnil;
28246 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28247 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28248 Vglobal_mode_string = Qnil;
28250 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28251 doc: /* Marker for where to display an arrow on top of the buffer text.
28252 This must be the beginning of a line in order to work.
28253 See also `overlay-arrow-string'. */);
28254 Voverlay_arrow_position = Qnil;
28256 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28257 doc: /* String to display as an arrow in non-window frames.
28258 See also `overlay-arrow-position'. */);
28259 Voverlay_arrow_string = make_pure_c_string ("=>");
28261 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28262 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28263 The symbols on this list are examined during redisplay to determine
28264 where to display overlay arrows. */);
28265 Voverlay_arrow_variable_list
28266 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28268 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28269 doc: /* *The number of lines to try scrolling a window by when point moves out.
28270 If that fails to bring point back on frame, point is centered instead.
28271 If this is zero, point is always centered after it moves off frame.
28272 If you want scrolling to always be a line at a time, you should set
28273 `scroll-conservatively' to a large value rather than set this to 1. */);
28275 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28276 doc: /* *Scroll up to this many lines, to bring point back on screen.
28277 If point moves off-screen, redisplay will scroll by up to
28278 `scroll-conservatively' lines in order to bring point just barely
28279 onto the screen again. If that cannot be done, then redisplay
28280 recenters point as usual.
28282 If the value is greater than 100, redisplay will never recenter point,
28283 but will always scroll just enough text to bring point into view, even
28284 if you move far away.
28286 A value of zero means always recenter point if it moves off screen. */);
28287 scroll_conservatively = 0;
28289 DEFVAR_INT ("scroll-margin", scroll_margin,
28290 doc: /* *Number of lines of margin at the top and bottom of a window.
28291 Recenter the window whenever point gets within this many lines
28292 of the top or bottom of the window. */);
28293 scroll_margin = 0;
28295 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28296 doc: /* Pixels per inch value for non-window system displays.
28297 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28298 Vdisplay_pixels_per_inch = make_float (72.0);
28300 #if GLYPH_DEBUG
28301 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28302 #endif
28304 DEFVAR_LISP ("truncate-partial-width-windows",
28305 Vtruncate_partial_width_windows,
28306 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28307 For an integer value, truncate lines in each window narrower than the
28308 full frame width, provided the window width is less than that integer;
28309 otherwise, respect the value of `truncate-lines'.
28311 For any other non-nil value, truncate lines in all windows that do
28312 not span the full frame width.
28314 A value of nil means to respect the value of `truncate-lines'.
28316 If `word-wrap' is enabled, you might want to reduce this. */);
28317 Vtruncate_partial_width_windows = make_number (50);
28319 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28320 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28321 Any other value means to use the appropriate face, `mode-line',
28322 `header-line', or `menu' respectively. */);
28323 mode_line_inverse_video = 1;
28325 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28326 doc: /* *Maximum buffer size for which line number should be displayed.
28327 If the buffer is bigger than this, the line number does not appear
28328 in the mode line. A value of nil means no limit. */);
28329 Vline_number_display_limit = Qnil;
28331 DEFVAR_INT ("line-number-display-limit-width",
28332 line_number_display_limit_width,
28333 doc: /* *Maximum line width (in characters) for line number display.
28334 If the average length of the lines near point is bigger than this, then the
28335 line number may be omitted from the mode line. */);
28336 line_number_display_limit_width = 200;
28338 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28339 doc: /* *Non-nil means highlight region even in nonselected windows. */);
28340 highlight_nonselected_windows = 0;
28342 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28343 doc: /* Non-nil if more than one frame is visible on this display.
28344 Minibuffer-only frames don't count, but iconified frames do.
28345 This variable is not guaranteed to be accurate except while processing
28346 `frame-title-format' and `icon-title-format'. */);
28348 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28349 doc: /* Template for displaying the title bar of visible frames.
28350 \(Assuming the window manager supports this feature.)
28352 This variable has the same structure as `mode-line-format', except that
28353 the %c and %l constructs are ignored. It is used only on frames for
28354 which no explicit name has been set \(see `modify-frame-parameters'). */);
28356 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28357 doc: /* Template for displaying the title bar of an iconified frame.
28358 \(Assuming the window manager supports this feature.)
28359 This variable has the same structure as `mode-line-format' (which see),
28360 and is used only on frames for which no explicit name has been set
28361 \(see `modify-frame-parameters'). */);
28362 Vicon_title_format
28363 = Vframe_title_format
28364 = pure_cons (intern_c_string ("multiple-frames"),
28365 pure_cons (make_pure_c_string ("%b"),
28366 pure_cons (pure_cons (empty_unibyte_string,
28367 pure_cons (intern_c_string ("invocation-name"),
28368 pure_cons (make_pure_c_string ("@"),
28369 pure_cons (intern_c_string ("system-name"),
28370 Qnil)))),
28371 Qnil)));
28373 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28374 doc: /* Maximum number of lines to keep in the message log buffer.
28375 If nil, disable message logging. If t, log messages but don't truncate
28376 the buffer when it becomes large. */);
28377 Vmessage_log_max = make_number (100);
28379 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28380 doc: /* Functions called before redisplay, if window sizes have changed.
28381 The value should be a list of functions that take one argument.
28382 Just before redisplay, for each frame, if any of its windows have changed
28383 size since the last redisplay, or have been split or deleted,
28384 all the functions in the list are called, with the frame as argument. */);
28385 Vwindow_size_change_functions = Qnil;
28387 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28388 doc: /* List of functions to call before redisplaying a window with scrolling.
28389 Each function is called with two arguments, the window and its new
28390 display-start position. Note that these functions are also called by
28391 `set-window-buffer'. Also note that the value of `window-end' is not
28392 valid when these functions are called.
28394 Warning: Do not use this feature to alter the way the window
28395 is scrolled. It is not designed for that, and such use probably won't
28396 work. */);
28397 Vwindow_scroll_functions = Qnil;
28399 DEFVAR_LISP ("window-text-change-functions",
28400 Vwindow_text_change_functions,
28401 doc: /* Functions to call in redisplay when text in the window might change. */);
28402 Vwindow_text_change_functions = Qnil;
28404 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28405 doc: /* Functions called when redisplay of a window reaches the end trigger.
28406 Each function is called with two arguments, the window and the end trigger value.
28407 See `set-window-redisplay-end-trigger'. */);
28408 Vredisplay_end_trigger_functions = Qnil;
28410 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28411 doc: /* *Non-nil means autoselect window with mouse pointer.
28412 If nil, do not autoselect windows.
28413 A positive number means delay autoselection by that many seconds: a
28414 window is autoselected only after the mouse has remained in that
28415 window for the duration of the delay.
28416 A negative number has a similar effect, but causes windows to be
28417 autoselected only after the mouse has stopped moving. \(Because of
28418 the way Emacs compares mouse events, you will occasionally wait twice
28419 that time before the window gets selected.\)
28420 Any other value means to autoselect window instantaneously when the
28421 mouse pointer enters it.
28423 Autoselection selects the minibuffer only if it is active, and never
28424 unselects the minibuffer if it is active.
28426 When customizing this variable make sure that the actual value of
28427 `focus-follows-mouse' matches the behavior of your window manager. */);
28428 Vmouse_autoselect_window = Qnil;
28430 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28431 doc: /* *Non-nil means automatically resize tool-bars.
28432 This dynamically changes the tool-bar's height to the minimum height
28433 that is needed to make all tool-bar items visible.
28434 If value is `grow-only', the tool-bar's height is only increased
28435 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28436 Vauto_resize_tool_bars = Qt;
28438 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28439 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28440 auto_raise_tool_bar_buttons_p = 1;
28442 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28443 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28444 make_cursor_line_fully_visible_p = 1;
28446 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
28447 doc: /* *Border below tool-bar in pixels.
28448 If an integer, use it as the height of the border.
28449 If it is one of `internal-border-width' or `border-width', use the
28450 value of the corresponding frame parameter.
28451 Otherwise, no border is added below the tool-bar. */);
28452 Vtool_bar_border = Qinternal_border_width;
28454 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
28455 doc: /* *Margin around tool-bar buttons in pixels.
28456 If an integer, use that for both horizontal and vertical margins.
28457 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
28458 HORZ specifying the horizontal margin, and VERT specifying the
28459 vertical margin. */);
28460 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
28462 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
28463 doc: /* *Relief thickness of tool-bar buttons. */);
28464 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
28466 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
28467 doc: /* Tool bar style to use.
28468 It can be one of
28469 image - show images only
28470 text - show text only
28471 both - show both, text below image
28472 both-horiz - show text to the right of the image
28473 text-image-horiz - show text to the left of the image
28474 any other - use system default or image if no system default. */);
28475 Vtool_bar_style = Qnil;
28477 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
28478 doc: /* *Maximum number of characters a label can have to be shown.
28479 The tool bar style must also show labels for this to have any effect, see
28480 `tool-bar-style'. */);
28481 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
28483 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
28484 doc: /* List of functions to call to fontify regions of text.
28485 Each function is called with one argument POS. Functions must
28486 fontify a region starting at POS in the current buffer, and give
28487 fontified regions the property `fontified'. */);
28488 Vfontification_functions = Qnil;
28489 Fmake_variable_buffer_local (Qfontification_functions);
28491 DEFVAR_BOOL ("unibyte-display-via-language-environment",
28492 unibyte_display_via_language_environment,
28493 doc: /* *Non-nil means display unibyte text according to language environment.
28494 Specifically, this means that raw bytes in the range 160-255 decimal
28495 are displayed by converting them to the equivalent multibyte characters
28496 according to the current language environment. As a result, they are
28497 displayed according to the current fontset.
28499 Note that this variable affects only how these bytes are displayed,
28500 but does not change the fact they are interpreted as raw bytes. */);
28501 unibyte_display_via_language_environment = 0;
28503 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
28504 doc: /* *Maximum height for resizing mini-windows (the minibuffer and the echo area).
28505 If a float, it specifies a fraction of the mini-window frame's height.
28506 If an integer, it specifies a number of lines. */);
28507 Vmax_mini_window_height = make_float (0.25);
28509 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
28510 doc: /* How to resize mini-windows (the minibuffer and the echo area).
28511 A value of nil means don't automatically resize mini-windows.
28512 A value of t means resize them to fit the text displayed in them.
28513 A value of `grow-only', the default, means let mini-windows grow only;
28514 they return to their normal size when the minibuffer is closed, or the
28515 echo area becomes empty. */);
28516 Vresize_mini_windows = Qgrow_only;
28518 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
28519 doc: /* Alist specifying how to blink the cursor off.
28520 Each element has the form (ON-STATE . OFF-STATE). Whenever the
28521 `cursor-type' frame-parameter or variable equals ON-STATE,
28522 comparing using `equal', Emacs uses OFF-STATE to specify
28523 how to blink it off. ON-STATE and OFF-STATE are values for
28524 the `cursor-type' frame parameter.
28526 If a frame's ON-STATE has no entry in this list,
28527 the frame's other specifications determine how to blink the cursor off. */);
28528 Vblink_cursor_alist = Qnil;
28530 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
28531 doc: /* Allow or disallow automatic horizontal scrolling of windows.
28532 If non-nil, windows are automatically scrolled horizontally to make
28533 point visible. */);
28534 automatic_hscrolling_p = 1;
28535 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
28537 DEFVAR_INT ("hscroll-margin", hscroll_margin,
28538 doc: /* *How many columns away from the window edge point is allowed to get
28539 before automatic hscrolling will horizontally scroll the window. */);
28540 hscroll_margin = 5;
28542 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
28543 doc: /* *How many columns to scroll the window when point gets too close to the edge.
28544 When point is less than `hscroll-margin' columns from the window
28545 edge, automatic hscrolling will scroll the window by the amount of columns
28546 determined by this variable. If its value is a positive integer, scroll that
28547 many columns. If it's a positive floating-point number, it specifies the
28548 fraction of the window's width to scroll. If it's nil or zero, point will be
28549 centered horizontally after the scroll. Any other value, including negative
28550 numbers, are treated as if the value were zero.
28552 Automatic hscrolling always moves point outside the scroll margin, so if
28553 point was more than scroll step columns inside the margin, the window will
28554 scroll more than the value given by the scroll step.
28556 Note that the lower bound for automatic hscrolling specified by `scroll-left'
28557 and `scroll-right' overrides this variable's effect. */);
28558 Vhscroll_step = make_number (0);
28560 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
28561 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
28562 Bind this around calls to `message' to let it take effect. */);
28563 message_truncate_lines = 0;
28565 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
28566 doc: /* Normal hook run to update the menu bar definitions.
28567 Redisplay runs this hook before it redisplays the menu bar.
28568 This is used to update submenus such as Buffers,
28569 whose contents depend on various data. */);
28570 Vmenu_bar_update_hook = Qnil;
28572 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
28573 doc: /* Frame for which we are updating a menu.
28574 The enable predicate for a menu binding should check this variable. */);
28575 Vmenu_updating_frame = Qnil;
28577 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
28578 doc: /* Non-nil means don't update menu bars. Internal use only. */);
28579 inhibit_menubar_update = 0;
28581 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
28582 doc: /* Prefix prepended to all continuation lines at display time.
28583 The value may be a string, an image, or a stretch-glyph; it is
28584 interpreted in the same way as the value of a `display' text property.
28586 This variable is overridden by any `wrap-prefix' text or overlay
28587 property.
28589 To add a prefix to non-continuation lines, use `line-prefix'. */);
28590 Vwrap_prefix = Qnil;
28591 DEFSYM (Qwrap_prefix, "wrap-prefix");
28592 Fmake_variable_buffer_local (Qwrap_prefix);
28594 DEFVAR_LISP ("line-prefix", Vline_prefix,
28595 doc: /* Prefix prepended to all non-continuation lines at display time.
28596 The value may be a string, an image, or a stretch-glyph; it is
28597 interpreted in the same way as the value of a `display' text property.
28599 This variable is overridden by any `line-prefix' text or overlay
28600 property.
28602 To add a prefix to continuation lines, use `wrap-prefix'. */);
28603 Vline_prefix = Qnil;
28604 DEFSYM (Qline_prefix, "line-prefix");
28605 Fmake_variable_buffer_local (Qline_prefix);
28607 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
28608 doc: /* Non-nil means don't eval Lisp during redisplay. */);
28609 inhibit_eval_during_redisplay = 0;
28611 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
28612 doc: /* Non-nil means don't free realized faces. Internal use only. */);
28613 inhibit_free_realized_faces = 0;
28615 #if GLYPH_DEBUG
28616 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
28617 doc: /* Inhibit try_window_id display optimization. */);
28618 inhibit_try_window_id = 0;
28620 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
28621 doc: /* Inhibit try_window_reusing display optimization. */);
28622 inhibit_try_window_reusing = 0;
28624 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
28625 doc: /* Inhibit try_cursor_movement display optimization. */);
28626 inhibit_try_cursor_movement = 0;
28627 #endif /* GLYPH_DEBUG */
28629 DEFVAR_INT ("overline-margin", overline_margin,
28630 doc: /* *Space between overline and text, in pixels.
28631 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
28632 margin to the character height. */);
28633 overline_margin = 2;
28635 DEFVAR_INT ("underline-minimum-offset",
28636 underline_minimum_offset,
28637 doc: /* Minimum distance between baseline and underline.
28638 This can improve legibility of underlined text at small font sizes,
28639 particularly when using variable `x-use-underline-position-properties'
28640 with fonts that specify an UNDERLINE_POSITION relatively close to the
28641 baseline. The default value is 1. */);
28642 underline_minimum_offset = 1;
28644 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
28645 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
28646 This feature only works when on a window system that can change
28647 cursor shapes. */);
28648 display_hourglass_p = 1;
28650 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
28651 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
28652 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
28654 hourglass_atimer = NULL;
28655 hourglass_shown_p = 0;
28657 DEFSYM (Qglyphless_char, "glyphless-char");
28658 DEFSYM (Qhex_code, "hex-code");
28659 DEFSYM (Qempty_box, "empty-box");
28660 DEFSYM (Qthin_space, "thin-space");
28661 DEFSYM (Qzero_width, "zero-width");
28663 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
28664 /* Intern this now in case it isn't already done.
28665 Setting this variable twice is harmless.
28666 But don't staticpro it here--that is done in alloc.c. */
28667 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
28668 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
28670 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
28671 doc: /* Char-table defining glyphless characters.
28672 Each element, if non-nil, should be one of the following:
28673 an ASCII acronym string: display this string in a box
28674 `hex-code': display the hexadecimal code of a character in a box
28675 `empty-box': display as an empty box
28676 `thin-space': display as 1-pixel width space
28677 `zero-width': don't display
28678 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
28679 display method for graphical terminals and text terminals respectively.
28680 GRAPHICAL and TEXT should each have one of the values listed above.
28682 The char-table has one extra slot to control the display of a character for
28683 which no font is found. This slot only takes effect on graphical terminals.
28684 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
28685 `thin-space'. The default is `empty-box'. */);
28686 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
28687 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
28688 Qempty_box);
28692 /* Initialize this module when Emacs starts. */
28694 void
28695 init_xdisp (void)
28697 current_header_line_height = current_mode_line_height = -1;
28699 CHARPOS (this_line_start_pos) = 0;
28701 if (!noninteractive)
28703 struct window *m = XWINDOW (minibuf_window);
28704 Lisp_Object frame = m->frame;
28705 struct frame *f = XFRAME (frame);
28706 Lisp_Object root = FRAME_ROOT_WINDOW (f);
28707 struct window *r = XWINDOW (root);
28708 int i;
28710 echo_area_window = minibuf_window;
28712 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
28713 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
28714 XSETFASTINT (r->total_cols, FRAME_COLS (f));
28715 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
28716 XSETFASTINT (m->total_lines, 1);
28717 XSETFASTINT (m->total_cols, FRAME_COLS (f));
28719 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
28720 scratch_glyph_row.glyphs[TEXT_AREA + 1]
28721 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
28723 /* The default ellipsis glyphs `...'. */
28724 for (i = 0; i < 3; ++i)
28725 default_invis_vector[i] = make_number ('.');
28729 /* Allocate the buffer for frame titles.
28730 Also used for `format-mode-line'. */
28731 int size = 100;
28732 mode_line_noprop_buf = (char *) xmalloc (size);
28733 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
28734 mode_line_noprop_ptr = mode_line_noprop_buf;
28735 mode_line_target = MODE_LINE_DISPLAY;
28738 help_echo_showing_p = 0;
28741 /* Since w32 does not support atimers, it defines its own implementation of
28742 the following three functions in w32fns.c. */
28743 #ifndef WINDOWSNT
28745 /* Platform-independent portion of hourglass implementation. */
28747 /* Return non-zero if hourglass timer has been started or hourglass is
28748 shown. */
28750 hourglass_started (void)
28752 return hourglass_shown_p || hourglass_atimer != NULL;
28755 /* Cancel a currently active hourglass timer, and start a new one. */
28756 void
28757 start_hourglass (void)
28759 #if defined (HAVE_WINDOW_SYSTEM)
28760 EMACS_TIME delay;
28761 int secs, usecs = 0;
28763 cancel_hourglass ();
28765 if (INTEGERP (Vhourglass_delay)
28766 && XINT (Vhourglass_delay) > 0)
28767 secs = XFASTINT (Vhourglass_delay);
28768 else if (FLOATP (Vhourglass_delay)
28769 && XFLOAT_DATA (Vhourglass_delay) > 0)
28771 Lisp_Object tem;
28772 tem = Ftruncate (Vhourglass_delay, Qnil);
28773 secs = XFASTINT (tem);
28774 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
28776 else
28777 secs = DEFAULT_HOURGLASS_DELAY;
28779 EMACS_SET_SECS_USECS (delay, secs, usecs);
28780 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
28781 show_hourglass, NULL);
28782 #endif
28786 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
28787 shown. */
28788 void
28789 cancel_hourglass (void)
28791 #if defined (HAVE_WINDOW_SYSTEM)
28792 if (hourglass_atimer)
28794 cancel_atimer (hourglass_atimer);
28795 hourglass_atimer = NULL;
28798 if (hourglass_shown_p)
28799 hide_hourglass ();
28800 #endif
28802 #endif /* ! WINDOWSNT */