Fix bug #11464 with pos-visible-in-window-p and R2L text in L2R paragraph.
[emacs.git] / src / xdisp.c
bloba2c4589766d26b415c2897f34e3140fb1fb8c666
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 iterate_out_of_display_property (struct it *);
843 static void pop_it (struct it *);
844 static void sync_frame_with_window_matrix_rows (struct window *);
845 static void select_frame_for_redisplay (Lisp_Object);
846 static void redisplay_internal (void);
847 static int echo_area_display (int);
848 static void redisplay_windows (Lisp_Object);
849 static void redisplay_window (Lisp_Object, int);
850 static Lisp_Object redisplay_window_error (Lisp_Object);
851 static Lisp_Object redisplay_window_0 (Lisp_Object);
852 static Lisp_Object redisplay_window_1 (Lisp_Object);
853 static int set_cursor_from_row (struct window *, struct glyph_row *,
854 struct glyph_matrix *, EMACS_INT, EMACS_INT,
855 int, int);
856 static int update_menu_bar (struct frame *, int, int);
857 static int try_window_reusing_current_matrix (struct window *);
858 static int try_window_id (struct window *);
859 static int display_line (struct it *);
860 static int display_mode_lines (struct window *);
861 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
862 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
863 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
864 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
865 static void display_menu_bar (struct window *);
866 static EMACS_INT display_count_lines (EMACS_INT, EMACS_INT, EMACS_INT,
867 EMACS_INT *);
868 static int display_string (const char *, Lisp_Object, Lisp_Object,
869 EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
870 static void compute_line_metrics (struct it *);
871 static void run_redisplay_end_trigger_hook (struct it *);
872 static int get_overlay_strings (struct it *, EMACS_INT);
873 static int get_overlay_strings_1 (struct it *, EMACS_INT, int);
874 static void next_overlay_string (struct it *);
875 static void reseat (struct it *, struct text_pos, int);
876 static void reseat_1 (struct it *, struct text_pos, int);
877 static void back_to_previous_visible_line_start (struct it *);
878 void reseat_at_previous_visible_line_start (struct it *);
879 static void reseat_at_next_visible_line_start (struct it *, int);
880 static int next_element_from_ellipsis (struct it *);
881 static int next_element_from_display_vector (struct it *);
882 static int next_element_from_string (struct it *);
883 static int next_element_from_c_string (struct it *);
884 static int next_element_from_buffer (struct it *);
885 static int next_element_from_composition (struct it *);
886 static int next_element_from_image (struct it *);
887 static int next_element_from_stretch (struct it *);
888 static void load_overlay_strings (struct it *, EMACS_INT);
889 static int init_from_display_pos (struct it *, struct window *,
890 struct display_pos *);
891 static void reseat_to_string (struct it *, const char *,
892 Lisp_Object, EMACS_INT, EMACS_INT, int, int);
893 static int get_next_display_element (struct it *);
894 static enum move_it_result
895 move_it_in_display_line_to (struct it *, EMACS_INT, int,
896 enum move_operation_enum);
897 void move_it_vertically_backward (struct it *, int);
898 static void init_to_row_start (struct it *, struct window *,
899 struct glyph_row *);
900 static int init_to_row_end (struct it *, struct window *,
901 struct glyph_row *);
902 static void back_to_previous_line_start (struct it *);
903 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
904 static struct text_pos string_pos_nchars_ahead (struct text_pos,
905 Lisp_Object, EMACS_INT);
906 static struct text_pos string_pos (EMACS_INT, Lisp_Object);
907 static struct text_pos c_string_pos (EMACS_INT, const char *, int);
908 static EMACS_INT number_of_chars (const char *, int);
909 static void compute_stop_pos (struct it *);
910 static void compute_string_pos (struct text_pos *, struct text_pos,
911 Lisp_Object);
912 static int face_before_or_after_it_pos (struct it *, int);
913 static EMACS_INT next_overlay_change (EMACS_INT);
914 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
915 Lisp_Object, struct text_pos *, EMACS_INT, int);
916 static int handle_single_display_spec (struct it *, Lisp_Object,
917 Lisp_Object, Lisp_Object,
918 struct text_pos *, EMACS_INT, int, int);
919 static int underlying_face_id (struct it *);
920 static int in_ellipses_for_invisible_text_p (struct display_pos *,
921 struct window *);
923 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
924 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
926 #ifdef HAVE_WINDOW_SYSTEM
928 static void x_consider_frame_title (Lisp_Object);
929 static int tool_bar_lines_needed (struct frame *, int *);
930 static void update_tool_bar (struct frame *, int);
931 static void build_desired_tool_bar_string (struct frame *f);
932 static int redisplay_tool_bar (struct frame *);
933 static void display_tool_bar_line (struct it *, int);
934 static void notice_overwritten_cursor (struct window *,
935 enum glyph_row_area,
936 int, int, int, int);
937 static void append_stretch_glyph (struct it *, Lisp_Object,
938 int, int, int);
941 #endif /* HAVE_WINDOW_SYSTEM */
943 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
944 static int coords_in_mouse_face_p (struct window *, int, int);
948 /***********************************************************************
949 Window display dimensions
950 ***********************************************************************/
952 /* Return the bottom boundary y-position for text lines in window W.
953 This is the first y position at which a line cannot start.
954 It is relative to the top of the window.
956 This is the height of W minus the height of a mode line, if any. */
959 window_text_bottom_y (struct window *w)
961 int height = WINDOW_TOTAL_HEIGHT (w);
963 if (WINDOW_WANTS_MODELINE_P (w))
964 height -= CURRENT_MODE_LINE_HEIGHT (w);
965 return height;
968 /* Return the pixel width of display area AREA of window W. AREA < 0
969 means return the total width of W, not including fringes to
970 the left and right of the window. */
973 window_box_width (struct window *w, int area)
975 int cols = XFASTINT (w->total_cols);
976 int pixels = 0;
978 if (!w->pseudo_window_p)
980 cols -= WINDOW_SCROLL_BAR_COLS (w);
982 if (area == TEXT_AREA)
984 if (INTEGERP (w->left_margin_cols))
985 cols -= XFASTINT (w->left_margin_cols);
986 if (INTEGERP (w->right_margin_cols))
987 cols -= XFASTINT (w->right_margin_cols);
988 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
990 else if (area == LEFT_MARGIN_AREA)
992 cols = (INTEGERP (w->left_margin_cols)
993 ? XFASTINT (w->left_margin_cols) : 0);
994 pixels = 0;
996 else if (area == RIGHT_MARGIN_AREA)
998 cols = (INTEGERP (w->right_margin_cols)
999 ? XFASTINT (w->right_margin_cols) : 0);
1000 pixels = 0;
1004 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1008 /* Return the pixel height of the display area of window W, not
1009 including mode lines of W, if any. */
1012 window_box_height (struct window *w)
1014 struct frame *f = XFRAME (w->frame);
1015 int height = WINDOW_TOTAL_HEIGHT (w);
1017 xassert (height >= 0);
1019 /* Note: the code below that determines the mode-line/header-line
1020 height is essentially the same as that contained in the macro
1021 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1022 the appropriate glyph row has its `mode_line_p' flag set,
1023 and if it doesn't, uses estimate_mode_line_height instead. */
1025 if (WINDOW_WANTS_MODELINE_P (w))
1027 struct glyph_row *ml_row
1028 = (w->current_matrix && w->current_matrix->rows
1029 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1030 : 0);
1031 if (ml_row && ml_row->mode_line_p)
1032 height -= ml_row->height;
1033 else
1034 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1037 if (WINDOW_WANTS_HEADER_LINE_P (w))
1039 struct glyph_row *hl_row
1040 = (w->current_matrix && w->current_matrix->rows
1041 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1042 : 0);
1043 if (hl_row && hl_row->mode_line_p)
1044 height -= hl_row->height;
1045 else
1046 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1049 /* With a very small font and a mode-line that's taller than
1050 default, we might end up with a negative height. */
1051 return max (0, height);
1054 /* Return the window-relative coordinate of the left edge of display
1055 area AREA of window W. AREA < 0 means return the left edge of the
1056 whole window, to the right of the left fringe of W. */
1059 window_box_left_offset (struct window *w, int area)
1061 int x;
1063 if (w->pseudo_window_p)
1064 return 0;
1066 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1068 if (area == TEXT_AREA)
1069 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1070 + window_box_width (w, LEFT_MARGIN_AREA));
1071 else if (area == RIGHT_MARGIN_AREA)
1072 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1073 + window_box_width (w, LEFT_MARGIN_AREA)
1074 + window_box_width (w, TEXT_AREA)
1075 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1077 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1078 else if (area == LEFT_MARGIN_AREA
1079 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1080 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1082 return x;
1086 /* Return the window-relative coordinate of the right edge of display
1087 area AREA of window W. AREA < 0 means return the right edge of the
1088 whole window, to the left of the right fringe of W. */
1091 window_box_right_offset (struct window *w, int area)
1093 return window_box_left_offset (w, area) + window_box_width (w, area);
1096 /* Return the frame-relative coordinate of the left edge of display
1097 area AREA of window W. AREA < 0 means return the left edge of the
1098 whole window, to the right of the left fringe of W. */
1101 window_box_left (struct window *w, int area)
1103 struct frame *f = XFRAME (w->frame);
1104 int x;
1106 if (w->pseudo_window_p)
1107 return FRAME_INTERNAL_BORDER_WIDTH (f);
1109 x = (WINDOW_LEFT_EDGE_X (w)
1110 + window_box_left_offset (w, area));
1112 return x;
1116 /* Return the frame-relative coordinate of the right edge of display
1117 area AREA of window W. AREA < 0 means return the right edge of the
1118 whole window, to the left of the right fringe of W. */
1121 window_box_right (struct window *w, int area)
1123 return window_box_left (w, area) + window_box_width (w, area);
1126 /* Get the bounding box of the display area AREA of window W, without
1127 mode lines, in frame-relative coordinates. AREA < 0 means the
1128 whole window, not including the left and right fringes of
1129 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1130 coordinates of the upper-left corner of the box. Return in
1131 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1133 void
1134 window_box (struct window *w, int area, int *box_x, int *box_y,
1135 int *box_width, int *box_height)
1137 if (box_width)
1138 *box_width = window_box_width (w, area);
1139 if (box_height)
1140 *box_height = window_box_height (w);
1141 if (box_x)
1142 *box_x = window_box_left (w, area);
1143 if (box_y)
1145 *box_y = WINDOW_TOP_EDGE_Y (w);
1146 if (WINDOW_WANTS_HEADER_LINE_P (w))
1147 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1152 /* Get the bounding box of the display area AREA of window W, without
1153 mode lines. AREA < 0 means the whole window, not including the
1154 left and right fringe of the window. Return in *TOP_LEFT_X
1155 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1156 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1157 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1158 box. */
1160 static inline void
1161 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1162 int *bottom_right_x, int *bottom_right_y)
1164 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1165 bottom_right_y);
1166 *bottom_right_x += *top_left_x;
1167 *bottom_right_y += *top_left_y;
1172 /***********************************************************************
1173 Utilities
1174 ***********************************************************************/
1176 /* Return the bottom y-position of the line the iterator IT is in.
1177 This can modify IT's settings. */
1180 line_bottom_y (struct it *it)
1182 int line_height = it->max_ascent + it->max_descent;
1183 int line_top_y = it->current_y;
1185 if (line_height == 0)
1187 if (last_height)
1188 line_height = last_height;
1189 else if (IT_CHARPOS (*it) < ZV)
1191 move_it_by_lines (it, 1);
1192 line_height = (it->max_ascent || it->max_descent
1193 ? it->max_ascent + it->max_descent
1194 : last_height);
1196 else
1198 struct glyph_row *row = it->glyph_row;
1200 /* Use the default character height. */
1201 it->glyph_row = NULL;
1202 it->what = IT_CHARACTER;
1203 it->c = ' ';
1204 it->len = 1;
1205 PRODUCE_GLYPHS (it);
1206 line_height = it->ascent + it->descent;
1207 it->glyph_row = row;
1211 return line_top_y + line_height;
1214 /* Subroutine of pos_visible_p below. Extracts a display string, if
1215 any, from the display spec given as its argument. */
1216 static Lisp_Object
1217 string_from_display_spec (Lisp_Object spec)
1219 if (CONSP (spec))
1221 while (CONSP (spec))
1223 if (STRINGP (XCAR (spec)))
1224 return XCAR (spec);
1225 spec = XCDR (spec);
1228 else if (VECTORP (spec))
1230 ptrdiff_t i;
1232 for (i = 0; i < ASIZE (spec); i++)
1234 if (STRINGP (AREF (spec, i)))
1235 return AREF (spec, i);
1237 return Qnil;
1240 return spec;
1243 /* Return 1 if position CHARPOS is visible in window W.
1244 CHARPOS < 0 means return info about WINDOW_END position.
1245 If visible, set *X and *Y to pixel coordinates of top left corner.
1246 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1247 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1250 pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1251 int *rtop, int *rbot, int *rowh, int *vpos)
1253 struct it it;
1254 void *itdata = bidi_shelve_cache ();
1255 struct text_pos top;
1256 int visible_p = 0;
1257 struct buffer *old_buffer = NULL;
1259 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1260 return visible_p;
1262 if (XBUFFER (w->buffer) != current_buffer)
1264 old_buffer = current_buffer;
1265 set_buffer_internal_1 (XBUFFER (w->buffer));
1268 SET_TEXT_POS_FROM_MARKER (top, w->start);
1269 /* Scrolling a minibuffer window via scroll bar when the echo area
1270 shows long text sometimes resets the minibuffer contents behind
1271 our backs. */
1272 if (CHARPOS (top) > ZV)
1273 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1275 /* Compute exact mode line heights. */
1276 if (WINDOW_WANTS_MODELINE_P (w))
1277 current_mode_line_height
1278 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1279 BVAR (current_buffer, mode_line_format));
1281 if (WINDOW_WANTS_HEADER_LINE_P (w))
1282 current_header_line_height
1283 = display_mode_line (w, HEADER_LINE_FACE_ID,
1284 BVAR (current_buffer, header_line_format));
1286 start_display (&it, w, top);
1287 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1288 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1290 if (charpos >= 0
1291 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1292 && IT_CHARPOS (it) >= charpos)
1293 /* When scanning backwards under bidi iteration, move_it_to
1294 stops at or _before_ CHARPOS, because it stops at or to
1295 the _right_ of the character at CHARPOS. */
1296 || (it.bidi_p && it.bidi_it.scan_dir == -1
1297 && IT_CHARPOS (it) <= charpos)))
1299 /* We have reached CHARPOS, or passed it. How the call to
1300 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1301 or covered by a display property, move_it_to stops at the end
1302 of the invisible text, to the right of CHARPOS. (ii) If
1303 CHARPOS is in a display vector, move_it_to stops on its last
1304 glyph. */
1305 int top_x = it.current_x;
1306 int top_y = it.current_y;
1307 /* Calling line_bottom_y may change it.method, it.position, etc. */
1308 enum it_method it_method = it.method;
1309 int bottom_y = (last_height = 0, line_bottom_y (&it));
1310 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1312 if (top_y < window_top_y)
1313 visible_p = bottom_y > window_top_y;
1314 else if (top_y < it.last_visible_y)
1315 visible_p = 1;
1316 if (bottom_y >= it.last_visible_y
1317 && it.bidi_p && it.bidi_it.scan_dir == -1
1318 && IT_CHARPOS (it) < charpos)
1320 /* When the last line of the window is scanned backwards
1321 under bidi iteration, we could be duped into thinking
1322 that we have passed CHARPOS, when in fact move_it_to
1323 simply stopped short of CHARPOS because it reached
1324 last_visible_y. To see if that's what happened, we call
1325 move_it_to again with a slightly larger vertical limit,
1326 and see if it actually moved vertically; if it did, we
1327 didn't really reach CHARPOS, which is beyond window end. */
1328 struct it save_it = it;
1329 /* Why 10? because we don't know how many canonical lines
1330 will the height of the next line(s) be. So we guess. */
1331 int ten_more_lines =
1332 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1334 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1335 MOVE_TO_POS | MOVE_TO_Y);
1336 if (it.current_y > top_y)
1337 visible_p = 0;
1339 it = save_it;
1341 if (visible_p)
1343 if (it_method == GET_FROM_DISPLAY_VECTOR)
1345 /* We stopped on the last glyph of a display vector.
1346 Try and recompute. Hack alert! */
1347 if (charpos < 2 || top.charpos >= charpos)
1348 top_x = it.glyph_row->x;
1349 else
1351 struct it it2;
1352 start_display (&it2, w, top);
1353 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1354 get_next_display_element (&it2);
1355 PRODUCE_GLYPHS (&it2);
1356 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1357 || it2.current_x > it2.last_visible_x)
1358 top_x = it.glyph_row->x;
1359 else
1361 top_x = it2.current_x;
1362 top_y = it2.current_y;
1366 else if (IT_CHARPOS (it) != charpos)
1368 Lisp_Object cpos = make_number (charpos);
1369 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1370 Lisp_Object string = string_from_display_spec (spec);
1371 int newline_in_string = 0;
1373 if (STRINGP (string))
1375 const char *s = SSDATA (string);
1376 const char *e = s + SBYTES (string);
1377 while (s < e)
1379 if (*s++ == '\n')
1381 newline_in_string = 1;
1382 break;
1386 /* The tricky code below is needed because there's a
1387 discrepancy between move_it_to and how we set cursor
1388 when the display line ends in a newline from a
1389 display string. move_it_to will stop _after_ such
1390 display strings, whereas set_cursor_from_row
1391 conspires with cursor_row_p to place the cursor on
1392 the first glyph produced from the display string. */
1394 /* We have overshoot PT because it is covered by a
1395 display property whose value is a string. If the
1396 string includes embedded newlines, we are also in the
1397 wrong display line. Backtrack to the correct line,
1398 where the display string begins. */
1399 if (newline_in_string)
1401 Lisp_Object startpos, endpos;
1402 EMACS_INT start, end;
1403 struct it it3;
1404 int it3_moved;
1406 /* Find the first and the last buffer positions
1407 covered by the display string. */
1408 endpos =
1409 Fnext_single_char_property_change (cpos, Qdisplay,
1410 Qnil, Qnil);
1411 startpos =
1412 Fprevious_single_char_property_change (endpos, Qdisplay,
1413 Qnil, Qnil);
1414 start = XFASTINT (startpos);
1415 end = XFASTINT (endpos);
1416 /* Move to the last buffer position before the
1417 display property. */
1418 start_display (&it3, w, top);
1419 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1420 /* Move forward one more line if the position before
1421 the display string is a newline or if it is the
1422 rightmost character on a line that is
1423 continued or word-wrapped. */
1424 if (it3.method == GET_FROM_BUFFER
1425 && it3.c == '\n')
1426 move_it_by_lines (&it3, 1);
1427 else if (move_it_in_display_line_to (&it3, -1,
1428 it3.current_x
1429 + it3.pixel_width,
1430 MOVE_TO_X)
1431 == MOVE_LINE_CONTINUED)
1433 move_it_by_lines (&it3, 1);
1434 /* When we are under word-wrap, the #$@%!
1435 move_it_by_lines moves 2 lines, so we need to
1436 fix that up. */
1437 if (it3.line_wrap == WORD_WRAP)
1438 move_it_by_lines (&it3, -1);
1441 /* Record the vertical coordinate of the display
1442 line where we wound up. */
1443 top_y = it3.current_y;
1444 if (it3.bidi_p)
1446 /* When characters are reordered for display,
1447 the character displayed to the left of the
1448 display string could be _after_ the display
1449 property in the logical order. Use the
1450 smallest vertical position of these two. */
1451 start_display (&it3, w, top);
1452 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1453 if (it3.current_y < top_y)
1454 top_y = it3.current_y;
1456 /* Move from the top of the window to the beginning
1457 of the display line where the display string
1458 begins. */
1459 start_display (&it3, w, top);
1460 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1461 /* If it3_moved stays zero after the 'while' loop
1462 below, that means we already were at a newline
1463 before the loop (e.g., the display string begins
1464 with a newline), so we don't need to (and cannot)
1465 inspect the glyphs of it3.glyph_row, because
1466 PRODUCE_GLYPHS will not produce anything for a
1467 newline, and thus it3.glyph_row stays at its
1468 stale content it got at top of the window. */
1469 it3_moved = 0;
1470 /* Finally, advance the iterator until we hit the
1471 first display element whose character position is
1472 CHARPOS, or until the first newline from the
1473 display string, which signals the end of the
1474 display line. */
1475 while (get_next_display_element (&it3))
1477 PRODUCE_GLYPHS (&it3);
1478 if (IT_CHARPOS (it3) == charpos
1479 || ITERATOR_AT_END_OF_LINE_P (&it3))
1480 break;
1481 it3_moved = 1;
1482 set_iterator_to_next (&it3, 0);
1484 top_x = it3.current_x - it3.pixel_width;
1485 /* Normally, we would exit the above loop because we
1486 found the display element whose character
1487 position is CHARPOS. For the contingency that we
1488 didn't, and stopped at the first newline from the
1489 display string, move back over the glyphs
1490 produced from the string, until we find the
1491 rightmost glyph not from the string. */
1492 if (it3_moved
1493 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1495 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1496 + it3.glyph_row->used[TEXT_AREA];
1498 while (EQ ((g - 1)->object, string))
1500 --g;
1501 top_x -= g->pixel_width;
1503 xassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1504 + it3.glyph_row->used[TEXT_AREA]);
1509 *x = top_x;
1510 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1511 *rtop = max (0, window_top_y - top_y);
1512 *rbot = max (0, bottom_y - it.last_visible_y);
1513 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1514 - max (top_y, window_top_y)));
1515 *vpos = it.vpos;
1518 else
1520 /* We were asked to provide info about WINDOW_END. */
1521 struct it it2;
1522 void *it2data = NULL;
1524 SAVE_IT (it2, it, it2data);
1525 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1526 move_it_by_lines (&it, 1);
1527 if (charpos < IT_CHARPOS (it)
1528 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1530 visible_p = 1;
1531 RESTORE_IT (&it2, &it2, it2data);
1532 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1533 *x = it2.current_x;
1534 *y = it2.current_y + it2.max_ascent - it2.ascent;
1535 *rtop = max (0, -it2.current_y);
1536 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1537 - it.last_visible_y));
1538 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1539 it.last_visible_y)
1540 - max (it2.current_y,
1541 WINDOW_HEADER_LINE_HEIGHT (w))));
1542 *vpos = it2.vpos;
1544 else
1545 bidi_unshelve_cache (it2data, 1);
1547 bidi_unshelve_cache (itdata, 0);
1549 if (old_buffer)
1550 set_buffer_internal_1 (old_buffer);
1552 current_header_line_height = current_mode_line_height = -1;
1554 if (visible_p && XFASTINT (w->hscroll) > 0)
1555 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1557 #if 0
1558 /* Debugging code. */
1559 if (visible_p)
1560 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1561 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1562 else
1563 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1564 #endif
1566 return visible_p;
1570 /* Return the next character from STR. Return in *LEN the length of
1571 the character. This is like STRING_CHAR_AND_LENGTH but never
1572 returns an invalid character. If we find one, we return a `?', but
1573 with the length of the invalid character. */
1575 static inline int
1576 string_char_and_length (const unsigned char *str, int *len)
1578 int c;
1580 c = STRING_CHAR_AND_LENGTH (str, *len);
1581 if (!CHAR_VALID_P (c))
1582 /* We may not change the length here because other places in Emacs
1583 don't use this function, i.e. they silently accept invalid
1584 characters. */
1585 c = '?';
1587 return c;
1592 /* Given a position POS containing a valid character and byte position
1593 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1595 static struct text_pos
1596 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1598 xassert (STRINGP (string) && nchars >= 0);
1600 if (STRING_MULTIBYTE (string))
1602 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1603 int len;
1605 while (nchars--)
1607 string_char_and_length (p, &len);
1608 p += len;
1609 CHARPOS (pos) += 1;
1610 BYTEPOS (pos) += len;
1613 else
1614 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1616 return pos;
1620 /* Value is the text position, i.e. character and byte position,
1621 for character position CHARPOS in STRING. */
1623 static inline struct text_pos
1624 string_pos (EMACS_INT charpos, Lisp_Object string)
1626 struct text_pos pos;
1627 xassert (STRINGP (string));
1628 xassert (charpos >= 0);
1629 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1630 return pos;
1634 /* Value is a text position, i.e. character and byte position, for
1635 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1636 means recognize multibyte characters. */
1638 static struct text_pos
1639 c_string_pos (EMACS_INT charpos, const char *s, int multibyte_p)
1641 struct text_pos pos;
1643 xassert (s != NULL);
1644 xassert (charpos >= 0);
1646 if (multibyte_p)
1648 int len;
1650 SET_TEXT_POS (pos, 0, 0);
1651 while (charpos--)
1653 string_char_and_length ((const unsigned char *) s, &len);
1654 s += len;
1655 CHARPOS (pos) += 1;
1656 BYTEPOS (pos) += len;
1659 else
1660 SET_TEXT_POS (pos, charpos, charpos);
1662 return pos;
1666 /* Value is the number of characters in C string S. MULTIBYTE_P
1667 non-zero means recognize multibyte characters. */
1669 static EMACS_INT
1670 number_of_chars (const char *s, int multibyte_p)
1672 EMACS_INT nchars;
1674 if (multibyte_p)
1676 EMACS_INT rest = strlen (s);
1677 int len;
1678 const unsigned char *p = (const unsigned char *) s;
1680 for (nchars = 0; rest > 0; ++nchars)
1682 string_char_and_length (p, &len);
1683 rest -= len, p += len;
1686 else
1687 nchars = strlen (s);
1689 return nchars;
1693 /* Compute byte position NEWPOS->bytepos corresponding to
1694 NEWPOS->charpos. POS is a known position in string STRING.
1695 NEWPOS->charpos must be >= POS.charpos. */
1697 static void
1698 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1700 xassert (STRINGP (string));
1701 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1703 if (STRING_MULTIBYTE (string))
1704 *newpos = string_pos_nchars_ahead (pos, string,
1705 CHARPOS (*newpos) - CHARPOS (pos));
1706 else
1707 BYTEPOS (*newpos) = CHARPOS (*newpos);
1710 /* EXPORT:
1711 Return an estimation of the pixel height of mode or header lines on
1712 frame F. FACE_ID specifies what line's height to estimate. */
1715 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1717 #ifdef HAVE_WINDOW_SYSTEM
1718 if (FRAME_WINDOW_P (f))
1720 int height = FONT_HEIGHT (FRAME_FONT (f));
1722 /* This function is called so early when Emacs starts that the face
1723 cache and mode line face are not yet initialized. */
1724 if (FRAME_FACE_CACHE (f))
1726 struct face *face = FACE_FROM_ID (f, face_id);
1727 if (face)
1729 if (face->font)
1730 height = FONT_HEIGHT (face->font);
1731 if (face->box_line_width > 0)
1732 height += 2 * face->box_line_width;
1736 return height;
1738 #endif
1740 return 1;
1743 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1744 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1745 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1746 not force the value into range. */
1748 void
1749 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1750 int *x, int *y, NativeRectangle *bounds, int noclip)
1753 #ifdef HAVE_WINDOW_SYSTEM
1754 if (FRAME_WINDOW_P (f))
1756 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1757 even for negative values. */
1758 if (pix_x < 0)
1759 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1760 if (pix_y < 0)
1761 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1763 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1764 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1766 if (bounds)
1767 STORE_NATIVE_RECT (*bounds,
1768 FRAME_COL_TO_PIXEL_X (f, pix_x),
1769 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1770 FRAME_COLUMN_WIDTH (f) - 1,
1771 FRAME_LINE_HEIGHT (f) - 1);
1773 if (!noclip)
1775 if (pix_x < 0)
1776 pix_x = 0;
1777 else if (pix_x > FRAME_TOTAL_COLS (f))
1778 pix_x = FRAME_TOTAL_COLS (f);
1780 if (pix_y < 0)
1781 pix_y = 0;
1782 else if (pix_y > FRAME_LINES (f))
1783 pix_y = FRAME_LINES (f);
1786 #endif
1788 *x = pix_x;
1789 *y = pix_y;
1793 /* Find the glyph under window-relative coordinates X/Y in window W.
1794 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1795 strings. Return in *HPOS and *VPOS the row and column number of
1796 the glyph found. Return in *AREA the glyph area containing X.
1797 Value is a pointer to the glyph found or null if X/Y is not on
1798 text, or we can't tell because W's current matrix is not up to
1799 date. */
1801 static
1802 struct glyph *
1803 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1804 int *dx, int *dy, int *area)
1806 struct glyph *glyph, *end;
1807 struct glyph_row *row = NULL;
1808 int x0, i;
1810 /* Find row containing Y. Give up if some row is not enabled. */
1811 for (i = 0; i < w->current_matrix->nrows; ++i)
1813 row = MATRIX_ROW (w->current_matrix, i);
1814 if (!row->enabled_p)
1815 return NULL;
1816 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1817 break;
1820 *vpos = i;
1821 *hpos = 0;
1823 /* Give up if Y is not in the window. */
1824 if (i == w->current_matrix->nrows)
1825 return NULL;
1827 /* Get the glyph area containing X. */
1828 if (w->pseudo_window_p)
1830 *area = TEXT_AREA;
1831 x0 = 0;
1833 else
1835 if (x < window_box_left_offset (w, TEXT_AREA))
1837 *area = LEFT_MARGIN_AREA;
1838 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1840 else if (x < window_box_right_offset (w, TEXT_AREA))
1842 *area = TEXT_AREA;
1843 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1845 else
1847 *area = RIGHT_MARGIN_AREA;
1848 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1852 /* Find glyph containing X. */
1853 glyph = row->glyphs[*area];
1854 end = glyph + row->used[*area];
1855 x -= x0;
1856 while (glyph < end && x >= glyph->pixel_width)
1858 x -= glyph->pixel_width;
1859 ++glyph;
1862 if (glyph == end)
1863 return NULL;
1865 if (dx)
1867 *dx = x;
1868 *dy = y - (row->y + row->ascent - glyph->ascent);
1871 *hpos = glyph - row->glyphs[*area];
1872 return glyph;
1875 /* Convert frame-relative x/y to coordinates relative to window W.
1876 Takes pseudo-windows into account. */
1878 static void
1879 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1881 if (w->pseudo_window_p)
1883 /* A pseudo-window is always full-width, and starts at the
1884 left edge of the frame, plus a frame border. */
1885 struct frame *f = XFRAME (w->frame);
1886 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1887 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1889 else
1891 *x -= WINDOW_LEFT_EDGE_X (w);
1892 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1896 #ifdef HAVE_WINDOW_SYSTEM
1898 /* EXPORT:
1899 Return in RECTS[] at most N clipping rectangles for glyph string S.
1900 Return the number of stored rectangles. */
1903 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1905 XRectangle r;
1907 if (n <= 0)
1908 return 0;
1910 if (s->row->full_width_p)
1912 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1913 r.x = WINDOW_LEFT_EDGE_X (s->w);
1914 r.width = WINDOW_TOTAL_WIDTH (s->w);
1916 /* Unless displaying a mode or menu bar line, which are always
1917 fully visible, clip to the visible part of the row. */
1918 if (s->w->pseudo_window_p)
1919 r.height = s->row->visible_height;
1920 else
1921 r.height = s->height;
1923 else
1925 /* This is a text line that may be partially visible. */
1926 r.x = window_box_left (s->w, s->area);
1927 r.width = window_box_width (s->w, s->area);
1928 r.height = s->row->visible_height;
1931 if (s->clip_head)
1932 if (r.x < s->clip_head->x)
1934 if (r.width >= s->clip_head->x - r.x)
1935 r.width -= s->clip_head->x - r.x;
1936 else
1937 r.width = 0;
1938 r.x = s->clip_head->x;
1940 if (s->clip_tail)
1941 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1943 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1944 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1945 else
1946 r.width = 0;
1949 /* If S draws overlapping rows, it's sufficient to use the top and
1950 bottom of the window for clipping because this glyph string
1951 intentionally draws over other lines. */
1952 if (s->for_overlaps)
1954 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1955 r.height = window_text_bottom_y (s->w) - r.y;
1957 /* Alas, the above simple strategy does not work for the
1958 environments with anti-aliased text: if the same text is
1959 drawn onto the same place multiple times, it gets thicker.
1960 If the overlap we are processing is for the erased cursor, we
1961 take the intersection with the rectangle of the cursor. */
1962 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1964 XRectangle rc, r_save = r;
1966 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1967 rc.y = s->w->phys_cursor.y;
1968 rc.width = s->w->phys_cursor_width;
1969 rc.height = s->w->phys_cursor_height;
1971 x_intersect_rectangles (&r_save, &rc, &r);
1974 else
1976 /* Don't use S->y for clipping because it doesn't take partially
1977 visible lines into account. For example, it can be negative for
1978 partially visible lines at the top of a window. */
1979 if (!s->row->full_width_p
1980 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1981 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1982 else
1983 r.y = max (0, s->row->y);
1986 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1988 /* If drawing the cursor, don't let glyph draw outside its
1989 advertised boundaries. Cleartype does this under some circumstances. */
1990 if (s->hl == DRAW_CURSOR)
1992 struct glyph *glyph = s->first_glyph;
1993 int height, max_y;
1995 if (s->x > r.x)
1997 r.width -= s->x - r.x;
1998 r.x = s->x;
2000 r.width = min (r.width, glyph->pixel_width);
2002 /* If r.y is below window bottom, ensure that we still see a cursor. */
2003 height = min (glyph->ascent + glyph->descent,
2004 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2005 max_y = window_text_bottom_y (s->w) - height;
2006 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2007 if (s->ybase - glyph->ascent > max_y)
2009 r.y = max_y;
2010 r.height = height;
2012 else
2014 /* Don't draw cursor glyph taller than our actual glyph. */
2015 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2016 if (height < r.height)
2018 max_y = r.y + r.height;
2019 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2020 r.height = min (max_y - r.y, height);
2025 if (s->row->clip)
2027 XRectangle r_save = r;
2029 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2030 r.width = 0;
2033 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2034 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2036 #ifdef CONVERT_FROM_XRECT
2037 CONVERT_FROM_XRECT (r, *rects);
2038 #else
2039 *rects = r;
2040 #endif
2041 return 1;
2043 else
2045 /* If we are processing overlapping and allowed to return
2046 multiple clipping rectangles, we exclude the row of the glyph
2047 string from the clipping rectangle. This is to avoid drawing
2048 the same text on the environment with anti-aliasing. */
2049 #ifdef CONVERT_FROM_XRECT
2050 XRectangle rs[2];
2051 #else
2052 XRectangle *rs = rects;
2053 #endif
2054 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2056 if (s->for_overlaps & OVERLAPS_PRED)
2058 rs[i] = r;
2059 if (r.y + r.height > row_y)
2061 if (r.y < row_y)
2062 rs[i].height = row_y - r.y;
2063 else
2064 rs[i].height = 0;
2066 i++;
2068 if (s->for_overlaps & OVERLAPS_SUCC)
2070 rs[i] = r;
2071 if (r.y < row_y + s->row->visible_height)
2073 if (r.y + r.height > row_y + s->row->visible_height)
2075 rs[i].y = row_y + s->row->visible_height;
2076 rs[i].height = r.y + r.height - rs[i].y;
2078 else
2079 rs[i].height = 0;
2081 i++;
2084 n = i;
2085 #ifdef CONVERT_FROM_XRECT
2086 for (i = 0; i < n; i++)
2087 CONVERT_FROM_XRECT (rs[i], rects[i]);
2088 #endif
2089 return n;
2093 /* EXPORT:
2094 Return in *NR the clipping rectangle for glyph string S. */
2096 void
2097 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2099 get_glyph_string_clip_rects (s, nr, 1);
2103 /* EXPORT:
2104 Return the position and height of the phys cursor in window W.
2105 Set w->phys_cursor_width to width of phys cursor.
2108 void
2109 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2110 struct glyph *glyph, int *xp, int *yp, int *heightp)
2112 struct frame *f = XFRAME (WINDOW_FRAME (w));
2113 int x, y, wd, h, h0, y0;
2115 /* Compute the width of the rectangle to draw. If on a stretch
2116 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2117 rectangle as wide as the glyph, but use a canonical character
2118 width instead. */
2119 wd = glyph->pixel_width - 1;
2120 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2121 wd++; /* Why? */
2122 #endif
2124 x = w->phys_cursor.x;
2125 if (x < 0)
2127 wd += x;
2128 x = 0;
2131 if (glyph->type == STRETCH_GLYPH
2132 && !x_stretch_cursor_p)
2133 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2134 w->phys_cursor_width = wd;
2136 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2138 /* If y is below window bottom, ensure that we still see a cursor. */
2139 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2141 h = max (h0, glyph->ascent + glyph->descent);
2142 h0 = min (h0, glyph->ascent + glyph->descent);
2144 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2145 if (y < y0)
2147 h = max (h - (y0 - y) + 1, h0);
2148 y = y0 - 1;
2150 else
2152 y0 = window_text_bottom_y (w) - h0;
2153 if (y > y0)
2155 h += y - y0;
2156 y = y0;
2160 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2161 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2162 *heightp = h;
2166 * Remember which glyph the mouse is over.
2169 void
2170 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2172 Lisp_Object window;
2173 struct window *w;
2174 struct glyph_row *r, *gr, *end_row;
2175 enum window_part part;
2176 enum glyph_row_area area;
2177 int x, y, width, height;
2179 /* Try to determine frame pixel position and size of the glyph under
2180 frame pixel coordinates X/Y on frame F. */
2182 if (!f->glyphs_initialized_p
2183 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2184 NILP (window)))
2186 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2187 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2188 goto virtual_glyph;
2191 w = XWINDOW (window);
2192 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2193 height = WINDOW_FRAME_LINE_HEIGHT (w);
2195 x = window_relative_x_coord (w, part, gx);
2196 y = gy - WINDOW_TOP_EDGE_Y (w);
2198 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2199 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2201 if (w->pseudo_window_p)
2203 area = TEXT_AREA;
2204 part = ON_MODE_LINE; /* Don't adjust margin. */
2205 goto text_glyph;
2208 switch (part)
2210 case ON_LEFT_MARGIN:
2211 area = LEFT_MARGIN_AREA;
2212 goto text_glyph;
2214 case ON_RIGHT_MARGIN:
2215 area = RIGHT_MARGIN_AREA;
2216 goto text_glyph;
2218 case ON_HEADER_LINE:
2219 case ON_MODE_LINE:
2220 gr = (part == ON_HEADER_LINE
2221 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2222 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2223 gy = gr->y;
2224 area = TEXT_AREA;
2225 goto text_glyph_row_found;
2227 case ON_TEXT:
2228 area = TEXT_AREA;
2230 text_glyph:
2231 gr = 0; gy = 0;
2232 for (; r <= end_row && r->enabled_p; ++r)
2233 if (r->y + r->height > y)
2235 gr = r; gy = r->y;
2236 break;
2239 text_glyph_row_found:
2240 if (gr && gy <= y)
2242 struct glyph *g = gr->glyphs[area];
2243 struct glyph *end = g + gr->used[area];
2245 height = gr->height;
2246 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2247 if (gx + g->pixel_width > x)
2248 break;
2250 if (g < end)
2252 if (g->type == IMAGE_GLYPH)
2254 /* Don't remember when mouse is over image, as
2255 image may have hot-spots. */
2256 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2257 return;
2259 width = g->pixel_width;
2261 else
2263 /* Use nominal char spacing at end of line. */
2264 x -= gx;
2265 gx += (x / width) * width;
2268 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2269 gx += window_box_left_offset (w, area);
2271 else
2273 /* Use nominal line height at end of window. */
2274 gx = (x / width) * width;
2275 y -= gy;
2276 gy += (y / height) * height;
2278 break;
2280 case ON_LEFT_FRINGE:
2281 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2282 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2283 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2284 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2285 goto row_glyph;
2287 case ON_RIGHT_FRINGE:
2288 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2289 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2290 : window_box_right_offset (w, TEXT_AREA));
2291 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2292 goto row_glyph;
2294 case ON_SCROLL_BAR:
2295 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2297 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2298 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2299 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2300 : 0)));
2301 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2303 row_glyph:
2304 gr = 0, gy = 0;
2305 for (; r <= end_row && r->enabled_p; ++r)
2306 if (r->y + r->height > y)
2308 gr = r; gy = r->y;
2309 break;
2312 if (gr && gy <= y)
2313 height = gr->height;
2314 else
2316 /* Use nominal line height at end of window. */
2317 y -= gy;
2318 gy += (y / height) * height;
2320 break;
2322 default:
2324 virtual_glyph:
2325 /* If there is no glyph under the mouse, then we divide the screen
2326 into a grid of the smallest glyph in the frame, and use that
2327 as our "glyph". */
2329 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2330 round down even for negative values. */
2331 if (gx < 0)
2332 gx -= width - 1;
2333 if (gy < 0)
2334 gy -= height - 1;
2336 gx = (gx / width) * width;
2337 gy = (gy / height) * height;
2339 goto store_rect;
2342 gx += WINDOW_LEFT_EDGE_X (w);
2343 gy += WINDOW_TOP_EDGE_Y (w);
2345 store_rect:
2346 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2348 /* Visible feedback for debugging. */
2349 #if 0
2350 #if HAVE_X_WINDOWS
2351 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2352 f->output_data.x->normal_gc,
2353 gx, gy, width, height);
2354 #endif
2355 #endif
2359 #endif /* HAVE_WINDOW_SYSTEM */
2362 /***********************************************************************
2363 Lisp form evaluation
2364 ***********************************************************************/
2366 /* Error handler for safe_eval and safe_call. */
2368 static Lisp_Object
2369 safe_eval_handler (Lisp_Object arg)
2371 add_to_log ("Error during redisplay: %S", arg, Qnil);
2372 return Qnil;
2376 /* Evaluate SEXPR and return the result, or nil if something went
2377 wrong. Prevent redisplay during the evaluation. */
2379 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2380 Return the result, or nil if something went wrong. Prevent
2381 redisplay during the evaluation. */
2383 Lisp_Object
2384 safe_call (ptrdiff_t nargs, Lisp_Object *args)
2386 Lisp_Object val;
2388 if (inhibit_eval_during_redisplay)
2389 val = Qnil;
2390 else
2392 int count = SPECPDL_INDEX ();
2393 struct gcpro gcpro1;
2395 GCPRO1 (args[0]);
2396 gcpro1.nvars = nargs;
2397 specbind (Qinhibit_redisplay, Qt);
2398 /* Use Qt to ensure debugger does not run,
2399 so there is no possibility of wanting to redisplay. */
2400 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2401 safe_eval_handler);
2402 UNGCPRO;
2403 val = unbind_to (count, val);
2406 return val;
2410 /* Call function FN with one argument ARG.
2411 Return the result, or nil if something went wrong. */
2413 Lisp_Object
2414 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2416 Lisp_Object args[2];
2417 args[0] = fn;
2418 args[1] = arg;
2419 return safe_call (2, args);
2422 static Lisp_Object Qeval;
2424 Lisp_Object
2425 safe_eval (Lisp_Object sexpr)
2427 return safe_call1 (Qeval, sexpr);
2430 /* Call function FN with one argument ARG.
2431 Return the result, or nil if something went wrong. */
2433 Lisp_Object
2434 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2436 Lisp_Object args[3];
2437 args[0] = fn;
2438 args[1] = arg1;
2439 args[2] = arg2;
2440 return safe_call (3, args);
2445 /***********************************************************************
2446 Debugging
2447 ***********************************************************************/
2449 #if 0
2451 /* Define CHECK_IT to perform sanity checks on iterators.
2452 This is for debugging. It is too slow to do unconditionally. */
2454 static void
2455 check_it (struct it *it)
2457 if (it->method == GET_FROM_STRING)
2459 xassert (STRINGP (it->string));
2460 xassert (IT_STRING_CHARPOS (*it) >= 0);
2462 else
2464 xassert (IT_STRING_CHARPOS (*it) < 0);
2465 if (it->method == GET_FROM_BUFFER)
2467 /* Check that character and byte positions agree. */
2468 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2472 if (it->dpvec)
2473 xassert (it->current.dpvec_index >= 0);
2474 else
2475 xassert (it->current.dpvec_index < 0);
2478 #define CHECK_IT(IT) check_it ((IT))
2480 #else /* not 0 */
2482 #define CHECK_IT(IT) (void) 0
2484 #endif /* not 0 */
2487 #if GLYPH_DEBUG && XASSERTS
2489 /* Check that the window end of window W is what we expect it
2490 to be---the last row in the current matrix displaying text. */
2492 static void
2493 check_window_end (struct window *w)
2495 if (!MINI_WINDOW_P (w)
2496 && !NILP (w->window_end_valid))
2498 struct glyph_row *row;
2499 xassert ((row = MATRIX_ROW (w->current_matrix,
2500 XFASTINT (w->window_end_vpos)),
2501 !row->enabled_p
2502 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2503 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2507 #define CHECK_WINDOW_END(W) check_window_end ((W))
2509 #else
2511 #define CHECK_WINDOW_END(W) (void) 0
2513 #endif
2517 /***********************************************************************
2518 Iterator initialization
2519 ***********************************************************************/
2521 /* Initialize IT for displaying current_buffer in window W, starting
2522 at character position CHARPOS. CHARPOS < 0 means that no buffer
2523 position is specified which is useful when the iterator is assigned
2524 a position later. BYTEPOS is the byte position corresponding to
2525 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2527 If ROW is not null, calls to produce_glyphs with IT as parameter
2528 will produce glyphs in that row.
2530 BASE_FACE_ID is the id of a base face to use. It must be one of
2531 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2532 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2533 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2535 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2536 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2537 will be initialized to use the corresponding mode line glyph row of
2538 the desired matrix of W. */
2540 void
2541 init_iterator (struct it *it, struct window *w,
2542 EMACS_INT charpos, EMACS_INT bytepos,
2543 struct glyph_row *row, enum face_id base_face_id)
2545 int highlight_region_p;
2546 enum face_id remapped_base_face_id = base_face_id;
2548 /* Some precondition checks. */
2549 xassert (w != NULL && it != NULL);
2550 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2551 && charpos <= ZV));
2553 /* If face attributes have been changed since the last redisplay,
2554 free realized faces now because they depend on face definitions
2555 that might have changed. Don't free faces while there might be
2556 desired matrices pending which reference these faces. */
2557 if (face_change_count && !inhibit_free_realized_faces)
2559 face_change_count = 0;
2560 free_all_realized_faces (Qnil);
2563 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2564 if (! NILP (Vface_remapping_alist))
2565 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2567 /* Use one of the mode line rows of W's desired matrix if
2568 appropriate. */
2569 if (row == NULL)
2571 if (base_face_id == MODE_LINE_FACE_ID
2572 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2573 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2574 else if (base_face_id == HEADER_LINE_FACE_ID)
2575 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2578 /* Clear IT. */
2579 memset (it, 0, sizeof *it);
2580 it->current.overlay_string_index = -1;
2581 it->current.dpvec_index = -1;
2582 it->base_face_id = remapped_base_face_id;
2583 it->string = Qnil;
2584 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2585 it->paragraph_embedding = L2R;
2586 it->bidi_it.string.lstring = Qnil;
2587 it->bidi_it.string.s = NULL;
2588 it->bidi_it.string.bufpos = 0;
2590 /* The window in which we iterate over current_buffer: */
2591 XSETWINDOW (it->window, w);
2592 it->w = w;
2593 it->f = XFRAME (w->frame);
2595 it->cmp_it.id = -1;
2597 /* Extra space between lines (on window systems only). */
2598 if (base_face_id == DEFAULT_FACE_ID
2599 && FRAME_WINDOW_P (it->f))
2601 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2602 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2603 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2604 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2605 * FRAME_LINE_HEIGHT (it->f));
2606 else if (it->f->extra_line_spacing > 0)
2607 it->extra_line_spacing = it->f->extra_line_spacing;
2608 it->max_extra_line_spacing = 0;
2611 /* If realized faces have been removed, e.g. because of face
2612 attribute changes of named faces, recompute them. When running
2613 in batch mode, the face cache of the initial frame is null. If
2614 we happen to get called, make a dummy face cache. */
2615 if (FRAME_FACE_CACHE (it->f) == NULL)
2616 init_frame_faces (it->f);
2617 if (FRAME_FACE_CACHE (it->f)->used == 0)
2618 recompute_basic_faces (it->f);
2620 /* Current value of the `slice', `space-width', and 'height' properties. */
2621 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2622 it->space_width = Qnil;
2623 it->font_height = Qnil;
2624 it->override_ascent = -1;
2626 /* Are control characters displayed as `^C'? */
2627 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2629 /* -1 means everything between a CR and the following line end
2630 is invisible. >0 means lines indented more than this value are
2631 invisible. */
2632 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2633 ? XINT (BVAR (current_buffer, selective_display))
2634 : (!NILP (BVAR (current_buffer, selective_display))
2635 ? -1 : 0));
2636 it->selective_display_ellipsis_p
2637 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2639 /* Display table to use. */
2640 it->dp = window_display_table (w);
2642 /* Are multibyte characters enabled in current_buffer? */
2643 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2645 /* Non-zero if we should highlight the region. */
2646 highlight_region_p
2647 = (!NILP (Vtransient_mark_mode)
2648 && !NILP (BVAR (current_buffer, mark_active))
2649 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2651 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2652 start and end of a visible region in window IT->w. Set both to
2653 -1 to indicate no region. */
2654 if (highlight_region_p
2655 /* Maybe highlight only in selected window. */
2656 && (/* Either show region everywhere. */
2657 highlight_nonselected_windows
2658 /* Or show region in the selected window. */
2659 || w == XWINDOW (selected_window)
2660 /* Or show the region if we are in the mini-buffer and W is
2661 the window the mini-buffer refers to. */
2662 || (MINI_WINDOW_P (XWINDOW (selected_window))
2663 && WINDOWP (minibuf_selected_window)
2664 && w == XWINDOW (minibuf_selected_window))))
2666 EMACS_INT markpos = marker_position (BVAR (current_buffer, mark));
2667 it->region_beg_charpos = min (PT, markpos);
2668 it->region_end_charpos = max (PT, markpos);
2670 else
2671 it->region_beg_charpos = it->region_end_charpos = -1;
2673 /* Get the position at which the redisplay_end_trigger hook should
2674 be run, if it is to be run at all. */
2675 if (MARKERP (w->redisplay_end_trigger)
2676 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2677 it->redisplay_end_trigger_charpos
2678 = marker_position (w->redisplay_end_trigger);
2679 else if (INTEGERP (w->redisplay_end_trigger))
2680 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2682 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2684 /* Are lines in the display truncated? */
2685 if (base_face_id != DEFAULT_FACE_ID
2686 || XINT (it->w->hscroll)
2687 || (! WINDOW_FULL_WIDTH_P (it->w)
2688 && ((!NILP (Vtruncate_partial_width_windows)
2689 && !INTEGERP (Vtruncate_partial_width_windows))
2690 || (INTEGERP (Vtruncate_partial_width_windows)
2691 && (WINDOW_TOTAL_COLS (it->w)
2692 < XINT (Vtruncate_partial_width_windows))))))
2693 it->line_wrap = TRUNCATE;
2694 else if (NILP (BVAR (current_buffer, truncate_lines)))
2695 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2696 ? WINDOW_WRAP : WORD_WRAP;
2697 else
2698 it->line_wrap = TRUNCATE;
2700 /* Get dimensions of truncation and continuation glyphs. These are
2701 displayed as fringe bitmaps under X, so we don't need them for such
2702 frames. */
2703 if (!FRAME_WINDOW_P (it->f))
2705 if (it->line_wrap == TRUNCATE)
2707 /* We will need the truncation glyph. */
2708 xassert (it->glyph_row == NULL);
2709 produce_special_glyphs (it, IT_TRUNCATION);
2710 it->truncation_pixel_width = it->pixel_width;
2712 else
2714 /* We will need the continuation glyph. */
2715 xassert (it->glyph_row == NULL);
2716 produce_special_glyphs (it, IT_CONTINUATION);
2717 it->continuation_pixel_width = it->pixel_width;
2720 /* Reset these values to zero because the produce_special_glyphs
2721 above has changed them. */
2722 it->pixel_width = it->ascent = it->descent = 0;
2723 it->phys_ascent = it->phys_descent = 0;
2726 /* Set this after getting the dimensions of truncation and
2727 continuation glyphs, so that we don't produce glyphs when calling
2728 produce_special_glyphs, above. */
2729 it->glyph_row = row;
2730 it->area = TEXT_AREA;
2732 /* Forget any previous info about this row being reversed. */
2733 if (it->glyph_row)
2734 it->glyph_row->reversed_p = 0;
2736 /* Get the dimensions of the display area. The display area
2737 consists of the visible window area plus a horizontally scrolled
2738 part to the left of the window. All x-values are relative to the
2739 start of this total display area. */
2740 if (base_face_id != DEFAULT_FACE_ID)
2742 /* Mode lines, menu bar in terminal frames. */
2743 it->first_visible_x = 0;
2744 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2746 else
2748 it->first_visible_x
2749 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2750 it->last_visible_x = (it->first_visible_x
2751 + window_box_width (w, TEXT_AREA));
2753 /* If we truncate lines, leave room for the truncator glyph(s) at
2754 the right margin. Otherwise, leave room for the continuation
2755 glyph(s). Truncation and continuation glyphs are not inserted
2756 for window-based redisplay. */
2757 if (!FRAME_WINDOW_P (it->f))
2759 if (it->line_wrap == TRUNCATE)
2760 it->last_visible_x -= it->truncation_pixel_width;
2761 else
2762 it->last_visible_x -= it->continuation_pixel_width;
2765 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2766 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2769 /* Leave room for a border glyph. */
2770 if (!FRAME_WINDOW_P (it->f)
2771 && !WINDOW_RIGHTMOST_P (it->w))
2772 it->last_visible_x -= 1;
2774 it->last_visible_y = window_text_bottom_y (w);
2776 /* For mode lines and alike, arrange for the first glyph having a
2777 left box line if the face specifies a box. */
2778 if (base_face_id != DEFAULT_FACE_ID)
2780 struct face *face;
2782 it->face_id = remapped_base_face_id;
2784 /* If we have a boxed mode line, make the first character appear
2785 with a left box line. */
2786 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2787 if (face->box != FACE_NO_BOX)
2788 it->start_of_box_run_p = 1;
2791 /* If a buffer position was specified, set the iterator there,
2792 getting overlays and face properties from that position. */
2793 if (charpos >= BUF_BEG (current_buffer))
2795 it->end_charpos = ZV;
2796 IT_CHARPOS (*it) = charpos;
2798 /* We will rely on `reseat' to set this up properly, via
2799 handle_face_prop. */
2800 it->face_id = it->base_face_id;
2802 /* Compute byte position if not specified. */
2803 if (bytepos < charpos)
2804 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2805 else
2806 IT_BYTEPOS (*it) = bytepos;
2808 it->start = it->current;
2809 /* Do we need to reorder bidirectional text? Not if this is a
2810 unibyte buffer: by definition, none of the single-byte
2811 characters are strong R2L, so no reordering is needed. And
2812 bidi.c doesn't support unibyte buffers anyway. Also, don't
2813 reorder while we are loading loadup.el, since the tables of
2814 character properties needed for reordering are not yet
2815 available. */
2816 it->bidi_p =
2817 NILP (Vpurify_flag)
2818 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2819 && it->multibyte_p;
2821 /* If we are to reorder bidirectional text, init the bidi
2822 iterator. */
2823 if (it->bidi_p)
2825 /* Note the paragraph direction that this buffer wants to
2826 use. */
2827 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2828 Qleft_to_right))
2829 it->paragraph_embedding = L2R;
2830 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2831 Qright_to_left))
2832 it->paragraph_embedding = R2L;
2833 else
2834 it->paragraph_embedding = NEUTRAL_DIR;
2835 bidi_unshelve_cache (NULL, 0);
2836 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2837 &it->bidi_it);
2840 /* Compute faces etc. */
2841 reseat (it, it->current.pos, 1);
2844 CHECK_IT (it);
2848 /* Initialize IT for the display of window W with window start POS. */
2850 void
2851 start_display (struct it *it, struct window *w, struct text_pos pos)
2853 struct glyph_row *row;
2854 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2856 row = w->desired_matrix->rows + first_vpos;
2857 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2858 it->first_vpos = first_vpos;
2860 /* Don't reseat to previous visible line start if current start
2861 position is in a string or image. */
2862 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2864 int start_at_line_beg_p;
2865 int first_y = it->current_y;
2867 /* If window start is not at a line start, skip forward to POS to
2868 get the correct continuation lines width. */
2869 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2870 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2871 if (!start_at_line_beg_p)
2873 int new_x;
2875 reseat_at_previous_visible_line_start (it);
2876 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2878 new_x = it->current_x + it->pixel_width;
2880 /* If lines are continued, this line may end in the middle
2881 of a multi-glyph character (e.g. a control character
2882 displayed as \003, or in the middle of an overlay
2883 string). In this case move_it_to above will not have
2884 taken us to the start of the continuation line but to the
2885 end of the continued line. */
2886 if (it->current_x > 0
2887 && it->line_wrap != TRUNCATE /* Lines are continued. */
2888 && (/* And glyph doesn't fit on the line. */
2889 new_x > it->last_visible_x
2890 /* Or it fits exactly and we're on a window
2891 system frame. */
2892 || (new_x == it->last_visible_x
2893 && FRAME_WINDOW_P (it->f))))
2895 if ((it->current.dpvec_index >= 0
2896 || it->current.overlay_string_index >= 0)
2897 /* If we are on a newline from a display vector or
2898 overlay string, then we are already at the end of
2899 a screen line; no need to go to the next line in
2900 that case, as this line is not really continued.
2901 (If we do go to the next line, C-e will not DTRT.) */
2902 && it->c != '\n')
2904 set_iterator_to_next (it, 1);
2905 move_it_in_display_line_to (it, -1, -1, 0);
2908 it->continuation_lines_width += it->current_x;
2910 /* If the character at POS is displayed via a display
2911 vector, move_it_to above stops at the final glyph of
2912 IT->dpvec. To make the caller redisplay that character
2913 again (a.k.a. start at POS), we need to reset the
2914 dpvec_index to the beginning of IT->dpvec. */
2915 else if (it->current.dpvec_index >= 0)
2916 it->current.dpvec_index = 0;
2918 /* We're starting a new display line, not affected by the
2919 height of the continued line, so clear the appropriate
2920 fields in the iterator structure. */
2921 it->max_ascent = it->max_descent = 0;
2922 it->max_phys_ascent = it->max_phys_descent = 0;
2924 it->current_y = first_y;
2925 it->vpos = 0;
2926 it->current_x = it->hpos = 0;
2932 /* Return 1 if POS is a position in ellipses displayed for invisible
2933 text. W is the window we display, for text property lookup. */
2935 static int
2936 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2938 Lisp_Object prop, window;
2939 int ellipses_p = 0;
2940 EMACS_INT charpos = CHARPOS (pos->pos);
2942 /* If POS specifies a position in a display vector, this might
2943 be for an ellipsis displayed for invisible text. We won't
2944 get the iterator set up for delivering that ellipsis unless
2945 we make sure that it gets aware of the invisible text. */
2946 if (pos->dpvec_index >= 0
2947 && pos->overlay_string_index < 0
2948 && CHARPOS (pos->string_pos) < 0
2949 && charpos > BEGV
2950 && (XSETWINDOW (window, w),
2951 prop = Fget_char_property (make_number (charpos),
2952 Qinvisible, window),
2953 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2955 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2956 window);
2957 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2960 return ellipses_p;
2964 /* Initialize IT for stepping through current_buffer in window W,
2965 starting at position POS that includes overlay string and display
2966 vector/ control character translation position information. Value
2967 is zero if there are overlay strings with newlines at POS. */
2969 static int
2970 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2972 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2973 int i, overlay_strings_with_newlines = 0;
2975 /* If POS specifies a position in a display vector, this might
2976 be for an ellipsis displayed for invisible text. We won't
2977 get the iterator set up for delivering that ellipsis unless
2978 we make sure that it gets aware of the invisible text. */
2979 if (in_ellipses_for_invisible_text_p (pos, w))
2981 --charpos;
2982 bytepos = 0;
2985 /* Keep in mind: the call to reseat in init_iterator skips invisible
2986 text, so we might end up at a position different from POS. This
2987 is only a problem when POS is a row start after a newline and an
2988 overlay starts there with an after-string, and the overlay has an
2989 invisible property. Since we don't skip invisible text in
2990 display_line and elsewhere immediately after consuming the
2991 newline before the row start, such a POS will not be in a string,
2992 but the call to init_iterator below will move us to the
2993 after-string. */
2994 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2996 /* This only scans the current chunk -- it should scan all chunks.
2997 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2998 to 16 in 22.1 to make this a lesser problem. */
2999 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3001 const char *s = SSDATA (it->overlay_strings[i]);
3002 const char *e = s + SBYTES (it->overlay_strings[i]);
3004 while (s < e && *s != '\n')
3005 ++s;
3007 if (s < e)
3009 overlay_strings_with_newlines = 1;
3010 break;
3014 /* If position is within an overlay string, set up IT to the right
3015 overlay string. */
3016 if (pos->overlay_string_index >= 0)
3018 int relative_index;
3020 /* If the first overlay string happens to have a `display'
3021 property for an image, the iterator will be set up for that
3022 image, and we have to undo that setup first before we can
3023 correct the overlay string index. */
3024 if (it->method == GET_FROM_IMAGE)
3025 pop_it (it);
3027 /* We already have the first chunk of overlay strings in
3028 IT->overlay_strings. Load more until the one for
3029 pos->overlay_string_index is in IT->overlay_strings. */
3030 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3032 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3033 it->current.overlay_string_index = 0;
3034 while (n--)
3036 load_overlay_strings (it, 0);
3037 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3041 it->current.overlay_string_index = pos->overlay_string_index;
3042 relative_index = (it->current.overlay_string_index
3043 % OVERLAY_STRING_CHUNK_SIZE);
3044 it->string = it->overlay_strings[relative_index];
3045 xassert (STRINGP (it->string));
3046 it->current.string_pos = pos->string_pos;
3047 it->method = GET_FROM_STRING;
3050 if (CHARPOS (pos->string_pos) >= 0)
3052 /* Recorded position is not in an overlay string, but in another
3053 string. This can only be a string from a `display' property.
3054 IT should already be filled with that string. */
3055 it->current.string_pos = pos->string_pos;
3056 xassert (STRINGP (it->string));
3059 /* Restore position in display vector translations, control
3060 character translations or ellipses. */
3061 if (pos->dpvec_index >= 0)
3063 if (it->dpvec == NULL)
3064 get_next_display_element (it);
3065 xassert (it->dpvec && it->current.dpvec_index == 0);
3066 it->current.dpvec_index = pos->dpvec_index;
3069 CHECK_IT (it);
3070 return !overlay_strings_with_newlines;
3074 /* Initialize IT for stepping through current_buffer in window W
3075 starting at ROW->start. */
3077 static void
3078 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3080 init_from_display_pos (it, w, &row->start);
3081 it->start = row->start;
3082 it->continuation_lines_width = row->continuation_lines_width;
3083 CHECK_IT (it);
3087 /* Initialize IT for stepping through current_buffer in window W
3088 starting in the line following ROW, i.e. starting at ROW->end.
3089 Value is zero if there are overlay strings with newlines at ROW's
3090 end position. */
3092 static int
3093 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3095 int success = 0;
3097 if (init_from_display_pos (it, w, &row->end))
3099 if (row->continued_p)
3100 it->continuation_lines_width
3101 = row->continuation_lines_width + row->pixel_width;
3102 CHECK_IT (it);
3103 success = 1;
3106 return success;
3112 /***********************************************************************
3113 Text properties
3114 ***********************************************************************/
3116 /* Called when IT reaches IT->stop_charpos. Handle text property and
3117 overlay changes. Set IT->stop_charpos to the next position where
3118 to stop. */
3120 static void
3121 handle_stop (struct it *it)
3123 enum prop_handled handled;
3124 int handle_overlay_change_p;
3125 struct props *p;
3127 it->dpvec = NULL;
3128 it->current.dpvec_index = -1;
3129 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3130 it->ignore_overlay_strings_at_pos_p = 0;
3131 it->ellipsis_p = 0;
3133 /* Use face of preceding text for ellipsis (if invisible) */
3134 if (it->selective_display_ellipsis_p)
3135 it->saved_face_id = it->face_id;
3139 handled = HANDLED_NORMALLY;
3141 /* Call text property handlers. */
3142 for (p = it_props; p->handler; ++p)
3144 handled = p->handler (it);
3146 if (handled == HANDLED_RECOMPUTE_PROPS)
3147 break;
3148 else if (handled == HANDLED_RETURN)
3150 /* We still want to show before and after strings from
3151 overlays even if the actual buffer text is replaced. */
3152 if (!handle_overlay_change_p
3153 || it->sp > 1
3154 /* Don't call get_overlay_strings_1 if we already
3155 have overlay strings loaded, because doing so
3156 will load them again and push the iterator state
3157 onto the stack one more time, which is not
3158 expected by the rest of the code that processes
3159 overlay strings. */
3160 || (it->n_overlay_strings <= 0
3161 ? !get_overlay_strings_1 (it, 0, 0)
3162 : 0))
3164 if (it->ellipsis_p)
3165 setup_for_ellipsis (it, 0);
3166 /* When handling a display spec, we might load an
3167 empty string. In that case, discard it here. We
3168 used to discard it in handle_single_display_spec,
3169 but that causes get_overlay_strings_1, above, to
3170 ignore overlay strings that we must check. */
3171 if (STRINGP (it->string) && !SCHARS (it->string))
3172 pop_it (it);
3173 return;
3175 else if (STRINGP (it->string) && !SCHARS (it->string))
3176 pop_it (it);
3177 else
3179 it->ignore_overlay_strings_at_pos_p = 1;
3180 it->string_from_display_prop_p = 0;
3181 it->from_disp_prop_p = 0;
3182 handle_overlay_change_p = 0;
3184 handled = HANDLED_RECOMPUTE_PROPS;
3185 break;
3187 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3188 handle_overlay_change_p = 0;
3191 if (handled != HANDLED_RECOMPUTE_PROPS)
3193 /* Don't check for overlay strings below when set to deliver
3194 characters from a display vector. */
3195 if (it->method == GET_FROM_DISPLAY_VECTOR)
3196 handle_overlay_change_p = 0;
3198 /* Handle overlay changes.
3199 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3200 if it finds overlays. */
3201 if (handle_overlay_change_p)
3202 handled = handle_overlay_change (it);
3205 if (it->ellipsis_p)
3207 setup_for_ellipsis (it, 0);
3208 break;
3211 while (handled == HANDLED_RECOMPUTE_PROPS);
3213 /* Determine where to stop next. */
3214 if (handled == HANDLED_NORMALLY)
3215 compute_stop_pos (it);
3219 /* Compute IT->stop_charpos from text property and overlay change
3220 information for IT's current position. */
3222 static void
3223 compute_stop_pos (struct it *it)
3225 register INTERVAL iv, next_iv;
3226 Lisp_Object object, limit, position;
3227 EMACS_INT charpos, bytepos;
3229 if (STRINGP (it->string))
3231 /* Strings are usually short, so don't limit the search for
3232 properties. */
3233 it->stop_charpos = it->end_charpos;
3234 object = it->string;
3235 limit = Qnil;
3236 charpos = IT_STRING_CHARPOS (*it);
3237 bytepos = IT_STRING_BYTEPOS (*it);
3239 else
3241 EMACS_INT pos;
3243 /* If end_charpos is out of range for some reason, such as a
3244 misbehaving display function, rationalize it (Bug#5984). */
3245 if (it->end_charpos > ZV)
3246 it->end_charpos = ZV;
3247 it->stop_charpos = it->end_charpos;
3249 /* If next overlay change is in front of the current stop pos
3250 (which is IT->end_charpos), stop there. Note: value of
3251 next_overlay_change is point-max if no overlay change
3252 follows. */
3253 charpos = IT_CHARPOS (*it);
3254 bytepos = IT_BYTEPOS (*it);
3255 pos = next_overlay_change (charpos);
3256 if (pos < it->stop_charpos)
3257 it->stop_charpos = pos;
3259 /* If showing the region, we have to stop at the region
3260 start or end because the face might change there. */
3261 if (it->region_beg_charpos > 0)
3263 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3264 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3265 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3266 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3269 /* Set up variables for computing the stop position from text
3270 property changes. */
3271 XSETBUFFER (object, current_buffer);
3272 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3275 /* Get the interval containing IT's position. Value is a null
3276 interval if there isn't such an interval. */
3277 position = make_number (charpos);
3278 iv = validate_interval_range (object, &position, &position, 0);
3279 if (!NULL_INTERVAL_P (iv))
3281 Lisp_Object values_here[LAST_PROP_IDX];
3282 struct props *p;
3284 /* Get properties here. */
3285 for (p = it_props; p->handler; ++p)
3286 values_here[p->idx] = textget (iv->plist, *p->name);
3288 /* Look for an interval following iv that has different
3289 properties. */
3290 for (next_iv = next_interval (iv);
3291 (!NULL_INTERVAL_P (next_iv)
3292 && (NILP (limit)
3293 || XFASTINT (limit) > next_iv->position));
3294 next_iv = next_interval (next_iv))
3296 for (p = it_props; p->handler; ++p)
3298 Lisp_Object new_value;
3300 new_value = textget (next_iv->plist, *p->name);
3301 if (!EQ (values_here[p->idx], new_value))
3302 break;
3305 if (p->handler)
3306 break;
3309 if (!NULL_INTERVAL_P (next_iv))
3311 if (INTEGERP (limit)
3312 && next_iv->position >= XFASTINT (limit))
3313 /* No text property change up to limit. */
3314 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3315 else
3316 /* Text properties change in next_iv. */
3317 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3321 if (it->cmp_it.id < 0)
3323 EMACS_INT stoppos = it->end_charpos;
3325 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3326 stoppos = -1;
3327 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3328 stoppos, it->string);
3331 xassert (STRINGP (it->string)
3332 || (it->stop_charpos >= BEGV
3333 && it->stop_charpos >= IT_CHARPOS (*it)));
3337 /* Return the position of the next overlay change after POS in
3338 current_buffer. Value is point-max if no overlay change
3339 follows. This is like `next-overlay-change' but doesn't use
3340 xmalloc. */
3342 static EMACS_INT
3343 next_overlay_change (EMACS_INT pos)
3345 ptrdiff_t i, noverlays;
3346 EMACS_INT endpos;
3347 Lisp_Object *overlays;
3349 /* Get all overlays at the given position. */
3350 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3352 /* If any of these overlays ends before endpos,
3353 use its ending point instead. */
3354 for (i = 0; i < noverlays; ++i)
3356 Lisp_Object oend;
3357 EMACS_INT oendpos;
3359 oend = OVERLAY_END (overlays[i]);
3360 oendpos = OVERLAY_POSITION (oend);
3361 endpos = min (endpos, oendpos);
3364 return endpos;
3367 /* How many characters forward to search for a display property or
3368 display string. Searching too far forward makes the bidi display
3369 sluggish, especially in small windows. */
3370 #define MAX_DISP_SCAN 250
3372 /* Return the character position of a display string at or after
3373 position specified by POSITION. If no display string exists at or
3374 after POSITION, return ZV. A display string is either an overlay
3375 with `display' property whose value is a string, or a `display'
3376 text property whose value is a string. STRING is data about the
3377 string to iterate; if STRING->lstring is nil, we are iterating a
3378 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3379 on a GUI frame. DISP_PROP is set to zero if we searched
3380 MAX_DISP_SCAN characters forward without finding any display
3381 strings, non-zero otherwise. It is set to 2 if the display string
3382 uses any kind of `(space ...)' spec that will produce a stretch of
3383 white space in the text area. */
3384 EMACS_INT
3385 compute_display_string_pos (struct text_pos *position,
3386 struct bidi_string_data *string,
3387 int frame_window_p, int *disp_prop)
3389 /* OBJECT = nil means current buffer. */
3390 Lisp_Object object =
3391 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3392 Lisp_Object pos, spec, limpos;
3393 int string_p = (string && (STRINGP (string->lstring) || string->s));
3394 EMACS_INT eob = string_p ? string->schars : ZV;
3395 EMACS_INT begb = string_p ? 0 : BEGV;
3396 EMACS_INT bufpos, charpos = CHARPOS (*position);
3397 EMACS_INT lim =
3398 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3399 struct text_pos tpos;
3400 int rv = 0;
3402 *disp_prop = 1;
3404 if (charpos >= eob
3405 /* We don't support display properties whose values are strings
3406 that have display string properties. */
3407 || string->from_disp_str
3408 /* C strings cannot have display properties. */
3409 || (string->s && !STRINGP (object)))
3411 *disp_prop = 0;
3412 return eob;
3415 /* If the character at CHARPOS is where the display string begins,
3416 return CHARPOS. */
3417 pos = make_number (charpos);
3418 if (STRINGP (object))
3419 bufpos = string->bufpos;
3420 else
3421 bufpos = charpos;
3422 tpos = *position;
3423 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3424 && (charpos <= begb
3425 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3426 object),
3427 spec))
3428 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3429 frame_window_p)))
3431 if (rv == 2)
3432 *disp_prop = 2;
3433 return charpos;
3436 /* Look forward for the first character with a `display' property
3437 that will replace the underlying text when displayed. */
3438 limpos = make_number (lim);
3439 do {
3440 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3441 CHARPOS (tpos) = XFASTINT (pos);
3442 if (CHARPOS (tpos) >= lim)
3444 *disp_prop = 0;
3445 break;
3447 if (STRINGP (object))
3448 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3449 else
3450 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3451 spec = Fget_char_property (pos, Qdisplay, object);
3452 if (!STRINGP (object))
3453 bufpos = CHARPOS (tpos);
3454 } while (NILP (spec)
3455 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3456 bufpos, frame_window_p)));
3457 if (rv == 2)
3458 *disp_prop = 2;
3460 return CHARPOS (tpos);
3463 /* Return the character position of the end of the display string that
3464 started at CHARPOS. If there's no display string at CHARPOS,
3465 return -1. A display string is either an overlay with `display'
3466 property whose value is a string or a `display' text property whose
3467 value is a string. */
3468 EMACS_INT
3469 compute_display_string_end (EMACS_INT charpos, struct bidi_string_data *string)
3471 /* OBJECT = nil means current buffer. */
3472 Lisp_Object object =
3473 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3474 Lisp_Object pos = make_number (charpos);
3475 EMACS_INT eob =
3476 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3478 if (charpos >= eob || (string->s && !STRINGP (object)))
3479 return eob;
3481 /* It could happen that the display property or overlay was removed
3482 since we found it in compute_display_string_pos above. One way
3483 this can happen is if JIT font-lock was called (through
3484 handle_fontified_prop), and jit-lock-functions remove text
3485 properties or overlays from the portion of buffer that includes
3486 CHARPOS. Muse mode is known to do that, for example. In this
3487 case, we return -1 to the caller, to signal that no display
3488 string is actually present at CHARPOS. See bidi_fetch_char for
3489 how this is handled.
3491 An alternative would be to never look for display properties past
3492 it->stop_charpos. But neither compute_display_string_pos nor
3493 bidi_fetch_char that calls it know or care where the next
3494 stop_charpos is. */
3495 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3496 return -1;
3498 /* Look forward for the first character where the `display' property
3499 changes. */
3500 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3502 return XFASTINT (pos);
3507 /***********************************************************************
3508 Fontification
3509 ***********************************************************************/
3511 /* Handle changes in the `fontified' property of the current buffer by
3512 calling hook functions from Qfontification_functions to fontify
3513 regions of text. */
3515 static enum prop_handled
3516 handle_fontified_prop (struct it *it)
3518 Lisp_Object prop, pos;
3519 enum prop_handled handled = HANDLED_NORMALLY;
3521 if (!NILP (Vmemory_full))
3522 return handled;
3524 /* Get the value of the `fontified' property at IT's current buffer
3525 position. (The `fontified' property doesn't have a special
3526 meaning in strings.) If the value is nil, call functions from
3527 Qfontification_functions. */
3528 if (!STRINGP (it->string)
3529 && it->s == NULL
3530 && !NILP (Vfontification_functions)
3531 && !NILP (Vrun_hooks)
3532 && (pos = make_number (IT_CHARPOS (*it)),
3533 prop = Fget_char_property (pos, Qfontified, Qnil),
3534 /* Ignore the special cased nil value always present at EOB since
3535 no amount of fontifying will be able to change it. */
3536 NILP (prop) && IT_CHARPOS (*it) < Z))
3538 int count = SPECPDL_INDEX ();
3539 Lisp_Object val;
3540 struct buffer *obuf = current_buffer;
3541 int begv = BEGV, zv = ZV;
3542 int old_clip_changed = current_buffer->clip_changed;
3544 val = Vfontification_functions;
3545 specbind (Qfontification_functions, Qnil);
3547 xassert (it->end_charpos == ZV);
3549 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3550 safe_call1 (val, pos);
3551 else
3553 Lisp_Object fns, fn;
3554 struct gcpro gcpro1, gcpro2;
3556 fns = Qnil;
3557 GCPRO2 (val, fns);
3559 for (; CONSP (val); val = XCDR (val))
3561 fn = XCAR (val);
3563 if (EQ (fn, Qt))
3565 /* A value of t indicates this hook has a local
3566 binding; it means to run the global binding too.
3567 In a global value, t should not occur. If it
3568 does, we must ignore it to avoid an endless
3569 loop. */
3570 for (fns = Fdefault_value (Qfontification_functions);
3571 CONSP (fns);
3572 fns = XCDR (fns))
3574 fn = XCAR (fns);
3575 if (!EQ (fn, Qt))
3576 safe_call1 (fn, pos);
3579 else
3580 safe_call1 (fn, pos);
3583 UNGCPRO;
3586 unbind_to (count, Qnil);
3588 /* Fontification functions routinely call `save-restriction'.
3589 Normally, this tags clip_changed, which can confuse redisplay
3590 (see discussion in Bug#6671). Since we don't perform any
3591 special handling of fontification changes in the case where
3592 `save-restriction' isn't called, there's no point doing so in
3593 this case either. So, if the buffer's restrictions are
3594 actually left unchanged, reset clip_changed. */
3595 if (obuf == current_buffer)
3597 if (begv == BEGV && zv == ZV)
3598 current_buffer->clip_changed = old_clip_changed;
3600 /* There isn't much we can reasonably do to protect against
3601 misbehaving fontification, but here's a fig leaf. */
3602 else if (!NILP (BVAR (obuf, name)))
3603 set_buffer_internal_1 (obuf);
3605 /* The fontification code may have added/removed text.
3606 It could do even a lot worse, but let's at least protect against
3607 the most obvious case where only the text past `pos' gets changed',
3608 as is/was done in grep.el where some escapes sequences are turned
3609 into face properties (bug#7876). */
3610 it->end_charpos = ZV;
3612 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3613 something. This avoids an endless loop if they failed to
3614 fontify the text for which reason ever. */
3615 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3616 handled = HANDLED_RECOMPUTE_PROPS;
3619 return handled;
3624 /***********************************************************************
3625 Faces
3626 ***********************************************************************/
3628 /* Set up iterator IT from face properties at its current position.
3629 Called from handle_stop. */
3631 static enum prop_handled
3632 handle_face_prop (struct it *it)
3634 int new_face_id;
3635 EMACS_INT next_stop;
3637 if (!STRINGP (it->string))
3639 new_face_id
3640 = face_at_buffer_position (it->w,
3641 IT_CHARPOS (*it),
3642 it->region_beg_charpos,
3643 it->region_end_charpos,
3644 &next_stop,
3645 (IT_CHARPOS (*it)
3646 + TEXT_PROP_DISTANCE_LIMIT),
3647 0, it->base_face_id);
3649 /* Is this a start of a run of characters with box face?
3650 Caveat: this can be called for a freshly initialized
3651 iterator; face_id is -1 in this case. We know that the new
3652 face will not change until limit, i.e. if the new face has a
3653 box, all characters up to limit will have one. But, as
3654 usual, we don't know whether limit is really the end. */
3655 if (new_face_id != it->face_id)
3657 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3659 /* If new face has a box but old face has not, this is
3660 the start of a run of characters with box, i.e. it has
3661 a shadow on the left side. The value of face_id of the
3662 iterator will be -1 if this is the initial call that gets
3663 the face. In this case, we have to look in front of IT's
3664 position and see whether there is a face != new_face_id. */
3665 it->start_of_box_run_p
3666 = (new_face->box != FACE_NO_BOX
3667 && (it->face_id >= 0
3668 || IT_CHARPOS (*it) == BEG
3669 || new_face_id != face_before_it_pos (it)));
3670 it->face_box_p = new_face->box != FACE_NO_BOX;
3673 else
3675 int base_face_id;
3676 EMACS_INT bufpos;
3677 int i;
3678 Lisp_Object from_overlay
3679 = (it->current.overlay_string_index >= 0
3680 ? it->string_overlays[it->current.overlay_string_index]
3681 : Qnil);
3683 /* See if we got to this string directly or indirectly from
3684 an overlay property. That includes the before-string or
3685 after-string of an overlay, strings in display properties
3686 provided by an overlay, their text properties, etc.
3688 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3689 if (! NILP (from_overlay))
3690 for (i = it->sp - 1; i >= 0; i--)
3692 if (it->stack[i].current.overlay_string_index >= 0)
3693 from_overlay
3694 = it->string_overlays[it->stack[i].current.overlay_string_index];
3695 else if (! NILP (it->stack[i].from_overlay))
3696 from_overlay = it->stack[i].from_overlay;
3698 if (!NILP (from_overlay))
3699 break;
3702 if (! NILP (from_overlay))
3704 bufpos = IT_CHARPOS (*it);
3705 /* For a string from an overlay, the base face depends
3706 only on text properties and ignores overlays. */
3707 base_face_id
3708 = face_for_overlay_string (it->w,
3709 IT_CHARPOS (*it),
3710 it->region_beg_charpos,
3711 it->region_end_charpos,
3712 &next_stop,
3713 (IT_CHARPOS (*it)
3714 + TEXT_PROP_DISTANCE_LIMIT),
3716 from_overlay);
3718 else
3720 bufpos = 0;
3722 /* For strings from a `display' property, use the face at
3723 IT's current buffer position as the base face to merge
3724 with, so that overlay strings appear in the same face as
3725 surrounding text, unless they specify their own
3726 faces. */
3727 base_face_id = it->string_from_prefix_prop_p
3728 ? DEFAULT_FACE_ID
3729 : underlying_face_id (it);
3732 new_face_id = face_at_string_position (it->w,
3733 it->string,
3734 IT_STRING_CHARPOS (*it),
3735 bufpos,
3736 it->region_beg_charpos,
3737 it->region_end_charpos,
3738 &next_stop,
3739 base_face_id, 0);
3741 /* Is this a start of a run of characters with box? Caveat:
3742 this can be called for a freshly allocated iterator; face_id
3743 is -1 is this case. We know that the new face will not
3744 change until the next check pos, i.e. if the new face has a
3745 box, all characters up to that position will have a
3746 box. But, as usual, we don't know whether that position
3747 is really the end. */
3748 if (new_face_id != it->face_id)
3750 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3751 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3753 /* If new face has a box but old face hasn't, this is the
3754 start of a run of characters with box, i.e. it has a
3755 shadow on the left side. */
3756 it->start_of_box_run_p
3757 = new_face->box && (old_face == NULL || !old_face->box);
3758 it->face_box_p = new_face->box != FACE_NO_BOX;
3762 it->face_id = new_face_id;
3763 return HANDLED_NORMALLY;
3767 /* Return the ID of the face ``underlying'' IT's current position,
3768 which is in a string. If the iterator is associated with a
3769 buffer, return the face at IT's current buffer position.
3770 Otherwise, use the iterator's base_face_id. */
3772 static int
3773 underlying_face_id (struct it *it)
3775 int face_id = it->base_face_id, i;
3777 xassert (STRINGP (it->string));
3779 for (i = it->sp - 1; i >= 0; --i)
3780 if (NILP (it->stack[i].string))
3781 face_id = it->stack[i].face_id;
3783 return face_id;
3787 /* Compute the face one character before or after the current position
3788 of IT, in the visual order. BEFORE_P non-zero means get the face
3789 in front (to the left in L2R paragraphs, to the right in R2L
3790 paragraphs) of IT's screen position. Value is the ID of the face. */
3792 static int
3793 face_before_or_after_it_pos (struct it *it, int before_p)
3795 int face_id, limit;
3796 EMACS_INT next_check_charpos;
3797 struct it it_copy;
3798 void *it_copy_data = NULL;
3800 xassert (it->s == NULL);
3802 if (STRINGP (it->string))
3804 EMACS_INT bufpos, charpos;
3805 int base_face_id;
3807 /* No face change past the end of the string (for the case
3808 we are padding with spaces). No face change before the
3809 string start. */
3810 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3811 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3812 return it->face_id;
3814 if (!it->bidi_p)
3816 /* Set charpos to the position before or after IT's current
3817 position, in the logical order, which in the non-bidi
3818 case is the same as the visual order. */
3819 if (before_p)
3820 charpos = IT_STRING_CHARPOS (*it) - 1;
3821 else if (it->what == IT_COMPOSITION)
3822 /* For composition, we must check the character after the
3823 composition. */
3824 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3825 else
3826 charpos = IT_STRING_CHARPOS (*it) + 1;
3828 else
3830 if (before_p)
3832 /* With bidi iteration, the character before the current
3833 in the visual order cannot be found by simple
3834 iteration, because "reverse" reordering is not
3835 supported. Instead, we need to use the move_it_*
3836 family of functions. */
3837 /* Ignore face changes before the first visible
3838 character on this display line. */
3839 if (it->current_x <= it->first_visible_x)
3840 return it->face_id;
3841 SAVE_IT (it_copy, *it, it_copy_data);
3842 /* Implementation note: Since move_it_in_display_line
3843 works in the iterator geometry, and thinks the first
3844 character is always the leftmost, even in R2L lines,
3845 we don't need to distinguish between the R2L and L2R
3846 cases here. */
3847 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3848 it_copy.current_x - 1, MOVE_TO_X);
3849 charpos = IT_STRING_CHARPOS (it_copy);
3850 RESTORE_IT (it, it, it_copy_data);
3852 else
3854 /* Set charpos to the string position of the character
3855 that comes after IT's current position in the visual
3856 order. */
3857 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3859 it_copy = *it;
3860 while (n--)
3861 bidi_move_to_visually_next (&it_copy.bidi_it);
3863 charpos = it_copy.bidi_it.charpos;
3866 xassert (0 <= charpos && charpos <= SCHARS (it->string));
3868 if (it->current.overlay_string_index >= 0)
3869 bufpos = IT_CHARPOS (*it);
3870 else
3871 bufpos = 0;
3873 base_face_id = underlying_face_id (it);
3875 /* Get the face for ASCII, or unibyte. */
3876 face_id = face_at_string_position (it->w,
3877 it->string,
3878 charpos,
3879 bufpos,
3880 it->region_beg_charpos,
3881 it->region_end_charpos,
3882 &next_check_charpos,
3883 base_face_id, 0);
3885 /* Correct the face for charsets different from ASCII. Do it
3886 for the multibyte case only. The face returned above is
3887 suitable for unibyte text if IT->string is unibyte. */
3888 if (STRING_MULTIBYTE (it->string))
3890 struct text_pos pos1 = string_pos (charpos, it->string);
3891 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3892 int c, len;
3893 struct face *face = FACE_FROM_ID (it->f, face_id);
3895 c = string_char_and_length (p, &len);
3896 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3899 else
3901 struct text_pos pos;
3903 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3904 || (IT_CHARPOS (*it) <= BEGV && before_p))
3905 return it->face_id;
3907 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3908 pos = it->current.pos;
3910 if (!it->bidi_p)
3912 if (before_p)
3913 DEC_TEXT_POS (pos, it->multibyte_p);
3914 else
3916 if (it->what == IT_COMPOSITION)
3918 /* For composition, we must check the position after
3919 the composition. */
3920 pos.charpos += it->cmp_it.nchars;
3921 pos.bytepos += it->len;
3923 else
3924 INC_TEXT_POS (pos, it->multibyte_p);
3927 else
3929 if (before_p)
3931 /* With bidi iteration, the character before the current
3932 in the visual order cannot be found by simple
3933 iteration, because "reverse" reordering is not
3934 supported. Instead, we need to use the move_it_*
3935 family of functions. */
3936 /* Ignore face changes before the first visible
3937 character on this display line. */
3938 if (it->current_x <= it->first_visible_x)
3939 return it->face_id;
3940 SAVE_IT (it_copy, *it, it_copy_data);
3941 /* Implementation note: Since move_it_in_display_line
3942 works in the iterator geometry, and thinks the first
3943 character is always the leftmost, even in R2L lines,
3944 we don't need to distinguish between the R2L and L2R
3945 cases here. */
3946 move_it_in_display_line (&it_copy, ZV,
3947 it_copy.current_x - 1, MOVE_TO_X);
3948 pos = it_copy.current.pos;
3949 RESTORE_IT (it, it, it_copy_data);
3951 else
3953 /* Set charpos to the buffer position of the character
3954 that comes after IT's current position in the visual
3955 order. */
3956 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3958 it_copy = *it;
3959 while (n--)
3960 bidi_move_to_visually_next (&it_copy.bidi_it);
3962 SET_TEXT_POS (pos,
3963 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3966 xassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
3968 /* Determine face for CHARSET_ASCII, or unibyte. */
3969 face_id = face_at_buffer_position (it->w,
3970 CHARPOS (pos),
3971 it->region_beg_charpos,
3972 it->region_end_charpos,
3973 &next_check_charpos,
3974 limit, 0, -1);
3976 /* Correct the face for charsets different from ASCII. Do it
3977 for the multibyte case only. The face returned above is
3978 suitable for unibyte text if current_buffer is unibyte. */
3979 if (it->multibyte_p)
3981 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3982 struct face *face = FACE_FROM_ID (it->f, face_id);
3983 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3987 return face_id;
3992 /***********************************************************************
3993 Invisible text
3994 ***********************************************************************/
3996 /* Set up iterator IT from invisible properties at its current
3997 position. Called from handle_stop. */
3999 static enum prop_handled
4000 handle_invisible_prop (struct it *it)
4002 enum prop_handled handled = HANDLED_NORMALLY;
4004 if (STRINGP (it->string))
4006 Lisp_Object prop, end_charpos, limit, charpos;
4008 /* Get the value of the invisible text property at the
4009 current position. Value will be nil if there is no such
4010 property. */
4011 charpos = make_number (IT_STRING_CHARPOS (*it));
4012 prop = Fget_text_property (charpos, Qinvisible, it->string);
4014 if (!NILP (prop)
4015 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4017 EMACS_INT endpos;
4019 handled = HANDLED_RECOMPUTE_PROPS;
4021 /* Get the position at which the next change of the
4022 invisible text property can be found in IT->string.
4023 Value will be nil if the property value is the same for
4024 all the rest of IT->string. */
4025 XSETINT (limit, SCHARS (it->string));
4026 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4027 it->string, limit);
4029 /* Text at current position is invisible. The next
4030 change in the property is at position end_charpos.
4031 Move IT's current position to that position. */
4032 if (INTEGERP (end_charpos)
4033 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
4035 struct text_pos old;
4036 EMACS_INT oldpos;
4038 old = it->current.string_pos;
4039 oldpos = CHARPOS (old);
4040 if (it->bidi_p)
4042 if (it->bidi_it.first_elt
4043 && it->bidi_it.charpos < SCHARS (it->string))
4044 bidi_paragraph_init (it->paragraph_embedding,
4045 &it->bidi_it, 1);
4046 /* Bidi-iterate out of the invisible text. */
4049 bidi_move_to_visually_next (&it->bidi_it);
4051 while (oldpos <= it->bidi_it.charpos
4052 && it->bidi_it.charpos < endpos);
4054 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4055 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4056 if (IT_CHARPOS (*it) >= endpos)
4057 it->prev_stop = endpos;
4059 else
4061 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4062 compute_string_pos (&it->current.string_pos, old, it->string);
4065 else
4067 /* The rest of the string is invisible. If this is an
4068 overlay string, proceed with the next overlay string
4069 or whatever comes and return a character from there. */
4070 if (it->current.overlay_string_index >= 0)
4072 next_overlay_string (it);
4073 /* Don't check for overlay strings when we just
4074 finished processing them. */
4075 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4077 else
4079 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4080 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4085 else
4087 int invis_p;
4088 EMACS_INT newpos, next_stop, start_charpos, tem;
4089 Lisp_Object pos, prop, overlay;
4091 /* First of all, is there invisible text at this position? */
4092 tem = start_charpos = IT_CHARPOS (*it);
4093 pos = make_number (tem);
4094 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4095 &overlay);
4096 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4098 /* If we are on invisible text, skip over it. */
4099 if (invis_p && start_charpos < it->end_charpos)
4101 /* Record whether we have to display an ellipsis for the
4102 invisible text. */
4103 int display_ellipsis_p = invis_p == 2;
4105 handled = HANDLED_RECOMPUTE_PROPS;
4107 /* Loop skipping over invisible text. The loop is left at
4108 ZV or with IT on the first char being visible again. */
4111 /* Try to skip some invisible text. Return value is the
4112 position reached which can be equal to where we start
4113 if there is nothing invisible there. This skips both
4114 over invisible text properties and overlays with
4115 invisible property. */
4116 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4118 /* If we skipped nothing at all we weren't at invisible
4119 text in the first place. If everything to the end of
4120 the buffer was skipped, end the loop. */
4121 if (newpos == tem || newpos >= ZV)
4122 invis_p = 0;
4123 else
4125 /* We skipped some characters but not necessarily
4126 all there are. Check if we ended up on visible
4127 text. Fget_char_property returns the property of
4128 the char before the given position, i.e. if we
4129 get invis_p = 0, this means that the char at
4130 newpos is visible. */
4131 pos = make_number (newpos);
4132 prop = Fget_char_property (pos, Qinvisible, it->window);
4133 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4136 /* If we ended up on invisible text, proceed to
4137 skip starting with next_stop. */
4138 if (invis_p)
4139 tem = next_stop;
4141 /* If there are adjacent invisible texts, don't lose the
4142 second one's ellipsis. */
4143 if (invis_p == 2)
4144 display_ellipsis_p = 1;
4146 while (invis_p);
4148 /* The position newpos is now either ZV or on visible text. */
4149 if (it->bidi_p)
4151 EMACS_INT bpos = CHAR_TO_BYTE (newpos);
4152 int on_newline =
4153 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4154 int after_newline =
4155 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4157 /* If the invisible text ends on a newline or on a
4158 character after a newline, we can avoid the costly,
4159 character by character, bidi iteration to NEWPOS, and
4160 instead simply reseat the iterator there. That's
4161 because all bidi reordering information is tossed at
4162 the newline. This is a big win for modes that hide
4163 complete lines, like Outline, Org, etc. */
4164 if (on_newline || after_newline)
4166 struct text_pos tpos;
4167 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4169 SET_TEXT_POS (tpos, newpos, bpos);
4170 reseat_1 (it, tpos, 0);
4171 /* If we reseat on a newline/ZV, we need to prep the
4172 bidi iterator for advancing to the next character
4173 after the newline/EOB, keeping the current paragraph
4174 direction (so that PRODUCE_GLYPHS does TRT wrt
4175 prepending/appending glyphs to a glyph row). */
4176 if (on_newline)
4178 it->bidi_it.first_elt = 0;
4179 it->bidi_it.paragraph_dir = pdir;
4180 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4181 it->bidi_it.nchars = 1;
4182 it->bidi_it.ch_len = 1;
4185 else /* Must use the slow method. */
4187 /* With bidi iteration, the region of invisible text
4188 could start and/or end in the middle of a
4189 non-base embedding level. Therefore, we need to
4190 skip invisible text using the bidi iterator,
4191 starting at IT's current position, until we find
4192 ourselves outside of the invisible text.
4193 Skipping invisible text _after_ bidi iteration
4194 avoids affecting the visual order of the
4195 displayed text when invisible properties are
4196 added or removed. */
4197 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4199 /* If we were `reseat'ed to a new paragraph,
4200 determine the paragraph base direction. We
4201 need to do it now because
4202 next_element_from_buffer may not have a
4203 chance to do it, if we are going to skip any
4204 text at the beginning, which resets the
4205 FIRST_ELT flag. */
4206 bidi_paragraph_init (it->paragraph_embedding,
4207 &it->bidi_it, 1);
4211 bidi_move_to_visually_next (&it->bidi_it);
4213 while (it->stop_charpos <= it->bidi_it.charpos
4214 && it->bidi_it.charpos < newpos);
4215 IT_CHARPOS (*it) = it->bidi_it.charpos;
4216 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4217 /* If we overstepped NEWPOS, record its position in
4218 the iterator, so that we skip invisible text if
4219 later the bidi iteration lands us in the
4220 invisible region again. */
4221 if (IT_CHARPOS (*it) >= newpos)
4222 it->prev_stop = newpos;
4225 else
4227 IT_CHARPOS (*it) = newpos;
4228 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4231 /* If there are before-strings at the start of invisible
4232 text, and the text is invisible because of a text
4233 property, arrange to show before-strings because 20.x did
4234 it that way. (If the text is invisible because of an
4235 overlay property instead of a text property, this is
4236 already handled in the overlay code.) */
4237 if (NILP (overlay)
4238 && get_overlay_strings (it, it->stop_charpos))
4240 handled = HANDLED_RECOMPUTE_PROPS;
4241 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4243 else if (display_ellipsis_p)
4245 /* Make sure that the glyphs of the ellipsis will get
4246 correct `charpos' values. If we would not update
4247 it->position here, the glyphs would belong to the
4248 last visible character _before_ the invisible
4249 text, which confuses `set_cursor_from_row'.
4251 We use the last invisible position instead of the
4252 first because this way the cursor is always drawn on
4253 the first "." of the ellipsis, whenever PT is inside
4254 the invisible text. Otherwise the cursor would be
4255 placed _after_ the ellipsis when the point is after the
4256 first invisible character. */
4257 if (!STRINGP (it->object))
4259 it->position.charpos = newpos - 1;
4260 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4262 it->ellipsis_p = 1;
4263 /* Let the ellipsis display before
4264 considering any properties of the following char.
4265 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4266 handled = HANDLED_RETURN;
4271 return handled;
4275 /* Make iterator IT return `...' next.
4276 Replaces LEN characters from buffer. */
4278 static void
4279 setup_for_ellipsis (struct it *it, int len)
4281 /* Use the display table definition for `...'. Invalid glyphs
4282 will be handled by the method returning elements from dpvec. */
4283 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4285 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4286 it->dpvec = v->contents;
4287 it->dpend = v->contents + v->header.size;
4289 else
4291 /* Default `...'. */
4292 it->dpvec = default_invis_vector;
4293 it->dpend = default_invis_vector + 3;
4296 it->dpvec_char_len = len;
4297 it->current.dpvec_index = 0;
4298 it->dpvec_face_id = -1;
4300 /* Remember the current face id in case glyphs specify faces.
4301 IT's face is restored in set_iterator_to_next.
4302 saved_face_id was set to preceding char's face in handle_stop. */
4303 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4304 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4306 it->method = GET_FROM_DISPLAY_VECTOR;
4307 it->ellipsis_p = 1;
4312 /***********************************************************************
4313 'display' property
4314 ***********************************************************************/
4316 /* Set up iterator IT from `display' property at its current position.
4317 Called from handle_stop.
4318 We return HANDLED_RETURN if some part of the display property
4319 overrides the display of the buffer text itself.
4320 Otherwise we return HANDLED_NORMALLY. */
4322 static enum prop_handled
4323 handle_display_prop (struct it *it)
4325 Lisp_Object propval, object, overlay;
4326 struct text_pos *position;
4327 EMACS_INT bufpos;
4328 /* Nonzero if some property replaces the display of the text itself. */
4329 int display_replaced_p = 0;
4331 if (STRINGP (it->string))
4333 object = it->string;
4334 position = &it->current.string_pos;
4335 bufpos = CHARPOS (it->current.pos);
4337 else
4339 XSETWINDOW (object, it->w);
4340 position = &it->current.pos;
4341 bufpos = CHARPOS (*position);
4344 /* Reset those iterator values set from display property values. */
4345 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4346 it->space_width = Qnil;
4347 it->font_height = Qnil;
4348 it->voffset = 0;
4350 /* We don't support recursive `display' properties, i.e. string
4351 values that have a string `display' property, that have a string
4352 `display' property etc. */
4353 if (!it->string_from_display_prop_p)
4354 it->area = TEXT_AREA;
4356 propval = get_char_property_and_overlay (make_number (position->charpos),
4357 Qdisplay, object, &overlay);
4358 if (NILP (propval))
4359 return HANDLED_NORMALLY;
4360 /* Now OVERLAY is the overlay that gave us this property, or nil
4361 if it was a text property. */
4363 if (!STRINGP (it->string))
4364 object = it->w->buffer;
4366 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4367 position, bufpos,
4368 FRAME_WINDOW_P (it->f));
4370 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4373 /* Subroutine of handle_display_prop. Returns non-zero if the display
4374 specification in SPEC is a replacing specification, i.e. it would
4375 replace the text covered by `display' property with something else,
4376 such as an image or a display string. If SPEC includes any kind or
4377 `(space ...) specification, the value is 2; this is used by
4378 compute_display_string_pos, which see.
4380 See handle_single_display_spec for documentation of arguments.
4381 frame_window_p is non-zero if the window being redisplayed is on a
4382 GUI frame; this argument is used only if IT is NULL, see below.
4384 IT can be NULL, if this is called by the bidi reordering code
4385 through compute_display_string_pos, which see. In that case, this
4386 function only examines SPEC, but does not otherwise "handle" it, in
4387 the sense that it doesn't set up members of IT from the display
4388 spec. */
4389 static int
4390 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4391 Lisp_Object overlay, struct text_pos *position,
4392 EMACS_INT bufpos, int frame_window_p)
4394 int replacing_p = 0;
4395 int rv;
4397 if (CONSP (spec)
4398 /* Simple specifications. */
4399 && !EQ (XCAR (spec), Qimage)
4400 && !EQ (XCAR (spec), Qspace)
4401 && !EQ (XCAR (spec), Qwhen)
4402 && !EQ (XCAR (spec), Qslice)
4403 && !EQ (XCAR (spec), Qspace_width)
4404 && !EQ (XCAR (spec), Qheight)
4405 && !EQ (XCAR (spec), Qraise)
4406 /* Marginal area specifications. */
4407 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4408 && !EQ (XCAR (spec), Qleft_fringe)
4409 && !EQ (XCAR (spec), Qright_fringe)
4410 && !NILP (XCAR (spec)))
4412 for (; CONSP (spec); spec = XCDR (spec))
4414 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4415 overlay, position, bufpos,
4416 replacing_p, frame_window_p)))
4418 replacing_p = rv;
4419 /* If some text in a string is replaced, `position' no
4420 longer points to the position of `object'. */
4421 if (!it || STRINGP (object))
4422 break;
4426 else if (VECTORP (spec))
4428 int i;
4429 for (i = 0; i < ASIZE (spec); ++i)
4430 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4431 overlay, position, bufpos,
4432 replacing_p, frame_window_p)))
4434 replacing_p = rv;
4435 /* If some text in a string is replaced, `position' no
4436 longer points to the position of `object'. */
4437 if (!it || STRINGP (object))
4438 break;
4441 else
4443 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4444 position, bufpos, 0,
4445 frame_window_p)))
4446 replacing_p = rv;
4449 return replacing_p;
4452 /* Value is the position of the end of the `display' property starting
4453 at START_POS in OBJECT. */
4455 static struct text_pos
4456 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4458 Lisp_Object end;
4459 struct text_pos end_pos;
4461 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4462 Qdisplay, object, Qnil);
4463 CHARPOS (end_pos) = XFASTINT (end);
4464 if (STRINGP (object))
4465 compute_string_pos (&end_pos, start_pos, it->string);
4466 else
4467 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4469 return end_pos;
4473 /* Set up IT from a single `display' property specification SPEC. OBJECT
4474 is the object in which the `display' property was found. *POSITION
4475 is the position in OBJECT at which the `display' property was found.
4476 BUFPOS is the buffer position of OBJECT (different from POSITION if
4477 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4478 previously saw a display specification which already replaced text
4479 display with something else, for example an image; we ignore such
4480 properties after the first one has been processed.
4482 OVERLAY is the overlay this `display' property came from,
4483 or nil if it was a text property.
4485 If SPEC is a `space' or `image' specification, and in some other
4486 cases too, set *POSITION to the position where the `display'
4487 property ends.
4489 If IT is NULL, only examine the property specification in SPEC, but
4490 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4491 is intended to be displayed in a window on a GUI frame.
4493 Value is non-zero if something was found which replaces the display
4494 of buffer or string text. */
4496 static int
4497 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4498 Lisp_Object overlay, struct text_pos *position,
4499 EMACS_INT bufpos, int display_replaced_p,
4500 int frame_window_p)
4502 Lisp_Object form;
4503 Lisp_Object location, value;
4504 struct text_pos start_pos = *position;
4505 int valid_p;
4507 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4508 If the result is non-nil, use VALUE instead of SPEC. */
4509 form = Qt;
4510 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4512 spec = XCDR (spec);
4513 if (!CONSP (spec))
4514 return 0;
4515 form = XCAR (spec);
4516 spec = XCDR (spec);
4519 if (!NILP (form) && !EQ (form, Qt))
4521 int count = SPECPDL_INDEX ();
4522 struct gcpro gcpro1;
4524 /* Bind `object' to the object having the `display' property, a
4525 buffer or string. Bind `position' to the position in the
4526 object where the property was found, and `buffer-position'
4527 to the current position in the buffer. */
4529 if (NILP (object))
4530 XSETBUFFER (object, current_buffer);
4531 specbind (Qobject, object);
4532 specbind (Qposition, make_number (CHARPOS (*position)));
4533 specbind (Qbuffer_position, make_number (bufpos));
4534 GCPRO1 (form);
4535 form = safe_eval (form);
4536 UNGCPRO;
4537 unbind_to (count, Qnil);
4540 if (NILP (form))
4541 return 0;
4543 /* Handle `(height HEIGHT)' specifications. */
4544 if (CONSP (spec)
4545 && EQ (XCAR (spec), Qheight)
4546 && CONSP (XCDR (spec)))
4548 if (it)
4550 if (!FRAME_WINDOW_P (it->f))
4551 return 0;
4553 it->font_height = XCAR (XCDR (spec));
4554 if (!NILP (it->font_height))
4556 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4557 int new_height = -1;
4559 if (CONSP (it->font_height)
4560 && (EQ (XCAR (it->font_height), Qplus)
4561 || EQ (XCAR (it->font_height), Qminus))
4562 && CONSP (XCDR (it->font_height))
4563 && INTEGERP (XCAR (XCDR (it->font_height))))
4565 /* `(+ N)' or `(- N)' where N is an integer. */
4566 int steps = XINT (XCAR (XCDR (it->font_height)));
4567 if (EQ (XCAR (it->font_height), Qplus))
4568 steps = - steps;
4569 it->face_id = smaller_face (it->f, it->face_id, steps);
4571 else if (FUNCTIONP (it->font_height))
4573 /* Call function with current height as argument.
4574 Value is the new height. */
4575 Lisp_Object height;
4576 height = safe_call1 (it->font_height,
4577 face->lface[LFACE_HEIGHT_INDEX]);
4578 if (NUMBERP (height))
4579 new_height = XFLOATINT (height);
4581 else if (NUMBERP (it->font_height))
4583 /* Value is a multiple of the canonical char height. */
4584 struct face *f;
4586 f = FACE_FROM_ID (it->f,
4587 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4588 new_height = (XFLOATINT (it->font_height)
4589 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4591 else
4593 /* Evaluate IT->font_height with `height' bound to the
4594 current specified height to get the new height. */
4595 int count = SPECPDL_INDEX ();
4597 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4598 value = safe_eval (it->font_height);
4599 unbind_to (count, Qnil);
4601 if (NUMBERP (value))
4602 new_height = XFLOATINT (value);
4605 if (new_height > 0)
4606 it->face_id = face_with_height (it->f, it->face_id, new_height);
4610 return 0;
4613 /* Handle `(space-width WIDTH)'. */
4614 if (CONSP (spec)
4615 && EQ (XCAR (spec), Qspace_width)
4616 && CONSP (XCDR (spec)))
4618 if (it)
4620 if (!FRAME_WINDOW_P (it->f))
4621 return 0;
4623 value = XCAR (XCDR (spec));
4624 if (NUMBERP (value) && XFLOATINT (value) > 0)
4625 it->space_width = value;
4628 return 0;
4631 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4632 if (CONSP (spec)
4633 && EQ (XCAR (spec), Qslice))
4635 Lisp_Object tem;
4637 if (it)
4639 if (!FRAME_WINDOW_P (it->f))
4640 return 0;
4642 if (tem = XCDR (spec), CONSP (tem))
4644 it->slice.x = XCAR (tem);
4645 if (tem = XCDR (tem), CONSP (tem))
4647 it->slice.y = XCAR (tem);
4648 if (tem = XCDR (tem), CONSP (tem))
4650 it->slice.width = XCAR (tem);
4651 if (tem = XCDR (tem), CONSP (tem))
4652 it->slice.height = XCAR (tem);
4658 return 0;
4661 /* Handle `(raise FACTOR)'. */
4662 if (CONSP (spec)
4663 && EQ (XCAR (spec), Qraise)
4664 && CONSP (XCDR (spec)))
4666 if (it)
4668 if (!FRAME_WINDOW_P (it->f))
4669 return 0;
4671 #ifdef HAVE_WINDOW_SYSTEM
4672 value = XCAR (XCDR (spec));
4673 if (NUMBERP (value))
4675 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4676 it->voffset = - (XFLOATINT (value)
4677 * (FONT_HEIGHT (face->font)));
4679 #endif /* HAVE_WINDOW_SYSTEM */
4682 return 0;
4685 /* Don't handle the other kinds of display specifications
4686 inside a string that we got from a `display' property. */
4687 if (it && it->string_from_display_prop_p)
4688 return 0;
4690 /* Characters having this form of property are not displayed, so
4691 we have to find the end of the property. */
4692 if (it)
4694 start_pos = *position;
4695 *position = display_prop_end (it, object, start_pos);
4697 value = Qnil;
4699 /* Stop the scan at that end position--we assume that all
4700 text properties change there. */
4701 if (it)
4702 it->stop_charpos = position->charpos;
4704 /* Handle `(left-fringe BITMAP [FACE])'
4705 and `(right-fringe BITMAP [FACE])'. */
4706 if (CONSP (spec)
4707 && (EQ (XCAR (spec), Qleft_fringe)
4708 || EQ (XCAR (spec), Qright_fringe))
4709 && CONSP (XCDR (spec)))
4711 int fringe_bitmap;
4713 if (it)
4715 if (!FRAME_WINDOW_P (it->f))
4716 /* If we return here, POSITION has been advanced
4717 across the text with this property. */
4719 /* Synchronize the bidi iterator with POSITION. This is
4720 needed because we are not going to push the iterator
4721 on behalf of this display property, so there will be
4722 no pop_it call to do this synchronization for us. */
4723 if (it->bidi_p)
4725 it->position = *position;
4726 iterate_out_of_display_property (it);
4727 *position = it->position;
4729 return 1;
4732 else if (!frame_window_p)
4733 return 1;
4735 #ifdef HAVE_WINDOW_SYSTEM
4736 value = XCAR (XCDR (spec));
4737 if (!SYMBOLP (value)
4738 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4739 /* If we return here, POSITION has been advanced
4740 across the text with this property. */
4742 if (it && it->bidi_p)
4744 it->position = *position;
4745 iterate_out_of_display_property (it);
4746 *position = it->position;
4748 return 1;
4751 if (it)
4753 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4755 if (CONSP (XCDR (XCDR (spec))))
4757 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4758 int face_id2 = lookup_derived_face (it->f, face_name,
4759 FRINGE_FACE_ID, 0);
4760 if (face_id2 >= 0)
4761 face_id = face_id2;
4764 /* Save current settings of IT so that we can restore them
4765 when we are finished with the glyph property value. */
4766 push_it (it, position);
4768 it->area = TEXT_AREA;
4769 it->what = IT_IMAGE;
4770 it->image_id = -1; /* no image */
4771 it->position = start_pos;
4772 it->object = NILP (object) ? it->w->buffer : object;
4773 it->method = GET_FROM_IMAGE;
4774 it->from_overlay = Qnil;
4775 it->face_id = face_id;
4776 it->from_disp_prop_p = 1;
4778 /* Say that we haven't consumed the characters with
4779 `display' property yet. The call to pop_it in
4780 set_iterator_to_next will clean this up. */
4781 *position = start_pos;
4783 if (EQ (XCAR (spec), Qleft_fringe))
4785 it->left_user_fringe_bitmap = fringe_bitmap;
4786 it->left_user_fringe_face_id = face_id;
4788 else
4790 it->right_user_fringe_bitmap = fringe_bitmap;
4791 it->right_user_fringe_face_id = face_id;
4794 #endif /* HAVE_WINDOW_SYSTEM */
4795 return 1;
4798 /* Prepare to handle `((margin left-margin) ...)',
4799 `((margin right-margin) ...)' and `((margin nil) ...)'
4800 prefixes for display specifications. */
4801 location = Qunbound;
4802 if (CONSP (spec) && CONSP (XCAR (spec)))
4804 Lisp_Object tem;
4806 value = XCDR (spec);
4807 if (CONSP (value))
4808 value = XCAR (value);
4810 tem = XCAR (spec);
4811 if (EQ (XCAR (tem), Qmargin)
4812 && (tem = XCDR (tem),
4813 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4814 (NILP (tem)
4815 || EQ (tem, Qleft_margin)
4816 || EQ (tem, Qright_margin))))
4817 location = tem;
4820 if (EQ (location, Qunbound))
4822 location = Qnil;
4823 value = spec;
4826 /* After this point, VALUE is the property after any
4827 margin prefix has been stripped. It must be a string,
4828 an image specification, or `(space ...)'.
4830 LOCATION specifies where to display: `left-margin',
4831 `right-margin' or nil. */
4833 valid_p = (STRINGP (value)
4834 #ifdef HAVE_WINDOW_SYSTEM
4835 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4836 && valid_image_p (value))
4837 #endif /* not HAVE_WINDOW_SYSTEM */
4838 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4840 if (valid_p && !display_replaced_p)
4842 int retval = 1;
4844 if (!it)
4846 /* Callers need to know whether the display spec is any kind
4847 of `(space ...)' spec that is about to affect text-area
4848 display. */
4849 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4850 retval = 2;
4851 return retval;
4854 /* Save current settings of IT so that we can restore them
4855 when we are finished with the glyph property value. */
4856 push_it (it, position);
4857 it->from_overlay = overlay;
4858 it->from_disp_prop_p = 1;
4860 if (NILP (location))
4861 it->area = TEXT_AREA;
4862 else if (EQ (location, Qleft_margin))
4863 it->area = LEFT_MARGIN_AREA;
4864 else
4865 it->area = RIGHT_MARGIN_AREA;
4867 if (STRINGP (value))
4869 it->string = value;
4870 it->multibyte_p = STRING_MULTIBYTE (it->string);
4871 it->current.overlay_string_index = -1;
4872 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4873 it->end_charpos = it->string_nchars = SCHARS (it->string);
4874 it->method = GET_FROM_STRING;
4875 it->stop_charpos = 0;
4876 it->prev_stop = 0;
4877 it->base_level_stop = 0;
4878 it->string_from_display_prop_p = 1;
4879 /* Say that we haven't consumed the characters with
4880 `display' property yet. The call to pop_it in
4881 set_iterator_to_next will clean this up. */
4882 if (BUFFERP (object))
4883 *position = start_pos;
4885 /* Force paragraph direction to be that of the parent
4886 object. If the parent object's paragraph direction is
4887 not yet determined, default to L2R. */
4888 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4889 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4890 else
4891 it->paragraph_embedding = L2R;
4893 /* Set up the bidi iterator for this display string. */
4894 if (it->bidi_p)
4896 it->bidi_it.string.lstring = it->string;
4897 it->bidi_it.string.s = NULL;
4898 it->bidi_it.string.schars = it->end_charpos;
4899 it->bidi_it.string.bufpos = bufpos;
4900 it->bidi_it.string.from_disp_str = 1;
4901 it->bidi_it.string.unibyte = !it->multibyte_p;
4902 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4905 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4907 it->method = GET_FROM_STRETCH;
4908 it->object = value;
4909 *position = it->position = start_pos;
4910 retval = 1 + (it->area == TEXT_AREA);
4912 #ifdef HAVE_WINDOW_SYSTEM
4913 else
4915 it->what = IT_IMAGE;
4916 it->image_id = lookup_image (it->f, value);
4917 it->position = start_pos;
4918 it->object = NILP (object) ? it->w->buffer : object;
4919 it->method = GET_FROM_IMAGE;
4921 /* Say that we haven't consumed the characters with
4922 `display' property yet. The call to pop_it in
4923 set_iterator_to_next will clean this up. */
4924 *position = start_pos;
4926 #endif /* HAVE_WINDOW_SYSTEM */
4928 return retval;
4931 /* Invalid property or property not supported. Restore
4932 POSITION to what it was before. */
4933 *position = start_pos;
4934 return 0;
4937 /* Check if PROP is a display property value whose text should be
4938 treated as intangible. OVERLAY is the overlay from which PROP
4939 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4940 specify the buffer position covered by PROP. */
4943 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4944 EMACS_INT charpos, EMACS_INT bytepos)
4946 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4947 struct text_pos position;
4949 SET_TEXT_POS (position, charpos, bytepos);
4950 return handle_display_spec (NULL, prop, Qnil, overlay,
4951 &position, charpos, frame_window_p);
4955 /* Return 1 if PROP is a display sub-property value containing STRING.
4957 Implementation note: this and the following function are really
4958 special cases of handle_display_spec and
4959 handle_single_display_spec, and should ideally use the same code.
4960 Until they do, these two pairs must be consistent and must be
4961 modified in sync. */
4963 static int
4964 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4966 if (EQ (string, prop))
4967 return 1;
4969 /* Skip over `when FORM'. */
4970 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4972 prop = XCDR (prop);
4973 if (!CONSP (prop))
4974 return 0;
4975 /* Actually, the condition following `when' should be eval'ed,
4976 like handle_single_display_spec does, and we should return
4977 zero if it evaluates to nil. However, this function is
4978 called only when the buffer was already displayed and some
4979 glyph in the glyph matrix was found to come from a display
4980 string. Therefore, the condition was already evaluated, and
4981 the result was non-nil, otherwise the display string wouldn't
4982 have been displayed and we would have never been called for
4983 this property. Thus, we can skip the evaluation and assume
4984 its result is non-nil. */
4985 prop = XCDR (prop);
4988 if (CONSP (prop))
4989 /* Skip over `margin LOCATION'. */
4990 if (EQ (XCAR (prop), Qmargin))
4992 prop = XCDR (prop);
4993 if (!CONSP (prop))
4994 return 0;
4996 prop = XCDR (prop);
4997 if (!CONSP (prop))
4998 return 0;
5001 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5005 /* Return 1 if STRING appears in the `display' property PROP. */
5007 static int
5008 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5010 if (CONSP (prop)
5011 && !EQ (XCAR (prop), Qwhen)
5012 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5014 /* A list of sub-properties. */
5015 while (CONSP (prop))
5017 if (single_display_spec_string_p (XCAR (prop), string))
5018 return 1;
5019 prop = XCDR (prop);
5022 else if (VECTORP (prop))
5024 /* A vector of sub-properties. */
5025 int i;
5026 for (i = 0; i < ASIZE (prop); ++i)
5027 if (single_display_spec_string_p (AREF (prop, i), string))
5028 return 1;
5030 else
5031 return single_display_spec_string_p (prop, string);
5033 return 0;
5036 /* Look for STRING in overlays and text properties in the current
5037 buffer, between character positions FROM and TO (excluding TO).
5038 BACK_P non-zero means look back (in this case, TO is supposed to be
5039 less than FROM).
5040 Value is the first character position where STRING was found, or
5041 zero if it wasn't found before hitting TO.
5043 This function may only use code that doesn't eval because it is
5044 called asynchronously from note_mouse_highlight. */
5046 static EMACS_INT
5047 string_buffer_position_lim (Lisp_Object string,
5048 EMACS_INT from, EMACS_INT to, int back_p)
5050 Lisp_Object limit, prop, pos;
5051 int found = 0;
5053 pos = make_number (max (from, BEGV));
5055 if (!back_p) /* looking forward */
5057 limit = make_number (min (to, ZV));
5058 while (!found && !EQ (pos, limit))
5060 prop = Fget_char_property (pos, Qdisplay, Qnil);
5061 if (!NILP (prop) && display_prop_string_p (prop, string))
5062 found = 1;
5063 else
5064 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5065 limit);
5068 else /* looking back */
5070 limit = make_number (max (to, BEGV));
5071 while (!found && !EQ (pos, limit))
5073 prop = Fget_char_property (pos, Qdisplay, Qnil);
5074 if (!NILP (prop) && display_prop_string_p (prop, string))
5075 found = 1;
5076 else
5077 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5078 limit);
5082 return found ? XINT (pos) : 0;
5085 /* Determine which buffer position in current buffer STRING comes from.
5086 AROUND_CHARPOS is an approximate position where it could come from.
5087 Value is the buffer position or 0 if it couldn't be determined.
5089 This function is necessary because we don't record buffer positions
5090 in glyphs generated from strings (to keep struct glyph small).
5091 This function may only use code that doesn't eval because it is
5092 called asynchronously from note_mouse_highlight. */
5094 static EMACS_INT
5095 string_buffer_position (Lisp_Object string, EMACS_INT around_charpos)
5097 const int MAX_DISTANCE = 1000;
5098 EMACS_INT found = string_buffer_position_lim (string, around_charpos,
5099 around_charpos + MAX_DISTANCE,
5102 if (!found)
5103 found = string_buffer_position_lim (string, around_charpos,
5104 around_charpos - MAX_DISTANCE, 1);
5105 return found;
5110 /***********************************************************************
5111 `composition' property
5112 ***********************************************************************/
5114 /* Set up iterator IT from `composition' property at its current
5115 position. Called from handle_stop. */
5117 static enum prop_handled
5118 handle_composition_prop (struct it *it)
5120 Lisp_Object prop, string;
5121 EMACS_INT pos, pos_byte, start, end;
5123 if (STRINGP (it->string))
5125 unsigned char *s;
5127 pos = IT_STRING_CHARPOS (*it);
5128 pos_byte = IT_STRING_BYTEPOS (*it);
5129 string = it->string;
5130 s = SDATA (string) + pos_byte;
5131 it->c = STRING_CHAR (s);
5133 else
5135 pos = IT_CHARPOS (*it);
5136 pos_byte = IT_BYTEPOS (*it);
5137 string = Qnil;
5138 it->c = FETCH_CHAR (pos_byte);
5141 /* If there's a valid composition and point is not inside of the
5142 composition (in the case that the composition is from the current
5143 buffer), draw a glyph composed from the composition components. */
5144 if (find_composition (pos, -1, &start, &end, &prop, string)
5145 && COMPOSITION_VALID_P (start, end, prop)
5146 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5148 if (start < pos)
5149 /* As we can't handle this situation (perhaps font-lock added
5150 a new composition), we just return here hoping that next
5151 redisplay will detect this composition much earlier. */
5152 return HANDLED_NORMALLY;
5153 if (start != pos)
5155 if (STRINGP (it->string))
5156 pos_byte = string_char_to_byte (it->string, start);
5157 else
5158 pos_byte = CHAR_TO_BYTE (start);
5160 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5161 prop, string);
5163 if (it->cmp_it.id >= 0)
5165 it->cmp_it.ch = -1;
5166 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5167 it->cmp_it.nglyphs = -1;
5171 return HANDLED_NORMALLY;
5176 /***********************************************************************
5177 Overlay strings
5178 ***********************************************************************/
5180 /* The following structure is used to record overlay strings for
5181 later sorting in load_overlay_strings. */
5183 struct overlay_entry
5185 Lisp_Object overlay;
5186 Lisp_Object string;
5187 int priority;
5188 int after_string_p;
5192 /* Set up iterator IT from overlay strings at its current position.
5193 Called from handle_stop. */
5195 static enum prop_handled
5196 handle_overlay_change (struct it *it)
5198 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5199 return HANDLED_RECOMPUTE_PROPS;
5200 else
5201 return HANDLED_NORMALLY;
5205 /* Set up the next overlay string for delivery by IT, if there is an
5206 overlay string to deliver. Called by set_iterator_to_next when the
5207 end of the current overlay string is reached. If there are more
5208 overlay strings to display, IT->string and
5209 IT->current.overlay_string_index are set appropriately here.
5210 Otherwise IT->string is set to nil. */
5212 static void
5213 next_overlay_string (struct it *it)
5215 ++it->current.overlay_string_index;
5216 if (it->current.overlay_string_index == it->n_overlay_strings)
5218 /* No more overlay strings. Restore IT's settings to what
5219 they were before overlay strings were processed, and
5220 continue to deliver from current_buffer. */
5222 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5223 pop_it (it);
5224 xassert (it->sp > 0
5225 || (NILP (it->string)
5226 && it->method == GET_FROM_BUFFER
5227 && it->stop_charpos >= BEGV
5228 && it->stop_charpos <= it->end_charpos));
5229 it->current.overlay_string_index = -1;
5230 it->n_overlay_strings = 0;
5231 it->overlay_strings_charpos = -1;
5232 /* If there's an empty display string on the stack, pop the
5233 stack, to resync the bidi iterator with IT's position. Such
5234 empty strings are pushed onto the stack in
5235 get_overlay_strings_1. */
5236 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5237 pop_it (it);
5239 /* If we're at the end of the buffer, record that we have
5240 processed the overlay strings there already, so that
5241 next_element_from_buffer doesn't try it again. */
5242 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5243 it->overlay_strings_at_end_processed_p = 1;
5245 else
5247 /* There are more overlay strings to process. If
5248 IT->current.overlay_string_index has advanced to a position
5249 where we must load IT->overlay_strings with more strings, do
5250 it. We must load at the IT->overlay_strings_charpos where
5251 IT->n_overlay_strings was originally computed; when invisible
5252 text is present, this might not be IT_CHARPOS (Bug#7016). */
5253 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5255 if (it->current.overlay_string_index && i == 0)
5256 load_overlay_strings (it, it->overlay_strings_charpos);
5258 /* Initialize IT to deliver display elements from the overlay
5259 string. */
5260 it->string = it->overlay_strings[i];
5261 it->multibyte_p = STRING_MULTIBYTE (it->string);
5262 SET_TEXT_POS (it->current.string_pos, 0, 0);
5263 it->method = GET_FROM_STRING;
5264 it->stop_charpos = 0;
5265 if (it->cmp_it.stop_pos >= 0)
5266 it->cmp_it.stop_pos = 0;
5267 it->prev_stop = 0;
5268 it->base_level_stop = 0;
5270 /* Set up the bidi iterator for this overlay string. */
5271 if (it->bidi_p)
5273 it->bidi_it.string.lstring = it->string;
5274 it->bidi_it.string.s = NULL;
5275 it->bidi_it.string.schars = SCHARS (it->string);
5276 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5277 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5278 it->bidi_it.string.unibyte = !it->multibyte_p;
5279 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5283 CHECK_IT (it);
5287 /* Compare two overlay_entry structures E1 and E2. Used as a
5288 comparison function for qsort in load_overlay_strings. Overlay
5289 strings for the same position are sorted so that
5291 1. All after-strings come in front of before-strings, except
5292 when they come from the same overlay.
5294 2. Within after-strings, strings are sorted so that overlay strings
5295 from overlays with higher priorities come first.
5297 2. Within before-strings, strings are sorted so that overlay
5298 strings from overlays with higher priorities come last.
5300 Value is analogous to strcmp. */
5303 static int
5304 compare_overlay_entries (const void *e1, const void *e2)
5306 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5307 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5308 int result;
5310 if (entry1->after_string_p != entry2->after_string_p)
5312 /* Let after-strings appear in front of before-strings if
5313 they come from different overlays. */
5314 if (EQ (entry1->overlay, entry2->overlay))
5315 result = entry1->after_string_p ? 1 : -1;
5316 else
5317 result = entry1->after_string_p ? -1 : 1;
5319 else if (entry1->after_string_p)
5320 /* After-strings sorted in order of decreasing priority. */
5321 result = entry2->priority - entry1->priority;
5322 else
5323 /* Before-strings sorted in order of increasing priority. */
5324 result = entry1->priority - entry2->priority;
5326 return result;
5330 /* Load the vector IT->overlay_strings with overlay strings from IT's
5331 current buffer position, or from CHARPOS if that is > 0. Set
5332 IT->n_overlays to the total number of overlay strings found.
5334 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5335 a time. On entry into load_overlay_strings,
5336 IT->current.overlay_string_index gives the number of overlay
5337 strings that have already been loaded by previous calls to this
5338 function.
5340 IT->add_overlay_start contains an additional overlay start
5341 position to consider for taking overlay strings from, if non-zero.
5342 This position comes into play when the overlay has an `invisible'
5343 property, and both before and after-strings. When we've skipped to
5344 the end of the overlay, because of its `invisible' property, we
5345 nevertheless want its before-string to appear.
5346 IT->add_overlay_start will contain the overlay start position
5347 in this case.
5349 Overlay strings are sorted so that after-string strings come in
5350 front of before-string strings. Within before and after-strings,
5351 strings are sorted by overlay priority. See also function
5352 compare_overlay_entries. */
5354 static void
5355 load_overlay_strings (struct it *it, EMACS_INT charpos)
5357 Lisp_Object overlay, window, str, invisible;
5358 struct Lisp_Overlay *ov;
5359 EMACS_INT start, end;
5360 int size = 20;
5361 int n = 0, i, j, invis_p;
5362 struct overlay_entry *entries
5363 = (struct overlay_entry *) alloca (size * sizeof *entries);
5365 if (charpos <= 0)
5366 charpos = IT_CHARPOS (*it);
5368 /* Append the overlay string STRING of overlay OVERLAY to vector
5369 `entries' which has size `size' and currently contains `n'
5370 elements. AFTER_P non-zero means STRING is an after-string of
5371 OVERLAY. */
5372 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5373 do \
5375 Lisp_Object priority; \
5377 if (n == size) \
5379 int new_size = 2 * size; \
5380 struct overlay_entry *old = entries; \
5381 entries = \
5382 (struct overlay_entry *) alloca (new_size \
5383 * sizeof *entries); \
5384 memcpy (entries, old, size * sizeof *entries); \
5385 size = new_size; \
5388 entries[n].string = (STRING); \
5389 entries[n].overlay = (OVERLAY); \
5390 priority = Foverlay_get ((OVERLAY), Qpriority); \
5391 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5392 entries[n].after_string_p = (AFTER_P); \
5393 ++n; \
5395 while (0)
5397 /* Process overlay before the overlay center. */
5398 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5400 XSETMISC (overlay, ov);
5401 xassert (OVERLAYP (overlay));
5402 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5403 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5405 if (end < charpos)
5406 break;
5408 /* Skip this overlay if it doesn't start or end at IT's current
5409 position. */
5410 if (end != charpos && start != charpos)
5411 continue;
5413 /* Skip this overlay if it doesn't apply to IT->w. */
5414 window = Foverlay_get (overlay, Qwindow);
5415 if (WINDOWP (window) && XWINDOW (window) != it->w)
5416 continue;
5418 /* If the text ``under'' the overlay is invisible, both before-
5419 and after-strings from this overlay are visible; start and
5420 end position are indistinguishable. */
5421 invisible = Foverlay_get (overlay, Qinvisible);
5422 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5424 /* If overlay has a non-empty before-string, record it. */
5425 if ((start == charpos || (end == charpos && invis_p))
5426 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5427 && SCHARS (str))
5428 RECORD_OVERLAY_STRING (overlay, str, 0);
5430 /* If overlay has a non-empty after-string, record it. */
5431 if ((end == charpos || (start == charpos && invis_p))
5432 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5433 && SCHARS (str))
5434 RECORD_OVERLAY_STRING (overlay, str, 1);
5437 /* Process overlays after the overlay center. */
5438 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5440 XSETMISC (overlay, ov);
5441 xassert (OVERLAYP (overlay));
5442 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5443 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5445 if (start > charpos)
5446 break;
5448 /* Skip this overlay if it doesn't start or end at IT's current
5449 position. */
5450 if (end != charpos && start != charpos)
5451 continue;
5453 /* Skip this overlay if it doesn't apply to IT->w. */
5454 window = Foverlay_get (overlay, Qwindow);
5455 if (WINDOWP (window) && XWINDOW (window) != it->w)
5456 continue;
5458 /* If the text ``under'' the overlay is invisible, it has a zero
5459 dimension, and both before- and after-strings apply. */
5460 invisible = Foverlay_get (overlay, Qinvisible);
5461 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5463 /* If overlay has a non-empty before-string, record it. */
5464 if ((start == charpos || (end == charpos && invis_p))
5465 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5466 && SCHARS (str))
5467 RECORD_OVERLAY_STRING (overlay, str, 0);
5469 /* If overlay has a non-empty after-string, record it. */
5470 if ((end == charpos || (start == charpos && invis_p))
5471 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5472 && SCHARS (str))
5473 RECORD_OVERLAY_STRING (overlay, str, 1);
5476 #undef RECORD_OVERLAY_STRING
5478 /* Sort entries. */
5479 if (n > 1)
5480 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5482 /* Record number of overlay strings, and where we computed it. */
5483 it->n_overlay_strings = n;
5484 it->overlay_strings_charpos = charpos;
5486 /* IT->current.overlay_string_index is the number of overlay strings
5487 that have already been consumed by IT. Copy some of the
5488 remaining overlay strings to IT->overlay_strings. */
5489 i = 0;
5490 j = it->current.overlay_string_index;
5491 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5493 it->overlay_strings[i] = entries[j].string;
5494 it->string_overlays[i++] = entries[j++].overlay;
5497 CHECK_IT (it);
5501 /* Get the first chunk of overlay strings at IT's current buffer
5502 position, or at CHARPOS if that is > 0. Value is non-zero if at
5503 least one overlay string was found. */
5505 static int
5506 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
5508 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5509 process. This fills IT->overlay_strings with strings, and sets
5510 IT->n_overlay_strings to the total number of strings to process.
5511 IT->pos.overlay_string_index has to be set temporarily to zero
5512 because load_overlay_strings needs this; it must be set to -1
5513 when no overlay strings are found because a zero value would
5514 indicate a position in the first overlay string. */
5515 it->current.overlay_string_index = 0;
5516 load_overlay_strings (it, charpos);
5518 /* If we found overlay strings, set up IT to deliver display
5519 elements from the first one. Otherwise set up IT to deliver
5520 from current_buffer. */
5521 if (it->n_overlay_strings)
5523 /* Make sure we know settings in current_buffer, so that we can
5524 restore meaningful values when we're done with the overlay
5525 strings. */
5526 if (compute_stop_p)
5527 compute_stop_pos (it);
5528 xassert (it->face_id >= 0);
5530 /* Save IT's settings. They are restored after all overlay
5531 strings have been processed. */
5532 xassert (!compute_stop_p || it->sp == 0);
5534 /* When called from handle_stop, there might be an empty display
5535 string loaded. In that case, don't bother saving it. But
5536 don't use this optimization with the bidi iterator, since we
5537 need the corresponding pop_it call to resync the bidi
5538 iterator's position with IT's position, after we are done
5539 with the overlay strings. (The corresponding call to pop_it
5540 in case of an empty display string is in
5541 next_overlay_string.) */
5542 if (!(!it->bidi_p
5543 && STRINGP (it->string) && !SCHARS (it->string)))
5544 push_it (it, NULL);
5546 /* Set up IT to deliver display elements from the first overlay
5547 string. */
5548 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5549 it->string = it->overlay_strings[0];
5550 it->from_overlay = Qnil;
5551 it->stop_charpos = 0;
5552 xassert (STRINGP (it->string));
5553 it->end_charpos = SCHARS (it->string);
5554 it->prev_stop = 0;
5555 it->base_level_stop = 0;
5556 it->multibyte_p = STRING_MULTIBYTE (it->string);
5557 it->method = GET_FROM_STRING;
5558 it->from_disp_prop_p = 0;
5560 /* Force paragraph direction to be that of the parent
5561 buffer. */
5562 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5563 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5564 else
5565 it->paragraph_embedding = L2R;
5567 /* Set up the bidi iterator for this overlay string. */
5568 if (it->bidi_p)
5570 EMACS_INT pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5572 it->bidi_it.string.lstring = it->string;
5573 it->bidi_it.string.s = NULL;
5574 it->bidi_it.string.schars = SCHARS (it->string);
5575 it->bidi_it.string.bufpos = pos;
5576 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5577 it->bidi_it.string.unibyte = !it->multibyte_p;
5578 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5580 return 1;
5583 it->current.overlay_string_index = -1;
5584 return 0;
5587 static int
5588 get_overlay_strings (struct it *it, EMACS_INT charpos)
5590 it->string = Qnil;
5591 it->method = GET_FROM_BUFFER;
5593 (void) get_overlay_strings_1 (it, charpos, 1);
5595 CHECK_IT (it);
5597 /* Value is non-zero if we found at least one overlay string. */
5598 return STRINGP (it->string);
5603 /***********************************************************************
5604 Saving and restoring state
5605 ***********************************************************************/
5607 /* Save current settings of IT on IT->stack. Called, for example,
5608 before setting up IT for an overlay string, to be able to restore
5609 IT's settings to what they were after the overlay string has been
5610 processed. If POSITION is non-NULL, it is the position to save on
5611 the stack instead of IT->position. */
5613 static void
5614 push_it (struct it *it, struct text_pos *position)
5616 struct iterator_stack_entry *p;
5618 xassert (it->sp < IT_STACK_SIZE);
5619 p = it->stack + it->sp;
5621 p->stop_charpos = it->stop_charpos;
5622 p->prev_stop = it->prev_stop;
5623 p->base_level_stop = it->base_level_stop;
5624 p->cmp_it = it->cmp_it;
5625 xassert (it->face_id >= 0);
5626 p->face_id = it->face_id;
5627 p->string = it->string;
5628 p->method = it->method;
5629 p->from_overlay = it->from_overlay;
5630 switch (p->method)
5632 case GET_FROM_IMAGE:
5633 p->u.image.object = it->object;
5634 p->u.image.image_id = it->image_id;
5635 p->u.image.slice = it->slice;
5636 break;
5637 case GET_FROM_STRETCH:
5638 p->u.stretch.object = it->object;
5639 break;
5641 p->position = position ? *position : it->position;
5642 p->current = it->current;
5643 p->end_charpos = it->end_charpos;
5644 p->string_nchars = it->string_nchars;
5645 p->area = it->area;
5646 p->multibyte_p = it->multibyte_p;
5647 p->avoid_cursor_p = it->avoid_cursor_p;
5648 p->space_width = it->space_width;
5649 p->font_height = it->font_height;
5650 p->voffset = it->voffset;
5651 p->string_from_display_prop_p = it->string_from_display_prop_p;
5652 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5653 p->display_ellipsis_p = 0;
5654 p->line_wrap = it->line_wrap;
5655 p->bidi_p = it->bidi_p;
5656 p->paragraph_embedding = it->paragraph_embedding;
5657 p->from_disp_prop_p = it->from_disp_prop_p;
5658 ++it->sp;
5660 /* Save the state of the bidi iterator as well. */
5661 if (it->bidi_p)
5662 bidi_push_it (&it->bidi_it);
5665 static void
5666 iterate_out_of_display_property (struct it *it)
5668 int buffer_p = !STRINGP (it->string);
5669 EMACS_INT eob = (buffer_p ? ZV : it->end_charpos);
5670 EMACS_INT bob = (buffer_p ? BEGV : 0);
5672 xassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5674 /* Maybe initialize paragraph direction. If we are at the beginning
5675 of a new paragraph, next_element_from_buffer may not have a
5676 chance to do that. */
5677 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5678 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5679 /* prev_stop can be zero, so check against BEGV as well. */
5680 while (it->bidi_it.charpos >= bob
5681 && it->prev_stop <= it->bidi_it.charpos
5682 && it->bidi_it.charpos < CHARPOS (it->position)
5683 && it->bidi_it.charpos < eob)
5684 bidi_move_to_visually_next (&it->bidi_it);
5685 /* Record the stop_pos we just crossed, for when we cross it
5686 back, maybe. */
5687 if (it->bidi_it.charpos > CHARPOS (it->position))
5688 it->prev_stop = CHARPOS (it->position);
5689 /* If we ended up not where pop_it put us, resync IT's
5690 positional members with the bidi iterator. */
5691 if (it->bidi_it.charpos != CHARPOS (it->position))
5692 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5693 if (buffer_p)
5694 it->current.pos = it->position;
5695 else
5696 it->current.string_pos = it->position;
5699 /* Restore IT's settings from IT->stack. Called, for example, when no
5700 more overlay strings must be processed, and we return to delivering
5701 display elements from a buffer, or when the end of a string from a
5702 `display' property is reached and we return to delivering display
5703 elements from an overlay string, or from a buffer. */
5705 static void
5706 pop_it (struct it *it)
5708 struct iterator_stack_entry *p;
5709 int from_display_prop = it->from_disp_prop_p;
5711 xassert (it->sp > 0);
5712 --it->sp;
5713 p = it->stack + it->sp;
5714 it->stop_charpos = p->stop_charpos;
5715 it->prev_stop = p->prev_stop;
5716 it->base_level_stop = p->base_level_stop;
5717 it->cmp_it = p->cmp_it;
5718 it->face_id = p->face_id;
5719 it->current = p->current;
5720 it->position = p->position;
5721 it->string = p->string;
5722 it->from_overlay = p->from_overlay;
5723 if (NILP (it->string))
5724 SET_TEXT_POS (it->current.string_pos, -1, -1);
5725 it->method = p->method;
5726 switch (it->method)
5728 case GET_FROM_IMAGE:
5729 it->image_id = p->u.image.image_id;
5730 it->object = p->u.image.object;
5731 it->slice = p->u.image.slice;
5732 break;
5733 case GET_FROM_STRETCH:
5734 it->object = p->u.stretch.object;
5735 break;
5736 case GET_FROM_BUFFER:
5737 it->object = it->w->buffer;
5738 break;
5739 case GET_FROM_STRING:
5740 it->object = it->string;
5741 break;
5742 case GET_FROM_DISPLAY_VECTOR:
5743 if (it->s)
5744 it->method = GET_FROM_C_STRING;
5745 else if (STRINGP (it->string))
5746 it->method = GET_FROM_STRING;
5747 else
5749 it->method = GET_FROM_BUFFER;
5750 it->object = it->w->buffer;
5753 it->end_charpos = p->end_charpos;
5754 it->string_nchars = p->string_nchars;
5755 it->area = p->area;
5756 it->multibyte_p = p->multibyte_p;
5757 it->avoid_cursor_p = p->avoid_cursor_p;
5758 it->space_width = p->space_width;
5759 it->font_height = p->font_height;
5760 it->voffset = p->voffset;
5761 it->string_from_display_prop_p = p->string_from_display_prop_p;
5762 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5763 it->line_wrap = p->line_wrap;
5764 it->bidi_p = p->bidi_p;
5765 it->paragraph_embedding = p->paragraph_embedding;
5766 it->from_disp_prop_p = p->from_disp_prop_p;
5767 if (it->bidi_p)
5769 bidi_pop_it (&it->bidi_it);
5770 /* Bidi-iterate until we get out of the portion of text, if any,
5771 covered by a `display' text property or by an overlay with
5772 `display' property. (We cannot just jump there, because the
5773 internal coherency of the bidi iterator state can not be
5774 preserved across such jumps.) We also must determine the
5775 paragraph base direction if the overlay we just processed is
5776 at the beginning of a new paragraph. */
5777 if (from_display_prop
5778 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5779 iterate_out_of_display_property (it);
5781 xassert ((BUFFERP (it->object)
5782 && IT_CHARPOS (*it) == it->bidi_it.charpos
5783 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5784 || (STRINGP (it->object)
5785 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5786 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5787 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5793 /***********************************************************************
5794 Moving over lines
5795 ***********************************************************************/
5797 /* Set IT's current position to the previous line start. */
5799 static void
5800 back_to_previous_line_start (struct it *it)
5802 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5803 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5807 /* Move IT to the next line start.
5809 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5810 we skipped over part of the text (as opposed to moving the iterator
5811 continuously over the text). Otherwise, don't change the value
5812 of *SKIPPED_P.
5814 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5815 iterator on the newline, if it was found.
5817 Newlines may come from buffer text, overlay strings, or strings
5818 displayed via the `display' property. That's the reason we can't
5819 simply use find_next_newline_no_quit.
5821 Note that this function may not skip over invisible text that is so
5822 because of text properties and immediately follows a newline. If
5823 it would, function reseat_at_next_visible_line_start, when called
5824 from set_iterator_to_next, would effectively make invisible
5825 characters following a newline part of the wrong glyph row, which
5826 leads to wrong cursor motion. */
5828 static int
5829 forward_to_next_line_start (struct it *it, int *skipped_p,
5830 struct bidi_it *bidi_it_prev)
5832 EMACS_INT old_selective;
5833 int newline_found_p, n;
5834 const int MAX_NEWLINE_DISTANCE = 500;
5836 /* If already on a newline, just consume it to avoid unintended
5837 skipping over invisible text below. */
5838 if (it->what == IT_CHARACTER
5839 && it->c == '\n'
5840 && CHARPOS (it->position) == IT_CHARPOS (*it))
5842 if (it->bidi_p && bidi_it_prev)
5843 *bidi_it_prev = it->bidi_it;
5844 set_iterator_to_next (it, 0);
5845 it->c = 0;
5846 return 1;
5849 /* Don't handle selective display in the following. It's (a)
5850 unnecessary because it's done by the caller, and (b) leads to an
5851 infinite recursion because next_element_from_ellipsis indirectly
5852 calls this function. */
5853 old_selective = it->selective;
5854 it->selective = 0;
5856 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5857 from buffer text. */
5858 for (n = newline_found_p = 0;
5859 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5860 n += STRINGP (it->string) ? 0 : 1)
5862 if (!get_next_display_element (it))
5863 return 0;
5864 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5865 if (newline_found_p && it->bidi_p && bidi_it_prev)
5866 *bidi_it_prev = it->bidi_it;
5867 set_iterator_to_next (it, 0);
5870 /* If we didn't find a newline near enough, see if we can use a
5871 short-cut. */
5872 if (!newline_found_p)
5874 EMACS_INT start = IT_CHARPOS (*it);
5875 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5876 Lisp_Object pos;
5878 xassert (!STRINGP (it->string));
5880 /* If there isn't any `display' property in sight, and no
5881 overlays, we can just use the position of the newline in
5882 buffer text. */
5883 if (it->stop_charpos >= limit
5884 || ((pos = Fnext_single_property_change (make_number (start),
5885 Qdisplay, Qnil,
5886 make_number (limit)),
5887 NILP (pos))
5888 && next_overlay_change (start) == ZV))
5890 if (!it->bidi_p)
5892 IT_CHARPOS (*it) = limit;
5893 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5895 else
5897 struct bidi_it bprev;
5899 /* Help bidi.c avoid expensive searches for display
5900 properties and overlays, by telling it that there are
5901 none up to `limit'. */
5902 if (it->bidi_it.disp_pos < limit)
5904 it->bidi_it.disp_pos = limit;
5905 it->bidi_it.disp_prop = 0;
5907 do {
5908 bprev = it->bidi_it;
5909 bidi_move_to_visually_next (&it->bidi_it);
5910 } while (it->bidi_it.charpos != limit);
5911 IT_CHARPOS (*it) = limit;
5912 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5913 if (bidi_it_prev)
5914 *bidi_it_prev = bprev;
5916 *skipped_p = newline_found_p = 1;
5918 else
5920 while (get_next_display_element (it)
5921 && !newline_found_p)
5923 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5924 if (newline_found_p && it->bidi_p && bidi_it_prev)
5925 *bidi_it_prev = it->bidi_it;
5926 set_iterator_to_next (it, 0);
5931 it->selective = old_selective;
5932 return newline_found_p;
5936 /* Set IT's current position to the previous visible line start. Skip
5937 invisible text that is so either due to text properties or due to
5938 selective display. Caution: this does not change IT->current_x and
5939 IT->hpos. */
5941 static void
5942 back_to_previous_visible_line_start (struct it *it)
5944 while (IT_CHARPOS (*it) > BEGV)
5946 back_to_previous_line_start (it);
5948 if (IT_CHARPOS (*it) <= BEGV)
5949 break;
5951 /* If selective > 0, then lines indented more than its value are
5952 invisible. */
5953 if (it->selective > 0
5954 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5955 it->selective))
5956 continue;
5958 /* Check the newline before point for invisibility. */
5960 Lisp_Object prop;
5961 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5962 Qinvisible, it->window);
5963 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5964 continue;
5967 if (IT_CHARPOS (*it) <= BEGV)
5968 break;
5971 struct it it2;
5972 void *it2data = NULL;
5973 EMACS_INT pos;
5974 EMACS_INT beg, end;
5975 Lisp_Object val, overlay;
5977 SAVE_IT (it2, *it, it2data);
5979 /* If newline is part of a composition, continue from start of composition */
5980 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5981 && beg < IT_CHARPOS (*it))
5982 goto replaced;
5984 /* If newline is replaced by a display property, find start of overlay
5985 or interval and continue search from that point. */
5986 pos = --IT_CHARPOS (it2);
5987 --IT_BYTEPOS (it2);
5988 it2.sp = 0;
5989 bidi_unshelve_cache (NULL, 0);
5990 it2.string_from_display_prop_p = 0;
5991 it2.from_disp_prop_p = 0;
5992 if (handle_display_prop (&it2) == HANDLED_RETURN
5993 && !NILP (val = get_char_property_and_overlay
5994 (make_number (pos), Qdisplay, Qnil, &overlay))
5995 && (OVERLAYP (overlay)
5996 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5997 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5999 RESTORE_IT (it, it, it2data);
6000 goto replaced;
6003 /* Newline is not replaced by anything -- so we are done. */
6004 RESTORE_IT (it, it, it2data);
6005 break;
6007 replaced:
6008 if (beg < BEGV)
6009 beg = BEGV;
6010 IT_CHARPOS (*it) = beg;
6011 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6015 it->continuation_lines_width = 0;
6017 xassert (IT_CHARPOS (*it) >= BEGV);
6018 xassert (IT_CHARPOS (*it) == BEGV
6019 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6020 CHECK_IT (it);
6024 /* Reseat iterator IT at the previous visible line start. Skip
6025 invisible text that is so either due to text properties or due to
6026 selective display. At the end, update IT's overlay information,
6027 face information etc. */
6029 void
6030 reseat_at_previous_visible_line_start (struct it *it)
6032 back_to_previous_visible_line_start (it);
6033 reseat (it, it->current.pos, 1);
6034 CHECK_IT (it);
6038 /* Reseat iterator IT on the next visible line start in the current
6039 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6040 preceding the line start. Skip over invisible text that is so
6041 because of selective display. Compute faces, overlays etc at the
6042 new position. Note that this function does not skip over text that
6043 is invisible because of text properties. */
6045 static void
6046 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6048 int newline_found_p, skipped_p = 0;
6049 struct bidi_it bidi_it_prev;
6051 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6053 /* Skip over lines that are invisible because they are indented
6054 more than the value of IT->selective. */
6055 if (it->selective > 0)
6056 while (IT_CHARPOS (*it) < ZV
6057 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6058 it->selective))
6060 xassert (IT_BYTEPOS (*it) == BEGV
6061 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6062 newline_found_p =
6063 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6066 /* Position on the newline if that's what's requested. */
6067 if (on_newline_p && newline_found_p)
6069 if (STRINGP (it->string))
6071 if (IT_STRING_CHARPOS (*it) > 0)
6073 if (!it->bidi_p)
6075 --IT_STRING_CHARPOS (*it);
6076 --IT_STRING_BYTEPOS (*it);
6078 else
6080 /* We need to restore the bidi iterator to the state
6081 it had on the newline, and resync the IT's
6082 position with that. */
6083 it->bidi_it = bidi_it_prev;
6084 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6085 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6089 else if (IT_CHARPOS (*it) > BEGV)
6091 if (!it->bidi_p)
6093 --IT_CHARPOS (*it);
6094 --IT_BYTEPOS (*it);
6096 else
6098 /* We need to restore the bidi iterator to the state it
6099 had on the newline and resync IT with that. */
6100 it->bidi_it = bidi_it_prev;
6101 IT_CHARPOS (*it) = it->bidi_it.charpos;
6102 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6104 reseat (it, it->current.pos, 0);
6107 else if (skipped_p)
6108 reseat (it, it->current.pos, 0);
6110 CHECK_IT (it);
6115 /***********************************************************************
6116 Changing an iterator's position
6117 ***********************************************************************/
6119 /* Change IT's current position to POS in current_buffer. If FORCE_P
6120 is non-zero, always check for text properties at the new position.
6121 Otherwise, text properties are only looked up if POS >=
6122 IT->check_charpos of a property. */
6124 static void
6125 reseat (struct it *it, struct text_pos pos, int force_p)
6127 EMACS_INT original_pos = IT_CHARPOS (*it);
6129 reseat_1 (it, pos, 0);
6131 /* Determine where to check text properties. Avoid doing it
6132 where possible because text property lookup is very expensive. */
6133 if (force_p
6134 || CHARPOS (pos) > it->stop_charpos
6135 || CHARPOS (pos) < original_pos)
6137 if (it->bidi_p)
6139 /* For bidi iteration, we need to prime prev_stop and
6140 base_level_stop with our best estimations. */
6141 /* Implementation note: Of course, POS is not necessarily a
6142 stop position, so assigning prev_pos to it is a lie; we
6143 should have called compute_stop_backwards. However, if
6144 the current buffer does not include any R2L characters,
6145 that call would be a waste of cycles, because the
6146 iterator will never move back, and thus never cross this
6147 "fake" stop position. So we delay that backward search
6148 until the time we really need it, in next_element_from_buffer. */
6149 if (CHARPOS (pos) != it->prev_stop)
6150 it->prev_stop = CHARPOS (pos);
6151 if (CHARPOS (pos) < it->base_level_stop)
6152 it->base_level_stop = 0; /* meaning it's unknown */
6153 handle_stop (it);
6155 else
6157 handle_stop (it);
6158 it->prev_stop = it->base_level_stop = 0;
6163 CHECK_IT (it);
6167 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6168 IT->stop_pos to POS, also. */
6170 static void
6171 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6173 /* Don't call this function when scanning a C string. */
6174 xassert (it->s == NULL);
6176 /* POS must be a reasonable value. */
6177 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6179 it->current.pos = it->position = pos;
6180 it->end_charpos = ZV;
6181 it->dpvec = NULL;
6182 it->current.dpvec_index = -1;
6183 it->current.overlay_string_index = -1;
6184 IT_STRING_CHARPOS (*it) = -1;
6185 IT_STRING_BYTEPOS (*it) = -1;
6186 it->string = Qnil;
6187 it->method = GET_FROM_BUFFER;
6188 it->object = it->w->buffer;
6189 it->area = TEXT_AREA;
6190 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6191 it->sp = 0;
6192 it->string_from_display_prop_p = 0;
6193 it->string_from_prefix_prop_p = 0;
6195 it->from_disp_prop_p = 0;
6196 it->face_before_selective_p = 0;
6197 if (it->bidi_p)
6199 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6200 &it->bidi_it);
6201 bidi_unshelve_cache (NULL, 0);
6202 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6203 it->bidi_it.string.s = NULL;
6204 it->bidi_it.string.lstring = Qnil;
6205 it->bidi_it.string.bufpos = 0;
6206 it->bidi_it.string.unibyte = 0;
6209 if (set_stop_p)
6211 it->stop_charpos = CHARPOS (pos);
6212 it->base_level_stop = CHARPOS (pos);
6217 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6218 If S is non-null, it is a C string to iterate over. Otherwise,
6219 STRING gives a Lisp string to iterate over.
6221 If PRECISION > 0, don't return more then PRECISION number of
6222 characters from the string.
6224 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6225 characters have been returned. FIELD_WIDTH < 0 means an infinite
6226 field width.
6228 MULTIBYTE = 0 means disable processing of multibyte characters,
6229 MULTIBYTE > 0 means enable it,
6230 MULTIBYTE < 0 means use IT->multibyte_p.
6232 IT must be initialized via a prior call to init_iterator before
6233 calling this function. */
6235 static void
6236 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6237 EMACS_INT charpos, EMACS_INT precision, int field_width,
6238 int multibyte)
6240 /* No region in strings. */
6241 it->region_beg_charpos = it->region_end_charpos = -1;
6243 /* No text property checks performed by default, but see below. */
6244 it->stop_charpos = -1;
6246 /* Set iterator position and end position. */
6247 memset (&it->current, 0, sizeof it->current);
6248 it->current.overlay_string_index = -1;
6249 it->current.dpvec_index = -1;
6250 xassert (charpos >= 0);
6252 /* If STRING is specified, use its multibyteness, otherwise use the
6253 setting of MULTIBYTE, if specified. */
6254 if (multibyte >= 0)
6255 it->multibyte_p = multibyte > 0;
6257 /* Bidirectional reordering of strings is controlled by the default
6258 value of bidi-display-reordering. Don't try to reorder while
6259 loading loadup.el, as the necessary character property tables are
6260 not yet available. */
6261 it->bidi_p =
6262 NILP (Vpurify_flag)
6263 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6265 if (s == NULL)
6267 xassert (STRINGP (string));
6268 it->string = string;
6269 it->s = NULL;
6270 it->end_charpos = it->string_nchars = SCHARS (string);
6271 it->method = GET_FROM_STRING;
6272 it->current.string_pos = string_pos (charpos, string);
6274 if (it->bidi_p)
6276 it->bidi_it.string.lstring = string;
6277 it->bidi_it.string.s = NULL;
6278 it->bidi_it.string.schars = it->end_charpos;
6279 it->bidi_it.string.bufpos = 0;
6280 it->bidi_it.string.from_disp_str = 0;
6281 it->bidi_it.string.unibyte = !it->multibyte_p;
6282 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6283 FRAME_WINDOW_P (it->f), &it->bidi_it);
6286 else
6288 it->s = (const unsigned char *) s;
6289 it->string = Qnil;
6291 /* Note that we use IT->current.pos, not it->current.string_pos,
6292 for displaying C strings. */
6293 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6294 if (it->multibyte_p)
6296 it->current.pos = c_string_pos (charpos, s, 1);
6297 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6299 else
6301 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6302 it->end_charpos = it->string_nchars = strlen (s);
6305 if (it->bidi_p)
6307 it->bidi_it.string.lstring = Qnil;
6308 it->bidi_it.string.s = (const unsigned char *) s;
6309 it->bidi_it.string.schars = it->end_charpos;
6310 it->bidi_it.string.bufpos = 0;
6311 it->bidi_it.string.from_disp_str = 0;
6312 it->bidi_it.string.unibyte = !it->multibyte_p;
6313 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6314 &it->bidi_it);
6316 it->method = GET_FROM_C_STRING;
6319 /* PRECISION > 0 means don't return more than PRECISION characters
6320 from the string. */
6321 if (precision > 0 && it->end_charpos - charpos > precision)
6323 it->end_charpos = it->string_nchars = charpos + precision;
6324 if (it->bidi_p)
6325 it->bidi_it.string.schars = it->end_charpos;
6328 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6329 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6330 FIELD_WIDTH < 0 means infinite field width. This is useful for
6331 padding with `-' at the end of a mode line. */
6332 if (field_width < 0)
6333 field_width = INFINITY;
6334 /* Implementation note: We deliberately don't enlarge
6335 it->bidi_it.string.schars here to fit it->end_charpos, because
6336 the bidi iterator cannot produce characters out of thin air. */
6337 if (field_width > it->end_charpos - charpos)
6338 it->end_charpos = charpos + field_width;
6340 /* Use the standard display table for displaying strings. */
6341 if (DISP_TABLE_P (Vstandard_display_table))
6342 it->dp = XCHAR_TABLE (Vstandard_display_table);
6344 it->stop_charpos = charpos;
6345 it->prev_stop = charpos;
6346 it->base_level_stop = 0;
6347 if (it->bidi_p)
6349 it->bidi_it.first_elt = 1;
6350 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6351 it->bidi_it.disp_pos = -1;
6353 if (s == NULL && it->multibyte_p)
6355 EMACS_INT endpos = SCHARS (it->string);
6356 if (endpos > it->end_charpos)
6357 endpos = it->end_charpos;
6358 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6359 it->string);
6361 CHECK_IT (it);
6366 /***********************************************************************
6367 Iteration
6368 ***********************************************************************/
6370 /* Map enum it_method value to corresponding next_element_from_* function. */
6372 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6374 next_element_from_buffer,
6375 next_element_from_display_vector,
6376 next_element_from_string,
6377 next_element_from_c_string,
6378 next_element_from_image,
6379 next_element_from_stretch
6382 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6385 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6386 (possibly with the following characters). */
6388 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6389 ((IT)->cmp_it.id >= 0 \
6390 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6391 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6392 END_CHARPOS, (IT)->w, \
6393 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6394 (IT)->string)))
6397 /* Lookup the char-table Vglyphless_char_display for character C (-1
6398 if we want information for no-font case), and return the display
6399 method symbol. By side-effect, update it->what and
6400 it->glyphless_method. This function is called from
6401 get_next_display_element for each character element, and from
6402 x_produce_glyphs when no suitable font was found. */
6404 Lisp_Object
6405 lookup_glyphless_char_display (int c, struct it *it)
6407 Lisp_Object glyphless_method = Qnil;
6409 if (CHAR_TABLE_P (Vglyphless_char_display)
6410 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6412 if (c >= 0)
6414 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6415 if (CONSP (glyphless_method))
6416 glyphless_method = FRAME_WINDOW_P (it->f)
6417 ? XCAR (glyphless_method)
6418 : XCDR (glyphless_method);
6420 else
6421 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6424 retry:
6425 if (NILP (glyphless_method))
6427 if (c >= 0)
6428 /* The default is to display the character by a proper font. */
6429 return Qnil;
6430 /* The default for the no-font case is to display an empty box. */
6431 glyphless_method = Qempty_box;
6433 if (EQ (glyphless_method, Qzero_width))
6435 if (c >= 0)
6436 return glyphless_method;
6437 /* This method can't be used for the no-font case. */
6438 glyphless_method = Qempty_box;
6440 if (EQ (glyphless_method, Qthin_space))
6441 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6442 else if (EQ (glyphless_method, Qempty_box))
6443 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6444 else if (EQ (glyphless_method, Qhex_code))
6445 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6446 else if (STRINGP (glyphless_method))
6447 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6448 else
6450 /* Invalid value. We use the default method. */
6451 glyphless_method = Qnil;
6452 goto retry;
6454 it->what = IT_GLYPHLESS;
6455 return glyphless_method;
6458 /* Load IT's display element fields with information about the next
6459 display element from the current position of IT. Value is zero if
6460 end of buffer (or C string) is reached. */
6462 static struct frame *last_escape_glyph_frame = NULL;
6463 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6464 static int last_escape_glyph_merged_face_id = 0;
6466 struct frame *last_glyphless_glyph_frame = NULL;
6467 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6468 int last_glyphless_glyph_merged_face_id = 0;
6470 static int
6471 get_next_display_element (struct it *it)
6473 /* Non-zero means that we found a display element. Zero means that
6474 we hit the end of what we iterate over. Performance note: the
6475 function pointer `method' used here turns out to be faster than
6476 using a sequence of if-statements. */
6477 int success_p;
6479 get_next:
6480 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6482 if (it->what == IT_CHARACTER)
6484 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6485 and only if (a) the resolved directionality of that character
6486 is R..." */
6487 /* FIXME: Do we need an exception for characters from display
6488 tables? */
6489 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6490 it->c = bidi_mirror_char (it->c);
6491 /* Map via display table or translate control characters.
6492 IT->c, IT->len etc. have been set to the next character by
6493 the function call above. If we have a display table, and it
6494 contains an entry for IT->c, translate it. Don't do this if
6495 IT->c itself comes from a display table, otherwise we could
6496 end up in an infinite recursion. (An alternative could be to
6497 count the recursion depth of this function and signal an
6498 error when a certain maximum depth is reached.) Is it worth
6499 it? */
6500 if (success_p && it->dpvec == NULL)
6502 Lisp_Object dv;
6503 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6504 int nonascii_space_p = 0;
6505 int nonascii_hyphen_p = 0;
6506 int c = it->c; /* This is the character to display. */
6508 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6510 xassert (SINGLE_BYTE_CHAR_P (c));
6511 if (unibyte_display_via_language_environment)
6513 c = DECODE_CHAR (unibyte, c);
6514 if (c < 0)
6515 c = BYTE8_TO_CHAR (it->c);
6517 else
6518 c = BYTE8_TO_CHAR (it->c);
6521 if (it->dp
6522 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6523 VECTORP (dv)))
6525 struct Lisp_Vector *v = XVECTOR (dv);
6527 /* Return the first character from the display table
6528 entry, if not empty. If empty, don't display the
6529 current character. */
6530 if (v->header.size)
6532 it->dpvec_char_len = it->len;
6533 it->dpvec = v->contents;
6534 it->dpend = v->contents + v->header.size;
6535 it->current.dpvec_index = 0;
6536 it->dpvec_face_id = -1;
6537 it->saved_face_id = it->face_id;
6538 it->method = GET_FROM_DISPLAY_VECTOR;
6539 it->ellipsis_p = 0;
6541 else
6543 set_iterator_to_next (it, 0);
6545 goto get_next;
6548 if (! NILP (lookup_glyphless_char_display (c, it)))
6550 if (it->what == IT_GLYPHLESS)
6551 goto done;
6552 /* Don't display this character. */
6553 set_iterator_to_next (it, 0);
6554 goto get_next;
6557 /* If `nobreak-char-display' is non-nil, we display
6558 non-ASCII spaces and hyphens specially. */
6559 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6561 if (c == 0xA0)
6562 nonascii_space_p = 1;
6563 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6564 nonascii_hyphen_p = 1;
6567 /* Translate control characters into `\003' or `^C' form.
6568 Control characters coming from a display table entry are
6569 currently not translated because we use IT->dpvec to hold
6570 the translation. This could easily be changed but I
6571 don't believe that it is worth doing.
6573 The characters handled by `nobreak-char-display' must be
6574 translated too.
6576 Non-printable characters and raw-byte characters are also
6577 translated to octal form. */
6578 if (((c < ' ' || c == 127) /* ASCII control chars */
6579 ? (it->area != TEXT_AREA
6580 /* In mode line, treat \n, \t like other crl chars. */
6581 || (c != '\t'
6582 && it->glyph_row
6583 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6584 || (c != '\n' && c != '\t'))
6585 : (nonascii_space_p
6586 || nonascii_hyphen_p
6587 || CHAR_BYTE8_P (c)
6588 || ! CHAR_PRINTABLE_P (c))))
6590 /* C is a control character, non-ASCII space/hyphen,
6591 raw-byte, or a non-printable character which must be
6592 displayed either as '\003' or as `^C' where the '\\'
6593 and '^' can be defined in the display table. Fill
6594 IT->ctl_chars with glyphs for what we have to
6595 display. Then, set IT->dpvec to these glyphs. */
6596 Lisp_Object gc;
6597 int ctl_len;
6598 int face_id;
6599 EMACS_INT lface_id = 0;
6600 int escape_glyph;
6602 /* Handle control characters with ^. */
6604 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6606 int g;
6608 g = '^'; /* default glyph for Control */
6609 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6610 if (it->dp
6611 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
6612 && GLYPH_CODE_CHAR_VALID_P (gc))
6614 g = GLYPH_CODE_CHAR (gc);
6615 lface_id = GLYPH_CODE_FACE (gc);
6617 if (lface_id)
6619 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6621 else if (it->f == last_escape_glyph_frame
6622 && it->face_id == last_escape_glyph_face_id)
6624 face_id = last_escape_glyph_merged_face_id;
6626 else
6628 /* Merge the escape-glyph face into the current face. */
6629 face_id = merge_faces (it->f, Qescape_glyph, 0,
6630 it->face_id);
6631 last_escape_glyph_frame = it->f;
6632 last_escape_glyph_face_id = it->face_id;
6633 last_escape_glyph_merged_face_id = face_id;
6636 XSETINT (it->ctl_chars[0], g);
6637 XSETINT (it->ctl_chars[1], c ^ 0100);
6638 ctl_len = 2;
6639 goto display_control;
6642 /* Handle non-ascii space in the mode where it only gets
6643 highlighting. */
6645 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6647 /* Merge `nobreak-space' into the current face. */
6648 face_id = merge_faces (it->f, Qnobreak_space, 0,
6649 it->face_id);
6650 XSETINT (it->ctl_chars[0], ' ');
6651 ctl_len = 1;
6652 goto display_control;
6655 /* Handle sequences that start with the "escape glyph". */
6657 /* the default escape glyph is \. */
6658 escape_glyph = '\\';
6660 if (it->dp
6661 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
6662 && GLYPH_CODE_CHAR_VALID_P (gc))
6664 escape_glyph = GLYPH_CODE_CHAR (gc);
6665 lface_id = GLYPH_CODE_FACE (gc);
6667 if (lface_id)
6669 /* The display table specified a face.
6670 Merge it into face_id and also into escape_glyph. */
6671 face_id = merge_faces (it->f, Qt, lface_id,
6672 it->face_id);
6674 else if (it->f == last_escape_glyph_frame
6675 && it->face_id == last_escape_glyph_face_id)
6677 face_id = last_escape_glyph_merged_face_id;
6679 else
6681 /* Merge the escape-glyph face into the current face. */
6682 face_id = merge_faces (it->f, Qescape_glyph, 0,
6683 it->face_id);
6684 last_escape_glyph_frame = it->f;
6685 last_escape_glyph_face_id = it->face_id;
6686 last_escape_glyph_merged_face_id = face_id;
6689 /* Draw non-ASCII hyphen with just highlighting: */
6691 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6693 XSETINT (it->ctl_chars[0], '-');
6694 ctl_len = 1;
6695 goto display_control;
6698 /* Draw non-ASCII space/hyphen with escape glyph: */
6700 if (nonascii_space_p || nonascii_hyphen_p)
6702 XSETINT (it->ctl_chars[0], escape_glyph);
6703 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6704 ctl_len = 2;
6705 goto display_control;
6709 char str[10];
6710 int len, i;
6712 if (CHAR_BYTE8_P (c))
6713 /* Display \200 instead of \17777600. */
6714 c = CHAR_TO_BYTE8 (c);
6715 len = sprintf (str, "%03o", c);
6717 XSETINT (it->ctl_chars[0], escape_glyph);
6718 for (i = 0; i < len; i++)
6719 XSETINT (it->ctl_chars[i + 1], str[i]);
6720 ctl_len = len + 1;
6723 display_control:
6724 /* Set up IT->dpvec and return first character from it. */
6725 it->dpvec_char_len = it->len;
6726 it->dpvec = it->ctl_chars;
6727 it->dpend = it->dpvec + ctl_len;
6728 it->current.dpvec_index = 0;
6729 it->dpvec_face_id = face_id;
6730 it->saved_face_id = it->face_id;
6731 it->method = GET_FROM_DISPLAY_VECTOR;
6732 it->ellipsis_p = 0;
6733 goto get_next;
6735 it->char_to_display = c;
6737 else if (success_p)
6739 it->char_to_display = it->c;
6743 /* Adjust face id for a multibyte character. There are no multibyte
6744 character in unibyte text. */
6745 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6746 && it->multibyte_p
6747 && success_p
6748 && FRAME_WINDOW_P (it->f))
6750 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6752 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6754 /* Automatic composition with glyph-string. */
6755 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6757 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6759 else
6761 EMACS_INT pos = (it->s ? -1
6762 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6763 : IT_CHARPOS (*it));
6764 int c;
6766 if (it->what == IT_CHARACTER)
6767 c = it->char_to_display;
6768 else
6770 struct composition *cmp = composition_table[it->cmp_it.id];
6771 int i;
6773 c = ' ';
6774 for (i = 0; i < cmp->glyph_len; i++)
6775 /* TAB in a composition means display glyphs with
6776 padding space on the left or right. */
6777 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6778 break;
6780 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6784 done:
6785 /* Is this character the last one of a run of characters with
6786 box? If yes, set IT->end_of_box_run_p to 1. */
6787 if (it->face_box_p
6788 && it->s == NULL)
6790 if (it->method == GET_FROM_STRING && it->sp)
6792 int face_id = underlying_face_id (it);
6793 struct face *face = FACE_FROM_ID (it->f, face_id);
6795 if (face)
6797 if (face->box == FACE_NO_BOX)
6799 /* If the box comes from face properties in a
6800 display string, check faces in that string. */
6801 int string_face_id = face_after_it_pos (it);
6802 it->end_of_box_run_p
6803 = (FACE_FROM_ID (it->f, string_face_id)->box
6804 == FACE_NO_BOX);
6806 /* Otherwise, the box comes from the underlying face.
6807 If this is the last string character displayed, check
6808 the next buffer location. */
6809 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6810 && (it->current.overlay_string_index
6811 == it->n_overlay_strings - 1))
6813 EMACS_INT ignore;
6814 int next_face_id;
6815 struct text_pos pos = it->current.pos;
6816 INC_TEXT_POS (pos, it->multibyte_p);
6818 next_face_id = face_at_buffer_position
6819 (it->w, CHARPOS (pos), it->region_beg_charpos,
6820 it->region_end_charpos, &ignore,
6821 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6822 -1);
6823 it->end_of_box_run_p
6824 = (FACE_FROM_ID (it->f, next_face_id)->box
6825 == FACE_NO_BOX);
6829 else
6831 int face_id = face_after_it_pos (it);
6832 it->end_of_box_run_p
6833 = (face_id != it->face_id
6834 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6837 /* If we reached the end of the object we've been iterating (e.g., a
6838 display string or an overlay string), and there's something on
6839 IT->stack, proceed with what's on the stack. It doesn't make
6840 sense to return zero if there's unprocessed stuff on the stack,
6841 because otherwise that stuff will never be displayed. */
6842 if (!success_p && it->sp > 0)
6844 set_iterator_to_next (it, 0);
6845 success_p = get_next_display_element (it);
6848 /* Value is 0 if end of buffer or string reached. */
6849 return success_p;
6853 /* Move IT to the next display element.
6855 RESEAT_P non-zero means if called on a newline in buffer text,
6856 skip to the next visible line start.
6858 Functions get_next_display_element and set_iterator_to_next are
6859 separate because I find this arrangement easier to handle than a
6860 get_next_display_element function that also increments IT's
6861 position. The way it is we can first look at an iterator's current
6862 display element, decide whether it fits on a line, and if it does,
6863 increment the iterator position. The other way around we probably
6864 would either need a flag indicating whether the iterator has to be
6865 incremented the next time, or we would have to implement a
6866 decrement position function which would not be easy to write. */
6868 void
6869 set_iterator_to_next (struct it *it, int reseat_p)
6871 /* Reset flags indicating start and end of a sequence of characters
6872 with box. Reset them at the start of this function because
6873 moving the iterator to a new position might set them. */
6874 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6876 switch (it->method)
6878 case GET_FROM_BUFFER:
6879 /* The current display element of IT is a character from
6880 current_buffer. Advance in the buffer, and maybe skip over
6881 invisible lines that are so because of selective display. */
6882 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6883 reseat_at_next_visible_line_start (it, 0);
6884 else if (it->cmp_it.id >= 0)
6886 /* We are currently getting glyphs from a composition. */
6887 int i;
6889 if (! it->bidi_p)
6891 IT_CHARPOS (*it) += it->cmp_it.nchars;
6892 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6893 if (it->cmp_it.to < it->cmp_it.nglyphs)
6895 it->cmp_it.from = it->cmp_it.to;
6897 else
6899 it->cmp_it.id = -1;
6900 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6901 IT_BYTEPOS (*it),
6902 it->end_charpos, Qnil);
6905 else if (! it->cmp_it.reversed_p)
6907 /* Composition created while scanning forward. */
6908 /* Update IT's char/byte positions to point to the first
6909 character of the next grapheme cluster, or to the
6910 character visually after the current composition. */
6911 for (i = 0; i < it->cmp_it.nchars; i++)
6912 bidi_move_to_visually_next (&it->bidi_it);
6913 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6914 IT_CHARPOS (*it) = it->bidi_it.charpos;
6916 if (it->cmp_it.to < it->cmp_it.nglyphs)
6918 /* Proceed to the next grapheme cluster. */
6919 it->cmp_it.from = it->cmp_it.to;
6921 else
6923 /* No more grapheme clusters in this composition.
6924 Find the next stop position. */
6925 EMACS_INT stop = it->end_charpos;
6926 if (it->bidi_it.scan_dir < 0)
6927 /* Now we are scanning backward and don't know
6928 where to stop. */
6929 stop = -1;
6930 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6931 IT_BYTEPOS (*it), stop, Qnil);
6934 else
6936 /* Composition created while scanning backward. */
6937 /* Update IT's char/byte positions to point to the last
6938 character of the previous grapheme cluster, or the
6939 character visually after the current composition. */
6940 for (i = 0; i < it->cmp_it.nchars; i++)
6941 bidi_move_to_visually_next (&it->bidi_it);
6942 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6943 IT_CHARPOS (*it) = it->bidi_it.charpos;
6944 if (it->cmp_it.from > 0)
6946 /* Proceed to the previous grapheme cluster. */
6947 it->cmp_it.to = it->cmp_it.from;
6949 else
6951 /* No more grapheme clusters in this composition.
6952 Find the next stop position. */
6953 EMACS_INT stop = it->end_charpos;
6954 if (it->bidi_it.scan_dir < 0)
6955 /* Now we are scanning backward and don't know
6956 where to stop. */
6957 stop = -1;
6958 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6959 IT_BYTEPOS (*it), stop, Qnil);
6963 else
6965 xassert (it->len != 0);
6967 if (!it->bidi_p)
6969 IT_BYTEPOS (*it) += it->len;
6970 IT_CHARPOS (*it) += 1;
6972 else
6974 int prev_scan_dir = it->bidi_it.scan_dir;
6975 /* If this is a new paragraph, determine its base
6976 direction (a.k.a. its base embedding level). */
6977 if (it->bidi_it.new_paragraph)
6978 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6979 bidi_move_to_visually_next (&it->bidi_it);
6980 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6981 IT_CHARPOS (*it) = it->bidi_it.charpos;
6982 if (prev_scan_dir != it->bidi_it.scan_dir)
6984 /* As the scan direction was changed, we must
6985 re-compute the stop position for composition. */
6986 EMACS_INT stop = it->end_charpos;
6987 if (it->bidi_it.scan_dir < 0)
6988 stop = -1;
6989 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6990 IT_BYTEPOS (*it), stop, Qnil);
6993 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6995 break;
6997 case GET_FROM_C_STRING:
6998 /* Current display element of IT is from a C string. */
6999 if (!it->bidi_p
7000 /* If the string position is beyond string's end, it means
7001 next_element_from_c_string is padding the string with
7002 blanks, in which case we bypass the bidi iterator,
7003 because it cannot deal with such virtual characters. */
7004 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7006 IT_BYTEPOS (*it) += it->len;
7007 IT_CHARPOS (*it) += 1;
7009 else
7011 bidi_move_to_visually_next (&it->bidi_it);
7012 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7013 IT_CHARPOS (*it) = it->bidi_it.charpos;
7015 break;
7017 case GET_FROM_DISPLAY_VECTOR:
7018 /* Current display element of IT is from a display table entry.
7019 Advance in the display table definition. Reset it to null if
7020 end reached, and continue with characters from buffers/
7021 strings. */
7022 ++it->current.dpvec_index;
7024 /* Restore face of the iterator to what they were before the
7025 display vector entry (these entries may contain faces). */
7026 it->face_id = it->saved_face_id;
7028 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7030 int recheck_faces = it->ellipsis_p;
7032 if (it->s)
7033 it->method = GET_FROM_C_STRING;
7034 else if (STRINGP (it->string))
7035 it->method = GET_FROM_STRING;
7036 else
7038 it->method = GET_FROM_BUFFER;
7039 it->object = it->w->buffer;
7042 it->dpvec = NULL;
7043 it->current.dpvec_index = -1;
7045 /* Skip over characters which were displayed via IT->dpvec. */
7046 if (it->dpvec_char_len < 0)
7047 reseat_at_next_visible_line_start (it, 1);
7048 else if (it->dpvec_char_len > 0)
7050 if (it->method == GET_FROM_STRING
7051 && it->n_overlay_strings > 0)
7052 it->ignore_overlay_strings_at_pos_p = 1;
7053 it->len = it->dpvec_char_len;
7054 set_iterator_to_next (it, reseat_p);
7057 /* Maybe recheck faces after display vector */
7058 if (recheck_faces)
7059 it->stop_charpos = IT_CHARPOS (*it);
7061 break;
7063 case GET_FROM_STRING:
7064 /* Current display element is a character from a Lisp string. */
7065 xassert (it->s == NULL && STRINGP (it->string));
7066 /* Don't advance past string end. These conditions are true
7067 when set_iterator_to_next is called at the end of
7068 get_next_display_element, in which case the Lisp string is
7069 already exhausted, and all we want is pop the iterator
7070 stack. */
7071 if (it->current.overlay_string_index >= 0)
7073 /* This is an overlay string, so there's no padding with
7074 spaces, and the number of characters in the string is
7075 where the string ends. */
7076 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7077 goto consider_string_end;
7079 else
7081 /* Not an overlay string. There could be padding, so test
7082 against it->end_charpos . */
7083 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7084 goto consider_string_end;
7086 if (it->cmp_it.id >= 0)
7088 int i;
7090 if (! it->bidi_p)
7092 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7093 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7094 if (it->cmp_it.to < it->cmp_it.nglyphs)
7095 it->cmp_it.from = it->cmp_it.to;
7096 else
7098 it->cmp_it.id = -1;
7099 composition_compute_stop_pos (&it->cmp_it,
7100 IT_STRING_CHARPOS (*it),
7101 IT_STRING_BYTEPOS (*it),
7102 it->end_charpos, it->string);
7105 else if (! it->cmp_it.reversed_p)
7107 for (i = 0; i < it->cmp_it.nchars; i++)
7108 bidi_move_to_visually_next (&it->bidi_it);
7109 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7110 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7112 if (it->cmp_it.to < it->cmp_it.nglyphs)
7113 it->cmp_it.from = it->cmp_it.to;
7114 else
7116 EMACS_INT stop = it->end_charpos;
7117 if (it->bidi_it.scan_dir < 0)
7118 stop = -1;
7119 composition_compute_stop_pos (&it->cmp_it,
7120 IT_STRING_CHARPOS (*it),
7121 IT_STRING_BYTEPOS (*it), stop,
7122 it->string);
7125 else
7127 for (i = 0; i < it->cmp_it.nchars; i++)
7128 bidi_move_to_visually_next (&it->bidi_it);
7129 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7130 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7131 if (it->cmp_it.from > 0)
7132 it->cmp_it.to = it->cmp_it.from;
7133 else
7135 EMACS_INT stop = it->end_charpos;
7136 if (it->bidi_it.scan_dir < 0)
7137 stop = -1;
7138 composition_compute_stop_pos (&it->cmp_it,
7139 IT_STRING_CHARPOS (*it),
7140 IT_STRING_BYTEPOS (*it), stop,
7141 it->string);
7145 else
7147 if (!it->bidi_p
7148 /* If the string position is beyond string's end, it
7149 means next_element_from_string is padding the string
7150 with blanks, in which case we bypass the bidi
7151 iterator, because it cannot deal with such virtual
7152 characters. */
7153 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7155 IT_STRING_BYTEPOS (*it) += it->len;
7156 IT_STRING_CHARPOS (*it) += 1;
7158 else
7160 int prev_scan_dir = it->bidi_it.scan_dir;
7162 bidi_move_to_visually_next (&it->bidi_it);
7163 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7164 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7165 if (prev_scan_dir != it->bidi_it.scan_dir)
7167 EMACS_INT stop = it->end_charpos;
7169 if (it->bidi_it.scan_dir < 0)
7170 stop = -1;
7171 composition_compute_stop_pos (&it->cmp_it,
7172 IT_STRING_CHARPOS (*it),
7173 IT_STRING_BYTEPOS (*it), stop,
7174 it->string);
7179 consider_string_end:
7181 if (it->current.overlay_string_index >= 0)
7183 /* IT->string is an overlay string. Advance to the
7184 next, if there is one. */
7185 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7187 it->ellipsis_p = 0;
7188 next_overlay_string (it);
7189 if (it->ellipsis_p)
7190 setup_for_ellipsis (it, 0);
7193 else
7195 /* IT->string is not an overlay string. If we reached
7196 its end, and there is something on IT->stack, proceed
7197 with what is on the stack. This can be either another
7198 string, this time an overlay string, or a buffer. */
7199 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7200 && it->sp > 0)
7202 pop_it (it);
7203 if (it->method == GET_FROM_STRING)
7204 goto consider_string_end;
7207 break;
7209 case GET_FROM_IMAGE:
7210 case GET_FROM_STRETCH:
7211 /* The position etc with which we have to proceed are on
7212 the stack. The position may be at the end of a string,
7213 if the `display' property takes up the whole string. */
7214 xassert (it->sp > 0);
7215 pop_it (it);
7216 if (it->method == GET_FROM_STRING)
7217 goto consider_string_end;
7218 break;
7220 default:
7221 /* There are no other methods defined, so this should be a bug. */
7222 abort ();
7225 xassert (it->method != GET_FROM_STRING
7226 || (STRINGP (it->string)
7227 && IT_STRING_CHARPOS (*it) >= 0));
7230 /* Load IT's display element fields with information about the next
7231 display element which comes from a display table entry or from the
7232 result of translating a control character to one of the forms `^C'
7233 or `\003'.
7235 IT->dpvec holds the glyphs to return as characters.
7236 IT->saved_face_id holds the face id before the display vector--it
7237 is restored into IT->face_id in set_iterator_to_next. */
7239 static int
7240 next_element_from_display_vector (struct it *it)
7242 Lisp_Object gc;
7244 /* Precondition. */
7245 xassert (it->dpvec && it->current.dpvec_index >= 0);
7247 it->face_id = it->saved_face_id;
7249 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7250 That seemed totally bogus - so I changed it... */
7251 gc = it->dpvec[it->current.dpvec_index];
7253 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
7255 it->c = GLYPH_CODE_CHAR (gc);
7256 it->len = CHAR_BYTES (it->c);
7258 /* The entry may contain a face id to use. Such a face id is
7259 the id of a Lisp face, not a realized face. A face id of
7260 zero means no face is specified. */
7261 if (it->dpvec_face_id >= 0)
7262 it->face_id = it->dpvec_face_id;
7263 else
7265 EMACS_INT lface_id = GLYPH_CODE_FACE (gc);
7266 if (lface_id > 0)
7267 it->face_id = merge_faces (it->f, Qt, lface_id,
7268 it->saved_face_id);
7271 else
7272 /* Display table entry is invalid. Return a space. */
7273 it->c = ' ', it->len = 1;
7275 /* Don't change position and object of the iterator here. They are
7276 still the values of the character that had this display table
7277 entry or was translated, and that's what we want. */
7278 it->what = IT_CHARACTER;
7279 return 1;
7282 /* Get the first element of string/buffer in the visual order, after
7283 being reseated to a new position in a string or a buffer. */
7284 static void
7285 get_visually_first_element (struct it *it)
7287 int string_p = STRINGP (it->string) || it->s;
7288 EMACS_INT eob = (string_p ? it->bidi_it.string.schars : ZV);
7289 EMACS_INT bob = (string_p ? 0 : BEGV);
7291 if (STRINGP (it->string))
7293 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7294 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7296 else
7298 it->bidi_it.charpos = IT_CHARPOS (*it);
7299 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7302 if (it->bidi_it.charpos == eob)
7304 /* Nothing to do, but reset the FIRST_ELT flag, like
7305 bidi_paragraph_init does, because we are not going to
7306 call it. */
7307 it->bidi_it.first_elt = 0;
7309 else if (it->bidi_it.charpos == bob
7310 || (!string_p
7311 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7312 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7314 /* If we are at the beginning of a line/string, we can produce
7315 the next element right away. */
7316 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7317 bidi_move_to_visually_next (&it->bidi_it);
7319 else
7321 EMACS_INT orig_bytepos = it->bidi_it.bytepos;
7323 /* We need to prime the bidi iterator starting at the line's or
7324 string's beginning, before we will be able to produce the
7325 next element. */
7326 if (string_p)
7327 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7328 else
7330 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7331 -1);
7332 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7334 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7337 /* Now return to buffer/string position where we were asked
7338 to get the next display element, and produce that. */
7339 bidi_move_to_visually_next (&it->bidi_it);
7341 while (it->bidi_it.bytepos != orig_bytepos
7342 && it->bidi_it.charpos < eob);
7345 /* Adjust IT's position information to where we ended up. */
7346 if (STRINGP (it->string))
7348 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7349 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7351 else
7353 IT_CHARPOS (*it) = it->bidi_it.charpos;
7354 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7357 if (STRINGP (it->string) || !it->s)
7359 EMACS_INT stop, charpos, bytepos;
7361 if (STRINGP (it->string))
7363 xassert (!it->s);
7364 stop = SCHARS (it->string);
7365 if (stop > it->end_charpos)
7366 stop = it->end_charpos;
7367 charpos = IT_STRING_CHARPOS (*it);
7368 bytepos = IT_STRING_BYTEPOS (*it);
7370 else
7372 stop = it->end_charpos;
7373 charpos = IT_CHARPOS (*it);
7374 bytepos = IT_BYTEPOS (*it);
7376 if (it->bidi_it.scan_dir < 0)
7377 stop = -1;
7378 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7379 it->string);
7383 /* Load IT with the next display element from Lisp string IT->string.
7384 IT->current.string_pos is the current position within the string.
7385 If IT->current.overlay_string_index >= 0, the Lisp string is an
7386 overlay string. */
7388 static int
7389 next_element_from_string (struct it *it)
7391 struct text_pos position;
7393 xassert (STRINGP (it->string));
7394 xassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7395 xassert (IT_STRING_CHARPOS (*it) >= 0);
7396 position = it->current.string_pos;
7398 /* With bidi reordering, the character to display might not be the
7399 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7400 that we were reseat()ed to a new string, whose paragraph
7401 direction is not known. */
7402 if (it->bidi_p && it->bidi_it.first_elt)
7404 get_visually_first_element (it);
7405 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7408 /* Time to check for invisible text? */
7409 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7411 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7413 if (!(!it->bidi_p
7414 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7415 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7417 /* With bidi non-linear iteration, we could find
7418 ourselves far beyond the last computed stop_charpos,
7419 with several other stop positions in between that we
7420 missed. Scan them all now, in buffer's logical
7421 order, until we find and handle the last stop_charpos
7422 that precedes our current position. */
7423 handle_stop_backwards (it, it->stop_charpos);
7424 return GET_NEXT_DISPLAY_ELEMENT (it);
7426 else
7428 if (it->bidi_p)
7430 /* Take note of the stop position we just moved
7431 across, for when we will move back across it. */
7432 it->prev_stop = it->stop_charpos;
7433 /* If we are at base paragraph embedding level, take
7434 note of the last stop position seen at this
7435 level. */
7436 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7437 it->base_level_stop = it->stop_charpos;
7439 handle_stop (it);
7441 /* Since a handler may have changed IT->method, we must
7442 recurse here. */
7443 return GET_NEXT_DISPLAY_ELEMENT (it);
7446 else if (it->bidi_p
7447 /* If we are before prev_stop, we may have overstepped
7448 on our way backwards a stop_pos, and if so, we need
7449 to handle that stop_pos. */
7450 && IT_STRING_CHARPOS (*it) < it->prev_stop
7451 /* We can sometimes back up for reasons that have nothing
7452 to do with bidi reordering. E.g., compositions. The
7453 code below is only needed when we are above the base
7454 embedding level, so test for that explicitly. */
7455 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7457 /* If we lost track of base_level_stop, we have no better
7458 place for handle_stop_backwards to start from than string
7459 beginning. This happens, e.g., when we were reseated to
7460 the previous screenful of text by vertical-motion. */
7461 if (it->base_level_stop <= 0
7462 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7463 it->base_level_stop = 0;
7464 handle_stop_backwards (it, it->base_level_stop);
7465 return GET_NEXT_DISPLAY_ELEMENT (it);
7469 if (it->current.overlay_string_index >= 0)
7471 /* Get the next character from an overlay string. In overlay
7472 strings, there is no field width or padding with spaces to
7473 do. */
7474 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7476 it->what = IT_EOB;
7477 return 0;
7479 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7480 IT_STRING_BYTEPOS (*it),
7481 it->bidi_it.scan_dir < 0
7482 ? -1
7483 : SCHARS (it->string))
7484 && next_element_from_composition (it))
7486 return 1;
7488 else if (STRING_MULTIBYTE (it->string))
7490 const unsigned char *s = (SDATA (it->string)
7491 + IT_STRING_BYTEPOS (*it));
7492 it->c = string_char_and_length (s, &it->len);
7494 else
7496 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7497 it->len = 1;
7500 else
7502 /* Get the next character from a Lisp string that is not an
7503 overlay string. Such strings come from the mode line, for
7504 example. We may have to pad with spaces, or truncate the
7505 string. See also next_element_from_c_string. */
7506 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7508 it->what = IT_EOB;
7509 return 0;
7511 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7513 /* Pad with spaces. */
7514 it->c = ' ', it->len = 1;
7515 CHARPOS (position) = BYTEPOS (position) = -1;
7517 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7518 IT_STRING_BYTEPOS (*it),
7519 it->bidi_it.scan_dir < 0
7520 ? -1
7521 : it->string_nchars)
7522 && next_element_from_composition (it))
7524 return 1;
7526 else if (STRING_MULTIBYTE (it->string))
7528 const unsigned char *s = (SDATA (it->string)
7529 + IT_STRING_BYTEPOS (*it));
7530 it->c = string_char_and_length (s, &it->len);
7532 else
7534 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7535 it->len = 1;
7539 /* Record what we have and where it came from. */
7540 it->what = IT_CHARACTER;
7541 it->object = it->string;
7542 it->position = position;
7543 return 1;
7547 /* Load IT with next display element from C string IT->s.
7548 IT->string_nchars is the maximum number of characters to return
7549 from the string. IT->end_charpos may be greater than
7550 IT->string_nchars when this function is called, in which case we
7551 may have to return padding spaces. Value is zero if end of string
7552 reached, including padding spaces. */
7554 static int
7555 next_element_from_c_string (struct it *it)
7557 int success_p = 1;
7559 xassert (it->s);
7560 xassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7561 it->what = IT_CHARACTER;
7562 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7563 it->object = Qnil;
7565 /* With bidi reordering, the character to display might not be the
7566 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7567 we were reseated to a new string, whose paragraph direction is
7568 not known. */
7569 if (it->bidi_p && it->bidi_it.first_elt)
7570 get_visually_first_element (it);
7572 /* IT's position can be greater than IT->string_nchars in case a
7573 field width or precision has been specified when the iterator was
7574 initialized. */
7575 if (IT_CHARPOS (*it) >= it->end_charpos)
7577 /* End of the game. */
7578 it->what = IT_EOB;
7579 success_p = 0;
7581 else if (IT_CHARPOS (*it) >= it->string_nchars)
7583 /* Pad with spaces. */
7584 it->c = ' ', it->len = 1;
7585 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7587 else if (it->multibyte_p)
7588 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7589 else
7590 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7592 return success_p;
7596 /* Set up IT to return characters from an ellipsis, if appropriate.
7597 The definition of the ellipsis glyphs may come from a display table
7598 entry. This function fills IT with the first glyph from the
7599 ellipsis if an ellipsis is to be displayed. */
7601 static int
7602 next_element_from_ellipsis (struct it *it)
7604 if (it->selective_display_ellipsis_p)
7605 setup_for_ellipsis (it, it->len);
7606 else
7608 /* The face at the current position may be different from the
7609 face we find after the invisible text. Remember what it
7610 was in IT->saved_face_id, and signal that it's there by
7611 setting face_before_selective_p. */
7612 it->saved_face_id = it->face_id;
7613 it->method = GET_FROM_BUFFER;
7614 it->object = it->w->buffer;
7615 reseat_at_next_visible_line_start (it, 1);
7616 it->face_before_selective_p = 1;
7619 return GET_NEXT_DISPLAY_ELEMENT (it);
7623 /* Deliver an image display element. The iterator IT is already
7624 filled with image information (done in handle_display_prop). Value
7625 is always 1. */
7628 static int
7629 next_element_from_image (struct it *it)
7631 it->what = IT_IMAGE;
7632 it->ignore_overlay_strings_at_pos_p = 0;
7633 return 1;
7637 /* Fill iterator IT with next display element from a stretch glyph
7638 property. IT->object is the value of the text property. Value is
7639 always 1. */
7641 static int
7642 next_element_from_stretch (struct it *it)
7644 it->what = IT_STRETCH;
7645 return 1;
7648 /* Scan backwards from IT's current position until we find a stop
7649 position, or until BEGV. This is called when we find ourself
7650 before both the last known prev_stop and base_level_stop while
7651 reordering bidirectional text. */
7653 static void
7654 compute_stop_pos_backwards (struct it *it)
7656 const int SCAN_BACK_LIMIT = 1000;
7657 struct text_pos pos;
7658 struct display_pos save_current = it->current;
7659 struct text_pos save_position = it->position;
7660 EMACS_INT charpos = IT_CHARPOS (*it);
7661 EMACS_INT where_we_are = charpos;
7662 EMACS_INT save_stop_pos = it->stop_charpos;
7663 EMACS_INT save_end_pos = it->end_charpos;
7665 xassert (NILP (it->string) && !it->s);
7666 xassert (it->bidi_p);
7667 it->bidi_p = 0;
7670 it->end_charpos = min (charpos + 1, ZV);
7671 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7672 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7673 reseat_1 (it, pos, 0);
7674 compute_stop_pos (it);
7675 /* We must advance forward, right? */
7676 if (it->stop_charpos <= charpos)
7677 abort ();
7679 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7681 if (it->stop_charpos <= where_we_are)
7682 it->prev_stop = it->stop_charpos;
7683 else
7684 it->prev_stop = BEGV;
7685 it->bidi_p = 1;
7686 it->current = save_current;
7687 it->position = save_position;
7688 it->stop_charpos = save_stop_pos;
7689 it->end_charpos = save_end_pos;
7692 /* Scan forward from CHARPOS in the current buffer/string, until we
7693 find a stop position > current IT's position. Then handle the stop
7694 position before that. This is called when we bump into a stop
7695 position while reordering bidirectional text. CHARPOS should be
7696 the last previously processed stop_pos (or BEGV/0, if none were
7697 processed yet) whose position is less that IT's current
7698 position. */
7700 static void
7701 handle_stop_backwards (struct it *it, EMACS_INT charpos)
7703 int bufp = !STRINGP (it->string);
7704 EMACS_INT where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7705 struct display_pos save_current = it->current;
7706 struct text_pos save_position = it->position;
7707 struct text_pos pos1;
7708 EMACS_INT next_stop;
7710 /* Scan in strict logical order. */
7711 xassert (it->bidi_p);
7712 it->bidi_p = 0;
7715 it->prev_stop = charpos;
7716 if (bufp)
7718 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7719 reseat_1 (it, pos1, 0);
7721 else
7722 it->current.string_pos = string_pos (charpos, it->string);
7723 compute_stop_pos (it);
7724 /* We must advance forward, right? */
7725 if (it->stop_charpos <= it->prev_stop)
7726 abort ();
7727 charpos = it->stop_charpos;
7729 while (charpos <= where_we_are);
7731 it->bidi_p = 1;
7732 it->current = save_current;
7733 it->position = save_position;
7734 next_stop = it->stop_charpos;
7735 it->stop_charpos = it->prev_stop;
7736 handle_stop (it);
7737 it->stop_charpos = next_stop;
7740 /* Load IT with the next display element from current_buffer. Value
7741 is zero if end of buffer reached. IT->stop_charpos is the next
7742 position at which to stop and check for text properties or buffer
7743 end. */
7745 static int
7746 next_element_from_buffer (struct it *it)
7748 int success_p = 1;
7750 xassert (IT_CHARPOS (*it) >= BEGV);
7751 xassert (NILP (it->string) && !it->s);
7752 xassert (!it->bidi_p
7753 || (EQ (it->bidi_it.string.lstring, Qnil)
7754 && it->bidi_it.string.s == NULL));
7756 /* With bidi reordering, the character to display might not be the
7757 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7758 we were reseat()ed to a new buffer position, which is potentially
7759 a different paragraph. */
7760 if (it->bidi_p && it->bidi_it.first_elt)
7762 get_visually_first_element (it);
7763 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7766 if (IT_CHARPOS (*it) >= it->stop_charpos)
7768 if (IT_CHARPOS (*it) >= it->end_charpos)
7770 int overlay_strings_follow_p;
7772 /* End of the game, except when overlay strings follow that
7773 haven't been returned yet. */
7774 if (it->overlay_strings_at_end_processed_p)
7775 overlay_strings_follow_p = 0;
7776 else
7778 it->overlay_strings_at_end_processed_p = 1;
7779 overlay_strings_follow_p = get_overlay_strings (it, 0);
7782 if (overlay_strings_follow_p)
7783 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7784 else
7786 it->what = IT_EOB;
7787 it->position = it->current.pos;
7788 success_p = 0;
7791 else if (!(!it->bidi_p
7792 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7793 || IT_CHARPOS (*it) == it->stop_charpos))
7795 /* With bidi non-linear iteration, we could find ourselves
7796 far beyond the last computed stop_charpos, with several
7797 other stop positions in between that we missed. Scan
7798 them all now, in buffer's logical order, until we find
7799 and handle the last stop_charpos that precedes our
7800 current position. */
7801 handle_stop_backwards (it, it->stop_charpos);
7802 return GET_NEXT_DISPLAY_ELEMENT (it);
7804 else
7806 if (it->bidi_p)
7808 /* Take note of the stop position we just moved across,
7809 for when we will move back across it. */
7810 it->prev_stop = it->stop_charpos;
7811 /* If we are at base paragraph embedding level, take
7812 note of the last stop position seen at this
7813 level. */
7814 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7815 it->base_level_stop = it->stop_charpos;
7817 handle_stop (it);
7818 return GET_NEXT_DISPLAY_ELEMENT (it);
7821 else if (it->bidi_p
7822 /* If we are before prev_stop, we may have overstepped on
7823 our way backwards a stop_pos, and if so, we need to
7824 handle that stop_pos. */
7825 && IT_CHARPOS (*it) < it->prev_stop
7826 /* We can sometimes back up for reasons that have nothing
7827 to do with bidi reordering. E.g., compositions. The
7828 code below is only needed when we are above the base
7829 embedding level, so test for that explicitly. */
7830 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7832 if (it->base_level_stop <= 0
7833 || IT_CHARPOS (*it) < it->base_level_stop)
7835 /* If we lost track of base_level_stop, we need to find
7836 prev_stop by looking backwards. This happens, e.g., when
7837 we were reseated to the previous screenful of text by
7838 vertical-motion. */
7839 it->base_level_stop = BEGV;
7840 compute_stop_pos_backwards (it);
7841 handle_stop_backwards (it, it->prev_stop);
7843 else
7844 handle_stop_backwards (it, it->base_level_stop);
7845 return GET_NEXT_DISPLAY_ELEMENT (it);
7847 else
7849 /* No face changes, overlays etc. in sight, so just return a
7850 character from current_buffer. */
7851 unsigned char *p;
7852 EMACS_INT stop;
7854 /* Maybe run the redisplay end trigger hook. Performance note:
7855 This doesn't seem to cost measurable time. */
7856 if (it->redisplay_end_trigger_charpos
7857 && it->glyph_row
7858 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7859 run_redisplay_end_trigger_hook (it);
7861 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7862 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7863 stop)
7864 && next_element_from_composition (it))
7866 return 1;
7869 /* Get the next character, maybe multibyte. */
7870 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7871 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7872 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7873 else
7874 it->c = *p, it->len = 1;
7876 /* Record what we have and where it came from. */
7877 it->what = IT_CHARACTER;
7878 it->object = it->w->buffer;
7879 it->position = it->current.pos;
7881 /* Normally we return the character found above, except when we
7882 really want to return an ellipsis for selective display. */
7883 if (it->selective)
7885 if (it->c == '\n')
7887 /* A value of selective > 0 means hide lines indented more
7888 than that number of columns. */
7889 if (it->selective > 0
7890 && IT_CHARPOS (*it) + 1 < ZV
7891 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7892 IT_BYTEPOS (*it) + 1,
7893 it->selective))
7895 success_p = next_element_from_ellipsis (it);
7896 it->dpvec_char_len = -1;
7899 else if (it->c == '\r' && it->selective == -1)
7901 /* A value of selective == -1 means that everything from the
7902 CR to the end of the line is invisible, with maybe an
7903 ellipsis displayed for it. */
7904 success_p = next_element_from_ellipsis (it);
7905 it->dpvec_char_len = -1;
7910 /* Value is zero if end of buffer reached. */
7911 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7912 return success_p;
7916 /* Run the redisplay end trigger hook for IT. */
7918 static void
7919 run_redisplay_end_trigger_hook (struct it *it)
7921 Lisp_Object args[3];
7923 /* IT->glyph_row should be non-null, i.e. we should be actually
7924 displaying something, or otherwise we should not run the hook. */
7925 xassert (it->glyph_row);
7927 /* Set up hook arguments. */
7928 args[0] = Qredisplay_end_trigger_functions;
7929 args[1] = it->window;
7930 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7931 it->redisplay_end_trigger_charpos = 0;
7933 /* Since we are *trying* to run these functions, don't try to run
7934 them again, even if they get an error. */
7935 it->w->redisplay_end_trigger = Qnil;
7936 Frun_hook_with_args (3, args);
7938 /* Notice if it changed the face of the character we are on. */
7939 handle_face_prop (it);
7943 /* Deliver a composition display element. Unlike the other
7944 next_element_from_XXX, this function is not registered in the array
7945 get_next_element[]. It is called from next_element_from_buffer and
7946 next_element_from_string when necessary. */
7948 static int
7949 next_element_from_composition (struct it *it)
7951 it->what = IT_COMPOSITION;
7952 it->len = it->cmp_it.nbytes;
7953 if (STRINGP (it->string))
7955 if (it->c < 0)
7957 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7958 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7959 return 0;
7961 it->position = it->current.string_pos;
7962 it->object = it->string;
7963 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7964 IT_STRING_BYTEPOS (*it), it->string);
7966 else
7968 if (it->c < 0)
7970 IT_CHARPOS (*it) += it->cmp_it.nchars;
7971 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7972 if (it->bidi_p)
7974 if (it->bidi_it.new_paragraph)
7975 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7976 /* Resync the bidi iterator with IT's new position.
7977 FIXME: this doesn't support bidirectional text. */
7978 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7979 bidi_move_to_visually_next (&it->bidi_it);
7981 return 0;
7983 it->position = it->current.pos;
7984 it->object = it->w->buffer;
7985 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7986 IT_BYTEPOS (*it), Qnil);
7988 return 1;
7993 /***********************************************************************
7994 Moving an iterator without producing glyphs
7995 ***********************************************************************/
7997 /* Check if iterator is at a position corresponding to a valid buffer
7998 position after some move_it_ call. */
8000 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8001 ((it)->method == GET_FROM_STRING \
8002 ? IT_STRING_CHARPOS (*it) == 0 \
8003 : 1)
8006 /* Move iterator IT to a specified buffer or X position within one
8007 line on the display without producing glyphs.
8009 OP should be a bit mask including some or all of these bits:
8010 MOVE_TO_X: Stop upon reaching x-position TO_X.
8011 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8012 Regardless of OP's value, stop upon reaching the end of the display line.
8014 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8015 This means, in particular, that TO_X includes window's horizontal
8016 scroll amount.
8018 The return value has several possible values that
8019 say what condition caused the scan to stop:
8021 MOVE_POS_MATCH_OR_ZV
8022 - when TO_POS or ZV was reached.
8024 MOVE_X_REACHED
8025 -when TO_X was reached before TO_POS or ZV were reached.
8027 MOVE_LINE_CONTINUED
8028 - when we reached the end of the display area and the line must
8029 be continued.
8031 MOVE_LINE_TRUNCATED
8032 - when we reached the end of the display area and the line is
8033 truncated.
8035 MOVE_NEWLINE_OR_CR
8036 - when we stopped at a line end, i.e. a newline or a CR and selective
8037 display is on. */
8039 static enum move_it_result
8040 move_it_in_display_line_to (struct it *it,
8041 EMACS_INT to_charpos, int to_x,
8042 enum move_operation_enum op)
8044 enum move_it_result result = MOVE_UNDEFINED;
8045 struct glyph_row *saved_glyph_row;
8046 struct it wrap_it, atpos_it, atx_it, ppos_it;
8047 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8048 void *ppos_data = NULL;
8049 int may_wrap = 0;
8050 enum it_method prev_method = it->method;
8051 EMACS_INT prev_pos = IT_CHARPOS (*it);
8052 int saw_smaller_pos = prev_pos < to_charpos;
8054 /* Don't produce glyphs in produce_glyphs. */
8055 saved_glyph_row = it->glyph_row;
8056 it->glyph_row = NULL;
8058 /* Use wrap_it to save a copy of IT wherever a word wrap could
8059 occur. Use atpos_it to save a copy of IT at the desired buffer
8060 position, if found, so that we can scan ahead and check if the
8061 word later overshoots the window edge. Use atx_it similarly, for
8062 pixel positions. */
8063 wrap_it.sp = -1;
8064 atpos_it.sp = -1;
8065 atx_it.sp = -1;
8067 /* Use ppos_it under bidi reordering to save a copy of IT for the
8068 position > CHARPOS that is the closest to CHARPOS. We restore
8069 that position in IT when we have scanned the entire display line
8070 without finding a match for CHARPOS and all the character
8071 positions are greater than CHARPOS. */
8072 if (it->bidi_p)
8074 SAVE_IT (ppos_it, *it, ppos_data);
8075 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8076 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8077 SAVE_IT (ppos_it, *it, ppos_data);
8080 #define BUFFER_POS_REACHED_P() \
8081 ((op & MOVE_TO_POS) != 0 \
8082 && BUFFERP (it->object) \
8083 && (IT_CHARPOS (*it) == to_charpos \
8084 || ((!it->bidi_p \
8085 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8086 && IT_CHARPOS (*it) > to_charpos) \
8087 || (it->what == IT_COMPOSITION \
8088 && ((IT_CHARPOS (*it) > to_charpos \
8089 && to_charpos >= it->cmp_it.charpos) \
8090 || (IT_CHARPOS (*it) < to_charpos \
8091 && to_charpos <= it->cmp_it.charpos)))) \
8092 && (it->method == GET_FROM_BUFFER \
8093 || (it->method == GET_FROM_DISPLAY_VECTOR \
8094 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8096 /* If there's a line-/wrap-prefix, handle it. */
8097 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8098 && it->current_y < it->last_visible_y)
8099 handle_line_prefix (it);
8101 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8102 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8104 while (1)
8106 int x, i, ascent = 0, descent = 0;
8108 /* Utility macro to reset an iterator with x, ascent, and descent. */
8109 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8110 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8111 (IT)->max_descent = descent)
8113 /* Stop if we move beyond TO_CHARPOS (after an image or a
8114 display string or stretch glyph). */
8115 if ((op & MOVE_TO_POS) != 0
8116 && BUFFERP (it->object)
8117 && it->method == GET_FROM_BUFFER
8118 && (((!it->bidi_p
8119 /* When the iterator is at base embedding level, we
8120 are guaranteed that characters are delivered for
8121 display in strictly increasing order of their
8122 buffer positions. */
8123 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8124 && IT_CHARPOS (*it) > to_charpos)
8125 || (it->bidi_p
8126 && (prev_method == GET_FROM_IMAGE
8127 || prev_method == GET_FROM_STRETCH
8128 || prev_method == GET_FROM_STRING)
8129 /* Passed TO_CHARPOS from left to right. */
8130 && ((prev_pos < to_charpos
8131 && IT_CHARPOS (*it) > to_charpos)
8132 /* Passed TO_CHARPOS from right to left. */
8133 || (prev_pos > to_charpos
8134 && IT_CHARPOS (*it) < to_charpos)))))
8136 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8138 result = MOVE_POS_MATCH_OR_ZV;
8139 break;
8141 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8142 /* If wrap_it is valid, the current position might be in a
8143 word that is wrapped. So, save the iterator in
8144 atpos_it and continue to see if wrapping happens. */
8145 SAVE_IT (atpos_it, *it, atpos_data);
8148 /* Stop when ZV reached.
8149 We used to stop here when TO_CHARPOS reached as well, but that is
8150 too soon if this glyph does not fit on this line. So we handle it
8151 explicitly below. */
8152 if (!get_next_display_element (it))
8154 result = MOVE_POS_MATCH_OR_ZV;
8155 break;
8158 if (it->line_wrap == TRUNCATE)
8160 if (BUFFER_POS_REACHED_P ())
8162 result = MOVE_POS_MATCH_OR_ZV;
8163 break;
8166 else
8168 if (it->line_wrap == WORD_WRAP)
8170 if (IT_DISPLAYING_WHITESPACE (it))
8171 may_wrap = 1;
8172 else if (may_wrap)
8174 /* We have reached a glyph that follows one or more
8175 whitespace characters. If the position is
8176 already found, we are done. */
8177 if (atpos_it.sp >= 0)
8179 RESTORE_IT (it, &atpos_it, atpos_data);
8180 result = MOVE_POS_MATCH_OR_ZV;
8181 goto done;
8183 if (atx_it.sp >= 0)
8185 RESTORE_IT (it, &atx_it, atx_data);
8186 result = MOVE_X_REACHED;
8187 goto done;
8189 /* Otherwise, we can wrap here. */
8190 SAVE_IT (wrap_it, *it, wrap_data);
8191 may_wrap = 0;
8196 /* Remember the line height for the current line, in case
8197 the next element doesn't fit on the line. */
8198 ascent = it->max_ascent;
8199 descent = it->max_descent;
8201 /* The call to produce_glyphs will get the metrics of the
8202 display element IT is loaded with. Record the x-position
8203 before this display element, in case it doesn't fit on the
8204 line. */
8205 x = it->current_x;
8207 PRODUCE_GLYPHS (it);
8209 if (it->area != TEXT_AREA)
8211 prev_method = it->method;
8212 if (it->method == GET_FROM_BUFFER)
8213 prev_pos = IT_CHARPOS (*it);
8214 set_iterator_to_next (it, 1);
8215 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8216 SET_TEXT_POS (this_line_min_pos,
8217 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8218 if (it->bidi_p
8219 && (op & MOVE_TO_POS)
8220 && IT_CHARPOS (*it) > to_charpos
8221 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8222 SAVE_IT (ppos_it, *it, ppos_data);
8223 continue;
8226 /* The number of glyphs we get back in IT->nglyphs will normally
8227 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8228 character on a terminal frame, or (iii) a line end. For the
8229 second case, IT->nglyphs - 1 padding glyphs will be present.
8230 (On X frames, there is only one glyph produced for a
8231 composite character.)
8233 The behavior implemented below means, for continuation lines,
8234 that as many spaces of a TAB as fit on the current line are
8235 displayed there. For terminal frames, as many glyphs of a
8236 multi-glyph character are displayed in the current line, too.
8237 This is what the old redisplay code did, and we keep it that
8238 way. Under X, the whole shape of a complex character must
8239 fit on the line or it will be completely displayed in the
8240 next line.
8242 Note that both for tabs and padding glyphs, all glyphs have
8243 the same width. */
8244 if (it->nglyphs)
8246 /* More than one glyph or glyph doesn't fit on line. All
8247 glyphs have the same width. */
8248 int single_glyph_width = it->pixel_width / it->nglyphs;
8249 int new_x;
8250 int x_before_this_char = x;
8251 int hpos_before_this_char = it->hpos;
8253 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8255 new_x = x + single_glyph_width;
8257 /* We want to leave anything reaching TO_X to the caller. */
8258 if ((op & MOVE_TO_X) && new_x > to_x)
8260 if (BUFFER_POS_REACHED_P ())
8262 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8263 goto buffer_pos_reached;
8264 if (atpos_it.sp < 0)
8266 SAVE_IT (atpos_it, *it, atpos_data);
8267 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8270 else
8272 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8274 it->current_x = x;
8275 result = MOVE_X_REACHED;
8276 break;
8278 if (atx_it.sp < 0)
8280 SAVE_IT (atx_it, *it, atx_data);
8281 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8286 if (/* Lines are continued. */
8287 it->line_wrap != TRUNCATE
8288 && (/* And glyph doesn't fit on the line. */
8289 new_x > it->last_visible_x
8290 /* Or it fits exactly and we're on a window
8291 system frame. */
8292 || (new_x == it->last_visible_x
8293 && FRAME_WINDOW_P (it->f))))
8295 if (/* IT->hpos == 0 means the very first glyph
8296 doesn't fit on the line, e.g. a wide image. */
8297 it->hpos == 0
8298 || (new_x == it->last_visible_x
8299 && FRAME_WINDOW_P (it->f)))
8301 ++it->hpos;
8302 it->current_x = new_x;
8304 /* The character's last glyph just barely fits
8305 in this row. */
8306 if (i == it->nglyphs - 1)
8308 /* If this is the destination position,
8309 return a position *before* it in this row,
8310 now that we know it fits in this row. */
8311 if (BUFFER_POS_REACHED_P ())
8313 if (it->line_wrap != WORD_WRAP
8314 || wrap_it.sp < 0)
8316 it->hpos = hpos_before_this_char;
8317 it->current_x = x_before_this_char;
8318 result = MOVE_POS_MATCH_OR_ZV;
8319 break;
8321 if (it->line_wrap == WORD_WRAP
8322 && atpos_it.sp < 0)
8324 SAVE_IT (atpos_it, *it, atpos_data);
8325 atpos_it.current_x = x_before_this_char;
8326 atpos_it.hpos = hpos_before_this_char;
8330 prev_method = it->method;
8331 if (it->method == GET_FROM_BUFFER)
8332 prev_pos = IT_CHARPOS (*it);
8333 set_iterator_to_next (it, 1);
8334 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8335 SET_TEXT_POS (this_line_min_pos,
8336 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8337 /* On graphical terminals, newlines may
8338 "overflow" into the fringe if
8339 overflow-newline-into-fringe is non-nil.
8340 On text-only terminals, newlines may
8341 overflow into the last glyph on the
8342 display line.*/
8343 if (!FRAME_WINDOW_P (it->f)
8344 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8346 if (!get_next_display_element (it))
8348 result = MOVE_POS_MATCH_OR_ZV;
8349 break;
8351 if (BUFFER_POS_REACHED_P ())
8353 if (ITERATOR_AT_END_OF_LINE_P (it))
8354 result = MOVE_POS_MATCH_OR_ZV;
8355 else
8356 result = MOVE_LINE_CONTINUED;
8357 break;
8359 if (ITERATOR_AT_END_OF_LINE_P (it))
8361 result = MOVE_NEWLINE_OR_CR;
8362 break;
8367 else
8368 IT_RESET_X_ASCENT_DESCENT (it);
8370 if (wrap_it.sp >= 0)
8372 RESTORE_IT (it, &wrap_it, wrap_data);
8373 atpos_it.sp = -1;
8374 atx_it.sp = -1;
8377 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8378 IT_CHARPOS (*it)));
8379 result = MOVE_LINE_CONTINUED;
8380 break;
8383 if (BUFFER_POS_REACHED_P ())
8385 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8386 goto buffer_pos_reached;
8387 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8389 SAVE_IT (atpos_it, *it, atpos_data);
8390 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8394 if (new_x > it->first_visible_x)
8396 /* Glyph is visible. Increment number of glyphs that
8397 would be displayed. */
8398 ++it->hpos;
8402 if (result != MOVE_UNDEFINED)
8403 break;
8405 else if (BUFFER_POS_REACHED_P ())
8407 buffer_pos_reached:
8408 IT_RESET_X_ASCENT_DESCENT (it);
8409 result = MOVE_POS_MATCH_OR_ZV;
8410 break;
8412 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8414 /* Stop when TO_X specified and reached. This check is
8415 necessary here because of lines consisting of a line end,
8416 only. The line end will not produce any glyphs and we
8417 would never get MOVE_X_REACHED. */
8418 xassert (it->nglyphs == 0);
8419 result = MOVE_X_REACHED;
8420 break;
8423 /* Is this a line end? If yes, we're done. */
8424 if (ITERATOR_AT_END_OF_LINE_P (it))
8426 /* If we are past TO_CHARPOS, but never saw any character
8427 positions smaller than TO_CHARPOS, return
8428 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8429 did. */
8430 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8432 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8434 if (IT_CHARPOS (ppos_it) < ZV)
8436 RESTORE_IT (it, &ppos_it, ppos_data);
8437 result = MOVE_POS_MATCH_OR_ZV;
8439 else
8440 goto buffer_pos_reached;
8442 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8443 && IT_CHARPOS (*it) > to_charpos)
8444 goto buffer_pos_reached;
8445 else
8446 result = MOVE_NEWLINE_OR_CR;
8448 else
8449 result = MOVE_NEWLINE_OR_CR;
8450 break;
8453 prev_method = it->method;
8454 if (it->method == GET_FROM_BUFFER)
8455 prev_pos = IT_CHARPOS (*it);
8456 /* The current display element has been consumed. Advance
8457 to the next. */
8458 set_iterator_to_next (it, 1);
8459 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8460 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8461 if (IT_CHARPOS (*it) < to_charpos)
8462 saw_smaller_pos = 1;
8463 if (it->bidi_p
8464 && (op & MOVE_TO_POS)
8465 && IT_CHARPOS (*it) >= to_charpos
8466 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8467 SAVE_IT (ppos_it, *it, ppos_data);
8469 /* Stop if lines are truncated and IT's current x-position is
8470 past the right edge of the window now. */
8471 if (it->line_wrap == TRUNCATE
8472 && it->current_x >= it->last_visible_x)
8474 if (!FRAME_WINDOW_P (it->f)
8475 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8477 int at_eob_p = 0;
8479 if ((at_eob_p = !get_next_display_element (it))
8480 || BUFFER_POS_REACHED_P ()
8481 /* If we are past TO_CHARPOS, but never saw any
8482 character positions smaller than TO_CHARPOS,
8483 return MOVE_POS_MATCH_OR_ZV, like the
8484 unidirectional display did. */
8485 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8486 && !saw_smaller_pos
8487 && IT_CHARPOS (*it) > to_charpos))
8489 if (it->bidi_p
8490 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8491 RESTORE_IT (it, &ppos_it, ppos_data);
8492 result = MOVE_POS_MATCH_OR_ZV;
8493 break;
8495 if (ITERATOR_AT_END_OF_LINE_P (it))
8497 result = MOVE_NEWLINE_OR_CR;
8498 break;
8501 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8502 && !saw_smaller_pos
8503 && IT_CHARPOS (*it) > to_charpos)
8505 if (IT_CHARPOS (ppos_it) < ZV)
8506 RESTORE_IT (it, &ppos_it, ppos_data);
8507 result = MOVE_POS_MATCH_OR_ZV;
8508 break;
8510 result = MOVE_LINE_TRUNCATED;
8511 break;
8513 #undef IT_RESET_X_ASCENT_DESCENT
8516 #undef BUFFER_POS_REACHED_P
8518 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8519 restore the saved iterator. */
8520 if (atpos_it.sp >= 0)
8521 RESTORE_IT (it, &atpos_it, atpos_data);
8522 else if (atx_it.sp >= 0)
8523 RESTORE_IT (it, &atx_it, atx_data);
8525 done:
8527 if (atpos_data)
8528 bidi_unshelve_cache (atpos_data, 1);
8529 if (atx_data)
8530 bidi_unshelve_cache (atx_data, 1);
8531 if (wrap_data)
8532 bidi_unshelve_cache (wrap_data, 1);
8533 if (ppos_data)
8534 bidi_unshelve_cache (ppos_data, 1);
8536 /* Restore the iterator settings altered at the beginning of this
8537 function. */
8538 it->glyph_row = saved_glyph_row;
8539 return result;
8542 /* For external use. */
8543 void
8544 move_it_in_display_line (struct it *it,
8545 EMACS_INT to_charpos, int to_x,
8546 enum move_operation_enum op)
8548 if (it->line_wrap == WORD_WRAP
8549 && (op & MOVE_TO_X))
8551 struct it save_it;
8552 void *save_data = NULL;
8553 int skip;
8555 SAVE_IT (save_it, *it, save_data);
8556 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8557 /* When word-wrap is on, TO_X may lie past the end
8558 of a wrapped line. Then it->current is the
8559 character on the next line, so backtrack to the
8560 space before the wrap point. */
8561 if (skip == MOVE_LINE_CONTINUED)
8563 int prev_x = max (it->current_x - 1, 0);
8564 RESTORE_IT (it, &save_it, save_data);
8565 move_it_in_display_line_to
8566 (it, -1, prev_x, MOVE_TO_X);
8568 else
8569 bidi_unshelve_cache (save_data, 1);
8571 else
8572 move_it_in_display_line_to (it, to_charpos, to_x, op);
8576 /* Move IT forward until it satisfies one or more of the criteria in
8577 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8579 OP is a bit-mask that specifies where to stop, and in particular,
8580 which of those four position arguments makes a difference. See the
8581 description of enum move_operation_enum.
8583 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8584 screen line, this function will set IT to the next position that is
8585 displayed to the right of TO_CHARPOS on the screen. */
8587 void
8588 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
8590 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8591 int line_height, line_start_x = 0, reached = 0;
8592 void *backup_data = NULL;
8594 for (;;)
8596 if (op & MOVE_TO_VPOS)
8598 /* If no TO_CHARPOS and no TO_X specified, stop at the
8599 start of the line TO_VPOS. */
8600 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8602 if (it->vpos == to_vpos)
8604 reached = 1;
8605 break;
8607 else
8608 skip = move_it_in_display_line_to (it, -1, -1, 0);
8610 else
8612 /* TO_VPOS >= 0 means stop at TO_X in the line at
8613 TO_VPOS, or at TO_POS, whichever comes first. */
8614 if (it->vpos == to_vpos)
8616 reached = 2;
8617 break;
8620 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8622 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8624 reached = 3;
8625 break;
8627 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8629 /* We have reached TO_X but not in the line we want. */
8630 skip = move_it_in_display_line_to (it, to_charpos,
8631 -1, MOVE_TO_POS);
8632 if (skip == MOVE_POS_MATCH_OR_ZV)
8634 reached = 4;
8635 break;
8640 else if (op & MOVE_TO_Y)
8642 struct it it_backup;
8644 if (it->line_wrap == WORD_WRAP)
8645 SAVE_IT (it_backup, *it, backup_data);
8647 /* TO_Y specified means stop at TO_X in the line containing
8648 TO_Y---or at TO_CHARPOS if this is reached first. The
8649 problem is that we can't really tell whether the line
8650 contains TO_Y before we have completely scanned it, and
8651 this may skip past TO_X. What we do is to first scan to
8652 TO_X.
8654 If TO_X is not specified, use a TO_X of zero. The reason
8655 is to make the outcome of this function more predictable.
8656 If we didn't use TO_X == 0, we would stop at the end of
8657 the line which is probably not what a caller would expect
8658 to happen. */
8659 skip = move_it_in_display_line_to
8660 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8661 (MOVE_TO_X | (op & MOVE_TO_POS)));
8663 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8664 if (skip == MOVE_POS_MATCH_OR_ZV)
8665 reached = 5;
8666 else if (skip == MOVE_X_REACHED)
8668 /* If TO_X was reached, we want to know whether TO_Y is
8669 in the line. We know this is the case if the already
8670 scanned glyphs make the line tall enough. Otherwise,
8671 we must check by scanning the rest of the line. */
8672 line_height = it->max_ascent + it->max_descent;
8673 if (to_y >= it->current_y
8674 && to_y < it->current_y + line_height)
8676 reached = 6;
8677 break;
8679 SAVE_IT (it_backup, *it, backup_data);
8680 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8681 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8682 op & MOVE_TO_POS);
8683 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8684 line_height = it->max_ascent + it->max_descent;
8685 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8687 if (to_y >= it->current_y
8688 && to_y < it->current_y + line_height)
8690 /* If TO_Y is in this line and TO_X was reached
8691 above, we scanned too far. We have to restore
8692 IT's settings to the ones before skipping. */
8693 RESTORE_IT (it, &it_backup, backup_data);
8694 reached = 6;
8696 else
8698 skip = skip2;
8699 if (skip == MOVE_POS_MATCH_OR_ZV)
8700 reached = 7;
8703 else
8705 /* Check whether TO_Y is in this line. */
8706 line_height = it->max_ascent + it->max_descent;
8707 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8709 if (to_y >= it->current_y
8710 && to_y < it->current_y + line_height)
8712 /* When word-wrap is on, TO_X may lie past the end
8713 of a wrapped line. Then it->current is the
8714 character on the next line, so backtrack to the
8715 space before the wrap point. */
8716 if (skip == MOVE_LINE_CONTINUED
8717 && it->line_wrap == WORD_WRAP)
8719 int prev_x = max (it->current_x - 1, 0);
8720 RESTORE_IT (it, &it_backup, backup_data);
8721 skip = move_it_in_display_line_to
8722 (it, -1, prev_x, MOVE_TO_X);
8724 reached = 6;
8728 if (reached)
8729 break;
8731 else if (BUFFERP (it->object)
8732 && (it->method == GET_FROM_BUFFER
8733 || it->method == GET_FROM_STRETCH)
8734 && IT_CHARPOS (*it) >= to_charpos
8735 /* Under bidi iteration, a call to set_iterator_to_next
8736 can scan far beyond to_charpos if the initial
8737 portion of the next line needs to be reordered. In
8738 that case, give move_it_in_display_line_to another
8739 chance below. */
8740 && !(it->bidi_p
8741 && it->bidi_it.scan_dir == -1))
8742 skip = MOVE_POS_MATCH_OR_ZV;
8743 else
8744 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8746 switch (skip)
8748 case MOVE_POS_MATCH_OR_ZV:
8749 reached = 8;
8750 goto out;
8752 case MOVE_NEWLINE_OR_CR:
8753 set_iterator_to_next (it, 1);
8754 it->continuation_lines_width = 0;
8755 break;
8757 case MOVE_LINE_TRUNCATED:
8758 it->continuation_lines_width = 0;
8759 reseat_at_next_visible_line_start (it, 0);
8760 if ((op & MOVE_TO_POS) != 0
8761 && IT_CHARPOS (*it) > to_charpos)
8763 reached = 9;
8764 goto out;
8766 break;
8768 case MOVE_LINE_CONTINUED:
8769 /* For continued lines ending in a tab, some of the glyphs
8770 associated with the tab are displayed on the current
8771 line. Since it->current_x does not include these glyphs,
8772 we use it->last_visible_x instead. */
8773 if (it->c == '\t')
8775 it->continuation_lines_width += it->last_visible_x;
8776 /* When moving by vpos, ensure that the iterator really
8777 advances to the next line (bug#847, bug#969). Fixme:
8778 do we need to do this in other circumstances? */
8779 if (it->current_x != it->last_visible_x
8780 && (op & MOVE_TO_VPOS)
8781 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8783 line_start_x = it->current_x + it->pixel_width
8784 - it->last_visible_x;
8785 set_iterator_to_next (it, 0);
8788 else
8789 it->continuation_lines_width += it->current_x;
8790 break;
8792 default:
8793 abort ();
8796 /* Reset/increment for the next run. */
8797 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8798 it->current_x = line_start_x;
8799 line_start_x = 0;
8800 it->hpos = 0;
8801 it->current_y += it->max_ascent + it->max_descent;
8802 ++it->vpos;
8803 last_height = it->max_ascent + it->max_descent;
8804 last_max_ascent = it->max_ascent;
8805 it->max_ascent = it->max_descent = 0;
8808 out:
8810 /* On text terminals, we may stop at the end of a line in the middle
8811 of a multi-character glyph. If the glyph itself is continued,
8812 i.e. it is actually displayed on the next line, don't treat this
8813 stopping point as valid; move to the next line instead (unless
8814 that brings us offscreen). */
8815 if (!FRAME_WINDOW_P (it->f)
8816 && op & MOVE_TO_POS
8817 && IT_CHARPOS (*it) == to_charpos
8818 && it->what == IT_CHARACTER
8819 && it->nglyphs > 1
8820 && it->line_wrap == WINDOW_WRAP
8821 && it->current_x == it->last_visible_x - 1
8822 && it->c != '\n'
8823 && it->c != '\t'
8824 && it->vpos < XFASTINT (it->w->window_end_vpos))
8826 it->continuation_lines_width += it->current_x;
8827 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8828 it->current_y += it->max_ascent + it->max_descent;
8829 ++it->vpos;
8830 last_height = it->max_ascent + it->max_descent;
8831 last_max_ascent = it->max_ascent;
8834 if (backup_data)
8835 bidi_unshelve_cache (backup_data, 1);
8837 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8841 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8843 If DY > 0, move IT backward at least that many pixels. DY = 0
8844 means move IT backward to the preceding line start or BEGV. This
8845 function may move over more than DY pixels if IT->current_y - DY
8846 ends up in the middle of a line; in this case IT->current_y will be
8847 set to the top of the line moved to. */
8849 void
8850 move_it_vertically_backward (struct it *it, int dy)
8852 int nlines, h;
8853 struct it it2, it3;
8854 void *it2data = NULL, *it3data = NULL;
8855 EMACS_INT start_pos;
8857 move_further_back:
8858 xassert (dy >= 0);
8860 start_pos = IT_CHARPOS (*it);
8862 /* Estimate how many newlines we must move back. */
8863 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8865 /* Set the iterator's position that many lines back. */
8866 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8867 back_to_previous_visible_line_start (it);
8869 /* Reseat the iterator here. When moving backward, we don't want
8870 reseat to skip forward over invisible text, set up the iterator
8871 to deliver from overlay strings at the new position etc. So,
8872 use reseat_1 here. */
8873 reseat_1 (it, it->current.pos, 1);
8875 /* We are now surely at a line start. */
8876 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8877 reordering is in effect. */
8878 it->continuation_lines_width = 0;
8880 /* Move forward and see what y-distance we moved. First move to the
8881 start of the next line so that we get its height. We need this
8882 height to be able to tell whether we reached the specified
8883 y-distance. */
8884 SAVE_IT (it2, *it, it2data);
8885 it2.max_ascent = it2.max_descent = 0;
8888 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8889 MOVE_TO_POS | MOVE_TO_VPOS);
8891 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8892 /* If we are in a display string which starts at START_POS,
8893 and that display string includes a newline, and we are
8894 right after that newline (i.e. at the beginning of a
8895 display line), exit the loop, because otherwise we will
8896 infloop, since move_it_to will see that it is already at
8897 START_POS and will not move. */
8898 || (it2.method == GET_FROM_STRING
8899 && IT_CHARPOS (it2) == start_pos
8900 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
8901 xassert (IT_CHARPOS (*it) >= BEGV);
8902 SAVE_IT (it3, it2, it3data);
8904 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8905 xassert (IT_CHARPOS (*it) >= BEGV);
8906 /* H is the actual vertical distance from the position in *IT
8907 and the starting position. */
8908 h = it2.current_y - it->current_y;
8909 /* NLINES is the distance in number of lines. */
8910 nlines = it2.vpos - it->vpos;
8912 /* Correct IT's y and vpos position
8913 so that they are relative to the starting point. */
8914 it->vpos -= nlines;
8915 it->current_y -= h;
8917 if (dy == 0)
8919 /* DY == 0 means move to the start of the screen line. The
8920 value of nlines is > 0 if continuation lines were involved,
8921 or if the original IT position was at start of a line. */
8922 RESTORE_IT (it, it, it2data);
8923 if (nlines > 0)
8924 move_it_by_lines (it, nlines);
8925 /* The above code moves us to some position NLINES down,
8926 usually to its first glyph (leftmost in an L2R line), but
8927 that's not necessarily the start of the line, under bidi
8928 reordering. We want to get to the character position
8929 that is immediately after the newline of the previous
8930 line. */
8931 if (it->bidi_p
8932 && !it->continuation_lines_width
8933 && !STRINGP (it->string)
8934 && IT_CHARPOS (*it) > BEGV
8935 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8937 EMACS_INT nl_pos =
8938 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
8940 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
8942 bidi_unshelve_cache (it3data, 1);
8944 else
8946 /* The y-position we try to reach, relative to *IT.
8947 Note that H has been subtracted in front of the if-statement. */
8948 int target_y = it->current_y + h - dy;
8949 int y0 = it3.current_y;
8950 int y1;
8951 int line_height;
8953 RESTORE_IT (&it3, &it3, it3data);
8954 y1 = line_bottom_y (&it3);
8955 line_height = y1 - y0;
8956 RESTORE_IT (it, it, it2data);
8957 /* If we did not reach target_y, try to move further backward if
8958 we can. If we moved too far backward, try to move forward. */
8959 if (target_y < it->current_y
8960 /* This is heuristic. In a window that's 3 lines high, with
8961 a line height of 13 pixels each, recentering with point
8962 on the bottom line will try to move -39/2 = 19 pixels
8963 backward. Try to avoid moving into the first line. */
8964 && (it->current_y - target_y
8965 > min (window_box_height (it->w), line_height * 2 / 3))
8966 && IT_CHARPOS (*it) > BEGV)
8968 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8969 target_y - it->current_y));
8970 dy = it->current_y - target_y;
8971 goto move_further_back;
8973 else if (target_y >= it->current_y + line_height
8974 && IT_CHARPOS (*it) < ZV)
8976 /* Should move forward by at least one line, maybe more.
8978 Note: Calling move_it_by_lines can be expensive on
8979 terminal frames, where compute_motion is used (via
8980 vmotion) to do the job, when there are very long lines
8981 and truncate-lines is nil. That's the reason for
8982 treating terminal frames specially here. */
8984 if (!FRAME_WINDOW_P (it->f))
8985 move_it_vertically (it, target_y - (it->current_y + line_height));
8986 else
8990 move_it_by_lines (it, 1);
8992 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
8999 /* Move IT by a specified amount of pixel lines DY. DY negative means
9000 move backwards. DY = 0 means move to start of screen line. At the
9001 end, IT will be on the start of a screen line. */
9003 void
9004 move_it_vertically (struct it *it, int dy)
9006 if (dy <= 0)
9007 move_it_vertically_backward (it, -dy);
9008 else
9010 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9011 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9012 MOVE_TO_POS | MOVE_TO_Y);
9013 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9015 /* If buffer ends in ZV without a newline, move to the start of
9016 the line to satisfy the post-condition. */
9017 if (IT_CHARPOS (*it) == ZV
9018 && ZV > BEGV
9019 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9020 move_it_by_lines (it, 0);
9025 /* Move iterator IT past the end of the text line it is in. */
9027 void
9028 move_it_past_eol (struct it *it)
9030 enum move_it_result rc;
9032 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9033 if (rc == MOVE_NEWLINE_OR_CR)
9034 set_iterator_to_next (it, 0);
9038 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9039 negative means move up. DVPOS == 0 means move to the start of the
9040 screen line.
9042 Optimization idea: If we would know that IT->f doesn't use
9043 a face with proportional font, we could be faster for
9044 truncate-lines nil. */
9046 void
9047 move_it_by_lines (struct it *it, int dvpos)
9050 /* The commented-out optimization uses vmotion on terminals. This
9051 gives bad results, because elements like it->what, on which
9052 callers such as pos_visible_p rely, aren't updated. */
9053 /* struct position pos;
9054 if (!FRAME_WINDOW_P (it->f))
9056 struct text_pos textpos;
9058 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9059 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9060 reseat (it, textpos, 1);
9061 it->vpos += pos.vpos;
9062 it->current_y += pos.vpos;
9064 else */
9066 if (dvpos == 0)
9068 /* DVPOS == 0 means move to the start of the screen line. */
9069 move_it_vertically_backward (it, 0);
9070 /* Let next call to line_bottom_y calculate real line height */
9071 last_height = 0;
9073 else if (dvpos > 0)
9075 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9076 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9078 /* Only move to the next buffer position if we ended up in a
9079 string from display property, not in an overlay string
9080 (before-string or after-string). That is because the
9081 latter don't conceal the underlying buffer position, so
9082 we can ask to move the iterator to the exact position we
9083 are interested in. Note that, even if we are already at
9084 IT_CHARPOS (*it), the call below is not a no-op, as it
9085 will detect that we are at the end of the string, pop the
9086 iterator, and compute it->current_x and it->hpos
9087 correctly. */
9088 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9089 -1, -1, -1, MOVE_TO_POS);
9092 else
9094 struct it it2;
9095 void *it2data = NULL;
9096 EMACS_INT start_charpos, i;
9098 /* Start at the beginning of the screen line containing IT's
9099 position. This may actually move vertically backwards,
9100 in case of overlays, so adjust dvpos accordingly. */
9101 dvpos += it->vpos;
9102 move_it_vertically_backward (it, 0);
9103 dvpos -= it->vpos;
9105 /* Go back -DVPOS visible lines and reseat the iterator there. */
9106 start_charpos = IT_CHARPOS (*it);
9107 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9108 back_to_previous_visible_line_start (it);
9109 reseat (it, it->current.pos, 1);
9111 /* Move further back if we end up in a string or an image. */
9112 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9114 /* First try to move to start of display line. */
9115 dvpos += it->vpos;
9116 move_it_vertically_backward (it, 0);
9117 dvpos -= it->vpos;
9118 if (IT_POS_VALID_AFTER_MOVE_P (it))
9119 break;
9120 /* If start of line is still in string or image,
9121 move further back. */
9122 back_to_previous_visible_line_start (it);
9123 reseat (it, it->current.pos, 1);
9124 dvpos--;
9127 it->current_x = it->hpos = 0;
9129 /* Above call may have moved too far if continuation lines
9130 are involved. Scan forward and see if it did. */
9131 SAVE_IT (it2, *it, it2data);
9132 it2.vpos = it2.current_y = 0;
9133 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9134 it->vpos -= it2.vpos;
9135 it->current_y -= it2.current_y;
9136 it->current_x = it->hpos = 0;
9138 /* If we moved too far back, move IT some lines forward. */
9139 if (it2.vpos > -dvpos)
9141 int delta = it2.vpos + dvpos;
9143 RESTORE_IT (&it2, &it2, it2data);
9144 SAVE_IT (it2, *it, it2data);
9145 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9146 /* Move back again if we got too far ahead. */
9147 if (IT_CHARPOS (*it) >= start_charpos)
9148 RESTORE_IT (it, &it2, it2data);
9149 else
9150 bidi_unshelve_cache (it2data, 1);
9152 else
9153 RESTORE_IT (it, it, it2data);
9157 /* Return 1 if IT points into the middle of a display vector. */
9160 in_display_vector_p (struct it *it)
9162 return (it->method == GET_FROM_DISPLAY_VECTOR
9163 && it->current.dpvec_index > 0
9164 && it->dpvec + it->current.dpvec_index != it->dpend);
9168 /***********************************************************************
9169 Messages
9170 ***********************************************************************/
9173 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9174 to *Messages*. */
9176 void
9177 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9179 Lisp_Object args[3];
9180 Lisp_Object msg, fmt;
9181 char *buffer;
9182 EMACS_INT len;
9183 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9184 USE_SAFE_ALLOCA;
9186 /* Do nothing if called asynchronously. Inserting text into
9187 a buffer may call after-change-functions and alike and
9188 that would means running Lisp asynchronously. */
9189 if (handling_signal)
9190 return;
9192 fmt = msg = Qnil;
9193 GCPRO4 (fmt, msg, arg1, arg2);
9195 args[0] = fmt = build_string (format);
9196 args[1] = arg1;
9197 args[2] = arg2;
9198 msg = Fformat (3, args);
9200 len = SBYTES (msg) + 1;
9201 SAFE_ALLOCA (buffer, char *, len);
9202 memcpy (buffer, SDATA (msg), len);
9204 message_dolog (buffer, len - 1, 1, 0);
9205 SAFE_FREE ();
9207 UNGCPRO;
9211 /* Output a newline in the *Messages* buffer if "needs" one. */
9213 void
9214 message_log_maybe_newline (void)
9216 if (message_log_need_newline)
9217 message_dolog ("", 0, 1, 0);
9221 /* Add a string M of length NBYTES to the message log, optionally
9222 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9223 nonzero, means interpret the contents of M as multibyte. This
9224 function calls low-level routines in order to bypass text property
9225 hooks, etc. which might not be safe to run.
9227 This may GC (insert may run before/after change hooks),
9228 so the buffer M must NOT point to a Lisp string. */
9230 void
9231 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
9233 const unsigned char *msg = (const unsigned char *) m;
9235 if (!NILP (Vmemory_full))
9236 return;
9238 if (!NILP (Vmessage_log_max))
9240 struct buffer *oldbuf;
9241 Lisp_Object oldpoint, oldbegv, oldzv;
9242 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9243 EMACS_INT point_at_end = 0;
9244 EMACS_INT zv_at_end = 0;
9245 Lisp_Object old_deactivate_mark, tem;
9246 struct gcpro gcpro1;
9248 old_deactivate_mark = Vdeactivate_mark;
9249 oldbuf = current_buffer;
9250 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9251 BVAR (current_buffer, undo_list) = Qt;
9253 oldpoint = message_dolog_marker1;
9254 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9255 oldbegv = message_dolog_marker2;
9256 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9257 oldzv = message_dolog_marker3;
9258 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9259 GCPRO1 (old_deactivate_mark);
9261 if (PT == Z)
9262 point_at_end = 1;
9263 if (ZV == Z)
9264 zv_at_end = 1;
9266 BEGV = BEG;
9267 BEGV_BYTE = BEG_BYTE;
9268 ZV = Z;
9269 ZV_BYTE = Z_BYTE;
9270 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9272 /* Insert the string--maybe converting multibyte to single byte
9273 or vice versa, so that all the text fits the buffer. */
9274 if (multibyte
9275 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9277 EMACS_INT i;
9278 int c, char_bytes;
9279 char work[1];
9281 /* Convert a multibyte string to single-byte
9282 for the *Message* buffer. */
9283 for (i = 0; i < nbytes; i += char_bytes)
9285 c = string_char_and_length (msg + i, &char_bytes);
9286 work[0] = (ASCII_CHAR_P (c)
9288 : multibyte_char_to_unibyte (c));
9289 insert_1_both (work, 1, 1, 1, 0, 0);
9292 else if (! multibyte
9293 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9295 EMACS_INT i;
9296 int c, char_bytes;
9297 unsigned char str[MAX_MULTIBYTE_LENGTH];
9298 /* Convert a single-byte string to multibyte
9299 for the *Message* buffer. */
9300 for (i = 0; i < nbytes; i++)
9302 c = msg[i];
9303 MAKE_CHAR_MULTIBYTE (c);
9304 char_bytes = CHAR_STRING (c, str);
9305 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9308 else if (nbytes)
9309 insert_1 (m, nbytes, 1, 0, 0);
9311 if (nlflag)
9313 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9314 printmax_t dups;
9315 insert_1 ("\n", 1, 1, 0, 0);
9317 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9318 this_bol = PT;
9319 this_bol_byte = PT_BYTE;
9321 /* See if this line duplicates the previous one.
9322 If so, combine duplicates. */
9323 if (this_bol > BEG)
9325 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9326 prev_bol = PT;
9327 prev_bol_byte = PT_BYTE;
9329 dups = message_log_check_duplicate (prev_bol_byte,
9330 this_bol_byte);
9331 if (dups)
9333 del_range_both (prev_bol, prev_bol_byte,
9334 this_bol, this_bol_byte, 0);
9335 if (dups > 1)
9337 char dupstr[sizeof " [ times]"
9338 + INT_STRLEN_BOUND (printmax_t)];
9339 int duplen;
9341 /* If you change this format, don't forget to also
9342 change message_log_check_duplicate. */
9343 sprintf (dupstr, " [%"pMd" times]", dups);
9344 duplen = strlen (dupstr);
9345 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9346 insert_1 (dupstr, duplen, 1, 0, 1);
9351 /* If we have more than the desired maximum number of lines
9352 in the *Messages* buffer now, delete the oldest ones.
9353 This is safe because we don't have undo in this buffer. */
9355 if (NATNUMP (Vmessage_log_max))
9357 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9358 -XFASTINT (Vmessage_log_max) - 1, 0);
9359 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9362 BEGV = XMARKER (oldbegv)->charpos;
9363 BEGV_BYTE = marker_byte_position (oldbegv);
9365 if (zv_at_end)
9367 ZV = Z;
9368 ZV_BYTE = Z_BYTE;
9370 else
9372 ZV = XMARKER (oldzv)->charpos;
9373 ZV_BYTE = marker_byte_position (oldzv);
9376 if (point_at_end)
9377 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9378 else
9379 /* We can't do Fgoto_char (oldpoint) because it will run some
9380 Lisp code. */
9381 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9382 XMARKER (oldpoint)->bytepos);
9384 UNGCPRO;
9385 unchain_marker (XMARKER (oldpoint));
9386 unchain_marker (XMARKER (oldbegv));
9387 unchain_marker (XMARKER (oldzv));
9389 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9390 set_buffer_internal (oldbuf);
9391 if (NILP (tem))
9392 windows_or_buffers_changed = old_windows_or_buffers_changed;
9393 message_log_need_newline = !nlflag;
9394 Vdeactivate_mark = old_deactivate_mark;
9399 /* We are at the end of the buffer after just having inserted a newline.
9400 (Note: We depend on the fact we won't be crossing the gap.)
9401 Check to see if the most recent message looks a lot like the previous one.
9402 Return 0 if different, 1 if the new one should just replace it, or a
9403 value N > 1 if we should also append " [N times]". */
9405 static intmax_t
9406 message_log_check_duplicate (EMACS_INT prev_bol_byte, EMACS_INT this_bol_byte)
9408 EMACS_INT i;
9409 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
9410 int seen_dots = 0;
9411 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9412 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9414 for (i = 0; i < len; i++)
9416 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9417 seen_dots = 1;
9418 if (p1[i] != p2[i])
9419 return seen_dots;
9421 p1 += len;
9422 if (*p1 == '\n')
9423 return 2;
9424 if (*p1++ == ' ' && *p1++ == '[')
9426 char *pend;
9427 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9428 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9429 return n+1;
9431 return 0;
9435 /* Display an echo area message M with a specified length of NBYTES
9436 bytes. The string may include null characters. If M is 0, clear
9437 out any existing message, and let the mini-buffer text show
9438 through.
9440 This may GC, so the buffer M must NOT point to a Lisp string. */
9442 void
9443 message2 (const char *m, EMACS_INT nbytes, int multibyte)
9445 /* First flush out any partial line written with print. */
9446 message_log_maybe_newline ();
9447 if (m)
9448 message_dolog (m, nbytes, 1, multibyte);
9449 message2_nolog (m, nbytes, multibyte);
9453 /* The non-logging counterpart of message2. */
9455 void
9456 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
9458 struct frame *sf = SELECTED_FRAME ();
9459 message_enable_multibyte = multibyte;
9461 if (FRAME_INITIAL_P (sf))
9463 if (noninteractive_need_newline)
9464 putc ('\n', stderr);
9465 noninteractive_need_newline = 0;
9466 if (m)
9467 fwrite (m, nbytes, 1, stderr);
9468 if (cursor_in_echo_area == 0)
9469 fprintf (stderr, "\n");
9470 fflush (stderr);
9472 /* A null message buffer means that the frame hasn't really been
9473 initialized yet. Error messages get reported properly by
9474 cmd_error, so this must be just an informative message; toss it. */
9475 else if (INTERACTIVE
9476 && sf->glyphs_initialized_p
9477 && FRAME_MESSAGE_BUF (sf))
9479 Lisp_Object mini_window;
9480 struct frame *f;
9482 /* Get the frame containing the mini-buffer
9483 that the selected frame is using. */
9484 mini_window = FRAME_MINIBUF_WINDOW (sf);
9485 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9487 FRAME_SAMPLE_VISIBILITY (f);
9488 if (FRAME_VISIBLE_P (sf)
9489 && ! FRAME_VISIBLE_P (f))
9490 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9492 if (m)
9494 set_message (m, Qnil, nbytes, multibyte);
9495 if (minibuffer_auto_raise)
9496 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9498 else
9499 clear_message (1, 1);
9501 do_pending_window_change (0);
9502 echo_area_display (1);
9503 do_pending_window_change (0);
9504 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9505 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9510 /* Display an echo area message M with a specified length of NBYTES
9511 bytes. The string may include null characters. If M is not a
9512 string, clear out any existing message, and let the mini-buffer
9513 text show through.
9515 This function cancels echoing. */
9517 void
9518 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9520 struct gcpro gcpro1;
9522 GCPRO1 (m);
9523 clear_message (1,1);
9524 cancel_echoing ();
9526 /* First flush out any partial line written with print. */
9527 message_log_maybe_newline ();
9528 if (STRINGP (m))
9530 char *buffer;
9531 USE_SAFE_ALLOCA;
9533 SAFE_ALLOCA (buffer, char *, nbytes);
9534 memcpy (buffer, SDATA (m), nbytes);
9535 message_dolog (buffer, nbytes, 1, multibyte);
9536 SAFE_FREE ();
9538 message3_nolog (m, nbytes, multibyte);
9540 UNGCPRO;
9544 /* The non-logging version of message3.
9545 This does not cancel echoing, because it is used for echoing.
9546 Perhaps we need to make a separate function for echoing
9547 and make this cancel echoing. */
9549 void
9550 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9552 struct frame *sf = SELECTED_FRAME ();
9553 message_enable_multibyte = multibyte;
9555 if (FRAME_INITIAL_P (sf))
9557 if (noninteractive_need_newline)
9558 putc ('\n', stderr);
9559 noninteractive_need_newline = 0;
9560 if (STRINGP (m))
9561 fwrite (SDATA (m), nbytes, 1, stderr);
9562 if (cursor_in_echo_area == 0)
9563 fprintf (stderr, "\n");
9564 fflush (stderr);
9566 /* A null message buffer means that the frame hasn't really been
9567 initialized yet. Error messages get reported properly by
9568 cmd_error, so this must be just an informative message; toss it. */
9569 else if (INTERACTIVE
9570 && sf->glyphs_initialized_p
9571 && FRAME_MESSAGE_BUF (sf))
9573 Lisp_Object mini_window;
9574 Lisp_Object frame;
9575 struct frame *f;
9577 /* Get the frame containing the mini-buffer
9578 that the selected frame is using. */
9579 mini_window = FRAME_MINIBUF_WINDOW (sf);
9580 frame = XWINDOW (mini_window)->frame;
9581 f = XFRAME (frame);
9583 FRAME_SAMPLE_VISIBILITY (f);
9584 if (FRAME_VISIBLE_P (sf)
9585 && !FRAME_VISIBLE_P (f))
9586 Fmake_frame_visible (frame);
9588 if (STRINGP (m) && SCHARS (m) > 0)
9590 set_message (NULL, m, nbytes, multibyte);
9591 if (minibuffer_auto_raise)
9592 Fraise_frame (frame);
9593 /* Assume we are not echoing.
9594 (If we are, echo_now will override this.) */
9595 echo_message_buffer = Qnil;
9597 else
9598 clear_message (1, 1);
9600 do_pending_window_change (0);
9601 echo_area_display (1);
9602 do_pending_window_change (0);
9603 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9604 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9609 /* Display a null-terminated echo area message M. If M is 0, clear
9610 out any existing message, and let the mini-buffer text show through.
9612 The buffer M must continue to exist until after the echo area gets
9613 cleared or some other message gets displayed there. Do not pass
9614 text that is stored in a Lisp string. Do not pass text in a buffer
9615 that was alloca'd. */
9617 void
9618 message1 (const char *m)
9620 message2 (m, (m ? strlen (m) : 0), 0);
9624 /* The non-logging counterpart of message1. */
9626 void
9627 message1_nolog (const char *m)
9629 message2_nolog (m, (m ? strlen (m) : 0), 0);
9632 /* Display a message M which contains a single %s
9633 which gets replaced with STRING. */
9635 void
9636 message_with_string (const char *m, Lisp_Object string, int log)
9638 CHECK_STRING (string);
9640 if (noninteractive)
9642 if (m)
9644 if (noninteractive_need_newline)
9645 putc ('\n', stderr);
9646 noninteractive_need_newline = 0;
9647 fprintf (stderr, m, SDATA (string));
9648 if (!cursor_in_echo_area)
9649 fprintf (stderr, "\n");
9650 fflush (stderr);
9653 else if (INTERACTIVE)
9655 /* The frame whose minibuffer we're going to display the message on.
9656 It may be larger than the selected frame, so we need
9657 to use its buffer, not the selected frame's buffer. */
9658 Lisp_Object mini_window;
9659 struct frame *f, *sf = SELECTED_FRAME ();
9661 /* Get the frame containing the minibuffer
9662 that the selected frame is using. */
9663 mini_window = FRAME_MINIBUF_WINDOW (sf);
9664 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9666 /* A null message buffer means that the frame hasn't really been
9667 initialized yet. Error messages get reported properly by
9668 cmd_error, so this must be just an informative message; toss it. */
9669 if (FRAME_MESSAGE_BUF (f))
9671 Lisp_Object args[2], msg;
9672 struct gcpro gcpro1, gcpro2;
9674 args[0] = build_string (m);
9675 args[1] = msg = string;
9676 GCPRO2 (args[0], msg);
9677 gcpro1.nvars = 2;
9679 msg = Fformat (2, args);
9681 if (log)
9682 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9683 else
9684 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9686 UNGCPRO;
9688 /* Print should start at the beginning of the message
9689 buffer next time. */
9690 message_buf_print = 0;
9696 /* Dump an informative message to the minibuf. If M is 0, clear out
9697 any existing message, and let the mini-buffer text show through. */
9699 static void
9700 vmessage (const char *m, va_list ap)
9702 if (noninteractive)
9704 if (m)
9706 if (noninteractive_need_newline)
9707 putc ('\n', stderr);
9708 noninteractive_need_newline = 0;
9709 vfprintf (stderr, m, ap);
9710 if (cursor_in_echo_area == 0)
9711 fprintf (stderr, "\n");
9712 fflush (stderr);
9715 else if (INTERACTIVE)
9717 /* The frame whose mini-buffer we're going to display the message
9718 on. It may be larger than the selected frame, so we need to
9719 use its buffer, not the selected frame's buffer. */
9720 Lisp_Object mini_window;
9721 struct frame *f, *sf = SELECTED_FRAME ();
9723 /* Get the frame containing the mini-buffer
9724 that the selected frame is using. */
9725 mini_window = FRAME_MINIBUF_WINDOW (sf);
9726 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9728 /* A null message buffer means that the frame hasn't really been
9729 initialized yet. Error messages get reported properly by
9730 cmd_error, so this must be just an informative message; toss
9731 it. */
9732 if (FRAME_MESSAGE_BUF (f))
9734 if (m)
9736 ptrdiff_t len;
9738 len = doprnt (FRAME_MESSAGE_BUF (f),
9739 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9741 message2 (FRAME_MESSAGE_BUF (f), len, 0);
9743 else
9744 message1 (0);
9746 /* Print should start at the beginning of the message
9747 buffer next time. */
9748 message_buf_print = 0;
9753 void
9754 message (const char *m, ...)
9756 va_list ap;
9757 va_start (ap, m);
9758 vmessage (m, ap);
9759 va_end (ap);
9763 #if 0
9764 /* The non-logging version of message. */
9766 void
9767 message_nolog (const char *m, ...)
9769 Lisp_Object old_log_max;
9770 va_list ap;
9771 va_start (ap, m);
9772 old_log_max = Vmessage_log_max;
9773 Vmessage_log_max = Qnil;
9774 vmessage (m, ap);
9775 Vmessage_log_max = old_log_max;
9776 va_end (ap);
9778 #endif
9781 /* Display the current message in the current mini-buffer. This is
9782 only called from error handlers in process.c, and is not time
9783 critical. */
9785 void
9786 update_echo_area (void)
9788 if (!NILP (echo_area_buffer[0]))
9790 Lisp_Object string;
9791 string = Fcurrent_message ();
9792 message3 (string, SBYTES (string),
9793 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9798 /* Make sure echo area buffers in `echo_buffers' are live.
9799 If they aren't, make new ones. */
9801 static void
9802 ensure_echo_area_buffers (void)
9804 int i;
9806 for (i = 0; i < 2; ++i)
9807 if (!BUFFERP (echo_buffer[i])
9808 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9810 char name[30];
9811 Lisp_Object old_buffer;
9812 int j;
9814 old_buffer = echo_buffer[i];
9815 sprintf (name, " *Echo Area %d*", i);
9816 echo_buffer[i] = Fget_buffer_create (build_string (name));
9817 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9818 /* to force word wrap in echo area -
9819 it was decided to postpone this*/
9820 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9822 for (j = 0; j < 2; ++j)
9823 if (EQ (old_buffer, echo_area_buffer[j]))
9824 echo_area_buffer[j] = echo_buffer[i];
9829 /* Call FN with args A1..A4 with either the current or last displayed
9830 echo_area_buffer as current buffer.
9832 WHICH zero means use the current message buffer
9833 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9834 from echo_buffer[] and clear it.
9836 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9837 suitable buffer from echo_buffer[] and clear it.
9839 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9840 that the current message becomes the last displayed one, make
9841 choose a suitable buffer for echo_area_buffer[0], and clear it.
9843 Value is what FN returns. */
9845 static int
9846 with_echo_area_buffer (struct window *w, int which,
9847 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
9848 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9850 Lisp_Object buffer;
9851 int this_one, the_other, clear_buffer_p, rc;
9852 int count = SPECPDL_INDEX ();
9854 /* If buffers aren't live, make new ones. */
9855 ensure_echo_area_buffers ();
9857 clear_buffer_p = 0;
9859 if (which == 0)
9860 this_one = 0, the_other = 1;
9861 else if (which > 0)
9862 this_one = 1, the_other = 0;
9863 else
9865 this_one = 0, the_other = 1;
9866 clear_buffer_p = 1;
9868 /* We need a fresh one in case the current echo buffer equals
9869 the one containing the last displayed echo area message. */
9870 if (!NILP (echo_area_buffer[this_one])
9871 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9872 echo_area_buffer[this_one] = Qnil;
9875 /* Choose a suitable buffer from echo_buffer[] is we don't
9876 have one. */
9877 if (NILP (echo_area_buffer[this_one]))
9879 echo_area_buffer[this_one]
9880 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9881 ? echo_buffer[the_other]
9882 : echo_buffer[this_one]);
9883 clear_buffer_p = 1;
9886 buffer = echo_area_buffer[this_one];
9888 /* Don't get confused by reusing the buffer used for echoing
9889 for a different purpose. */
9890 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9891 cancel_echoing ();
9893 record_unwind_protect (unwind_with_echo_area_buffer,
9894 with_echo_area_buffer_unwind_data (w));
9896 /* Make the echo area buffer current. Note that for display
9897 purposes, it is not necessary that the displayed window's buffer
9898 == current_buffer, except for text property lookup. So, let's
9899 only set that buffer temporarily here without doing a full
9900 Fset_window_buffer. We must also change w->pointm, though,
9901 because otherwise an assertions in unshow_buffer fails, and Emacs
9902 aborts. */
9903 set_buffer_internal_1 (XBUFFER (buffer));
9904 if (w)
9906 w->buffer = buffer;
9907 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9910 BVAR (current_buffer, undo_list) = Qt;
9911 BVAR (current_buffer, read_only) = Qnil;
9912 specbind (Qinhibit_read_only, Qt);
9913 specbind (Qinhibit_modification_hooks, Qt);
9915 if (clear_buffer_p && Z > BEG)
9916 del_range (BEG, Z);
9918 xassert (BEGV >= BEG);
9919 xassert (ZV <= Z && ZV >= BEGV);
9921 rc = fn (a1, a2, a3, a4);
9923 xassert (BEGV >= BEG);
9924 xassert (ZV <= Z && ZV >= BEGV);
9926 unbind_to (count, Qnil);
9927 return rc;
9931 /* Save state that should be preserved around the call to the function
9932 FN called in with_echo_area_buffer. */
9934 static Lisp_Object
9935 with_echo_area_buffer_unwind_data (struct window *w)
9937 int i = 0;
9938 Lisp_Object vector, tmp;
9940 /* Reduce consing by keeping one vector in
9941 Vwith_echo_area_save_vector. */
9942 vector = Vwith_echo_area_save_vector;
9943 Vwith_echo_area_save_vector = Qnil;
9945 if (NILP (vector))
9946 vector = Fmake_vector (make_number (7), Qnil);
9948 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9949 ASET (vector, i, Vdeactivate_mark); ++i;
9950 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9952 if (w)
9954 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9955 ASET (vector, i, w->buffer); ++i;
9956 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9957 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9959 else
9961 int end = i + 4;
9962 for (; i < end; ++i)
9963 ASET (vector, i, Qnil);
9966 xassert (i == ASIZE (vector));
9967 return vector;
9971 /* Restore global state from VECTOR which was created by
9972 with_echo_area_buffer_unwind_data. */
9974 static Lisp_Object
9975 unwind_with_echo_area_buffer (Lisp_Object vector)
9977 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
9978 Vdeactivate_mark = AREF (vector, 1);
9979 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
9981 if (WINDOWP (AREF (vector, 3)))
9983 struct window *w;
9984 Lisp_Object buffer, charpos, bytepos;
9986 w = XWINDOW (AREF (vector, 3));
9987 buffer = AREF (vector, 4);
9988 charpos = AREF (vector, 5);
9989 bytepos = AREF (vector, 6);
9991 w->buffer = buffer;
9992 set_marker_both (w->pointm, buffer,
9993 XFASTINT (charpos), XFASTINT (bytepos));
9996 Vwith_echo_area_save_vector = vector;
9997 return Qnil;
10001 /* Set up the echo area for use by print functions. MULTIBYTE_P
10002 non-zero means we will print multibyte. */
10004 void
10005 setup_echo_area_for_printing (int multibyte_p)
10007 /* If we can't find an echo area any more, exit. */
10008 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10009 Fkill_emacs (Qnil);
10011 ensure_echo_area_buffers ();
10013 if (!message_buf_print)
10015 /* A message has been output since the last time we printed.
10016 Choose a fresh echo area buffer. */
10017 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10018 echo_area_buffer[0] = echo_buffer[1];
10019 else
10020 echo_area_buffer[0] = echo_buffer[0];
10022 /* Switch to that buffer and clear it. */
10023 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10024 BVAR (current_buffer, truncate_lines) = Qnil;
10026 if (Z > BEG)
10028 int count = SPECPDL_INDEX ();
10029 specbind (Qinhibit_read_only, Qt);
10030 /* Note that undo recording is always disabled. */
10031 del_range (BEG, Z);
10032 unbind_to (count, Qnil);
10034 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10036 /* Set up the buffer for the multibyteness we need. */
10037 if (multibyte_p
10038 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10039 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10041 /* Raise the frame containing the echo area. */
10042 if (minibuffer_auto_raise)
10044 struct frame *sf = SELECTED_FRAME ();
10045 Lisp_Object mini_window;
10046 mini_window = FRAME_MINIBUF_WINDOW (sf);
10047 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10050 message_log_maybe_newline ();
10051 message_buf_print = 1;
10053 else
10055 if (NILP (echo_area_buffer[0]))
10057 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10058 echo_area_buffer[0] = echo_buffer[1];
10059 else
10060 echo_area_buffer[0] = echo_buffer[0];
10063 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10065 /* Someone switched buffers between print requests. */
10066 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10067 BVAR (current_buffer, truncate_lines) = Qnil;
10073 /* Display an echo area message in window W. Value is non-zero if W's
10074 height is changed. If display_last_displayed_message_p is
10075 non-zero, display the message that was last displayed, otherwise
10076 display the current message. */
10078 static int
10079 display_echo_area (struct window *w)
10081 int i, no_message_p, window_height_changed_p, count;
10083 /* Temporarily disable garbage collections while displaying the echo
10084 area. This is done because a GC can print a message itself.
10085 That message would modify the echo area buffer's contents while a
10086 redisplay of the buffer is going on, and seriously confuse
10087 redisplay. */
10088 count = inhibit_garbage_collection ();
10090 /* If there is no message, we must call display_echo_area_1
10091 nevertheless because it resizes the window. But we will have to
10092 reset the echo_area_buffer in question to nil at the end because
10093 with_echo_area_buffer will sets it to an empty buffer. */
10094 i = display_last_displayed_message_p ? 1 : 0;
10095 no_message_p = NILP (echo_area_buffer[i]);
10097 window_height_changed_p
10098 = with_echo_area_buffer (w, display_last_displayed_message_p,
10099 display_echo_area_1,
10100 (intptr_t) w, Qnil, 0, 0);
10102 if (no_message_p)
10103 echo_area_buffer[i] = Qnil;
10105 unbind_to (count, Qnil);
10106 return window_height_changed_p;
10110 /* Helper for display_echo_area. Display the current buffer which
10111 contains the current echo area message in window W, a mini-window,
10112 a pointer to which is passed in A1. A2..A4 are currently not used.
10113 Change the height of W so that all of the message is displayed.
10114 Value is non-zero if height of W was changed. */
10116 static int
10117 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10119 intptr_t i1 = a1;
10120 struct window *w = (struct window *) i1;
10121 Lisp_Object window;
10122 struct text_pos start;
10123 int window_height_changed_p = 0;
10125 /* Do this before displaying, so that we have a large enough glyph
10126 matrix for the display. If we can't get enough space for the
10127 whole text, display the last N lines. That works by setting w->start. */
10128 window_height_changed_p = resize_mini_window (w, 0);
10130 /* Use the starting position chosen by resize_mini_window. */
10131 SET_TEXT_POS_FROM_MARKER (start, w->start);
10133 /* Display. */
10134 clear_glyph_matrix (w->desired_matrix);
10135 XSETWINDOW (window, w);
10136 try_window (window, start, 0);
10138 return window_height_changed_p;
10142 /* Resize the echo area window to exactly the size needed for the
10143 currently displayed message, if there is one. If a mini-buffer
10144 is active, don't shrink it. */
10146 void
10147 resize_echo_area_exactly (void)
10149 if (BUFFERP (echo_area_buffer[0])
10150 && WINDOWP (echo_area_window))
10152 struct window *w = XWINDOW (echo_area_window);
10153 int resized_p;
10154 Lisp_Object resize_exactly;
10156 if (minibuf_level == 0)
10157 resize_exactly = Qt;
10158 else
10159 resize_exactly = Qnil;
10161 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10162 (intptr_t) w, resize_exactly,
10163 0, 0);
10164 if (resized_p)
10166 ++windows_or_buffers_changed;
10167 ++update_mode_lines;
10168 redisplay_internal ();
10174 /* Callback function for with_echo_area_buffer, when used from
10175 resize_echo_area_exactly. A1 contains a pointer to the window to
10176 resize, EXACTLY non-nil means resize the mini-window exactly to the
10177 size of the text displayed. A3 and A4 are not used. Value is what
10178 resize_mini_window returns. */
10180 static int
10181 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
10183 intptr_t i1 = a1;
10184 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10188 /* Resize mini-window W to fit the size of its contents. EXACT_P
10189 means size the window exactly to the size needed. Otherwise, it's
10190 only enlarged until W's buffer is empty.
10192 Set W->start to the right place to begin display. If the whole
10193 contents fit, start at the beginning. Otherwise, start so as
10194 to make the end of the contents appear. This is particularly
10195 important for y-or-n-p, but seems desirable generally.
10197 Value is non-zero if the window height has been changed. */
10200 resize_mini_window (struct window *w, int exact_p)
10202 struct frame *f = XFRAME (w->frame);
10203 int window_height_changed_p = 0;
10205 xassert (MINI_WINDOW_P (w));
10207 /* By default, start display at the beginning. */
10208 set_marker_both (w->start, w->buffer,
10209 BUF_BEGV (XBUFFER (w->buffer)),
10210 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10212 /* Don't resize windows while redisplaying a window; it would
10213 confuse redisplay functions when the size of the window they are
10214 displaying changes from under them. Such a resizing can happen,
10215 for instance, when which-func prints a long message while
10216 we are running fontification-functions. We're running these
10217 functions with safe_call which binds inhibit-redisplay to t. */
10218 if (!NILP (Vinhibit_redisplay))
10219 return 0;
10221 /* Nil means don't try to resize. */
10222 if (NILP (Vresize_mini_windows)
10223 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10224 return 0;
10226 if (!FRAME_MINIBUF_ONLY_P (f))
10228 struct it it;
10229 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10230 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10231 int height, max_height;
10232 int unit = FRAME_LINE_HEIGHT (f);
10233 struct text_pos start;
10234 struct buffer *old_current_buffer = NULL;
10236 if (current_buffer != XBUFFER (w->buffer))
10238 old_current_buffer = current_buffer;
10239 set_buffer_internal (XBUFFER (w->buffer));
10242 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10244 /* Compute the max. number of lines specified by the user. */
10245 if (FLOATP (Vmax_mini_window_height))
10246 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10247 else if (INTEGERP (Vmax_mini_window_height))
10248 max_height = XINT (Vmax_mini_window_height);
10249 else
10250 max_height = total_height / 4;
10252 /* Correct that max. height if it's bogus. */
10253 max_height = max (1, max_height);
10254 max_height = min (total_height, max_height);
10256 /* Find out the height of the text in the window. */
10257 if (it.line_wrap == TRUNCATE)
10258 height = 1;
10259 else
10261 last_height = 0;
10262 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10263 if (it.max_ascent == 0 && it.max_descent == 0)
10264 height = it.current_y + last_height;
10265 else
10266 height = it.current_y + it.max_ascent + it.max_descent;
10267 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10268 height = (height + unit - 1) / unit;
10271 /* Compute a suitable window start. */
10272 if (height > max_height)
10274 height = max_height;
10275 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10276 move_it_vertically_backward (&it, (height - 1) * unit);
10277 start = it.current.pos;
10279 else
10280 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10281 SET_MARKER_FROM_TEXT_POS (w->start, start);
10283 if (EQ (Vresize_mini_windows, Qgrow_only))
10285 /* Let it grow only, until we display an empty message, in which
10286 case the window shrinks again. */
10287 if (height > WINDOW_TOTAL_LINES (w))
10289 int old_height = WINDOW_TOTAL_LINES (w);
10290 freeze_window_starts (f, 1);
10291 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10292 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10294 else if (height < WINDOW_TOTAL_LINES (w)
10295 && (exact_p || BEGV == ZV))
10297 int old_height = WINDOW_TOTAL_LINES (w);
10298 freeze_window_starts (f, 0);
10299 shrink_mini_window (w);
10300 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10303 else
10305 /* Always resize to exact size needed. */
10306 if (height > WINDOW_TOTAL_LINES (w))
10308 int old_height = WINDOW_TOTAL_LINES (w);
10309 freeze_window_starts (f, 1);
10310 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10311 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10313 else if (height < WINDOW_TOTAL_LINES (w))
10315 int old_height = WINDOW_TOTAL_LINES (w);
10316 freeze_window_starts (f, 0);
10317 shrink_mini_window (w);
10319 if (height)
10321 freeze_window_starts (f, 1);
10322 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10325 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10329 if (old_current_buffer)
10330 set_buffer_internal (old_current_buffer);
10333 return window_height_changed_p;
10337 /* Value is the current message, a string, or nil if there is no
10338 current message. */
10340 Lisp_Object
10341 current_message (void)
10343 Lisp_Object msg;
10345 if (!BUFFERP (echo_area_buffer[0]))
10346 msg = Qnil;
10347 else
10349 with_echo_area_buffer (0, 0, current_message_1,
10350 (intptr_t) &msg, Qnil, 0, 0);
10351 if (NILP (msg))
10352 echo_area_buffer[0] = Qnil;
10355 return msg;
10359 static int
10360 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10362 intptr_t i1 = a1;
10363 Lisp_Object *msg = (Lisp_Object *) i1;
10365 if (Z > BEG)
10366 *msg = make_buffer_string (BEG, Z, 1);
10367 else
10368 *msg = Qnil;
10369 return 0;
10373 /* Push the current message on Vmessage_stack for later restoration
10374 by restore_message. Value is non-zero if the current message isn't
10375 empty. This is a relatively infrequent operation, so it's not
10376 worth optimizing. */
10379 push_message (void)
10381 Lisp_Object msg;
10382 msg = current_message ();
10383 Vmessage_stack = Fcons (msg, Vmessage_stack);
10384 return STRINGP (msg);
10388 /* Restore message display from the top of Vmessage_stack. */
10390 void
10391 restore_message (void)
10393 Lisp_Object msg;
10395 xassert (CONSP (Vmessage_stack));
10396 msg = XCAR (Vmessage_stack);
10397 if (STRINGP (msg))
10398 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10399 else
10400 message3_nolog (msg, 0, 0);
10404 /* Handler for record_unwind_protect calling pop_message. */
10406 Lisp_Object
10407 pop_message_unwind (Lisp_Object dummy)
10409 pop_message ();
10410 return Qnil;
10413 /* Pop the top-most entry off Vmessage_stack. */
10415 static void
10416 pop_message (void)
10418 xassert (CONSP (Vmessage_stack));
10419 Vmessage_stack = XCDR (Vmessage_stack);
10423 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10424 exits. If the stack is not empty, we have a missing pop_message
10425 somewhere. */
10427 void
10428 check_message_stack (void)
10430 if (!NILP (Vmessage_stack))
10431 abort ();
10435 /* Truncate to NCHARS what will be displayed in the echo area the next
10436 time we display it---but don't redisplay it now. */
10438 void
10439 truncate_echo_area (EMACS_INT nchars)
10441 if (nchars == 0)
10442 echo_area_buffer[0] = Qnil;
10443 /* A null message buffer means that the frame hasn't really been
10444 initialized yet. Error messages get reported properly by
10445 cmd_error, so this must be just an informative message; toss it. */
10446 else if (!noninteractive
10447 && INTERACTIVE
10448 && !NILP (echo_area_buffer[0]))
10450 struct frame *sf = SELECTED_FRAME ();
10451 if (FRAME_MESSAGE_BUF (sf))
10452 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10457 /* Helper function for truncate_echo_area. Truncate the current
10458 message to at most NCHARS characters. */
10460 static int
10461 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10463 if (BEG + nchars < Z)
10464 del_range (BEG + nchars, Z);
10465 if (Z == BEG)
10466 echo_area_buffer[0] = Qnil;
10467 return 0;
10471 /* Set the current message to a substring of S or STRING.
10473 If STRING is a Lisp string, set the message to the first NBYTES
10474 bytes from STRING. NBYTES zero means use the whole string. If
10475 STRING is multibyte, the message will be displayed multibyte.
10477 If S is not null, set the message to the first LEN bytes of S. LEN
10478 zero means use the whole string. MULTIBYTE_P non-zero means S is
10479 multibyte. Display the message multibyte in that case.
10481 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10482 to t before calling set_message_1 (which calls insert).
10485 static void
10486 set_message (const char *s, Lisp_Object string,
10487 EMACS_INT nbytes, int multibyte_p)
10489 message_enable_multibyte
10490 = ((s && multibyte_p)
10491 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10493 with_echo_area_buffer (0, -1, set_message_1,
10494 (intptr_t) s, string, nbytes, multibyte_p);
10495 message_buf_print = 0;
10496 help_echo_showing_p = 0;
10500 /* Helper function for set_message. Arguments have the same meaning
10501 as there, with A1 corresponding to S and A2 corresponding to STRING
10502 This function is called with the echo area buffer being
10503 current. */
10505 static int
10506 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
10508 intptr_t i1 = a1;
10509 const char *s = (const char *) i1;
10510 const unsigned char *msg = (const unsigned char *) s;
10511 Lisp_Object string = a2;
10513 /* Change multibyteness of the echo buffer appropriately. */
10514 if (message_enable_multibyte
10515 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10516 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10518 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10519 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10520 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10522 /* Insert new message at BEG. */
10523 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10525 if (STRINGP (string))
10527 EMACS_INT nchars;
10529 if (nbytes == 0)
10530 nbytes = SBYTES (string);
10531 nchars = string_byte_to_char (string, nbytes);
10533 /* This function takes care of single/multibyte conversion. We
10534 just have to ensure that the echo area buffer has the right
10535 setting of enable_multibyte_characters. */
10536 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10538 else if (s)
10540 if (nbytes == 0)
10541 nbytes = strlen (s);
10543 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10545 /* Convert from multi-byte to single-byte. */
10546 EMACS_INT i;
10547 int c, n;
10548 char work[1];
10550 /* Convert a multibyte string to single-byte. */
10551 for (i = 0; i < nbytes; i += n)
10553 c = string_char_and_length (msg + i, &n);
10554 work[0] = (ASCII_CHAR_P (c)
10556 : multibyte_char_to_unibyte (c));
10557 insert_1_both (work, 1, 1, 1, 0, 0);
10560 else if (!multibyte_p
10561 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10563 /* Convert from single-byte to multi-byte. */
10564 EMACS_INT i;
10565 int c, n;
10566 unsigned char str[MAX_MULTIBYTE_LENGTH];
10568 /* Convert a single-byte string to multibyte. */
10569 for (i = 0; i < nbytes; i++)
10571 c = msg[i];
10572 MAKE_CHAR_MULTIBYTE (c);
10573 n = CHAR_STRING (c, str);
10574 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10577 else
10578 insert_1 (s, nbytes, 1, 0, 0);
10581 return 0;
10585 /* Clear messages. CURRENT_P non-zero means clear the current
10586 message. LAST_DISPLAYED_P non-zero means clear the message
10587 last displayed. */
10589 void
10590 clear_message (int current_p, int last_displayed_p)
10592 if (current_p)
10594 echo_area_buffer[0] = Qnil;
10595 message_cleared_p = 1;
10598 if (last_displayed_p)
10599 echo_area_buffer[1] = Qnil;
10601 message_buf_print = 0;
10604 /* Clear garbaged frames.
10606 This function is used where the old redisplay called
10607 redraw_garbaged_frames which in turn called redraw_frame which in
10608 turn called clear_frame. The call to clear_frame was a source of
10609 flickering. I believe a clear_frame is not necessary. It should
10610 suffice in the new redisplay to invalidate all current matrices,
10611 and ensure a complete redisplay of all windows. */
10613 static void
10614 clear_garbaged_frames (void)
10616 if (frame_garbaged)
10618 Lisp_Object tail, frame;
10619 int changed_count = 0;
10621 FOR_EACH_FRAME (tail, frame)
10623 struct frame *f = XFRAME (frame);
10625 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10627 if (f->resized_p)
10629 Fredraw_frame (frame);
10630 f->force_flush_display_p = 1;
10632 clear_current_matrices (f);
10633 changed_count++;
10634 f->garbaged = 0;
10635 f->resized_p = 0;
10639 frame_garbaged = 0;
10640 if (changed_count)
10641 ++windows_or_buffers_changed;
10646 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10647 is non-zero update selected_frame. Value is non-zero if the
10648 mini-windows height has been changed. */
10650 static int
10651 echo_area_display (int update_frame_p)
10653 Lisp_Object mini_window;
10654 struct window *w;
10655 struct frame *f;
10656 int window_height_changed_p = 0;
10657 struct frame *sf = SELECTED_FRAME ();
10659 mini_window = FRAME_MINIBUF_WINDOW (sf);
10660 w = XWINDOW (mini_window);
10661 f = XFRAME (WINDOW_FRAME (w));
10663 /* Don't display if frame is invisible or not yet initialized. */
10664 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10665 return 0;
10667 #ifdef HAVE_WINDOW_SYSTEM
10668 /* When Emacs starts, selected_frame may be the initial terminal
10669 frame. If we let this through, a message would be displayed on
10670 the terminal. */
10671 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10672 return 0;
10673 #endif /* HAVE_WINDOW_SYSTEM */
10675 /* Redraw garbaged frames. */
10676 if (frame_garbaged)
10677 clear_garbaged_frames ();
10679 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10681 echo_area_window = mini_window;
10682 window_height_changed_p = display_echo_area (w);
10683 w->must_be_updated_p = 1;
10685 /* Update the display, unless called from redisplay_internal.
10686 Also don't update the screen during redisplay itself. The
10687 update will happen at the end of redisplay, and an update
10688 here could cause confusion. */
10689 if (update_frame_p && !redisplaying_p)
10691 int n = 0;
10693 /* If the display update has been interrupted by pending
10694 input, update mode lines in the frame. Due to the
10695 pending input, it might have been that redisplay hasn't
10696 been called, so that mode lines above the echo area are
10697 garbaged. This looks odd, so we prevent it here. */
10698 if (!display_completed)
10699 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10701 if (window_height_changed_p
10702 /* Don't do this if Emacs is shutting down. Redisplay
10703 needs to run hooks. */
10704 && !NILP (Vrun_hooks))
10706 /* Must update other windows. Likewise as in other
10707 cases, don't let this update be interrupted by
10708 pending input. */
10709 int count = SPECPDL_INDEX ();
10710 specbind (Qredisplay_dont_pause, Qt);
10711 windows_or_buffers_changed = 1;
10712 redisplay_internal ();
10713 unbind_to (count, Qnil);
10715 else if (FRAME_WINDOW_P (f) && n == 0)
10717 /* Window configuration is the same as before.
10718 Can do with a display update of the echo area,
10719 unless we displayed some mode lines. */
10720 update_single_window (w, 1);
10721 FRAME_RIF (f)->flush_display (f);
10723 else
10724 update_frame (f, 1, 1);
10726 /* If cursor is in the echo area, make sure that the next
10727 redisplay displays the minibuffer, so that the cursor will
10728 be replaced with what the minibuffer wants. */
10729 if (cursor_in_echo_area)
10730 ++windows_or_buffers_changed;
10733 else if (!EQ (mini_window, selected_window))
10734 windows_or_buffers_changed++;
10736 /* Last displayed message is now the current message. */
10737 echo_area_buffer[1] = echo_area_buffer[0];
10738 /* Inform read_char that we're not echoing. */
10739 echo_message_buffer = Qnil;
10741 /* Prevent redisplay optimization in redisplay_internal by resetting
10742 this_line_start_pos. This is done because the mini-buffer now
10743 displays the message instead of its buffer text. */
10744 if (EQ (mini_window, selected_window))
10745 CHARPOS (this_line_start_pos) = 0;
10747 return window_height_changed_p;
10752 /***********************************************************************
10753 Mode Lines and Frame Titles
10754 ***********************************************************************/
10756 /* A buffer for constructing non-propertized mode-line strings and
10757 frame titles in it; allocated from the heap in init_xdisp and
10758 resized as needed in store_mode_line_noprop_char. */
10760 static char *mode_line_noprop_buf;
10762 /* The buffer's end, and a current output position in it. */
10764 static char *mode_line_noprop_buf_end;
10765 static char *mode_line_noprop_ptr;
10767 #define MODE_LINE_NOPROP_LEN(start) \
10768 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10770 static enum {
10771 MODE_LINE_DISPLAY = 0,
10772 MODE_LINE_TITLE,
10773 MODE_LINE_NOPROP,
10774 MODE_LINE_STRING
10775 } mode_line_target;
10777 /* Alist that caches the results of :propertize.
10778 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10779 static Lisp_Object mode_line_proptrans_alist;
10781 /* List of strings making up the mode-line. */
10782 static Lisp_Object mode_line_string_list;
10784 /* Base face property when building propertized mode line string. */
10785 static Lisp_Object mode_line_string_face;
10786 static Lisp_Object mode_line_string_face_prop;
10789 /* Unwind data for mode line strings */
10791 static Lisp_Object Vmode_line_unwind_vector;
10793 static Lisp_Object
10794 format_mode_line_unwind_data (struct buffer *obuf,
10795 Lisp_Object owin,
10796 int save_proptrans)
10798 Lisp_Object vector, tmp;
10800 /* Reduce consing by keeping one vector in
10801 Vwith_echo_area_save_vector. */
10802 vector = Vmode_line_unwind_vector;
10803 Vmode_line_unwind_vector = Qnil;
10805 if (NILP (vector))
10806 vector = Fmake_vector (make_number (8), Qnil);
10808 ASET (vector, 0, make_number (mode_line_target));
10809 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10810 ASET (vector, 2, mode_line_string_list);
10811 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10812 ASET (vector, 4, mode_line_string_face);
10813 ASET (vector, 5, mode_line_string_face_prop);
10815 if (obuf)
10816 XSETBUFFER (tmp, obuf);
10817 else
10818 tmp = Qnil;
10819 ASET (vector, 6, tmp);
10820 ASET (vector, 7, owin);
10822 return vector;
10825 static Lisp_Object
10826 unwind_format_mode_line (Lisp_Object vector)
10828 mode_line_target = XINT (AREF (vector, 0));
10829 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10830 mode_line_string_list = AREF (vector, 2);
10831 if (! EQ (AREF (vector, 3), Qt))
10832 mode_line_proptrans_alist = AREF (vector, 3);
10833 mode_line_string_face = AREF (vector, 4);
10834 mode_line_string_face_prop = AREF (vector, 5);
10836 if (!NILP (AREF (vector, 7)))
10837 /* Select window before buffer, since it may change the buffer. */
10838 Fselect_window (AREF (vector, 7), Qt);
10840 if (!NILP (AREF (vector, 6)))
10842 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10843 ASET (vector, 6, Qnil);
10846 Vmode_line_unwind_vector = vector;
10847 return Qnil;
10851 /* Store a single character C for the frame title in mode_line_noprop_buf.
10852 Re-allocate mode_line_noprop_buf if necessary. */
10854 static void
10855 store_mode_line_noprop_char (char c)
10857 /* If output position has reached the end of the allocated buffer,
10858 increase the buffer's size. */
10859 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10861 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10862 ptrdiff_t size = len;
10863 mode_line_noprop_buf =
10864 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10865 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10866 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10869 *mode_line_noprop_ptr++ = c;
10873 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10874 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10875 characters that yield more columns than PRECISION; PRECISION <= 0
10876 means copy the whole string. Pad with spaces until FIELD_WIDTH
10877 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10878 pad. Called from display_mode_element when it is used to build a
10879 frame title. */
10881 static int
10882 store_mode_line_noprop (const char *string, int field_width, int precision)
10884 const unsigned char *str = (const unsigned char *) string;
10885 int n = 0;
10886 EMACS_INT dummy, nbytes;
10888 /* Copy at most PRECISION chars from STR. */
10889 nbytes = strlen (string);
10890 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10891 while (nbytes--)
10892 store_mode_line_noprop_char (*str++);
10894 /* Fill up with spaces until FIELD_WIDTH reached. */
10895 while (field_width > 0
10896 && n < field_width)
10898 store_mode_line_noprop_char (' ');
10899 ++n;
10902 return n;
10905 /***********************************************************************
10906 Frame Titles
10907 ***********************************************************************/
10909 #ifdef HAVE_WINDOW_SYSTEM
10911 /* Set the title of FRAME, if it has changed. The title format is
10912 Vicon_title_format if FRAME is iconified, otherwise it is
10913 frame_title_format. */
10915 static void
10916 x_consider_frame_title (Lisp_Object frame)
10918 struct frame *f = XFRAME (frame);
10920 if (FRAME_WINDOW_P (f)
10921 || FRAME_MINIBUF_ONLY_P (f)
10922 || f->explicit_name)
10924 /* Do we have more than one visible frame on this X display? */
10925 Lisp_Object tail;
10926 Lisp_Object fmt;
10927 ptrdiff_t title_start;
10928 char *title;
10929 ptrdiff_t len;
10930 struct it it;
10931 int count = SPECPDL_INDEX ();
10933 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
10935 Lisp_Object other_frame = XCAR (tail);
10936 struct frame *tf = XFRAME (other_frame);
10938 if (tf != f
10939 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10940 && !FRAME_MINIBUF_ONLY_P (tf)
10941 && !EQ (other_frame, tip_frame)
10942 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10943 break;
10946 /* Set global variable indicating that multiple frames exist. */
10947 multiple_frames = CONSP (tail);
10949 /* Switch to the buffer of selected window of the frame. Set up
10950 mode_line_target so that display_mode_element will output into
10951 mode_line_noprop_buf; then display the title. */
10952 record_unwind_protect (unwind_format_mode_line,
10953 format_mode_line_unwind_data
10954 (current_buffer, selected_window, 0));
10956 Fselect_window (f->selected_window, Qt);
10957 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
10958 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
10960 mode_line_target = MODE_LINE_TITLE;
10961 title_start = MODE_LINE_NOPROP_LEN (0);
10962 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
10963 NULL, DEFAULT_FACE_ID);
10964 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
10965 len = MODE_LINE_NOPROP_LEN (title_start);
10966 title = mode_line_noprop_buf + title_start;
10967 unbind_to (count, Qnil);
10969 /* Set the title only if it's changed. This avoids consing in
10970 the common case where it hasn't. (If it turns out that we've
10971 already wasted too much time by walking through the list with
10972 display_mode_element, then we might need to optimize at a
10973 higher level than this.) */
10974 if (! STRINGP (f->name)
10975 || SBYTES (f->name) != len
10976 || memcmp (title, SDATA (f->name), len) != 0)
10977 x_implicitly_set_name (f, make_string (title, len), Qnil);
10981 #endif /* not HAVE_WINDOW_SYSTEM */
10986 /***********************************************************************
10987 Menu Bars
10988 ***********************************************************************/
10991 /* Prepare for redisplay by updating menu-bar item lists when
10992 appropriate. This can call eval. */
10994 void
10995 prepare_menu_bars (void)
10997 int all_windows;
10998 struct gcpro gcpro1, gcpro2;
10999 struct frame *f;
11000 Lisp_Object tooltip_frame;
11002 #ifdef HAVE_WINDOW_SYSTEM
11003 tooltip_frame = tip_frame;
11004 #else
11005 tooltip_frame = Qnil;
11006 #endif
11008 /* Update all frame titles based on their buffer names, etc. We do
11009 this before the menu bars so that the buffer-menu will show the
11010 up-to-date frame titles. */
11011 #ifdef HAVE_WINDOW_SYSTEM
11012 if (windows_or_buffers_changed || update_mode_lines)
11014 Lisp_Object tail, frame;
11016 FOR_EACH_FRAME (tail, frame)
11018 f = XFRAME (frame);
11019 if (!EQ (frame, tooltip_frame)
11020 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11021 x_consider_frame_title (frame);
11024 #endif /* HAVE_WINDOW_SYSTEM */
11026 /* Update the menu bar item lists, if appropriate. This has to be
11027 done before any actual redisplay or generation of display lines. */
11028 all_windows = (update_mode_lines
11029 || buffer_shared > 1
11030 || windows_or_buffers_changed);
11031 if (all_windows)
11033 Lisp_Object tail, frame;
11034 int count = SPECPDL_INDEX ();
11035 /* 1 means that update_menu_bar has run its hooks
11036 so any further calls to update_menu_bar shouldn't do so again. */
11037 int menu_bar_hooks_run = 0;
11039 record_unwind_save_match_data ();
11041 FOR_EACH_FRAME (tail, frame)
11043 f = XFRAME (frame);
11045 /* Ignore tooltip frame. */
11046 if (EQ (frame, tooltip_frame))
11047 continue;
11049 /* If a window on this frame changed size, report that to
11050 the user and clear the size-change flag. */
11051 if (FRAME_WINDOW_SIZES_CHANGED (f))
11053 Lisp_Object functions;
11055 /* Clear flag first in case we get an error below. */
11056 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11057 functions = Vwindow_size_change_functions;
11058 GCPRO2 (tail, functions);
11060 while (CONSP (functions))
11062 if (!EQ (XCAR (functions), Qt))
11063 call1 (XCAR (functions), frame);
11064 functions = XCDR (functions);
11066 UNGCPRO;
11069 GCPRO1 (tail);
11070 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11071 #ifdef HAVE_WINDOW_SYSTEM
11072 update_tool_bar (f, 0);
11073 #endif
11074 #ifdef HAVE_NS
11075 if (windows_or_buffers_changed
11076 && FRAME_NS_P (f))
11077 ns_set_doc_edited (f, Fbuffer_modified_p
11078 (XWINDOW (f->selected_window)->buffer));
11079 #endif
11080 UNGCPRO;
11083 unbind_to (count, Qnil);
11085 else
11087 struct frame *sf = SELECTED_FRAME ();
11088 update_menu_bar (sf, 1, 0);
11089 #ifdef HAVE_WINDOW_SYSTEM
11090 update_tool_bar (sf, 1);
11091 #endif
11096 /* Update the menu bar item list for frame F. This has to be done
11097 before we start to fill in any display lines, because it can call
11098 eval.
11100 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11102 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11103 already ran the menu bar hooks for this redisplay, so there
11104 is no need to run them again. The return value is the
11105 updated value of this flag, to pass to the next call. */
11107 static int
11108 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11110 Lisp_Object window;
11111 register struct window *w;
11113 /* If called recursively during a menu update, do nothing. This can
11114 happen when, for instance, an activate-menubar-hook causes a
11115 redisplay. */
11116 if (inhibit_menubar_update)
11117 return hooks_run;
11119 window = FRAME_SELECTED_WINDOW (f);
11120 w = XWINDOW (window);
11122 if (FRAME_WINDOW_P (f)
11124 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11125 || defined (HAVE_NS) || defined (USE_GTK)
11126 FRAME_EXTERNAL_MENU_BAR (f)
11127 #else
11128 FRAME_MENU_BAR_LINES (f) > 0
11129 #endif
11130 : FRAME_MENU_BAR_LINES (f) > 0)
11132 /* If the user has switched buffers or windows, we need to
11133 recompute to reflect the new bindings. But we'll
11134 recompute when update_mode_lines is set too; that means
11135 that people can use force-mode-line-update to request
11136 that the menu bar be recomputed. The adverse effect on
11137 the rest of the redisplay algorithm is about the same as
11138 windows_or_buffers_changed anyway. */
11139 if (windows_or_buffers_changed
11140 /* This used to test w->update_mode_line, but we believe
11141 there is no need to recompute the menu in that case. */
11142 || update_mode_lines
11143 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11144 < BUF_MODIFF (XBUFFER (w->buffer)))
11145 != !NILP (w->last_had_star))
11146 || ((!NILP (Vtransient_mark_mode)
11147 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11148 != !NILP (w->region_showing)))
11150 struct buffer *prev = current_buffer;
11151 int count = SPECPDL_INDEX ();
11153 specbind (Qinhibit_menubar_update, Qt);
11155 set_buffer_internal_1 (XBUFFER (w->buffer));
11156 if (save_match_data)
11157 record_unwind_save_match_data ();
11158 if (NILP (Voverriding_local_map_menu_flag))
11160 specbind (Qoverriding_terminal_local_map, Qnil);
11161 specbind (Qoverriding_local_map, Qnil);
11164 if (!hooks_run)
11166 /* Run the Lucid hook. */
11167 safe_run_hooks (Qactivate_menubar_hook);
11169 /* If it has changed current-menubar from previous value,
11170 really recompute the menu-bar from the value. */
11171 if (! NILP (Vlucid_menu_bar_dirty_flag))
11172 call0 (Qrecompute_lucid_menubar);
11174 safe_run_hooks (Qmenu_bar_update_hook);
11176 hooks_run = 1;
11179 XSETFRAME (Vmenu_updating_frame, f);
11180 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
11182 /* Redisplay the menu bar in case we changed it. */
11183 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11184 || defined (HAVE_NS) || defined (USE_GTK)
11185 if (FRAME_WINDOW_P (f))
11187 #if defined (HAVE_NS)
11188 /* All frames on Mac OS share the same menubar. So only
11189 the selected frame should be allowed to set it. */
11190 if (f == SELECTED_FRAME ())
11191 #endif
11192 set_frame_menubar (f, 0, 0);
11194 else
11195 /* On a terminal screen, the menu bar is an ordinary screen
11196 line, and this makes it get updated. */
11197 w->update_mode_line = Qt;
11198 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11199 /* In the non-toolkit version, the menu bar is an ordinary screen
11200 line, and this makes it get updated. */
11201 w->update_mode_line = Qt;
11202 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11204 unbind_to (count, Qnil);
11205 set_buffer_internal_1 (prev);
11209 return hooks_run;
11214 /***********************************************************************
11215 Output Cursor
11216 ***********************************************************************/
11218 #ifdef HAVE_WINDOW_SYSTEM
11220 /* EXPORT:
11221 Nominal cursor position -- where to draw output.
11222 HPOS and VPOS are window relative glyph matrix coordinates.
11223 X and Y are window relative pixel coordinates. */
11225 struct cursor_pos output_cursor;
11228 /* EXPORT:
11229 Set the global variable output_cursor to CURSOR. All cursor
11230 positions are relative to updated_window. */
11232 void
11233 set_output_cursor (struct cursor_pos *cursor)
11235 output_cursor.hpos = cursor->hpos;
11236 output_cursor.vpos = cursor->vpos;
11237 output_cursor.x = cursor->x;
11238 output_cursor.y = cursor->y;
11242 /* EXPORT for RIF:
11243 Set a nominal cursor position.
11245 HPOS and VPOS are column/row positions in a window glyph matrix. X
11246 and Y are window text area relative pixel positions.
11248 If this is done during an update, updated_window will contain the
11249 window that is being updated and the position is the future output
11250 cursor position for that window. If updated_window is null, use
11251 selected_window and display the cursor at the given position. */
11253 void
11254 x_cursor_to (int vpos, int hpos, int y, int x)
11256 struct window *w;
11258 /* If updated_window is not set, work on selected_window. */
11259 if (updated_window)
11260 w = updated_window;
11261 else
11262 w = XWINDOW (selected_window);
11264 /* Set the output cursor. */
11265 output_cursor.hpos = hpos;
11266 output_cursor.vpos = vpos;
11267 output_cursor.x = x;
11268 output_cursor.y = y;
11270 /* If not called as part of an update, really display the cursor.
11271 This will also set the cursor position of W. */
11272 if (updated_window == NULL)
11274 BLOCK_INPUT;
11275 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11276 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11277 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11278 UNBLOCK_INPUT;
11282 #endif /* HAVE_WINDOW_SYSTEM */
11285 /***********************************************************************
11286 Tool-bars
11287 ***********************************************************************/
11289 #ifdef HAVE_WINDOW_SYSTEM
11291 /* Where the mouse was last time we reported a mouse event. */
11293 FRAME_PTR last_mouse_frame;
11295 /* Tool-bar item index of the item on which a mouse button was pressed
11296 or -1. */
11298 int last_tool_bar_item;
11301 static Lisp_Object
11302 update_tool_bar_unwind (Lisp_Object frame)
11304 selected_frame = frame;
11305 return Qnil;
11308 /* Update the tool-bar item list for frame F. This has to be done
11309 before we start to fill in any display lines. Called from
11310 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11311 and restore it here. */
11313 static void
11314 update_tool_bar (struct frame *f, int save_match_data)
11316 #if defined (USE_GTK) || defined (HAVE_NS)
11317 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11318 #else
11319 int do_update = WINDOWP (f->tool_bar_window)
11320 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11321 #endif
11323 if (do_update)
11325 Lisp_Object window;
11326 struct window *w;
11328 window = FRAME_SELECTED_WINDOW (f);
11329 w = XWINDOW (window);
11331 /* If the user has switched buffers or windows, we need to
11332 recompute to reflect the new bindings. But we'll
11333 recompute when update_mode_lines is set too; that means
11334 that people can use force-mode-line-update to request
11335 that the menu bar be recomputed. The adverse effect on
11336 the rest of the redisplay algorithm is about the same as
11337 windows_or_buffers_changed anyway. */
11338 if (windows_or_buffers_changed
11339 || !NILP (w->update_mode_line)
11340 || update_mode_lines
11341 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11342 < BUF_MODIFF (XBUFFER (w->buffer)))
11343 != !NILP (w->last_had_star))
11344 || ((!NILP (Vtransient_mark_mode)
11345 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11346 != !NILP (w->region_showing)))
11348 struct buffer *prev = current_buffer;
11349 int count = SPECPDL_INDEX ();
11350 Lisp_Object frame, new_tool_bar;
11351 int new_n_tool_bar;
11352 struct gcpro gcpro1;
11354 /* Set current_buffer to the buffer of the selected
11355 window of the frame, so that we get the right local
11356 keymaps. */
11357 set_buffer_internal_1 (XBUFFER (w->buffer));
11359 /* Save match data, if we must. */
11360 if (save_match_data)
11361 record_unwind_save_match_data ();
11363 /* Make sure that we don't accidentally use bogus keymaps. */
11364 if (NILP (Voverriding_local_map_menu_flag))
11366 specbind (Qoverriding_terminal_local_map, Qnil);
11367 specbind (Qoverriding_local_map, Qnil);
11370 GCPRO1 (new_tool_bar);
11372 /* We must temporarily set the selected frame to this frame
11373 before calling tool_bar_items, because the calculation of
11374 the tool-bar keymap uses the selected frame (see
11375 `tool-bar-make-keymap' in tool-bar.el). */
11376 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11377 XSETFRAME (frame, f);
11378 selected_frame = frame;
11380 /* Build desired tool-bar items from keymaps. */
11381 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11382 &new_n_tool_bar);
11384 /* Redisplay the tool-bar if we changed it. */
11385 if (new_n_tool_bar != f->n_tool_bar_items
11386 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11388 /* Redisplay that happens asynchronously due to an expose event
11389 may access f->tool_bar_items. Make sure we update both
11390 variables within BLOCK_INPUT so no such event interrupts. */
11391 BLOCK_INPUT;
11392 f->tool_bar_items = new_tool_bar;
11393 f->n_tool_bar_items = new_n_tool_bar;
11394 w->update_mode_line = Qt;
11395 UNBLOCK_INPUT;
11398 UNGCPRO;
11400 unbind_to (count, Qnil);
11401 set_buffer_internal_1 (prev);
11407 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11408 F's desired tool-bar contents. F->tool_bar_items must have
11409 been set up previously by calling prepare_menu_bars. */
11411 static void
11412 build_desired_tool_bar_string (struct frame *f)
11414 int i, size, size_needed;
11415 struct gcpro gcpro1, gcpro2, gcpro3;
11416 Lisp_Object image, plist, props;
11418 image = plist = props = Qnil;
11419 GCPRO3 (image, plist, props);
11421 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11422 Otherwise, make a new string. */
11424 /* The size of the string we might be able to reuse. */
11425 size = (STRINGP (f->desired_tool_bar_string)
11426 ? SCHARS (f->desired_tool_bar_string)
11427 : 0);
11429 /* We need one space in the string for each image. */
11430 size_needed = f->n_tool_bar_items;
11432 /* Reuse f->desired_tool_bar_string, if possible. */
11433 if (size < size_needed || NILP (f->desired_tool_bar_string))
11434 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
11435 make_number (' '));
11436 else
11438 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11439 Fremove_text_properties (make_number (0), make_number (size),
11440 props, f->desired_tool_bar_string);
11443 /* Put a `display' property on the string for the images to display,
11444 put a `menu_item' property on tool-bar items with a value that
11445 is the index of the item in F's tool-bar item vector. */
11446 for (i = 0; i < f->n_tool_bar_items; ++i)
11448 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11450 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11451 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11452 int hmargin, vmargin, relief, idx, end;
11454 /* If image is a vector, choose the image according to the
11455 button state. */
11456 image = PROP (TOOL_BAR_ITEM_IMAGES);
11457 if (VECTORP (image))
11459 if (enabled_p)
11460 idx = (selected_p
11461 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11462 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11463 else
11464 idx = (selected_p
11465 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11466 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11468 xassert (ASIZE (image) >= idx);
11469 image = AREF (image, idx);
11471 else
11472 idx = -1;
11474 /* Ignore invalid image specifications. */
11475 if (!valid_image_p (image))
11476 continue;
11478 /* Display the tool-bar button pressed, or depressed. */
11479 plist = Fcopy_sequence (XCDR (image));
11481 /* Compute margin and relief to draw. */
11482 relief = (tool_bar_button_relief >= 0
11483 ? tool_bar_button_relief
11484 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11485 hmargin = vmargin = relief;
11487 if (INTEGERP (Vtool_bar_button_margin)
11488 && XINT (Vtool_bar_button_margin) > 0)
11490 hmargin += XFASTINT (Vtool_bar_button_margin);
11491 vmargin += XFASTINT (Vtool_bar_button_margin);
11493 else if (CONSP (Vtool_bar_button_margin))
11495 if (INTEGERP (XCAR (Vtool_bar_button_margin))
11496 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
11497 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11499 if (INTEGERP (XCDR (Vtool_bar_button_margin))
11500 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
11501 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11504 if (auto_raise_tool_bar_buttons_p)
11506 /* Add a `:relief' property to the image spec if the item is
11507 selected. */
11508 if (selected_p)
11510 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11511 hmargin -= relief;
11512 vmargin -= relief;
11515 else
11517 /* If image is selected, display it pressed, i.e. with a
11518 negative relief. If it's not selected, display it with a
11519 raised relief. */
11520 plist = Fplist_put (plist, QCrelief,
11521 (selected_p
11522 ? make_number (-relief)
11523 : make_number (relief)));
11524 hmargin -= relief;
11525 vmargin -= relief;
11528 /* Put a margin around the image. */
11529 if (hmargin || vmargin)
11531 if (hmargin == vmargin)
11532 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11533 else
11534 plist = Fplist_put (plist, QCmargin,
11535 Fcons (make_number (hmargin),
11536 make_number (vmargin)));
11539 /* If button is not enabled, and we don't have special images
11540 for the disabled state, make the image appear disabled by
11541 applying an appropriate algorithm to it. */
11542 if (!enabled_p && idx < 0)
11543 plist = Fplist_put (plist, QCconversion, Qdisabled);
11545 /* Put a `display' text property on the string for the image to
11546 display. Put a `menu-item' property on the string that gives
11547 the start of this item's properties in the tool-bar items
11548 vector. */
11549 image = Fcons (Qimage, plist);
11550 props = list4 (Qdisplay, image,
11551 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11553 /* Let the last image hide all remaining spaces in the tool bar
11554 string. The string can be longer than needed when we reuse a
11555 previous string. */
11556 if (i + 1 == f->n_tool_bar_items)
11557 end = SCHARS (f->desired_tool_bar_string);
11558 else
11559 end = i + 1;
11560 Fadd_text_properties (make_number (i), make_number (end),
11561 props, f->desired_tool_bar_string);
11562 #undef PROP
11565 UNGCPRO;
11569 /* Display one line of the tool-bar of frame IT->f.
11571 HEIGHT specifies the desired height of the tool-bar line.
11572 If the actual height of the glyph row is less than HEIGHT, the
11573 row's height is increased to HEIGHT, and the icons are centered
11574 vertically in the new height.
11576 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11577 count a final empty row in case the tool-bar width exactly matches
11578 the window width.
11581 static void
11582 display_tool_bar_line (struct it *it, int height)
11584 struct glyph_row *row = it->glyph_row;
11585 int max_x = it->last_visible_x;
11586 struct glyph *last;
11588 prepare_desired_row (row);
11589 row->y = it->current_y;
11591 /* Note that this isn't made use of if the face hasn't a box,
11592 so there's no need to check the face here. */
11593 it->start_of_box_run_p = 1;
11595 while (it->current_x < max_x)
11597 int x, n_glyphs_before, i, nglyphs;
11598 struct it it_before;
11600 /* Get the next display element. */
11601 if (!get_next_display_element (it))
11603 /* Don't count empty row if we are counting needed tool-bar lines. */
11604 if (height < 0 && !it->hpos)
11605 return;
11606 break;
11609 /* Produce glyphs. */
11610 n_glyphs_before = row->used[TEXT_AREA];
11611 it_before = *it;
11613 PRODUCE_GLYPHS (it);
11615 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11616 i = 0;
11617 x = it_before.current_x;
11618 while (i < nglyphs)
11620 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11622 if (x + glyph->pixel_width > max_x)
11624 /* Glyph doesn't fit on line. Backtrack. */
11625 row->used[TEXT_AREA] = n_glyphs_before;
11626 *it = it_before;
11627 /* If this is the only glyph on this line, it will never fit on the
11628 tool-bar, so skip it. But ensure there is at least one glyph,
11629 so we don't accidentally disable the tool-bar. */
11630 if (n_glyphs_before == 0
11631 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11632 break;
11633 goto out;
11636 ++it->hpos;
11637 x += glyph->pixel_width;
11638 ++i;
11641 /* Stop at line end. */
11642 if (ITERATOR_AT_END_OF_LINE_P (it))
11643 break;
11645 set_iterator_to_next (it, 1);
11648 out:;
11650 row->displays_text_p = row->used[TEXT_AREA] != 0;
11652 /* Use default face for the border below the tool bar.
11654 FIXME: When auto-resize-tool-bars is grow-only, there is
11655 no additional border below the possibly empty tool-bar lines.
11656 So to make the extra empty lines look "normal", we have to
11657 use the tool-bar face for the border too. */
11658 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11659 it->face_id = DEFAULT_FACE_ID;
11661 extend_face_to_end_of_line (it);
11662 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11663 last->right_box_line_p = 1;
11664 if (last == row->glyphs[TEXT_AREA])
11665 last->left_box_line_p = 1;
11667 /* Make line the desired height and center it vertically. */
11668 if ((height -= it->max_ascent + it->max_descent) > 0)
11670 /* Don't add more than one line height. */
11671 height %= FRAME_LINE_HEIGHT (it->f);
11672 it->max_ascent += height / 2;
11673 it->max_descent += (height + 1) / 2;
11676 compute_line_metrics (it);
11678 /* If line is empty, make it occupy the rest of the tool-bar. */
11679 if (!row->displays_text_p)
11681 row->height = row->phys_height = it->last_visible_y - row->y;
11682 row->visible_height = row->height;
11683 row->ascent = row->phys_ascent = 0;
11684 row->extra_line_spacing = 0;
11687 row->full_width_p = 1;
11688 row->continued_p = 0;
11689 row->truncated_on_left_p = 0;
11690 row->truncated_on_right_p = 0;
11692 it->current_x = it->hpos = 0;
11693 it->current_y += row->height;
11694 ++it->vpos;
11695 ++it->glyph_row;
11699 /* Max tool-bar height. */
11701 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11702 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11704 /* Value is the number of screen lines needed to make all tool-bar
11705 items of frame F visible. The number of actual rows needed is
11706 returned in *N_ROWS if non-NULL. */
11708 static int
11709 tool_bar_lines_needed (struct frame *f, int *n_rows)
11711 struct window *w = XWINDOW (f->tool_bar_window);
11712 struct it it;
11713 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11714 the desired matrix, so use (unused) mode-line row as temporary row to
11715 avoid destroying the first tool-bar row. */
11716 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11718 /* Initialize an iterator for iteration over
11719 F->desired_tool_bar_string in the tool-bar window of frame F. */
11720 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11721 it.first_visible_x = 0;
11722 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11723 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11724 it.paragraph_embedding = L2R;
11726 while (!ITERATOR_AT_END_P (&it))
11728 clear_glyph_row (temp_row);
11729 it.glyph_row = temp_row;
11730 display_tool_bar_line (&it, -1);
11732 clear_glyph_row (temp_row);
11734 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11735 if (n_rows)
11736 *n_rows = it.vpos > 0 ? it.vpos : -1;
11738 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11742 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11743 0, 1, 0,
11744 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11745 (Lisp_Object frame)
11747 struct frame *f;
11748 struct window *w;
11749 int nlines = 0;
11751 if (NILP (frame))
11752 frame = selected_frame;
11753 else
11754 CHECK_FRAME (frame);
11755 f = XFRAME (frame);
11757 if (WINDOWP (f->tool_bar_window)
11758 && (w = XWINDOW (f->tool_bar_window),
11759 WINDOW_TOTAL_LINES (w) > 0))
11761 update_tool_bar (f, 1);
11762 if (f->n_tool_bar_items)
11764 build_desired_tool_bar_string (f);
11765 nlines = tool_bar_lines_needed (f, NULL);
11769 return make_number (nlines);
11773 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11774 height should be changed. */
11776 static int
11777 redisplay_tool_bar (struct frame *f)
11779 struct window *w;
11780 struct it it;
11781 struct glyph_row *row;
11783 #if defined (USE_GTK) || defined (HAVE_NS)
11784 if (FRAME_EXTERNAL_TOOL_BAR (f))
11785 update_frame_tool_bar (f);
11786 return 0;
11787 #endif
11789 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11790 do anything. This means you must start with tool-bar-lines
11791 non-zero to get the auto-sizing effect. Or in other words, you
11792 can turn off tool-bars by specifying tool-bar-lines zero. */
11793 if (!WINDOWP (f->tool_bar_window)
11794 || (w = XWINDOW (f->tool_bar_window),
11795 WINDOW_TOTAL_LINES (w) == 0))
11796 return 0;
11798 /* Set up an iterator for the tool-bar window. */
11799 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11800 it.first_visible_x = 0;
11801 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11802 row = it.glyph_row;
11804 /* Build a string that represents the contents of the tool-bar. */
11805 build_desired_tool_bar_string (f);
11806 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11807 /* FIXME: This should be controlled by a user option. But it
11808 doesn't make sense to have an R2L tool bar if the menu bar cannot
11809 be drawn also R2L, and making the menu bar R2L is tricky due
11810 toolkit-specific code that implements it. If an R2L tool bar is
11811 ever supported, display_tool_bar_line should also be augmented to
11812 call unproduce_glyphs like display_line and display_string
11813 do. */
11814 it.paragraph_embedding = L2R;
11816 if (f->n_tool_bar_rows == 0)
11818 int nlines;
11820 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11821 nlines != WINDOW_TOTAL_LINES (w)))
11823 Lisp_Object frame;
11824 int old_height = WINDOW_TOTAL_LINES (w);
11826 XSETFRAME (frame, f);
11827 Fmodify_frame_parameters (frame,
11828 Fcons (Fcons (Qtool_bar_lines,
11829 make_number (nlines)),
11830 Qnil));
11831 if (WINDOW_TOTAL_LINES (w) != old_height)
11833 clear_glyph_matrix (w->desired_matrix);
11834 fonts_changed_p = 1;
11835 return 1;
11840 /* Display as many lines as needed to display all tool-bar items. */
11842 if (f->n_tool_bar_rows > 0)
11844 int border, rows, height, extra;
11846 if (INTEGERP (Vtool_bar_border))
11847 border = XINT (Vtool_bar_border);
11848 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11849 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11850 else if (EQ (Vtool_bar_border, Qborder_width))
11851 border = f->border_width;
11852 else
11853 border = 0;
11854 if (border < 0)
11855 border = 0;
11857 rows = f->n_tool_bar_rows;
11858 height = max (1, (it.last_visible_y - border) / rows);
11859 extra = it.last_visible_y - border - height * rows;
11861 while (it.current_y < it.last_visible_y)
11863 int h = 0;
11864 if (extra > 0 && rows-- > 0)
11866 h = (extra + rows - 1) / rows;
11867 extra -= h;
11869 display_tool_bar_line (&it, height + h);
11872 else
11874 while (it.current_y < it.last_visible_y)
11875 display_tool_bar_line (&it, 0);
11878 /* It doesn't make much sense to try scrolling in the tool-bar
11879 window, so don't do it. */
11880 w->desired_matrix->no_scrolling_p = 1;
11881 w->must_be_updated_p = 1;
11883 if (!NILP (Vauto_resize_tool_bars))
11885 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11886 int change_height_p = 0;
11888 /* If we couldn't display everything, change the tool-bar's
11889 height if there is room for more. */
11890 if (IT_STRING_CHARPOS (it) < it.end_charpos
11891 && it.current_y < max_tool_bar_height)
11892 change_height_p = 1;
11894 row = it.glyph_row - 1;
11896 /* If there are blank lines at the end, except for a partially
11897 visible blank line at the end that is smaller than
11898 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11899 if (!row->displays_text_p
11900 && row->height >= FRAME_LINE_HEIGHT (f))
11901 change_height_p = 1;
11903 /* If row displays tool-bar items, but is partially visible,
11904 change the tool-bar's height. */
11905 if (row->displays_text_p
11906 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11907 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11908 change_height_p = 1;
11910 /* Resize windows as needed by changing the `tool-bar-lines'
11911 frame parameter. */
11912 if (change_height_p)
11914 Lisp_Object frame;
11915 int old_height = WINDOW_TOTAL_LINES (w);
11916 int nrows;
11917 int nlines = tool_bar_lines_needed (f, &nrows);
11919 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11920 && !f->minimize_tool_bar_window_p)
11921 ? (nlines > old_height)
11922 : (nlines != old_height));
11923 f->minimize_tool_bar_window_p = 0;
11925 if (change_height_p)
11927 XSETFRAME (frame, f);
11928 Fmodify_frame_parameters (frame,
11929 Fcons (Fcons (Qtool_bar_lines,
11930 make_number (nlines)),
11931 Qnil));
11932 if (WINDOW_TOTAL_LINES (w) != old_height)
11934 clear_glyph_matrix (w->desired_matrix);
11935 f->n_tool_bar_rows = nrows;
11936 fonts_changed_p = 1;
11937 return 1;
11943 f->minimize_tool_bar_window_p = 0;
11944 return 0;
11948 /* Get information about the tool-bar item which is displayed in GLYPH
11949 on frame F. Return in *PROP_IDX the index where tool-bar item
11950 properties start in F->tool_bar_items. Value is zero if
11951 GLYPH doesn't display a tool-bar item. */
11953 static int
11954 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11956 Lisp_Object prop;
11957 int success_p;
11958 int charpos;
11960 /* This function can be called asynchronously, which means we must
11961 exclude any possibility that Fget_text_property signals an
11962 error. */
11963 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
11964 charpos = max (0, charpos);
11966 /* Get the text property `menu-item' at pos. The value of that
11967 property is the start index of this item's properties in
11968 F->tool_bar_items. */
11969 prop = Fget_text_property (make_number (charpos),
11970 Qmenu_item, f->current_tool_bar_string);
11971 if (INTEGERP (prop))
11973 *prop_idx = XINT (prop);
11974 success_p = 1;
11976 else
11977 success_p = 0;
11979 return success_p;
11983 /* Get information about the tool-bar item at position X/Y on frame F.
11984 Return in *GLYPH a pointer to the glyph of the tool-bar item in
11985 the current matrix of the tool-bar window of F, or NULL if not
11986 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
11987 item in F->tool_bar_items. Value is
11989 -1 if X/Y is not on a tool-bar item
11990 0 if X/Y is on the same item that was highlighted before.
11991 1 otherwise. */
11993 static int
11994 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
11995 int *hpos, int *vpos, int *prop_idx)
11997 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11998 struct window *w = XWINDOW (f->tool_bar_window);
11999 int area;
12001 /* Find the glyph under X/Y. */
12002 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12003 if (*glyph == NULL)
12004 return -1;
12006 /* Get the start of this tool-bar item's properties in
12007 f->tool_bar_items. */
12008 if (!tool_bar_item_info (f, *glyph, prop_idx))
12009 return -1;
12011 /* Is mouse on the highlighted item? */
12012 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12013 && *vpos >= hlinfo->mouse_face_beg_row
12014 && *vpos <= hlinfo->mouse_face_end_row
12015 && (*vpos > hlinfo->mouse_face_beg_row
12016 || *hpos >= hlinfo->mouse_face_beg_col)
12017 && (*vpos < hlinfo->mouse_face_end_row
12018 || *hpos < hlinfo->mouse_face_end_col
12019 || hlinfo->mouse_face_past_end))
12020 return 0;
12022 return 1;
12026 /* EXPORT:
12027 Handle mouse button event on the tool-bar of frame F, at
12028 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12029 0 for button release. MODIFIERS is event modifiers for button
12030 release. */
12032 void
12033 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12034 unsigned int modifiers)
12036 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12037 struct window *w = XWINDOW (f->tool_bar_window);
12038 int hpos, vpos, prop_idx;
12039 struct glyph *glyph;
12040 Lisp_Object enabled_p;
12042 /* If not on the highlighted tool-bar item, return. */
12043 frame_to_window_pixel_xy (w, &x, &y);
12044 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12045 return;
12047 /* If item is disabled, do nothing. */
12048 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12049 if (NILP (enabled_p))
12050 return;
12052 if (down_p)
12054 /* Show item in pressed state. */
12055 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12056 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
12057 last_tool_bar_item = prop_idx;
12059 else
12061 Lisp_Object key, frame;
12062 struct input_event event;
12063 EVENT_INIT (event);
12065 /* Show item in released state. */
12066 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12067 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
12069 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12071 XSETFRAME (frame, f);
12072 event.kind = TOOL_BAR_EVENT;
12073 event.frame_or_window = frame;
12074 event.arg = frame;
12075 kbd_buffer_store_event (&event);
12077 event.kind = TOOL_BAR_EVENT;
12078 event.frame_or_window = frame;
12079 event.arg = key;
12080 event.modifiers = modifiers;
12081 kbd_buffer_store_event (&event);
12082 last_tool_bar_item = -1;
12087 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12088 tool-bar window-relative coordinates X/Y. Called from
12089 note_mouse_highlight. */
12091 static void
12092 note_tool_bar_highlight (struct frame *f, int x, int y)
12094 Lisp_Object window = f->tool_bar_window;
12095 struct window *w = XWINDOW (window);
12096 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12097 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12098 int hpos, vpos;
12099 struct glyph *glyph;
12100 struct glyph_row *row;
12101 int i;
12102 Lisp_Object enabled_p;
12103 int prop_idx;
12104 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12105 int mouse_down_p, rc;
12107 /* Function note_mouse_highlight is called with negative X/Y
12108 values when mouse moves outside of the frame. */
12109 if (x <= 0 || y <= 0)
12111 clear_mouse_face (hlinfo);
12112 return;
12115 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12116 if (rc < 0)
12118 /* Not on tool-bar item. */
12119 clear_mouse_face (hlinfo);
12120 return;
12122 else if (rc == 0)
12123 /* On same tool-bar item as before. */
12124 goto set_help_echo;
12126 clear_mouse_face (hlinfo);
12128 /* Mouse is down, but on different tool-bar item? */
12129 mouse_down_p = (dpyinfo->grabbed
12130 && f == last_mouse_frame
12131 && FRAME_LIVE_P (f));
12132 if (mouse_down_p
12133 && last_tool_bar_item != prop_idx)
12134 return;
12136 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12137 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12139 /* If tool-bar item is not enabled, don't highlight it. */
12140 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12141 if (!NILP (enabled_p))
12143 /* Compute the x-position of the glyph. In front and past the
12144 image is a space. We include this in the highlighted area. */
12145 row = MATRIX_ROW (w->current_matrix, vpos);
12146 for (i = x = 0; i < hpos; ++i)
12147 x += row->glyphs[TEXT_AREA][i].pixel_width;
12149 /* Record this as the current active region. */
12150 hlinfo->mouse_face_beg_col = hpos;
12151 hlinfo->mouse_face_beg_row = vpos;
12152 hlinfo->mouse_face_beg_x = x;
12153 hlinfo->mouse_face_beg_y = row->y;
12154 hlinfo->mouse_face_past_end = 0;
12156 hlinfo->mouse_face_end_col = hpos + 1;
12157 hlinfo->mouse_face_end_row = vpos;
12158 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12159 hlinfo->mouse_face_end_y = row->y;
12160 hlinfo->mouse_face_window = window;
12161 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12163 /* Display it as active. */
12164 show_mouse_face (hlinfo, draw);
12165 hlinfo->mouse_face_image_state = draw;
12168 set_help_echo:
12170 /* Set help_echo_string to a help string to display for this tool-bar item.
12171 XTread_socket does the rest. */
12172 help_echo_object = help_echo_window = Qnil;
12173 help_echo_pos = -1;
12174 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12175 if (NILP (help_echo_string))
12176 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12179 #endif /* HAVE_WINDOW_SYSTEM */
12183 /************************************************************************
12184 Horizontal scrolling
12185 ************************************************************************/
12187 static int hscroll_window_tree (Lisp_Object);
12188 static int hscroll_windows (Lisp_Object);
12190 /* For all leaf windows in the window tree rooted at WINDOW, set their
12191 hscroll value so that PT is (i) visible in the window, and (ii) so
12192 that it is not within a certain margin at the window's left and
12193 right border. Value is non-zero if any window's hscroll has been
12194 changed. */
12196 static int
12197 hscroll_window_tree (Lisp_Object window)
12199 int hscrolled_p = 0;
12200 int hscroll_relative_p = FLOATP (Vhscroll_step);
12201 int hscroll_step_abs = 0;
12202 double hscroll_step_rel = 0;
12204 if (hscroll_relative_p)
12206 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12207 if (hscroll_step_rel < 0)
12209 hscroll_relative_p = 0;
12210 hscroll_step_abs = 0;
12213 else if (INTEGERP (Vhscroll_step))
12215 hscroll_step_abs = XINT (Vhscroll_step);
12216 if (hscroll_step_abs < 0)
12217 hscroll_step_abs = 0;
12219 else
12220 hscroll_step_abs = 0;
12222 while (WINDOWP (window))
12224 struct window *w = XWINDOW (window);
12226 if (WINDOWP (w->hchild))
12227 hscrolled_p |= hscroll_window_tree (w->hchild);
12228 else if (WINDOWP (w->vchild))
12229 hscrolled_p |= hscroll_window_tree (w->vchild);
12230 else if (w->cursor.vpos >= 0)
12232 int h_margin;
12233 int text_area_width;
12234 struct glyph_row *current_cursor_row
12235 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12236 struct glyph_row *desired_cursor_row
12237 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12238 struct glyph_row *cursor_row
12239 = (desired_cursor_row->enabled_p
12240 ? desired_cursor_row
12241 : current_cursor_row);
12242 int row_r2l_p = cursor_row->reversed_p;
12244 text_area_width = window_box_width (w, TEXT_AREA);
12246 /* Scroll when cursor is inside this scroll margin. */
12247 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12249 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12250 /* For left-to-right rows, hscroll when cursor is either
12251 (i) inside the right hscroll margin, or (ii) if it is
12252 inside the left margin and the window is already
12253 hscrolled. */
12254 && ((!row_r2l_p
12255 && ((XFASTINT (w->hscroll)
12256 && w->cursor.x <= h_margin)
12257 || (cursor_row->enabled_p
12258 && cursor_row->truncated_on_right_p
12259 && (w->cursor.x >= text_area_width - h_margin))))
12260 /* For right-to-left rows, the logic is similar,
12261 except that rules for scrolling to left and right
12262 are reversed. E.g., if cursor.x <= h_margin, we
12263 need to hscroll "to the right" unconditionally,
12264 and that will scroll the screen to the left so as
12265 to reveal the next portion of the row. */
12266 || (row_r2l_p
12267 && ((cursor_row->enabled_p
12268 /* FIXME: It is confusing to set the
12269 truncated_on_right_p flag when R2L rows
12270 are actually truncated on the left. */
12271 && cursor_row->truncated_on_right_p
12272 && w->cursor.x <= h_margin)
12273 || (XFASTINT (w->hscroll)
12274 && (w->cursor.x >= text_area_width - h_margin))))))
12276 struct it it;
12277 int hscroll;
12278 struct buffer *saved_current_buffer;
12279 EMACS_INT pt;
12280 int wanted_x;
12282 /* Find point in a display of infinite width. */
12283 saved_current_buffer = current_buffer;
12284 current_buffer = XBUFFER (w->buffer);
12286 if (w == XWINDOW (selected_window))
12287 pt = PT;
12288 else
12290 pt = marker_position (w->pointm);
12291 pt = max (BEGV, pt);
12292 pt = min (ZV, pt);
12295 /* Move iterator to pt starting at cursor_row->start in
12296 a line with infinite width. */
12297 init_to_row_start (&it, w, cursor_row);
12298 it.last_visible_x = INFINITY;
12299 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12300 current_buffer = saved_current_buffer;
12302 /* Position cursor in window. */
12303 if (!hscroll_relative_p && hscroll_step_abs == 0)
12304 hscroll = max (0, (it.current_x
12305 - (ITERATOR_AT_END_OF_LINE_P (&it)
12306 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12307 : (text_area_width / 2))))
12308 / FRAME_COLUMN_WIDTH (it.f);
12309 else if ((!row_r2l_p
12310 && w->cursor.x >= text_area_width - h_margin)
12311 || (row_r2l_p && w->cursor.x <= h_margin))
12313 if (hscroll_relative_p)
12314 wanted_x = text_area_width * (1 - hscroll_step_rel)
12315 - h_margin;
12316 else
12317 wanted_x = text_area_width
12318 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12319 - h_margin;
12320 hscroll
12321 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12323 else
12325 if (hscroll_relative_p)
12326 wanted_x = text_area_width * hscroll_step_rel
12327 + h_margin;
12328 else
12329 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12330 + h_margin;
12331 hscroll
12332 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12334 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
12336 /* Don't prevent redisplay optimizations if hscroll
12337 hasn't changed, as it will unnecessarily slow down
12338 redisplay. */
12339 if (XFASTINT (w->hscroll) != hscroll)
12341 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12342 w->hscroll = make_number (hscroll);
12343 hscrolled_p = 1;
12348 window = w->next;
12351 /* Value is non-zero if hscroll of any leaf window has been changed. */
12352 return hscrolled_p;
12356 /* Set hscroll so that cursor is visible and not inside horizontal
12357 scroll margins for all windows in the tree rooted at WINDOW. See
12358 also hscroll_window_tree above. Value is non-zero if any window's
12359 hscroll has been changed. If it has, desired matrices on the frame
12360 of WINDOW are cleared. */
12362 static int
12363 hscroll_windows (Lisp_Object window)
12365 int hscrolled_p = hscroll_window_tree (window);
12366 if (hscrolled_p)
12367 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12368 return hscrolled_p;
12373 /************************************************************************
12374 Redisplay
12375 ************************************************************************/
12377 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12378 to a non-zero value. This is sometimes handy to have in a debugger
12379 session. */
12381 #if GLYPH_DEBUG
12383 /* First and last unchanged row for try_window_id. */
12385 static int debug_first_unchanged_at_end_vpos;
12386 static int debug_last_unchanged_at_beg_vpos;
12388 /* Delta vpos and y. */
12390 static int debug_dvpos, debug_dy;
12392 /* Delta in characters and bytes for try_window_id. */
12394 static EMACS_INT debug_delta, debug_delta_bytes;
12396 /* Values of window_end_pos and window_end_vpos at the end of
12397 try_window_id. */
12399 static EMACS_INT debug_end_vpos;
12401 /* Append a string to W->desired_matrix->method. FMT is a printf
12402 format string. If trace_redisplay_p is non-zero also printf the
12403 resulting string to stderr. */
12405 static void debug_method_add (struct window *, char const *, ...)
12406 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12408 static void
12409 debug_method_add (struct window *w, char const *fmt, ...)
12411 char buffer[512];
12412 char *method = w->desired_matrix->method;
12413 int len = strlen (method);
12414 int size = sizeof w->desired_matrix->method;
12415 int remaining = size - len - 1;
12416 va_list ap;
12418 va_start (ap, fmt);
12419 vsprintf (buffer, fmt, ap);
12420 va_end (ap);
12421 if (len && remaining)
12423 method[len] = '|';
12424 --remaining, ++len;
12427 strncpy (method + len, buffer, remaining);
12429 if (trace_redisplay_p)
12430 fprintf (stderr, "%p (%s): %s\n",
12432 ((BUFFERP (w->buffer)
12433 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12434 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12435 : "no buffer"),
12436 buffer);
12439 #endif /* GLYPH_DEBUG */
12442 /* Value is non-zero if all changes in window W, which displays
12443 current_buffer, are in the text between START and END. START is a
12444 buffer position, END is given as a distance from Z. Used in
12445 redisplay_internal for display optimization. */
12447 static inline int
12448 text_outside_line_unchanged_p (struct window *w,
12449 EMACS_INT start, EMACS_INT end)
12451 int unchanged_p = 1;
12453 /* If text or overlays have changed, see where. */
12454 if (XFASTINT (w->last_modified) < MODIFF
12455 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12457 /* Gap in the line? */
12458 if (GPT < start || Z - GPT < end)
12459 unchanged_p = 0;
12461 /* Changes start in front of the line, or end after it? */
12462 if (unchanged_p
12463 && (BEG_UNCHANGED < start - 1
12464 || END_UNCHANGED < end))
12465 unchanged_p = 0;
12467 /* If selective display, can't optimize if changes start at the
12468 beginning of the line. */
12469 if (unchanged_p
12470 && INTEGERP (BVAR (current_buffer, selective_display))
12471 && XINT (BVAR (current_buffer, selective_display)) > 0
12472 && (BEG_UNCHANGED < start || GPT <= start))
12473 unchanged_p = 0;
12475 /* If there are overlays at the start or end of the line, these
12476 may have overlay strings with newlines in them. A change at
12477 START, for instance, may actually concern the display of such
12478 overlay strings as well, and they are displayed on different
12479 lines. So, quickly rule out this case. (For the future, it
12480 might be desirable to implement something more telling than
12481 just BEG/END_UNCHANGED.) */
12482 if (unchanged_p)
12484 if (BEG + BEG_UNCHANGED == start
12485 && overlay_touches_p (start))
12486 unchanged_p = 0;
12487 if (END_UNCHANGED == end
12488 && overlay_touches_p (Z - end))
12489 unchanged_p = 0;
12492 /* Under bidi reordering, adding or deleting a character in the
12493 beginning of a paragraph, before the first strong directional
12494 character, can change the base direction of the paragraph (unless
12495 the buffer specifies a fixed paragraph direction), which will
12496 require to redisplay the whole paragraph. It might be worthwhile
12497 to find the paragraph limits and widen the range of redisplayed
12498 lines to that, but for now just give up this optimization. */
12499 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12500 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12501 unchanged_p = 0;
12504 return unchanged_p;
12508 /* Do a frame update, taking possible shortcuts into account. This is
12509 the main external entry point for redisplay.
12511 If the last redisplay displayed an echo area message and that message
12512 is no longer requested, we clear the echo area or bring back the
12513 mini-buffer if that is in use. */
12515 void
12516 redisplay (void)
12518 redisplay_internal ();
12522 static Lisp_Object
12523 overlay_arrow_string_or_property (Lisp_Object var)
12525 Lisp_Object val;
12527 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12528 return val;
12530 return Voverlay_arrow_string;
12533 /* Return 1 if there are any overlay-arrows in current_buffer. */
12534 static int
12535 overlay_arrow_in_current_buffer_p (void)
12537 Lisp_Object vlist;
12539 for (vlist = Voverlay_arrow_variable_list;
12540 CONSP (vlist);
12541 vlist = XCDR (vlist))
12543 Lisp_Object var = XCAR (vlist);
12544 Lisp_Object val;
12546 if (!SYMBOLP (var))
12547 continue;
12548 val = find_symbol_value (var);
12549 if (MARKERP (val)
12550 && current_buffer == XMARKER (val)->buffer)
12551 return 1;
12553 return 0;
12557 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12558 has changed. */
12560 static int
12561 overlay_arrows_changed_p (void)
12563 Lisp_Object vlist;
12565 for (vlist = Voverlay_arrow_variable_list;
12566 CONSP (vlist);
12567 vlist = XCDR (vlist))
12569 Lisp_Object var = XCAR (vlist);
12570 Lisp_Object val, pstr;
12572 if (!SYMBOLP (var))
12573 continue;
12574 val = find_symbol_value (var);
12575 if (!MARKERP (val))
12576 continue;
12577 if (! EQ (COERCE_MARKER (val),
12578 Fget (var, Qlast_arrow_position))
12579 || ! (pstr = overlay_arrow_string_or_property (var),
12580 EQ (pstr, Fget (var, Qlast_arrow_string))))
12581 return 1;
12583 return 0;
12586 /* Mark overlay arrows to be updated on next redisplay. */
12588 static void
12589 update_overlay_arrows (int up_to_date)
12591 Lisp_Object vlist;
12593 for (vlist = Voverlay_arrow_variable_list;
12594 CONSP (vlist);
12595 vlist = XCDR (vlist))
12597 Lisp_Object var = XCAR (vlist);
12599 if (!SYMBOLP (var))
12600 continue;
12602 if (up_to_date > 0)
12604 Lisp_Object val = find_symbol_value (var);
12605 Fput (var, Qlast_arrow_position,
12606 COERCE_MARKER (val));
12607 Fput (var, Qlast_arrow_string,
12608 overlay_arrow_string_or_property (var));
12610 else if (up_to_date < 0
12611 || !NILP (Fget (var, Qlast_arrow_position)))
12613 Fput (var, Qlast_arrow_position, Qt);
12614 Fput (var, Qlast_arrow_string, Qt);
12620 /* Return overlay arrow string to display at row.
12621 Return integer (bitmap number) for arrow bitmap in left fringe.
12622 Return nil if no overlay arrow. */
12624 static Lisp_Object
12625 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12627 Lisp_Object vlist;
12629 for (vlist = Voverlay_arrow_variable_list;
12630 CONSP (vlist);
12631 vlist = XCDR (vlist))
12633 Lisp_Object var = XCAR (vlist);
12634 Lisp_Object val;
12636 if (!SYMBOLP (var))
12637 continue;
12639 val = find_symbol_value (var);
12641 if (MARKERP (val)
12642 && current_buffer == XMARKER (val)->buffer
12643 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12645 if (FRAME_WINDOW_P (it->f)
12646 /* FIXME: if ROW->reversed_p is set, this should test
12647 the right fringe, not the left one. */
12648 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12650 #ifdef HAVE_WINDOW_SYSTEM
12651 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12653 int fringe_bitmap;
12654 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12655 return make_number (fringe_bitmap);
12657 #endif
12658 return make_number (-1); /* Use default arrow bitmap */
12660 return overlay_arrow_string_or_property (var);
12664 return Qnil;
12667 /* Return 1 if point moved out of or into a composition. Otherwise
12668 return 0. PREV_BUF and PREV_PT are the last point buffer and
12669 position. BUF and PT are the current point buffer and position. */
12671 static int
12672 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
12673 struct buffer *buf, EMACS_INT pt)
12675 EMACS_INT start, end;
12676 Lisp_Object prop;
12677 Lisp_Object buffer;
12679 XSETBUFFER (buffer, buf);
12680 /* Check a composition at the last point if point moved within the
12681 same buffer. */
12682 if (prev_buf == buf)
12684 if (prev_pt == pt)
12685 /* Point didn't move. */
12686 return 0;
12688 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12689 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12690 && COMPOSITION_VALID_P (start, end, prop)
12691 && start < prev_pt && end > prev_pt)
12692 /* The last point was within the composition. Return 1 iff
12693 point moved out of the composition. */
12694 return (pt <= start || pt >= end);
12697 /* Check a composition at the current point. */
12698 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12699 && find_composition (pt, -1, &start, &end, &prop, buffer)
12700 && COMPOSITION_VALID_P (start, end, prop)
12701 && start < pt && end > pt);
12705 /* Reconsider the setting of B->clip_changed which is displayed
12706 in window W. */
12708 static inline void
12709 reconsider_clip_changes (struct window *w, struct buffer *b)
12711 if (b->clip_changed
12712 && !NILP (w->window_end_valid)
12713 && w->current_matrix->buffer == b
12714 && w->current_matrix->zv == BUF_ZV (b)
12715 && w->current_matrix->begv == BUF_BEGV (b))
12716 b->clip_changed = 0;
12718 /* If display wasn't paused, and W is not a tool bar window, see if
12719 point has been moved into or out of a composition. In that case,
12720 we set b->clip_changed to 1 to force updating the screen. If
12721 b->clip_changed has already been set to 1, we can skip this
12722 check. */
12723 if (!b->clip_changed
12724 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12726 EMACS_INT pt;
12728 if (w == XWINDOW (selected_window))
12729 pt = PT;
12730 else
12731 pt = marker_position (w->pointm);
12733 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12734 || pt != XINT (w->last_point))
12735 && check_point_in_composition (w->current_matrix->buffer,
12736 XINT (w->last_point),
12737 XBUFFER (w->buffer), pt))
12738 b->clip_changed = 1;
12743 /* Select FRAME to forward the values of frame-local variables into C
12744 variables so that the redisplay routines can access those values
12745 directly. */
12747 static void
12748 select_frame_for_redisplay (Lisp_Object frame)
12750 Lisp_Object tail, tem;
12751 Lisp_Object old = selected_frame;
12752 struct Lisp_Symbol *sym;
12754 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12756 selected_frame = frame;
12758 do {
12759 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12760 if (CONSP (XCAR (tail))
12761 && (tem = XCAR (XCAR (tail)),
12762 SYMBOLP (tem))
12763 && (sym = indirect_variable (XSYMBOL (tem)),
12764 sym->redirect == SYMBOL_LOCALIZED)
12765 && sym->val.blv->frame_local)
12766 /* Use find_symbol_value rather than Fsymbol_value
12767 to avoid an error if it is void. */
12768 find_symbol_value (tem);
12769 } while (!EQ (frame, old) && (frame = old, 1));
12773 #define STOP_POLLING \
12774 do { if (! polling_stopped_here) stop_polling (); \
12775 polling_stopped_here = 1; } while (0)
12777 #define RESUME_POLLING \
12778 do { if (polling_stopped_here) start_polling (); \
12779 polling_stopped_here = 0; } while (0)
12782 /* Perhaps in the future avoid recentering windows if it
12783 is not necessary; currently that causes some problems. */
12785 static void
12786 redisplay_internal (void)
12788 struct window *w = XWINDOW (selected_window);
12789 struct window *sw;
12790 struct frame *fr;
12791 int pending;
12792 int must_finish = 0;
12793 struct text_pos tlbufpos, tlendpos;
12794 int number_of_visible_frames;
12795 int count, count1;
12796 struct frame *sf;
12797 int polling_stopped_here = 0;
12798 Lisp_Object old_frame = selected_frame;
12800 /* Non-zero means redisplay has to consider all windows on all
12801 frames. Zero means, only selected_window is considered. */
12802 int consider_all_windows_p;
12804 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12806 /* No redisplay if running in batch mode or frame is not yet fully
12807 initialized, or redisplay is explicitly turned off by setting
12808 Vinhibit_redisplay. */
12809 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12810 || !NILP (Vinhibit_redisplay))
12811 return;
12813 /* Don't examine these until after testing Vinhibit_redisplay.
12814 When Emacs is shutting down, perhaps because its connection to
12815 X has dropped, we should not look at them at all. */
12816 fr = XFRAME (w->frame);
12817 sf = SELECTED_FRAME ();
12819 if (!fr->glyphs_initialized_p)
12820 return;
12822 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12823 if (popup_activated ())
12824 return;
12825 #endif
12827 /* I don't think this happens but let's be paranoid. */
12828 if (redisplaying_p)
12829 return;
12831 /* Record a function that resets redisplaying_p to its old value
12832 when we leave this function. */
12833 count = SPECPDL_INDEX ();
12834 record_unwind_protect (unwind_redisplay,
12835 Fcons (make_number (redisplaying_p), selected_frame));
12836 ++redisplaying_p;
12837 specbind (Qinhibit_free_realized_faces, Qnil);
12840 Lisp_Object tail, frame;
12842 FOR_EACH_FRAME (tail, frame)
12844 struct frame *f = XFRAME (frame);
12845 f->already_hscrolled_p = 0;
12849 retry:
12850 /* Remember the currently selected window. */
12851 sw = w;
12853 if (!EQ (old_frame, selected_frame)
12854 && FRAME_LIVE_P (XFRAME (old_frame)))
12855 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12856 selected_frame and selected_window to be temporarily out-of-sync so
12857 when we come back here via `goto retry', we need to resync because we
12858 may need to run Elisp code (via prepare_menu_bars). */
12859 select_frame_for_redisplay (old_frame);
12861 pending = 0;
12862 reconsider_clip_changes (w, current_buffer);
12863 last_escape_glyph_frame = NULL;
12864 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12865 last_glyphless_glyph_frame = NULL;
12866 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12868 /* If new fonts have been loaded that make a glyph matrix adjustment
12869 necessary, do it. */
12870 if (fonts_changed_p)
12872 adjust_glyphs (NULL);
12873 ++windows_or_buffers_changed;
12874 fonts_changed_p = 0;
12877 /* If face_change_count is non-zero, init_iterator will free all
12878 realized faces, which includes the faces referenced from current
12879 matrices. So, we can't reuse current matrices in this case. */
12880 if (face_change_count)
12881 ++windows_or_buffers_changed;
12883 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12884 && FRAME_TTY (sf)->previous_frame != sf)
12886 /* Since frames on a single ASCII terminal share the same
12887 display area, displaying a different frame means redisplay
12888 the whole thing. */
12889 windows_or_buffers_changed++;
12890 SET_FRAME_GARBAGED (sf);
12891 #ifndef DOS_NT
12892 set_tty_color_mode (FRAME_TTY (sf), sf);
12893 #endif
12894 FRAME_TTY (sf)->previous_frame = sf;
12897 /* Set the visible flags for all frames. Do this before checking
12898 for resized or garbaged frames; they want to know if their frames
12899 are visible. See the comment in frame.h for
12900 FRAME_SAMPLE_VISIBILITY. */
12902 Lisp_Object tail, frame;
12904 number_of_visible_frames = 0;
12906 FOR_EACH_FRAME (tail, frame)
12908 struct frame *f = XFRAME (frame);
12910 FRAME_SAMPLE_VISIBILITY (f);
12911 if (FRAME_VISIBLE_P (f))
12912 ++number_of_visible_frames;
12913 clear_desired_matrices (f);
12917 /* Notice any pending interrupt request to change frame size. */
12918 do_pending_window_change (1);
12920 /* do_pending_window_change could change the selected_window due to
12921 frame resizing which makes the selected window too small. */
12922 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12924 sw = w;
12925 reconsider_clip_changes (w, current_buffer);
12928 /* Clear frames marked as garbaged. */
12929 if (frame_garbaged)
12930 clear_garbaged_frames ();
12932 /* Build menubar and tool-bar items. */
12933 if (NILP (Vmemory_full))
12934 prepare_menu_bars ();
12936 if (windows_or_buffers_changed)
12937 update_mode_lines++;
12939 /* Detect case that we need to write or remove a star in the mode line. */
12940 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
12942 w->update_mode_line = Qt;
12943 if (buffer_shared > 1)
12944 update_mode_lines++;
12947 /* Avoid invocation of point motion hooks by `current_column' below. */
12948 count1 = SPECPDL_INDEX ();
12949 specbind (Qinhibit_point_motion_hooks, Qt);
12951 /* If %c is in the mode line, update it if needed. */
12952 if (!NILP (w->column_number_displayed)
12953 /* This alternative quickly identifies a common case
12954 where no change is needed. */
12955 && !(PT == XFASTINT (w->last_point)
12956 && XFASTINT (w->last_modified) >= MODIFF
12957 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12958 && (XFASTINT (w->column_number_displayed) != current_column ()))
12959 w->update_mode_line = Qt;
12961 unbind_to (count1, Qnil);
12963 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12965 /* The variable buffer_shared is set in redisplay_window and
12966 indicates that we redisplay a buffer in different windows. See
12967 there. */
12968 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
12969 || cursor_type_changed);
12971 /* If specs for an arrow have changed, do thorough redisplay
12972 to ensure we remove any arrow that should no longer exist. */
12973 if (overlay_arrows_changed_p ())
12974 consider_all_windows_p = windows_or_buffers_changed = 1;
12976 /* Normally the message* functions will have already displayed and
12977 updated the echo area, but the frame may have been trashed, or
12978 the update may have been preempted, so display the echo area
12979 again here. Checking message_cleared_p captures the case that
12980 the echo area should be cleared. */
12981 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12982 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12983 || (message_cleared_p
12984 && minibuf_level == 0
12985 /* If the mini-window is currently selected, this means the
12986 echo-area doesn't show through. */
12987 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12989 int window_height_changed_p = echo_area_display (0);
12990 must_finish = 1;
12992 /* If we don't display the current message, don't clear the
12993 message_cleared_p flag, because, if we did, we wouldn't clear
12994 the echo area in the next redisplay which doesn't preserve
12995 the echo area. */
12996 if (!display_last_displayed_message_p)
12997 message_cleared_p = 0;
12999 if (fonts_changed_p)
13000 goto retry;
13001 else if (window_height_changed_p)
13003 consider_all_windows_p = 1;
13004 ++update_mode_lines;
13005 ++windows_or_buffers_changed;
13007 /* If window configuration was changed, frames may have been
13008 marked garbaged. Clear them or we will experience
13009 surprises wrt scrolling. */
13010 if (frame_garbaged)
13011 clear_garbaged_frames ();
13014 else if (EQ (selected_window, minibuf_window)
13015 && (current_buffer->clip_changed
13016 || XFASTINT (w->last_modified) < MODIFF
13017 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
13018 && resize_mini_window (w, 0))
13020 /* Resized active mini-window to fit the size of what it is
13021 showing if its contents might have changed. */
13022 must_finish = 1;
13023 /* FIXME: this causes all frames to be updated, which seems unnecessary
13024 since only the current frame needs to be considered. This function needs
13025 to be rewritten with two variables, consider_all_windows and
13026 consider_all_frames. */
13027 consider_all_windows_p = 1;
13028 ++windows_or_buffers_changed;
13029 ++update_mode_lines;
13031 /* If window configuration was changed, frames may have been
13032 marked garbaged. Clear them or we will experience
13033 surprises wrt scrolling. */
13034 if (frame_garbaged)
13035 clear_garbaged_frames ();
13039 /* If showing the region, and mark has changed, we must redisplay
13040 the whole window. The assignment to this_line_start_pos prevents
13041 the optimization directly below this if-statement. */
13042 if (((!NILP (Vtransient_mark_mode)
13043 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13044 != !NILP (w->region_showing))
13045 || (!NILP (w->region_showing)
13046 && !EQ (w->region_showing,
13047 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13048 CHARPOS (this_line_start_pos) = 0;
13050 /* Optimize the case that only the line containing the cursor in the
13051 selected window has changed. Variables starting with this_ are
13052 set in display_line and record information about the line
13053 containing the cursor. */
13054 tlbufpos = this_line_start_pos;
13055 tlendpos = this_line_end_pos;
13056 if (!consider_all_windows_p
13057 && CHARPOS (tlbufpos) > 0
13058 && NILP (w->update_mode_line)
13059 && !current_buffer->clip_changed
13060 && !current_buffer->prevent_redisplay_optimizations_p
13061 && FRAME_VISIBLE_P (XFRAME (w->frame))
13062 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13063 /* Make sure recorded data applies to current buffer, etc. */
13064 && this_line_buffer == current_buffer
13065 && current_buffer == XBUFFER (w->buffer)
13066 && NILP (w->force_start)
13067 && NILP (w->optional_new_start)
13068 /* Point must be on the line that we have info recorded about. */
13069 && PT >= CHARPOS (tlbufpos)
13070 && PT <= Z - CHARPOS (tlendpos)
13071 /* All text outside that line, including its final newline,
13072 must be unchanged. */
13073 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13074 CHARPOS (tlendpos)))
13076 if (CHARPOS (tlbufpos) > BEGV
13077 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13078 && (CHARPOS (tlbufpos) == ZV
13079 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13080 /* Former continuation line has disappeared by becoming empty. */
13081 goto cancel;
13082 else if (XFASTINT (w->last_modified) < MODIFF
13083 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
13084 || MINI_WINDOW_P (w))
13086 /* We have to handle the case of continuation around a
13087 wide-column character (see the comment in indent.c around
13088 line 1340).
13090 For instance, in the following case:
13092 -------- Insert --------
13093 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13094 J_I_ ==> J_I_ `^^' are cursors.
13095 ^^ ^^
13096 -------- --------
13098 As we have to redraw the line above, we cannot use this
13099 optimization. */
13101 struct it it;
13102 int line_height_before = this_line_pixel_height;
13104 /* Note that start_display will handle the case that the
13105 line starting at tlbufpos is a continuation line. */
13106 start_display (&it, w, tlbufpos);
13108 /* Implementation note: It this still necessary? */
13109 if (it.current_x != this_line_start_x)
13110 goto cancel;
13112 TRACE ((stderr, "trying display optimization 1\n"));
13113 w->cursor.vpos = -1;
13114 overlay_arrow_seen = 0;
13115 it.vpos = this_line_vpos;
13116 it.current_y = this_line_y;
13117 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13118 display_line (&it);
13120 /* If line contains point, is not continued,
13121 and ends at same distance from eob as before, we win. */
13122 if (w->cursor.vpos >= 0
13123 /* Line is not continued, otherwise this_line_start_pos
13124 would have been set to 0 in display_line. */
13125 && CHARPOS (this_line_start_pos)
13126 /* Line ends as before. */
13127 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13128 /* Line has same height as before. Otherwise other lines
13129 would have to be shifted up or down. */
13130 && this_line_pixel_height == line_height_before)
13132 /* If this is not the window's last line, we must adjust
13133 the charstarts of the lines below. */
13134 if (it.current_y < it.last_visible_y)
13136 struct glyph_row *row
13137 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13138 EMACS_INT delta, delta_bytes;
13140 /* We used to distinguish between two cases here,
13141 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13142 when the line ends in a newline or the end of the
13143 buffer's accessible portion. But both cases did
13144 the same, so they were collapsed. */
13145 delta = (Z
13146 - CHARPOS (tlendpos)
13147 - MATRIX_ROW_START_CHARPOS (row));
13148 delta_bytes = (Z_BYTE
13149 - BYTEPOS (tlendpos)
13150 - MATRIX_ROW_START_BYTEPOS (row));
13152 increment_matrix_positions (w->current_matrix,
13153 this_line_vpos + 1,
13154 w->current_matrix->nrows,
13155 delta, delta_bytes);
13158 /* If this row displays text now but previously didn't,
13159 or vice versa, w->window_end_vpos may have to be
13160 adjusted. */
13161 if ((it.glyph_row - 1)->displays_text_p)
13163 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13164 XSETINT (w->window_end_vpos, this_line_vpos);
13166 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13167 && this_line_vpos > 0)
13168 XSETINT (w->window_end_vpos, this_line_vpos - 1);
13169 w->window_end_valid = Qnil;
13171 /* Update hint: No need to try to scroll in update_window. */
13172 w->desired_matrix->no_scrolling_p = 1;
13174 #if GLYPH_DEBUG
13175 *w->desired_matrix->method = 0;
13176 debug_method_add (w, "optimization 1");
13177 #endif
13178 #ifdef HAVE_WINDOW_SYSTEM
13179 update_window_fringes (w, 0);
13180 #endif
13181 goto update;
13183 else
13184 goto cancel;
13186 else if (/* Cursor position hasn't changed. */
13187 PT == XFASTINT (w->last_point)
13188 /* Make sure the cursor was last displayed
13189 in this window. Otherwise we have to reposition it. */
13190 && 0 <= w->cursor.vpos
13191 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13193 if (!must_finish)
13195 do_pending_window_change (1);
13196 /* If selected_window changed, redisplay again. */
13197 if (WINDOWP (selected_window)
13198 && (w = XWINDOW (selected_window)) != sw)
13199 goto retry;
13201 /* We used to always goto end_of_redisplay here, but this
13202 isn't enough if we have a blinking cursor. */
13203 if (w->cursor_off_p == w->last_cursor_off_p)
13204 goto end_of_redisplay;
13206 goto update;
13208 /* If highlighting the region, or if the cursor is in the echo area,
13209 then we can't just move the cursor. */
13210 else if (! (!NILP (Vtransient_mark_mode)
13211 && !NILP (BVAR (current_buffer, mark_active)))
13212 && (EQ (selected_window, BVAR (current_buffer, last_selected_window))
13213 || highlight_nonselected_windows)
13214 && NILP (w->region_showing)
13215 && NILP (Vshow_trailing_whitespace)
13216 && !cursor_in_echo_area)
13218 struct it it;
13219 struct glyph_row *row;
13221 /* Skip from tlbufpos to PT and see where it is. Note that
13222 PT may be in invisible text. If so, we will end at the
13223 next visible position. */
13224 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13225 NULL, DEFAULT_FACE_ID);
13226 it.current_x = this_line_start_x;
13227 it.current_y = this_line_y;
13228 it.vpos = this_line_vpos;
13230 /* The call to move_it_to stops in front of PT, but
13231 moves over before-strings. */
13232 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13234 if (it.vpos == this_line_vpos
13235 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13236 row->enabled_p))
13238 xassert (this_line_vpos == it.vpos);
13239 xassert (this_line_y == it.current_y);
13240 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13241 #if GLYPH_DEBUG
13242 *w->desired_matrix->method = 0;
13243 debug_method_add (w, "optimization 3");
13244 #endif
13245 goto update;
13247 else
13248 goto cancel;
13251 cancel:
13252 /* Text changed drastically or point moved off of line. */
13253 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13256 CHARPOS (this_line_start_pos) = 0;
13257 consider_all_windows_p |= buffer_shared > 1;
13258 ++clear_face_cache_count;
13259 #ifdef HAVE_WINDOW_SYSTEM
13260 ++clear_image_cache_count;
13261 #endif
13263 /* Build desired matrices, and update the display. If
13264 consider_all_windows_p is non-zero, do it for all windows on all
13265 frames. Otherwise do it for selected_window, only. */
13267 if (consider_all_windows_p)
13269 Lisp_Object tail, frame;
13271 FOR_EACH_FRAME (tail, frame)
13272 XFRAME (frame)->updated_p = 0;
13274 /* Recompute # windows showing selected buffer. This will be
13275 incremented each time such a window is displayed. */
13276 buffer_shared = 0;
13278 FOR_EACH_FRAME (tail, frame)
13280 struct frame *f = XFRAME (frame);
13282 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13284 if (! EQ (frame, selected_frame))
13285 /* Select the frame, for the sake of frame-local
13286 variables. */
13287 select_frame_for_redisplay (frame);
13289 /* Mark all the scroll bars to be removed; we'll redeem
13290 the ones we want when we redisplay their windows. */
13291 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13292 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13294 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13295 redisplay_windows (FRAME_ROOT_WINDOW (f));
13297 /* The X error handler may have deleted that frame. */
13298 if (!FRAME_LIVE_P (f))
13299 continue;
13301 /* Any scroll bars which redisplay_windows should have
13302 nuked should now go away. */
13303 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13304 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13306 /* If fonts changed, display again. */
13307 /* ??? rms: I suspect it is a mistake to jump all the way
13308 back to retry here. It should just retry this frame. */
13309 if (fonts_changed_p)
13310 goto retry;
13312 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13314 /* See if we have to hscroll. */
13315 if (!f->already_hscrolled_p)
13317 f->already_hscrolled_p = 1;
13318 if (hscroll_windows (f->root_window))
13319 goto retry;
13322 /* Prevent various kinds of signals during display
13323 update. stdio is not robust about handling
13324 signals, which can cause an apparent I/O
13325 error. */
13326 if (interrupt_input)
13327 unrequest_sigio ();
13328 STOP_POLLING;
13330 /* Update the display. */
13331 set_window_update_flags (XWINDOW (f->root_window), 1);
13332 pending |= update_frame (f, 0, 0);
13333 f->updated_p = 1;
13338 if (!EQ (old_frame, selected_frame)
13339 && FRAME_LIVE_P (XFRAME (old_frame)))
13340 /* We played a bit fast-and-loose above and allowed selected_frame
13341 and selected_window to be temporarily out-of-sync but let's make
13342 sure this stays contained. */
13343 select_frame_for_redisplay (old_frame);
13344 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13346 if (!pending)
13348 /* Do the mark_window_display_accurate after all windows have
13349 been redisplayed because this call resets flags in buffers
13350 which are needed for proper redisplay. */
13351 FOR_EACH_FRAME (tail, frame)
13353 struct frame *f = XFRAME (frame);
13354 if (f->updated_p)
13356 mark_window_display_accurate (f->root_window, 1);
13357 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13358 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13363 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13365 Lisp_Object mini_window;
13366 struct frame *mini_frame;
13368 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13369 /* Use list_of_error, not Qerror, so that
13370 we catch only errors and don't run the debugger. */
13371 internal_condition_case_1 (redisplay_window_1, selected_window,
13372 list_of_error,
13373 redisplay_window_error);
13375 /* Compare desired and current matrices, perform output. */
13377 update:
13378 /* If fonts changed, display again. */
13379 if (fonts_changed_p)
13380 goto retry;
13382 /* Prevent various kinds of signals during display update.
13383 stdio is not robust about handling signals,
13384 which can cause an apparent I/O error. */
13385 if (interrupt_input)
13386 unrequest_sigio ();
13387 STOP_POLLING;
13389 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13391 if (hscroll_windows (selected_window))
13392 goto retry;
13394 XWINDOW (selected_window)->must_be_updated_p = 1;
13395 pending = update_frame (sf, 0, 0);
13398 /* We may have called echo_area_display at the top of this
13399 function. If the echo area is on another frame, that may
13400 have put text on a frame other than the selected one, so the
13401 above call to update_frame would not have caught it. Catch
13402 it here. */
13403 mini_window = FRAME_MINIBUF_WINDOW (sf);
13404 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13406 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13408 XWINDOW (mini_window)->must_be_updated_p = 1;
13409 pending |= update_frame (mini_frame, 0, 0);
13410 if (!pending && hscroll_windows (mini_window))
13411 goto retry;
13415 /* If display was paused because of pending input, make sure we do a
13416 thorough update the next time. */
13417 if (pending)
13419 /* Prevent the optimization at the beginning of
13420 redisplay_internal that tries a single-line update of the
13421 line containing the cursor in the selected window. */
13422 CHARPOS (this_line_start_pos) = 0;
13424 /* Let the overlay arrow be updated the next time. */
13425 update_overlay_arrows (0);
13427 /* If we pause after scrolling, some rows in the current
13428 matrices of some windows are not valid. */
13429 if (!WINDOW_FULL_WIDTH_P (w)
13430 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13431 update_mode_lines = 1;
13433 else
13435 if (!consider_all_windows_p)
13437 /* This has already been done above if
13438 consider_all_windows_p is set. */
13439 mark_window_display_accurate_1 (w, 1);
13441 /* Say overlay arrows are up to date. */
13442 update_overlay_arrows (1);
13444 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13445 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13448 update_mode_lines = 0;
13449 windows_or_buffers_changed = 0;
13450 cursor_type_changed = 0;
13453 /* Start SIGIO interrupts coming again. Having them off during the
13454 code above makes it less likely one will discard output, but not
13455 impossible, since there might be stuff in the system buffer here.
13456 But it is much hairier to try to do anything about that. */
13457 if (interrupt_input)
13458 request_sigio ();
13459 RESUME_POLLING;
13461 /* If a frame has become visible which was not before, redisplay
13462 again, so that we display it. Expose events for such a frame
13463 (which it gets when becoming visible) don't call the parts of
13464 redisplay constructing glyphs, so simply exposing a frame won't
13465 display anything in this case. So, we have to display these
13466 frames here explicitly. */
13467 if (!pending)
13469 Lisp_Object tail, frame;
13470 int new_count = 0;
13472 FOR_EACH_FRAME (tail, frame)
13474 int this_is_visible = 0;
13476 if (XFRAME (frame)->visible)
13477 this_is_visible = 1;
13478 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13479 if (XFRAME (frame)->visible)
13480 this_is_visible = 1;
13482 if (this_is_visible)
13483 new_count++;
13486 if (new_count != number_of_visible_frames)
13487 windows_or_buffers_changed++;
13490 /* Change frame size now if a change is pending. */
13491 do_pending_window_change (1);
13493 /* If we just did a pending size change, or have additional
13494 visible frames, or selected_window changed, redisplay again. */
13495 if ((windows_or_buffers_changed && !pending)
13496 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13497 goto retry;
13499 /* Clear the face and image caches.
13501 We used to do this only if consider_all_windows_p. But the cache
13502 needs to be cleared if a timer creates images in the current
13503 buffer (e.g. the test case in Bug#6230). */
13505 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13507 clear_face_cache (0);
13508 clear_face_cache_count = 0;
13511 #ifdef HAVE_WINDOW_SYSTEM
13512 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13514 clear_image_caches (Qnil);
13515 clear_image_cache_count = 0;
13517 #endif /* HAVE_WINDOW_SYSTEM */
13519 end_of_redisplay:
13520 unbind_to (count, Qnil);
13521 RESUME_POLLING;
13525 /* Redisplay, but leave alone any recent echo area message unless
13526 another message has been requested in its place.
13528 This is useful in situations where you need to redisplay but no
13529 user action has occurred, making it inappropriate for the message
13530 area to be cleared. See tracking_off and
13531 wait_reading_process_output for examples of these situations.
13533 FROM_WHERE is an integer saying from where this function was
13534 called. This is useful for debugging. */
13536 void
13537 redisplay_preserve_echo_area (int from_where)
13539 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13541 if (!NILP (echo_area_buffer[1]))
13543 /* We have a previously displayed message, but no current
13544 message. Redisplay the previous message. */
13545 display_last_displayed_message_p = 1;
13546 redisplay_internal ();
13547 display_last_displayed_message_p = 0;
13549 else
13550 redisplay_internal ();
13552 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13553 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13554 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13558 /* Function registered with record_unwind_protect in
13559 redisplay_internal. Reset redisplaying_p to the value it had
13560 before redisplay_internal was called, and clear
13561 prevent_freeing_realized_faces_p. It also selects the previously
13562 selected frame, unless it has been deleted (by an X connection
13563 failure during redisplay, for example). */
13565 static Lisp_Object
13566 unwind_redisplay (Lisp_Object val)
13568 Lisp_Object old_redisplaying_p, old_frame;
13570 old_redisplaying_p = XCAR (val);
13571 redisplaying_p = XFASTINT (old_redisplaying_p);
13572 old_frame = XCDR (val);
13573 if (! EQ (old_frame, selected_frame)
13574 && FRAME_LIVE_P (XFRAME (old_frame)))
13575 select_frame_for_redisplay (old_frame);
13576 return Qnil;
13580 /* Mark the display of window W as accurate or inaccurate. If
13581 ACCURATE_P is non-zero mark display of W as accurate. If
13582 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13583 redisplay_internal is called. */
13585 static void
13586 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13588 if (BUFFERP (w->buffer))
13590 struct buffer *b = XBUFFER (w->buffer);
13592 w->last_modified
13593 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
13594 w->last_overlay_modified
13595 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
13596 w->last_had_star
13597 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
13599 if (accurate_p)
13601 b->clip_changed = 0;
13602 b->prevent_redisplay_optimizations_p = 0;
13604 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13605 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13606 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13607 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13609 w->current_matrix->buffer = b;
13610 w->current_matrix->begv = BUF_BEGV (b);
13611 w->current_matrix->zv = BUF_ZV (b);
13613 w->last_cursor = w->cursor;
13614 w->last_cursor_off_p = w->cursor_off_p;
13616 if (w == XWINDOW (selected_window))
13617 w->last_point = make_number (BUF_PT (b));
13618 else
13619 w->last_point = make_number (XMARKER (w->pointm)->charpos);
13623 if (accurate_p)
13625 w->window_end_valid = w->buffer;
13626 w->update_mode_line = Qnil;
13631 /* Mark the display of windows in the window tree rooted at WINDOW as
13632 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13633 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13634 be redisplayed the next time redisplay_internal is called. */
13636 void
13637 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13639 struct window *w;
13641 for (; !NILP (window); window = w->next)
13643 w = XWINDOW (window);
13644 mark_window_display_accurate_1 (w, accurate_p);
13646 if (!NILP (w->vchild))
13647 mark_window_display_accurate (w->vchild, accurate_p);
13648 if (!NILP (w->hchild))
13649 mark_window_display_accurate (w->hchild, accurate_p);
13652 if (accurate_p)
13654 update_overlay_arrows (1);
13656 else
13658 /* Force a thorough redisplay the next time by setting
13659 last_arrow_position and last_arrow_string to t, which is
13660 unequal to any useful value of Voverlay_arrow_... */
13661 update_overlay_arrows (-1);
13666 /* Return value in display table DP (Lisp_Char_Table *) for character
13667 C. Since a display table doesn't have any parent, we don't have to
13668 follow parent. Do not call this function directly but use the
13669 macro DISP_CHAR_VECTOR. */
13671 Lisp_Object
13672 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13674 Lisp_Object val;
13676 if (ASCII_CHAR_P (c))
13678 val = dp->ascii;
13679 if (SUB_CHAR_TABLE_P (val))
13680 val = XSUB_CHAR_TABLE (val)->contents[c];
13682 else
13684 Lisp_Object table;
13686 XSETCHAR_TABLE (table, dp);
13687 val = char_table_ref (table, c);
13689 if (NILP (val))
13690 val = dp->defalt;
13691 return val;
13696 /***********************************************************************
13697 Window Redisplay
13698 ***********************************************************************/
13700 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13702 static void
13703 redisplay_windows (Lisp_Object window)
13705 while (!NILP (window))
13707 struct window *w = XWINDOW (window);
13709 if (!NILP (w->hchild))
13710 redisplay_windows (w->hchild);
13711 else if (!NILP (w->vchild))
13712 redisplay_windows (w->vchild);
13713 else if (!NILP (w->buffer))
13715 displayed_buffer = XBUFFER (w->buffer);
13716 /* Use list_of_error, not Qerror, so that
13717 we catch only errors and don't run the debugger. */
13718 internal_condition_case_1 (redisplay_window_0, window,
13719 list_of_error,
13720 redisplay_window_error);
13723 window = w->next;
13727 static Lisp_Object
13728 redisplay_window_error (Lisp_Object ignore)
13730 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13731 return Qnil;
13734 static Lisp_Object
13735 redisplay_window_0 (Lisp_Object window)
13737 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13738 redisplay_window (window, 0);
13739 return Qnil;
13742 static Lisp_Object
13743 redisplay_window_1 (Lisp_Object window)
13745 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13746 redisplay_window (window, 1);
13747 return Qnil;
13751 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13752 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13753 which positions recorded in ROW differ from current buffer
13754 positions.
13756 Return 0 if cursor is not on this row, 1 otherwise. */
13758 static int
13759 set_cursor_from_row (struct window *w, struct glyph_row *row,
13760 struct glyph_matrix *matrix,
13761 EMACS_INT delta, EMACS_INT delta_bytes,
13762 int dy, int dvpos)
13764 struct glyph *glyph = row->glyphs[TEXT_AREA];
13765 struct glyph *end = glyph + row->used[TEXT_AREA];
13766 struct glyph *cursor = NULL;
13767 /* The last known character position in row. */
13768 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13769 int x = row->x;
13770 EMACS_INT pt_old = PT - delta;
13771 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13772 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13773 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13774 /* A glyph beyond the edge of TEXT_AREA which we should never
13775 touch. */
13776 struct glyph *glyphs_end = end;
13777 /* Non-zero means we've found a match for cursor position, but that
13778 glyph has the avoid_cursor_p flag set. */
13779 int match_with_avoid_cursor = 0;
13780 /* Non-zero means we've seen at least one glyph that came from a
13781 display string. */
13782 int string_seen = 0;
13783 /* Largest and smallest buffer positions seen so far during scan of
13784 glyph row. */
13785 EMACS_INT bpos_max = pos_before;
13786 EMACS_INT bpos_min = pos_after;
13787 /* Last buffer position covered by an overlay string with an integer
13788 `cursor' property. */
13789 EMACS_INT bpos_covered = 0;
13790 /* Non-zero means the display string on which to display the cursor
13791 comes from a text property, not from an overlay. */
13792 int string_from_text_prop = 0;
13794 /* Don't even try doing anything if called for a mode-line or
13795 header-line row, since the rest of the code isn't prepared to
13796 deal with such calamities. */
13797 xassert (!row->mode_line_p);
13798 if (row->mode_line_p)
13799 return 0;
13801 /* Skip over glyphs not having an object at the start and the end of
13802 the row. These are special glyphs like truncation marks on
13803 terminal frames. */
13804 if (row->displays_text_p)
13806 if (!row->reversed_p)
13808 while (glyph < end
13809 && INTEGERP (glyph->object)
13810 && glyph->charpos < 0)
13812 x += glyph->pixel_width;
13813 ++glyph;
13815 while (end > glyph
13816 && INTEGERP ((end - 1)->object)
13817 /* CHARPOS is zero for blanks and stretch glyphs
13818 inserted by extend_face_to_end_of_line. */
13819 && (end - 1)->charpos <= 0)
13820 --end;
13821 glyph_before = glyph - 1;
13822 glyph_after = end;
13824 else
13826 struct glyph *g;
13828 /* If the glyph row is reversed, we need to process it from back
13829 to front, so swap the edge pointers. */
13830 glyphs_end = end = glyph - 1;
13831 glyph += row->used[TEXT_AREA] - 1;
13833 while (glyph > end + 1
13834 && INTEGERP (glyph->object)
13835 && glyph->charpos < 0)
13837 --glyph;
13838 x -= glyph->pixel_width;
13840 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13841 --glyph;
13842 /* By default, in reversed rows we put the cursor on the
13843 rightmost (first in the reading order) glyph. */
13844 for (g = end + 1; g < glyph; g++)
13845 x += g->pixel_width;
13846 while (end < glyph
13847 && INTEGERP ((end + 1)->object)
13848 && (end + 1)->charpos <= 0)
13849 ++end;
13850 glyph_before = glyph + 1;
13851 glyph_after = end;
13854 else if (row->reversed_p)
13856 /* In R2L rows that don't display text, put the cursor on the
13857 rightmost glyph. Case in point: an empty last line that is
13858 part of an R2L paragraph. */
13859 cursor = end - 1;
13860 /* Avoid placing the cursor on the last glyph of the row, where
13861 on terminal frames we hold the vertical border between
13862 adjacent windows. */
13863 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13864 && !WINDOW_RIGHTMOST_P (w)
13865 && cursor == row->glyphs[LAST_AREA] - 1)
13866 cursor--;
13867 x = -1; /* will be computed below, at label compute_x */
13870 /* Step 1: Try to find the glyph whose character position
13871 corresponds to point. If that's not possible, find 2 glyphs
13872 whose character positions are the closest to point, one before
13873 point, the other after it. */
13874 if (!row->reversed_p)
13875 while (/* not marched to end of glyph row */
13876 glyph < end
13877 /* glyph was not inserted by redisplay for internal purposes */
13878 && !INTEGERP (glyph->object))
13880 if (BUFFERP (glyph->object))
13882 EMACS_INT dpos = glyph->charpos - pt_old;
13884 if (glyph->charpos > bpos_max)
13885 bpos_max = glyph->charpos;
13886 if (glyph->charpos < bpos_min)
13887 bpos_min = glyph->charpos;
13888 if (!glyph->avoid_cursor_p)
13890 /* If we hit point, we've found the glyph on which to
13891 display the cursor. */
13892 if (dpos == 0)
13894 match_with_avoid_cursor = 0;
13895 break;
13897 /* See if we've found a better approximation to
13898 POS_BEFORE or to POS_AFTER. Note that we want the
13899 first (leftmost) glyph of all those that are the
13900 closest from below, and the last (rightmost) of all
13901 those from above. */
13902 if (0 > dpos && dpos > pos_before - pt_old)
13904 pos_before = glyph->charpos;
13905 glyph_before = glyph;
13907 else if (0 < dpos && dpos <= pos_after - pt_old)
13909 pos_after = glyph->charpos;
13910 glyph_after = glyph;
13913 else if (dpos == 0)
13914 match_with_avoid_cursor = 1;
13916 else if (STRINGP (glyph->object))
13918 Lisp_Object chprop;
13919 EMACS_INT glyph_pos = glyph->charpos;
13921 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13922 glyph->object);
13923 if (!NILP (chprop))
13925 /* If the string came from a `display' text property,
13926 look up the buffer position of that property and
13927 use that position to update bpos_max, as if we
13928 actually saw such a position in one of the row's
13929 glyphs. This helps with supporting integer values
13930 of `cursor' property on the display string in
13931 situations where most or all of the row's buffer
13932 text is completely covered by display properties,
13933 so that no glyph with valid buffer positions is
13934 ever seen in the row. */
13935 EMACS_INT prop_pos =
13936 string_buffer_position_lim (glyph->object, pos_before,
13937 pos_after, 0);
13939 if (prop_pos >= pos_before)
13940 bpos_max = prop_pos - 1;
13942 if (INTEGERP (chprop))
13944 bpos_covered = bpos_max + XINT (chprop);
13945 /* If the `cursor' property covers buffer positions up
13946 to and including point, we should display cursor on
13947 this glyph. Note that, if a `cursor' property on one
13948 of the string's characters has an integer value, we
13949 will break out of the loop below _before_ we get to
13950 the position match above. IOW, integer values of
13951 the `cursor' property override the "exact match for
13952 point" strategy of positioning the cursor. */
13953 /* Implementation note: bpos_max == pt_old when, e.g.,
13954 we are in an empty line, where bpos_max is set to
13955 MATRIX_ROW_START_CHARPOS, see above. */
13956 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13958 cursor = glyph;
13959 break;
13963 string_seen = 1;
13965 x += glyph->pixel_width;
13966 ++glyph;
13968 else if (glyph > end) /* row is reversed */
13969 while (!INTEGERP (glyph->object))
13971 if (BUFFERP (glyph->object))
13973 EMACS_INT dpos = glyph->charpos - pt_old;
13975 if (glyph->charpos > bpos_max)
13976 bpos_max = glyph->charpos;
13977 if (glyph->charpos < bpos_min)
13978 bpos_min = glyph->charpos;
13979 if (!glyph->avoid_cursor_p)
13981 if (dpos == 0)
13983 match_with_avoid_cursor = 0;
13984 break;
13986 if (0 > dpos && dpos > pos_before - pt_old)
13988 pos_before = glyph->charpos;
13989 glyph_before = glyph;
13991 else if (0 < dpos && dpos <= pos_after - pt_old)
13993 pos_after = glyph->charpos;
13994 glyph_after = glyph;
13997 else if (dpos == 0)
13998 match_with_avoid_cursor = 1;
14000 else if (STRINGP (glyph->object))
14002 Lisp_Object chprop;
14003 EMACS_INT glyph_pos = glyph->charpos;
14005 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14006 glyph->object);
14007 if (!NILP (chprop))
14009 EMACS_INT prop_pos =
14010 string_buffer_position_lim (glyph->object, pos_before,
14011 pos_after, 0);
14013 if (prop_pos >= pos_before)
14014 bpos_max = prop_pos - 1;
14016 if (INTEGERP (chprop))
14018 bpos_covered = bpos_max + XINT (chprop);
14019 /* If the `cursor' property covers buffer positions up
14020 to and including point, we should display cursor on
14021 this glyph. */
14022 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14024 cursor = glyph;
14025 break;
14028 string_seen = 1;
14030 --glyph;
14031 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14033 x--; /* can't use any pixel_width */
14034 break;
14036 x -= glyph->pixel_width;
14039 /* Step 2: If we didn't find an exact match for point, we need to
14040 look for a proper place to put the cursor among glyphs between
14041 GLYPH_BEFORE and GLYPH_AFTER. */
14042 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14043 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14044 && bpos_covered < pt_old)
14046 /* An empty line has a single glyph whose OBJECT is zero and
14047 whose CHARPOS is the position of a newline on that line.
14048 Note that on a TTY, there are more glyphs after that, which
14049 were produced by extend_face_to_end_of_line, but their
14050 CHARPOS is zero or negative. */
14051 int empty_line_p =
14052 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14053 && INTEGERP (glyph->object) && glyph->charpos > 0;
14055 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14057 EMACS_INT ellipsis_pos;
14059 /* Scan back over the ellipsis glyphs. */
14060 if (!row->reversed_p)
14062 ellipsis_pos = (glyph - 1)->charpos;
14063 while (glyph > row->glyphs[TEXT_AREA]
14064 && (glyph - 1)->charpos == ellipsis_pos)
14065 glyph--, x -= glyph->pixel_width;
14066 /* That loop always goes one position too far, including
14067 the glyph before the ellipsis. So scan forward over
14068 that one. */
14069 x += glyph->pixel_width;
14070 glyph++;
14072 else /* row is reversed */
14074 ellipsis_pos = (glyph + 1)->charpos;
14075 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14076 && (glyph + 1)->charpos == ellipsis_pos)
14077 glyph++, x += glyph->pixel_width;
14078 x -= glyph->pixel_width;
14079 glyph--;
14082 else if (match_with_avoid_cursor)
14084 cursor = glyph_after;
14085 x = -1;
14087 else if (string_seen)
14089 int incr = row->reversed_p ? -1 : +1;
14091 /* Need to find the glyph that came out of a string which is
14092 present at point. That glyph is somewhere between
14093 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14094 positioned between POS_BEFORE and POS_AFTER in the
14095 buffer. */
14096 struct glyph *start, *stop;
14097 EMACS_INT pos = pos_before;
14099 x = -1;
14101 /* If the row ends in a newline from a display string,
14102 reordering could have moved the glyphs belonging to the
14103 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14104 in this case we extend the search to the last glyph in
14105 the row that was not inserted by redisplay. */
14106 if (row->ends_in_newline_from_string_p)
14108 glyph_after = end;
14109 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14112 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14113 correspond to POS_BEFORE and POS_AFTER, respectively. We
14114 need START and STOP in the order that corresponds to the
14115 row's direction as given by its reversed_p flag. If the
14116 directionality of characters between POS_BEFORE and
14117 POS_AFTER is the opposite of the row's base direction,
14118 these characters will have been reordered for display,
14119 and we need to reverse START and STOP. */
14120 if (!row->reversed_p)
14122 start = min (glyph_before, glyph_after);
14123 stop = max (glyph_before, glyph_after);
14125 else
14127 start = max (glyph_before, glyph_after);
14128 stop = min (glyph_before, glyph_after);
14130 for (glyph = start + incr;
14131 row->reversed_p ? glyph > stop : glyph < stop; )
14134 /* Any glyphs that come from the buffer are here because
14135 of bidi reordering. Skip them, and only pay
14136 attention to glyphs that came from some string. */
14137 if (STRINGP (glyph->object))
14139 Lisp_Object str;
14140 EMACS_INT tem;
14141 /* If the display property covers the newline, we
14142 need to search for it one position farther. */
14143 EMACS_INT lim = pos_after
14144 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14146 string_from_text_prop = 0;
14147 str = glyph->object;
14148 tem = string_buffer_position_lim (str, pos, lim, 0);
14149 if (tem == 0 /* from overlay */
14150 || pos <= tem)
14152 /* If the string from which this glyph came is
14153 found in the buffer at point, then we've
14154 found the glyph we've been looking for. If
14155 it comes from an overlay (tem == 0), and it
14156 has the `cursor' property on one of its
14157 glyphs, record that glyph as a candidate for
14158 displaying the cursor. (As in the
14159 unidirectional version, we will display the
14160 cursor on the last candidate we find.) */
14161 if (tem == 0 || tem == pt_old)
14163 /* The glyphs from this string could have
14164 been reordered. Find the one with the
14165 smallest string position. Or there could
14166 be a character in the string with the
14167 `cursor' property, which means display
14168 cursor on that character's glyph. */
14169 EMACS_INT strpos = glyph->charpos;
14171 if (tem)
14173 cursor = glyph;
14174 string_from_text_prop = 1;
14176 for ( ;
14177 (row->reversed_p ? glyph > stop : glyph < stop)
14178 && EQ (glyph->object, str);
14179 glyph += incr)
14181 Lisp_Object cprop;
14182 EMACS_INT gpos = glyph->charpos;
14184 cprop = Fget_char_property (make_number (gpos),
14185 Qcursor,
14186 glyph->object);
14187 if (!NILP (cprop))
14189 cursor = glyph;
14190 break;
14192 if (tem && glyph->charpos < strpos)
14194 strpos = glyph->charpos;
14195 cursor = glyph;
14199 if (tem == pt_old)
14200 goto compute_x;
14202 if (tem)
14203 pos = tem + 1; /* don't find previous instances */
14205 /* This string is not what we want; skip all of the
14206 glyphs that came from it. */
14207 while ((row->reversed_p ? glyph > stop : glyph < stop)
14208 && EQ (glyph->object, str))
14209 glyph += incr;
14211 else
14212 glyph += incr;
14215 /* If we reached the end of the line, and END was from a string,
14216 the cursor is not on this line. */
14217 if (cursor == NULL
14218 && (row->reversed_p ? glyph <= end : glyph >= end)
14219 && STRINGP (end->object)
14220 && row->continued_p)
14221 return 0;
14223 /* A truncated row may not include PT among its character positions.
14224 Setting the cursor inside the scroll margin will trigger
14225 recalculation of hscroll in hscroll_window_tree. But if a
14226 display string covers point, defer to the string-handling
14227 code below to figure this out. */
14228 else if (row->truncated_on_left_p && pt_old < bpos_min)
14230 cursor = glyph_before;
14231 x = -1;
14233 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14234 /* Zero-width characters produce no glyphs. */
14235 || (!empty_line_p
14236 && (row->reversed_p
14237 ? glyph_after > glyphs_end
14238 : glyph_after < glyphs_end)))
14240 cursor = glyph_after;
14241 x = -1;
14245 compute_x:
14246 if (cursor != NULL)
14247 glyph = cursor;
14248 if (x < 0)
14250 struct glyph *g;
14252 /* Need to compute x that corresponds to GLYPH. */
14253 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14255 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14256 abort ();
14257 x += g->pixel_width;
14261 /* ROW could be part of a continued line, which, under bidi
14262 reordering, might have other rows whose start and end charpos
14263 occlude point. Only set w->cursor if we found a better
14264 approximation to the cursor position than we have from previously
14265 examined candidate rows belonging to the same continued line. */
14266 if (/* we already have a candidate row */
14267 w->cursor.vpos >= 0
14268 /* that candidate is not the row we are processing */
14269 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14270 /* Make sure cursor.vpos specifies a row whose start and end
14271 charpos occlude point, and it is valid candidate for being a
14272 cursor-row. This is because some callers of this function
14273 leave cursor.vpos at the row where the cursor was displayed
14274 during the last redisplay cycle. */
14275 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14276 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14277 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14279 struct glyph *g1 =
14280 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14282 /* Don't consider glyphs that are outside TEXT_AREA. */
14283 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14284 return 0;
14285 /* Keep the candidate whose buffer position is the closest to
14286 point or has the `cursor' property. */
14287 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14288 w->cursor.hpos >= 0
14289 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14290 && ((BUFFERP (g1->object)
14291 && (g1->charpos == pt_old /* an exact match always wins */
14292 || (BUFFERP (glyph->object)
14293 && eabs (g1->charpos - pt_old)
14294 < eabs (glyph->charpos - pt_old))))
14295 /* previous candidate is a glyph from a string that has
14296 a non-nil `cursor' property */
14297 || (STRINGP (g1->object)
14298 && (!NILP (Fget_char_property (make_number (g1->charpos),
14299 Qcursor, g1->object))
14300 /* previous candidate is from the same display
14301 string as this one, and the display string
14302 came from a text property */
14303 || (EQ (g1->object, glyph->object)
14304 && string_from_text_prop)
14305 /* this candidate is from newline and its
14306 position is not an exact match */
14307 || (INTEGERP (glyph->object)
14308 && glyph->charpos != pt_old)))))
14309 return 0;
14310 /* If this candidate gives an exact match, use that. */
14311 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14312 /* If this candidate is a glyph created for the
14313 terminating newline of a line, and point is on that
14314 newline, it wins because it's an exact match. */
14315 || (!row->continued_p
14316 && INTEGERP (glyph->object)
14317 && glyph->charpos == 0
14318 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14319 /* Otherwise, keep the candidate that comes from a row
14320 spanning less buffer positions. This may win when one or
14321 both candidate positions are on glyphs that came from
14322 display strings, for which we cannot compare buffer
14323 positions. */
14324 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14325 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14326 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14327 return 0;
14329 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14330 w->cursor.x = x;
14331 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14332 w->cursor.y = row->y + dy;
14334 if (w == XWINDOW (selected_window))
14336 if (!row->continued_p
14337 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14338 && row->x == 0)
14340 this_line_buffer = XBUFFER (w->buffer);
14342 CHARPOS (this_line_start_pos)
14343 = MATRIX_ROW_START_CHARPOS (row) + delta;
14344 BYTEPOS (this_line_start_pos)
14345 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14347 CHARPOS (this_line_end_pos)
14348 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14349 BYTEPOS (this_line_end_pos)
14350 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14352 this_line_y = w->cursor.y;
14353 this_line_pixel_height = row->height;
14354 this_line_vpos = w->cursor.vpos;
14355 this_line_start_x = row->x;
14357 else
14358 CHARPOS (this_line_start_pos) = 0;
14361 return 1;
14365 /* Run window scroll functions, if any, for WINDOW with new window
14366 start STARTP. Sets the window start of WINDOW to that position.
14368 We assume that the window's buffer is really current. */
14370 static inline struct text_pos
14371 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14373 struct window *w = XWINDOW (window);
14374 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14376 if (current_buffer != XBUFFER (w->buffer))
14377 abort ();
14379 if (!NILP (Vwindow_scroll_functions))
14381 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14382 make_number (CHARPOS (startp)));
14383 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14384 /* In case the hook functions switch buffers. */
14385 if (current_buffer != XBUFFER (w->buffer))
14386 set_buffer_internal_1 (XBUFFER (w->buffer));
14389 return startp;
14393 /* Make sure the line containing the cursor is fully visible.
14394 A value of 1 means there is nothing to be done.
14395 (Either the line is fully visible, or it cannot be made so,
14396 or we cannot tell.)
14398 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14399 is higher than window.
14401 A value of 0 means the caller should do scrolling
14402 as if point had gone off the screen. */
14404 static int
14405 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14407 struct glyph_matrix *matrix;
14408 struct glyph_row *row;
14409 int window_height;
14411 if (!make_cursor_line_fully_visible_p)
14412 return 1;
14414 /* It's not always possible to find the cursor, e.g, when a window
14415 is full of overlay strings. Don't do anything in that case. */
14416 if (w->cursor.vpos < 0)
14417 return 1;
14419 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14420 row = MATRIX_ROW (matrix, w->cursor.vpos);
14422 /* If the cursor row is not partially visible, there's nothing to do. */
14423 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14424 return 1;
14426 /* If the row the cursor is in is taller than the window's height,
14427 it's not clear what to do, so do nothing. */
14428 window_height = window_box_height (w);
14429 if (row->height >= window_height)
14431 if (!force_p || MINI_WINDOW_P (w)
14432 || w->vscroll || w->cursor.vpos == 0)
14433 return 1;
14435 return 0;
14439 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14440 non-zero means only WINDOW is redisplayed in redisplay_internal.
14441 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14442 in redisplay_window to bring a partially visible line into view in
14443 the case that only the cursor has moved.
14445 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14446 last screen line's vertical height extends past the end of the screen.
14448 Value is
14450 1 if scrolling succeeded
14452 0 if scrolling didn't find point.
14454 -1 if new fonts have been loaded so that we must interrupt
14455 redisplay, adjust glyph matrices, and try again. */
14457 enum
14459 SCROLLING_SUCCESS,
14460 SCROLLING_FAILED,
14461 SCROLLING_NEED_LARGER_MATRICES
14464 /* If scroll-conservatively is more than this, never recenter.
14466 If you change this, don't forget to update the doc string of
14467 `scroll-conservatively' and the Emacs manual. */
14468 #define SCROLL_LIMIT 100
14470 static int
14471 try_scrolling (Lisp_Object window, int just_this_one_p,
14472 EMACS_INT arg_scroll_conservatively, EMACS_INT scroll_step,
14473 int temp_scroll_step, int last_line_misfit)
14475 struct window *w = XWINDOW (window);
14476 struct frame *f = XFRAME (w->frame);
14477 struct text_pos pos, startp;
14478 struct it it;
14479 int this_scroll_margin, scroll_max, rc, height;
14480 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14481 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14482 Lisp_Object aggressive;
14483 /* We will never try scrolling more than this number of lines. */
14484 int scroll_limit = SCROLL_LIMIT;
14486 #if GLYPH_DEBUG
14487 debug_method_add (w, "try_scrolling");
14488 #endif
14490 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14492 /* Compute scroll margin height in pixels. We scroll when point is
14493 within this distance from the top or bottom of the window. */
14494 if (scroll_margin > 0)
14495 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14496 * FRAME_LINE_HEIGHT (f);
14497 else
14498 this_scroll_margin = 0;
14500 /* Force arg_scroll_conservatively to have a reasonable value, to
14501 avoid scrolling too far away with slow move_it_* functions. Note
14502 that the user can supply scroll-conservatively equal to
14503 `most-positive-fixnum', which can be larger than INT_MAX. */
14504 if (arg_scroll_conservatively > scroll_limit)
14506 arg_scroll_conservatively = scroll_limit + 1;
14507 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14509 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14510 /* Compute how much we should try to scroll maximally to bring
14511 point into view. */
14512 scroll_max = (max (scroll_step,
14513 max (arg_scroll_conservatively, temp_scroll_step))
14514 * FRAME_LINE_HEIGHT (f));
14515 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14516 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14517 /* We're trying to scroll because of aggressive scrolling but no
14518 scroll_step is set. Choose an arbitrary one. */
14519 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14520 else
14521 scroll_max = 0;
14523 too_near_end:
14525 /* Decide whether to scroll down. */
14526 if (PT > CHARPOS (startp))
14528 int scroll_margin_y;
14530 /* Compute the pixel ypos of the scroll margin, then move IT to
14531 either that ypos or PT, whichever comes first. */
14532 start_display (&it, w, startp);
14533 scroll_margin_y = it.last_visible_y - this_scroll_margin
14534 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14535 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14536 (MOVE_TO_POS | MOVE_TO_Y));
14538 if (PT > CHARPOS (it.current.pos))
14540 int y0 = line_bottom_y (&it);
14541 /* Compute how many pixels below window bottom to stop searching
14542 for PT. This avoids costly search for PT that is far away if
14543 the user limited scrolling by a small number of lines, but
14544 always finds PT if scroll_conservatively is set to a large
14545 number, such as most-positive-fixnum. */
14546 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14547 int y_to_move = it.last_visible_y + slack;
14549 /* Compute the distance from the scroll margin to PT or to
14550 the scroll limit, whichever comes first. This should
14551 include the height of the cursor line, to make that line
14552 fully visible. */
14553 move_it_to (&it, PT, -1, y_to_move,
14554 -1, MOVE_TO_POS | MOVE_TO_Y);
14555 dy = line_bottom_y (&it) - y0;
14557 if (dy > scroll_max)
14558 return SCROLLING_FAILED;
14560 if (dy > 0)
14561 scroll_down_p = 1;
14565 if (scroll_down_p)
14567 /* Point is in or below the bottom scroll margin, so move the
14568 window start down. If scrolling conservatively, move it just
14569 enough down to make point visible. If scroll_step is set,
14570 move it down by scroll_step. */
14571 if (arg_scroll_conservatively)
14572 amount_to_scroll
14573 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14574 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14575 else if (scroll_step || temp_scroll_step)
14576 amount_to_scroll = scroll_max;
14577 else
14579 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14580 height = WINDOW_BOX_TEXT_HEIGHT (w);
14581 if (NUMBERP (aggressive))
14583 double float_amount = XFLOATINT (aggressive) * height;
14584 amount_to_scroll = float_amount;
14585 if (amount_to_scroll == 0 && float_amount > 0)
14586 amount_to_scroll = 1;
14587 /* Don't let point enter the scroll margin near top of
14588 the window. */
14589 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14590 amount_to_scroll = height - 2*this_scroll_margin + dy;
14594 if (amount_to_scroll <= 0)
14595 return SCROLLING_FAILED;
14597 start_display (&it, w, startp);
14598 if (arg_scroll_conservatively <= scroll_limit)
14599 move_it_vertically (&it, amount_to_scroll);
14600 else
14602 /* Extra precision for users who set scroll-conservatively
14603 to a large number: make sure the amount we scroll
14604 the window start is never less than amount_to_scroll,
14605 which was computed as distance from window bottom to
14606 point. This matters when lines at window top and lines
14607 below window bottom have different height. */
14608 struct it it1;
14609 void *it1data = NULL;
14610 /* We use a temporary it1 because line_bottom_y can modify
14611 its argument, if it moves one line down; see there. */
14612 int start_y;
14614 SAVE_IT (it1, it, it1data);
14615 start_y = line_bottom_y (&it1);
14616 do {
14617 RESTORE_IT (&it, &it, it1data);
14618 move_it_by_lines (&it, 1);
14619 SAVE_IT (it1, it, it1data);
14620 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14623 /* If STARTP is unchanged, move it down another screen line. */
14624 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14625 move_it_by_lines (&it, 1);
14626 startp = it.current.pos;
14628 else
14630 struct text_pos scroll_margin_pos = startp;
14632 /* See if point is inside the scroll margin at the top of the
14633 window. */
14634 if (this_scroll_margin)
14636 start_display (&it, w, startp);
14637 move_it_vertically (&it, this_scroll_margin);
14638 scroll_margin_pos = it.current.pos;
14641 if (PT < CHARPOS (scroll_margin_pos))
14643 /* Point is in the scroll margin at the top of the window or
14644 above what is displayed in the window. */
14645 int y0, y_to_move;
14647 /* Compute the vertical distance from PT to the scroll
14648 margin position. Move as far as scroll_max allows, or
14649 one screenful, or 10 screen lines, whichever is largest.
14650 Give up if distance is greater than scroll_max. */
14651 SET_TEXT_POS (pos, PT, PT_BYTE);
14652 start_display (&it, w, pos);
14653 y0 = it.current_y;
14654 y_to_move = max (it.last_visible_y,
14655 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14656 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14657 y_to_move, -1,
14658 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14659 dy = it.current_y - y0;
14660 if (dy > scroll_max)
14661 return SCROLLING_FAILED;
14663 /* Compute new window start. */
14664 start_display (&it, w, startp);
14666 if (arg_scroll_conservatively)
14667 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14668 max (scroll_step, temp_scroll_step));
14669 else if (scroll_step || temp_scroll_step)
14670 amount_to_scroll = scroll_max;
14671 else
14673 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14674 height = WINDOW_BOX_TEXT_HEIGHT (w);
14675 if (NUMBERP (aggressive))
14677 double float_amount = XFLOATINT (aggressive) * height;
14678 amount_to_scroll = float_amount;
14679 if (amount_to_scroll == 0 && float_amount > 0)
14680 amount_to_scroll = 1;
14681 amount_to_scroll -=
14682 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14683 /* Don't let point enter the scroll margin near
14684 bottom of the window. */
14685 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14686 amount_to_scroll = height - 2*this_scroll_margin + dy;
14690 if (amount_to_scroll <= 0)
14691 return SCROLLING_FAILED;
14693 move_it_vertically_backward (&it, amount_to_scroll);
14694 startp = it.current.pos;
14698 /* Run window scroll functions. */
14699 startp = run_window_scroll_functions (window, startp);
14701 /* Display the window. Give up if new fonts are loaded, or if point
14702 doesn't appear. */
14703 if (!try_window (window, startp, 0))
14704 rc = SCROLLING_NEED_LARGER_MATRICES;
14705 else if (w->cursor.vpos < 0)
14707 clear_glyph_matrix (w->desired_matrix);
14708 rc = SCROLLING_FAILED;
14710 else
14712 /* Maybe forget recorded base line for line number display. */
14713 if (!just_this_one_p
14714 || current_buffer->clip_changed
14715 || BEG_UNCHANGED < CHARPOS (startp))
14716 w->base_line_number = Qnil;
14718 /* If cursor ends up on a partially visible line,
14719 treat that as being off the bottom of the screen. */
14720 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14721 /* It's possible that the cursor is on the first line of the
14722 buffer, which is partially obscured due to a vscroll
14723 (Bug#7537). In that case, avoid looping forever . */
14724 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14726 clear_glyph_matrix (w->desired_matrix);
14727 ++extra_scroll_margin_lines;
14728 goto too_near_end;
14730 rc = SCROLLING_SUCCESS;
14733 return rc;
14737 /* Compute a suitable window start for window W if display of W starts
14738 on a continuation line. Value is non-zero if a new window start
14739 was computed.
14741 The new window start will be computed, based on W's width, starting
14742 from the start of the continued line. It is the start of the
14743 screen line with the minimum distance from the old start W->start. */
14745 static int
14746 compute_window_start_on_continuation_line (struct window *w)
14748 struct text_pos pos, start_pos;
14749 int window_start_changed_p = 0;
14751 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14753 /* If window start is on a continuation line... Window start may be
14754 < BEGV in case there's invisible text at the start of the
14755 buffer (M-x rmail, for example). */
14756 if (CHARPOS (start_pos) > BEGV
14757 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14759 struct it it;
14760 struct glyph_row *row;
14762 /* Handle the case that the window start is out of range. */
14763 if (CHARPOS (start_pos) < BEGV)
14764 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14765 else if (CHARPOS (start_pos) > ZV)
14766 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14768 /* Find the start of the continued line. This should be fast
14769 because scan_buffer is fast (newline cache). */
14770 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14771 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14772 row, DEFAULT_FACE_ID);
14773 reseat_at_previous_visible_line_start (&it);
14775 /* If the line start is "too far" away from the window start,
14776 say it takes too much time to compute a new window start. */
14777 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14778 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14780 int min_distance, distance;
14782 /* Move forward by display lines to find the new window
14783 start. If window width was enlarged, the new start can
14784 be expected to be > the old start. If window width was
14785 decreased, the new window start will be < the old start.
14786 So, we're looking for the display line start with the
14787 minimum distance from the old window start. */
14788 pos = it.current.pos;
14789 min_distance = INFINITY;
14790 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14791 distance < min_distance)
14793 min_distance = distance;
14794 pos = it.current.pos;
14795 move_it_by_lines (&it, 1);
14798 /* Set the window start there. */
14799 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14800 window_start_changed_p = 1;
14804 return window_start_changed_p;
14808 /* Try cursor movement in case text has not changed in window WINDOW,
14809 with window start STARTP. Value is
14811 CURSOR_MOVEMENT_SUCCESS if successful
14813 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14815 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14816 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14817 we want to scroll as if scroll-step were set to 1. See the code.
14819 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14820 which case we have to abort this redisplay, and adjust matrices
14821 first. */
14823 enum
14825 CURSOR_MOVEMENT_SUCCESS,
14826 CURSOR_MOVEMENT_CANNOT_BE_USED,
14827 CURSOR_MOVEMENT_MUST_SCROLL,
14828 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14831 static int
14832 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14834 struct window *w = XWINDOW (window);
14835 struct frame *f = XFRAME (w->frame);
14836 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14838 #if GLYPH_DEBUG
14839 if (inhibit_try_cursor_movement)
14840 return rc;
14841 #endif
14843 /* Handle case where text has not changed, only point, and it has
14844 not moved off the frame. */
14845 if (/* Point may be in this window. */
14846 PT >= CHARPOS (startp)
14847 /* Selective display hasn't changed. */
14848 && !current_buffer->clip_changed
14849 /* Function force-mode-line-update is used to force a thorough
14850 redisplay. It sets either windows_or_buffers_changed or
14851 update_mode_lines. So don't take a shortcut here for these
14852 cases. */
14853 && !update_mode_lines
14854 && !windows_or_buffers_changed
14855 && !cursor_type_changed
14856 /* Can't use this case if highlighting a region. When a
14857 region exists, cursor movement has to do more than just
14858 set the cursor. */
14859 && !(!NILP (Vtransient_mark_mode)
14860 && !NILP (BVAR (current_buffer, mark_active)))
14861 && NILP (w->region_showing)
14862 && NILP (Vshow_trailing_whitespace)
14863 /* Right after splitting windows, last_point may be nil. */
14864 && INTEGERP (w->last_point)
14865 /* This code is not used for mini-buffer for the sake of the case
14866 of redisplaying to replace an echo area message; since in
14867 that case the mini-buffer contents per se are usually
14868 unchanged. This code is of no real use in the mini-buffer
14869 since the handling of this_line_start_pos, etc., in redisplay
14870 handles the same cases. */
14871 && !EQ (window, minibuf_window)
14872 /* When splitting windows or for new windows, it happens that
14873 redisplay is called with a nil window_end_vpos or one being
14874 larger than the window. This should really be fixed in
14875 window.c. I don't have this on my list, now, so we do
14876 approximately the same as the old redisplay code. --gerd. */
14877 && INTEGERP (w->window_end_vpos)
14878 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14879 && (FRAME_WINDOW_P (f)
14880 || !overlay_arrow_in_current_buffer_p ()))
14882 int this_scroll_margin, top_scroll_margin;
14883 struct glyph_row *row = NULL;
14885 #if GLYPH_DEBUG
14886 debug_method_add (w, "cursor movement");
14887 #endif
14889 /* Scroll if point within this distance from the top or bottom
14890 of the window. This is a pixel value. */
14891 if (scroll_margin > 0)
14893 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14894 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14896 else
14897 this_scroll_margin = 0;
14899 top_scroll_margin = this_scroll_margin;
14900 if (WINDOW_WANTS_HEADER_LINE_P (w))
14901 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14903 /* Start with the row the cursor was displayed during the last
14904 not paused redisplay. Give up if that row is not valid. */
14905 if (w->last_cursor.vpos < 0
14906 || w->last_cursor.vpos >= w->current_matrix->nrows)
14907 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14908 else
14910 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14911 if (row->mode_line_p)
14912 ++row;
14913 if (!row->enabled_p)
14914 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14917 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14919 int scroll_p = 0, must_scroll = 0;
14920 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14922 if (PT > XFASTINT (w->last_point))
14924 /* Point has moved forward. */
14925 while (MATRIX_ROW_END_CHARPOS (row) < PT
14926 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14928 xassert (row->enabled_p);
14929 ++row;
14932 /* If the end position of a row equals the start
14933 position of the next row, and PT is at that position,
14934 we would rather display cursor in the next line. */
14935 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14936 && MATRIX_ROW_END_CHARPOS (row) == PT
14937 && row < w->current_matrix->rows
14938 + w->current_matrix->nrows - 1
14939 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14940 && !cursor_row_p (row))
14941 ++row;
14943 /* If within the scroll margin, scroll. Note that
14944 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14945 the next line would be drawn, and that
14946 this_scroll_margin can be zero. */
14947 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14948 || PT > MATRIX_ROW_END_CHARPOS (row)
14949 /* Line is completely visible last line in window
14950 and PT is to be set in the next line. */
14951 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14952 && PT == MATRIX_ROW_END_CHARPOS (row)
14953 && !row->ends_at_zv_p
14954 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14955 scroll_p = 1;
14957 else if (PT < XFASTINT (w->last_point))
14959 /* Cursor has to be moved backward. Note that PT >=
14960 CHARPOS (startp) because of the outer if-statement. */
14961 while (!row->mode_line_p
14962 && (MATRIX_ROW_START_CHARPOS (row) > PT
14963 || (MATRIX_ROW_START_CHARPOS (row) == PT
14964 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14965 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14966 row > w->current_matrix->rows
14967 && (row-1)->ends_in_newline_from_string_p))))
14968 && (row->y > top_scroll_margin
14969 || CHARPOS (startp) == BEGV))
14971 xassert (row->enabled_p);
14972 --row;
14975 /* Consider the following case: Window starts at BEGV,
14976 there is invisible, intangible text at BEGV, so that
14977 display starts at some point START > BEGV. It can
14978 happen that we are called with PT somewhere between
14979 BEGV and START. Try to handle that case. */
14980 if (row < w->current_matrix->rows
14981 || row->mode_line_p)
14983 row = w->current_matrix->rows;
14984 if (row->mode_line_p)
14985 ++row;
14988 /* Due to newlines in overlay strings, we may have to
14989 skip forward over overlay strings. */
14990 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14991 && MATRIX_ROW_END_CHARPOS (row) == PT
14992 && !cursor_row_p (row))
14993 ++row;
14995 /* If within the scroll margin, scroll. */
14996 if (row->y < top_scroll_margin
14997 && CHARPOS (startp) != BEGV)
14998 scroll_p = 1;
15000 else
15002 /* Cursor did not move. So don't scroll even if cursor line
15003 is partially visible, as it was so before. */
15004 rc = CURSOR_MOVEMENT_SUCCESS;
15007 if (PT < MATRIX_ROW_START_CHARPOS (row)
15008 || PT > MATRIX_ROW_END_CHARPOS (row))
15010 /* if PT is not in the glyph row, give up. */
15011 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15012 must_scroll = 1;
15014 else if (rc != CURSOR_MOVEMENT_SUCCESS
15015 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15017 struct glyph_row *row1;
15019 /* If rows are bidi-reordered and point moved, back up
15020 until we find a row that does not belong to a
15021 continuation line. This is because we must consider
15022 all rows of a continued line as candidates for the
15023 new cursor positioning, since row start and end
15024 positions change non-linearly with vertical position
15025 in such rows. */
15026 /* FIXME: Revisit this when glyph ``spilling'' in
15027 continuation lines' rows is implemented for
15028 bidi-reordered rows. */
15029 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15030 MATRIX_ROW_CONTINUATION_LINE_P (row);
15031 --row)
15033 /* If we hit the beginning of the displayed portion
15034 without finding the first row of a continued
15035 line, give up. */
15036 if (row <= row1)
15038 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15039 break;
15041 xassert (row->enabled_p);
15044 if (must_scroll)
15046 else if (rc != CURSOR_MOVEMENT_SUCCESS
15047 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15048 /* Make sure this isn't a header line by any chance, since
15049 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15050 && !row->mode_line_p
15051 && make_cursor_line_fully_visible_p)
15053 if (PT == MATRIX_ROW_END_CHARPOS (row)
15054 && !row->ends_at_zv_p
15055 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15056 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15057 else if (row->height > window_box_height (w))
15059 /* If we end up in a partially visible line, let's
15060 make it fully visible, except when it's taller
15061 than the window, in which case we can't do much
15062 about it. */
15063 *scroll_step = 1;
15064 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15066 else
15068 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15069 if (!cursor_row_fully_visible_p (w, 0, 1))
15070 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15071 else
15072 rc = CURSOR_MOVEMENT_SUCCESS;
15075 else if (scroll_p)
15076 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15077 else if (rc != CURSOR_MOVEMENT_SUCCESS
15078 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15080 /* With bidi-reordered rows, there could be more than
15081 one candidate row whose start and end positions
15082 occlude point. We need to let set_cursor_from_row
15083 find the best candidate. */
15084 /* FIXME: Revisit this when glyph ``spilling'' in
15085 continuation lines' rows is implemented for
15086 bidi-reordered rows. */
15087 int rv = 0;
15091 int at_zv_p = 0, exact_match_p = 0;
15093 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15094 && PT <= MATRIX_ROW_END_CHARPOS (row)
15095 && cursor_row_p (row))
15096 rv |= set_cursor_from_row (w, row, w->current_matrix,
15097 0, 0, 0, 0);
15098 /* As soon as we've found the exact match for point,
15099 or the first suitable row whose ends_at_zv_p flag
15100 is set, we are done. */
15101 at_zv_p =
15102 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15103 if (rv && !at_zv_p
15104 && w->cursor.hpos >= 0
15105 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15106 w->cursor.vpos))
15108 struct glyph_row *candidate =
15109 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15110 struct glyph *g =
15111 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15112 EMACS_INT endpos = MATRIX_ROW_END_CHARPOS (candidate);
15114 exact_match_p =
15115 (BUFFERP (g->object) && g->charpos == PT)
15116 || (INTEGERP (g->object)
15117 && (g->charpos == PT
15118 || (g->charpos == 0 && endpos - 1 == PT)));
15120 if (rv && (at_zv_p || exact_match_p))
15122 rc = CURSOR_MOVEMENT_SUCCESS;
15123 break;
15125 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15126 break;
15127 ++row;
15129 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15130 || row->continued_p)
15131 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15132 || (MATRIX_ROW_START_CHARPOS (row) == PT
15133 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15134 /* If we didn't find any candidate rows, or exited the
15135 loop before all the candidates were examined, signal
15136 to the caller that this method failed. */
15137 if (rc != CURSOR_MOVEMENT_SUCCESS
15138 && !(rv
15139 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15140 && !row->continued_p))
15141 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15142 else if (rv)
15143 rc = CURSOR_MOVEMENT_SUCCESS;
15145 else
15149 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15151 rc = CURSOR_MOVEMENT_SUCCESS;
15152 break;
15154 ++row;
15156 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15157 && MATRIX_ROW_START_CHARPOS (row) == PT
15158 && cursor_row_p (row));
15163 return rc;
15166 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15167 static
15168 #endif
15169 void
15170 set_vertical_scroll_bar (struct window *w)
15172 EMACS_INT start, end, whole;
15174 /* Calculate the start and end positions for the current window.
15175 At some point, it would be nice to choose between scrollbars
15176 which reflect the whole buffer size, with special markers
15177 indicating narrowing, and scrollbars which reflect only the
15178 visible region.
15180 Note that mini-buffers sometimes aren't displaying any text. */
15181 if (!MINI_WINDOW_P (w)
15182 || (w == XWINDOW (minibuf_window)
15183 && NILP (echo_area_buffer[0])))
15185 struct buffer *buf = XBUFFER (w->buffer);
15186 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15187 start = marker_position (w->start) - BUF_BEGV (buf);
15188 /* I don't think this is guaranteed to be right. For the
15189 moment, we'll pretend it is. */
15190 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15192 if (end < start)
15193 end = start;
15194 if (whole < (end - start))
15195 whole = end - start;
15197 else
15198 start = end = whole = 0;
15200 /* Indicate what this scroll bar ought to be displaying now. */
15201 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15202 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15203 (w, end - start, whole, start);
15207 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15208 selected_window is redisplayed.
15210 We can return without actually redisplaying the window if
15211 fonts_changed_p is nonzero. In that case, redisplay_internal will
15212 retry. */
15214 static void
15215 redisplay_window (Lisp_Object window, int just_this_one_p)
15217 struct window *w = XWINDOW (window);
15218 struct frame *f = XFRAME (w->frame);
15219 struct buffer *buffer = XBUFFER (w->buffer);
15220 struct buffer *old = current_buffer;
15221 struct text_pos lpoint, opoint, startp;
15222 int update_mode_line;
15223 int tem;
15224 struct it it;
15225 /* Record it now because it's overwritten. */
15226 int current_matrix_up_to_date_p = 0;
15227 int used_current_matrix_p = 0;
15228 /* This is less strict than current_matrix_up_to_date_p.
15229 It indicates that the buffer contents and narrowing are unchanged. */
15230 int buffer_unchanged_p = 0;
15231 int temp_scroll_step = 0;
15232 int count = SPECPDL_INDEX ();
15233 int rc;
15234 int centering_position = -1;
15235 int last_line_misfit = 0;
15236 EMACS_INT beg_unchanged, end_unchanged;
15238 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15239 opoint = lpoint;
15241 /* W must be a leaf window here. */
15242 xassert (!NILP (w->buffer));
15243 #if GLYPH_DEBUG
15244 *w->desired_matrix->method = 0;
15245 #endif
15247 restart:
15248 reconsider_clip_changes (w, buffer);
15250 /* Has the mode line to be updated? */
15251 update_mode_line = (!NILP (w->update_mode_line)
15252 || update_mode_lines
15253 || buffer->clip_changed
15254 || buffer->prevent_redisplay_optimizations_p);
15256 if (MINI_WINDOW_P (w))
15258 if (w == XWINDOW (echo_area_window)
15259 && !NILP (echo_area_buffer[0]))
15261 if (update_mode_line)
15262 /* We may have to update a tty frame's menu bar or a
15263 tool-bar. Example `M-x C-h C-h C-g'. */
15264 goto finish_menu_bars;
15265 else
15266 /* We've already displayed the echo area glyphs in this window. */
15267 goto finish_scroll_bars;
15269 else if ((w != XWINDOW (minibuf_window)
15270 || minibuf_level == 0)
15271 /* When buffer is nonempty, redisplay window normally. */
15272 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15273 /* Quail displays non-mini buffers in minibuffer window.
15274 In that case, redisplay the window normally. */
15275 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15277 /* W is a mini-buffer window, but it's not active, so clear
15278 it. */
15279 int yb = window_text_bottom_y (w);
15280 struct glyph_row *row;
15281 int y;
15283 for (y = 0, row = w->desired_matrix->rows;
15284 y < yb;
15285 y += row->height, ++row)
15286 blank_row (w, row, y);
15287 goto finish_scroll_bars;
15290 clear_glyph_matrix (w->desired_matrix);
15293 /* Otherwise set up data on this window; select its buffer and point
15294 value. */
15295 /* Really select the buffer, for the sake of buffer-local
15296 variables. */
15297 set_buffer_internal_1 (XBUFFER (w->buffer));
15299 current_matrix_up_to_date_p
15300 = (!NILP (w->window_end_valid)
15301 && !current_buffer->clip_changed
15302 && !current_buffer->prevent_redisplay_optimizations_p
15303 && XFASTINT (w->last_modified) >= MODIFF
15304 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15306 /* Run the window-bottom-change-functions
15307 if it is possible that the text on the screen has changed
15308 (either due to modification of the text, or any other reason). */
15309 if (!current_matrix_up_to_date_p
15310 && !NILP (Vwindow_text_change_functions))
15312 safe_run_hooks (Qwindow_text_change_functions);
15313 goto restart;
15316 beg_unchanged = BEG_UNCHANGED;
15317 end_unchanged = END_UNCHANGED;
15319 SET_TEXT_POS (opoint, PT, PT_BYTE);
15321 specbind (Qinhibit_point_motion_hooks, Qt);
15323 buffer_unchanged_p
15324 = (!NILP (w->window_end_valid)
15325 && !current_buffer->clip_changed
15326 && XFASTINT (w->last_modified) >= MODIFF
15327 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15329 /* When windows_or_buffers_changed is non-zero, we can't rely on
15330 the window end being valid, so set it to nil there. */
15331 if (windows_or_buffers_changed)
15333 /* If window starts on a continuation line, maybe adjust the
15334 window start in case the window's width changed. */
15335 if (XMARKER (w->start)->buffer == current_buffer)
15336 compute_window_start_on_continuation_line (w);
15338 w->window_end_valid = Qnil;
15341 /* Some sanity checks. */
15342 CHECK_WINDOW_END (w);
15343 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15344 abort ();
15345 if (BYTEPOS (opoint) < CHARPOS (opoint))
15346 abort ();
15348 /* If %c is in mode line, update it if needed. */
15349 if (!NILP (w->column_number_displayed)
15350 /* This alternative quickly identifies a common case
15351 where no change is needed. */
15352 && !(PT == XFASTINT (w->last_point)
15353 && XFASTINT (w->last_modified) >= MODIFF
15354 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
15355 && (XFASTINT (w->column_number_displayed) != current_column ()))
15356 update_mode_line = 1;
15358 /* Count number of windows showing the selected buffer. An indirect
15359 buffer counts as its base buffer. */
15360 if (!just_this_one_p)
15362 struct buffer *current_base, *window_base;
15363 current_base = current_buffer;
15364 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15365 if (current_base->base_buffer)
15366 current_base = current_base->base_buffer;
15367 if (window_base->base_buffer)
15368 window_base = window_base->base_buffer;
15369 if (current_base == window_base)
15370 buffer_shared++;
15373 /* Point refers normally to the selected window. For any other
15374 window, set up appropriate value. */
15375 if (!EQ (window, selected_window))
15377 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
15378 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
15379 if (new_pt < BEGV)
15381 new_pt = BEGV;
15382 new_pt_byte = BEGV_BYTE;
15383 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15385 else if (new_pt > (ZV - 1))
15387 new_pt = ZV;
15388 new_pt_byte = ZV_BYTE;
15389 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15392 /* We don't use SET_PT so that the point-motion hooks don't run. */
15393 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15396 /* If any of the character widths specified in the display table
15397 have changed, invalidate the width run cache. It's true that
15398 this may be a bit late to catch such changes, but the rest of
15399 redisplay goes (non-fatally) haywire when the display table is
15400 changed, so why should we worry about doing any better? */
15401 if (current_buffer->width_run_cache)
15403 struct Lisp_Char_Table *disptab = buffer_display_table ();
15405 if (! disptab_matches_widthtab (disptab,
15406 XVECTOR (BVAR (current_buffer, width_table))))
15408 invalidate_region_cache (current_buffer,
15409 current_buffer->width_run_cache,
15410 BEG, Z);
15411 recompute_width_table (current_buffer, disptab);
15415 /* If window-start is screwed up, choose a new one. */
15416 if (XMARKER (w->start)->buffer != current_buffer)
15417 goto recenter;
15419 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15421 /* If someone specified a new starting point but did not insist,
15422 check whether it can be used. */
15423 if (!NILP (w->optional_new_start)
15424 && CHARPOS (startp) >= BEGV
15425 && CHARPOS (startp) <= ZV)
15427 w->optional_new_start = Qnil;
15428 start_display (&it, w, startp);
15429 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15430 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15431 if (IT_CHARPOS (it) == PT)
15432 w->force_start = Qt;
15433 /* IT may overshoot PT if text at PT is invisible. */
15434 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15435 w->force_start = Qt;
15438 force_start:
15440 /* Handle case where place to start displaying has been specified,
15441 unless the specified location is outside the accessible range. */
15442 if (!NILP (w->force_start)
15443 || w->frozen_window_start_p)
15445 /* We set this later on if we have to adjust point. */
15446 int new_vpos = -1;
15448 w->force_start = Qnil;
15449 w->vscroll = 0;
15450 w->window_end_valid = Qnil;
15452 /* Forget any recorded base line for line number display. */
15453 if (!buffer_unchanged_p)
15454 w->base_line_number = Qnil;
15456 /* Redisplay the mode line. Select the buffer properly for that.
15457 Also, run the hook window-scroll-functions
15458 because we have scrolled. */
15459 /* Note, we do this after clearing force_start because
15460 if there's an error, it is better to forget about force_start
15461 than to get into an infinite loop calling the hook functions
15462 and having them get more errors. */
15463 if (!update_mode_line
15464 || ! NILP (Vwindow_scroll_functions))
15466 update_mode_line = 1;
15467 w->update_mode_line = Qt;
15468 startp = run_window_scroll_functions (window, startp);
15471 w->last_modified = make_number (0);
15472 w->last_overlay_modified = make_number (0);
15473 if (CHARPOS (startp) < BEGV)
15474 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15475 else if (CHARPOS (startp) > ZV)
15476 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15478 /* Redisplay, then check if cursor has been set during the
15479 redisplay. Give up if new fonts were loaded. */
15480 /* We used to issue a CHECK_MARGINS argument to try_window here,
15481 but this causes scrolling to fail when point begins inside
15482 the scroll margin (bug#148) -- cyd */
15483 if (!try_window (window, startp, 0))
15485 w->force_start = Qt;
15486 clear_glyph_matrix (w->desired_matrix);
15487 goto need_larger_matrices;
15490 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15492 /* If point does not appear, try to move point so it does
15493 appear. The desired matrix has been built above, so we
15494 can use it here. */
15495 new_vpos = window_box_height (w) / 2;
15498 if (!cursor_row_fully_visible_p (w, 0, 0))
15500 /* Point does appear, but on a line partly visible at end of window.
15501 Move it back to a fully-visible line. */
15502 new_vpos = window_box_height (w);
15505 /* If we need to move point for either of the above reasons,
15506 now actually do it. */
15507 if (new_vpos >= 0)
15509 struct glyph_row *row;
15511 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15512 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15513 ++row;
15515 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15516 MATRIX_ROW_START_BYTEPOS (row));
15518 if (w != XWINDOW (selected_window))
15519 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15520 else if (current_buffer == old)
15521 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15523 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15525 /* If we are highlighting the region, then we just changed
15526 the region, so redisplay to show it. */
15527 if (!NILP (Vtransient_mark_mode)
15528 && !NILP (BVAR (current_buffer, mark_active)))
15530 clear_glyph_matrix (w->desired_matrix);
15531 if (!try_window (window, startp, 0))
15532 goto need_larger_matrices;
15536 #if GLYPH_DEBUG
15537 debug_method_add (w, "forced window start");
15538 #endif
15539 goto done;
15542 /* Handle case where text has not changed, only point, and it has
15543 not moved off the frame, and we are not retrying after hscroll.
15544 (current_matrix_up_to_date_p is nonzero when retrying.) */
15545 if (current_matrix_up_to_date_p
15546 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15547 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15549 switch (rc)
15551 case CURSOR_MOVEMENT_SUCCESS:
15552 used_current_matrix_p = 1;
15553 goto done;
15555 case CURSOR_MOVEMENT_MUST_SCROLL:
15556 goto try_to_scroll;
15558 default:
15559 abort ();
15562 /* If current starting point was originally the beginning of a line
15563 but no longer is, find a new starting point. */
15564 else if (!NILP (w->start_at_line_beg)
15565 && !(CHARPOS (startp) <= BEGV
15566 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15568 #if GLYPH_DEBUG
15569 debug_method_add (w, "recenter 1");
15570 #endif
15571 goto recenter;
15574 /* Try scrolling with try_window_id. Value is > 0 if update has
15575 been done, it is -1 if we know that the same window start will
15576 not work. It is 0 if unsuccessful for some other reason. */
15577 else if ((tem = try_window_id (w)) != 0)
15579 #if GLYPH_DEBUG
15580 debug_method_add (w, "try_window_id %d", tem);
15581 #endif
15583 if (fonts_changed_p)
15584 goto need_larger_matrices;
15585 if (tem > 0)
15586 goto done;
15588 /* Otherwise try_window_id has returned -1 which means that we
15589 don't want the alternative below this comment to execute. */
15591 else if (CHARPOS (startp) >= BEGV
15592 && CHARPOS (startp) <= ZV
15593 && PT >= CHARPOS (startp)
15594 && (CHARPOS (startp) < ZV
15595 /* Avoid starting at end of buffer. */
15596 || CHARPOS (startp) == BEGV
15597 || (XFASTINT (w->last_modified) >= MODIFF
15598 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
15600 int d1, d2, d3, d4, d5, d6;
15602 /* If first window line is a continuation line, and window start
15603 is inside the modified region, but the first change is before
15604 current window start, we must select a new window start.
15606 However, if this is the result of a down-mouse event (e.g. by
15607 extending the mouse-drag-overlay), we don't want to select a
15608 new window start, since that would change the position under
15609 the mouse, resulting in an unwanted mouse-movement rather
15610 than a simple mouse-click. */
15611 if (NILP (w->start_at_line_beg)
15612 && NILP (do_mouse_tracking)
15613 && CHARPOS (startp) > BEGV
15614 && CHARPOS (startp) > BEG + beg_unchanged
15615 && CHARPOS (startp) <= Z - end_unchanged
15616 /* Even if w->start_at_line_beg is nil, a new window may
15617 start at a line_beg, since that's how set_buffer_window
15618 sets it. So, we need to check the return value of
15619 compute_window_start_on_continuation_line. (See also
15620 bug#197). */
15621 && XMARKER (w->start)->buffer == current_buffer
15622 && compute_window_start_on_continuation_line (w)
15623 /* It doesn't make sense to force the window start like we
15624 do at label force_start if it is already known that point
15625 will not be visible in the resulting window, because
15626 doing so will move point from its correct position
15627 instead of scrolling the window to bring point into view.
15628 See bug#9324. */
15629 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15631 w->force_start = Qt;
15632 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15633 goto force_start;
15636 #if GLYPH_DEBUG
15637 debug_method_add (w, "same window start");
15638 #endif
15640 /* Try to redisplay starting at same place as before.
15641 If point has not moved off frame, accept the results. */
15642 if (!current_matrix_up_to_date_p
15643 /* Don't use try_window_reusing_current_matrix in this case
15644 because a window scroll function can have changed the
15645 buffer. */
15646 || !NILP (Vwindow_scroll_functions)
15647 || MINI_WINDOW_P (w)
15648 || !(used_current_matrix_p
15649 = try_window_reusing_current_matrix (w)))
15651 IF_DEBUG (debug_method_add (w, "1"));
15652 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15653 /* -1 means we need to scroll.
15654 0 means we need new matrices, but fonts_changed_p
15655 is set in that case, so we will detect it below. */
15656 goto try_to_scroll;
15659 if (fonts_changed_p)
15660 goto need_larger_matrices;
15662 if (w->cursor.vpos >= 0)
15664 if (!just_this_one_p
15665 || current_buffer->clip_changed
15666 || BEG_UNCHANGED < CHARPOS (startp))
15667 /* Forget any recorded base line for line number display. */
15668 w->base_line_number = Qnil;
15670 if (!cursor_row_fully_visible_p (w, 1, 0))
15672 clear_glyph_matrix (w->desired_matrix);
15673 last_line_misfit = 1;
15675 /* Drop through and scroll. */
15676 else
15677 goto done;
15679 else
15680 clear_glyph_matrix (w->desired_matrix);
15683 try_to_scroll:
15685 w->last_modified = make_number (0);
15686 w->last_overlay_modified = make_number (0);
15688 /* Redisplay the mode line. Select the buffer properly for that. */
15689 if (!update_mode_line)
15691 update_mode_line = 1;
15692 w->update_mode_line = Qt;
15695 /* Try to scroll by specified few lines. */
15696 if ((scroll_conservatively
15697 || emacs_scroll_step
15698 || temp_scroll_step
15699 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15700 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15701 && CHARPOS (startp) >= BEGV
15702 && CHARPOS (startp) <= ZV)
15704 /* The function returns -1 if new fonts were loaded, 1 if
15705 successful, 0 if not successful. */
15706 int ss = try_scrolling (window, just_this_one_p,
15707 scroll_conservatively,
15708 emacs_scroll_step,
15709 temp_scroll_step, last_line_misfit);
15710 switch (ss)
15712 case SCROLLING_SUCCESS:
15713 goto done;
15715 case SCROLLING_NEED_LARGER_MATRICES:
15716 goto need_larger_matrices;
15718 case SCROLLING_FAILED:
15719 break;
15721 default:
15722 abort ();
15726 /* Finally, just choose a place to start which positions point
15727 according to user preferences. */
15729 recenter:
15731 #if GLYPH_DEBUG
15732 debug_method_add (w, "recenter");
15733 #endif
15735 /* w->vscroll = 0; */
15737 /* Forget any previously recorded base line for line number display. */
15738 if (!buffer_unchanged_p)
15739 w->base_line_number = Qnil;
15741 /* Determine the window start relative to point. */
15742 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15743 it.current_y = it.last_visible_y;
15744 if (centering_position < 0)
15746 int margin =
15747 scroll_margin > 0
15748 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15749 : 0;
15750 EMACS_INT margin_pos = CHARPOS (startp);
15751 Lisp_Object aggressive;
15752 int scrolling_up;
15754 /* If there is a scroll margin at the top of the window, find
15755 its character position. */
15756 if (margin
15757 /* Cannot call start_display if startp is not in the
15758 accessible region of the buffer. This can happen when we
15759 have just switched to a different buffer and/or changed
15760 its restriction. In that case, startp is initialized to
15761 the character position 1 (BEGV) because we did not yet
15762 have chance to display the buffer even once. */
15763 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15765 struct it it1;
15766 void *it1data = NULL;
15768 SAVE_IT (it1, it, it1data);
15769 start_display (&it1, w, startp);
15770 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15771 margin_pos = IT_CHARPOS (it1);
15772 RESTORE_IT (&it, &it, it1data);
15774 scrolling_up = PT > margin_pos;
15775 aggressive =
15776 scrolling_up
15777 ? BVAR (current_buffer, scroll_up_aggressively)
15778 : BVAR (current_buffer, scroll_down_aggressively);
15780 if (!MINI_WINDOW_P (w)
15781 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15783 int pt_offset = 0;
15785 /* Setting scroll-conservatively overrides
15786 scroll-*-aggressively. */
15787 if (!scroll_conservatively && NUMBERP (aggressive))
15789 double float_amount = XFLOATINT (aggressive);
15791 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15792 if (pt_offset == 0 && float_amount > 0)
15793 pt_offset = 1;
15794 if (pt_offset && margin > 0)
15795 margin -= 1;
15797 /* Compute how much to move the window start backward from
15798 point so that point will be displayed where the user
15799 wants it. */
15800 if (scrolling_up)
15802 centering_position = it.last_visible_y;
15803 if (pt_offset)
15804 centering_position -= pt_offset;
15805 centering_position -=
15806 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15807 + WINDOW_HEADER_LINE_HEIGHT (w);
15808 /* Don't let point enter the scroll margin near top of
15809 the window. */
15810 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15811 centering_position = margin * FRAME_LINE_HEIGHT (f);
15813 else
15814 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15816 else
15817 /* Set the window start half the height of the window backward
15818 from point. */
15819 centering_position = window_box_height (w) / 2;
15821 move_it_vertically_backward (&it, centering_position);
15823 xassert (IT_CHARPOS (it) >= BEGV);
15825 /* The function move_it_vertically_backward may move over more
15826 than the specified y-distance. If it->w is small, e.g. a
15827 mini-buffer window, we may end up in front of the window's
15828 display area. Start displaying at the start of the line
15829 containing PT in this case. */
15830 if (it.current_y <= 0)
15832 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15833 move_it_vertically_backward (&it, 0);
15834 it.current_y = 0;
15837 it.current_x = it.hpos = 0;
15839 /* Set the window start position here explicitly, to avoid an
15840 infinite loop in case the functions in window-scroll-functions
15841 get errors. */
15842 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15844 /* Run scroll hooks. */
15845 startp = run_window_scroll_functions (window, it.current.pos);
15847 /* Redisplay the window. */
15848 if (!current_matrix_up_to_date_p
15849 || windows_or_buffers_changed
15850 || cursor_type_changed
15851 /* Don't use try_window_reusing_current_matrix in this case
15852 because it can have changed the buffer. */
15853 || !NILP (Vwindow_scroll_functions)
15854 || !just_this_one_p
15855 || MINI_WINDOW_P (w)
15856 || !(used_current_matrix_p
15857 = try_window_reusing_current_matrix (w)))
15858 try_window (window, startp, 0);
15860 /* If new fonts have been loaded (due to fontsets), give up. We
15861 have to start a new redisplay since we need to re-adjust glyph
15862 matrices. */
15863 if (fonts_changed_p)
15864 goto need_larger_matrices;
15866 /* If cursor did not appear assume that the middle of the window is
15867 in the first line of the window. Do it again with the next line.
15868 (Imagine a window of height 100, displaying two lines of height
15869 60. Moving back 50 from it->last_visible_y will end in the first
15870 line.) */
15871 if (w->cursor.vpos < 0)
15873 if (!NILP (w->window_end_valid)
15874 && PT >= Z - XFASTINT (w->window_end_pos))
15876 clear_glyph_matrix (w->desired_matrix);
15877 move_it_by_lines (&it, 1);
15878 try_window (window, it.current.pos, 0);
15880 else if (PT < IT_CHARPOS (it))
15882 clear_glyph_matrix (w->desired_matrix);
15883 move_it_by_lines (&it, -1);
15884 try_window (window, it.current.pos, 0);
15886 else
15888 /* Not much we can do about it. */
15892 /* Consider the following case: Window starts at BEGV, there is
15893 invisible, intangible text at BEGV, so that display starts at
15894 some point START > BEGV. It can happen that we are called with
15895 PT somewhere between BEGV and START. Try to handle that case. */
15896 if (w->cursor.vpos < 0)
15898 struct glyph_row *row = w->current_matrix->rows;
15899 if (row->mode_line_p)
15900 ++row;
15901 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15904 if (!cursor_row_fully_visible_p (w, 0, 0))
15906 /* If vscroll is enabled, disable it and try again. */
15907 if (w->vscroll)
15909 w->vscroll = 0;
15910 clear_glyph_matrix (w->desired_matrix);
15911 goto recenter;
15914 /* Users who set scroll-conservatively to a large number want
15915 point just above/below the scroll margin. If we ended up
15916 with point's row partially visible, move the window start to
15917 make that row fully visible and out of the margin. */
15918 if (scroll_conservatively > SCROLL_LIMIT)
15920 int margin =
15921 scroll_margin > 0
15922 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15923 : 0;
15924 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
15926 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
15927 clear_glyph_matrix (w->desired_matrix);
15928 if (1 == try_window (window, it.current.pos,
15929 TRY_WINDOW_CHECK_MARGINS))
15930 goto done;
15933 /* If centering point failed to make the whole line visible,
15934 put point at the top instead. That has to make the whole line
15935 visible, if it can be done. */
15936 if (centering_position == 0)
15937 goto done;
15939 clear_glyph_matrix (w->desired_matrix);
15940 centering_position = 0;
15941 goto recenter;
15944 done:
15946 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15947 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
15948 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
15949 ? Qt : Qnil);
15951 /* Display the mode line, if we must. */
15952 if ((update_mode_line
15953 /* If window not full width, must redo its mode line
15954 if (a) the window to its side is being redone and
15955 (b) we do a frame-based redisplay. This is a consequence
15956 of how inverted lines are drawn in frame-based redisplay. */
15957 || (!just_this_one_p
15958 && !FRAME_WINDOW_P (f)
15959 && !WINDOW_FULL_WIDTH_P (w))
15960 /* Line number to display. */
15961 || INTEGERP (w->base_line_pos)
15962 /* Column number is displayed and different from the one displayed. */
15963 || (!NILP (w->column_number_displayed)
15964 && (XFASTINT (w->column_number_displayed) != current_column ())))
15965 /* This means that the window has a mode line. */
15966 && (WINDOW_WANTS_MODELINE_P (w)
15967 || WINDOW_WANTS_HEADER_LINE_P (w)))
15969 display_mode_lines (w);
15971 /* If mode line height has changed, arrange for a thorough
15972 immediate redisplay using the correct mode line height. */
15973 if (WINDOW_WANTS_MODELINE_P (w)
15974 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
15976 fonts_changed_p = 1;
15977 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
15978 = DESIRED_MODE_LINE_HEIGHT (w);
15981 /* If header line height has changed, arrange for a thorough
15982 immediate redisplay using the correct header line height. */
15983 if (WINDOW_WANTS_HEADER_LINE_P (w)
15984 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
15986 fonts_changed_p = 1;
15987 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
15988 = DESIRED_HEADER_LINE_HEIGHT (w);
15991 if (fonts_changed_p)
15992 goto need_larger_matrices;
15995 if (!line_number_displayed
15996 && !BUFFERP (w->base_line_pos))
15998 w->base_line_pos = Qnil;
15999 w->base_line_number = Qnil;
16002 finish_menu_bars:
16004 /* When we reach a frame's selected window, redo the frame's menu bar. */
16005 if (update_mode_line
16006 && EQ (FRAME_SELECTED_WINDOW (f), window))
16008 int redisplay_menu_p = 0;
16010 if (FRAME_WINDOW_P (f))
16012 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16013 || defined (HAVE_NS) || defined (USE_GTK)
16014 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16015 #else
16016 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16017 #endif
16019 else
16020 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16022 if (redisplay_menu_p)
16023 display_menu_bar (w);
16025 #ifdef HAVE_WINDOW_SYSTEM
16026 if (FRAME_WINDOW_P (f))
16028 #if defined (USE_GTK) || defined (HAVE_NS)
16029 if (FRAME_EXTERNAL_TOOL_BAR (f))
16030 redisplay_tool_bar (f);
16031 #else
16032 if (WINDOWP (f->tool_bar_window)
16033 && (FRAME_TOOL_BAR_LINES (f) > 0
16034 || !NILP (Vauto_resize_tool_bars))
16035 && redisplay_tool_bar (f))
16036 ignore_mouse_drag_p = 1;
16037 #endif
16039 #endif
16042 #ifdef HAVE_WINDOW_SYSTEM
16043 if (FRAME_WINDOW_P (f)
16044 && update_window_fringes (w, (just_this_one_p
16045 || (!used_current_matrix_p && !overlay_arrow_seen)
16046 || w->pseudo_window_p)))
16048 update_begin (f);
16049 BLOCK_INPUT;
16050 if (draw_window_fringes (w, 1))
16051 x_draw_vertical_border (w);
16052 UNBLOCK_INPUT;
16053 update_end (f);
16055 #endif /* HAVE_WINDOW_SYSTEM */
16057 /* We go to this label, with fonts_changed_p nonzero,
16058 if it is necessary to try again using larger glyph matrices.
16059 We have to redeem the scroll bar even in this case,
16060 because the loop in redisplay_internal expects that. */
16061 need_larger_matrices:
16063 finish_scroll_bars:
16065 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16067 /* Set the thumb's position and size. */
16068 set_vertical_scroll_bar (w);
16070 /* Note that we actually used the scroll bar attached to this
16071 window, so it shouldn't be deleted at the end of redisplay. */
16072 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16073 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16076 /* Restore current_buffer and value of point in it. The window
16077 update may have changed the buffer, so first make sure `opoint'
16078 is still valid (Bug#6177). */
16079 if (CHARPOS (opoint) < BEGV)
16080 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16081 else if (CHARPOS (opoint) > ZV)
16082 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16083 else
16084 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16086 set_buffer_internal_1 (old);
16087 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16088 shorter. This can be caused by log truncation in *Messages*. */
16089 if (CHARPOS (lpoint) <= ZV)
16090 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16092 unbind_to (count, Qnil);
16096 /* Build the complete desired matrix of WINDOW with a window start
16097 buffer position POS.
16099 Value is 1 if successful. It is zero if fonts were loaded during
16100 redisplay which makes re-adjusting glyph matrices necessary, and -1
16101 if point would appear in the scroll margins.
16102 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16103 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16104 set in FLAGS.) */
16107 try_window (Lisp_Object window, struct text_pos pos, int flags)
16109 struct window *w = XWINDOW (window);
16110 struct it it;
16111 struct glyph_row *last_text_row = NULL;
16112 struct frame *f = XFRAME (w->frame);
16114 /* Make POS the new window start. */
16115 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16117 /* Mark cursor position as unknown. No overlay arrow seen. */
16118 w->cursor.vpos = -1;
16119 overlay_arrow_seen = 0;
16121 /* Initialize iterator and info to start at POS. */
16122 start_display (&it, w, pos);
16124 /* Display all lines of W. */
16125 while (it.current_y < it.last_visible_y)
16127 if (display_line (&it))
16128 last_text_row = it.glyph_row - 1;
16129 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16130 return 0;
16133 /* Don't let the cursor end in the scroll margins. */
16134 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16135 && !MINI_WINDOW_P (w))
16137 int this_scroll_margin;
16139 if (scroll_margin > 0)
16141 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16142 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16144 else
16145 this_scroll_margin = 0;
16147 if ((w->cursor.y >= 0 /* not vscrolled */
16148 && w->cursor.y < this_scroll_margin
16149 && CHARPOS (pos) > BEGV
16150 && IT_CHARPOS (it) < ZV)
16151 /* rms: considering make_cursor_line_fully_visible_p here
16152 seems to give wrong results. We don't want to recenter
16153 when the last line is partly visible, we want to allow
16154 that case to be handled in the usual way. */
16155 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16157 w->cursor.vpos = -1;
16158 clear_glyph_matrix (w->desired_matrix);
16159 return -1;
16163 /* If bottom moved off end of frame, change mode line percentage. */
16164 if (XFASTINT (w->window_end_pos) <= 0
16165 && Z != IT_CHARPOS (it))
16166 w->update_mode_line = Qt;
16168 /* Set window_end_pos to the offset of the last character displayed
16169 on the window from the end of current_buffer. Set
16170 window_end_vpos to its row number. */
16171 if (last_text_row)
16173 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16174 w->window_end_bytepos
16175 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16176 w->window_end_pos
16177 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16178 w->window_end_vpos
16179 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16180 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
16181 ->displays_text_p);
16183 else
16185 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16186 w->window_end_pos = make_number (Z - ZV);
16187 w->window_end_vpos = make_number (0);
16190 /* But that is not valid info until redisplay finishes. */
16191 w->window_end_valid = Qnil;
16192 return 1;
16197 /************************************************************************
16198 Window redisplay reusing current matrix when buffer has not changed
16199 ************************************************************************/
16201 /* Try redisplay of window W showing an unchanged buffer with a
16202 different window start than the last time it was displayed by
16203 reusing its current matrix. Value is non-zero if successful.
16204 W->start is the new window start. */
16206 static int
16207 try_window_reusing_current_matrix (struct window *w)
16209 struct frame *f = XFRAME (w->frame);
16210 struct glyph_row *bottom_row;
16211 struct it it;
16212 struct run run;
16213 struct text_pos start, new_start;
16214 int nrows_scrolled, i;
16215 struct glyph_row *last_text_row;
16216 struct glyph_row *last_reused_text_row;
16217 struct glyph_row *start_row;
16218 int start_vpos, min_y, max_y;
16220 #if GLYPH_DEBUG
16221 if (inhibit_try_window_reusing)
16222 return 0;
16223 #endif
16225 if (/* This function doesn't handle terminal frames. */
16226 !FRAME_WINDOW_P (f)
16227 /* Don't try to reuse the display if windows have been split
16228 or such. */
16229 || windows_or_buffers_changed
16230 || cursor_type_changed)
16231 return 0;
16233 /* Can't do this if region may have changed. */
16234 if ((!NILP (Vtransient_mark_mode)
16235 && !NILP (BVAR (current_buffer, mark_active)))
16236 || !NILP (w->region_showing)
16237 || !NILP (Vshow_trailing_whitespace))
16238 return 0;
16240 /* If top-line visibility has changed, give up. */
16241 if (WINDOW_WANTS_HEADER_LINE_P (w)
16242 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16243 return 0;
16245 /* Give up if old or new display is scrolled vertically. We could
16246 make this function handle this, but right now it doesn't. */
16247 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16248 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16249 return 0;
16251 /* The variable new_start now holds the new window start. The old
16252 start `start' can be determined from the current matrix. */
16253 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16254 start = start_row->minpos;
16255 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16257 /* Clear the desired matrix for the display below. */
16258 clear_glyph_matrix (w->desired_matrix);
16260 if (CHARPOS (new_start) <= CHARPOS (start))
16262 /* Don't use this method if the display starts with an ellipsis
16263 displayed for invisible text. It's not easy to handle that case
16264 below, and it's certainly not worth the effort since this is
16265 not a frequent case. */
16266 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16267 return 0;
16269 IF_DEBUG (debug_method_add (w, "twu1"));
16271 /* Display up to a row that can be reused. The variable
16272 last_text_row is set to the last row displayed that displays
16273 text. Note that it.vpos == 0 if or if not there is a
16274 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16275 start_display (&it, w, new_start);
16276 w->cursor.vpos = -1;
16277 last_text_row = last_reused_text_row = NULL;
16279 while (it.current_y < it.last_visible_y
16280 && !fonts_changed_p)
16282 /* If we have reached into the characters in the START row,
16283 that means the line boundaries have changed. So we
16284 can't start copying with the row START. Maybe it will
16285 work to start copying with the following row. */
16286 while (IT_CHARPOS (it) > CHARPOS (start))
16288 /* Advance to the next row as the "start". */
16289 start_row++;
16290 start = start_row->minpos;
16291 /* If there are no more rows to try, or just one, give up. */
16292 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16293 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16294 || CHARPOS (start) == ZV)
16296 clear_glyph_matrix (w->desired_matrix);
16297 return 0;
16300 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16302 /* If we have reached alignment, we can copy the rest of the
16303 rows. */
16304 if (IT_CHARPOS (it) == CHARPOS (start)
16305 /* Don't accept "alignment" inside a display vector,
16306 since start_row could have started in the middle of
16307 that same display vector (thus their character
16308 positions match), and we have no way of telling if
16309 that is the case. */
16310 && it.current.dpvec_index < 0)
16311 break;
16313 if (display_line (&it))
16314 last_text_row = it.glyph_row - 1;
16318 /* A value of current_y < last_visible_y means that we stopped
16319 at the previous window start, which in turn means that we
16320 have at least one reusable row. */
16321 if (it.current_y < it.last_visible_y)
16323 struct glyph_row *row;
16325 /* IT.vpos always starts from 0; it counts text lines. */
16326 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16328 /* Find PT if not already found in the lines displayed. */
16329 if (w->cursor.vpos < 0)
16331 int dy = it.current_y - start_row->y;
16333 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16334 row = row_containing_pos (w, PT, row, NULL, dy);
16335 if (row)
16336 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16337 dy, nrows_scrolled);
16338 else
16340 clear_glyph_matrix (w->desired_matrix);
16341 return 0;
16345 /* Scroll the display. Do it before the current matrix is
16346 changed. The problem here is that update has not yet
16347 run, i.e. part of the current matrix is not up to date.
16348 scroll_run_hook will clear the cursor, and use the
16349 current matrix to get the height of the row the cursor is
16350 in. */
16351 run.current_y = start_row->y;
16352 run.desired_y = it.current_y;
16353 run.height = it.last_visible_y - it.current_y;
16355 if (run.height > 0 && run.current_y != run.desired_y)
16357 update_begin (f);
16358 FRAME_RIF (f)->update_window_begin_hook (w);
16359 FRAME_RIF (f)->clear_window_mouse_face (w);
16360 FRAME_RIF (f)->scroll_run_hook (w, &run);
16361 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16362 update_end (f);
16365 /* Shift current matrix down by nrows_scrolled lines. */
16366 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16367 rotate_matrix (w->current_matrix,
16368 start_vpos,
16369 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16370 nrows_scrolled);
16372 /* Disable lines that must be updated. */
16373 for (i = 0; i < nrows_scrolled; ++i)
16374 (start_row + i)->enabled_p = 0;
16376 /* Re-compute Y positions. */
16377 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16378 max_y = it.last_visible_y;
16379 for (row = start_row + nrows_scrolled;
16380 row < bottom_row;
16381 ++row)
16383 row->y = it.current_y;
16384 row->visible_height = row->height;
16386 if (row->y < min_y)
16387 row->visible_height -= min_y - row->y;
16388 if (row->y + row->height > max_y)
16389 row->visible_height -= row->y + row->height - max_y;
16390 if (row->fringe_bitmap_periodic_p)
16391 row->redraw_fringe_bitmaps_p = 1;
16393 it.current_y += row->height;
16395 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16396 last_reused_text_row = row;
16397 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16398 break;
16401 /* Disable lines in the current matrix which are now
16402 below the window. */
16403 for (++row; row < bottom_row; ++row)
16404 row->enabled_p = row->mode_line_p = 0;
16407 /* Update window_end_pos etc.; last_reused_text_row is the last
16408 reused row from the current matrix containing text, if any.
16409 The value of last_text_row is the last displayed line
16410 containing text. */
16411 if (last_reused_text_row)
16413 w->window_end_bytepos
16414 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16415 w->window_end_pos
16416 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
16417 w->window_end_vpos
16418 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16419 w->current_matrix));
16421 else if (last_text_row)
16423 w->window_end_bytepos
16424 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16425 w->window_end_pos
16426 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16427 w->window_end_vpos
16428 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16430 else
16432 /* This window must be completely empty. */
16433 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16434 w->window_end_pos = make_number (Z - ZV);
16435 w->window_end_vpos = make_number (0);
16437 w->window_end_valid = Qnil;
16439 /* Update hint: don't try scrolling again in update_window. */
16440 w->desired_matrix->no_scrolling_p = 1;
16442 #if GLYPH_DEBUG
16443 debug_method_add (w, "try_window_reusing_current_matrix 1");
16444 #endif
16445 return 1;
16447 else if (CHARPOS (new_start) > CHARPOS (start))
16449 struct glyph_row *pt_row, *row;
16450 struct glyph_row *first_reusable_row;
16451 struct glyph_row *first_row_to_display;
16452 int dy;
16453 int yb = window_text_bottom_y (w);
16455 /* Find the row starting at new_start, if there is one. Don't
16456 reuse a partially visible line at the end. */
16457 first_reusable_row = start_row;
16458 while (first_reusable_row->enabled_p
16459 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16460 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16461 < CHARPOS (new_start)))
16462 ++first_reusable_row;
16464 /* Give up if there is no row to reuse. */
16465 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16466 || !first_reusable_row->enabled_p
16467 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16468 != CHARPOS (new_start)))
16469 return 0;
16471 /* We can reuse fully visible rows beginning with
16472 first_reusable_row to the end of the window. Set
16473 first_row_to_display to the first row that cannot be reused.
16474 Set pt_row to the row containing point, if there is any. */
16475 pt_row = NULL;
16476 for (first_row_to_display = first_reusable_row;
16477 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16478 ++first_row_to_display)
16480 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16481 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16482 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16483 && first_row_to_display->ends_at_zv_p
16484 && pt_row == NULL)))
16485 pt_row = first_row_to_display;
16488 /* Start displaying at the start of first_row_to_display. */
16489 xassert (first_row_to_display->y < yb);
16490 init_to_row_start (&it, w, first_row_to_display);
16492 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16493 - start_vpos);
16494 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16495 - nrows_scrolled);
16496 it.current_y = (first_row_to_display->y - first_reusable_row->y
16497 + WINDOW_HEADER_LINE_HEIGHT (w));
16499 /* Display lines beginning with first_row_to_display in the
16500 desired matrix. Set last_text_row to the last row displayed
16501 that displays text. */
16502 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16503 if (pt_row == NULL)
16504 w->cursor.vpos = -1;
16505 last_text_row = NULL;
16506 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16507 if (display_line (&it))
16508 last_text_row = it.glyph_row - 1;
16510 /* If point is in a reused row, adjust y and vpos of the cursor
16511 position. */
16512 if (pt_row)
16514 w->cursor.vpos -= nrows_scrolled;
16515 w->cursor.y -= first_reusable_row->y - start_row->y;
16518 /* Give up if point isn't in a row displayed or reused. (This
16519 also handles the case where w->cursor.vpos < nrows_scrolled
16520 after the calls to display_line, which can happen with scroll
16521 margins. See bug#1295.) */
16522 if (w->cursor.vpos < 0)
16524 clear_glyph_matrix (w->desired_matrix);
16525 return 0;
16528 /* Scroll the display. */
16529 run.current_y = first_reusable_row->y;
16530 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16531 run.height = it.last_visible_y - run.current_y;
16532 dy = run.current_y - run.desired_y;
16534 if (run.height)
16536 update_begin (f);
16537 FRAME_RIF (f)->update_window_begin_hook (w);
16538 FRAME_RIF (f)->clear_window_mouse_face (w);
16539 FRAME_RIF (f)->scroll_run_hook (w, &run);
16540 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16541 update_end (f);
16544 /* Adjust Y positions of reused rows. */
16545 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16546 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16547 max_y = it.last_visible_y;
16548 for (row = first_reusable_row; row < first_row_to_display; ++row)
16550 row->y -= dy;
16551 row->visible_height = row->height;
16552 if (row->y < min_y)
16553 row->visible_height -= min_y - row->y;
16554 if (row->y + row->height > max_y)
16555 row->visible_height -= row->y + row->height - max_y;
16556 if (row->fringe_bitmap_periodic_p)
16557 row->redraw_fringe_bitmaps_p = 1;
16560 /* Scroll the current matrix. */
16561 xassert (nrows_scrolled > 0);
16562 rotate_matrix (w->current_matrix,
16563 start_vpos,
16564 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16565 -nrows_scrolled);
16567 /* Disable rows not reused. */
16568 for (row -= nrows_scrolled; row < bottom_row; ++row)
16569 row->enabled_p = 0;
16571 /* Point may have moved to a different line, so we cannot assume that
16572 the previous cursor position is valid; locate the correct row. */
16573 if (pt_row)
16575 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16576 row < bottom_row
16577 && PT >= MATRIX_ROW_END_CHARPOS (row)
16578 && !row->ends_at_zv_p;
16579 row++)
16581 w->cursor.vpos++;
16582 w->cursor.y = row->y;
16584 if (row < bottom_row)
16586 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16587 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16589 /* Can't use this optimization with bidi-reordered glyph
16590 rows, unless cursor is already at point. */
16591 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16593 if (!(w->cursor.hpos >= 0
16594 && w->cursor.hpos < row->used[TEXT_AREA]
16595 && BUFFERP (glyph->object)
16596 && glyph->charpos == PT))
16597 return 0;
16599 else
16600 for (; glyph < end
16601 && (!BUFFERP (glyph->object)
16602 || glyph->charpos < PT);
16603 glyph++)
16605 w->cursor.hpos++;
16606 w->cursor.x += glyph->pixel_width;
16611 /* Adjust window end. A null value of last_text_row means that
16612 the window end is in reused rows which in turn means that
16613 only its vpos can have changed. */
16614 if (last_text_row)
16616 w->window_end_bytepos
16617 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16618 w->window_end_pos
16619 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16620 w->window_end_vpos
16621 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16623 else
16625 w->window_end_vpos
16626 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16629 w->window_end_valid = Qnil;
16630 w->desired_matrix->no_scrolling_p = 1;
16632 #if GLYPH_DEBUG
16633 debug_method_add (w, "try_window_reusing_current_matrix 2");
16634 #endif
16635 return 1;
16638 return 0;
16643 /************************************************************************
16644 Window redisplay reusing current matrix when buffer has changed
16645 ************************************************************************/
16647 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16648 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16649 EMACS_INT *, EMACS_INT *);
16650 static struct glyph_row *
16651 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16652 struct glyph_row *);
16655 /* Return the last row in MATRIX displaying text. If row START is
16656 non-null, start searching with that row. IT gives the dimensions
16657 of the display. Value is null if matrix is empty; otherwise it is
16658 a pointer to the row found. */
16660 static struct glyph_row *
16661 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16662 struct glyph_row *start)
16664 struct glyph_row *row, *row_found;
16666 /* Set row_found to the last row in IT->w's current matrix
16667 displaying text. The loop looks funny but think of partially
16668 visible lines. */
16669 row_found = NULL;
16670 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16671 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16673 xassert (row->enabled_p);
16674 row_found = row;
16675 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16676 break;
16677 ++row;
16680 return row_found;
16684 /* Return the last row in the current matrix of W that is not affected
16685 by changes at the start of current_buffer that occurred since W's
16686 current matrix was built. Value is null if no such row exists.
16688 BEG_UNCHANGED us the number of characters unchanged at the start of
16689 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16690 first changed character in current_buffer. Characters at positions <
16691 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16692 when the current matrix was built. */
16694 static struct glyph_row *
16695 find_last_unchanged_at_beg_row (struct window *w)
16697 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
16698 struct glyph_row *row;
16699 struct glyph_row *row_found = NULL;
16700 int yb = window_text_bottom_y (w);
16702 /* Find the last row displaying unchanged text. */
16703 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16704 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16705 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16706 ++row)
16708 if (/* If row ends before first_changed_pos, it is unchanged,
16709 except in some case. */
16710 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16711 /* When row ends in ZV and we write at ZV it is not
16712 unchanged. */
16713 && !row->ends_at_zv_p
16714 /* When first_changed_pos is the end of a continued line,
16715 row is not unchanged because it may be no longer
16716 continued. */
16717 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16718 && (row->continued_p
16719 || row->exact_window_width_line_p))
16720 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16721 needs to be recomputed, so don't consider this row as
16722 unchanged. This happens when the last line was
16723 bidi-reordered and was killed immediately before this
16724 redisplay cycle. In that case, ROW->end stores the
16725 buffer position of the first visual-order character of
16726 the killed text, which is now beyond ZV. */
16727 && CHARPOS (row->end.pos) <= ZV)
16728 row_found = row;
16730 /* Stop if last visible row. */
16731 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16732 break;
16735 return row_found;
16739 /* Find the first glyph row in the current matrix of W that is not
16740 affected by changes at the end of current_buffer since the
16741 time W's current matrix was built.
16743 Return in *DELTA the number of chars by which buffer positions in
16744 unchanged text at the end of current_buffer must be adjusted.
16746 Return in *DELTA_BYTES the corresponding number of bytes.
16748 Value is null if no such row exists, i.e. all rows are affected by
16749 changes. */
16751 static struct glyph_row *
16752 find_first_unchanged_at_end_row (struct window *w,
16753 EMACS_INT *delta, EMACS_INT *delta_bytes)
16755 struct glyph_row *row;
16756 struct glyph_row *row_found = NULL;
16758 *delta = *delta_bytes = 0;
16760 /* Display must not have been paused, otherwise the current matrix
16761 is not up to date. */
16762 eassert (!NILP (w->window_end_valid));
16764 /* A value of window_end_pos >= END_UNCHANGED means that the window
16765 end is in the range of changed text. If so, there is no
16766 unchanged row at the end of W's current matrix. */
16767 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16768 return NULL;
16770 /* Set row to the last row in W's current matrix displaying text. */
16771 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16773 /* If matrix is entirely empty, no unchanged row exists. */
16774 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16776 /* The value of row is the last glyph row in the matrix having a
16777 meaningful buffer position in it. The end position of row
16778 corresponds to window_end_pos. This allows us to translate
16779 buffer positions in the current matrix to current buffer
16780 positions for characters not in changed text. */
16781 EMACS_INT Z_old =
16782 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16783 EMACS_INT Z_BYTE_old =
16784 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16785 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
16786 struct glyph_row *first_text_row
16787 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16789 *delta = Z - Z_old;
16790 *delta_bytes = Z_BYTE - Z_BYTE_old;
16792 /* Set last_unchanged_pos to the buffer position of the last
16793 character in the buffer that has not been changed. Z is the
16794 index + 1 of the last character in current_buffer, i.e. by
16795 subtracting END_UNCHANGED we get the index of the last
16796 unchanged character, and we have to add BEG to get its buffer
16797 position. */
16798 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16799 last_unchanged_pos_old = last_unchanged_pos - *delta;
16801 /* Search backward from ROW for a row displaying a line that
16802 starts at a minimum position >= last_unchanged_pos_old. */
16803 for (; row > first_text_row; --row)
16805 /* This used to abort, but it can happen.
16806 It is ok to just stop the search instead here. KFS. */
16807 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16808 break;
16810 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16811 row_found = row;
16815 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16817 return row_found;
16821 /* Make sure that glyph rows in the current matrix of window W
16822 reference the same glyph memory as corresponding rows in the
16823 frame's frame matrix. This function is called after scrolling W's
16824 current matrix on a terminal frame in try_window_id and
16825 try_window_reusing_current_matrix. */
16827 static void
16828 sync_frame_with_window_matrix_rows (struct window *w)
16830 struct frame *f = XFRAME (w->frame);
16831 struct glyph_row *window_row, *window_row_end, *frame_row;
16833 /* Preconditions: W must be a leaf window and full-width. Its frame
16834 must have a frame matrix. */
16835 xassert (NILP (w->hchild) && NILP (w->vchild));
16836 xassert (WINDOW_FULL_WIDTH_P (w));
16837 xassert (!FRAME_WINDOW_P (f));
16839 /* If W is a full-width window, glyph pointers in W's current matrix
16840 have, by definition, to be the same as glyph pointers in the
16841 corresponding frame matrix. Note that frame matrices have no
16842 marginal areas (see build_frame_matrix). */
16843 window_row = w->current_matrix->rows;
16844 window_row_end = window_row + w->current_matrix->nrows;
16845 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16846 while (window_row < window_row_end)
16848 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16849 struct glyph *end = window_row->glyphs[LAST_AREA];
16851 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16852 frame_row->glyphs[TEXT_AREA] = start;
16853 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16854 frame_row->glyphs[LAST_AREA] = end;
16856 /* Disable frame rows whose corresponding window rows have
16857 been disabled in try_window_id. */
16858 if (!window_row->enabled_p)
16859 frame_row->enabled_p = 0;
16861 ++window_row, ++frame_row;
16866 /* Find the glyph row in window W containing CHARPOS. Consider all
16867 rows between START and END (not inclusive). END null means search
16868 all rows to the end of the display area of W. Value is the row
16869 containing CHARPOS or null. */
16871 struct glyph_row *
16872 row_containing_pos (struct window *w, EMACS_INT charpos,
16873 struct glyph_row *start, struct glyph_row *end, int dy)
16875 struct glyph_row *row = start;
16876 struct glyph_row *best_row = NULL;
16877 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16878 int last_y;
16880 /* If we happen to start on a header-line, skip that. */
16881 if (row->mode_line_p)
16882 ++row;
16884 if ((end && row >= end) || !row->enabled_p)
16885 return NULL;
16887 last_y = window_text_bottom_y (w) - dy;
16889 while (1)
16891 /* Give up if we have gone too far. */
16892 if (end && row >= end)
16893 return NULL;
16894 /* This formerly returned if they were equal.
16895 I think that both quantities are of a "last plus one" type;
16896 if so, when they are equal, the row is within the screen. -- rms. */
16897 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16898 return NULL;
16900 /* If it is in this row, return this row. */
16901 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16902 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16903 /* The end position of a row equals the start
16904 position of the next row. If CHARPOS is there, we
16905 would rather display it in the next line, except
16906 when this line ends in ZV. */
16907 && !row->ends_at_zv_p
16908 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16909 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16911 struct glyph *g;
16913 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16914 || (!best_row && !row->continued_p))
16915 return row;
16916 /* In bidi-reordered rows, there could be several rows
16917 occluding point, all of them belonging to the same
16918 continued line. We need to find the row which fits
16919 CHARPOS the best. */
16920 for (g = row->glyphs[TEXT_AREA];
16921 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16922 g++)
16924 if (!STRINGP (g->object))
16926 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16928 mindif = eabs (g->charpos - charpos);
16929 best_row = row;
16930 /* Exact match always wins. */
16931 if (mindif == 0)
16932 return best_row;
16937 else if (best_row && !row->continued_p)
16938 return best_row;
16939 ++row;
16944 /* Try to redisplay window W by reusing its existing display. W's
16945 current matrix must be up to date when this function is called,
16946 i.e. window_end_valid must not be nil.
16948 Value is
16950 1 if display has been updated
16951 0 if otherwise unsuccessful
16952 -1 if redisplay with same window start is known not to succeed
16954 The following steps are performed:
16956 1. Find the last row in the current matrix of W that is not
16957 affected by changes at the start of current_buffer. If no such row
16958 is found, give up.
16960 2. Find the first row in W's current matrix that is not affected by
16961 changes at the end of current_buffer. Maybe there is no such row.
16963 3. Display lines beginning with the row + 1 found in step 1 to the
16964 row found in step 2 or, if step 2 didn't find a row, to the end of
16965 the window.
16967 4. If cursor is not known to appear on the window, give up.
16969 5. If display stopped at the row found in step 2, scroll the
16970 display and current matrix as needed.
16972 6. Maybe display some lines at the end of W, if we must. This can
16973 happen under various circumstances, like a partially visible line
16974 becoming fully visible, or because newly displayed lines are displayed
16975 in smaller font sizes.
16977 7. Update W's window end information. */
16979 static int
16980 try_window_id (struct window *w)
16982 struct frame *f = XFRAME (w->frame);
16983 struct glyph_matrix *current_matrix = w->current_matrix;
16984 struct glyph_matrix *desired_matrix = w->desired_matrix;
16985 struct glyph_row *last_unchanged_at_beg_row;
16986 struct glyph_row *first_unchanged_at_end_row;
16987 struct glyph_row *row;
16988 struct glyph_row *bottom_row;
16989 int bottom_vpos;
16990 struct it it;
16991 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
16992 int dvpos, dy;
16993 struct text_pos start_pos;
16994 struct run run;
16995 int first_unchanged_at_end_vpos = 0;
16996 struct glyph_row *last_text_row, *last_text_row_at_end;
16997 struct text_pos start;
16998 EMACS_INT first_changed_charpos, last_changed_charpos;
17000 #if GLYPH_DEBUG
17001 if (inhibit_try_window_id)
17002 return 0;
17003 #endif
17005 /* This is handy for debugging. */
17006 #if 0
17007 #define GIVE_UP(X) \
17008 do { \
17009 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17010 return 0; \
17011 } while (0)
17012 #else
17013 #define GIVE_UP(X) return 0
17014 #endif
17016 SET_TEXT_POS_FROM_MARKER (start, w->start);
17018 /* Don't use this for mini-windows because these can show
17019 messages and mini-buffers, and we don't handle that here. */
17020 if (MINI_WINDOW_P (w))
17021 GIVE_UP (1);
17023 /* This flag is used to prevent redisplay optimizations. */
17024 if (windows_or_buffers_changed || cursor_type_changed)
17025 GIVE_UP (2);
17027 /* Verify that narrowing has not changed.
17028 Also verify that we were not told to prevent redisplay optimizations.
17029 It would be nice to further
17030 reduce the number of cases where this prevents try_window_id. */
17031 if (current_buffer->clip_changed
17032 || current_buffer->prevent_redisplay_optimizations_p)
17033 GIVE_UP (3);
17035 /* Window must either use window-based redisplay or be full width. */
17036 if (!FRAME_WINDOW_P (f)
17037 && (!FRAME_LINE_INS_DEL_OK (f)
17038 || !WINDOW_FULL_WIDTH_P (w)))
17039 GIVE_UP (4);
17041 /* Give up if point is known NOT to appear in W. */
17042 if (PT < CHARPOS (start))
17043 GIVE_UP (5);
17045 /* Another way to prevent redisplay optimizations. */
17046 if (XFASTINT (w->last_modified) == 0)
17047 GIVE_UP (6);
17049 /* Verify that window is not hscrolled. */
17050 if (XFASTINT (w->hscroll) != 0)
17051 GIVE_UP (7);
17053 /* Verify that display wasn't paused. */
17054 if (NILP (w->window_end_valid))
17055 GIVE_UP (8);
17057 /* Can't use this if highlighting a region because a cursor movement
17058 will do more than just set the cursor. */
17059 if (!NILP (Vtransient_mark_mode)
17060 && !NILP (BVAR (current_buffer, mark_active)))
17061 GIVE_UP (9);
17063 /* Likewise if highlighting trailing whitespace. */
17064 if (!NILP (Vshow_trailing_whitespace))
17065 GIVE_UP (11);
17067 /* Likewise if showing a region. */
17068 if (!NILP (w->region_showing))
17069 GIVE_UP (10);
17071 /* Can't use this if overlay arrow position and/or string have
17072 changed. */
17073 if (overlay_arrows_changed_p ())
17074 GIVE_UP (12);
17076 /* When word-wrap is on, adding a space to the first word of a
17077 wrapped line can change the wrap position, altering the line
17078 above it. It might be worthwhile to handle this more
17079 intelligently, but for now just redisplay from scratch. */
17080 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17081 GIVE_UP (21);
17083 /* Under bidi reordering, adding or deleting a character in the
17084 beginning of a paragraph, before the first strong directional
17085 character, can change the base direction of the paragraph (unless
17086 the buffer specifies a fixed paragraph direction), which will
17087 require to redisplay the whole paragraph. It might be worthwhile
17088 to find the paragraph limits and widen the range of redisplayed
17089 lines to that, but for now just give up this optimization and
17090 redisplay from scratch. */
17091 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17092 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17093 GIVE_UP (22);
17095 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17096 only if buffer has really changed. The reason is that the gap is
17097 initially at Z for freshly visited files. The code below would
17098 set end_unchanged to 0 in that case. */
17099 if (MODIFF > SAVE_MODIFF
17100 /* This seems to happen sometimes after saving a buffer. */
17101 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17103 if (GPT - BEG < BEG_UNCHANGED)
17104 BEG_UNCHANGED = GPT - BEG;
17105 if (Z - GPT < END_UNCHANGED)
17106 END_UNCHANGED = Z - GPT;
17109 /* The position of the first and last character that has been changed. */
17110 first_changed_charpos = BEG + BEG_UNCHANGED;
17111 last_changed_charpos = Z - END_UNCHANGED;
17113 /* If window starts after a line end, and the last change is in
17114 front of that newline, then changes don't affect the display.
17115 This case happens with stealth-fontification. Note that although
17116 the display is unchanged, glyph positions in the matrix have to
17117 be adjusted, of course. */
17118 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17119 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17120 && ((last_changed_charpos < CHARPOS (start)
17121 && CHARPOS (start) == BEGV)
17122 || (last_changed_charpos < CHARPOS (start) - 1
17123 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17125 EMACS_INT Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17126 struct glyph_row *r0;
17128 /* Compute how many chars/bytes have been added to or removed
17129 from the buffer. */
17130 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17131 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17132 Z_delta = Z - Z_old;
17133 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17135 /* Give up if PT is not in the window. Note that it already has
17136 been checked at the start of try_window_id that PT is not in
17137 front of the window start. */
17138 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17139 GIVE_UP (13);
17141 /* If window start is unchanged, we can reuse the whole matrix
17142 as is, after adjusting glyph positions. No need to compute
17143 the window end again, since its offset from Z hasn't changed. */
17144 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17145 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17146 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17147 /* PT must not be in a partially visible line. */
17148 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17149 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17151 /* Adjust positions in the glyph matrix. */
17152 if (Z_delta || Z_delta_bytes)
17154 struct glyph_row *r1
17155 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17156 increment_matrix_positions (w->current_matrix,
17157 MATRIX_ROW_VPOS (r0, current_matrix),
17158 MATRIX_ROW_VPOS (r1, current_matrix),
17159 Z_delta, Z_delta_bytes);
17162 /* Set the cursor. */
17163 row = row_containing_pos (w, PT, r0, NULL, 0);
17164 if (row)
17165 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17166 else
17167 abort ();
17168 return 1;
17172 /* Handle the case that changes are all below what is displayed in
17173 the window, and that PT is in the window. This shortcut cannot
17174 be taken if ZV is visible in the window, and text has been added
17175 there that is visible in the window. */
17176 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17177 /* ZV is not visible in the window, or there are no
17178 changes at ZV, actually. */
17179 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17180 || first_changed_charpos == last_changed_charpos))
17182 struct glyph_row *r0;
17184 /* Give up if PT is not in the window. Note that it already has
17185 been checked at the start of try_window_id that PT is not in
17186 front of the window start. */
17187 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17188 GIVE_UP (14);
17190 /* If window start is unchanged, we can reuse the whole matrix
17191 as is, without changing glyph positions since no text has
17192 been added/removed in front of the window end. */
17193 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17194 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17195 /* PT must not be in a partially visible line. */
17196 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17197 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17199 /* We have to compute the window end anew since text
17200 could have been added/removed after it. */
17201 w->window_end_pos
17202 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17203 w->window_end_bytepos
17204 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17206 /* Set the cursor. */
17207 row = row_containing_pos (w, PT, r0, NULL, 0);
17208 if (row)
17209 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17210 else
17211 abort ();
17212 return 2;
17216 /* Give up if window start is in the changed area.
17218 The condition used to read
17220 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17222 but why that was tested escapes me at the moment. */
17223 if (CHARPOS (start) >= first_changed_charpos
17224 && CHARPOS (start) <= last_changed_charpos)
17225 GIVE_UP (15);
17227 /* Check that window start agrees with the start of the first glyph
17228 row in its current matrix. Check this after we know the window
17229 start is not in changed text, otherwise positions would not be
17230 comparable. */
17231 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17232 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17233 GIVE_UP (16);
17235 /* Give up if the window ends in strings. Overlay strings
17236 at the end are difficult to handle, so don't try. */
17237 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17238 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17239 GIVE_UP (20);
17241 /* Compute the position at which we have to start displaying new
17242 lines. Some of the lines at the top of the window might be
17243 reusable because they are not displaying changed text. Find the
17244 last row in W's current matrix not affected by changes at the
17245 start of current_buffer. Value is null if changes start in the
17246 first line of window. */
17247 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17248 if (last_unchanged_at_beg_row)
17250 /* Avoid starting to display in the middle of a character, a TAB
17251 for instance. This is easier than to set up the iterator
17252 exactly, and it's not a frequent case, so the additional
17253 effort wouldn't really pay off. */
17254 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17255 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17256 && last_unchanged_at_beg_row > w->current_matrix->rows)
17257 --last_unchanged_at_beg_row;
17259 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17260 GIVE_UP (17);
17262 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17263 GIVE_UP (18);
17264 start_pos = it.current.pos;
17266 /* Start displaying new lines in the desired matrix at the same
17267 vpos we would use in the current matrix, i.e. below
17268 last_unchanged_at_beg_row. */
17269 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17270 current_matrix);
17271 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17272 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17274 xassert (it.hpos == 0 && it.current_x == 0);
17276 else
17278 /* There are no reusable lines at the start of the window.
17279 Start displaying in the first text line. */
17280 start_display (&it, w, start);
17281 it.vpos = it.first_vpos;
17282 start_pos = it.current.pos;
17285 /* Find the first row that is not affected by changes at the end of
17286 the buffer. Value will be null if there is no unchanged row, in
17287 which case we must redisplay to the end of the window. delta
17288 will be set to the value by which buffer positions beginning with
17289 first_unchanged_at_end_row have to be adjusted due to text
17290 changes. */
17291 first_unchanged_at_end_row
17292 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17293 IF_DEBUG (debug_delta = delta);
17294 IF_DEBUG (debug_delta_bytes = delta_bytes);
17296 /* Set stop_pos to the buffer position up to which we will have to
17297 display new lines. If first_unchanged_at_end_row != NULL, this
17298 is the buffer position of the start of the line displayed in that
17299 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17300 that we don't stop at a buffer position. */
17301 stop_pos = 0;
17302 if (first_unchanged_at_end_row)
17304 xassert (last_unchanged_at_beg_row == NULL
17305 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17307 /* If this is a continuation line, move forward to the next one
17308 that isn't. Changes in lines above affect this line.
17309 Caution: this may move first_unchanged_at_end_row to a row
17310 not displaying text. */
17311 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17312 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17313 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17314 < it.last_visible_y))
17315 ++first_unchanged_at_end_row;
17317 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17318 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17319 >= it.last_visible_y))
17320 first_unchanged_at_end_row = NULL;
17321 else
17323 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17324 + delta);
17325 first_unchanged_at_end_vpos
17326 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17327 xassert (stop_pos >= Z - END_UNCHANGED);
17330 else if (last_unchanged_at_beg_row == NULL)
17331 GIVE_UP (19);
17334 #if GLYPH_DEBUG
17336 /* Either there is no unchanged row at the end, or the one we have
17337 now displays text. This is a necessary condition for the window
17338 end pos calculation at the end of this function. */
17339 xassert (first_unchanged_at_end_row == NULL
17340 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17342 debug_last_unchanged_at_beg_vpos
17343 = (last_unchanged_at_beg_row
17344 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17345 : -1);
17346 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17348 #endif /* GLYPH_DEBUG != 0 */
17351 /* Display new lines. Set last_text_row to the last new line
17352 displayed which has text on it, i.e. might end up as being the
17353 line where the window_end_vpos is. */
17354 w->cursor.vpos = -1;
17355 last_text_row = NULL;
17356 overlay_arrow_seen = 0;
17357 while (it.current_y < it.last_visible_y
17358 && !fonts_changed_p
17359 && (first_unchanged_at_end_row == NULL
17360 || IT_CHARPOS (it) < stop_pos))
17362 if (display_line (&it))
17363 last_text_row = it.glyph_row - 1;
17366 if (fonts_changed_p)
17367 return -1;
17370 /* Compute differences in buffer positions, y-positions etc. for
17371 lines reused at the bottom of the window. Compute what we can
17372 scroll. */
17373 if (first_unchanged_at_end_row
17374 /* No lines reused because we displayed everything up to the
17375 bottom of the window. */
17376 && it.current_y < it.last_visible_y)
17378 dvpos = (it.vpos
17379 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17380 current_matrix));
17381 dy = it.current_y - first_unchanged_at_end_row->y;
17382 run.current_y = first_unchanged_at_end_row->y;
17383 run.desired_y = run.current_y + dy;
17384 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17386 else
17388 delta = delta_bytes = dvpos = dy
17389 = run.current_y = run.desired_y = run.height = 0;
17390 first_unchanged_at_end_row = NULL;
17392 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17395 /* Find the cursor if not already found. We have to decide whether
17396 PT will appear on this window (it sometimes doesn't, but this is
17397 not a very frequent case.) This decision has to be made before
17398 the current matrix is altered. A value of cursor.vpos < 0 means
17399 that PT is either in one of the lines beginning at
17400 first_unchanged_at_end_row or below the window. Don't care for
17401 lines that might be displayed later at the window end; as
17402 mentioned, this is not a frequent case. */
17403 if (w->cursor.vpos < 0)
17405 /* Cursor in unchanged rows at the top? */
17406 if (PT < CHARPOS (start_pos)
17407 && last_unchanged_at_beg_row)
17409 row = row_containing_pos (w, PT,
17410 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17411 last_unchanged_at_beg_row + 1, 0);
17412 if (row)
17413 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17416 /* Start from first_unchanged_at_end_row looking for PT. */
17417 else if (first_unchanged_at_end_row)
17419 row = row_containing_pos (w, PT - delta,
17420 first_unchanged_at_end_row, NULL, 0);
17421 if (row)
17422 set_cursor_from_row (w, row, w->current_matrix, delta,
17423 delta_bytes, dy, dvpos);
17426 /* Give up if cursor was not found. */
17427 if (w->cursor.vpos < 0)
17429 clear_glyph_matrix (w->desired_matrix);
17430 return -1;
17434 /* Don't let the cursor end in the scroll margins. */
17436 int this_scroll_margin, cursor_height;
17438 this_scroll_margin =
17439 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17440 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17441 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17443 if ((w->cursor.y < this_scroll_margin
17444 && CHARPOS (start) > BEGV)
17445 /* Old redisplay didn't take scroll margin into account at the bottom,
17446 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17447 || (w->cursor.y + (make_cursor_line_fully_visible_p
17448 ? cursor_height + this_scroll_margin
17449 : 1)) > it.last_visible_y)
17451 w->cursor.vpos = -1;
17452 clear_glyph_matrix (w->desired_matrix);
17453 return -1;
17457 /* Scroll the display. Do it before changing the current matrix so
17458 that xterm.c doesn't get confused about where the cursor glyph is
17459 found. */
17460 if (dy && run.height)
17462 update_begin (f);
17464 if (FRAME_WINDOW_P (f))
17466 FRAME_RIF (f)->update_window_begin_hook (w);
17467 FRAME_RIF (f)->clear_window_mouse_face (w);
17468 FRAME_RIF (f)->scroll_run_hook (w, &run);
17469 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17471 else
17473 /* Terminal frame. In this case, dvpos gives the number of
17474 lines to scroll by; dvpos < 0 means scroll up. */
17475 int from_vpos
17476 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17477 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17478 int end = (WINDOW_TOP_EDGE_LINE (w)
17479 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17480 + window_internal_height (w));
17482 #if defined (HAVE_GPM) || defined (MSDOS)
17483 x_clear_window_mouse_face (w);
17484 #endif
17485 /* Perform the operation on the screen. */
17486 if (dvpos > 0)
17488 /* Scroll last_unchanged_at_beg_row to the end of the
17489 window down dvpos lines. */
17490 set_terminal_window (f, end);
17492 /* On dumb terminals delete dvpos lines at the end
17493 before inserting dvpos empty lines. */
17494 if (!FRAME_SCROLL_REGION_OK (f))
17495 ins_del_lines (f, end - dvpos, -dvpos);
17497 /* Insert dvpos empty lines in front of
17498 last_unchanged_at_beg_row. */
17499 ins_del_lines (f, from, dvpos);
17501 else if (dvpos < 0)
17503 /* Scroll up last_unchanged_at_beg_vpos to the end of
17504 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17505 set_terminal_window (f, end);
17507 /* Delete dvpos lines in front of
17508 last_unchanged_at_beg_vpos. ins_del_lines will set
17509 the cursor to the given vpos and emit |dvpos| delete
17510 line sequences. */
17511 ins_del_lines (f, from + dvpos, dvpos);
17513 /* On a dumb terminal insert dvpos empty lines at the
17514 end. */
17515 if (!FRAME_SCROLL_REGION_OK (f))
17516 ins_del_lines (f, end + dvpos, -dvpos);
17519 set_terminal_window (f, 0);
17522 update_end (f);
17525 /* Shift reused rows of the current matrix to the right position.
17526 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17527 text. */
17528 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17529 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17530 if (dvpos < 0)
17532 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17533 bottom_vpos, dvpos);
17534 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17535 bottom_vpos, 0);
17537 else if (dvpos > 0)
17539 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17540 bottom_vpos, dvpos);
17541 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17542 first_unchanged_at_end_vpos + dvpos, 0);
17545 /* For frame-based redisplay, make sure that current frame and window
17546 matrix are in sync with respect to glyph memory. */
17547 if (!FRAME_WINDOW_P (f))
17548 sync_frame_with_window_matrix_rows (w);
17550 /* Adjust buffer positions in reused rows. */
17551 if (delta || delta_bytes)
17552 increment_matrix_positions (current_matrix,
17553 first_unchanged_at_end_vpos + dvpos,
17554 bottom_vpos, delta, delta_bytes);
17556 /* Adjust Y positions. */
17557 if (dy)
17558 shift_glyph_matrix (w, current_matrix,
17559 first_unchanged_at_end_vpos + dvpos,
17560 bottom_vpos, dy);
17562 if (first_unchanged_at_end_row)
17564 first_unchanged_at_end_row += dvpos;
17565 if (first_unchanged_at_end_row->y >= it.last_visible_y
17566 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17567 first_unchanged_at_end_row = NULL;
17570 /* If scrolling up, there may be some lines to display at the end of
17571 the window. */
17572 last_text_row_at_end = NULL;
17573 if (dy < 0)
17575 /* Scrolling up can leave for example a partially visible line
17576 at the end of the window to be redisplayed. */
17577 /* Set last_row to the glyph row in the current matrix where the
17578 window end line is found. It has been moved up or down in
17579 the matrix by dvpos. */
17580 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17581 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17583 /* If last_row is the window end line, it should display text. */
17584 xassert (last_row->displays_text_p);
17586 /* If window end line was partially visible before, begin
17587 displaying at that line. Otherwise begin displaying with the
17588 line following it. */
17589 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17591 init_to_row_start (&it, w, last_row);
17592 it.vpos = last_vpos;
17593 it.current_y = last_row->y;
17595 else
17597 init_to_row_end (&it, w, last_row);
17598 it.vpos = 1 + last_vpos;
17599 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17600 ++last_row;
17603 /* We may start in a continuation line. If so, we have to
17604 get the right continuation_lines_width and current_x. */
17605 it.continuation_lines_width = last_row->continuation_lines_width;
17606 it.hpos = it.current_x = 0;
17608 /* Display the rest of the lines at the window end. */
17609 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17610 while (it.current_y < it.last_visible_y
17611 && !fonts_changed_p)
17613 /* Is it always sure that the display agrees with lines in
17614 the current matrix? I don't think so, so we mark rows
17615 displayed invalid in the current matrix by setting their
17616 enabled_p flag to zero. */
17617 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17618 if (display_line (&it))
17619 last_text_row_at_end = it.glyph_row - 1;
17623 /* Update window_end_pos and window_end_vpos. */
17624 if (first_unchanged_at_end_row
17625 && !last_text_row_at_end)
17627 /* Window end line if one of the preserved rows from the current
17628 matrix. Set row to the last row displaying text in current
17629 matrix starting at first_unchanged_at_end_row, after
17630 scrolling. */
17631 xassert (first_unchanged_at_end_row->displays_text_p);
17632 row = find_last_row_displaying_text (w->current_matrix, &it,
17633 first_unchanged_at_end_row);
17634 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17636 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17637 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17638 w->window_end_vpos
17639 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17640 xassert (w->window_end_bytepos >= 0);
17641 IF_DEBUG (debug_method_add (w, "A"));
17643 else if (last_text_row_at_end)
17645 w->window_end_pos
17646 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17647 w->window_end_bytepos
17648 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17649 w->window_end_vpos
17650 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17651 xassert (w->window_end_bytepos >= 0);
17652 IF_DEBUG (debug_method_add (w, "B"));
17654 else if (last_text_row)
17656 /* We have displayed either to the end of the window or at the
17657 end of the window, i.e. the last row with text is to be found
17658 in the desired matrix. */
17659 w->window_end_pos
17660 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17661 w->window_end_bytepos
17662 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17663 w->window_end_vpos
17664 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17665 xassert (w->window_end_bytepos >= 0);
17667 else if (first_unchanged_at_end_row == NULL
17668 && last_text_row == NULL
17669 && last_text_row_at_end == NULL)
17671 /* Displayed to end of window, but no line containing text was
17672 displayed. Lines were deleted at the end of the window. */
17673 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17674 int vpos = XFASTINT (w->window_end_vpos);
17675 struct glyph_row *current_row = current_matrix->rows + vpos;
17676 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17678 for (row = NULL;
17679 row == NULL && vpos >= first_vpos;
17680 --vpos, --current_row, --desired_row)
17682 if (desired_row->enabled_p)
17684 if (desired_row->displays_text_p)
17685 row = desired_row;
17687 else if (current_row->displays_text_p)
17688 row = current_row;
17691 xassert (row != NULL);
17692 w->window_end_vpos = make_number (vpos + 1);
17693 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17694 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17695 xassert (w->window_end_bytepos >= 0);
17696 IF_DEBUG (debug_method_add (w, "C"));
17698 else
17699 abort ();
17701 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17702 debug_end_vpos = XFASTINT (w->window_end_vpos));
17704 /* Record that display has not been completed. */
17705 w->window_end_valid = Qnil;
17706 w->desired_matrix->no_scrolling_p = 1;
17707 return 3;
17709 #undef GIVE_UP
17714 /***********************************************************************
17715 More debugging support
17716 ***********************************************************************/
17718 #if GLYPH_DEBUG
17720 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17721 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17722 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17725 /* Dump the contents of glyph matrix MATRIX on stderr.
17727 GLYPHS 0 means don't show glyph contents.
17728 GLYPHS 1 means show glyphs in short form
17729 GLYPHS > 1 means show glyphs in long form. */
17731 void
17732 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17734 int i;
17735 for (i = 0; i < matrix->nrows; ++i)
17736 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17740 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17741 the glyph row and area where the glyph comes from. */
17743 void
17744 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17746 if (glyph->type == CHAR_GLYPH)
17748 fprintf (stderr,
17749 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17750 glyph - row->glyphs[TEXT_AREA],
17751 'C',
17752 glyph->charpos,
17753 (BUFFERP (glyph->object)
17754 ? 'B'
17755 : (STRINGP (glyph->object)
17756 ? 'S'
17757 : '-')),
17758 glyph->pixel_width,
17759 glyph->u.ch,
17760 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17761 ? glyph->u.ch
17762 : '.'),
17763 glyph->face_id,
17764 glyph->left_box_line_p,
17765 glyph->right_box_line_p);
17767 else if (glyph->type == STRETCH_GLYPH)
17769 fprintf (stderr,
17770 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17771 glyph - row->glyphs[TEXT_AREA],
17772 'S',
17773 glyph->charpos,
17774 (BUFFERP (glyph->object)
17775 ? 'B'
17776 : (STRINGP (glyph->object)
17777 ? 'S'
17778 : '-')),
17779 glyph->pixel_width,
17781 '.',
17782 glyph->face_id,
17783 glyph->left_box_line_p,
17784 glyph->right_box_line_p);
17786 else if (glyph->type == IMAGE_GLYPH)
17788 fprintf (stderr,
17789 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17790 glyph - row->glyphs[TEXT_AREA],
17791 'I',
17792 glyph->charpos,
17793 (BUFFERP (glyph->object)
17794 ? 'B'
17795 : (STRINGP (glyph->object)
17796 ? 'S'
17797 : '-')),
17798 glyph->pixel_width,
17799 glyph->u.img_id,
17800 '.',
17801 glyph->face_id,
17802 glyph->left_box_line_p,
17803 glyph->right_box_line_p);
17805 else if (glyph->type == COMPOSITE_GLYPH)
17807 fprintf (stderr,
17808 " %5td %4c %6"pI"d %c %3d 0x%05x",
17809 glyph - row->glyphs[TEXT_AREA],
17810 '+',
17811 glyph->charpos,
17812 (BUFFERP (glyph->object)
17813 ? 'B'
17814 : (STRINGP (glyph->object)
17815 ? 'S'
17816 : '-')),
17817 glyph->pixel_width,
17818 glyph->u.cmp.id);
17819 if (glyph->u.cmp.automatic)
17820 fprintf (stderr,
17821 "[%d-%d]",
17822 glyph->slice.cmp.from, glyph->slice.cmp.to);
17823 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17824 glyph->face_id,
17825 glyph->left_box_line_p,
17826 glyph->right_box_line_p);
17831 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17832 GLYPHS 0 means don't show glyph contents.
17833 GLYPHS 1 means show glyphs in short form
17834 GLYPHS > 1 means show glyphs in long form. */
17836 void
17837 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17839 if (glyphs != 1)
17841 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17842 fprintf (stderr, "======================================================================\n");
17844 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17845 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17846 vpos,
17847 MATRIX_ROW_START_CHARPOS (row),
17848 MATRIX_ROW_END_CHARPOS (row),
17849 row->used[TEXT_AREA],
17850 row->contains_overlapping_glyphs_p,
17851 row->enabled_p,
17852 row->truncated_on_left_p,
17853 row->truncated_on_right_p,
17854 row->continued_p,
17855 MATRIX_ROW_CONTINUATION_LINE_P (row),
17856 row->displays_text_p,
17857 row->ends_at_zv_p,
17858 row->fill_line_p,
17859 row->ends_in_middle_of_char_p,
17860 row->starts_in_middle_of_char_p,
17861 row->mouse_face_p,
17862 row->x,
17863 row->y,
17864 row->pixel_width,
17865 row->height,
17866 row->visible_height,
17867 row->ascent,
17868 row->phys_ascent);
17869 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
17870 row->end.overlay_string_index,
17871 row->continuation_lines_width);
17872 fprintf (stderr, "%9"pI"d %5"pI"d\n",
17873 CHARPOS (row->start.string_pos),
17874 CHARPOS (row->end.string_pos));
17875 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17876 row->end.dpvec_index);
17879 if (glyphs > 1)
17881 int area;
17883 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17885 struct glyph *glyph = row->glyphs[area];
17886 struct glyph *glyph_end = glyph + row->used[area];
17888 /* Glyph for a line end in text. */
17889 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17890 ++glyph_end;
17892 if (glyph < glyph_end)
17893 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
17895 for (; glyph < glyph_end; ++glyph)
17896 dump_glyph (row, glyph, area);
17899 else if (glyphs == 1)
17901 int area;
17903 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17905 char *s = (char *) alloca (row->used[area] + 1);
17906 int i;
17908 for (i = 0; i < row->used[area]; ++i)
17910 struct glyph *glyph = row->glyphs[area] + i;
17911 if (glyph->type == CHAR_GLYPH
17912 && glyph->u.ch < 0x80
17913 && glyph->u.ch >= ' ')
17914 s[i] = glyph->u.ch;
17915 else
17916 s[i] = '.';
17919 s[i] = '\0';
17920 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17926 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17927 Sdump_glyph_matrix, 0, 1, "p",
17928 doc: /* Dump the current matrix of the selected window to stderr.
17929 Shows contents of glyph row structures. With non-nil
17930 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17931 glyphs in short form, otherwise show glyphs in long form. */)
17932 (Lisp_Object glyphs)
17934 struct window *w = XWINDOW (selected_window);
17935 struct buffer *buffer = XBUFFER (w->buffer);
17937 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17938 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17939 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17940 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17941 fprintf (stderr, "=============================================\n");
17942 dump_glyph_matrix (w->current_matrix,
17943 NILP (glyphs) ? 0 : XINT (glyphs));
17944 return Qnil;
17948 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17949 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17950 (void)
17952 struct frame *f = XFRAME (selected_frame);
17953 dump_glyph_matrix (f->current_matrix, 1);
17954 return Qnil;
17958 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17959 doc: /* Dump glyph row ROW to stderr.
17960 GLYPH 0 means don't dump glyphs.
17961 GLYPH 1 means dump glyphs in short form.
17962 GLYPH > 1 or omitted means dump glyphs in long form. */)
17963 (Lisp_Object row, Lisp_Object glyphs)
17965 struct glyph_matrix *matrix;
17966 int vpos;
17968 CHECK_NUMBER (row);
17969 matrix = XWINDOW (selected_window)->current_matrix;
17970 vpos = XINT (row);
17971 if (vpos >= 0 && vpos < matrix->nrows)
17972 dump_glyph_row (MATRIX_ROW (matrix, vpos),
17973 vpos,
17974 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17975 return Qnil;
17979 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
17980 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
17981 GLYPH 0 means don't dump glyphs.
17982 GLYPH 1 means dump glyphs in short form.
17983 GLYPH > 1 or omitted means dump glyphs in long form. */)
17984 (Lisp_Object row, Lisp_Object glyphs)
17986 struct frame *sf = SELECTED_FRAME ();
17987 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
17988 int vpos;
17990 CHECK_NUMBER (row);
17991 vpos = XINT (row);
17992 if (vpos >= 0 && vpos < m->nrows)
17993 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
17994 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17995 return Qnil;
17999 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18000 doc: /* Toggle tracing of redisplay.
18001 With ARG, turn tracing on if and only if ARG is positive. */)
18002 (Lisp_Object arg)
18004 if (NILP (arg))
18005 trace_redisplay_p = !trace_redisplay_p;
18006 else
18008 arg = Fprefix_numeric_value (arg);
18009 trace_redisplay_p = XINT (arg) > 0;
18012 return Qnil;
18016 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18017 doc: /* Like `format', but print result to stderr.
18018 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18019 (ptrdiff_t nargs, Lisp_Object *args)
18021 Lisp_Object s = Fformat (nargs, args);
18022 fprintf (stderr, "%s", SDATA (s));
18023 return Qnil;
18026 #endif /* GLYPH_DEBUG */
18030 /***********************************************************************
18031 Building Desired Matrix Rows
18032 ***********************************************************************/
18034 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18035 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18037 static struct glyph_row *
18038 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18040 struct frame *f = XFRAME (WINDOW_FRAME (w));
18041 struct buffer *buffer = XBUFFER (w->buffer);
18042 struct buffer *old = current_buffer;
18043 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18044 int arrow_len = SCHARS (overlay_arrow_string);
18045 const unsigned char *arrow_end = arrow_string + arrow_len;
18046 const unsigned char *p;
18047 struct it it;
18048 int multibyte_p;
18049 int n_glyphs_before;
18051 set_buffer_temp (buffer);
18052 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18053 it.glyph_row->used[TEXT_AREA] = 0;
18054 SET_TEXT_POS (it.position, 0, 0);
18056 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18057 p = arrow_string;
18058 while (p < arrow_end)
18060 Lisp_Object face, ilisp;
18062 /* Get the next character. */
18063 if (multibyte_p)
18064 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18065 else
18067 it.c = it.char_to_display = *p, it.len = 1;
18068 if (! ASCII_CHAR_P (it.c))
18069 it.char_to_display = BYTE8_TO_CHAR (it.c);
18071 p += it.len;
18073 /* Get its face. */
18074 ilisp = make_number (p - arrow_string);
18075 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18076 it.face_id = compute_char_face (f, it.char_to_display, face);
18078 /* Compute its width, get its glyphs. */
18079 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18080 SET_TEXT_POS (it.position, -1, -1);
18081 PRODUCE_GLYPHS (&it);
18083 /* If this character doesn't fit any more in the line, we have
18084 to remove some glyphs. */
18085 if (it.current_x > it.last_visible_x)
18087 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18088 break;
18092 set_buffer_temp (old);
18093 return it.glyph_row;
18097 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
18098 glyphs are only inserted for terminal frames since we can't really
18099 win with truncation glyphs when partially visible glyphs are
18100 involved. Which glyphs to insert is determined by
18101 produce_special_glyphs. */
18103 static void
18104 insert_left_trunc_glyphs (struct it *it)
18106 struct it truncate_it;
18107 struct glyph *from, *end, *to, *toend;
18109 xassert (!FRAME_WINDOW_P (it->f));
18111 /* Get the truncation glyphs. */
18112 truncate_it = *it;
18113 truncate_it.current_x = 0;
18114 truncate_it.face_id = DEFAULT_FACE_ID;
18115 truncate_it.glyph_row = &scratch_glyph_row;
18116 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18117 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18118 truncate_it.object = make_number (0);
18119 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18121 /* Overwrite glyphs from IT with truncation glyphs. */
18122 if (!it->glyph_row->reversed_p)
18124 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18125 end = from + truncate_it.glyph_row->used[TEXT_AREA];
18126 to = it->glyph_row->glyphs[TEXT_AREA];
18127 toend = to + it->glyph_row->used[TEXT_AREA];
18129 while (from < end)
18130 *to++ = *from++;
18132 /* There may be padding glyphs left over. Overwrite them too. */
18133 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18135 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18136 while (from < end)
18137 *to++ = *from++;
18140 if (to > toend)
18141 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18143 else
18145 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18146 that back to front. */
18147 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18148 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18149 toend = it->glyph_row->glyphs[TEXT_AREA];
18150 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18152 while (from >= end && to >= toend)
18153 *to-- = *from--;
18154 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18156 from =
18157 truncate_it.glyph_row->glyphs[TEXT_AREA]
18158 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18159 while (from >= end && to >= toend)
18160 *to-- = *from--;
18162 if (from >= end)
18164 /* Need to free some room before prepending additional
18165 glyphs. */
18166 int move_by = from - end + 1;
18167 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18168 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18170 for ( ; g >= g0; g--)
18171 g[move_by] = *g;
18172 while (from >= end)
18173 *to-- = *from--;
18174 it->glyph_row->used[TEXT_AREA] += move_by;
18179 /* Compute the hash code for ROW. */
18180 unsigned
18181 row_hash (struct glyph_row *row)
18183 int area, k;
18184 unsigned hashval = 0;
18186 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18187 for (k = 0; k < row->used[area]; ++k)
18188 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18189 + row->glyphs[area][k].u.val
18190 + row->glyphs[area][k].face_id
18191 + row->glyphs[area][k].padding_p
18192 + (row->glyphs[area][k].type << 2));
18194 return hashval;
18197 /* Compute the pixel height and width of IT->glyph_row.
18199 Most of the time, ascent and height of a display line will be equal
18200 to the max_ascent and max_height values of the display iterator
18201 structure. This is not the case if
18203 1. We hit ZV without displaying anything. In this case, max_ascent
18204 and max_height will be zero.
18206 2. We have some glyphs that don't contribute to the line height.
18207 (The glyph row flag contributes_to_line_height_p is for future
18208 pixmap extensions).
18210 The first case is easily covered by using default values because in
18211 these cases, the line height does not really matter, except that it
18212 must not be zero. */
18214 static void
18215 compute_line_metrics (struct it *it)
18217 struct glyph_row *row = it->glyph_row;
18219 if (FRAME_WINDOW_P (it->f))
18221 int i, min_y, max_y;
18223 /* The line may consist of one space only, that was added to
18224 place the cursor on it. If so, the row's height hasn't been
18225 computed yet. */
18226 if (row->height == 0)
18228 if (it->max_ascent + it->max_descent == 0)
18229 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18230 row->ascent = it->max_ascent;
18231 row->height = it->max_ascent + it->max_descent;
18232 row->phys_ascent = it->max_phys_ascent;
18233 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18234 row->extra_line_spacing = it->max_extra_line_spacing;
18237 /* Compute the width of this line. */
18238 row->pixel_width = row->x;
18239 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18240 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18242 xassert (row->pixel_width >= 0);
18243 xassert (row->ascent >= 0 && row->height > 0);
18245 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18246 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18248 /* If first line's physical ascent is larger than its logical
18249 ascent, use the physical ascent, and make the row taller.
18250 This makes accented characters fully visible. */
18251 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18252 && row->phys_ascent > row->ascent)
18254 row->height += row->phys_ascent - row->ascent;
18255 row->ascent = row->phys_ascent;
18258 /* Compute how much of the line is visible. */
18259 row->visible_height = row->height;
18261 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18262 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18264 if (row->y < min_y)
18265 row->visible_height -= min_y - row->y;
18266 if (row->y + row->height > max_y)
18267 row->visible_height -= row->y + row->height - max_y;
18269 else
18271 row->pixel_width = row->used[TEXT_AREA];
18272 if (row->continued_p)
18273 row->pixel_width -= it->continuation_pixel_width;
18274 else if (row->truncated_on_right_p)
18275 row->pixel_width -= it->truncation_pixel_width;
18276 row->ascent = row->phys_ascent = 0;
18277 row->height = row->phys_height = row->visible_height = 1;
18278 row->extra_line_spacing = 0;
18281 /* Compute a hash code for this row. */
18282 row->hash = row_hash (row);
18284 it->max_ascent = it->max_descent = 0;
18285 it->max_phys_ascent = it->max_phys_descent = 0;
18289 /* Append one space to the glyph row of iterator IT if doing a
18290 window-based redisplay. The space has the same face as
18291 IT->face_id. Value is non-zero if a space was added.
18293 This function is called to make sure that there is always one glyph
18294 at the end of a glyph row that the cursor can be set on under
18295 window-systems. (If there weren't such a glyph we would not know
18296 how wide and tall a box cursor should be displayed).
18298 At the same time this space let's a nicely handle clearing to the
18299 end of the line if the row ends in italic text. */
18301 static int
18302 append_space_for_newline (struct it *it, int default_face_p)
18304 if (FRAME_WINDOW_P (it->f))
18306 int n = it->glyph_row->used[TEXT_AREA];
18308 if (it->glyph_row->glyphs[TEXT_AREA] + n
18309 < it->glyph_row->glyphs[1 + TEXT_AREA])
18311 /* Save some values that must not be changed.
18312 Must save IT->c and IT->len because otherwise
18313 ITERATOR_AT_END_P wouldn't work anymore after
18314 append_space_for_newline has been called. */
18315 enum display_element_type saved_what = it->what;
18316 int saved_c = it->c, saved_len = it->len;
18317 int saved_char_to_display = it->char_to_display;
18318 int saved_x = it->current_x;
18319 int saved_face_id = it->face_id;
18320 struct text_pos saved_pos;
18321 Lisp_Object saved_object;
18322 struct face *face;
18324 saved_object = it->object;
18325 saved_pos = it->position;
18327 it->what = IT_CHARACTER;
18328 memset (&it->position, 0, sizeof it->position);
18329 it->object = make_number (0);
18330 it->c = it->char_to_display = ' ';
18331 it->len = 1;
18333 /* If the default face was remapped, be sure to use the
18334 remapped face for the appended newline. */
18335 if (default_face_p)
18336 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18337 else if (it->face_before_selective_p)
18338 it->face_id = it->saved_face_id;
18339 face = FACE_FROM_ID (it->f, it->face_id);
18340 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18342 PRODUCE_GLYPHS (it);
18344 it->override_ascent = -1;
18345 it->constrain_row_ascent_descent_p = 0;
18346 it->current_x = saved_x;
18347 it->object = saved_object;
18348 it->position = saved_pos;
18349 it->what = saved_what;
18350 it->face_id = saved_face_id;
18351 it->len = saved_len;
18352 it->c = saved_c;
18353 it->char_to_display = saved_char_to_display;
18354 return 1;
18358 return 0;
18362 /* Extend the face of the last glyph in the text area of IT->glyph_row
18363 to the end of the display line. Called from display_line. If the
18364 glyph row is empty, add a space glyph to it so that we know the
18365 face to draw. Set the glyph row flag fill_line_p. If the glyph
18366 row is R2L, prepend a stretch glyph to cover the empty space to the
18367 left of the leftmost glyph. */
18369 static void
18370 extend_face_to_end_of_line (struct it *it)
18372 struct face *face, *default_face;
18373 struct frame *f = it->f;
18375 /* If line is already filled, do nothing. Non window-system frames
18376 get a grace of one more ``pixel'' because their characters are
18377 1-``pixel'' wide, so they hit the equality too early. This grace
18378 is needed only for R2L rows that are not continued, to produce
18379 one extra blank where we could display the cursor. */
18380 if (it->current_x >= it->last_visible_x
18381 + (!FRAME_WINDOW_P (f)
18382 && it->glyph_row->reversed_p
18383 && !it->glyph_row->continued_p))
18384 return;
18386 /* The default face, possibly remapped. */
18387 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18389 /* Face extension extends the background and box of IT->face_id
18390 to the end of the line. If the background equals the background
18391 of the frame, we don't have to do anything. */
18392 if (it->face_before_selective_p)
18393 face = FACE_FROM_ID (f, it->saved_face_id);
18394 else
18395 face = FACE_FROM_ID (f, it->face_id);
18397 if (FRAME_WINDOW_P (f)
18398 && it->glyph_row->displays_text_p
18399 && face->box == FACE_NO_BOX
18400 && face->background == FRAME_BACKGROUND_PIXEL (f)
18401 && !face->stipple
18402 && !it->glyph_row->reversed_p)
18403 return;
18405 /* Set the glyph row flag indicating that the face of the last glyph
18406 in the text area has to be drawn to the end of the text area. */
18407 it->glyph_row->fill_line_p = 1;
18409 /* If current character of IT is not ASCII, make sure we have the
18410 ASCII face. This will be automatically undone the next time
18411 get_next_display_element returns a multibyte character. Note
18412 that the character will always be single byte in unibyte
18413 text. */
18414 if (!ASCII_CHAR_P (it->c))
18416 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18419 if (FRAME_WINDOW_P (f))
18421 /* If the row is empty, add a space with the current face of IT,
18422 so that we know which face to draw. */
18423 if (it->glyph_row->used[TEXT_AREA] == 0)
18425 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18426 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18427 it->glyph_row->used[TEXT_AREA] = 1;
18429 #ifdef HAVE_WINDOW_SYSTEM
18430 if (it->glyph_row->reversed_p)
18432 /* Prepend a stretch glyph to the row, such that the
18433 rightmost glyph will be drawn flushed all the way to the
18434 right margin of the window. The stretch glyph that will
18435 occupy the empty space, if any, to the left of the
18436 glyphs. */
18437 struct font *font = face->font ? face->font : FRAME_FONT (f);
18438 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18439 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18440 struct glyph *g;
18441 int row_width, stretch_ascent, stretch_width;
18442 struct text_pos saved_pos;
18443 int saved_face_id, saved_avoid_cursor;
18445 for (row_width = 0, g = row_start; g < row_end; g++)
18446 row_width += g->pixel_width;
18447 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18448 if (stretch_width > 0)
18450 stretch_ascent =
18451 (((it->ascent + it->descent)
18452 * FONT_BASE (font)) / FONT_HEIGHT (font));
18453 saved_pos = it->position;
18454 memset (&it->position, 0, sizeof it->position);
18455 saved_avoid_cursor = it->avoid_cursor_p;
18456 it->avoid_cursor_p = 1;
18457 saved_face_id = it->face_id;
18458 /* The last row's stretch glyph should get the default
18459 face, to avoid painting the rest of the window with
18460 the region face, if the region ends at ZV. */
18461 if (it->glyph_row->ends_at_zv_p)
18462 it->face_id = default_face->id;
18463 else
18464 it->face_id = face->id;
18465 append_stretch_glyph (it, make_number (0), stretch_width,
18466 it->ascent + it->descent, stretch_ascent);
18467 it->position = saved_pos;
18468 it->avoid_cursor_p = saved_avoid_cursor;
18469 it->face_id = saved_face_id;
18472 #endif /* HAVE_WINDOW_SYSTEM */
18474 else
18476 /* Save some values that must not be changed. */
18477 int saved_x = it->current_x;
18478 struct text_pos saved_pos;
18479 Lisp_Object saved_object;
18480 enum display_element_type saved_what = it->what;
18481 int saved_face_id = it->face_id;
18483 saved_object = it->object;
18484 saved_pos = it->position;
18486 it->what = IT_CHARACTER;
18487 memset (&it->position, 0, sizeof it->position);
18488 it->object = make_number (0);
18489 it->c = it->char_to_display = ' ';
18490 it->len = 1;
18491 /* The last row's blank glyphs should get the default face, to
18492 avoid painting the rest of the window with the region face,
18493 if the region ends at ZV. */
18494 if (it->glyph_row->ends_at_zv_p)
18495 it->face_id = default_face->id;
18496 else
18497 it->face_id = face->id;
18499 PRODUCE_GLYPHS (it);
18501 while (it->current_x <= it->last_visible_x)
18502 PRODUCE_GLYPHS (it);
18504 /* Don't count these blanks really. It would let us insert a left
18505 truncation glyph below and make us set the cursor on them, maybe. */
18506 it->current_x = saved_x;
18507 it->object = saved_object;
18508 it->position = saved_pos;
18509 it->what = saved_what;
18510 it->face_id = saved_face_id;
18515 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18516 trailing whitespace. */
18518 static int
18519 trailing_whitespace_p (EMACS_INT charpos)
18521 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
18522 int c = 0;
18524 while (bytepos < ZV_BYTE
18525 && (c = FETCH_CHAR (bytepos),
18526 c == ' ' || c == '\t'))
18527 ++bytepos;
18529 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18531 if (bytepos != PT_BYTE)
18532 return 1;
18534 return 0;
18538 /* Highlight trailing whitespace, if any, in ROW. */
18540 static void
18541 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18543 int used = row->used[TEXT_AREA];
18545 if (used)
18547 struct glyph *start = row->glyphs[TEXT_AREA];
18548 struct glyph *glyph = start + used - 1;
18550 if (row->reversed_p)
18552 /* Right-to-left rows need to be processed in the opposite
18553 direction, so swap the edge pointers. */
18554 glyph = start;
18555 start = row->glyphs[TEXT_AREA] + used - 1;
18558 /* Skip over glyphs inserted to display the cursor at the
18559 end of a line, for extending the face of the last glyph
18560 to the end of the line on terminals, and for truncation
18561 and continuation glyphs. */
18562 if (!row->reversed_p)
18564 while (glyph >= start
18565 && glyph->type == CHAR_GLYPH
18566 && INTEGERP (glyph->object))
18567 --glyph;
18569 else
18571 while (glyph <= start
18572 && glyph->type == CHAR_GLYPH
18573 && INTEGERP (glyph->object))
18574 ++glyph;
18577 /* If last glyph is a space or stretch, and it's trailing
18578 whitespace, set the face of all trailing whitespace glyphs in
18579 IT->glyph_row to `trailing-whitespace'. */
18580 if ((row->reversed_p ? glyph <= start : glyph >= start)
18581 && BUFFERP (glyph->object)
18582 && (glyph->type == STRETCH_GLYPH
18583 || (glyph->type == CHAR_GLYPH
18584 && glyph->u.ch == ' '))
18585 && trailing_whitespace_p (glyph->charpos))
18587 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18588 if (face_id < 0)
18589 return;
18591 if (!row->reversed_p)
18593 while (glyph >= start
18594 && BUFFERP (glyph->object)
18595 && (glyph->type == STRETCH_GLYPH
18596 || (glyph->type == CHAR_GLYPH
18597 && glyph->u.ch == ' ')))
18598 (glyph--)->face_id = face_id;
18600 else
18602 while (glyph <= start
18603 && BUFFERP (glyph->object)
18604 && (glyph->type == STRETCH_GLYPH
18605 || (glyph->type == CHAR_GLYPH
18606 && glyph->u.ch == ' ')))
18607 (glyph++)->face_id = face_id;
18614 /* Value is non-zero if glyph row ROW should be
18615 used to hold the cursor. */
18617 static int
18618 cursor_row_p (struct glyph_row *row)
18620 int result = 1;
18622 if (PT == CHARPOS (row->end.pos)
18623 || PT == MATRIX_ROW_END_CHARPOS (row))
18625 /* Suppose the row ends on a string.
18626 Unless the row is continued, that means it ends on a newline
18627 in the string. If it's anything other than a display string
18628 (e.g., a before-string from an overlay), we don't want the
18629 cursor there. (This heuristic seems to give the optimal
18630 behavior for the various types of multi-line strings.)
18631 One exception: if the string has `cursor' property on one of
18632 its characters, we _do_ want the cursor there. */
18633 if (CHARPOS (row->end.string_pos) >= 0)
18635 if (row->continued_p)
18636 result = 1;
18637 else
18639 /* Check for `display' property. */
18640 struct glyph *beg = row->glyphs[TEXT_AREA];
18641 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18642 struct glyph *glyph;
18644 result = 0;
18645 for (glyph = end; glyph >= beg; --glyph)
18646 if (STRINGP (glyph->object))
18648 Lisp_Object prop
18649 = Fget_char_property (make_number (PT),
18650 Qdisplay, Qnil);
18651 result =
18652 (!NILP (prop)
18653 && display_prop_string_p (prop, glyph->object));
18654 /* If there's a `cursor' property on one of the
18655 string's characters, this row is a cursor row,
18656 even though this is not a display string. */
18657 if (!result)
18659 Lisp_Object s = glyph->object;
18661 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18663 EMACS_INT gpos = glyph->charpos;
18665 if (!NILP (Fget_char_property (make_number (gpos),
18666 Qcursor, s)))
18668 result = 1;
18669 break;
18673 break;
18677 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18679 /* If the row ends in middle of a real character,
18680 and the line is continued, we want the cursor here.
18681 That's because CHARPOS (ROW->end.pos) would equal
18682 PT if PT is before the character. */
18683 if (!row->ends_in_ellipsis_p)
18684 result = row->continued_p;
18685 else
18686 /* If the row ends in an ellipsis, then
18687 CHARPOS (ROW->end.pos) will equal point after the
18688 invisible text. We want that position to be displayed
18689 after the ellipsis. */
18690 result = 0;
18692 /* If the row ends at ZV, display the cursor at the end of that
18693 row instead of at the start of the row below. */
18694 else if (row->ends_at_zv_p)
18695 result = 1;
18696 else
18697 result = 0;
18700 return result;
18705 /* Push the property PROP so that it will be rendered at the current
18706 position in IT. Return 1 if PROP was successfully pushed, 0
18707 otherwise. Called from handle_line_prefix to handle the
18708 `line-prefix' and `wrap-prefix' properties. */
18710 static int
18711 push_prefix_prop (struct it *it, Lisp_Object prop)
18713 struct text_pos pos =
18714 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18716 xassert (it->method == GET_FROM_BUFFER
18717 || it->method == GET_FROM_DISPLAY_VECTOR
18718 || it->method == GET_FROM_STRING);
18720 /* We need to save the current buffer/string position, so it will be
18721 restored by pop_it, because iterate_out_of_display_property
18722 depends on that being set correctly, but some situations leave
18723 it->position not yet set when this function is called. */
18724 push_it (it, &pos);
18726 if (STRINGP (prop))
18728 if (SCHARS (prop) == 0)
18730 pop_it (it);
18731 return 0;
18734 it->string = prop;
18735 it->string_from_prefix_prop_p = 1;
18736 it->multibyte_p = STRING_MULTIBYTE (it->string);
18737 it->current.overlay_string_index = -1;
18738 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18739 it->end_charpos = it->string_nchars = SCHARS (it->string);
18740 it->method = GET_FROM_STRING;
18741 it->stop_charpos = 0;
18742 it->prev_stop = 0;
18743 it->base_level_stop = 0;
18745 /* Force paragraph direction to be that of the parent
18746 buffer/string. */
18747 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18748 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18749 else
18750 it->paragraph_embedding = L2R;
18752 /* Set up the bidi iterator for this display string. */
18753 if (it->bidi_p)
18755 it->bidi_it.string.lstring = it->string;
18756 it->bidi_it.string.s = NULL;
18757 it->bidi_it.string.schars = it->end_charpos;
18758 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18759 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18760 it->bidi_it.string.unibyte = !it->multibyte_p;
18761 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18764 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18766 it->method = GET_FROM_STRETCH;
18767 it->object = prop;
18769 #ifdef HAVE_WINDOW_SYSTEM
18770 else if (IMAGEP (prop))
18772 it->what = IT_IMAGE;
18773 it->image_id = lookup_image (it->f, prop);
18774 it->method = GET_FROM_IMAGE;
18776 #endif /* HAVE_WINDOW_SYSTEM */
18777 else
18779 pop_it (it); /* bogus display property, give up */
18780 return 0;
18783 return 1;
18786 /* Return the character-property PROP at the current position in IT. */
18788 static Lisp_Object
18789 get_it_property (struct it *it, Lisp_Object prop)
18791 Lisp_Object position;
18793 if (STRINGP (it->object))
18794 position = make_number (IT_STRING_CHARPOS (*it));
18795 else if (BUFFERP (it->object))
18796 position = make_number (IT_CHARPOS (*it));
18797 else
18798 return Qnil;
18800 return Fget_char_property (position, prop, it->object);
18803 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18805 static void
18806 handle_line_prefix (struct it *it)
18808 Lisp_Object prefix;
18810 if (it->continuation_lines_width > 0)
18812 prefix = get_it_property (it, Qwrap_prefix);
18813 if (NILP (prefix))
18814 prefix = Vwrap_prefix;
18816 else
18818 prefix = get_it_property (it, Qline_prefix);
18819 if (NILP (prefix))
18820 prefix = Vline_prefix;
18822 if (! NILP (prefix) && push_prefix_prop (it, prefix))
18824 /* If the prefix is wider than the window, and we try to wrap
18825 it, it would acquire its own wrap prefix, and so on till the
18826 iterator stack overflows. So, don't wrap the prefix. */
18827 it->line_wrap = TRUNCATE;
18828 it->avoid_cursor_p = 1;
18834 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18835 only for R2L lines from display_line and display_string, when they
18836 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18837 the line/string needs to be continued on the next glyph row. */
18838 static void
18839 unproduce_glyphs (struct it *it, int n)
18841 struct glyph *glyph, *end;
18843 xassert (it->glyph_row);
18844 xassert (it->glyph_row->reversed_p);
18845 xassert (it->area == TEXT_AREA);
18846 xassert (n <= it->glyph_row->used[TEXT_AREA]);
18848 if (n > it->glyph_row->used[TEXT_AREA])
18849 n = it->glyph_row->used[TEXT_AREA];
18850 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18851 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18852 for ( ; glyph < end; glyph++)
18853 glyph[-n] = *glyph;
18856 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18857 and ROW->maxpos. */
18858 static void
18859 find_row_edges (struct it *it, struct glyph_row *row,
18860 EMACS_INT min_pos, EMACS_INT min_bpos,
18861 EMACS_INT max_pos, EMACS_INT max_bpos)
18863 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18864 lines' rows is implemented for bidi-reordered rows. */
18866 /* ROW->minpos is the value of min_pos, the minimal buffer position
18867 we have in ROW, or ROW->start.pos if that is smaller. */
18868 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18869 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18870 else
18871 /* We didn't find buffer positions smaller than ROW->start, or
18872 didn't find _any_ valid buffer positions in any of the glyphs,
18873 so we must trust the iterator's computed positions. */
18874 row->minpos = row->start.pos;
18875 if (max_pos <= 0)
18877 max_pos = CHARPOS (it->current.pos);
18878 max_bpos = BYTEPOS (it->current.pos);
18881 /* Here are the various use-cases for ending the row, and the
18882 corresponding values for ROW->maxpos:
18884 Line ends in a newline from buffer eol_pos + 1
18885 Line is continued from buffer max_pos + 1
18886 Line is truncated on right it->current.pos
18887 Line ends in a newline from string max_pos + 1(*)
18888 (*) + 1 only when line ends in a forward scan
18889 Line is continued from string max_pos
18890 Line is continued from display vector max_pos
18891 Line is entirely from a string min_pos == max_pos
18892 Line is entirely from a display vector min_pos == max_pos
18893 Line that ends at ZV ZV
18895 If you discover other use-cases, please add them here as
18896 appropriate. */
18897 if (row->ends_at_zv_p)
18898 row->maxpos = it->current.pos;
18899 else if (row->used[TEXT_AREA])
18901 int seen_this_string = 0;
18902 struct glyph_row *r1 = row - 1;
18904 /* Did we see the same display string on the previous row? */
18905 if (STRINGP (it->object)
18906 /* this is not the first row */
18907 && row > it->w->desired_matrix->rows
18908 /* previous row is not the header line */
18909 && !r1->mode_line_p
18910 /* previous row also ends in a newline from a string */
18911 && r1->ends_in_newline_from_string_p)
18913 struct glyph *start, *end;
18915 /* Search for the last glyph of the previous row that came
18916 from buffer or string. Depending on whether the row is
18917 L2R or R2L, we need to process it front to back or the
18918 other way round. */
18919 if (!r1->reversed_p)
18921 start = r1->glyphs[TEXT_AREA];
18922 end = start + r1->used[TEXT_AREA];
18923 /* Glyphs inserted by redisplay have an integer (zero)
18924 as their object. */
18925 while (end > start
18926 && INTEGERP ((end - 1)->object)
18927 && (end - 1)->charpos <= 0)
18928 --end;
18929 if (end > start)
18931 if (EQ ((end - 1)->object, it->object))
18932 seen_this_string = 1;
18934 else
18935 /* If all the glyphs of the previous row were inserted
18936 by redisplay, it means the previous row was
18937 produced from a single newline, which is only
18938 possible if that newline came from the same string
18939 as the one which produced this ROW. */
18940 seen_this_string = 1;
18942 else
18944 end = r1->glyphs[TEXT_AREA] - 1;
18945 start = end + r1->used[TEXT_AREA];
18946 while (end < start
18947 && INTEGERP ((end + 1)->object)
18948 && (end + 1)->charpos <= 0)
18949 ++end;
18950 if (end < start)
18952 if (EQ ((end + 1)->object, it->object))
18953 seen_this_string = 1;
18955 else
18956 seen_this_string = 1;
18959 /* Take note of each display string that covers a newline only
18960 once, the first time we see it. This is for when a display
18961 string includes more than one newline in it. */
18962 if (row->ends_in_newline_from_string_p && !seen_this_string)
18964 /* If we were scanning the buffer forward when we displayed
18965 the string, we want to account for at least one buffer
18966 position that belongs to this row (position covered by
18967 the display string), so that cursor positioning will
18968 consider this row as a candidate when point is at the end
18969 of the visual line represented by this row. This is not
18970 required when scanning back, because max_pos will already
18971 have a much larger value. */
18972 if (CHARPOS (row->end.pos) > max_pos)
18973 INC_BOTH (max_pos, max_bpos);
18974 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18976 else if (CHARPOS (it->eol_pos) > 0)
18977 SET_TEXT_POS (row->maxpos,
18978 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
18979 else if (row->continued_p)
18981 /* If max_pos is different from IT's current position, it
18982 means IT->method does not belong to the display element
18983 at max_pos. However, it also means that the display
18984 element at max_pos was displayed in its entirety on this
18985 line, which is equivalent to saying that the next line
18986 starts at the next buffer position. */
18987 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
18988 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18989 else
18991 INC_BOTH (max_pos, max_bpos);
18992 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18995 else if (row->truncated_on_right_p)
18996 /* display_line already called reseat_at_next_visible_line_start,
18997 which puts the iterator at the beginning of the next line, in
18998 the logical order. */
18999 row->maxpos = it->current.pos;
19000 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19001 /* A line that is entirely from a string/image/stretch... */
19002 row->maxpos = row->minpos;
19003 else
19004 abort ();
19006 else
19007 row->maxpos = it->current.pos;
19010 /* Construct the glyph row IT->glyph_row in the desired matrix of
19011 IT->w from text at the current position of IT. See dispextern.h
19012 for an overview of struct it. Value is non-zero if
19013 IT->glyph_row displays text, as opposed to a line displaying ZV
19014 only. */
19016 static int
19017 display_line (struct it *it)
19019 struct glyph_row *row = it->glyph_row;
19020 Lisp_Object overlay_arrow_string;
19021 struct it wrap_it;
19022 void *wrap_data = NULL;
19023 int may_wrap = 0, wrap_x IF_LINT (= 0);
19024 int wrap_row_used = -1;
19025 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19026 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19027 int wrap_row_extra_line_spacing IF_LINT (= 0);
19028 EMACS_INT wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19029 EMACS_INT wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19030 int cvpos;
19031 EMACS_INT min_pos = ZV + 1, max_pos = 0;
19032 EMACS_INT min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19034 /* We always start displaying at hpos zero even if hscrolled. */
19035 xassert (it->hpos == 0 && it->current_x == 0);
19037 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19038 >= it->w->desired_matrix->nrows)
19040 it->w->nrows_scale_factor++;
19041 fonts_changed_p = 1;
19042 return 0;
19045 /* Is IT->w showing the region? */
19046 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
19048 /* Clear the result glyph row and enable it. */
19049 prepare_desired_row (row);
19051 row->y = it->current_y;
19052 row->start = it->start;
19053 row->continuation_lines_width = it->continuation_lines_width;
19054 row->displays_text_p = 1;
19055 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19056 it->starts_in_middle_of_char_p = 0;
19058 /* Arrange the overlays nicely for our purposes. Usually, we call
19059 display_line on only one line at a time, in which case this
19060 can't really hurt too much, or we call it on lines which appear
19061 one after another in the buffer, in which case all calls to
19062 recenter_overlay_lists but the first will be pretty cheap. */
19063 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19065 /* Move over display elements that are not visible because we are
19066 hscrolled. This may stop at an x-position < IT->first_visible_x
19067 if the first glyph is partially visible or if we hit a line end. */
19068 if (it->current_x < it->first_visible_x)
19070 this_line_min_pos = row->start.pos;
19071 move_it_in_display_line_to (it, ZV, it->first_visible_x,
19072 MOVE_TO_POS | MOVE_TO_X);
19073 /* Record the smallest positions seen while we moved over
19074 display elements that are not visible. This is needed by
19075 redisplay_internal for optimizing the case where the cursor
19076 stays inside the same line. The rest of this function only
19077 considers positions that are actually displayed, so
19078 RECORD_MAX_MIN_POS will not otherwise record positions that
19079 are hscrolled to the left of the left edge of the window. */
19080 min_pos = CHARPOS (this_line_min_pos);
19081 min_bpos = BYTEPOS (this_line_min_pos);
19083 else
19085 /* We only do this when not calling `move_it_in_display_line_to'
19086 above, because move_it_in_display_line_to calls
19087 handle_line_prefix itself. */
19088 handle_line_prefix (it);
19091 /* Get the initial row height. This is either the height of the
19092 text hscrolled, if there is any, or zero. */
19093 row->ascent = it->max_ascent;
19094 row->height = it->max_ascent + it->max_descent;
19095 row->phys_ascent = it->max_phys_ascent;
19096 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19097 row->extra_line_spacing = it->max_extra_line_spacing;
19099 /* Utility macro to record max and min buffer positions seen until now. */
19100 #define RECORD_MAX_MIN_POS(IT) \
19101 do \
19103 int composition_p = !STRINGP ((IT)->string) \
19104 && ((IT)->what == IT_COMPOSITION); \
19105 EMACS_INT current_pos = \
19106 composition_p ? (IT)->cmp_it.charpos \
19107 : IT_CHARPOS (*(IT)); \
19108 EMACS_INT current_bpos = \
19109 composition_p ? CHAR_TO_BYTE (current_pos) \
19110 : IT_BYTEPOS (*(IT)); \
19111 if (current_pos < min_pos) \
19113 min_pos = current_pos; \
19114 min_bpos = current_bpos; \
19116 if (IT_CHARPOS (*it) > max_pos) \
19118 max_pos = IT_CHARPOS (*it); \
19119 max_bpos = IT_BYTEPOS (*it); \
19122 while (0)
19124 /* Loop generating characters. The loop is left with IT on the next
19125 character to display. */
19126 while (1)
19128 int n_glyphs_before, hpos_before, x_before;
19129 int x, nglyphs;
19130 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19132 /* Retrieve the next thing to display. Value is zero if end of
19133 buffer reached. */
19134 if (!get_next_display_element (it))
19136 /* Maybe add a space at the end of this line that is used to
19137 display the cursor there under X. Set the charpos of the
19138 first glyph of blank lines not corresponding to any text
19139 to -1. */
19140 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19141 row->exact_window_width_line_p = 1;
19142 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19143 || row->used[TEXT_AREA] == 0)
19145 row->glyphs[TEXT_AREA]->charpos = -1;
19146 row->displays_text_p = 0;
19148 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19149 && (!MINI_WINDOW_P (it->w)
19150 || (minibuf_level && EQ (it->window, minibuf_window))))
19151 row->indicate_empty_line_p = 1;
19154 it->continuation_lines_width = 0;
19155 row->ends_at_zv_p = 1;
19156 /* A row that displays right-to-left text must always have
19157 its last face extended all the way to the end of line,
19158 even if this row ends in ZV, because we still write to
19159 the screen left to right. We also need to extend the
19160 last face if the default face is remapped to some
19161 different face, otherwise the functions that clear
19162 portions of the screen will clear with the default face's
19163 background color. */
19164 if (row->reversed_p
19165 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19166 extend_face_to_end_of_line (it);
19167 break;
19170 /* Now, get the metrics of what we want to display. This also
19171 generates glyphs in `row' (which is IT->glyph_row). */
19172 n_glyphs_before = row->used[TEXT_AREA];
19173 x = it->current_x;
19175 /* Remember the line height so far in case the next element doesn't
19176 fit on the line. */
19177 if (it->line_wrap != TRUNCATE)
19179 ascent = it->max_ascent;
19180 descent = it->max_descent;
19181 phys_ascent = it->max_phys_ascent;
19182 phys_descent = it->max_phys_descent;
19184 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19186 if (IT_DISPLAYING_WHITESPACE (it))
19187 may_wrap = 1;
19188 else if (may_wrap)
19190 SAVE_IT (wrap_it, *it, wrap_data);
19191 wrap_x = x;
19192 wrap_row_used = row->used[TEXT_AREA];
19193 wrap_row_ascent = row->ascent;
19194 wrap_row_height = row->height;
19195 wrap_row_phys_ascent = row->phys_ascent;
19196 wrap_row_phys_height = row->phys_height;
19197 wrap_row_extra_line_spacing = row->extra_line_spacing;
19198 wrap_row_min_pos = min_pos;
19199 wrap_row_min_bpos = min_bpos;
19200 wrap_row_max_pos = max_pos;
19201 wrap_row_max_bpos = max_bpos;
19202 may_wrap = 0;
19207 PRODUCE_GLYPHS (it);
19209 /* If this display element was in marginal areas, continue with
19210 the next one. */
19211 if (it->area != TEXT_AREA)
19213 row->ascent = max (row->ascent, it->max_ascent);
19214 row->height = max (row->height, it->max_ascent + it->max_descent);
19215 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19216 row->phys_height = max (row->phys_height,
19217 it->max_phys_ascent + it->max_phys_descent);
19218 row->extra_line_spacing = max (row->extra_line_spacing,
19219 it->max_extra_line_spacing);
19220 set_iterator_to_next (it, 1);
19221 continue;
19224 /* Does the display element fit on the line? If we truncate
19225 lines, we should draw past the right edge of the window. If
19226 we don't truncate, we want to stop so that we can display the
19227 continuation glyph before the right margin. If lines are
19228 continued, there are two possible strategies for characters
19229 resulting in more than 1 glyph (e.g. tabs): Display as many
19230 glyphs as possible in this line and leave the rest for the
19231 continuation line, or display the whole element in the next
19232 line. Original redisplay did the former, so we do it also. */
19233 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19234 hpos_before = it->hpos;
19235 x_before = x;
19237 if (/* Not a newline. */
19238 nglyphs > 0
19239 /* Glyphs produced fit entirely in the line. */
19240 && it->current_x < it->last_visible_x)
19242 it->hpos += nglyphs;
19243 row->ascent = max (row->ascent, it->max_ascent);
19244 row->height = max (row->height, it->max_ascent + it->max_descent);
19245 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19246 row->phys_height = max (row->phys_height,
19247 it->max_phys_ascent + it->max_phys_descent);
19248 row->extra_line_spacing = max (row->extra_line_spacing,
19249 it->max_extra_line_spacing);
19250 if (it->current_x - it->pixel_width < it->first_visible_x)
19251 row->x = x - it->first_visible_x;
19252 /* Record the maximum and minimum buffer positions seen so
19253 far in glyphs that will be displayed by this row. */
19254 if (it->bidi_p)
19255 RECORD_MAX_MIN_POS (it);
19257 else
19259 int i, new_x;
19260 struct glyph *glyph;
19262 for (i = 0; i < nglyphs; ++i, x = new_x)
19264 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19265 new_x = x + glyph->pixel_width;
19267 if (/* Lines are continued. */
19268 it->line_wrap != TRUNCATE
19269 && (/* Glyph doesn't fit on the line. */
19270 new_x > it->last_visible_x
19271 /* Or it fits exactly on a window system frame. */
19272 || (new_x == it->last_visible_x
19273 && FRAME_WINDOW_P (it->f))))
19275 /* End of a continued line. */
19277 if (it->hpos == 0
19278 || (new_x == it->last_visible_x
19279 && FRAME_WINDOW_P (it->f)))
19281 /* Current glyph is the only one on the line or
19282 fits exactly on the line. We must continue
19283 the line because we can't draw the cursor
19284 after the glyph. */
19285 row->continued_p = 1;
19286 it->current_x = new_x;
19287 it->continuation_lines_width += new_x;
19288 ++it->hpos;
19289 if (i == nglyphs - 1)
19291 /* If line-wrap is on, check if a previous
19292 wrap point was found. */
19293 if (wrap_row_used > 0
19294 /* Even if there is a previous wrap
19295 point, continue the line here as
19296 usual, if (i) the previous character
19297 was a space or tab AND (ii) the
19298 current character is not. */
19299 && (!may_wrap
19300 || IT_DISPLAYING_WHITESPACE (it)))
19301 goto back_to_wrap;
19303 /* Record the maximum and minimum buffer
19304 positions seen so far in glyphs that will be
19305 displayed by this row. */
19306 if (it->bidi_p)
19307 RECORD_MAX_MIN_POS (it);
19308 set_iterator_to_next (it, 1);
19309 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19311 if (!get_next_display_element (it))
19313 row->exact_window_width_line_p = 1;
19314 it->continuation_lines_width = 0;
19315 row->continued_p = 0;
19316 row->ends_at_zv_p = 1;
19318 else if (ITERATOR_AT_END_OF_LINE_P (it))
19320 row->continued_p = 0;
19321 row->exact_window_width_line_p = 1;
19325 else if (it->bidi_p)
19326 RECORD_MAX_MIN_POS (it);
19328 else if (CHAR_GLYPH_PADDING_P (*glyph)
19329 && !FRAME_WINDOW_P (it->f))
19331 /* A padding glyph that doesn't fit on this line.
19332 This means the whole character doesn't fit
19333 on the line. */
19334 if (row->reversed_p)
19335 unproduce_glyphs (it, row->used[TEXT_AREA]
19336 - n_glyphs_before);
19337 row->used[TEXT_AREA] = n_glyphs_before;
19339 /* Fill the rest of the row with continuation
19340 glyphs like in 20.x. */
19341 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19342 < row->glyphs[1 + TEXT_AREA])
19343 produce_special_glyphs (it, IT_CONTINUATION);
19345 row->continued_p = 1;
19346 it->current_x = x_before;
19347 it->continuation_lines_width += x_before;
19349 /* Restore the height to what it was before the
19350 element not fitting on the line. */
19351 it->max_ascent = ascent;
19352 it->max_descent = descent;
19353 it->max_phys_ascent = phys_ascent;
19354 it->max_phys_descent = phys_descent;
19356 else if (wrap_row_used > 0)
19358 back_to_wrap:
19359 if (row->reversed_p)
19360 unproduce_glyphs (it,
19361 row->used[TEXT_AREA] - wrap_row_used);
19362 RESTORE_IT (it, &wrap_it, wrap_data);
19363 it->continuation_lines_width += wrap_x;
19364 row->used[TEXT_AREA] = wrap_row_used;
19365 row->ascent = wrap_row_ascent;
19366 row->height = wrap_row_height;
19367 row->phys_ascent = wrap_row_phys_ascent;
19368 row->phys_height = wrap_row_phys_height;
19369 row->extra_line_spacing = wrap_row_extra_line_spacing;
19370 min_pos = wrap_row_min_pos;
19371 min_bpos = wrap_row_min_bpos;
19372 max_pos = wrap_row_max_pos;
19373 max_bpos = wrap_row_max_bpos;
19374 row->continued_p = 1;
19375 row->ends_at_zv_p = 0;
19376 row->exact_window_width_line_p = 0;
19377 it->continuation_lines_width += x;
19379 /* Make sure that a non-default face is extended
19380 up to the right margin of the window. */
19381 extend_face_to_end_of_line (it);
19383 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19385 /* A TAB that extends past the right edge of the
19386 window. This produces a single glyph on
19387 window system frames. We leave the glyph in
19388 this row and let it fill the row, but don't
19389 consume the TAB. */
19390 it->continuation_lines_width += it->last_visible_x;
19391 row->ends_in_middle_of_char_p = 1;
19392 row->continued_p = 1;
19393 glyph->pixel_width = it->last_visible_x - x;
19394 it->starts_in_middle_of_char_p = 1;
19396 else
19398 /* Something other than a TAB that draws past
19399 the right edge of the window. Restore
19400 positions to values before the element. */
19401 if (row->reversed_p)
19402 unproduce_glyphs (it, row->used[TEXT_AREA]
19403 - (n_glyphs_before + i));
19404 row->used[TEXT_AREA] = n_glyphs_before + i;
19406 /* Display continuation glyphs. */
19407 if (!FRAME_WINDOW_P (it->f))
19408 produce_special_glyphs (it, IT_CONTINUATION);
19409 row->continued_p = 1;
19411 it->current_x = x_before;
19412 it->continuation_lines_width += x;
19413 extend_face_to_end_of_line (it);
19415 if (nglyphs > 1 && i > 0)
19417 row->ends_in_middle_of_char_p = 1;
19418 it->starts_in_middle_of_char_p = 1;
19421 /* Restore the height to what it was before the
19422 element not fitting on the line. */
19423 it->max_ascent = ascent;
19424 it->max_descent = descent;
19425 it->max_phys_ascent = phys_ascent;
19426 it->max_phys_descent = phys_descent;
19429 break;
19431 else if (new_x > it->first_visible_x)
19433 /* Increment number of glyphs actually displayed. */
19434 ++it->hpos;
19436 /* Record the maximum and minimum buffer positions
19437 seen so far in glyphs that will be displayed by
19438 this row. */
19439 if (it->bidi_p)
19440 RECORD_MAX_MIN_POS (it);
19442 if (x < it->first_visible_x)
19443 /* Glyph is partially visible, i.e. row starts at
19444 negative X position. */
19445 row->x = x - it->first_visible_x;
19447 else
19449 /* Glyph is completely off the left margin of the
19450 window. This should not happen because of the
19451 move_it_in_display_line at the start of this
19452 function, unless the text display area of the
19453 window is empty. */
19454 xassert (it->first_visible_x <= it->last_visible_x);
19457 /* Even if this display element produced no glyphs at all,
19458 we want to record its position. */
19459 if (it->bidi_p && nglyphs == 0)
19460 RECORD_MAX_MIN_POS (it);
19462 row->ascent = max (row->ascent, it->max_ascent);
19463 row->height = max (row->height, it->max_ascent + it->max_descent);
19464 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19465 row->phys_height = max (row->phys_height,
19466 it->max_phys_ascent + it->max_phys_descent);
19467 row->extra_line_spacing = max (row->extra_line_spacing,
19468 it->max_extra_line_spacing);
19470 /* End of this display line if row is continued. */
19471 if (row->continued_p || row->ends_at_zv_p)
19472 break;
19475 at_end_of_line:
19476 /* Is this a line end? If yes, we're also done, after making
19477 sure that a non-default face is extended up to the right
19478 margin of the window. */
19479 if (ITERATOR_AT_END_OF_LINE_P (it))
19481 int used_before = row->used[TEXT_AREA];
19483 row->ends_in_newline_from_string_p = STRINGP (it->object);
19485 /* Add a space at the end of the line that is used to
19486 display the cursor there. */
19487 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19488 append_space_for_newline (it, 0);
19490 /* Extend the face to the end of the line. */
19491 extend_face_to_end_of_line (it);
19493 /* Make sure we have the position. */
19494 if (used_before == 0)
19495 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19497 /* Record the position of the newline, for use in
19498 find_row_edges. */
19499 it->eol_pos = it->current.pos;
19501 /* Consume the line end. This skips over invisible lines. */
19502 set_iterator_to_next (it, 1);
19503 it->continuation_lines_width = 0;
19504 break;
19507 /* Proceed with next display element. Note that this skips
19508 over lines invisible because of selective display. */
19509 set_iterator_to_next (it, 1);
19511 /* If we truncate lines, we are done when the last displayed
19512 glyphs reach past the right margin of the window. */
19513 if (it->line_wrap == TRUNCATE
19514 && (FRAME_WINDOW_P (it->f)
19515 ? (it->current_x >= it->last_visible_x)
19516 : (it->current_x > it->last_visible_x)))
19518 /* Maybe add truncation glyphs. */
19519 if (!FRAME_WINDOW_P (it->f))
19521 int i, n;
19523 if (!row->reversed_p)
19525 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19526 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19527 break;
19529 else
19531 for (i = 0; i < row->used[TEXT_AREA]; i++)
19532 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19533 break;
19534 /* Remove any padding glyphs at the front of ROW, to
19535 make room for the truncation glyphs we will be
19536 adding below. The loop below always inserts at
19537 least one truncation glyph, so also remove the
19538 last glyph added to ROW. */
19539 unproduce_glyphs (it, i + 1);
19540 /* Adjust i for the loop below. */
19541 i = row->used[TEXT_AREA] - (i + 1);
19544 for (n = row->used[TEXT_AREA]; i < n; ++i)
19546 row->used[TEXT_AREA] = i;
19547 produce_special_glyphs (it, IT_TRUNCATION);
19550 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19552 /* Don't truncate if we can overflow newline into fringe. */
19553 if (!get_next_display_element (it))
19555 it->continuation_lines_width = 0;
19556 row->ends_at_zv_p = 1;
19557 row->exact_window_width_line_p = 1;
19558 break;
19560 if (ITERATOR_AT_END_OF_LINE_P (it))
19562 row->exact_window_width_line_p = 1;
19563 goto at_end_of_line;
19567 row->truncated_on_right_p = 1;
19568 it->continuation_lines_width = 0;
19569 reseat_at_next_visible_line_start (it, 0);
19570 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19571 it->hpos = hpos_before;
19572 it->current_x = x_before;
19573 break;
19577 if (wrap_data)
19578 bidi_unshelve_cache (wrap_data, 1);
19580 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19581 at the left window margin. */
19582 if (it->first_visible_x
19583 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19585 if (!FRAME_WINDOW_P (it->f))
19586 insert_left_trunc_glyphs (it);
19587 row->truncated_on_left_p = 1;
19590 /* Remember the position at which this line ends.
19592 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19593 cannot be before the call to find_row_edges below, since that is
19594 where these positions are determined. */
19595 row->end = it->current;
19596 if (!it->bidi_p)
19598 row->minpos = row->start.pos;
19599 row->maxpos = row->end.pos;
19601 else
19603 /* ROW->minpos and ROW->maxpos must be the smallest and
19604 `1 + the largest' buffer positions in ROW. But if ROW was
19605 bidi-reordered, these two positions can be anywhere in the
19606 row, so we must determine them now. */
19607 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19610 /* If the start of this line is the overlay arrow-position, then
19611 mark this glyph row as the one containing the overlay arrow.
19612 This is clearly a mess with variable size fonts. It would be
19613 better to let it be displayed like cursors under X. */
19614 if ((row->displays_text_p || !overlay_arrow_seen)
19615 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19616 !NILP (overlay_arrow_string)))
19618 /* Overlay arrow in window redisplay is a fringe bitmap. */
19619 if (STRINGP (overlay_arrow_string))
19621 struct glyph_row *arrow_row
19622 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19623 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19624 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19625 struct glyph *p = row->glyphs[TEXT_AREA];
19626 struct glyph *p2, *end;
19628 /* Copy the arrow glyphs. */
19629 while (glyph < arrow_end)
19630 *p++ = *glyph++;
19632 /* Throw away padding glyphs. */
19633 p2 = p;
19634 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19635 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19636 ++p2;
19637 if (p2 > p)
19639 while (p2 < end)
19640 *p++ = *p2++;
19641 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19644 else
19646 xassert (INTEGERP (overlay_arrow_string));
19647 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19649 overlay_arrow_seen = 1;
19652 /* Highlight trailing whitespace. */
19653 if (!NILP (Vshow_trailing_whitespace))
19654 highlight_trailing_whitespace (it->f, it->glyph_row);
19656 /* Compute pixel dimensions of this line. */
19657 compute_line_metrics (it);
19659 /* Implementation note: No changes in the glyphs of ROW or in their
19660 faces can be done past this point, because compute_line_metrics
19661 computes ROW's hash value and stores it within the glyph_row
19662 structure. */
19664 /* Record whether this row ends inside an ellipsis. */
19665 row->ends_in_ellipsis_p
19666 = (it->method == GET_FROM_DISPLAY_VECTOR
19667 && it->ellipsis_p);
19669 /* Save fringe bitmaps in this row. */
19670 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19671 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19672 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19673 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19675 it->left_user_fringe_bitmap = 0;
19676 it->left_user_fringe_face_id = 0;
19677 it->right_user_fringe_bitmap = 0;
19678 it->right_user_fringe_face_id = 0;
19680 /* Maybe set the cursor. */
19681 cvpos = it->w->cursor.vpos;
19682 if ((cvpos < 0
19683 /* In bidi-reordered rows, keep checking for proper cursor
19684 position even if one has been found already, because buffer
19685 positions in such rows change non-linearly with ROW->VPOS,
19686 when a line is continued. One exception: when we are at ZV,
19687 display cursor on the first suitable glyph row, since all
19688 the empty rows after that also have their position set to ZV. */
19689 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19690 lines' rows is implemented for bidi-reordered rows. */
19691 || (it->bidi_p
19692 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19693 && PT >= MATRIX_ROW_START_CHARPOS (row)
19694 && PT <= MATRIX_ROW_END_CHARPOS (row)
19695 && cursor_row_p (row))
19696 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19698 /* Prepare for the next line. This line starts horizontally at (X
19699 HPOS) = (0 0). Vertical positions are incremented. As a
19700 convenience for the caller, IT->glyph_row is set to the next
19701 row to be used. */
19702 it->current_x = it->hpos = 0;
19703 it->current_y += row->height;
19704 SET_TEXT_POS (it->eol_pos, 0, 0);
19705 ++it->vpos;
19706 ++it->glyph_row;
19707 /* The next row should by default use the same value of the
19708 reversed_p flag as this one. set_iterator_to_next decides when
19709 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19710 the flag accordingly. */
19711 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19712 it->glyph_row->reversed_p = row->reversed_p;
19713 it->start = row->end;
19714 return row->displays_text_p;
19716 #undef RECORD_MAX_MIN_POS
19719 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19720 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19721 doc: /* Return paragraph direction at point in BUFFER.
19722 Value is either `left-to-right' or `right-to-left'.
19723 If BUFFER is omitted or nil, it defaults to the current buffer.
19725 Paragraph direction determines how the text in the paragraph is displayed.
19726 In left-to-right paragraphs, text begins at the left margin of the window
19727 and the reading direction is generally left to right. In right-to-left
19728 paragraphs, text begins at the right margin and is read from right to left.
19730 See also `bidi-paragraph-direction'. */)
19731 (Lisp_Object buffer)
19733 struct buffer *buf = current_buffer;
19734 struct buffer *old = buf;
19736 if (! NILP (buffer))
19738 CHECK_BUFFER (buffer);
19739 buf = XBUFFER (buffer);
19742 if (NILP (BVAR (buf, bidi_display_reordering))
19743 || NILP (BVAR (buf, enable_multibyte_characters))
19744 /* When we are loading loadup.el, the character property tables
19745 needed for bidi iteration are not yet available. */
19746 || !NILP (Vpurify_flag))
19747 return Qleft_to_right;
19748 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19749 return BVAR (buf, bidi_paragraph_direction);
19750 else
19752 /* Determine the direction from buffer text. We could try to
19753 use current_matrix if it is up to date, but this seems fast
19754 enough as it is. */
19755 struct bidi_it itb;
19756 EMACS_INT pos = BUF_PT (buf);
19757 EMACS_INT bytepos = BUF_PT_BYTE (buf);
19758 int c;
19759 void *itb_data = bidi_shelve_cache ();
19761 set_buffer_temp (buf);
19762 /* bidi_paragraph_init finds the base direction of the paragraph
19763 by searching forward from paragraph start. We need the base
19764 direction of the current or _previous_ paragraph, so we need
19765 to make sure we are within that paragraph. To that end, find
19766 the previous non-empty line. */
19767 if (pos >= ZV && pos > BEGV)
19769 pos--;
19770 bytepos = CHAR_TO_BYTE (pos);
19772 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19773 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19775 while ((c = FETCH_BYTE (bytepos)) == '\n'
19776 || c == ' ' || c == '\t' || c == '\f')
19778 if (bytepos <= BEGV_BYTE)
19779 break;
19780 bytepos--;
19781 pos--;
19783 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19784 bytepos--;
19786 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19787 itb.paragraph_dir = NEUTRAL_DIR;
19788 itb.string.s = NULL;
19789 itb.string.lstring = Qnil;
19790 itb.string.bufpos = 0;
19791 itb.string.unibyte = 0;
19792 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19793 bidi_unshelve_cache (itb_data, 0);
19794 set_buffer_temp (old);
19795 switch (itb.paragraph_dir)
19797 case L2R:
19798 return Qleft_to_right;
19799 break;
19800 case R2L:
19801 return Qright_to_left;
19802 break;
19803 default:
19804 abort ();
19811 /***********************************************************************
19812 Menu Bar
19813 ***********************************************************************/
19815 /* Redisplay the menu bar in the frame for window W.
19817 The menu bar of X frames that don't have X toolkit support is
19818 displayed in a special window W->frame->menu_bar_window.
19820 The menu bar of terminal frames is treated specially as far as
19821 glyph matrices are concerned. Menu bar lines are not part of
19822 windows, so the update is done directly on the frame matrix rows
19823 for the menu bar. */
19825 static void
19826 display_menu_bar (struct window *w)
19828 struct frame *f = XFRAME (WINDOW_FRAME (w));
19829 struct it it;
19830 Lisp_Object items;
19831 int i;
19833 /* Don't do all this for graphical frames. */
19834 #ifdef HAVE_NTGUI
19835 if (FRAME_W32_P (f))
19836 return;
19837 #endif
19838 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
19839 if (FRAME_X_P (f))
19840 return;
19841 #endif
19843 #ifdef HAVE_NS
19844 if (FRAME_NS_P (f))
19845 return;
19846 #endif /* HAVE_NS */
19848 #ifdef USE_X_TOOLKIT
19849 xassert (!FRAME_WINDOW_P (f));
19850 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
19851 it.first_visible_x = 0;
19852 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19853 #else /* not USE_X_TOOLKIT */
19854 if (FRAME_WINDOW_P (f))
19856 /* Menu bar lines are displayed in the desired matrix of the
19857 dummy window menu_bar_window. */
19858 struct window *menu_w;
19859 xassert (WINDOWP (f->menu_bar_window));
19860 menu_w = XWINDOW (f->menu_bar_window);
19861 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
19862 MENU_FACE_ID);
19863 it.first_visible_x = 0;
19864 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19866 else
19868 /* This is a TTY frame, i.e. character hpos/vpos are used as
19869 pixel x/y. */
19870 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
19871 MENU_FACE_ID);
19872 it.first_visible_x = 0;
19873 it.last_visible_x = FRAME_COLS (f);
19875 #endif /* not USE_X_TOOLKIT */
19877 /* FIXME: This should be controlled by a user option. See the
19878 comments in redisplay_tool_bar and display_mode_line about
19879 this. */
19880 it.paragraph_embedding = L2R;
19882 if (! mode_line_inverse_video)
19883 /* Force the menu-bar to be displayed in the default face. */
19884 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19886 /* Clear all rows of the menu bar. */
19887 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
19889 struct glyph_row *row = it.glyph_row + i;
19890 clear_glyph_row (row);
19891 row->enabled_p = 1;
19892 row->full_width_p = 1;
19895 /* Display all items of the menu bar. */
19896 items = FRAME_MENU_BAR_ITEMS (it.f);
19897 for (i = 0; i < ASIZE (items); i += 4)
19899 Lisp_Object string;
19901 /* Stop at nil string. */
19902 string = AREF (items, i + 1);
19903 if (NILP (string))
19904 break;
19906 /* Remember where item was displayed. */
19907 ASET (items, i + 3, make_number (it.hpos));
19909 /* Display the item, pad with one space. */
19910 if (it.current_x < it.last_visible_x)
19911 display_string (NULL, string, Qnil, 0, 0, &it,
19912 SCHARS (string) + 1, 0, 0, -1);
19915 /* Fill out the line with spaces. */
19916 if (it.current_x < it.last_visible_x)
19917 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
19919 /* Compute the total height of the lines. */
19920 compute_line_metrics (&it);
19925 /***********************************************************************
19926 Mode Line
19927 ***********************************************************************/
19929 /* Redisplay mode lines in the window tree whose root is WINDOW. If
19930 FORCE is non-zero, redisplay mode lines unconditionally.
19931 Otherwise, redisplay only mode lines that are garbaged. Value is
19932 the number of windows whose mode lines were redisplayed. */
19934 static int
19935 redisplay_mode_lines (Lisp_Object window, int force)
19937 int nwindows = 0;
19939 while (!NILP (window))
19941 struct window *w = XWINDOW (window);
19943 if (WINDOWP (w->hchild))
19944 nwindows += redisplay_mode_lines (w->hchild, force);
19945 else if (WINDOWP (w->vchild))
19946 nwindows += redisplay_mode_lines (w->vchild, force);
19947 else if (force
19948 || FRAME_GARBAGED_P (XFRAME (w->frame))
19949 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
19951 struct text_pos lpoint;
19952 struct buffer *old = current_buffer;
19954 /* Set the window's buffer for the mode line display. */
19955 SET_TEXT_POS (lpoint, PT, PT_BYTE);
19956 set_buffer_internal_1 (XBUFFER (w->buffer));
19958 /* Point refers normally to the selected window. For any
19959 other window, set up appropriate value. */
19960 if (!EQ (window, selected_window))
19962 struct text_pos pt;
19964 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
19965 if (CHARPOS (pt) < BEGV)
19966 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
19967 else if (CHARPOS (pt) > (ZV - 1))
19968 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
19969 else
19970 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
19973 /* Display mode lines. */
19974 clear_glyph_matrix (w->desired_matrix);
19975 if (display_mode_lines (w))
19977 ++nwindows;
19978 w->must_be_updated_p = 1;
19981 /* Restore old settings. */
19982 set_buffer_internal_1 (old);
19983 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
19986 window = w->next;
19989 return nwindows;
19993 /* Display the mode and/or header line of window W. Value is the
19994 sum number of mode lines and header lines displayed. */
19996 static int
19997 display_mode_lines (struct window *w)
19999 Lisp_Object old_selected_window, old_selected_frame;
20000 int n = 0;
20002 old_selected_frame = selected_frame;
20003 selected_frame = w->frame;
20004 old_selected_window = selected_window;
20005 XSETWINDOW (selected_window, w);
20007 /* These will be set while the mode line specs are processed. */
20008 line_number_displayed = 0;
20009 w->column_number_displayed = Qnil;
20011 if (WINDOW_WANTS_MODELINE_P (w))
20013 struct window *sel_w = XWINDOW (old_selected_window);
20015 /* Select mode line face based on the real selected window. */
20016 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20017 BVAR (current_buffer, mode_line_format));
20018 ++n;
20021 if (WINDOW_WANTS_HEADER_LINE_P (w))
20023 display_mode_line (w, HEADER_LINE_FACE_ID,
20024 BVAR (current_buffer, header_line_format));
20025 ++n;
20028 selected_frame = old_selected_frame;
20029 selected_window = old_selected_window;
20030 return n;
20034 /* Display mode or header line of window W. FACE_ID specifies which
20035 line to display; it is either MODE_LINE_FACE_ID or
20036 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20037 display. Value is the pixel height of the mode/header line
20038 displayed. */
20040 static int
20041 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20043 struct it it;
20044 struct face *face;
20045 int count = SPECPDL_INDEX ();
20047 init_iterator (&it, w, -1, -1, NULL, face_id);
20048 /* Don't extend on a previously drawn mode-line.
20049 This may happen if called from pos_visible_p. */
20050 it.glyph_row->enabled_p = 0;
20051 prepare_desired_row (it.glyph_row);
20053 it.glyph_row->mode_line_p = 1;
20055 if (! mode_line_inverse_video)
20056 /* Force the mode-line to be displayed in the default face. */
20057 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20059 /* FIXME: This should be controlled by a user option. But
20060 supporting such an option is not trivial, since the mode line is
20061 made up of many separate strings. */
20062 it.paragraph_embedding = L2R;
20064 record_unwind_protect (unwind_format_mode_line,
20065 format_mode_line_unwind_data (NULL, Qnil, 0));
20067 mode_line_target = MODE_LINE_DISPLAY;
20069 /* Temporarily make frame's keyboard the current kboard so that
20070 kboard-local variables in the mode_line_format will get the right
20071 values. */
20072 push_kboard (FRAME_KBOARD (it.f));
20073 record_unwind_save_match_data ();
20074 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20075 pop_kboard ();
20077 unbind_to (count, Qnil);
20079 /* Fill up with spaces. */
20080 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20082 compute_line_metrics (&it);
20083 it.glyph_row->full_width_p = 1;
20084 it.glyph_row->continued_p = 0;
20085 it.glyph_row->truncated_on_left_p = 0;
20086 it.glyph_row->truncated_on_right_p = 0;
20088 /* Make a 3D mode-line have a shadow at its right end. */
20089 face = FACE_FROM_ID (it.f, face_id);
20090 extend_face_to_end_of_line (&it);
20091 if (face->box != FACE_NO_BOX)
20093 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20094 + it.glyph_row->used[TEXT_AREA] - 1);
20095 last->right_box_line_p = 1;
20098 return it.glyph_row->height;
20101 /* Move element ELT in LIST to the front of LIST.
20102 Return the updated list. */
20104 static Lisp_Object
20105 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20107 register Lisp_Object tail, prev;
20108 register Lisp_Object tem;
20110 tail = list;
20111 prev = Qnil;
20112 while (CONSP (tail))
20114 tem = XCAR (tail);
20116 if (EQ (elt, tem))
20118 /* Splice out the link TAIL. */
20119 if (NILP (prev))
20120 list = XCDR (tail);
20121 else
20122 Fsetcdr (prev, XCDR (tail));
20124 /* Now make it the first. */
20125 Fsetcdr (tail, list);
20126 return tail;
20128 else
20129 prev = tail;
20130 tail = XCDR (tail);
20131 QUIT;
20134 /* Not found--return unchanged LIST. */
20135 return list;
20138 /* Contribute ELT to the mode line for window IT->w. How it
20139 translates into text depends on its data type.
20141 IT describes the display environment in which we display, as usual.
20143 DEPTH is the depth in recursion. It is used to prevent
20144 infinite recursion here.
20146 FIELD_WIDTH is the number of characters the display of ELT should
20147 occupy in the mode line, and PRECISION is the maximum number of
20148 characters to display from ELT's representation. See
20149 display_string for details.
20151 Returns the hpos of the end of the text generated by ELT.
20153 PROPS is a property list to add to any string we encounter.
20155 If RISKY is nonzero, remove (disregard) any properties in any string
20156 we encounter, and ignore :eval and :propertize.
20158 The global variable `mode_line_target' determines whether the
20159 output is passed to `store_mode_line_noprop',
20160 `store_mode_line_string', or `display_string'. */
20162 static int
20163 display_mode_element (struct it *it, int depth, int field_width, int precision,
20164 Lisp_Object elt, Lisp_Object props, int risky)
20166 int n = 0, field, prec;
20167 int literal = 0;
20169 tail_recurse:
20170 if (depth > 100)
20171 elt = build_string ("*too-deep*");
20173 depth++;
20175 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
20177 case Lisp_String:
20179 /* A string: output it and check for %-constructs within it. */
20180 unsigned char c;
20181 EMACS_INT offset = 0;
20183 if (SCHARS (elt) > 0
20184 && (!NILP (props) || risky))
20186 Lisp_Object oprops, aelt;
20187 oprops = Ftext_properties_at (make_number (0), elt);
20189 /* If the starting string's properties are not what
20190 we want, translate the string. Also, if the string
20191 is risky, do that anyway. */
20193 if (NILP (Fequal (props, oprops)) || risky)
20195 /* If the starting string has properties,
20196 merge the specified ones onto the existing ones. */
20197 if (! NILP (oprops) && !risky)
20199 Lisp_Object tem;
20201 oprops = Fcopy_sequence (oprops);
20202 tem = props;
20203 while (CONSP (tem))
20205 oprops = Fplist_put (oprops, XCAR (tem),
20206 XCAR (XCDR (tem)));
20207 tem = XCDR (XCDR (tem));
20209 props = oprops;
20212 aelt = Fassoc (elt, mode_line_proptrans_alist);
20213 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20215 /* AELT is what we want. Move it to the front
20216 without consing. */
20217 elt = XCAR (aelt);
20218 mode_line_proptrans_alist
20219 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20221 else
20223 Lisp_Object tem;
20225 /* If AELT has the wrong props, it is useless.
20226 so get rid of it. */
20227 if (! NILP (aelt))
20228 mode_line_proptrans_alist
20229 = Fdelq (aelt, mode_line_proptrans_alist);
20231 elt = Fcopy_sequence (elt);
20232 Fset_text_properties (make_number (0), Flength (elt),
20233 props, elt);
20234 /* Add this item to mode_line_proptrans_alist. */
20235 mode_line_proptrans_alist
20236 = Fcons (Fcons (elt, props),
20237 mode_line_proptrans_alist);
20238 /* Truncate mode_line_proptrans_alist
20239 to at most 50 elements. */
20240 tem = Fnthcdr (make_number (50),
20241 mode_line_proptrans_alist);
20242 if (! NILP (tem))
20243 XSETCDR (tem, Qnil);
20248 offset = 0;
20250 if (literal)
20252 prec = precision - n;
20253 switch (mode_line_target)
20255 case MODE_LINE_NOPROP:
20256 case MODE_LINE_TITLE:
20257 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20258 break;
20259 case MODE_LINE_STRING:
20260 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20261 break;
20262 case MODE_LINE_DISPLAY:
20263 n += display_string (NULL, elt, Qnil, 0, 0, it,
20264 0, prec, 0, STRING_MULTIBYTE (elt));
20265 break;
20268 break;
20271 /* Handle the non-literal case. */
20273 while ((precision <= 0 || n < precision)
20274 && SREF (elt, offset) != 0
20275 && (mode_line_target != MODE_LINE_DISPLAY
20276 || it->current_x < it->last_visible_x))
20278 EMACS_INT last_offset = offset;
20280 /* Advance to end of string or next format specifier. */
20281 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20284 if (offset - 1 != last_offset)
20286 EMACS_INT nchars, nbytes;
20288 /* Output to end of string or up to '%'. Field width
20289 is length of string. Don't output more than
20290 PRECISION allows us. */
20291 offset--;
20293 prec = c_string_width (SDATA (elt) + last_offset,
20294 offset - last_offset, precision - n,
20295 &nchars, &nbytes);
20297 switch (mode_line_target)
20299 case MODE_LINE_NOPROP:
20300 case MODE_LINE_TITLE:
20301 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20302 break;
20303 case MODE_LINE_STRING:
20305 EMACS_INT bytepos = last_offset;
20306 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
20307 EMACS_INT endpos = (precision <= 0
20308 ? string_byte_to_char (elt, offset)
20309 : charpos + nchars);
20311 n += store_mode_line_string (NULL,
20312 Fsubstring (elt, make_number (charpos),
20313 make_number (endpos)),
20314 0, 0, 0, Qnil);
20316 break;
20317 case MODE_LINE_DISPLAY:
20319 EMACS_INT bytepos = last_offset;
20320 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
20322 if (precision <= 0)
20323 nchars = string_byte_to_char (elt, offset) - charpos;
20324 n += display_string (NULL, elt, Qnil, 0, charpos,
20325 it, 0, nchars, 0,
20326 STRING_MULTIBYTE (elt));
20328 break;
20331 else /* c == '%' */
20333 EMACS_INT percent_position = offset;
20335 /* Get the specified minimum width. Zero means
20336 don't pad. */
20337 field = 0;
20338 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20339 field = field * 10 + c - '0';
20341 /* Don't pad beyond the total padding allowed. */
20342 if (field_width - n > 0 && field > field_width - n)
20343 field = field_width - n;
20345 /* Note that either PRECISION <= 0 or N < PRECISION. */
20346 prec = precision - n;
20348 if (c == 'M')
20349 n += display_mode_element (it, depth, field, prec,
20350 Vglobal_mode_string, props,
20351 risky);
20352 else if (c != 0)
20354 int multibyte;
20355 EMACS_INT bytepos, charpos;
20356 const char *spec;
20357 Lisp_Object string;
20359 bytepos = percent_position;
20360 charpos = (STRING_MULTIBYTE (elt)
20361 ? string_byte_to_char (elt, bytepos)
20362 : bytepos);
20363 spec = decode_mode_spec (it->w, c, field, &string);
20364 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20366 switch (mode_line_target)
20368 case MODE_LINE_NOPROP:
20369 case MODE_LINE_TITLE:
20370 n += store_mode_line_noprop (spec, field, prec);
20371 break;
20372 case MODE_LINE_STRING:
20374 Lisp_Object tem = build_string (spec);
20375 props = Ftext_properties_at (make_number (charpos), elt);
20376 /* Should only keep face property in props */
20377 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20379 break;
20380 case MODE_LINE_DISPLAY:
20382 int nglyphs_before, nwritten;
20384 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20385 nwritten = display_string (spec, string, elt,
20386 charpos, 0, it,
20387 field, prec, 0,
20388 multibyte);
20390 /* Assign to the glyphs written above the
20391 string where the `%x' came from, position
20392 of the `%'. */
20393 if (nwritten > 0)
20395 struct glyph *glyph
20396 = (it->glyph_row->glyphs[TEXT_AREA]
20397 + nglyphs_before);
20398 int i;
20400 for (i = 0; i < nwritten; ++i)
20402 glyph[i].object = elt;
20403 glyph[i].charpos = charpos;
20406 n += nwritten;
20409 break;
20412 else /* c == 0 */
20413 break;
20417 break;
20419 case Lisp_Symbol:
20420 /* A symbol: process the value of the symbol recursively
20421 as if it appeared here directly. Avoid error if symbol void.
20422 Special case: if value of symbol is a string, output the string
20423 literally. */
20425 register Lisp_Object tem;
20427 /* If the variable is not marked as risky to set
20428 then its contents are risky to use. */
20429 if (NILP (Fget (elt, Qrisky_local_variable)))
20430 risky = 1;
20432 tem = Fboundp (elt);
20433 if (!NILP (tem))
20435 tem = Fsymbol_value (elt);
20436 /* If value is a string, output that string literally:
20437 don't check for % within it. */
20438 if (STRINGP (tem))
20439 literal = 1;
20441 if (!EQ (tem, elt))
20443 /* Give up right away for nil or t. */
20444 elt = tem;
20445 goto tail_recurse;
20449 break;
20451 case Lisp_Cons:
20453 register Lisp_Object car, tem;
20455 /* A cons cell: five distinct cases.
20456 If first element is :eval or :propertize, do something special.
20457 If first element is a string or a cons, process all the elements
20458 and effectively concatenate them.
20459 If first element is a negative number, truncate displaying cdr to
20460 at most that many characters. If positive, pad (with spaces)
20461 to at least that many characters.
20462 If first element is a symbol, process the cadr or caddr recursively
20463 according to whether the symbol's value is non-nil or nil. */
20464 car = XCAR (elt);
20465 if (EQ (car, QCeval))
20467 /* An element of the form (:eval FORM) means evaluate FORM
20468 and use the result as mode line elements. */
20470 if (risky)
20471 break;
20473 if (CONSP (XCDR (elt)))
20475 Lisp_Object spec;
20476 spec = safe_eval (XCAR (XCDR (elt)));
20477 n += display_mode_element (it, depth, field_width - n,
20478 precision - n, spec, props,
20479 risky);
20482 else if (EQ (car, QCpropertize))
20484 /* An element of the form (:propertize ELT PROPS...)
20485 means display ELT but applying properties PROPS. */
20487 if (risky)
20488 break;
20490 if (CONSP (XCDR (elt)))
20491 n += display_mode_element (it, depth, field_width - n,
20492 precision - n, XCAR (XCDR (elt)),
20493 XCDR (XCDR (elt)), risky);
20495 else if (SYMBOLP (car))
20497 tem = Fboundp (car);
20498 elt = XCDR (elt);
20499 if (!CONSP (elt))
20500 goto invalid;
20501 /* elt is now the cdr, and we know it is a cons cell.
20502 Use its car if CAR has a non-nil value. */
20503 if (!NILP (tem))
20505 tem = Fsymbol_value (car);
20506 if (!NILP (tem))
20508 elt = XCAR (elt);
20509 goto tail_recurse;
20512 /* Symbol's value is nil (or symbol is unbound)
20513 Get the cddr of the original list
20514 and if possible find the caddr and use that. */
20515 elt = XCDR (elt);
20516 if (NILP (elt))
20517 break;
20518 else if (!CONSP (elt))
20519 goto invalid;
20520 elt = XCAR (elt);
20521 goto tail_recurse;
20523 else if (INTEGERP (car))
20525 register int lim = XINT (car);
20526 elt = XCDR (elt);
20527 if (lim < 0)
20529 /* Negative int means reduce maximum width. */
20530 if (precision <= 0)
20531 precision = -lim;
20532 else
20533 precision = min (precision, -lim);
20535 else if (lim > 0)
20537 /* Padding specified. Don't let it be more than
20538 current maximum. */
20539 if (precision > 0)
20540 lim = min (precision, lim);
20542 /* If that's more padding than already wanted, queue it.
20543 But don't reduce padding already specified even if
20544 that is beyond the current truncation point. */
20545 field_width = max (lim, field_width);
20547 goto tail_recurse;
20549 else if (STRINGP (car) || CONSP (car))
20551 Lisp_Object halftail = elt;
20552 int len = 0;
20554 while (CONSP (elt)
20555 && (precision <= 0 || n < precision))
20557 n += display_mode_element (it, depth,
20558 /* Do padding only after the last
20559 element in the list. */
20560 (! CONSP (XCDR (elt))
20561 ? field_width - n
20562 : 0),
20563 precision - n, XCAR (elt),
20564 props, risky);
20565 elt = XCDR (elt);
20566 len++;
20567 if ((len & 1) == 0)
20568 halftail = XCDR (halftail);
20569 /* Check for cycle. */
20570 if (EQ (halftail, elt))
20571 break;
20575 break;
20577 default:
20578 invalid:
20579 elt = build_string ("*invalid*");
20580 goto tail_recurse;
20583 /* Pad to FIELD_WIDTH. */
20584 if (field_width > 0 && n < field_width)
20586 switch (mode_line_target)
20588 case MODE_LINE_NOPROP:
20589 case MODE_LINE_TITLE:
20590 n += store_mode_line_noprop ("", field_width - n, 0);
20591 break;
20592 case MODE_LINE_STRING:
20593 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20594 break;
20595 case MODE_LINE_DISPLAY:
20596 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20597 0, 0, 0);
20598 break;
20602 return n;
20605 /* Store a mode-line string element in mode_line_string_list.
20607 If STRING is non-null, display that C string. Otherwise, the Lisp
20608 string LISP_STRING is displayed.
20610 FIELD_WIDTH is the minimum number of output glyphs to produce.
20611 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20612 with spaces. FIELD_WIDTH <= 0 means don't pad.
20614 PRECISION is the maximum number of characters to output from
20615 STRING. PRECISION <= 0 means don't truncate the string.
20617 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20618 properties to the string.
20620 PROPS are the properties to add to the string.
20621 The mode_line_string_face face property is always added to the string.
20624 static int
20625 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20626 int field_width, int precision, Lisp_Object props)
20628 EMACS_INT len;
20629 int n = 0;
20631 if (string != NULL)
20633 len = strlen (string);
20634 if (precision > 0 && len > precision)
20635 len = precision;
20636 lisp_string = make_string (string, len);
20637 if (NILP (props))
20638 props = mode_line_string_face_prop;
20639 else if (!NILP (mode_line_string_face))
20641 Lisp_Object face = Fplist_get (props, Qface);
20642 props = Fcopy_sequence (props);
20643 if (NILP (face))
20644 face = mode_line_string_face;
20645 else
20646 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20647 props = Fplist_put (props, Qface, face);
20649 Fadd_text_properties (make_number (0), make_number (len),
20650 props, lisp_string);
20652 else
20654 len = XFASTINT (Flength (lisp_string));
20655 if (precision > 0 && len > precision)
20657 len = precision;
20658 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20659 precision = -1;
20661 if (!NILP (mode_line_string_face))
20663 Lisp_Object face;
20664 if (NILP (props))
20665 props = Ftext_properties_at (make_number (0), lisp_string);
20666 face = Fplist_get (props, Qface);
20667 if (NILP (face))
20668 face = mode_line_string_face;
20669 else
20670 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20671 props = Fcons (Qface, Fcons (face, Qnil));
20672 if (copy_string)
20673 lisp_string = Fcopy_sequence (lisp_string);
20675 if (!NILP (props))
20676 Fadd_text_properties (make_number (0), make_number (len),
20677 props, lisp_string);
20680 if (len > 0)
20682 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20683 n += len;
20686 if (field_width > len)
20688 field_width -= len;
20689 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20690 if (!NILP (props))
20691 Fadd_text_properties (make_number (0), make_number (field_width),
20692 props, lisp_string);
20693 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20694 n += field_width;
20697 return n;
20701 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20702 1, 4, 0,
20703 doc: /* Format a string out of a mode line format specification.
20704 First arg FORMAT specifies the mode line format (see `mode-line-format'
20705 for details) to use.
20707 By default, the format is evaluated for the currently selected window.
20709 Optional second arg FACE specifies the face property to put on all
20710 characters for which no face is specified. The value nil means the
20711 default face. The value t means whatever face the window's mode line
20712 currently uses (either `mode-line' or `mode-line-inactive',
20713 depending on whether the window is the selected window or not).
20714 An integer value means the value string has no text
20715 properties.
20717 Optional third and fourth args WINDOW and BUFFER specify the window
20718 and buffer to use as the context for the formatting (defaults
20719 are the selected window and the WINDOW's buffer). */)
20720 (Lisp_Object format, Lisp_Object face,
20721 Lisp_Object window, Lisp_Object buffer)
20723 struct it it;
20724 int len;
20725 struct window *w;
20726 struct buffer *old_buffer = NULL;
20727 int face_id;
20728 int no_props = INTEGERP (face);
20729 int count = SPECPDL_INDEX ();
20730 Lisp_Object str;
20731 int string_start = 0;
20733 if (NILP (window))
20734 window = selected_window;
20735 CHECK_WINDOW (window);
20736 w = XWINDOW (window);
20738 if (NILP (buffer))
20739 buffer = w->buffer;
20740 CHECK_BUFFER (buffer);
20742 /* Make formatting the modeline a non-op when noninteractive, otherwise
20743 there will be problems later caused by a partially initialized frame. */
20744 if (NILP (format) || noninteractive)
20745 return empty_unibyte_string;
20747 if (no_props)
20748 face = Qnil;
20750 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20751 : EQ (face, Qt) ? (EQ (window, selected_window)
20752 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20753 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20754 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20755 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20756 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20757 : DEFAULT_FACE_ID;
20759 if (XBUFFER (buffer) != current_buffer)
20760 old_buffer = current_buffer;
20762 /* Save things including mode_line_proptrans_alist,
20763 and set that to nil so that we don't alter the outer value. */
20764 record_unwind_protect (unwind_format_mode_line,
20765 format_mode_line_unwind_data
20766 (old_buffer, selected_window, 1));
20767 mode_line_proptrans_alist = Qnil;
20769 Fselect_window (window, Qt);
20770 if (old_buffer)
20771 set_buffer_internal_1 (XBUFFER (buffer));
20773 init_iterator (&it, w, -1, -1, NULL, face_id);
20775 if (no_props)
20777 mode_line_target = MODE_LINE_NOPROP;
20778 mode_line_string_face_prop = Qnil;
20779 mode_line_string_list = Qnil;
20780 string_start = MODE_LINE_NOPROP_LEN (0);
20782 else
20784 mode_line_target = MODE_LINE_STRING;
20785 mode_line_string_list = Qnil;
20786 mode_line_string_face = face;
20787 mode_line_string_face_prop
20788 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20791 push_kboard (FRAME_KBOARD (it.f));
20792 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20793 pop_kboard ();
20795 if (no_props)
20797 len = MODE_LINE_NOPROP_LEN (string_start);
20798 str = make_string (mode_line_noprop_buf + string_start, len);
20800 else
20802 mode_line_string_list = Fnreverse (mode_line_string_list);
20803 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20804 empty_unibyte_string);
20807 unbind_to (count, Qnil);
20808 return str;
20811 /* Write a null-terminated, right justified decimal representation of
20812 the positive integer D to BUF using a minimal field width WIDTH. */
20814 static void
20815 pint2str (register char *buf, register int width, register EMACS_INT d)
20817 register char *p = buf;
20819 if (d <= 0)
20820 *p++ = '0';
20821 else
20823 while (d > 0)
20825 *p++ = d % 10 + '0';
20826 d /= 10;
20830 for (width -= (int) (p - buf); width > 0; --width)
20831 *p++ = ' ';
20832 *p-- = '\0';
20833 while (p > buf)
20835 d = *buf;
20836 *buf++ = *p;
20837 *p-- = d;
20841 /* Write a null-terminated, right justified decimal and "human
20842 readable" representation of the nonnegative integer D to BUF using
20843 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20845 static const char power_letter[] =
20847 0, /* no letter */
20848 'k', /* kilo */
20849 'M', /* mega */
20850 'G', /* giga */
20851 'T', /* tera */
20852 'P', /* peta */
20853 'E', /* exa */
20854 'Z', /* zetta */
20855 'Y' /* yotta */
20858 static void
20859 pint2hrstr (char *buf, int width, EMACS_INT d)
20861 /* We aim to represent the nonnegative integer D as
20862 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
20863 EMACS_INT quotient = d;
20864 int remainder = 0;
20865 /* -1 means: do not use TENTHS. */
20866 int tenths = -1;
20867 int exponent = 0;
20869 /* Length of QUOTIENT.TENTHS as a string. */
20870 int length;
20872 char * psuffix;
20873 char * p;
20875 if (1000 <= quotient)
20877 /* Scale to the appropriate EXPONENT. */
20880 remainder = quotient % 1000;
20881 quotient /= 1000;
20882 exponent++;
20884 while (1000 <= quotient);
20886 /* Round to nearest and decide whether to use TENTHS or not. */
20887 if (quotient <= 9)
20889 tenths = remainder / 100;
20890 if (50 <= remainder % 100)
20892 if (tenths < 9)
20893 tenths++;
20894 else
20896 quotient++;
20897 if (quotient == 10)
20898 tenths = -1;
20899 else
20900 tenths = 0;
20904 else
20905 if (500 <= remainder)
20907 if (quotient < 999)
20908 quotient++;
20909 else
20911 quotient = 1;
20912 exponent++;
20913 tenths = 0;
20918 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
20919 if (tenths == -1 && quotient <= 99)
20920 if (quotient <= 9)
20921 length = 1;
20922 else
20923 length = 2;
20924 else
20925 length = 3;
20926 p = psuffix = buf + max (width, length);
20928 /* Print EXPONENT. */
20929 *psuffix++ = power_letter[exponent];
20930 *psuffix = '\0';
20932 /* Print TENTHS. */
20933 if (tenths >= 0)
20935 *--p = '0' + tenths;
20936 *--p = '.';
20939 /* Print QUOTIENT. */
20942 int digit = quotient % 10;
20943 *--p = '0' + digit;
20945 while ((quotient /= 10) != 0);
20947 /* Print leading spaces. */
20948 while (buf < p)
20949 *--p = ' ';
20952 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
20953 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
20954 type of CODING_SYSTEM. Return updated pointer into BUF. */
20956 static unsigned char invalid_eol_type[] = "(*invalid*)";
20958 static char *
20959 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
20961 Lisp_Object val;
20962 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
20963 const unsigned char *eol_str;
20964 int eol_str_len;
20965 /* The EOL conversion we are using. */
20966 Lisp_Object eoltype;
20968 val = CODING_SYSTEM_SPEC (coding_system);
20969 eoltype = Qnil;
20971 if (!VECTORP (val)) /* Not yet decided. */
20973 if (multibyte)
20974 *buf++ = '-';
20975 if (eol_flag)
20976 eoltype = eol_mnemonic_undecided;
20977 /* Don't mention EOL conversion if it isn't decided. */
20979 else
20981 Lisp_Object attrs;
20982 Lisp_Object eolvalue;
20984 attrs = AREF (val, 0);
20985 eolvalue = AREF (val, 2);
20987 if (multibyte)
20988 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
20990 if (eol_flag)
20992 /* The EOL conversion that is normal on this system. */
20994 if (NILP (eolvalue)) /* Not yet decided. */
20995 eoltype = eol_mnemonic_undecided;
20996 else if (VECTORP (eolvalue)) /* Not yet decided. */
20997 eoltype = eol_mnemonic_undecided;
20998 else /* eolvalue is Qunix, Qdos, or Qmac. */
20999 eoltype = (EQ (eolvalue, Qunix)
21000 ? eol_mnemonic_unix
21001 : (EQ (eolvalue, Qdos) == 1
21002 ? eol_mnemonic_dos : eol_mnemonic_mac));
21006 if (eol_flag)
21008 /* Mention the EOL conversion if it is not the usual one. */
21009 if (STRINGP (eoltype))
21011 eol_str = SDATA (eoltype);
21012 eol_str_len = SBYTES (eoltype);
21014 else if (CHARACTERP (eoltype))
21016 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
21017 int c = XFASTINT (eoltype);
21018 eol_str_len = CHAR_STRING (c, tmp);
21019 eol_str = tmp;
21021 else
21023 eol_str = invalid_eol_type;
21024 eol_str_len = sizeof (invalid_eol_type) - 1;
21026 memcpy (buf, eol_str, eol_str_len);
21027 buf += eol_str_len;
21030 return buf;
21033 /* Return a string for the output of a mode line %-spec for window W,
21034 generated by character C. FIELD_WIDTH > 0 means pad the string
21035 returned with spaces to that value. Return a Lisp string in
21036 *STRING if the resulting string is taken from that Lisp string.
21038 Note we operate on the current buffer for most purposes,
21039 the exception being w->base_line_pos. */
21041 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21043 static const char *
21044 decode_mode_spec (struct window *w, register int c, int field_width,
21045 Lisp_Object *string)
21047 Lisp_Object obj;
21048 struct frame *f = XFRAME (WINDOW_FRAME (w));
21049 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21050 struct buffer *b = current_buffer;
21052 obj = Qnil;
21053 *string = Qnil;
21055 switch (c)
21057 case '*':
21058 if (!NILP (BVAR (b, read_only)))
21059 return "%";
21060 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21061 return "*";
21062 return "-";
21064 case '+':
21065 /* This differs from %* only for a modified read-only buffer. */
21066 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21067 return "*";
21068 if (!NILP (BVAR (b, read_only)))
21069 return "%";
21070 return "-";
21072 case '&':
21073 /* This differs from %* in ignoring read-only-ness. */
21074 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21075 return "*";
21076 return "-";
21078 case '%':
21079 return "%";
21081 case '[':
21083 int i;
21084 char *p;
21086 if (command_loop_level > 5)
21087 return "[[[... ";
21088 p = decode_mode_spec_buf;
21089 for (i = 0; i < command_loop_level; i++)
21090 *p++ = '[';
21091 *p = 0;
21092 return decode_mode_spec_buf;
21095 case ']':
21097 int i;
21098 char *p;
21100 if (command_loop_level > 5)
21101 return " ...]]]";
21102 p = decode_mode_spec_buf;
21103 for (i = 0; i < command_loop_level; i++)
21104 *p++ = ']';
21105 *p = 0;
21106 return decode_mode_spec_buf;
21109 case '-':
21111 register int i;
21113 /* Let lots_of_dashes be a string of infinite length. */
21114 if (mode_line_target == MODE_LINE_NOPROP ||
21115 mode_line_target == MODE_LINE_STRING)
21116 return "--";
21117 if (field_width <= 0
21118 || field_width > sizeof (lots_of_dashes))
21120 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21121 decode_mode_spec_buf[i] = '-';
21122 decode_mode_spec_buf[i] = '\0';
21123 return decode_mode_spec_buf;
21125 else
21126 return lots_of_dashes;
21129 case 'b':
21130 obj = BVAR (b, name);
21131 break;
21133 case 'c':
21134 /* %c and %l are ignored in `frame-title-format'.
21135 (In redisplay_internal, the frame title is drawn _before_ the
21136 windows are updated, so the stuff which depends on actual
21137 window contents (such as %l) may fail to render properly, or
21138 even crash emacs.) */
21139 if (mode_line_target == MODE_LINE_TITLE)
21140 return "";
21141 else
21143 EMACS_INT col = current_column ();
21144 w->column_number_displayed = make_number (col);
21145 pint2str (decode_mode_spec_buf, field_width, col);
21146 return decode_mode_spec_buf;
21149 case 'e':
21150 #ifndef SYSTEM_MALLOC
21152 if (NILP (Vmemory_full))
21153 return "";
21154 else
21155 return "!MEM FULL! ";
21157 #else
21158 return "";
21159 #endif
21161 case 'F':
21162 /* %F displays the frame name. */
21163 if (!NILP (f->title))
21164 return SSDATA (f->title);
21165 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21166 return SSDATA (f->name);
21167 return "Emacs";
21169 case 'f':
21170 obj = BVAR (b, filename);
21171 break;
21173 case 'i':
21175 EMACS_INT size = ZV - BEGV;
21176 pint2str (decode_mode_spec_buf, field_width, size);
21177 return decode_mode_spec_buf;
21180 case 'I':
21182 EMACS_INT size = ZV - BEGV;
21183 pint2hrstr (decode_mode_spec_buf, field_width, size);
21184 return decode_mode_spec_buf;
21187 case 'l':
21189 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
21190 EMACS_INT topline, nlines, height;
21191 EMACS_INT junk;
21193 /* %c and %l are ignored in `frame-title-format'. */
21194 if (mode_line_target == MODE_LINE_TITLE)
21195 return "";
21197 startpos = XMARKER (w->start)->charpos;
21198 startpos_byte = marker_byte_position (w->start);
21199 height = WINDOW_TOTAL_LINES (w);
21201 /* If we decided that this buffer isn't suitable for line numbers,
21202 don't forget that too fast. */
21203 if (EQ (w->base_line_pos, w->buffer))
21204 goto no_value;
21205 /* But do forget it, if the window shows a different buffer now. */
21206 else if (BUFFERP (w->base_line_pos))
21207 w->base_line_pos = Qnil;
21209 /* If the buffer is very big, don't waste time. */
21210 if (INTEGERP (Vline_number_display_limit)
21211 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21213 w->base_line_pos = Qnil;
21214 w->base_line_number = Qnil;
21215 goto no_value;
21218 if (INTEGERP (w->base_line_number)
21219 && INTEGERP (w->base_line_pos)
21220 && XFASTINT (w->base_line_pos) <= startpos)
21222 line = XFASTINT (w->base_line_number);
21223 linepos = XFASTINT (w->base_line_pos);
21224 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21226 else
21228 line = 1;
21229 linepos = BUF_BEGV (b);
21230 linepos_byte = BUF_BEGV_BYTE (b);
21233 /* Count lines from base line to window start position. */
21234 nlines = display_count_lines (linepos_byte,
21235 startpos_byte,
21236 startpos, &junk);
21238 topline = nlines + line;
21240 /* Determine a new base line, if the old one is too close
21241 or too far away, or if we did not have one.
21242 "Too close" means it's plausible a scroll-down would
21243 go back past it. */
21244 if (startpos == BUF_BEGV (b))
21246 w->base_line_number = make_number (topline);
21247 w->base_line_pos = make_number (BUF_BEGV (b));
21249 else if (nlines < height + 25 || nlines > height * 3 + 50
21250 || linepos == BUF_BEGV (b))
21252 EMACS_INT limit = BUF_BEGV (b);
21253 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
21254 EMACS_INT position;
21255 EMACS_INT distance =
21256 (height * 2 + 30) * line_number_display_limit_width;
21258 if (startpos - distance > limit)
21260 limit = startpos - distance;
21261 limit_byte = CHAR_TO_BYTE (limit);
21264 nlines = display_count_lines (startpos_byte,
21265 limit_byte,
21266 - (height * 2 + 30),
21267 &position);
21268 /* If we couldn't find the lines we wanted within
21269 line_number_display_limit_width chars per line,
21270 give up on line numbers for this window. */
21271 if (position == limit_byte && limit == startpos - distance)
21273 w->base_line_pos = w->buffer;
21274 w->base_line_number = Qnil;
21275 goto no_value;
21278 w->base_line_number = make_number (topline - nlines);
21279 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
21282 /* Now count lines from the start pos to point. */
21283 nlines = display_count_lines (startpos_byte,
21284 PT_BYTE, PT, &junk);
21286 /* Record that we did display the line number. */
21287 line_number_displayed = 1;
21289 /* Make the string to show. */
21290 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21291 return decode_mode_spec_buf;
21292 no_value:
21294 char* p = decode_mode_spec_buf;
21295 int pad = field_width - 2;
21296 while (pad-- > 0)
21297 *p++ = ' ';
21298 *p++ = '?';
21299 *p++ = '?';
21300 *p = '\0';
21301 return decode_mode_spec_buf;
21304 break;
21306 case 'm':
21307 obj = BVAR (b, mode_name);
21308 break;
21310 case 'n':
21311 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21312 return " Narrow";
21313 break;
21315 case 'p':
21317 EMACS_INT pos = marker_position (w->start);
21318 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
21320 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21322 if (pos <= BUF_BEGV (b))
21323 return "All";
21324 else
21325 return "Bottom";
21327 else if (pos <= BUF_BEGV (b))
21328 return "Top";
21329 else
21331 if (total > 1000000)
21332 /* Do it differently for a large value, to avoid overflow. */
21333 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21334 else
21335 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21336 /* We can't normally display a 3-digit number,
21337 so get us a 2-digit number that is close. */
21338 if (total == 100)
21339 total = 99;
21340 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
21341 return decode_mode_spec_buf;
21345 /* Display percentage of size above the bottom of the screen. */
21346 case 'P':
21348 EMACS_INT toppos = marker_position (w->start);
21349 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21350 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
21352 if (botpos >= BUF_ZV (b))
21354 if (toppos <= BUF_BEGV (b))
21355 return "All";
21356 else
21357 return "Bottom";
21359 else
21361 if (total > 1000000)
21362 /* Do it differently for a large value, to avoid overflow. */
21363 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21364 else
21365 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21366 /* We can't normally display a 3-digit number,
21367 so get us a 2-digit number that is close. */
21368 if (total == 100)
21369 total = 99;
21370 if (toppos <= BUF_BEGV (b))
21371 sprintf (decode_mode_spec_buf, "Top%2"pI"d%%", total);
21372 else
21373 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
21374 return decode_mode_spec_buf;
21378 case 's':
21379 /* status of process */
21380 obj = Fget_buffer_process (Fcurrent_buffer ());
21381 if (NILP (obj))
21382 return "no process";
21383 #ifndef MSDOS
21384 obj = Fsymbol_name (Fprocess_status (obj));
21385 #endif
21386 break;
21388 case '@':
21390 int count = inhibit_garbage_collection ();
21391 Lisp_Object val = call1 (intern ("file-remote-p"),
21392 BVAR (current_buffer, directory));
21393 unbind_to (count, Qnil);
21395 if (NILP (val))
21396 return "-";
21397 else
21398 return "@";
21401 case 't': /* indicate TEXT or BINARY */
21402 return "T";
21404 case 'z':
21405 /* coding-system (not including end-of-line format) */
21406 case 'Z':
21407 /* coding-system (including end-of-line type) */
21409 int eol_flag = (c == 'Z');
21410 char *p = decode_mode_spec_buf;
21412 if (! FRAME_WINDOW_P (f))
21414 /* No need to mention EOL here--the terminal never needs
21415 to do EOL conversion. */
21416 p = decode_mode_spec_coding (CODING_ID_NAME
21417 (FRAME_KEYBOARD_CODING (f)->id),
21418 p, 0);
21419 p = decode_mode_spec_coding (CODING_ID_NAME
21420 (FRAME_TERMINAL_CODING (f)->id),
21421 p, 0);
21423 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21424 p, eol_flag);
21426 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21427 #ifdef subprocesses
21428 obj = Fget_buffer_process (Fcurrent_buffer ());
21429 if (PROCESSP (obj))
21431 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
21432 p, eol_flag);
21433 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
21434 p, eol_flag);
21436 #endif /* subprocesses */
21437 #endif /* 0 */
21438 *p = 0;
21439 return decode_mode_spec_buf;
21443 if (STRINGP (obj))
21445 *string = obj;
21446 return SSDATA (obj);
21448 else
21449 return "";
21453 /* Count up to COUNT lines starting from START_BYTE.
21454 But don't go beyond LIMIT_BYTE.
21455 Return the number of lines thus found (always nonnegative).
21457 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21459 static EMACS_INT
21460 display_count_lines (EMACS_INT start_byte,
21461 EMACS_INT limit_byte, EMACS_INT count,
21462 EMACS_INT *byte_pos_ptr)
21464 register unsigned char *cursor;
21465 unsigned char *base;
21467 register EMACS_INT ceiling;
21468 register unsigned char *ceiling_addr;
21469 EMACS_INT orig_count = count;
21471 /* If we are not in selective display mode,
21472 check only for newlines. */
21473 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21474 && !INTEGERP (BVAR (current_buffer, selective_display)));
21476 if (count > 0)
21478 while (start_byte < limit_byte)
21480 ceiling = BUFFER_CEILING_OF (start_byte);
21481 ceiling = min (limit_byte - 1, ceiling);
21482 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21483 base = (cursor = BYTE_POS_ADDR (start_byte));
21484 while (1)
21486 if (selective_display)
21487 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21489 else
21490 while (*cursor != '\n' && ++cursor != ceiling_addr)
21493 if (cursor != ceiling_addr)
21495 if (--count == 0)
21497 start_byte += cursor - base + 1;
21498 *byte_pos_ptr = start_byte;
21499 return orig_count;
21501 else
21502 if (++cursor == ceiling_addr)
21503 break;
21505 else
21506 break;
21508 start_byte += cursor - base;
21511 else
21513 while (start_byte > limit_byte)
21515 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21516 ceiling = max (limit_byte, ceiling);
21517 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21518 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21519 while (1)
21521 if (selective_display)
21522 while (--cursor != ceiling_addr
21523 && *cursor != '\n' && *cursor != 015)
21525 else
21526 while (--cursor != ceiling_addr && *cursor != '\n')
21529 if (cursor != ceiling_addr)
21531 if (++count == 0)
21533 start_byte += cursor - base + 1;
21534 *byte_pos_ptr = start_byte;
21535 /* When scanning backwards, we should
21536 not count the newline posterior to which we stop. */
21537 return - orig_count - 1;
21540 else
21541 break;
21543 /* Here we add 1 to compensate for the last decrement
21544 of CURSOR, which took it past the valid range. */
21545 start_byte += cursor - base + 1;
21549 *byte_pos_ptr = limit_byte;
21551 if (count < 0)
21552 return - orig_count + count;
21553 return orig_count - count;
21559 /***********************************************************************
21560 Displaying strings
21561 ***********************************************************************/
21563 /* Display a NUL-terminated string, starting with index START.
21565 If STRING is non-null, display that C string. Otherwise, the Lisp
21566 string LISP_STRING is displayed. There's a case that STRING is
21567 non-null and LISP_STRING is not nil. It means STRING is a string
21568 data of LISP_STRING. In that case, we display LISP_STRING while
21569 ignoring its text properties.
21571 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21572 FACE_STRING. Display STRING or LISP_STRING with the face at
21573 FACE_STRING_POS in FACE_STRING:
21575 Display the string in the environment given by IT, but use the
21576 standard display table, temporarily.
21578 FIELD_WIDTH is the minimum number of output glyphs to produce.
21579 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21580 with spaces. If STRING has more characters, more than FIELD_WIDTH
21581 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21583 PRECISION is the maximum number of characters to output from
21584 STRING. PRECISION < 0 means don't truncate the string.
21586 This is roughly equivalent to printf format specifiers:
21588 FIELD_WIDTH PRECISION PRINTF
21589 ----------------------------------------
21590 -1 -1 %s
21591 -1 10 %.10s
21592 10 -1 %10s
21593 20 10 %20.10s
21595 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21596 display them, and < 0 means obey the current buffer's value of
21597 enable_multibyte_characters.
21599 Value is the number of columns displayed. */
21601 static int
21602 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21603 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
21604 int field_width, int precision, int max_x, int multibyte)
21606 int hpos_at_start = it->hpos;
21607 int saved_face_id = it->face_id;
21608 struct glyph_row *row = it->glyph_row;
21609 EMACS_INT it_charpos;
21611 /* Initialize the iterator IT for iteration over STRING beginning
21612 with index START. */
21613 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21614 precision, field_width, multibyte);
21615 if (string && STRINGP (lisp_string))
21616 /* LISP_STRING is the one returned by decode_mode_spec. We should
21617 ignore its text properties. */
21618 it->stop_charpos = it->end_charpos;
21620 /* If displaying STRING, set up the face of the iterator from
21621 FACE_STRING, if that's given. */
21622 if (STRINGP (face_string))
21624 EMACS_INT endptr;
21625 struct face *face;
21627 it->face_id
21628 = face_at_string_position (it->w, face_string, face_string_pos,
21629 0, it->region_beg_charpos,
21630 it->region_end_charpos,
21631 &endptr, it->base_face_id, 0);
21632 face = FACE_FROM_ID (it->f, it->face_id);
21633 it->face_box_p = face->box != FACE_NO_BOX;
21636 /* Set max_x to the maximum allowed X position. Don't let it go
21637 beyond the right edge of the window. */
21638 if (max_x <= 0)
21639 max_x = it->last_visible_x;
21640 else
21641 max_x = min (max_x, it->last_visible_x);
21643 /* Skip over display elements that are not visible. because IT->w is
21644 hscrolled. */
21645 if (it->current_x < it->first_visible_x)
21646 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21647 MOVE_TO_POS | MOVE_TO_X);
21649 row->ascent = it->max_ascent;
21650 row->height = it->max_ascent + it->max_descent;
21651 row->phys_ascent = it->max_phys_ascent;
21652 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21653 row->extra_line_spacing = it->max_extra_line_spacing;
21655 if (STRINGP (it->string))
21656 it_charpos = IT_STRING_CHARPOS (*it);
21657 else
21658 it_charpos = IT_CHARPOS (*it);
21660 /* This condition is for the case that we are called with current_x
21661 past last_visible_x. */
21662 while (it->current_x < max_x)
21664 int x_before, x, n_glyphs_before, i, nglyphs;
21666 /* Get the next display element. */
21667 if (!get_next_display_element (it))
21668 break;
21670 /* Produce glyphs. */
21671 x_before = it->current_x;
21672 n_glyphs_before = row->used[TEXT_AREA];
21673 PRODUCE_GLYPHS (it);
21675 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21676 i = 0;
21677 x = x_before;
21678 while (i < nglyphs)
21680 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21682 if (it->line_wrap != TRUNCATE
21683 && x + glyph->pixel_width > max_x)
21685 /* End of continued line or max_x reached. */
21686 if (CHAR_GLYPH_PADDING_P (*glyph))
21688 /* A wide character is unbreakable. */
21689 if (row->reversed_p)
21690 unproduce_glyphs (it, row->used[TEXT_AREA]
21691 - n_glyphs_before);
21692 row->used[TEXT_AREA] = n_glyphs_before;
21693 it->current_x = x_before;
21695 else
21697 if (row->reversed_p)
21698 unproduce_glyphs (it, row->used[TEXT_AREA]
21699 - (n_glyphs_before + i));
21700 row->used[TEXT_AREA] = n_glyphs_before + i;
21701 it->current_x = x;
21703 break;
21705 else if (x + glyph->pixel_width >= it->first_visible_x)
21707 /* Glyph is at least partially visible. */
21708 ++it->hpos;
21709 if (x < it->first_visible_x)
21710 row->x = x - it->first_visible_x;
21712 else
21714 /* Glyph is off the left margin of the display area.
21715 Should not happen. */
21716 abort ();
21719 row->ascent = max (row->ascent, it->max_ascent);
21720 row->height = max (row->height, it->max_ascent + it->max_descent);
21721 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21722 row->phys_height = max (row->phys_height,
21723 it->max_phys_ascent + it->max_phys_descent);
21724 row->extra_line_spacing = max (row->extra_line_spacing,
21725 it->max_extra_line_spacing);
21726 x += glyph->pixel_width;
21727 ++i;
21730 /* Stop if max_x reached. */
21731 if (i < nglyphs)
21732 break;
21734 /* Stop at line ends. */
21735 if (ITERATOR_AT_END_OF_LINE_P (it))
21737 it->continuation_lines_width = 0;
21738 break;
21741 set_iterator_to_next (it, 1);
21742 if (STRINGP (it->string))
21743 it_charpos = IT_STRING_CHARPOS (*it);
21744 else
21745 it_charpos = IT_CHARPOS (*it);
21747 /* Stop if truncating at the right edge. */
21748 if (it->line_wrap == TRUNCATE
21749 && it->current_x >= it->last_visible_x)
21751 /* Add truncation mark, but don't do it if the line is
21752 truncated at a padding space. */
21753 if (it_charpos < it->string_nchars)
21755 if (!FRAME_WINDOW_P (it->f))
21757 int ii, n;
21759 if (it->current_x > it->last_visible_x)
21761 if (!row->reversed_p)
21763 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21764 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21765 break;
21767 else
21769 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21770 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21771 break;
21772 unproduce_glyphs (it, ii + 1);
21773 ii = row->used[TEXT_AREA] - (ii + 1);
21775 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21777 row->used[TEXT_AREA] = ii;
21778 produce_special_glyphs (it, IT_TRUNCATION);
21781 produce_special_glyphs (it, IT_TRUNCATION);
21783 row->truncated_on_right_p = 1;
21785 break;
21789 /* Maybe insert a truncation at the left. */
21790 if (it->first_visible_x
21791 && it_charpos > 0)
21793 if (!FRAME_WINDOW_P (it->f))
21794 insert_left_trunc_glyphs (it);
21795 row->truncated_on_left_p = 1;
21798 it->face_id = saved_face_id;
21800 /* Value is number of columns displayed. */
21801 return it->hpos - hpos_at_start;
21806 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21807 appears as an element of LIST or as the car of an element of LIST.
21808 If PROPVAL is a list, compare each element against LIST in that
21809 way, and return 1/2 if any element of PROPVAL is found in LIST.
21810 Otherwise return 0. This function cannot quit.
21811 The return value is 2 if the text is invisible but with an ellipsis
21812 and 1 if it's invisible and without an ellipsis. */
21815 invisible_p (register Lisp_Object propval, Lisp_Object list)
21817 register Lisp_Object tail, proptail;
21819 for (tail = list; CONSP (tail); tail = XCDR (tail))
21821 register Lisp_Object tem;
21822 tem = XCAR (tail);
21823 if (EQ (propval, tem))
21824 return 1;
21825 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21826 return NILP (XCDR (tem)) ? 1 : 2;
21829 if (CONSP (propval))
21831 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
21833 Lisp_Object propelt;
21834 propelt = XCAR (proptail);
21835 for (tail = list; CONSP (tail); tail = XCDR (tail))
21837 register Lisp_Object tem;
21838 tem = XCAR (tail);
21839 if (EQ (propelt, tem))
21840 return 1;
21841 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
21842 return NILP (XCDR (tem)) ? 1 : 2;
21847 return 0;
21850 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
21851 doc: /* Non-nil if the property makes the text invisible.
21852 POS-OR-PROP can be a marker or number, in which case it is taken to be
21853 a position in the current buffer and the value of the `invisible' property
21854 is checked; or it can be some other value, which is then presumed to be the
21855 value of the `invisible' property of the text of interest.
21856 The non-nil value returned can be t for truly invisible text or something
21857 else if the text is replaced by an ellipsis. */)
21858 (Lisp_Object pos_or_prop)
21860 Lisp_Object prop
21861 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
21862 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
21863 : pos_or_prop);
21864 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
21865 return (invis == 0 ? Qnil
21866 : invis == 1 ? Qt
21867 : make_number (invis));
21870 /* Calculate a width or height in pixels from a specification using
21871 the following elements:
21873 SPEC ::=
21874 NUM - a (fractional) multiple of the default font width/height
21875 (NUM) - specifies exactly NUM pixels
21876 UNIT - a fixed number of pixels, see below.
21877 ELEMENT - size of a display element in pixels, see below.
21878 (NUM . SPEC) - equals NUM * SPEC
21879 (+ SPEC SPEC ...) - add pixel values
21880 (- SPEC SPEC ...) - subtract pixel values
21881 (- SPEC) - negate pixel value
21883 NUM ::=
21884 INT or FLOAT - a number constant
21885 SYMBOL - use symbol's (buffer local) variable binding.
21887 UNIT ::=
21888 in - pixels per inch *)
21889 mm - pixels per 1/1000 meter *)
21890 cm - pixels per 1/100 meter *)
21891 width - width of current font in pixels.
21892 height - height of current font in pixels.
21894 *) using the ratio(s) defined in display-pixels-per-inch.
21896 ELEMENT ::=
21898 left-fringe - left fringe width in pixels
21899 right-fringe - right fringe width in pixels
21901 left-margin - left margin width in pixels
21902 right-margin - right margin width in pixels
21904 scroll-bar - scroll-bar area width in pixels
21906 Examples:
21908 Pixels corresponding to 5 inches:
21909 (5 . in)
21911 Total width of non-text areas on left side of window (if scroll-bar is on left):
21912 '(space :width (+ left-fringe left-margin scroll-bar))
21914 Align to first text column (in header line):
21915 '(space :align-to 0)
21917 Align to middle of text area minus half the width of variable `my-image'
21918 containing a loaded image:
21919 '(space :align-to (0.5 . (- text my-image)))
21921 Width of left margin minus width of 1 character in the default font:
21922 '(space :width (- left-margin 1))
21924 Width of left margin minus width of 2 characters in the current font:
21925 '(space :width (- left-margin (2 . width)))
21927 Center 1 character over left-margin (in header line):
21928 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
21930 Different ways to express width of left fringe plus left margin minus one pixel:
21931 '(space :width (- (+ left-fringe left-margin) (1)))
21932 '(space :width (+ left-fringe left-margin (- (1))))
21933 '(space :width (+ left-fringe left-margin (-1)))
21937 #define NUMVAL(X) \
21938 ((INTEGERP (X) || FLOATP (X)) \
21939 ? XFLOATINT (X) \
21940 : - 1)
21942 static int
21943 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
21944 struct font *font, int width_p, int *align_to)
21946 double pixels;
21948 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
21949 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
21951 if (NILP (prop))
21952 return OK_PIXELS (0);
21954 xassert (FRAME_LIVE_P (it->f));
21956 if (SYMBOLP (prop))
21958 if (SCHARS (SYMBOL_NAME (prop)) == 2)
21960 char *unit = SSDATA (SYMBOL_NAME (prop));
21962 if (unit[0] == 'i' && unit[1] == 'n')
21963 pixels = 1.0;
21964 else if (unit[0] == 'm' && unit[1] == 'm')
21965 pixels = 25.4;
21966 else if (unit[0] == 'c' && unit[1] == 'm')
21967 pixels = 2.54;
21968 else
21969 pixels = 0;
21970 if (pixels > 0)
21972 double ppi;
21973 #ifdef HAVE_WINDOW_SYSTEM
21974 if (FRAME_WINDOW_P (it->f)
21975 && (ppi = (width_p
21976 ? FRAME_X_DISPLAY_INFO (it->f)->resx
21977 : FRAME_X_DISPLAY_INFO (it->f)->resy),
21978 ppi > 0))
21979 return OK_PIXELS (ppi / pixels);
21980 #endif
21982 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
21983 || (CONSP (Vdisplay_pixels_per_inch)
21984 && (ppi = (width_p
21985 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
21986 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
21987 ppi > 0)))
21988 return OK_PIXELS (ppi / pixels);
21990 return 0;
21994 #ifdef HAVE_WINDOW_SYSTEM
21995 if (EQ (prop, Qheight))
21996 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
21997 if (EQ (prop, Qwidth))
21998 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
21999 #else
22000 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22001 return OK_PIXELS (1);
22002 #endif
22004 if (EQ (prop, Qtext))
22005 return OK_PIXELS (width_p
22006 ? window_box_width (it->w, TEXT_AREA)
22007 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22009 if (align_to && *align_to < 0)
22011 *res = 0;
22012 if (EQ (prop, Qleft))
22013 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22014 if (EQ (prop, Qright))
22015 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22016 if (EQ (prop, Qcenter))
22017 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22018 + window_box_width (it->w, TEXT_AREA) / 2);
22019 if (EQ (prop, Qleft_fringe))
22020 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22021 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22022 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22023 if (EQ (prop, Qright_fringe))
22024 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22025 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22026 : window_box_right_offset (it->w, TEXT_AREA));
22027 if (EQ (prop, Qleft_margin))
22028 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22029 if (EQ (prop, Qright_margin))
22030 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22031 if (EQ (prop, Qscroll_bar))
22032 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22034 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22035 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22036 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22037 : 0)));
22039 else
22041 if (EQ (prop, Qleft_fringe))
22042 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22043 if (EQ (prop, Qright_fringe))
22044 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22045 if (EQ (prop, Qleft_margin))
22046 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22047 if (EQ (prop, Qright_margin))
22048 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22049 if (EQ (prop, Qscroll_bar))
22050 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22053 prop = Fbuffer_local_value (prop, it->w->buffer);
22056 if (INTEGERP (prop) || FLOATP (prop))
22058 int base_unit = (width_p
22059 ? FRAME_COLUMN_WIDTH (it->f)
22060 : FRAME_LINE_HEIGHT (it->f));
22061 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22064 if (CONSP (prop))
22066 Lisp_Object car = XCAR (prop);
22067 Lisp_Object cdr = XCDR (prop);
22069 if (SYMBOLP (car))
22071 #ifdef HAVE_WINDOW_SYSTEM
22072 if (FRAME_WINDOW_P (it->f)
22073 && valid_image_p (prop))
22075 ptrdiff_t id = lookup_image (it->f, prop);
22076 struct image *img = IMAGE_FROM_ID (it->f, id);
22078 return OK_PIXELS (width_p ? img->width : img->height);
22080 #endif
22081 if (EQ (car, Qplus) || EQ (car, Qminus))
22083 int first = 1;
22084 double px;
22086 pixels = 0;
22087 while (CONSP (cdr))
22089 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22090 font, width_p, align_to))
22091 return 0;
22092 if (first)
22093 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22094 else
22095 pixels += px;
22096 cdr = XCDR (cdr);
22098 if (EQ (car, Qminus))
22099 pixels = -pixels;
22100 return OK_PIXELS (pixels);
22103 car = Fbuffer_local_value (car, it->w->buffer);
22106 if (INTEGERP (car) || FLOATP (car))
22108 double fact;
22109 pixels = XFLOATINT (car);
22110 if (NILP (cdr))
22111 return OK_PIXELS (pixels);
22112 if (calc_pixel_width_or_height (&fact, it, cdr,
22113 font, width_p, align_to))
22114 return OK_PIXELS (pixels * fact);
22115 return 0;
22118 return 0;
22121 return 0;
22125 /***********************************************************************
22126 Glyph Display
22127 ***********************************************************************/
22129 #ifdef HAVE_WINDOW_SYSTEM
22131 #if GLYPH_DEBUG
22133 void
22134 dump_glyph_string (struct glyph_string *s)
22136 fprintf (stderr, "glyph string\n");
22137 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22138 s->x, s->y, s->width, s->height);
22139 fprintf (stderr, " ybase = %d\n", s->ybase);
22140 fprintf (stderr, " hl = %d\n", s->hl);
22141 fprintf (stderr, " left overhang = %d, right = %d\n",
22142 s->left_overhang, s->right_overhang);
22143 fprintf (stderr, " nchars = %d\n", s->nchars);
22144 fprintf (stderr, " extends to end of line = %d\n",
22145 s->extends_to_end_of_line_p);
22146 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22147 fprintf (stderr, " bg width = %d\n", s->background_width);
22150 #endif /* GLYPH_DEBUG */
22152 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22153 of XChar2b structures for S; it can't be allocated in
22154 init_glyph_string because it must be allocated via `alloca'. W
22155 is the window on which S is drawn. ROW and AREA are the glyph row
22156 and area within the row from which S is constructed. START is the
22157 index of the first glyph structure covered by S. HL is a
22158 face-override for drawing S. */
22160 #ifdef HAVE_NTGUI
22161 #define OPTIONAL_HDC(hdc) HDC hdc,
22162 #define DECLARE_HDC(hdc) HDC hdc;
22163 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22164 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22165 #endif
22167 #ifndef OPTIONAL_HDC
22168 #define OPTIONAL_HDC(hdc)
22169 #define DECLARE_HDC(hdc)
22170 #define ALLOCATE_HDC(hdc, f)
22171 #define RELEASE_HDC(hdc, f)
22172 #endif
22174 static void
22175 init_glyph_string (struct glyph_string *s,
22176 OPTIONAL_HDC (hdc)
22177 XChar2b *char2b, struct window *w, struct glyph_row *row,
22178 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22180 memset (s, 0, sizeof *s);
22181 s->w = w;
22182 s->f = XFRAME (w->frame);
22183 #ifdef HAVE_NTGUI
22184 s->hdc = hdc;
22185 #endif
22186 s->display = FRAME_X_DISPLAY (s->f);
22187 s->window = FRAME_X_WINDOW (s->f);
22188 s->char2b = char2b;
22189 s->hl = hl;
22190 s->row = row;
22191 s->area = area;
22192 s->first_glyph = row->glyphs[area] + start;
22193 s->height = row->height;
22194 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22195 s->ybase = s->y + row->ascent;
22199 /* Append the list of glyph strings with head H and tail T to the list
22200 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22202 static inline void
22203 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22204 struct glyph_string *h, struct glyph_string *t)
22206 if (h)
22208 if (*head)
22209 (*tail)->next = h;
22210 else
22211 *head = h;
22212 h->prev = *tail;
22213 *tail = t;
22218 /* Prepend the list of glyph strings with head H and tail T to the
22219 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22220 result. */
22222 static inline void
22223 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22224 struct glyph_string *h, struct glyph_string *t)
22226 if (h)
22228 if (*head)
22229 (*head)->prev = t;
22230 else
22231 *tail = t;
22232 t->next = *head;
22233 *head = h;
22238 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22239 Set *HEAD and *TAIL to the resulting list. */
22241 static inline void
22242 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22243 struct glyph_string *s)
22245 s->next = s->prev = NULL;
22246 append_glyph_string_lists (head, tail, s, s);
22250 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22251 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22252 make sure that X resources for the face returned are allocated.
22253 Value is a pointer to a realized face that is ready for display if
22254 DISPLAY_P is non-zero. */
22256 static inline struct face *
22257 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22258 XChar2b *char2b, int display_p)
22260 struct face *face = FACE_FROM_ID (f, face_id);
22262 if (face->font)
22264 unsigned code = face->font->driver->encode_char (face->font, c);
22266 if (code != FONT_INVALID_CODE)
22267 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22268 else
22269 STORE_XCHAR2B (char2b, 0, 0);
22272 /* Make sure X resources of the face are allocated. */
22273 #ifdef HAVE_X_WINDOWS
22274 if (display_p)
22275 #endif
22277 xassert (face != NULL);
22278 PREPARE_FACE_FOR_DISPLAY (f, face);
22281 return face;
22285 /* Get face and two-byte form of character glyph GLYPH on frame F.
22286 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22287 a pointer to a realized face that is ready for display. */
22289 static inline struct face *
22290 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22291 XChar2b *char2b, int *two_byte_p)
22293 struct face *face;
22295 xassert (glyph->type == CHAR_GLYPH);
22296 face = FACE_FROM_ID (f, glyph->face_id);
22298 if (two_byte_p)
22299 *two_byte_p = 0;
22301 if (face->font)
22303 unsigned code;
22305 if (CHAR_BYTE8_P (glyph->u.ch))
22306 code = CHAR_TO_BYTE8 (glyph->u.ch);
22307 else
22308 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22310 if (code != FONT_INVALID_CODE)
22311 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22312 else
22313 STORE_XCHAR2B (char2b, 0, 0);
22316 /* Make sure X resources of the face are allocated. */
22317 xassert (face != NULL);
22318 PREPARE_FACE_FOR_DISPLAY (f, face);
22319 return face;
22323 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22324 Return 1 if FONT has a glyph for C, otherwise return 0. */
22326 static inline int
22327 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22329 unsigned code;
22331 if (CHAR_BYTE8_P (c))
22332 code = CHAR_TO_BYTE8 (c);
22333 else
22334 code = font->driver->encode_char (font, c);
22336 if (code == FONT_INVALID_CODE)
22337 return 0;
22338 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22339 return 1;
22343 /* Fill glyph string S with composition components specified by S->cmp.
22345 BASE_FACE is the base face of the composition.
22346 S->cmp_from is the index of the first component for S.
22348 OVERLAPS non-zero means S should draw the foreground only, and use
22349 its physical height for clipping. See also draw_glyphs.
22351 Value is the index of a component not in S. */
22353 static int
22354 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22355 int overlaps)
22357 int i;
22358 /* For all glyphs of this composition, starting at the offset
22359 S->cmp_from, until we reach the end of the definition or encounter a
22360 glyph that requires the different face, add it to S. */
22361 struct face *face;
22363 xassert (s);
22365 s->for_overlaps = overlaps;
22366 s->face = NULL;
22367 s->font = NULL;
22368 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22370 int c = COMPOSITION_GLYPH (s->cmp, i);
22372 /* TAB in a composition means display glyphs with padding space
22373 on the left or right. */
22374 if (c != '\t')
22376 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22377 -1, Qnil);
22379 face = get_char_face_and_encoding (s->f, c, face_id,
22380 s->char2b + i, 1);
22381 if (face)
22383 if (! s->face)
22385 s->face = face;
22386 s->font = s->face->font;
22388 else if (s->face != face)
22389 break;
22392 ++s->nchars;
22394 s->cmp_to = i;
22396 if (s->face == NULL)
22398 s->face = base_face->ascii_face;
22399 s->font = s->face->font;
22402 /* All glyph strings for the same composition has the same width,
22403 i.e. the width set for the first component of the composition. */
22404 s->width = s->first_glyph->pixel_width;
22406 /* If the specified font could not be loaded, use the frame's
22407 default font, but record the fact that we couldn't load it in
22408 the glyph string so that we can draw rectangles for the
22409 characters of the glyph string. */
22410 if (s->font == NULL)
22412 s->font_not_found_p = 1;
22413 s->font = FRAME_FONT (s->f);
22416 /* Adjust base line for subscript/superscript text. */
22417 s->ybase += s->first_glyph->voffset;
22419 /* This glyph string must always be drawn with 16-bit functions. */
22420 s->two_byte_p = 1;
22422 return s->cmp_to;
22425 static int
22426 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22427 int start, int end, int overlaps)
22429 struct glyph *glyph, *last;
22430 Lisp_Object lgstring;
22431 int i;
22433 s->for_overlaps = overlaps;
22434 glyph = s->row->glyphs[s->area] + start;
22435 last = s->row->glyphs[s->area] + end;
22436 s->cmp_id = glyph->u.cmp.id;
22437 s->cmp_from = glyph->slice.cmp.from;
22438 s->cmp_to = glyph->slice.cmp.to + 1;
22439 s->face = FACE_FROM_ID (s->f, face_id);
22440 lgstring = composition_gstring_from_id (s->cmp_id);
22441 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22442 glyph++;
22443 while (glyph < last
22444 && glyph->u.cmp.automatic
22445 && glyph->u.cmp.id == s->cmp_id
22446 && s->cmp_to == glyph->slice.cmp.from)
22447 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22449 for (i = s->cmp_from; i < s->cmp_to; i++)
22451 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22452 unsigned code = LGLYPH_CODE (lglyph);
22454 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22456 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22457 return glyph - s->row->glyphs[s->area];
22461 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22462 See the comment of fill_glyph_string for arguments.
22463 Value is the index of the first glyph not in S. */
22466 static int
22467 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22468 int start, int end, int overlaps)
22470 struct glyph *glyph, *last;
22471 int voffset;
22473 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22474 s->for_overlaps = overlaps;
22475 glyph = s->row->glyphs[s->area] + start;
22476 last = s->row->glyphs[s->area] + end;
22477 voffset = glyph->voffset;
22478 s->face = FACE_FROM_ID (s->f, face_id);
22479 s->font = s->face->font;
22480 s->nchars = 1;
22481 s->width = glyph->pixel_width;
22482 glyph++;
22483 while (glyph < last
22484 && glyph->type == GLYPHLESS_GLYPH
22485 && glyph->voffset == voffset
22486 && glyph->face_id == face_id)
22488 s->nchars++;
22489 s->width += glyph->pixel_width;
22490 glyph++;
22492 s->ybase += voffset;
22493 return glyph - s->row->glyphs[s->area];
22497 /* Fill glyph string S from a sequence of character glyphs.
22499 FACE_ID is the face id of the string. START is the index of the
22500 first glyph to consider, END is the index of the last + 1.
22501 OVERLAPS non-zero means S should draw the foreground only, and use
22502 its physical height for clipping. See also draw_glyphs.
22504 Value is the index of the first glyph not in S. */
22506 static int
22507 fill_glyph_string (struct glyph_string *s, int face_id,
22508 int start, int end, int overlaps)
22510 struct glyph *glyph, *last;
22511 int voffset;
22512 int glyph_not_available_p;
22514 xassert (s->f == XFRAME (s->w->frame));
22515 xassert (s->nchars == 0);
22516 xassert (start >= 0 && end > start);
22518 s->for_overlaps = overlaps;
22519 glyph = s->row->glyphs[s->area] + start;
22520 last = s->row->glyphs[s->area] + end;
22521 voffset = glyph->voffset;
22522 s->padding_p = glyph->padding_p;
22523 glyph_not_available_p = glyph->glyph_not_available_p;
22525 while (glyph < last
22526 && glyph->type == CHAR_GLYPH
22527 && glyph->voffset == voffset
22528 /* Same face id implies same font, nowadays. */
22529 && glyph->face_id == face_id
22530 && glyph->glyph_not_available_p == glyph_not_available_p)
22532 int two_byte_p;
22534 s->face = get_glyph_face_and_encoding (s->f, glyph,
22535 s->char2b + s->nchars,
22536 &two_byte_p);
22537 s->two_byte_p = two_byte_p;
22538 ++s->nchars;
22539 xassert (s->nchars <= end - start);
22540 s->width += glyph->pixel_width;
22541 if (glyph++->padding_p != s->padding_p)
22542 break;
22545 s->font = s->face->font;
22547 /* If the specified font could not be loaded, use the frame's font,
22548 but record the fact that we couldn't load it in
22549 S->font_not_found_p so that we can draw rectangles for the
22550 characters of the glyph string. */
22551 if (s->font == NULL || glyph_not_available_p)
22553 s->font_not_found_p = 1;
22554 s->font = FRAME_FONT (s->f);
22557 /* Adjust base line for subscript/superscript text. */
22558 s->ybase += voffset;
22560 xassert (s->face && s->face->gc);
22561 return glyph - s->row->glyphs[s->area];
22565 /* Fill glyph string S from image glyph S->first_glyph. */
22567 static void
22568 fill_image_glyph_string (struct glyph_string *s)
22570 xassert (s->first_glyph->type == IMAGE_GLYPH);
22571 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22572 xassert (s->img);
22573 s->slice = s->first_glyph->slice.img;
22574 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22575 s->font = s->face->font;
22576 s->width = s->first_glyph->pixel_width;
22578 /* Adjust base line for subscript/superscript text. */
22579 s->ybase += s->first_glyph->voffset;
22583 /* Fill glyph string S from a sequence of stretch glyphs.
22585 START is the index of the first glyph to consider,
22586 END is the index of the last + 1.
22588 Value is the index of the first glyph not in S. */
22590 static int
22591 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22593 struct glyph *glyph, *last;
22594 int voffset, face_id;
22596 xassert (s->first_glyph->type == STRETCH_GLYPH);
22598 glyph = s->row->glyphs[s->area] + start;
22599 last = s->row->glyphs[s->area] + end;
22600 face_id = glyph->face_id;
22601 s->face = FACE_FROM_ID (s->f, face_id);
22602 s->font = s->face->font;
22603 s->width = glyph->pixel_width;
22604 s->nchars = 1;
22605 voffset = glyph->voffset;
22607 for (++glyph;
22608 (glyph < last
22609 && glyph->type == STRETCH_GLYPH
22610 && glyph->voffset == voffset
22611 && glyph->face_id == face_id);
22612 ++glyph)
22613 s->width += glyph->pixel_width;
22615 /* Adjust base line for subscript/superscript text. */
22616 s->ybase += voffset;
22618 /* The case that face->gc == 0 is handled when drawing the glyph
22619 string by calling PREPARE_FACE_FOR_DISPLAY. */
22620 xassert (s->face);
22621 return glyph - s->row->glyphs[s->area];
22624 static struct font_metrics *
22625 get_per_char_metric (struct font *font, XChar2b *char2b)
22627 static struct font_metrics metrics;
22628 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22630 if (! font || code == FONT_INVALID_CODE)
22631 return NULL;
22632 font->driver->text_extents (font, &code, 1, &metrics);
22633 return &metrics;
22636 /* EXPORT for RIF:
22637 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22638 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22639 assumed to be zero. */
22641 void
22642 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22644 *left = *right = 0;
22646 if (glyph->type == CHAR_GLYPH)
22648 struct face *face;
22649 XChar2b char2b;
22650 struct font_metrics *pcm;
22652 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22653 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22655 if (pcm->rbearing > pcm->width)
22656 *right = pcm->rbearing - pcm->width;
22657 if (pcm->lbearing < 0)
22658 *left = -pcm->lbearing;
22661 else if (glyph->type == COMPOSITE_GLYPH)
22663 if (! glyph->u.cmp.automatic)
22665 struct composition *cmp = composition_table[glyph->u.cmp.id];
22667 if (cmp->rbearing > cmp->pixel_width)
22668 *right = cmp->rbearing - cmp->pixel_width;
22669 if (cmp->lbearing < 0)
22670 *left = - cmp->lbearing;
22672 else
22674 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22675 struct font_metrics metrics;
22677 composition_gstring_width (gstring, glyph->slice.cmp.from,
22678 glyph->slice.cmp.to + 1, &metrics);
22679 if (metrics.rbearing > metrics.width)
22680 *right = metrics.rbearing - metrics.width;
22681 if (metrics.lbearing < 0)
22682 *left = - metrics.lbearing;
22688 /* Return the index of the first glyph preceding glyph string S that
22689 is overwritten by S because of S's left overhang. Value is -1
22690 if no glyphs are overwritten. */
22692 static int
22693 left_overwritten (struct glyph_string *s)
22695 int k;
22697 if (s->left_overhang)
22699 int x = 0, i;
22700 struct glyph *glyphs = s->row->glyphs[s->area];
22701 int first = s->first_glyph - glyphs;
22703 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22704 x -= glyphs[i].pixel_width;
22706 k = i + 1;
22708 else
22709 k = -1;
22711 return k;
22715 /* Return the index of the first glyph preceding glyph string S that
22716 is overwriting S because of its right overhang. Value is -1 if no
22717 glyph in front of S overwrites S. */
22719 static int
22720 left_overwriting (struct glyph_string *s)
22722 int i, k, x;
22723 struct glyph *glyphs = s->row->glyphs[s->area];
22724 int first = s->first_glyph - glyphs;
22726 k = -1;
22727 x = 0;
22728 for (i = first - 1; i >= 0; --i)
22730 int left, right;
22731 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22732 if (x + right > 0)
22733 k = i;
22734 x -= glyphs[i].pixel_width;
22737 return k;
22741 /* Return the index of the last glyph following glyph string S that is
22742 overwritten by S because of S's right overhang. Value is -1 if
22743 no such glyph is found. */
22745 static int
22746 right_overwritten (struct glyph_string *s)
22748 int k = -1;
22750 if (s->right_overhang)
22752 int x = 0, i;
22753 struct glyph *glyphs = s->row->glyphs[s->area];
22754 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22755 int end = s->row->used[s->area];
22757 for (i = first; i < end && s->right_overhang > x; ++i)
22758 x += glyphs[i].pixel_width;
22760 k = i;
22763 return k;
22767 /* Return the index of the last glyph following glyph string S that
22768 overwrites S because of its left overhang. Value is negative
22769 if no such glyph is found. */
22771 static int
22772 right_overwriting (struct glyph_string *s)
22774 int i, k, x;
22775 int end = s->row->used[s->area];
22776 struct glyph *glyphs = s->row->glyphs[s->area];
22777 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22779 k = -1;
22780 x = 0;
22781 for (i = first; i < end; ++i)
22783 int left, right;
22784 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22785 if (x - left < 0)
22786 k = i;
22787 x += glyphs[i].pixel_width;
22790 return k;
22794 /* Set background width of glyph string S. START is the index of the
22795 first glyph following S. LAST_X is the right-most x-position + 1
22796 in the drawing area. */
22798 static inline void
22799 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22801 /* If the face of this glyph string has to be drawn to the end of
22802 the drawing area, set S->extends_to_end_of_line_p. */
22804 if (start == s->row->used[s->area]
22805 && s->area == TEXT_AREA
22806 && ((s->row->fill_line_p
22807 && (s->hl == DRAW_NORMAL_TEXT
22808 || s->hl == DRAW_IMAGE_RAISED
22809 || s->hl == DRAW_IMAGE_SUNKEN))
22810 || s->hl == DRAW_MOUSE_FACE))
22811 s->extends_to_end_of_line_p = 1;
22813 /* If S extends its face to the end of the line, set its
22814 background_width to the distance to the right edge of the drawing
22815 area. */
22816 if (s->extends_to_end_of_line_p)
22817 s->background_width = last_x - s->x + 1;
22818 else
22819 s->background_width = s->width;
22823 /* Compute overhangs and x-positions for glyph string S and its
22824 predecessors, or successors. X is the starting x-position for S.
22825 BACKWARD_P non-zero means process predecessors. */
22827 static void
22828 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22830 if (backward_p)
22832 while (s)
22834 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22835 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22836 x -= s->width;
22837 s->x = x;
22838 s = s->prev;
22841 else
22843 while (s)
22845 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22846 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22847 s->x = x;
22848 x += s->width;
22849 s = s->next;
22856 /* The following macros are only called from draw_glyphs below.
22857 They reference the following parameters of that function directly:
22858 `w', `row', `area', and `overlap_p'
22859 as well as the following local variables:
22860 `s', `f', and `hdc' (in W32) */
22862 #ifdef HAVE_NTGUI
22863 /* On W32, silently add local `hdc' variable to argument list of
22864 init_glyph_string. */
22865 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22866 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
22867 #else
22868 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22869 init_glyph_string (s, char2b, w, row, area, start, hl)
22870 #endif
22872 /* Add a glyph string for a stretch glyph to the list of strings
22873 between HEAD and TAIL. START is the index of the stretch glyph in
22874 row area AREA of glyph row ROW. END is the index of the last glyph
22875 in that glyph row area. X is the current output position assigned
22876 to the new glyph string constructed. HL overrides that face of the
22877 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22878 is the right-most x-position of the drawing area. */
22880 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
22881 and below -- keep them on one line. */
22882 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22883 do \
22885 s = (struct glyph_string *) alloca (sizeof *s); \
22886 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22887 START = fill_stretch_glyph_string (s, START, END); \
22888 append_glyph_string (&HEAD, &TAIL, s); \
22889 s->x = (X); \
22891 while (0)
22894 /* Add a glyph string for an image glyph to the list of strings
22895 between HEAD and TAIL. START is the index of the image glyph in
22896 row area AREA of glyph row ROW. END is the index of the last glyph
22897 in that glyph row area. X is the current output position assigned
22898 to the new glyph string constructed. HL overrides that face of the
22899 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22900 is the right-most x-position of the drawing area. */
22902 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22903 do \
22905 s = (struct glyph_string *) alloca (sizeof *s); \
22906 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22907 fill_image_glyph_string (s); \
22908 append_glyph_string (&HEAD, &TAIL, s); \
22909 ++START; \
22910 s->x = (X); \
22912 while (0)
22915 /* Add a glyph string for a sequence of character glyphs to the list
22916 of strings between HEAD and TAIL. START is the index of the first
22917 glyph in row area AREA of glyph row ROW that is part of the new
22918 glyph string. END is the index of the last glyph in that glyph row
22919 area. X is the current output position assigned to the new glyph
22920 string constructed. HL overrides that face of the glyph; e.g. it
22921 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
22922 right-most x-position of the drawing area. */
22924 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22925 do \
22927 int face_id; \
22928 XChar2b *char2b; \
22930 face_id = (row)->glyphs[area][START].face_id; \
22932 s = (struct glyph_string *) alloca (sizeof *s); \
22933 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
22934 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22935 append_glyph_string (&HEAD, &TAIL, s); \
22936 s->x = (X); \
22937 START = fill_glyph_string (s, face_id, START, END, overlaps); \
22939 while (0)
22942 /* Add a glyph string for a composite sequence to the list of strings
22943 between HEAD and TAIL. START is the index of the first glyph in
22944 row area AREA of glyph row ROW that is part of the new glyph
22945 string. END is the index of the last glyph in that glyph row area.
22946 X is the current output position assigned to the new glyph string
22947 constructed. HL overrides that face of the glyph; e.g. it is
22948 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
22949 x-position of the drawing area. */
22951 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22952 do { \
22953 int face_id = (row)->glyphs[area][START].face_id; \
22954 struct face *base_face = FACE_FROM_ID (f, face_id); \
22955 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
22956 struct composition *cmp = composition_table[cmp_id]; \
22957 XChar2b *char2b; \
22958 struct glyph_string *first_s = NULL; \
22959 int n; \
22961 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
22963 /* Make glyph_strings for each glyph sequence that is drawable by \
22964 the same face, and append them to HEAD/TAIL. */ \
22965 for (n = 0; n < cmp->glyph_len;) \
22967 s = (struct glyph_string *) alloca (sizeof *s); \
22968 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22969 append_glyph_string (&(HEAD), &(TAIL), s); \
22970 s->cmp = cmp; \
22971 s->cmp_from = n; \
22972 s->x = (X); \
22973 if (n == 0) \
22974 first_s = s; \
22975 n = fill_composite_glyph_string (s, base_face, overlaps); \
22978 ++START; \
22979 s = first_s; \
22980 } while (0)
22983 /* Add a glyph string for a glyph-string sequence to the list of strings
22984 between HEAD and TAIL. */
22986 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22987 do { \
22988 int face_id; \
22989 XChar2b *char2b; \
22990 Lisp_Object gstring; \
22992 face_id = (row)->glyphs[area][START].face_id; \
22993 gstring = (composition_gstring_from_id \
22994 ((row)->glyphs[area][START].u.cmp.id)); \
22995 s = (struct glyph_string *) alloca (sizeof *s); \
22996 char2b = (XChar2b *) alloca ((sizeof *char2b) \
22997 * LGSTRING_GLYPH_LEN (gstring)); \
22998 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22999 append_glyph_string (&(HEAD), &(TAIL), s); \
23000 s->x = (X); \
23001 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23002 } while (0)
23005 /* Add a glyph string for a sequence of glyphless character's glyphs
23006 to the list of strings between HEAD and TAIL. The meanings of
23007 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23009 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23010 do \
23012 int face_id; \
23014 face_id = (row)->glyphs[area][START].face_id; \
23016 s = (struct glyph_string *) alloca (sizeof *s); \
23017 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23018 append_glyph_string (&HEAD, &TAIL, s); \
23019 s->x = (X); \
23020 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23021 overlaps); \
23023 while (0)
23026 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23027 of AREA of glyph row ROW on window W between indices START and END.
23028 HL overrides the face for drawing glyph strings, e.g. it is
23029 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23030 x-positions of the drawing area.
23032 This is an ugly monster macro construct because we must use alloca
23033 to allocate glyph strings (because draw_glyphs can be called
23034 asynchronously). */
23036 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23037 do \
23039 HEAD = TAIL = NULL; \
23040 while (START < END) \
23042 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23043 switch (first_glyph->type) \
23045 case CHAR_GLYPH: \
23046 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23047 HL, X, LAST_X); \
23048 break; \
23050 case COMPOSITE_GLYPH: \
23051 if (first_glyph->u.cmp.automatic) \
23052 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23053 HL, X, LAST_X); \
23054 else \
23055 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23056 HL, X, LAST_X); \
23057 break; \
23059 case STRETCH_GLYPH: \
23060 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23061 HL, X, LAST_X); \
23062 break; \
23064 case IMAGE_GLYPH: \
23065 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23066 HL, X, LAST_X); \
23067 break; \
23069 case GLYPHLESS_GLYPH: \
23070 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23071 HL, X, LAST_X); \
23072 break; \
23074 default: \
23075 abort (); \
23078 if (s) \
23080 set_glyph_string_background_width (s, START, LAST_X); \
23081 (X) += s->width; \
23084 } while (0)
23087 /* Draw glyphs between START and END in AREA of ROW on window W,
23088 starting at x-position X. X is relative to AREA in W. HL is a
23089 face-override with the following meaning:
23091 DRAW_NORMAL_TEXT draw normally
23092 DRAW_CURSOR draw in cursor face
23093 DRAW_MOUSE_FACE draw in mouse face.
23094 DRAW_INVERSE_VIDEO draw in mode line face
23095 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23096 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23098 If OVERLAPS is non-zero, draw only the foreground of characters and
23099 clip to the physical height of ROW. Non-zero value also defines
23100 the overlapping part to be drawn:
23102 OVERLAPS_PRED overlap with preceding rows
23103 OVERLAPS_SUCC overlap with succeeding rows
23104 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23105 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23107 Value is the x-position reached, relative to AREA of W. */
23109 static int
23110 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23111 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
23112 enum draw_glyphs_face hl, int overlaps)
23114 struct glyph_string *head, *tail;
23115 struct glyph_string *s;
23116 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23117 int i, j, x_reached, last_x, area_left = 0;
23118 struct frame *f = XFRAME (WINDOW_FRAME (w));
23119 DECLARE_HDC (hdc);
23121 ALLOCATE_HDC (hdc, f);
23123 /* Let's rather be paranoid than getting a SEGV. */
23124 end = min (end, row->used[area]);
23125 start = max (0, start);
23126 start = min (end, start);
23128 /* Translate X to frame coordinates. Set last_x to the right
23129 end of the drawing area. */
23130 if (row->full_width_p)
23132 /* X is relative to the left edge of W, without scroll bars
23133 or fringes. */
23134 area_left = WINDOW_LEFT_EDGE_X (w);
23135 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23137 else
23139 area_left = window_box_left (w, area);
23140 last_x = area_left + window_box_width (w, area);
23142 x += area_left;
23144 /* Build a doubly-linked list of glyph_string structures between
23145 head and tail from what we have to draw. Note that the macro
23146 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23147 the reason we use a separate variable `i'. */
23148 i = start;
23149 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23150 if (tail)
23151 x_reached = tail->x + tail->background_width;
23152 else
23153 x_reached = x;
23155 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23156 the row, redraw some glyphs in front or following the glyph
23157 strings built above. */
23158 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23160 struct glyph_string *h, *t;
23161 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23162 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23163 int check_mouse_face = 0;
23164 int dummy_x = 0;
23166 /* If mouse highlighting is on, we may need to draw adjacent
23167 glyphs using mouse-face highlighting. */
23168 if (area == TEXT_AREA && row->mouse_face_p)
23170 struct glyph_row *mouse_beg_row, *mouse_end_row;
23172 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23173 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23175 if (row >= mouse_beg_row && row <= mouse_end_row)
23177 check_mouse_face = 1;
23178 mouse_beg_col = (row == mouse_beg_row)
23179 ? hlinfo->mouse_face_beg_col : 0;
23180 mouse_end_col = (row == mouse_end_row)
23181 ? hlinfo->mouse_face_end_col
23182 : row->used[TEXT_AREA];
23186 /* Compute overhangs for all glyph strings. */
23187 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23188 for (s = head; s; s = s->next)
23189 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23191 /* Prepend glyph strings for glyphs in front of the first glyph
23192 string that are overwritten because of the first glyph
23193 string's left overhang. The background of all strings
23194 prepended must be drawn because the first glyph string
23195 draws over it. */
23196 i = left_overwritten (head);
23197 if (i >= 0)
23199 enum draw_glyphs_face overlap_hl;
23201 /* If this row contains mouse highlighting, attempt to draw
23202 the overlapped glyphs with the correct highlight. This
23203 code fails if the overlap encompasses more than one glyph
23204 and mouse-highlight spans only some of these glyphs.
23205 However, making it work perfectly involves a lot more
23206 code, and I don't know if the pathological case occurs in
23207 practice, so we'll stick to this for now. --- cyd */
23208 if (check_mouse_face
23209 && mouse_beg_col < start && mouse_end_col > i)
23210 overlap_hl = DRAW_MOUSE_FACE;
23211 else
23212 overlap_hl = DRAW_NORMAL_TEXT;
23214 j = i;
23215 BUILD_GLYPH_STRINGS (j, start, h, t,
23216 overlap_hl, dummy_x, last_x);
23217 start = i;
23218 compute_overhangs_and_x (t, head->x, 1);
23219 prepend_glyph_string_lists (&head, &tail, h, t);
23220 clip_head = head;
23223 /* Prepend glyph strings for glyphs in front of the first glyph
23224 string that overwrite that glyph string because of their
23225 right overhang. For these strings, only the foreground must
23226 be drawn, because it draws over the glyph string at `head'.
23227 The background must not be drawn because this would overwrite
23228 right overhangs of preceding glyphs for which no glyph
23229 strings exist. */
23230 i = left_overwriting (head);
23231 if (i >= 0)
23233 enum draw_glyphs_face overlap_hl;
23235 if (check_mouse_face
23236 && mouse_beg_col < start && mouse_end_col > i)
23237 overlap_hl = DRAW_MOUSE_FACE;
23238 else
23239 overlap_hl = DRAW_NORMAL_TEXT;
23241 clip_head = head;
23242 BUILD_GLYPH_STRINGS (i, start, h, t,
23243 overlap_hl, dummy_x, last_x);
23244 for (s = h; s; s = s->next)
23245 s->background_filled_p = 1;
23246 compute_overhangs_and_x (t, head->x, 1);
23247 prepend_glyph_string_lists (&head, &tail, h, t);
23250 /* Append glyphs strings for glyphs following the last glyph
23251 string tail that are overwritten by tail. The background of
23252 these strings has to be drawn because tail's foreground draws
23253 over it. */
23254 i = right_overwritten (tail);
23255 if (i >= 0)
23257 enum draw_glyphs_face overlap_hl;
23259 if (check_mouse_face
23260 && mouse_beg_col < i && mouse_end_col > end)
23261 overlap_hl = DRAW_MOUSE_FACE;
23262 else
23263 overlap_hl = DRAW_NORMAL_TEXT;
23265 BUILD_GLYPH_STRINGS (end, i, h, t,
23266 overlap_hl, x, last_x);
23267 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23268 we don't have `end = i;' here. */
23269 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23270 append_glyph_string_lists (&head, &tail, h, t);
23271 clip_tail = tail;
23274 /* Append glyph strings for glyphs following the last glyph
23275 string tail that overwrite tail. The foreground of such
23276 glyphs has to be drawn because it writes into the background
23277 of tail. The background must not be drawn because it could
23278 paint over the foreground of following glyphs. */
23279 i = right_overwriting (tail);
23280 if (i >= 0)
23282 enum draw_glyphs_face overlap_hl;
23283 if (check_mouse_face
23284 && mouse_beg_col < i && mouse_end_col > end)
23285 overlap_hl = DRAW_MOUSE_FACE;
23286 else
23287 overlap_hl = DRAW_NORMAL_TEXT;
23289 clip_tail = tail;
23290 i++; /* We must include the Ith glyph. */
23291 BUILD_GLYPH_STRINGS (end, i, h, t,
23292 overlap_hl, x, last_x);
23293 for (s = h; s; s = s->next)
23294 s->background_filled_p = 1;
23295 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23296 append_glyph_string_lists (&head, &tail, h, t);
23298 if (clip_head || clip_tail)
23299 for (s = head; s; s = s->next)
23301 s->clip_head = clip_head;
23302 s->clip_tail = clip_tail;
23306 /* Draw all strings. */
23307 for (s = head; s; s = s->next)
23308 FRAME_RIF (f)->draw_glyph_string (s);
23310 #ifndef HAVE_NS
23311 /* When focus a sole frame and move horizontally, this sets on_p to 0
23312 causing a failure to erase prev cursor position. */
23313 if (area == TEXT_AREA
23314 && !row->full_width_p
23315 /* When drawing overlapping rows, only the glyph strings'
23316 foreground is drawn, which doesn't erase a cursor
23317 completely. */
23318 && !overlaps)
23320 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23321 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23322 : (tail ? tail->x + tail->background_width : x));
23323 x0 -= area_left;
23324 x1 -= area_left;
23326 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23327 row->y, MATRIX_ROW_BOTTOM_Y (row));
23329 #endif
23331 /* Value is the x-position up to which drawn, relative to AREA of W.
23332 This doesn't include parts drawn because of overhangs. */
23333 if (row->full_width_p)
23334 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23335 else
23336 x_reached -= area_left;
23338 RELEASE_HDC (hdc, f);
23340 return x_reached;
23343 /* Expand row matrix if too narrow. Don't expand if area
23344 is not present. */
23346 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23348 if (!fonts_changed_p \
23349 && (it->glyph_row->glyphs[area] \
23350 < it->glyph_row->glyphs[area + 1])) \
23352 it->w->ncols_scale_factor++; \
23353 fonts_changed_p = 1; \
23357 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23358 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23360 static inline void
23361 append_glyph (struct it *it)
23363 struct glyph *glyph;
23364 enum glyph_row_area area = it->area;
23366 xassert (it->glyph_row);
23367 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23369 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23370 if (glyph < it->glyph_row->glyphs[area + 1])
23372 /* If the glyph row is reversed, we need to prepend the glyph
23373 rather than append it. */
23374 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23376 struct glyph *g;
23378 /* Make room for the additional glyph. */
23379 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23380 g[1] = *g;
23381 glyph = it->glyph_row->glyphs[area];
23383 glyph->charpos = CHARPOS (it->position);
23384 glyph->object = it->object;
23385 if (it->pixel_width > 0)
23387 glyph->pixel_width = it->pixel_width;
23388 glyph->padding_p = 0;
23390 else
23392 /* Assure at least 1-pixel width. Otherwise, cursor can't
23393 be displayed correctly. */
23394 glyph->pixel_width = 1;
23395 glyph->padding_p = 1;
23397 glyph->ascent = it->ascent;
23398 glyph->descent = it->descent;
23399 glyph->voffset = it->voffset;
23400 glyph->type = CHAR_GLYPH;
23401 glyph->avoid_cursor_p = it->avoid_cursor_p;
23402 glyph->multibyte_p = it->multibyte_p;
23403 glyph->left_box_line_p = it->start_of_box_run_p;
23404 glyph->right_box_line_p = it->end_of_box_run_p;
23405 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23406 || it->phys_descent > it->descent);
23407 glyph->glyph_not_available_p = it->glyph_not_available_p;
23408 glyph->face_id = it->face_id;
23409 glyph->u.ch = it->char_to_display;
23410 glyph->slice.img = null_glyph_slice;
23411 glyph->font_type = FONT_TYPE_UNKNOWN;
23412 if (it->bidi_p)
23414 glyph->resolved_level = it->bidi_it.resolved_level;
23415 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23416 abort ();
23417 glyph->bidi_type = it->bidi_it.type;
23419 else
23421 glyph->resolved_level = 0;
23422 glyph->bidi_type = UNKNOWN_BT;
23424 ++it->glyph_row->used[area];
23426 else
23427 IT_EXPAND_MATRIX_WIDTH (it, area);
23430 /* Store one glyph for the composition IT->cmp_it.id in
23431 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23432 non-null. */
23434 static inline void
23435 append_composite_glyph (struct it *it)
23437 struct glyph *glyph;
23438 enum glyph_row_area area = it->area;
23440 xassert (it->glyph_row);
23442 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23443 if (glyph < it->glyph_row->glyphs[area + 1])
23445 /* If the glyph row is reversed, we need to prepend the glyph
23446 rather than append it. */
23447 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23449 struct glyph *g;
23451 /* Make room for the new glyph. */
23452 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23453 g[1] = *g;
23454 glyph = it->glyph_row->glyphs[it->area];
23456 glyph->charpos = it->cmp_it.charpos;
23457 glyph->object = it->object;
23458 glyph->pixel_width = it->pixel_width;
23459 glyph->ascent = it->ascent;
23460 glyph->descent = it->descent;
23461 glyph->voffset = it->voffset;
23462 glyph->type = COMPOSITE_GLYPH;
23463 if (it->cmp_it.ch < 0)
23465 glyph->u.cmp.automatic = 0;
23466 glyph->u.cmp.id = it->cmp_it.id;
23467 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23469 else
23471 glyph->u.cmp.automatic = 1;
23472 glyph->u.cmp.id = it->cmp_it.id;
23473 glyph->slice.cmp.from = it->cmp_it.from;
23474 glyph->slice.cmp.to = it->cmp_it.to - 1;
23476 glyph->avoid_cursor_p = it->avoid_cursor_p;
23477 glyph->multibyte_p = it->multibyte_p;
23478 glyph->left_box_line_p = it->start_of_box_run_p;
23479 glyph->right_box_line_p = it->end_of_box_run_p;
23480 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23481 || it->phys_descent > it->descent);
23482 glyph->padding_p = 0;
23483 glyph->glyph_not_available_p = 0;
23484 glyph->face_id = it->face_id;
23485 glyph->font_type = FONT_TYPE_UNKNOWN;
23486 if (it->bidi_p)
23488 glyph->resolved_level = it->bidi_it.resolved_level;
23489 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23490 abort ();
23491 glyph->bidi_type = it->bidi_it.type;
23493 ++it->glyph_row->used[area];
23495 else
23496 IT_EXPAND_MATRIX_WIDTH (it, area);
23500 /* Change IT->ascent and IT->height according to the setting of
23501 IT->voffset. */
23503 static inline void
23504 take_vertical_position_into_account (struct it *it)
23506 if (it->voffset)
23508 if (it->voffset < 0)
23509 /* Increase the ascent so that we can display the text higher
23510 in the line. */
23511 it->ascent -= it->voffset;
23512 else
23513 /* Increase the descent so that we can display the text lower
23514 in the line. */
23515 it->descent += it->voffset;
23520 /* Produce glyphs/get display metrics for the image IT is loaded with.
23521 See the description of struct display_iterator in dispextern.h for
23522 an overview of struct display_iterator. */
23524 static void
23525 produce_image_glyph (struct it *it)
23527 struct image *img;
23528 struct face *face;
23529 int glyph_ascent, crop;
23530 struct glyph_slice slice;
23532 xassert (it->what == IT_IMAGE);
23534 face = FACE_FROM_ID (it->f, it->face_id);
23535 xassert (face);
23536 /* Make sure X resources of the face is loaded. */
23537 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23539 if (it->image_id < 0)
23541 /* Fringe bitmap. */
23542 it->ascent = it->phys_ascent = 0;
23543 it->descent = it->phys_descent = 0;
23544 it->pixel_width = 0;
23545 it->nglyphs = 0;
23546 return;
23549 img = IMAGE_FROM_ID (it->f, it->image_id);
23550 xassert (img);
23551 /* Make sure X resources of the image is loaded. */
23552 prepare_image_for_display (it->f, img);
23554 slice.x = slice.y = 0;
23555 slice.width = img->width;
23556 slice.height = img->height;
23558 if (INTEGERP (it->slice.x))
23559 slice.x = XINT (it->slice.x);
23560 else if (FLOATP (it->slice.x))
23561 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23563 if (INTEGERP (it->slice.y))
23564 slice.y = XINT (it->slice.y);
23565 else if (FLOATP (it->slice.y))
23566 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23568 if (INTEGERP (it->slice.width))
23569 slice.width = XINT (it->slice.width);
23570 else if (FLOATP (it->slice.width))
23571 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23573 if (INTEGERP (it->slice.height))
23574 slice.height = XINT (it->slice.height);
23575 else if (FLOATP (it->slice.height))
23576 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23578 if (slice.x >= img->width)
23579 slice.x = img->width;
23580 if (slice.y >= img->height)
23581 slice.y = img->height;
23582 if (slice.x + slice.width >= img->width)
23583 slice.width = img->width - slice.x;
23584 if (slice.y + slice.height > img->height)
23585 slice.height = img->height - slice.y;
23587 if (slice.width == 0 || slice.height == 0)
23588 return;
23590 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23592 it->descent = slice.height - glyph_ascent;
23593 if (slice.y == 0)
23594 it->descent += img->vmargin;
23595 if (slice.y + slice.height == img->height)
23596 it->descent += img->vmargin;
23597 it->phys_descent = it->descent;
23599 it->pixel_width = slice.width;
23600 if (slice.x == 0)
23601 it->pixel_width += img->hmargin;
23602 if (slice.x + slice.width == img->width)
23603 it->pixel_width += img->hmargin;
23605 /* It's quite possible for images to have an ascent greater than
23606 their height, so don't get confused in that case. */
23607 if (it->descent < 0)
23608 it->descent = 0;
23610 it->nglyphs = 1;
23612 if (face->box != FACE_NO_BOX)
23614 if (face->box_line_width > 0)
23616 if (slice.y == 0)
23617 it->ascent += face->box_line_width;
23618 if (slice.y + slice.height == img->height)
23619 it->descent += face->box_line_width;
23622 if (it->start_of_box_run_p && slice.x == 0)
23623 it->pixel_width += eabs (face->box_line_width);
23624 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23625 it->pixel_width += eabs (face->box_line_width);
23628 take_vertical_position_into_account (it);
23630 /* Automatically crop wide image glyphs at right edge so we can
23631 draw the cursor on same display row. */
23632 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23633 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23635 it->pixel_width -= crop;
23636 slice.width -= crop;
23639 if (it->glyph_row)
23641 struct glyph *glyph;
23642 enum glyph_row_area area = it->area;
23644 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23645 if (glyph < it->glyph_row->glyphs[area + 1])
23647 glyph->charpos = CHARPOS (it->position);
23648 glyph->object = it->object;
23649 glyph->pixel_width = it->pixel_width;
23650 glyph->ascent = glyph_ascent;
23651 glyph->descent = it->descent;
23652 glyph->voffset = it->voffset;
23653 glyph->type = IMAGE_GLYPH;
23654 glyph->avoid_cursor_p = it->avoid_cursor_p;
23655 glyph->multibyte_p = it->multibyte_p;
23656 glyph->left_box_line_p = it->start_of_box_run_p;
23657 glyph->right_box_line_p = it->end_of_box_run_p;
23658 glyph->overlaps_vertically_p = 0;
23659 glyph->padding_p = 0;
23660 glyph->glyph_not_available_p = 0;
23661 glyph->face_id = it->face_id;
23662 glyph->u.img_id = img->id;
23663 glyph->slice.img = slice;
23664 glyph->font_type = FONT_TYPE_UNKNOWN;
23665 if (it->bidi_p)
23667 glyph->resolved_level = it->bidi_it.resolved_level;
23668 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23669 abort ();
23670 glyph->bidi_type = it->bidi_it.type;
23672 ++it->glyph_row->used[area];
23674 else
23675 IT_EXPAND_MATRIX_WIDTH (it, area);
23680 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23681 of the glyph, WIDTH and HEIGHT are the width and height of the
23682 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23684 static void
23685 append_stretch_glyph (struct it *it, Lisp_Object object,
23686 int width, int height, int ascent)
23688 struct glyph *glyph;
23689 enum glyph_row_area area = it->area;
23691 xassert (ascent >= 0 && ascent <= height);
23693 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23694 if (glyph < it->glyph_row->glyphs[area + 1])
23696 /* If the glyph row is reversed, we need to prepend the glyph
23697 rather than append it. */
23698 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23700 struct glyph *g;
23702 /* Make room for the additional glyph. */
23703 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23704 g[1] = *g;
23705 glyph = it->glyph_row->glyphs[area];
23707 glyph->charpos = CHARPOS (it->position);
23708 glyph->object = object;
23709 glyph->pixel_width = width;
23710 glyph->ascent = ascent;
23711 glyph->descent = height - ascent;
23712 glyph->voffset = it->voffset;
23713 glyph->type = STRETCH_GLYPH;
23714 glyph->avoid_cursor_p = it->avoid_cursor_p;
23715 glyph->multibyte_p = it->multibyte_p;
23716 glyph->left_box_line_p = it->start_of_box_run_p;
23717 glyph->right_box_line_p = it->end_of_box_run_p;
23718 glyph->overlaps_vertically_p = 0;
23719 glyph->padding_p = 0;
23720 glyph->glyph_not_available_p = 0;
23721 glyph->face_id = it->face_id;
23722 glyph->u.stretch.ascent = ascent;
23723 glyph->u.stretch.height = height;
23724 glyph->slice.img = null_glyph_slice;
23725 glyph->font_type = FONT_TYPE_UNKNOWN;
23726 if (it->bidi_p)
23728 glyph->resolved_level = it->bidi_it.resolved_level;
23729 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23730 abort ();
23731 glyph->bidi_type = it->bidi_it.type;
23733 else
23735 glyph->resolved_level = 0;
23736 glyph->bidi_type = UNKNOWN_BT;
23738 ++it->glyph_row->used[area];
23740 else
23741 IT_EXPAND_MATRIX_WIDTH (it, area);
23744 #endif /* HAVE_WINDOW_SYSTEM */
23746 /* Produce a stretch glyph for iterator IT. IT->object is the value
23747 of the glyph property displayed. The value must be a list
23748 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23749 being recognized:
23751 1. `:width WIDTH' specifies that the space should be WIDTH *
23752 canonical char width wide. WIDTH may be an integer or floating
23753 point number.
23755 2. `:relative-width FACTOR' specifies that the width of the stretch
23756 should be computed from the width of the first character having the
23757 `glyph' property, and should be FACTOR times that width.
23759 3. `:align-to HPOS' specifies that the space should be wide enough
23760 to reach HPOS, a value in canonical character units.
23762 Exactly one of the above pairs must be present.
23764 4. `:height HEIGHT' specifies that the height of the stretch produced
23765 should be HEIGHT, measured in canonical character units.
23767 5. `:relative-height FACTOR' specifies that the height of the
23768 stretch should be FACTOR times the height of the characters having
23769 the glyph property.
23771 Either none or exactly one of 4 or 5 must be present.
23773 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23774 of the stretch should be used for the ascent of the stretch.
23775 ASCENT must be in the range 0 <= ASCENT <= 100. */
23777 void
23778 produce_stretch_glyph (struct it *it)
23780 /* (space :width WIDTH :height HEIGHT ...) */
23781 Lisp_Object prop, plist;
23782 int width = 0, height = 0, align_to = -1;
23783 int zero_width_ok_p = 0;
23784 int ascent = 0;
23785 double tem;
23786 struct face *face = NULL;
23787 struct font *font = NULL;
23789 #ifdef HAVE_WINDOW_SYSTEM
23790 int zero_height_ok_p = 0;
23792 if (FRAME_WINDOW_P (it->f))
23794 face = FACE_FROM_ID (it->f, it->face_id);
23795 font = face->font ? face->font : FRAME_FONT (it->f);
23796 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23798 #endif
23800 /* List should start with `space'. */
23801 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
23802 plist = XCDR (it->object);
23804 /* Compute the width of the stretch. */
23805 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
23806 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
23808 /* Absolute width `:width WIDTH' specified and valid. */
23809 zero_width_ok_p = 1;
23810 width = (int)tem;
23812 #ifdef HAVE_WINDOW_SYSTEM
23813 else if (FRAME_WINDOW_P (it->f)
23814 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
23816 /* Relative width `:relative-width FACTOR' specified and valid.
23817 Compute the width of the characters having the `glyph'
23818 property. */
23819 struct it it2;
23820 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
23822 it2 = *it;
23823 if (it->multibyte_p)
23824 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
23825 else
23827 it2.c = it2.char_to_display = *p, it2.len = 1;
23828 if (! ASCII_CHAR_P (it2.c))
23829 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
23832 it2.glyph_row = NULL;
23833 it2.what = IT_CHARACTER;
23834 x_produce_glyphs (&it2);
23835 width = NUMVAL (prop) * it2.pixel_width;
23837 #endif /* HAVE_WINDOW_SYSTEM */
23838 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
23839 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
23841 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
23842 align_to = (align_to < 0
23844 : align_to - window_box_left_offset (it->w, TEXT_AREA));
23845 else if (align_to < 0)
23846 align_to = window_box_left_offset (it->w, TEXT_AREA);
23847 width = max (0, (int)tem + align_to - it->current_x);
23848 zero_width_ok_p = 1;
23850 else
23851 /* Nothing specified -> width defaults to canonical char width. */
23852 width = FRAME_COLUMN_WIDTH (it->f);
23854 if (width <= 0 && (width < 0 || !zero_width_ok_p))
23855 width = 1;
23857 #ifdef HAVE_WINDOW_SYSTEM
23858 /* Compute height. */
23859 if (FRAME_WINDOW_P (it->f))
23861 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
23862 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23864 height = (int)tem;
23865 zero_height_ok_p = 1;
23867 else if (prop = Fplist_get (plist, QCrelative_height),
23868 NUMVAL (prop) > 0)
23869 height = FONT_HEIGHT (font) * NUMVAL (prop);
23870 else
23871 height = FONT_HEIGHT (font);
23873 if (height <= 0 && (height < 0 || !zero_height_ok_p))
23874 height = 1;
23876 /* Compute percentage of height used for ascent. If
23877 `:ascent ASCENT' is present and valid, use that. Otherwise,
23878 derive the ascent from the font in use. */
23879 if (prop = Fplist_get (plist, QCascent),
23880 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
23881 ascent = height * NUMVAL (prop) / 100.0;
23882 else if (!NILP (prop)
23883 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23884 ascent = min (max (0, (int)tem), height);
23885 else
23886 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
23888 else
23889 #endif /* HAVE_WINDOW_SYSTEM */
23890 height = 1;
23892 if (width > 0 && it->line_wrap != TRUNCATE
23893 && it->current_x + width > it->last_visible_x)
23895 width = it->last_visible_x - it->current_x;
23896 #ifdef HAVE_WINDOW_SYSTEM
23897 /* Subtract one more pixel from the stretch width, but only on
23898 GUI frames, since on a TTY each glyph is one "pixel" wide. */
23899 width -= FRAME_WINDOW_P (it->f);
23900 #endif
23903 if (width > 0 && height > 0 && it->glyph_row)
23905 Lisp_Object o_object = it->object;
23906 Lisp_Object object = it->stack[it->sp - 1].string;
23907 int n = width;
23909 if (!STRINGP (object))
23910 object = it->w->buffer;
23911 #ifdef HAVE_WINDOW_SYSTEM
23912 if (FRAME_WINDOW_P (it->f))
23913 append_stretch_glyph (it, object, width, height, ascent);
23914 else
23915 #endif
23917 it->object = object;
23918 it->char_to_display = ' ';
23919 it->pixel_width = it->len = 1;
23920 while (n--)
23921 tty_append_glyph (it);
23922 it->object = o_object;
23926 it->pixel_width = width;
23927 #ifdef HAVE_WINDOW_SYSTEM
23928 if (FRAME_WINDOW_P (it->f))
23930 it->ascent = it->phys_ascent = ascent;
23931 it->descent = it->phys_descent = height - it->ascent;
23932 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
23933 take_vertical_position_into_account (it);
23935 else
23936 #endif
23937 it->nglyphs = width;
23940 #ifdef HAVE_WINDOW_SYSTEM
23942 /* Calculate line-height and line-spacing properties.
23943 An integer value specifies explicit pixel value.
23944 A float value specifies relative value to current face height.
23945 A cons (float . face-name) specifies relative value to
23946 height of specified face font.
23948 Returns height in pixels, or nil. */
23951 static Lisp_Object
23952 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
23953 int boff, int override)
23955 Lisp_Object face_name = Qnil;
23956 int ascent, descent, height;
23958 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
23959 return val;
23961 if (CONSP (val))
23963 face_name = XCAR (val);
23964 val = XCDR (val);
23965 if (!NUMBERP (val))
23966 val = make_number (1);
23967 if (NILP (face_name))
23969 height = it->ascent + it->descent;
23970 goto scale;
23974 if (NILP (face_name))
23976 font = FRAME_FONT (it->f);
23977 boff = FRAME_BASELINE_OFFSET (it->f);
23979 else if (EQ (face_name, Qt))
23981 override = 0;
23983 else
23985 int face_id;
23986 struct face *face;
23988 face_id = lookup_named_face (it->f, face_name, 0);
23989 if (face_id < 0)
23990 return make_number (-1);
23992 face = FACE_FROM_ID (it->f, face_id);
23993 font = face->font;
23994 if (font == NULL)
23995 return make_number (-1);
23996 boff = font->baseline_offset;
23997 if (font->vertical_centering)
23998 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24001 ascent = FONT_BASE (font) + boff;
24002 descent = FONT_DESCENT (font) - boff;
24004 if (override)
24006 it->override_ascent = ascent;
24007 it->override_descent = descent;
24008 it->override_boff = boff;
24011 height = ascent + descent;
24013 scale:
24014 if (FLOATP (val))
24015 height = (int)(XFLOAT_DATA (val) * height);
24016 else if (INTEGERP (val))
24017 height *= XINT (val);
24019 return make_number (height);
24023 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24024 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24025 and only if this is for a character for which no font was found.
24027 If the display method (it->glyphless_method) is
24028 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24029 length of the acronym or the hexadecimal string, UPPER_XOFF and
24030 UPPER_YOFF are pixel offsets for the upper part of the string,
24031 LOWER_XOFF and LOWER_YOFF are for the lower part.
24033 For the other display methods, LEN through LOWER_YOFF are zero. */
24035 static void
24036 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24037 short upper_xoff, short upper_yoff,
24038 short lower_xoff, short lower_yoff)
24040 struct glyph *glyph;
24041 enum glyph_row_area area = it->area;
24043 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24044 if (glyph < it->glyph_row->glyphs[area + 1])
24046 /* If the glyph row is reversed, we need to prepend the glyph
24047 rather than append it. */
24048 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24050 struct glyph *g;
24052 /* Make room for the additional glyph. */
24053 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24054 g[1] = *g;
24055 glyph = it->glyph_row->glyphs[area];
24057 glyph->charpos = CHARPOS (it->position);
24058 glyph->object = it->object;
24059 glyph->pixel_width = it->pixel_width;
24060 glyph->ascent = it->ascent;
24061 glyph->descent = it->descent;
24062 glyph->voffset = it->voffset;
24063 glyph->type = GLYPHLESS_GLYPH;
24064 glyph->u.glyphless.method = it->glyphless_method;
24065 glyph->u.glyphless.for_no_font = for_no_font;
24066 glyph->u.glyphless.len = len;
24067 glyph->u.glyphless.ch = it->c;
24068 glyph->slice.glyphless.upper_xoff = upper_xoff;
24069 glyph->slice.glyphless.upper_yoff = upper_yoff;
24070 glyph->slice.glyphless.lower_xoff = lower_xoff;
24071 glyph->slice.glyphless.lower_yoff = lower_yoff;
24072 glyph->avoid_cursor_p = it->avoid_cursor_p;
24073 glyph->multibyte_p = it->multibyte_p;
24074 glyph->left_box_line_p = it->start_of_box_run_p;
24075 glyph->right_box_line_p = it->end_of_box_run_p;
24076 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24077 || it->phys_descent > it->descent);
24078 glyph->padding_p = 0;
24079 glyph->glyph_not_available_p = 0;
24080 glyph->face_id = face_id;
24081 glyph->font_type = FONT_TYPE_UNKNOWN;
24082 if (it->bidi_p)
24084 glyph->resolved_level = it->bidi_it.resolved_level;
24085 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24086 abort ();
24087 glyph->bidi_type = it->bidi_it.type;
24089 ++it->glyph_row->used[area];
24091 else
24092 IT_EXPAND_MATRIX_WIDTH (it, area);
24096 /* Produce a glyph for a glyphless character for iterator IT.
24097 IT->glyphless_method specifies which method to use for displaying
24098 the character. See the description of enum
24099 glyphless_display_method in dispextern.h for the detail.
24101 FOR_NO_FONT is nonzero if and only if this is for a character for
24102 which no font was found. ACRONYM, if non-nil, is an acronym string
24103 for the character. */
24105 static void
24106 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24108 int face_id;
24109 struct face *face;
24110 struct font *font;
24111 int base_width, base_height, width, height;
24112 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24113 int len;
24115 /* Get the metrics of the base font. We always refer to the current
24116 ASCII face. */
24117 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24118 font = face->font ? face->font : FRAME_FONT (it->f);
24119 it->ascent = FONT_BASE (font) + font->baseline_offset;
24120 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24121 base_height = it->ascent + it->descent;
24122 base_width = font->average_width;
24124 /* Get a face ID for the glyph by utilizing a cache (the same way as
24125 done for `escape-glyph' in get_next_display_element). */
24126 if (it->f == last_glyphless_glyph_frame
24127 && it->face_id == last_glyphless_glyph_face_id)
24129 face_id = last_glyphless_glyph_merged_face_id;
24131 else
24133 /* Merge the `glyphless-char' face into the current face. */
24134 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24135 last_glyphless_glyph_frame = it->f;
24136 last_glyphless_glyph_face_id = it->face_id;
24137 last_glyphless_glyph_merged_face_id = face_id;
24140 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24142 it->pixel_width = THIN_SPACE_WIDTH;
24143 len = 0;
24144 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24146 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24148 width = CHAR_WIDTH (it->c);
24149 if (width == 0)
24150 width = 1;
24151 else if (width > 4)
24152 width = 4;
24153 it->pixel_width = base_width * width;
24154 len = 0;
24155 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24157 else
24159 char buf[7];
24160 const char *str;
24161 unsigned int code[6];
24162 int upper_len;
24163 int ascent, descent;
24164 struct font_metrics metrics_upper, metrics_lower;
24166 face = FACE_FROM_ID (it->f, face_id);
24167 font = face->font ? face->font : FRAME_FONT (it->f);
24168 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24170 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24172 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24173 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24174 if (CONSP (acronym))
24175 acronym = XCAR (acronym);
24176 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24178 else
24180 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24181 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24182 str = buf;
24184 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24185 code[len] = font->driver->encode_char (font, str[len]);
24186 upper_len = (len + 1) / 2;
24187 font->driver->text_extents (font, code, upper_len,
24188 &metrics_upper);
24189 font->driver->text_extents (font, code + upper_len, len - upper_len,
24190 &metrics_lower);
24194 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24195 width = max (metrics_upper.width, metrics_lower.width) + 4;
24196 upper_xoff = upper_yoff = 2; /* the typical case */
24197 if (base_width >= width)
24199 /* Align the upper to the left, the lower to the right. */
24200 it->pixel_width = base_width;
24201 lower_xoff = base_width - 2 - metrics_lower.width;
24203 else
24205 /* Center the shorter one. */
24206 it->pixel_width = width;
24207 if (metrics_upper.width >= metrics_lower.width)
24208 lower_xoff = (width - metrics_lower.width) / 2;
24209 else
24211 /* FIXME: This code doesn't look right. It formerly was
24212 missing the "lower_xoff = 0;", which couldn't have
24213 been right since it left lower_xoff uninitialized. */
24214 lower_xoff = 0;
24215 upper_xoff = (width - metrics_upper.width) / 2;
24219 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24220 top, bottom, and between upper and lower strings. */
24221 height = (metrics_upper.ascent + metrics_upper.descent
24222 + metrics_lower.ascent + metrics_lower.descent) + 5;
24223 /* Center vertically.
24224 H:base_height, D:base_descent
24225 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24227 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24228 descent = D - H/2 + h/2;
24229 lower_yoff = descent - 2 - ld;
24230 upper_yoff = lower_yoff - la - 1 - ud; */
24231 ascent = - (it->descent - (base_height + height + 1) / 2);
24232 descent = it->descent - (base_height - height) / 2;
24233 lower_yoff = descent - 2 - metrics_lower.descent;
24234 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24235 - metrics_upper.descent);
24236 /* Don't make the height shorter than the base height. */
24237 if (height > base_height)
24239 it->ascent = ascent;
24240 it->descent = descent;
24244 it->phys_ascent = it->ascent;
24245 it->phys_descent = it->descent;
24246 if (it->glyph_row)
24247 append_glyphless_glyph (it, face_id, for_no_font, len,
24248 upper_xoff, upper_yoff,
24249 lower_xoff, lower_yoff);
24250 it->nglyphs = 1;
24251 take_vertical_position_into_account (it);
24255 /* RIF:
24256 Produce glyphs/get display metrics for the display element IT is
24257 loaded with. See the description of struct it in dispextern.h
24258 for an overview of struct it. */
24260 void
24261 x_produce_glyphs (struct it *it)
24263 int extra_line_spacing = it->extra_line_spacing;
24265 it->glyph_not_available_p = 0;
24267 if (it->what == IT_CHARACTER)
24269 XChar2b char2b;
24270 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24271 struct font *font = face->font;
24272 struct font_metrics *pcm = NULL;
24273 int boff; /* baseline offset */
24275 if (font == NULL)
24277 /* When no suitable font is found, display this character by
24278 the method specified in the first extra slot of
24279 Vglyphless_char_display. */
24280 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24282 xassert (it->what == IT_GLYPHLESS);
24283 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24284 goto done;
24287 boff = font->baseline_offset;
24288 if (font->vertical_centering)
24289 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24291 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24293 int stretched_p;
24295 it->nglyphs = 1;
24297 if (it->override_ascent >= 0)
24299 it->ascent = it->override_ascent;
24300 it->descent = it->override_descent;
24301 boff = it->override_boff;
24303 else
24305 it->ascent = FONT_BASE (font) + boff;
24306 it->descent = FONT_DESCENT (font) - boff;
24309 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24311 pcm = get_per_char_metric (font, &char2b);
24312 if (pcm->width == 0
24313 && pcm->rbearing == 0 && pcm->lbearing == 0)
24314 pcm = NULL;
24317 if (pcm)
24319 it->phys_ascent = pcm->ascent + boff;
24320 it->phys_descent = pcm->descent - boff;
24321 it->pixel_width = pcm->width;
24323 else
24325 it->glyph_not_available_p = 1;
24326 it->phys_ascent = it->ascent;
24327 it->phys_descent = it->descent;
24328 it->pixel_width = font->space_width;
24331 if (it->constrain_row_ascent_descent_p)
24333 if (it->descent > it->max_descent)
24335 it->ascent += it->descent - it->max_descent;
24336 it->descent = it->max_descent;
24338 if (it->ascent > it->max_ascent)
24340 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24341 it->ascent = it->max_ascent;
24343 it->phys_ascent = min (it->phys_ascent, it->ascent);
24344 it->phys_descent = min (it->phys_descent, it->descent);
24345 extra_line_spacing = 0;
24348 /* If this is a space inside a region of text with
24349 `space-width' property, change its width. */
24350 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24351 if (stretched_p)
24352 it->pixel_width *= XFLOATINT (it->space_width);
24354 /* If face has a box, add the box thickness to the character
24355 height. If character has a box line to the left and/or
24356 right, add the box line width to the character's width. */
24357 if (face->box != FACE_NO_BOX)
24359 int thick = face->box_line_width;
24361 if (thick > 0)
24363 it->ascent += thick;
24364 it->descent += thick;
24366 else
24367 thick = -thick;
24369 if (it->start_of_box_run_p)
24370 it->pixel_width += thick;
24371 if (it->end_of_box_run_p)
24372 it->pixel_width += thick;
24375 /* If face has an overline, add the height of the overline
24376 (1 pixel) and a 1 pixel margin to the character height. */
24377 if (face->overline_p)
24378 it->ascent += overline_margin;
24380 if (it->constrain_row_ascent_descent_p)
24382 if (it->ascent > it->max_ascent)
24383 it->ascent = it->max_ascent;
24384 if (it->descent > it->max_descent)
24385 it->descent = it->max_descent;
24388 take_vertical_position_into_account (it);
24390 /* If we have to actually produce glyphs, do it. */
24391 if (it->glyph_row)
24393 if (stretched_p)
24395 /* Translate a space with a `space-width' property
24396 into a stretch glyph. */
24397 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24398 / FONT_HEIGHT (font));
24399 append_stretch_glyph (it, it->object, it->pixel_width,
24400 it->ascent + it->descent, ascent);
24402 else
24403 append_glyph (it);
24405 /* If characters with lbearing or rbearing are displayed
24406 in this line, record that fact in a flag of the
24407 glyph row. This is used to optimize X output code. */
24408 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24409 it->glyph_row->contains_overlapping_glyphs_p = 1;
24411 if (! stretched_p && it->pixel_width == 0)
24412 /* We assure that all visible glyphs have at least 1-pixel
24413 width. */
24414 it->pixel_width = 1;
24416 else if (it->char_to_display == '\n')
24418 /* A newline has no width, but we need the height of the
24419 line. But if previous part of the line sets a height,
24420 don't increase that height */
24422 Lisp_Object height;
24423 Lisp_Object total_height = Qnil;
24425 it->override_ascent = -1;
24426 it->pixel_width = 0;
24427 it->nglyphs = 0;
24429 height = get_it_property (it, Qline_height);
24430 /* Split (line-height total-height) list */
24431 if (CONSP (height)
24432 && CONSP (XCDR (height))
24433 && NILP (XCDR (XCDR (height))))
24435 total_height = XCAR (XCDR (height));
24436 height = XCAR (height);
24438 height = calc_line_height_property (it, height, font, boff, 1);
24440 if (it->override_ascent >= 0)
24442 it->ascent = it->override_ascent;
24443 it->descent = it->override_descent;
24444 boff = it->override_boff;
24446 else
24448 it->ascent = FONT_BASE (font) + boff;
24449 it->descent = FONT_DESCENT (font) - boff;
24452 if (EQ (height, Qt))
24454 if (it->descent > it->max_descent)
24456 it->ascent += it->descent - it->max_descent;
24457 it->descent = it->max_descent;
24459 if (it->ascent > it->max_ascent)
24461 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24462 it->ascent = it->max_ascent;
24464 it->phys_ascent = min (it->phys_ascent, it->ascent);
24465 it->phys_descent = min (it->phys_descent, it->descent);
24466 it->constrain_row_ascent_descent_p = 1;
24467 extra_line_spacing = 0;
24469 else
24471 Lisp_Object spacing;
24473 it->phys_ascent = it->ascent;
24474 it->phys_descent = it->descent;
24476 if ((it->max_ascent > 0 || it->max_descent > 0)
24477 && face->box != FACE_NO_BOX
24478 && face->box_line_width > 0)
24480 it->ascent += face->box_line_width;
24481 it->descent += face->box_line_width;
24483 if (!NILP (height)
24484 && XINT (height) > it->ascent + it->descent)
24485 it->ascent = XINT (height) - it->descent;
24487 if (!NILP (total_height))
24488 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24489 else
24491 spacing = get_it_property (it, Qline_spacing);
24492 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24494 if (INTEGERP (spacing))
24496 extra_line_spacing = XINT (spacing);
24497 if (!NILP (total_height))
24498 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24502 else /* i.e. (it->char_to_display == '\t') */
24504 if (font->space_width > 0)
24506 int tab_width = it->tab_width * font->space_width;
24507 int x = it->current_x + it->continuation_lines_width;
24508 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24510 /* If the distance from the current position to the next tab
24511 stop is less than a space character width, use the
24512 tab stop after that. */
24513 if (next_tab_x - x < font->space_width)
24514 next_tab_x += tab_width;
24516 it->pixel_width = next_tab_x - x;
24517 it->nglyphs = 1;
24518 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24519 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24521 if (it->glyph_row)
24523 append_stretch_glyph (it, it->object, it->pixel_width,
24524 it->ascent + it->descent, it->ascent);
24527 else
24529 it->pixel_width = 0;
24530 it->nglyphs = 1;
24534 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24536 /* A static composition.
24538 Note: A composition is represented as one glyph in the
24539 glyph matrix. There are no padding glyphs.
24541 Important note: pixel_width, ascent, and descent are the
24542 values of what is drawn by draw_glyphs (i.e. the values of
24543 the overall glyphs composed). */
24544 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24545 int boff; /* baseline offset */
24546 struct composition *cmp = composition_table[it->cmp_it.id];
24547 int glyph_len = cmp->glyph_len;
24548 struct font *font = face->font;
24550 it->nglyphs = 1;
24552 /* If we have not yet calculated pixel size data of glyphs of
24553 the composition for the current face font, calculate them
24554 now. Theoretically, we have to check all fonts for the
24555 glyphs, but that requires much time and memory space. So,
24556 here we check only the font of the first glyph. This may
24557 lead to incorrect display, but it's very rare, and C-l
24558 (recenter-top-bottom) can correct the display anyway. */
24559 if (! cmp->font || cmp->font != font)
24561 /* Ascent and descent of the font of the first character
24562 of this composition (adjusted by baseline offset).
24563 Ascent and descent of overall glyphs should not be less
24564 than these, respectively. */
24565 int font_ascent, font_descent, font_height;
24566 /* Bounding box of the overall glyphs. */
24567 int leftmost, rightmost, lowest, highest;
24568 int lbearing, rbearing;
24569 int i, width, ascent, descent;
24570 int left_padded = 0, right_padded = 0;
24571 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24572 XChar2b char2b;
24573 struct font_metrics *pcm;
24574 int font_not_found_p;
24575 EMACS_INT pos;
24577 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24578 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24579 break;
24580 if (glyph_len < cmp->glyph_len)
24581 right_padded = 1;
24582 for (i = 0; i < glyph_len; i++)
24584 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24585 break;
24586 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24588 if (i > 0)
24589 left_padded = 1;
24591 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24592 : IT_CHARPOS (*it));
24593 /* If no suitable font is found, use the default font. */
24594 font_not_found_p = font == NULL;
24595 if (font_not_found_p)
24597 face = face->ascii_face;
24598 font = face->font;
24600 boff = font->baseline_offset;
24601 if (font->vertical_centering)
24602 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24603 font_ascent = FONT_BASE (font) + boff;
24604 font_descent = FONT_DESCENT (font) - boff;
24605 font_height = FONT_HEIGHT (font);
24607 cmp->font = (void *) font;
24609 pcm = NULL;
24610 if (! font_not_found_p)
24612 get_char_face_and_encoding (it->f, c, it->face_id,
24613 &char2b, 0);
24614 pcm = get_per_char_metric (font, &char2b);
24617 /* Initialize the bounding box. */
24618 if (pcm)
24620 width = cmp->glyph_len > 0 ? pcm->width : 0;
24621 ascent = pcm->ascent;
24622 descent = pcm->descent;
24623 lbearing = pcm->lbearing;
24624 rbearing = pcm->rbearing;
24626 else
24628 width = cmp->glyph_len > 0 ? font->space_width : 0;
24629 ascent = FONT_BASE (font);
24630 descent = FONT_DESCENT (font);
24631 lbearing = 0;
24632 rbearing = width;
24635 rightmost = width;
24636 leftmost = 0;
24637 lowest = - descent + boff;
24638 highest = ascent + boff;
24640 if (! font_not_found_p
24641 && font->default_ascent
24642 && CHAR_TABLE_P (Vuse_default_ascent)
24643 && !NILP (Faref (Vuse_default_ascent,
24644 make_number (it->char_to_display))))
24645 highest = font->default_ascent + boff;
24647 /* Draw the first glyph at the normal position. It may be
24648 shifted to right later if some other glyphs are drawn
24649 at the left. */
24650 cmp->offsets[i * 2] = 0;
24651 cmp->offsets[i * 2 + 1] = boff;
24652 cmp->lbearing = lbearing;
24653 cmp->rbearing = rbearing;
24655 /* Set cmp->offsets for the remaining glyphs. */
24656 for (i++; i < glyph_len; i++)
24658 int left, right, btm, top;
24659 int ch = COMPOSITION_GLYPH (cmp, i);
24660 int face_id;
24661 struct face *this_face;
24663 if (ch == '\t')
24664 ch = ' ';
24665 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24666 this_face = FACE_FROM_ID (it->f, face_id);
24667 font = this_face->font;
24669 if (font == NULL)
24670 pcm = NULL;
24671 else
24673 get_char_face_and_encoding (it->f, ch, face_id,
24674 &char2b, 0);
24675 pcm = get_per_char_metric (font, &char2b);
24677 if (! pcm)
24678 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24679 else
24681 width = pcm->width;
24682 ascent = pcm->ascent;
24683 descent = pcm->descent;
24684 lbearing = pcm->lbearing;
24685 rbearing = pcm->rbearing;
24686 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
24688 /* Relative composition with or without
24689 alternate chars. */
24690 left = (leftmost + rightmost - width) / 2;
24691 btm = - descent + boff;
24692 if (font->relative_compose
24693 && (! CHAR_TABLE_P (Vignore_relative_composition)
24694 || NILP (Faref (Vignore_relative_composition,
24695 make_number (ch)))))
24698 if (- descent >= font->relative_compose)
24699 /* One extra pixel between two glyphs. */
24700 btm = highest + 1;
24701 else if (ascent <= 0)
24702 /* One extra pixel between two glyphs. */
24703 btm = lowest - 1 - ascent - descent;
24706 else
24708 /* A composition rule is specified by an integer
24709 value that encodes global and new reference
24710 points (GREF and NREF). GREF and NREF are
24711 specified by numbers as below:
24713 0---1---2 -- ascent
24717 9--10--11 -- center
24719 ---3---4---5--- baseline
24721 6---7---8 -- descent
24723 int rule = COMPOSITION_RULE (cmp, i);
24724 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
24726 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
24727 grefx = gref % 3, nrefx = nref % 3;
24728 grefy = gref / 3, nrefy = nref / 3;
24729 if (xoff)
24730 xoff = font_height * (xoff - 128) / 256;
24731 if (yoff)
24732 yoff = font_height * (yoff - 128) / 256;
24734 left = (leftmost
24735 + grefx * (rightmost - leftmost) / 2
24736 - nrefx * width / 2
24737 + xoff);
24739 btm = ((grefy == 0 ? highest
24740 : grefy == 1 ? 0
24741 : grefy == 2 ? lowest
24742 : (highest + lowest) / 2)
24743 - (nrefy == 0 ? ascent + descent
24744 : nrefy == 1 ? descent - boff
24745 : nrefy == 2 ? 0
24746 : (ascent + descent) / 2)
24747 + yoff);
24750 cmp->offsets[i * 2] = left;
24751 cmp->offsets[i * 2 + 1] = btm + descent;
24753 /* Update the bounding box of the overall glyphs. */
24754 if (width > 0)
24756 right = left + width;
24757 if (left < leftmost)
24758 leftmost = left;
24759 if (right > rightmost)
24760 rightmost = right;
24762 top = btm + descent + ascent;
24763 if (top > highest)
24764 highest = top;
24765 if (btm < lowest)
24766 lowest = btm;
24768 if (cmp->lbearing > left + lbearing)
24769 cmp->lbearing = left + lbearing;
24770 if (cmp->rbearing < left + rbearing)
24771 cmp->rbearing = left + rbearing;
24775 /* If there are glyphs whose x-offsets are negative,
24776 shift all glyphs to the right and make all x-offsets
24777 non-negative. */
24778 if (leftmost < 0)
24780 for (i = 0; i < cmp->glyph_len; i++)
24781 cmp->offsets[i * 2] -= leftmost;
24782 rightmost -= leftmost;
24783 cmp->lbearing -= leftmost;
24784 cmp->rbearing -= leftmost;
24787 if (left_padded && cmp->lbearing < 0)
24789 for (i = 0; i < cmp->glyph_len; i++)
24790 cmp->offsets[i * 2] -= cmp->lbearing;
24791 rightmost -= cmp->lbearing;
24792 cmp->rbearing -= cmp->lbearing;
24793 cmp->lbearing = 0;
24795 if (right_padded && rightmost < cmp->rbearing)
24797 rightmost = cmp->rbearing;
24800 cmp->pixel_width = rightmost;
24801 cmp->ascent = highest;
24802 cmp->descent = - lowest;
24803 if (cmp->ascent < font_ascent)
24804 cmp->ascent = font_ascent;
24805 if (cmp->descent < font_descent)
24806 cmp->descent = font_descent;
24809 if (it->glyph_row
24810 && (cmp->lbearing < 0
24811 || cmp->rbearing > cmp->pixel_width))
24812 it->glyph_row->contains_overlapping_glyphs_p = 1;
24814 it->pixel_width = cmp->pixel_width;
24815 it->ascent = it->phys_ascent = cmp->ascent;
24816 it->descent = it->phys_descent = cmp->descent;
24817 if (face->box != FACE_NO_BOX)
24819 int thick = face->box_line_width;
24821 if (thick > 0)
24823 it->ascent += thick;
24824 it->descent += thick;
24826 else
24827 thick = - thick;
24829 if (it->start_of_box_run_p)
24830 it->pixel_width += thick;
24831 if (it->end_of_box_run_p)
24832 it->pixel_width += thick;
24835 /* If face has an overline, add the height of the overline
24836 (1 pixel) and a 1 pixel margin to the character height. */
24837 if (face->overline_p)
24838 it->ascent += overline_margin;
24840 take_vertical_position_into_account (it);
24841 if (it->ascent < 0)
24842 it->ascent = 0;
24843 if (it->descent < 0)
24844 it->descent = 0;
24846 if (it->glyph_row && cmp->glyph_len > 0)
24847 append_composite_glyph (it);
24849 else if (it->what == IT_COMPOSITION)
24851 /* A dynamic (automatic) composition. */
24852 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24853 Lisp_Object gstring;
24854 struct font_metrics metrics;
24856 it->nglyphs = 1;
24858 gstring = composition_gstring_from_id (it->cmp_it.id);
24859 it->pixel_width
24860 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
24861 &metrics);
24862 if (it->glyph_row
24863 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
24864 it->glyph_row->contains_overlapping_glyphs_p = 1;
24865 it->ascent = it->phys_ascent = metrics.ascent;
24866 it->descent = it->phys_descent = metrics.descent;
24867 if (face->box != FACE_NO_BOX)
24869 int thick = face->box_line_width;
24871 if (thick > 0)
24873 it->ascent += thick;
24874 it->descent += thick;
24876 else
24877 thick = - thick;
24879 if (it->start_of_box_run_p)
24880 it->pixel_width += thick;
24881 if (it->end_of_box_run_p)
24882 it->pixel_width += thick;
24884 /* If face has an overline, add the height of the overline
24885 (1 pixel) and a 1 pixel margin to the character height. */
24886 if (face->overline_p)
24887 it->ascent += overline_margin;
24888 take_vertical_position_into_account (it);
24889 if (it->ascent < 0)
24890 it->ascent = 0;
24891 if (it->descent < 0)
24892 it->descent = 0;
24894 if (it->glyph_row)
24895 append_composite_glyph (it);
24897 else if (it->what == IT_GLYPHLESS)
24898 produce_glyphless_glyph (it, 0, Qnil);
24899 else if (it->what == IT_IMAGE)
24900 produce_image_glyph (it);
24901 else if (it->what == IT_STRETCH)
24902 produce_stretch_glyph (it);
24904 done:
24905 /* Accumulate dimensions. Note: can't assume that it->descent > 0
24906 because this isn't true for images with `:ascent 100'. */
24907 xassert (it->ascent >= 0 && it->descent >= 0);
24908 if (it->area == TEXT_AREA)
24909 it->current_x += it->pixel_width;
24911 if (extra_line_spacing > 0)
24913 it->descent += extra_line_spacing;
24914 if (extra_line_spacing > it->max_extra_line_spacing)
24915 it->max_extra_line_spacing = extra_line_spacing;
24918 it->max_ascent = max (it->max_ascent, it->ascent);
24919 it->max_descent = max (it->max_descent, it->descent);
24920 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
24921 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
24924 /* EXPORT for RIF:
24925 Output LEN glyphs starting at START at the nominal cursor position.
24926 Advance the nominal cursor over the text. The global variable
24927 updated_window contains the window being updated, updated_row is
24928 the glyph row being updated, and updated_area is the area of that
24929 row being updated. */
24931 void
24932 x_write_glyphs (struct glyph *start, int len)
24934 int x, hpos, chpos = updated_window->phys_cursor.hpos;
24936 xassert (updated_window && updated_row);
24937 /* When the window is hscrolled, cursor hpos can legitimately be out
24938 of bounds, but we draw the cursor at the corresponding window
24939 margin in that case. */
24940 if (!updated_row->reversed_p && chpos < 0)
24941 chpos = 0;
24942 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
24943 chpos = updated_row->used[TEXT_AREA] - 1;
24945 BLOCK_INPUT;
24947 /* Write glyphs. */
24949 hpos = start - updated_row->glyphs[updated_area];
24950 x = draw_glyphs (updated_window, output_cursor.x,
24951 updated_row, updated_area,
24952 hpos, hpos + len,
24953 DRAW_NORMAL_TEXT, 0);
24955 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
24956 if (updated_area == TEXT_AREA
24957 && updated_window->phys_cursor_on_p
24958 && updated_window->phys_cursor.vpos == output_cursor.vpos
24959 && chpos >= hpos
24960 && chpos < hpos + len)
24961 updated_window->phys_cursor_on_p = 0;
24963 UNBLOCK_INPUT;
24965 /* Advance the output cursor. */
24966 output_cursor.hpos += len;
24967 output_cursor.x = x;
24971 /* EXPORT for RIF:
24972 Insert LEN glyphs from START at the nominal cursor position. */
24974 void
24975 x_insert_glyphs (struct glyph *start, int len)
24977 struct frame *f;
24978 struct window *w;
24979 int line_height, shift_by_width, shifted_region_width;
24980 struct glyph_row *row;
24981 struct glyph *glyph;
24982 int frame_x, frame_y;
24983 EMACS_INT hpos;
24985 xassert (updated_window && updated_row);
24986 BLOCK_INPUT;
24987 w = updated_window;
24988 f = XFRAME (WINDOW_FRAME (w));
24990 /* Get the height of the line we are in. */
24991 row = updated_row;
24992 line_height = row->height;
24994 /* Get the width of the glyphs to insert. */
24995 shift_by_width = 0;
24996 for (glyph = start; glyph < start + len; ++glyph)
24997 shift_by_width += glyph->pixel_width;
24999 /* Get the width of the region to shift right. */
25000 shifted_region_width = (window_box_width (w, updated_area)
25001 - output_cursor.x
25002 - shift_by_width);
25004 /* Shift right. */
25005 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25006 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25008 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25009 line_height, shift_by_width);
25011 /* Write the glyphs. */
25012 hpos = start - row->glyphs[updated_area];
25013 draw_glyphs (w, output_cursor.x, row, updated_area,
25014 hpos, hpos + len,
25015 DRAW_NORMAL_TEXT, 0);
25017 /* Advance the output cursor. */
25018 output_cursor.hpos += len;
25019 output_cursor.x += shift_by_width;
25020 UNBLOCK_INPUT;
25024 /* EXPORT for RIF:
25025 Erase the current text line from the nominal cursor position
25026 (inclusive) to pixel column TO_X (exclusive). The idea is that
25027 everything from TO_X onward is already erased.
25029 TO_X is a pixel position relative to updated_area of
25030 updated_window. TO_X == -1 means clear to the end of this area. */
25032 void
25033 x_clear_end_of_line (int to_x)
25035 struct frame *f;
25036 struct window *w = updated_window;
25037 int max_x, min_y, max_y;
25038 int from_x, from_y, to_y;
25040 xassert (updated_window && updated_row);
25041 f = XFRAME (w->frame);
25043 if (updated_row->full_width_p)
25044 max_x = WINDOW_TOTAL_WIDTH (w);
25045 else
25046 max_x = window_box_width (w, updated_area);
25047 max_y = window_text_bottom_y (w);
25049 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25050 of window. For TO_X > 0, truncate to end of drawing area. */
25051 if (to_x == 0)
25052 return;
25053 else if (to_x < 0)
25054 to_x = max_x;
25055 else
25056 to_x = min (to_x, max_x);
25058 to_y = min (max_y, output_cursor.y + updated_row->height);
25060 /* Notice if the cursor will be cleared by this operation. */
25061 if (!updated_row->full_width_p)
25062 notice_overwritten_cursor (w, updated_area,
25063 output_cursor.x, -1,
25064 updated_row->y,
25065 MATRIX_ROW_BOTTOM_Y (updated_row));
25067 from_x = output_cursor.x;
25069 /* Translate to frame coordinates. */
25070 if (updated_row->full_width_p)
25072 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25073 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25075 else
25077 int area_left = window_box_left (w, updated_area);
25078 from_x += area_left;
25079 to_x += area_left;
25082 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25083 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25084 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25086 /* Prevent inadvertently clearing to end of the X window. */
25087 if (to_x > from_x && to_y > from_y)
25089 BLOCK_INPUT;
25090 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25091 to_x - from_x, to_y - from_y);
25092 UNBLOCK_INPUT;
25096 #endif /* HAVE_WINDOW_SYSTEM */
25100 /***********************************************************************
25101 Cursor types
25102 ***********************************************************************/
25104 /* Value is the internal representation of the specified cursor type
25105 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25106 of the bar cursor. */
25108 static enum text_cursor_kinds
25109 get_specified_cursor_type (Lisp_Object arg, int *width)
25111 enum text_cursor_kinds type;
25113 if (NILP (arg))
25114 return NO_CURSOR;
25116 if (EQ (arg, Qbox))
25117 return FILLED_BOX_CURSOR;
25119 if (EQ (arg, Qhollow))
25120 return HOLLOW_BOX_CURSOR;
25122 if (EQ (arg, Qbar))
25124 *width = 2;
25125 return BAR_CURSOR;
25128 if (CONSP (arg)
25129 && EQ (XCAR (arg), Qbar)
25130 && INTEGERP (XCDR (arg))
25131 && XINT (XCDR (arg)) >= 0)
25133 *width = XINT (XCDR (arg));
25134 return BAR_CURSOR;
25137 if (EQ (arg, Qhbar))
25139 *width = 2;
25140 return HBAR_CURSOR;
25143 if (CONSP (arg)
25144 && EQ (XCAR (arg), Qhbar)
25145 && INTEGERP (XCDR (arg))
25146 && XINT (XCDR (arg)) >= 0)
25148 *width = XINT (XCDR (arg));
25149 return HBAR_CURSOR;
25152 /* Treat anything unknown as "hollow box cursor".
25153 It was bad to signal an error; people have trouble fixing
25154 .Xdefaults with Emacs, when it has something bad in it. */
25155 type = HOLLOW_BOX_CURSOR;
25157 return type;
25160 /* Set the default cursor types for specified frame. */
25161 void
25162 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25164 int width = 1;
25165 Lisp_Object tem;
25167 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25168 FRAME_CURSOR_WIDTH (f) = width;
25170 /* By default, set up the blink-off state depending on the on-state. */
25172 tem = Fassoc (arg, Vblink_cursor_alist);
25173 if (!NILP (tem))
25175 FRAME_BLINK_OFF_CURSOR (f)
25176 = get_specified_cursor_type (XCDR (tem), &width);
25177 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25179 else
25180 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25184 #ifdef HAVE_WINDOW_SYSTEM
25186 /* Return the cursor we want to be displayed in window W. Return
25187 width of bar/hbar cursor through WIDTH arg. Return with
25188 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25189 (i.e. if the `system caret' should track this cursor).
25191 In a mini-buffer window, we want the cursor only to appear if we
25192 are reading input from this window. For the selected window, we
25193 want the cursor type given by the frame parameter or buffer local
25194 setting of cursor-type. If explicitly marked off, draw no cursor.
25195 In all other cases, we want a hollow box cursor. */
25197 static enum text_cursor_kinds
25198 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25199 int *active_cursor)
25201 struct frame *f = XFRAME (w->frame);
25202 struct buffer *b = XBUFFER (w->buffer);
25203 int cursor_type = DEFAULT_CURSOR;
25204 Lisp_Object alt_cursor;
25205 int non_selected = 0;
25207 *active_cursor = 1;
25209 /* Echo area */
25210 if (cursor_in_echo_area
25211 && FRAME_HAS_MINIBUF_P (f)
25212 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25214 if (w == XWINDOW (echo_area_window))
25216 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25218 *width = FRAME_CURSOR_WIDTH (f);
25219 return FRAME_DESIRED_CURSOR (f);
25221 else
25222 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25225 *active_cursor = 0;
25226 non_selected = 1;
25229 /* Detect a nonselected window or nonselected frame. */
25230 else if (w != XWINDOW (f->selected_window)
25231 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25233 *active_cursor = 0;
25235 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25236 return NO_CURSOR;
25238 non_selected = 1;
25241 /* Never display a cursor in a window in which cursor-type is nil. */
25242 if (NILP (BVAR (b, cursor_type)))
25243 return NO_CURSOR;
25245 /* Get the normal cursor type for this window. */
25246 if (EQ (BVAR (b, cursor_type), Qt))
25248 cursor_type = FRAME_DESIRED_CURSOR (f);
25249 *width = FRAME_CURSOR_WIDTH (f);
25251 else
25252 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25254 /* Use cursor-in-non-selected-windows instead
25255 for non-selected window or frame. */
25256 if (non_selected)
25258 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25259 if (!EQ (Qt, alt_cursor))
25260 return get_specified_cursor_type (alt_cursor, width);
25261 /* t means modify the normal cursor type. */
25262 if (cursor_type == FILLED_BOX_CURSOR)
25263 cursor_type = HOLLOW_BOX_CURSOR;
25264 else if (cursor_type == BAR_CURSOR && *width > 1)
25265 --*width;
25266 return cursor_type;
25269 /* Use normal cursor if not blinked off. */
25270 if (!w->cursor_off_p)
25272 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25274 if (cursor_type == FILLED_BOX_CURSOR)
25276 /* Using a block cursor on large images can be very annoying.
25277 So use a hollow cursor for "large" images.
25278 If image is not transparent (no mask), also use hollow cursor. */
25279 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25280 if (img != NULL && IMAGEP (img->spec))
25282 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25283 where N = size of default frame font size.
25284 This should cover most of the "tiny" icons people may use. */
25285 if (!img->mask
25286 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25287 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25288 cursor_type = HOLLOW_BOX_CURSOR;
25291 else if (cursor_type != NO_CURSOR)
25293 /* Display current only supports BOX and HOLLOW cursors for images.
25294 So for now, unconditionally use a HOLLOW cursor when cursor is
25295 not a solid box cursor. */
25296 cursor_type = HOLLOW_BOX_CURSOR;
25299 return cursor_type;
25302 /* Cursor is blinked off, so determine how to "toggle" it. */
25304 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25305 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25306 return get_specified_cursor_type (XCDR (alt_cursor), width);
25308 /* Then see if frame has specified a specific blink off cursor type. */
25309 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25311 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25312 return FRAME_BLINK_OFF_CURSOR (f);
25315 #if 0
25316 /* Some people liked having a permanently visible blinking cursor,
25317 while others had very strong opinions against it. So it was
25318 decided to remove it. KFS 2003-09-03 */
25320 /* Finally perform built-in cursor blinking:
25321 filled box <-> hollow box
25322 wide [h]bar <-> narrow [h]bar
25323 narrow [h]bar <-> no cursor
25324 other type <-> no cursor */
25326 if (cursor_type == FILLED_BOX_CURSOR)
25327 return HOLLOW_BOX_CURSOR;
25329 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25331 *width = 1;
25332 return cursor_type;
25334 #endif
25336 return NO_CURSOR;
25340 /* Notice when the text cursor of window W has been completely
25341 overwritten by a drawing operation that outputs glyphs in AREA
25342 starting at X0 and ending at X1 in the line starting at Y0 and
25343 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25344 the rest of the line after X0 has been written. Y coordinates
25345 are window-relative. */
25347 static void
25348 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25349 int x0, int x1, int y0, int y1)
25351 int cx0, cx1, cy0, cy1;
25352 struct glyph_row *row;
25354 if (!w->phys_cursor_on_p)
25355 return;
25356 if (area != TEXT_AREA)
25357 return;
25359 if (w->phys_cursor.vpos < 0
25360 || w->phys_cursor.vpos >= w->current_matrix->nrows
25361 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25362 !(row->enabled_p && row->displays_text_p)))
25363 return;
25365 if (row->cursor_in_fringe_p)
25367 row->cursor_in_fringe_p = 0;
25368 draw_fringe_bitmap (w, row, row->reversed_p);
25369 w->phys_cursor_on_p = 0;
25370 return;
25373 cx0 = w->phys_cursor.x;
25374 cx1 = cx0 + w->phys_cursor_width;
25375 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25376 return;
25378 /* The cursor image will be completely removed from the
25379 screen if the output area intersects the cursor area in
25380 y-direction. When we draw in [y0 y1[, and some part of
25381 the cursor is at y < y0, that part must have been drawn
25382 before. When scrolling, the cursor is erased before
25383 actually scrolling, so we don't come here. When not
25384 scrolling, the rows above the old cursor row must have
25385 changed, and in this case these rows must have written
25386 over the cursor image.
25388 Likewise if part of the cursor is below y1, with the
25389 exception of the cursor being in the first blank row at
25390 the buffer and window end because update_text_area
25391 doesn't draw that row. (Except when it does, but
25392 that's handled in update_text_area.) */
25394 cy0 = w->phys_cursor.y;
25395 cy1 = cy0 + w->phys_cursor_height;
25396 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25397 return;
25399 w->phys_cursor_on_p = 0;
25402 #endif /* HAVE_WINDOW_SYSTEM */
25405 /************************************************************************
25406 Mouse Face
25407 ************************************************************************/
25409 #ifdef HAVE_WINDOW_SYSTEM
25411 /* EXPORT for RIF:
25412 Fix the display of area AREA of overlapping row ROW in window W
25413 with respect to the overlapping part OVERLAPS. */
25415 void
25416 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25417 enum glyph_row_area area, int overlaps)
25419 int i, x;
25421 BLOCK_INPUT;
25423 x = 0;
25424 for (i = 0; i < row->used[area];)
25426 if (row->glyphs[area][i].overlaps_vertically_p)
25428 int start = i, start_x = x;
25432 x += row->glyphs[area][i].pixel_width;
25433 ++i;
25435 while (i < row->used[area]
25436 && row->glyphs[area][i].overlaps_vertically_p);
25438 draw_glyphs (w, start_x, row, area,
25439 start, i,
25440 DRAW_NORMAL_TEXT, overlaps);
25442 else
25444 x += row->glyphs[area][i].pixel_width;
25445 ++i;
25449 UNBLOCK_INPUT;
25453 /* EXPORT:
25454 Draw the cursor glyph of window W in glyph row ROW. See the
25455 comment of draw_glyphs for the meaning of HL. */
25457 void
25458 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25459 enum draw_glyphs_face hl)
25461 /* If cursor hpos is out of bounds, don't draw garbage. This can
25462 happen in mini-buffer windows when switching between echo area
25463 glyphs and mini-buffer. */
25464 if ((row->reversed_p
25465 ? (w->phys_cursor.hpos >= 0)
25466 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25468 int on_p = w->phys_cursor_on_p;
25469 int x1;
25470 int hpos = w->phys_cursor.hpos;
25472 /* When the window is hscrolled, cursor hpos can legitimately be
25473 out of bounds, but we draw the cursor at the corresponding
25474 window margin in that case. */
25475 if (!row->reversed_p && hpos < 0)
25476 hpos = 0;
25477 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25478 hpos = row->used[TEXT_AREA] - 1;
25480 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25481 hl, 0);
25482 w->phys_cursor_on_p = on_p;
25484 if (hl == DRAW_CURSOR)
25485 w->phys_cursor_width = x1 - w->phys_cursor.x;
25486 /* When we erase the cursor, and ROW is overlapped by other
25487 rows, make sure that these overlapping parts of other rows
25488 are redrawn. */
25489 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25491 w->phys_cursor_width = x1 - w->phys_cursor.x;
25493 if (row > w->current_matrix->rows
25494 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25495 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25496 OVERLAPS_ERASED_CURSOR);
25498 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25499 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25500 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25501 OVERLAPS_ERASED_CURSOR);
25507 /* EXPORT:
25508 Erase the image of a cursor of window W from the screen. */
25510 void
25511 erase_phys_cursor (struct window *w)
25513 struct frame *f = XFRAME (w->frame);
25514 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25515 int hpos = w->phys_cursor.hpos;
25516 int vpos = w->phys_cursor.vpos;
25517 int mouse_face_here_p = 0;
25518 struct glyph_matrix *active_glyphs = w->current_matrix;
25519 struct glyph_row *cursor_row;
25520 struct glyph *cursor_glyph;
25521 enum draw_glyphs_face hl;
25523 /* No cursor displayed or row invalidated => nothing to do on the
25524 screen. */
25525 if (w->phys_cursor_type == NO_CURSOR)
25526 goto mark_cursor_off;
25528 /* VPOS >= active_glyphs->nrows means that window has been resized.
25529 Don't bother to erase the cursor. */
25530 if (vpos >= active_glyphs->nrows)
25531 goto mark_cursor_off;
25533 /* If row containing cursor is marked invalid, there is nothing we
25534 can do. */
25535 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25536 if (!cursor_row->enabled_p)
25537 goto mark_cursor_off;
25539 /* If line spacing is > 0, old cursor may only be partially visible in
25540 window after split-window. So adjust visible height. */
25541 cursor_row->visible_height = min (cursor_row->visible_height,
25542 window_text_bottom_y (w) - cursor_row->y);
25544 /* If row is completely invisible, don't attempt to delete a cursor which
25545 isn't there. This can happen if cursor is at top of a window, and
25546 we switch to a buffer with a header line in that window. */
25547 if (cursor_row->visible_height <= 0)
25548 goto mark_cursor_off;
25550 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25551 if (cursor_row->cursor_in_fringe_p)
25553 cursor_row->cursor_in_fringe_p = 0;
25554 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25555 goto mark_cursor_off;
25558 /* This can happen when the new row is shorter than the old one.
25559 In this case, either draw_glyphs or clear_end_of_line
25560 should have cleared the cursor. Note that we wouldn't be
25561 able to erase the cursor in this case because we don't have a
25562 cursor glyph at hand. */
25563 if ((cursor_row->reversed_p
25564 ? (w->phys_cursor.hpos < 0)
25565 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25566 goto mark_cursor_off;
25568 /* When the window is hscrolled, cursor hpos can legitimately be out
25569 of bounds, but we draw the cursor at the corresponding window
25570 margin in that case. */
25571 if (!cursor_row->reversed_p && hpos < 0)
25572 hpos = 0;
25573 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25574 hpos = cursor_row->used[TEXT_AREA] - 1;
25576 /* If the cursor is in the mouse face area, redisplay that when
25577 we clear the cursor. */
25578 if (! NILP (hlinfo->mouse_face_window)
25579 && coords_in_mouse_face_p (w, hpos, vpos)
25580 /* Don't redraw the cursor's spot in mouse face if it is at the
25581 end of a line (on a newline). The cursor appears there, but
25582 mouse highlighting does not. */
25583 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25584 mouse_face_here_p = 1;
25586 /* Maybe clear the display under the cursor. */
25587 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25589 int x, y, left_x;
25590 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25591 int width;
25593 cursor_glyph = get_phys_cursor_glyph (w);
25594 if (cursor_glyph == NULL)
25595 goto mark_cursor_off;
25597 width = cursor_glyph->pixel_width;
25598 left_x = window_box_left_offset (w, TEXT_AREA);
25599 x = w->phys_cursor.x;
25600 if (x < left_x)
25601 width -= left_x - x;
25602 width = min (width, window_box_width (w, TEXT_AREA) - x);
25603 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25604 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25606 if (width > 0)
25607 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25610 /* Erase the cursor by redrawing the character underneath it. */
25611 if (mouse_face_here_p)
25612 hl = DRAW_MOUSE_FACE;
25613 else
25614 hl = DRAW_NORMAL_TEXT;
25615 draw_phys_cursor_glyph (w, cursor_row, hl);
25617 mark_cursor_off:
25618 w->phys_cursor_on_p = 0;
25619 w->phys_cursor_type = NO_CURSOR;
25623 /* EXPORT:
25624 Display or clear cursor of window W. If ON is zero, clear the
25625 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25626 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25628 void
25629 display_and_set_cursor (struct window *w, int on,
25630 int hpos, int vpos, int x, int y)
25632 struct frame *f = XFRAME (w->frame);
25633 int new_cursor_type;
25634 int new_cursor_width;
25635 int active_cursor;
25636 struct glyph_row *glyph_row;
25637 struct glyph *glyph;
25639 /* This is pointless on invisible frames, and dangerous on garbaged
25640 windows and frames; in the latter case, the frame or window may
25641 be in the midst of changing its size, and x and y may be off the
25642 window. */
25643 if (! FRAME_VISIBLE_P (f)
25644 || FRAME_GARBAGED_P (f)
25645 || vpos >= w->current_matrix->nrows
25646 || hpos >= w->current_matrix->matrix_w)
25647 return;
25649 /* If cursor is off and we want it off, return quickly. */
25650 if (!on && !w->phys_cursor_on_p)
25651 return;
25653 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25654 /* If cursor row is not enabled, we don't really know where to
25655 display the cursor. */
25656 if (!glyph_row->enabled_p)
25658 w->phys_cursor_on_p = 0;
25659 return;
25662 glyph = NULL;
25663 if (!glyph_row->exact_window_width_line_p
25664 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25665 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25667 xassert (interrupt_input_blocked);
25669 /* Set new_cursor_type to the cursor we want to be displayed. */
25670 new_cursor_type = get_window_cursor_type (w, glyph,
25671 &new_cursor_width, &active_cursor);
25673 /* If cursor is currently being shown and we don't want it to be or
25674 it is in the wrong place, or the cursor type is not what we want,
25675 erase it. */
25676 if (w->phys_cursor_on_p
25677 && (!on
25678 || w->phys_cursor.x != x
25679 || w->phys_cursor.y != y
25680 || new_cursor_type != w->phys_cursor_type
25681 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
25682 && new_cursor_width != w->phys_cursor_width)))
25683 erase_phys_cursor (w);
25685 /* Don't check phys_cursor_on_p here because that flag is only set
25686 to zero in some cases where we know that the cursor has been
25687 completely erased, to avoid the extra work of erasing the cursor
25688 twice. In other words, phys_cursor_on_p can be 1 and the cursor
25689 still not be visible, or it has only been partly erased. */
25690 if (on)
25692 w->phys_cursor_ascent = glyph_row->ascent;
25693 w->phys_cursor_height = glyph_row->height;
25695 /* Set phys_cursor_.* before x_draw_.* is called because some
25696 of them may need the information. */
25697 w->phys_cursor.x = x;
25698 w->phys_cursor.y = glyph_row->y;
25699 w->phys_cursor.hpos = hpos;
25700 w->phys_cursor.vpos = vpos;
25703 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
25704 new_cursor_type, new_cursor_width,
25705 on, active_cursor);
25709 /* Switch the display of W's cursor on or off, according to the value
25710 of ON. */
25712 static void
25713 update_window_cursor (struct window *w, int on)
25715 /* Don't update cursor in windows whose frame is in the process
25716 of being deleted. */
25717 if (w->current_matrix)
25719 int hpos = w->phys_cursor.hpos;
25720 int vpos = w->phys_cursor.vpos;
25721 struct glyph_row *row;
25723 if (vpos >= w->current_matrix->nrows
25724 || hpos >= w->current_matrix->matrix_w)
25725 return;
25727 row = MATRIX_ROW (w->current_matrix, vpos);
25729 /* When the window is hscrolled, cursor hpos can legitimately be
25730 out of bounds, but we draw the cursor at the corresponding
25731 window margin in that case. */
25732 if (!row->reversed_p && hpos < 0)
25733 hpos = 0;
25734 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25735 hpos = row->used[TEXT_AREA] - 1;
25737 BLOCK_INPUT;
25738 display_and_set_cursor (w, on, hpos, vpos,
25739 w->phys_cursor.x, w->phys_cursor.y);
25740 UNBLOCK_INPUT;
25745 /* Call update_window_cursor with parameter ON_P on all leaf windows
25746 in the window tree rooted at W. */
25748 static void
25749 update_cursor_in_window_tree (struct window *w, int on_p)
25751 while (w)
25753 if (!NILP (w->hchild))
25754 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
25755 else if (!NILP (w->vchild))
25756 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
25757 else
25758 update_window_cursor (w, on_p);
25760 w = NILP (w->next) ? 0 : XWINDOW (w->next);
25765 /* EXPORT:
25766 Display the cursor on window W, or clear it, according to ON_P.
25767 Don't change the cursor's position. */
25769 void
25770 x_update_cursor (struct frame *f, int on_p)
25772 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
25776 /* EXPORT:
25777 Clear the cursor of window W to background color, and mark the
25778 cursor as not shown. This is used when the text where the cursor
25779 is about to be rewritten. */
25781 void
25782 x_clear_cursor (struct window *w)
25784 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
25785 update_window_cursor (w, 0);
25788 #endif /* HAVE_WINDOW_SYSTEM */
25790 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
25791 and MSDOS. */
25792 static void
25793 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
25794 int start_hpos, int end_hpos,
25795 enum draw_glyphs_face draw)
25797 #ifdef HAVE_WINDOW_SYSTEM
25798 if (FRAME_WINDOW_P (XFRAME (w->frame)))
25800 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
25801 return;
25803 #endif
25804 #if defined (HAVE_GPM) || defined (MSDOS)
25805 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
25806 #endif
25809 /* Display the active region described by mouse_face_* according to DRAW. */
25811 static void
25812 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
25814 struct window *w = XWINDOW (hlinfo->mouse_face_window);
25815 struct frame *f = XFRAME (WINDOW_FRAME (w));
25817 if (/* If window is in the process of being destroyed, don't bother
25818 to do anything. */
25819 w->current_matrix != NULL
25820 /* Don't update mouse highlight if hidden */
25821 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
25822 /* Recognize when we are called to operate on rows that don't exist
25823 anymore. This can happen when a window is split. */
25824 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
25826 int phys_cursor_on_p = w->phys_cursor_on_p;
25827 struct glyph_row *row, *first, *last;
25829 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
25830 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
25832 for (row = first; row <= last && row->enabled_p; ++row)
25834 int start_hpos, end_hpos, start_x;
25836 /* For all but the first row, the highlight starts at column 0. */
25837 if (row == first)
25839 /* R2L rows have BEG and END in reversed order, but the
25840 screen drawing geometry is always left to right. So
25841 we need to mirror the beginning and end of the
25842 highlighted area in R2L rows. */
25843 if (!row->reversed_p)
25845 start_hpos = hlinfo->mouse_face_beg_col;
25846 start_x = hlinfo->mouse_face_beg_x;
25848 else if (row == last)
25850 start_hpos = hlinfo->mouse_face_end_col;
25851 start_x = hlinfo->mouse_face_end_x;
25853 else
25855 start_hpos = 0;
25856 start_x = 0;
25859 else if (row->reversed_p && row == last)
25861 start_hpos = hlinfo->mouse_face_end_col;
25862 start_x = hlinfo->mouse_face_end_x;
25864 else
25866 start_hpos = 0;
25867 start_x = 0;
25870 if (row == last)
25872 if (!row->reversed_p)
25873 end_hpos = hlinfo->mouse_face_end_col;
25874 else if (row == first)
25875 end_hpos = hlinfo->mouse_face_beg_col;
25876 else
25878 end_hpos = row->used[TEXT_AREA];
25879 if (draw == DRAW_NORMAL_TEXT)
25880 row->fill_line_p = 1; /* Clear to end of line */
25883 else if (row->reversed_p && row == first)
25884 end_hpos = hlinfo->mouse_face_beg_col;
25885 else
25887 end_hpos = row->used[TEXT_AREA];
25888 if (draw == DRAW_NORMAL_TEXT)
25889 row->fill_line_p = 1; /* Clear to end of line */
25892 if (end_hpos > start_hpos)
25894 draw_row_with_mouse_face (w, start_x, row,
25895 start_hpos, end_hpos, draw);
25897 row->mouse_face_p
25898 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
25902 #ifdef HAVE_WINDOW_SYSTEM
25903 /* When we've written over the cursor, arrange for it to
25904 be displayed again. */
25905 if (FRAME_WINDOW_P (f)
25906 && phys_cursor_on_p && !w->phys_cursor_on_p)
25908 int hpos = w->phys_cursor.hpos;
25910 /* When the window is hscrolled, cursor hpos can legitimately be
25911 out of bounds, but we draw the cursor at the corresponding
25912 window margin in that case. */
25913 if (!row->reversed_p && hpos < 0)
25914 hpos = 0;
25915 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25916 hpos = row->used[TEXT_AREA] - 1;
25918 BLOCK_INPUT;
25919 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
25920 w->phys_cursor.x, w->phys_cursor.y);
25921 UNBLOCK_INPUT;
25923 #endif /* HAVE_WINDOW_SYSTEM */
25926 #ifdef HAVE_WINDOW_SYSTEM
25927 /* Change the mouse cursor. */
25928 if (FRAME_WINDOW_P (f))
25930 if (draw == DRAW_NORMAL_TEXT
25931 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
25932 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
25933 else if (draw == DRAW_MOUSE_FACE)
25934 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
25935 else
25936 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
25938 #endif /* HAVE_WINDOW_SYSTEM */
25941 /* EXPORT:
25942 Clear out the mouse-highlighted active region.
25943 Redraw it un-highlighted first. Value is non-zero if mouse
25944 face was actually drawn unhighlighted. */
25947 clear_mouse_face (Mouse_HLInfo *hlinfo)
25949 int cleared = 0;
25951 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
25953 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
25954 cleared = 1;
25957 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25958 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25959 hlinfo->mouse_face_window = Qnil;
25960 hlinfo->mouse_face_overlay = Qnil;
25961 return cleared;
25964 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
25965 within the mouse face on that window. */
25966 static int
25967 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
25969 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25971 /* Quickly resolve the easy cases. */
25972 if (!(WINDOWP (hlinfo->mouse_face_window)
25973 && XWINDOW (hlinfo->mouse_face_window) == w))
25974 return 0;
25975 if (vpos < hlinfo->mouse_face_beg_row
25976 || vpos > hlinfo->mouse_face_end_row)
25977 return 0;
25978 if (vpos > hlinfo->mouse_face_beg_row
25979 && vpos < hlinfo->mouse_face_end_row)
25980 return 1;
25982 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
25984 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25986 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
25987 return 1;
25989 else if ((vpos == hlinfo->mouse_face_beg_row
25990 && hpos >= hlinfo->mouse_face_beg_col)
25991 || (vpos == hlinfo->mouse_face_end_row
25992 && hpos < hlinfo->mouse_face_end_col))
25993 return 1;
25995 else
25997 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25999 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26000 return 1;
26002 else if ((vpos == hlinfo->mouse_face_beg_row
26003 && hpos <= hlinfo->mouse_face_beg_col)
26004 || (vpos == hlinfo->mouse_face_end_row
26005 && hpos > hlinfo->mouse_face_end_col))
26006 return 1;
26008 return 0;
26012 /* EXPORT:
26013 Non-zero if physical cursor of window W is within mouse face. */
26016 cursor_in_mouse_face_p (struct window *w)
26018 int hpos = w->phys_cursor.hpos;
26019 int vpos = w->phys_cursor.vpos;
26020 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26022 /* When the window is hscrolled, cursor hpos can legitimately be out
26023 of bounds, but we draw the cursor at the corresponding window
26024 margin in that case. */
26025 if (!row->reversed_p && hpos < 0)
26026 hpos = 0;
26027 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26028 hpos = row->used[TEXT_AREA] - 1;
26030 return coords_in_mouse_face_p (w, hpos, vpos);
26035 /* Find the glyph rows START_ROW and END_ROW of window W that display
26036 characters between buffer positions START_CHARPOS and END_CHARPOS
26037 (excluding END_CHARPOS). DISP_STRING is a display string that
26038 covers these buffer positions. This is similar to
26039 row_containing_pos, but is more accurate when bidi reordering makes
26040 buffer positions change non-linearly with glyph rows. */
26041 static void
26042 rows_from_pos_range (struct window *w,
26043 EMACS_INT start_charpos, EMACS_INT end_charpos,
26044 Lisp_Object disp_string,
26045 struct glyph_row **start, struct glyph_row **end)
26047 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26048 int last_y = window_text_bottom_y (w);
26049 struct glyph_row *row;
26051 *start = NULL;
26052 *end = NULL;
26054 while (!first->enabled_p
26055 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26056 first++;
26058 /* Find the START row. */
26059 for (row = first;
26060 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26061 row++)
26063 /* A row can potentially be the START row if the range of the
26064 characters it displays intersects the range
26065 [START_CHARPOS..END_CHARPOS). */
26066 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26067 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26068 /* See the commentary in row_containing_pos, for the
26069 explanation of the complicated way to check whether
26070 some position is beyond the end of the characters
26071 displayed by a row. */
26072 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26073 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26074 && !row->ends_at_zv_p
26075 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26076 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26077 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26078 && !row->ends_at_zv_p
26079 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26081 /* Found a candidate row. Now make sure at least one of the
26082 glyphs it displays has a charpos from the range
26083 [START_CHARPOS..END_CHARPOS).
26085 This is not obvious because bidi reordering could make
26086 buffer positions of a row be 1,2,3,102,101,100, and if we
26087 want to highlight characters in [50..60), we don't want
26088 this row, even though [50..60) does intersect [1..103),
26089 the range of character positions given by the row's start
26090 and end positions. */
26091 struct glyph *g = row->glyphs[TEXT_AREA];
26092 struct glyph *e = g + row->used[TEXT_AREA];
26094 while (g < e)
26096 if (((BUFFERP (g->object) || INTEGERP (g->object))
26097 && start_charpos <= g->charpos && g->charpos < end_charpos)
26098 /* A glyph that comes from DISP_STRING is by
26099 definition to be highlighted. */
26100 || EQ (g->object, disp_string))
26101 *start = row;
26102 g++;
26104 if (*start)
26105 break;
26109 /* Find the END row. */
26110 if (!*start
26111 /* If the last row is partially visible, start looking for END
26112 from that row, instead of starting from FIRST. */
26113 && !(row->enabled_p
26114 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26115 row = first;
26116 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26118 struct glyph_row *next = row + 1;
26119 EMACS_INT next_start = MATRIX_ROW_START_CHARPOS (next);
26121 if (!next->enabled_p
26122 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26123 /* The first row >= START whose range of displayed characters
26124 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26125 is the row END + 1. */
26126 || (start_charpos < next_start
26127 && end_charpos < next_start)
26128 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26129 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26130 && !next->ends_at_zv_p
26131 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26132 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26133 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26134 && !next->ends_at_zv_p
26135 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26137 *end = row;
26138 break;
26140 else
26142 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26143 but none of the characters it displays are in the range, it is
26144 also END + 1. */
26145 struct glyph *g = next->glyphs[TEXT_AREA];
26146 struct glyph *s = g;
26147 struct glyph *e = g + next->used[TEXT_AREA];
26149 while (g < e)
26151 if (((BUFFERP (g->object) || INTEGERP (g->object))
26152 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26153 /* If the buffer position of the first glyph in
26154 the row is equal to END_CHARPOS, it means
26155 the last character to be highlighted is the
26156 newline of ROW, and we must consider NEXT as
26157 END, not END+1. */
26158 || (((!next->reversed_p && g == s)
26159 || (next->reversed_p && g == e - 1))
26160 && (g->charpos == end_charpos
26161 /* Special case for when NEXT is an
26162 empty line at ZV. */
26163 || (g->charpos == -1
26164 && !row->ends_at_zv_p
26165 && next_start == end_charpos)))))
26166 /* A glyph that comes from DISP_STRING is by
26167 definition to be highlighted. */
26168 || EQ (g->object, disp_string))
26169 break;
26170 g++;
26172 if (g == e)
26174 *end = row;
26175 break;
26177 /* The first row that ends at ZV must be the last to be
26178 highlighted. */
26179 else if (next->ends_at_zv_p)
26181 *end = next;
26182 break;
26188 /* This function sets the mouse_face_* elements of HLINFO, assuming
26189 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26190 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26191 for the overlay or run of text properties specifying the mouse
26192 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26193 before-string and after-string that must also be highlighted.
26194 DISP_STRING, if non-nil, is a display string that may cover some
26195 or all of the highlighted text. */
26197 static void
26198 mouse_face_from_buffer_pos (Lisp_Object window,
26199 Mouse_HLInfo *hlinfo,
26200 EMACS_INT mouse_charpos,
26201 EMACS_INT start_charpos,
26202 EMACS_INT end_charpos,
26203 Lisp_Object before_string,
26204 Lisp_Object after_string,
26205 Lisp_Object disp_string)
26207 struct window *w = XWINDOW (window);
26208 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26209 struct glyph_row *r1, *r2;
26210 struct glyph *glyph, *end;
26211 EMACS_INT ignore, pos;
26212 int x;
26214 xassert (NILP (disp_string) || STRINGP (disp_string));
26215 xassert (NILP (before_string) || STRINGP (before_string));
26216 xassert (NILP (after_string) || STRINGP (after_string));
26218 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26219 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26220 if (r1 == NULL)
26221 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26222 /* If the before-string or display-string contains newlines,
26223 rows_from_pos_range skips to its last row. Move back. */
26224 if (!NILP (before_string) || !NILP (disp_string))
26226 struct glyph_row *prev;
26227 while ((prev = r1 - 1, prev >= first)
26228 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26229 && prev->used[TEXT_AREA] > 0)
26231 struct glyph *beg = prev->glyphs[TEXT_AREA];
26232 glyph = beg + prev->used[TEXT_AREA];
26233 while (--glyph >= beg && INTEGERP (glyph->object));
26234 if (glyph < beg
26235 || !(EQ (glyph->object, before_string)
26236 || EQ (glyph->object, disp_string)))
26237 break;
26238 r1 = prev;
26241 if (r2 == NULL)
26243 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26244 hlinfo->mouse_face_past_end = 1;
26246 else if (!NILP (after_string))
26248 /* If the after-string has newlines, advance to its last row. */
26249 struct glyph_row *next;
26250 struct glyph_row *last
26251 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26253 for (next = r2 + 1;
26254 next <= last
26255 && next->used[TEXT_AREA] > 0
26256 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26257 ++next)
26258 r2 = next;
26260 /* The rest of the display engine assumes that mouse_face_beg_row is
26261 either above mouse_face_end_row or identical to it. But with
26262 bidi-reordered continued lines, the row for START_CHARPOS could
26263 be below the row for END_CHARPOS. If so, swap the rows and store
26264 them in correct order. */
26265 if (r1->y > r2->y)
26267 struct glyph_row *tem = r2;
26269 r2 = r1;
26270 r1 = tem;
26273 hlinfo->mouse_face_beg_y = r1->y;
26274 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26275 hlinfo->mouse_face_end_y = r2->y;
26276 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26278 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26279 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26280 could be anywhere in the row and in any order. The strategy
26281 below is to find the leftmost and the rightmost glyph that
26282 belongs to either of these 3 strings, or whose position is
26283 between START_CHARPOS and END_CHARPOS, and highlight all the
26284 glyphs between those two. This may cover more than just the text
26285 between START_CHARPOS and END_CHARPOS if the range of characters
26286 strides the bidi level boundary, e.g. if the beginning is in R2L
26287 text while the end is in L2R text or vice versa. */
26288 if (!r1->reversed_p)
26290 /* This row is in a left to right paragraph. Scan it left to
26291 right. */
26292 glyph = r1->glyphs[TEXT_AREA];
26293 end = glyph + r1->used[TEXT_AREA];
26294 x = r1->x;
26296 /* Skip truncation glyphs at the start of the glyph row. */
26297 if (r1->displays_text_p)
26298 for (; glyph < end
26299 && INTEGERP (glyph->object)
26300 && glyph->charpos < 0;
26301 ++glyph)
26302 x += glyph->pixel_width;
26304 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26305 or DISP_STRING, and the first glyph from buffer whose
26306 position is between START_CHARPOS and END_CHARPOS. */
26307 for (; glyph < end
26308 && !INTEGERP (glyph->object)
26309 && !EQ (glyph->object, disp_string)
26310 && !(BUFFERP (glyph->object)
26311 && (glyph->charpos >= start_charpos
26312 && glyph->charpos < end_charpos));
26313 ++glyph)
26315 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26316 are present at buffer positions between START_CHARPOS and
26317 END_CHARPOS, or if they come from an overlay. */
26318 if (EQ (glyph->object, before_string))
26320 pos = string_buffer_position (before_string,
26321 start_charpos);
26322 /* If pos == 0, it means before_string came from an
26323 overlay, not from a buffer position. */
26324 if (!pos || (pos >= start_charpos && pos < end_charpos))
26325 break;
26327 else if (EQ (glyph->object, after_string))
26329 pos = string_buffer_position (after_string, end_charpos);
26330 if (!pos || (pos >= start_charpos && pos < end_charpos))
26331 break;
26333 x += glyph->pixel_width;
26335 hlinfo->mouse_face_beg_x = x;
26336 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26338 else
26340 /* This row is in a right to left paragraph. Scan it right to
26341 left. */
26342 struct glyph *g;
26344 end = r1->glyphs[TEXT_AREA] - 1;
26345 glyph = end + r1->used[TEXT_AREA];
26347 /* Skip truncation glyphs at the start of the glyph row. */
26348 if (r1->displays_text_p)
26349 for (; glyph > end
26350 && INTEGERP (glyph->object)
26351 && glyph->charpos < 0;
26352 --glyph)
26355 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26356 or DISP_STRING, and the first glyph from buffer whose
26357 position is between START_CHARPOS and END_CHARPOS. */
26358 for (; glyph > end
26359 && !INTEGERP (glyph->object)
26360 && !EQ (glyph->object, disp_string)
26361 && !(BUFFERP (glyph->object)
26362 && (glyph->charpos >= start_charpos
26363 && glyph->charpos < end_charpos));
26364 --glyph)
26366 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26367 are present at buffer positions between START_CHARPOS and
26368 END_CHARPOS, or if they come from an overlay. */
26369 if (EQ (glyph->object, before_string))
26371 pos = string_buffer_position (before_string, start_charpos);
26372 /* If pos == 0, it means before_string came from an
26373 overlay, not from a buffer position. */
26374 if (!pos || (pos >= start_charpos && pos < end_charpos))
26375 break;
26377 else if (EQ (glyph->object, after_string))
26379 pos = string_buffer_position (after_string, end_charpos);
26380 if (!pos || (pos >= start_charpos && pos < end_charpos))
26381 break;
26385 glyph++; /* first glyph to the right of the highlighted area */
26386 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26387 x += g->pixel_width;
26388 hlinfo->mouse_face_beg_x = x;
26389 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26392 /* If the highlight ends in a different row, compute GLYPH and END
26393 for the end row. Otherwise, reuse the values computed above for
26394 the row where the highlight begins. */
26395 if (r2 != r1)
26397 if (!r2->reversed_p)
26399 glyph = r2->glyphs[TEXT_AREA];
26400 end = glyph + r2->used[TEXT_AREA];
26401 x = r2->x;
26403 else
26405 end = r2->glyphs[TEXT_AREA] - 1;
26406 glyph = end + r2->used[TEXT_AREA];
26410 if (!r2->reversed_p)
26412 /* Skip truncation and continuation glyphs near the end of the
26413 row, and also blanks and stretch glyphs inserted by
26414 extend_face_to_end_of_line. */
26415 while (end > glyph
26416 && INTEGERP ((end - 1)->object))
26417 --end;
26418 /* Scan the rest of the glyph row from the end, looking for the
26419 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26420 DISP_STRING, or whose position is between START_CHARPOS
26421 and END_CHARPOS */
26422 for (--end;
26423 end > glyph
26424 && !INTEGERP (end->object)
26425 && !EQ (end->object, disp_string)
26426 && !(BUFFERP (end->object)
26427 && (end->charpos >= start_charpos
26428 && end->charpos < end_charpos));
26429 --end)
26431 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26432 are present at buffer positions between START_CHARPOS and
26433 END_CHARPOS, or if they come from an overlay. */
26434 if (EQ (end->object, before_string))
26436 pos = string_buffer_position (before_string, start_charpos);
26437 if (!pos || (pos >= start_charpos && pos < end_charpos))
26438 break;
26440 else if (EQ (end->object, after_string))
26442 pos = string_buffer_position (after_string, end_charpos);
26443 if (!pos || (pos >= start_charpos && pos < end_charpos))
26444 break;
26447 /* Find the X coordinate of the last glyph to be highlighted. */
26448 for (; glyph <= end; ++glyph)
26449 x += glyph->pixel_width;
26451 hlinfo->mouse_face_end_x = x;
26452 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26454 else
26456 /* Skip truncation and continuation glyphs near the end of the
26457 row, and also blanks and stretch glyphs inserted by
26458 extend_face_to_end_of_line. */
26459 x = r2->x;
26460 end++;
26461 while (end < glyph
26462 && INTEGERP (end->object))
26464 x += end->pixel_width;
26465 ++end;
26467 /* Scan the rest of the glyph row from the end, looking for the
26468 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26469 DISP_STRING, or whose position is between START_CHARPOS
26470 and END_CHARPOS */
26471 for ( ;
26472 end < glyph
26473 && !INTEGERP (end->object)
26474 && !EQ (end->object, disp_string)
26475 && !(BUFFERP (end->object)
26476 && (end->charpos >= start_charpos
26477 && end->charpos < end_charpos));
26478 ++end)
26480 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26481 are present at buffer positions between START_CHARPOS and
26482 END_CHARPOS, or if they come from an overlay. */
26483 if (EQ (end->object, before_string))
26485 pos = string_buffer_position (before_string, start_charpos);
26486 if (!pos || (pos >= start_charpos && pos < end_charpos))
26487 break;
26489 else if (EQ (end->object, after_string))
26491 pos = string_buffer_position (after_string, end_charpos);
26492 if (!pos || (pos >= start_charpos && pos < end_charpos))
26493 break;
26495 x += end->pixel_width;
26497 /* If we exited the above loop because we arrived at the last
26498 glyph of the row, and its buffer position is still not in
26499 range, it means the last character in range is the preceding
26500 newline. Bump the end column and x values to get past the
26501 last glyph. */
26502 if (end == glyph
26503 && BUFFERP (end->object)
26504 && (end->charpos < start_charpos
26505 || end->charpos >= end_charpos))
26507 x += end->pixel_width;
26508 ++end;
26510 hlinfo->mouse_face_end_x = x;
26511 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26514 hlinfo->mouse_face_window = window;
26515 hlinfo->mouse_face_face_id
26516 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26517 mouse_charpos + 1,
26518 !hlinfo->mouse_face_hidden, -1);
26519 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26522 /* The following function is not used anymore (replaced with
26523 mouse_face_from_string_pos), but I leave it here for the time
26524 being, in case someone would. */
26526 #if 0 /* not used */
26528 /* Find the position of the glyph for position POS in OBJECT in
26529 window W's current matrix, and return in *X, *Y the pixel
26530 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26532 RIGHT_P non-zero means return the position of the right edge of the
26533 glyph, RIGHT_P zero means return the left edge position.
26535 If no glyph for POS exists in the matrix, return the position of
26536 the glyph with the next smaller position that is in the matrix, if
26537 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26538 exists in the matrix, return the position of the glyph with the
26539 next larger position in OBJECT.
26541 Value is non-zero if a glyph was found. */
26543 static int
26544 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
26545 int *hpos, int *vpos, int *x, int *y, int right_p)
26547 int yb = window_text_bottom_y (w);
26548 struct glyph_row *r;
26549 struct glyph *best_glyph = NULL;
26550 struct glyph_row *best_row = NULL;
26551 int best_x = 0;
26553 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26554 r->enabled_p && r->y < yb;
26555 ++r)
26557 struct glyph *g = r->glyphs[TEXT_AREA];
26558 struct glyph *e = g + r->used[TEXT_AREA];
26559 int gx;
26561 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26562 if (EQ (g->object, object))
26564 if (g->charpos == pos)
26566 best_glyph = g;
26567 best_x = gx;
26568 best_row = r;
26569 goto found;
26571 else if (best_glyph == NULL
26572 || ((eabs (g->charpos - pos)
26573 < eabs (best_glyph->charpos - pos))
26574 && (right_p
26575 ? g->charpos < pos
26576 : g->charpos > pos)))
26578 best_glyph = g;
26579 best_x = gx;
26580 best_row = r;
26585 found:
26587 if (best_glyph)
26589 *x = best_x;
26590 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26592 if (right_p)
26594 *x += best_glyph->pixel_width;
26595 ++*hpos;
26598 *y = best_row->y;
26599 *vpos = best_row - w->current_matrix->rows;
26602 return best_glyph != NULL;
26604 #endif /* not used */
26606 /* Find the positions of the first and the last glyphs in window W's
26607 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26608 (assumed to be a string), and return in HLINFO's mouse_face_*
26609 members the pixel and column/row coordinates of those glyphs. */
26611 static void
26612 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26613 Lisp_Object object,
26614 EMACS_INT startpos, EMACS_INT endpos)
26616 int yb = window_text_bottom_y (w);
26617 struct glyph_row *r;
26618 struct glyph *g, *e;
26619 int gx;
26620 int found = 0;
26622 /* Find the glyph row with at least one position in the range
26623 [STARTPOS..ENDPOS], and the first glyph in that row whose
26624 position belongs to that range. */
26625 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26626 r->enabled_p && r->y < yb;
26627 ++r)
26629 if (!r->reversed_p)
26631 g = r->glyphs[TEXT_AREA];
26632 e = g + r->used[TEXT_AREA];
26633 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26634 if (EQ (g->object, object)
26635 && startpos <= g->charpos && g->charpos <= endpos)
26637 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26638 hlinfo->mouse_face_beg_y = r->y;
26639 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26640 hlinfo->mouse_face_beg_x = gx;
26641 found = 1;
26642 break;
26645 else
26647 struct glyph *g1;
26649 e = r->glyphs[TEXT_AREA];
26650 g = e + r->used[TEXT_AREA];
26651 for ( ; g > e; --g)
26652 if (EQ ((g-1)->object, object)
26653 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26655 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26656 hlinfo->mouse_face_beg_y = r->y;
26657 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26658 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26659 gx += g1->pixel_width;
26660 hlinfo->mouse_face_beg_x = gx;
26661 found = 1;
26662 break;
26665 if (found)
26666 break;
26669 if (!found)
26670 return;
26672 /* Starting with the next row, look for the first row which does NOT
26673 include any glyphs whose positions are in the range. */
26674 for (++r; r->enabled_p && r->y < yb; ++r)
26676 g = r->glyphs[TEXT_AREA];
26677 e = g + r->used[TEXT_AREA];
26678 found = 0;
26679 for ( ; g < e; ++g)
26680 if (EQ (g->object, object)
26681 && startpos <= g->charpos && g->charpos <= endpos)
26683 found = 1;
26684 break;
26686 if (!found)
26687 break;
26690 /* The highlighted region ends on the previous row. */
26691 r--;
26693 /* Set the end row and its vertical pixel coordinate. */
26694 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
26695 hlinfo->mouse_face_end_y = r->y;
26697 /* Compute and set the end column and the end column's horizontal
26698 pixel coordinate. */
26699 if (!r->reversed_p)
26701 g = r->glyphs[TEXT_AREA];
26702 e = g + r->used[TEXT_AREA];
26703 for ( ; e > g; --e)
26704 if (EQ ((e-1)->object, object)
26705 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
26706 break;
26707 hlinfo->mouse_face_end_col = e - g;
26709 for (gx = r->x; g < e; ++g)
26710 gx += g->pixel_width;
26711 hlinfo->mouse_face_end_x = gx;
26713 else
26715 e = r->glyphs[TEXT_AREA];
26716 g = e + r->used[TEXT_AREA];
26717 for (gx = r->x ; e < g; ++e)
26719 if (EQ (e->object, object)
26720 && startpos <= e->charpos && e->charpos <= endpos)
26721 break;
26722 gx += e->pixel_width;
26724 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
26725 hlinfo->mouse_face_end_x = gx;
26729 #ifdef HAVE_WINDOW_SYSTEM
26731 /* See if position X, Y is within a hot-spot of an image. */
26733 static int
26734 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
26736 if (!CONSP (hot_spot))
26737 return 0;
26739 if (EQ (XCAR (hot_spot), Qrect))
26741 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
26742 Lisp_Object rect = XCDR (hot_spot);
26743 Lisp_Object tem;
26744 if (!CONSP (rect))
26745 return 0;
26746 if (!CONSP (XCAR (rect)))
26747 return 0;
26748 if (!CONSP (XCDR (rect)))
26749 return 0;
26750 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
26751 return 0;
26752 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
26753 return 0;
26754 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
26755 return 0;
26756 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
26757 return 0;
26758 return 1;
26760 else if (EQ (XCAR (hot_spot), Qcircle))
26762 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
26763 Lisp_Object circ = XCDR (hot_spot);
26764 Lisp_Object lr, lx0, ly0;
26765 if (CONSP (circ)
26766 && CONSP (XCAR (circ))
26767 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
26768 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
26769 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
26771 double r = XFLOATINT (lr);
26772 double dx = XINT (lx0) - x;
26773 double dy = XINT (ly0) - y;
26774 return (dx * dx + dy * dy <= r * r);
26777 else if (EQ (XCAR (hot_spot), Qpoly))
26779 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
26780 if (VECTORP (XCDR (hot_spot)))
26782 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
26783 Lisp_Object *poly = v->contents;
26784 int n = v->header.size;
26785 int i;
26786 int inside = 0;
26787 Lisp_Object lx, ly;
26788 int x0, y0;
26790 /* Need an even number of coordinates, and at least 3 edges. */
26791 if (n < 6 || n & 1)
26792 return 0;
26794 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
26795 If count is odd, we are inside polygon. Pixels on edges
26796 may or may not be included depending on actual geometry of the
26797 polygon. */
26798 if ((lx = poly[n-2], !INTEGERP (lx))
26799 || (ly = poly[n-1], !INTEGERP (lx)))
26800 return 0;
26801 x0 = XINT (lx), y0 = XINT (ly);
26802 for (i = 0; i < n; i += 2)
26804 int x1 = x0, y1 = y0;
26805 if ((lx = poly[i], !INTEGERP (lx))
26806 || (ly = poly[i+1], !INTEGERP (ly)))
26807 return 0;
26808 x0 = XINT (lx), y0 = XINT (ly);
26810 /* Does this segment cross the X line? */
26811 if (x0 >= x)
26813 if (x1 >= x)
26814 continue;
26816 else if (x1 < x)
26817 continue;
26818 if (y > y0 && y > y1)
26819 continue;
26820 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
26821 inside = !inside;
26823 return inside;
26826 return 0;
26829 Lisp_Object
26830 find_hot_spot (Lisp_Object map, int x, int y)
26832 while (CONSP (map))
26834 if (CONSP (XCAR (map))
26835 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
26836 return XCAR (map);
26837 map = XCDR (map);
26840 return Qnil;
26843 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
26844 3, 3, 0,
26845 doc: /* Lookup in image map MAP coordinates X and Y.
26846 An image map is an alist where each element has the format (AREA ID PLIST).
26847 An AREA is specified as either a rectangle, a circle, or a polygon:
26848 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
26849 pixel coordinates of the upper left and bottom right corners.
26850 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
26851 and the radius of the circle; r may be a float or integer.
26852 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
26853 vector describes one corner in the polygon.
26854 Returns the alist element for the first matching AREA in MAP. */)
26855 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
26857 if (NILP (map))
26858 return Qnil;
26860 CHECK_NUMBER (x);
26861 CHECK_NUMBER (y);
26863 return find_hot_spot (map, XINT (x), XINT (y));
26867 /* Display frame CURSOR, optionally using shape defined by POINTER. */
26868 static void
26869 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
26871 /* Do not change cursor shape while dragging mouse. */
26872 if (!NILP (do_mouse_tracking))
26873 return;
26875 if (!NILP (pointer))
26877 if (EQ (pointer, Qarrow))
26878 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26879 else if (EQ (pointer, Qhand))
26880 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
26881 else if (EQ (pointer, Qtext))
26882 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26883 else if (EQ (pointer, intern ("hdrag")))
26884 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26885 #ifdef HAVE_X_WINDOWS
26886 else if (EQ (pointer, intern ("vdrag")))
26887 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
26888 #endif
26889 else if (EQ (pointer, intern ("hourglass")))
26890 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
26891 else if (EQ (pointer, Qmodeline))
26892 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
26893 else
26894 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26897 if (cursor != No_Cursor)
26898 FRAME_RIF (f)->define_frame_cursor (f, cursor);
26901 #endif /* HAVE_WINDOW_SYSTEM */
26903 /* Take proper action when mouse has moved to the mode or header line
26904 or marginal area AREA of window W, x-position X and y-position Y.
26905 X is relative to the start of the text display area of W, so the
26906 width of bitmap areas and scroll bars must be subtracted to get a
26907 position relative to the start of the mode line. */
26909 static void
26910 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
26911 enum window_part area)
26913 struct window *w = XWINDOW (window);
26914 struct frame *f = XFRAME (w->frame);
26915 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26916 #ifdef HAVE_WINDOW_SYSTEM
26917 Display_Info *dpyinfo;
26918 #endif
26919 Cursor cursor = No_Cursor;
26920 Lisp_Object pointer = Qnil;
26921 int dx, dy, width, height;
26922 EMACS_INT charpos;
26923 Lisp_Object string, object = Qnil;
26924 Lisp_Object pos, help;
26926 Lisp_Object mouse_face;
26927 int original_x_pixel = x;
26928 struct glyph * glyph = NULL, * row_start_glyph = NULL;
26929 struct glyph_row *row;
26931 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
26933 int x0;
26934 struct glyph *end;
26936 /* Kludge alert: mode_line_string takes X/Y in pixels, but
26937 returns them in row/column units! */
26938 string = mode_line_string (w, area, &x, &y, &charpos,
26939 &object, &dx, &dy, &width, &height);
26941 row = (area == ON_MODE_LINE
26942 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
26943 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
26945 /* Find the glyph under the mouse pointer. */
26946 if (row->mode_line_p && row->enabled_p)
26948 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
26949 end = glyph + row->used[TEXT_AREA];
26951 for (x0 = original_x_pixel;
26952 glyph < end && x0 >= glyph->pixel_width;
26953 ++glyph)
26954 x0 -= glyph->pixel_width;
26956 if (glyph >= end)
26957 glyph = NULL;
26960 else
26962 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
26963 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
26964 returns them in row/column units! */
26965 string = marginal_area_string (w, area, &x, &y, &charpos,
26966 &object, &dx, &dy, &width, &height);
26969 help = Qnil;
26971 #ifdef HAVE_WINDOW_SYSTEM
26972 if (IMAGEP (object))
26974 Lisp_Object image_map, hotspot;
26975 if ((image_map = Fplist_get (XCDR (object), QCmap),
26976 !NILP (image_map))
26977 && (hotspot = find_hot_spot (image_map, dx, dy),
26978 CONSP (hotspot))
26979 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
26981 Lisp_Object plist;
26983 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
26984 If so, we could look for mouse-enter, mouse-leave
26985 properties in PLIST (and do something...). */
26986 hotspot = XCDR (hotspot);
26987 if (CONSP (hotspot)
26988 && (plist = XCAR (hotspot), CONSP (plist)))
26990 pointer = Fplist_get (plist, Qpointer);
26991 if (NILP (pointer))
26992 pointer = Qhand;
26993 help = Fplist_get (plist, Qhelp_echo);
26994 if (!NILP (help))
26996 help_echo_string = help;
26997 /* Is this correct? ++kfs */
26998 XSETWINDOW (help_echo_window, w);
26999 help_echo_object = w->buffer;
27000 help_echo_pos = charpos;
27004 if (NILP (pointer))
27005 pointer = Fplist_get (XCDR (object), QCpointer);
27007 #endif /* HAVE_WINDOW_SYSTEM */
27009 if (STRINGP (string))
27011 pos = make_number (charpos);
27012 /* If we're on a string with `help-echo' text property, arrange
27013 for the help to be displayed. This is done by setting the
27014 global variable help_echo_string to the help string. */
27015 if (NILP (help))
27017 help = Fget_text_property (pos, Qhelp_echo, string);
27018 if (!NILP (help))
27020 help_echo_string = help;
27021 XSETWINDOW (help_echo_window, w);
27022 help_echo_object = string;
27023 help_echo_pos = charpos;
27027 #ifdef HAVE_WINDOW_SYSTEM
27028 if (FRAME_WINDOW_P (f))
27030 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27031 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27032 if (NILP (pointer))
27033 pointer = Fget_text_property (pos, Qpointer, string);
27035 /* Change the mouse pointer according to what is under X/Y. */
27036 if (NILP (pointer)
27037 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27039 Lisp_Object map;
27040 map = Fget_text_property (pos, Qlocal_map, string);
27041 if (!KEYMAPP (map))
27042 map = Fget_text_property (pos, Qkeymap, string);
27043 if (!KEYMAPP (map))
27044 cursor = dpyinfo->vertical_scroll_bar_cursor;
27047 #endif
27049 /* Change the mouse face according to what is under X/Y. */
27050 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27051 if (!NILP (mouse_face)
27052 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27053 && glyph)
27055 Lisp_Object b, e;
27057 struct glyph * tmp_glyph;
27059 int gpos;
27060 int gseq_length;
27061 int total_pixel_width;
27062 EMACS_INT begpos, endpos, ignore;
27064 int vpos, hpos;
27066 b = Fprevious_single_property_change (make_number (charpos + 1),
27067 Qmouse_face, string, Qnil);
27068 if (NILP (b))
27069 begpos = 0;
27070 else
27071 begpos = XINT (b);
27073 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27074 if (NILP (e))
27075 endpos = SCHARS (string);
27076 else
27077 endpos = XINT (e);
27079 /* Calculate the glyph position GPOS of GLYPH in the
27080 displayed string, relative to the beginning of the
27081 highlighted part of the string.
27083 Note: GPOS is different from CHARPOS. CHARPOS is the
27084 position of GLYPH in the internal string object. A mode
27085 line string format has structures which are converted to
27086 a flattened string by the Emacs Lisp interpreter. The
27087 internal string is an element of those structures. The
27088 displayed string is the flattened string. */
27089 tmp_glyph = row_start_glyph;
27090 while (tmp_glyph < glyph
27091 && (!(EQ (tmp_glyph->object, glyph->object)
27092 && begpos <= tmp_glyph->charpos
27093 && tmp_glyph->charpos < endpos)))
27094 tmp_glyph++;
27095 gpos = glyph - tmp_glyph;
27097 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27098 the highlighted part of the displayed string to which
27099 GLYPH belongs. Note: GSEQ_LENGTH is different from
27100 SCHARS (STRING), because the latter returns the length of
27101 the internal string. */
27102 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27103 tmp_glyph > glyph
27104 && (!(EQ (tmp_glyph->object, glyph->object)
27105 && begpos <= tmp_glyph->charpos
27106 && tmp_glyph->charpos < endpos));
27107 tmp_glyph--)
27109 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27111 /* Calculate the total pixel width of all the glyphs between
27112 the beginning of the highlighted area and GLYPH. */
27113 total_pixel_width = 0;
27114 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27115 total_pixel_width += tmp_glyph->pixel_width;
27117 /* Pre calculation of re-rendering position. Note: X is in
27118 column units here, after the call to mode_line_string or
27119 marginal_area_string. */
27120 hpos = x - gpos;
27121 vpos = (area == ON_MODE_LINE
27122 ? (w->current_matrix)->nrows - 1
27123 : 0);
27125 /* If GLYPH's position is included in the region that is
27126 already drawn in mouse face, we have nothing to do. */
27127 if ( EQ (window, hlinfo->mouse_face_window)
27128 && (!row->reversed_p
27129 ? (hlinfo->mouse_face_beg_col <= hpos
27130 && hpos < hlinfo->mouse_face_end_col)
27131 /* In R2L rows we swap BEG and END, see below. */
27132 : (hlinfo->mouse_face_end_col <= hpos
27133 && hpos < hlinfo->mouse_face_beg_col))
27134 && hlinfo->mouse_face_beg_row == vpos )
27135 return;
27137 if (clear_mouse_face (hlinfo))
27138 cursor = No_Cursor;
27140 if (!row->reversed_p)
27142 hlinfo->mouse_face_beg_col = hpos;
27143 hlinfo->mouse_face_beg_x = original_x_pixel
27144 - (total_pixel_width + dx);
27145 hlinfo->mouse_face_end_col = hpos + gseq_length;
27146 hlinfo->mouse_face_end_x = 0;
27148 else
27150 /* In R2L rows, show_mouse_face expects BEG and END
27151 coordinates to be swapped. */
27152 hlinfo->mouse_face_end_col = hpos;
27153 hlinfo->mouse_face_end_x = original_x_pixel
27154 - (total_pixel_width + dx);
27155 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27156 hlinfo->mouse_face_beg_x = 0;
27159 hlinfo->mouse_face_beg_row = vpos;
27160 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27161 hlinfo->mouse_face_beg_y = 0;
27162 hlinfo->mouse_face_end_y = 0;
27163 hlinfo->mouse_face_past_end = 0;
27164 hlinfo->mouse_face_window = window;
27166 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27167 charpos,
27168 0, 0, 0,
27169 &ignore,
27170 glyph->face_id,
27172 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27174 if (NILP (pointer))
27175 pointer = Qhand;
27177 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27178 clear_mouse_face (hlinfo);
27180 #ifdef HAVE_WINDOW_SYSTEM
27181 if (FRAME_WINDOW_P (f))
27182 define_frame_cursor1 (f, cursor, pointer);
27183 #endif
27187 /* EXPORT:
27188 Take proper action when the mouse has moved to position X, Y on
27189 frame F as regards highlighting characters that have mouse-face
27190 properties. Also de-highlighting chars where the mouse was before.
27191 X and Y can be negative or out of range. */
27193 void
27194 note_mouse_highlight (struct frame *f, int x, int y)
27196 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27197 enum window_part part = ON_NOTHING;
27198 Lisp_Object window;
27199 struct window *w;
27200 Cursor cursor = No_Cursor;
27201 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27202 struct buffer *b;
27204 /* When a menu is active, don't highlight because this looks odd. */
27205 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27206 if (popup_activated ())
27207 return;
27208 #endif
27210 if (NILP (Vmouse_highlight)
27211 || !f->glyphs_initialized_p
27212 || f->pointer_invisible)
27213 return;
27215 hlinfo->mouse_face_mouse_x = x;
27216 hlinfo->mouse_face_mouse_y = y;
27217 hlinfo->mouse_face_mouse_frame = f;
27219 if (hlinfo->mouse_face_defer)
27220 return;
27222 if (gc_in_progress)
27224 hlinfo->mouse_face_deferred_gc = 1;
27225 return;
27228 /* Which window is that in? */
27229 window = window_from_coordinates (f, x, y, &part, 1);
27231 /* If displaying active text in another window, clear that. */
27232 if (! EQ (window, hlinfo->mouse_face_window)
27233 /* Also clear if we move out of text area in same window. */
27234 || (!NILP (hlinfo->mouse_face_window)
27235 && !NILP (window)
27236 && part != ON_TEXT
27237 && part != ON_MODE_LINE
27238 && part != ON_HEADER_LINE))
27239 clear_mouse_face (hlinfo);
27241 /* Not on a window -> return. */
27242 if (!WINDOWP (window))
27243 return;
27245 /* Reset help_echo_string. It will get recomputed below. */
27246 help_echo_string = Qnil;
27248 /* Convert to window-relative pixel coordinates. */
27249 w = XWINDOW (window);
27250 frame_to_window_pixel_xy (w, &x, &y);
27252 #ifdef HAVE_WINDOW_SYSTEM
27253 /* Handle tool-bar window differently since it doesn't display a
27254 buffer. */
27255 if (EQ (window, f->tool_bar_window))
27257 note_tool_bar_highlight (f, x, y);
27258 return;
27260 #endif
27262 /* Mouse is on the mode, header line or margin? */
27263 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27264 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27266 note_mode_line_or_margin_highlight (window, x, y, part);
27267 return;
27270 #ifdef HAVE_WINDOW_SYSTEM
27271 if (part == ON_VERTICAL_BORDER)
27273 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27274 help_echo_string = build_string ("drag-mouse-1: resize");
27276 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27277 || part == ON_SCROLL_BAR)
27278 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27279 else
27280 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27281 #endif
27283 /* Are we in a window whose display is up to date?
27284 And verify the buffer's text has not changed. */
27285 b = XBUFFER (w->buffer);
27286 if (part == ON_TEXT
27287 && EQ (w->window_end_valid, w->buffer)
27288 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
27289 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
27291 int hpos, vpos, dx, dy, area = LAST_AREA;
27292 EMACS_INT pos;
27293 struct glyph *glyph;
27294 Lisp_Object object;
27295 Lisp_Object mouse_face = Qnil, position;
27296 Lisp_Object *overlay_vec = NULL;
27297 ptrdiff_t i, noverlays;
27298 struct buffer *obuf;
27299 EMACS_INT obegv, ozv;
27300 int same_region;
27302 /* Find the glyph under X/Y. */
27303 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27305 #ifdef HAVE_WINDOW_SYSTEM
27306 /* Look for :pointer property on image. */
27307 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27309 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27310 if (img != NULL && IMAGEP (img->spec))
27312 Lisp_Object image_map, hotspot;
27313 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27314 !NILP (image_map))
27315 && (hotspot = find_hot_spot (image_map,
27316 glyph->slice.img.x + dx,
27317 glyph->slice.img.y + dy),
27318 CONSP (hotspot))
27319 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27321 Lisp_Object plist;
27323 /* Could check XCAR (hotspot) to see if we enter/leave
27324 this hot-spot.
27325 If so, we could look for mouse-enter, mouse-leave
27326 properties in PLIST (and do something...). */
27327 hotspot = XCDR (hotspot);
27328 if (CONSP (hotspot)
27329 && (plist = XCAR (hotspot), CONSP (plist)))
27331 pointer = Fplist_get (plist, Qpointer);
27332 if (NILP (pointer))
27333 pointer = Qhand;
27334 help_echo_string = Fplist_get (plist, Qhelp_echo);
27335 if (!NILP (help_echo_string))
27337 help_echo_window = window;
27338 help_echo_object = glyph->object;
27339 help_echo_pos = glyph->charpos;
27343 if (NILP (pointer))
27344 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27347 #endif /* HAVE_WINDOW_SYSTEM */
27349 /* Clear mouse face if X/Y not over text. */
27350 if (glyph == NULL
27351 || area != TEXT_AREA
27352 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27353 /* Glyph's OBJECT is an integer for glyphs inserted by the
27354 display engine for its internal purposes, like truncation
27355 and continuation glyphs and blanks beyond the end of
27356 line's text on text terminals. If we are over such a
27357 glyph, we are not over any text. */
27358 || INTEGERP (glyph->object)
27359 /* R2L rows have a stretch glyph at their front, which
27360 stands for no text, whereas L2R rows have no glyphs at
27361 all beyond the end of text. Treat such stretch glyphs
27362 like we do with NULL glyphs in L2R rows. */
27363 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27364 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27365 && glyph->type == STRETCH_GLYPH
27366 && glyph->avoid_cursor_p))
27368 if (clear_mouse_face (hlinfo))
27369 cursor = No_Cursor;
27370 #ifdef HAVE_WINDOW_SYSTEM
27371 if (FRAME_WINDOW_P (f) && NILP (pointer))
27373 if (area != TEXT_AREA)
27374 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27375 else
27376 pointer = Vvoid_text_area_pointer;
27378 #endif
27379 goto set_cursor;
27382 pos = glyph->charpos;
27383 object = glyph->object;
27384 if (!STRINGP (object) && !BUFFERP (object))
27385 goto set_cursor;
27387 /* If we get an out-of-range value, return now; avoid an error. */
27388 if (BUFFERP (object) && pos > BUF_Z (b))
27389 goto set_cursor;
27391 /* Make the window's buffer temporarily current for
27392 overlays_at and compute_char_face. */
27393 obuf = current_buffer;
27394 current_buffer = b;
27395 obegv = BEGV;
27396 ozv = ZV;
27397 BEGV = BEG;
27398 ZV = Z;
27400 /* Is this char mouse-active or does it have help-echo? */
27401 position = make_number (pos);
27403 if (BUFFERP (object))
27405 /* Put all the overlays we want in a vector in overlay_vec. */
27406 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27407 /* Sort overlays into increasing priority order. */
27408 noverlays = sort_overlays (overlay_vec, noverlays, w);
27410 else
27411 noverlays = 0;
27413 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27415 if (same_region)
27416 cursor = No_Cursor;
27418 /* Check mouse-face highlighting. */
27419 if (! same_region
27420 /* If there exists an overlay with mouse-face overlapping
27421 the one we are currently highlighting, we have to
27422 check if we enter the overlapping overlay, and then
27423 highlight only that. */
27424 || (OVERLAYP (hlinfo->mouse_face_overlay)
27425 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27427 /* Find the highest priority overlay with a mouse-face. */
27428 Lisp_Object overlay = Qnil;
27429 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27431 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27432 if (!NILP (mouse_face))
27433 overlay = overlay_vec[i];
27436 /* If we're highlighting the same overlay as before, there's
27437 no need to do that again. */
27438 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27439 goto check_help_echo;
27440 hlinfo->mouse_face_overlay = overlay;
27442 /* Clear the display of the old active region, if any. */
27443 if (clear_mouse_face (hlinfo))
27444 cursor = No_Cursor;
27446 /* If no overlay applies, get a text property. */
27447 if (NILP (overlay))
27448 mouse_face = Fget_text_property (position, Qmouse_face, object);
27450 /* Next, compute the bounds of the mouse highlighting and
27451 display it. */
27452 if (!NILP (mouse_face) && STRINGP (object))
27454 /* The mouse-highlighting comes from a display string
27455 with a mouse-face. */
27456 Lisp_Object s, e;
27457 EMACS_INT ignore;
27459 s = Fprevious_single_property_change
27460 (make_number (pos + 1), Qmouse_face, object, Qnil);
27461 e = Fnext_single_property_change
27462 (position, Qmouse_face, object, Qnil);
27463 if (NILP (s))
27464 s = make_number (0);
27465 if (NILP (e))
27466 e = make_number (SCHARS (object) - 1);
27467 mouse_face_from_string_pos (w, hlinfo, object,
27468 XINT (s), XINT (e));
27469 hlinfo->mouse_face_past_end = 0;
27470 hlinfo->mouse_face_window = window;
27471 hlinfo->mouse_face_face_id
27472 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27473 glyph->face_id, 1);
27474 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27475 cursor = No_Cursor;
27477 else
27479 /* The mouse-highlighting, if any, comes from an overlay
27480 or text property in the buffer. */
27481 Lisp_Object buffer IF_LINT (= Qnil);
27482 Lisp_Object disp_string IF_LINT (= Qnil);
27484 if (STRINGP (object))
27486 /* If we are on a display string with no mouse-face,
27487 check if the text under it has one. */
27488 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27489 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27490 pos = string_buffer_position (object, start);
27491 if (pos > 0)
27493 mouse_face = get_char_property_and_overlay
27494 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27495 buffer = w->buffer;
27496 disp_string = object;
27499 else
27501 buffer = object;
27502 disp_string = Qnil;
27505 if (!NILP (mouse_face))
27507 Lisp_Object before, after;
27508 Lisp_Object before_string, after_string;
27509 /* To correctly find the limits of mouse highlight
27510 in a bidi-reordered buffer, we must not use the
27511 optimization of limiting the search in
27512 previous-single-property-change and
27513 next-single-property-change, because
27514 rows_from_pos_range needs the real start and end
27515 positions to DTRT in this case. That's because
27516 the first row visible in a window does not
27517 necessarily display the character whose position
27518 is the smallest. */
27519 Lisp_Object lim1 =
27520 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27521 ? Fmarker_position (w->start)
27522 : Qnil;
27523 Lisp_Object lim2 =
27524 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27525 ? make_number (BUF_Z (XBUFFER (buffer))
27526 - XFASTINT (w->window_end_pos))
27527 : Qnil;
27529 if (NILP (overlay))
27531 /* Handle the text property case. */
27532 before = Fprevious_single_property_change
27533 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27534 after = Fnext_single_property_change
27535 (make_number (pos), Qmouse_face, buffer, lim2);
27536 before_string = after_string = Qnil;
27538 else
27540 /* Handle the overlay case. */
27541 before = Foverlay_start (overlay);
27542 after = Foverlay_end (overlay);
27543 before_string = Foverlay_get (overlay, Qbefore_string);
27544 after_string = Foverlay_get (overlay, Qafter_string);
27546 if (!STRINGP (before_string)) before_string = Qnil;
27547 if (!STRINGP (after_string)) after_string = Qnil;
27550 mouse_face_from_buffer_pos (window, hlinfo, pos,
27551 NILP (before)
27553 : XFASTINT (before),
27554 NILP (after)
27555 ? BUF_Z (XBUFFER (buffer))
27556 : XFASTINT (after),
27557 before_string, after_string,
27558 disp_string);
27559 cursor = No_Cursor;
27564 check_help_echo:
27566 /* Look for a `help-echo' property. */
27567 if (NILP (help_echo_string)) {
27568 Lisp_Object help, overlay;
27570 /* Check overlays first. */
27571 help = overlay = Qnil;
27572 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27574 overlay = overlay_vec[i];
27575 help = Foverlay_get (overlay, Qhelp_echo);
27578 if (!NILP (help))
27580 help_echo_string = help;
27581 help_echo_window = window;
27582 help_echo_object = overlay;
27583 help_echo_pos = pos;
27585 else
27587 Lisp_Object obj = glyph->object;
27588 EMACS_INT charpos = glyph->charpos;
27590 /* Try text properties. */
27591 if (STRINGP (obj)
27592 && charpos >= 0
27593 && charpos < SCHARS (obj))
27595 help = Fget_text_property (make_number (charpos),
27596 Qhelp_echo, obj);
27597 if (NILP (help))
27599 /* If the string itself doesn't specify a help-echo,
27600 see if the buffer text ``under'' it does. */
27601 struct glyph_row *r
27602 = MATRIX_ROW (w->current_matrix, vpos);
27603 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27604 EMACS_INT p = string_buffer_position (obj, start);
27605 if (p > 0)
27607 help = Fget_char_property (make_number (p),
27608 Qhelp_echo, w->buffer);
27609 if (!NILP (help))
27611 charpos = p;
27612 obj = w->buffer;
27617 else if (BUFFERP (obj)
27618 && charpos >= BEGV
27619 && charpos < ZV)
27620 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27621 obj);
27623 if (!NILP (help))
27625 help_echo_string = help;
27626 help_echo_window = window;
27627 help_echo_object = obj;
27628 help_echo_pos = charpos;
27633 #ifdef HAVE_WINDOW_SYSTEM
27634 /* Look for a `pointer' property. */
27635 if (FRAME_WINDOW_P (f) && NILP (pointer))
27637 /* Check overlays first. */
27638 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27639 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27641 if (NILP (pointer))
27643 Lisp_Object obj = glyph->object;
27644 EMACS_INT charpos = glyph->charpos;
27646 /* Try text properties. */
27647 if (STRINGP (obj)
27648 && charpos >= 0
27649 && charpos < SCHARS (obj))
27651 pointer = Fget_text_property (make_number (charpos),
27652 Qpointer, obj);
27653 if (NILP (pointer))
27655 /* If the string itself doesn't specify a pointer,
27656 see if the buffer text ``under'' it does. */
27657 struct glyph_row *r
27658 = MATRIX_ROW (w->current_matrix, vpos);
27659 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27660 EMACS_INT p = string_buffer_position (obj, start);
27661 if (p > 0)
27662 pointer = Fget_char_property (make_number (p),
27663 Qpointer, w->buffer);
27666 else if (BUFFERP (obj)
27667 && charpos >= BEGV
27668 && charpos < ZV)
27669 pointer = Fget_text_property (make_number (charpos),
27670 Qpointer, obj);
27673 #endif /* HAVE_WINDOW_SYSTEM */
27675 BEGV = obegv;
27676 ZV = ozv;
27677 current_buffer = obuf;
27680 set_cursor:
27682 #ifdef HAVE_WINDOW_SYSTEM
27683 if (FRAME_WINDOW_P (f))
27684 define_frame_cursor1 (f, cursor, pointer);
27685 #else
27686 /* This is here to prevent a compiler error, about "label at end of
27687 compound statement". */
27688 return;
27689 #endif
27693 /* EXPORT for RIF:
27694 Clear any mouse-face on window W. This function is part of the
27695 redisplay interface, and is called from try_window_id and similar
27696 functions to ensure the mouse-highlight is off. */
27698 void
27699 x_clear_window_mouse_face (struct window *w)
27701 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27702 Lisp_Object window;
27704 BLOCK_INPUT;
27705 XSETWINDOW (window, w);
27706 if (EQ (window, hlinfo->mouse_face_window))
27707 clear_mouse_face (hlinfo);
27708 UNBLOCK_INPUT;
27712 /* EXPORT:
27713 Just discard the mouse face information for frame F, if any.
27714 This is used when the size of F is changed. */
27716 void
27717 cancel_mouse_face (struct frame *f)
27719 Lisp_Object window;
27720 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27722 window = hlinfo->mouse_face_window;
27723 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
27725 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27726 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27727 hlinfo->mouse_face_window = Qnil;
27733 /***********************************************************************
27734 Exposure Events
27735 ***********************************************************************/
27737 #ifdef HAVE_WINDOW_SYSTEM
27739 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
27740 which intersects rectangle R. R is in window-relative coordinates. */
27742 static void
27743 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
27744 enum glyph_row_area area)
27746 struct glyph *first = row->glyphs[area];
27747 struct glyph *end = row->glyphs[area] + row->used[area];
27748 struct glyph *last;
27749 int first_x, start_x, x;
27751 if (area == TEXT_AREA && row->fill_line_p)
27752 /* If row extends face to end of line write the whole line. */
27753 draw_glyphs (w, 0, row, area,
27754 0, row->used[area],
27755 DRAW_NORMAL_TEXT, 0);
27756 else
27758 /* Set START_X to the window-relative start position for drawing glyphs of
27759 AREA. The first glyph of the text area can be partially visible.
27760 The first glyphs of other areas cannot. */
27761 start_x = window_box_left_offset (w, area);
27762 x = start_x;
27763 if (area == TEXT_AREA)
27764 x += row->x;
27766 /* Find the first glyph that must be redrawn. */
27767 while (first < end
27768 && x + first->pixel_width < r->x)
27770 x += first->pixel_width;
27771 ++first;
27774 /* Find the last one. */
27775 last = first;
27776 first_x = x;
27777 while (last < end
27778 && x < r->x + r->width)
27780 x += last->pixel_width;
27781 ++last;
27784 /* Repaint. */
27785 if (last > first)
27786 draw_glyphs (w, first_x - start_x, row, area,
27787 first - row->glyphs[area], last - row->glyphs[area],
27788 DRAW_NORMAL_TEXT, 0);
27793 /* Redraw the parts of the glyph row ROW on window W intersecting
27794 rectangle R. R is in window-relative coordinates. Value is
27795 non-zero if mouse-face was overwritten. */
27797 static int
27798 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
27800 xassert (row->enabled_p);
27802 if (row->mode_line_p || w->pseudo_window_p)
27803 draw_glyphs (w, 0, row, TEXT_AREA,
27804 0, row->used[TEXT_AREA],
27805 DRAW_NORMAL_TEXT, 0);
27806 else
27808 if (row->used[LEFT_MARGIN_AREA])
27809 expose_area (w, row, r, LEFT_MARGIN_AREA);
27810 if (row->used[TEXT_AREA])
27811 expose_area (w, row, r, TEXT_AREA);
27812 if (row->used[RIGHT_MARGIN_AREA])
27813 expose_area (w, row, r, RIGHT_MARGIN_AREA);
27814 draw_row_fringe_bitmaps (w, row);
27817 return row->mouse_face_p;
27821 /* Redraw those parts of glyphs rows during expose event handling that
27822 overlap other rows. Redrawing of an exposed line writes over parts
27823 of lines overlapping that exposed line; this function fixes that.
27825 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
27826 row in W's current matrix that is exposed and overlaps other rows.
27827 LAST_OVERLAPPING_ROW is the last such row. */
27829 static void
27830 expose_overlaps (struct window *w,
27831 struct glyph_row *first_overlapping_row,
27832 struct glyph_row *last_overlapping_row,
27833 XRectangle *r)
27835 struct glyph_row *row;
27837 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
27838 if (row->overlapping_p)
27840 xassert (row->enabled_p && !row->mode_line_p);
27842 row->clip = r;
27843 if (row->used[LEFT_MARGIN_AREA])
27844 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
27846 if (row->used[TEXT_AREA])
27847 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
27849 if (row->used[RIGHT_MARGIN_AREA])
27850 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
27851 row->clip = NULL;
27856 /* Return non-zero if W's cursor intersects rectangle R. */
27858 static int
27859 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
27861 XRectangle cr, result;
27862 struct glyph *cursor_glyph;
27863 struct glyph_row *row;
27865 if (w->phys_cursor.vpos >= 0
27866 && w->phys_cursor.vpos < w->current_matrix->nrows
27867 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
27868 row->enabled_p)
27869 && row->cursor_in_fringe_p)
27871 /* Cursor is in the fringe. */
27872 cr.x = window_box_right_offset (w,
27873 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
27874 ? RIGHT_MARGIN_AREA
27875 : TEXT_AREA));
27876 cr.y = row->y;
27877 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
27878 cr.height = row->height;
27879 return x_intersect_rectangles (&cr, r, &result);
27882 cursor_glyph = get_phys_cursor_glyph (w);
27883 if (cursor_glyph)
27885 /* r is relative to W's box, but w->phys_cursor.x is relative
27886 to left edge of W's TEXT area. Adjust it. */
27887 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
27888 cr.y = w->phys_cursor.y;
27889 cr.width = cursor_glyph->pixel_width;
27890 cr.height = w->phys_cursor_height;
27891 /* ++KFS: W32 version used W32-specific IntersectRect here, but
27892 I assume the effect is the same -- and this is portable. */
27893 return x_intersect_rectangles (&cr, r, &result);
27895 /* If we don't understand the format, pretend we're not in the hot-spot. */
27896 return 0;
27900 /* EXPORT:
27901 Draw a vertical window border to the right of window W if W doesn't
27902 have vertical scroll bars. */
27904 void
27905 x_draw_vertical_border (struct window *w)
27907 struct frame *f = XFRAME (WINDOW_FRAME (w));
27909 /* We could do better, if we knew what type of scroll-bar the adjacent
27910 windows (on either side) have... But we don't :-(
27911 However, I think this works ok. ++KFS 2003-04-25 */
27913 /* Redraw borders between horizontally adjacent windows. Don't
27914 do it for frames with vertical scroll bars because either the
27915 right scroll bar of a window, or the left scroll bar of its
27916 neighbor will suffice as a border. */
27917 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
27918 return;
27920 if (!WINDOW_RIGHTMOST_P (w)
27921 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
27923 int x0, x1, y0, y1;
27925 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27926 y1 -= 1;
27928 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27929 x1 -= 1;
27931 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
27933 else if (!WINDOW_LEFTMOST_P (w)
27934 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
27936 int x0, x1, y0, y1;
27938 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27939 y1 -= 1;
27941 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27942 x0 -= 1;
27944 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
27949 /* Redraw the part of window W intersection rectangle FR. Pixel
27950 coordinates in FR are frame-relative. Call this function with
27951 input blocked. Value is non-zero if the exposure overwrites
27952 mouse-face. */
27954 static int
27955 expose_window (struct window *w, XRectangle *fr)
27957 struct frame *f = XFRAME (w->frame);
27958 XRectangle wr, r;
27959 int mouse_face_overwritten_p = 0;
27961 /* If window is not yet fully initialized, do nothing. This can
27962 happen when toolkit scroll bars are used and a window is split.
27963 Reconfiguring the scroll bar will generate an expose for a newly
27964 created window. */
27965 if (w->current_matrix == NULL)
27966 return 0;
27968 /* When we're currently updating the window, display and current
27969 matrix usually don't agree. Arrange for a thorough display
27970 later. */
27971 if (w == updated_window)
27973 SET_FRAME_GARBAGED (f);
27974 return 0;
27977 /* Frame-relative pixel rectangle of W. */
27978 wr.x = WINDOW_LEFT_EDGE_X (w);
27979 wr.y = WINDOW_TOP_EDGE_Y (w);
27980 wr.width = WINDOW_TOTAL_WIDTH (w);
27981 wr.height = WINDOW_TOTAL_HEIGHT (w);
27983 if (x_intersect_rectangles (fr, &wr, &r))
27985 int yb = window_text_bottom_y (w);
27986 struct glyph_row *row;
27987 int cursor_cleared_p, phys_cursor_on_p;
27988 struct glyph_row *first_overlapping_row, *last_overlapping_row;
27990 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
27991 r.x, r.y, r.width, r.height));
27993 /* Convert to window coordinates. */
27994 r.x -= WINDOW_LEFT_EDGE_X (w);
27995 r.y -= WINDOW_TOP_EDGE_Y (w);
27997 /* Turn off the cursor. */
27998 if (!w->pseudo_window_p
27999 && phys_cursor_in_rect_p (w, &r))
28001 x_clear_cursor (w);
28002 cursor_cleared_p = 1;
28004 else
28005 cursor_cleared_p = 0;
28007 /* If the row containing the cursor extends face to end of line,
28008 then expose_area might overwrite the cursor outside the
28009 rectangle and thus notice_overwritten_cursor might clear
28010 w->phys_cursor_on_p. We remember the original value and
28011 check later if it is changed. */
28012 phys_cursor_on_p = w->phys_cursor_on_p;
28014 /* Update lines intersecting rectangle R. */
28015 first_overlapping_row = last_overlapping_row = NULL;
28016 for (row = w->current_matrix->rows;
28017 row->enabled_p;
28018 ++row)
28020 int y0 = row->y;
28021 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28023 if ((y0 >= r.y && y0 < r.y + r.height)
28024 || (y1 > r.y && y1 < r.y + r.height)
28025 || (r.y >= y0 && r.y < y1)
28026 || (r.y + r.height > y0 && r.y + r.height < y1))
28028 /* A header line may be overlapping, but there is no need
28029 to fix overlapping areas for them. KFS 2005-02-12 */
28030 if (row->overlapping_p && !row->mode_line_p)
28032 if (first_overlapping_row == NULL)
28033 first_overlapping_row = row;
28034 last_overlapping_row = row;
28037 row->clip = fr;
28038 if (expose_line (w, row, &r))
28039 mouse_face_overwritten_p = 1;
28040 row->clip = NULL;
28042 else if (row->overlapping_p)
28044 /* We must redraw a row overlapping the exposed area. */
28045 if (y0 < r.y
28046 ? y0 + row->phys_height > r.y
28047 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28049 if (first_overlapping_row == NULL)
28050 first_overlapping_row = row;
28051 last_overlapping_row = row;
28055 if (y1 >= yb)
28056 break;
28059 /* Display the mode line if there is one. */
28060 if (WINDOW_WANTS_MODELINE_P (w)
28061 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28062 row->enabled_p)
28063 && row->y < r.y + r.height)
28065 if (expose_line (w, row, &r))
28066 mouse_face_overwritten_p = 1;
28069 if (!w->pseudo_window_p)
28071 /* Fix the display of overlapping rows. */
28072 if (first_overlapping_row)
28073 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28074 fr);
28076 /* Draw border between windows. */
28077 x_draw_vertical_border (w);
28079 /* Turn the cursor on again. */
28080 if (cursor_cleared_p
28081 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28082 update_window_cursor (w, 1);
28086 return mouse_face_overwritten_p;
28091 /* Redraw (parts) of all windows in the window tree rooted at W that
28092 intersect R. R contains frame pixel coordinates. Value is
28093 non-zero if the exposure overwrites mouse-face. */
28095 static int
28096 expose_window_tree (struct window *w, XRectangle *r)
28098 struct frame *f = XFRAME (w->frame);
28099 int mouse_face_overwritten_p = 0;
28101 while (w && !FRAME_GARBAGED_P (f))
28103 if (!NILP (w->hchild))
28104 mouse_face_overwritten_p
28105 |= expose_window_tree (XWINDOW (w->hchild), r);
28106 else if (!NILP (w->vchild))
28107 mouse_face_overwritten_p
28108 |= expose_window_tree (XWINDOW (w->vchild), r);
28109 else
28110 mouse_face_overwritten_p |= expose_window (w, r);
28112 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28115 return mouse_face_overwritten_p;
28119 /* EXPORT:
28120 Redisplay an exposed area of frame F. X and Y are the upper-left
28121 corner of the exposed rectangle. W and H are width and height of
28122 the exposed area. All are pixel values. W or H zero means redraw
28123 the entire frame. */
28125 void
28126 expose_frame (struct frame *f, int x, int y, int w, int h)
28128 XRectangle r;
28129 int mouse_face_overwritten_p = 0;
28131 TRACE ((stderr, "expose_frame "));
28133 /* No need to redraw if frame will be redrawn soon. */
28134 if (FRAME_GARBAGED_P (f))
28136 TRACE ((stderr, " garbaged\n"));
28137 return;
28140 /* If basic faces haven't been realized yet, there is no point in
28141 trying to redraw anything. This can happen when we get an expose
28142 event while Emacs is starting, e.g. by moving another window. */
28143 if (FRAME_FACE_CACHE (f) == NULL
28144 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28146 TRACE ((stderr, " no faces\n"));
28147 return;
28150 if (w == 0 || h == 0)
28152 r.x = r.y = 0;
28153 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28154 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28156 else
28158 r.x = x;
28159 r.y = y;
28160 r.width = w;
28161 r.height = h;
28164 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28165 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28167 if (WINDOWP (f->tool_bar_window))
28168 mouse_face_overwritten_p
28169 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28171 #ifdef HAVE_X_WINDOWS
28172 #ifndef MSDOS
28173 #ifndef USE_X_TOOLKIT
28174 if (WINDOWP (f->menu_bar_window))
28175 mouse_face_overwritten_p
28176 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28177 #endif /* not USE_X_TOOLKIT */
28178 #endif
28179 #endif
28181 /* Some window managers support a focus-follows-mouse style with
28182 delayed raising of frames. Imagine a partially obscured frame,
28183 and moving the mouse into partially obscured mouse-face on that
28184 frame. The visible part of the mouse-face will be highlighted,
28185 then the WM raises the obscured frame. With at least one WM, KDE
28186 2.1, Emacs is not getting any event for the raising of the frame
28187 (even tried with SubstructureRedirectMask), only Expose events.
28188 These expose events will draw text normally, i.e. not
28189 highlighted. Which means we must redo the highlight here.
28190 Subsume it under ``we love X''. --gerd 2001-08-15 */
28191 /* Included in Windows version because Windows most likely does not
28192 do the right thing if any third party tool offers
28193 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28194 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28196 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28197 if (f == hlinfo->mouse_face_mouse_frame)
28199 int mouse_x = hlinfo->mouse_face_mouse_x;
28200 int mouse_y = hlinfo->mouse_face_mouse_y;
28201 clear_mouse_face (hlinfo);
28202 note_mouse_highlight (f, mouse_x, mouse_y);
28208 /* EXPORT:
28209 Determine the intersection of two rectangles R1 and R2. Return
28210 the intersection in *RESULT. Value is non-zero if RESULT is not
28211 empty. */
28214 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28216 XRectangle *left, *right;
28217 XRectangle *upper, *lower;
28218 int intersection_p = 0;
28220 /* Rearrange so that R1 is the left-most rectangle. */
28221 if (r1->x < r2->x)
28222 left = r1, right = r2;
28223 else
28224 left = r2, right = r1;
28226 /* X0 of the intersection is right.x0, if this is inside R1,
28227 otherwise there is no intersection. */
28228 if (right->x <= left->x + left->width)
28230 result->x = right->x;
28232 /* The right end of the intersection is the minimum of
28233 the right ends of left and right. */
28234 result->width = (min (left->x + left->width, right->x + right->width)
28235 - result->x);
28237 /* Same game for Y. */
28238 if (r1->y < r2->y)
28239 upper = r1, lower = r2;
28240 else
28241 upper = r2, lower = r1;
28243 /* The upper end of the intersection is lower.y0, if this is inside
28244 of upper. Otherwise, there is no intersection. */
28245 if (lower->y <= upper->y + upper->height)
28247 result->y = lower->y;
28249 /* The lower end of the intersection is the minimum of the lower
28250 ends of upper and lower. */
28251 result->height = (min (lower->y + lower->height,
28252 upper->y + upper->height)
28253 - result->y);
28254 intersection_p = 1;
28258 return intersection_p;
28261 #endif /* HAVE_WINDOW_SYSTEM */
28264 /***********************************************************************
28265 Initialization
28266 ***********************************************************************/
28268 void
28269 syms_of_xdisp (void)
28271 Vwith_echo_area_save_vector = Qnil;
28272 staticpro (&Vwith_echo_area_save_vector);
28274 Vmessage_stack = Qnil;
28275 staticpro (&Vmessage_stack);
28277 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28279 message_dolog_marker1 = Fmake_marker ();
28280 staticpro (&message_dolog_marker1);
28281 message_dolog_marker2 = Fmake_marker ();
28282 staticpro (&message_dolog_marker2);
28283 message_dolog_marker3 = Fmake_marker ();
28284 staticpro (&message_dolog_marker3);
28286 #if GLYPH_DEBUG
28287 defsubr (&Sdump_frame_glyph_matrix);
28288 defsubr (&Sdump_glyph_matrix);
28289 defsubr (&Sdump_glyph_row);
28290 defsubr (&Sdump_tool_bar_row);
28291 defsubr (&Strace_redisplay);
28292 defsubr (&Strace_to_stderr);
28293 #endif
28294 #ifdef HAVE_WINDOW_SYSTEM
28295 defsubr (&Stool_bar_lines_needed);
28296 defsubr (&Slookup_image_map);
28297 #endif
28298 defsubr (&Sformat_mode_line);
28299 defsubr (&Sinvisible_p);
28300 defsubr (&Scurrent_bidi_paragraph_direction);
28302 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28303 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28304 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28305 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28306 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28307 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28308 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28309 DEFSYM (Qeval, "eval");
28310 DEFSYM (QCdata, ":data");
28311 DEFSYM (Qdisplay, "display");
28312 DEFSYM (Qspace_width, "space-width");
28313 DEFSYM (Qraise, "raise");
28314 DEFSYM (Qslice, "slice");
28315 DEFSYM (Qspace, "space");
28316 DEFSYM (Qmargin, "margin");
28317 DEFSYM (Qpointer, "pointer");
28318 DEFSYM (Qleft_margin, "left-margin");
28319 DEFSYM (Qright_margin, "right-margin");
28320 DEFSYM (Qcenter, "center");
28321 DEFSYM (Qline_height, "line-height");
28322 DEFSYM (QCalign_to, ":align-to");
28323 DEFSYM (QCrelative_width, ":relative-width");
28324 DEFSYM (QCrelative_height, ":relative-height");
28325 DEFSYM (QCeval, ":eval");
28326 DEFSYM (QCpropertize, ":propertize");
28327 DEFSYM (QCfile, ":file");
28328 DEFSYM (Qfontified, "fontified");
28329 DEFSYM (Qfontification_functions, "fontification-functions");
28330 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28331 DEFSYM (Qescape_glyph, "escape-glyph");
28332 DEFSYM (Qnobreak_space, "nobreak-space");
28333 DEFSYM (Qimage, "image");
28334 DEFSYM (Qtext, "text");
28335 DEFSYM (Qboth, "both");
28336 DEFSYM (Qboth_horiz, "both-horiz");
28337 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28338 DEFSYM (QCmap, ":map");
28339 DEFSYM (QCpointer, ":pointer");
28340 DEFSYM (Qrect, "rect");
28341 DEFSYM (Qcircle, "circle");
28342 DEFSYM (Qpoly, "poly");
28343 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28344 DEFSYM (Qgrow_only, "grow-only");
28345 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28346 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28347 DEFSYM (Qposition, "position");
28348 DEFSYM (Qbuffer_position, "buffer-position");
28349 DEFSYM (Qobject, "object");
28350 DEFSYM (Qbar, "bar");
28351 DEFSYM (Qhbar, "hbar");
28352 DEFSYM (Qbox, "box");
28353 DEFSYM (Qhollow, "hollow");
28354 DEFSYM (Qhand, "hand");
28355 DEFSYM (Qarrow, "arrow");
28356 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28358 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28359 Fcons (intern_c_string ("void-variable"), Qnil)),
28360 Qnil);
28361 staticpro (&list_of_error);
28363 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28364 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28365 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28366 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28368 echo_buffer[0] = echo_buffer[1] = Qnil;
28369 staticpro (&echo_buffer[0]);
28370 staticpro (&echo_buffer[1]);
28372 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28373 staticpro (&echo_area_buffer[0]);
28374 staticpro (&echo_area_buffer[1]);
28376 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
28377 staticpro (&Vmessages_buffer_name);
28379 mode_line_proptrans_alist = Qnil;
28380 staticpro (&mode_line_proptrans_alist);
28381 mode_line_string_list = Qnil;
28382 staticpro (&mode_line_string_list);
28383 mode_line_string_face = Qnil;
28384 staticpro (&mode_line_string_face);
28385 mode_line_string_face_prop = Qnil;
28386 staticpro (&mode_line_string_face_prop);
28387 Vmode_line_unwind_vector = Qnil;
28388 staticpro (&Vmode_line_unwind_vector);
28390 help_echo_string = Qnil;
28391 staticpro (&help_echo_string);
28392 help_echo_object = Qnil;
28393 staticpro (&help_echo_object);
28394 help_echo_window = Qnil;
28395 staticpro (&help_echo_window);
28396 previous_help_echo_string = Qnil;
28397 staticpro (&previous_help_echo_string);
28398 help_echo_pos = -1;
28400 DEFSYM (Qright_to_left, "right-to-left");
28401 DEFSYM (Qleft_to_right, "left-to-right");
28403 #ifdef HAVE_WINDOW_SYSTEM
28404 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28405 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
28406 For example, if a block cursor is over a tab, it will be drawn as
28407 wide as that tab on the display. */);
28408 x_stretch_cursor_p = 0;
28409 #endif
28411 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28412 doc: /* *Non-nil means highlight trailing whitespace.
28413 The face used for trailing whitespace is `trailing-whitespace'. */);
28414 Vshow_trailing_whitespace = Qnil;
28416 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28417 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28418 If the value is t, Emacs highlights non-ASCII chars which have the
28419 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28420 or `escape-glyph' face respectively.
28422 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28423 U+2011 (non-breaking hyphen) are affected.
28425 Any other non-nil value means to display these characters as a escape
28426 glyph followed by an ordinary space or hyphen.
28428 A value of nil means no special handling of these characters. */);
28429 Vnobreak_char_display = Qt;
28431 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28432 doc: /* *The pointer shape to show in void text areas.
28433 A value of nil means to show the text pointer. Other options are `arrow',
28434 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28435 Vvoid_text_area_pointer = Qarrow;
28437 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28438 doc: /* Non-nil means don't actually do any redisplay.
28439 This is used for internal purposes. */);
28440 Vinhibit_redisplay = Qnil;
28442 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28443 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28444 Vglobal_mode_string = Qnil;
28446 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28447 doc: /* Marker for where to display an arrow on top of the buffer text.
28448 This must be the beginning of a line in order to work.
28449 See also `overlay-arrow-string'. */);
28450 Voverlay_arrow_position = Qnil;
28452 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28453 doc: /* String to display as an arrow in non-window frames.
28454 See also `overlay-arrow-position'. */);
28455 Voverlay_arrow_string = make_pure_c_string ("=>");
28457 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28458 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28459 The symbols on this list are examined during redisplay to determine
28460 where to display overlay arrows. */);
28461 Voverlay_arrow_variable_list
28462 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28464 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28465 doc: /* *The number of lines to try scrolling a window by when point moves out.
28466 If that fails to bring point back on frame, point is centered instead.
28467 If this is zero, point is always centered after it moves off frame.
28468 If you want scrolling to always be a line at a time, you should set
28469 `scroll-conservatively' to a large value rather than set this to 1. */);
28471 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28472 doc: /* *Scroll up to this many lines, to bring point back on screen.
28473 If point moves off-screen, redisplay will scroll by up to
28474 `scroll-conservatively' lines in order to bring point just barely
28475 onto the screen again. If that cannot be done, then redisplay
28476 recenters point as usual.
28478 If the value is greater than 100, redisplay will never recenter point,
28479 but will always scroll just enough text to bring point into view, even
28480 if you move far away.
28482 A value of zero means always recenter point if it moves off screen. */);
28483 scroll_conservatively = 0;
28485 DEFVAR_INT ("scroll-margin", scroll_margin,
28486 doc: /* *Number of lines of margin at the top and bottom of a window.
28487 Recenter the window whenever point gets within this many lines
28488 of the top or bottom of the window. */);
28489 scroll_margin = 0;
28491 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28492 doc: /* Pixels per inch value for non-window system displays.
28493 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28494 Vdisplay_pixels_per_inch = make_float (72.0);
28496 #if GLYPH_DEBUG
28497 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28498 #endif
28500 DEFVAR_LISP ("truncate-partial-width-windows",
28501 Vtruncate_partial_width_windows,
28502 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28503 For an integer value, truncate lines in each window narrower than the
28504 full frame width, provided the window width is less than that integer;
28505 otherwise, respect the value of `truncate-lines'.
28507 For any other non-nil value, truncate lines in all windows that do
28508 not span the full frame width.
28510 A value of nil means to respect the value of `truncate-lines'.
28512 If `word-wrap' is enabled, you might want to reduce this. */);
28513 Vtruncate_partial_width_windows = make_number (50);
28515 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28516 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28517 Any other value means to use the appropriate face, `mode-line',
28518 `header-line', or `menu' respectively. */);
28519 mode_line_inverse_video = 1;
28521 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28522 doc: /* *Maximum buffer size for which line number should be displayed.
28523 If the buffer is bigger than this, the line number does not appear
28524 in the mode line. A value of nil means no limit. */);
28525 Vline_number_display_limit = Qnil;
28527 DEFVAR_INT ("line-number-display-limit-width",
28528 line_number_display_limit_width,
28529 doc: /* *Maximum line width (in characters) for line number display.
28530 If the average length of the lines near point is bigger than this, then the
28531 line number may be omitted from the mode line. */);
28532 line_number_display_limit_width = 200;
28534 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28535 doc: /* *Non-nil means highlight region even in nonselected windows. */);
28536 highlight_nonselected_windows = 0;
28538 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28539 doc: /* Non-nil if more than one frame is visible on this display.
28540 Minibuffer-only frames don't count, but iconified frames do.
28541 This variable is not guaranteed to be accurate except while processing
28542 `frame-title-format' and `icon-title-format'. */);
28544 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28545 doc: /* Template for displaying the title bar of visible frames.
28546 \(Assuming the window manager supports this feature.)
28548 This variable has the same structure as `mode-line-format', except that
28549 the %c and %l constructs are ignored. It is used only on frames for
28550 which no explicit name has been set \(see `modify-frame-parameters'). */);
28552 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28553 doc: /* Template for displaying the title bar of an iconified frame.
28554 \(Assuming the window manager supports this feature.)
28555 This variable has the same structure as `mode-line-format' (which see),
28556 and is used only on frames for which no explicit name has been set
28557 \(see `modify-frame-parameters'). */);
28558 Vicon_title_format
28559 = Vframe_title_format
28560 = pure_cons (intern_c_string ("multiple-frames"),
28561 pure_cons (make_pure_c_string ("%b"),
28562 pure_cons (pure_cons (empty_unibyte_string,
28563 pure_cons (intern_c_string ("invocation-name"),
28564 pure_cons (make_pure_c_string ("@"),
28565 pure_cons (intern_c_string ("system-name"),
28566 Qnil)))),
28567 Qnil)));
28569 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28570 doc: /* Maximum number of lines to keep in the message log buffer.
28571 If nil, disable message logging. If t, log messages but don't truncate
28572 the buffer when it becomes large. */);
28573 Vmessage_log_max = make_number (100);
28575 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28576 doc: /* Functions called before redisplay, if window sizes have changed.
28577 The value should be a list of functions that take one argument.
28578 Just before redisplay, for each frame, if any of its windows have changed
28579 size since the last redisplay, or have been split or deleted,
28580 all the functions in the list are called, with the frame as argument. */);
28581 Vwindow_size_change_functions = Qnil;
28583 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28584 doc: /* List of functions to call before redisplaying a window with scrolling.
28585 Each function is called with two arguments, the window and its new
28586 display-start position. Note that these functions are also called by
28587 `set-window-buffer'. Also note that the value of `window-end' is not
28588 valid when these functions are called.
28590 Warning: Do not use this feature to alter the way the window
28591 is scrolled. It is not designed for that, and such use probably won't
28592 work. */);
28593 Vwindow_scroll_functions = Qnil;
28595 DEFVAR_LISP ("window-text-change-functions",
28596 Vwindow_text_change_functions,
28597 doc: /* Functions to call in redisplay when text in the window might change. */);
28598 Vwindow_text_change_functions = Qnil;
28600 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28601 doc: /* Functions called when redisplay of a window reaches the end trigger.
28602 Each function is called with two arguments, the window and the end trigger value.
28603 See `set-window-redisplay-end-trigger'. */);
28604 Vredisplay_end_trigger_functions = Qnil;
28606 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28607 doc: /* *Non-nil means autoselect window with mouse pointer.
28608 If nil, do not autoselect windows.
28609 A positive number means delay autoselection by that many seconds: a
28610 window is autoselected only after the mouse has remained in that
28611 window for the duration of the delay.
28612 A negative number has a similar effect, but causes windows to be
28613 autoselected only after the mouse has stopped moving. \(Because of
28614 the way Emacs compares mouse events, you will occasionally wait twice
28615 that time before the window gets selected.\)
28616 Any other value means to autoselect window instantaneously when the
28617 mouse pointer enters it.
28619 Autoselection selects the minibuffer only if it is active, and never
28620 unselects the minibuffer if it is active.
28622 When customizing this variable make sure that the actual value of
28623 `focus-follows-mouse' matches the behavior of your window manager. */);
28624 Vmouse_autoselect_window = Qnil;
28626 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28627 doc: /* *Non-nil means automatically resize tool-bars.
28628 This dynamically changes the tool-bar's height to the minimum height
28629 that is needed to make all tool-bar items visible.
28630 If value is `grow-only', the tool-bar's height is only increased
28631 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28632 Vauto_resize_tool_bars = Qt;
28634 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28635 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28636 auto_raise_tool_bar_buttons_p = 1;
28638 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28639 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28640 make_cursor_line_fully_visible_p = 1;
28642 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
28643 doc: /* *Border below tool-bar in pixels.
28644 If an integer, use it as the height of the border.
28645 If it is one of `internal-border-width' or `border-width', use the
28646 value of the corresponding frame parameter.
28647 Otherwise, no border is added below the tool-bar. */);
28648 Vtool_bar_border = Qinternal_border_width;
28650 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
28651 doc: /* *Margin around tool-bar buttons in pixels.
28652 If an integer, use that for both horizontal and vertical margins.
28653 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
28654 HORZ specifying the horizontal margin, and VERT specifying the
28655 vertical margin. */);
28656 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
28658 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
28659 doc: /* *Relief thickness of tool-bar buttons. */);
28660 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
28662 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
28663 doc: /* Tool bar style to use.
28664 It can be one of
28665 image - show images only
28666 text - show text only
28667 both - show both, text below image
28668 both-horiz - show text to the right of the image
28669 text-image-horiz - show text to the left of the image
28670 any other - use system default or image if no system default. */);
28671 Vtool_bar_style = Qnil;
28673 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
28674 doc: /* *Maximum number of characters a label can have to be shown.
28675 The tool bar style must also show labels for this to have any effect, see
28676 `tool-bar-style'. */);
28677 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
28679 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
28680 doc: /* List of functions to call to fontify regions of text.
28681 Each function is called with one argument POS. Functions must
28682 fontify a region starting at POS in the current buffer, and give
28683 fontified regions the property `fontified'. */);
28684 Vfontification_functions = Qnil;
28685 Fmake_variable_buffer_local (Qfontification_functions);
28687 DEFVAR_BOOL ("unibyte-display-via-language-environment",
28688 unibyte_display_via_language_environment,
28689 doc: /* *Non-nil means display unibyte text according to language environment.
28690 Specifically, this means that raw bytes in the range 160-255 decimal
28691 are displayed by converting them to the equivalent multibyte characters
28692 according to the current language environment. As a result, they are
28693 displayed according to the current fontset.
28695 Note that this variable affects only how these bytes are displayed,
28696 but does not change the fact they are interpreted as raw bytes. */);
28697 unibyte_display_via_language_environment = 0;
28699 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
28700 doc: /* *Maximum height for resizing mini-windows (the minibuffer and the echo area).
28701 If a float, it specifies a fraction of the mini-window frame's height.
28702 If an integer, it specifies a number of lines. */);
28703 Vmax_mini_window_height = make_float (0.25);
28705 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
28706 doc: /* How to resize mini-windows (the minibuffer and the echo area).
28707 A value of nil means don't automatically resize mini-windows.
28708 A value of t means resize them to fit the text displayed in them.
28709 A value of `grow-only', the default, means let mini-windows grow only;
28710 they return to their normal size when the minibuffer is closed, or the
28711 echo area becomes empty. */);
28712 Vresize_mini_windows = Qgrow_only;
28714 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
28715 doc: /* Alist specifying how to blink the cursor off.
28716 Each element has the form (ON-STATE . OFF-STATE). Whenever the
28717 `cursor-type' frame-parameter or variable equals ON-STATE,
28718 comparing using `equal', Emacs uses OFF-STATE to specify
28719 how to blink it off. ON-STATE and OFF-STATE are values for
28720 the `cursor-type' frame parameter.
28722 If a frame's ON-STATE has no entry in this list,
28723 the frame's other specifications determine how to blink the cursor off. */);
28724 Vblink_cursor_alist = Qnil;
28726 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
28727 doc: /* Allow or disallow automatic horizontal scrolling of windows.
28728 If non-nil, windows are automatically scrolled horizontally to make
28729 point visible. */);
28730 automatic_hscrolling_p = 1;
28731 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
28733 DEFVAR_INT ("hscroll-margin", hscroll_margin,
28734 doc: /* *How many columns away from the window edge point is allowed to get
28735 before automatic hscrolling will horizontally scroll the window. */);
28736 hscroll_margin = 5;
28738 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
28739 doc: /* *How many columns to scroll the window when point gets too close to the edge.
28740 When point is less than `hscroll-margin' columns from the window
28741 edge, automatic hscrolling will scroll the window by the amount of columns
28742 determined by this variable. If its value is a positive integer, scroll that
28743 many columns. If it's a positive floating-point number, it specifies the
28744 fraction of the window's width to scroll. If it's nil or zero, point will be
28745 centered horizontally after the scroll. Any other value, including negative
28746 numbers, are treated as if the value were zero.
28748 Automatic hscrolling always moves point outside the scroll margin, so if
28749 point was more than scroll step columns inside the margin, the window will
28750 scroll more than the value given by the scroll step.
28752 Note that the lower bound for automatic hscrolling specified by `scroll-left'
28753 and `scroll-right' overrides this variable's effect. */);
28754 Vhscroll_step = make_number (0);
28756 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
28757 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
28758 Bind this around calls to `message' to let it take effect. */);
28759 message_truncate_lines = 0;
28761 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
28762 doc: /* Normal hook run to update the menu bar definitions.
28763 Redisplay runs this hook before it redisplays the menu bar.
28764 This is used to update submenus such as Buffers,
28765 whose contents depend on various data. */);
28766 Vmenu_bar_update_hook = Qnil;
28768 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
28769 doc: /* Frame for which we are updating a menu.
28770 The enable predicate for a menu binding should check this variable. */);
28771 Vmenu_updating_frame = Qnil;
28773 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
28774 doc: /* Non-nil means don't update menu bars. Internal use only. */);
28775 inhibit_menubar_update = 0;
28777 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
28778 doc: /* Prefix prepended to all continuation lines at display time.
28779 The value may be a string, an image, or a stretch-glyph; it is
28780 interpreted in the same way as the value of a `display' text property.
28782 This variable is overridden by any `wrap-prefix' text or overlay
28783 property.
28785 To add a prefix to non-continuation lines, use `line-prefix'. */);
28786 Vwrap_prefix = Qnil;
28787 DEFSYM (Qwrap_prefix, "wrap-prefix");
28788 Fmake_variable_buffer_local (Qwrap_prefix);
28790 DEFVAR_LISP ("line-prefix", Vline_prefix,
28791 doc: /* Prefix prepended to all non-continuation lines at display time.
28792 The value may be a string, an image, or a stretch-glyph; it is
28793 interpreted in the same way as the value of a `display' text property.
28795 This variable is overridden by any `line-prefix' text or overlay
28796 property.
28798 To add a prefix to continuation lines, use `wrap-prefix'. */);
28799 Vline_prefix = Qnil;
28800 DEFSYM (Qline_prefix, "line-prefix");
28801 Fmake_variable_buffer_local (Qline_prefix);
28803 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
28804 doc: /* Non-nil means don't eval Lisp during redisplay. */);
28805 inhibit_eval_during_redisplay = 0;
28807 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
28808 doc: /* Non-nil means don't free realized faces. Internal use only. */);
28809 inhibit_free_realized_faces = 0;
28811 #if GLYPH_DEBUG
28812 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
28813 doc: /* Inhibit try_window_id display optimization. */);
28814 inhibit_try_window_id = 0;
28816 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
28817 doc: /* Inhibit try_window_reusing display optimization. */);
28818 inhibit_try_window_reusing = 0;
28820 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
28821 doc: /* Inhibit try_cursor_movement display optimization. */);
28822 inhibit_try_cursor_movement = 0;
28823 #endif /* GLYPH_DEBUG */
28825 DEFVAR_INT ("overline-margin", overline_margin,
28826 doc: /* *Space between overline and text, in pixels.
28827 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
28828 margin to the character height. */);
28829 overline_margin = 2;
28831 DEFVAR_INT ("underline-minimum-offset",
28832 underline_minimum_offset,
28833 doc: /* Minimum distance between baseline and underline.
28834 This can improve legibility of underlined text at small font sizes,
28835 particularly when using variable `x-use-underline-position-properties'
28836 with fonts that specify an UNDERLINE_POSITION relatively close to the
28837 baseline. The default value is 1. */);
28838 underline_minimum_offset = 1;
28840 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
28841 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
28842 This feature only works when on a window system that can change
28843 cursor shapes. */);
28844 display_hourglass_p = 1;
28846 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
28847 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
28848 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
28850 hourglass_atimer = NULL;
28851 hourglass_shown_p = 0;
28853 DEFSYM (Qglyphless_char, "glyphless-char");
28854 DEFSYM (Qhex_code, "hex-code");
28855 DEFSYM (Qempty_box, "empty-box");
28856 DEFSYM (Qthin_space, "thin-space");
28857 DEFSYM (Qzero_width, "zero-width");
28859 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
28860 /* Intern this now in case it isn't already done.
28861 Setting this variable twice is harmless.
28862 But don't staticpro it here--that is done in alloc.c. */
28863 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
28864 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
28866 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
28867 doc: /* Char-table defining glyphless characters.
28868 Each element, if non-nil, should be one of the following:
28869 an ASCII acronym string: display this string in a box
28870 `hex-code': display the hexadecimal code of a character in a box
28871 `empty-box': display as an empty box
28872 `thin-space': display as 1-pixel width space
28873 `zero-width': don't display
28874 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
28875 display method for graphical terminals and text terminals respectively.
28876 GRAPHICAL and TEXT should each have one of the values listed above.
28878 The char-table has one extra slot to control the display of a character for
28879 which no font is found. This slot only takes effect on graphical terminals.
28880 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
28881 `thin-space'. The default is `empty-box'. */);
28882 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
28883 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
28884 Qempty_box);
28888 /* Initialize this module when Emacs starts. */
28890 void
28891 init_xdisp (void)
28893 current_header_line_height = current_mode_line_height = -1;
28895 CHARPOS (this_line_start_pos) = 0;
28897 if (!noninteractive)
28899 struct window *m = XWINDOW (minibuf_window);
28900 Lisp_Object frame = m->frame;
28901 struct frame *f = XFRAME (frame);
28902 Lisp_Object root = FRAME_ROOT_WINDOW (f);
28903 struct window *r = XWINDOW (root);
28904 int i;
28906 echo_area_window = minibuf_window;
28908 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
28909 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
28910 XSETFASTINT (r->total_cols, FRAME_COLS (f));
28911 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
28912 XSETFASTINT (m->total_lines, 1);
28913 XSETFASTINT (m->total_cols, FRAME_COLS (f));
28915 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
28916 scratch_glyph_row.glyphs[TEXT_AREA + 1]
28917 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
28919 /* The default ellipsis glyphs `...'. */
28920 for (i = 0; i < 3; ++i)
28921 default_invis_vector[i] = make_number ('.');
28925 /* Allocate the buffer for frame titles.
28926 Also used for `format-mode-line'. */
28927 int size = 100;
28928 mode_line_noprop_buf = (char *) xmalloc (size);
28929 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
28930 mode_line_noprop_ptr = mode_line_noprop_buf;
28931 mode_line_target = MODE_LINE_DISPLAY;
28934 help_echo_showing_p = 0;
28937 /* Since w32 does not support atimers, it defines its own implementation of
28938 the following three functions in w32fns.c. */
28939 #ifndef WINDOWSNT
28941 /* Platform-independent portion of hourglass implementation. */
28943 /* Return non-zero if hourglass timer has been started or hourglass is
28944 shown. */
28946 hourglass_started (void)
28948 return hourglass_shown_p || hourglass_atimer != NULL;
28951 /* Cancel a currently active hourglass timer, and start a new one. */
28952 void
28953 start_hourglass (void)
28955 #if defined (HAVE_WINDOW_SYSTEM)
28956 EMACS_TIME delay;
28957 int secs, usecs = 0;
28959 cancel_hourglass ();
28961 if (INTEGERP (Vhourglass_delay)
28962 && XINT (Vhourglass_delay) > 0)
28963 secs = XFASTINT (Vhourglass_delay);
28964 else if (FLOATP (Vhourglass_delay)
28965 && XFLOAT_DATA (Vhourglass_delay) > 0)
28967 Lisp_Object tem;
28968 tem = Ftruncate (Vhourglass_delay, Qnil);
28969 secs = XFASTINT (tem);
28970 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
28972 else
28973 secs = DEFAULT_HOURGLASS_DELAY;
28975 EMACS_SET_SECS_USECS (delay, secs, usecs);
28976 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
28977 show_hourglass, NULL);
28978 #endif
28982 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
28983 shown. */
28984 void
28985 cancel_hourglass (void)
28987 #if defined (HAVE_WINDOW_SYSTEM)
28988 if (hourglass_atimer)
28990 cancel_atimer (hourglass_atimer);
28991 hourglass_atimer = NULL;
28994 if (hourglass_shown_p)
28995 hide_hourglass ();
28996 #endif
28998 #endif /* ! WINDOWSNT */