Speed up keyboard auto-repeat cursor motion under bidi redisplay.
[emacs.git] / src / xdisp.c
blob4075688ea0a74e071a7456d2e9c2c1e3646bfea9
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2011 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22 Redisplay.
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
53 expose_window (asynchronous) |
55 X expose events -----+
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
84 . try_cursor_movement
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
90 . try_window_reusing_current_matrix
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
96 . try_window_id
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
102 . try_window
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
115 Desired matrices.
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
160 Frame matrices.
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
181 Bidirectional display.
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
224 Bidirectional display and character compositions
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
251 Moving an iterator in bidirectional text
252 without producing glyphs
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
276 #include <setjmp.h>
278 #include "lisp.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "buffer.h"
285 #include "character.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
302 #ifdef HAVE_X_WINDOWS
303 #include "xterm.h"
304 #endif
305 #ifdef WINDOWSNT
306 #include "w32term.h"
307 #endif
308 #ifdef HAVE_NS
309 #include "nsterm.h"
310 #endif
311 #ifdef USE_GTK
312 #include "gtkutil.h"
313 #endif
315 #include "font.h"
317 #ifndef FRAME_X_OUTPUT
318 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
319 #endif
321 #define INFINITY 10000000
323 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
324 Lisp_Object Qwindow_scroll_functions;
325 static Lisp_Object Qwindow_text_change_functions;
326 static Lisp_Object Qredisplay_end_trigger_functions;
327 Lisp_Object Qinhibit_point_motion_hooks;
328 static Lisp_Object QCeval, QCpropertize;
329 Lisp_Object QCfile, QCdata;
330 static Lisp_Object Qfontified;
331 static Lisp_Object Qgrow_only;
332 static Lisp_Object Qinhibit_eval_during_redisplay;
333 static Lisp_Object Qbuffer_position, Qposition, Qobject;
334 static Lisp_Object Qright_to_left, Qleft_to_right;
336 /* Cursor shapes */
337 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
339 /* Pointer shapes */
340 static Lisp_Object Qarrow, Qhand;
341 Lisp_Object Qtext;
343 /* Holds the list (error). */
344 static Lisp_Object list_of_error;
346 static Lisp_Object Qfontification_functions;
348 static Lisp_Object Qwrap_prefix;
349 static Lisp_Object Qline_prefix;
351 /* Non-nil means don't actually do any redisplay. */
353 Lisp_Object Qinhibit_redisplay;
355 /* Names of text properties relevant for redisplay. */
357 Lisp_Object Qdisplay;
359 Lisp_Object Qspace, QCalign_to;
360 static Lisp_Object QCrelative_width, QCrelative_height;
361 Lisp_Object Qleft_margin, Qright_margin;
362 static Lisp_Object Qspace_width, Qraise;
363 static Lisp_Object Qslice;
364 Lisp_Object Qcenter;
365 static Lisp_Object Qmargin, Qpointer;
366 static Lisp_Object Qline_height;
368 #ifdef HAVE_WINDOW_SYSTEM
370 /* Test if overflow newline into fringe. Called with iterator IT
371 at or past right window margin, and with IT->current_x set. */
373 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
374 (!NILP (Voverflow_newline_into_fringe) \
375 && FRAME_WINDOW_P ((IT)->f) \
376 && ((IT)->bidi_it.paragraph_dir == R2L \
377 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
378 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
379 && (IT)->current_x == (IT)->last_visible_x \
380 && (IT)->line_wrap != WORD_WRAP)
382 #else /* !HAVE_WINDOW_SYSTEM */
383 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
384 #endif /* HAVE_WINDOW_SYSTEM */
386 /* Test if the display element loaded in IT is a space or tab
387 character. This is used to determine word wrapping. */
389 #define IT_DISPLAYING_WHITESPACE(it) \
390 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
392 /* Name of the face used to highlight trailing whitespace. */
394 static Lisp_Object Qtrailing_whitespace;
396 /* Name and number of the face used to highlight escape glyphs. */
398 static Lisp_Object Qescape_glyph;
400 /* Name and number of the face used to highlight non-breaking spaces. */
402 static Lisp_Object Qnobreak_space;
404 /* The symbol `image' which is the car of the lists used to represent
405 images in Lisp. Also a tool bar style. */
407 Lisp_Object Qimage;
409 /* The image map types. */
410 Lisp_Object QCmap;
411 static Lisp_Object QCpointer;
412 static Lisp_Object Qrect, Qcircle, Qpoly;
414 /* Tool bar styles */
415 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
417 /* Non-zero means print newline to stdout before next mini-buffer
418 message. */
420 int noninteractive_need_newline;
422 /* Non-zero means print newline to message log before next message. */
424 static int message_log_need_newline;
426 /* Three markers that message_dolog uses.
427 It could allocate them itself, but that causes trouble
428 in handling memory-full errors. */
429 static Lisp_Object message_dolog_marker1;
430 static Lisp_Object message_dolog_marker2;
431 static Lisp_Object message_dolog_marker3;
433 /* The buffer position of the first character appearing entirely or
434 partially on the line of the selected window which contains the
435 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
436 redisplay optimization in redisplay_internal. */
438 static struct text_pos this_line_start_pos;
440 /* Number of characters past the end of the line above, including the
441 terminating newline. */
443 static struct text_pos this_line_end_pos;
445 /* The vertical positions and the height of this line. */
447 static int this_line_vpos;
448 static int this_line_y;
449 static int this_line_pixel_height;
451 /* X position at which this display line starts. Usually zero;
452 negative if first character is partially visible. */
454 static int this_line_start_x;
456 /* The smallest character position seen by move_it_* functions as they
457 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
458 hscrolled lines, see display_line. */
460 static struct text_pos this_line_min_pos;
462 /* Buffer that this_line_.* variables are referring to. */
464 static struct buffer *this_line_buffer;
467 /* Values of those variables at last redisplay are stored as
468 properties on `overlay-arrow-position' symbol. However, if
469 Voverlay_arrow_position is a marker, last-arrow-position is its
470 numerical position. */
472 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
474 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
475 properties on a symbol in overlay-arrow-variable-list. */
477 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
479 Lisp_Object Qmenu_bar_update_hook;
481 /* Nonzero if an overlay arrow has been displayed in this window. */
483 static int overlay_arrow_seen;
485 /* Number of windows showing the buffer of the selected window (or
486 another buffer with the same base buffer). keyboard.c refers to
487 this. */
489 int buffer_shared;
491 /* Vector containing glyphs for an ellipsis `...'. */
493 static Lisp_Object default_invis_vector[3];
495 /* This is the window where the echo area message was displayed. It
496 is always a mini-buffer window, but it may not be the same window
497 currently active as a mini-buffer. */
499 Lisp_Object echo_area_window;
501 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
502 pushes the current message and the value of
503 message_enable_multibyte on the stack, the function restore_message
504 pops the stack and displays MESSAGE again. */
506 static Lisp_Object Vmessage_stack;
508 /* Nonzero means multibyte characters were enabled when the echo area
509 message was specified. */
511 static int message_enable_multibyte;
513 /* Nonzero if we should redraw the mode lines on the next redisplay. */
515 int update_mode_lines;
517 /* Nonzero if window sizes or contents have changed since last
518 redisplay that finished. */
520 int windows_or_buffers_changed;
522 /* Nonzero means a frame's cursor type has been changed. */
524 int cursor_type_changed;
526 /* Nonzero after display_mode_line if %l was used and it displayed a
527 line number. */
529 static int line_number_displayed;
531 /* The name of the *Messages* buffer, a string. */
533 static Lisp_Object Vmessages_buffer_name;
535 /* Current, index 0, and last displayed echo area message. Either
536 buffers from echo_buffers, or nil to indicate no message. */
538 Lisp_Object echo_area_buffer[2];
540 /* The buffers referenced from echo_area_buffer. */
542 static Lisp_Object echo_buffer[2];
544 /* A vector saved used in with_area_buffer to reduce consing. */
546 static Lisp_Object Vwith_echo_area_save_vector;
548 /* Non-zero means display_echo_area should display the last echo area
549 message again. Set by redisplay_preserve_echo_area. */
551 static int display_last_displayed_message_p;
553 /* Nonzero if echo area is being used by print; zero if being used by
554 message. */
556 static int message_buf_print;
558 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
560 static Lisp_Object Qinhibit_menubar_update;
561 static Lisp_Object Qmessage_truncate_lines;
563 /* Set to 1 in clear_message to make redisplay_internal aware
564 of an emptied echo area. */
566 static int message_cleared_p;
568 /* A scratch glyph row with contents used for generating truncation
569 glyphs. Also used in direct_output_for_insert. */
571 #define MAX_SCRATCH_GLYPHS 100
572 static struct glyph_row scratch_glyph_row;
573 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
575 /* Ascent and height of the last line processed by move_it_to. */
577 static int last_max_ascent, last_height;
579 /* Non-zero if there's a help-echo in the echo area. */
581 int help_echo_showing_p;
583 /* If >= 0, computed, exact values of mode-line and header-line height
584 to use in the macros CURRENT_MODE_LINE_HEIGHT and
585 CURRENT_HEADER_LINE_HEIGHT. */
587 int current_mode_line_height, current_header_line_height;
589 /* The maximum distance to look ahead for text properties. Values
590 that are too small let us call compute_char_face and similar
591 functions too often which is expensive. Values that are too large
592 let us call compute_char_face and alike too often because we
593 might not be interested in text properties that far away. */
595 #define TEXT_PROP_DISTANCE_LIMIT 100
597 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
598 iterator state and later restore it. This is needed because the
599 bidi iterator on bidi.c keeps a stacked cache of its states, which
600 is really a singleton. When we use scratch iterator objects to
601 move around the buffer, we can cause the bidi cache to be pushed or
602 popped, and therefore we need to restore the cache state when we
603 return to the original iterator. */
604 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
605 do { \
606 if (CACHE) \
607 xfree (CACHE); \
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); \
617 CACHE = NULL; \
618 } while (0)
620 #if GLYPH_DEBUG
622 /* Non-zero means print traces of redisplay if compiled with
623 GLYPH_DEBUG != 0. */
625 int trace_redisplay_p;
627 #endif /* GLYPH_DEBUG */
629 #ifdef DEBUG_TRACE_MOVE
630 /* Non-zero means trace with TRACE_MOVE to stderr. */
631 int trace_move;
633 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
634 #else
635 #define TRACE_MOVE(x) (void) 0
636 #endif
638 static Lisp_Object Qauto_hscroll_mode;
640 /* Buffer being redisplayed -- for redisplay_window_error. */
642 static struct buffer *displayed_buffer;
644 /* Value returned from text property handlers (see below). */
646 enum prop_handled
648 HANDLED_NORMALLY,
649 HANDLED_RECOMPUTE_PROPS,
650 HANDLED_OVERLAY_STRING_CONSUMED,
651 HANDLED_RETURN
654 /* A description of text properties that redisplay is interested
655 in. */
657 struct props
659 /* The name of the property. */
660 Lisp_Object *name;
662 /* A unique index for the property. */
663 enum prop_idx idx;
665 /* A handler function called to set up iterator IT from the property
666 at IT's current position. Value is used to steer handle_stop. */
667 enum prop_handled (*handler) (struct it *it);
670 static enum prop_handled handle_face_prop (struct it *);
671 static enum prop_handled handle_invisible_prop (struct it *);
672 static enum prop_handled handle_display_prop (struct it *);
673 static enum prop_handled handle_composition_prop (struct it *);
674 static enum prop_handled handle_overlay_change (struct it *);
675 static enum prop_handled handle_fontified_prop (struct it *);
677 /* Properties handled by iterators. */
679 static struct props it_props[] =
681 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
682 /* Handle `face' before `display' because some sub-properties of
683 `display' need to know the face. */
684 {&Qface, FACE_PROP_IDX, handle_face_prop},
685 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
686 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
687 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
688 {NULL, 0, NULL}
691 /* Value is the position described by X. If X is a marker, value is
692 the marker_position of X. Otherwise, value is X. */
694 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
696 /* Enumeration returned by some move_it_.* functions internally. */
698 enum move_it_result
700 /* Not used. Undefined value. */
701 MOVE_UNDEFINED,
703 /* Move ended at the requested buffer position or ZV. */
704 MOVE_POS_MATCH_OR_ZV,
706 /* Move ended at the requested X pixel position. */
707 MOVE_X_REACHED,
709 /* Move within a line ended at the end of a line that must be
710 continued. */
711 MOVE_LINE_CONTINUED,
713 /* Move within a line ended at the end of a line that would
714 be displayed truncated. */
715 MOVE_LINE_TRUNCATED,
717 /* Move within a line ended at a line end. */
718 MOVE_NEWLINE_OR_CR
721 /* This counter is used to clear the face cache every once in a while
722 in redisplay_internal. It is incremented for each redisplay.
723 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
724 cleared. */
726 #define CLEAR_FACE_CACHE_COUNT 500
727 static int clear_face_cache_count;
729 /* Similarly for the image cache. */
731 #ifdef HAVE_WINDOW_SYSTEM
732 #define CLEAR_IMAGE_CACHE_COUNT 101
733 static int clear_image_cache_count;
735 /* Null glyph slice */
736 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
737 #endif
739 /* Non-zero while redisplay_internal is in progress. */
741 int redisplaying_p;
743 static Lisp_Object Qinhibit_free_realized_faces;
745 /* If a string, XTread_socket generates an event to display that string.
746 (The display is done in read_char.) */
748 Lisp_Object help_echo_string;
749 Lisp_Object help_echo_window;
750 Lisp_Object help_echo_object;
751 EMACS_INT help_echo_pos;
753 /* Temporary variable for XTread_socket. */
755 Lisp_Object previous_help_echo_string;
757 /* Platform-independent portion of hourglass implementation. */
759 /* Non-zero means an hourglass cursor is currently shown. */
760 int hourglass_shown_p;
762 /* If non-null, an asynchronous timer that, when it expires, displays
763 an hourglass cursor on all frames. */
764 struct atimer *hourglass_atimer;
766 /* Name of the face used to display glyphless characters. */
767 Lisp_Object Qglyphless_char;
769 /* Symbol for the purpose of Vglyphless_char_display. */
770 static Lisp_Object Qglyphless_char_display;
772 /* Method symbols for Vglyphless_char_display. */
773 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
775 /* Default pixel width of `thin-space' display method. */
776 #define THIN_SPACE_WIDTH 1
778 /* Default number of seconds to wait before displaying an hourglass
779 cursor. */
780 #define DEFAULT_HOURGLASS_DELAY 1
783 /* Function prototypes. */
785 static void setup_for_ellipsis (struct it *, int);
786 static void set_iterator_to_next (struct it *, int);
787 static void mark_window_display_accurate_1 (struct window *, int);
788 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
789 static int display_prop_string_p (Lisp_Object, Lisp_Object);
790 static int cursor_row_p (struct glyph_row *);
791 static int redisplay_mode_lines (Lisp_Object, int);
792 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
794 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
796 static void handle_line_prefix (struct it *);
798 static void pint2str (char *, int, EMACS_INT);
799 static void pint2hrstr (char *, int, EMACS_INT);
800 static struct text_pos run_window_scroll_functions (Lisp_Object,
801 struct text_pos);
802 static void reconsider_clip_changes (struct window *, struct buffer *);
803 static int text_outside_line_unchanged_p (struct window *,
804 EMACS_INT, EMACS_INT);
805 static void store_mode_line_noprop_char (char);
806 static int store_mode_line_noprop (const char *, int, int);
807 static void handle_stop (struct it *);
808 static void handle_stop_backwards (struct it *, EMACS_INT);
809 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
810 static void ensure_echo_area_buffers (void);
811 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
812 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
813 static int with_echo_area_buffer (struct window *, int,
814 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
815 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
816 static void clear_garbaged_frames (void);
817 static int current_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
818 static void pop_message (void);
819 static int truncate_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
820 static void set_message (const char *, Lisp_Object, EMACS_INT, int);
821 static int set_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
822 static int display_echo_area (struct window *);
823 static int display_echo_area_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
824 static int resize_mini_window_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
825 static Lisp_Object unwind_redisplay (Lisp_Object);
826 static int string_char_and_length (const unsigned char *, int *);
827 static struct text_pos display_prop_end (struct it *, Lisp_Object,
828 struct text_pos);
829 static int compute_window_start_on_continuation_line (struct window *);
830 static Lisp_Object safe_eval_handler (Lisp_Object);
831 static void insert_left_trunc_glyphs (struct it *);
832 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
833 Lisp_Object);
834 static void extend_face_to_end_of_line (struct it *);
835 static int append_space_for_newline (struct it *, int);
836 static int cursor_row_fully_visible_p (struct window *, int, int);
837 static int try_scrolling (Lisp_Object, int, EMACS_INT, EMACS_INT, int, int);
838 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
839 static int trailing_whitespace_p (EMACS_INT);
840 static intmax_t message_log_check_duplicate (EMACS_INT, EMACS_INT);
841 static void push_it (struct it *, struct text_pos *);
842 static void pop_it (struct it *);
843 static void sync_frame_with_window_matrix_rows (struct window *);
844 static void select_frame_for_redisplay (Lisp_Object);
845 static void redisplay_internal (void);
846 static int echo_area_display (int);
847 static void redisplay_windows (Lisp_Object);
848 static void redisplay_window (Lisp_Object, int);
849 static Lisp_Object redisplay_window_error (Lisp_Object);
850 static Lisp_Object redisplay_window_0 (Lisp_Object);
851 static Lisp_Object redisplay_window_1 (Lisp_Object);
852 static int set_cursor_from_row (struct window *, struct glyph_row *,
853 struct glyph_matrix *, EMACS_INT, EMACS_INT,
854 int, int);
855 static int update_menu_bar (struct frame *, int, int);
856 static int try_window_reusing_current_matrix (struct window *);
857 static int try_window_id (struct window *);
858 static int display_line (struct it *);
859 static int display_mode_lines (struct window *);
860 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
861 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
862 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
863 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
864 static void display_menu_bar (struct window *);
865 static EMACS_INT display_count_lines (EMACS_INT, EMACS_INT, EMACS_INT,
866 EMACS_INT *);
867 static int display_string (const char *, Lisp_Object, Lisp_Object,
868 EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
869 static void compute_line_metrics (struct it *);
870 static void run_redisplay_end_trigger_hook (struct it *);
871 static int get_overlay_strings (struct it *, EMACS_INT);
872 static int get_overlay_strings_1 (struct it *, EMACS_INT, int);
873 static void next_overlay_string (struct it *);
874 static void reseat (struct it *, struct text_pos, int);
875 static void reseat_1 (struct it *, struct text_pos, int);
876 static void back_to_previous_visible_line_start (struct it *);
877 void reseat_at_previous_visible_line_start (struct it *);
878 static void reseat_at_next_visible_line_start (struct it *, int);
879 static int next_element_from_ellipsis (struct it *);
880 static int next_element_from_display_vector (struct it *);
881 static int next_element_from_string (struct it *);
882 static int next_element_from_c_string (struct it *);
883 static int next_element_from_buffer (struct it *);
884 static int next_element_from_composition (struct it *);
885 static int next_element_from_image (struct it *);
886 static int next_element_from_stretch (struct it *);
887 static void load_overlay_strings (struct it *, EMACS_INT);
888 static int init_from_display_pos (struct it *, struct window *,
889 struct display_pos *);
890 static void reseat_to_string (struct it *, const char *,
891 Lisp_Object, EMACS_INT, EMACS_INT, int, int);
892 static int get_next_display_element (struct it *);
893 static enum move_it_result
894 move_it_in_display_line_to (struct it *, EMACS_INT, int,
895 enum move_operation_enum);
896 void move_it_vertically_backward (struct it *, int);
897 static void init_to_row_start (struct it *, struct window *,
898 struct glyph_row *);
899 static int init_to_row_end (struct it *, struct window *,
900 struct glyph_row *);
901 static void back_to_previous_line_start (struct it *);
902 static int forward_to_next_line_start (struct it *, int *);
903 static struct text_pos string_pos_nchars_ahead (struct text_pos,
904 Lisp_Object, EMACS_INT);
905 static struct text_pos string_pos (EMACS_INT, Lisp_Object);
906 static struct text_pos c_string_pos (EMACS_INT, const char *, int);
907 static EMACS_INT number_of_chars (const char *, int);
908 static void compute_stop_pos (struct it *);
909 static void compute_string_pos (struct text_pos *, struct text_pos,
910 Lisp_Object);
911 static int face_before_or_after_it_pos (struct it *, int);
912 static EMACS_INT next_overlay_change (EMACS_INT);
913 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
914 Lisp_Object, struct text_pos *, EMACS_INT, int);
915 static int handle_single_display_spec (struct it *, Lisp_Object,
916 Lisp_Object, Lisp_Object,
917 struct text_pos *, EMACS_INT, int, int);
918 static int underlying_face_id (struct it *);
919 static int in_ellipses_for_invisible_text_p (struct display_pos *,
920 struct window *);
922 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
923 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
925 #ifdef HAVE_WINDOW_SYSTEM
927 static void x_consider_frame_title (Lisp_Object);
928 static int tool_bar_lines_needed (struct frame *, int *);
929 static void update_tool_bar (struct frame *, int);
930 static void build_desired_tool_bar_string (struct frame *f);
931 static int redisplay_tool_bar (struct frame *);
932 static void display_tool_bar_line (struct it *, int);
933 static void notice_overwritten_cursor (struct window *,
934 enum glyph_row_area,
935 int, int, int, int);
936 static void append_stretch_glyph (struct it *, Lisp_Object,
937 int, int, int);
940 #endif /* HAVE_WINDOW_SYSTEM */
942 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
943 static int coords_in_mouse_face_p (struct window *, int, int);
947 /***********************************************************************
948 Window display dimensions
949 ***********************************************************************/
951 /* Return the bottom boundary y-position for text lines in window W.
952 This is the first y position at which a line cannot start.
953 It is relative to the top of the window.
955 This is the height of W minus the height of a mode line, if any. */
957 inline int
958 window_text_bottom_y (struct window *w)
960 int height = WINDOW_TOTAL_HEIGHT (w);
962 if (WINDOW_WANTS_MODELINE_P (w))
963 height -= CURRENT_MODE_LINE_HEIGHT (w);
964 return height;
967 /* Return the pixel width of display area AREA of window W. AREA < 0
968 means return the total width of W, not including fringes to
969 the left and right of the window. */
971 inline int
972 window_box_width (struct window *w, int area)
974 int cols = XFASTINT (w->total_cols);
975 int pixels = 0;
977 if (!w->pseudo_window_p)
979 cols -= WINDOW_SCROLL_BAR_COLS (w);
981 if (area == TEXT_AREA)
983 if (INTEGERP (w->left_margin_cols))
984 cols -= XFASTINT (w->left_margin_cols);
985 if (INTEGERP (w->right_margin_cols))
986 cols -= XFASTINT (w->right_margin_cols);
987 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
989 else if (area == LEFT_MARGIN_AREA)
991 cols = (INTEGERP (w->left_margin_cols)
992 ? XFASTINT (w->left_margin_cols) : 0);
993 pixels = 0;
995 else if (area == RIGHT_MARGIN_AREA)
997 cols = (INTEGERP (w->right_margin_cols)
998 ? XFASTINT (w->right_margin_cols) : 0);
999 pixels = 0;
1003 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1007 /* Return the pixel height of the display area of window W, not
1008 including mode lines of W, if any. */
1010 inline int
1011 window_box_height (struct window *w)
1013 struct frame *f = XFRAME (w->frame);
1014 int height = WINDOW_TOTAL_HEIGHT (w);
1016 xassert (height >= 0);
1018 /* Note: the code below that determines the mode-line/header-line
1019 height is essentially the same as that contained in the macro
1020 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1021 the appropriate glyph row has its `mode_line_p' flag set,
1022 and if it doesn't, uses estimate_mode_line_height instead. */
1024 if (WINDOW_WANTS_MODELINE_P (w))
1026 struct glyph_row *ml_row
1027 = (w->current_matrix && w->current_matrix->rows
1028 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1029 : 0);
1030 if (ml_row && ml_row->mode_line_p)
1031 height -= ml_row->height;
1032 else
1033 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1036 if (WINDOW_WANTS_HEADER_LINE_P (w))
1038 struct glyph_row *hl_row
1039 = (w->current_matrix && w->current_matrix->rows
1040 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1041 : 0);
1042 if (hl_row && hl_row->mode_line_p)
1043 height -= hl_row->height;
1044 else
1045 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1048 /* With a very small font and a mode-line that's taller than
1049 default, we might end up with a negative height. */
1050 return max (0, height);
1053 /* Return the window-relative coordinate of the left edge of display
1054 area AREA of window W. AREA < 0 means return the left edge of the
1055 whole window, to the right of the left fringe of W. */
1057 inline int
1058 window_box_left_offset (struct window *w, int area)
1060 int x;
1062 if (w->pseudo_window_p)
1063 return 0;
1065 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1067 if (area == TEXT_AREA)
1068 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1069 + window_box_width (w, LEFT_MARGIN_AREA));
1070 else if (area == RIGHT_MARGIN_AREA)
1071 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1072 + window_box_width (w, LEFT_MARGIN_AREA)
1073 + window_box_width (w, TEXT_AREA)
1074 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1076 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1077 else if (area == LEFT_MARGIN_AREA
1078 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1079 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1081 return x;
1085 /* Return the window-relative coordinate of the right edge of display
1086 area AREA of window W. AREA < 0 means return the right edge of the
1087 whole window, to the left of the right fringe of W. */
1089 inline int
1090 window_box_right_offset (struct window *w, int area)
1092 return window_box_left_offset (w, area) + window_box_width (w, area);
1095 /* Return the frame-relative coordinate of the left edge of display
1096 area AREA of window W. AREA < 0 means return the left edge of the
1097 whole window, to the right of the left fringe of W. */
1099 inline int
1100 window_box_left (struct window *w, int area)
1102 struct frame *f = XFRAME (w->frame);
1103 int x;
1105 if (w->pseudo_window_p)
1106 return FRAME_INTERNAL_BORDER_WIDTH (f);
1108 x = (WINDOW_LEFT_EDGE_X (w)
1109 + window_box_left_offset (w, area));
1111 return x;
1115 /* Return the frame-relative coordinate of the right edge of display
1116 area AREA of window W. AREA < 0 means return the right edge of the
1117 whole window, to the left of the right fringe of W. */
1119 inline int
1120 window_box_right (struct window *w, int area)
1122 return window_box_left (w, area) + window_box_width (w, area);
1125 /* Get the bounding box of the display area AREA of window W, without
1126 mode lines, in frame-relative coordinates. AREA < 0 means the
1127 whole window, not including the left and right fringes of
1128 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1129 coordinates of the upper-left corner of the box. Return in
1130 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1132 inline void
1133 window_box (struct window *w, int area, int *box_x, int *box_y,
1134 int *box_width, int *box_height)
1136 if (box_width)
1137 *box_width = window_box_width (w, area);
1138 if (box_height)
1139 *box_height = window_box_height (w);
1140 if (box_x)
1141 *box_x = window_box_left (w, area);
1142 if (box_y)
1144 *box_y = WINDOW_TOP_EDGE_Y (w);
1145 if (WINDOW_WANTS_HEADER_LINE_P (w))
1146 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1151 /* Get the bounding box of the display area AREA of window W, without
1152 mode lines. AREA < 0 means the whole window, not including the
1153 left and right fringe of the window. Return in *TOP_LEFT_X
1154 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1155 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1156 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1157 box. */
1159 static inline void
1160 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1161 int *bottom_right_x, int *bottom_right_y)
1163 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1164 bottom_right_y);
1165 *bottom_right_x += *top_left_x;
1166 *bottom_right_y += *top_left_y;
1171 /***********************************************************************
1172 Utilities
1173 ***********************************************************************/
1175 /* Return the bottom y-position of the line the iterator IT is in.
1176 This can modify IT's settings. */
1179 line_bottom_y (struct it *it)
1181 int line_height = it->max_ascent + it->max_descent;
1182 int line_top_y = it->current_y;
1184 if (line_height == 0)
1186 if (last_height)
1187 line_height = last_height;
1188 else if (IT_CHARPOS (*it) < ZV)
1190 move_it_by_lines (it, 1);
1191 line_height = (it->max_ascent || it->max_descent
1192 ? it->max_ascent + it->max_descent
1193 : last_height);
1195 else
1197 struct glyph_row *row = it->glyph_row;
1199 /* Use the default character height. */
1200 it->glyph_row = NULL;
1201 it->what = IT_CHARACTER;
1202 it->c = ' ';
1203 it->len = 1;
1204 PRODUCE_GLYPHS (it);
1205 line_height = it->ascent + it->descent;
1206 it->glyph_row = row;
1210 return line_top_y + line_height;
1214 /* Return 1 if position CHARPOS is visible in window W.
1215 CHARPOS < 0 means return info about WINDOW_END position.
1216 If visible, set *X and *Y to pixel coordinates of top left corner.
1217 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1218 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1221 pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1222 int *rtop, int *rbot, int *rowh, int *vpos)
1224 struct it it;
1225 void *itdata = bidi_shelve_cache ();
1226 struct text_pos top;
1227 int visible_p = 0;
1228 struct buffer *old_buffer = NULL;
1230 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1231 return visible_p;
1233 if (XBUFFER (w->buffer) != current_buffer)
1235 old_buffer = current_buffer;
1236 set_buffer_internal_1 (XBUFFER (w->buffer));
1239 SET_TEXT_POS_FROM_MARKER (top, w->start);
1241 /* Compute exact mode line heights. */
1242 if (WINDOW_WANTS_MODELINE_P (w))
1243 current_mode_line_height
1244 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1245 BVAR (current_buffer, mode_line_format));
1247 if (WINDOW_WANTS_HEADER_LINE_P (w))
1248 current_header_line_height
1249 = display_mode_line (w, HEADER_LINE_FACE_ID,
1250 BVAR (current_buffer, header_line_format));
1252 start_display (&it, w, top);
1253 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1254 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1256 if (charpos >= 0
1257 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1258 && IT_CHARPOS (it) >= charpos)
1259 /* When scanning backwards under bidi iteration, move_it_to
1260 stops at or _before_ CHARPOS, because it stops at or to
1261 the _right_ of the character at CHARPOS. */
1262 || (it.bidi_p && it.bidi_it.scan_dir == -1
1263 && IT_CHARPOS (it) <= charpos)))
1265 /* We have reached CHARPOS, or passed it. How the call to
1266 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1267 or covered by a display property, move_it_to stops at the end
1268 of the invisible text, to the right of CHARPOS. (ii) If
1269 CHARPOS is in a display vector, move_it_to stops on its last
1270 glyph. */
1271 int top_x = it.current_x;
1272 int top_y = it.current_y;
1273 enum it_method it_method = it.method;
1274 /* Calling line_bottom_y may change it.method, it.position, etc. */
1275 int bottom_y = (last_height = 0, line_bottom_y (&it));
1276 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1278 if (top_y < window_top_y)
1279 visible_p = bottom_y > window_top_y;
1280 else if (top_y < it.last_visible_y)
1281 visible_p = 1;
1282 if (visible_p)
1284 if (it_method == GET_FROM_DISPLAY_VECTOR)
1286 /* We stopped on the last glyph of a display vector.
1287 Try and recompute. Hack alert! */
1288 if (charpos < 2 || top.charpos >= charpos)
1289 top_x = it.glyph_row->x;
1290 else
1292 struct it it2;
1293 start_display (&it2, w, top);
1294 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1295 get_next_display_element (&it2);
1296 PRODUCE_GLYPHS (&it2);
1297 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1298 || it2.current_x > it2.last_visible_x)
1299 top_x = it.glyph_row->x;
1300 else
1302 top_x = it2.current_x;
1303 top_y = it2.current_y;
1308 *x = top_x;
1309 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1310 *rtop = max (0, window_top_y - top_y);
1311 *rbot = max (0, bottom_y - it.last_visible_y);
1312 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1313 - max (top_y, window_top_y)));
1314 *vpos = it.vpos;
1317 else
1319 /* We were asked to provide info about WINDOW_END. */
1320 struct it it2;
1321 void *it2data = NULL;
1323 SAVE_IT (it2, it, it2data);
1324 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1325 move_it_by_lines (&it, 1);
1326 if (charpos < IT_CHARPOS (it)
1327 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1329 visible_p = 1;
1330 RESTORE_IT (&it2, &it2, it2data);
1331 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1332 *x = it2.current_x;
1333 *y = it2.current_y + it2.max_ascent - it2.ascent;
1334 *rtop = max (0, -it2.current_y);
1335 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1336 - it.last_visible_y));
1337 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1338 it.last_visible_y)
1339 - max (it2.current_y,
1340 WINDOW_HEADER_LINE_HEIGHT (w))));
1341 *vpos = it2.vpos;
1343 else
1344 xfree (it2data);
1346 bidi_unshelve_cache (itdata);
1348 if (old_buffer)
1349 set_buffer_internal_1 (old_buffer);
1351 current_header_line_height = current_mode_line_height = -1;
1353 if (visible_p && XFASTINT (w->hscroll) > 0)
1354 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1356 #if 0
1357 /* Debugging code. */
1358 if (visible_p)
1359 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1360 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1361 else
1362 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1363 #endif
1365 return visible_p;
1369 /* Return the next character from STR. Return in *LEN the length of
1370 the character. This is like STRING_CHAR_AND_LENGTH but never
1371 returns an invalid character. If we find one, we return a `?', but
1372 with the length of the invalid character. */
1374 static inline int
1375 string_char_and_length (const unsigned char *str, int *len)
1377 int c;
1379 c = STRING_CHAR_AND_LENGTH (str, *len);
1380 if (!CHAR_VALID_P (c))
1381 /* We may not change the length here because other places in Emacs
1382 don't use this function, i.e. they silently accept invalid
1383 characters. */
1384 c = '?';
1386 return c;
1391 /* Given a position POS containing a valid character and byte position
1392 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1394 static struct text_pos
1395 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1397 xassert (STRINGP (string) && nchars >= 0);
1399 if (STRING_MULTIBYTE (string))
1401 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1402 int len;
1404 while (nchars--)
1406 string_char_and_length (p, &len);
1407 p += len;
1408 CHARPOS (pos) += 1;
1409 BYTEPOS (pos) += len;
1412 else
1413 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1415 return pos;
1419 /* Value is the text position, i.e. character and byte position,
1420 for character position CHARPOS in STRING. */
1422 static inline struct text_pos
1423 string_pos (EMACS_INT charpos, Lisp_Object string)
1425 struct text_pos pos;
1426 xassert (STRINGP (string));
1427 xassert (charpos >= 0);
1428 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1429 return pos;
1433 /* Value is a text position, i.e. character and byte position, for
1434 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1435 means recognize multibyte characters. */
1437 static struct text_pos
1438 c_string_pos (EMACS_INT charpos, const char *s, int multibyte_p)
1440 struct text_pos pos;
1442 xassert (s != NULL);
1443 xassert (charpos >= 0);
1445 if (multibyte_p)
1447 int len;
1449 SET_TEXT_POS (pos, 0, 0);
1450 while (charpos--)
1452 string_char_and_length ((const unsigned char *) s, &len);
1453 s += len;
1454 CHARPOS (pos) += 1;
1455 BYTEPOS (pos) += len;
1458 else
1459 SET_TEXT_POS (pos, charpos, charpos);
1461 return pos;
1465 /* Value is the number of characters in C string S. MULTIBYTE_P
1466 non-zero means recognize multibyte characters. */
1468 static EMACS_INT
1469 number_of_chars (const char *s, int multibyte_p)
1471 EMACS_INT nchars;
1473 if (multibyte_p)
1475 EMACS_INT rest = strlen (s);
1476 int len;
1477 const unsigned char *p = (const unsigned char *) s;
1479 for (nchars = 0; rest > 0; ++nchars)
1481 string_char_and_length (p, &len);
1482 rest -= len, p += len;
1485 else
1486 nchars = strlen (s);
1488 return nchars;
1492 /* Compute byte position NEWPOS->bytepos corresponding to
1493 NEWPOS->charpos. POS is a known position in string STRING.
1494 NEWPOS->charpos must be >= POS.charpos. */
1496 static void
1497 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1499 xassert (STRINGP (string));
1500 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1502 if (STRING_MULTIBYTE (string))
1503 *newpos = string_pos_nchars_ahead (pos, string,
1504 CHARPOS (*newpos) - CHARPOS (pos));
1505 else
1506 BYTEPOS (*newpos) = CHARPOS (*newpos);
1509 /* EXPORT:
1510 Return an estimation of the pixel height of mode or header lines on
1511 frame F. FACE_ID specifies what line's height to estimate. */
1514 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1516 #ifdef HAVE_WINDOW_SYSTEM
1517 if (FRAME_WINDOW_P (f))
1519 int height = FONT_HEIGHT (FRAME_FONT (f));
1521 /* This function is called so early when Emacs starts that the face
1522 cache and mode line face are not yet initialized. */
1523 if (FRAME_FACE_CACHE (f))
1525 struct face *face = FACE_FROM_ID (f, face_id);
1526 if (face)
1528 if (face->font)
1529 height = FONT_HEIGHT (face->font);
1530 if (face->box_line_width > 0)
1531 height += 2 * face->box_line_width;
1535 return height;
1537 #endif
1539 return 1;
1542 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1543 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1544 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1545 not force the value into range. */
1547 void
1548 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1549 int *x, int *y, NativeRectangle *bounds, int noclip)
1552 #ifdef HAVE_WINDOW_SYSTEM
1553 if (FRAME_WINDOW_P (f))
1555 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1556 even for negative values. */
1557 if (pix_x < 0)
1558 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1559 if (pix_y < 0)
1560 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1562 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1563 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1565 if (bounds)
1566 STORE_NATIVE_RECT (*bounds,
1567 FRAME_COL_TO_PIXEL_X (f, pix_x),
1568 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1569 FRAME_COLUMN_WIDTH (f) - 1,
1570 FRAME_LINE_HEIGHT (f) - 1);
1572 if (!noclip)
1574 if (pix_x < 0)
1575 pix_x = 0;
1576 else if (pix_x > FRAME_TOTAL_COLS (f))
1577 pix_x = FRAME_TOTAL_COLS (f);
1579 if (pix_y < 0)
1580 pix_y = 0;
1581 else if (pix_y > FRAME_LINES (f))
1582 pix_y = FRAME_LINES (f);
1585 #endif
1587 *x = pix_x;
1588 *y = pix_y;
1592 /* Find the glyph under window-relative coordinates X/Y in window W.
1593 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1594 strings. Return in *HPOS and *VPOS the row and column number of
1595 the glyph found. Return in *AREA the glyph area containing X.
1596 Value is a pointer to the glyph found or null if X/Y is not on
1597 text, or we can't tell because W's current matrix is not up to
1598 date. */
1600 static
1601 struct glyph *
1602 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1603 int *dx, int *dy, int *area)
1605 struct glyph *glyph, *end;
1606 struct glyph_row *row = NULL;
1607 int x0, i;
1609 /* Find row containing Y. Give up if some row is not enabled. */
1610 for (i = 0; i < w->current_matrix->nrows; ++i)
1612 row = MATRIX_ROW (w->current_matrix, i);
1613 if (!row->enabled_p)
1614 return NULL;
1615 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1616 break;
1619 *vpos = i;
1620 *hpos = 0;
1622 /* Give up if Y is not in the window. */
1623 if (i == w->current_matrix->nrows)
1624 return NULL;
1626 /* Get the glyph area containing X. */
1627 if (w->pseudo_window_p)
1629 *area = TEXT_AREA;
1630 x0 = 0;
1632 else
1634 if (x < window_box_left_offset (w, TEXT_AREA))
1636 *area = LEFT_MARGIN_AREA;
1637 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1639 else if (x < window_box_right_offset (w, TEXT_AREA))
1641 *area = TEXT_AREA;
1642 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1644 else
1646 *area = RIGHT_MARGIN_AREA;
1647 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1651 /* Find glyph containing X. */
1652 glyph = row->glyphs[*area];
1653 end = glyph + row->used[*area];
1654 x -= x0;
1655 while (glyph < end && x >= glyph->pixel_width)
1657 x -= glyph->pixel_width;
1658 ++glyph;
1661 if (glyph == end)
1662 return NULL;
1664 if (dx)
1666 *dx = x;
1667 *dy = y - (row->y + row->ascent - glyph->ascent);
1670 *hpos = glyph - row->glyphs[*area];
1671 return glyph;
1674 /* Convert frame-relative x/y to coordinates relative to window W.
1675 Takes pseudo-windows into account. */
1677 static void
1678 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1680 if (w->pseudo_window_p)
1682 /* A pseudo-window is always full-width, and starts at the
1683 left edge of the frame, plus a frame border. */
1684 struct frame *f = XFRAME (w->frame);
1685 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1686 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1688 else
1690 *x -= WINDOW_LEFT_EDGE_X (w);
1691 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1695 #ifdef HAVE_WINDOW_SYSTEM
1697 /* EXPORT:
1698 Return in RECTS[] at most N clipping rectangles for glyph string S.
1699 Return the number of stored rectangles. */
1702 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1704 XRectangle r;
1706 if (n <= 0)
1707 return 0;
1709 if (s->row->full_width_p)
1711 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1712 r.x = WINDOW_LEFT_EDGE_X (s->w);
1713 r.width = WINDOW_TOTAL_WIDTH (s->w);
1715 /* Unless displaying a mode or menu bar line, which are always
1716 fully visible, clip to the visible part of the row. */
1717 if (s->w->pseudo_window_p)
1718 r.height = s->row->visible_height;
1719 else
1720 r.height = s->height;
1722 else
1724 /* This is a text line that may be partially visible. */
1725 r.x = window_box_left (s->w, s->area);
1726 r.width = window_box_width (s->w, s->area);
1727 r.height = s->row->visible_height;
1730 if (s->clip_head)
1731 if (r.x < s->clip_head->x)
1733 if (r.width >= s->clip_head->x - r.x)
1734 r.width -= s->clip_head->x - r.x;
1735 else
1736 r.width = 0;
1737 r.x = s->clip_head->x;
1739 if (s->clip_tail)
1740 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1742 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1743 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1744 else
1745 r.width = 0;
1748 /* If S draws overlapping rows, it's sufficient to use the top and
1749 bottom of the window for clipping because this glyph string
1750 intentionally draws over other lines. */
1751 if (s->for_overlaps)
1753 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1754 r.height = window_text_bottom_y (s->w) - r.y;
1756 /* Alas, the above simple strategy does not work for the
1757 environments with anti-aliased text: if the same text is
1758 drawn onto the same place multiple times, it gets thicker.
1759 If the overlap we are processing is for the erased cursor, we
1760 take the intersection with the rectagle of the cursor. */
1761 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1763 XRectangle rc, r_save = r;
1765 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1766 rc.y = s->w->phys_cursor.y;
1767 rc.width = s->w->phys_cursor_width;
1768 rc.height = s->w->phys_cursor_height;
1770 x_intersect_rectangles (&r_save, &rc, &r);
1773 else
1775 /* Don't use S->y for clipping because it doesn't take partially
1776 visible lines into account. For example, it can be negative for
1777 partially visible lines at the top of a window. */
1778 if (!s->row->full_width_p
1779 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1780 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1781 else
1782 r.y = max (0, s->row->y);
1785 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1787 /* If drawing the cursor, don't let glyph draw outside its
1788 advertised boundaries. Cleartype does this under some circumstances. */
1789 if (s->hl == DRAW_CURSOR)
1791 struct glyph *glyph = s->first_glyph;
1792 int height, max_y;
1794 if (s->x > r.x)
1796 r.width -= s->x - r.x;
1797 r.x = s->x;
1799 r.width = min (r.width, glyph->pixel_width);
1801 /* If r.y is below window bottom, ensure that we still see a cursor. */
1802 height = min (glyph->ascent + glyph->descent,
1803 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1804 max_y = window_text_bottom_y (s->w) - height;
1805 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1806 if (s->ybase - glyph->ascent > max_y)
1808 r.y = max_y;
1809 r.height = height;
1811 else
1813 /* Don't draw cursor glyph taller than our actual glyph. */
1814 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1815 if (height < r.height)
1817 max_y = r.y + r.height;
1818 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1819 r.height = min (max_y - r.y, height);
1824 if (s->row->clip)
1826 XRectangle r_save = r;
1828 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
1829 r.width = 0;
1832 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1833 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1835 #ifdef CONVERT_FROM_XRECT
1836 CONVERT_FROM_XRECT (r, *rects);
1837 #else
1838 *rects = r;
1839 #endif
1840 return 1;
1842 else
1844 /* If we are processing overlapping and allowed to return
1845 multiple clipping rectangles, we exclude the row of the glyph
1846 string from the clipping rectangle. This is to avoid drawing
1847 the same text on the environment with anti-aliasing. */
1848 #ifdef CONVERT_FROM_XRECT
1849 XRectangle rs[2];
1850 #else
1851 XRectangle *rs = rects;
1852 #endif
1853 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1855 if (s->for_overlaps & OVERLAPS_PRED)
1857 rs[i] = r;
1858 if (r.y + r.height > row_y)
1860 if (r.y < row_y)
1861 rs[i].height = row_y - r.y;
1862 else
1863 rs[i].height = 0;
1865 i++;
1867 if (s->for_overlaps & OVERLAPS_SUCC)
1869 rs[i] = r;
1870 if (r.y < row_y + s->row->visible_height)
1872 if (r.y + r.height > row_y + s->row->visible_height)
1874 rs[i].y = row_y + s->row->visible_height;
1875 rs[i].height = r.y + r.height - rs[i].y;
1877 else
1878 rs[i].height = 0;
1880 i++;
1883 n = i;
1884 #ifdef CONVERT_FROM_XRECT
1885 for (i = 0; i < n; i++)
1886 CONVERT_FROM_XRECT (rs[i], rects[i]);
1887 #endif
1888 return n;
1892 /* EXPORT:
1893 Return in *NR the clipping rectangle for glyph string S. */
1895 void
1896 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
1898 get_glyph_string_clip_rects (s, nr, 1);
1902 /* EXPORT:
1903 Return the position and height of the phys cursor in window W.
1904 Set w->phys_cursor_width to width of phys cursor.
1907 void
1908 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
1909 struct glyph *glyph, int *xp, int *yp, int *heightp)
1911 struct frame *f = XFRAME (WINDOW_FRAME (w));
1912 int x, y, wd, h, h0, y0;
1914 /* Compute the width of the rectangle to draw. If on a stretch
1915 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1916 rectangle as wide as the glyph, but use a canonical character
1917 width instead. */
1918 wd = glyph->pixel_width - 1;
1919 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
1920 wd++; /* Why? */
1921 #endif
1923 x = w->phys_cursor.x;
1924 if (x < 0)
1926 wd += x;
1927 x = 0;
1930 if (glyph->type == STRETCH_GLYPH
1931 && !x_stretch_cursor_p)
1932 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1933 w->phys_cursor_width = wd;
1935 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1937 /* If y is below window bottom, ensure that we still see a cursor. */
1938 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1940 h = max (h0, glyph->ascent + glyph->descent);
1941 h0 = min (h0, glyph->ascent + glyph->descent);
1943 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
1944 if (y < y0)
1946 h = max (h - (y0 - y) + 1, h0);
1947 y = y0 - 1;
1949 else
1951 y0 = window_text_bottom_y (w) - h0;
1952 if (y > y0)
1954 h += y - y0;
1955 y = y0;
1959 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
1960 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
1961 *heightp = h;
1965 * Remember which glyph the mouse is over.
1968 void
1969 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
1971 Lisp_Object window;
1972 struct window *w;
1973 struct glyph_row *r, *gr, *end_row;
1974 enum window_part part;
1975 enum glyph_row_area area;
1976 int x, y, width, height;
1978 /* Try to determine frame pixel position and size of the glyph under
1979 frame pixel coordinates X/Y on frame F. */
1981 if (!f->glyphs_initialized_p
1982 || (window = window_from_coordinates (f, gx, gy, &part, 0),
1983 NILP (window)))
1985 width = FRAME_SMALLEST_CHAR_WIDTH (f);
1986 height = FRAME_SMALLEST_FONT_HEIGHT (f);
1987 goto virtual_glyph;
1990 w = XWINDOW (window);
1991 width = WINDOW_FRAME_COLUMN_WIDTH (w);
1992 height = WINDOW_FRAME_LINE_HEIGHT (w);
1994 x = window_relative_x_coord (w, part, gx);
1995 y = gy - WINDOW_TOP_EDGE_Y (w);
1997 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1998 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2000 if (w->pseudo_window_p)
2002 area = TEXT_AREA;
2003 part = ON_MODE_LINE; /* Don't adjust margin. */
2004 goto text_glyph;
2007 switch (part)
2009 case ON_LEFT_MARGIN:
2010 area = LEFT_MARGIN_AREA;
2011 goto text_glyph;
2013 case ON_RIGHT_MARGIN:
2014 area = RIGHT_MARGIN_AREA;
2015 goto text_glyph;
2017 case ON_HEADER_LINE:
2018 case ON_MODE_LINE:
2019 gr = (part == ON_HEADER_LINE
2020 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2021 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2022 gy = gr->y;
2023 area = TEXT_AREA;
2024 goto text_glyph_row_found;
2026 case ON_TEXT:
2027 area = TEXT_AREA;
2029 text_glyph:
2030 gr = 0; gy = 0;
2031 for (; r <= end_row && r->enabled_p; ++r)
2032 if (r->y + r->height > y)
2034 gr = r; gy = r->y;
2035 break;
2038 text_glyph_row_found:
2039 if (gr && gy <= y)
2041 struct glyph *g = gr->glyphs[area];
2042 struct glyph *end = g + gr->used[area];
2044 height = gr->height;
2045 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2046 if (gx + g->pixel_width > x)
2047 break;
2049 if (g < end)
2051 if (g->type == IMAGE_GLYPH)
2053 /* Don't remember when mouse is over image, as
2054 image may have hot-spots. */
2055 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2056 return;
2058 width = g->pixel_width;
2060 else
2062 /* Use nominal char spacing at end of line. */
2063 x -= gx;
2064 gx += (x / width) * width;
2067 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2068 gx += window_box_left_offset (w, area);
2070 else
2072 /* Use nominal line height at end of window. */
2073 gx = (x / width) * width;
2074 y -= gy;
2075 gy += (y / height) * height;
2077 break;
2079 case ON_LEFT_FRINGE:
2080 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2081 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2082 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2083 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2084 goto row_glyph;
2086 case ON_RIGHT_FRINGE:
2087 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2088 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2089 : window_box_right_offset (w, TEXT_AREA));
2090 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2091 goto row_glyph;
2093 case ON_SCROLL_BAR:
2094 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2096 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2097 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2098 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2099 : 0)));
2100 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2102 row_glyph:
2103 gr = 0, gy = 0;
2104 for (; r <= end_row && r->enabled_p; ++r)
2105 if (r->y + r->height > y)
2107 gr = r; gy = r->y;
2108 break;
2111 if (gr && gy <= y)
2112 height = gr->height;
2113 else
2115 /* Use nominal line height at end of window. */
2116 y -= gy;
2117 gy += (y / height) * height;
2119 break;
2121 default:
2123 virtual_glyph:
2124 /* If there is no glyph under the mouse, then we divide the screen
2125 into a grid of the smallest glyph in the frame, and use that
2126 as our "glyph". */
2128 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2129 round down even for negative values. */
2130 if (gx < 0)
2131 gx -= width - 1;
2132 if (gy < 0)
2133 gy -= height - 1;
2135 gx = (gx / width) * width;
2136 gy = (gy / height) * height;
2138 goto store_rect;
2141 gx += WINDOW_LEFT_EDGE_X (w);
2142 gy += WINDOW_TOP_EDGE_Y (w);
2144 store_rect:
2145 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2147 /* Visible feedback for debugging. */
2148 #if 0
2149 #if HAVE_X_WINDOWS
2150 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2151 f->output_data.x->normal_gc,
2152 gx, gy, width, height);
2153 #endif
2154 #endif
2158 #endif /* HAVE_WINDOW_SYSTEM */
2161 /***********************************************************************
2162 Lisp form evaluation
2163 ***********************************************************************/
2165 /* Error handler for safe_eval and safe_call. */
2167 static Lisp_Object
2168 safe_eval_handler (Lisp_Object arg)
2170 add_to_log ("Error during redisplay: %S", arg, Qnil);
2171 return Qnil;
2175 /* Evaluate SEXPR and return the result, or nil if something went
2176 wrong. Prevent redisplay during the evaluation. */
2178 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2179 Return the result, or nil if something went wrong. Prevent
2180 redisplay during the evaluation. */
2182 Lisp_Object
2183 safe_call (ptrdiff_t nargs, Lisp_Object *args)
2185 Lisp_Object val;
2187 if (inhibit_eval_during_redisplay)
2188 val = Qnil;
2189 else
2191 int count = SPECPDL_INDEX ();
2192 struct gcpro gcpro1;
2194 GCPRO1 (args[0]);
2195 gcpro1.nvars = nargs;
2196 specbind (Qinhibit_redisplay, Qt);
2197 /* Use Qt to ensure debugger does not run,
2198 so there is no possibility of wanting to redisplay. */
2199 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2200 safe_eval_handler);
2201 UNGCPRO;
2202 val = unbind_to (count, val);
2205 return val;
2209 /* Call function FN with one argument ARG.
2210 Return the result, or nil if something went wrong. */
2212 Lisp_Object
2213 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2215 Lisp_Object args[2];
2216 args[0] = fn;
2217 args[1] = arg;
2218 return safe_call (2, args);
2221 static Lisp_Object Qeval;
2223 Lisp_Object
2224 safe_eval (Lisp_Object sexpr)
2226 return safe_call1 (Qeval, sexpr);
2229 /* Call function FN with one argument ARG.
2230 Return the result, or nil if something went wrong. */
2232 Lisp_Object
2233 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2235 Lisp_Object args[3];
2236 args[0] = fn;
2237 args[1] = arg1;
2238 args[2] = arg2;
2239 return safe_call (3, args);
2244 /***********************************************************************
2245 Debugging
2246 ***********************************************************************/
2248 #if 0
2250 /* Define CHECK_IT to perform sanity checks on iterators.
2251 This is for debugging. It is too slow to do unconditionally. */
2253 static void
2254 check_it (struct it *it)
2256 if (it->method == GET_FROM_STRING)
2258 xassert (STRINGP (it->string));
2259 xassert (IT_STRING_CHARPOS (*it) >= 0);
2261 else
2263 xassert (IT_STRING_CHARPOS (*it) < 0);
2264 if (it->method == GET_FROM_BUFFER)
2266 /* Check that character and byte positions agree. */
2267 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2271 if (it->dpvec)
2272 xassert (it->current.dpvec_index >= 0);
2273 else
2274 xassert (it->current.dpvec_index < 0);
2277 #define CHECK_IT(IT) check_it ((IT))
2279 #else /* not 0 */
2281 #define CHECK_IT(IT) (void) 0
2283 #endif /* not 0 */
2286 #if GLYPH_DEBUG && XASSERTS
2288 /* Check that the window end of window W is what we expect it
2289 to be---the last row in the current matrix displaying text. */
2291 static void
2292 check_window_end (struct window *w)
2294 if (!MINI_WINDOW_P (w)
2295 && !NILP (w->window_end_valid))
2297 struct glyph_row *row;
2298 xassert ((row = MATRIX_ROW (w->current_matrix,
2299 XFASTINT (w->window_end_vpos)),
2300 !row->enabled_p
2301 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2302 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2306 #define CHECK_WINDOW_END(W) check_window_end ((W))
2308 #else
2310 #define CHECK_WINDOW_END(W) (void) 0
2312 #endif
2316 /***********************************************************************
2317 Iterator initialization
2318 ***********************************************************************/
2320 /* Initialize IT for displaying current_buffer in window W, starting
2321 at character position CHARPOS. CHARPOS < 0 means that no buffer
2322 position is specified which is useful when the iterator is assigned
2323 a position later. BYTEPOS is the byte position corresponding to
2324 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2326 If ROW is not null, calls to produce_glyphs with IT as parameter
2327 will produce glyphs in that row.
2329 BASE_FACE_ID is the id of a base face to use. It must be one of
2330 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2331 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2332 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2334 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2335 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2336 will be initialized to use the corresponding mode line glyph row of
2337 the desired matrix of W. */
2339 void
2340 init_iterator (struct it *it, struct window *w,
2341 EMACS_INT charpos, EMACS_INT bytepos,
2342 struct glyph_row *row, enum face_id base_face_id)
2344 int highlight_region_p;
2345 enum face_id remapped_base_face_id = base_face_id;
2347 /* Some precondition checks. */
2348 xassert (w != NULL && it != NULL);
2349 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2350 && charpos <= ZV));
2352 /* If face attributes have been changed since the last redisplay,
2353 free realized faces now because they depend on face definitions
2354 that might have changed. Don't free faces while there might be
2355 desired matrices pending which reference these faces. */
2356 if (face_change_count && !inhibit_free_realized_faces)
2358 face_change_count = 0;
2359 free_all_realized_faces (Qnil);
2362 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2363 if (! NILP (Vface_remapping_alist))
2364 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2366 /* Use one of the mode line rows of W's desired matrix if
2367 appropriate. */
2368 if (row == NULL)
2370 if (base_face_id == MODE_LINE_FACE_ID
2371 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2372 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2373 else if (base_face_id == HEADER_LINE_FACE_ID)
2374 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2377 /* Clear IT. */
2378 memset (it, 0, sizeof *it);
2379 it->current.overlay_string_index = -1;
2380 it->current.dpvec_index = -1;
2381 it->base_face_id = remapped_base_face_id;
2382 it->string = Qnil;
2383 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2384 it->paragraph_embedding = L2R;
2385 it->bidi_it.string.lstring = Qnil;
2386 it->bidi_it.string.s = NULL;
2387 it->bidi_it.string.bufpos = 0;
2389 /* The window in which we iterate over current_buffer: */
2390 XSETWINDOW (it->window, w);
2391 it->w = w;
2392 it->f = XFRAME (w->frame);
2394 it->cmp_it.id = -1;
2396 /* Extra space between lines (on window systems only). */
2397 if (base_face_id == DEFAULT_FACE_ID
2398 && FRAME_WINDOW_P (it->f))
2400 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2401 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2402 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2403 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2404 * FRAME_LINE_HEIGHT (it->f));
2405 else if (it->f->extra_line_spacing > 0)
2406 it->extra_line_spacing = it->f->extra_line_spacing;
2407 it->max_extra_line_spacing = 0;
2410 /* If realized faces have been removed, e.g. because of face
2411 attribute changes of named faces, recompute them. When running
2412 in batch mode, the face cache of the initial frame is null. If
2413 we happen to get called, make a dummy face cache. */
2414 if (FRAME_FACE_CACHE (it->f) == NULL)
2415 init_frame_faces (it->f);
2416 if (FRAME_FACE_CACHE (it->f)->used == 0)
2417 recompute_basic_faces (it->f);
2419 /* Current value of the `slice', `space-width', and 'height' properties. */
2420 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2421 it->space_width = Qnil;
2422 it->font_height = Qnil;
2423 it->override_ascent = -1;
2425 /* Are control characters displayed as `^C'? */
2426 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2428 /* -1 means everything between a CR and the following line end
2429 is invisible. >0 means lines indented more than this value are
2430 invisible. */
2431 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2432 ? XINT (BVAR (current_buffer, selective_display))
2433 : (!NILP (BVAR (current_buffer, selective_display))
2434 ? -1 : 0));
2435 it->selective_display_ellipsis_p
2436 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2438 /* Display table to use. */
2439 it->dp = window_display_table (w);
2441 /* Are multibyte characters enabled in current_buffer? */
2442 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2444 /* Non-zero if we should highlight the region. */
2445 highlight_region_p
2446 = (!NILP (Vtransient_mark_mode)
2447 && !NILP (BVAR (current_buffer, mark_active))
2448 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2450 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2451 start and end of a visible region in window IT->w. Set both to
2452 -1 to indicate no region. */
2453 if (highlight_region_p
2454 /* Maybe highlight only in selected window. */
2455 && (/* Either show region everywhere. */
2456 highlight_nonselected_windows
2457 /* Or show region in the selected window. */
2458 || w == XWINDOW (selected_window)
2459 /* Or show the region if we are in the mini-buffer and W is
2460 the window the mini-buffer refers to. */
2461 || (MINI_WINDOW_P (XWINDOW (selected_window))
2462 && WINDOWP (minibuf_selected_window)
2463 && w == XWINDOW (minibuf_selected_window))))
2465 EMACS_INT markpos = marker_position (BVAR (current_buffer, mark));
2466 it->region_beg_charpos = min (PT, markpos);
2467 it->region_end_charpos = max (PT, markpos);
2469 else
2470 it->region_beg_charpos = it->region_end_charpos = -1;
2472 /* Get the position at which the redisplay_end_trigger hook should
2473 be run, if it is to be run at all. */
2474 if (MARKERP (w->redisplay_end_trigger)
2475 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2476 it->redisplay_end_trigger_charpos
2477 = marker_position (w->redisplay_end_trigger);
2478 else if (INTEGERP (w->redisplay_end_trigger))
2479 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2481 /* Correct bogus values of tab_width. */
2482 it->tab_width = XINT (BVAR (current_buffer, tab_width));
2483 if (it->tab_width <= 0 || it->tab_width > 1000)
2484 it->tab_width = 8;
2486 /* Are lines in the display truncated? */
2487 if (base_face_id != DEFAULT_FACE_ID
2488 || XINT (it->w->hscroll)
2489 || (! WINDOW_FULL_WIDTH_P (it->w)
2490 && ((!NILP (Vtruncate_partial_width_windows)
2491 && !INTEGERP (Vtruncate_partial_width_windows))
2492 || (INTEGERP (Vtruncate_partial_width_windows)
2493 && (WINDOW_TOTAL_COLS (it->w)
2494 < XINT (Vtruncate_partial_width_windows))))))
2495 it->line_wrap = TRUNCATE;
2496 else if (NILP (BVAR (current_buffer, truncate_lines)))
2497 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2498 ? WINDOW_WRAP : WORD_WRAP;
2499 else
2500 it->line_wrap = TRUNCATE;
2502 /* Get dimensions of truncation and continuation glyphs. These are
2503 displayed as fringe bitmaps under X, so we don't need them for such
2504 frames. */
2505 if (!FRAME_WINDOW_P (it->f))
2507 if (it->line_wrap == TRUNCATE)
2509 /* We will need the truncation glyph. */
2510 xassert (it->glyph_row == NULL);
2511 produce_special_glyphs (it, IT_TRUNCATION);
2512 it->truncation_pixel_width = it->pixel_width;
2514 else
2516 /* We will need the continuation glyph. */
2517 xassert (it->glyph_row == NULL);
2518 produce_special_glyphs (it, IT_CONTINUATION);
2519 it->continuation_pixel_width = it->pixel_width;
2522 /* Reset these values to zero because the produce_special_glyphs
2523 above has changed them. */
2524 it->pixel_width = it->ascent = it->descent = 0;
2525 it->phys_ascent = it->phys_descent = 0;
2528 /* Set this after getting the dimensions of truncation and
2529 continuation glyphs, so that we don't produce glyphs when calling
2530 produce_special_glyphs, above. */
2531 it->glyph_row = row;
2532 it->area = TEXT_AREA;
2534 /* Forget any previous info about this row being reversed. */
2535 if (it->glyph_row)
2536 it->glyph_row->reversed_p = 0;
2538 /* Get the dimensions of the display area. The display area
2539 consists of the visible window area plus a horizontally scrolled
2540 part to the left of the window. All x-values are relative to the
2541 start of this total display area. */
2542 if (base_face_id != DEFAULT_FACE_ID)
2544 /* Mode lines, menu bar in terminal frames. */
2545 it->first_visible_x = 0;
2546 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2548 else
2550 it->first_visible_x
2551 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2552 it->last_visible_x = (it->first_visible_x
2553 + window_box_width (w, TEXT_AREA));
2555 /* If we truncate lines, leave room for the truncator glyph(s) at
2556 the right margin. Otherwise, leave room for the continuation
2557 glyph(s). Truncation and continuation glyphs are not inserted
2558 for window-based redisplay. */
2559 if (!FRAME_WINDOW_P (it->f))
2561 if (it->line_wrap == TRUNCATE)
2562 it->last_visible_x -= it->truncation_pixel_width;
2563 else
2564 it->last_visible_x -= it->continuation_pixel_width;
2567 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2568 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2571 /* Leave room for a border glyph. */
2572 if (!FRAME_WINDOW_P (it->f)
2573 && !WINDOW_RIGHTMOST_P (it->w))
2574 it->last_visible_x -= 1;
2576 it->last_visible_y = window_text_bottom_y (w);
2578 /* For mode lines and alike, arrange for the first glyph having a
2579 left box line if the face specifies a box. */
2580 if (base_face_id != DEFAULT_FACE_ID)
2582 struct face *face;
2584 it->face_id = remapped_base_face_id;
2586 /* If we have a boxed mode line, make the first character appear
2587 with a left box line. */
2588 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2589 if (face->box != FACE_NO_BOX)
2590 it->start_of_box_run_p = 1;
2593 /* If a buffer position was specified, set the iterator there,
2594 getting overlays and face properties from that position. */
2595 if (charpos >= BUF_BEG (current_buffer))
2597 it->end_charpos = ZV;
2598 it->face_id = -1;
2599 IT_CHARPOS (*it) = charpos;
2601 /* Compute byte position if not specified. */
2602 if (bytepos < charpos)
2603 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2604 else
2605 IT_BYTEPOS (*it) = bytepos;
2607 it->start = it->current;
2608 /* Do we need to reorder bidirectional text? Not if this is a
2609 unibyte buffer: by definition, none of the single-byte
2610 characters are strong R2L, so no reordering is needed. And
2611 bidi.c doesn't support unibyte buffers anyway. */
2612 it->bidi_p =
2613 !NILP (BVAR (current_buffer, bidi_display_reordering))
2614 && it->multibyte_p;
2616 /* If we are to reorder bidirectional text, init the bidi
2617 iterator. */
2618 if (it->bidi_p)
2620 /* Note the paragraph direction that this buffer wants to
2621 use. */
2622 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2623 Qleft_to_right))
2624 it->paragraph_embedding = L2R;
2625 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2626 Qright_to_left))
2627 it->paragraph_embedding = R2L;
2628 else
2629 it->paragraph_embedding = NEUTRAL_DIR;
2630 bidi_unshelve_cache (NULL);
2631 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2632 &it->bidi_it);
2635 /* Compute faces etc. */
2636 reseat (it, it->current.pos, 1);
2639 CHECK_IT (it);
2643 /* Initialize IT for the display of window W with window start POS. */
2645 void
2646 start_display (struct it *it, struct window *w, struct text_pos pos)
2648 struct glyph_row *row;
2649 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2651 row = w->desired_matrix->rows + first_vpos;
2652 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2653 it->first_vpos = first_vpos;
2655 /* Don't reseat to previous visible line start if current start
2656 position is in a string or image. */
2657 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2659 int start_at_line_beg_p;
2660 int first_y = it->current_y;
2662 /* If window start is not at a line start, skip forward to POS to
2663 get the correct continuation lines width. */
2664 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2665 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2666 if (!start_at_line_beg_p)
2668 int new_x;
2670 reseat_at_previous_visible_line_start (it);
2671 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2673 new_x = it->current_x + it->pixel_width;
2675 /* If lines are continued, this line may end in the middle
2676 of a multi-glyph character (e.g. a control character
2677 displayed as \003, or in the middle of an overlay
2678 string). In this case move_it_to above will not have
2679 taken us to the start of the continuation line but to the
2680 end of the continued line. */
2681 if (it->current_x > 0
2682 && it->line_wrap != TRUNCATE /* Lines are continued. */
2683 && (/* And glyph doesn't fit on the line. */
2684 new_x > it->last_visible_x
2685 /* Or it fits exactly and we're on a window
2686 system frame. */
2687 || (new_x == it->last_visible_x
2688 && FRAME_WINDOW_P (it->f))))
2690 if (it->current.dpvec_index >= 0
2691 || it->current.overlay_string_index >= 0)
2693 set_iterator_to_next (it, 1);
2694 move_it_in_display_line_to (it, -1, -1, 0);
2697 it->continuation_lines_width += it->current_x;
2700 /* We're starting a new display line, not affected by the
2701 height of the continued line, so clear the appropriate
2702 fields in the iterator structure. */
2703 it->max_ascent = it->max_descent = 0;
2704 it->max_phys_ascent = it->max_phys_descent = 0;
2706 it->current_y = first_y;
2707 it->vpos = 0;
2708 it->current_x = it->hpos = 0;
2714 /* Return 1 if POS is a position in ellipses displayed for invisible
2715 text. W is the window we display, for text property lookup. */
2717 static int
2718 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2720 Lisp_Object prop, window;
2721 int ellipses_p = 0;
2722 EMACS_INT charpos = CHARPOS (pos->pos);
2724 /* If POS specifies a position in a display vector, this might
2725 be for an ellipsis displayed for invisible text. We won't
2726 get the iterator set up for delivering that ellipsis unless
2727 we make sure that it gets aware of the invisible text. */
2728 if (pos->dpvec_index >= 0
2729 && pos->overlay_string_index < 0
2730 && CHARPOS (pos->string_pos) < 0
2731 && charpos > BEGV
2732 && (XSETWINDOW (window, w),
2733 prop = Fget_char_property (make_number (charpos),
2734 Qinvisible, window),
2735 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2737 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2738 window);
2739 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2742 return ellipses_p;
2746 /* Initialize IT for stepping through current_buffer in window W,
2747 starting at position POS that includes overlay string and display
2748 vector/ control character translation position information. Value
2749 is zero if there are overlay strings with newlines at POS. */
2751 static int
2752 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2754 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2755 int i, overlay_strings_with_newlines = 0;
2757 /* If POS specifies a position in a display vector, this might
2758 be for an ellipsis displayed for invisible text. We won't
2759 get the iterator set up for delivering that ellipsis unless
2760 we make sure that it gets aware of the invisible text. */
2761 if (in_ellipses_for_invisible_text_p (pos, w))
2763 --charpos;
2764 bytepos = 0;
2767 /* Keep in mind: the call to reseat in init_iterator skips invisible
2768 text, so we might end up at a position different from POS. This
2769 is only a problem when POS is a row start after a newline and an
2770 overlay starts there with an after-string, and the overlay has an
2771 invisible property. Since we don't skip invisible text in
2772 display_line and elsewhere immediately after consuming the
2773 newline before the row start, such a POS will not be in a string,
2774 but the call to init_iterator below will move us to the
2775 after-string. */
2776 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2778 /* This only scans the current chunk -- it should scan all chunks.
2779 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2780 to 16 in 22.1 to make this a lesser problem. */
2781 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2783 const char *s = SSDATA (it->overlay_strings[i]);
2784 const char *e = s + SBYTES (it->overlay_strings[i]);
2786 while (s < e && *s != '\n')
2787 ++s;
2789 if (s < e)
2791 overlay_strings_with_newlines = 1;
2792 break;
2796 /* If position is within an overlay string, set up IT to the right
2797 overlay string. */
2798 if (pos->overlay_string_index >= 0)
2800 int relative_index;
2802 /* If the first overlay string happens to have a `display'
2803 property for an image, the iterator will be set up for that
2804 image, and we have to undo that setup first before we can
2805 correct the overlay string index. */
2806 if (it->method == GET_FROM_IMAGE)
2807 pop_it (it);
2809 /* We already have the first chunk of overlay strings in
2810 IT->overlay_strings. Load more until the one for
2811 pos->overlay_string_index is in IT->overlay_strings. */
2812 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2814 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2815 it->current.overlay_string_index = 0;
2816 while (n--)
2818 load_overlay_strings (it, 0);
2819 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2823 it->current.overlay_string_index = pos->overlay_string_index;
2824 relative_index = (it->current.overlay_string_index
2825 % OVERLAY_STRING_CHUNK_SIZE);
2826 it->string = it->overlay_strings[relative_index];
2827 xassert (STRINGP (it->string));
2828 it->current.string_pos = pos->string_pos;
2829 it->method = GET_FROM_STRING;
2832 if (CHARPOS (pos->string_pos) >= 0)
2834 /* Recorded position is not in an overlay string, but in another
2835 string. This can only be a string from a `display' property.
2836 IT should already be filled with that string. */
2837 it->current.string_pos = pos->string_pos;
2838 xassert (STRINGP (it->string));
2841 /* Restore position in display vector translations, control
2842 character translations or ellipses. */
2843 if (pos->dpvec_index >= 0)
2845 if (it->dpvec == NULL)
2846 get_next_display_element (it);
2847 xassert (it->dpvec && it->current.dpvec_index == 0);
2848 it->current.dpvec_index = pos->dpvec_index;
2851 CHECK_IT (it);
2852 return !overlay_strings_with_newlines;
2856 /* Initialize IT for stepping through current_buffer in window W
2857 starting at ROW->start. */
2859 static void
2860 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
2862 init_from_display_pos (it, w, &row->start);
2863 it->start = row->start;
2864 it->continuation_lines_width = row->continuation_lines_width;
2865 CHECK_IT (it);
2869 /* Initialize IT for stepping through current_buffer in window W
2870 starting in the line following ROW, i.e. starting at ROW->end.
2871 Value is zero if there are overlay strings with newlines at ROW's
2872 end position. */
2874 static int
2875 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
2877 int success = 0;
2879 if (init_from_display_pos (it, w, &row->end))
2881 if (row->continued_p)
2882 it->continuation_lines_width
2883 = row->continuation_lines_width + row->pixel_width;
2884 CHECK_IT (it);
2885 success = 1;
2888 return success;
2894 /***********************************************************************
2895 Text properties
2896 ***********************************************************************/
2898 /* Called when IT reaches IT->stop_charpos. Handle text property and
2899 overlay changes. Set IT->stop_charpos to the next position where
2900 to stop. */
2902 static void
2903 handle_stop (struct it *it)
2905 enum prop_handled handled;
2906 int handle_overlay_change_p;
2907 struct props *p;
2909 it->dpvec = NULL;
2910 it->current.dpvec_index = -1;
2911 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
2912 it->ignore_overlay_strings_at_pos_p = 0;
2913 it->ellipsis_p = 0;
2915 /* Use face of preceding text for ellipsis (if invisible) */
2916 if (it->selective_display_ellipsis_p)
2917 it->saved_face_id = it->face_id;
2921 handled = HANDLED_NORMALLY;
2923 /* Call text property handlers. */
2924 for (p = it_props; p->handler; ++p)
2926 handled = p->handler (it);
2928 if (handled == HANDLED_RECOMPUTE_PROPS)
2929 break;
2930 else if (handled == HANDLED_RETURN)
2932 /* We still want to show before and after strings from
2933 overlays even if the actual buffer text is replaced. */
2934 if (!handle_overlay_change_p
2935 || it->sp > 1
2936 || !get_overlay_strings_1 (it, 0, 0))
2938 if (it->ellipsis_p)
2939 setup_for_ellipsis (it, 0);
2940 /* When handling a display spec, we might load an
2941 empty string. In that case, discard it here. We
2942 used to discard it in handle_single_display_spec,
2943 but that causes get_overlay_strings_1, above, to
2944 ignore overlay strings that we must check. */
2945 if (STRINGP (it->string) && !SCHARS (it->string))
2946 pop_it (it);
2947 return;
2949 else if (STRINGP (it->string) && !SCHARS (it->string))
2950 pop_it (it);
2951 else
2953 it->ignore_overlay_strings_at_pos_p = 1;
2954 it->string_from_display_prop_p = 0;
2955 it->from_disp_prop_p = 0;
2956 handle_overlay_change_p = 0;
2958 handled = HANDLED_RECOMPUTE_PROPS;
2959 break;
2961 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2962 handle_overlay_change_p = 0;
2965 if (handled != HANDLED_RECOMPUTE_PROPS)
2967 /* Don't check for overlay strings below when set to deliver
2968 characters from a display vector. */
2969 if (it->method == GET_FROM_DISPLAY_VECTOR)
2970 handle_overlay_change_p = 0;
2972 /* Handle overlay changes.
2973 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
2974 if it finds overlays. */
2975 if (handle_overlay_change_p)
2976 handled = handle_overlay_change (it);
2979 if (it->ellipsis_p)
2981 setup_for_ellipsis (it, 0);
2982 break;
2985 while (handled == HANDLED_RECOMPUTE_PROPS);
2987 /* Determine where to stop next. */
2988 if (handled == HANDLED_NORMALLY)
2989 compute_stop_pos (it);
2993 /* Compute IT->stop_charpos from text property and overlay change
2994 information for IT's current position. */
2996 static void
2997 compute_stop_pos (struct it *it)
2999 register INTERVAL iv, next_iv;
3000 Lisp_Object object, limit, position;
3001 EMACS_INT charpos, bytepos;
3003 /* If nowhere else, stop at the end. */
3004 it->stop_charpos = it->end_charpos;
3006 if (STRINGP (it->string))
3008 /* Strings are usually short, so don't limit the search for
3009 properties. */
3010 object = it->string;
3011 limit = Qnil;
3012 charpos = IT_STRING_CHARPOS (*it);
3013 bytepos = IT_STRING_BYTEPOS (*it);
3015 else
3017 EMACS_INT pos;
3019 /* If next overlay change is in front of the current stop pos
3020 (which is IT->end_charpos), stop there. Note: value of
3021 next_overlay_change is point-max if no overlay change
3022 follows. */
3023 charpos = IT_CHARPOS (*it);
3024 bytepos = IT_BYTEPOS (*it);
3025 pos = next_overlay_change (charpos);
3026 if (pos < it->stop_charpos)
3027 it->stop_charpos = pos;
3029 /* If showing the region, we have to stop at the region
3030 start or end because the face might change there. */
3031 if (it->region_beg_charpos > 0)
3033 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3034 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3035 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3036 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3039 /* Set up variables for computing the stop position from text
3040 property changes. */
3041 XSETBUFFER (object, current_buffer);
3042 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3045 /* Get the interval containing IT's position. Value is a null
3046 interval if there isn't such an interval. */
3047 position = make_number (charpos);
3048 iv = validate_interval_range (object, &position, &position, 0);
3049 if (!NULL_INTERVAL_P (iv))
3051 Lisp_Object values_here[LAST_PROP_IDX];
3052 struct props *p;
3054 /* Get properties here. */
3055 for (p = it_props; p->handler; ++p)
3056 values_here[p->idx] = textget (iv->plist, *p->name);
3058 /* Look for an interval following iv that has different
3059 properties. */
3060 for (next_iv = next_interval (iv);
3061 (!NULL_INTERVAL_P (next_iv)
3062 && (NILP (limit)
3063 || XFASTINT (limit) > next_iv->position));
3064 next_iv = next_interval (next_iv))
3066 for (p = it_props; p->handler; ++p)
3068 Lisp_Object new_value;
3070 new_value = textget (next_iv->plist, *p->name);
3071 if (!EQ (values_here[p->idx], new_value))
3072 break;
3075 if (p->handler)
3076 break;
3079 if (!NULL_INTERVAL_P (next_iv))
3081 if (INTEGERP (limit)
3082 && next_iv->position >= XFASTINT (limit))
3083 /* No text property change up to limit. */
3084 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3085 else
3086 /* Text properties change in next_iv. */
3087 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3091 if (it->cmp_it.id < 0)
3093 EMACS_INT stoppos = it->end_charpos;
3095 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3096 stoppos = -1;
3097 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3098 stoppos, it->string);
3101 xassert (STRINGP (it->string)
3102 || (it->stop_charpos >= BEGV
3103 && it->stop_charpos >= IT_CHARPOS (*it)));
3107 /* Return the position of the next overlay change after POS in
3108 current_buffer. Value is point-max if no overlay change
3109 follows. This is like `next-overlay-change' but doesn't use
3110 xmalloc. */
3112 static EMACS_INT
3113 next_overlay_change (EMACS_INT pos)
3115 ptrdiff_t i, noverlays;
3116 EMACS_INT endpos;
3117 Lisp_Object *overlays;
3119 /* Get all overlays at the given position. */
3120 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3122 /* If any of these overlays ends before endpos,
3123 use its ending point instead. */
3124 for (i = 0; i < noverlays; ++i)
3126 Lisp_Object oend;
3127 EMACS_INT oendpos;
3129 oend = OVERLAY_END (overlays[i]);
3130 oendpos = OVERLAY_POSITION (oend);
3131 endpos = min (endpos, oendpos);
3134 return endpos;
3137 /* Record one cached display string position found recently by
3138 compute_display_string_pos. */
3139 static EMACS_INT cached_disp_pos;
3140 static EMACS_INT cached_prev_pos;
3141 static struct buffer *cached_disp_buffer;
3142 static int cached_disp_modiff;
3143 static int cached_disp_overlay_modiff;
3145 /* Return the character position of a display string at or after
3146 position specified by POSITION. If no display string exists at or
3147 after POSITION, return ZV. A display string is either an overlay
3148 with `display' property whose value is a string, or a `display'
3149 text property whose value is a string. STRING is data about the
3150 string to iterate; if STRING->lstring is nil, we are iterating a
3151 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3152 on a GUI frame. */
3153 EMACS_INT
3154 compute_display_string_pos (struct text_pos *position,
3155 struct bidi_string_data *string, int frame_window_p)
3157 /* OBJECT = nil means current buffer. */
3158 Lisp_Object object =
3159 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3160 Lisp_Object pos, spec;
3161 int string_p = (string && (STRINGP (string->lstring) || string->s));
3162 EMACS_INT eob = string_p ? string->schars : ZV;
3163 EMACS_INT begb = string_p ? 0 : BEGV;
3164 EMACS_INT bufpos, charpos = CHARPOS (*position);
3165 struct text_pos tpos;
3166 struct buffer *b;
3168 if (charpos >= eob
3169 /* We don't support display properties whose values are strings
3170 that have display string properties. */
3171 || string->from_disp_str
3172 /* C strings cannot have display properties. */
3173 || (string->s && !STRINGP (object)))
3174 return eob;
3176 /* Check the cached values. */
3177 if (!STRINGP (object))
3179 if (NILP (object))
3180 b = current_buffer;
3181 else
3182 b = XBUFFER (object);
3183 if (b == cached_disp_buffer
3184 && BUF_MODIFF (b) == cached_disp_modiff
3185 && BUF_OVERLAY_MODIFF (b) == cached_disp_overlay_modiff)
3187 if (cached_prev_pos
3188 && cached_prev_pos < charpos && charpos <= cached_disp_pos)
3189 return cached_disp_pos;
3190 /* Handle overstepping either end of the known interval. */
3191 if (charpos > cached_disp_pos)
3192 cached_prev_pos = cached_disp_pos;
3193 else /* charpos <= cached_prev_pos */
3194 cached_prev_pos = max (charpos - 1, BEGV);
3197 /* Record new values in the cache. */
3198 cached_disp_buffer = b;
3199 cached_disp_modiff = BUF_MODIFF (b);
3200 cached_disp_overlay_modiff = BUF_OVERLAY_MODIFF (b);
3203 /* If the character at CHARPOS is where the display string begins,
3204 return CHARPOS. */
3205 pos = make_number (charpos);
3206 if (STRINGP (object))
3207 bufpos = string->bufpos;
3208 else
3209 bufpos = charpos;
3210 tpos = *position;
3211 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3212 && (charpos <= begb
3213 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3214 object),
3215 spec))
3216 && handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3217 frame_window_p))
3219 if (!STRINGP (object))
3220 cached_disp_pos = charpos;
3221 return charpos;
3224 /* Look forward for the first character with a `display' property
3225 that will replace the underlying text when displayed. */
3226 do {
3227 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3228 CHARPOS (tpos) = XFASTINT (pos);
3229 if (STRINGP (object))
3230 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3231 else
3232 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3233 if (CHARPOS (tpos) >= eob)
3234 break;
3235 spec = Fget_char_property (pos, Qdisplay, object);
3236 if (!STRINGP (object))
3237 bufpos = CHARPOS (tpos);
3238 } while (NILP (spec)
3239 || !handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3240 frame_window_p));
3242 if (!STRINGP (object))
3243 cached_disp_pos = CHARPOS (tpos);
3244 return CHARPOS (tpos);
3247 /* Return the character position of the end of the display string that
3248 started at CHARPOS. A display string is either an overlay with
3249 `display' property whose value is a string or a `display' text
3250 property whose value is a string. */
3251 EMACS_INT
3252 compute_display_string_end (EMACS_INT charpos, struct bidi_string_data *string)
3254 /* OBJECT = nil means current buffer. */
3255 Lisp_Object object =
3256 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3257 Lisp_Object pos = make_number (charpos);
3258 EMACS_INT eob =
3259 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3261 if (charpos >= eob || (string->s && !STRINGP (object)))
3262 return eob;
3264 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3265 abort ();
3267 /* Look forward for the first character where the `display' property
3268 changes. */
3269 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3271 return XFASTINT (pos);
3276 /***********************************************************************
3277 Fontification
3278 ***********************************************************************/
3280 /* Handle changes in the `fontified' property of the current buffer by
3281 calling hook functions from Qfontification_functions to fontify
3282 regions of text. */
3284 static enum prop_handled
3285 handle_fontified_prop (struct it *it)
3287 Lisp_Object prop, pos;
3288 enum prop_handled handled = HANDLED_NORMALLY;
3290 if (!NILP (Vmemory_full))
3291 return handled;
3293 /* Get the value of the `fontified' property at IT's current buffer
3294 position. (The `fontified' property doesn't have a special
3295 meaning in strings.) If the value is nil, call functions from
3296 Qfontification_functions. */
3297 if (!STRINGP (it->string)
3298 && it->s == NULL
3299 && !NILP (Vfontification_functions)
3300 && !NILP (Vrun_hooks)
3301 && (pos = make_number (IT_CHARPOS (*it)),
3302 prop = Fget_char_property (pos, Qfontified, Qnil),
3303 /* Ignore the special cased nil value always present at EOB since
3304 no amount of fontifying will be able to change it. */
3305 NILP (prop) && IT_CHARPOS (*it) < Z))
3307 int count = SPECPDL_INDEX ();
3308 Lisp_Object val;
3309 struct buffer *obuf = current_buffer;
3310 int begv = BEGV, zv = ZV;
3311 int old_clip_changed = current_buffer->clip_changed;
3313 val = Vfontification_functions;
3314 specbind (Qfontification_functions, Qnil);
3316 xassert (it->end_charpos == ZV);
3318 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3319 safe_call1 (val, pos);
3320 else
3322 Lisp_Object fns, fn;
3323 struct gcpro gcpro1, gcpro2;
3325 fns = Qnil;
3326 GCPRO2 (val, fns);
3328 for (; CONSP (val); val = XCDR (val))
3330 fn = XCAR (val);
3332 if (EQ (fn, Qt))
3334 /* A value of t indicates this hook has a local
3335 binding; it means to run the global binding too.
3336 In a global value, t should not occur. If it
3337 does, we must ignore it to avoid an endless
3338 loop. */
3339 for (fns = Fdefault_value (Qfontification_functions);
3340 CONSP (fns);
3341 fns = XCDR (fns))
3343 fn = XCAR (fns);
3344 if (!EQ (fn, Qt))
3345 safe_call1 (fn, pos);
3348 else
3349 safe_call1 (fn, pos);
3352 UNGCPRO;
3355 unbind_to (count, Qnil);
3357 /* Fontification functions routinely call `save-restriction'.
3358 Normally, this tags clip_changed, which can confuse redisplay
3359 (see discussion in Bug#6671). Since we don't perform any
3360 special handling of fontification changes in the case where
3361 `save-restriction' isn't called, there's no point doing so in
3362 this case either. So, if the buffer's restrictions are
3363 actually left unchanged, reset clip_changed. */
3364 if (obuf == current_buffer)
3366 if (begv == BEGV && zv == ZV)
3367 current_buffer->clip_changed = old_clip_changed;
3369 /* There isn't much we can reasonably do to protect against
3370 misbehaving fontification, but here's a fig leaf. */
3371 else if (!NILP (BVAR (obuf, name)))
3372 set_buffer_internal_1 (obuf);
3374 /* The fontification code may have added/removed text.
3375 It could do even a lot worse, but let's at least protect against
3376 the most obvious case where only the text past `pos' gets changed',
3377 as is/was done in grep.el where some escapes sequences are turned
3378 into face properties (bug#7876). */
3379 it->end_charpos = ZV;
3381 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3382 something. This avoids an endless loop if they failed to
3383 fontify the text for which reason ever. */
3384 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3385 handled = HANDLED_RECOMPUTE_PROPS;
3388 return handled;
3393 /***********************************************************************
3394 Faces
3395 ***********************************************************************/
3397 /* Set up iterator IT from face properties at its current position.
3398 Called from handle_stop. */
3400 static enum prop_handled
3401 handle_face_prop (struct it *it)
3403 int new_face_id;
3404 EMACS_INT next_stop;
3406 if (!STRINGP (it->string))
3408 new_face_id
3409 = face_at_buffer_position (it->w,
3410 IT_CHARPOS (*it),
3411 it->region_beg_charpos,
3412 it->region_end_charpos,
3413 &next_stop,
3414 (IT_CHARPOS (*it)
3415 + TEXT_PROP_DISTANCE_LIMIT),
3416 0, it->base_face_id);
3418 /* Is this a start of a run of characters with box face?
3419 Caveat: this can be called for a freshly initialized
3420 iterator; face_id is -1 in this case. We know that the new
3421 face will not change until limit, i.e. if the new face has a
3422 box, all characters up to limit will have one. But, as
3423 usual, we don't know whether limit is really the end. */
3424 if (new_face_id != it->face_id)
3426 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3428 /* If new face has a box but old face has not, this is
3429 the start of a run of characters with box, i.e. it has
3430 a shadow on the left side. The value of face_id of the
3431 iterator will be -1 if this is the initial call that gets
3432 the face. In this case, we have to look in front of IT's
3433 position and see whether there is a face != new_face_id. */
3434 it->start_of_box_run_p
3435 = (new_face->box != FACE_NO_BOX
3436 && (it->face_id >= 0
3437 || IT_CHARPOS (*it) == BEG
3438 || new_face_id != face_before_it_pos (it)));
3439 it->face_box_p = new_face->box != FACE_NO_BOX;
3442 else
3444 int base_face_id;
3445 EMACS_INT bufpos;
3446 int i;
3447 Lisp_Object from_overlay
3448 = (it->current.overlay_string_index >= 0
3449 ? it->string_overlays[it->current.overlay_string_index]
3450 : Qnil);
3452 /* See if we got to this string directly or indirectly from
3453 an overlay property. That includes the before-string or
3454 after-string of an overlay, strings in display properties
3455 provided by an overlay, their text properties, etc.
3457 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3458 if (! NILP (from_overlay))
3459 for (i = it->sp - 1; i >= 0; i--)
3461 if (it->stack[i].current.overlay_string_index >= 0)
3462 from_overlay
3463 = it->string_overlays[it->stack[i].current.overlay_string_index];
3464 else if (! NILP (it->stack[i].from_overlay))
3465 from_overlay = it->stack[i].from_overlay;
3467 if (!NILP (from_overlay))
3468 break;
3471 if (! NILP (from_overlay))
3473 bufpos = IT_CHARPOS (*it);
3474 /* For a string from an overlay, the base face depends
3475 only on text properties and ignores overlays. */
3476 base_face_id
3477 = face_for_overlay_string (it->w,
3478 IT_CHARPOS (*it),
3479 it->region_beg_charpos,
3480 it->region_end_charpos,
3481 &next_stop,
3482 (IT_CHARPOS (*it)
3483 + TEXT_PROP_DISTANCE_LIMIT),
3485 from_overlay);
3487 else
3489 bufpos = 0;
3491 /* For strings from a `display' property, use the face at
3492 IT's current buffer position as the base face to merge
3493 with, so that overlay strings appear in the same face as
3494 surrounding text, unless they specify their own
3495 faces. */
3496 base_face_id = underlying_face_id (it);
3499 new_face_id = face_at_string_position (it->w,
3500 it->string,
3501 IT_STRING_CHARPOS (*it),
3502 bufpos,
3503 it->region_beg_charpos,
3504 it->region_end_charpos,
3505 &next_stop,
3506 base_face_id, 0);
3508 /* Is this a start of a run of characters with box? Caveat:
3509 this can be called for a freshly allocated iterator; face_id
3510 is -1 is this case. We know that the new face will not
3511 change until the next check pos, i.e. if the new face has a
3512 box, all characters up to that position will have a
3513 box. But, as usual, we don't know whether that position
3514 is really the end. */
3515 if (new_face_id != it->face_id)
3517 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3518 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3520 /* If new face has a box but old face hasn't, this is the
3521 start of a run of characters with box, i.e. it has a
3522 shadow on the left side. */
3523 it->start_of_box_run_p
3524 = new_face->box && (old_face == NULL || !old_face->box);
3525 it->face_box_p = new_face->box != FACE_NO_BOX;
3529 it->face_id = new_face_id;
3530 return HANDLED_NORMALLY;
3534 /* Return the ID of the face ``underlying'' IT's current position,
3535 which is in a string. If the iterator is associated with a
3536 buffer, return the face at IT's current buffer position.
3537 Otherwise, use the iterator's base_face_id. */
3539 static int
3540 underlying_face_id (struct it *it)
3542 int face_id = it->base_face_id, i;
3544 xassert (STRINGP (it->string));
3546 for (i = it->sp - 1; i >= 0; --i)
3547 if (NILP (it->stack[i].string))
3548 face_id = it->stack[i].face_id;
3550 return face_id;
3554 /* Compute the face one character before or after the current position
3555 of IT, in the visual order. BEFORE_P non-zero means get the face
3556 in front (to the left in L2R paragraphs, to the right in R2L
3557 paragraphs) of IT's screen position. Value is the ID of the face. */
3559 static int
3560 face_before_or_after_it_pos (struct it *it, int before_p)
3562 int face_id, limit;
3563 EMACS_INT next_check_charpos;
3564 struct it it_copy;
3565 void *it_copy_data = NULL;
3567 xassert (it->s == NULL);
3569 if (STRINGP (it->string))
3571 EMACS_INT bufpos, charpos;
3572 int base_face_id;
3574 /* No face change past the end of the string (for the case
3575 we are padding with spaces). No face change before the
3576 string start. */
3577 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3578 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3579 return it->face_id;
3581 if (!it->bidi_p)
3583 /* Set charpos to the position before or after IT's current
3584 position, in the logical order, which in the non-bidi
3585 case is the same as the visual order. */
3586 if (before_p)
3587 charpos = IT_STRING_CHARPOS (*it) - 1;
3588 else if (it->what == IT_COMPOSITION)
3589 /* For composition, we must check the character after the
3590 composition. */
3591 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3592 else
3593 charpos = IT_STRING_CHARPOS (*it) + 1;
3595 else
3597 if (before_p)
3599 /* With bidi iteration, the character before the current
3600 in the visual order cannot be found by simple
3601 iteration, because "reverse" reordering is not
3602 supported. Instead, we need to use the move_it_*
3603 family of functions. */
3604 /* Ignore face changes before the first visible
3605 character on this display line. */
3606 if (it->current_x <= it->first_visible_x)
3607 return it->face_id;
3608 SAVE_IT (it_copy, *it, it_copy_data);
3609 /* Implementation note: Since move_it_in_display_line
3610 works in the iterator geometry, and thinks the first
3611 character is always the leftmost, even in R2L lines,
3612 we don't need to distinguish between the R2L and L2R
3613 cases here. */
3614 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3615 it_copy.current_x - 1, MOVE_TO_X);
3616 charpos = IT_STRING_CHARPOS (it_copy);
3617 RESTORE_IT (it, it, it_copy_data);
3619 else
3621 /* Set charpos to the string position of the character
3622 that comes after IT's current position in the visual
3623 order. */
3624 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3626 it_copy = *it;
3627 while (n--)
3628 bidi_move_to_visually_next (&it_copy.bidi_it);
3630 charpos = it_copy.bidi_it.charpos;
3633 xassert (0 <= charpos && charpos <= SCHARS (it->string));
3635 if (it->current.overlay_string_index >= 0)
3636 bufpos = IT_CHARPOS (*it);
3637 else
3638 bufpos = 0;
3640 base_face_id = underlying_face_id (it);
3642 /* Get the face for ASCII, or unibyte. */
3643 face_id = face_at_string_position (it->w,
3644 it->string,
3645 charpos,
3646 bufpos,
3647 it->region_beg_charpos,
3648 it->region_end_charpos,
3649 &next_check_charpos,
3650 base_face_id, 0);
3652 /* Correct the face for charsets different from ASCII. Do it
3653 for the multibyte case only. The face returned above is
3654 suitable for unibyte text if IT->string is unibyte. */
3655 if (STRING_MULTIBYTE (it->string))
3657 struct text_pos pos1 = string_pos (charpos, it->string);
3658 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3659 int c, len;
3660 struct face *face = FACE_FROM_ID (it->f, face_id);
3662 c = string_char_and_length (p, &len);
3663 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3666 else
3668 struct text_pos pos;
3670 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3671 || (IT_CHARPOS (*it) <= BEGV && before_p))
3672 return it->face_id;
3674 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3675 pos = it->current.pos;
3677 if (!it->bidi_p)
3679 if (before_p)
3680 DEC_TEXT_POS (pos, it->multibyte_p);
3681 else
3683 if (it->what == IT_COMPOSITION)
3685 /* For composition, we must check the position after
3686 the composition. */
3687 pos.charpos += it->cmp_it.nchars;
3688 pos.bytepos += it->len;
3690 else
3691 INC_TEXT_POS (pos, it->multibyte_p);
3694 else
3696 if (before_p)
3698 /* With bidi iteration, the character before the current
3699 in the visual order cannot be found by simple
3700 iteration, because "reverse" reordering is not
3701 supported. Instead, we need to use the move_it_*
3702 family of functions. */
3703 /* Ignore face changes before the first visible
3704 character on this display line. */
3705 if (it->current_x <= it->first_visible_x)
3706 return it->face_id;
3707 SAVE_IT (it_copy, *it, it_copy_data);
3708 /* Implementation note: Since move_it_in_display_line
3709 works in the iterator geometry, and thinks the first
3710 character is always the leftmost, even in R2L lines,
3711 we don't need to distinguish between the R2L and L2R
3712 cases here. */
3713 move_it_in_display_line (&it_copy, ZV,
3714 it_copy.current_x - 1, MOVE_TO_X);
3715 pos = it_copy.current.pos;
3716 RESTORE_IT (it, it, it_copy_data);
3718 else
3720 /* Set charpos to the buffer position of the character
3721 that comes after IT's current position in the visual
3722 order. */
3723 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3725 it_copy = *it;
3726 while (n--)
3727 bidi_move_to_visually_next (&it_copy.bidi_it);
3729 SET_TEXT_POS (pos,
3730 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3733 xassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
3735 /* Determine face for CHARSET_ASCII, or unibyte. */
3736 face_id = face_at_buffer_position (it->w,
3737 CHARPOS (pos),
3738 it->region_beg_charpos,
3739 it->region_end_charpos,
3740 &next_check_charpos,
3741 limit, 0, -1);
3743 /* Correct the face for charsets different from ASCII. Do it
3744 for the multibyte case only. The face returned above is
3745 suitable for unibyte text if current_buffer is unibyte. */
3746 if (it->multibyte_p)
3748 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3749 struct face *face = FACE_FROM_ID (it->f, face_id);
3750 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3754 return face_id;
3759 /***********************************************************************
3760 Invisible text
3761 ***********************************************************************/
3763 /* Set up iterator IT from invisible properties at its current
3764 position. Called from handle_stop. */
3766 static enum prop_handled
3767 handle_invisible_prop (struct it *it)
3769 enum prop_handled handled = HANDLED_NORMALLY;
3771 if (STRINGP (it->string))
3773 Lisp_Object prop, end_charpos, limit, charpos;
3775 /* Get the value of the invisible text property at the
3776 current position. Value will be nil if there is no such
3777 property. */
3778 charpos = make_number (IT_STRING_CHARPOS (*it));
3779 prop = Fget_text_property (charpos, Qinvisible, it->string);
3781 if (!NILP (prop)
3782 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3784 EMACS_INT endpos;
3786 handled = HANDLED_RECOMPUTE_PROPS;
3788 /* Get the position at which the next change of the
3789 invisible text property can be found in IT->string.
3790 Value will be nil if the property value is the same for
3791 all the rest of IT->string. */
3792 XSETINT (limit, SCHARS (it->string));
3793 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3794 it->string, limit);
3796 /* Text at current position is invisible. The next
3797 change in the property is at position end_charpos.
3798 Move IT's current position to that position. */
3799 if (INTEGERP (end_charpos)
3800 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
3802 struct text_pos old;
3803 EMACS_INT oldpos;
3805 old = it->current.string_pos;
3806 oldpos = CHARPOS (old);
3807 if (it->bidi_p)
3809 if (it->bidi_it.first_elt
3810 && it->bidi_it.charpos < SCHARS (it->string))
3811 bidi_paragraph_init (it->paragraph_embedding,
3812 &it->bidi_it, 1);
3813 /* Bidi-iterate out of the invisible text. */
3816 bidi_move_to_visually_next (&it->bidi_it);
3818 while (oldpos <= it->bidi_it.charpos
3819 && it->bidi_it.charpos < endpos);
3821 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
3822 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
3823 if (IT_CHARPOS (*it) >= endpos)
3824 it->prev_stop = endpos;
3826 else
3828 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3829 compute_string_pos (&it->current.string_pos, old, it->string);
3832 else
3834 /* The rest of the string is invisible. If this is an
3835 overlay string, proceed with the next overlay string
3836 or whatever comes and return a character from there. */
3837 if (it->current.overlay_string_index >= 0)
3839 next_overlay_string (it);
3840 /* Don't check for overlay strings when we just
3841 finished processing them. */
3842 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3844 else
3846 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3847 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3852 else
3854 int invis_p;
3855 EMACS_INT newpos, next_stop, start_charpos, tem;
3856 Lisp_Object pos, prop, overlay;
3858 /* First of all, is there invisible text at this position? */
3859 tem = start_charpos = IT_CHARPOS (*it);
3860 pos = make_number (tem);
3861 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3862 &overlay);
3863 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3865 /* If we are on invisible text, skip over it. */
3866 if (invis_p && start_charpos < it->end_charpos)
3868 /* Record whether we have to display an ellipsis for the
3869 invisible text. */
3870 int display_ellipsis_p = invis_p == 2;
3872 handled = HANDLED_RECOMPUTE_PROPS;
3874 /* Loop skipping over invisible text. The loop is left at
3875 ZV or with IT on the first char being visible again. */
3878 /* Try to skip some invisible text. Return value is the
3879 position reached which can be equal to where we start
3880 if there is nothing invisible there. This skips both
3881 over invisible text properties and overlays with
3882 invisible property. */
3883 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3885 /* If we skipped nothing at all we weren't at invisible
3886 text in the first place. If everything to the end of
3887 the buffer was skipped, end the loop. */
3888 if (newpos == tem || newpos >= ZV)
3889 invis_p = 0;
3890 else
3892 /* We skipped some characters but not necessarily
3893 all there are. Check if we ended up on visible
3894 text. Fget_char_property returns the property of
3895 the char before the given position, i.e. if we
3896 get invis_p = 0, this means that the char at
3897 newpos is visible. */
3898 pos = make_number (newpos);
3899 prop = Fget_char_property (pos, Qinvisible, it->window);
3900 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3903 /* If we ended up on invisible text, proceed to
3904 skip starting with next_stop. */
3905 if (invis_p)
3906 tem = next_stop;
3908 /* If there are adjacent invisible texts, don't lose the
3909 second one's ellipsis. */
3910 if (invis_p == 2)
3911 display_ellipsis_p = 1;
3913 while (invis_p);
3915 /* The position newpos is now either ZV or on visible text. */
3916 if (it->bidi_p && newpos < ZV)
3918 /* With bidi iteration, the region of invisible text
3919 could start and/or end in the middle of a non-base
3920 embedding level. Therefore, we need to skip
3921 invisible text using the bidi iterator, starting at
3922 IT's current position, until we find ourselves
3923 outside the invisible text. Skipping invisible text
3924 _after_ bidi iteration avoids affecting the visual
3925 order of the displayed text when invisible properties
3926 are added or removed. */
3927 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
3929 /* If we were `reseat'ed to a new paragraph,
3930 determine the paragraph base direction. We need
3931 to do it now because next_element_from_buffer may
3932 not have a chance to do it, if we are going to
3933 skip any text at the beginning, which resets the
3934 FIRST_ELT flag. */
3935 bidi_paragraph_init (it->paragraph_embedding,
3936 &it->bidi_it, 1);
3940 bidi_move_to_visually_next (&it->bidi_it);
3942 while (it->stop_charpos <= it->bidi_it.charpos
3943 && it->bidi_it.charpos < newpos);
3944 IT_CHARPOS (*it) = it->bidi_it.charpos;
3945 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3946 /* If we overstepped NEWPOS, record its position in the
3947 iterator, so that we skip invisible text if later the
3948 bidi iteration lands us in the invisible region
3949 again. */
3950 if (IT_CHARPOS (*it) >= newpos)
3951 it->prev_stop = newpos;
3953 else
3955 IT_CHARPOS (*it) = newpos;
3956 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3959 /* If there are before-strings at the start of invisible
3960 text, and the text is invisible because of a text
3961 property, arrange to show before-strings because 20.x did
3962 it that way. (If the text is invisible because of an
3963 overlay property instead of a text property, this is
3964 already handled in the overlay code.) */
3965 if (NILP (overlay)
3966 && get_overlay_strings (it, it->stop_charpos))
3968 handled = HANDLED_RECOMPUTE_PROPS;
3969 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3971 else if (display_ellipsis_p)
3973 /* Make sure that the glyphs of the ellipsis will get
3974 correct `charpos' values. If we would not update
3975 it->position here, the glyphs would belong to the
3976 last visible character _before_ the invisible
3977 text, which confuses `set_cursor_from_row'.
3979 We use the last invisible position instead of the
3980 first because this way the cursor is always drawn on
3981 the first "." of the ellipsis, whenever PT is inside
3982 the invisible text. Otherwise the cursor would be
3983 placed _after_ the ellipsis when the point is after the
3984 first invisible character. */
3985 if (!STRINGP (it->object))
3987 it->position.charpos = newpos - 1;
3988 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3990 it->ellipsis_p = 1;
3991 /* Let the ellipsis display before
3992 considering any properties of the following char.
3993 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3994 handled = HANDLED_RETURN;
3999 return handled;
4003 /* Make iterator IT return `...' next.
4004 Replaces LEN characters from buffer. */
4006 static void
4007 setup_for_ellipsis (struct it *it, int len)
4009 /* Use the display table definition for `...'. Invalid glyphs
4010 will be handled by the method returning elements from dpvec. */
4011 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4013 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4014 it->dpvec = v->contents;
4015 it->dpend = v->contents + v->header.size;
4017 else
4019 /* Default `...'. */
4020 it->dpvec = default_invis_vector;
4021 it->dpend = default_invis_vector + 3;
4024 it->dpvec_char_len = len;
4025 it->current.dpvec_index = 0;
4026 it->dpvec_face_id = -1;
4028 /* Remember the current face id in case glyphs specify faces.
4029 IT's face is restored in set_iterator_to_next.
4030 saved_face_id was set to preceding char's face in handle_stop. */
4031 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4032 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4034 it->method = GET_FROM_DISPLAY_VECTOR;
4035 it->ellipsis_p = 1;
4040 /***********************************************************************
4041 'display' property
4042 ***********************************************************************/
4044 /* Set up iterator IT from `display' property at its current position.
4045 Called from handle_stop.
4046 We return HANDLED_RETURN if some part of the display property
4047 overrides the display of the buffer text itself.
4048 Otherwise we return HANDLED_NORMALLY. */
4050 static enum prop_handled
4051 handle_display_prop (struct it *it)
4053 Lisp_Object propval, object, overlay;
4054 struct text_pos *position;
4055 EMACS_INT bufpos;
4056 /* Nonzero if some property replaces the display of the text itself. */
4057 int display_replaced_p = 0;
4059 if (STRINGP (it->string))
4061 object = it->string;
4062 position = &it->current.string_pos;
4063 bufpos = CHARPOS (it->current.pos);
4065 else
4067 XSETWINDOW (object, it->w);
4068 position = &it->current.pos;
4069 bufpos = CHARPOS (*position);
4072 /* Reset those iterator values set from display property values. */
4073 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4074 it->space_width = Qnil;
4075 it->font_height = Qnil;
4076 it->voffset = 0;
4078 /* We don't support recursive `display' properties, i.e. string
4079 values that have a string `display' property, that have a string
4080 `display' property etc. */
4081 if (!it->string_from_display_prop_p)
4082 it->area = TEXT_AREA;
4084 propval = get_char_property_and_overlay (make_number (position->charpos),
4085 Qdisplay, object, &overlay);
4086 if (NILP (propval))
4087 return HANDLED_NORMALLY;
4088 /* Now OVERLAY is the overlay that gave us this property, or nil
4089 if it was a text property. */
4091 if (!STRINGP (it->string))
4092 object = it->w->buffer;
4094 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4095 position, bufpos,
4096 FRAME_WINDOW_P (it->f));
4098 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4101 /* Subroutine of handle_display_prop. Returns non-zero if the display
4102 specification in SPEC is a replacing specification, i.e. it would
4103 replace the text covered by `display' property with something else,
4104 such as an image or a display string.
4106 See handle_single_display_spec for documentation of arguments.
4107 frame_window_p is non-zero if the window being redisplayed is on a
4108 GUI frame; this argument is used only if IT is NULL, see below.
4110 IT can be NULL, if this is called by the bidi reordering code
4111 through compute_display_string_pos, which see. In that case, this
4112 function only examines SPEC, but does not otherwise "handle" it, in
4113 the sense that it doesn't set up members of IT from the display
4114 spec. */
4115 static int
4116 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4117 Lisp_Object overlay, struct text_pos *position,
4118 EMACS_INT bufpos, int frame_window_p)
4120 int replacing_p = 0;
4122 if (CONSP (spec)
4123 /* Simple specerties. */
4124 && !EQ (XCAR (spec), Qimage)
4125 && !EQ (XCAR (spec), Qspace)
4126 && !EQ (XCAR (spec), Qwhen)
4127 && !EQ (XCAR (spec), Qslice)
4128 && !EQ (XCAR (spec), Qspace_width)
4129 && !EQ (XCAR (spec), Qheight)
4130 && !EQ (XCAR (spec), Qraise)
4131 /* Marginal area specifications. */
4132 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4133 && !EQ (XCAR (spec), Qleft_fringe)
4134 && !EQ (XCAR (spec), Qright_fringe)
4135 && !NILP (XCAR (spec)))
4137 for (; CONSP (spec); spec = XCDR (spec))
4139 if (handle_single_display_spec (it, XCAR (spec), object, overlay,
4140 position, bufpos, replacing_p,
4141 frame_window_p))
4143 replacing_p = 1;
4144 /* If some text in a string is replaced, `position' no
4145 longer points to the position of `object'. */
4146 if (!it || STRINGP (object))
4147 break;
4151 else if (VECTORP (spec))
4153 int i;
4154 for (i = 0; i < ASIZE (spec); ++i)
4155 if (handle_single_display_spec (it, AREF (spec, i), object, overlay,
4156 position, bufpos, replacing_p,
4157 frame_window_p))
4159 replacing_p = 1;
4160 /* If some text in a string is replaced, `position' no
4161 longer points to the position of `object'. */
4162 if (!it || STRINGP (object))
4163 break;
4166 else
4168 if (handle_single_display_spec (it, spec, object, overlay,
4169 position, bufpos, 0, frame_window_p))
4170 replacing_p = 1;
4173 return replacing_p;
4176 /* Value is the position of the end of the `display' property starting
4177 at START_POS in OBJECT. */
4179 static struct text_pos
4180 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4182 Lisp_Object end;
4183 struct text_pos end_pos;
4185 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4186 Qdisplay, object, Qnil);
4187 CHARPOS (end_pos) = XFASTINT (end);
4188 if (STRINGP (object))
4189 compute_string_pos (&end_pos, start_pos, it->string);
4190 else
4191 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4193 return end_pos;
4197 /* Set up IT from a single `display' property specification SPEC. OBJECT
4198 is the object in which the `display' property was found. *POSITION
4199 is the position in OBJECT at which the `display' property was found.
4200 BUFPOS is the buffer position of OBJECT (different from POSITION if
4201 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4202 previously saw a display specification which already replaced text
4203 display with something else, for example an image; we ignore such
4204 properties after the first one has been processed.
4206 OVERLAY is the overlay this `display' property came from,
4207 or nil if it was a text property.
4209 If SPEC is a `space' or `image' specification, and in some other
4210 cases too, set *POSITION to the position where the `display'
4211 property ends.
4213 If IT is NULL, only examine the property specification in SPEC, but
4214 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4215 is intended to be displayed in a window on a GUI frame.
4217 Value is non-zero if something was found which replaces the display
4218 of buffer or string text. */
4220 static int
4221 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4222 Lisp_Object overlay, struct text_pos *position,
4223 EMACS_INT bufpos, int display_replaced_p,
4224 int frame_window_p)
4226 Lisp_Object form;
4227 Lisp_Object location, value;
4228 struct text_pos start_pos = *position;
4229 int valid_p;
4231 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4232 If the result is non-nil, use VALUE instead of SPEC. */
4233 form = Qt;
4234 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4236 spec = XCDR (spec);
4237 if (!CONSP (spec))
4238 return 0;
4239 form = XCAR (spec);
4240 spec = XCDR (spec);
4243 if (!NILP (form) && !EQ (form, Qt))
4245 int count = SPECPDL_INDEX ();
4246 struct gcpro gcpro1;
4248 /* Bind `object' to the object having the `display' property, a
4249 buffer or string. Bind `position' to the position in the
4250 object where the property was found, and `buffer-position'
4251 to the current position in the buffer. */
4253 if (NILP (object))
4254 XSETBUFFER (object, current_buffer);
4255 specbind (Qobject, object);
4256 specbind (Qposition, make_number (CHARPOS (*position)));
4257 specbind (Qbuffer_position, make_number (bufpos));
4258 GCPRO1 (form);
4259 form = safe_eval (form);
4260 UNGCPRO;
4261 unbind_to (count, Qnil);
4264 if (NILP (form))
4265 return 0;
4267 /* Handle `(height HEIGHT)' specifications. */
4268 if (CONSP (spec)
4269 && EQ (XCAR (spec), Qheight)
4270 && CONSP (XCDR (spec)))
4272 if (it)
4274 if (!FRAME_WINDOW_P (it->f))
4275 return 0;
4277 it->font_height = XCAR (XCDR (spec));
4278 if (!NILP (it->font_height))
4280 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4281 int new_height = -1;
4283 if (CONSP (it->font_height)
4284 && (EQ (XCAR (it->font_height), Qplus)
4285 || EQ (XCAR (it->font_height), Qminus))
4286 && CONSP (XCDR (it->font_height))
4287 && INTEGERP (XCAR (XCDR (it->font_height))))
4289 /* `(+ N)' or `(- N)' where N is an integer. */
4290 int steps = XINT (XCAR (XCDR (it->font_height)));
4291 if (EQ (XCAR (it->font_height), Qplus))
4292 steps = - steps;
4293 it->face_id = smaller_face (it->f, it->face_id, steps);
4295 else if (FUNCTIONP (it->font_height))
4297 /* Call function with current height as argument.
4298 Value is the new height. */
4299 Lisp_Object height;
4300 height = safe_call1 (it->font_height,
4301 face->lface[LFACE_HEIGHT_INDEX]);
4302 if (NUMBERP (height))
4303 new_height = XFLOATINT (height);
4305 else if (NUMBERP (it->font_height))
4307 /* Value is a multiple of the canonical char height. */
4308 struct face *f;
4310 f = FACE_FROM_ID (it->f,
4311 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4312 new_height = (XFLOATINT (it->font_height)
4313 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4315 else
4317 /* Evaluate IT->font_height with `height' bound to the
4318 current specified height to get the new height. */
4319 int count = SPECPDL_INDEX ();
4321 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4322 value = safe_eval (it->font_height);
4323 unbind_to (count, Qnil);
4325 if (NUMBERP (value))
4326 new_height = XFLOATINT (value);
4329 if (new_height > 0)
4330 it->face_id = face_with_height (it->f, it->face_id, new_height);
4334 return 0;
4337 /* Handle `(space-width WIDTH)'. */
4338 if (CONSP (spec)
4339 && EQ (XCAR (spec), Qspace_width)
4340 && CONSP (XCDR (spec)))
4342 if (it)
4344 if (!FRAME_WINDOW_P (it->f))
4345 return 0;
4347 value = XCAR (XCDR (spec));
4348 if (NUMBERP (value) && XFLOATINT (value) > 0)
4349 it->space_width = value;
4352 return 0;
4355 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4356 if (CONSP (spec)
4357 && EQ (XCAR (spec), Qslice))
4359 Lisp_Object tem;
4361 if (it)
4363 if (!FRAME_WINDOW_P (it->f))
4364 return 0;
4366 if (tem = XCDR (spec), CONSP (tem))
4368 it->slice.x = XCAR (tem);
4369 if (tem = XCDR (tem), CONSP (tem))
4371 it->slice.y = XCAR (tem);
4372 if (tem = XCDR (tem), CONSP (tem))
4374 it->slice.width = XCAR (tem);
4375 if (tem = XCDR (tem), CONSP (tem))
4376 it->slice.height = XCAR (tem);
4382 return 0;
4385 /* Handle `(raise FACTOR)'. */
4386 if (CONSP (spec)
4387 && EQ (XCAR (spec), Qraise)
4388 && CONSP (XCDR (spec)))
4390 if (it)
4392 if (!FRAME_WINDOW_P (it->f))
4393 return 0;
4395 #ifdef HAVE_WINDOW_SYSTEM
4396 value = XCAR (XCDR (spec));
4397 if (NUMBERP (value))
4399 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4400 it->voffset = - (XFLOATINT (value)
4401 * (FONT_HEIGHT (face->font)));
4403 #endif /* HAVE_WINDOW_SYSTEM */
4406 return 0;
4409 /* Don't handle the other kinds of display specifications
4410 inside a string that we got from a `display' property. */
4411 if (it && it->string_from_display_prop_p)
4412 return 0;
4414 /* Characters having this form of property are not displayed, so
4415 we have to find the end of the property. */
4416 if (it)
4418 start_pos = *position;
4419 *position = display_prop_end (it, object, start_pos);
4421 value = Qnil;
4423 /* Stop the scan at that end position--we assume that all
4424 text properties change there. */
4425 if (it)
4426 it->stop_charpos = position->charpos;
4428 /* Handle `(left-fringe BITMAP [FACE])'
4429 and `(right-fringe BITMAP [FACE])'. */
4430 if (CONSP (spec)
4431 && (EQ (XCAR (spec), Qleft_fringe)
4432 || EQ (XCAR (spec), Qright_fringe))
4433 && CONSP (XCDR (spec)))
4435 int fringe_bitmap;
4437 if (it)
4439 if (!FRAME_WINDOW_P (it->f))
4440 /* If we return here, POSITION has been advanced
4441 across the text with this property. */
4442 return 0;
4444 else if (!frame_window_p)
4445 return 0;
4447 #ifdef HAVE_WINDOW_SYSTEM
4448 value = XCAR (XCDR (spec));
4449 if (!SYMBOLP (value)
4450 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4451 /* If we return here, POSITION has been advanced
4452 across the text with this property. */
4453 return 0;
4455 if (it)
4457 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4459 if (CONSP (XCDR (XCDR (spec))))
4461 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4462 int face_id2 = lookup_derived_face (it->f, face_name,
4463 FRINGE_FACE_ID, 0);
4464 if (face_id2 >= 0)
4465 face_id = face_id2;
4468 /* Save current settings of IT so that we can restore them
4469 when we are finished with the glyph property value. */
4470 push_it (it, position);
4472 it->area = TEXT_AREA;
4473 it->what = IT_IMAGE;
4474 it->image_id = -1; /* no image */
4475 it->position = start_pos;
4476 it->object = NILP (object) ? it->w->buffer : object;
4477 it->method = GET_FROM_IMAGE;
4478 it->from_overlay = Qnil;
4479 it->face_id = face_id;
4480 it->from_disp_prop_p = 1;
4482 /* Say that we haven't consumed the characters with
4483 `display' property yet. The call to pop_it in
4484 set_iterator_to_next will clean this up. */
4485 *position = start_pos;
4487 if (EQ (XCAR (spec), Qleft_fringe))
4489 it->left_user_fringe_bitmap = fringe_bitmap;
4490 it->left_user_fringe_face_id = face_id;
4492 else
4494 it->right_user_fringe_bitmap = fringe_bitmap;
4495 it->right_user_fringe_face_id = face_id;
4498 #endif /* HAVE_WINDOW_SYSTEM */
4499 return 1;
4502 /* Prepare to handle `((margin left-margin) ...)',
4503 `((margin right-margin) ...)' and `((margin nil) ...)'
4504 prefixes for display specifications. */
4505 location = Qunbound;
4506 if (CONSP (spec) && CONSP (XCAR (spec)))
4508 Lisp_Object tem;
4510 value = XCDR (spec);
4511 if (CONSP (value))
4512 value = XCAR (value);
4514 tem = XCAR (spec);
4515 if (EQ (XCAR (tem), Qmargin)
4516 && (tem = XCDR (tem),
4517 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4518 (NILP (tem)
4519 || EQ (tem, Qleft_margin)
4520 || EQ (tem, Qright_margin))))
4521 location = tem;
4524 if (EQ (location, Qunbound))
4526 location = Qnil;
4527 value = spec;
4530 /* After this point, VALUE is the property after any
4531 margin prefix has been stripped. It must be a string,
4532 an image specification, or `(space ...)'.
4534 LOCATION specifies where to display: `left-margin',
4535 `right-margin' or nil. */
4537 valid_p = (STRINGP (value)
4538 #ifdef HAVE_WINDOW_SYSTEM
4539 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4540 && valid_image_p (value))
4541 #endif /* not HAVE_WINDOW_SYSTEM */
4542 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4544 if (valid_p && !display_replaced_p)
4546 if (!it)
4547 return 1;
4549 /* Save current settings of IT so that we can restore them
4550 when we are finished with the glyph property value. */
4551 push_it (it, position);
4552 it->from_overlay = overlay;
4553 it->from_disp_prop_p = 1;
4555 if (NILP (location))
4556 it->area = TEXT_AREA;
4557 else if (EQ (location, Qleft_margin))
4558 it->area = LEFT_MARGIN_AREA;
4559 else
4560 it->area = RIGHT_MARGIN_AREA;
4562 if (STRINGP (value))
4564 it->string = value;
4565 it->multibyte_p = STRING_MULTIBYTE (it->string);
4566 it->current.overlay_string_index = -1;
4567 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4568 it->end_charpos = it->string_nchars = SCHARS (it->string);
4569 it->method = GET_FROM_STRING;
4570 it->stop_charpos = 0;
4571 it->prev_stop = 0;
4572 it->base_level_stop = 0;
4573 it->string_from_display_prop_p = 1;
4574 /* Say that we haven't consumed the characters with
4575 `display' property yet. The call to pop_it in
4576 set_iterator_to_next will clean this up. */
4577 if (BUFFERP (object))
4578 *position = start_pos;
4580 /* Force paragraph direction to be that of the parent
4581 object. If the parent object's paragraph direction is
4582 not yet determined, default to L2R. */
4583 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4584 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4585 else
4586 it->paragraph_embedding = L2R;
4588 /* Set up the bidi iterator for this display string. */
4589 if (it->bidi_p)
4591 it->bidi_it.string.lstring = it->string;
4592 it->bidi_it.string.s = NULL;
4593 it->bidi_it.string.schars = it->end_charpos;
4594 it->bidi_it.string.bufpos = bufpos;
4595 it->bidi_it.string.from_disp_str = 1;
4596 it->bidi_it.string.unibyte = !it->multibyte_p;
4597 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4600 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4602 it->method = GET_FROM_STRETCH;
4603 it->object = value;
4604 *position = it->position = start_pos;
4606 #ifdef HAVE_WINDOW_SYSTEM
4607 else
4609 it->what = IT_IMAGE;
4610 it->image_id = lookup_image (it->f, value);
4611 it->position = start_pos;
4612 it->object = NILP (object) ? it->w->buffer : object;
4613 it->method = GET_FROM_IMAGE;
4615 /* Say that we haven't consumed the characters with
4616 `display' property yet. The call to pop_it in
4617 set_iterator_to_next will clean this up. */
4618 *position = start_pos;
4620 #endif /* HAVE_WINDOW_SYSTEM */
4622 return 1;
4625 /* Invalid property or property not supported. Restore
4626 POSITION to what it was before. */
4627 *position = start_pos;
4628 return 0;
4631 /* Check if PROP is a display property value whose text should be
4632 treated as intangible. OVERLAY is the overlay from which PROP
4633 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4634 specify the buffer position covered by PROP. */
4637 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4638 EMACS_INT charpos, EMACS_INT bytepos)
4640 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4641 struct text_pos position;
4643 SET_TEXT_POS (position, charpos, bytepos);
4644 return handle_display_spec (NULL, prop, Qnil, overlay,
4645 &position, charpos, frame_window_p);
4649 /* Return 1 if PROP is a display sub-property value containing STRING.
4651 Implementation note: this and the following function are really
4652 special cases of handle_display_spec and
4653 handle_single_display_spec, and should ideally use the same code.
4654 Until they do, these two pairs must be consistent and must be
4655 modified in sync. */
4657 static int
4658 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4660 if (EQ (string, prop))
4661 return 1;
4663 /* Skip over `when FORM'. */
4664 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4666 prop = XCDR (prop);
4667 if (!CONSP (prop))
4668 return 0;
4669 /* Actually, the condition following `when' should be eval'ed,
4670 like handle_single_display_spec does, and we should return
4671 zero if it evaluates to nil. However, this function is
4672 called only when the buffer was already displayed and some
4673 glyph in the glyph matrix was found to come from a display
4674 string. Therefore, the condition was already evaluated, and
4675 the result was non-nil, otherwise the display string wouldn't
4676 have been displayed and we would have never been called for
4677 this property. Thus, we can skip the evaluation and assume
4678 its result is non-nil. */
4679 prop = XCDR (prop);
4682 if (CONSP (prop))
4683 /* Skip over `margin LOCATION'. */
4684 if (EQ (XCAR (prop), Qmargin))
4686 prop = XCDR (prop);
4687 if (!CONSP (prop))
4688 return 0;
4690 prop = XCDR (prop);
4691 if (!CONSP (prop))
4692 return 0;
4695 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
4699 /* Return 1 if STRING appears in the `display' property PROP. */
4701 static int
4702 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4704 if (CONSP (prop)
4705 && !EQ (XCAR (prop), Qwhen)
4706 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
4708 /* A list of sub-properties. */
4709 while (CONSP (prop))
4711 if (single_display_spec_string_p (XCAR (prop), string))
4712 return 1;
4713 prop = XCDR (prop);
4716 else if (VECTORP (prop))
4718 /* A vector of sub-properties. */
4719 int i;
4720 for (i = 0; i < ASIZE (prop); ++i)
4721 if (single_display_spec_string_p (AREF (prop, i), string))
4722 return 1;
4724 else
4725 return single_display_spec_string_p (prop, string);
4727 return 0;
4730 /* Look for STRING in overlays and text properties in the current
4731 buffer, between character positions FROM and TO (excluding TO).
4732 BACK_P non-zero means look back (in this case, TO is supposed to be
4733 less than FROM).
4734 Value is the first character position where STRING was found, or
4735 zero if it wasn't found before hitting TO.
4737 This function may only use code that doesn't eval because it is
4738 called asynchronously from note_mouse_highlight. */
4740 static EMACS_INT
4741 string_buffer_position_lim (Lisp_Object string,
4742 EMACS_INT from, EMACS_INT to, int back_p)
4744 Lisp_Object limit, prop, pos;
4745 int found = 0;
4747 pos = make_number (from);
4749 if (!back_p) /* looking forward */
4751 limit = make_number (min (to, ZV));
4752 while (!found && !EQ (pos, limit))
4754 prop = Fget_char_property (pos, Qdisplay, Qnil);
4755 if (!NILP (prop) && display_prop_string_p (prop, string))
4756 found = 1;
4757 else
4758 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4759 limit);
4762 else /* looking back */
4764 limit = make_number (max (to, BEGV));
4765 while (!found && !EQ (pos, limit))
4767 prop = Fget_char_property (pos, Qdisplay, Qnil);
4768 if (!NILP (prop) && display_prop_string_p (prop, string))
4769 found = 1;
4770 else
4771 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4772 limit);
4776 return found ? XINT (pos) : 0;
4779 /* Determine which buffer position in current buffer STRING comes from.
4780 AROUND_CHARPOS is an approximate position where it could come from.
4781 Value is the buffer position or 0 if it couldn't be determined.
4783 This function is necessary because we don't record buffer positions
4784 in glyphs generated from strings (to keep struct glyph small).
4785 This function may only use code that doesn't eval because it is
4786 called asynchronously from note_mouse_highlight. */
4788 static EMACS_INT
4789 string_buffer_position (Lisp_Object string, EMACS_INT around_charpos)
4791 const int MAX_DISTANCE = 1000;
4792 EMACS_INT found = string_buffer_position_lim (string, around_charpos,
4793 around_charpos + MAX_DISTANCE,
4796 if (!found)
4797 found = string_buffer_position_lim (string, around_charpos,
4798 around_charpos - MAX_DISTANCE, 1);
4799 return found;
4804 /***********************************************************************
4805 `composition' property
4806 ***********************************************************************/
4808 /* Set up iterator IT from `composition' property at its current
4809 position. Called from handle_stop. */
4811 static enum prop_handled
4812 handle_composition_prop (struct it *it)
4814 Lisp_Object prop, string;
4815 EMACS_INT pos, pos_byte, start, end;
4817 if (STRINGP (it->string))
4819 unsigned char *s;
4821 pos = IT_STRING_CHARPOS (*it);
4822 pos_byte = IT_STRING_BYTEPOS (*it);
4823 string = it->string;
4824 s = SDATA (string) + pos_byte;
4825 it->c = STRING_CHAR (s);
4827 else
4829 pos = IT_CHARPOS (*it);
4830 pos_byte = IT_BYTEPOS (*it);
4831 string = Qnil;
4832 it->c = FETCH_CHAR (pos_byte);
4835 /* If there's a valid composition and point is not inside of the
4836 composition (in the case that the composition is from the current
4837 buffer), draw a glyph composed from the composition components. */
4838 if (find_composition (pos, -1, &start, &end, &prop, string)
4839 && COMPOSITION_VALID_P (start, end, prop)
4840 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4842 if (start < pos)
4843 /* As we can't handle this situation (perhaps font-lock added
4844 a new composition), we just return here hoping that next
4845 redisplay will detect this composition much earlier. */
4846 return HANDLED_NORMALLY;
4847 if (start != pos)
4849 if (STRINGP (it->string))
4850 pos_byte = string_char_to_byte (it->string, start);
4851 else
4852 pos_byte = CHAR_TO_BYTE (start);
4854 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4855 prop, string);
4857 if (it->cmp_it.id >= 0)
4859 it->cmp_it.ch = -1;
4860 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4861 it->cmp_it.nglyphs = -1;
4865 return HANDLED_NORMALLY;
4870 /***********************************************************************
4871 Overlay strings
4872 ***********************************************************************/
4874 /* The following structure is used to record overlay strings for
4875 later sorting in load_overlay_strings. */
4877 struct overlay_entry
4879 Lisp_Object overlay;
4880 Lisp_Object string;
4881 int priority;
4882 int after_string_p;
4886 /* Set up iterator IT from overlay strings at its current position.
4887 Called from handle_stop. */
4889 static enum prop_handled
4890 handle_overlay_change (struct it *it)
4892 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4893 return HANDLED_RECOMPUTE_PROPS;
4894 else
4895 return HANDLED_NORMALLY;
4899 /* Set up the next overlay string for delivery by IT, if there is an
4900 overlay string to deliver. Called by set_iterator_to_next when the
4901 end of the current overlay string is reached. If there are more
4902 overlay strings to display, IT->string and
4903 IT->current.overlay_string_index are set appropriately here.
4904 Otherwise IT->string is set to nil. */
4906 static void
4907 next_overlay_string (struct it *it)
4909 ++it->current.overlay_string_index;
4910 if (it->current.overlay_string_index == it->n_overlay_strings)
4912 /* No more overlay strings. Restore IT's settings to what
4913 they were before overlay strings were processed, and
4914 continue to deliver from current_buffer. */
4916 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4917 pop_it (it);
4918 xassert (it->sp > 0
4919 || (NILP (it->string)
4920 && it->method == GET_FROM_BUFFER
4921 && it->stop_charpos >= BEGV
4922 && it->stop_charpos <= it->end_charpos));
4923 it->current.overlay_string_index = -1;
4924 it->n_overlay_strings = 0;
4925 it->overlay_strings_charpos = -1;
4927 /* If we're at the end of the buffer, record that we have
4928 processed the overlay strings there already, so that
4929 next_element_from_buffer doesn't try it again. */
4930 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4931 it->overlay_strings_at_end_processed_p = 1;
4933 else
4935 /* There are more overlay strings to process. If
4936 IT->current.overlay_string_index has advanced to a position
4937 where we must load IT->overlay_strings with more strings, do
4938 it. We must load at the IT->overlay_strings_charpos where
4939 IT->n_overlay_strings was originally computed; when invisible
4940 text is present, this might not be IT_CHARPOS (Bug#7016). */
4941 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4943 if (it->current.overlay_string_index && i == 0)
4944 load_overlay_strings (it, it->overlay_strings_charpos);
4946 /* Initialize IT to deliver display elements from the overlay
4947 string. */
4948 it->string = it->overlay_strings[i];
4949 it->multibyte_p = STRING_MULTIBYTE (it->string);
4950 SET_TEXT_POS (it->current.string_pos, 0, 0);
4951 it->method = GET_FROM_STRING;
4952 it->stop_charpos = 0;
4953 if (it->cmp_it.stop_pos >= 0)
4954 it->cmp_it.stop_pos = 0;
4955 it->prev_stop = 0;
4956 it->base_level_stop = 0;
4958 /* Set up the bidi iterator for this overlay string. */
4959 if (it->bidi_p)
4961 it->bidi_it.string.lstring = it->string;
4962 it->bidi_it.string.s = NULL;
4963 it->bidi_it.string.schars = SCHARS (it->string);
4964 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
4965 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
4966 it->bidi_it.string.unibyte = !it->multibyte_p;
4967 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4971 CHECK_IT (it);
4975 /* Compare two overlay_entry structures E1 and E2. Used as a
4976 comparison function for qsort in load_overlay_strings. Overlay
4977 strings for the same position are sorted so that
4979 1. All after-strings come in front of before-strings, except
4980 when they come from the same overlay.
4982 2. Within after-strings, strings are sorted so that overlay strings
4983 from overlays with higher priorities come first.
4985 2. Within before-strings, strings are sorted so that overlay
4986 strings from overlays with higher priorities come last.
4988 Value is analogous to strcmp. */
4991 static int
4992 compare_overlay_entries (const void *e1, const void *e2)
4994 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4995 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4996 int result;
4998 if (entry1->after_string_p != entry2->after_string_p)
5000 /* Let after-strings appear in front of before-strings if
5001 they come from different overlays. */
5002 if (EQ (entry1->overlay, entry2->overlay))
5003 result = entry1->after_string_p ? 1 : -1;
5004 else
5005 result = entry1->after_string_p ? -1 : 1;
5007 else if (entry1->after_string_p)
5008 /* After-strings sorted in order of decreasing priority. */
5009 result = entry2->priority - entry1->priority;
5010 else
5011 /* Before-strings sorted in order of increasing priority. */
5012 result = entry1->priority - entry2->priority;
5014 return result;
5018 /* Load the vector IT->overlay_strings with overlay strings from IT's
5019 current buffer position, or from CHARPOS if that is > 0. Set
5020 IT->n_overlays to the total number of overlay strings found.
5022 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5023 a time. On entry into load_overlay_strings,
5024 IT->current.overlay_string_index gives the number of overlay
5025 strings that have already been loaded by previous calls to this
5026 function.
5028 IT->add_overlay_start contains an additional overlay start
5029 position to consider for taking overlay strings from, if non-zero.
5030 This position comes into play when the overlay has an `invisible'
5031 property, and both before and after-strings. When we've skipped to
5032 the end of the overlay, because of its `invisible' property, we
5033 nevertheless want its before-string to appear.
5034 IT->add_overlay_start will contain the overlay start position
5035 in this case.
5037 Overlay strings are sorted so that after-string strings come in
5038 front of before-string strings. Within before and after-strings,
5039 strings are sorted by overlay priority. See also function
5040 compare_overlay_entries. */
5042 static void
5043 load_overlay_strings (struct it *it, EMACS_INT charpos)
5045 Lisp_Object overlay, window, str, invisible;
5046 struct Lisp_Overlay *ov;
5047 EMACS_INT start, end;
5048 int size = 20;
5049 int n = 0, i, j, invis_p;
5050 struct overlay_entry *entries
5051 = (struct overlay_entry *) alloca (size * sizeof *entries);
5053 if (charpos <= 0)
5054 charpos = IT_CHARPOS (*it);
5056 /* Append the overlay string STRING of overlay OVERLAY to vector
5057 `entries' which has size `size' and currently contains `n'
5058 elements. AFTER_P non-zero means STRING is an after-string of
5059 OVERLAY. */
5060 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5061 do \
5063 Lisp_Object priority; \
5065 if (n == size) \
5067 int new_size = 2 * size; \
5068 struct overlay_entry *old = entries; \
5069 entries = \
5070 (struct overlay_entry *) alloca (new_size \
5071 * sizeof *entries); \
5072 memcpy (entries, old, size * sizeof *entries); \
5073 size = new_size; \
5076 entries[n].string = (STRING); \
5077 entries[n].overlay = (OVERLAY); \
5078 priority = Foverlay_get ((OVERLAY), Qpriority); \
5079 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5080 entries[n].after_string_p = (AFTER_P); \
5081 ++n; \
5083 while (0)
5085 /* Process overlay before the overlay center. */
5086 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5088 XSETMISC (overlay, ov);
5089 xassert (OVERLAYP (overlay));
5090 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5091 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5093 if (end < charpos)
5094 break;
5096 /* Skip this overlay if it doesn't start or end at IT's current
5097 position. */
5098 if (end != charpos && start != charpos)
5099 continue;
5101 /* Skip this overlay if it doesn't apply to IT->w. */
5102 window = Foverlay_get (overlay, Qwindow);
5103 if (WINDOWP (window) && XWINDOW (window) != it->w)
5104 continue;
5106 /* If the text ``under'' the overlay is invisible, both before-
5107 and after-strings from this overlay are visible; start and
5108 end position are indistinguishable. */
5109 invisible = Foverlay_get (overlay, Qinvisible);
5110 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5112 /* If overlay has a non-empty before-string, record it. */
5113 if ((start == charpos || (end == charpos && invis_p))
5114 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5115 && SCHARS (str))
5116 RECORD_OVERLAY_STRING (overlay, str, 0);
5118 /* If overlay has a non-empty after-string, record it. */
5119 if ((end == charpos || (start == charpos && invis_p))
5120 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5121 && SCHARS (str))
5122 RECORD_OVERLAY_STRING (overlay, str, 1);
5125 /* Process overlays after the overlay center. */
5126 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5128 XSETMISC (overlay, ov);
5129 xassert (OVERLAYP (overlay));
5130 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5131 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5133 if (start > charpos)
5134 break;
5136 /* Skip this overlay if it doesn't start or end at IT's current
5137 position. */
5138 if (end != charpos && start != charpos)
5139 continue;
5141 /* Skip this overlay if it doesn't apply to IT->w. */
5142 window = Foverlay_get (overlay, Qwindow);
5143 if (WINDOWP (window) && XWINDOW (window) != it->w)
5144 continue;
5146 /* If the text ``under'' the overlay is invisible, it has a zero
5147 dimension, and both before- and after-strings apply. */
5148 invisible = Foverlay_get (overlay, Qinvisible);
5149 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5151 /* If overlay has a non-empty before-string, record it. */
5152 if ((start == charpos || (end == charpos && invis_p))
5153 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5154 && SCHARS (str))
5155 RECORD_OVERLAY_STRING (overlay, str, 0);
5157 /* If overlay has a non-empty after-string, record it. */
5158 if ((end == charpos || (start == charpos && invis_p))
5159 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5160 && SCHARS (str))
5161 RECORD_OVERLAY_STRING (overlay, str, 1);
5164 #undef RECORD_OVERLAY_STRING
5166 /* Sort entries. */
5167 if (n > 1)
5168 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5170 /* Record number of overlay strings, and where we computed it. */
5171 it->n_overlay_strings = n;
5172 it->overlay_strings_charpos = charpos;
5174 /* IT->current.overlay_string_index is the number of overlay strings
5175 that have already been consumed by IT. Copy some of the
5176 remaining overlay strings to IT->overlay_strings. */
5177 i = 0;
5178 j = it->current.overlay_string_index;
5179 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5181 it->overlay_strings[i] = entries[j].string;
5182 it->string_overlays[i++] = entries[j++].overlay;
5185 CHECK_IT (it);
5189 /* Get the first chunk of overlay strings at IT's current buffer
5190 position, or at CHARPOS if that is > 0. Value is non-zero if at
5191 least one overlay string was found. */
5193 static int
5194 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
5196 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5197 process. This fills IT->overlay_strings with strings, and sets
5198 IT->n_overlay_strings to the total number of strings to process.
5199 IT->pos.overlay_string_index has to be set temporarily to zero
5200 because load_overlay_strings needs this; it must be set to -1
5201 when no overlay strings are found because a zero value would
5202 indicate a position in the first overlay string. */
5203 it->current.overlay_string_index = 0;
5204 load_overlay_strings (it, charpos);
5206 /* If we found overlay strings, set up IT to deliver display
5207 elements from the first one. Otherwise set up IT to deliver
5208 from current_buffer. */
5209 if (it->n_overlay_strings)
5211 /* Make sure we know settings in current_buffer, so that we can
5212 restore meaningful values when we're done with the overlay
5213 strings. */
5214 if (compute_stop_p)
5215 compute_stop_pos (it);
5216 xassert (it->face_id >= 0);
5218 /* Save IT's settings. They are restored after all overlay
5219 strings have been processed. */
5220 xassert (!compute_stop_p || it->sp == 0);
5222 /* When called from handle_stop, there might be an empty display
5223 string loaded. In that case, don't bother saving it. */
5224 if (!STRINGP (it->string) || SCHARS (it->string))
5225 push_it (it, NULL);
5227 /* Set up IT to deliver display elements from the first overlay
5228 string. */
5229 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5230 it->string = it->overlay_strings[0];
5231 it->from_overlay = Qnil;
5232 it->stop_charpos = 0;
5233 xassert (STRINGP (it->string));
5234 it->end_charpos = SCHARS (it->string);
5235 it->prev_stop = 0;
5236 it->base_level_stop = 0;
5237 it->multibyte_p = STRING_MULTIBYTE (it->string);
5238 it->method = GET_FROM_STRING;
5239 it->from_disp_prop_p = 0;
5241 /* Force paragraph direction to be that of the parent
5242 buffer. */
5243 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5244 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5245 else
5246 it->paragraph_embedding = L2R;
5248 /* Set up the bidi iterator for this overlay string. */
5249 if (it->bidi_p)
5251 EMACS_INT pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5253 it->bidi_it.string.lstring = it->string;
5254 it->bidi_it.string.s = NULL;
5255 it->bidi_it.string.schars = SCHARS (it->string);
5256 it->bidi_it.string.bufpos = pos;
5257 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5258 it->bidi_it.string.unibyte = !it->multibyte_p;
5259 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5261 return 1;
5264 it->current.overlay_string_index = -1;
5265 return 0;
5268 static int
5269 get_overlay_strings (struct it *it, EMACS_INT charpos)
5271 it->string = Qnil;
5272 it->method = GET_FROM_BUFFER;
5274 (void) get_overlay_strings_1 (it, charpos, 1);
5276 CHECK_IT (it);
5278 /* Value is non-zero if we found at least one overlay string. */
5279 return STRINGP (it->string);
5284 /***********************************************************************
5285 Saving and restoring state
5286 ***********************************************************************/
5288 /* Save current settings of IT on IT->stack. Called, for example,
5289 before setting up IT for an overlay string, to be able to restore
5290 IT's settings to what they were after the overlay string has been
5291 processed. If POSITION is non-NULL, it is the position to save on
5292 the stack instead of IT->position. */
5294 static void
5295 push_it (struct it *it, struct text_pos *position)
5297 struct iterator_stack_entry *p;
5299 xassert (it->sp < IT_STACK_SIZE);
5300 p = it->stack + it->sp;
5302 p->stop_charpos = it->stop_charpos;
5303 p->prev_stop = it->prev_stop;
5304 p->base_level_stop = it->base_level_stop;
5305 p->cmp_it = it->cmp_it;
5306 xassert (it->face_id >= 0);
5307 p->face_id = it->face_id;
5308 p->string = it->string;
5309 p->method = it->method;
5310 p->from_overlay = it->from_overlay;
5311 switch (p->method)
5313 case GET_FROM_IMAGE:
5314 p->u.image.object = it->object;
5315 p->u.image.image_id = it->image_id;
5316 p->u.image.slice = it->slice;
5317 break;
5318 case GET_FROM_STRETCH:
5319 p->u.stretch.object = it->object;
5320 break;
5322 p->position = position ? *position : it->position;
5323 p->current = it->current;
5324 p->end_charpos = it->end_charpos;
5325 p->string_nchars = it->string_nchars;
5326 p->area = it->area;
5327 p->multibyte_p = it->multibyte_p;
5328 p->avoid_cursor_p = it->avoid_cursor_p;
5329 p->space_width = it->space_width;
5330 p->font_height = it->font_height;
5331 p->voffset = it->voffset;
5332 p->string_from_display_prop_p = it->string_from_display_prop_p;
5333 p->display_ellipsis_p = 0;
5334 p->line_wrap = it->line_wrap;
5335 p->bidi_p = it->bidi_p;
5336 p->paragraph_embedding = it->paragraph_embedding;
5337 p->from_disp_prop_p = it->from_disp_prop_p;
5338 ++it->sp;
5340 /* Save the state of the bidi iterator as well. */
5341 if (it->bidi_p)
5342 bidi_push_it (&it->bidi_it);
5345 static void
5346 iterate_out_of_display_property (struct it *it)
5348 int buffer_p = BUFFERP (it->object);
5349 EMACS_INT eob = (buffer_p ? ZV : it->end_charpos);
5350 EMACS_INT bob = (buffer_p ? BEGV : 0);
5352 /* Maybe initialize paragraph direction. If we are at the beginning
5353 of a new paragraph, next_element_from_buffer may not have a
5354 chance to do that. */
5355 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5356 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5357 /* prev_stop can be zero, so check against BEGV as well. */
5358 while (it->bidi_it.charpos >= bob
5359 && it->prev_stop <= it->bidi_it.charpos
5360 && it->bidi_it.charpos < CHARPOS (it->position))
5361 bidi_move_to_visually_next (&it->bidi_it);
5362 /* Record the stop_pos we just crossed, for when we cross it
5363 back, maybe. */
5364 if (it->bidi_it.charpos > CHARPOS (it->position))
5365 it->prev_stop = CHARPOS (it->position);
5366 /* If we ended up not where pop_it put us, resync IT's
5367 positional members with the bidi iterator. */
5368 if (it->bidi_it.charpos != CHARPOS (it->position))
5370 SET_TEXT_POS (it->position,
5371 it->bidi_it.charpos, it->bidi_it.bytepos);
5372 if (buffer_p)
5373 it->current.pos = it->position;
5374 else
5375 it->current.string_pos = it->position;
5379 /* Restore IT's settings from IT->stack. Called, for example, when no
5380 more overlay strings must be processed, and we return to delivering
5381 display elements from a buffer, or when the end of a string from a
5382 `display' property is reached and we return to delivering display
5383 elements from an overlay string, or from a buffer. */
5385 static void
5386 pop_it (struct it *it)
5388 struct iterator_stack_entry *p;
5389 int from_display_prop = it->from_disp_prop_p;
5391 xassert (it->sp > 0);
5392 --it->sp;
5393 p = it->stack + it->sp;
5394 it->stop_charpos = p->stop_charpos;
5395 it->prev_stop = p->prev_stop;
5396 it->base_level_stop = p->base_level_stop;
5397 it->cmp_it = p->cmp_it;
5398 it->face_id = p->face_id;
5399 it->current = p->current;
5400 it->position = p->position;
5401 it->string = p->string;
5402 it->from_overlay = p->from_overlay;
5403 if (NILP (it->string))
5404 SET_TEXT_POS (it->current.string_pos, -1, -1);
5405 it->method = p->method;
5406 switch (it->method)
5408 case GET_FROM_IMAGE:
5409 it->image_id = p->u.image.image_id;
5410 it->object = p->u.image.object;
5411 it->slice = p->u.image.slice;
5412 break;
5413 case GET_FROM_STRETCH:
5414 it->object = p->u.stretch.object;
5415 break;
5416 case GET_FROM_BUFFER:
5417 it->object = it->w->buffer;
5418 break;
5419 case GET_FROM_STRING:
5420 it->object = it->string;
5421 break;
5422 case GET_FROM_DISPLAY_VECTOR:
5423 if (it->s)
5424 it->method = GET_FROM_C_STRING;
5425 else if (STRINGP (it->string))
5426 it->method = GET_FROM_STRING;
5427 else
5429 it->method = GET_FROM_BUFFER;
5430 it->object = it->w->buffer;
5433 it->end_charpos = p->end_charpos;
5434 it->string_nchars = p->string_nchars;
5435 it->area = p->area;
5436 it->multibyte_p = p->multibyte_p;
5437 it->avoid_cursor_p = p->avoid_cursor_p;
5438 it->space_width = p->space_width;
5439 it->font_height = p->font_height;
5440 it->voffset = p->voffset;
5441 it->string_from_display_prop_p = p->string_from_display_prop_p;
5442 it->line_wrap = p->line_wrap;
5443 it->bidi_p = p->bidi_p;
5444 it->paragraph_embedding = p->paragraph_embedding;
5445 it->from_disp_prop_p = p->from_disp_prop_p;
5446 if (it->bidi_p)
5448 bidi_pop_it (&it->bidi_it);
5449 /* Bidi-iterate until we get out of the portion of text, if any,
5450 covered by a `display' text property or by an overlay with
5451 `display' property. (We cannot just jump there, because the
5452 internal coherency of the bidi iterator state can not be
5453 preserved across such jumps.) We also must determine the
5454 paragraph base direction if the overlay we just processed is
5455 at the beginning of a new paragraph. */
5456 if (from_display_prop
5457 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5458 iterate_out_of_display_property (it);
5460 xassert ((BUFFERP (it->object)
5461 && IT_CHARPOS (*it) == it->bidi_it.charpos
5462 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5463 || (STRINGP (it->object)
5464 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5465 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos));
5471 /***********************************************************************
5472 Moving over lines
5473 ***********************************************************************/
5475 /* Set IT's current position to the previous line start. */
5477 static void
5478 back_to_previous_line_start (struct it *it)
5480 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5481 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5485 /* Move IT to the next line start.
5487 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5488 we skipped over part of the text (as opposed to moving the iterator
5489 continuously over the text). Otherwise, don't change the value
5490 of *SKIPPED_P.
5492 Newlines may come from buffer text, overlay strings, or strings
5493 displayed via the `display' property. That's the reason we can't
5494 simply use find_next_newline_no_quit.
5496 Note that this function may not skip over invisible text that is so
5497 because of text properties and immediately follows a newline. If
5498 it would, function reseat_at_next_visible_line_start, when called
5499 from set_iterator_to_next, would effectively make invisible
5500 characters following a newline part of the wrong glyph row, which
5501 leads to wrong cursor motion. */
5503 static int
5504 forward_to_next_line_start (struct it *it, int *skipped_p)
5506 EMACS_INT old_selective;
5507 int newline_found_p, n;
5508 const int MAX_NEWLINE_DISTANCE = 500;
5510 /* If already on a newline, just consume it to avoid unintended
5511 skipping over invisible text below. */
5512 if (it->what == IT_CHARACTER
5513 && it->c == '\n'
5514 && CHARPOS (it->position) == IT_CHARPOS (*it))
5516 set_iterator_to_next (it, 0);
5517 it->c = 0;
5518 return 1;
5521 /* Don't handle selective display in the following. It's (a)
5522 unnecessary because it's done by the caller, and (b) leads to an
5523 infinite recursion because next_element_from_ellipsis indirectly
5524 calls this function. */
5525 old_selective = it->selective;
5526 it->selective = 0;
5528 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5529 from buffer text. */
5530 for (n = newline_found_p = 0;
5531 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5532 n += STRINGP (it->string) ? 0 : 1)
5534 if (!get_next_display_element (it))
5535 return 0;
5536 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5537 set_iterator_to_next (it, 0);
5540 /* If we didn't find a newline near enough, see if we can use a
5541 short-cut. */
5542 if (!newline_found_p)
5544 EMACS_INT start = IT_CHARPOS (*it);
5545 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5546 Lisp_Object pos;
5548 xassert (!STRINGP (it->string));
5550 /* If we are not bidi-reordering, and there isn't any `display'
5551 property in sight, and no overlays, we can just use the
5552 position of the newline in buffer text. */
5553 if (!it->bidi_p
5554 && (it->stop_charpos >= limit
5555 || ((pos = Fnext_single_property_change (make_number (start),
5556 Qdisplay, Qnil,
5557 make_number (limit)),
5558 NILP (pos))
5559 && next_overlay_change (start) == ZV)))
5561 IT_CHARPOS (*it) = limit;
5562 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5563 *skipped_p = newline_found_p = 1;
5565 else
5567 while (get_next_display_element (it)
5568 && !newline_found_p)
5570 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5571 set_iterator_to_next (it, 0);
5576 it->selective = old_selective;
5577 return newline_found_p;
5581 /* Set IT's current position to the previous visible line start. Skip
5582 invisible text that is so either due to text properties or due to
5583 selective display. Caution: this does not change IT->current_x and
5584 IT->hpos. */
5586 static void
5587 back_to_previous_visible_line_start (struct it *it)
5589 while (IT_CHARPOS (*it) > BEGV)
5591 back_to_previous_line_start (it);
5593 if (IT_CHARPOS (*it) <= BEGV)
5594 break;
5596 /* If selective > 0, then lines indented more than its value are
5597 invisible. */
5598 if (it->selective > 0
5599 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5600 it->selective))
5601 continue;
5603 /* Check the newline before point for invisibility. */
5605 Lisp_Object prop;
5606 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5607 Qinvisible, it->window);
5608 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5609 continue;
5612 if (IT_CHARPOS (*it) <= BEGV)
5613 break;
5616 struct it it2;
5617 void *it2data = NULL;
5618 EMACS_INT pos;
5619 EMACS_INT beg, end;
5620 Lisp_Object val, overlay;
5622 SAVE_IT (it2, *it, it2data);
5624 /* If newline is part of a composition, continue from start of composition */
5625 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5626 && beg < IT_CHARPOS (*it))
5627 goto replaced;
5629 /* If newline is replaced by a display property, find start of overlay
5630 or interval and continue search from that point. */
5631 pos = --IT_CHARPOS (it2);
5632 --IT_BYTEPOS (it2);
5633 it2.sp = 0;
5634 bidi_unshelve_cache (NULL);
5635 it2.string_from_display_prop_p = 0;
5636 it2.from_disp_prop_p = 0;
5637 if (handle_display_prop (&it2) == HANDLED_RETURN
5638 && !NILP (val = get_char_property_and_overlay
5639 (make_number (pos), Qdisplay, Qnil, &overlay))
5640 && (OVERLAYP (overlay)
5641 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5642 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5644 RESTORE_IT (it, it, it2data);
5645 goto replaced;
5648 /* Newline is not replaced by anything -- so we are done. */
5649 RESTORE_IT (it, it, it2data);
5650 break;
5652 replaced:
5653 if (beg < BEGV)
5654 beg = BEGV;
5655 IT_CHARPOS (*it) = beg;
5656 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5660 it->continuation_lines_width = 0;
5662 xassert (IT_CHARPOS (*it) >= BEGV);
5663 xassert (IT_CHARPOS (*it) == BEGV
5664 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5665 CHECK_IT (it);
5669 /* Reseat iterator IT at the previous visible line start. Skip
5670 invisible text that is so either due to text properties or due to
5671 selective display. At the end, update IT's overlay information,
5672 face information etc. */
5674 void
5675 reseat_at_previous_visible_line_start (struct it *it)
5677 back_to_previous_visible_line_start (it);
5678 reseat (it, it->current.pos, 1);
5679 CHECK_IT (it);
5683 /* Reseat iterator IT on the next visible line start in the current
5684 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5685 preceding the line start. Skip over invisible text that is so
5686 because of selective display. Compute faces, overlays etc at the
5687 new position. Note that this function does not skip over text that
5688 is invisible because of text properties. */
5690 static void
5691 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5693 int newline_found_p, skipped_p = 0;
5695 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5697 /* Skip over lines that are invisible because they are indented
5698 more than the value of IT->selective. */
5699 if (it->selective > 0)
5700 while (IT_CHARPOS (*it) < ZV
5701 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5702 it->selective))
5704 xassert (IT_BYTEPOS (*it) == BEGV
5705 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5706 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5709 /* Position on the newline if that's what's requested. */
5710 if (on_newline_p && newline_found_p)
5712 if (STRINGP (it->string))
5714 if (IT_STRING_CHARPOS (*it) > 0)
5716 if (!it->bidi_p)
5718 --IT_STRING_CHARPOS (*it);
5719 --IT_STRING_BYTEPOS (*it);
5721 else
5722 /* Setting this flag will cause
5723 bidi_move_to_visually_next not to advance, but
5724 instead deliver the current character (newline),
5725 which is what the ON_NEWLINE_P flag wants. */
5726 it->bidi_it.first_elt = 1;
5729 else if (IT_CHARPOS (*it) > BEGV)
5731 if (!it->bidi_p)
5733 --IT_CHARPOS (*it);
5734 --IT_BYTEPOS (*it);
5736 /* With bidi iteration, the call to `reseat' will cause
5737 bidi_move_to_visually_next deliver the current character,
5738 the newline, instead of advancing. */
5739 reseat (it, it->current.pos, 0);
5742 else if (skipped_p)
5743 reseat (it, it->current.pos, 0);
5745 CHECK_IT (it);
5750 /***********************************************************************
5751 Changing an iterator's position
5752 ***********************************************************************/
5754 /* Change IT's current position to POS in current_buffer. If FORCE_P
5755 is non-zero, always check for text properties at the new position.
5756 Otherwise, text properties are only looked up if POS >=
5757 IT->check_charpos of a property. */
5759 static void
5760 reseat (struct it *it, struct text_pos pos, int force_p)
5762 EMACS_INT original_pos = IT_CHARPOS (*it);
5764 reseat_1 (it, pos, 0);
5766 /* Determine where to check text properties. Avoid doing it
5767 where possible because text property lookup is very expensive. */
5768 if (force_p
5769 || CHARPOS (pos) > it->stop_charpos
5770 || CHARPOS (pos) < original_pos)
5772 if (it->bidi_p)
5774 /* For bidi iteration, we need to prime prev_stop and
5775 base_level_stop with our best estimations. */
5776 /* Implementation note: Of course, POS is not necessarily a
5777 stop position, so assigning prev_pos to it is a lie; we
5778 should have called compute_stop_backwards. However, if
5779 the current buffer does not include any R2L characters,
5780 that call would be a waste of cycles, because the
5781 iterator will never move back, and thus never cross this
5782 "fake" stop position. So we delay that backward search
5783 until the time we really need it, in next_element_from_buffer. */
5784 if (CHARPOS (pos) != it->prev_stop)
5785 it->prev_stop = CHARPOS (pos);
5786 if (CHARPOS (pos) < it->base_level_stop)
5787 it->base_level_stop = 0; /* meaning it's unknown */
5788 handle_stop (it);
5790 else
5792 handle_stop (it);
5793 it->prev_stop = it->base_level_stop = 0;
5798 CHECK_IT (it);
5802 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5803 IT->stop_pos to POS, also. */
5805 static void
5806 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
5808 /* Don't call this function when scanning a C string. */
5809 xassert (it->s == NULL);
5811 /* POS must be a reasonable value. */
5812 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5814 it->current.pos = it->position = pos;
5815 it->end_charpos = ZV;
5816 it->dpvec = NULL;
5817 it->current.dpvec_index = -1;
5818 it->current.overlay_string_index = -1;
5819 IT_STRING_CHARPOS (*it) = -1;
5820 IT_STRING_BYTEPOS (*it) = -1;
5821 it->string = Qnil;
5822 it->method = GET_FROM_BUFFER;
5823 it->object = it->w->buffer;
5824 it->area = TEXT_AREA;
5825 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
5826 it->sp = 0;
5827 it->string_from_display_prop_p = 0;
5828 it->from_disp_prop_p = 0;
5829 it->face_before_selective_p = 0;
5830 if (it->bidi_p)
5832 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
5833 &it->bidi_it);
5834 bidi_unshelve_cache (NULL);
5835 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5836 it->bidi_it.string.s = NULL;
5837 it->bidi_it.string.lstring = Qnil;
5838 it->bidi_it.string.bufpos = 0;
5839 it->bidi_it.string.unibyte = 0;
5842 if (set_stop_p)
5844 it->stop_charpos = CHARPOS (pos);
5845 it->base_level_stop = CHARPOS (pos);
5850 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5851 If S is non-null, it is a C string to iterate over. Otherwise,
5852 STRING gives a Lisp string to iterate over.
5854 If PRECISION > 0, don't return more then PRECISION number of
5855 characters from the string.
5857 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5858 characters have been returned. FIELD_WIDTH < 0 means an infinite
5859 field width.
5861 MULTIBYTE = 0 means disable processing of multibyte characters,
5862 MULTIBYTE > 0 means enable it,
5863 MULTIBYTE < 0 means use IT->multibyte_p.
5865 IT must be initialized via a prior call to init_iterator before
5866 calling this function. */
5868 static void
5869 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
5870 EMACS_INT charpos, EMACS_INT precision, int field_width,
5871 int multibyte)
5873 /* No region in strings. */
5874 it->region_beg_charpos = it->region_end_charpos = -1;
5876 /* No text property checks performed by default, but see below. */
5877 it->stop_charpos = -1;
5879 /* Set iterator position and end position. */
5880 memset (&it->current, 0, sizeof it->current);
5881 it->current.overlay_string_index = -1;
5882 it->current.dpvec_index = -1;
5883 xassert (charpos >= 0);
5885 /* If STRING is specified, use its multibyteness, otherwise use the
5886 setting of MULTIBYTE, if specified. */
5887 if (multibyte >= 0)
5888 it->multibyte_p = multibyte > 0;
5890 /* Bidirectional reordering of strings is controlled by the default
5891 value of bidi-display-reordering. */
5892 it->bidi_p = !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
5894 if (s == NULL)
5896 xassert (STRINGP (string));
5897 it->string = string;
5898 it->s = NULL;
5899 it->end_charpos = it->string_nchars = SCHARS (string);
5900 it->method = GET_FROM_STRING;
5901 it->current.string_pos = string_pos (charpos, string);
5903 if (it->bidi_p)
5905 it->bidi_it.string.lstring = string;
5906 it->bidi_it.string.s = NULL;
5907 it->bidi_it.string.schars = it->end_charpos;
5908 it->bidi_it.string.bufpos = 0;
5909 it->bidi_it.string.from_disp_str = 0;
5910 it->bidi_it.string.unibyte = !it->multibyte_p;
5911 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
5912 FRAME_WINDOW_P (it->f), &it->bidi_it);
5915 else
5917 it->s = (const unsigned char *) s;
5918 it->string = Qnil;
5920 /* Note that we use IT->current.pos, not it->current.string_pos,
5921 for displaying C strings. */
5922 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5923 if (it->multibyte_p)
5925 it->current.pos = c_string_pos (charpos, s, 1);
5926 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5928 else
5930 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5931 it->end_charpos = it->string_nchars = strlen (s);
5934 if (it->bidi_p)
5936 it->bidi_it.string.lstring = Qnil;
5937 it->bidi_it.string.s = (const unsigned char *) s;
5938 it->bidi_it.string.schars = it->end_charpos;
5939 it->bidi_it.string.bufpos = 0;
5940 it->bidi_it.string.from_disp_str = 0;
5941 it->bidi_it.string.unibyte = !it->multibyte_p;
5942 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
5943 &it->bidi_it);
5945 it->method = GET_FROM_C_STRING;
5948 /* PRECISION > 0 means don't return more than PRECISION characters
5949 from the string. */
5950 if (precision > 0 && it->end_charpos - charpos > precision)
5952 it->end_charpos = it->string_nchars = charpos + precision;
5953 if (it->bidi_p)
5954 it->bidi_it.string.schars = it->end_charpos;
5957 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5958 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5959 FIELD_WIDTH < 0 means infinite field width. This is useful for
5960 padding with `-' at the end of a mode line. */
5961 if (field_width < 0)
5962 field_width = INFINITY;
5963 /* Implementation note: We deliberately don't enlarge
5964 it->bidi_it.string.schars here to fit it->end_charpos, because
5965 the bidi iterator cannot produce characters out of thin air. */
5966 if (field_width > it->end_charpos - charpos)
5967 it->end_charpos = charpos + field_width;
5969 /* Use the standard display table for displaying strings. */
5970 if (DISP_TABLE_P (Vstandard_display_table))
5971 it->dp = XCHAR_TABLE (Vstandard_display_table);
5973 it->stop_charpos = charpos;
5974 it->prev_stop = charpos;
5975 it->base_level_stop = 0;
5976 if (it->bidi_p)
5978 it->bidi_it.first_elt = 1;
5979 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5980 it->bidi_it.disp_pos = -1;
5982 if (s == NULL && it->multibyte_p)
5984 EMACS_INT endpos = SCHARS (it->string);
5985 if (endpos > it->end_charpos)
5986 endpos = it->end_charpos;
5987 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5988 it->string);
5990 CHECK_IT (it);
5995 /***********************************************************************
5996 Iteration
5997 ***********************************************************************/
5999 /* Map enum it_method value to corresponding next_element_from_* function. */
6001 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6003 next_element_from_buffer,
6004 next_element_from_display_vector,
6005 next_element_from_string,
6006 next_element_from_c_string,
6007 next_element_from_image,
6008 next_element_from_stretch
6011 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6014 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6015 (possibly with the following characters). */
6017 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6018 ((IT)->cmp_it.id >= 0 \
6019 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6020 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6021 END_CHARPOS, (IT)->w, \
6022 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6023 (IT)->string)))
6026 /* Lookup the char-table Vglyphless_char_display for character C (-1
6027 if we want information for no-font case), and return the display
6028 method symbol. By side-effect, update it->what and
6029 it->glyphless_method. This function is called from
6030 get_next_display_element for each character element, and from
6031 x_produce_glyphs when no suitable font was found. */
6033 Lisp_Object
6034 lookup_glyphless_char_display (int c, struct it *it)
6036 Lisp_Object glyphless_method = Qnil;
6038 if (CHAR_TABLE_P (Vglyphless_char_display)
6039 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6041 if (c >= 0)
6043 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6044 if (CONSP (glyphless_method))
6045 glyphless_method = FRAME_WINDOW_P (it->f)
6046 ? XCAR (glyphless_method)
6047 : XCDR (glyphless_method);
6049 else
6050 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6053 retry:
6054 if (NILP (glyphless_method))
6056 if (c >= 0)
6057 /* The default is to display the character by a proper font. */
6058 return Qnil;
6059 /* The default for the no-font case is to display an empty box. */
6060 glyphless_method = Qempty_box;
6062 if (EQ (glyphless_method, Qzero_width))
6064 if (c >= 0)
6065 return glyphless_method;
6066 /* This method can't be used for the no-font case. */
6067 glyphless_method = Qempty_box;
6069 if (EQ (glyphless_method, Qthin_space))
6070 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6071 else if (EQ (glyphless_method, Qempty_box))
6072 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6073 else if (EQ (glyphless_method, Qhex_code))
6074 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6075 else if (STRINGP (glyphless_method))
6076 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6077 else
6079 /* Invalid value. We use the default method. */
6080 glyphless_method = Qnil;
6081 goto retry;
6083 it->what = IT_GLYPHLESS;
6084 return glyphless_method;
6087 /* Load IT's display element fields with information about the next
6088 display element from the current position of IT. Value is zero if
6089 end of buffer (or C string) is reached. */
6091 static struct frame *last_escape_glyph_frame = NULL;
6092 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6093 static int last_escape_glyph_merged_face_id = 0;
6095 struct frame *last_glyphless_glyph_frame = NULL;
6096 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6097 int last_glyphless_glyph_merged_face_id = 0;
6099 static int
6100 get_next_display_element (struct it *it)
6102 /* Non-zero means that we found a display element. Zero means that
6103 we hit the end of what we iterate over. Performance note: the
6104 function pointer `method' used here turns out to be faster than
6105 using a sequence of if-statements. */
6106 int success_p;
6108 get_next:
6109 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6111 if (it->what == IT_CHARACTER)
6113 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6114 and only if (a) the resolved directionality of that character
6115 is R..." */
6116 /* FIXME: Do we need an exception for characters from display
6117 tables? */
6118 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6119 it->c = bidi_mirror_char (it->c);
6120 /* Map via display table or translate control characters.
6121 IT->c, IT->len etc. have been set to the next character by
6122 the function call above. If we have a display table, and it
6123 contains an entry for IT->c, translate it. Don't do this if
6124 IT->c itself comes from a display table, otherwise we could
6125 end up in an infinite recursion. (An alternative could be to
6126 count the recursion depth of this function and signal an
6127 error when a certain maximum depth is reached.) Is it worth
6128 it? */
6129 if (success_p && it->dpvec == NULL)
6131 Lisp_Object dv;
6132 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6133 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
6134 nbsp_or_shy = char_is_other;
6135 int c = it->c; /* This is the character to display. */
6137 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6139 xassert (SINGLE_BYTE_CHAR_P (c));
6140 if (unibyte_display_via_language_environment)
6142 c = DECODE_CHAR (unibyte, c);
6143 if (c < 0)
6144 c = BYTE8_TO_CHAR (it->c);
6146 else
6147 c = BYTE8_TO_CHAR (it->c);
6150 if (it->dp
6151 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6152 VECTORP (dv)))
6154 struct Lisp_Vector *v = XVECTOR (dv);
6156 /* Return the first character from the display table
6157 entry, if not empty. If empty, don't display the
6158 current character. */
6159 if (v->header.size)
6161 it->dpvec_char_len = it->len;
6162 it->dpvec = v->contents;
6163 it->dpend = v->contents + v->header.size;
6164 it->current.dpvec_index = 0;
6165 it->dpvec_face_id = -1;
6166 it->saved_face_id = it->face_id;
6167 it->method = GET_FROM_DISPLAY_VECTOR;
6168 it->ellipsis_p = 0;
6170 else
6172 set_iterator_to_next (it, 0);
6174 goto get_next;
6177 if (! NILP (lookup_glyphless_char_display (c, it)))
6179 if (it->what == IT_GLYPHLESS)
6180 goto done;
6181 /* Don't display this character. */
6182 set_iterator_to_next (it, 0);
6183 goto get_next;
6186 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6187 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
6188 : c == 0xAD ? char_is_soft_hyphen
6189 : char_is_other);
6191 /* Translate control characters into `\003' or `^C' form.
6192 Control characters coming from a display table entry are
6193 currently not translated because we use IT->dpvec to hold
6194 the translation. This could easily be changed but I
6195 don't believe that it is worth doing.
6197 NBSP and SOFT-HYPEN are property translated too.
6199 Non-printable characters and raw-byte characters are also
6200 translated to octal form. */
6201 if (((c < ' ' || c == 127) /* ASCII control chars */
6202 ? (it->area != TEXT_AREA
6203 /* In mode line, treat \n, \t like other crl chars. */
6204 || (c != '\t'
6205 && it->glyph_row
6206 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6207 || (c != '\n' && c != '\t'))
6208 : (nbsp_or_shy
6209 || CHAR_BYTE8_P (c)
6210 || ! CHAR_PRINTABLE_P (c))))
6212 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
6213 or a non-printable character which must be displayed
6214 either as '\003' or as `^C' where the '\\' and '^'
6215 can be defined in the display table. Fill
6216 IT->ctl_chars with glyphs for what we have to
6217 display. Then, set IT->dpvec to these glyphs. */
6218 Lisp_Object gc;
6219 int ctl_len;
6220 int face_id;
6221 EMACS_INT lface_id = 0;
6222 int escape_glyph;
6224 /* Handle control characters with ^. */
6226 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6228 int g;
6230 g = '^'; /* default glyph for Control */
6231 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6232 if (it->dp
6233 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
6234 && GLYPH_CODE_CHAR_VALID_P (gc))
6236 g = GLYPH_CODE_CHAR (gc);
6237 lface_id = GLYPH_CODE_FACE (gc);
6239 if (lface_id)
6241 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6243 else if (it->f == last_escape_glyph_frame
6244 && it->face_id == last_escape_glyph_face_id)
6246 face_id = last_escape_glyph_merged_face_id;
6248 else
6250 /* Merge the escape-glyph face into the current face. */
6251 face_id = merge_faces (it->f, Qescape_glyph, 0,
6252 it->face_id);
6253 last_escape_glyph_frame = it->f;
6254 last_escape_glyph_face_id = it->face_id;
6255 last_escape_glyph_merged_face_id = face_id;
6258 XSETINT (it->ctl_chars[0], g);
6259 XSETINT (it->ctl_chars[1], c ^ 0100);
6260 ctl_len = 2;
6261 goto display_control;
6264 /* Handle non-break space in the mode where it only gets
6265 highlighting. */
6267 if (EQ (Vnobreak_char_display, Qt)
6268 && nbsp_or_shy == char_is_nbsp)
6270 /* Merge the no-break-space face into the current face. */
6271 face_id = merge_faces (it->f, Qnobreak_space, 0,
6272 it->face_id);
6274 c = ' ';
6275 XSETINT (it->ctl_chars[0], ' ');
6276 ctl_len = 1;
6277 goto display_control;
6280 /* Handle sequences that start with the "escape glyph". */
6282 /* the default escape glyph is \. */
6283 escape_glyph = '\\';
6285 if (it->dp
6286 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
6287 && GLYPH_CODE_CHAR_VALID_P (gc))
6289 escape_glyph = GLYPH_CODE_CHAR (gc);
6290 lface_id = GLYPH_CODE_FACE (gc);
6292 if (lface_id)
6294 /* The display table specified a face.
6295 Merge it into face_id and also into escape_glyph. */
6296 face_id = merge_faces (it->f, Qt, lface_id,
6297 it->face_id);
6299 else if (it->f == last_escape_glyph_frame
6300 && it->face_id == last_escape_glyph_face_id)
6302 face_id = last_escape_glyph_merged_face_id;
6304 else
6306 /* Merge the escape-glyph face into the current face. */
6307 face_id = merge_faces (it->f, Qescape_glyph, 0,
6308 it->face_id);
6309 last_escape_glyph_frame = it->f;
6310 last_escape_glyph_face_id = it->face_id;
6311 last_escape_glyph_merged_face_id = face_id;
6314 /* Handle soft hyphens in the mode where they only get
6315 highlighting. */
6317 if (EQ (Vnobreak_char_display, Qt)
6318 && nbsp_or_shy == char_is_soft_hyphen)
6320 XSETINT (it->ctl_chars[0], '-');
6321 ctl_len = 1;
6322 goto display_control;
6325 /* Handle non-break space and soft hyphen
6326 with the escape glyph. */
6328 if (nbsp_or_shy)
6330 XSETINT (it->ctl_chars[0], escape_glyph);
6331 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
6332 XSETINT (it->ctl_chars[1], c);
6333 ctl_len = 2;
6334 goto display_control;
6338 char str[10];
6339 int len, i;
6341 if (CHAR_BYTE8_P (c))
6342 /* Display \200 instead of \17777600. */
6343 c = CHAR_TO_BYTE8 (c);
6344 len = sprintf (str, "%03o", c);
6346 XSETINT (it->ctl_chars[0], escape_glyph);
6347 for (i = 0; i < len; i++)
6348 XSETINT (it->ctl_chars[i + 1], str[i]);
6349 ctl_len = len + 1;
6352 display_control:
6353 /* Set up IT->dpvec and return first character from it. */
6354 it->dpvec_char_len = it->len;
6355 it->dpvec = it->ctl_chars;
6356 it->dpend = it->dpvec + ctl_len;
6357 it->current.dpvec_index = 0;
6358 it->dpvec_face_id = face_id;
6359 it->saved_face_id = it->face_id;
6360 it->method = GET_FROM_DISPLAY_VECTOR;
6361 it->ellipsis_p = 0;
6362 goto get_next;
6364 it->char_to_display = c;
6366 else if (success_p)
6368 it->char_to_display = it->c;
6372 /* Adjust face id for a multibyte character. There are no multibyte
6373 character in unibyte text. */
6374 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6375 && it->multibyte_p
6376 && success_p
6377 && FRAME_WINDOW_P (it->f))
6379 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6381 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6383 /* Automatic composition with glyph-string. */
6384 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6386 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6388 else
6390 EMACS_INT pos = (it->s ? -1
6391 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6392 : IT_CHARPOS (*it));
6393 int c;
6395 if (it->what == IT_CHARACTER)
6396 c = it->char_to_display;
6397 else
6399 struct composition *cmp = composition_table[it->cmp_it.id];
6400 int i;
6402 c = ' ';
6403 for (i = 0; i < cmp->glyph_len; i++)
6404 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6405 break;
6407 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6411 done:
6412 /* Is this character the last one of a run of characters with
6413 box? If yes, set IT->end_of_box_run_p to 1. */
6414 if (it->face_box_p
6415 && it->s == NULL)
6417 if (it->method == GET_FROM_STRING && it->sp)
6419 int face_id = underlying_face_id (it);
6420 struct face *face = FACE_FROM_ID (it->f, face_id);
6422 if (face)
6424 if (face->box == FACE_NO_BOX)
6426 /* If the box comes from face properties in a
6427 display string, check faces in that string. */
6428 int string_face_id = face_after_it_pos (it);
6429 it->end_of_box_run_p
6430 = (FACE_FROM_ID (it->f, string_face_id)->box
6431 == FACE_NO_BOX);
6433 /* Otherwise, the box comes from the underlying face.
6434 If this is the last string character displayed, check
6435 the next buffer location. */
6436 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6437 && (it->current.overlay_string_index
6438 == it->n_overlay_strings - 1))
6440 EMACS_INT ignore;
6441 int next_face_id;
6442 struct text_pos pos = it->current.pos;
6443 INC_TEXT_POS (pos, it->multibyte_p);
6445 next_face_id = face_at_buffer_position
6446 (it->w, CHARPOS (pos), it->region_beg_charpos,
6447 it->region_end_charpos, &ignore,
6448 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6449 -1);
6450 it->end_of_box_run_p
6451 = (FACE_FROM_ID (it->f, next_face_id)->box
6452 == FACE_NO_BOX);
6456 else
6458 int face_id = face_after_it_pos (it);
6459 it->end_of_box_run_p
6460 = (face_id != it->face_id
6461 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6465 /* Value is 0 if end of buffer or string reached. */
6466 return success_p;
6470 /* Move IT to the next display element.
6472 RESEAT_P non-zero means if called on a newline in buffer text,
6473 skip to the next visible line start.
6475 Functions get_next_display_element and set_iterator_to_next are
6476 separate because I find this arrangement easier to handle than a
6477 get_next_display_element function that also increments IT's
6478 position. The way it is we can first look at an iterator's current
6479 display element, decide whether it fits on a line, and if it does,
6480 increment the iterator position. The other way around we probably
6481 would either need a flag indicating whether the iterator has to be
6482 incremented the next time, or we would have to implement a
6483 decrement position function which would not be easy to write. */
6485 void
6486 set_iterator_to_next (struct it *it, int reseat_p)
6488 /* Reset flags indicating start and end of a sequence of characters
6489 with box. Reset them at the start of this function because
6490 moving the iterator to a new position might set them. */
6491 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6493 switch (it->method)
6495 case GET_FROM_BUFFER:
6496 /* The current display element of IT is a character from
6497 current_buffer. Advance in the buffer, and maybe skip over
6498 invisible lines that are so because of selective display. */
6499 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6500 reseat_at_next_visible_line_start (it, 0);
6501 else if (it->cmp_it.id >= 0)
6503 /* We are currently getting glyphs from a composition. */
6504 int i;
6506 if (! it->bidi_p)
6508 IT_CHARPOS (*it) += it->cmp_it.nchars;
6509 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6510 if (it->cmp_it.to < it->cmp_it.nglyphs)
6512 it->cmp_it.from = it->cmp_it.to;
6514 else
6516 it->cmp_it.id = -1;
6517 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6518 IT_BYTEPOS (*it),
6519 it->end_charpos, Qnil);
6522 else if (! it->cmp_it.reversed_p)
6524 /* Composition created while scanning forward. */
6525 /* Update IT's char/byte positions to point to the first
6526 character of the next grapheme cluster, or to the
6527 character visually after the current composition. */
6528 for (i = 0; i < it->cmp_it.nchars; i++)
6529 bidi_move_to_visually_next (&it->bidi_it);
6530 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6531 IT_CHARPOS (*it) = it->bidi_it.charpos;
6533 if (it->cmp_it.to < it->cmp_it.nglyphs)
6535 /* Proceed to the next grapheme cluster. */
6536 it->cmp_it.from = it->cmp_it.to;
6538 else
6540 /* No more grapheme clusters in this composition.
6541 Find the next stop position. */
6542 EMACS_INT stop = it->end_charpos;
6543 if (it->bidi_it.scan_dir < 0)
6544 /* Now we are scanning backward and don't know
6545 where to stop. */
6546 stop = -1;
6547 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6548 IT_BYTEPOS (*it), stop, Qnil);
6551 else
6553 /* Composition created while scanning backward. */
6554 /* Update IT's char/byte positions to point to the last
6555 character of the previous grapheme cluster, or the
6556 character visually after the current composition. */
6557 for (i = 0; i < it->cmp_it.nchars; i++)
6558 bidi_move_to_visually_next (&it->bidi_it);
6559 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6560 IT_CHARPOS (*it) = it->bidi_it.charpos;
6561 if (it->cmp_it.from > 0)
6563 /* Proceed to the previous grapheme cluster. */
6564 it->cmp_it.to = it->cmp_it.from;
6566 else
6568 /* No more grapheme clusters in this composition.
6569 Find the next stop position. */
6570 EMACS_INT stop = it->end_charpos;
6571 if (it->bidi_it.scan_dir < 0)
6572 /* Now we are scanning backward and don't know
6573 where to stop. */
6574 stop = -1;
6575 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6576 IT_BYTEPOS (*it), stop, Qnil);
6580 else
6582 xassert (it->len != 0);
6584 if (!it->bidi_p)
6586 IT_BYTEPOS (*it) += it->len;
6587 IT_CHARPOS (*it) += 1;
6589 else
6591 int prev_scan_dir = it->bidi_it.scan_dir;
6592 /* If this is a new paragraph, determine its base
6593 direction (a.k.a. its base embedding level). */
6594 if (it->bidi_it.new_paragraph)
6595 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6596 bidi_move_to_visually_next (&it->bidi_it);
6597 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6598 IT_CHARPOS (*it) = it->bidi_it.charpos;
6599 if (prev_scan_dir != it->bidi_it.scan_dir)
6601 /* As the scan direction was changed, we must
6602 re-compute the stop position for composition. */
6603 EMACS_INT stop = it->end_charpos;
6604 if (it->bidi_it.scan_dir < 0)
6605 stop = -1;
6606 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6607 IT_BYTEPOS (*it), stop, Qnil);
6610 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6612 break;
6614 case GET_FROM_C_STRING:
6615 /* Current display element of IT is from a C string. */
6616 if (!it->bidi_p
6617 /* If the string position is beyond string's end, it means
6618 next_element_from_c_string is padding the string with
6619 blanks, in which case we bypass the bidi iterator,
6620 because it cannot deal with such virtual characters. */
6621 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
6623 IT_BYTEPOS (*it) += it->len;
6624 IT_CHARPOS (*it) += 1;
6626 else
6628 bidi_move_to_visually_next (&it->bidi_it);
6629 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6630 IT_CHARPOS (*it) = it->bidi_it.charpos;
6632 break;
6634 case GET_FROM_DISPLAY_VECTOR:
6635 /* Current display element of IT is from a display table entry.
6636 Advance in the display table definition. Reset it to null if
6637 end reached, and continue with characters from buffers/
6638 strings. */
6639 ++it->current.dpvec_index;
6641 /* Restore face of the iterator to what they were before the
6642 display vector entry (these entries may contain faces). */
6643 it->face_id = it->saved_face_id;
6645 if (it->dpvec + it->current.dpvec_index == it->dpend)
6647 int recheck_faces = it->ellipsis_p;
6649 if (it->s)
6650 it->method = GET_FROM_C_STRING;
6651 else if (STRINGP (it->string))
6652 it->method = GET_FROM_STRING;
6653 else
6655 it->method = GET_FROM_BUFFER;
6656 it->object = it->w->buffer;
6659 it->dpvec = NULL;
6660 it->current.dpvec_index = -1;
6662 /* Skip over characters which were displayed via IT->dpvec. */
6663 if (it->dpvec_char_len < 0)
6664 reseat_at_next_visible_line_start (it, 1);
6665 else if (it->dpvec_char_len > 0)
6667 if (it->method == GET_FROM_STRING
6668 && it->n_overlay_strings > 0)
6669 it->ignore_overlay_strings_at_pos_p = 1;
6670 it->len = it->dpvec_char_len;
6671 set_iterator_to_next (it, reseat_p);
6674 /* Maybe recheck faces after display vector */
6675 if (recheck_faces)
6676 it->stop_charpos = IT_CHARPOS (*it);
6678 break;
6680 case GET_FROM_STRING:
6681 /* Current display element is a character from a Lisp string. */
6682 xassert (it->s == NULL && STRINGP (it->string));
6683 if (it->cmp_it.id >= 0)
6685 int i;
6687 if (! it->bidi_p)
6689 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6690 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6691 if (it->cmp_it.to < it->cmp_it.nglyphs)
6692 it->cmp_it.from = it->cmp_it.to;
6693 else
6695 it->cmp_it.id = -1;
6696 composition_compute_stop_pos (&it->cmp_it,
6697 IT_STRING_CHARPOS (*it),
6698 IT_STRING_BYTEPOS (*it),
6699 it->end_charpos, it->string);
6702 else if (! it->cmp_it.reversed_p)
6704 for (i = 0; i < it->cmp_it.nchars; i++)
6705 bidi_move_to_visually_next (&it->bidi_it);
6706 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6707 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6709 if (it->cmp_it.to < it->cmp_it.nglyphs)
6710 it->cmp_it.from = it->cmp_it.to;
6711 else
6713 EMACS_INT stop = it->end_charpos;
6714 if (it->bidi_it.scan_dir < 0)
6715 stop = -1;
6716 composition_compute_stop_pos (&it->cmp_it,
6717 IT_STRING_CHARPOS (*it),
6718 IT_STRING_BYTEPOS (*it), stop,
6719 it->string);
6722 else
6724 for (i = 0; i < it->cmp_it.nchars; i++)
6725 bidi_move_to_visually_next (&it->bidi_it);
6726 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6727 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6728 if (it->cmp_it.from > 0)
6729 it->cmp_it.to = it->cmp_it.from;
6730 else
6732 EMACS_INT stop = it->end_charpos;
6733 if (it->bidi_it.scan_dir < 0)
6734 stop = -1;
6735 composition_compute_stop_pos (&it->cmp_it,
6736 IT_STRING_CHARPOS (*it),
6737 IT_STRING_BYTEPOS (*it), stop,
6738 it->string);
6742 else
6744 if (!it->bidi_p
6745 /* If the string position is beyond string's end, it
6746 means next_element_from_string is padding the string
6747 with blanks, in which case we bypass the bidi
6748 iterator, because it cannot deal with such virtual
6749 characters. */
6750 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
6752 IT_STRING_BYTEPOS (*it) += it->len;
6753 IT_STRING_CHARPOS (*it) += 1;
6755 else
6757 int prev_scan_dir = it->bidi_it.scan_dir;
6759 bidi_move_to_visually_next (&it->bidi_it);
6760 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6761 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6762 if (prev_scan_dir != it->bidi_it.scan_dir)
6764 EMACS_INT stop = it->end_charpos;
6766 if (it->bidi_it.scan_dir < 0)
6767 stop = -1;
6768 composition_compute_stop_pos (&it->cmp_it,
6769 IT_STRING_CHARPOS (*it),
6770 IT_STRING_BYTEPOS (*it), stop,
6771 it->string);
6776 consider_string_end:
6778 if (it->current.overlay_string_index >= 0)
6780 /* IT->string is an overlay string. Advance to the
6781 next, if there is one. */
6782 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6784 it->ellipsis_p = 0;
6785 next_overlay_string (it);
6786 if (it->ellipsis_p)
6787 setup_for_ellipsis (it, 0);
6790 else
6792 /* IT->string is not an overlay string. If we reached
6793 its end, and there is something on IT->stack, proceed
6794 with what is on the stack. This can be either another
6795 string, this time an overlay string, or a buffer. */
6796 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6797 && it->sp > 0)
6799 pop_it (it);
6800 if (it->method == GET_FROM_STRING)
6801 goto consider_string_end;
6804 break;
6806 case GET_FROM_IMAGE:
6807 case GET_FROM_STRETCH:
6808 /* The position etc with which we have to proceed are on
6809 the stack. The position may be at the end of a string,
6810 if the `display' property takes up the whole string. */
6811 xassert (it->sp > 0);
6812 pop_it (it);
6813 if (it->method == GET_FROM_STRING)
6814 goto consider_string_end;
6815 break;
6817 default:
6818 /* There are no other methods defined, so this should be a bug. */
6819 abort ();
6822 xassert (it->method != GET_FROM_STRING
6823 || (STRINGP (it->string)
6824 && IT_STRING_CHARPOS (*it) >= 0));
6827 /* Load IT's display element fields with information about the next
6828 display element which comes from a display table entry or from the
6829 result of translating a control character to one of the forms `^C'
6830 or `\003'.
6832 IT->dpvec holds the glyphs to return as characters.
6833 IT->saved_face_id holds the face id before the display vector--it
6834 is restored into IT->face_id in set_iterator_to_next. */
6836 static int
6837 next_element_from_display_vector (struct it *it)
6839 Lisp_Object gc;
6841 /* Precondition. */
6842 xassert (it->dpvec && it->current.dpvec_index >= 0);
6844 it->face_id = it->saved_face_id;
6846 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6847 That seemed totally bogus - so I changed it... */
6848 gc = it->dpvec[it->current.dpvec_index];
6850 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6852 it->c = GLYPH_CODE_CHAR (gc);
6853 it->len = CHAR_BYTES (it->c);
6855 /* The entry may contain a face id to use. Such a face id is
6856 the id of a Lisp face, not a realized face. A face id of
6857 zero means no face is specified. */
6858 if (it->dpvec_face_id >= 0)
6859 it->face_id = it->dpvec_face_id;
6860 else
6862 EMACS_INT lface_id = GLYPH_CODE_FACE (gc);
6863 if (lface_id > 0)
6864 it->face_id = merge_faces (it->f, Qt, lface_id,
6865 it->saved_face_id);
6868 else
6869 /* Display table entry is invalid. Return a space. */
6870 it->c = ' ', it->len = 1;
6872 /* Don't change position and object of the iterator here. They are
6873 still the values of the character that had this display table
6874 entry or was translated, and that's what we want. */
6875 it->what = IT_CHARACTER;
6876 return 1;
6879 /* Get the first element of string/buffer in the visual order, after
6880 being reseated to a new position in a string or a buffer. */
6881 static void
6882 get_visually_first_element (struct it *it)
6884 int string_p = STRINGP (it->string) || it->s;
6885 EMACS_INT eob = (string_p ? it->bidi_it.string.schars : ZV);
6886 EMACS_INT bob = (string_p ? 0 : BEGV);
6888 if (STRINGP (it->string))
6890 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
6891 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
6893 else
6895 it->bidi_it.charpos = IT_CHARPOS (*it);
6896 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6899 if (it->bidi_it.charpos == eob)
6901 /* Nothing to do, but reset the FIRST_ELT flag, like
6902 bidi_paragraph_init does, because we are not going to
6903 call it. */
6904 it->bidi_it.first_elt = 0;
6906 else if (it->bidi_it.charpos == bob
6907 || (!string_p
6908 /* FIXME: Should support all Unicode line separators. */
6909 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6910 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
6912 /* If we are at the beginning of a line/string, we can produce
6913 the next element right away. */
6914 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6915 bidi_move_to_visually_next (&it->bidi_it);
6917 else
6919 EMACS_INT orig_bytepos = it->bidi_it.bytepos;
6921 /* We need to prime the bidi iterator starting at the line's or
6922 string's beginning, before we will be able to produce the
6923 next element. */
6924 if (string_p)
6925 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
6926 else
6928 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
6929 -1);
6930 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
6932 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6935 /* Now return to buffer/string position where we were asked
6936 to get the next display element, and produce that. */
6937 bidi_move_to_visually_next (&it->bidi_it);
6939 while (it->bidi_it.bytepos != orig_bytepos
6940 && it->bidi_it.charpos < eob);
6943 /* Adjust IT's position information to where we ended up. */
6944 if (STRINGP (it->string))
6946 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6947 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6949 else
6951 IT_CHARPOS (*it) = it->bidi_it.charpos;
6952 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6955 if (STRINGP (it->string) || !it->s)
6957 EMACS_INT stop, charpos, bytepos;
6959 if (STRINGP (it->string))
6961 xassert (!it->s);
6962 stop = SCHARS (it->string);
6963 if (stop > it->end_charpos)
6964 stop = it->end_charpos;
6965 charpos = IT_STRING_CHARPOS (*it);
6966 bytepos = IT_STRING_BYTEPOS (*it);
6968 else
6970 stop = it->end_charpos;
6971 charpos = IT_CHARPOS (*it);
6972 bytepos = IT_BYTEPOS (*it);
6974 if (it->bidi_it.scan_dir < 0)
6975 stop = -1;
6976 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
6977 it->string);
6981 /* Load IT with the next display element from Lisp string IT->string.
6982 IT->current.string_pos is the current position within the string.
6983 If IT->current.overlay_string_index >= 0, the Lisp string is an
6984 overlay string. */
6986 static int
6987 next_element_from_string (struct it *it)
6989 struct text_pos position;
6991 xassert (STRINGP (it->string));
6992 xassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
6993 xassert (IT_STRING_CHARPOS (*it) >= 0);
6994 position = it->current.string_pos;
6996 /* With bidi reordering, the character to display might not be the
6997 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
6998 that we were reseat()ed to a new string, whose paragraph
6999 direction is not known. */
7000 if (it->bidi_p && it->bidi_it.first_elt)
7002 get_visually_first_element (it);
7003 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7006 /* Time to check for invisible text? */
7007 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7009 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7011 if (!(!it->bidi_p
7012 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7013 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7015 /* With bidi non-linear iteration, we could find
7016 ourselves far beyond the last computed stop_charpos,
7017 with several other stop positions in between that we
7018 missed. Scan them all now, in buffer's logical
7019 order, until we find and handle the last stop_charpos
7020 that precedes our current position. */
7021 handle_stop_backwards (it, it->stop_charpos);
7022 return GET_NEXT_DISPLAY_ELEMENT (it);
7024 else
7026 if (it->bidi_p)
7028 /* Take note of the stop position we just moved
7029 across, for when we will move back across it. */
7030 it->prev_stop = it->stop_charpos;
7031 /* If we are at base paragraph embedding level, take
7032 note of the last stop position seen at this
7033 level. */
7034 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7035 it->base_level_stop = it->stop_charpos;
7037 handle_stop (it);
7039 /* Since a handler may have changed IT->method, we must
7040 recurse here. */
7041 return GET_NEXT_DISPLAY_ELEMENT (it);
7044 else if (it->bidi_p
7045 /* If we are before prev_stop, we may have overstepped
7046 on our way backwards a stop_pos, and if so, we need
7047 to handle that stop_pos. */
7048 && IT_STRING_CHARPOS (*it) < it->prev_stop
7049 /* We can sometimes back up for reasons that have nothing
7050 to do with bidi reordering. E.g., compositions. The
7051 code below is only needed when we are above the base
7052 embedding level, so test for that explicitly. */
7053 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7055 /* If we lost track of base_level_stop, we have no better
7056 place for handle_stop_backwards to start from than string
7057 beginning. This happens, e.g., when we were reseated to
7058 the previous screenful of text by vertical-motion. */
7059 if (it->base_level_stop <= 0
7060 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7061 it->base_level_stop = 0;
7062 handle_stop_backwards (it, it->base_level_stop);
7063 return GET_NEXT_DISPLAY_ELEMENT (it);
7067 if (it->current.overlay_string_index >= 0)
7069 /* Get the next character from an overlay string. In overlay
7070 strings, There is no field width or padding with spaces to
7071 do. */
7072 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7074 it->what = IT_EOB;
7075 return 0;
7077 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7078 IT_STRING_BYTEPOS (*it),
7079 it->bidi_it.scan_dir < 0
7080 ? -1
7081 : SCHARS (it->string))
7082 && next_element_from_composition (it))
7084 return 1;
7086 else if (STRING_MULTIBYTE (it->string))
7088 const unsigned char *s = (SDATA (it->string)
7089 + IT_STRING_BYTEPOS (*it));
7090 it->c = string_char_and_length (s, &it->len);
7092 else
7094 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7095 it->len = 1;
7098 else
7100 /* Get the next character from a Lisp string that is not an
7101 overlay string. Such strings come from the mode line, for
7102 example. We may have to pad with spaces, or truncate the
7103 string. See also next_element_from_c_string. */
7104 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7106 it->what = IT_EOB;
7107 return 0;
7109 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7111 /* Pad with spaces. */
7112 it->c = ' ', it->len = 1;
7113 CHARPOS (position) = BYTEPOS (position) = -1;
7115 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7116 IT_STRING_BYTEPOS (*it),
7117 it->bidi_it.scan_dir < 0
7118 ? -1
7119 : it->string_nchars)
7120 && next_element_from_composition (it))
7122 return 1;
7124 else if (STRING_MULTIBYTE (it->string))
7126 const unsigned char *s = (SDATA (it->string)
7127 + IT_STRING_BYTEPOS (*it));
7128 it->c = string_char_and_length (s, &it->len);
7130 else
7132 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7133 it->len = 1;
7137 /* Record what we have and where it came from. */
7138 it->what = IT_CHARACTER;
7139 it->object = it->string;
7140 it->position = position;
7141 return 1;
7145 /* Load IT with next display element from C string IT->s.
7146 IT->string_nchars is the maximum number of characters to return
7147 from the string. IT->end_charpos may be greater than
7148 IT->string_nchars when this function is called, in which case we
7149 may have to return padding spaces. Value is zero if end of string
7150 reached, including padding spaces. */
7152 static int
7153 next_element_from_c_string (struct it *it)
7155 int success_p = 1;
7157 xassert (it->s);
7158 xassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7159 it->what = IT_CHARACTER;
7160 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7161 it->object = Qnil;
7163 /* With bidi reordering, the character to display might not be the
7164 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7165 we were reseated to a new string, whose paragraph direction is
7166 not known. */
7167 if (it->bidi_p && it->bidi_it.first_elt)
7168 get_visually_first_element (it);
7170 /* IT's position can be greater than IT->string_nchars in case a
7171 field width or precision has been specified when the iterator was
7172 initialized. */
7173 if (IT_CHARPOS (*it) >= it->end_charpos)
7175 /* End of the game. */
7176 it->what = IT_EOB;
7177 success_p = 0;
7179 else if (IT_CHARPOS (*it) >= it->string_nchars)
7181 /* Pad with spaces. */
7182 it->c = ' ', it->len = 1;
7183 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7185 else if (it->multibyte_p)
7186 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7187 else
7188 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7190 return success_p;
7194 /* Set up IT to return characters from an ellipsis, if appropriate.
7195 The definition of the ellipsis glyphs may come from a display table
7196 entry. This function fills IT with the first glyph from the
7197 ellipsis if an ellipsis is to be displayed. */
7199 static int
7200 next_element_from_ellipsis (struct it *it)
7202 if (it->selective_display_ellipsis_p)
7203 setup_for_ellipsis (it, it->len);
7204 else
7206 /* The face at the current position may be different from the
7207 face we find after the invisible text. Remember what it
7208 was in IT->saved_face_id, and signal that it's there by
7209 setting face_before_selective_p. */
7210 it->saved_face_id = it->face_id;
7211 it->method = GET_FROM_BUFFER;
7212 it->object = it->w->buffer;
7213 reseat_at_next_visible_line_start (it, 1);
7214 it->face_before_selective_p = 1;
7217 return GET_NEXT_DISPLAY_ELEMENT (it);
7221 /* Deliver an image display element. The iterator IT is already
7222 filled with image information (done in handle_display_prop). Value
7223 is always 1. */
7226 static int
7227 next_element_from_image (struct it *it)
7229 it->what = IT_IMAGE;
7230 it->ignore_overlay_strings_at_pos_p = 0;
7231 return 1;
7235 /* Fill iterator IT with next display element from a stretch glyph
7236 property. IT->object is the value of the text property. Value is
7237 always 1. */
7239 static int
7240 next_element_from_stretch (struct it *it)
7242 it->what = IT_STRETCH;
7243 return 1;
7246 /* Scan backwards from IT's current position until we find a stop
7247 position, or until BEGV. This is called when we find ourself
7248 before both the last known prev_stop and base_level_stop while
7249 reordering bidirectional text. */
7251 static void
7252 compute_stop_pos_backwards (struct it *it)
7254 const int SCAN_BACK_LIMIT = 1000;
7255 struct text_pos pos;
7256 struct display_pos save_current = it->current;
7257 struct text_pos save_position = it->position;
7258 EMACS_INT charpos = IT_CHARPOS (*it);
7259 EMACS_INT where_we_are = charpos;
7260 EMACS_INT save_stop_pos = it->stop_charpos;
7261 EMACS_INT save_end_pos = it->end_charpos;
7263 xassert (NILP (it->string) && !it->s);
7264 xassert (it->bidi_p);
7265 it->bidi_p = 0;
7268 it->end_charpos = min (charpos + 1, ZV);
7269 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7270 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7271 reseat_1 (it, pos, 0);
7272 compute_stop_pos (it);
7273 /* We must advance forward, right? */
7274 if (it->stop_charpos <= charpos)
7275 abort ();
7277 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7279 if (it->stop_charpos <= where_we_are)
7280 it->prev_stop = it->stop_charpos;
7281 else
7282 it->prev_stop = BEGV;
7283 it->bidi_p = 1;
7284 it->current = save_current;
7285 it->position = save_position;
7286 it->stop_charpos = save_stop_pos;
7287 it->end_charpos = save_end_pos;
7290 /* Scan forward from CHARPOS in the current buffer/string, until we
7291 find a stop position > current IT's position. Then handle the stop
7292 position before that. This is called when we bump into a stop
7293 position while reordering bidirectional text. CHARPOS should be
7294 the last previously processed stop_pos (or BEGV/0, if none were
7295 processed yet) whose position is less that IT's current
7296 position. */
7298 static void
7299 handle_stop_backwards (struct it *it, EMACS_INT charpos)
7301 int bufp = !STRINGP (it->string);
7302 EMACS_INT where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7303 struct display_pos save_current = it->current;
7304 struct text_pos save_position = it->position;
7305 struct text_pos pos1;
7306 EMACS_INT next_stop;
7308 /* Scan in strict logical order. */
7309 xassert (it->bidi_p);
7310 it->bidi_p = 0;
7313 it->prev_stop = charpos;
7314 if (bufp)
7316 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7317 reseat_1 (it, pos1, 0);
7319 else
7320 it->current.string_pos = string_pos (charpos, it->string);
7321 compute_stop_pos (it);
7322 /* We must advance forward, right? */
7323 if (it->stop_charpos <= it->prev_stop)
7324 abort ();
7325 charpos = it->stop_charpos;
7327 while (charpos <= where_we_are);
7329 it->bidi_p = 1;
7330 it->current = save_current;
7331 it->position = save_position;
7332 next_stop = it->stop_charpos;
7333 it->stop_charpos = it->prev_stop;
7334 handle_stop (it);
7335 it->stop_charpos = next_stop;
7338 /* Load IT with the next display element from current_buffer. Value
7339 is zero if end of buffer reached. IT->stop_charpos is the next
7340 position at which to stop and check for text properties or buffer
7341 end. */
7343 static int
7344 next_element_from_buffer (struct it *it)
7346 int success_p = 1;
7348 xassert (IT_CHARPOS (*it) >= BEGV);
7349 xassert (NILP (it->string) && !it->s);
7350 xassert (!it->bidi_p
7351 || (EQ (it->bidi_it.string.lstring, Qnil)
7352 && it->bidi_it.string.s == NULL));
7354 /* With bidi reordering, the character to display might not be the
7355 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7356 we were reseat()ed to a new buffer position, which is potentially
7357 a different paragraph. */
7358 if (it->bidi_p && it->bidi_it.first_elt)
7360 get_visually_first_element (it);
7361 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7364 if (IT_CHARPOS (*it) >= it->stop_charpos)
7366 if (IT_CHARPOS (*it) >= it->end_charpos)
7368 int overlay_strings_follow_p;
7370 /* End of the game, except when overlay strings follow that
7371 haven't been returned yet. */
7372 if (it->overlay_strings_at_end_processed_p)
7373 overlay_strings_follow_p = 0;
7374 else
7376 it->overlay_strings_at_end_processed_p = 1;
7377 overlay_strings_follow_p = get_overlay_strings (it, 0);
7380 if (overlay_strings_follow_p)
7381 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7382 else
7384 it->what = IT_EOB;
7385 it->position = it->current.pos;
7386 success_p = 0;
7389 else if (!(!it->bidi_p
7390 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7391 || IT_CHARPOS (*it) == it->stop_charpos))
7393 /* With bidi non-linear iteration, we could find ourselves
7394 far beyond the last computed stop_charpos, with several
7395 other stop positions in between that we missed. Scan
7396 them all now, in buffer's logical order, until we find
7397 and handle the last stop_charpos that precedes our
7398 current position. */
7399 handle_stop_backwards (it, it->stop_charpos);
7400 return GET_NEXT_DISPLAY_ELEMENT (it);
7402 else
7404 if (it->bidi_p)
7406 /* Take note of the stop position we just moved across,
7407 for when we will move back across it. */
7408 it->prev_stop = it->stop_charpos;
7409 /* If we are at base paragraph embedding level, take
7410 note of the last stop position seen at this
7411 level. */
7412 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7413 it->base_level_stop = it->stop_charpos;
7415 handle_stop (it);
7416 return GET_NEXT_DISPLAY_ELEMENT (it);
7419 else if (it->bidi_p
7420 /* If we are before prev_stop, we may have overstepped on
7421 our way backwards a stop_pos, and if so, we need to
7422 handle that stop_pos. */
7423 && IT_CHARPOS (*it) < it->prev_stop
7424 /* We can sometimes back up for reasons that have nothing
7425 to do with bidi reordering. E.g., compositions. The
7426 code below is only needed when we are above the base
7427 embedding level, so test for that explicitly. */
7428 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7430 if (it->base_level_stop <= 0
7431 || IT_CHARPOS (*it) < it->base_level_stop)
7433 /* If we lost track of base_level_stop, we need to find
7434 prev_stop by looking backwards. This happens, e.g., when
7435 we were reseated to the previous screenful of text by
7436 vertical-motion. */
7437 it->base_level_stop = BEGV;
7438 compute_stop_pos_backwards (it);
7439 handle_stop_backwards (it, it->prev_stop);
7441 else
7442 handle_stop_backwards (it, it->base_level_stop);
7443 return GET_NEXT_DISPLAY_ELEMENT (it);
7445 else
7447 /* No face changes, overlays etc. in sight, so just return a
7448 character from current_buffer. */
7449 unsigned char *p;
7450 EMACS_INT stop;
7452 /* Maybe run the redisplay end trigger hook. Performance note:
7453 This doesn't seem to cost measurable time. */
7454 if (it->redisplay_end_trigger_charpos
7455 && it->glyph_row
7456 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7457 run_redisplay_end_trigger_hook (it);
7459 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7460 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7461 stop)
7462 && next_element_from_composition (it))
7464 return 1;
7467 /* Get the next character, maybe multibyte. */
7468 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7469 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7470 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7471 else
7472 it->c = *p, it->len = 1;
7474 /* Record what we have and where it came from. */
7475 it->what = IT_CHARACTER;
7476 it->object = it->w->buffer;
7477 it->position = it->current.pos;
7479 /* Normally we return the character found above, except when we
7480 really want to return an ellipsis for selective display. */
7481 if (it->selective)
7483 if (it->c == '\n')
7485 /* A value of selective > 0 means hide lines indented more
7486 than that number of columns. */
7487 if (it->selective > 0
7488 && IT_CHARPOS (*it) + 1 < ZV
7489 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7490 IT_BYTEPOS (*it) + 1,
7491 it->selective))
7493 success_p = next_element_from_ellipsis (it);
7494 it->dpvec_char_len = -1;
7497 else if (it->c == '\r' && it->selective == -1)
7499 /* A value of selective == -1 means that everything from the
7500 CR to the end of the line is invisible, with maybe an
7501 ellipsis displayed for it. */
7502 success_p = next_element_from_ellipsis (it);
7503 it->dpvec_char_len = -1;
7508 /* Value is zero if end of buffer reached. */
7509 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7510 return success_p;
7514 /* Run the redisplay end trigger hook for IT. */
7516 static void
7517 run_redisplay_end_trigger_hook (struct it *it)
7519 Lisp_Object args[3];
7521 /* IT->glyph_row should be non-null, i.e. we should be actually
7522 displaying something, or otherwise we should not run the hook. */
7523 xassert (it->glyph_row);
7525 /* Set up hook arguments. */
7526 args[0] = Qredisplay_end_trigger_functions;
7527 args[1] = it->window;
7528 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7529 it->redisplay_end_trigger_charpos = 0;
7531 /* Since we are *trying* to run these functions, don't try to run
7532 them again, even if they get an error. */
7533 it->w->redisplay_end_trigger = Qnil;
7534 Frun_hook_with_args (3, args);
7536 /* Notice if it changed the face of the character we are on. */
7537 handle_face_prop (it);
7541 /* Deliver a composition display element. Unlike the other
7542 next_element_from_XXX, this function is not registered in the array
7543 get_next_element[]. It is called from next_element_from_buffer and
7544 next_element_from_string when necessary. */
7546 static int
7547 next_element_from_composition (struct it *it)
7549 it->what = IT_COMPOSITION;
7550 it->len = it->cmp_it.nbytes;
7551 if (STRINGP (it->string))
7553 if (it->c < 0)
7555 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7556 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7557 return 0;
7559 it->position = it->current.string_pos;
7560 it->object = it->string;
7561 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7562 IT_STRING_BYTEPOS (*it), it->string);
7564 else
7566 if (it->c < 0)
7568 IT_CHARPOS (*it) += it->cmp_it.nchars;
7569 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7570 if (it->bidi_p)
7572 if (it->bidi_it.new_paragraph)
7573 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7574 /* Resync the bidi iterator with IT's new position.
7575 FIXME: this doesn't support bidirectional text. */
7576 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7577 bidi_move_to_visually_next (&it->bidi_it);
7579 return 0;
7581 it->position = it->current.pos;
7582 it->object = it->w->buffer;
7583 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7584 IT_BYTEPOS (*it), Qnil);
7586 return 1;
7591 /***********************************************************************
7592 Moving an iterator without producing glyphs
7593 ***********************************************************************/
7595 /* Check if iterator is at a position corresponding to a valid buffer
7596 position after some move_it_ call. */
7598 #define IT_POS_VALID_AFTER_MOVE_P(it) \
7599 ((it)->method == GET_FROM_STRING \
7600 ? IT_STRING_CHARPOS (*it) == 0 \
7601 : 1)
7604 /* Move iterator IT to a specified buffer or X position within one
7605 line on the display without producing glyphs.
7607 OP should be a bit mask including some or all of these bits:
7608 MOVE_TO_X: Stop upon reaching x-position TO_X.
7609 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
7610 Regardless of OP's value, stop upon reaching the end of the display line.
7612 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
7613 This means, in particular, that TO_X includes window's horizontal
7614 scroll amount.
7616 The return value has several possible values that
7617 say what condition caused the scan to stop:
7619 MOVE_POS_MATCH_OR_ZV
7620 - when TO_POS or ZV was reached.
7622 MOVE_X_REACHED
7623 -when TO_X was reached before TO_POS or ZV were reached.
7625 MOVE_LINE_CONTINUED
7626 - when we reached the end of the display area and the line must
7627 be continued.
7629 MOVE_LINE_TRUNCATED
7630 - when we reached the end of the display area and the line is
7631 truncated.
7633 MOVE_NEWLINE_OR_CR
7634 - when we stopped at a line end, i.e. a newline or a CR and selective
7635 display is on. */
7637 static enum move_it_result
7638 move_it_in_display_line_to (struct it *it,
7639 EMACS_INT to_charpos, int to_x,
7640 enum move_operation_enum op)
7642 enum move_it_result result = MOVE_UNDEFINED;
7643 struct glyph_row *saved_glyph_row;
7644 struct it wrap_it, atpos_it, atx_it;
7645 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
7646 int may_wrap = 0;
7647 enum it_method prev_method = it->method;
7648 EMACS_INT prev_pos = IT_CHARPOS (*it);
7649 int saw_smaller_pos = prev_pos < to_charpos;
7651 /* Don't produce glyphs in produce_glyphs. */
7652 saved_glyph_row = it->glyph_row;
7653 it->glyph_row = NULL;
7655 /* Use wrap_it to save a copy of IT wherever a word wrap could
7656 occur. Use atpos_it to save a copy of IT at the desired buffer
7657 position, if found, so that we can scan ahead and check if the
7658 word later overshoots the window edge. Use atx_it similarly, for
7659 pixel positions. */
7660 wrap_it.sp = -1;
7661 atpos_it.sp = -1;
7662 atx_it.sp = -1;
7664 #define BUFFER_POS_REACHED_P() \
7665 ((op & MOVE_TO_POS) != 0 \
7666 && BUFFERP (it->object) \
7667 && (IT_CHARPOS (*it) == to_charpos \
7668 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos)) \
7669 && (it->method == GET_FROM_BUFFER \
7670 || (it->method == GET_FROM_DISPLAY_VECTOR \
7671 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
7673 /* If there's a line-/wrap-prefix, handle it. */
7674 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7675 && it->current_y < it->last_visible_y)
7676 handle_line_prefix (it);
7678 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7679 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7681 while (1)
7683 int x, i, ascent = 0, descent = 0;
7685 /* Utility macro to reset an iterator with x, ascent, and descent. */
7686 #define IT_RESET_X_ASCENT_DESCENT(IT) \
7687 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
7688 (IT)->max_descent = descent)
7690 /* Stop if we move beyond TO_CHARPOS (after an image or a
7691 display string or stretch glyph). */
7692 if ((op & MOVE_TO_POS) != 0
7693 && BUFFERP (it->object)
7694 && it->method == GET_FROM_BUFFER
7695 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
7696 || (it->bidi_p
7697 && (prev_method == GET_FROM_IMAGE
7698 || prev_method == GET_FROM_STRETCH
7699 || prev_method == GET_FROM_STRING)
7700 /* Passed TO_CHARPOS from left to right. */
7701 && ((prev_pos < to_charpos
7702 && IT_CHARPOS (*it) > to_charpos)
7703 /* Passed TO_CHARPOS from right to left. */
7704 || (prev_pos > to_charpos
7705 && IT_CHARPOS (*it) < to_charpos)))))
7707 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7709 result = MOVE_POS_MATCH_OR_ZV;
7710 break;
7712 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7713 /* If wrap_it is valid, the current position might be in a
7714 word that is wrapped. So, save the iterator in
7715 atpos_it and continue to see if wrapping happens. */
7716 SAVE_IT (atpos_it, *it, atpos_data);
7719 /* Stop when ZV reached.
7720 We used to stop here when TO_CHARPOS reached as well, but that is
7721 too soon if this glyph does not fit on this line. So we handle it
7722 explicitly below. */
7723 if (!get_next_display_element (it))
7725 result = MOVE_POS_MATCH_OR_ZV;
7726 break;
7729 if (it->line_wrap == TRUNCATE)
7731 if (BUFFER_POS_REACHED_P ())
7733 result = MOVE_POS_MATCH_OR_ZV;
7734 break;
7737 else
7739 if (it->line_wrap == WORD_WRAP)
7741 if (IT_DISPLAYING_WHITESPACE (it))
7742 may_wrap = 1;
7743 else if (may_wrap)
7745 /* We have reached a glyph that follows one or more
7746 whitespace characters. If the position is
7747 already found, we are done. */
7748 if (atpos_it.sp >= 0)
7750 RESTORE_IT (it, &atpos_it, atpos_data);
7751 result = MOVE_POS_MATCH_OR_ZV;
7752 goto done;
7754 if (atx_it.sp >= 0)
7756 RESTORE_IT (it, &atx_it, atx_data);
7757 result = MOVE_X_REACHED;
7758 goto done;
7760 /* Otherwise, we can wrap here. */
7761 SAVE_IT (wrap_it, *it, wrap_data);
7762 may_wrap = 0;
7767 /* Remember the line height for the current line, in case
7768 the next element doesn't fit on the line. */
7769 ascent = it->max_ascent;
7770 descent = it->max_descent;
7772 /* The call to produce_glyphs will get the metrics of the
7773 display element IT is loaded with. Record the x-position
7774 before this display element, in case it doesn't fit on the
7775 line. */
7776 x = it->current_x;
7778 PRODUCE_GLYPHS (it);
7780 if (it->area != TEXT_AREA)
7782 prev_method = it->method;
7783 if (it->method == GET_FROM_BUFFER)
7784 prev_pos = IT_CHARPOS (*it);
7785 set_iterator_to_next (it, 1);
7786 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7787 SET_TEXT_POS (this_line_min_pos,
7788 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7789 continue;
7792 /* The number of glyphs we get back in IT->nglyphs will normally
7793 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7794 character on a terminal frame, or (iii) a line end. For the
7795 second case, IT->nglyphs - 1 padding glyphs will be present.
7796 (On X frames, there is only one glyph produced for a
7797 composite character.)
7799 The behavior implemented below means, for continuation lines,
7800 that as many spaces of a TAB as fit on the current line are
7801 displayed there. For terminal frames, as many glyphs of a
7802 multi-glyph character are displayed in the current line, too.
7803 This is what the old redisplay code did, and we keep it that
7804 way. Under X, the whole shape of a complex character must
7805 fit on the line or it will be completely displayed in the
7806 next line.
7808 Note that both for tabs and padding glyphs, all glyphs have
7809 the same width. */
7810 if (it->nglyphs)
7812 /* More than one glyph or glyph doesn't fit on line. All
7813 glyphs have the same width. */
7814 int single_glyph_width = it->pixel_width / it->nglyphs;
7815 int new_x;
7816 int x_before_this_char = x;
7817 int hpos_before_this_char = it->hpos;
7819 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7821 new_x = x + single_glyph_width;
7823 /* We want to leave anything reaching TO_X to the caller. */
7824 if ((op & MOVE_TO_X) && new_x > to_x)
7826 if (BUFFER_POS_REACHED_P ())
7828 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7829 goto buffer_pos_reached;
7830 if (atpos_it.sp < 0)
7832 SAVE_IT (atpos_it, *it, atpos_data);
7833 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7836 else
7838 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7840 it->current_x = x;
7841 result = MOVE_X_REACHED;
7842 break;
7844 if (atx_it.sp < 0)
7846 SAVE_IT (atx_it, *it, atx_data);
7847 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7852 if (/* Lines are continued. */
7853 it->line_wrap != TRUNCATE
7854 && (/* And glyph doesn't fit on the line. */
7855 new_x > it->last_visible_x
7856 /* Or it fits exactly and we're on a window
7857 system frame. */
7858 || (new_x == it->last_visible_x
7859 && FRAME_WINDOW_P (it->f))))
7861 if (/* IT->hpos == 0 means the very first glyph
7862 doesn't fit on the line, e.g. a wide image. */
7863 it->hpos == 0
7864 || (new_x == it->last_visible_x
7865 && FRAME_WINDOW_P (it->f)))
7867 ++it->hpos;
7868 it->current_x = new_x;
7870 /* The character's last glyph just barely fits
7871 in this row. */
7872 if (i == it->nglyphs - 1)
7874 /* If this is the destination position,
7875 return a position *before* it in this row,
7876 now that we know it fits in this row. */
7877 if (BUFFER_POS_REACHED_P ())
7879 if (it->line_wrap != WORD_WRAP
7880 || wrap_it.sp < 0)
7882 it->hpos = hpos_before_this_char;
7883 it->current_x = x_before_this_char;
7884 result = MOVE_POS_MATCH_OR_ZV;
7885 break;
7887 if (it->line_wrap == WORD_WRAP
7888 && atpos_it.sp < 0)
7890 SAVE_IT (atpos_it, *it, atpos_data);
7891 atpos_it.current_x = x_before_this_char;
7892 atpos_it.hpos = hpos_before_this_char;
7896 prev_method = it->method;
7897 if (it->method == GET_FROM_BUFFER)
7898 prev_pos = IT_CHARPOS (*it);
7899 set_iterator_to_next (it, 1);
7900 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7901 SET_TEXT_POS (this_line_min_pos,
7902 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7903 /* On graphical terminals, newlines may
7904 "overflow" into the fringe if
7905 overflow-newline-into-fringe is non-nil.
7906 On text-only terminals, newlines may
7907 overflow into the last glyph on the
7908 display line.*/
7909 if (!FRAME_WINDOW_P (it->f)
7910 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7912 if (!get_next_display_element (it))
7914 result = MOVE_POS_MATCH_OR_ZV;
7915 break;
7917 if (BUFFER_POS_REACHED_P ())
7919 if (ITERATOR_AT_END_OF_LINE_P (it))
7920 result = MOVE_POS_MATCH_OR_ZV;
7921 else
7922 result = MOVE_LINE_CONTINUED;
7923 break;
7925 if (ITERATOR_AT_END_OF_LINE_P (it))
7927 result = MOVE_NEWLINE_OR_CR;
7928 break;
7933 else
7934 IT_RESET_X_ASCENT_DESCENT (it);
7936 if (wrap_it.sp >= 0)
7938 RESTORE_IT (it, &wrap_it, wrap_data);
7939 atpos_it.sp = -1;
7940 atx_it.sp = -1;
7943 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7944 IT_CHARPOS (*it)));
7945 result = MOVE_LINE_CONTINUED;
7946 break;
7949 if (BUFFER_POS_REACHED_P ())
7951 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7952 goto buffer_pos_reached;
7953 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7955 SAVE_IT (atpos_it, *it, atpos_data);
7956 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7960 if (new_x > it->first_visible_x)
7962 /* Glyph is visible. Increment number of glyphs that
7963 would be displayed. */
7964 ++it->hpos;
7968 if (result != MOVE_UNDEFINED)
7969 break;
7971 else if (BUFFER_POS_REACHED_P ())
7973 buffer_pos_reached:
7974 IT_RESET_X_ASCENT_DESCENT (it);
7975 result = MOVE_POS_MATCH_OR_ZV;
7976 break;
7978 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7980 /* Stop when TO_X specified and reached. This check is
7981 necessary here because of lines consisting of a line end,
7982 only. The line end will not produce any glyphs and we
7983 would never get MOVE_X_REACHED. */
7984 xassert (it->nglyphs == 0);
7985 result = MOVE_X_REACHED;
7986 break;
7989 /* Is this a line end? If yes, we're done. */
7990 if (ITERATOR_AT_END_OF_LINE_P (it))
7992 /* If we are past TO_CHARPOS, but never saw any character
7993 positions smaller than TO_CHARPOS, return
7994 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
7995 did. */
7996 if ((op & MOVE_TO_POS) != 0
7997 && !saw_smaller_pos
7998 && IT_CHARPOS (*it) > to_charpos)
7999 result = MOVE_POS_MATCH_OR_ZV;
8000 else
8001 result = MOVE_NEWLINE_OR_CR;
8002 break;
8005 prev_method = it->method;
8006 if (it->method == GET_FROM_BUFFER)
8007 prev_pos = IT_CHARPOS (*it);
8008 /* The current display element has been consumed. Advance
8009 to the next. */
8010 set_iterator_to_next (it, 1);
8011 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8012 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8013 if (IT_CHARPOS (*it) < to_charpos)
8014 saw_smaller_pos = 1;
8016 /* Stop if lines are truncated and IT's current x-position is
8017 past the right edge of the window now. */
8018 if (it->line_wrap == TRUNCATE
8019 && it->current_x >= it->last_visible_x)
8021 if (!FRAME_WINDOW_P (it->f)
8022 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8024 if (!get_next_display_element (it)
8025 || BUFFER_POS_REACHED_P ()
8026 /* If we are past TO_CHARPOS, but never saw any
8027 character positions smaller than TO_CHARPOS,
8028 return MOVE_POS_MATCH_OR_ZV, like the
8029 unidirectional display did. */
8030 || ((op & MOVE_TO_POS) != 0
8031 && !saw_smaller_pos
8032 && IT_CHARPOS (*it) > to_charpos))
8034 result = MOVE_POS_MATCH_OR_ZV;
8035 break;
8037 if (ITERATOR_AT_END_OF_LINE_P (it))
8039 result = MOVE_NEWLINE_OR_CR;
8040 break;
8043 else if ((op & MOVE_TO_POS) != 0
8044 && !saw_smaller_pos
8045 && IT_CHARPOS (*it) > to_charpos)
8047 result = MOVE_POS_MATCH_OR_ZV;
8048 break;
8050 result = MOVE_LINE_TRUNCATED;
8051 break;
8053 #undef IT_RESET_X_ASCENT_DESCENT
8056 #undef BUFFER_POS_REACHED_P
8058 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8059 restore the saved iterator. */
8060 if (atpos_it.sp >= 0)
8061 RESTORE_IT (it, &atpos_it, atpos_data);
8062 else if (atx_it.sp >= 0)
8063 RESTORE_IT (it, &atx_it, atx_data);
8065 done:
8067 if (atpos_data)
8068 xfree (atpos_data);
8069 if (atx_data)
8070 xfree (atx_data);
8071 if (wrap_data)
8072 xfree (wrap_data);
8074 /* Restore the iterator settings altered at the beginning of this
8075 function. */
8076 it->glyph_row = saved_glyph_row;
8077 return result;
8080 /* For external use. */
8081 void
8082 move_it_in_display_line (struct it *it,
8083 EMACS_INT to_charpos, int to_x,
8084 enum move_operation_enum op)
8086 if (it->line_wrap == WORD_WRAP
8087 && (op & MOVE_TO_X))
8089 struct it save_it;
8090 void *save_data = NULL;
8091 int skip;
8093 SAVE_IT (save_it, *it, save_data);
8094 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8095 /* When word-wrap is on, TO_X may lie past the end
8096 of a wrapped line. Then it->current is the
8097 character on the next line, so backtrack to the
8098 space before the wrap point. */
8099 if (skip == MOVE_LINE_CONTINUED)
8101 int prev_x = max (it->current_x - 1, 0);
8102 RESTORE_IT (it, &save_it, save_data);
8103 move_it_in_display_line_to
8104 (it, -1, prev_x, MOVE_TO_X);
8106 else
8107 xfree (save_data);
8109 else
8110 move_it_in_display_line_to (it, to_charpos, to_x, op);
8114 /* Move IT forward until it satisfies one or more of the criteria in
8115 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8117 OP is a bit-mask that specifies where to stop, and in particular,
8118 which of those four position arguments makes a difference. See the
8119 description of enum move_operation_enum.
8121 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8122 screen line, this function will set IT to the next position that is
8123 displayed to the right of TO_CHARPOS on the screen. */
8125 void
8126 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
8128 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8129 int line_height, line_start_x = 0, reached = 0;
8130 void *backup_data = NULL;
8132 for (;;)
8134 if (op & MOVE_TO_VPOS)
8136 /* If no TO_CHARPOS and no TO_X specified, stop at the
8137 start of the line TO_VPOS. */
8138 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8140 if (it->vpos == to_vpos)
8142 reached = 1;
8143 break;
8145 else
8146 skip = move_it_in_display_line_to (it, -1, -1, 0);
8148 else
8150 /* TO_VPOS >= 0 means stop at TO_X in the line at
8151 TO_VPOS, or at TO_POS, whichever comes first. */
8152 if (it->vpos == to_vpos)
8154 reached = 2;
8155 break;
8158 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8160 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8162 reached = 3;
8163 break;
8165 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8167 /* We have reached TO_X but not in the line we want. */
8168 skip = move_it_in_display_line_to (it, to_charpos,
8169 -1, MOVE_TO_POS);
8170 if (skip == MOVE_POS_MATCH_OR_ZV)
8172 reached = 4;
8173 break;
8178 else if (op & MOVE_TO_Y)
8180 struct it it_backup;
8182 if (it->line_wrap == WORD_WRAP)
8183 SAVE_IT (it_backup, *it, backup_data);
8185 /* TO_Y specified means stop at TO_X in the line containing
8186 TO_Y---or at TO_CHARPOS if this is reached first. The
8187 problem is that we can't really tell whether the line
8188 contains TO_Y before we have completely scanned it, and
8189 this may skip past TO_X. What we do is to first scan to
8190 TO_X.
8192 If TO_X is not specified, use a TO_X of zero. The reason
8193 is to make the outcome of this function more predictable.
8194 If we didn't use TO_X == 0, we would stop at the end of
8195 the line which is probably not what a caller would expect
8196 to happen. */
8197 skip = move_it_in_display_line_to
8198 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8199 (MOVE_TO_X | (op & MOVE_TO_POS)));
8201 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8202 if (skip == MOVE_POS_MATCH_OR_ZV)
8203 reached = 5;
8204 else if (skip == MOVE_X_REACHED)
8206 /* If TO_X was reached, we want to know whether TO_Y is
8207 in the line. We know this is the case if the already
8208 scanned glyphs make the line tall enough. Otherwise,
8209 we must check by scanning the rest of the line. */
8210 line_height = it->max_ascent + it->max_descent;
8211 if (to_y >= it->current_y
8212 && to_y < it->current_y + line_height)
8214 reached = 6;
8215 break;
8217 SAVE_IT (it_backup, *it, backup_data);
8218 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8219 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8220 op & MOVE_TO_POS);
8221 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8222 line_height = it->max_ascent + it->max_descent;
8223 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8225 if (to_y >= it->current_y
8226 && to_y < it->current_y + line_height)
8228 /* If TO_Y is in this line and TO_X was reached
8229 above, we scanned too far. We have to restore
8230 IT's settings to the ones before skipping. */
8231 RESTORE_IT (it, &it_backup, backup_data);
8232 reached = 6;
8234 else
8236 skip = skip2;
8237 if (skip == MOVE_POS_MATCH_OR_ZV)
8238 reached = 7;
8241 else
8243 /* Check whether TO_Y is in this line. */
8244 line_height = it->max_ascent + it->max_descent;
8245 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8247 if (to_y >= it->current_y
8248 && to_y < it->current_y + line_height)
8250 /* When word-wrap is on, TO_X may lie past the end
8251 of a wrapped line. Then it->current is the
8252 character on the next line, so backtrack to the
8253 space before the wrap point. */
8254 if (skip == MOVE_LINE_CONTINUED
8255 && it->line_wrap == WORD_WRAP)
8257 int prev_x = max (it->current_x - 1, 0);
8258 RESTORE_IT (it, &it_backup, backup_data);
8259 skip = move_it_in_display_line_to
8260 (it, -1, prev_x, MOVE_TO_X);
8262 reached = 6;
8266 if (reached)
8267 break;
8269 else if (BUFFERP (it->object)
8270 && (it->method == GET_FROM_BUFFER
8271 || it->method == GET_FROM_STRETCH)
8272 && IT_CHARPOS (*it) >= to_charpos)
8273 skip = MOVE_POS_MATCH_OR_ZV;
8274 else
8275 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8277 switch (skip)
8279 case MOVE_POS_MATCH_OR_ZV:
8280 reached = 8;
8281 goto out;
8283 case MOVE_NEWLINE_OR_CR:
8284 set_iterator_to_next (it, 1);
8285 it->continuation_lines_width = 0;
8286 break;
8288 case MOVE_LINE_TRUNCATED:
8289 it->continuation_lines_width = 0;
8290 reseat_at_next_visible_line_start (it, 0);
8291 if ((op & MOVE_TO_POS) != 0
8292 && IT_CHARPOS (*it) > to_charpos)
8294 reached = 9;
8295 goto out;
8297 break;
8299 case MOVE_LINE_CONTINUED:
8300 /* For continued lines ending in a tab, some of the glyphs
8301 associated with the tab are displayed on the current
8302 line. Since it->current_x does not include these glyphs,
8303 we use it->last_visible_x instead. */
8304 if (it->c == '\t')
8306 it->continuation_lines_width += it->last_visible_x;
8307 /* When moving by vpos, ensure that the iterator really
8308 advances to the next line (bug#847, bug#969). Fixme:
8309 do we need to do this in other circumstances? */
8310 if (it->current_x != it->last_visible_x
8311 && (op & MOVE_TO_VPOS)
8312 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8314 line_start_x = it->current_x + it->pixel_width
8315 - it->last_visible_x;
8316 set_iterator_to_next (it, 0);
8319 else
8320 it->continuation_lines_width += it->current_x;
8321 break;
8323 default:
8324 abort ();
8327 /* Reset/increment for the next run. */
8328 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8329 it->current_x = line_start_x;
8330 line_start_x = 0;
8331 it->hpos = 0;
8332 it->current_y += it->max_ascent + it->max_descent;
8333 ++it->vpos;
8334 last_height = it->max_ascent + it->max_descent;
8335 last_max_ascent = it->max_ascent;
8336 it->max_ascent = it->max_descent = 0;
8339 out:
8341 /* On text terminals, we may stop at the end of a line in the middle
8342 of a multi-character glyph. If the glyph itself is continued,
8343 i.e. it is actually displayed on the next line, don't treat this
8344 stopping point as valid; move to the next line instead (unless
8345 that brings us offscreen). */
8346 if (!FRAME_WINDOW_P (it->f)
8347 && op & MOVE_TO_POS
8348 && IT_CHARPOS (*it) == to_charpos
8349 && it->what == IT_CHARACTER
8350 && it->nglyphs > 1
8351 && it->line_wrap == WINDOW_WRAP
8352 && it->current_x == it->last_visible_x - 1
8353 && it->c != '\n'
8354 && it->c != '\t'
8355 && it->vpos < XFASTINT (it->w->window_end_vpos))
8357 it->continuation_lines_width += it->current_x;
8358 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8359 it->current_y += it->max_ascent + it->max_descent;
8360 ++it->vpos;
8361 last_height = it->max_ascent + it->max_descent;
8362 last_max_ascent = it->max_ascent;
8365 if (backup_data)
8366 xfree (backup_data);
8368 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8372 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8374 If DY > 0, move IT backward at least that many pixels. DY = 0
8375 means move IT backward to the preceding line start or BEGV. This
8376 function may move over more than DY pixels if IT->current_y - DY
8377 ends up in the middle of a line; in this case IT->current_y will be
8378 set to the top of the line moved to. */
8380 void
8381 move_it_vertically_backward (struct it *it, int dy)
8383 int nlines, h;
8384 struct it it2, it3;
8385 void *it2data = NULL, *it3data = NULL;
8386 EMACS_INT start_pos;
8388 move_further_back:
8389 xassert (dy >= 0);
8391 start_pos = IT_CHARPOS (*it);
8393 /* Estimate how many newlines we must move back. */
8394 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8396 /* Set the iterator's position that many lines back. */
8397 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8398 back_to_previous_visible_line_start (it);
8400 /* Reseat the iterator here. When moving backward, we don't want
8401 reseat to skip forward over invisible text, set up the iterator
8402 to deliver from overlay strings at the new position etc. So,
8403 use reseat_1 here. */
8404 reseat_1 (it, it->current.pos, 1);
8406 /* We are now surely at a line start. */
8407 it->current_x = it->hpos = 0;
8408 it->continuation_lines_width = 0;
8410 /* Move forward and see what y-distance we moved. First move to the
8411 start of the next line so that we get its height. We need this
8412 height to be able to tell whether we reached the specified
8413 y-distance. */
8414 SAVE_IT (it2, *it, it2data);
8415 it2.max_ascent = it2.max_descent = 0;
8418 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8419 MOVE_TO_POS | MOVE_TO_VPOS);
8421 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
8422 xassert (IT_CHARPOS (*it) >= BEGV);
8423 SAVE_IT (it3, it2, it3data);
8425 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8426 xassert (IT_CHARPOS (*it) >= BEGV);
8427 /* H is the actual vertical distance from the position in *IT
8428 and the starting position. */
8429 h = it2.current_y - it->current_y;
8430 /* NLINES is the distance in number of lines. */
8431 nlines = it2.vpos - it->vpos;
8433 /* Correct IT's y and vpos position
8434 so that they are relative to the starting point. */
8435 it->vpos -= nlines;
8436 it->current_y -= h;
8438 if (dy == 0)
8440 /* DY == 0 means move to the start of the screen line. The
8441 value of nlines is > 0 if continuation lines were involved. */
8442 RESTORE_IT (it, it, it2data);
8443 if (nlines > 0)
8444 move_it_by_lines (it, nlines);
8445 xfree (it3data);
8447 else
8449 /* The y-position we try to reach, relative to *IT.
8450 Note that H has been subtracted in front of the if-statement. */
8451 int target_y = it->current_y + h - dy;
8452 int y0 = it3.current_y;
8453 int y1;
8454 int line_height;
8456 RESTORE_IT (&it3, &it3, it3data);
8457 y1 = line_bottom_y (&it3);
8458 line_height = y1 - y0;
8459 RESTORE_IT (it, it, it2data);
8460 /* If we did not reach target_y, try to move further backward if
8461 we can. If we moved too far backward, try to move forward. */
8462 if (target_y < it->current_y
8463 /* This is heuristic. In a window that's 3 lines high, with
8464 a line height of 13 pixels each, recentering with point
8465 on the bottom line will try to move -39/2 = 19 pixels
8466 backward. Try to avoid moving into the first line. */
8467 && (it->current_y - target_y
8468 > min (window_box_height (it->w), line_height * 2 / 3))
8469 && IT_CHARPOS (*it) > BEGV)
8471 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8472 target_y - it->current_y));
8473 dy = it->current_y - target_y;
8474 goto move_further_back;
8476 else if (target_y >= it->current_y + line_height
8477 && IT_CHARPOS (*it) < ZV)
8479 /* Should move forward by at least one line, maybe more.
8481 Note: Calling move_it_by_lines can be expensive on
8482 terminal frames, where compute_motion is used (via
8483 vmotion) to do the job, when there are very long lines
8484 and truncate-lines is nil. That's the reason for
8485 treating terminal frames specially here. */
8487 if (!FRAME_WINDOW_P (it->f))
8488 move_it_vertically (it, target_y - (it->current_y + line_height));
8489 else
8493 move_it_by_lines (it, 1);
8495 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
8502 /* Move IT by a specified amount of pixel lines DY. DY negative means
8503 move backwards. DY = 0 means move to start of screen line. At the
8504 end, IT will be on the start of a screen line. */
8506 void
8507 move_it_vertically (struct it *it, int dy)
8509 if (dy <= 0)
8510 move_it_vertically_backward (it, -dy);
8511 else
8513 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
8514 move_it_to (it, ZV, -1, it->current_y + dy, -1,
8515 MOVE_TO_POS | MOVE_TO_Y);
8516 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
8518 /* If buffer ends in ZV without a newline, move to the start of
8519 the line to satisfy the post-condition. */
8520 if (IT_CHARPOS (*it) == ZV
8521 && ZV > BEGV
8522 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8523 move_it_by_lines (it, 0);
8528 /* Move iterator IT past the end of the text line it is in. */
8530 void
8531 move_it_past_eol (struct it *it)
8533 enum move_it_result rc;
8535 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
8536 if (rc == MOVE_NEWLINE_OR_CR)
8537 set_iterator_to_next (it, 0);
8541 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
8542 negative means move up. DVPOS == 0 means move to the start of the
8543 screen line.
8545 Optimization idea: If we would know that IT->f doesn't use
8546 a face with proportional font, we could be faster for
8547 truncate-lines nil. */
8549 void
8550 move_it_by_lines (struct it *it, int dvpos)
8553 /* The commented-out optimization uses vmotion on terminals. This
8554 gives bad results, because elements like it->what, on which
8555 callers such as pos_visible_p rely, aren't updated. */
8556 /* struct position pos;
8557 if (!FRAME_WINDOW_P (it->f))
8559 struct text_pos textpos;
8561 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
8562 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
8563 reseat (it, textpos, 1);
8564 it->vpos += pos.vpos;
8565 it->current_y += pos.vpos;
8567 else */
8569 if (dvpos == 0)
8571 /* DVPOS == 0 means move to the start of the screen line. */
8572 move_it_vertically_backward (it, 0);
8573 xassert (it->current_x == 0 && it->hpos == 0);
8574 /* Let next call to line_bottom_y calculate real line height */
8575 last_height = 0;
8577 else if (dvpos > 0)
8579 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
8580 if (!IT_POS_VALID_AFTER_MOVE_P (it))
8581 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
8583 else
8585 struct it it2;
8586 void *it2data = NULL;
8587 EMACS_INT start_charpos, i;
8589 /* Start at the beginning of the screen line containing IT's
8590 position. This may actually move vertically backwards,
8591 in case of overlays, so adjust dvpos accordingly. */
8592 dvpos += it->vpos;
8593 move_it_vertically_backward (it, 0);
8594 dvpos -= it->vpos;
8596 /* Go back -DVPOS visible lines and reseat the iterator there. */
8597 start_charpos = IT_CHARPOS (*it);
8598 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
8599 back_to_previous_visible_line_start (it);
8600 reseat (it, it->current.pos, 1);
8602 /* Move further back if we end up in a string or an image. */
8603 while (!IT_POS_VALID_AFTER_MOVE_P (it))
8605 /* First try to move to start of display line. */
8606 dvpos += it->vpos;
8607 move_it_vertically_backward (it, 0);
8608 dvpos -= it->vpos;
8609 if (IT_POS_VALID_AFTER_MOVE_P (it))
8610 break;
8611 /* If start of line is still in string or image,
8612 move further back. */
8613 back_to_previous_visible_line_start (it);
8614 reseat (it, it->current.pos, 1);
8615 dvpos--;
8618 it->current_x = it->hpos = 0;
8620 /* Above call may have moved too far if continuation lines
8621 are involved. Scan forward and see if it did. */
8622 SAVE_IT (it2, *it, it2data);
8623 it2.vpos = it2.current_y = 0;
8624 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
8625 it->vpos -= it2.vpos;
8626 it->current_y -= it2.current_y;
8627 it->current_x = it->hpos = 0;
8629 /* If we moved too far back, move IT some lines forward. */
8630 if (it2.vpos > -dvpos)
8632 int delta = it2.vpos + dvpos;
8634 RESTORE_IT (&it2, &it2, it2data);
8635 SAVE_IT (it2, *it, it2data);
8636 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
8637 /* Move back again if we got too far ahead. */
8638 if (IT_CHARPOS (*it) >= start_charpos)
8639 RESTORE_IT (it, &it2, it2data);
8640 else
8641 xfree (it2data);
8643 else
8644 RESTORE_IT (it, it, it2data);
8648 /* Return 1 if IT points into the middle of a display vector. */
8651 in_display_vector_p (struct it *it)
8653 return (it->method == GET_FROM_DISPLAY_VECTOR
8654 && it->current.dpvec_index > 0
8655 && it->dpvec + it->current.dpvec_index != it->dpend);
8659 /***********************************************************************
8660 Messages
8661 ***********************************************************************/
8664 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
8665 to *Messages*. */
8667 void
8668 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
8670 Lisp_Object args[3];
8671 Lisp_Object msg, fmt;
8672 char *buffer;
8673 EMACS_INT len;
8674 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
8675 USE_SAFE_ALLOCA;
8677 /* Do nothing if called asynchronously. Inserting text into
8678 a buffer may call after-change-functions and alike and
8679 that would means running Lisp asynchronously. */
8680 if (handling_signal)
8681 return;
8683 fmt = msg = Qnil;
8684 GCPRO4 (fmt, msg, arg1, arg2);
8686 args[0] = fmt = build_string (format);
8687 args[1] = arg1;
8688 args[2] = arg2;
8689 msg = Fformat (3, args);
8691 len = SBYTES (msg) + 1;
8692 SAFE_ALLOCA (buffer, char *, len);
8693 memcpy (buffer, SDATA (msg), len);
8695 message_dolog (buffer, len - 1, 1, 0);
8696 SAFE_FREE ();
8698 UNGCPRO;
8702 /* Output a newline in the *Messages* buffer if "needs" one. */
8704 void
8705 message_log_maybe_newline (void)
8707 if (message_log_need_newline)
8708 message_dolog ("", 0, 1, 0);
8712 /* Add a string M of length NBYTES to the message log, optionally
8713 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
8714 nonzero, means interpret the contents of M as multibyte. This
8715 function calls low-level routines in order to bypass text property
8716 hooks, etc. which might not be safe to run.
8718 This may GC (insert may run before/after change hooks),
8719 so the buffer M must NOT point to a Lisp string. */
8721 void
8722 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
8724 const unsigned char *msg = (const unsigned char *) m;
8726 if (!NILP (Vmemory_full))
8727 return;
8729 if (!NILP (Vmessage_log_max))
8731 struct buffer *oldbuf;
8732 Lisp_Object oldpoint, oldbegv, oldzv;
8733 int old_windows_or_buffers_changed = windows_or_buffers_changed;
8734 EMACS_INT point_at_end = 0;
8735 EMACS_INT zv_at_end = 0;
8736 Lisp_Object old_deactivate_mark, tem;
8737 struct gcpro gcpro1;
8739 old_deactivate_mark = Vdeactivate_mark;
8740 oldbuf = current_buffer;
8741 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
8742 BVAR (current_buffer, undo_list) = Qt;
8744 oldpoint = message_dolog_marker1;
8745 set_marker_restricted (oldpoint, make_number (PT), Qnil);
8746 oldbegv = message_dolog_marker2;
8747 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
8748 oldzv = message_dolog_marker3;
8749 set_marker_restricted (oldzv, make_number (ZV), Qnil);
8750 GCPRO1 (old_deactivate_mark);
8752 if (PT == Z)
8753 point_at_end = 1;
8754 if (ZV == Z)
8755 zv_at_end = 1;
8757 BEGV = BEG;
8758 BEGV_BYTE = BEG_BYTE;
8759 ZV = Z;
8760 ZV_BYTE = Z_BYTE;
8761 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8763 /* Insert the string--maybe converting multibyte to single byte
8764 or vice versa, so that all the text fits the buffer. */
8765 if (multibyte
8766 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
8768 EMACS_INT i;
8769 int c, char_bytes;
8770 char work[1];
8772 /* Convert a multibyte string to single-byte
8773 for the *Message* buffer. */
8774 for (i = 0; i < nbytes; i += char_bytes)
8776 c = string_char_and_length (msg + i, &char_bytes);
8777 work[0] = (ASCII_CHAR_P (c)
8779 : multibyte_char_to_unibyte (c));
8780 insert_1_both (work, 1, 1, 1, 0, 0);
8783 else if (! multibyte
8784 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
8786 EMACS_INT i;
8787 int c, char_bytes;
8788 unsigned char str[MAX_MULTIBYTE_LENGTH];
8789 /* Convert a single-byte string to multibyte
8790 for the *Message* buffer. */
8791 for (i = 0; i < nbytes; i++)
8793 c = msg[i];
8794 MAKE_CHAR_MULTIBYTE (c);
8795 char_bytes = CHAR_STRING (c, str);
8796 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
8799 else if (nbytes)
8800 insert_1 (m, nbytes, 1, 0, 0);
8802 if (nlflag)
8804 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
8805 intmax_t dups;
8806 insert_1 ("\n", 1, 1, 0, 0);
8808 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8809 this_bol = PT;
8810 this_bol_byte = PT_BYTE;
8812 /* See if this line duplicates the previous one.
8813 If so, combine duplicates. */
8814 if (this_bol > BEG)
8816 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8817 prev_bol = PT;
8818 prev_bol_byte = PT_BYTE;
8820 dups = message_log_check_duplicate (prev_bol_byte,
8821 this_bol_byte);
8822 if (dups)
8824 del_range_both (prev_bol, prev_bol_byte,
8825 this_bol, this_bol_byte, 0);
8826 if (dups > 1)
8828 char dupstr[sizeof " [ times]"
8829 + INT_STRLEN_BOUND (intmax_t)];
8830 int duplen;
8832 /* If you change this format, don't forget to also
8833 change message_log_check_duplicate. */
8834 sprintf (dupstr, " [%"PRIdMAX" times]", dups);
8835 duplen = strlen (dupstr);
8836 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8837 insert_1 (dupstr, duplen, 1, 0, 1);
8842 /* If we have more than the desired maximum number of lines
8843 in the *Messages* buffer now, delete the oldest ones.
8844 This is safe because we don't have undo in this buffer. */
8846 if (NATNUMP (Vmessage_log_max))
8848 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8849 -XFASTINT (Vmessage_log_max) - 1, 0);
8850 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8853 BEGV = XMARKER (oldbegv)->charpos;
8854 BEGV_BYTE = marker_byte_position (oldbegv);
8856 if (zv_at_end)
8858 ZV = Z;
8859 ZV_BYTE = Z_BYTE;
8861 else
8863 ZV = XMARKER (oldzv)->charpos;
8864 ZV_BYTE = marker_byte_position (oldzv);
8867 if (point_at_end)
8868 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8869 else
8870 /* We can't do Fgoto_char (oldpoint) because it will run some
8871 Lisp code. */
8872 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8873 XMARKER (oldpoint)->bytepos);
8875 UNGCPRO;
8876 unchain_marker (XMARKER (oldpoint));
8877 unchain_marker (XMARKER (oldbegv));
8878 unchain_marker (XMARKER (oldzv));
8880 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8881 set_buffer_internal (oldbuf);
8882 if (NILP (tem))
8883 windows_or_buffers_changed = old_windows_or_buffers_changed;
8884 message_log_need_newline = !nlflag;
8885 Vdeactivate_mark = old_deactivate_mark;
8890 /* We are at the end of the buffer after just having inserted a newline.
8891 (Note: We depend on the fact we won't be crossing the gap.)
8892 Check to see if the most recent message looks a lot like the previous one.
8893 Return 0 if different, 1 if the new one should just replace it, or a
8894 value N > 1 if we should also append " [N times]". */
8896 static intmax_t
8897 message_log_check_duplicate (EMACS_INT prev_bol_byte, EMACS_INT this_bol_byte)
8899 EMACS_INT i;
8900 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
8901 int seen_dots = 0;
8902 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8903 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8905 for (i = 0; i < len; i++)
8907 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8908 seen_dots = 1;
8909 if (p1[i] != p2[i])
8910 return seen_dots;
8912 p1 += len;
8913 if (*p1 == '\n')
8914 return 2;
8915 if (*p1++ == ' ' && *p1++ == '[')
8917 char *pend;
8918 intmax_t n = strtoimax ((char *) p1, &pend, 10);
8919 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
8920 return n+1;
8922 return 0;
8926 /* Display an echo area message M with a specified length of NBYTES
8927 bytes. The string may include null characters. If M is 0, clear
8928 out any existing message, and let the mini-buffer text show
8929 through.
8931 This may GC, so the buffer M must NOT point to a Lisp string. */
8933 void
8934 message2 (const char *m, EMACS_INT nbytes, int multibyte)
8936 /* First flush out any partial line written with print. */
8937 message_log_maybe_newline ();
8938 if (m)
8939 message_dolog (m, nbytes, 1, multibyte);
8940 message2_nolog (m, nbytes, multibyte);
8944 /* The non-logging counterpart of message2. */
8946 void
8947 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
8949 struct frame *sf = SELECTED_FRAME ();
8950 message_enable_multibyte = multibyte;
8952 if (FRAME_INITIAL_P (sf))
8954 if (noninteractive_need_newline)
8955 putc ('\n', stderr);
8956 noninteractive_need_newline = 0;
8957 if (m)
8958 fwrite (m, nbytes, 1, stderr);
8959 if (cursor_in_echo_area == 0)
8960 fprintf (stderr, "\n");
8961 fflush (stderr);
8963 /* A null message buffer means that the frame hasn't really been
8964 initialized yet. Error messages get reported properly by
8965 cmd_error, so this must be just an informative message; toss it. */
8966 else if (INTERACTIVE
8967 && sf->glyphs_initialized_p
8968 && FRAME_MESSAGE_BUF (sf))
8970 Lisp_Object mini_window;
8971 struct frame *f;
8973 /* Get the frame containing the mini-buffer
8974 that the selected frame is using. */
8975 mini_window = FRAME_MINIBUF_WINDOW (sf);
8976 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8978 FRAME_SAMPLE_VISIBILITY (f);
8979 if (FRAME_VISIBLE_P (sf)
8980 && ! FRAME_VISIBLE_P (f))
8981 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
8983 if (m)
8985 set_message (m, Qnil, nbytes, multibyte);
8986 if (minibuffer_auto_raise)
8987 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8989 else
8990 clear_message (1, 1);
8992 do_pending_window_change (0);
8993 echo_area_display (1);
8994 do_pending_window_change (0);
8995 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8996 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9001 /* Display an echo area message M with a specified length of NBYTES
9002 bytes. The string may include null characters. If M is not a
9003 string, clear out any existing message, and let the mini-buffer
9004 text show through.
9006 This function cancels echoing. */
9008 void
9009 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9011 struct gcpro gcpro1;
9013 GCPRO1 (m);
9014 clear_message (1,1);
9015 cancel_echoing ();
9017 /* First flush out any partial line written with print. */
9018 message_log_maybe_newline ();
9019 if (STRINGP (m))
9021 char *buffer;
9022 USE_SAFE_ALLOCA;
9024 SAFE_ALLOCA (buffer, char *, nbytes);
9025 memcpy (buffer, SDATA (m), nbytes);
9026 message_dolog (buffer, nbytes, 1, multibyte);
9027 SAFE_FREE ();
9029 message3_nolog (m, nbytes, multibyte);
9031 UNGCPRO;
9035 /* The non-logging version of message3.
9036 This does not cancel echoing, because it is used for echoing.
9037 Perhaps we need to make a separate function for echoing
9038 and make this cancel echoing. */
9040 void
9041 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9043 struct frame *sf = SELECTED_FRAME ();
9044 message_enable_multibyte = multibyte;
9046 if (FRAME_INITIAL_P (sf))
9048 if (noninteractive_need_newline)
9049 putc ('\n', stderr);
9050 noninteractive_need_newline = 0;
9051 if (STRINGP (m))
9052 fwrite (SDATA (m), nbytes, 1, stderr);
9053 if (cursor_in_echo_area == 0)
9054 fprintf (stderr, "\n");
9055 fflush (stderr);
9057 /* A null message buffer means that the frame hasn't really been
9058 initialized yet. Error messages get reported properly by
9059 cmd_error, so this must be just an informative message; toss it. */
9060 else if (INTERACTIVE
9061 && sf->glyphs_initialized_p
9062 && FRAME_MESSAGE_BUF (sf))
9064 Lisp_Object mini_window;
9065 Lisp_Object frame;
9066 struct frame *f;
9068 /* Get the frame containing the mini-buffer
9069 that the selected frame is using. */
9070 mini_window = FRAME_MINIBUF_WINDOW (sf);
9071 frame = XWINDOW (mini_window)->frame;
9072 f = XFRAME (frame);
9074 FRAME_SAMPLE_VISIBILITY (f);
9075 if (FRAME_VISIBLE_P (sf)
9076 && !FRAME_VISIBLE_P (f))
9077 Fmake_frame_visible (frame);
9079 if (STRINGP (m) && SCHARS (m) > 0)
9081 set_message (NULL, m, nbytes, multibyte);
9082 if (minibuffer_auto_raise)
9083 Fraise_frame (frame);
9084 /* Assume we are not echoing.
9085 (If we are, echo_now will override this.) */
9086 echo_message_buffer = Qnil;
9088 else
9089 clear_message (1, 1);
9091 do_pending_window_change (0);
9092 echo_area_display (1);
9093 do_pending_window_change (0);
9094 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9095 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9100 /* Display a null-terminated echo area message M. If M is 0, clear
9101 out any existing message, and let the mini-buffer text show through.
9103 The buffer M must continue to exist until after the echo area gets
9104 cleared or some other message gets displayed there. Do not pass
9105 text that is stored in a Lisp string. Do not pass text in a buffer
9106 that was alloca'd. */
9108 void
9109 message1 (const char *m)
9111 message2 (m, (m ? strlen (m) : 0), 0);
9115 /* The non-logging counterpart of message1. */
9117 void
9118 message1_nolog (const char *m)
9120 message2_nolog (m, (m ? strlen (m) : 0), 0);
9123 /* Display a message M which contains a single %s
9124 which gets replaced with STRING. */
9126 void
9127 message_with_string (const char *m, Lisp_Object string, int log)
9129 CHECK_STRING (string);
9131 if (noninteractive)
9133 if (m)
9135 if (noninteractive_need_newline)
9136 putc ('\n', stderr);
9137 noninteractive_need_newline = 0;
9138 fprintf (stderr, m, SDATA (string));
9139 if (!cursor_in_echo_area)
9140 fprintf (stderr, "\n");
9141 fflush (stderr);
9144 else if (INTERACTIVE)
9146 /* The frame whose minibuffer we're going to display the message on.
9147 It may be larger than the selected frame, so we need
9148 to use its buffer, not the selected frame's buffer. */
9149 Lisp_Object mini_window;
9150 struct frame *f, *sf = SELECTED_FRAME ();
9152 /* Get the frame containing the minibuffer
9153 that the selected frame is using. */
9154 mini_window = FRAME_MINIBUF_WINDOW (sf);
9155 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9157 /* A null message buffer means that the frame hasn't really been
9158 initialized yet. Error messages get reported properly by
9159 cmd_error, so this must be just an informative message; toss it. */
9160 if (FRAME_MESSAGE_BUF (f))
9162 Lisp_Object args[2], msg;
9163 struct gcpro gcpro1, gcpro2;
9165 args[0] = build_string (m);
9166 args[1] = msg = string;
9167 GCPRO2 (args[0], msg);
9168 gcpro1.nvars = 2;
9170 msg = Fformat (2, args);
9172 if (log)
9173 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9174 else
9175 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9177 UNGCPRO;
9179 /* Print should start at the beginning of the message
9180 buffer next time. */
9181 message_buf_print = 0;
9187 /* Dump an informative message to the minibuf. If M is 0, clear out
9188 any existing message, and let the mini-buffer text show through. */
9190 static void
9191 vmessage (const char *m, va_list ap)
9193 if (noninteractive)
9195 if (m)
9197 if (noninteractive_need_newline)
9198 putc ('\n', stderr);
9199 noninteractive_need_newline = 0;
9200 vfprintf (stderr, m, ap);
9201 if (cursor_in_echo_area == 0)
9202 fprintf (stderr, "\n");
9203 fflush (stderr);
9206 else if (INTERACTIVE)
9208 /* The frame whose mini-buffer we're going to display the message
9209 on. It may be larger than the selected frame, so we need to
9210 use its buffer, not the selected frame's buffer. */
9211 Lisp_Object mini_window;
9212 struct frame *f, *sf = SELECTED_FRAME ();
9214 /* Get the frame containing the mini-buffer
9215 that the selected frame is using. */
9216 mini_window = FRAME_MINIBUF_WINDOW (sf);
9217 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9219 /* A null message buffer means that the frame hasn't really been
9220 initialized yet. Error messages get reported properly by
9221 cmd_error, so this must be just an informative message; toss
9222 it. */
9223 if (FRAME_MESSAGE_BUF (f))
9225 if (m)
9227 size_t len;
9229 len = doprnt (FRAME_MESSAGE_BUF (f),
9230 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9232 message2 (FRAME_MESSAGE_BUF (f), len, 0);
9234 else
9235 message1 (0);
9237 /* Print should start at the beginning of the message
9238 buffer next time. */
9239 message_buf_print = 0;
9244 void
9245 message (const char *m, ...)
9247 va_list ap;
9248 va_start (ap, m);
9249 vmessage (m, ap);
9250 va_end (ap);
9254 #if 0
9255 /* The non-logging version of message. */
9257 void
9258 message_nolog (const char *m, ...)
9260 Lisp_Object old_log_max;
9261 va_list ap;
9262 va_start (ap, m);
9263 old_log_max = Vmessage_log_max;
9264 Vmessage_log_max = Qnil;
9265 vmessage (m, ap);
9266 Vmessage_log_max = old_log_max;
9267 va_end (ap);
9269 #endif
9272 /* Display the current message in the current mini-buffer. This is
9273 only called from error handlers in process.c, and is not time
9274 critical. */
9276 void
9277 update_echo_area (void)
9279 if (!NILP (echo_area_buffer[0]))
9281 Lisp_Object string;
9282 string = Fcurrent_message ();
9283 message3 (string, SBYTES (string),
9284 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9289 /* Make sure echo area buffers in `echo_buffers' are live.
9290 If they aren't, make new ones. */
9292 static void
9293 ensure_echo_area_buffers (void)
9295 int i;
9297 for (i = 0; i < 2; ++i)
9298 if (!BUFFERP (echo_buffer[i])
9299 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9301 char name[30];
9302 Lisp_Object old_buffer;
9303 int j;
9305 old_buffer = echo_buffer[i];
9306 sprintf (name, " *Echo Area %d*", i);
9307 echo_buffer[i] = Fget_buffer_create (build_string (name));
9308 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9309 /* to force word wrap in echo area -
9310 it was decided to postpone this*/
9311 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9313 for (j = 0; j < 2; ++j)
9314 if (EQ (old_buffer, echo_area_buffer[j]))
9315 echo_area_buffer[j] = echo_buffer[i];
9320 /* Call FN with args A1..A4 with either the current or last displayed
9321 echo_area_buffer as current buffer.
9323 WHICH zero means use the current message buffer
9324 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9325 from echo_buffer[] and clear it.
9327 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9328 suitable buffer from echo_buffer[] and clear it.
9330 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9331 that the current message becomes the last displayed one, make
9332 choose a suitable buffer for echo_area_buffer[0], and clear it.
9334 Value is what FN returns. */
9336 static int
9337 with_echo_area_buffer (struct window *w, int which,
9338 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
9339 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9341 Lisp_Object buffer;
9342 int this_one, the_other, clear_buffer_p, rc;
9343 int count = SPECPDL_INDEX ();
9345 /* If buffers aren't live, make new ones. */
9346 ensure_echo_area_buffers ();
9348 clear_buffer_p = 0;
9350 if (which == 0)
9351 this_one = 0, the_other = 1;
9352 else if (which > 0)
9353 this_one = 1, the_other = 0;
9354 else
9356 this_one = 0, the_other = 1;
9357 clear_buffer_p = 1;
9359 /* We need a fresh one in case the current echo buffer equals
9360 the one containing the last displayed echo area message. */
9361 if (!NILP (echo_area_buffer[this_one])
9362 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9363 echo_area_buffer[this_one] = Qnil;
9366 /* Choose a suitable buffer from echo_buffer[] is we don't
9367 have one. */
9368 if (NILP (echo_area_buffer[this_one]))
9370 echo_area_buffer[this_one]
9371 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9372 ? echo_buffer[the_other]
9373 : echo_buffer[this_one]);
9374 clear_buffer_p = 1;
9377 buffer = echo_area_buffer[this_one];
9379 /* Don't get confused by reusing the buffer used for echoing
9380 for a different purpose. */
9381 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9382 cancel_echoing ();
9384 record_unwind_protect (unwind_with_echo_area_buffer,
9385 with_echo_area_buffer_unwind_data (w));
9387 /* Make the echo area buffer current. Note that for display
9388 purposes, it is not necessary that the displayed window's buffer
9389 == current_buffer, except for text property lookup. So, let's
9390 only set that buffer temporarily here without doing a full
9391 Fset_window_buffer. We must also change w->pointm, though,
9392 because otherwise an assertions in unshow_buffer fails, and Emacs
9393 aborts. */
9394 set_buffer_internal_1 (XBUFFER (buffer));
9395 if (w)
9397 w->buffer = buffer;
9398 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9401 BVAR (current_buffer, undo_list) = Qt;
9402 BVAR (current_buffer, read_only) = Qnil;
9403 specbind (Qinhibit_read_only, Qt);
9404 specbind (Qinhibit_modification_hooks, Qt);
9406 if (clear_buffer_p && Z > BEG)
9407 del_range (BEG, Z);
9409 xassert (BEGV >= BEG);
9410 xassert (ZV <= Z && ZV >= BEGV);
9412 rc = fn (a1, a2, a3, a4);
9414 xassert (BEGV >= BEG);
9415 xassert (ZV <= Z && ZV >= BEGV);
9417 unbind_to (count, Qnil);
9418 return rc;
9422 /* Save state that should be preserved around the call to the function
9423 FN called in with_echo_area_buffer. */
9425 static Lisp_Object
9426 with_echo_area_buffer_unwind_data (struct window *w)
9428 int i = 0;
9429 Lisp_Object vector, tmp;
9431 /* Reduce consing by keeping one vector in
9432 Vwith_echo_area_save_vector. */
9433 vector = Vwith_echo_area_save_vector;
9434 Vwith_echo_area_save_vector = Qnil;
9436 if (NILP (vector))
9437 vector = Fmake_vector (make_number (7), Qnil);
9439 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9440 ASET (vector, i, Vdeactivate_mark); ++i;
9441 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9443 if (w)
9445 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9446 ASET (vector, i, w->buffer); ++i;
9447 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9448 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9450 else
9452 int end = i + 4;
9453 for (; i < end; ++i)
9454 ASET (vector, i, Qnil);
9457 xassert (i == ASIZE (vector));
9458 return vector;
9462 /* Restore global state from VECTOR which was created by
9463 with_echo_area_buffer_unwind_data. */
9465 static Lisp_Object
9466 unwind_with_echo_area_buffer (Lisp_Object vector)
9468 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
9469 Vdeactivate_mark = AREF (vector, 1);
9470 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
9472 if (WINDOWP (AREF (vector, 3)))
9474 struct window *w;
9475 Lisp_Object buffer, charpos, bytepos;
9477 w = XWINDOW (AREF (vector, 3));
9478 buffer = AREF (vector, 4);
9479 charpos = AREF (vector, 5);
9480 bytepos = AREF (vector, 6);
9482 w->buffer = buffer;
9483 set_marker_both (w->pointm, buffer,
9484 XFASTINT (charpos), XFASTINT (bytepos));
9487 Vwith_echo_area_save_vector = vector;
9488 return Qnil;
9492 /* Set up the echo area for use by print functions. MULTIBYTE_P
9493 non-zero means we will print multibyte. */
9495 void
9496 setup_echo_area_for_printing (int multibyte_p)
9498 /* If we can't find an echo area any more, exit. */
9499 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
9500 Fkill_emacs (Qnil);
9502 ensure_echo_area_buffers ();
9504 if (!message_buf_print)
9506 /* A message has been output since the last time we printed.
9507 Choose a fresh echo area buffer. */
9508 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9509 echo_area_buffer[0] = echo_buffer[1];
9510 else
9511 echo_area_buffer[0] = echo_buffer[0];
9513 /* Switch to that buffer and clear it. */
9514 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9515 BVAR (current_buffer, truncate_lines) = Qnil;
9517 if (Z > BEG)
9519 int count = SPECPDL_INDEX ();
9520 specbind (Qinhibit_read_only, Qt);
9521 /* Note that undo recording is always disabled. */
9522 del_range (BEG, Z);
9523 unbind_to (count, Qnil);
9525 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9527 /* Set up the buffer for the multibyteness we need. */
9528 if (multibyte_p
9529 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9530 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
9532 /* Raise the frame containing the echo area. */
9533 if (minibuffer_auto_raise)
9535 struct frame *sf = SELECTED_FRAME ();
9536 Lisp_Object mini_window;
9537 mini_window = FRAME_MINIBUF_WINDOW (sf);
9538 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9541 message_log_maybe_newline ();
9542 message_buf_print = 1;
9544 else
9546 if (NILP (echo_area_buffer[0]))
9548 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9549 echo_area_buffer[0] = echo_buffer[1];
9550 else
9551 echo_area_buffer[0] = echo_buffer[0];
9554 if (current_buffer != XBUFFER (echo_area_buffer[0]))
9556 /* Someone switched buffers between print requests. */
9557 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9558 BVAR (current_buffer, truncate_lines) = Qnil;
9564 /* Display an echo area message in window W. Value is non-zero if W's
9565 height is changed. If display_last_displayed_message_p is
9566 non-zero, display the message that was last displayed, otherwise
9567 display the current message. */
9569 static int
9570 display_echo_area (struct window *w)
9572 int i, no_message_p, window_height_changed_p, count;
9574 /* Temporarily disable garbage collections while displaying the echo
9575 area. This is done because a GC can print a message itself.
9576 That message would modify the echo area buffer's contents while a
9577 redisplay of the buffer is going on, and seriously confuse
9578 redisplay. */
9579 count = inhibit_garbage_collection ();
9581 /* If there is no message, we must call display_echo_area_1
9582 nevertheless because it resizes the window. But we will have to
9583 reset the echo_area_buffer in question to nil at the end because
9584 with_echo_area_buffer will sets it to an empty buffer. */
9585 i = display_last_displayed_message_p ? 1 : 0;
9586 no_message_p = NILP (echo_area_buffer[i]);
9588 window_height_changed_p
9589 = with_echo_area_buffer (w, display_last_displayed_message_p,
9590 display_echo_area_1,
9591 (intptr_t) w, Qnil, 0, 0);
9593 if (no_message_p)
9594 echo_area_buffer[i] = Qnil;
9596 unbind_to (count, Qnil);
9597 return window_height_changed_p;
9601 /* Helper for display_echo_area. Display the current buffer which
9602 contains the current echo area message in window W, a mini-window,
9603 a pointer to which is passed in A1. A2..A4 are currently not used.
9604 Change the height of W so that all of the message is displayed.
9605 Value is non-zero if height of W was changed. */
9607 static int
9608 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9610 intptr_t i1 = a1;
9611 struct window *w = (struct window *) i1;
9612 Lisp_Object window;
9613 struct text_pos start;
9614 int window_height_changed_p = 0;
9616 /* Do this before displaying, so that we have a large enough glyph
9617 matrix for the display. If we can't get enough space for the
9618 whole text, display the last N lines. That works by setting w->start. */
9619 window_height_changed_p = resize_mini_window (w, 0);
9621 /* Use the starting position chosen by resize_mini_window. */
9622 SET_TEXT_POS_FROM_MARKER (start, w->start);
9624 /* Display. */
9625 clear_glyph_matrix (w->desired_matrix);
9626 XSETWINDOW (window, w);
9627 try_window (window, start, 0);
9629 return window_height_changed_p;
9633 /* Resize the echo area window to exactly the size needed for the
9634 currently displayed message, if there is one. If a mini-buffer
9635 is active, don't shrink it. */
9637 void
9638 resize_echo_area_exactly (void)
9640 if (BUFFERP (echo_area_buffer[0])
9641 && WINDOWP (echo_area_window))
9643 struct window *w = XWINDOW (echo_area_window);
9644 int resized_p;
9645 Lisp_Object resize_exactly;
9647 if (minibuf_level == 0)
9648 resize_exactly = Qt;
9649 else
9650 resize_exactly = Qnil;
9652 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
9653 (intptr_t) w, resize_exactly,
9654 0, 0);
9655 if (resized_p)
9657 ++windows_or_buffers_changed;
9658 ++update_mode_lines;
9659 redisplay_internal ();
9665 /* Callback function for with_echo_area_buffer, when used from
9666 resize_echo_area_exactly. A1 contains a pointer to the window to
9667 resize, EXACTLY non-nil means resize the mini-window exactly to the
9668 size of the text displayed. A3 and A4 are not used. Value is what
9669 resize_mini_window returns. */
9671 static int
9672 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
9674 intptr_t i1 = a1;
9675 return resize_mini_window ((struct window *) i1, !NILP (exactly));
9679 /* Resize mini-window W to fit the size of its contents. EXACT_P
9680 means size the window exactly to the size needed. Otherwise, it's
9681 only enlarged until W's buffer is empty.
9683 Set W->start to the right place to begin display. If the whole
9684 contents fit, start at the beginning. Otherwise, start so as
9685 to make the end of the contents appear. This is particularly
9686 important for y-or-n-p, but seems desirable generally.
9688 Value is non-zero if the window height has been changed. */
9691 resize_mini_window (struct window *w, int exact_p)
9693 struct frame *f = XFRAME (w->frame);
9694 int window_height_changed_p = 0;
9696 xassert (MINI_WINDOW_P (w));
9698 /* By default, start display at the beginning. */
9699 set_marker_both (w->start, w->buffer,
9700 BUF_BEGV (XBUFFER (w->buffer)),
9701 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
9703 /* Don't resize windows while redisplaying a window; it would
9704 confuse redisplay functions when the size of the window they are
9705 displaying changes from under them. Such a resizing can happen,
9706 for instance, when which-func prints a long message while
9707 we are running fontification-functions. We're running these
9708 functions with safe_call which binds inhibit-redisplay to t. */
9709 if (!NILP (Vinhibit_redisplay))
9710 return 0;
9712 /* Nil means don't try to resize. */
9713 if (NILP (Vresize_mini_windows)
9714 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
9715 return 0;
9717 if (!FRAME_MINIBUF_ONLY_P (f))
9719 struct it it;
9720 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
9721 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
9722 int height, max_height;
9723 int unit = FRAME_LINE_HEIGHT (f);
9724 struct text_pos start;
9725 struct buffer *old_current_buffer = NULL;
9727 if (current_buffer != XBUFFER (w->buffer))
9729 old_current_buffer = current_buffer;
9730 set_buffer_internal (XBUFFER (w->buffer));
9733 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
9735 /* Compute the max. number of lines specified by the user. */
9736 if (FLOATP (Vmax_mini_window_height))
9737 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
9738 else if (INTEGERP (Vmax_mini_window_height))
9739 max_height = XINT (Vmax_mini_window_height);
9740 else
9741 max_height = total_height / 4;
9743 /* Correct that max. height if it's bogus. */
9744 max_height = max (1, max_height);
9745 max_height = min (total_height, max_height);
9747 /* Find out the height of the text in the window. */
9748 if (it.line_wrap == TRUNCATE)
9749 height = 1;
9750 else
9752 last_height = 0;
9753 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
9754 if (it.max_ascent == 0 && it.max_descent == 0)
9755 height = it.current_y + last_height;
9756 else
9757 height = it.current_y + it.max_ascent + it.max_descent;
9758 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
9759 height = (height + unit - 1) / unit;
9762 /* Compute a suitable window start. */
9763 if (height > max_height)
9765 height = max_height;
9766 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
9767 move_it_vertically_backward (&it, (height - 1) * unit);
9768 start = it.current.pos;
9770 else
9771 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
9772 SET_MARKER_FROM_TEXT_POS (w->start, start);
9774 if (EQ (Vresize_mini_windows, Qgrow_only))
9776 /* Let it grow only, until we display an empty message, in which
9777 case the window shrinks again. */
9778 if (height > WINDOW_TOTAL_LINES (w))
9780 int old_height = WINDOW_TOTAL_LINES (w);
9781 freeze_window_starts (f, 1);
9782 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9783 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9785 else if (height < WINDOW_TOTAL_LINES (w)
9786 && (exact_p || BEGV == ZV))
9788 int old_height = WINDOW_TOTAL_LINES (w);
9789 freeze_window_starts (f, 0);
9790 shrink_mini_window (w);
9791 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9794 else
9796 /* Always resize to exact size needed. */
9797 if (height > WINDOW_TOTAL_LINES (w))
9799 int old_height = WINDOW_TOTAL_LINES (w);
9800 freeze_window_starts (f, 1);
9801 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9802 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9804 else if (height < WINDOW_TOTAL_LINES (w))
9806 int old_height = WINDOW_TOTAL_LINES (w);
9807 freeze_window_starts (f, 0);
9808 shrink_mini_window (w);
9810 if (height)
9812 freeze_window_starts (f, 1);
9813 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9816 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9820 if (old_current_buffer)
9821 set_buffer_internal (old_current_buffer);
9824 return window_height_changed_p;
9828 /* Value is the current message, a string, or nil if there is no
9829 current message. */
9831 Lisp_Object
9832 current_message (void)
9834 Lisp_Object msg;
9836 if (!BUFFERP (echo_area_buffer[0]))
9837 msg = Qnil;
9838 else
9840 with_echo_area_buffer (0, 0, current_message_1,
9841 (intptr_t) &msg, Qnil, 0, 0);
9842 if (NILP (msg))
9843 echo_area_buffer[0] = Qnil;
9846 return msg;
9850 static int
9851 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9853 intptr_t i1 = a1;
9854 Lisp_Object *msg = (Lisp_Object *) i1;
9856 if (Z > BEG)
9857 *msg = make_buffer_string (BEG, Z, 1);
9858 else
9859 *msg = Qnil;
9860 return 0;
9864 /* Push the current message on Vmessage_stack for later restauration
9865 by restore_message. Value is non-zero if the current message isn't
9866 empty. This is a relatively infrequent operation, so it's not
9867 worth optimizing. */
9870 push_message (void)
9872 Lisp_Object msg;
9873 msg = current_message ();
9874 Vmessage_stack = Fcons (msg, Vmessage_stack);
9875 return STRINGP (msg);
9879 /* Restore message display from the top of Vmessage_stack. */
9881 void
9882 restore_message (void)
9884 Lisp_Object msg;
9886 xassert (CONSP (Vmessage_stack));
9887 msg = XCAR (Vmessage_stack);
9888 if (STRINGP (msg))
9889 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9890 else
9891 message3_nolog (msg, 0, 0);
9895 /* Handler for record_unwind_protect calling pop_message. */
9897 Lisp_Object
9898 pop_message_unwind (Lisp_Object dummy)
9900 pop_message ();
9901 return Qnil;
9904 /* Pop the top-most entry off Vmessage_stack. */
9906 static void
9907 pop_message (void)
9909 xassert (CONSP (Vmessage_stack));
9910 Vmessage_stack = XCDR (Vmessage_stack);
9914 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9915 exits. If the stack is not empty, we have a missing pop_message
9916 somewhere. */
9918 void
9919 check_message_stack (void)
9921 if (!NILP (Vmessage_stack))
9922 abort ();
9926 /* Truncate to NCHARS what will be displayed in the echo area the next
9927 time we display it---but don't redisplay it now. */
9929 void
9930 truncate_echo_area (EMACS_INT nchars)
9932 if (nchars == 0)
9933 echo_area_buffer[0] = Qnil;
9934 /* A null message buffer means that the frame hasn't really been
9935 initialized yet. Error messages get reported properly by
9936 cmd_error, so this must be just an informative message; toss it. */
9937 else if (!noninteractive
9938 && INTERACTIVE
9939 && !NILP (echo_area_buffer[0]))
9941 struct frame *sf = SELECTED_FRAME ();
9942 if (FRAME_MESSAGE_BUF (sf))
9943 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
9948 /* Helper function for truncate_echo_area. Truncate the current
9949 message to at most NCHARS characters. */
9951 static int
9952 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9954 if (BEG + nchars < Z)
9955 del_range (BEG + nchars, Z);
9956 if (Z == BEG)
9957 echo_area_buffer[0] = Qnil;
9958 return 0;
9962 /* Set the current message to a substring of S or STRING.
9964 If STRING is a Lisp string, set the message to the first NBYTES
9965 bytes from STRING. NBYTES zero means use the whole string. If
9966 STRING is multibyte, the message will be displayed multibyte.
9968 If S is not null, set the message to the first LEN bytes of S. LEN
9969 zero means use the whole string. MULTIBYTE_P non-zero means S is
9970 multibyte. Display the message multibyte in that case.
9972 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
9973 to t before calling set_message_1 (which calls insert).
9976 static void
9977 set_message (const char *s, Lisp_Object string,
9978 EMACS_INT nbytes, int multibyte_p)
9980 message_enable_multibyte
9981 = ((s && multibyte_p)
9982 || (STRINGP (string) && STRING_MULTIBYTE (string)));
9984 with_echo_area_buffer (0, -1, set_message_1,
9985 (intptr_t) s, string, nbytes, multibyte_p);
9986 message_buf_print = 0;
9987 help_echo_showing_p = 0;
9991 /* Helper function for set_message. Arguments have the same meaning
9992 as there, with A1 corresponding to S and A2 corresponding to STRING
9993 This function is called with the echo area buffer being
9994 current. */
9996 static int
9997 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
9999 intptr_t i1 = a1;
10000 const char *s = (const char *) i1;
10001 const unsigned char *msg = (const unsigned char *) s;
10002 Lisp_Object string = a2;
10004 /* Change multibyteness of the echo buffer appropriately. */
10005 if (message_enable_multibyte
10006 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10007 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10009 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10010 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10011 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10013 /* Insert new message at BEG. */
10014 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10016 if (STRINGP (string))
10018 EMACS_INT nchars;
10020 if (nbytes == 0)
10021 nbytes = SBYTES (string);
10022 nchars = string_byte_to_char (string, nbytes);
10024 /* This function takes care of single/multibyte conversion. We
10025 just have to ensure that the echo area buffer has the right
10026 setting of enable_multibyte_characters. */
10027 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10029 else if (s)
10031 if (nbytes == 0)
10032 nbytes = strlen (s);
10034 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10036 /* Convert from multi-byte to single-byte. */
10037 EMACS_INT i;
10038 int c, n;
10039 char work[1];
10041 /* Convert a multibyte string to single-byte. */
10042 for (i = 0; i < nbytes; i += n)
10044 c = string_char_and_length (msg + i, &n);
10045 work[0] = (ASCII_CHAR_P (c)
10047 : multibyte_char_to_unibyte (c));
10048 insert_1_both (work, 1, 1, 1, 0, 0);
10051 else if (!multibyte_p
10052 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10054 /* Convert from single-byte to multi-byte. */
10055 EMACS_INT i;
10056 int c, n;
10057 unsigned char str[MAX_MULTIBYTE_LENGTH];
10059 /* Convert a single-byte string to multibyte. */
10060 for (i = 0; i < nbytes; i++)
10062 c = msg[i];
10063 MAKE_CHAR_MULTIBYTE (c);
10064 n = CHAR_STRING (c, str);
10065 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10068 else
10069 insert_1 (s, nbytes, 1, 0, 0);
10072 return 0;
10076 /* Clear messages. CURRENT_P non-zero means clear the current
10077 message. LAST_DISPLAYED_P non-zero means clear the message
10078 last displayed. */
10080 void
10081 clear_message (int current_p, int last_displayed_p)
10083 if (current_p)
10085 echo_area_buffer[0] = Qnil;
10086 message_cleared_p = 1;
10089 if (last_displayed_p)
10090 echo_area_buffer[1] = Qnil;
10092 message_buf_print = 0;
10095 /* Clear garbaged frames.
10097 This function is used where the old redisplay called
10098 redraw_garbaged_frames which in turn called redraw_frame which in
10099 turn called clear_frame. The call to clear_frame was a source of
10100 flickering. I believe a clear_frame is not necessary. It should
10101 suffice in the new redisplay to invalidate all current matrices,
10102 and ensure a complete redisplay of all windows. */
10104 static void
10105 clear_garbaged_frames (void)
10107 if (frame_garbaged)
10109 Lisp_Object tail, frame;
10110 int changed_count = 0;
10112 FOR_EACH_FRAME (tail, frame)
10114 struct frame *f = XFRAME (frame);
10116 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10118 if (f->resized_p)
10120 Fredraw_frame (frame);
10121 f->force_flush_display_p = 1;
10123 clear_current_matrices (f);
10124 changed_count++;
10125 f->garbaged = 0;
10126 f->resized_p = 0;
10130 frame_garbaged = 0;
10131 if (changed_count)
10132 ++windows_or_buffers_changed;
10137 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10138 is non-zero update selected_frame. Value is non-zero if the
10139 mini-windows height has been changed. */
10141 static int
10142 echo_area_display (int update_frame_p)
10144 Lisp_Object mini_window;
10145 struct window *w;
10146 struct frame *f;
10147 int window_height_changed_p = 0;
10148 struct frame *sf = SELECTED_FRAME ();
10150 mini_window = FRAME_MINIBUF_WINDOW (sf);
10151 w = XWINDOW (mini_window);
10152 f = XFRAME (WINDOW_FRAME (w));
10154 /* Don't display if frame is invisible or not yet initialized. */
10155 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10156 return 0;
10158 #ifdef HAVE_WINDOW_SYSTEM
10159 /* When Emacs starts, selected_frame may be the initial terminal
10160 frame. If we let this through, a message would be displayed on
10161 the terminal. */
10162 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10163 return 0;
10164 #endif /* HAVE_WINDOW_SYSTEM */
10166 /* Redraw garbaged frames. */
10167 if (frame_garbaged)
10168 clear_garbaged_frames ();
10170 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10172 echo_area_window = mini_window;
10173 window_height_changed_p = display_echo_area (w);
10174 w->must_be_updated_p = 1;
10176 /* Update the display, unless called from redisplay_internal.
10177 Also don't update the screen during redisplay itself. The
10178 update will happen at the end of redisplay, and an update
10179 here could cause confusion. */
10180 if (update_frame_p && !redisplaying_p)
10182 int n = 0;
10184 /* If the display update has been interrupted by pending
10185 input, update mode lines in the frame. Due to the
10186 pending input, it might have been that redisplay hasn't
10187 been called, so that mode lines above the echo area are
10188 garbaged. This looks odd, so we prevent it here. */
10189 if (!display_completed)
10190 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10192 if (window_height_changed_p
10193 /* Don't do this if Emacs is shutting down. Redisplay
10194 needs to run hooks. */
10195 && !NILP (Vrun_hooks))
10197 /* Must update other windows. Likewise as in other
10198 cases, don't let this update be interrupted by
10199 pending input. */
10200 int count = SPECPDL_INDEX ();
10201 specbind (Qredisplay_dont_pause, Qt);
10202 windows_or_buffers_changed = 1;
10203 redisplay_internal ();
10204 unbind_to (count, Qnil);
10206 else if (FRAME_WINDOW_P (f) && n == 0)
10208 /* Window configuration is the same as before.
10209 Can do with a display update of the echo area,
10210 unless we displayed some mode lines. */
10211 update_single_window (w, 1);
10212 FRAME_RIF (f)->flush_display (f);
10214 else
10215 update_frame (f, 1, 1);
10217 /* If cursor is in the echo area, make sure that the next
10218 redisplay displays the minibuffer, so that the cursor will
10219 be replaced with what the minibuffer wants. */
10220 if (cursor_in_echo_area)
10221 ++windows_or_buffers_changed;
10224 else if (!EQ (mini_window, selected_window))
10225 windows_or_buffers_changed++;
10227 /* Last displayed message is now the current message. */
10228 echo_area_buffer[1] = echo_area_buffer[0];
10229 /* Inform read_char that we're not echoing. */
10230 echo_message_buffer = Qnil;
10232 /* Prevent redisplay optimization in redisplay_internal by resetting
10233 this_line_start_pos. This is done because the mini-buffer now
10234 displays the message instead of its buffer text. */
10235 if (EQ (mini_window, selected_window))
10236 CHARPOS (this_line_start_pos) = 0;
10238 return window_height_changed_p;
10243 /***********************************************************************
10244 Mode Lines and Frame Titles
10245 ***********************************************************************/
10247 /* A buffer for constructing non-propertized mode-line strings and
10248 frame titles in it; allocated from the heap in init_xdisp and
10249 resized as needed in store_mode_line_noprop_char. */
10251 static char *mode_line_noprop_buf;
10253 /* The buffer's end, and a current output position in it. */
10255 static char *mode_line_noprop_buf_end;
10256 static char *mode_line_noprop_ptr;
10258 #define MODE_LINE_NOPROP_LEN(start) \
10259 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10261 static enum {
10262 MODE_LINE_DISPLAY = 0,
10263 MODE_LINE_TITLE,
10264 MODE_LINE_NOPROP,
10265 MODE_LINE_STRING
10266 } mode_line_target;
10268 /* Alist that caches the results of :propertize.
10269 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10270 static Lisp_Object mode_line_proptrans_alist;
10272 /* List of strings making up the mode-line. */
10273 static Lisp_Object mode_line_string_list;
10275 /* Base face property when building propertized mode line string. */
10276 static Lisp_Object mode_line_string_face;
10277 static Lisp_Object mode_line_string_face_prop;
10280 /* Unwind data for mode line strings */
10282 static Lisp_Object Vmode_line_unwind_vector;
10284 static Lisp_Object
10285 format_mode_line_unwind_data (struct buffer *obuf,
10286 Lisp_Object owin,
10287 int save_proptrans)
10289 Lisp_Object vector, tmp;
10291 /* Reduce consing by keeping one vector in
10292 Vwith_echo_area_save_vector. */
10293 vector = Vmode_line_unwind_vector;
10294 Vmode_line_unwind_vector = Qnil;
10296 if (NILP (vector))
10297 vector = Fmake_vector (make_number (8), Qnil);
10299 ASET (vector, 0, make_number (mode_line_target));
10300 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10301 ASET (vector, 2, mode_line_string_list);
10302 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10303 ASET (vector, 4, mode_line_string_face);
10304 ASET (vector, 5, mode_line_string_face_prop);
10306 if (obuf)
10307 XSETBUFFER (tmp, obuf);
10308 else
10309 tmp = Qnil;
10310 ASET (vector, 6, tmp);
10311 ASET (vector, 7, owin);
10313 return vector;
10316 static Lisp_Object
10317 unwind_format_mode_line (Lisp_Object vector)
10319 mode_line_target = XINT (AREF (vector, 0));
10320 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10321 mode_line_string_list = AREF (vector, 2);
10322 if (! EQ (AREF (vector, 3), Qt))
10323 mode_line_proptrans_alist = AREF (vector, 3);
10324 mode_line_string_face = AREF (vector, 4);
10325 mode_line_string_face_prop = AREF (vector, 5);
10327 if (!NILP (AREF (vector, 7)))
10328 /* Select window before buffer, since it may change the buffer. */
10329 Fselect_window (AREF (vector, 7), Qt);
10331 if (!NILP (AREF (vector, 6)))
10333 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10334 ASET (vector, 6, Qnil);
10337 Vmode_line_unwind_vector = vector;
10338 return Qnil;
10342 /* Store a single character C for the frame title in mode_line_noprop_buf.
10343 Re-allocate mode_line_noprop_buf if necessary. */
10345 static void
10346 store_mode_line_noprop_char (char c)
10348 /* If output position has reached the end of the allocated buffer,
10349 double the buffer's size. */
10350 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10352 int len = MODE_LINE_NOPROP_LEN (0);
10353 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
10354 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
10355 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
10356 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10359 *mode_line_noprop_ptr++ = c;
10363 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10364 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10365 characters that yield more columns than PRECISION; PRECISION <= 0
10366 means copy the whole string. Pad with spaces until FIELD_WIDTH
10367 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10368 pad. Called from display_mode_element when it is used to build a
10369 frame title. */
10371 static int
10372 store_mode_line_noprop (const char *string, int field_width, int precision)
10374 const unsigned char *str = (const unsigned char *) string;
10375 int n = 0;
10376 EMACS_INT dummy, nbytes;
10378 /* Copy at most PRECISION chars from STR. */
10379 nbytes = strlen (string);
10380 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10381 while (nbytes--)
10382 store_mode_line_noprop_char (*str++);
10384 /* Fill up with spaces until FIELD_WIDTH reached. */
10385 while (field_width > 0
10386 && n < field_width)
10388 store_mode_line_noprop_char (' ');
10389 ++n;
10392 return n;
10395 /***********************************************************************
10396 Frame Titles
10397 ***********************************************************************/
10399 #ifdef HAVE_WINDOW_SYSTEM
10401 /* Set the title of FRAME, if it has changed. The title format is
10402 Vicon_title_format if FRAME is iconified, otherwise it is
10403 frame_title_format. */
10405 static void
10406 x_consider_frame_title (Lisp_Object frame)
10408 struct frame *f = XFRAME (frame);
10410 if (FRAME_WINDOW_P (f)
10411 || FRAME_MINIBUF_ONLY_P (f)
10412 || f->explicit_name)
10414 /* Do we have more than one visible frame on this X display? */
10415 Lisp_Object tail;
10416 Lisp_Object fmt;
10417 int title_start;
10418 char *title;
10419 int len;
10420 struct it it;
10421 int count = SPECPDL_INDEX ();
10423 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
10425 Lisp_Object other_frame = XCAR (tail);
10426 struct frame *tf = XFRAME (other_frame);
10428 if (tf != f
10429 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10430 && !FRAME_MINIBUF_ONLY_P (tf)
10431 && !EQ (other_frame, tip_frame)
10432 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10433 break;
10436 /* Set global variable indicating that multiple frames exist. */
10437 multiple_frames = CONSP (tail);
10439 /* Switch to the buffer of selected window of the frame. Set up
10440 mode_line_target so that display_mode_element will output into
10441 mode_line_noprop_buf; then display the title. */
10442 record_unwind_protect (unwind_format_mode_line,
10443 format_mode_line_unwind_data
10444 (current_buffer, selected_window, 0));
10446 Fselect_window (f->selected_window, Qt);
10447 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
10448 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
10450 mode_line_target = MODE_LINE_TITLE;
10451 title_start = MODE_LINE_NOPROP_LEN (0);
10452 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
10453 NULL, DEFAULT_FACE_ID);
10454 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
10455 len = MODE_LINE_NOPROP_LEN (title_start);
10456 title = mode_line_noprop_buf + title_start;
10457 unbind_to (count, Qnil);
10459 /* Set the title only if it's changed. This avoids consing in
10460 the common case where it hasn't. (If it turns out that we've
10461 already wasted too much time by walking through the list with
10462 display_mode_element, then we might need to optimize at a
10463 higher level than this.) */
10464 if (! STRINGP (f->name)
10465 || SBYTES (f->name) != len
10466 || memcmp (title, SDATA (f->name), len) != 0)
10467 x_implicitly_set_name (f, make_string (title, len), Qnil);
10471 #endif /* not HAVE_WINDOW_SYSTEM */
10476 /***********************************************************************
10477 Menu Bars
10478 ***********************************************************************/
10481 /* Prepare for redisplay by updating menu-bar item lists when
10482 appropriate. This can call eval. */
10484 void
10485 prepare_menu_bars (void)
10487 int all_windows;
10488 struct gcpro gcpro1, gcpro2;
10489 struct frame *f;
10490 Lisp_Object tooltip_frame;
10492 #ifdef HAVE_WINDOW_SYSTEM
10493 tooltip_frame = tip_frame;
10494 #else
10495 tooltip_frame = Qnil;
10496 #endif
10498 /* Update all frame titles based on their buffer names, etc. We do
10499 this before the menu bars so that the buffer-menu will show the
10500 up-to-date frame titles. */
10501 #ifdef HAVE_WINDOW_SYSTEM
10502 if (windows_or_buffers_changed || update_mode_lines)
10504 Lisp_Object tail, frame;
10506 FOR_EACH_FRAME (tail, frame)
10508 f = XFRAME (frame);
10509 if (!EQ (frame, tooltip_frame)
10510 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
10511 x_consider_frame_title (frame);
10514 #endif /* HAVE_WINDOW_SYSTEM */
10516 /* Update the menu bar item lists, if appropriate. This has to be
10517 done before any actual redisplay or generation of display lines. */
10518 all_windows = (update_mode_lines
10519 || buffer_shared > 1
10520 || windows_or_buffers_changed);
10521 if (all_windows)
10523 Lisp_Object tail, frame;
10524 int count = SPECPDL_INDEX ();
10525 /* 1 means that update_menu_bar has run its hooks
10526 so any further calls to update_menu_bar shouldn't do so again. */
10527 int menu_bar_hooks_run = 0;
10529 record_unwind_save_match_data ();
10531 FOR_EACH_FRAME (tail, frame)
10533 f = XFRAME (frame);
10535 /* Ignore tooltip frame. */
10536 if (EQ (frame, tooltip_frame))
10537 continue;
10539 /* If a window on this frame changed size, report that to
10540 the user and clear the size-change flag. */
10541 if (FRAME_WINDOW_SIZES_CHANGED (f))
10543 Lisp_Object functions;
10545 /* Clear flag first in case we get an error below. */
10546 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
10547 functions = Vwindow_size_change_functions;
10548 GCPRO2 (tail, functions);
10550 while (CONSP (functions))
10552 if (!EQ (XCAR (functions), Qt))
10553 call1 (XCAR (functions), frame);
10554 functions = XCDR (functions);
10556 UNGCPRO;
10559 GCPRO1 (tail);
10560 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
10561 #ifdef HAVE_WINDOW_SYSTEM
10562 update_tool_bar (f, 0);
10563 #endif
10564 #ifdef HAVE_NS
10565 if (windows_or_buffers_changed
10566 && FRAME_NS_P (f))
10567 ns_set_doc_edited (f, Fbuffer_modified_p
10568 (XWINDOW (f->selected_window)->buffer));
10569 #endif
10570 UNGCPRO;
10573 unbind_to (count, Qnil);
10575 else
10577 struct frame *sf = SELECTED_FRAME ();
10578 update_menu_bar (sf, 1, 0);
10579 #ifdef HAVE_WINDOW_SYSTEM
10580 update_tool_bar (sf, 1);
10581 #endif
10586 /* Update the menu bar item list for frame F. This has to be done
10587 before we start to fill in any display lines, because it can call
10588 eval.
10590 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
10592 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
10593 already ran the menu bar hooks for this redisplay, so there
10594 is no need to run them again. The return value is the
10595 updated value of this flag, to pass to the next call. */
10597 static int
10598 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
10600 Lisp_Object window;
10601 register struct window *w;
10603 /* If called recursively during a menu update, do nothing. This can
10604 happen when, for instance, an activate-menubar-hook causes a
10605 redisplay. */
10606 if (inhibit_menubar_update)
10607 return hooks_run;
10609 window = FRAME_SELECTED_WINDOW (f);
10610 w = XWINDOW (window);
10612 if (FRAME_WINDOW_P (f)
10614 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10615 || defined (HAVE_NS) || defined (USE_GTK)
10616 FRAME_EXTERNAL_MENU_BAR (f)
10617 #else
10618 FRAME_MENU_BAR_LINES (f) > 0
10619 #endif
10620 : FRAME_MENU_BAR_LINES (f) > 0)
10622 /* If the user has switched buffers or windows, we need to
10623 recompute to reflect the new bindings. But we'll
10624 recompute when update_mode_lines is set too; that means
10625 that people can use force-mode-line-update to request
10626 that the menu bar be recomputed. The adverse effect on
10627 the rest of the redisplay algorithm is about the same as
10628 windows_or_buffers_changed anyway. */
10629 if (windows_or_buffers_changed
10630 /* This used to test w->update_mode_line, but we believe
10631 there is no need to recompute the menu in that case. */
10632 || update_mode_lines
10633 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10634 < BUF_MODIFF (XBUFFER (w->buffer)))
10635 != !NILP (w->last_had_star))
10636 || ((!NILP (Vtransient_mark_mode)
10637 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
10638 != !NILP (w->region_showing)))
10640 struct buffer *prev = current_buffer;
10641 int count = SPECPDL_INDEX ();
10643 specbind (Qinhibit_menubar_update, Qt);
10645 set_buffer_internal_1 (XBUFFER (w->buffer));
10646 if (save_match_data)
10647 record_unwind_save_match_data ();
10648 if (NILP (Voverriding_local_map_menu_flag))
10650 specbind (Qoverriding_terminal_local_map, Qnil);
10651 specbind (Qoverriding_local_map, Qnil);
10654 if (!hooks_run)
10656 /* Run the Lucid hook. */
10657 safe_run_hooks (Qactivate_menubar_hook);
10659 /* If it has changed current-menubar from previous value,
10660 really recompute the menu-bar from the value. */
10661 if (! NILP (Vlucid_menu_bar_dirty_flag))
10662 call0 (Qrecompute_lucid_menubar);
10664 safe_run_hooks (Qmenu_bar_update_hook);
10666 hooks_run = 1;
10669 XSETFRAME (Vmenu_updating_frame, f);
10670 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
10672 /* Redisplay the menu bar in case we changed it. */
10673 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10674 || defined (HAVE_NS) || defined (USE_GTK)
10675 if (FRAME_WINDOW_P (f))
10677 #if defined (HAVE_NS)
10678 /* All frames on Mac OS share the same menubar. So only
10679 the selected frame should be allowed to set it. */
10680 if (f == SELECTED_FRAME ())
10681 #endif
10682 set_frame_menubar (f, 0, 0);
10684 else
10685 /* On a terminal screen, the menu bar is an ordinary screen
10686 line, and this makes it get updated. */
10687 w->update_mode_line = Qt;
10688 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10689 /* In the non-toolkit version, the menu bar is an ordinary screen
10690 line, and this makes it get updated. */
10691 w->update_mode_line = Qt;
10692 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10694 unbind_to (count, Qnil);
10695 set_buffer_internal_1 (prev);
10699 return hooks_run;
10704 /***********************************************************************
10705 Output Cursor
10706 ***********************************************************************/
10708 #ifdef HAVE_WINDOW_SYSTEM
10710 /* EXPORT:
10711 Nominal cursor position -- where to draw output.
10712 HPOS and VPOS are window relative glyph matrix coordinates.
10713 X and Y are window relative pixel coordinates. */
10715 struct cursor_pos output_cursor;
10718 /* EXPORT:
10719 Set the global variable output_cursor to CURSOR. All cursor
10720 positions are relative to updated_window. */
10722 void
10723 set_output_cursor (struct cursor_pos *cursor)
10725 output_cursor.hpos = cursor->hpos;
10726 output_cursor.vpos = cursor->vpos;
10727 output_cursor.x = cursor->x;
10728 output_cursor.y = cursor->y;
10732 /* EXPORT for RIF:
10733 Set a nominal cursor position.
10735 HPOS and VPOS are column/row positions in a window glyph matrix. X
10736 and Y are window text area relative pixel positions.
10738 If this is done during an update, updated_window will contain the
10739 window that is being updated and the position is the future output
10740 cursor position for that window. If updated_window is null, use
10741 selected_window and display the cursor at the given position. */
10743 void
10744 x_cursor_to (int vpos, int hpos, int y, int x)
10746 struct window *w;
10748 /* If updated_window is not set, work on selected_window. */
10749 if (updated_window)
10750 w = updated_window;
10751 else
10752 w = XWINDOW (selected_window);
10754 /* Set the output cursor. */
10755 output_cursor.hpos = hpos;
10756 output_cursor.vpos = vpos;
10757 output_cursor.x = x;
10758 output_cursor.y = y;
10760 /* If not called as part of an update, really display the cursor.
10761 This will also set the cursor position of W. */
10762 if (updated_window == NULL)
10764 BLOCK_INPUT;
10765 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10766 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10767 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10768 UNBLOCK_INPUT;
10772 #endif /* HAVE_WINDOW_SYSTEM */
10775 /***********************************************************************
10776 Tool-bars
10777 ***********************************************************************/
10779 #ifdef HAVE_WINDOW_SYSTEM
10781 /* Where the mouse was last time we reported a mouse event. */
10783 FRAME_PTR last_mouse_frame;
10785 /* Tool-bar item index of the item on which a mouse button was pressed
10786 or -1. */
10788 int last_tool_bar_item;
10791 static Lisp_Object
10792 update_tool_bar_unwind (Lisp_Object frame)
10794 selected_frame = frame;
10795 return Qnil;
10798 /* Update the tool-bar item list for frame F. This has to be done
10799 before we start to fill in any display lines. Called from
10800 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10801 and restore it here. */
10803 static void
10804 update_tool_bar (struct frame *f, int save_match_data)
10806 #if defined (USE_GTK) || defined (HAVE_NS)
10807 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10808 #else
10809 int do_update = WINDOWP (f->tool_bar_window)
10810 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10811 #endif
10813 if (do_update)
10815 Lisp_Object window;
10816 struct window *w;
10818 window = FRAME_SELECTED_WINDOW (f);
10819 w = XWINDOW (window);
10821 /* If the user has switched buffers or windows, we need to
10822 recompute to reflect the new bindings. But we'll
10823 recompute when update_mode_lines is set too; that means
10824 that people can use force-mode-line-update to request
10825 that the menu bar be recomputed. The adverse effect on
10826 the rest of the redisplay algorithm is about the same as
10827 windows_or_buffers_changed anyway. */
10828 if (windows_or_buffers_changed
10829 || !NILP (w->update_mode_line)
10830 || update_mode_lines
10831 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10832 < BUF_MODIFF (XBUFFER (w->buffer)))
10833 != !NILP (w->last_had_star))
10834 || ((!NILP (Vtransient_mark_mode)
10835 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
10836 != !NILP (w->region_showing)))
10838 struct buffer *prev = current_buffer;
10839 int count = SPECPDL_INDEX ();
10840 Lisp_Object frame, new_tool_bar;
10841 int new_n_tool_bar;
10842 struct gcpro gcpro1;
10844 /* Set current_buffer to the buffer of the selected
10845 window of the frame, so that we get the right local
10846 keymaps. */
10847 set_buffer_internal_1 (XBUFFER (w->buffer));
10849 /* Save match data, if we must. */
10850 if (save_match_data)
10851 record_unwind_save_match_data ();
10853 /* Make sure that we don't accidentally use bogus keymaps. */
10854 if (NILP (Voverriding_local_map_menu_flag))
10856 specbind (Qoverriding_terminal_local_map, Qnil);
10857 specbind (Qoverriding_local_map, Qnil);
10860 GCPRO1 (new_tool_bar);
10862 /* We must temporarily set the selected frame to this frame
10863 before calling tool_bar_items, because the calculation of
10864 the tool-bar keymap uses the selected frame (see
10865 `tool-bar-make-keymap' in tool-bar.el). */
10866 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10867 XSETFRAME (frame, f);
10868 selected_frame = frame;
10870 /* Build desired tool-bar items from keymaps. */
10871 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10872 &new_n_tool_bar);
10874 /* Redisplay the tool-bar if we changed it. */
10875 if (new_n_tool_bar != f->n_tool_bar_items
10876 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10878 /* Redisplay that happens asynchronously due to an expose event
10879 may access f->tool_bar_items. Make sure we update both
10880 variables within BLOCK_INPUT so no such event interrupts. */
10881 BLOCK_INPUT;
10882 f->tool_bar_items = new_tool_bar;
10883 f->n_tool_bar_items = new_n_tool_bar;
10884 w->update_mode_line = Qt;
10885 UNBLOCK_INPUT;
10888 UNGCPRO;
10890 unbind_to (count, Qnil);
10891 set_buffer_internal_1 (prev);
10897 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10898 F's desired tool-bar contents. F->tool_bar_items must have
10899 been set up previously by calling prepare_menu_bars. */
10901 static void
10902 build_desired_tool_bar_string (struct frame *f)
10904 int i, size, size_needed;
10905 struct gcpro gcpro1, gcpro2, gcpro3;
10906 Lisp_Object image, plist, props;
10908 image = plist = props = Qnil;
10909 GCPRO3 (image, plist, props);
10911 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10912 Otherwise, make a new string. */
10914 /* The size of the string we might be able to reuse. */
10915 size = (STRINGP (f->desired_tool_bar_string)
10916 ? SCHARS (f->desired_tool_bar_string)
10917 : 0);
10919 /* We need one space in the string for each image. */
10920 size_needed = f->n_tool_bar_items;
10922 /* Reuse f->desired_tool_bar_string, if possible. */
10923 if (size < size_needed || NILP (f->desired_tool_bar_string))
10924 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10925 make_number (' '));
10926 else
10928 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10929 Fremove_text_properties (make_number (0), make_number (size),
10930 props, f->desired_tool_bar_string);
10933 /* Put a `display' property on the string for the images to display,
10934 put a `menu_item' property on tool-bar items with a value that
10935 is the index of the item in F's tool-bar item vector. */
10936 for (i = 0; i < f->n_tool_bar_items; ++i)
10938 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10940 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10941 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10942 int hmargin, vmargin, relief, idx, end;
10944 /* If image is a vector, choose the image according to the
10945 button state. */
10946 image = PROP (TOOL_BAR_ITEM_IMAGES);
10947 if (VECTORP (image))
10949 if (enabled_p)
10950 idx = (selected_p
10951 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10952 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10953 else
10954 idx = (selected_p
10955 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10956 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10958 xassert (ASIZE (image) >= idx);
10959 image = AREF (image, idx);
10961 else
10962 idx = -1;
10964 /* Ignore invalid image specifications. */
10965 if (!valid_image_p (image))
10966 continue;
10968 /* Display the tool-bar button pressed, or depressed. */
10969 plist = Fcopy_sequence (XCDR (image));
10971 /* Compute margin and relief to draw. */
10972 relief = (tool_bar_button_relief >= 0
10973 ? tool_bar_button_relief
10974 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10975 hmargin = vmargin = relief;
10977 if (INTEGERP (Vtool_bar_button_margin)
10978 && XINT (Vtool_bar_button_margin) > 0)
10980 hmargin += XFASTINT (Vtool_bar_button_margin);
10981 vmargin += XFASTINT (Vtool_bar_button_margin);
10983 else if (CONSP (Vtool_bar_button_margin))
10985 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10986 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10987 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10989 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10990 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10991 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10994 if (auto_raise_tool_bar_buttons_p)
10996 /* Add a `:relief' property to the image spec if the item is
10997 selected. */
10998 if (selected_p)
11000 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11001 hmargin -= relief;
11002 vmargin -= relief;
11005 else
11007 /* If image is selected, display it pressed, i.e. with a
11008 negative relief. If it's not selected, display it with a
11009 raised relief. */
11010 plist = Fplist_put (plist, QCrelief,
11011 (selected_p
11012 ? make_number (-relief)
11013 : make_number (relief)));
11014 hmargin -= relief;
11015 vmargin -= relief;
11018 /* Put a margin around the image. */
11019 if (hmargin || vmargin)
11021 if (hmargin == vmargin)
11022 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11023 else
11024 plist = Fplist_put (plist, QCmargin,
11025 Fcons (make_number (hmargin),
11026 make_number (vmargin)));
11029 /* If button is not enabled, and we don't have special images
11030 for the disabled state, make the image appear disabled by
11031 applying an appropriate algorithm to it. */
11032 if (!enabled_p && idx < 0)
11033 plist = Fplist_put (plist, QCconversion, Qdisabled);
11035 /* Put a `display' text property on the string for the image to
11036 display. Put a `menu-item' property on the string that gives
11037 the start of this item's properties in the tool-bar items
11038 vector. */
11039 image = Fcons (Qimage, plist);
11040 props = list4 (Qdisplay, image,
11041 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11043 /* Let the last image hide all remaining spaces in the tool bar
11044 string. The string can be longer than needed when we reuse a
11045 previous string. */
11046 if (i + 1 == f->n_tool_bar_items)
11047 end = SCHARS (f->desired_tool_bar_string);
11048 else
11049 end = i + 1;
11050 Fadd_text_properties (make_number (i), make_number (end),
11051 props, f->desired_tool_bar_string);
11052 #undef PROP
11055 UNGCPRO;
11059 /* Display one line of the tool-bar of frame IT->f.
11061 HEIGHT specifies the desired height of the tool-bar line.
11062 If the actual height of the glyph row is less than HEIGHT, the
11063 row's height is increased to HEIGHT, and the icons are centered
11064 vertically in the new height.
11066 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11067 count a final empty row in case the tool-bar width exactly matches
11068 the window width.
11071 static void
11072 display_tool_bar_line (struct it *it, int height)
11074 struct glyph_row *row = it->glyph_row;
11075 int max_x = it->last_visible_x;
11076 struct glyph *last;
11078 prepare_desired_row (row);
11079 row->y = it->current_y;
11081 /* Note that this isn't made use of if the face hasn't a box,
11082 so there's no need to check the face here. */
11083 it->start_of_box_run_p = 1;
11085 while (it->current_x < max_x)
11087 int x, n_glyphs_before, i, nglyphs;
11088 struct it it_before;
11090 /* Get the next display element. */
11091 if (!get_next_display_element (it))
11093 /* Don't count empty row if we are counting needed tool-bar lines. */
11094 if (height < 0 && !it->hpos)
11095 return;
11096 break;
11099 /* Produce glyphs. */
11100 n_glyphs_before = row->used[TEXT_AREA];
11101 it_before = *it;
11103 PRODUCE_GLYPHS (it);
11105 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11106 i = 0;
11107 x = it_before.current_x;
11108 while (i < nglyphs)
11110 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11112 if (x + glyph->pixel_width > max_x)
11114 /* Glyph doesn't fit on line. Backtrack. */
11115 row->used[TEXT_AREA] = n_glyphs_before;
11116 *it = it_before;
11117 /* If this is the only glyph on this line, it will never fit on the
11118 tool-bar, so skip it. But ensure there is at least one glyph,
11119 so we don't accidentally disable the tool-bar. */
11120 if (n_glyphs_before == 0
11121 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11122 break;
11123 goto out;
11126 ++it->hpos;
11127 x += glyph->pixel_width;
11128 ++i;
11131 /* Stop at line end. */
11132 if (ITERATOR_AT_END_OF_LINE_P (it))
11133 break;
11135 set_iterator_to_next (it, 1);
11138 out:;
11140 row->displays_text_p = row->used[TEXT_AREA] != 0;
11142 /* Use default face for the border below the tool bar.
11144 FIXME: When auto-resize-tool-bars is grow-only, there is
11145 no additional border below the possibly empty tool-bar lines.
11146 So to make the extra empty lines look "normal", we have to
11147 use the tool-bar face for the border too. */
11148 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11149 it->face_id = DEFAULT_FACE_ID;
11151 extend_face_to_end_of_line (it);
11152 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11153 last->right_box_line_p = 1;
11154 if (last == row->glyphs[TEXT_AREA])
11155 last->left_box_line_p = 1;
11157 /* Make line the desired height and center it vertically. */
11158 if ((height -= it->max_ascent + it->max_descent) > 0)
11160 /* Don't add more than one line height. */
11161 height %= FRAME_LINE_HEIGHT (it->f);
11162 it->max_ascent += height / 2;
11163 it->max_descent += (height + 1) / 2;
11166 compute_line_metrics (it);
11168 /* If line is empty, make it occupy the rest of the tool-bar. */
11169 if (!row->displays_text_p)
11171 row->height = row->phys_height = it->last_visible_y - row->y;
11172 row->visible_height = row->height;
11173 row->ascent = row->phys_ascent = 0;
11174 row->extra_line_spacing = 0;
11177 row->full_width_p = 1;
11178 row->continued_p = 0;
11179 row->truncated_on_left_p = 0;
11180 row->truncated_on_right_p = 0;
11182 it->current_x = it->hpos = 0;
11183 it->current_y += row->height;
11184 ++it->vpos;
11185 ++it->glyph_row;
11189 /* Max tool-bar height. */
11191 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11192 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11194 /* Value is the number of screen lines needed to make all tool-bar
11195 items of frame F visible. The number of actual rows needed is
11196 returned in *N_ROWS if non-NULL. */
11198 static int
11199 tool_bar_lines_needed (struct frame *f, int *n_rows)
11201 struct window *w = XWINDOW (f->tool_bar_window);
11202 struct it it;
11203 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11204 the desired matrix, so use (unused) mode-line row as temporary row to
11205 avoid destroying the first tool-bar row. */
11206 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11208 /* Initialize an iterator for iteration over
11209 F->desired_tool_bar_string in the tool-bar window of frame F. */
11210 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11211 it.first_visible_x = 0;
11212 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11213 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11214 it.paragraph_embedding = L2R;
11216 while (!ITERATOR_AT_END_P (&it))
11218 clear_glyph_row (temp_row);
11219 it.glyph_row = temp_row;
11220 display_tool_bar_line (&it, -1);
11222 clear_glyph_row (temp_row);
11224 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11225 if (n_rows)
11226 *n_rows = it.vpos > 0 ? it.vpos : -1;
11228 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11232 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11233 0, 1, 0,
11234 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11235 (Lisp_Object frame)
11237 struct frame *f;
11238 struct window *w;
11239 int nlines = 0;
11241 if (NILP (frame))
11242 frame = selected_frame;
11243 else
11244 CHECK_FRAME (frame);
11245 f = XFRAME (frame);
11247 if (WINDOWP (f->tool_bar_window)
11248 && (w = XWINDOW (f->tool_bar_window),
11249 WINDOW_TOTAL_LINES (w) > 0))
11251 update_tool_bar (f, 1);
11252 if (f->n_tool_bar_items)
11254 build_desired_tool_bar_string (f);
11255 nlines = tool_bar_lines_needed (f, NULL);
11259 return make_number (nlines);
11263 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11264 height should be changed. */
11266 static int
11267 redisplay_tool_bar (struct frame *f)
11269 struct window *w;
11270 struct it it;
11271 struct glyph_row *row;
11273 #if defined (USE_GTK) || defined (HAVE_NS)
11274 if (FRAME_EXTERNAL_TOOL_BAR (f))
11275 update_frame_tool_bar (f);
11276 return 0;
11277 #endif
11279 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11280 do anything. This means you must start with tool-bar-lines
11281 non-zero to get the auto-sizing effect. Or in other words, you
11282 can turn off tool-bars by specifying tool-bar-lines zero. */
11283 if (!WINDOWP (f->tool_bar_window)
11284 || (w = XWINDOW (f->tool_bar_window),
11285 WINDOW_TOTAL_LINES (w) == 0))
11286 return 0;
11288 /* Set up an iterator for the tool-bar window. */
11289 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11290 it.first_visible_x = 0;
11291 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11292 row = it.glyph_row;
11294 /* Build a string that represents the contents of the tool-bar. */
11295 build_desired_tool_bar_string (f);
11296 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11297 /* FIXME: This should be controlled by a user option. But it
11298 doesn't make sense to have an R2L tool bar if the menu bar cannot
11299 be drawn also R2L, and making the menu bar R2L is tricky due
11300 toolkit-specific code that implements it. If an R2L tool bar is
11301 ever supported, display_tool_bar_line should also be augmented to
11302 call unproduce_glyphs like display_line and display_string
11303 do. */
11304 it.paragraph_embedding = L2R;
11306 if (f->n_tool_bar_rows == 0)
11308 int nlines;
11310 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11311 nlines != WINDOW_TOTAL_LINES (w)))
11313 Lisp_Object frame;
11314 int old_height = WINDOW_TOTAL_LINES (w);
11316 XSETFRAME (frame, f);
11317 Fmodify_frame_parameters (frame,
11318 Fcons (Fcons (Qtool_bar_lines,
11319 make_number (nlines)),
11320 Qnil));
11321 if (WINDOW_TOTAL_LINES (w) != old_height)
11323 clear_glyph_matrix (w->desired_matrix);
11324 fonts_changed_p = 1;
11325 return 1;
11330 /* Display as many lines as needed to display all tool-bar items. */
11332 if (f->n_tool_bar_rows > 0)
11334 int border, rows, height, extra;
11336 if (INTEGERP (Vtool_bar_border))
11337 border = XINT (Vtool_bar_border);
11338 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11339 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11340 else if (EQ (Vtool_bar_border, Qborder_width))
11341 border = f->border_width;
11342 else
11343 border = 0;
11344 if (border < 0)
11345 border = 0;
11347 rows = f->n_tool_bar_rows;
11348 height = max (1, (it.last_visible_y - border) / rows);
11349 extra = it.last_visible_y - border - height * rows;
11351 while (it.current_y < it.last_visible_y)
11353 int h = 0;
11354 if (extra > 0 && rows-- > 0)
11356 h = (extra + rows - 1) / rows;
11357 extra -= h;
11359 display_tool_bar_line (&it, height + h);
11362 else
11364 while (it.current_y < it.last_visible_y)
11365 display_tool_bar_line (&it, 0);
11368 /* It doesn't make much sense to try scrolling in the tool-bar
11369 window, so don't do it. */
11370 w->desired_matrix->no_scrolling_p = 1;
11371 w->must_be_updated_p = 1;
11373 if (!NILP (Vauto_resize_tool_bars))
11375 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11376 int change_height_p = 0;
11378 /* If we couldn't display everything, change the tool-bar's
11379 height if there is room for more. */
11380 if (IT_STRING_CHARPOS (it) < it.end_charpos
11381 && it.current_y < max_tool_bar_height)
11382 change_height_p = 1;
11384 row = it.glyph_row - 1;
11386 /* If there are blank lines at the end, except for a partially
11387 visible blank line at the end that is smaller than
11388 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11389 if (!row->displays_text_p
11390 && row->height >= FRAME_LINE_HEIGHT (f))
11391 change_height_p = 1;
11393 /* If row displays tool-bar items, but is partially visible,
11394 change the tool-bar's height. */
11395 if (row->displays_text_p
11396 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11397 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11398 change_height_p = 1;
11400 /* Resize windows as needed by changing the `tool-bar-lines'
11401 frame parameter. */
11402 if (change_height_p)
11404 Lisp_Object frame;
11405 int old_height = WINDOW_TOTAL_LINES (w);
11406 int nrows;
11407 int nlines = tool_bar_lines_needed (f, &nrows);
11409 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11410 && !f->minimize_tool_bar_window_p)
11411 ? (nlines > old_height)
11412 : (nlines != old_height));
11413 f->minimize_tool_bar_window_p = 0;
11415 if (change_height_p)
11417 XSETFRAME (frame, f);
11418 Fmodify_frame_parameters (frame,
11419 Fcons (Fcons (Qtool_bar_lines,
11420 make_number (nlines)),
11421 Qnil));
11422 if (WINDOW_TOTAL_LINES (w) != old_height)
11424 clear_glyph_matrix (w->desired_matrix);
11425 f->n_tool_bar_rows = nrows;
11426 fonts_changed_p = 1;
11427 return 1;
11433 f->minimize_tool_bar_window_p = 0;
11434 return 0;
11438 /* Get information about the tool-bar item which is displayed in GLYPH
11439 on frame F. Return in *PROP_IDX the index where tool-bar item
11440 properties start in F->tool_bar_items. Value is zero if
11441 GLYPH doesn't display a tool-bar item. */
11443 static int
11444 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11446 Lisp_Object prop;
11447 int success_p;
11448 int charpos;
11450 /* This function can be called asynchronously, which means we must
11451 exclude any possibility that Fget_text_property signals an
11452 error. */
11453 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
11454 charpos = max (0, charpos);
11456 /* Get the text property `menu-item' at pos. The value of that
11457 property is the start index of this item's properties in
11458 F->tool_bar_items. */
11459 prop = Fget_text_property (make_number (charpos),
11460 Qmenu_item, f->current_tool_bar_string);
11461 if (INTEGERP (prop))
11463 *prop_idx = XINT (prop);
11464 success_p = 1;
11466 else
11467 success_p = 0;
11469 return success_p;
11473 /* Get information about the tool-bar item at position X/Y on frame F.
11474 Return in *GLYPH a pointer to the glyph of the tool-bar item in
11475 the current matrix of the tool-bar window of F, or NULL if not
11476 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
11477 item in F->tool_bar_items. Value is
11479 -1 if X/Y is not on a tool-bar item
11480 0 if X/Y is on the same item that was highlighted before.
11481 1 otherwise. */
11483 static int
11484 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
11485 int *hpos, int *vpos, int *prop_idx)
11487 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11488 struct window *w = XWINDOW (f->tool_bar_window);
11489 int area;
11491 /* Find the glyph under X/Y. */
11492 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
11493 if (*glyph == NULL)
11494 return -1;
11496 /* Get the start of this tool-bar item's properties in
11497 f->tool_bar_items. */
11498 if (!tool_bar_item_info (f, *glyph, prop_idx))
11499 return -1;
11501 /* Is mouse on the highlighted item? */
11502 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
11503 && *vpos >= hlinfo->mouse_face_beg_row
11504 && *vpos <= hlinfo->mouse_face_end_row
11505 && (*vpos > hlinfo->mouse_face_beg_row
11506 || *hpos >= hlinfo->mouse_face_beg_col)
11507 && (*vpos < hlinfo->mouse_face_end_row
11508 || *hpos < hlinfo->mouse_face_end_col
11509 || hlinfo->mouse_face_past_end))
11510 return 0;
11512 return 1;
11516 /* EXPORT:
11517 Handle mouse button event on the tool-bar of frame F, at
11518 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
11519 0 for button release. MODIFIERS is event modifiers for button
11520 release. */
11522 void
11523 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
11524 unsigned int modifiers)
11526 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11527 struct window *w = XWINDOW (f->tool_bar_window);
11528 int hpos, vpos, prop_idx;
11529 struct glyph *glyph;
11530 Lisp_Object enabled_p;
11532 /* If not on the highlighted tool-bar item, return. */
11533 frame_to_window_pixel_xy (w, &x, &y);
11534 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
11535 return;
11537 /* If item is disabled, do nothing. */
11538 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11539 if (NILP (enabled_p))
11540 return;
11542 if (down_p)
11544 /* Show item in pressed state. */
11545 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
11546 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
11547 last_tool_bar_item = prop_idx;
11549 else
11551 Lisp_Object key, frame;
11552 struct input_event event;
11553 EVENT_INIT (event);
11555 /* Show item in released state. */
11556 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
11557 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
11559 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
11561 XSETFRAME (frame, f);
11562 event.kind = TOOL_BAR_EVENT;
11563 event.frame_or_window = frame;
11564 event.arg = frame;
11565 kbd_buffer_store_event (&event);
11567 event.kind = TOOL_BAR_EVENT;
11568 event.frame_or_window = frame;
11569 event.arg = key;
11570 event.modifiers = modifiers;
11571 kbd_buffer_store_event (&event);
11572 last_tool_bar_item = -1;
11577 /* Possibly highlight a tool-bar item on frame F when mouse moves to
11578 tool-bar window-relative coordinates X/Y. Called from
11579 note_mouse_highlight. */
11581 static void
11582 note_tool_bar_highlight (struct frame *f, int x, int y)
11584 Lisp_Object window = f->tool_bar_window;
11585 struct window *w = XWINDOW (window);
11586 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11587 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11588 int hpos, vpos;
11589 struct glyph *glyph;
11590 struct glyph_row *row;
11591 int i;
11592 Lisp_Object enabled_p;
11593 int prop_idx;
11594 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
11595 int mouse_down_p, rc;
11597 /* Function note_mouse_highlight is called with negative X/Y
11598 values when mouse moves outside of the frame. */
11599 if (x <= 0 || y <= 0)
11601 clear_mouse_face (hlinfo);
11602 return;
11605 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
11606 if (rc < 0)
11608 /* Not on tool-bar item. */
11609 clear_mouse_face (hlinfo);
11610 return;
11612 else if (rc == 0)
11613 /* On same tool-bar item as before. */
11614 goto set_help_echo;
11616 clear_mouse_face (hlinfo);
11618 /* Mouse is down, but on different tool-bar item? */
11619 mouse_down_p = (dpyinfo->grabbed
11620 && f == last_mouse_frame
11621 && FRAME_LIVE_P (f));
11622 if (mouse_down_p
11623 && last_tool_bar_item != prop_idx)
11624 return;
11626 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
11627 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
11629 /* If tool-bar item is not enabled, don't highlight it. */
11630 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11631 if (!NILP (enabled_p))
11633 /* Compute the x-position of the glyph. In front and past the
11634 image is a space. We include this in the highlighted area. */
11635 row = MATRIX_ROW (w->current_matrix, vpos);
11636 for (i = x = 0; i < hpos; ++i)
11637 x += row->glyphs[TEXT_AREA][i].pixel_width;
11639 /* Record this as the current active region. */
11640 hlinfo->mouse_face_beg_col = hpos;
11641 hlinfo->mouse_face_beg_row = vpos;
11642 hlinfo->mouse_face_beg_x = x;
11643 hlinfo->mouse_face_beg_y = row->y;
11644 hlinfo->mouse_face_past_end = 0;
11646 hlinfo->mouse_face_end_col = hpos + 1;
11647 hlinfo->mouse_face_end_row = vpos;
11648 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
11649 hlinfo->mouse_face_end_y = row->y;
11650 hlinfo->mouse_face_window = window;
11651 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
11653 /* Display it as active. */
11654 show_mouse_face (hlinfo, draw);
11655 hlinfo->mouse_face_image_state = draw;
11658 set_help_echo:
11660 /* Set help_echo_string to a help string to display for this tool-bar item.
11661 XTread_socket does the rest. */
11662 help_echo_object = help_echo_window = Qnil;
11663 help_echo_pos = -1;
11664 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
11665 if (NILP (help_echo_string))
11666 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
11669 #endif /* HAVE_WINDOW_SYSTEM */
11673 /************************************************************************
11674 Horizontal scrolling
11675 ************************************************************************/
11677 static int hscroll_window_tree (Lisp_Object);
11678 static int hscroll_windows (Lisp_Object);
11680 /* For all leaf windows in the window tree rooted at WINDOW, set their
11681 hscroll value so that PT is (i) visible in the window, and (ii) so
11682 that it is not within a certain margin at the window's left and
11683 right border. Value is non-zero if any window's hscroll has been
11684 changed. */
11686 static int
11687 hscroll_window_tree (Lisp_Object window)
11689 int hscrolled_p = 0;
11690 int hscroll_relative_p = FLOATP (Vhscroll_step);
11691 int hscroll_step_abs = 0;
11692 double hscroll_step_rel = 0;
11694 if (hscroll_relative_p)
11696 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
11697 if (hscroll_step_rel < 0)
11699 hscroll_relative_p = 0;
11700 hscroll_step_abs = 0;
11703 else if (INTEGERP (Vhscroll_step))
11705 hscroll_step_abs = XINT (Vhscroll_step);
11706 if (hscroll_step_abs < 0)
11707 hscroll_step_abs = 0;
11709 else
11710 hscroll_step_abs = 0;
11712 while (WINDOWP (window))
11714 struct window *w = XWINDOW (window);
11716 if (WINDOWP (w->hchild))
11717 hscrolled_p |= hscroll_window_tree (w->hchild);
11718 else if (WINDOWP (w->vchild))
11719 hscrolled_p |= hscroll_window_tree (w->vchild);
11720 else if (w->cursor.vpos >= 0)
11722 int h_margin;
11723 int text_area_width;
11724 struct glyph_row *current_cursor_row
11725 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
11726 struct glyph_row *desired_cursor_row
11727 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
11728 struct glyph_row *cursor_row
11729 = (desired_cursor_row->enabled_p
11730 ? desired_cursor_row
11731 : current_cursor_row);
11733 text_area_width = window_box_width (w, TEXT_AREA);
11735 /* Scroll when cursor is inside this scroll margin. */
11736 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
11738 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
11739 && ((XFASTINT (w->hscroll)
11740 && w->cursor.x <= h_margin)
11741 || (cursor_row->enabled_p
11742 && cursor_row->truncated_on_right_p
11743 && (w->cursor.x >= text_area_width - h_margin))))
11745 struct it it;
11746 int hscroll;
11747 struct buffer *saved_current_buffer;
11748 EMACS_INT pt;
11749 int wanted_x;
11751 /* Find point in a display of infinite width. */
11752 saved_current_buffer = current_buffer;
11753 current_buffer = XBUFFER (w->buffer);
11755 if (w == XWINDOW (selected_window))
11756 pt = PT;
11757 else
11759 pt = marker_position (w->pointm);
11760 pt = max (BEGV, pt);
11761 pt = min (ZV, pt);
11764 /* Move iterator to pt starting at cursor_row->start in
11765 a line with infinite width. */
11766 init_to_row_start (&it, w, cursor_row);
11767 it.last_visible_x = INFINITY;
11768 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11769 current_buffer = saved_current_buffer;
11771 /* Position cursor in window. */
11772 if (!hscroll_relative_p && hscroll_step_abs == 0)
11773 hscroll = max (0, (it.current_x
11774 - (ITERATOR_AT_END_OF_LINE_P (&it)
11775 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11776 : (text_area_width / 2))))
11777 / FRAME_COLUMN_WIDTH (it.f);
11778 else if (w->cursor.x >= text_area_width - h_margin)
11780 if (hscroll_relative_p)
11781 wanted_x = text_area_width * (1 - hscroll_step_rel)
11782 - h_margin;
11783 else
11784 wanted_x = text_area_width
11785 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11786 - h_margin;
11787 hscroll
11788 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11790 else
11792 if (hscroll_relative_p)
11793 wanted_x = text_area_width * hscroll_step_rel
11794 + h_margin;
11795 else
11796 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11797 + h_margin;
11798 hscroll
11799 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11801 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11803 /* Don't call Fset_window_hscroll if value hasn't
11804 changed because it will prevent redisplay
11805 optimizations. */
11806 if (XFASTINT (w->hscroll) != hscroll)
11808 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11809 w->hscroll = make_number (hscroll);
11810 hscrolled_p = 1;
11815 window = w->next;
11818 /* Value is non-zero if hscroll of any leaf window has been changed. */
11819 return hscrolled_p;
11823 /* Set hscroll so that cursor is visible and not inside horizontal
11824 scroll margins for all windows in the tree rooted at WINDOW. See
11825 also hscroll_window_tree above. Value is non-zero if any window's
11826 hscroll has been changed. If it has, desired matrices on the frame
11827 of WINDOW are cleared. */
11829 static int
11830 hscroll_windows (Lisp_Object window)
11832 int hscrolled_p = hscroll_window_tree (window);
11833 if (hscrolled_p)
11834 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11835 return hscrolled_p;
11840 /************************************************************************
11841 Redisplay
11842 ************************************************************************/
11844 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11845 to a non-zero value. This is sometimes handy to have in a debugger
11846 session. */
11848 #if GLYPH_DEBUG
11850 /* First and last unchanged row for try_window_id. */
11852 static int debug_first_unchanged_at_end_vpos;
11853 static int debug_last_unchanged_at_beg_vpos;
11855 /* Delta vpos and y. */
11857 static int debug_dvpos, debug_dy;
11859 /* Delta in characters and bytes for try_window_id. */
11861 static EMACS_INT debug_delta, debug_delta_bytes;
11863 /* Values of window_end_pos and window_end_vpos at the end of
11864 try_window_id. */
11866 static EMACS_INT debug_end_vpos;
11868 /* Append a string to W->desired_matrix->method. FMT is a printf
11869 format string. If trace_redisplay_p is non-zero also printf the
11870 resulting string to stderr. */
11872 static void debug_method_add (struct window *, char const *, ...)
11873 ATTRIBUTE_FORMAT_PRINTF (2, 3);
11875 static void
11876 debug_method_add (struct window *w, char const *fmt, ...)
11878 char buffer[512];
11879 char *method = w->desired_matrix->method;
11880 int len = strlen (method);
11881 int size = sizeof w->desired_matrix->method;
11882 int remaining = size - len - 1;
11883 va_list ap;
11885 va_start (ap, fmt);
11886 vsprintf (buffer, fmt, ap);
11887 va_end (ap);
11888 if (len && remaining)
11890 method[len] = '|';
11891 --remaining, ++len;
11894 strncpy (method + len, buffer, remaining);
11896 if (trace_redisplay_p)
11897 fprintf (stderr, "%p (%s): %s\n",
11899 ((BUFFERP (w->buffer)
11900 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
11901 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
11902 : "no buffer"),
11903 buffer);
11906 #endif /* GLYPH_DEBUG */
11909 /* Value is non-zero if all changes in window W, which displays
11910 current_buffer, are in the text between START and END. START is a
11911 buffer position, END is given as a distance from Z. Used in
11912 redisplay_internal for display optimization. */
11914 static inline int
11915 text_outside_line_unchanged_p (struct window *w,
11916 EMACS_INT start, EMACS_INT end)
11918 int unchanged_p = 1;
11920 /* If text or overlays have changed, see where. */
11921 if (XFASTINT (w->last_modified) < MODIFF
11922 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11924 /* Gap in the line? */
11925 if (GPT < start || Z - GPT < end)
11926 unchanged_p = 0;
11928 /* Changes start in front of the line, or end after it? */
11929 if (unchanged_p
11930 && (BEG_UNCHANGED < start - 1
11931 || END_UNCHANGED < end))
11932 unchanged_p = 0;
11934 /* If selective display, can't optimize if changes start at the
11935 beginning of the line. */
11936 if (unchanged_p
11937 && INTEGERP (BVAR (current_buffer, selective_display))
11938 && XINT (BVAR (current_buffer, selective_display)) > 0
11939 && (BEG_UNCHANGED < start || GPT <= start))
11940 unchanged_p = 0;
11942 /* If there are overlays at the start or end of the line, these
11943 may have overlay strings with newlines in them. A change at
11944 START, for instance, may actually concern the display of such
11945 overlay strings as well, and they are displayed on different
11946 lines. So, quickly rule out this case. (For the future, it
11947 might be desirable to implement something more telling than
11948 just BEG/END_UNCHANGED.) */
11949 if (unchanged_p)
11951 if (BEG + BEG_UNCHANGED == start
11952 && overlay_touches_p (start))
11953 unchanged_p = 0;
11954 if (END_UNCHANGED == end
11955 && overlay_touches_p (Z - end))
11956 unchanged_p = 0;
11959 /* Under bidi reordering, adding or deleting a character in the
11960 beginning of a paragraph, before the first strong directional
11961 character, can change the base direction of the paragraph (unless
11962 the buffer specifies a fixed paragraph direction), which will
11963 require to redisplay the whole paragraph. It might be worthwhile
11964 to find the paragraph limits and widen the range of redisplayed
11965 lines to that, but for now just give up this optimization. */
11966 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
11967 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
11968 unchanged_p = 0;
11971 return unchanged_p;
11975 /* Do a frame update, taking possible shortcuts into account. This is
11976 the main external entry point for redisplay.
11978 If the last redisplay displayed an echo area message and that message
11979 is no longer requested, we clear the echo area or bring back the
11980 mini-buffer if that is in use. */
11982 void
11983 redisplay (void)
11985 redisplay_internal ();
11989 static Lisp_Object
11990 overlay_arrow_string_or_property (Lisp_Object var)
11992 Lisp_Object val;
11994 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11995 return val;
11997 return Voverlay_arrow_string;
12000 /* Return 1 if there are any overlay-arrows in current_buffer. */
12001 static int
12002 overlay_arrow_in_current_buffer_p (void)
12004 Lisp_Object vlist;
12006 for (vlist = Voverlay_arrow_variable_list;
12007 CONSP (vlist);
12008 vlist = XCDR (vlist))
12010 Lisp_Object var = XCAR (vlist);
12011 Lisp_Object val;
12013 if (!SYMBOLP (var))
12014 continue;
12015 val = find_symbol_value (var);
12016 if (MARKERP (val)
12017 && current_buffer == XMARKER (val)->buffer)
12018 return 1;
12020 return 0;
12024 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12025 has changed. */
12027 static int
12028 overlay_arrows_changed_p (void)
12030 Lisp_Object vlist;
12032 for (vlist = Voverlay_arrow_variable_list;
12033 CONSP (vlist);
12034 vlist = XCDR (vlist))
12036 Lisp_Object var = XCAR (vlist);
12037 Lisp_Object val, pstr;
12039 if (!SYMBOLP (var))
12040 continue;
12041 val = find_symbol_value (var);
12042 if (!MARKERP (val))
12043 continue;
12044 if (! EQ (COERCE_MARKER (val),
12045 Fget (var, Qlast_arrow_position))
12046 || ! (pstr = overlay_arrow_string_or_property (var),
12047 EQ (pstr, Fget (var, Qlast_arrow_string))))
12048 return 1;
12050 return 0;
12053 /* Mark overlay arrows to be updated on next redisplay. */
12055 static void
12056 update_overlay_arrows (int up_to_date)
12058 Lisp_Object vlist;
12060 for (vlist = Voverlay_arrow_variable_list;
12061 CONSP (vlist);
12062 vlist = XCDR (vlist))
12064 Lisp_Object var = XCAR (vlist);
12066 if (!SYMBOLP (var))
12067 continue;
12069 if (up_to_date > 0)
12071 Lisp_Object val = find_symbol_value (var);
12072 Fput (var, Qlast_arrow_position,
12073 COERCE_MARKER (val));
12074 Fput (var, Qlast_arrow_string,
12075 overlay_arrow_string_or_property (var));
12077 else if (up_to_date < 0
12078 || !NILP (Fget (var, Qlast_arrow_position)))
12080 Fput (var, Qlast_arrow_position, Qt);
12081 Fput (var, Qlast_arrow_string, Qt);
12087 /* Return overlay arrow string to display at row.
12088 Return integer (bitmap number) for arrow bitmap in left fringe.
12089 Return nil if no overlay arrow. */
12091 static Lisp_Object
12092 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12094 Lisp_Object vlist;
12096 for (vlist = Voverlay_arrow_variable_list;
12097 CONSP (vlist);
12098 vlist = XCDR (vlist))
12100 Lisp_Object var = XCAR (vlist);
12101 Lisp_Object val;
12103 if (!SYMBOLP (var))
12104 continue;
12106 val = find_symbol_value (var);
12108 if (MARKERP (val)
12109 && current_buffer == XMARKER (val)->buffer
12110 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12112 if (FRAME_WINDOW_P (it->f)
12113 /* FIXME: if ROW->reversed_p is set, this should test
12114 the right fringe, not the left one. */
12115 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12117 #ifdef HAVE_WINDOW_SYSTEM
12118 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12120 int fringe_bitmap;
12121 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12122 return make_number (fringe_bitmap);
12124 #endif
12125 return make_number (-1); /* Use default arrow bitmap */
12127 return overlay_arrow_string_or_property (var);
12131 return Qnil;
12134 /* Return 1 if point moved out of or into a composition. Otherwise
12135 return 0. PREV_BUF and PREV_PT are the last point buffer and
12136 position. BUF and PT are the current point buffer and position. */
12138 static int
12139 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
12140 struct buffer *buf, EMACS_INT pt)
12142 EMACS_INT start, end;
12143 Lisp_Object prop;
12144 Lisp_Object buffer;
12146 XSETBUFFER (buffer, buf);
12147 /* Check a composition at the last point if point moved within the
12148 same buffer. */
12149 if (prev_buf == buf)
12151 if (prev_pt == pt)
12152 /* Point didn't move. */
12153 return 0;
12155 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12156 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12157 && COMPOSITION_VALID_P (start, end, prop)
12158 && start < prev_pt && end > prev_pt)
12159 /* The last point was within the composition. Return 1 iff
12160 point moved out of the composition. */
12161 return (pt <= start || pt >= end);
12164 /* Check a composition at the current point. */
12165 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12166 && find_composition (pt, -1, &start, &end, &prop, buffer)
12167 && COMPOSITION_VALID_P (start, end, prop)
12168 && start < pt && end > pt);
12172 /* Reconsider the setting of B->clip_changed which is displayed
12173 in window W. */
12175 static inline void
12176 reconsider_clip_changes (struct window *w, struct buffer *b)
12178 if (b->clip_changed
12179 && !NILP (w->window_end_valid)
12180 && w->current_matrix->buffer == b
12181 && w->current_matrix->zv == BUF_ZV (b)
12182 && w->current_matrix->begv == BUF_BEGV (b))
12183 b->clip_changed = 0;
12185 /* If display wasn't paused, and W is not a tool bar window, see if
12186 point has been moved into or out of a composition. In that case,
12187 we set b->clip_changed to 1 to force updating the screen. If
12188 b->clip_changed has already been set to 1, we can skip this
12189 check. */
12190 if (!b->clip_changed
12191 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12193 EMACS_INT pt;
12195 if (w == XWINDOW (selected_window))
12196 pt = PT;
12197 else
12198 pt = marker_position (w->pointm);
12200 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12201 || pt != XINT (w->last_point))
12202 && check_point_in_composition (w->current_matrix->buffer,
12203 XINT (w->last_point),
12204 XBUFFER (w->buffer), pt))
12205 b->clip_changed = 1;
12210 /* Select FRAME to forward the values of frame-local variables into C
12211 variables so that the redisplay routines can access those values
12212 directly. */
12214 static void
12215 select_frame_for_redisplay (Lisp_Object frame)
12217 Lisp_Object tail, tem;
12218 Lisp_Object old = selected_frame;
12219 struct Lisp_Symbol *sym;
12221 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12223 selected_frame = frame;
12225 do {
12226 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12227 if (CONSP (XCAR (tail))
12228 && (tem = XCAR (XCAR (tail)),
12229 SYMBOLP (tem))
12230 && (sym = indirect_variable (XSYMBOL (tem)),
12231 sym->redirect == SYMBOL_LOCALIZED)
12232 && sym->val.blv->frame_local)
12233 /* Use find_symbol_value rather than Fsymbol_value
12234 to avoid an error if it is void. */
12235 find_symbol_value (tem);
12236 } while (!EQ (frame, old) && (frame = old, 1));
12240 #define STOP_POLLING \
12241 do { if (! polling_stopped_here) stop_polling (); \
12242 polling_stopped_here = 1; } while (0)
12244 #define RESUME_POLLING \
12245 do { if (polling_stopped_here) start_polling (); \
12246 polling_stopped_here = 0; } while (0)
12249 /* Perhaps in the future avoid recentering windows if it
12250 is not necessary; currently that causes some problems. */
12252 static void
12253 redisplay_internal (void)
12255 struct window *w = XWINDOW (selected_window);
12256 struct window *sw;
12257 struct frame *fr;
12258 int pending;
12259 int must_finish = 0;
12260 struct text_pos tlbufpos, tlendpos;
12261 int number_of_visible_frames;
12262 int count, count1;
12263 struct frame *sf;
12264 int polling_stopped_here = 0;
12265 Lisp_Object old_frame = selected_frame;
12267 /* Non-zero means redisplay has to consider all windows on all
12268 frames. Zero means, only selected_window is considered. */
12269 int consider_all_windows_p;
12271 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12273 /* No redisplay if running in batch mode or frame is not yet fully
12274 initialized, or redisplay is explicitly turned off by setting
12275 Vinhibit_redisplay. */
12276 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12277 || !NILP (Vinhibit_redisplay))
12278 return;
12280 /* Don't examine these until after testing Vinhibit_redisplay.
12281 When Emacs is shutting down, perhaps because its connection to
12282 X has dropped, we should not look at them at all. */
12283 fr = XFRAME (w->frame);
12284 sf = SELECTED_FRAME ();
12286 if (!fr->glyphs_initialized_p)
12287 return;
12289 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12290 if (popup_activated ())
12291 return;
12292 #endif
12294 /* I don't think this happens but let's be paranoid. */
12295 if (redisplaying_p)
12296 return;
12298 /* Record a function that resets redisplaying_p to its old value
12299 when we leave this function. */
12300 count = SPECPDL_INDEX ();
12301 record_unwind_protect (unwind_redisplay,
12302 Fcons (make_number (redisplaying_p), selected_frame));
12303 ++redisplaying_p;
12304 specbind (Qinhibit_free_realized_faces, Qnil);
12307 Lisp_Object tail, frame;
12309 FOR_EACH_FRAME (tail, frame)
12311 struct frame *f = XFRAME (frame);
12312 f->already_hscrolled_p = 0;
12316 retry:
12317 /* Remember the currently selected window. */
12318 sw = w;
12320 if (!EQ (old_frame, selected_frame)
12321 && FRAME_LIVE_P (XFRAME (old_frame)))
12322 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12323 selected_frame and selected_window to be temporarily out-of-sync so
12324 when we come back here via `goto retry', we need to resync because we
12325 may need to run Elisp code (via prepare_menu_bars). */
12326 select_frame_for_redisplay (old_frame);
12328 pending = 0;
12329 reconsider_clip_changes (w, current_buffer);
12330 last_escape_glyph_frame = NULL;
12331 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12332 last_glyphless_glyph_frame = NULL;
12333 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12335 /* If new fonts have been loaded that make a glyph matrix adjustment
12336 necessary, do it. */
12337 if (fonts_changed_p)
12339 adjust_glyphs (NULL);
12340 ++windows_or_buffers_changed;
12341 fonts_changed_p = 0;
12344 /* If face_change_count is non-zero, init_iterator will free all
12345 realized faces, which includes the faces referenced from current
12346 matrices. So, we can't reuse current matrices in this case. */
12347 if (face_change_count)
12348 ++windows_or_buffers_changed;
12350 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12351 && FRAME_TTY (sf)->previous_frame != sf)
12353 /* Since frames on a single ASCII terminal share the same
12354 display area, displaying a different frame means redisplay
12355 the whole thing. */
12356 windows_or_buffers_changed++;
12357 SET_FRAME_GARBAGED (sf);
12358 #ifndef DOS_NT
12359 set_tty_color_mode (FRAME_TTY (sf), sf);
12360 #endif
12361 FRAME_TTY (sf)->previous_frame = sf;
12364 /* Set the visible flags for all frames. Do this before checking
12365 for resized or garbaged frames; they want to know if their frames
12366 are visible. See the comment in frame.h for
12367 FRAME_SAMPLE_VISIBILITY. */
12369 Lisp_Object tail, frame;
12371 number_of_visible_frames = 0;
12373 FOR_EACH_FRAME (tail, frame)
12375 struct frame *f = XFRAME (frame);
12377 FRAME_SAMPLE_VISIBILITY (f);
12378 if (FRAME_VISIBLE_P (f))
12379 ++number_of_visible_frames;
12380 clear_desired_matrices (f);
12384 /* Notice any pending interrupt request to change frame size. */
12385 do_pending_window_change (1);
12387 /* do_pending_window_change could change the selected_window due to
12388 frame resizing which makes the selected window too small. */
12389 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12391 sw = w;
12392 reconsider_clip_changes (w, current_buffer);
12395 /* Clear frames marked as garbaged. */
12396 if (frame_garbaged)
12397 clear_garbaged_frames ();
12399 /* Build menubar and tool-bar items. */
12400 if (NILP (Vmemory_full))
12401 prepare_menu_bars ();
12403 if (windows_or_buffers_changed)
12404 update_mode_lines++;
12406 /* Detect case that we need to write or remove a star in the mode line. */
12407 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
12409 w->update_mode_line = Qt;
12410 if (buffer_shared > 1)
12411 update_mode_lines++;
12414 /* Avoid invocation of point motion hooks by `current_column' below. */
12415 count1 = SPECPDL_INDEX ();
12416 specbind (Qinhibit_point_motion_hooks, Qt);
12418 /* If %c is in the mode line, update it if needed. */
12419 if (!NILP (w->column_number_displayed)
12420 /* This alternative quickly identifies a common case
12421 where no change is needed. */
12422 && !(PT == XFASTINT (w->last_point)
12423 && XFASTINT (w->last_modified) >= MODIFF
12424 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12425 && (XFASTINT (w->column_number_displayed) != current_column ()))
12426 w->update_mode_line = Qt;
12428 unbind_to (count1, Qnil);
12430 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12432 /* The variable buffer_shared is set in redisplay_window and
12433 indicates that we redisplay a buffer in different windows. See
12434 there. */
12435 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
12436 || cursor_type_changed);
12438 /* If specs for an arrow have changed, do thorough redisplay
12439 to ensure we remove any arrow that should no longer exist. */
12440 if (overlay_arrows_changed_p ())
12441 consider_all_windows_p = windows_or_buffers_changed = 1;
12443 /* Normally the message* functions will have already displayed and
12444 updated the echo area, but the frame may have been trashed, or
12445 the update may have been preempted, so display the echo area
12446 again here. Checking message_cleared_p captures the case that
12447 the echo area should be cleared. */
12448 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12449 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12450 || (message_cleared_p
12451 && minibuf_level == 0
12452 /* If the mini-window is currently selected, this means the
12453 echo-area doesn't show through. */
12454 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12456 int window_height_changed_p = echo_area_display (0);
12457 must_finish = 1;
12459 /* If we don't display the current message, don't clear the
12460 message_cleared_p flag, because, if we did, we wouldn't clear
12461 the echo area in the next redisplay which doesn't preserve
12462 the echo area. */
12463 if (!display_last_displayed_message_p)
12464 message_cleared_p = 0;
12466 if (fonts_changed_p)
12467 goto retry;
12468 else if (window_height_changed_p)
12470 consider_all_windows_p = 1;
12471 ++update_mode_lines;
12472 ++windows_or_buffers_changed;
12474 /* If window configuration was changed, frames may have been
12475 marked garbaged. Clear them or we will experience
12476 surprises wrt scrolling. */
12477 if (frame_garbaged)
12478 clear_garbaged_frames ();
12481 else if (EQ (selected_window, minibuf_window)
12482 && (current_buffer->clip_changed
12483 || XFASTINT (w->last_modified) < MODIFF
12484 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12485 && resize_mini_window (w, 0))
12487 /* Resized active mini-window to fit the size of what it is
12488 showing if its contents might have changed. */
12489 must_finish = 1;
12490 /* FIXME: this causes all frames to be updated, which seems unnecessary
12491 since only the current frame needs to be considered. This function needs
12492 to be rewritten with two variables, consider_all_windows and
12493 consider_all_frames. */
12494 consider_all_windows_p = 1;
12495 ++windows_or_buffers_changed;
12496 ++update_mode_lines;
12498 /* If window configuration was changed, frames may have been
12499 marked garbaged. Clear them or we will experience
12500 surprises wrt scrolling. */
12501 if (frame_garbaged)
12502 clear_garbaged_frames ();
12506 /* If showing the region, and mark has changed, we must redisplay
12507 the whole window. The assignment to this_line_start_pos prevents
12508 the optimization directly below this if-statement. */
12509 if (((!NILP (Vtransient_mark_mode)
12510 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
12511 != !NILP (w->region_showing))
12512 || (!NILP (w->region_showing)
12513 && !EQ (w->region_showing,
12514 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
12515 CHARPOS (this_line_start_pos) = 0;
12517 /* Optimize the case that only the line containing the cursor in the
12518 selected window has changed. Variables starting with this_ are
12519 set in display_line and record information about the line
12520 containing the cursor. */
12521 tlbufpos = this_line_start_pos;
12522 tlendpos = this_line_end_pos;
12523 if (!consider_all_windows_p
12524 && CHARPOS (tlbufpos) > 0
12525 && NILP (w->update_mode_line)
12526 && !current_buffer->clip_changed
12527 && !current_buffer->prevent_redisplay_optimizations_p
12528 && FRAME_VISIBLE_P (XFRAME (w->frame))
12529 && !FRAME_OBSCURED_P (XFRAME (w->frame))
12530 /* Make sure recorded data applies to current buffer, etc. */
12531 && this_line_buffer == current_buffer
12532 && current_buffer == XBUFFER (w->buffer)
12533 && NILP (w->force_start)
12534 && NILP (w->optional_new_start)
12535 /* Point must be on the line that we have info recorded about. */
12536 && PT >= CHARPOS (tlbufpos)
12537 && PT <= Z - CHARPOS (tlendpos)
12538 /* All text outside that line, including its final newline,
12539 must be unchanged. */
12540 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
12541 CHARPOS (tlendpos)))
12543 if (CHARPOS (tlbufpos) > BEGV
12544 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
12545 && (CHARPOS (tlbufpos) == ZV
12546 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
12547 /* Former continuation line has disappeared by becoming empty. */
12548 goto cancel;
12549 else if (XFASTINT (w->last_modified) < MODIFF
12550 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
12551 || MINI_WINDOW_P (w))
12553 /* We have to handle the case of continuation around a
12554 wide-column character (see the comment in indent.c around
12555 line 1340).
12557 For instance, in the following case:
12559 -------- Insert --------
12560 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
12561 J_I_ ==> J_I_ `^^' are cursors.
12562 ^^ ^^
12563 -------- --------
12565 As we have to redraw the line above, we cannot use this
12566 optimization. */
12568 struct it it;
12569 int line_height_before = this_line_pixel_height;
12571 /* Note that start_display will handle the case that the
12572 line starting at tlbufpos is a continuation line. */
12573 start_display (&it, w, tlbufpos);
12575 /* Implementation note: It this still necessary? */
12576 if (it.current_x != this_line_start_x)
12577 goto cancel;
12579 TRACE ((stderr, "trying display optimization 1\n"));
12580 w->cursor.vpos = -1;
12581 overlay_arrow_seen = 0;
12582 it.vpos = this_line_vpos;
12583 it.current_y = this_line_y;
12584 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
12585 display_line (&it);
12587 /* If line contains point, is not continued,
12588 and ends at same distance from eob as before, we win. */
12589 if (w->cursor.vpos >= 0
12590 /* Line is not continued, otherwise this_line_start_pos
12591 would have been set to 0 in display_line. */
12592 && CHARPOS (this_line_start_pos)
12593 /* Line ends as before. */
12594 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
12595 /* Line has same height as before. Otherwise other lines
12596 would have to be shifted up or down. */
12597 && this_line_pixel_height == line_height_before)
12599 /* If this is not the window's last line, we must adjust
12600 the charstarts of the lines below. */
12601 if (it.current_y < it.last_visible_y)
12603 struct glyph_row *row
12604 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
12605 EMACS_INT delta, delta_bytes;
12607 /* We used to distinguish between two cases here,
12608 conditioned by Z - CHARPOS (tlendpos) == ZV, for
12609 when the line ends in a newline or the end of the
12610 buffer's accessible portion. But both cases did
12611 the same, so they were collapsed. */
12612 delta = (Z
12613 - CHARPOS (tlendpos)
12614 - MATRIX_ROW_START_CHARPOS (row));
12615 delta_bytes = (Z_BYTE
12616 - BYTEPOS (tlendpos)
12617 - MATRIX_ROW_START_BYTEPOS (row));
12619 increment_matrix_positions (w->current_matrix,
12620 this_line_vpos + 1,
12621 w->current_matrix->nrows,
12622 delta, delta_bytes);
12625 /* If this row displays text now but previously didn't,
12626 or vice versa, w->window_end_vpos may have to be
12627 adjusted. */
12628 if ((it.glyph_row - 1)->displays_text_p)
12630 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
12631 XSETINT (w->window_end_vpos, this_line_vpos);
12633 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
12634 && this_line_vpos > 0)
12635 XSETINT (w->window_end_vpos, this_line_vpos - 1);
12636 w->window_end_valid = Qnil;
12638 /* Update hint: No need to try to scroll in update_window. */
12639 w->desired_matrix->no_scrolling_p = 1;
12641 #if GLYPH_DEBUG
12642 *w->desired_matrix->method = 0;
12643 debug_method_add (w, "optimization 1");
12644 #endif
12645 #ifdef HAVE_WINDOW_SYSTEM
12646 update_window_fringes (w, 0);
12647 #endif
12648 goto update;
12650 else
12651 goto cancel;
12653 else if (/* Cursor position hasn't changed. */
12654 PT == XFASTINT (w->last_point)
12655 /* Make sure the cursor was last displayed
12656 in this window. Otherwise we have to reposition it. */
12657 && 0 <= w->cursor.vpos
12658 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
12660 if (!must_finish)
12662 do_pending_window_change (1);
12663 /* If selected_window changed, redisplay again. */
12664 if (WINDOWP (selected_window)
12665 && (w = XWINDOW (selected_window)) != sw)
12666 goto retry;
12668 /* We used to always goto end_of_redisplay here, but this
12669 isn't enough if we have a blinking cursor. */
12670 if (w->cursor_off_p == w->last_cursor_off_p)
12671 goto end_of_redisplay;
12673 goto update;
12675 /* If highlighting the region, or if the cursor is in the echo area,
12676 then we can't just move the cursor. */
12677 else if (! (!NILP (Vtransient_mark_mode)
12678 && !NILP (BVAR (current_buffer, mark_active)))
12679 && (EQ (selected_window, BVAR (current_buffer, last_selected_window))
12680 || highlight_nonselected_windows)
12681 && NILP (w->region_showing)
12682 && NILP (Vshow_trailing_whitespace)
12683 && !cursor_in_echo_area)
12685 struct it it;
12686 struct glyph_row *row;
12688 /* Skip from tlbufpos to PT and see where it is. Note that
12689 PT may be in invisible text. If so, we will end at the
12690 next visible position. */
12691 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
12692 NULL, DEFAULT_FACE_ID);
12693 it.current_x = this_line_start_x;
12694 it.current_y = this_line_y;
12695 it.vpos = this_line_vpos;
12697 /* The call to move_it_to stops in front of PT, but
12698 moves over before-strings. */
12699 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
12701 if (it.vpos == this_line_vpos
12702 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
12703 row->enabled_p))
12705 xassert (this_line_vpos == it.vpos);
12706 xassert (this_line_y == it.current_y);
12707 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12708 #if GLYPH_DEBUG
12709 *w->desired_matrix->method = 0;
12710 debug_method_add (w, "optimization 3");
12711 #endif
12712 goto update;
12714 else
12715 goto cancel;
12718 cancel:
12719 /* Text changed drastically or point moved off of line. */
12720 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
12723 CHARPOS (this_line_start_pos) = 0;
12724 consider_all_windows_p |= buffer_shared > 1;
12725 ++clear_face_cache_count;
12726 #ifdef HAVE_WINDOW_SYSTEM
12727 ++clear_image_cache_count;
12728 #endif
12730 /* Build desired matrices, and update the display. If
12731 consider_all_windows_p is non-zero, do it for all windows on all
12732 frames. Otherwise do it for selected_window, only. */
12734 if (consider_all_windows_p)
12736 Lisp_Object tail, frame;
12738 FOR_EACH_FRAME (tail, frame)
12739 XFRAME (frame)->updated_p = 0;
12741 /* Recompute # windows showing selected buffer. This will be
12742 incremented each time such a window is displayed. */
12743 buffer_shared = 0;
12745 FOR_EACH_FRAME (tail, frame)
12747 struct frame *f = XFRAME (frame);
12749 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
12751 if (! EQ (frame, selected_frame))
12752 /* Select the frame, for the sake of frame-local
12753 variables. */
12754 select_frame_for_redisplay (frame);
12756 /* Mark all the scroll bars to be removed; we'll redeem
12757 the ones we want when we redisplay their windows. */
12758 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
12759 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
12761 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12762 redisplay_windows (FRAME_ROOT_WINDOW (f));
12764 /* The X error handler may have deleted that frame. */
12765 if (!FRAME_LIVE_P (f))
12766 continue;
12768 /* Any scroll bars which redisplay_windows should have
12769 nuked should now go away. */
12770 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12771 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12773 /* If fonts changed, display again. */
12774 /* ??? rms: I suspect it is a mistake to jump all the way
12775 back to retry here. It should just retry this frame. */
12776 if (fonts_changed_p)
12777 goto retry;
12779 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12781 /* See if we have to hscroll. */
12782 if (!f->already_hscrolled_p)
12784 f->already_hscrolled_p = 1;
12785 if (hscroll_windows (f->root_window))
12786 goto retry;
12789 /* Prevent various kinds of signals during display
12790 update. stdio is not robust about handling
12791 signals, which can cause an apparent I/O
12792 error. */
12793 if (interrupt_input)
12794 unrequest_sigio ();
12795 STOP_POLLING;
12797 /* Update the display. */
12798 set_window_update_flags (XWINDOW (f->root_window), 1);
12799 pending |= update_frame (f, 0, 0);
12800 f->updated_p = 1;
12805 if (!EQ (old_frame, selected_frame)
12806 && FRAME_LIVE_P (XFRAME (old_frame)))
12807 /* We played a bit fast-and-loose above and allowed selected_frame
12808 and selected_window to be temporarily out-of-sync but let's make
12809 sure this stays contained. */
12810 select_frame_for_redisplay (old_frame);
12811 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12813 if (!pending)
12815 /* Do the mark_window_display_accurate after all windows have
12816 been redisplayed because this call resets flags in buffers
12817 which are needed for proper redisplay. */
12818 FOR_EACH_FRAME (tail, frame)
12820 struct frame *f = XFRAME (frame);
12821 if (f->updated_p)
12823 mark_window_display_accurate (f->root_window, 1);
12824 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12825 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12830 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12832 Lisp_Object mini_window;
12833 struct frame *mini_frame;
12835 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12836 /* Use list_of_error, not Qerror, so that
12837 we catch only errors and don't run the debugger. */
12838 internal_condition_case_1 (redisplay_window_1, selected_window,
12839 list_of_error,
12840 redisplay_window_error);
12842 /* Compare desired and current matrices, perform output. */
12844 update:
12845 /* If fonts changed, display again. */
12846 if (fonts_changed_p)
12847 goto retry;
12849 /* Prevent various kinds of signals during display update.
12850 stdio is not robust about handling signals,
12851 which can cause an apparent I/O error. */
12852 if (interrupt_input)
12853 unrequest_sigio ();
12854 STOP_POLLING;
12856 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12858 if (hscroll_windows (selected_window))
12859 goto retry;
12861 XWINDOW (selected_window)->must_be_updated_p = 1;
12862 pending = update_frame (sf, 0, 0);
12865 /* We may have called echo_area_display at the top of this
12866 function. If the echo area is on another frame, that may
12867 have put text on a frame other than the selected one, so the
12868 above call to update_frame would not have caught it. Catch
12869 it here. */
12870 mini_window = FRAME_MINIBUF_WINDOW (sf);
12871 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12873 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12875 XWINDOW (mini_window)->must_be_updated_p = 1;
12876 pending |= update_frame (mini_frame, 0, 0);
12877 if (!pending && hscroll_windows (mini_window))
12878 goto retry;
12882 /* If display was paused because of pending input, make sure we do a
12883 thorough update the next time. */
12884 if (pending)
12886 /* Prevent the optimization at the beginning of
12887 redisplay_internal that tries a single-line update of the
12888 line containing the cursor in the selected window. */
12889 CHARPOS (this_line_start_pos) = 0;
12891 /* Let the overlay arrow be updated the next time. */
12892 update_overlay_arrows (0);
12894 /* If we pause after scrolling, some rows in the current
12895 matrices of some windows are not valid. */
12896 if (!WINDOW_FULL_WIDTH_P (w)
12897 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12898 update_mode_lines = 1;
12900 else
12902 if (!consider_all_windows_p)
12904 /* This has already been done above if
12905 consider_all_windows_p is set. */
12906 mark_window_display_accurate_1 (w, 1);
12908 /* Say overlay arrows are up to date. */
12909 update_overlay_arrows (1);
12911 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12912 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12915 update_mode_lines = 0;
12916 windows_or_buffers_changed = 0;
12917 cursor_type_changed = 0;
12920 /* Start SIGIO interrupts coming again. Having them off during the
12921 code above makes it less likely one will discard output, but not
12922 impossible, since there might be stuff in the system buffer here.
12923 But it is much hairier to try to do anything about that. */
12924 if (interrupt_input)
12925 request_sigio ();
12926 RESUME_POLLING;
12928 /* If a frame has become visible which was not before, redisplay
12929 again, so that we display it. Expose events for such a frame
12930 (which it gets when becoming visible) don't call the parts of
12931 redisplay constructing glyphs, so simply exposing a frame won't
12932 display anything in this case. So, we have to display these
12933 frames here explicitly. */
12934 if (!pending)
12936 Lisp_Object tail, frame;
12937 int new_count = 0;
12939 FOR_EACH_FRAME (tail, frame)
12941 int this_is_visible = 0;
12943 if (XFRAME (frame)->visible)
12944 this_is_visible = 1;
12945 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12946 if (XFRAME (frame)->visible)
12947 this_is_visible = 1;
12949 if (this_is_visible)
12950 new_count++;
12953 if (new_count != number_of_visible_frames)
12954 windows_or_buffers_changed++;
12957 /* Change frame size now if a change is pending. */
12958 do_pending_window_change (1);
12960 /* If we just did a pending size change, or have additional
12961 visible frames, or selected_window changed, redisplay again. */
12962 if ((windows_or_buffers_changed && !pending)
12963 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
12964 goto retry;
12966 /* Clear the face and image caches.
12968 We used to do this only if consider_all_windows_p. But the cache
12969 needs to be cleared if a timer creates images in the current
12970 buffer (e.g. the test case in Bug#6230). */
12972 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12974 clear_face_cache (0);
12975 clear_face_cache_count = 0;
12978 #ifdef HAVE_WINDOW_SYSTEM
12979 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12981 clear_image_caches (Qnil);
12982 clear_image_cache_count = 0;
12984 #endif /* HAVE_WINDOW_SYSTEM */
12986 end_of_redisplay:
12987 unbind_to (count, Qnil);
12988 RESUME_POLLING;
12992 /* Redisplay, but leave alone any recent echo area message unless
12993 another message has been requested in its place.
12995 This is useful in situations where you need to redisplay but no
12996 user action has occurred, making it inappropriate for the message
12997 area to be cleared. See tracking_off and
12998 wait_reading_process_output for examples of these situations.
13000 FROM_WHERE is an integer saying from where this function was
13001 called. This is useful for debugging. */
13003 void
13004 redisplay_preserve_echo_area (int from_where)
13006 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13008 if (!NILP (echo_area_buffer[1]))
13010 /* We have a previously displayed message, but no current
13011 message. Redisplay the previous message. */
13012 display_last_displayed_message_p = 1;
13013 redisplay_internal ();
13014 display_last_displayed_message_p = 0;
13016 else
13017 redisplay_internal ();
13019 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13020 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13021 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13025 /* Function registered with record_unwind_protect in
13026 redisplay_internal. Reset redisplaying_p to the value it had
13027 before redisplay_internal was called, and clear
13028 prevent_freeing_realized_faces_p. It also selects the previously
13029 selected frame, unless it has been deleted (by an X connection
13030 failure during redisplay, for example). */
13032 static Lisp_Object
13033 unwind_redisplay (Lisp_Object val)
13035 Lisp_Object old_redisplaying_p, old_frame;
13037 old_redisplaying_p = XCAR (val);
13038 redisplaying_p = XFASTINT (old_redisplaying_p);
13039 old_frame = XCDR (val);
13040 if (! EQ (old_frame, selected_frame)
13041 && FRAME_LIVE_P (XFRAME (old_frame)))
13042 select_frame_for_redisplay (old_frame);
13043 return Qnil;
13047 /* Mark the display of window W as accurate or inaccurate. If
13048 ACCURATE_P is non-zero mark display of W as accurate. If
13049 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13050 redisplay_internal is called. */
13052 static void
13053 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13055 if (BUFFERP (w->buffer))
13057 struct buffer *b = XBUFFER (w->buffer);
13059 w->last_modified
13060 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
13061 w->last_overlay_modified
13062 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
13063 w->last_had_star
13064 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
13066 if (accurate_p)
13068 b->clip_changed = 0;
13069 b->prevent_redisplay_optimizations_p = 0;
13071 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13072 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13073 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13074 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13076 w->current_matrix->buffer = b;
13077 w->current_matrix->begv = BUF_BEGV (b);
13078 w->current_matrix->zv = BUF_ZV (b);
13080 w->last_cursor = w->cursor;
13081 w->last_cursor_off_p = w->cursor_off_p;
13083 if (w == XWINDOW (selected_window))
13084 w->last_point = make_number (BUF_PT (b));
13085 else
13086 w->last_point = make_number (XMARKER (w->pointm)->charpos);
13090 if (accurate_p)
13092 w->window_end_valid = w->buffer;
13093 w->update_mode_line = Qnil;
13098 /* Mark the display of windows in the window tree rooted at WINDOW as
13099 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13100 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13101 be redisplayed the next time redisplay_internal is called. */
13103 void
13104 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13106 struct window *w;
13108 for (; !NILP (window); window = w->next)
13110 w = XWINDOW (window);
13111 mark_window_display_accurate_1 (w, accurate_p);
13113 if (!NILP (w->vchild))
13114 mark_window_display_accurate (w->vchild, accurate_p);
13115 if (!NILP (w->hchild))
13116 mark_window_display_accurate (w->hchild, accurate_p);
13119 if (accurate_p)
13121 update_overlay_arrows (1);
13123 else
13125 /* Force a thorough redisplay the next time by setting
13126 last_arrow_position and last_arrow_string to t, which is
13127 unequal to any useful value of Voverlay_arrow_... */
13128 update_overlay_arrows (-1);
13133 /* Return value in display table DP (Lisp_Char_Table *) for character
13134 C. Since a display table doesn't have any parent, we don't have to
13135 follow parent. Do not call this function directly but use the
13136 macro DISP_CHAR_VECTOR. */
13138 Lisp_Object
13139 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13141 Lisp_Object val;
13143 if (ASCII_CHAR_P (c))
13145 val = dp->ascii;
13146 if (SUB_CHAR_TABLE_P (val))
13147 val = XSUB_CHAR_TABLE (val)->contents[c];
13149 else
13151 Lisp_Object table;
13153 XSETCHAR_TABLE (table, dp);
13154 val = char_table_ref (table, c);
13156 if (NILP (val))
13157 val = dp->defalt;
13158 return val;
13163 /***********************************************************************
13164 Window Redisplay
13165 ***********************************************************************/
13167 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13169 static void
13170 redisplay_windows (Lisp_Object window)
13172 while (!NILP (window))
13174 struct window *w = XWINDOW (window);
13176 if (!NILP (w->hchild))
13177 redisplay_windows (w->hchild);
13178 else if (!NILP (w->vchild))
13179 redisplay_windows (w->vchild);
13180 else if (!NILP (w->buffer))
13182 displayed_buffer = XBUFFER (w->buffer);
13183 /* Use list_of_error, not Qerror, so that
13184 we catch only errors and don't run the debugger. */
13185 internal_condition_case_1 (redisplay_window_0, window,
13186 list_of_error,
13187 redisplay_window_error);
13190 window = w->next;
13194 static Lisp_Object
13195 redisplay_window_error (Lisp_Object ignore)
13197 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13198 return Qnil;
13201 static Lisp_Object
13202 redisplay_window_0 (Lisp_Object window)
13204 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13205 redisplay_window (window, 0);
13206 return Qnil;
13209 static Lisp_Object
13210 redisplay_window_1 (Lisp_Object window)
13212 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13213 redisplay_window (window, 1);
13214 return Qnil;
13218 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13219 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13220 which positions recorded in ROW differ from current buffer
13221 positions.
13223 Return 0 if cursor is not on this row, 1 otherwise. */
13225 static int
13226 set_cursor_from_row (struct window *w, struct glyph_row *row,
13227 struct glyph_matrix *matrix,
13228 EMACS_INT delta, EMACS_INT delta_bytes,
13229 int dy, int dvpos)
13231 struct glyph *glyph = row->glyphs[TEXT_AREA];
13232 struct glyph *end = glyph + row->used[TEXT_AREA];
13233 struct glyph *cursor = NULL;
13234 /* The last known character position in row. */
13235 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13236 int x = row->x;
13237 EMACS_INT pt_old = PT - delta;
13238 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13239 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13240 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13241 /* A glyph beyond the edge of TEXT_AREA which we should never
13242 touch. */
13243 struct glyph *glyphs_end = end;
13244 /* Non-zero means we've found a match for cursor position, but that
13245 glyph has the avoid_cursor_p flag set. */
13246 int match_with_avoid_cursor = 0;
13247 /* Non-zero means we've seen at least one glyph that came from a
13248 display string. */
13249 int string_seen = 0;
13250 /* Largest and smalles buffer positions seen so far during scan of
13251 glyph row. */
13252 EMACS_INT bpos_max = pos_before;
13253 EMACS_INT bpos_min = pos_after;
13254 /* Last buffer position covered by an overlay string with an integer
13255 `cursor' property. */
13256 EMACS_INT bpos_covered = 0;
13258 /* Skip over glyphs not having an object at the start and the end of
13259 the row. These are special glyphs like truncation marks on
13260 terminal frames. */
13261 if (row->displays_text_p)
13263 if (!row->reversed_p)
13265 while (glyph < end
13266 && INTEGERP (glyph->object)
13267 && glyph->charpos < 0)
13269 x += glyph->pixel_width;
13270 ++glyph;
13272 while (end > glyph
13273 && INTEGERP ((end - 1)->object)
13274 /* CHARPOS is zero for blanks and stretch glyphs
13275 inserted by extend_face_to_end_of_line. */
13276 && (end - 1)->charpos <= 0)
13277 --end;
13278 glyph_before = glyph - 1;
13279 glyph_after = end;
13281 else
13283 struct glyph *g;
13285 /* If the glyph row is reversed, we need to process it from back
13286 to front, so swap the edge pointers. */
13287 glyphs_end = end = glyph - 1;
13288 glyph += row->used[TEXT_AREA] - 1;
13290 while (glyph > end + 1
13291 && INTEGERP (glyph->object)
13292 && glyph->charpos < 0)
13294 --glyph;
13295 x -= glyph->pixel_width;
13297 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13298 --glyph;
13299 /* By default, in reversed rows we put the cursor on the
13300 rightmost (first in the reading order) glyph. */
13301 for (g = end + 1; g < glyph; g++)
13302 x += g->pixel_width;
13303 while (end < glyph
13304 && INTEGERP ((end + 1)->object)
13305 && (end + 1)->charpos <= 0)
13306 ++end;
13307 glyph_before = glyph + 1;
13308 glyph_after = end;
13311 else if (row->reversed_p)
13313 /* In R2L rows that don't display text, put the cursor on the
13314 rightmost glyph. Case in point: an empty last line that is
13315 part of an R2L paragraph. */
13316 cursor = end - 1;
13317 /* Avoid placing the cursor on the last glyph of the row, where
13318 on terminal frames we hold the vertical border between
13319 adjacent windows. */
13320 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13321 && !WINDOW_RIGHTMOST_P (w)
13322 && cursor == row->glyphs[LAST_AREA] - 1)
13323 cursor--;
13324 x = -1; /* will be computed below, at label compute_x */
13327 /* Step 1: Try to find the glyph whose character position
13328 corresponds to point. If that's not possible, find 2 glyphs
13329 whose character positions are the closest to point, one before
13330 point, the other after it. */
13331 if (!row->reversed_p)
13332 while (/* not marched to end of glyph row */
13333 glyph < end
13334 /* glyph was not inserted by redisplay for internal purposes */
13335 && !INTEGERP (glyph->object))
13337 if (BUFFERP (glyph->object))
13339 EMACS_INT dpos = glyph->charpos - pt_old;
13341 if (glyph->charpos > bpos_max)
13342 bpos_max = glyph->charpos;
13343 if (glyph->charpos < bpos_min)
13344 bpos_min = glyph->charpos;
13345 if (!glyph->avoid_cursor_p)
13347 /* If we hit point, we've found the glyph on which to
13348 display the cursor. */
13349 if (dpos == 0)
13351 match_with_avoid_cursor = 0;
13352 break;
13354 /* See if we've found a better approximation to
13355 POS_BEFORE or to POS_AFTER. Note that we want the
13356 first (leftmost) glyph of all those that are the
13357 closest from below, and the last (rightmost) of all
13358 those from above. */
13359 if (0 > dpos && dpos > pos_before - pt_old)
13361 pos_before = glyph->charpos;
13362 glyph_before = glyph;
13364 else if (0 < dpos && dpos <= pos_after - pt_old)
13366 pos_after = glyph->charpos;
13367 glyph_after = glyph;
13370 else if (dpos == 0)
13371 match_with_avoid_cursor = 1;
13373 else if (STRINGP (glyph->object))
13375 Lisp_Object chprop;
13376 EMACS_INT glyph_pos = glyph->charpos;
13378 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13379 glyph->object);
13380 if (INTEGERP (chprop))
13382 bpos_covered = bpos_max + XINT (chprop);
13383 /* If the `cursor' property covers buffer positions up
13384 to and including point, we should display cursor on
13385 this glyph. Note that overlays and text properties
13386 with string values stop bidi reordering, so every
13387 buffer position to the left of the string is always
13388 smaller than any position to the right of the
13389 string. Therefore, if a `cursor' property on one
13390 of the string's characters has an integer value, we
13391 will break out of the loop below _before_ we get to
13392 the position match above. IOW, integer values of
13393 the `cursor' property override the "exact match for
13394 point" strategy of positioning the cursor. */
13395 /* Implementation note: bpos_max == pt_old when, e.g.,
13396 we are in an empty line, where bpos_max is set to
13397 MATRIX_ROW_START_CHARPOS, see above. */
13398 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13400 cursor = glyph;
13401 break;
13405 string_seen = 1;
13407 x += glyph->pixel_width;
13408 ++glyph;
13410 else if (glyph > end) /* row is reversed */
13411 while (!INTEGERP (glyph->object))
13413 if (BUFFERP (glyph->object))
13415 EMACS_INT dpos = glyph->charpos - pt_old;
13417 if (glyph->charpos > bpos_max)
13418 bpos_max = glyph->charpos;
13419 if (glyph->charpos < bpos_min)
13420 bpos_min = glyph->charpos;
13421 if (!glyph->avoid_cursor_p)
13423 if (dpos == 0)
13425 match_with_avoid_cursor = 0;
13426 break;
13428 if (0 > dpos && dpos > pos_before - pt_old)
13430 pos_before = glyph->charpos;
13431 glyph_before = glyph;
13433 else if (0 < dpos && dpos <= pos_after - pt_old)
13435 pos_after = glyph->charpos;
13436 glyph_after = glyph;
13439 else if (dpos == 0)
13440 match_with_avoid_cursor = 1;
13442 else if (STRINGP (glyph->object))
13444 Lisp_Object chprop;
13445 EMACS_INT glyph_pos = glyph->charpos;
13447 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13448 glyph->object);
13449 if (INTEGERP (chprop))
13451 bpos_covered = bpos_max + XINT (chprop);
13452 /* If the `cursor' property covers buffer positions up
13453 to and including point, we should display cursor on
13454 this glyph. */
13455 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13457 cursor = glyph;
13458 break;
13461 string_seen = 1;
13463 --glyph;
13464 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
13466 x--; /* can't use any pixel_width */
13467 break;
13469 x -= glyph->pixel_width;
13472 /* Step 2: If we didn't find an exact match for point, we need to
13473 look for a proper place to put the cursor among glyphs between
13474 GLYPH_BEFORE and GLYPH_AFTER. */
13475 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13476 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
13477 && bpos_covered < pt_old)
13479 /* An empty line has a single glyph whose OBJECT is zero and
13480 whose CHARPOS is the position of a newline on that line.
13481 Note that on a TTY, there are more glyphs after that, which
13482 were produced by extend_face_to_end_of_line, but their
13483 CHARPOS is zero or negative. */
13484 int empty_line_p =
13485 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13486 && INTEGERP (glyph->object) && glyph->charpos > 0;
13488 if (row->ends_in_ellipsis_p && pos_after == last_pos)
13490 EMACS_INT ellipsis_pos;
13492 /* Scan back over the ellipsis glyphs. */
13493 if (!row->reversed_p)
13495 ellipsis_pos = (glyph - 1)->charpos;
13496 while (glyph > row->glyphs[TEXT_AREA]
13497 && (glyph - 1)->charpos == ellipsis_pos)
13498 glyph--, x -= glyph->pixel_width;
13499 /* That loop always goes one position too far, including
13500 the glyph before the ellipsis. So scan forward over
13501 that one. */
13502 x += glyph->pixel_width;
13503 glyph++;
13505 else /* row is reversed */
13507 ellipsis_pos = (glyph + 1)->charpos;
13508 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
13509 && (glyph + 1)->charpos == ellipsis_pos)
13510 glyph++, x += glyph->pixel_width;
13511 x -= glyph->pixel_width;
13512 glyph--;
13515 else if (match_with_avoid_cursor
13516 /* A truncated row may not include PT among its
13517 character positions. Setting the cursor inside the
13518 scroll margin will trigger recalculation of hscroll
13519 in hscroll_window_tree. */
13520 || (row->truncated_on_left_p && pt_old < bpos_min)
13521 || (row->truncated_on_right_p && pt_old > bpos_max)
13522 /* Zero-width characters produce no glyphs. */
13523 || (!string_seen
13524 && !empty_line_p
13525 && (row->reversed_p
13526 ? glyph_after > glyphs_end
13527 : glyph_after < glyphs_end)))
13529 cursor = glyph_after;
13530 x = -1;
13532 else if (string_seen)
13534 int incr = row->reversed_p ? -1 : +1;
13536 /* Need to find the glyph that came out of a string which is
13537 present at point. That glyph is somewhere between
13538 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
13539 positioned between POS_BEFORE and POS_AFTER in the
13540 buffer. */
13541 struct glyph *start, *stop;
13542 EMACS_INT pos = pos_before;
13544 x = -1;
13546 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
13547 correspond to POS_BEFORE and POS_AFTER, respectively. We
13548 need START and STOP in the order that corresponds to the
13549 row's direction as given by its reversed_p flag. If the
13550 directionality of characters between POS_BEFORE and
13551 POS_AFTER is the opposite of the row's base direction,
13552 these characters will have been reordered for display,
13553 and we need to reverse START and STOP. */
13554 if (!row->reversed_p)
13556 start = min (glyph_before, glyph_after);
13557 stop = max (glyph_before, glyph_after);
13559 else
13561 start = max (glyph_before, glyph_after);
13562 stop = min (glyph_before, glyph_after);
13564 for (glyph = start + incr;
13565 row->reversed_p ? glyph > stop : glyph < stop; )
13568 /* Any glyphs that come from the buffer are here because
13569 of bidi reordering. Skip them, and only pay
13570 attention to glyphs that came from some string. */
13571 if (STRINGP (glyph->object))
13573 Lisp_Object str;
13574 EMACS_INT tem;
13576 str = glyph->object;
13577 tem = string_buffer_position_lim (str, pos, pos_after, 0);
13578 if (tem == 0 /* from overlay */
13579 || pos <= tem)
13581 /* If the string from which this glyph came is
13582 found in the buffer at point, then we've
13583 found the glyph we've been looking for. If
13584 it comes from an overlay (tem == 0), and it
13585 has the `cursor' property on one of its
13586 glyphs, record that glyph as a candidate for
13587 displaying the cursor. (As in the
13588 unidirectional version, we will display the
13589 cursor on the last candidate we find.) */
13590 if (tem == 0 || tem == pt_old)
13592 /* The glyphs from this string could have
13593 been reordered. Find the one with the
13594 smallest string position. Or there could
13595 be a character in the string with the
13596 `cursor' property, which means display
13597 cursor on that character's glyph. */
13598 EMACS_INT strpos = glyph->charpos;
13600 if (tem)
13601 cursor = glyph;
13602 for ( ;
13603 (row->reversed_p ? glyph > stop : glyph < stop)
13604 && EQ (glyph->object, str);
13605 glyph += incr)
13607 Lisp_Object cprop;
13608 EMACS_INT gpos = glyph->charpos;
13610 cprop = Fget_char_property (make_number (gpos),
13611 Qcursor,
13612 glyph->object);
13613 if (!NILP (cprop))
13615 cursor = glyph;
13616 break;
13618 if (tem && glyph->charpos < strpos)
13620 strpos = glyph->charpos;
13621 cursor = glyph;
13625 if (tem == pt_old)
13626 goto compute_x;
13628 if (tem)
13629 pos = tem + 1; /* don't find previous instances */
13631 /* This string is not what we want; skip all of the
13632 glyphs that came from it. */
13633 while ((row->reversed_p ? glyph > stop : glyph < stop)
13634 && EQ (glyph->object, str))
13635 glyph += incr;
13637 else
13638 glyph += incr;
13641 /* If we reached the end of the line, and END was from a string,
13642 the cursor is not on this line. */
13643 if (cursor == NULL
13644 && (row->reversed_p ? glyph <= end : glyph >= end)
13645 && STRINGP (end->object)
13646 && row->continued_p)
13647 return 0;
13651 compute_x:
13652 if (cursor != NULL)
13653 glyph = cursor;
13654 if (x < 0)
13656 struct glyph *g;
13658 /* Need to compute x that corresponds to GLYPH. */
13659 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
13661 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
13662 abort ();
13663 x += g->pixel_width;
13667 /* ROW could be part of a continued line, which, under bidi
13668 reordering, might have other rows whose start and end charpos
13669 occlude point. Only set w->cursor if we found a better
13670 approximation to the cursor position than we have from previously
13671 examined candidate rows belonging to the same continued line. */
13672 if (/* we already have a candidate row */
13673 w->cursor.vpos >= 0
13674 /* that candidate is not the row we are processing */
13675 && MATRIX_ROW (matrix, w->cursor.vpos) != row
13676 /* the row we are processing is part of a continued line */
13677 && (row->continued_p || MATRIX_ROW_CONTINUATION_LINE_P (row))
13678 /* Make sure cursor.vpos specifies a row whose start and end
13679 charpos occlude point. This is because some callers of this
13680 function leave cursor.vpos at the row where the cursor was
13681 displayed during the last redisplay cycle. */
13682 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
13683 && pt_old < MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)))
13685 struct glyph *g1 =
13686 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
13688 /* Don't consider glyphs that are outside TEXT_AREA. */
13689 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
13690 return 0;
13691 /* Keep the candidate whose buffer position is the closest to
13692 point. */
13693 if (/* previous candidate is a glyph in TEXT_AREA of that row */
13694 w->cursor.hpos >= 0
13695 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
13696 && BUFFERP (g1->object)
13697 && (g1->charpos == pt_old /* an exact match always wins */
13698 || (BUFFERP (glyph->object)
13699 && eabs (g1->charpos - pt_old)
13700 < eabs (glyph->charpos - pt_old))))
13701 return 0;
13702 /* If this candidate gives an exact match, use that. */
13703 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
13704 /* Otherwise, keep the candidate that comes from a row
13705 spanning less buffer positions. This may win when one or
13706 both candidate positions are on glyphs that came from
13707 display strings, for which we cannot compare buffer
13708 positions. */
13709 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13710 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13711 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
13712 return 0;
13714 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
13715 w->cursor.x = x;
13716 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
13717 w->cursor.y = row->y + dy;
13719 if (w == XWINDOW (selected_window))
13721 if (!row->continued_p
13722 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
13723 && row->x == 0)
13725 this_line_buffer = XBUFFER (w->buffer);
13727 CHARPOS (this_line_start_pos)
13728 = MATRIX_ROW_START_CHARPOS (row) + delta;
13729 BYTEPOS (this_line_start_pos)
13730 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
13732 CHARPOS (this_line_end_pos)
13733 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
13734 BYTEPOS (this_line_end_pos)
13735 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
13737 this_line_y = w->cursor.y;
13738 this_line_pixel_height = row->height;
13739 this_line_vpos = w->cursor.vpos;
13740 this_line_start_x = row->x;
13742 else
13743 CHARPOS (this_line_start_pos) = 0;
13746 return 1;
13750 /* Run window scroll functions, if any, for WINDOW with new window
13751 start STARTP. Sets the window start of WINDOW to that position.
13753 We assume that the window's buffer is really current. */
13755 static inline struct text_pos
13756 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
13758 struct window *w = XWINDOW (window);
13759 SET_MARKER_FROM_TEXT_POS (w->start, startp);
13761 if (current_buffer != XBUFFER (w->buffer))
13762 abort ();
13764 if (!NILP (Vwindow_scroll_functions))
13766 run_hook_with_args_2 (Qwindow_scroll_functions, window,
13767 make_number (CHARPOS (startp)));
13768 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13769 /* In case the hook functions switch buffers. */
13770 if (current_buffer != XBUFFER (w->buffer))
13771 set_buffer_internal_1 (XBUFFER (w->buffer));
13774 return startp;
13778 /* Make sure the line containing the cursor is fully visible.
13779 A value of 1 means there is nothing to be done.
13780 (Either the line is fully visible, or it cannot be made so,
13781 or we cannot tell.)
13783 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13784 is higher than window.
13786 A value of 0 means the caller should do scrolling
13787 as if point had gone off the screen. */
13789 static int
13790 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
13792 struct glyph_matrix *matrix;
13793 struct glyph_row *row;
13794 int window_height;
13796 if (!make_cursor_line_fully_visible_p)
13797 return 1;
13799 /* It's not always possible to find the cursor, e.g, when a window
13800 is full of overlay strings. Don't do anything in that case. */
13801 if (w->cursor.vpos < 0)
13802 return 1;
13804 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
13805 row = MATRIX_ROW (matrix, w->cursor.vpos);
13807 /* If the cursor row is not partially visible, there's nothing to do. */
13808 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
13809 return 1;
13811 /* If the row the cursor is in is taller than the window's height,
13812 it's not clear what to do, so do nothing. */
13813 window_height = window_box_height (w);
13814 if (row->height >= window_height)
13816 if (!force_p || MINI_WINDOW_P (w)
13817 || w->vscroll || w->cursor.vpos == 0)
13818 return 1;
13820 return 0;
13824 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13825 non-zero means only WINDOW is redisplayed in redisplay_internal.
13826 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
13827 in redisplay_window to bring a partially visible line into view in
13828 the case that only the cursor has moved.
13830 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13831 last screen line's vertical height extends past the end of the screen.
13833 Value is
13835 1 if scrolling succeeded
13837 0 if scrolling didn't find point.
13839 -1 if new fonts have been loaded so that we must interrupt
13840 redisplay, adjust glyph matrices, and try again. */
13842 enum
13844 SCROLLING_SUCCESS,
13845 SCROLLING_FAILED,
13846 SCROLLING_NEED_LARGER_MATRICES
13849 /* If scroll-conservatively is more than this, never recenter.
13851 If you change this, don't forget to update the doc string of
13852 `scroll-conservatively' and the Emacs manual. */
13853 #define SCROLL_LIMIT 100
13855 static int
13856 try_scrolling (Lisp_Object window, int just_this_one_p,
13857 EMACS_INT arg_scroll_conservatively, EMACS_INT scroll_step,
13858 int temp_scroll_step, int last_line_misfit)
13860 struct window *w = XWINDOW (window);
13861 struct frame *f = XFRAME (w->frame);
13862 struct text_pos pos, startp;
13863 struct it it;
13864 int this_scroll_margin, scroll_max, rc, height;
13865 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13866 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13867 Lisp_Object aggressive;
13868 /* We will never try scrolling more than this number of lines. */
13869 int scroll_limit = SCROLL_LIMIT;
13871 #if GLYPH_DEBUG
13872 debug_method_add (w, "try_scrolling");
13873 #endif
13875 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13877 /* Compute scroll margin height in pixels. We scroll when point is
13878 within this distance from the top or bottom of the window. */
13879 if (scroll_margin > 0)
13880 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13881 * FRAME_LINE_HEIGHT (f);
13882 else
13883 this_scroll_margin = 0;
13885 /* Force arg_scroll_conservatively to have a reasonable value, to
13886 avoid scrolling too far away with slow move_it_* functions. Note
13887 that the user can supply scroll-conservatively equal to
13888 `most-positive-fixnum', which can be larger than INT_MAX. */
13889 if (arg_scroll_conservatively > scroll_limit)
13891 arg_scroll_conservatively = scroll_limit + 1;
13892 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
13894 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
13895 /* Compute how much we should try to scroll maximally to bring
13896 point into view. */
13897 scroll_max = (max (scroll_step,
13898 max (arg_scroll_conservatively, temp_scroll_step))
13899 * FRAME_LINE_HEIGHT (f));
13900 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
13901 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
13902 /* We're trying to scroll because of aggressive scrolling but no
13903 scroll_step is set. Choose an arbitrary one. */
13904 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
13905 else
13906 scroll_max = 0;
13908 too_near_end:
13910 /* Decide whether to scroll down. */
13911 if (PT > CHARPOS (startp))
13913 int scroll_margin_y;
13915 /* Compute the pixel ypos of the scroll margin, then move it to
13916 either that ypos or PT, whichever comes first. */
13917 start_display (&it, w, startp);
13918 scroll_margin_y = it.last_visible_y - this_scroll_margin
13919 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
13920 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
13921 (MOVE_TO_POS | MOVE_TO_Y));
13923 if (PT > CHARPOS (it.current.pos))
13925 int y0 = line_bottom_y (&it);
13926 /* Compute how many pixels below window bottom to stop searching
13927 for PT. This avoids costly search for PT that is far away if
13928 the user limited scrolling by a small number of lines, but
13929 always finds PT if scroll_conservatively is set to a large
13930 number, such as most-positive-fixnum. */
13931 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
13932 int y_to_move = it.last_visible_y + slack;
13934 /* Compute the distance from the scroll margin to PT or to
13935 the scroll limit, whichever comes first. This should
13936 include the height of the cursor line, to make that line
13937 fully visible. */
13938 move_it_to (&it, PT, -1, y_to_move,
13939 -1, MOVE_TO_POS | MOVE_TO_Y);
13940 dy = line_bottom_y (&it) - y0;
13942 if (dy > scroll_max)
13943 return SCROLLING_FAILED;
13945 scroll_down_p = 1;
13949 if (scroll_down_p)
13951 /* Point is in or below the bottom scroll margin, so move the
13952 window start down. If scrolling conservatively, move it just
13953 enough down to make point visible. If scroll_step is set,
13954 move it down by scroll_step. */
13955 if (arg_scroll_conservatively)
13956 amount_to_scroll
13957 = min (max (dy, FRAME_LINE_HEIGHT (f)),
13958 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
13959 else if (scroll_step || temp_scroll_step)
13960 amount_to_scroll = scroll_max;
13961 else
13963 aggressive = BVAR (current_buffer, scroll_up_aggressively);
13964 height = WINDOW_BOX_TEXT_HEIGHT (w);
13965 if (NUMBERP (aggressive))
13967 double float_amount = XFLOATINT (aggressive) * height;
13968 amount_to_scroll = float_amount;
13969 if (amount_to_scroll == 0 && float_amount > 0)
13970 amount_to_scroll = 1;
13971 /* Don't let point enter the scroll margin near top of
13972 the window. */
13973 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
13974 amount_to_scroll = height - 2*this_scroll_margin + dy;
13978 if (amount_to_scroll <= 0)
13979 return SCROLLING_FAILED;
13981 start_display (&it, w, startp);
13982 if (arg_scroll_conservatively <= scroll_limit)
13983 move_it_vertically (&it, amount_to_scroll);
13984 else
13986 /* Extra precision for users who set scroll-conservatively
13987 to a large number: make sure the amount we scroll
13988 the window start is never less than amount_to_scroll,
13989 which was computed as distance from window bottom to
13990 point. This matters when lines at window top and lines
13991 below window bottom have different height. */
13992 struct it it1;
13993 void *it1data = NULL;
13994 /* We use a temporary it1 because line_bottom_y can modify
13995 its argument, if it moves one line down; see there. */
13996 int start_y;
13998 SAVE_IT (it1, it, it1data);
13999 start_y = line_bottom_y (&it1);
14000 do {
14001 RESTORE_IT (&it, &it, it1data);
14002 move_it_by_lines (&it, 1);
14003 SAVE_IT (it1, it, it1data);
14004 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14007 /* If STARTP is unchanged, move it down another screen line. */
14008 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14009 move_it_by_lines (&it, 1);
14010 startp = it.current.pos;
14012 else
14014 struct text_pos scroll_margin_pos = startp;
14016 /* See if point is inside the scroll margin at the top of the
14017 window. */
14018 if (this_scroll_margin)
14020 start_display (&it, w, startp);
14021 move_it_vertically (&it, this_scroll_margin);
14022 scroll_margin_pos = it.current.pos;
14025 if (PT < CHARPOS (scroll_margin_pos))
14027 /* Point is in the scroll margin at the top of the window or
14028 above what is displayed in the window. */
14029 int y0, y_to_move;
14031 /* Compute the vertical distance from PT to the scroll
14032 margin position. Move as far as scroll_max allows, or
14033 one screenful, or 10 screen lines, whichever is largest.
14034 Give up if distance is greater than scroll_max. */
14035 SET_TEXT_POS (pos, PT, PT_BYTE);
14036 start_display (&it, w, pos);
14037 y0 = it.current_y;
14038 y_to_move = max (it.last_visible_y,
14039 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14040 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14041 y_to_move, -1,
14042 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14043 dy = it.current_y - y0;
14044 if (dy > scroll_max)
14045 return SCROLLING_FAILED;
14047 /* Compute new window start. */
14048 start_display (&it, w, startp);
14050 if (arg_scroll_conservatively)
14051 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14052 max (scroll_step, temp_scroll_step));
14053 else if (scroll_step || temp_scroll_step)
14054 amount_to_scroll = scroll_max;
14055 else
14057 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14058 height = WINDOW_BOX_TEXT_HEIGHT (w);
14059 if (NUMBERP (aggressive))
14061 double float_amount = XFLOATINT (aggressive) * height;
14062 amount_to_scroll = float_amount;
14063 if (amount_to_scroll == 0 && float_amount > 0)
14064 amount_to_scroll = 1;
14065 amount_to_scroll -=
14066 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14067 /* Don't let point enter the scroll margin near
14068 bottom of the window. */
14069 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14070 amount_to_scroll = height - 2*this_scroll_margin + dy;
14074 if (amount_to_scroll <= 0)
14075 return SCROLLING_FAILED;
14077 move_it_vertically_backward (&it, amount_to_scroll);
14078 startp = it.current.pos;
14082 /* Run window scroll functions. */
14083 startp = run_window_scroll_functions (window, startp);
14085 /* Display the window. Give up if new fonts are loaded, or if point
14086 doesn't appear. */
14087 if (!try_window (window, startp, 0))
14088 rc = SCROLLING_NEED_LARGER_MATRICES;
14089 else if (w->cursor.vpos < 0)
14091 clear_glyph_matrix (w->desired_matrix);
14092 rc = SCROLLING_FAILED;
14094 else
14096 /* Maybe forget recorded base line for line number display. */
14097 if (!just_this_one_p
14098 || current_buffer->clip_changed
14099 || BEG_UNCHANGED < CHARPOS (startp))
14100 w->base_line_number = Qnil;
14102 /* If cursor ends up on a partially visible line,
14103 treat that as being off the bottom of the screen. */
14104 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14105 /* It's possible that the cursor is on the first line of the
14106 buffer, which is partially obscured due to a vscroll
14107 (Bug#7537). In that case, avoid looping forever . */
14108 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14110 clear_glyph_matrix (w->desired_matrix);
14111 ++extra_scroll_margin_lines;
14112 goto too_near_end;
14114 rc = SCROLLING_SUCCESS;
14117 return rc;
14121 /* Compute a suitable window start for window W if display of W starts
14122 on a continuation line. Value is non-zero if a new window start
14123 was computed.
14125 The new window start will be computed, based on W's width, starting
14126 from the start of the continued line. It is the start of the
14127 screen line with the minimum distance from the old start W->start. */
14129 static int
14130 compute_window_start_on_continuation_line (struct window *w)
14132 struct text_pos pos, start_pos;
14133 int window_start_changed_p = 0;
14135 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14137 /* If window start is on a continuation line... Window start may be
14138 < BEGV in case there's invisible text at the start of the
14139 buffer (M-x rmail, for example). */
14140 if (CHARPOS (start_pos) > BEGV
14141 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14143 struct it it;
14144 struct glyph_row *row;
14146 /* Handle the case that the window start is out of range. */
14147 if (CHARPOS (start_pos) < BEGV)
14148 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14149 else if (CHARPOS (start_pos) > ZV)
14150 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14152 /* Find the start of the continued line. This should be fast
14153 because scan_buffer is fast (newline cache). */
14154 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14155 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14156 row, DEFAULT_FACE_ID);
14157 reseat_at_previous_visible_line_start (&it);
14159 /* If the line start is "too far" away from the window start,
14160 say it takes too much time to compute a new window start. */
14161 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14162 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14164 int min_distance, distance;
14166 /* Move forward by display lines to find the new window
14167 start. If window width was enlarged, the new start can
14168 be expected to be > the old start. If window width was
14169 decreased, the new window start will be < the old start.
14170 So, we're looking for the display line start with the
14171 minimum distance from the old window start. */
14172 pos = it.current.pos;
14173 min_distance = INFINITY;
14174 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14175 distance < min_distance)
14177 min_distance = distance;
14178 pos = it.current.pos;
14179 move_it_by_lines (&it, 1);
14182 /* Set the window start there. */
14183 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14184 window_start_changed_p = 1;
14188 return window_start_changed_p;
14192 /* Try cursor movement in case text has not changed in window WINDOW,
14193 with window start STARTP. Value is
14195 CURSOR_MOVEMENT_SUCCESS if successful
14197 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14199 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14200 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14201 we want to scroll as if scroll-step were set to 1. See the code.
14203 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14204 which case we have to abort this redisplay, and adjust matrices
14205 first. */
14207 enum
14209 CURSOR_MOVEMENT_SUCCESS,
14210 CURSOR_MOVEMENT_CANNOT_BE_USED,
14211 CURSOR_MOVEMENT_MUST_SCROLL,
14212 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14215 static int
14216 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14218 struct window *w = XWINDOW (window);
14219 struct frame *f = XFRAME (w->frame);
14220 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14222 #if GLYPH_DEBUG
14223 if (inhibit_try_cursor_movement)
14224 return rc;
14225 #endif
14227 /* Handle case where text has not changed, only point, and it has
14228 not moved off the frame. */
14229 if (/* Point may be in this window. */
14230 PT >= CHARPOS (startp)
14231 /* Selective display hasn't changed. */
14232 && !current_buffer->clip_changed
14233 /* Function force-mode-line-update is used to force a thorough
14234 redisplay. It sets either windows_or_buffers_changed or
14235 update_mode_lines. So don't take a shortcut here for these
14236 cases. */
14237 && !update_mode_lines
14238 && !windows_or_buffers_changed
14239 && !cursor_type_changed
14240 /* Can't use this case if highlighting a region. When a
14241 region exists, cursor movement has to do more than just
14242 set the cursor. */
14243 && !(!NILP (Vtransient_mark_mode)
14244 && !NILP (BVAR (current_buffer, mark_active)))
14245 && NILP (w->region_showing)
14246 && NILP (Vshow_trailing_whitespace)
14247 /* Right after splitting windows, last_point may be nil. */
14248 && INTEGERP (w->last_point)
14249 /* This code is not used for mini-buffer for the sake of the case
14250 of redisplaying to replace an echo area message; since in
14251 that case the mini-buffer contents per se are usually
14252 unchanged. This code is of no real use in the mini-buffer
14253 since the handling of this_line_start_pos, etc., in redisplay
14254 handles the same cases. */
14255 && !EQ (window, minibuf_window)
14256 /* When splitting windows or for new windows, it happens that
14257 redisplay is called with a nil window_end_vpos or one being
14258 larger than the window. This should really be fixed in
14259 window.c. I don't have this on my list, now, so we do
14260 approximately the same as the old redisplay code. --gerd. */
14261 && INTEGERP (w->window_end_vpos)
14262 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14263 && (FRAME_WINDOW_P (f)
14264 || !overlay_arrow_in_current_buffer_p ()))
14266 int this_scroll_margin, top_scroll_margin;
14267 struct glyph_row *row = NULL;
14269 #if GLYPH_DEBUG
14270 debug_method_add (w, "cursor movement");
14271 #endif
14273 /* Scroll if point within this distance from the top or bottom
14274 of the window. This is a pixel value. */
14275 if (scroll_margin > 0)
14277 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14278 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14280 else
14281 this_scroll_margin = 0;
14283 top_scroll_margin = this_scroll_margin;
14284 if (WINDOW_WANTS_HEADER_LINE_P (w))
14285 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14287 /* Start with the row the cursor was displayed during the last
14288 not paused redisplay. Give up if that row is not valid. */
14289 if (w->last_cursor.vpos < 0
14290 || w->last_cursor.vpos >= w->current_matrix->nrows)
14291 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14292 else
14294 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14295 if (row->mode_line_p)
14296 ++row;
14297 if (!row->enabled_p)
14298 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14301 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14303 int scroll_p = 0, must_scroll = 0;
14304 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14306 if (PT > XFASTINT (w->last_point))
14308 /* Point has moved forward. */
14309 while (MATRIX_ROW_END_CHARPOS (row) < PT
14310 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14312 xassert (row->enabled_p);
14313 ++row;
14316 /* If the end position of a row equals the start
14317 position of the next row, and PT is at that position,
14318 we would rather display cursor in the next line. */
14319 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14320 && MATRIX_ROW_END_CHARPOS (row) == PT
14321 && row < w->current_matrix->rows
14322 + w->current_matrix->nrows - 1
14323 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14324 && !cursor_row_p (row))
14325 ++row;
14327 /* If within the scroll margin, scroll. Note that
14328 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14329 the next line would be drawn, and that
14330 this_scroll_margin can be zero. */
14331 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14332 || PT > MATRIX_ROW_END_CHARPOS (row)
14333 /* Line is completely visible last line in window
14334 and PT is to be set in the next line. */
14335 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14336 && PT == MATRIX_ROW_END_CHARPOS (row)
14337 && !row->ends_at_zv_p
14338 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14339 scroll_p = 1;
14341 else if (PT < XFASTINT (w->last_point))
14343 /* Cursor has to be moved backward. Note that PT >=
14344 CHARPOS (startp) because of the outer if-statement. */
14345 while (!row->mode_line_p
14346 && (MATRIX_ROW_START_CHARPOS (row) > PT
14347 || (MATRIX_ROW_START_CHARPOS (row) == PT
14348 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14349 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14350 row > w->current_matrix->rows
14351 && (row-1)->ends_in_newline_from_string_p))))
14352 && (row->y > top_scroll_margin
14353 || CHARPOS (startp) == BEGV))
14355 xassert (row->enabled_p);
14356 --row;
14359 /* Consider the following case: Window starts at BEGV,
14360 there is invisible, intangible text at BEGV, so that
14361 display starts at some point START > BEGV. It can
14362 happen that we are called with PT somewhere between
14363 BEGV and START. Try to handle that case. */
14364 if (row < w->current_matrix->rows
14365 || row->mode_line_p)
14367 row = w->current_matrix->rows;
14368 if (row->mode_line_p)
14369 ++row;
14372 /* Due to newlines in overlay strings, we may have to
14373 skip forward over overlay strings. */
14374 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14375 && MATRIX_ROW_END_CHARPOS (row) == PT
14376 && !cursor_row_p (row))
14377 ++row;
14379 /* If within the scroll margin, scroll. */
14380 if (row->y < top_scroll_margin
14381 && CHARPOS (startp) != BEGV)
14382 scroll_p = 1;
14384 else
14386 /* Cursor did not move. So don't scroll even if cursor line
14387 is partially visible, as it was so before. */
14388 rc = CURSOR_MOVEMENT_SUCCESS;
14391 if (PT < MATRIX_ROW_START_CHARPOS (row)
14392 || PT > MATRIX_ROW_END_CHARPOS (row))
14394 /* if PT is not in the glyph row, give up. */
14395 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14396 must_scroll = 1;
14398 else if (rc != CURSOR_MOVEMENT_SUCCESS
14399 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14401 /* If rows are bidi-reordered and point moved, back up
14402 until we find a row that does not belong to a
14403 continuation line. This is because we must consider
14404 all rows of a continued line as candidates for the
14405 new cursor positioning, since row start and end
14406 positions change non-linearly with vertical position
14407 in such rows. */
14408 /* FIXME: Revisit this when glyph ``spilling'' in
14409 continuation lines' rows is implemented for
14410 bidi-reordered rows. */
14411 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
14413 xassert (row->enabled_p);
14414 --row;
14415 /* If we hit the beginning of the displayed portion
14416 without finding the first row of a continued
14417 line, give up. */
14418 if (row <= w->current_matrix->rows)
14420 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14421 break;
14426 if (must_scroll)
14428 else if (rc != CURSOR_MOVEMENT_SUCCESS
14429 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
14430 && make_cursor_line_fully_visible_p)
14432 if (PT == MATRIX_ROW_END_CHARPOS (row)
14433 && !row->ends_at_zv_p
14434 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14435 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14436 else if (row->height > window_box_height (w))
14438 /* If we end up in a partially visible line, let's
14439 make it fully visible, except when it's taller
14440 than the window, in which case we can't do much
14441 about it. */
14442 *scroll_step = 1;
14443 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14445 else
14447 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14448 if (!cursor_row_fully_visible_p (w, 0, 1))
14449 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14450 else
14451 rc = CURSOR_MOVEMENT_SUCCESS;
14454 else if (scroll_p)
14455 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14456 else if (rc != CURSOR_MOVEMENT_SUCCESS
14457 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14459 /* With bidi-reordered rows, there could be more than
14460 one candidate row whose start and end positions
14461 occlude point. We need to let set_cursor_from_row
14462 find the best candidate. */
14463 /* FIXME: Revisit this when glyph ``spilling'' in
14464 continuation lines' rows is implemented for
14465 bidi-reordered rows. */
14466 int rv = 0;
14470 if (MATRIX_ROW_START_CHARPOS (row) <= PT
14471 && PT <= MATRIX_ROW_END_CHARPOS (row)
14472 && cursor_row_p (row))
14473 rv |= set_cursor_from_row (w, row, w->current_matrix,
14474 0, 0, 0, 0);
14475 /* As soon as we've found the first suitable row
14476 whose ends_at_zv_p flag is set, we are done. */
14477 if (rv
14478 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
14480 rc = CURSOR_MOVEMENT_SUCCESS;
14481 break;
14483 ++row;
14485 while ((MATRIX_ROW_CONTINUATION_LINE_P (row)
14486 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
14487 || (MATRIX_ROW_START_CHARPOS (row) == PT
14488 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
14489 /* If we didn't find any candidate rows, or exited the
14490 loop before all the candidates were examined, signal
14491 to the caller that this method failed. */
14492 if (rc != CURSOR_MOVEMENT_SUCCESS
14493 && (!rv || MATRIX_ROW_CONTINUATION_LINE_P (row)))
14494 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14495 else if (rv)
14496 rc = CURSOR_MOVEMENT_SUCCESS;
14498 else
14502 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
14504 rc = CURSOR_MOVEMENT_SUCCESS;
14505 break;
14507 ++row;
14509 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14510 && MATRIX_ROW_START_CHARPOS (row) == PT
14511 && cursor_row_p (row));
14516 return rc;
14519 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
14520 static
14521 #endif
14522 void
14523 set_vertical_scroll_bar (struct window *w)
14525 EMACS_INT start, end, whole;
14527 /* Calculate the start and end positions for the current window.
14528 At some point, it would be nice to choose between scrollbars
14529 which reflect the whole buffer size, with special markers
14530 indicating narrowing, and scrollbars which reflect only the
14531 visible region.
14533 Note that mini-buffers sometimes aren't displaying any text. */
14534 if (!MINI_WINDOW_P (w)
14535 || (w == XWINDOW (minibuf_window)
14536 && NILP (echo_area_buffer[0])))
14538 struct buffer *buf = XBUFFER (w->buffer);
14539 whole = BUF_ZV (buf) - BUF_BEGV (buf);
14540 start = marker_position (w->start) - BUF_BEGV (buf);
14541 /* I don't think this is guaranteed to be right. For the
14542 moment, we'll pretend it is. */
14543 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
14545 if (end < start)
14546 end = start;
14547 if (whole < (end - start))
14548 whole = end - start;
14550 else
14551 start = end = whole = 0;
14553 /* Indicate what this scroll bar ought to be displaying now. */
14554 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
14555 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
14556 (w, end - start, whole, start);
14560 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
14561 selected_window is redisplayed.
14563 We can return without actually redisplaying the window if
14564 fonts_changed_p is nonzero. In that case, redisplay_internal will
14565 retry. */
14567 static void
14568 redisplay_window (Lisp_Object window, int just_this_one_p)
14570 struct window *w = XWINDOW (window);
14571 struct frame *f = XFRAME (w->frame);
14572 struct buffer *buffer = XBUFFER (w->buffer);
14573 struct buffer *old = current_buffer;
14574 struct text_pos lpoint, opoint, startp;
14575 int update_mode_line;
14576 int tem;
14577 struct it it;
14578 /* Record it now because it's overwritten. */
14579 int current_matrix_up_to_date_p = 0;
14580 int used_current_matrix_p = 0;
14581 /* This is less strict than current_matrix_up_to_date_p.
14582 It indictes that the buffer contents and narrowing are unchanged. */
14583 int buffer_unchanged_p = 0;
14584 int temp_scroll_step = 0;
14585 int count = SPECPDL_INDEX ();
14586 int rc;
14587 int centering_position = -1;
14588 int last_line_misfit = 0;
14589 EMACS_INT beg_unchanged, end_unchanged;
14591 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14592 opoint = lpoint;
14594 /* W must be a leaf window here. */
14595 xassert (!NILP (w->buffer));
14596 #if GLYPH_DEBUG
14597 *w->desired_matrix->method = 0;
14598 #endif
14600 restart:
14601 reconsider_clip_changes (w, buffer);
14603 /* Has the mode line to be updated? */
14604 update_mode_line = (!NILP (w->update_mode_line)
14605 || update_mode_lines
14606 || buffer->clip_changed
14607 || buffer->prevent_redisplay_optimizations_p);
14609 if (MINI_WINDOW_P (w))
14611 if (w == XWINDOW (echo_area_window)
14612 && !NILP (echo_area_buffer[0]))
14614 if (update_mode_line)
14615 /* We may have to update a tty frame's menu bar or a
14616 tool-bar. Example `M-x C-h C-h C-g'. */
14617 goto finish_menu_bars;
14618 else
14619 /* We've already displayed the echo area glyphs in this window. */
14620 goto finish_scroll_bars;
14622 else if ((w != XWINDOW (minibuf_window)
14623 || minibuf_level == 0)
14624 /* When buffer is nonempty, redisplay window normally. */
14625 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
14626 /* Quail displays non-mini buffers in minibuffer window.
14627 In that case, redisplay the window normally. */
14628 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
14630 /* W is a mini-buffer window, but it's not active, so clear
14631 it. */
14632 int yb = window_text_bottom_y (w);
14633 struct glyph_row *row;
14634 int y;
14636 for (y = 0, row = w->desired_matrix->rows;
14637 y < yb;
14638 y += row->height, ++row)
14639 blank_row (w, row, y);
14640 goto finish_scroll_bars;
14643 clear_glyph_matrix (w->desired_matrix);
14646 /* Otherwise set up data on this window; select its buffer and point
14647 value. */
14648 /* Really select the buffer, for the sake of buffer-local
14649 variables. */
14650 set_buffer_internal_1 (XBUFFER (w->buffer));
14652 current_matrix_up_to_date_p
14653 = (!NILP (w->window_end_valid)
14654 && !current_buffer->clip_changed
14655 && !current_buffer->prevent_redisplay_optimizations_p
14656 && XFASTINT (w->last_modified) >= MODIFF
14657 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
14659 /* Run the window-bottom-change-functions
14660 if it is possible that the text on the screen has changed
14661 (either due to modification of the text, or any other reason). */
14662 if (!current_matrix_up_to_date_p
14663 && !NILP (Vwindow_text_change_functions))
14665 safe_run_hooks (Qwindow_text_change_functions);
14666 goto restart;
14669 beg_unchanged = BEG_UNCHANGED;
14670 end_unchanged = END_UNCHANGED;
14672 SET_TEXT_POS (opoint, PT, PT_BYTE);
14674 specbind (Qinhibit_point_motion_hooks, Qt);
14676 buffer_unchanged_p
14677 = (!NILP (w->window_end_valid)
14678 && !current_buffer->clip_changed
14679 && XFASTINT (w->last_modified) >= MODIFF
14680 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
14682 /* When windows_or_buffers_changed is non-zero, we can't rely on
14683 the window end being valid, so set it to nil there. */
14684 if (windows_or_buffers_changed)
14686 /* If window starts on a continuation line, maybe adjust the
14687 window start in case the window's width changed. */
14688 if (XMARKER (w->start)->buffer == current_buffer)
14689 compute_window_start_on_continuation_line (w);
14691 w->window_end_valid = Qnil;
14694 /* Some sanity checks. */
14695 CHECK_WINDOW_END (w);
14696 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
14697 abort ();
14698 if (BYTEPOS (opoint) < CHARPOS (opoint))
14699 abort ();
14701 /* If %c is in mode line, update it if needed. */
14702 if (!NILP (w->column_number_displayed)
14703 /* This alternative quickly identifies a common case
14704 where no change is needed. */
14705 && !(PT == XFASTINT (w->last_point)
14706 && XFASTINT (w->last_modified) >= MODIFF
14707 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
14708 && (XFASTINT (w->column_number_displayed) != current_column ()))
14709 update_mode_line = 1;
14711 /* Count number of windows showing the selected buffer. An indirect
14712 buffer counts as its base buffer. */
14713 if (!just_this_one_p)
14715 struct buffer *current_base, *window_base;
14716 current_base = current_buffer;
14717 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
14718 if (current_base->base_buffer)
14719 current_base = current_base->base_buffer;
14720 if (window_base->base_buffer)
14721 window_base = window_base->base_buffer;
14722 if (current_base == window_base)
14723 buffer_shared++;
14726 /* Point refers normally to the selected window. For any other
14727 window, set up appropriate value. */
14728 if (!EQ (window, selected_window))
14730 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
14731 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
14732 if (new_pt < BEGV)
14734 new_pt = BEGV;
14735 new_pt_byte = BEGV_BYTE;
14736 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
14738 else if (new_pt > (ZV - 1))
14740 new_pt = ZV;
14741 new_pt_byte = ZV_BYTE;
14742 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
14745 /* We don't use SET_PT so that the point-motion hooks don't run. */
14746 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
14749 /* If any of the character widths specified in the display table
14750 have changed, invalidate the width run cache. It's true that
14751 this may be a bit late to catch such changes, but the rest of
14752 redisplay goes (non-fatally) haywire when the display table is
14753 changed, so why should we worry about doing any better? */
14754 if (current_buffer->width_run_cache)
14756 struct Lisp_Char_Table *disptab = buffer_display_table ();
14758 if (! disptab_matches_widthtab (disptab,
14759 XVECTOR (BVAR (current_buffer, width_table))))
14761 invalidate_region_cache (current_buffer,
14762 current_buffer->width_run_cache,
14763 BEG, Z);
14764 recompute_width_table (current_buffer, disptab);
14768 /* If window-start is screwed up, choose a new one. */
14769 if (XMARKER (w->start)->buffer != current_buffer)
14770 goto recenter;
14772 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14774 /* If someone specified a new starting point but did not insist,
14775 check whether it can be used. */
14776 if (!NILP (w->optional_new_start)
14777 && CHARPOS (startp) >= BEGV
14778 && CHARPOS (startp) <= ZV)
14780 w->optional_new_start = Qnil;
14781 start_display (&it, w, startp);
14782 move_it_to (&it, PT, 0, it.last_visible_y, -1,
14783 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14784 if (IT_CHARPOS (it) == PT)
14785 w->force_start = Qt;
14786 /* IT may overshoot PT if text at PT is invisible. */
14787 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
14788 w->force_start = Qt;
14791 force_start:
14793 /* Handle case where place to start displaying has been specified,
14794 unless the specified location is outside the accessible range. */
14795 if (!NILP (w->force_start)
14796 || w->frozen_window_start_p)
14798 /* We set this later on if we have to adjust point. */
14799 int new_vpos = -1;
14801 w->force_start = Qnil;
14802 w->vscroll = 0;
14803 w->window_end_valid = Qnil;
14805 /* Forget any recorded base line for line number display. */
14806 if (!buffer_unchanged_p)
14807 w->base_line_number = Qnil;
14809 /* Redisplay the mode line. Select the buffer properly for that.
14810 Also, run the hook window-scroll-functions
14811 because we have scrolled. */
14812 /* Note, we do this after clearing force_start because
14813 if there's an error, it is better to forget about force_start
14814 than to get into an infinite loop calling the hook functions
14815 and having them get more errors. */
14816 if (!update_mode_line
14817 || ! NILP (Vwindow_scroll_functions))
14819 update_mode_line = 1;
14820 w->update_mode_line = Qt;
14821 startp = run_window_scroll_functions (window, startp);
14824 w->last_modified = make_number (0);
14825 w->last_overlay_modified = make_number (0);
14826 if (CHARPOS (startp) < BEGV)
14827 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
14828 else if (CHARPOS (startp) > ZV)
14829 SET_TEXT_POS (startp, ZV, ZV_BYTE);
14831 /* Redisplay, then check if cursor has been set during the
14832 redisplay. Give up if new fonts were loaded. */
14833 /* We used to issue a CHECK_MARGINS argument to try_window here,
14834 but this causes scrolling to fail when point begins inside
14835 the scroll margin (bug#148) -- cyd */
14836 if (!try_window (window, startp, 0))
14838 w->force_start = Qt;
14839 clear_glyph_matrix (w->desired_matrix);
14840 goto need_larger_matrices;
14843 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
14845 /* If point does not appear, try to move point so it does
14846 appear. The desired matrix has been built above, so we
14847 can use it here. */
14848 new_vpos = window_box_height (w) / 2;
14851 if (!cursor_row_fully_visible_p (w, 0, 0))
14853 /* Point does appear, but on a line partly visible at end of window.
14854 Move it back to a fully-visible line. */
14855 new_vpos = window_box_height (w);
14858 /* If we need to move point for either of the above reasons,
14859 now actually do it. */
14860 if (new_vpos >= 0)
14862 struct glyph_row *row;
14864 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
14865 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
14866 ++row;
14868 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
14869 MATRIX_ROW_START_BYTEPOS (row));
14871 if (w != XWINDOW (selected_window))
14872 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
14873 else if (current_buffer == old)
14874 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14876 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
14878 /* If we are highlighting the region, then we just changed
14879 the region, so redisplay to show it. */
14880 if (!NILP (Vtransient_mark_mode)
14881 && !NILP (BVAR (current_buffer, mark_active)))
14883 clear_glyph_matrix (w->desired_matrix);
14884 if (!try_window (window, startp, 0))
14885 goto need_larger_matrices;
14889 #if GLYPH_DEBUG
14890 debug_method_add (w, "forced window start");
14891 #endif
14892 goto done;
14895 /* Handle case where text has not changed, only point, and it has
14896 not moved off the frame, and we are not retrying after hscroll.
14897 (current_matrix_up_to_date_p is nonzero when retrying.) */
14898 if (current_matrix_up_to_date_p
14899 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14900 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14902 switch (rc)
14904 case CURSOR_MOVEMENT_SUCCESS:
14905 used_current_matrix_p = 1;
14906 goto done;
14908 case CURSOR_MOVEMENT_MUST_SCROLL:
14909 goto try_to_scroll;
14911 default:
14912 abort ();
14915 /* If current starting point was originally the beginning of a line
14916 but no longer is, find a new starting point. */
14917 else if (!NILP (w->start_at_line_beg)
14918 && !(CHARPOS (startp) <= BEGV
14919 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
14921 #if GLYPH_DEBUG
14922 debug_method_add (w, "recenter 1");
14923 #endif
14924 goto recenter;
14927 /* Try scrolling with try_window_id. Value is > 0 if update has
14928 been done, it is -1 if we know that the same window start will
14929 not work. It is 0 if unsuccessful for some other reason. */
14930 else if ((tem = try_window_id (w)) != 0)
14932 #if GLYPH_DEBUG
14933 debug_method_add (w, "try_window_id %d", tem);
14934 #endif
14936 if (fonts_changed_p)
14937 goto need_larger_matrices;
14938 if (tem > 0)
14939 goto done;
14941 /* Otherwise try_window_id has returned -1 which means that we
14942 don't want the alternative below this comment to execute. */
14944 else if (CHARPOS (startp) >= BEGV
14945 && CHARPOS (startp) <= ZV
14946 && PT >= CHARPOS (startp)
14947 && (CHARPOS (startp) < ZV
14948 /* Avoid starting at end of buffer. */
14949 || CHARPOS (startp) == BEGV
14950 || (XFASTINT (w->last_modified) >= MODIFF
14951 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
14954 /* If first window line is a continuation line, and window start
14955 is inside the modified region, but the first change is before
14956 current window start, we must select a new window start.
14958 However, if this is the result of a down-mouse event (e.g. by
14959 extending the mouse-drag-overlay), we don't want to select a
14960 new window start, since that would change the position under
14961 the mouse, resulting in an unwanted mouse-movement rather
14962 than a simple mouse-click. */
14963 if (NILP (w->start_at_line_beg)
14964 && NILP (do_mouse_tracking)
14965 && CHARPOS (startp) > BEGV
14966 && CHARPOS (startp) > BEG + beg_unchanged
14967 && CHARPOS (startp) <= Z - end_unchanged
14968 /* Even if w->start_at_line_beg is nil, a new window may
14969 start at a line_beg, since that's how set_buffer_window
14970 sets it. So, we need to check the return value of
14971 compute_window_start_on_continuation_line. (See also
14972 bug#197). */
14973 && XMARKER (w->start)->buffer == current_buffer
14974 && compute_window_start_on_continuation_line (w))
14976 w->force_start = Qt;
14977 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14978 goto force_start;
14981 #if GLYPH_DEBUG
14982 debug_method_add (w, "same window start");
14983 #endif
14985 /* Try to redisplay starting at same place as before.
14986 If point has not moved off frame, accept the results. */
14987 if (!current_matrix_up_to_date_p
14988 /* Don't use try_window_reusing_current_matrix in this case
14989 because a window scroll function can have changed the
14990 buffer. */
14991 || !NILP (Vwindow_scroll_functions)
14992 || MINI_WINDOW_P (w)
14993 || !(used_current_matrix_p
14994 = try_window_reusing_current_matrix (w)))
14996 IF_DEBUG (debug_method_add (w, "1"));
14997 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
14998 /* -1 means we need to scroll.
14999 0 means we need new matrices, but fonts_changed_p
15000 is set in that case, so we will detect it below. */
15001 goto try_to_scroll;
15004 if (fonts_changed_p)
15005 goto need_larger_matrices;
15007 if (w->cursor.vpos >= 0)
15009 if (!just_this_one_p
15010 || current_buffer->clip_changed
15011 || BEG_UNCHANGED < CHARPOS (startp))
15012 /* Forget any recorded base line for line number display. */
15013 w->base_line_number = Qnil;
15015 if (!cursor_row_fully_visible_p (w, 1, 0))
15017 clear_glyph_matrix (w->desired_matrix);
15018 last_line_misfit = 1;
15020 /* Drop through and scroll. */
15021 else
15022 goto done;
15024 else
15025 clear_glyph_matrix (w->desired_matrix);
15028 try_to_scroll:
15030 w->last_modified = make_number (0);
15031 w->last_overlay_modified = make_number (0);
15033 /* Redisplay the mode line. Select the buffer properly for that. */
15034 if (!update_mode_line)
15036 update_mode_line = 1;
15037 w->update_mode_line = Qt;
15040 /* Try to scroll by specified few lines. */
15041 if ((scroll_conservatively
15042 || emacs_scroll_step
15043 || temp_scroll_step
15044 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15045 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15046 && CHARPOS (startp) >= BEGV
15047 && CHARPOS (startp) <= ZV)
15049 /* The function returns -1 if new fonts were loaded, 1 if
15050 successful, 0 if not successful. */
15051 int ss = try_scrolling (window, just_this_one_p,
15052 scroll_conservatively,
15053 emacs_scroll_step,
15054 temp_scroll_step, last_line_misfit);
15055 switch (ss)
15057 case SCROLLING_SUCCESS:
15058 goto done;
15060 case SCROLLING_NEED_LARGER_MATRICES:
15061 goto need_larger_matrices;
15063 case SCROLLING_FAILED:
15064 break;
15066 default:
15067 abort ();
15071 /* Finally, just choose a place to start which positions point
15072 according to user preferences. */
15074 recenter:
15076 #if GLYPH_DEBUG
15077 debug_method_add (w, "recenter");
15078 #endif
15080 /* w->vscroll = 0; */
15082 /* Forget any previously recorded base line for line number display. */
15083 if (!buffer_unchanged_p)
15084 w->base_line_number = Qnil;
15086 /* Determine the window start relative to point. */
15087 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15088 it.current_y = it.last_visible_y;
15089 if (centering_position < 0)
15091 int margin =
15092 scroll_margin > 0
15093 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15094 : 0;
15095 EMACS_INT margin_pos = CHARPOS (startp);
15096 int scrolling_up;
15097 Lisp_Object aggressive;
15099 /* If there is a scroll margin at the top of the window, find
15100 its character position. */
15101 if (margin
15102 /* Cannot call start_display if startp is not in the
15103 accessible region of the buffer. This can happen when we
15104 have just switched to a different buffer and/or changed
15105 its restriction. In that case, startp is initialized to
15106 the character position 1 (BEG) because we did not yet
15107 have chance to display the buffer even once. */
15108 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15110 struct it it1;
15111 void *it1data = NULL;
15113 SAVE_IT (it1, it, it1data);
15114 start_display (&it1, w, startp);
15115 move_it_vertically (&it1, margin);
15116 margin_pos = IT_CHARPOS (it1);
15117 RESTORE_IT (&it, &it, it1data);
15119 scrolling_up = PT > margin_pos;
15120 aggressive =
15121 scrolling_up
15122 ? BVAR (current_buffer, scroll_up_aggressively)
15123 : BVAR (current_buffer, scroll_down_aggressively);
15125 if (!MINI_WINDOW_P (w)
15126 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15128 int pt_offset = 0;
15130 /* Setting scroll-conservatively overrides
15131 scroll-*-aggressively. */
15132 if (!scroll_conservatively && NUMBERP (aggressive))
15134 double float_amount = XFLOATINT (aggressive);
15136 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15137 if (pt_offset == 0 && float_amount > 0)
15138 pt_offset = 1;
15139 if (pt_offset)
15140 margin -= 1;
15142 /* Compute how much to move the window start backward from
15143 point so that point will be displayed where the user
15144 wants it. */
15145 if (scrolling_up)
15147 centering_position = it.last_visible_y;
15148 if (pt_offset)
15149 centering_position -= pt_offset;
15150 centering_position -=
15151 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0));
15152 /* Don't let point enter the scroll margin near top of
15153 the window. */
15154 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15155 centering_position = margin * FRAME_LINE_HEIGHT (f);
15157 else
15158 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15160 else
15161 /* Set the window start half the height of the window backward
15162 from point. */
15163 centering_position = window_box_height (w) / 2;
15165 move_it_vertically_backward (&it, centering_position);
15167 xassert (IT_CHARPOS (it) >= BEGV);
15169 /* The function move_it_vertically_backward may move over more
15170 than the specified y-distance. If it->w is small, e.g. a
15171 mini-buffer window, we may end up in front of the window's
15172 display area. Start displaying at the start of the line
15173 containing PT in this case. */
15174 if (it.current_y <= 0)
15176 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15177 move_it_vertically_backward (&it, 0);
15178 it.current_y = 0;
15181 it.current_x = it.hpos = 0;
15183 /* Set the window start position here explicitly, to avoid an
15184 infinite loop in case the functions in window-scroll-functions
15185 get errors. */
15186 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15188 /* Run scroll hooks. */
15189 startp = run_window_scroll_functions (window, it.current.pos);
15191 /* Redisplay the window. */
15192 if (!current_matrix_up_to_date_p
15193 || windows_or_buffers_changed
15194 || cursor_type_changed
15195 /* Don't use try_window_reusing_current_matrix in this case
15196 because it can have changed the buffer. */
15197 || !NILP (Vwindow_scroll_functions)
15198 || !just_this_one_p
15199 || MINI_WINDOW_P (w)
15200 || !(used_current_matrix_p
15201 = try_window_reusing_current_matrix (w)))
15202 try_window (window, startp, 0);
15204 /* If new fonts have been loaded (due to fontsets), give up. We
15205 have to start a new redisplay since we need to re-adjust glyph
15206 matrices. */
15207 if (fonts_changed_p)
15208 goto need_larger_matrices;
15210 /* If cursor did not appear assume that the middle of the window is
15211 in the first line of the window. Do it again with the next line.
15212 (Imagine a window of height 100, displaying two lines of height
15213 60. Moving back 50 from it->last_visible_y will end in the first
15214 line.) */
15215 if (w->cursor.vpos < 0)
15217 if (!NILP (w->window_end_valid)
15218 && PT >= Z - XFASTINT (w->window_end_pos))
15220 clear_glyph_matrix (w->desired_matrix);
15221 move_it_by_lines (&it, 1);
15222 try_window (window, it.current.pos, 0);
15224 else if (PT < IT_CHARPOS (it))
15226 clear_glyph_matrix (w->desired_matrix);
15227 move_it_by_lines (&it, -1);
15228 try_window (window, it.current.pos, 0);
15230 else
15232 /* Not much we can do about it. */
15236 /* Consider the following case: Window starts at BEGV, there is
15237 invisible, intangible text at BEGV, so that display starts at
15238 some point START > BEGV. It can happen that we are called with
15239 PT somewhere between BEGV and START. Try to handle that case. */
15240 if (w->cursor.vpos < 0)
15242 struct glyph_row *row = w->current_matrix->rows;
15243 if (row->mode_line_p)
15244 ++row;
15245 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15248 if (!cursor_row_fully_visible_p (w, 0, 0))
15250 /* If vscroll is enabled, disable it and try again. */
15251 if (w->vscroll)
15253 w->vscroll = 0;
15254 clear_glyph_matrix (w->desired_matrix);
15255 goto recenter;
15258 /* If centering point failed to make the whole line visible,
15259 put point at the top instead. That has to make the whole line
15260 visible, if it can be done. */
15261 if (centering_position == 0)
15262 goto done;
15264 clear_glyph_matrix (w->desired_matrix);
15265 centering_position = 0;
15266 goto recenter;
15269 done:
15271 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15272 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
15273 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
15274 ? Qt : Qnil);
15276 /* Display the mode line, if we must. */
15277 if ((update_mode_line
15278 /* If window not full width, must redo its mode line
15279 if (a) the window to its side is being redone and
15280 (b) we do a frame-based redisplay. This is a consequence
15281 of how inverted lines are drawn in frame-based redisplay. */
15282 || (!just_this_one_p
15283 && !FRAME_WINDOW_P (f)
15284 && !WINDOW_FULL_WIDTH_P (w))
15285 /* Line number to display. */
15286 || INTEGERP (w->base_line_pos)
15287 /* Column number is displayed and different from the one displayed. */
15288 || (!NILP (w->column_number_displayed)
15289 && (XFASTINT (w->column_number_displayed) != current_column ())))
15290 /* This means that the window has a mode line. */
15291 && (WINDOW_WANTS_MODELINE_P (w)
15292 || WINDOW_WANTS_HEADER_LINE_P (w)))
15294 display_mode_lines (w);
15296 /* If mode line height has changed, arrange for a thorough
15297 immediate redisplay using the correct mode line height. */
15298 if (WINDOW_WANTS_MODELINE_P (w)
15299 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
15301 fonts_changed_p = 1;
15302 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
15303 = DESIRED_MODE_LINE_HEIGHT (w);
15306 /* If header line height has changed, arrange for a thorough
15307 immediate redisplay using the correct header line height. */
15308 if (WINDOW_WANTS_HEADER_LINE_P (w)
15309 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
15311 fonts_changed_p = 1;
15312 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
15313 = DESIRED_HEADER_LINE_HEIGHT (w);
15316 if (fonts_changed_p)
15317 goto need_larger_matrices;
15320 if (!line_number_displayed
15321 && !BUFFERP (w->base_line_pos))
15323 w->base_line_pos = Qnil;
15324 w->base_line_number = Qnil;
15327 finish_menu_bars:
15329 /* When we reach a frame's selected window, redo the frame's menu bar. */
15330 if (update_mode_line
15331 && EQ (FRAME_SELECTED_WINDOW (f), window))
15333 int redisplay_menu_p = 0;
15335 if (FRAME_WINDOW_P (f))
15337 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
15338 || defined (HAVE_NS) || defined (USE_GTK)
15339 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
15340 #else
15341 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15342 #endif
15344 else
15345 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15347 if (redisplay_menu_p)
15348 display_menu_bar (w);
15350 #ifdef HAVE_WINDOW_SYSTEM
15351 if (FRAME_WINDOW_P (f))
15353 #if defined (USE_GTK) || defined (HAVE_NS)
15354 if (FRAME_EXTERNAL_TOOL_BAR (f))
15355 redisplay_tool_bar (f);
15356 #else
15357 if (WINDOWP (f->tool_bar_window)
15358 && (FRAME_TOOL_BAR_LINES (f) > 0
15359 || !NILP (Vauto_resize_tool_bars))
15360 && redisplay_tool_bar (f))
15361 ignore_mouse_drag_p = 1;
15362 #endif
15364 #endif
15367 #ifdef HAVE_WINDOW_SYSTEM
15368 if (FRAME_WINDOW_P (f)
15369 && update_window_fringes (w, (just_this_one_p
15370 || (!used_current_matrix_p && !overlay_arrow_seen)
15371 || w->pseudo_window_p)))
15373 update_begin (f);
15374 BLOCK_INPUT;
15375 if (draw_window_fringes (w, 1))
15376 x_draw_vertical_border (w);
15377 UNBLOCK_INPUT;
15378 update_end (f);
15380 #endif /* HAVE_WINDOW_SYSTEM */
15382 /* We go to this label, with fonts_changed_p nonzero,
15383 if it is necessary to try again using larger glyph matrices.
15384 We have to redeem the scroll bar even in this case,
15385 because the loop in redisplay_internal expects that. */
15386 need_larger_matrices:
15388 finish_scroll_bars:
15390 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
15392 /* Set the thumb's position and size. */
15393 set_vertical_scroll_bar (w);
15395 /* Note that we actually used the scroll bar attached to this
15396 window, so it shouldn't be deleted at the end of redisplay. */
15397 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
15398 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
15401 /* Restore current_buffer and value of point in it. The window
15402 update may have changed the buffer, so first make sure `opoint'
15403 is still valid (Bug#6177). */
15404 if (CHARPOS (opoint) < BEGV)
15405 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15406 else if (CHARPOS (opoint) > ZV)
15407 TEMP_SET_PT_BOTH (Z, Z_BYTE);
15408 else
15409 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
15411 set_buffer_internal_1 (old);
15412 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
15413 shorter. This can be caused by log truncation in *Messages*. */
15414 if (CHARPOS (lpoint) <= ZV)
15415 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15417 unbind_to (count, Qnil);
15421 /* Build the complete desired matrix of WINDOW with a window start
15422 buffer position POS.
15424 Value is 1 if successful. It is zero if fonts were loaded during
15425 redisplay which makes re-adjusting glyph matrices necessary, and -1
15426 if point would appear in the scroll margins.
15427 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
15428 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
15429 set in FLAGS.) */
15432 try_window (Lisp_Object window, struct text_pos pos, int flags)
15434 struct window *w = XWINDOW (window);
15435 struct it it;
15436 struct glyph_row *last_text_row = NULL;
15437 struct frame *f = XFRAME (w->frame);
15439 /* Make POS the new window start. */
15440 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
15442 /* Mark cursor position as unknown. No overlay arrow seen. */
15443 w->cursor.vpos = -1;
15444 overlay_arrow_seen = 0;
15446 /* Initialize iterator and info to start at POS. */
15447 start_display (&it, w, pos);
15449 /* Display all lines of W. */
15450 while (it.current_y < it.last_visible_y)
15452 if (display_line (&it))
15453 last_text_row = it.glyph_row - 1;
15454 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
15455 return 0;
15458 /* Don't let the cursor end in the scroll margins. */
15459 if ((flags & TRY_WINDOW_CHECK_MARGINS)
15460 && !MINI_WINDOW_P (w))
15462 int this_scroll_margin;
15464 if (scroll_margin > 0)
15466 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15467 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15469 else
15470 this_scroll_margin = 0;
15472 if ((w->cursor.y >= 0 /* not vscrolled */
15473 && w->cursor.y < this_scroll_margin
15474 && CHARPOS (pos) > BEGV
15475 && IT_CHARPOS (it) < ZV)
15476 /* rms: considering make_cursor_line_fully_visible_p here
15477 seems to give wrong results. We don't want to recenter
15478 when the last line is partly visible, we want to allow
15479 that case to be handled in the usual way. */
15480 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
15482 w->cursor.vpos = -1;
15483 clear_glyph_matrix (w->desired_matrix);
15484 return -1;
15488 /* If bottom moved off end of frame, change mode line percentage. */
15489 if (XFASTINT (w->window_end_pos) <= 0
15490 && Z != IT_CHARPOS (it))
15491 w->update_mode_line = Qt;
15493 /* Set window_end_pos to the offset of the last character displayed
15494 on the window from the end of current_buffer. Set
15495 window_end_vpos to its row number. */
15496 if (last_text_row)
15498 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
15499 w->window_end_bytepos
15500 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15501 w->window_end_pos
15502 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15503 w->window_end_vpos
15504 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15505 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
15506 ->displays_text_p);
15508 else
15510 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
15511 w->window_end_pos = make_number (Z - ZV);
15512 w->window_end_vpos = make_number (0);
15515 /* But that is not valid info until redisplay finishes. */
15516 w->window_end_valid = Qnil;
15517 return 1;
15522 /************************************************************************
15523 Window redisplay reusing current matrix when buffer has not changed
15524 ************************************************************************/
15526 /* Try redisplay of window W showing an unchanged buffer with a
15527 different window start than the last time it was displayed by
15528 reusing its current matrix. Value is non-zero if successful.
15529 W->start is the new window start. */
15531 static int
15532 try_window_reusing_current_matrix (struct window *w)
15534 struct frame *f = XFRAME (w->frame);
15535 struct glyph_row *bottom_row;
15536 struct it it;
15537 struct run run;
15538 struct text_pos start, new_start;
15539 int nrows_scrolled, i;
15540 struct glyph_row *last_text_row;
15541 struct glyph_row *last_reused_text_row;
15542 struct glyph_row *start_row;
15543 int start_vpos, min_y, max_y;
15545 #if GLYPH_DEBUG
15546 if (inhibit_try_window_reusing)
15547 return 0;
15548 #endif
15550 if (/* This function doesn't handle terminal frames. */
15551 !FRAME_WINDOW_P (f)
15552 /* Don't try to reuse the display if windows have been split
15553 or such. */
15554 || windows_or_buffers_changed
15555 || cursor_type_changed)
15556 return 0;
15558 /* Can't do this if region may have changed. */
15559 if ((!NILP (Vtransient_mark_mode)
15560 && !NILP (BVAR (current_buffer, mark_active)))
15561 || !NILP (w->region_showing)
15562 || !NILP (Vshow_trailing_whitespace))
15563 return 0;
15565 /* If top-line visibility has changed, give up. */
15566 if (WINDOW_WANTS_HEADER_LINE_P (w)
15567 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
15568 return 0;
15570 /* Give up if old or new display is scrolled vertically. We could
15571 make this function handle this, but right now it doesn't. */
15572 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15573 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
15574 return 0;
15576 /* The variable new_start now holds the new window start. The old
15577 start `start' can be determined from the current matrix. */
15578 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
15579 start = start_row->minpos;
15580 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
15582 /* Clear the desired matrix for the display below. */
15583 clear_glyph_matrix (w->desired_matrix);
15585 if (CHARPOS (new_start) <= CHARPOS (start))
15587 /* Don't use this method if the display starts with an ellipsis
15588 displayed for invisible text. It's not easy to handle that case
15589 below, and it's certainly not worth the effort since this is
15590 not a frequent case. */
15591 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
15592 return 0;
15594 IF_DEBUG (debug_method_add (w, "twu1"));
15596 /* Display up to a row that can be reused. The variable
15597 last_text_row is set to the last row displayed that displays
15598 text. Note that it.vpos == 0 if or if not there is a
15599 header-line; it's not the same as the MATRIX_ROW_VPOS! */
15600 start_display (&it, w, new_start);
15601 w->cursor.vpos = -1;
15602 last_text_row = last_reused_text_row = NULL;
15604 while (it.current_y < it.last_visible_y
15605 && !fonts_changed_p)
15607 /* If we have reached into the characters in the START row,
15608 that means the line boundaries have changed. So we
15609 can't start copying with the row START. Maybe it will
15610 work to start copying with the following row. */
15611 while (IT_CHARPOS (it) > CHARPOS (start))
15613 /* Advance to the next row as the "start". */
15614 start_row++;
15615 start = start_row->minpos;
15616 /* If there are no more rows to try, or just one, give up. */
15617 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
15618 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
15619 || CHARPOS (start) == ZV)
15621 clear_glyph_matrix (w->desired_matrix);
15622 return 0;
15625 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
15627 /* If we have reached alignment,
15628 we can copy the rest of the rows. */
15629 if (IT_CHARPOS (it) == CHARPOS (start))
15630 break;
15632 if (display_line (&it))
15633 last_text_row = it.glyph_row - 1;
15636 /* A value of current_y < last_visible_y means that we stopped
15637 at the previous window start, which in turn means that we
15638 have at least one reusable row. */
15639 if (it.current_y < it.last_visible_y)
15641 struct glyph_row *row;
15643 /* IT.vpos always starts from 0; it counts text lines. */
15644 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
15646 /* Find PT if not already found in the lines displayed. */
15647 if (w->cursor.vpos < 0)
15649 int dy = it.current_y - start_row->y;
15651 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15652 row = row_containing_pos (w, PT, row, NULL, dy);
15653 if (row)
15654 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
15655 dy, nrows_scrolled);
15656 else
15658 clear_glyph_matrix (w->desired_matrix);
15659 return 0;
15663 /* Scroll the display. Do it before the current matrix is
15664 changed. The problem here is that update has not yet
15665 run, i.e. part of the current matrix is not up to date.
15666 scroll_run_hook will clear the cursor, and use the
15667 current matrix to get the height of the row the cursor is
15668 in. */
15669 run.current_y = start_row->y;
15670 run.desired_y = it.current_y;
15671 run.height = it.last_visible_y - it.current_y;
15673 if (run.height > 0 && run.current_y != run.desired_y)
15675 update_begin (f);
15676 FRAME_RIF (f)->update_window_begin_hook (w);
15677 FRAME_RIF (f)->clear_window_mouse_face (w);
15678 FRAME_RIF (f)->scroll_run_hook (w, &run);
15679 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15680 update_end (f);
15683 /* Shift current matrix down by nrows_scrolled lines. */
15684 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15685 rotate_matrix (w->current_matrix,
15686 start_vpos,
15687 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15688 nrows_scrolled);
15690 /* Disable lines that must be updated. */
15691 for (i = 0; i < nrows_scrolled; ++i)
15692 (start_row + i)->enabled_p = 0;
15694 /* Re-compute Y positions. */
15695 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15696 max_y = it.last_visible_y;
15697 for (row = start_row + nrows_scrolled;
15698 row < bottom_row;
15699 ++row)
15701 row->y = it.current_y;
15702 row->visible_height = row->height;
15704 if (row->y < min_y)
15705 row->visible_height -= min_y - row->y;
15706 if (row->y + row->height > max_y)
15707 row->visible_height -= row->y + row->height - max_y;
15708 if (row->fringe_bitmap_periodic_p)
15709 row->redraw_fringe_bitmaps_p = 1;
15711 it.current_y += row->height;
15713 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15714 last_reused_text_row = row;
15715 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
15716 break;
15719 /* Disable lines in the current matrix which are now
15720 below the window. */
15721 for (++row; row < bottom_row; ++row)
15722 row->enabled_p = row->mode_line_p = 0;
15725 /* Update window_end_pos etc.; last_reused_text_row is the last
15726 reused row from the current matrix containing text, if any.
15727 The value of last_text_row is the last displayed line
15728 containing text. */
15729 if (last_reused_text_row)
15731 w->window_end_bytepos
15732 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
15733 w->window_end_pos
15734 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
15735 w->window_end_vpos
15736 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
15737 w->current_matrix));
15739 else if (last_text_row)
15741 w->window_end_bytepos
15742 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15743 w->window_end_pos
15744 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15745 w->window_end_vpos
15746 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15748 else
15750 /* This window must be completely empty. */
15751 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
15752 w->window_end_pos = make_number (Z - ZV);
15753 w->window_end_vpos = make_number (0);
15755 w->window_end_valid = Qnil;
15757 /* Update hint: don't try scrolling again in update_window. */
15758 w->desired_matrix->no_scrolling_p = 1;
15760 #if GLYPH_DEBUG
15761 debug_method_add (w, "try_window_reusing_current_matrix 1");
15762 #endif
15763 return 1;
15765 else if (CHARPOS (new_start) > CHARPOS (start))
15767 struct glyph_row *pt_row, *row;
15768 struct glyph_row *first_reusable_row;
15769 struct glyph_row *first_row_to_display;
15770 int dy;
15771 int yb = window_text_bottom_y (w);
15773 /* Find the row starting at new_start, if there is one. Don't
15774 reuse a partially visible line at the end. */
15775 first_reusable_row = start_row;
15776 while (first_reusable_row->enabled_p
15777 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
15778 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15779 < CHARPOS (new_start)))
15780 ++first_reusable_row;
15782 /* Give up if there is no row to reuse. */
15783 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
15784 || !first_reusable_row->enabled_p
15785 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15786 != CHARPOS (new_start)))
15787 return 0;
15789 /* We can reuse fully visible rows beginning with
15790 first_reusable_row to the end of the window. Set
15791 first_row_to_display to the first row that cannot be reused.
15792 Set pt_row to the row containing point, if there is any. */
15793 pt_row = NULL;
15794 for (first_row_to_display = first_reusable_row;
15795 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
15796 ++first_row_to_display)
15798 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
15799 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
15800 pt_row = first_row_to_display;
15803 /* Start displaying at the start of first_row_to_display. */
15804 xassert (first_row_to_display->y < yb);
15805 init_to_row_start (&it, w, first_row_to_display);
15807 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
15808 - start_vpos);
15809 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
15810 - nrows_scrolled);
15811 it.current_y = (first_row_to_display->y - first_reusable_row->y
15812 + WINDOW_HEADER_LINE_HEIGHT (w));
15814 /* Display lines beginning with first_row_to_display in the
15815 desired matrix. Set last_text_row to the last row displayed
15816 that displays text. */
15817 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
15818 if (pt_row == NULL)
15819 w->cursor.vpos = -1;
15820 last_text_row = NULL;
15821 while (it.current_y < it.last_visible_y && !fonts_changed_p)
15822 if (display_line (&it))
15823 last_text_row = it.glyph_row - 1;
15825 /* If point is in a reused row, adjust y and vpos of the cursor
15826 position. */
15827 if (pt_row)
15829 w->cursor.vpos -= nrows_scrolled;
15830 w->cursor.y -= first_reusable_row->y - start_row->y;
15833 /* Give up if point isn't in a row displayed or reused. (This
15834 also handles the case where w->cursor.vpos < nrows_scrolled
15835 after the calls to display_line, which can happen with scroll
15836 margins. See bug#1295.) */
15837 if (w->cursor.vpos < 0)
15839 clear_glyph_matrix (w->desired_matrix);
15840 return 0;
15843 /* Scroll the display. */
15844 run.current_y = first_reusable_row->y;
15845 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
15846 run.height = it.last_visible_y - run.current_y;
15847 dy = run.current_y - run.desired_y;
15849 if (run.height)
15851 update_begin (f);
15852 FRAME_RIF (f)->update_window_begin_hook (w);
15853 FRAME_RIF (f)->clear_window_mouse_face (w);
15854 FRAME_RIF (f)->scroll_run_hook (w, &run);
15855 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15856 update_end (f);
15859 /* Adjust Y positions of reused rows. */
15860 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15861 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15862 max_y = it.last_visible_y;
15863 for (row = first_reusable_row; row < first_row_to_display; ++row)
15865 row->y -= dy;
15866 row->visible_height = row->height;
15867 if (row->y < min_y)
15868 row->visible_height -= min_y - row->y;
15869 if (row->y + row->height > max_y)
15870 row->visible_height -= row->y + row->height - max_y;
15871 if (row->fringe_bitmap_periodic_p)
15872 row->redraw_fringe_bitmaps_p = 1;
15875 /* Scroll the current matrix. */
15876 xassert (nrows_scrolled > 0);
15877 rotate_matrix (w->current_matrix,
15878 start_vpos,
15879 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15880 -nrows_scrolled);
15882 /* Disable rows not reused. */
15883 for (row -= nrows_scrolled; row < bottom_row; ++row)
15884 row->enabled_p = 0;
15886 /* Point may have moved to a different line, so we cannot assume that
15887 the previous cursor position is valid; locate the correct row. */
15888 if (pt_row)
15890 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15891 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
15892 row++)
15894 w->cursor.vpos++;
15895 w->cursor.y = row->y;
15897 if (row < bottom_row)
15899 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
15900 struct glyph *end = glyph + row->used[TEXT_AREA];
15902 /* Can't use this optimization with bidi-reordered glyph
15903 rows, unless cursor is already at point. */
15904 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15906 if (!(w->cursor.hpos >= 0
15907 && w->cursor.hpos < row->used[TEXT_AREA]
15908 && BUFFERP (glyph->object)
15909 && glyph->charpos == PT))
15910 return 0;
15912 else
15913 for (; glyph < end
15914 && (!BUFFERP (glyph->object)
15915 || glyph->charpos < PT);
15916 glyph++)
15918 w->cursor.hpos++;
15919 w->cursor.x += glyph->pixel_width;
15924 /* Adjust window end. A null value of last_text_row means that
15925 the window end is in reused rows which in turn means that
15926 only its vpos can have changed. */
15927 if (last_text_row)
15929 w->window_end_bytepos
15930 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15931 w->window_end_pos
15932 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15933 w->window_end_vpos
15934 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15936 else
15938 w->window_end_vpos
15939 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
15942 w->window_end_valid = Qnil;
15943 w->desired_matrix->no_scrolling_p = 1;
15945 #if GLYPH_DEBUG
15946 debug_method_add (w, "try_window_reusing_current_matrix 2");
15947 #endif
15948 return 1;
15951 return 0;
15956 /************************************************************************
15957 Window redisplay reusing current matrix when buffer has changed
15958 ************************************************************************/
15960 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
15961 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
15962 EMACS_INT *, EMACS_INT *);
15963 static struct glyph_row *
15964 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
15965 struct glyph_row *);
15968 /* Return the last row in MATRIX displaying text. If row START is
15969 non-null, start searching with that row. IT gives the dimensions
15970 of the display. Value is null if matrix is empty; otherwise it is
15971 a pointer to the row found. */
15973 static struct glyph_row *
15974 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
15975 struct glyph_row *start)
15977 struct glyph_row *row, *row_found;
15979 /* Set row_found to the last row in IT->w's current matrix
15980 displaying text. The loop looks funny but think of partially
15981 visible lines. */
15982 row_found = NULL;
15983 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
15984 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15986 xassert (row->enabled_p);
15987 row_found = row;
15988 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
15989 break;
15990 ++row;
15993 return row_found;
15997 /* Return the last row in the current matrix of W that is not affected
15998 by changes at the start of current_buffer that occurred since W's
15999 current matrix was built. Value is null if no such row exists.
16001 BEG_UNCHANGED us the number of characters unchanged at the start of
16002 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16003 first changed character in current_buffer. Characters at positions <
16004 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16005 when the current matrix was built. */
16007 static struct glyph_row *
16008 find_last_unchanged_at_beg_row (struct window *w)
16010 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
16011 struct glyph_row *row;
16012 struct glyph_row *row_found = NULL;
16013 int yb = window_text_bottom_y (w);
16015 /* Find the last row displaying unchanged text. */
16016 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16017 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16018 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16019 ++row)
16021 if (/* If row ends before first_changed_pos, it is unchanged,
16022 except in some case. */
16023 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16024 /* When row ends in ZV and we write at ZV it is not
16025 unchanged. */
16026 && !row->ends_at_zv_p
16027 /* When first_changed_pos is the end of a continued line,
16028 row is not unchanged because it may be no longer
16029 continued. */
16030 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16031 && (row->continued_p
16032 || row->exact_window_width_line_p)))
16033 row_found = row;
16035 /* Stop if last visible row. */
16036 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16037 break;
16040 return row_found;
16044 /* Find the first glyph row in the current matrix of W that is not
16045 affected by changes at the end of current_buffer since the
16046 time W's current matrix was built.
16048 Return in *DELTA the number of chars by which buffer positions in
16049 unchanged text at the end of current_buffer must be adjusted.
16051 Return in *DELTA_BYTES the corresponding number of bytes.
16053 Value is null if no such row exists, i.e. all rows are affected by
16054 changes. */
16056 static struct glyph_row *
16057 find_first_unchanged_at_end_row (struct window *w,
16058 EMACS_INT *delta, EMACS_INT *delta_bytes)
16060 struct glyph_row *row;
16061 struct glyph_row *row_found = NULL;
16063 *delta = *delta_bytes = 0;
16065 /* Display must not have been paused, otherwise the current matrix
16066 is not up to date. */
16067 eassert (!NILP (w->window_end_valid));
16069 /* A value of window_end_pos >= END_UNCHANGED means that the window
16070 end is in the range of changed text. If so, there is no
16071 unchanged row at the end of W's current matrix. */
16072 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16073 return NULL;
16075 /* Set row to the last row in W's current matrix displaying text. */
16076 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16078 /* If matrix is entirely empty, no unchanged row exists. */
16079 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16081 /* The value of row is the last glyph row in the matrix having a
16082 meaningful buffer position in it. The end position of row
16083 corresponds to window_end_pos. This allows us to translate
16084 buffer positions in the current matrix to current buffer
16085 positions for characters not in changed text. */
16086 EMACS_INT Z_old =
16087 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16088 EMACS_INT Z_BYTE_old =
16089 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16090 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
16091 struct glyph_row *first_text_row
16092 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16094 *delta = Z - Z_old;
16095 *delta_bytes = Z_BYTE - Z_BYTE_old;
16097 /* Set last_unchanged_pos to the buffer position of the last
16098 character in the buffer that has not been changed. Z is the
16099 index + 1 of the last character in current_buffer, i.e. by
16100 subtracting END_UNCHANGED we get the index of the last
16101 unchanged character, and we have to add BEG to get its buffer
16102 position. */
16103 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16104 last_unchanged_pos_old = last_unchanged_pos - *delta;
16106 /* Search backward from ROW for a row displaying a line that
16107 starts at a minimum position >= last_unchanged_pos_old. */
16108 for (; row > first_text_row; --row)
16110 /* This used to abort, but it can happen.
16111 It is ok to just stop the search instead here. KFS. */
16112 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16113 break;
16115 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16116 row_found = row;
16120 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16122 return row_found;
16126 /* Make sure that glyph rows in the current matrix of window W
16127 reference the same glyph memory as corresponding rows in the
16128 frame's frame matrix. This function is called after scrolling W's
16129 current matrix on a terminal frame in try_window_id and
16130 try_window_reusing_current_matrix. */
16132 static void
16133 sync_frame_with_window_matrix_rows (struct window *w)
16135 struct frame *f = XFRAME (w->frame);
16136 struct glyph_row *window_row, *window_row_end, *frame_row;
16138 /* Preconditions: W must be a leaf window and full-width. Its frame
16139 must have a frame matrix. */
16140 xassert (NILP (w->hchild) && NILP (w->vchild));
16141 xassert (WINDOW_FULL_WIDTH_P (w));
16142 xassert (!FRAME_WINDOW_P (f));
16144 /* If W is a full-width window, glyph pointers in W's current matrix
16145 have, by definition, to be the same as glyph pointers in the
16146 corresponding frame matrix. Note that frame matrices have no
16147 marginal areas (see build_frame_matrix). */
16148 window_row = w->current_matrix->rows;
16149 window_row_end = window_row + w->current_matrix->nrows;
16150 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16151 while (window_row < window_row_end)
16153 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16154 struct glyph *end = window_row->glyphs[LAST_AREA];
16156 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16157 frame_row->glyphs[TEXT_AREA] = start;
16158 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16159 frame_row->glyphs[LAST_AREA] = end;
16161 /* Disable frame rows whose corresponding window rows have
16162 been disabled in try_window_id. */
16163 if (!window_row->enabled_p)
16164 frame_row->enabled_p = 0;
16166 ++window_row, ++frame_row;
16171 /* Find the glyph row in window W containing CHARPOS. Consider all
16172 rows between START and END (not inclusive). END null means search
16173 all rows to the end of the display area of W. Value is the row
16174 containing CHARPOS or null. */
16176 struct glyph_row *
16177 row_containing_pos (struct window *w, EMACS_INT charpos,
16178 struct glyph_row *start, struct glyph_row *end, int dy)
16180 struct glyph_row *row = start;
16181 struct glyph_row *best_row = NULL;
16182 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16183 int last_y;
16185 /* If we happen to start on a header-line, skip that. */
16186 if (row->mode_line_p)
16187 ++row;
16189 if ((end && row >= end) || !row->enabled_p)
16190 return NULL;
16192 last_y = window_text_bottom_y (w) - dy;
16194 while (1)
16196 /* Give up if we have gone too far. */
16197 if (end && row >= end)
16198 return NULL;
16199 /* This formerly returned if they were equal.
16200 I think that both quantities are of a "last plus one" type;
16201 if so, when they are equal, the row is within the screen. -- rms. */
16202 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16203 return NULL;
16205 /* If it is in this row, return this row. */
16206 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16207 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16208 /* The end position of a row equals the start
16209 position of the next row. If CHARPOS is there, we
16210 would rather display it in the next line, except
16211 when this line ends in ZV. */
16212 && !row->ends_at_zv_p
16213 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16214 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16216 struct glyph *g;
16218 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16219 || (!best_row && !row->continued_p))
16220 return row;
16221 /* In bidi-reordered rows, there could be several rows
16222 occluding point, all of them belonging to the same
16223 continued line. We need to find the row which fits
16224 CHARPOS the best. */
16225 for (g = row->glyphs[TEXT_AREA];
16226 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16227 g++)
16229 if (!STRINGP (g->object))
16231 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16233 mindif = eabs (g->charpos - charpos);
16234 best_row = row;
16235 /* Exact match always wins. */
16236 if (mindif == 0)
16237 return best_row;
16242 else if (best_row && !row->continued_p)
16243 return best_row;
16244 ++row;
16249 /* Try to redisplay window W by reusing its existing display. W's
16250 current matrix must be up to date when this function is called,
16251 i.e. window_end_valid must not be nil.
16253 Value is
16255 1 if display has been updated
16256 0 if otherwise unsuccessful
16257 -1 if redisplay with same window start is known not to succeed
16259 The following steps are performed:
16261 1. Find the last row in the current matrix of W that is not
16262 affected by changes at the start of current_buffer. If no such row
16263 is found, give up.
16265 2. Find the first row in W's current matrix that is not affected by
16266 changes at the end of current_buffer. Maybe there is no such row.
16268 3. Display lines beginning with the row + 1 found in step 1 to the
16269 row found in step 2 or, if step 2 didn't find a row, to the end of
16270 the window.
16272 4. If cursor is not known to appear on the window, give up.
16274 5. If display stopped at the row found in step 2, scroll the
16275 display and current matrix as needed.
16277 6. Maybe display some lines at the end of W, if we must. This can
16278 happen under various circumstances, like a partially visible line
16279 becoming fully visible, or because newly displayed lines are displayed
16280 in smaller font sizes.
16282 7. Update W's window end information. */
16284 static int
16285 try_window_id (struct window *w)
16287 struct frame *f = XFRAME (w->frame);
16288 struct glyph_matrix *current_matrix = w->current_matrix;
16289 struct glyph_matrix *desired_matrix = w->desired_matrix;
16290 struct glyph_row *last_unchanged_at_beg_row;
16291 struct glyph_row *first_unchanged_at_end_row;
16292 struct glyph_row *row;
16293 struct glyph_row *bottom_row;
16294 int bottom_vpos;
16295 struct it it;
16296 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
16297 int dvpos, dy;
16298 struct text_pos start_pos;
16299 struct run run;
16300 int first_unchanged_at_end_vpos = 0;
16301 struct glyph_row *last_text_row, *last_text_row_at_end;
16302 struct text_pos start;
16303 EMACS_INT first_changed_charpos, last_changed_charpos;
16305 #if GLYPH_DEBUG
16306 if (inhibit_try_window_id)
16307 return 0;
16308 #endif
16310 /* This is handy for debugging. */
16311 #if 0
16312 #define GIVE_UP(X) \
16313 do { \
16314 fprintf (stderr, "try_window_id give up %d\n", (X)); \
16315 return 0; \
16316 } while (0)
16317 #else
16318 #define GIVE_UP(X) return 0
16319 #endif
16321 SET_TEXT_POS_FROM_MARKER (start, w->start);
16323 /* Don't use this for mini-windows because these can show
16324 messages and mini-buffers, and we don't handle that here. */
16325 if (MINI_WINDOW_P (w))
16326 GIVE_UP (1);
16328 /* This flag is used to prevent redisplay optimizations. */
16329 if (windows_or_buffers_changed || cursor_type_changed)
16330 GIVE_UP (2);
16332 /* Verify that narrowing has not changed.
16333 Also verify that we were not told to prevent redisplay optimizations.
16334 It would be nice to further
16335 reduce the number of cases where this prevents try_window_id. */
16336 if (current_buffer->clip_changed
16337 || current_buffer->prevent_redisplay_optimizations_p)
16338 GIVE_UP (3);
16340 /* Window must either use window-based redisplay or be full width. */
16341 if (!FRAME_WINDOW_P (f)
16342 && (!FRAME_LINE_INS_DEL_OK (f)
16343 || !WINDOW_FULL_WIDTH_P (w)))
16344 GIVE_UP (4);
16346 /* Give up if point is known NOT to appear in W. */
16347 if (PT < CHARPOS (start))
16348 GIVE_UP (5);
16350 /* Another way to prevent redisplay optimizations. */
16351 if (XFASTINT (w->last_modified) == 0)
16352 GIVE_UP (6);
16354 /* Verify that window is not hscrolled. */
16355 if (XFASTINT (w->hscroll) != 0)
16356 GIVE_UP (7);
16358 /* Verify that display wasn't paused. */
16359 if (NILP (w->window_end_valid))
16360 GIVE_UP (8);
16362 /* Can't use this if highlighting a region because a cursor movement
16363 will do more than just set the cursor. */
16364 if (!NILP (Vtransient_mark_mode)
16365 && !NILP (BVAR (current_buffer, mark_active)))
16366 GIVE_UP (9);
16368 /* Likewise if highlighting trailing whitespace. */
16369 if (!NILP (Vshow_trailing_whitespace))
16370 GIVE_UP (11);
16372 /* Likewise if showing a region. */
16373 if (!NILP (w->region_showing))
16374 GIVE_UP (10);
16376 /* Can't use this if overlay arrow position and/or string have
16377 changed. */
16378 if (overlay_arrows_changed_p ())
16379 GIVE_UP (12);
16381 /* When word-wrap is on, adding a space to the first word of a
16382 wrapped line can change the wrap position, altering the line
16383 above it. It might be worthwhile to handle this more
16384 intelligently, but for now just redisplay from scratch. */
16385 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
16386 GIVE_UP (21);
16388 /* Under bidi reordering, adding or deleting a character in the
16389 beginning of a paragraph, before the first strong directional
16390 character, can change the base direction of the paragraph (unless
16391 the buffer specifies a fixed paragraph direction), which will
16392 require to redisplay the whole paragraph. It might be worthwhile
16393 to find the paragraph limits and widen the range of redisplayed
16394 lines to that, but for now just give up this optimization and
16395 redisplay from scratch. */
16396 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16397 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
16398 GIVE_UP (22);
16400 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
16401 only if buffer has really changed. The reason is that the gap is
16402 initially at Z for freshly visited files. The code below would
16403 set end_unchanged to 0 in that case. */
16404 if (MODIFF > SAVE_MODIFF
16405 /* This seems to happen sometimes after saving a buffer. */
16406 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
16408 if (GPT - BEG < BEG_UNCHANGED)
16409 BEG_UNCHANGED = GPT - BEG;
16410 if (Z - GPT < END_UNCHANGED)
16411 END_UNCHANGED = Z - GPT;
16414 /* The position of the first and last character that has been changed. */
16415 first_changed_charpos = BEG + BEG_UNCHANGED;
16416 last_changed_charpos = Z - END_UNCHANGED;
16418 /* If window starts after a line end, and the last change is in
16419 front of that newline, then changes don't affect the display.
16420 This case happens with stealth-fontification. Note that although
16421 the display is unchanged, glyph positions in the matrix have to
16422 be adjusted, of course. */
16423 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16424 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
16425 && ((last_changed_charpos < CHARPOS (start)
16426 && CHARPOS (start) == BEGV)
16427 || (last_changed_charpos < CHARPOS (start) - 1
16428 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
16430 EMACS_INT Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
16431 struct glyph_row *r0;
16433 /* Compute how many chars/bytes have been added to or removed
16434 from the buffer. */
16435 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16436 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16437 Z_delta = Z - Z_old;
16438 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
16440 /* Give up if PT is not in the window. Note that it already has
16441 been checked at the start of try_window_id that PT is not in
16442 front of the window start. */
16443 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
16444 GIVE_UP (13);
16446 /* If window start is unchanged, we can reuse the whole matrix
16447 as is, after adjusting glyph positions. No need to compute
16448 the window end again, since its offset from Z hasn't changed. */
16449 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16450 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
16451 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
16452 /* PT must not be in a partially visible line. */
16453 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
16454 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16456 /* Adjust positions in the glyph matrix. */
16457 if (Z_delta || Z_delta_bytes)
16459 struct glyph_row *r1
16460 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16461 increment_matrix_positions (w->current_matrix,
16462 MATRIX_ROW_VPOS (r0, current_matrix),
16463 MATRIX_ROW_VPOS (r1, current_matrix),
16464 Z_delta, Z_delta_bytes);
16467 /* Set the cursor. */
16468 row = row_containing_pos (w, PT, r0, NULL, 0);
16469 if (row)
16470 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
16471 else
16472 abort ();
16473 return 1;
16477 /* Handle the case that changes are all below what is displayed in
16478 the window, and that PT is in the window. This shortcut cannot
16479 be taken if ZV is visible in the window, and text has been added
16480 there that is visible in the window. */
16481 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
16482 /* ZV is not visible in the window, or there are no
16483 changes at ZV, actually. */
16484 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
16485 || first_changed_charpos == last_changed_charpos))
16487 struct glyph_row *r0;
16489 /* Give up if PT is not in the window. Note that it already has
16490 been checked at the start of try_window_id that PT is not in
16491 front of the window start. */
16492 if (PT >= MATRIX_ROW_END_CHARPOS (row))
16493 GIVE_UP (14);
16495 /* If window start is unchanged, we can reuse the whole matrix
16496 as is, without changing glyph positions since no text has
16497 been added/removed in front of the window end. */
16498 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16499 if (TEXT_POS_EQUAL_P (start, r0->minpos)
16500 /* PT must not be in a partially visible line. */
16501 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
16502 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16504 /* We have to compute the window end anew since text
16505 could have been added/removed after it. */
16506 w->window_end_pos
16507 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16508 w->window_end_bytepos
16509 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16511 /* Set the cursor. */
16512 row = row_containing_pos (w, PT, r0, NULL, 0);
16513 if (row)
16514 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
16515 else
16516 abort ();
16517 return 2;
16521 /* Give up if window start is in the changed area.
16523 The condition used to read
16525 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
16527 but why that was tested escapes me at the moment. */
16528 if (CHARPOS (start) >= first_changed_charpos
16529 && CHARPOS (start) <= last_changed_charpos)
16530 GIVE_UP (15);
16532 /* Check that window start agrees with the start of the first glyph
16533 row in its current matrix. Check this after we know the window
16534 start is not in changed text, otherwise positions would not be
16535 comparable. */
16536 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
16537 if (!TEXT_POS_EQUAL_P (start, row->minpos))
16538 GIVE_UP (16);
16540 /* Give up if the window ends in strings. Overlay strings
16541 at the end are difficult to handle, so don't try. */
16542 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
16543 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
16544 GIVE_UP (20);
16546 /* Compute the position at which we have to start displaying new
16547 lines. Some of the lines at the top of the window might be
16548 reusable because they are not displaying changed text. Find the
16549 last row in W's current matrix not affected by changes at the
16550 start of current_buffer. Value is null if changes start in the
16551 first line of window. */
16552 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
16553 if (last_unchanged_at_beg_row)
16555 /* Avoid starting to display in the moddle of a character, a TAB
16556 for instance. This is easier than to set up the iterator
16557 exactly, and it's not a frequent case, so the additional
16558 effort wouldn't really pay off. */
16559 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
16560 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
16561 && last_unchanged_at_beg_row > w->current_matrix->rows)
16562 --last_unchanged_at_beg_row;
16564 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
16565 GIVE_UP (17);
16567 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
16568 GIVE_UP (18);
16569 start_pos = it.current.pos;
16571 /* Start displaying new lines in the desired matrix at the same
16572 vpos we would use in the current matrix, i.e. below
16573 last_unchanged_at_beg_row. */
16574 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
16575 current_matrix);
16576 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16577 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
16579 xassert (it.hpos == 0 && it.current_x == 0);
16581 else
16583 /* There are no reusable lines at the start of the window.
16584 Start displaying in the first text line. */
16585 start_display (&it, w, start);
16586 it.vpos = it.first_vpos;
16587 start_pos = it.current.pos;
16590 /* Find the first row that is not affected by changes at the end of
16591 the buffer. Value will be null if there is no unchanged row, in
16592 which case we must redisplay to the end of the window. delta
16593 will be set to the value by which buffer positions beginning with
16594 first_unchanged_at_end_row have to be adjusted due to text
16595 changes. */
16596 first_unchanged_at_end_row
16597 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
16598 IF_DEBUG (debug_delta = delta);
16599 IF_DEBUG (debug_delta_bytes = delta_bytes);
16601 /* Set stop_pos to the buffer position up to which we will have to
16602 display new lines. If first_unchanged_at_end_row != NULL, this
16603 is the buffer position of the start of the line displayed in that
16604 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
16605 that we don't stop at a buffer position. */
16606 stop_pos = 0;
16607 if (first_unchanged_at_end_row)
16609 xassert (last_unchanged_at_beg_row == NULL
16610 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
16612 /* If this is a continuation line, move forward to the next one
16613 that isn't. Changes in lines above affect this line.
16614 Caution: this may move first_unchanged_at_end_row to a row
16615 not displaying text. */
16616 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
16617 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
16618 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
16619 < it.last_visible_y))
16620 ++first_unchanged_at_end_row;
16622 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
16623 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
16624 >= it.last_visible_y))
16625 first_unchanged_at_end_row = NULL;
16626 else
16628 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
16629 + delta);
16630 first_unchanged_at_end_vpos
16631 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
16632 xassert (stop_pos >= Z - END_UNCHANGED);
16635 else if (last_unchanged_at_beg_row == NULL)
16636 GIVE_UP (19);
16639 #if GLYPH_DEBUG
16641 /* Either there is no unchanged row at the end, or the one we have
16642 now displays text. This is a necessary condition for the window
16643 end pos calculation at the end of this function. */
16644 xassert (first_unchanged_at_end_row == NULL
16645 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
16647 debug_last_unchanged_at_beg_vpos
16648 = (last_unchanged_at_beg_row
16649 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
16650 : -1);
16651 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
16653 #endif /* GLYPH_DEBUG != 0 */
16656 /* Display new lines. Set last_text_row to the last new line
16657 displayed which has text on it, i.e. might end up as being the
16658 line where the window_end_vpos is. */
16659 w->cursor.vpos = -1;
16660 last_text_row = NULL;
16661 overlay_arrow_seen = 0;
16662 while (it.current_y < it.last_visible_y
16663 && !fonts_changed_p
16664 && (first_unchanged_at_end_row == NULL
16665 || IT_CHARPOS (it) < stop_pos))
16667 if (display_line (&it))
16668 last_text_row = it.glyph_row - 1;
16671 if (fonts_changed_p)
16672 return -1;
16675 /* Compute differences in buffer positions, y-positions etc. for
16676 lines reused at the bottom of the window. Compute what we can
16677 scroll. */
16678 if (first_unchanged_at_end_row
16679 /* No lines reused because we displayed everything up to the
16680 bottom of the window. */
16681 && it.current_y < it.last_visible_y)
16683 dvpos = (it.vpos
16684 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
16685 current_matrix));
16686 dy = it.current_y - first_unchanged_at_end_row->y;
16687 run.current_y = first_unchanged_at_end_row->y;
16688 run.desired_y = run.current_y + dy;
16689 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
16691 else
16693 delta = delta_bytes = dvpos = dy
16694 = run.current_y = run.desired_y = run.height = 0;
16695 first_unchanged_at_end_row = NULL;
16697 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
16700 /* Find the cursor if not already found. We have to decide whether
16701 PT will appear on this window (it sometimes doesn't, but this is
16702 not a very frequent case.) This decision has to be made before
16703 the current matrix is altered. A value of cursor.vpos < 0 means
16704 that PT is either in one of the lines beginning at
16705 first_unchanged_at_end_row or below the window. Don't care for
16706 lines that might be displayed later at the window end; as
16707 mentioned, this is not a frequent case. */
16708 if (w->cursor.vpos < 0)
16710 /* Cursor in unchanged rows at the top? */
16711 if (PT < CHARPOS (start_pos)
16712 && last_unchanged_at_beg_row)
16714 row = row_containing_pos (w, PT,
16715 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
16716 last_unchanged_at_beg_row + 1, 0);
16717 if (row)
16718 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16721 /* Start from first_unchanged_at_end_row looking for PT. */
16722 else if (first_unchanged_at_end_row)
16724 row = row_containing_pos (w, PT - delta,
16725 first_unchanged_at_end_row, NULL, 0);
16726 if (row)
16727 set_cursor_from_row (w, row, w->current_matrix, delta,
16728 delta_bytes, dy, dvpos);
16731 /* Give up if cursor was not found. */
16732 if (w->cursor.vpos < 0)
16734 clear_glyph_matrix (w->desired_matrix);
16735 return -1;
16739 /* Don't let the cursor end in the scroll margins. */
16741 int this_scroll_margin, cursor_height;
16743 this_scroll_margin = max (0, scroll_margin);
16744 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16745 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
16746 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
16748 if ((w->cursor.y < this_scroll_margin
16749 && CHARPOS (start) > BEGV)
16750 /* Old redisplay didn't take scroll margin into account at the bottom,
16751 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
16752 || (w->cursor.y + (make_cursor_line_fully_visible_p
16753 ? cursor_height + this_scroll_margin
16754 : 1)) > it.last_visible_y)
16756 w->cursor.vpos = -1;
16757 clear_glyph_matrix (w->desired_matrix);
16758 return -1;
16762 /* Scroll the display. Do it before changing the current matrix so
16763 that xterm.c doesn't get confused about where the cursor glyph is
16764 found. */
16765 if (dy && run.height)
16767 update_begin (f);
16769 if (FRAME_WINDOW_P (f))
16771 FRAME_RIF (f)->update_window_begin_hook (w);
16772 FRAME_RIF (f)->clear_window_mouse_face (w);
16773 FRAME_RIF (f)->scroll_run_hook (w, &run);
16774 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16776 else
16778 /* Terminal frame. In this case, dvpos gives the number of
16779 lines to scroll by; dvpos < 0 means scroll up. */
16780 int from_vpos
16781 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
16782 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
16783 int end = (WINDOW_TOP_EDGE_LINE (w)
16784 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
16785 + window_internal_height (w));
16787 #if defined (HAVE_GPM) || defined (MSDOS)
16788 x_clear_window_mouse_face (w);
16789 #endif
16790 /* Perform the operation on the screen. */
16791 if (dvpos > 0)
16793 /* Scroll last_unchanged_at_beg_row to the end of the
16794 window down dvpos lines. */
16795 set_terminal_window (f, end);
16797 /* On dumb terminals delete dvpos lines at the end
16798 before inserting dvpos empty lines. */
16799 if (!FRAME_SCROLL_REGION_OK (f))
16800 ins_del_lines (f, end - dvpos, -dvpos);
16802 /* Insert dvpos empty lines in front of
16803 last_unchanged_at_beg_row. */
16804 ins_del_lines (f, from, dvpos);
16806 else if (dvpos < 0)
16808 /* Scroll up last_unchanged_at_beg_vpos to the end of
16809 the window to last_unchanged_at_beg_vpos - |dvpos|. */
16810 set_terminal_window (f, end);
16812 /* Delete dvpos lines in front of
16813 last_unchanged_at_beg_vpos. ins_del_lines will set
16814 the cursor to the given vpos and emit |dvpos| delete
16815 line sequences. */
16816 ins_del_lines (f, from + dvpos, dvpos);
16818 /* On a dumb terminal insert dvpos empty lines at the
16819 end. */
16820 if (!FRAME_SCROLL_REGION_OK (f))
16821 ins_del_lines (f, end + dvpos, -dvpos);
16824 set_terminal_window (f, 0);
16827 update_end (f);
16830 /* Shift reused rows of the current matrix to the right position.
16831 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
16832 text. */
16833 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16834 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
16835 if (dvpos < 0)
16837 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
16838 bottom_vpos, dvpos);
16839 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
16840 bottom_vpos, 0);
16842 else if (dvpos > 0)
16844 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
16845 bottom_vpos, dvpos);
16846 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
16847 first_unchanged_at_end_vpos + dvpos, 0);
16850 /* For frame-based redisplay, make sure that current frame and window
16851 matrix are in sync with respect to glyph memory. */
16852 if (!FRAME_WINDOW_P (f))
16853 sync_frame_with_window_matrix_rows (w);
16855 /* Adjust buffer positions in reused rows. */
16856 if (delta || delta_bytes)
16857 increment_matrix_positions (current_matrix,
16858 first_unchanged_at_end_vpos + dvpos,
16859 bottom_vpos, delta, delta_bytes);
16861 /* Adjust Y positions. */
16862 if (dy)
16863 shift_glyph_matrix (w, current_matrix,
16864 first_unchanged_at_end_vpos + dvpos,
16865 bottom_vpos, dy);
16867 if (first_unchanged_at_end_row)
16869 first_unchanged_at_end_row += dvpos;
16870 if (first_unchanged_at_end_row->y >= it.last_visible_y
16871 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
16872 first_unchanged_at_end_row = NULL;
16875 /* If scrolling up, there may be some lines to display at the end of
16876 the window. */
16877 last_text_row_at_end = NULL;
16878 if (dy < 0)
16880 /* Scrolling up can leave for example a partially visible line
16881 at the end of the window to be redisplayed. */
16882 /* Set last_row to the glyph row in the current matrix where the
16883 window end line is found. It has been moved up or down in
16884 the matrix by dvpos. */
16885 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
16886 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
16888 /* If last_row is the window end line, it should display text. */
16889 xassert (last_row->displays_text_p);
16891 /* If window end line was partially visible before, begin
16892 displaying at that line. Otherwise begin displaying with the
16893 line following it. */
16894 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
16896 init_to_row_start (&it, w, last_row);
16897 it.vpos = last_vpos;
16898 it.current_y = last_row->y;
16900 else
16902 init_to_row_end (&it, w, last_row);
16903 it.vpos = 1 + last_vpos;
16904 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
16905 ++last_row;
16908 /* We may start in a continuation line. If so, we have to
16909 get the right continuation_lines_width and current_x. */
16910 it.continuation_lines_width = last_row->continuation_lines_width;
16911 it.hpos = it.current_x = 0;
16913 /* Display the rest of the lines at the window end. */
16914 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16915 while (it.current_y < it.last_visible_y
16916 && !fonts_changed_p)
16918 /* Is it always sure that the display agrees with lines in
16919 the current matrix? I don't think so, so we mark rows
16920 displayed invalid in the current matrix by setting their
16921 enabled_p flag to zero. */
16922 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
16923 if (display_line (&it))
16924 last_text_row_at_end = it.glyph_row - 1;
16928 /* Update window_end_pos and window_end_vpos. */
16929 if (first_unchanged_at_end_row
16930 && !last_text_row_at_end)
16932 /* Window end line if one of the preserved rows from the current
16933 matrix. Set row to the last row displaying text in current
16934 matrix starting at first_unchanged_at_end_row, after
16935 scrolling. */
16936 xassert (first_unchanged_at_end_row->displays_text_p);
16937 row = find_last_row_displaying_text (w->current_matrix, &it,
16938 first_unchanged_at_end_row);
16939 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
16941 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16942 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16943 w->window_end_vpos
16944 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
16945 xassert (w->window_end_bytepos >= 0);
16946 IF_DEBUG (debug_method_add (w, "A"));
16948 else if (last_text_row_at_end)
16950 w->window_end_pos
16951 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
16952 w->window_end_bytepos
16953 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
16954 w->window_end_vpos
16955 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
16956 xassert (w->window_end_bytepos >= 0);
16957 IF_DEBUG (debug_method_add (w, "B"));
16959 else if (last_text_row)
16961 /* We have displayed either to the end of the window or at the
16962 end of the window, i.e. the last row with text is to be found
16963 in the desired matrix. */
16964 w->window_end_pos
16965 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16966 w->window_end_bytepos
16967 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16968 w->window_end_vpos
16969 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
16970 xassert (w->window_end_bytepos >= 0);
16972 else if (first_unchanged_at_end_row == NULL
16973 && last_text_row == NULL
16974 && last_text_row_at_end == NULL)
16976 /* Displayed to end of window, but no line containing text was
16977 displayed. Lines were deleted at the end of the window. */
16978 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
16979 int vpos = XFASTINT (w->window_end_vpos);
16980 struct glyph_row *current_row = current_matrix->rows + vpos;
16981 struct glyph_row *desired_row = desired_matrix->rows + vpos;
16983 for (row = NULL;
16984 row == NULL && vpos >= first_vpos;
16985 --vpos, --current_row, --desired_row)
16987 if (desired_row->enabled_p)
16989 if (desired_row->displays_text_p)
16990 row = desired_row;
16992 else if (current_row->displays_text_p)
16993 row = current_row;
16996 xassert (row != NULL);
16997 w->window_end_vpos = make_number (vpos + 1);
16998 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16999 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17000 xassert (w->window_end_bytepos >= 0);
17001 IF_DEBUG (debug_method_add (w, "C"));
17003 else
17004 abort ();
17006 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17007 debug_end_vpos = XFASTINT (w->window_end_vpos));
17009 /* Record that display has not been completed. */
17010 w->window_end_valid = Qnil;
17011 w->desired_matrix->no_scrolling_p = 1;
17012 return 3;
17014 #undef GIVE_UP
17019 /***********************************************************************
17020 More debugging support
17021 ***********************************************************************/
17023 #if GLYPH_DEBUG
17025 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17026 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17027 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17030 /* Dump the contents of glyph matrix MATRIX on stderr.
17032 GLYPHS 0 means don't show glyph contents.
17033 GLYPHS 1 means show glyphs in short form
17034 GLYPHS > 1 means show glyphs in long form. */
17036 void
17037 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17039 int i;
17040 for (i = 0; i < matrix->nrows; ++i)
17041 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17045 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17046 the glyph row and area where the glyph comes from. */
17048 void
17049 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17051 if (glyph->type == CHAR_GLYPH)
17053 fprintf (stderr,
17054 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17055 glyph - row->glyphs[TEXT_AREA],
17056 'C',
17057 glyph->charpos,
17058 (BUFFERP (glyph->object)
17059 ? 'B'
17060 : (STRINGP (glyph->object)
17061 ? 'S'
17062 : '-')),
17063 glyph->pixel_width,
17064 glyph->u.ch,
17065 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17066 ? glyph->u.ch
17067 : '.'),
17068 glyph->face_id,
17069 glyph->left_box_line_p,
17070 glyph->right_box_line_p);
17072 else if (glyph->type == STRETCH_GLYPH)
17074 fprintf (stderr,
17075 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17076 glyph - row->glyphs[TEXT_AREA],
17077 'S',
17078 glyph->charpos,
17079 (BUFFERP (glyph->object)
17080 ? 'B'
17081 : (STRINGP (glyph->object)
17082 ? 'S'
17083 : '-')),
17084 glyph->pixel_width,
17086 '.',
17087 glyph->face_id,
17088 glyph->left_box_line_p,
17089 glyph->right_box_line_p);
17091 else if (glyph->type == IMAGE_GLYPH)
17093 fprintf (stderr,
17094 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17095 glyph - row->glyphs[TEXT_AREA],
17096 'I',
17097 glyph->charpos,
17098 (BUFFERP (glyph->object)
17099 ? 'B'
17100 : (STRINGP (glyph->object)
17101 ? 'S'
17102 : '-')),
17103 glyph->pixel_width,
17104 glyph->u.img_id,
17105 '.',
17106 glyph->face_id,
17107 glyph->left_box_line_p,
17108 glyph->right_box_line_p);
17110 else if (glyph->type == COMPOSITE_GLYPH)
17112 fprintf (stderr,
17113 " %5td %4c %6"pI"d %c %3d 0x%05x",
17114 glyph - row->glyphs[TEXT_AREA],
17115 '+',
17116 glyph->charpos,
17117 (BUFFERP (glyph->object)
17118 ? 'B'
17119 : (STRINGP (glyph->object)
17120 ? 'S'
17121 : '-')),
17122 glyph->pixel_width,
17123 glyph->u.cmp.id);
17124 if (glyph->u.cmp.automatic)
17125 fprintf (stderr,
17126 "[%d-%d]",
17127 glyph->slice.cmp.from, glyph->slice.cmp.to);
17128 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17129 glyph->face_id,
17130 glyph->left_box_line_p,
17131 glyph->right_box_line_p);
17136 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17137 GLYPHS 0 means don't show glyph contents.
17138 GLYPHS 1 means show glyphs in short form
17139 GLYPHS > 1 means show glyphs in long form. */
17141 void
17142 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17144 if (glyphs != 1)
17146 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17147 fprintf (stderr, "======================================================================\n");
17149 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17150 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17151 vpos,
17152 MATRIX_ROW_START_CHARPOS (row),
17153 MATRIX_ROW_END_CHARPOS (row),
17154 row->used[TEXT_AREA],
17155 row->contains_overlapping_glyphs_p,
17156 row->enabled_p,
17157 row->truncated_on_left_p,
17158 row->truncated_on_right_p,
17159 row->continued_p,
17160 MATRIX_ROW_CONTINUATION_LINE_P (row),
17161 row->displays_text_p,
17162 row->ends_at_zv_p,
17163 row->fill_line_p,
17164 row->ends_in_middle_of_char_p,
17165 row->starts_in_middle_of_char_p,
17166 row->mouse_face_p,
17167 row->x,
17168 row->y,
17169 row->pixel_width,
17170 row->height,
17171 row->visible_height,
17172 row->ascent,
17173 row->phys_ascent);
17174 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
17175 row->end.overlay_string_index,
17176 row->continuation_lines_width);
17177 fprintf (stderr, "%9"pI"d %5"pI"d\n",
17178 CHARPOS (row->start.string_pos),
17179 CHARPOS (row->end.string_pos));
17180 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17181 row->end.dpvec_index);
17184 if (glyphs > 1)
17186 int area;
17188 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17190 struct glyph *glyph = row->glyphs[area];
17191 struct glyph *glyph_end = glyph + row->used[area];
17193 /* Glyph for a line end in text. */
17194 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17195 ++glyph_end;
17197 if (glyph < glyph_end)
17198 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
17200 for (; glyph < glyph_end; ++glyph)
17201 dump_glyph (row, glyph, area);
17204 else if (glyphs == 1)
17206 int area;
17208 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17210 char *s = (char *) alloca (row->used[area] + 1);
17211 int i;
17213 for (i = 0; i < row->used[area]; ++i)
17215 struct glyph *glyph = row->glyphs[area] + i;
17216 if (glyph->type == CHAR_GLYPH
17217 && glyph->u.ch < 0x80
17218 && glyph->u.ch >= ' ')
17219 s[i] = glyph->u.ch;
17220 else
17221 s[i] = '.';
17224 s[i] = '\0';
17225 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17231 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17232 Sdump_glyph_matrix, 0, 1, "p",
17233 doc: /* Dump the current matrix of the selected window to stderr.
17234 Shows contents of glyph row structures. With non-nil
17235 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17236 glyphs in short form, otherwise show glyphs in long form. */)
17237 (Lisp_Object glyphs)
17239 struct window *w = XWINDOW (selected_window);
17240 struct buffer *buffer = XBUFFER (w->buffer);
17242 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17243 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17244 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17245 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17246 fprintf (stderr, "=============================================\n");
17247 dump_glyph_matrix (w->current_matrix,
17248 NILP (glyphs) ? 0 : XINT (glyphs));
17249 return Qnil;
17253 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17254 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17255 (void)
17257 struct frame *f = XFRAME (selected_frame);
17258 dump_glyph_matrix (f->current_matrix, 1);
17259 return Qnil;
17263 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17264 doc: /* Dump glyph row ROW to stderr.
17265 GLYPH 0 means don't dump glyphs.
17266 GLYPH 1 means dump glyphs in short form.
17267 GLYPH > 1 or omitted means dump glyphs in long form. */)
17268 (Lisp_Object row, Lisp_Object glyphs)
17270 struct glyph_matrix *matrix;
17271 int vpos;
17273 CHECK_NUMBER (row);
17274 matrix = XWINDOW (selected_window)->current_matrix;
17275 vpos = XINT (row);
17276 if (vpos >= 0 && vpos < matrix->nrows)
17277 dump_glyph_row (MATRIX_ROW (matrix, vpos),
17278 vpos,
17279 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17280 return Qnil;
17284 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
17285 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
17286 GLYPH 0 means don't dump glyphs.
17287 GLYPH 1 means dump glyphs in short form.
17288 GLYPH > 1 or omitted means dump glyphs in long form. */)
17289 (Lisp_Object row, Lisp_Object glyphs)
17291 struct frame *sf = SELECTED_FRAME ();
17292 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
17293 int vpos;
17295 CHECK_NUMBER (row);
17296 vpos = XINT (row);
17297 if (vpos >= 0 && vpos < m->nrows)
17298 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
17299 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17300 return Qnil;
17304 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
17305 doc: /* Toggle tracing of redisplay.
17306 With ARG, turn tracing on if and only if ARG is positive. */)
17307 (Lisp_Object arg)
17309 if (NILP (arg))
17310 trace_redisplay_p = !trace_redisplay_p;
17311 else
17313 arg = Fprefix_numeric_value (arg);
17314 trace_redisplay_p = XINT (arg) > 0;
17317 return Qnil;
17321 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
17322 doc: /* Like `format', but print result to stderr.
17323 usage: (trace-to-stderr STRING &rest OBJECTS) */)
17324 (ptrdiff_t nargs, Lisp_Object *args)
17326 Lisp_Object s = Fformat (nargs, args);
17327 fprintf (stderr, "%s", SDATA (s));
17328 return Qnil;
17331 #endif /* GLYPH_DEBUG */
17335 /***********************************************************************
17336 Building Desired Matrix Rows
17337 ***********************************************************************/
17339 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
17340 Used for non-window-redisplay windows, and for windows w/o left fringe. */
17342 static struct glyph_row *
17343 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
17345 struct frame *f = XFRAME (WINDOW_FRAME (w));
17346 struct buffer *buffer = XBUFFER (w->buffer);
17347 struct buffer *old = current_buffer;
17348 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
17349 int arrow_len = SCHARS (overlay_arrow_string);
17350 const unsigned char *arrow_end = arrow_string + arrow_len;
17351 const unsigned char *p;
17352 struct it it;
17353 int multibyte_p;
17354 int n_glyphs_before;
17356 set_buffer_temp (buffer);
17357 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
17358 it.glyph_row->used[TEXT_AREA] = 0;
17359 SET_TEXT_POS (it.position, 0, 0);
17361 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
17362 p = arrow_string;
17363 while (p < arrow_end)
17365 Lisp_Object face, ilisp;
17367 /* Get the next character. */
17368 if (multibyte_p)
17369 it.c = it.char_to_display = string_char_and_length (p, &it.len);
17370 else
17372 it.c = it.char_to_display = *p, it.len = 1;
17373 if (! ASCII_CHAR_P (it.c))
17374 it.char_to_display = BYTE8_TO_CHAR (it.c);
17376 p += it.len;
17378 /* Get its face. */
17379 ilisp = make_number (p - arrow_string);
17380 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
17381 it.face_id = compute_char_face (f, it.char_to_display, face);
17383 /* Compute its width, get its glyphs. */
17384 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
17385 SET_TEXT_POS (it.position, -1, -1);
17386 PRODUCE_GLYPHS (&it);
17388 /* If this character doesn't fit any more in the line, we have
17389 to remove some glyphs. */
17390 if (it.current_x > it.last_visible_x)
17392 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
17393 break;
17397 set_buffer_temp (old);
17398 return it.glyph_row;
17402 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
17403 glyphs are only inserted for terminal frames since we can't really
17404 win with truncation glyphs when partially visible glyphs are
17405 involved. Which glyphs to insert is determined by
17406 produce_special_glyphs. */
17408 static void
17409 insert_left_trunc_glyphs (struct it *it)
17411 struct it truncate_it;
17412 struct glyph *from, *end, *to, *toend;
17414 xassert (!FRAME_WINDOW_P (it->f));
17416 /* Get the truncation glyphs. */
17417 truncate_it = *it;
17418 truncate_it.current_x = 0;
17419 truncate_it.face_id = DEFAULT_FACE_ID;
17420 truncate_it.glyph_row = &scratch_glyph_row;
17421 truncate_it.glyph_row->used[TEXT_AREA] = 0;
17422 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
17423 truncate_it.object = make_number (0);
17424 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
17426 /* Overwrite glyphs from IT with truncation glyphs. */
17427 if (!it->glyph_row->reversed_p)
17429 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17430 end = from + truncate_it.glyph_row->used[TEXT_AREA];
17431 to = it->glyph_row->glyphs[TEXT_AREA];
17432 toend = to + it->glyph_row->used[TEXT_AREA];
17434 while (from < end)
17435 *to++ = *from++;
17437 /* There may be padding glyphs left over. Overwrite them too. */
17438 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
17440 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17441 while (from < end)
17442 *to++ = *from++;
17445 if (to > toend)
17446 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
17448 else
17450 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
17451 that back to front. */
17452 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
17453 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17454 toend = it->glyph_row->glyphs[TEXT_AREA];
17455 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
17457 while (from >= end && to >= toend)
17458 *to-- = *from--;
17459 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
17461 from =
17462 truncate_it.glyph_row->glyphs[TEXT_AREA]
17463 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17464 while (from >= end && to >= toend)
17465 *to-- = *from--;
17467 if (from >= end)
17469 /* Need to free some room before prepending additional
17470 glyphs. */
17471 int move_by = from - end + 1;
17472 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
17473 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
17475 for ( ; g >= g0; g--)
17476 g[move_by] = *g;
17477 while (from >= end)
17478 *to-- = *from--;
17479 it->glyph_row->used[TEXT_AREA] += move_by;
17485 /* Compute the pixel height and width of IT->glyph_row.
17487 Most of the time, ascent and height of a display line will be equal
17488 to the max_ascent and max_height values of the display iterator
17489 structure. This is not the case if
17491 1. We hit ZV without displaying anything. In this case, max_ascent
17492 and max_height will be zero.
17494 2. We have some glyphs that don't contribute to the line height.
17495 (The glyph row flag contributes_to_line_height_p is for future
17496 pixmap extensions).
17498 The first case is easily covered by using default values because in
17499 these cases, the line height does not really matter, except that it
17500 must not be zero. */
17502 static void
17503 compute_line_metrics (struct it *it)
17505 struct glyph_row *row = it->glyph_row;
17507 if (FRAME_WINDOW_P (it->f))
17509 int i, min_y, max_y;
17511 /* The line may consist of one space only, that was added to
17512 place the cursor on it. If so, the row's height hasn't been
17513 computed yet. */
17514 if (row->height == 0)
17516 if (it->max_ascent + it->max_descent == 0)
17517 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
17518 row->ascent = it->max_ascent;
17519 row->height = it->max_ascent + it->max_descent;
17520 row->phys_ascent = it->max_phys_ascent;
17521 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17522 row->extra_line_spacing = it->max_extra_line_spacing;
17525 /* Compute the width of this line. */
17526 row->pixel_width = row->x;
17527 for (i = 0; i < row->used[TEXT_AREA]; ++i)
17528 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
17530 xassert (row->pixel_width >= 0);
17531 xassert (row->ascent >= 0 && row->height > 0);
17533 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
17534 || MATRIX_ROW_OVERLAPS_PRED_P (row));
17536 /* If first line's physical ascent is larger than its logical
17537 ascent, use the physical ascent, and make the row taller.
17538 This makes accented characters fully visible. */
17539 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
17540 && row->phys_ascent > row->ascent)
17542 row->height += row->phys_ascent - row->ascent;
17543 row->ascent = row->phys_ascent;
17546 /* Compute how much of the line is visible. */
17547 row->visible_height = row->height;
17549 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
17550 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
17552 if (row->y < min_y)
17553 row->visible_height -= min_y - row->y;
17554 if (row->y + row->height > max_y)
17555 row->visible_height -= row->y + row->height - max_y;
17557 else
17559 row->pixel_width = row->used[TEXT_AREA];
17560 if (row->continued_p)
17561 row->pixel_width -= it->continuation_pixel_width;
17562 else if (row->truncated_on_right_p)
17563 row->pixel_width -= it->truncation_pixel_width;
17564 row->ascent = row->phys_ascent = 0;
17565 row->height = row->phys_height = row->visible_height = 1;
17566 row->extra_line_spacing = 0;
17569 /* Compute a hash code for this row. */
17571 int area, i;
17572 row->hash = 0;
17573 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17574 for (i = 0; i < row->used[area]; ++i)
17575 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
17576 + row->glyphs[area][i].u.val
17577 + row->glyphs[area][i].face_id
17578 + row->glyphs[area][i].padding_p
17579 + (row->glyphs[area][i].type << 2));
17582 it->max_ascent = it->max_descent = 0;
17583 it->max_phys_ascent = it->max_phys_descent = 0;
17587 /* Append one space to the glyph row of iterator IT if doing a
17588 window-based redisplay. The space has the same face as
17589 IT->face_id. Value is non-zero if a space was added.
17591 This function is called to make sure that there is always one glyph
17592 at the end of a glyph row that the cursor can be set on under
17593 window-systems. (If there weren't such a glyph we would not know
17594 how wide and tall a box cursor should be displayed).
17596 At the same time this space let's a nicely handle clearing to the
17597 end of the line if the row ends in italic text. */
17599 static int
17600 append_space_for_newline (struct it *it, int default_face_p)
17602 if (FRAME_WINDOW_P (it->f))
17604 int n = it->glyph_row->used[TEXT_AREA];
17606 if (it->glyph_row->glyphs[TEXT_AREA] + n
17607 < it->glyph_row->glyphs[1 + TEXT_AREA])
17609 /* Save some values that must not be changed.
17610 Must save IT->c and IT->len because otherwise
17611 ITERATOR_AT_END_P wouldn't work anymore after
17612 append_space_for_newline has been called. */
17613 enum display_element_type saved_what = it->what;
17614 int saved_c = it->c, saved_len = it->len;
17615 int saved_char_to_display = it->char_to_display;
17616 int saved_x = it->current_x;
17617 int saved_face_id = it->face_id;
17618 struct text_pos saved_pos;
17619 Lisp_Object saved_object;
17620 struct face *face;
17622 saved_object = it->object;
17623 saved_pos = it->position;
17625 it->what = IT_CHARACTER;
17626 memset (&it->position, 0, sizeof it->position);
17627 it->object = make_number (0);
17628 it->c = it->char_to_display = ' ';
17629 it->len = 1;
17631 if (default_face_p)
17632 it->face_id = DEFAULT_FACE_ID;
17633 else if (it->face_before_selective_p)
17634 it->face_id = it->saved_face_id;
17635 face = FACE_FROM_ID (it->f, it->face_id);
17636 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
17638 PRODUCE_GLYPHS (it);
17640 it->override_ascent = -1;
17641 it->constrain_row_ascent_descent_p = 0;
17642 it->current_x = saved_x;
17643 it->object = saved_object;
17644 it->position = saved_pos;
17645 it->what = saved_what;
17646 it->face_id = saved_face_id;
17647 it->len = saved_len;
17648 it->c = saved_c;
17649 it->char_to_display = saved_char_to_display;
17650 return 1;
17654 return 0;
17658 /* Extend the face of the last glyph in the text area of IT->glyph_row
17659 to the end of the display line. Called from display_line. If the
17660 glyph row is empty, add a space glyph to it so that we know the
17661 face to draw. Set the glyph row flag fill_line_p. If the glyph
17662 row is R2L, prepend a stretch glyph to cover the empty space to the
17663 left of the leftmost glyph. */
17665 static void
17666 extend_face_to_end_of_line (struct it *it)
17668 struct face *face;
17669 struct frame *f = it->f;
17671 /* If line is already filled, do nothing. Non window-system frames
17672 get a grace of one more ``pixel'' because their characters are
17673 1-``pixel'' wide, so they hit the equality too early. This grace
17674 is needed only for R2L rows that are not continued, to produce
17675 one extra blank where we could display the cursor. */
17676 if (it->current_x >= it->last_visible_x
17677 + (!FRAME_WINDOW_P (f)
17678 && it->glyph_row->reversed_p
17679 && !it->glyph_row->continued_p))
17680 return;
17682 /* Face extension extends the background and box of IT->face_id
17683 to the end of the line. If the background equals the background
17684 of the frame, we don't have to do anything. */
17685 if (it->face_before_selective_p)
17686 face = FACE_FROM_ID (f, it->saved_face_id);
17687 else
17688 face = FACE_FROM_ID (f, it->face_id);
17690 if (FRAME_WINDOW_P (f)
17691 && it->glyph_row->displays_text_p
17692 && face->box == FACE_NO_BOX
17693 && face->background == FRAME_BACKGROUND_PIXEL (f)
17694 && !face->stipple
17695 && !it->glyph_row->reversed_p)
17696 return;
17698 /* Set the glyph row flag indicating that the face of the last glyph
17699 in the text area has to be drawn to the end of the text area. */
17700 it->glyph_row->fill_line_p = 1;
17702 /* If current character of IT is not ASCII, make sure we have the
17703 ASCII face. This will be automatically undone the next time
17704 get_next_display_element returns a multibyte character. Note
17705 that the character will always be single byte in unibyte
17706 text. */
17707 if (!ASCII_CHAR_P (it->c))
17709 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
17712 if (FRAME_WINDOW_P (f))
17714 /* If the row is empty, add a space with the current face of IT,
17715 so that we know which face to draw. */
17716 if (it->glyph_row->used[TEXT_AREA] == 0)
17718 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
17719 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
17720 it->glyph_row->used[TEXT_AREA] = 1;
17722 #ifdef HAVE_WINDOW_SYSTEM
17723 if (it->glyph_row->reversed_p)
17725 /* Prepend a stretch glyph to the row, such that the
17726 rightmost glyph will be drawn flushed all the way to the
17727 right margin of the window. The stretch glyph that will
17728 occupy the empty space, if any, to the left of the
17729 glyphs. */
17730 struct font *font = face->font ? face->font : FRAME_FONT (f);
17731 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
17732 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
17733 struct glyph *g;
17734 int row_width, stretch_ascent, stretch_width;
17735 struct text_pos saved_pos;
17736 int saved_face_id, saved_avoid_cursor;
17738 for (row_width = 0, g = row_start; g < row_end; g++)
17739 row_width += g->pixel_width;
17740 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
17741 if (stretch_width > 0)
17743 stretch_ascent =
17744 (((it->ascent + it->descent)
17745 * FONT_BASE (font)) / FONT_HEIGHT (font));
17746 saved_pos = it->position;
17747 memset (&it->position, 0, sizeof it->position);
17748 saved_avoid_cursor = it->avoid_cursor_p;
17749 it->avoid_cursor_p = 1;
17750 saved_face_id = it->face_id;
17751 /* The last row's stretch glyph should get the default
17752 face, to avoid painting the rest of the window with
17753 the region face, if the region ends at ZV. */
17754 if (it->glyph_row->ends_at_zv_p)
17755 it->face_id = DEFAULT_FACE_ID;
17756 else
17757 it->face_id = face->id;
17758 append_stretch_glyph (it, make_number (0), stretch_width,
17759 it->ascent + it->descent, stretch_ascent);
17760 it->position = saved_pos;
17761 it->avoid_cursor_p = saved_avoid_cursor;
17762 it->face_id = saved_face_id;
17765 #endif /* HAVE_WINDOW_SYSTEM */
17767 else
17769 /* Save some values that must not be changed. */
17770 int saved_x = it->current_x;
17771 struct text_pos saved_pos;
17772 Lisp_Object saved_object;
17773 enum display_element_type saved_what = it->what;
17774 int saved_face_id = it->face_id;
17776 saved_object = it->object;
17777 saved_pos = it->position;
17779 it->what = IT_CHARACTER;
17780 memset (&it->position, 0, sizeof it->position);
17781 it->object = make_number (0);
17782 it->c = it->char_to_display = ' ';
17783 it->len = 1;
17784 /* The last row's blank glyphs should get the default face, to
17785 avoid painting the rest of the window with the region face,
17786 if the region ends at ZV. */
17787 if (it->glyph_row->ends_at_zv_p)
17788 it->face_id = DEFAULT_FACE_ID;
17789 else
17790 it->face_id = face->id;
17792 PRODUCE_GLYPHS (it);
17794 while (it->current_x <= it->last_visible_x)
17795 PRODUCE_GLYPHS (it);
17797 /* Don't count these blanks really. It would let us insert a left
17798 truncation glyph below and make us set the cursor on them, maybe. */
17799 it->current_x = saved_x;
17800 it->object = saved_object;
17801 it->position = saved_pos;
17802 it->what = saved_what;
17803 it->face_id = saved_face_id;
17808 /* Value is non-zero if text starting at CHARPOS in current_buffer is
17809 trailing whitespace. */
17811 static int
17812 trailing_whitespace_p (EMACS_INT charpos)
17814 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
17815 int c = 0;
17817 while (bytepos < ZV_BYTE
17818 && (c = FETCH_CHAR (bytepos),
17819 c == ' ' || c == '\t'))
17820 ++bytepos;
17822 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
17824 if (bytepos != PT_BYTE)
17825 return 1;
17827 return 0;
17831 /* Highlight trailing whitespace, if any, in ROW. */
17833 static void
17834 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
17836 int used = row->used[TEXT_AREA];
17838 if (used)
17840 struct glyph *start = row->glyphs[TEXT_AREA];
17841 struct glyph *glyph = start + used - 1;
17843 if (row->reversed_p)
17845 /* Right-to-left rows need to be processed in the opposite
17846 direction, so swap the edge pointers. */
17847 glyph = start;
17848 start = row->glyphs[TEXT_AREA] + used - 1;
17851 /* Skip over glyphs inserted to display the cursor at the
17852 end of a line, for extending the face of the last glyph
17853 to the end of the line on terminals, and for truncation
17854 and continuation glyphs. */
17855 if (!row->reversed_p)
17857 while (glyph >= start
17858 && glyph->type == CHAR_GLYPH
17859 && INTEGERP (glyph->object))
17860 --glyph;
17862 else
17864 while (glyph <= start
17865 && glyph->type == CHAR_GLYPH
17866 && INTEGERP (glyph->object))
17867 ++glyph;
17870 /* If last glyph is a space or stretch, and it's trailing
17871 whitespace, set the face of all trailing whitespace glyphs in
17872 IT->glyph_row to `trailing-whitespace'. */
17873 if ((row->reversed_p ? glyph <= start : glyph >= start)
17874 && BUFFERP (glyph->object)
17875 && (glyph->type == STRETCH_GLYPH
17876 || (glyph->type == CHAR_GLYPH
17877 && glyph->u.ch == ' '))
17878 && trailing_whitespace_p (glyph->charpos))
17880 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
17881 if (face_id < 0)
17882 return;
17884 if (!row->reversed_p)
17886 while (glyph >= start
17887 && BUFFERP (glyph->object)
17888 && (glyph->type == STRETCH_GLYPH
17889 || (glyph->type == CHAR_GLYPH
17890 && glyph->u.ch == ' ')))
17891 (glyph--)->face_id = face_id;
17893 else
17895 while (glyph <= start
17896 && BUFFERP (glyph->object)
17897 && (glyph->type == STRETCH_GLYPH
17898 || (glyph->type == CHAR_GLYPH
17899 && glyph->u.ch == ' ')))
17900 (glyph++)->face_id = face_id;
17907 /* Value is non-zero if glyph row ROW should be
17908 used to hold the cursor. */
17910 static int
17911 cursor_row_p (struct glyph_row *row)
17913 int result = 1;
17915 if (PT == CHARPOS (row->end.pos))
17917 /* Suppose the row ends on a string.
17918 Unless the row is continued, that means it ends on a newline
17919 in the string. If it's anything other than a display string
17920 (e.g. a before-string from an overlay), we don't want the
17921 cursor there. (This heuristic seems to give the optimal
17922 behavior for the various types of multi-line strings.) */
17923 if (CHARPOS (row->end.string_pos) >= 0)
17925 if (row->continued_p)
17926 result = 1;
17927 else
17929 /* Check for `display' property. */
17930 struct glyph *beg = row->glyphs[TEXT_AREA];
17931 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
17932 struct glyph *glyph;
17934 result = 0;
17935 for (glyph = end; glyph >= beg; --glyph)
17936 if (STRINGP (glyph->object))
17938 Lisp_Object prop
17939 = Fget_char_property (make_number (PT),
17940 Qdisplay, Qnil);
17941 result =
17942 (!NILP (prop)
17943 && display_prop_string_p (prop, glyph->object));
17944 break;
17948 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
17950 /* If the row ends in middle of a real character,
17951 and the line is continued, we want the cursor here.
17952 That's because CHARPOS (ROW->end.pos) would equal
17953 PT if PT is before the character. */
17954 if (!row->ends_in_ellipsis_p)
17955 result = row->continued_p;
17956 else
17957 /* If the row ends in an ellipsis, then
17958 CHARPOS (ROW->end.pos) will equal point after the
17959 invisible text. We want that position to be displayed
17960 after the ellipsis. */
17961 result = 0;
17963 /* If the row ends at ZV, display the cursor at the end of that
17964 row instead of at the start of the row below. */
17965 else if (row->ends_at_zv_p)
17966 result = 1;
17967 else
17968 result = 0;
17971 return result;
17976 /* Push the display property PROP so that it will be rendered at the
17977 current position in IT. Return 1 if PROP was successfully pushed,
17978 0 otherwise. */
17980 static int
17981 push_display_prop (struct it *it, Lisp_Object prop)
17983 xassert (it->method == GET_FROM_BUFFER);
17985 push_it (it, NULL);
17987 if (STRINGP (prop))
17989 if (SCHARS (prop) == 0)
17991 pop_it (it);
17992 return 0;
17995 it->string = prop;
17996 it->multibyte_p = STRING_MULTIBYTE (it->string);
17997 it->current.overlay_string_index = -1;
17998 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
17999 it->end_charpos = it->string_nchars = SCHARS (it->string);
18000 it->method = GET_FROM_STRING;
18001 it->stop_charpos = 0;
18002 it->prev_stop = 0;
18003 it->base_level_stop = 0;
18004 it->string_from_display_prop_p = 1;
18005 it->from_disp_prop_p = 1;
18007 /* Force paragraph direction to be that of the parent
18008 buffer. */
18009 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18010 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18011 else
18012 it->paragraph_embedding = L2R;
18014 /* Set up the bidi iterator for this display string. */
18015 if (it->bidi_p)
18017 it->bidi_it.string.lstring = it->string;
18018 it->bidi_it.string.s = NULL;
18019 it->bidi_it.string.schars = it->end_charpos;
18020 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18021 it->bidi_it.string.from_disp_str = 1;
18022 it->bidi_it.string.unibyte = !it->multibyte_p;
18023 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18026 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18028 it->method = GET_FROM_STRETCH;
18029 it->object = prop;
18031 #ifdef HAVE_WINDOW_SYSTEM
18032 else if (IMAGEP (prop))
18034 it->what = IT_IMAGE;
18035 it->image_id = lookup_image (it->f, prop);
18036 it->method = GET_FROM_IMAGE;
18038 #endif /* HAVE_WINDOW_SYSTEM */
18039 else
18041 pop_it (it); /* bogus display property, give up */
18042 return 0;
18045 return 1;
18048 /* Return the character-property PROP at the current position in IT. */
18050 static Lisp_Object
18051 get_it_property (struct it *it, Lisp_Object prop)
18053 Lisp_Object position;
18055 if (STRINGP (it->object))
18056 position = make_number (IT_STRING_CHARPOS (*it));
18057 else if (BUFFERP (it->object))
18058 position = make_number (IT_CHARPOS (*it));
18059 else
18060 return Qnil;
18062 return Fget_char_property (position, prop, it->object);
18065 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18067 static void
18068 handle_line_prefix (struct it *it)
18070 Lisp_Object prefix;
18072 if (it->continuation_lines_width > 0)
18074 prefix = get_it_property (it, Qwrap_prefix);
18075 if (NILP (prefix))
18076 prefix = Vwrap_prefix;
18078 else
18080 prefix = get_it_property (it, Qline_prefix);
18081 if (NILP (prefix))
18082 prefix = Vline_prefix;
18084 if (! NILP (prefix) && push_display_prop (it, prefix))
18086 /* If the prefix is wider than the window, and we try to wrap
18087 it, it would acquire its own wrap prefix, and so on till the
18088 iterator stack overflows. So, don't wrap the prefix. */
18089 it->line_wrap = TRUNCATE;
18090 it->avoid_cursor_p = 1;
18096 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18097 only for R2L lines from display_line and display_string, when they
18098 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18099 the line/string needs to be continued on the next glyph row. */
18100 static void
18101 unproduce_glyphs (struct it *it, int n)
18103 struct glyph *glyph, *end;
18105 xassert (it->glyph_row);
18106 xassert (it->glyph_row->reversed_p);
18107 xassert (it->area == TEXT_AREA);
18108 xassert (n <= it->glyph_row->used[TEXT_AREA]);
18110 if (n > it->glyph_row->used[TEXT_AREA])
18111 n = it->glyph_row->used[TEXT_AREA];
18112 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18113 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18114 for ( ; glyph < end; glyph++)
18115 glyph[-n] = *glyph;
18118 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18119 and ROW->maxpos. */
18120 static void
18121 find_row_edges (struct it *it, struct glyph_row *row,
18122 EMACS_INT min_pos, EMACS_INT min_bpos,
18123 EMACS_INT max_pos, EMACS_INT max_bpos)
18125 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18126 lines' rows is implemented for bidi-reordered rows. */
18128 /* ROW->minpos is the value of min_pos, the minimal buffer position
18129 we have in ROW, or ROW->start.pos if that is smaller. */
18130 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18131 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18132 else
18133 /* We didn't find buffer positions smaller than ROW->start, or
18134 didn't find _any_ valid buffer positions in any of the glyphs,
18135 so we must trust the iterator's computed positions. */
18136 row->minpos = row->start.pos;
18137 if (max_pos <= 0)
18139 max_pos = CHARPOS (it->current.pos);
18140 max_bpos = BYTEPOS (it->current.pos);
18143 /* Here are the various use-cases for ending the row, and the
18144 corresponding values for ROW->maxpos:
18146 Line ends in a newline from buffer eol_pos + 1
18147 Line is continued from buffer max_pos + 1
18148 Line is truncated on right it->current.pos
18149 Line ends in a newline from string max_pos
18150 Line is continued from string max_pos
18151 Line is continued from display vector max_pos
18152 Line is entirely from a string min_pos == max_pos
18153 Line is entirely from a display vector min_pos == max_pos
18154 Line that ends at ZV ZV
18156 If you discover other use-cases, please add them here as
18157 appropriate. */
18158 if (row->ends_at_zv_p)
18159 row->maxpos = it->current.pos;
18160 else if (row->used[TEXT_AREA])
18162 if (row->ends_in_newline_from_string_p)
18163 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18164 else if (CHARPOS (it->eol_pos) > 0)
18165 SET_TEXT_POS (row->maxpos,
18166 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
18167 else if (row->continued_p)
18169 /* If max_pos is different from IT's current position, it
18170 means IT->method does not belong to the display element
18171 at max_pos. However, it also means that the display
18172 element at max_pos was displayed in its entirety on this
18173 line, which is equivalent to saying that the next line
18174 starts at the next buffer position. */
18175 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
18176 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18177 else
18179 INC_BOTH (max_pos, max_bpos);
18180 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18183 else if (row->truncated_on_right_p)
18184 /* display_line already called reseat_at_next_visible_line_start,
18185 which puts the iterator at the beginning of the next line, in
18186 the logical order. */
18187 row->maxpos = it->current.pos;
18188 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
18189 /* A line that is entirely from a string/image/stretch... */
18190 row->maxpos = row->minpos;
18191 else
18192 abort ();
18194 else
18195 row->maxpos = it->current.pos;
18198 /* Construct the glyph row IT->glyph_row in the desired matrix of
18199 IT->w from text at the current position of IT. See dispextern.h
18200 for an overview of struct it. Value is non-zero if
18201 IT->glyph_row displays text, as opposed to a line displaying ZV
18202 only. */
18204 static int
18205 display_line (struct it *it)
18207 struct glyph_row *row = it->glyph_row;
18208 Lisp_Object overlay_arrow_string;
18209 struct it wrap_it;
18210 void *wrap_data = NULL;
18211 int may_wrap = 0, wrap_x IF_LINT (= 0);
18212 int wrap_row_used = -1;
18213 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
18214 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
18215 int wrap_row_extra_line_spacing IF_LINT (= 0);
18216 EMACS_INT wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
18217 EMACS_INT wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
18218 int cvpos;
18219 EMACS_INT min_pos = ZV + 1, max_pos = 0;
18220 EMACS_INT min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
18222 /* We always start displaying at hpos zero even if hscrolled. */
18223 xassert (it->hpos == 0 && it->current_x == 0);
18225 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
18226 >= it->w->desired_matrix->nrows)
18228 it->w->nrows_scale_factor++;
18229 fonts_changed_p = 1;
18230 return 0;
18233 /* Is IT->w showing the region? */
18234 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
18236 /* Clear the result glyph row and enable it. */
18237 prepare_desired_row (row);
18239 row->y = it->current_y;
18240 row->start = it->start;
18241 row->continuation_lines_width = it->continuation_lines_width;
18242 row->displays_text_p = 1;
18243 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
18244 it->starts_in_middle_of_char_p = 0;
18246 /* Arrange the overlays nicely for our purposes. Usually, we call
18247 display_line on only one line at a time, in which case this
18248 can't really hurt too much, or we call it on lines which appear
18249 one after another in the buffer, in which case all calls to
18250 recenter_overlay_lists but the first will be pretty cheap. */
18251 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
18253 /* Move over display elements that are not visible because we are
18254 hscrolled. This may stop at an x-position < IT->first_visible_x
18255 if the first glyph is partially visible or if we hit a line end. */
18256 if (it->current_x < it->first_visible_x)
18258 this_line_min_pos = row->start.pos;
18259 move_it_in_display_line_to (it, ZV, it->first_visible_x,
18260 MOVE_TO_POS | MOVE_TO_X);
18261 /* Record the smallest positions seen while we moved over
18262 display elements that are not visible. This is needed by
18263 redisplay_internal for optimizing the case where the cursor
18264 stays inside the same line. The rest of this function only
18265 considers positions that are actually displayed, so
18266 RECORD_MAX_MIN_POS will not otherwise record positions that
18267 are hscrolled to the left of the left edge of the window. */
18268 min_pos = CHARPOS (this_line_min_pos);
18269 min_bpos = BYTEPOS (this_line_min_pos);
18271 else
18273 /* We only do this when not calling `move_it_in_display_line_to'
18274 above, because move_it_in_display_line_to calls
18275 handle_line_prefix itself. */
18276 handle_line_prefix (it);
18279 /* Get the initial row height. This is either the height of the
18280 text hscrolled, if there is any, or zero. */
18281 row->ascent = it->max_ascent;
18282 row->height = it->max_ascent + it->max_descent;
18283 row->phys_ascent = it->max_phys_ascent;
18284 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18285 row->extra_line_spacing = it->max_extra_line_spacing;
18287 /* Utility macro to record max and min buffer positions seen until now. */
18288 #define RECORD_MAX_MIN_POS(IT) \
18289 do \
18291 if (IT_CHARPOS (*(IT)) < min_pos) \
18293 min_pos = IT_CHARPOS (*(IT)); \
18294 min_bpos = IT_BYTEPOS (*(IT)); \
18296 if (IT_CHARPOS (*(IT)) > max_pos) \
18298 max_pos = IT_CHARPOS (*(IT)); \
18299 max_bpos = IT_BYTEPOS (*(IT)); \
18302 while (0)
18304 /* Loop generating characters. The loop is left with IT on the next
18305 character to display. */
18306 while (1)
18308 int n_glyphs_before, hpos_before, x_before;
18309 int x, nglyphs;
18310 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
18312 /* Retrieve the next thing to display. Value is zero if end of
18313 buffer reached. */
18314 if (!get_next_display_element (it))
18316 /* Maybe add a space at the end of this line that is used to
18317 display the cursor there under X. Set the charpos of the
18318 first glyph of blank lines not corresponding to any text
18319 to -1. */
18320 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18321 row->exact_window_width_line_p = 1;
18322 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
18323 || row->used[TEXT_AREA] == 0)
18325 row->glyphs[TEXT_AREA]->charpos = -1;
18326 row->displays_text_p = 0;
18328 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
18329 && (!MINI_WINDOW_P (it->w)
18330 || (minibuf_level && EQ (it->window, minibuf_window))))
18331 row->indicate_empty_line_p = 1;
18334 it->continuation_lines_width = 0;
18335 row->ends_at_zv_p = 1;
18336 /* A row that displays right-to-left text must always have
18337 its last face extended all the way to the end of line,
18338 even if this row ends in ZV, because we still write to
18339 the screen left to right. */
18340 if (row->reversed_p)
18341 extend_face_to_end_of_line (it);
18342 break;
18345 /* Now, get the metrics of what we want to display. This also
18346 generates glyphs in `row' (which is IT->glyph_row). */
18347 n_glyphs_before = row->used[TEXT_AREA];
18348 x = it->current_x;
18350 /* Remember the line height so far in case the next element doesn't
18351 fit on the line. */
18352 if (it->line_wrap != TRUNCATE)
18354 ascent = it->max_ascent;
18355 descent = it->max_descent;
18356 phys_ascent = it->max_phys_ascent;
18357 phys_descent = it->max_phys_descent;
18359 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
18361 if (IT_DISPLAYING_WHITESPACE (it))
18362 may_wrap = 1;
18363 else if (may_wrap)
18365 SAVE_IT (wrap_it, *it, wrap_data);
18366 wrap_x = x;
18367 wrap_row_used = row->used[TEXT_AREA];
18368 wrap_row_ascent = row->ascent;
18369 wrap_row_height = row->height;
18370 wrap_row_phys_ascent = row->phys_ascent;
18371 wrap_row_phys_height = row->phys_height;
18372 wrap_row_extra_line_spacing = row->extra_line_spacing;
18373 wrap_row_min_pos = min_pos;
18374 wrap_row_min_bpos = min_bpos;
18375 wrap_row_max_pos = max_pos;
18376 wrap_row_max_bpos = max_bpos;
18377 may_wrap = 0;
18382 PRODUCE_GLYPHS (it);
18384 /* If this display element was in marginal areas, continue with
18385 the next one. */
18386 if (it->area != TEXT_AREA)
18388 row->ascent = max (row->ascent, it->max_ascent);
18389 row->height = max (row->height, it->max_ascent + it->max_descent);
18390 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18391 row->phys_height = max (row->phys_height,
18392 it->max_phys_ascent + it->max_phys_descent);
18393 row->extra_line_spacing = max (row->extra_line_spacing,
18394 it->max_extra_line_spacing);
18395 set_iterator_to_next (it, 1);
18396 continue;
18399 /* Does the display element fit on the line? If we truncate
18400 lines, we should draw past the right edge of the window. If
18401 we don't truncate, we want to stop so that we can display the
18402 continuation glyph before the right margin. If lines are
18403 continued, there are two possible strategies for characters
18404 resulting in more than 1 glyph (e.g. tabs): Display as many
18405 glyphs as possible in this line and leave the rest for the
18406 continuation line, or display the whole element in the next
18407 line. Original redisplay did the former, so we do it also. */
18408 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
18409 hpos_before = it->hpos;
18410 x_before = x;
18412 if (/* Not a newline. */
18413 nglyphs > 0
18414 /* Glyphs produced fit entirely in the line. */
18415 && it->current_x < it->last_visible_x)
18417 it->hpos += nglyphs;
18418 row->ascent = max (row->ascent, it->max_ascent);
18419 row->height = max (row->height, it->max_ascent + it->max_descent);
18420 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18421 row->phys_height = max (row->phys_height,
18422 it->max_phys_ascent + it->max_phys_descent);
18423 row->extra_line_spacing = max (row->extra_line_spacing,
18424 it->max_extra_line_spacing);
18425 if (it->current_x - it->pixel_width < it->first_visible_x)
18426 row->x = x - it->first_visible_x;
18427 /* Record the maximum and minimum buffer positions seen so
18428 far in glyphs that will be displayed by this row. */
18429 if (it->bidi_p)
18430 RECORD_MAX_MIN_POS (it);
18432 else
18434 int i, new_x;
18435 struct glyph *glyph;
18437 for (i = 0; i < nglyphs; ++i, x = new_x)
18439 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18440 new_x = x + glyph->pixel_width;
18442 if (/* Lines are continued. */
18443 it->line_wrap != TRUNCATE
18444 && (/* Glyph doesn't fit on the line. */
18445 new_x > it->last_visible_x
18446 /* Or it fits exactly on a window system frame. */
18447 || (new_x == it->last_visible_x
18448 && FRAME_WINDOW_P (it->f))))
18450 /* End of a continued line. */
18452 if (it->hpos == 0
18453 || (new_x == it->last_visible_x
18454 && FRAME_WINDOW_P (it->f)))
18456 /* Current glyph is the only one on the line or
18457 fits exactly on the line. We must continue
18458 the line because we can't draw the cursor
18459 after the glyph. */
18460 row->continued_p = 1;
18461 it->current_x = new_x;
18462 it->continuation_lines_width += new_x;
18463 ++it->hpos;
18464 /* Record the maximum and minimum buffer
18465 positions seen so far in glyphs that will be
18466 displayed by this row. */
18467 if (it->bidi_p)
18468 RECORD_MAX_MIN_POS (it);
18469 if (i == nglyphs - 1)
18471 /* If line-wrap is on, check if a previous
18472 wrap point was found. */
18473 if (wrap_row_used > 0
18474 /* Even if there is a previous wrap
18475 point, continue the line here as
18476 usual, if (i) the previous character
18477 was a space or tab AND (ii) the
18478 current character is not. */
18479 && (!may_wrap
18480 || IT_DISPLAYING_WHITESPACE (it)))
18481 goto back_to_wrap;
18483 set_iterator_to_next (it, 1);
18484 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18486 if (!get_next_display_element (it))
18488 row->exact_window_width_line_p = 1;
18489 it->continuation_lines_width = 0;
18490 row->continued_p = 0;
18491 row->ends_at_zv_p = 1;
18493 else if (ITERATOR_AT_END_OF_LINE_P (it))
18495 row->continued_p = 0;
18496 row->exact_window_width_line_p = 1;
18501 else if (CHAR_GLYPH_PADDING_P (*glyph)
18502 && !FRAME_WINDOW_P (it->f))
18504 /* A padding glyph that doesn't fit on this line.
18505 This means the whole character doesn't fit
18506 on the line. */
18507 if (row->reversed_p)
18508 unproduce_glyphs (it, row->used[TEXT_AREA]
18509 - n_glyphs_before);
18510 row->used[TEXT_AREA] = n_glyphs_before;
18512 /* Fill the rest of the row with continuation
18513 glyphs like in 20.x. */
18514 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
18515 < row->glyphs[1 + TEXT_AREA])
18516 produce_special_glyphs (it, IT_CONTINUATION);
18518 row->continued_p = 1;
18519 it->current_x = x_before;
18520 it->continuation_lines_width += x_before;
18522 /* Restore the height to what it was before the
18523 element not fitting on the line. */
18524 it->max_ascent = ascent;
18525 it->max_descent = descent;
18526 it->max_phys_ascent = phys_ascent;
18527 it->max_phys_descent = phys_descent;
18529 else if (wrap_row_used > 0)
18531 back_to_wrap:
18532 if (row->reversed_p)
18533 unproduce_glyphs (it,
18534 row->used[TEXT_AREA] - wrap_row_used);
18535 RESTORE_IT (it, &wrap_it, wrap_data);
18536 it->continuation_lines_width += wrap_x;
18537 row->used[TEXT_AREA] = wrap_row_used;
18538 row->ascent = wrap_row_ascent;
18539 row->height = wrap_row_height;
18540 row->phys_ascent = wrap_row_phys_ascent;
18541 row->phys_height = wrap_row_phys_height;
18542 row->extra_line_spacing = wrap_row_extra_line_spacing;
18543 min_pos = wrap_row_min_pos;
18544 min_bpos = wrap_row_min_bpos;
18545 max_pos = wrap_row_max_pos;
18546 max_bpos = wrap_row_max_bpos;
18547 row->continued_p = 1;
18548 row->ends_at_zv_p = 0;
18549 row->exact_window_width_line_p = 0;
18550 it->continuation_lines_width += x;
18552 /* Make sure that a non-default face is extended
18553 up to the right margin of the window. */
18554 extend_face_to_end_of_line (it);
18556 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
18558 /* A TAB that extends past the right edge of the
18559 window. This produces a single glyph on
18560 window system frames. We leave the glyph in
18561 this row and let it fill the row, but don't
18562 consume the TAB. */
18563 it->continuation_lines_width += it->last_visible_x;
18564 row->ends_in_middle_of_char_p = 1;
18565 row->continued_p = 1;
18566 glyph->pixel_width = it->last_visible_x - x;
18567 it->starts_in_middle_of_char_p = 1;
18569 else
18571 /* Something other than a TAB that draws past
18572 the right edge of the window. Restore
18573 positions to values before the element. */
18574 if (row->reversed_p)
18575 unproduce_glyphs (it, row->used[TEXT_AREA]
18576 - (n_glyphs_before + i));
18577 row->used[TEXT_AREA] = n_glyphs_before + i;
18579 /* Display continuation glyphs. */
18580 if (!FRAME_WINDOW_P (it->f))
18581 produce_special_glyphs (it, IT_CONTINUATION);
18582 row->continued_p = 1;
18584 it->current_x = x_before;
18585 it->continuation_lines_width += x;
18586 extend_face_to_end_of_line (it);
18588 if (nglyphs > 1 && i > 0)
18590 row->ends_in_middle_of_char_p = 1;
18591 it->starts_in_middle_of_char_p = 1;
18594 /* Restore the height to what it was before the
18595 element not fitting on the line. */
18596 it->max_ascent = ascent;
18597 it->max_descent = descent;
18598 it->max_phys_ascent = phys_ascent;
18599 it->max_phys_descent = phys_descent;
18602 break;
18604 else if (new_x > it->first_visible_x)
18606 /* Increment number of glyphs actually displayed. */
18607 ++it->hpos;
18609 /* Record the maximum and minimum buffer positions
18610 seen so far in glyphs that will be displayed by
18611 this row. */
18612 if (it->bidi_p)
18613 RECORD_MAX_MIN_POS (it);
18615 if (x < it->first_visible_x)
18616 /* Glyph is partially visible, i.e. row starts at
18617 negative X position. */
18618 row->x = x - it->first_visible_x;
18620 else
18622 /* Glyph is completely off the left margin of the
18623 window. This should not happen because of the
18624 move_it_in_display_line at the start of this
18625 function, unless the text display area of the
18626 window is empty. */
18627 xassert (it->first_visible_x <= it->last_visible_x);
18631 row->ascent = max (row->ascent, it->max_ascent);
18632 row->height = max (row->height, it->max_ascent + it->max_descent);
18633 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18634 row->phys_height = max (row->phys_height,
18635 it->max_phys_ascent + it->max_phys_descent);
18636 row->extra_line_spacing = max (row->extra_line_spacing,
18637 it->max_extra_line_spacing);
18639 /* End of this display line if row is continued. */
18640 if (row->continued_p || row->ends_at_zv_p)
18641 break;
18644 at_end_of_line:
18645 /* Is this a line end? If yes, we're also done, after making
18646 sure that a non-default face is extended up to the right
18647 margin of the window. */
18648 if (ITERATOR_AT_END_OF_LINE_P (it))
18650 int used_before = row->used[TEXT_AREA];
18652 row->ends_in_newline_from_string_p = STRINGP (it->object);
18654 /* Add a space at the end of the line that is used to
18655 display the cursor there. */
18656 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18657 append_space_for_newline (it, 0);
18659 /* Extend the face to the end of the line. */
18660 extend_face_to_end_of_line (it);
18662 /* Make sure we have the position. */
18663 if (used_before == 0)
18664 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
18666 /* Record the position of the newline, for use in
18667 find_row_edges. */
18668 it->eol_pos = it->current.pos;
18670 /* Consume the line end. This skips over invisible lines. */
18671 set_iterator_to_next (it, 1);
18672 it->continuation_lines_width = 0;
18673 break;
18676 /* Proceed with next display element. Note that this skips
18677 over lines invisible because of selective display. */
18678 set_iterator_to_next (it, 1);
18680 /* If we truncate lines, we are done when the last displayed
18681 glyphs reach past the right margin of the window. */
18682 if (it->line_wrap == TRUNCATE
18683 && (FRAME_WINDOW_P (it->f)
18684 ? (it->current_x >= it->last_visible_x)
18685 : (it->current_x > it->last_visible_x)))
18687 /* Maybe add truncation glyphs. */
18688 if (!FRAME_WINDOW_P (it->f))
18690 int i, n;
18692 if (!row->reversed_p)
18694 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18695 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18696 break;
18698 else
18700 for (i = 0; i < row->used[TEXT_AREA]; i++)
18701 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18702 break;
18703 /* Remove any padding glyphs at the front of ROW, to
18704 make room for the truncation glyphs we will be
18705 adding below. The loop below always inserts at
18706 least one truncation glyph, so also remove the
18707 last glyph added to ROW. */
18708 unproduce_glyphs (it, i + 1);
18709 /* Adjust i for the loop below. */
18710 i = row->used[TEXT_AREA] - (i + 1);
18713 for (n = row->used[TEXT_AREA]; i < n; ++i)
18715 row->used[TEXT_AREA] = i;
18716 produce_special_glyphs (it, IT_TRUNCATION);
18719 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18721 /* Don't truncate if we can overflow newline into fringe. */
18722 if (!get_next_display_element (it))
18724 it->continuation_lines_width = 0;
18725 row->ends_at_zv_p = 1;
18726 row->exact_window_width_line_p = 1;
18727 break;
18729 if (ITERATOR_AT_END_OF_LINE_P (it))
18731 row->exact_window_width_line_p = 1;
18732 goto at_end_of_line;
18736 row->truncated_on_right_p = 1;
18737 it->continuation_lines_width = 0;
18738 reseat_at_next_visible_line_start (it, 0);
18739 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
18740 it->hpos = hpos_before;
18741 it->current_x = x_before;
18742 break;
18746 /* If line is not empty and hscrolled, maybe insert truncation glyphs
18747 at the left window margin. */
18748 if (it->first_visible_x
18749 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
18751 if (!FRAME_WINDOW_P (it->f))
18752 insert_left_trunc_glyphs (it);
18753 row->truncated_on_left_p = 1;
18756 /* Remember the position at which this line ends.
18758 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
18759 cannot be before the call to find_row_edges below, since that is
18760 where these positions are determined. */
18761 row->end = it->current;
18762 if (!it->bidi_p)
18764 row->minpos = row->start.pos;
18765 row->maxpos = row->end.pos;
18767 else
18769 /* ROW->minpos and ROW->maxpos must be the smallest and
18770 `1 + the largest' buffer positions in ROW. But if ROW was
18771 bidi-reordered, these two positions can be anywhere in the
18772 row, so we must determine them now. */
18773 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
18776 /* If the start of this line is the overlay arrow-position, then
18777 mark this glyph row as the one containing the overlay arrow.
18778 This is clearly a mess with variable size fonts. It would be
18779 better to let it be displayed like cursors under X. */
18780 if ((row->displays_text_p || !overlay_arrow_seen)
18781 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
18782 !NILP (overlay_arrow_string)))
18784 /* Overlay arrow in window redisplay is a fringe bitmap. */
18785 if (STRINGP (overlay_arrow_string))
18787 struct glyph_row *arrow_row
18788 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
18789 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
18790 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
18791 struct glyph *p = row->glyphs[TEXT_AREA];
18792 struct glyph *p2, *end;
18794 /* Copy the arrow glyphs. */
18795 while (glyph < arrow_end)
18796 *p++ = *glyph++;
18798 /* Throw away padding glyphs. */
18799 p2 = p;
18800 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18801 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
18802 ++p2;
18803 if (p2 > p)
18805 while (p2 < end)
18806 *p++ = *p2++;
18807 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
18810 else
18812 xassert (INTEGERP (overlay_arrow_string));
18813 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
18815 overlay_arrow_seen = 1;
18818 /* Compute pixel dimensions of this line. */
18819 compute_line_metrics (it);
18821 /* Record whether this row ends inside an ellipsis. */
18822 row->ends_in_ellipsis_p
18823 = (it->method == GET_FROM_DISPLAY_VECTOR
18824 && it->ellipsis_p);
18826 /* Save fringe bitmaps in this row. */
18827 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
18828 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
18829 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
18830 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
18832 it->left_user_fringe_bitmap = 0;
18833 it->left_user_fringe_face_id = 0;
18834 it->right_user_fringe_bitmap = 0;
18835 it->right_user_fringe_face_id = 0;
18837 /* Maybe set the cursor. */
18838 cvpos = it->w->cursor.vpos;
18839 if ((cvpos < 0
18840 /* In bidi-reordered rows, keep checking for proper cursor
18841 position even if one has been found already, because buffer
18842 positions in such rows change non-linearly with ROW->VPOS,
18843 when a line is continued. One exception: when we are at ZV,
18844 display cursor on the first suitable glyph row, since all
18845 the empty rows after that also have their position set to ZV. */
18846 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18847 lines' rows is implemented for bidi-reordered rows. */
18848 || (it->bidi_p
18849 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
18850 && PT >= MATRIX_ROW_START_CHARPOS (row)
18851 && PT <= MATRIX_ROW_END_CHARPOS (row)
18852 && cursor_row_p (row))
18853 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
18855 /* Highlight trailing whitespace. */
18856 if (!NILP (Vshow_trailing_whitespace))
18857 highlight_trailing_whitespace (it->f, it->glyph_row);
18859 /* Prepare for the next line. This line starts horizontally at (X
18860 HPOS) = (0 0). Vertical positions are incremented. As a
18861 convenience for the caller, IT->glyph_row is set to the next
18862 row to be used. */
18863 it->current_x = it->hpos = 0;
18864 it->current_y += row->height;
18865 SET_TEXT_POS (it->eol_pos, 0, 0);
18866 ++it->vpos;
18867 ++it->glyph_row;
18868 /* The next row should by default use the same value of the
18869 reversed_p flag as this one. set_iterator_to_next decides when
18870 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
18871 the flag accordingly. */
18872 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
18873 it->glyph_row->reversed_p = row->reversed_p;
18874 it->start = row->end;
18875 return row->displays_text_p;
18877 #undef RECORD_MAX_MIN_POS
18880 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
18881 Scurrent_bidi_paragraph_direction, 0, 1, 0,
18882 doc: /* Return paragraph direction at point in BUFFER.
18883 Value is either `left-to-right' or `right-to-left'.
18884 If BUFFER is omitted or nil, it defaults to the current buffer.
18886 Paragraph direction determines how the text in the paragraph is displayed.
18887 In left-to-right paragraphs, text begins at the left margin of the window
18888 and the reading direction is generally left to right. In right-to-left
18889 paragraphs, text begins at the right margin and is read from right to left.
18891 See also `bidi-paragraph-direction'. */)
18892 (Lisp_Object buffer)
18894 struct buffer *buf = current_buffer;
18895 struct buffer *old = buf;
18897 if (! NILP (buffer))
18899 CHECK_BUFFER (buffer);
18900 buf = XBUFFER (buffer);
18903 if (NILP (BVAR (buf, bidi_display_reordering)))
18904 return Qleft_to_right;
18905 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
18906 return BVAR (buf, bidi_paragraph_direction);
18907 else
18909 /* Determine the direction from buffer text. We could try to
18910 use current_matrix if it is up to date, but this seems fast
18911 enough as it is. */
18912 struct bidi_it itb;
18913 EMACS_INT pos = BUF_PT (buf);
18914 EMACS_INT bytepos = BUF_PT_BYTE (buf);
18915 int c;
18917 set_buffer_temp (buf);
18918 /* bidi_paragraph_init finds the base direction of the paragraph
18919 by searching forward from paragraph start. We need the base
18920 direction of the current or _previous_ paragraph, so we need
18921 to make sure we are within that paragraph. To that end, find
18922 the previous non-empty line. */
18923 if (pos >= ZV && pos > BEGV)
18925 pos--;
18926 bytepos = CHAR_TO_BYTE (pos);
18928 while ((c = FETCH_BYTE (bytepos)) == '\n'
18929 || c == ' ' || c == '\t' || c == '\f')
18931 if (bytepos <= BEGV_BYTE)
18932 break;
18933 bytepos--;
18934 pos--;
18936 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
18937 bytepos--;
18938 itb.charpos = pos;
18939 itb.bytepos = bytepos;
18940 itb.nchars = -1;
18941 itb.string.s = NULL;
18942 itb.string.lstring = Qnil;
18943 itb.frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ()); /* guesswork */
18944 itb.first_elt = 1;
18945 itb.separator_limit = -1;
18946 itb.paragraph_dir = NEUTRAL_DIR;
18948 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
18949 set_buffer_temp (old);
18950 switch (itb.paragraph_dir)
18952 case L2R:
18953 return Qleft_to_right;
18954 break;
18955 case R2L:
18956 return Qright_to_left;
18957 break;
18958 default:
18959 abort ();
18966 /***********************************************************************
18967 Menu Bar
18968 ***********************************************************************/
18970 /* Redisplay the menu bar in the frame for window W.
18972 The menu bar of X frames that don't have X toolkit support is
18973 displayed in a special window W->frame->menu_bar_window.
18975 The menu bar of terminal frames is treated specially as far as
18976 glyph matrices are concerned. Menu bar lines are not part of
18977 windows, so the update is done directly on the frame matrix rows
18978 for the menu bar. */
18980 static void
18981 display_menu_bar (struct window *w)
18983 struct frame *f = XFRAME (WINDOW_FRAME (w));
18984 struct it it;
18985 Lisp_Object items;
18986 int i;
18988 /* Don't do all this for graphical frames. */
18989 #ifdef HAVE_NTGUI
18990 if (FRAME_W32_P (f))
18991 return;
18992 #endif
18993 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
18994 if (FRAME_X_P (f))
18995 return;
18996 #endif
18998 #ifdef HAVE_NS
18999 if (FRAME_NS_P (f))
19000 return;
19001 #endif /* HAVE_NS */
19003 #ifdef USE_X_TOOLKIT
19004 xassert (!FRAME_WINDOW_P (f));
19005 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
19006 it.first_visible_x = 0;
19007 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19008 #else /* not USE_X_TOOLKIT */
19009 if (FRAME_WINDOW_P (f))
19011 /* Menu bar lines are displayed in the desired matrix of the
19012 dummy window menu_bar_window. */
19013 struct window *menu_w;
19014 xassert (WINDOWP (f->menu_bar_window));
19015 menu_w = XWINDOW (f->menu_bar_window);
19016 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
19017 MENU_FACE_ID);
19018 it.first_visible_x = 0;
19019 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19021 else
19023 /* This is a TTY frame, i.e. character hpos/vpos are used as
19024 pixel x/y. */
19025 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
19026 MENU_FACE_ID);
19027 it.first_visible_x = 0;
19028 it.last_visible_x = FRAME_COLS (f);
19030 #endif /* not USE_X_TOOLKIT */
19032 /* FIXME: This should be controlled by a user option. See the
19033 comments in redisplay_tool_bar and display_mode_line about
19034 this. */
19035 it.paragraph_embedding = L2R;
19037 if (! mode_line_inverse_video)
19038 /* Force the menu-bar to be displayed in the default face. */
19039 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19041 /* Clear all rows of the menu bar. */
19042 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
19044 struct glyph_row *row = it.glyph_row + i;
19045 clear_glyph_row (row);
19046 row->enabled_p = 1;
19047 row->full_width_p = 1;
19050 /* Display all items of the menu bar. */
19051 items = FRAME_MENU_BAR_ITEMS (it.f);
19052 for (i = 0; i < ASIZE (items); i += 4)
19054 Lisp_Object string;
19056 /* Stop at nil string. */
19057 string = AREF (items, i + 1);
19058 if (NILP (string))
19059 break;
19061 /* Remember where item was displayed. */
19062 ASET (items, i + 3, make_number (it.hpos));
19064 /* Display the item, pad with one space. */
19065 if (it.current_x < it.last_visible_x)
19066 display_string (NULL, string, Qnil, 0, 0, &it,
19067 SCHARS (string) + 1, 0, 0, -1);
19070 /* Fill out the line with spaces. */
19071 if (it.current_x < it.last_visible_x)
19072 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
19074 /* Compute the total height of the lines. */
19075 compute_line_metrics (&it);
19080 /***********************************************************************
19081 Mode Line
19082 ***********************************************************************/
19084 /* Redisplay mode lines in the window tree whose root is WINDOW. If
19085 FORCE is non-zero, redisplay mode lines unconditionally.
19086 Otherwise, redisplay only mode lines that are garbaged. Value is
19087 the number of windows whose mode lines were redisplayed. */
19089 static int
19090 redisplay_mode_lines (Lisp_Object window, int force)
19092 int nwindows = 0;
19094 while (!NILP (window))
19096 struct window *w = XWINDOW (window);
19098 if (WINDOWP (w->hchild))
19099 nwindows += redisplay_mode_lines (w->hchild, force);
19100 else if (WINDOWP (w->vchild))
19101 nwindows += redisplay_mode_lines (w->vchild, force);
19102 else if (force
19103 || FRAME_GARBAGED_P (XFRAME (w->frame))
19104 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
19106 struct text_pos lpoint;
19107 struct buffer *old = current_buffer;
19109 /* Set the window's buffer for the mode line display. */
19110 SET_TEXT_POS (lpoint, PT, PT_BYTE);
19111 set_buffer_internal_1 (XBUFFER (w->buffer));
19113 /* Point refers normally to the selected window. For any
19114 other window, set up appropriate value. */
19115 if (!EQ (window, selected_window))
19117 struct text_pos pt;
19119 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
19120 if (CHARPOS (pt) < BEGV)
19121 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
19122 else if (CHARPOS (pt) > (ZV - 1))
19123 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
19124 else
19125 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
19128 /* Display mode lines. */
19129 clear_glyph_matrix (w->desired_matrix);
19130 if (display_mode_lines (w))
19132 ++nwindows;
19133 w->must_be_updated_p = 1;
19136 /* Restore old settings. */
19137 set_buffer_internal_1 (old);
19138 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
19141 window = w->next;
19144 return nwindows;
19148 /* Display the mode and/or header line of window W. Value is the
19149 sum number of mode lines and header lines displayed. */
19151 static int
19152 display_mode_lines (struct window *w)
19154 Lisp_Object old_selected_window, old_selected_frame;
19155 int n = 0;
19157 old_selected_frame = selected_frame;
19158 selected_frame = w->frame;
19159 old_selected_window = selected_window;
19160 XSETWINDOW (selected_window, w);
19162 /* These will be set while the mode line specs are processed. */
19163 line_number_displayed = 0;
19164 w->column_number_displayed = Qnil;
19166 if (WINDOW_WANTS_MODELINE_P (w))
19168 struct window *sel_w = XWINDOW (old_selected_window);
19170 /* Select mode line face based on the real selected window. */
19171 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
19172 BVAR (current_buffer, mode_line_format));
19173 ++n;
19176 if (WINDOW_WANTS_HEADER_LINE_P (w))
19178 display_mode_line (w, HEADER_LINE_FACE_ID,
19179 BVAR (current_buffer, header_line_format));
19180 ++n;
19183 selected_frame = old_selected_frame;
19184 selected_window = old_selected_window;
19185 return n;
19189 /* Display mode or header line of window W. FACE_ID specifies which
19190 line to display; it is either MODE_LINE_FACE_ID or
19191 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
19192 display. Value is the pixel height of the mode/header line
19193 displayed. */
19195 static int
19196 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
19198 struct it it;
19199 struct face *face;
19200 int count = SPECPDL_INDEX ();
19202 init_iterator (&it, w, -1, -1, NULL, face_id);
19203 /* Don't extend on a previously drawn mode-line.
19204 This may happen if called from pos_visible_p. */
19205 it.glyph_row->enabled_p = 0;
19206 prepare_desired_row (it.glyph_row);
19208 it.glyph_row->mode_line_p = 1;
19210 if (! mode_line_inverse_video)
19211 /* Force the mode-line to be displayed in the default face. */
19212 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19214 /* FIXME: This should be controlled by a user option. But
19215 supporting such an option is not trivial, since the mode line is
19216 made up of many separate strings. */
19217 it.paragraph_embedding = L2R;
19219 record_unwind_protect (unwind_format_mode_line,
19220 format_mode_line_unwind_data (NULL, Qnil, 0));
19222 mode_line_target = MODE_LINE_DISPLAY;
19224 /* Temporarily make frame's keyboard the current kboard so that
19225 kboard-local variables in the mode_line_format will get the right
19226 values. */
19227 push_kboard (FRAME_KBOARD (it.f));
19228 record_unwind_save_match_data ();
19229 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19230 pop_kboard ();
19232 unbind_to (count, Qnil);
19234 /* Fill up with spaces. */
19235 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
19237 compute_line_metrics (&it);
19238 it.glyph_row->full_width_p = 1;
19239 it.glyph_row->continued_p = 0;
19240 it.glyph_row->truncated_on_left_p = 0;
19241 it.glyph_row->truncated_on_right_p = 0;
19243 /* Make a 3D mode-line have a shadow at its right end. */
19244 face = FACE_FROM_ID (it.f, face_id);
19245 extend_face_to_end_of_line (&it);
19246 if (face->box != FACE_NO_BOX)
19248 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
19249 + it.glyph_row->used[TEXT_AREA] - 1);
19250 last->right_box_line_p = 1;
19253 return it.glyph_row->height;
19256 /* Move element ELT in LIST to the front of LIST.
19257 Return the updated list. */
19259 static Lisp_Object
19260 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
19262 register Lisp_Object tail, prev;
19263 register Lisp_Object tem;
19265 tail = list;
19266 prev = Qnil;
19267 while (CONSP (tail))
19269 tem = XCAR (tail);
19271 if (EQ (elt, tem))
19273 /* Splice out the link TAIL. */
19274 if (NILP (prev))
19275 list = XCDR (tail);
19276 else
19277 Fsetcdr (prev, XCDR (tail));
19279 /* Now make it the first. */
19280 Fsetcdr (tail, list);
19281 return tail;
19283 else
19284 prev = tail;
19285 tail = XCDR (tail);
19286 QUIT;
19289 /* Not found--return unchanged LIST. */
19290 return list;
19293 /* Contribute ELT to the mode line for window IT->w. How it
19294 translates into text depends on its data type.
19296 IT describes the display environment in which we display, as usual.
19298 DEPTH is the depth in recursion. It is used to prevent
19299 infinite recursion here.
19301 FIELD_WIDTH is the number of characters the display of ELT should
19302 occupy in the mode line, and PRECISION is the maximum number of
19303 characters to display from ELT's representation. See
19304 display_string for details.
19306 Returns the hpos of the end of the text generated by ELT.
19308 PROPS is a property list to add to any string we encounter.
19310 If RISKY is nonzero, remove (disregard) any properties in any string
19311 we encounter, and ignore :eval and :propertize.
19313 The global variable `mode_line_target' determines whether the
19314 output is passed to `store_mode_line_noprop',
19315 `store_mode_line_string', or `display_string'. */
19317 static int
19318 display_mode_element (struct it *it, int depth, int field_width, int precision,
19319 Lisp_Object elt, Lisp_Object props, int risky)
19321 int n = 0, field, prec;
19322 int literal = 0;
19324 tail_recurse:
19325 if (depth > 100)
19326 elt = build_string ("*too-deep*");
19328 depth++;
19330 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
19332 case Lisp_String:
19334 /* A string: output it and check for %-constructs within it. */
19335 unsigned char c;
19336 EMACS_INT offset = 0;
19338 if (SCHARS (elt) > 0
19339 && (!NILP (props) || risky))
19341 Lisp_Object oprops, aelt;
19342 oprops = Ftext_properties_at (make_number (0), elt);
19344 /* If the starting string's properties are not what
19345 we want, translate the string. Also, if the string
19346 is risky, do that anyway. */
19348 if (NILP (Fequal (props, oprops)) || risky)
19350 /* If the starting string has properties,
19351 merge the specified ones onto the existing ones. */
19352 if (! NILP (oprops) && !risky)
19354 Lisp_Object tem;
19356 oprops = Fcopy_sequence (oprops);
19357 tem = props;
19358 while (CONSP (tem))
19360 oprops = Fplist_put (oprops, XCAR (tem),
19361 XCAR (XCDR (tem)));
19362 tem = XCDR (XCDR (tem));
19364 props = oprops;
19367 aelt = Fassoc (elt, mode_line_proptrans_alist);
19368 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
19370 /* AELT is what we want. Move it to the front
19371 without consing. */
19372 elt = XCAR (aelt);
19373 mode_line_proptrans_alist
19374 = move_elt_to_front (aelt, mode_line_proptrans_alist);
19376 else
19378 Lisp_Object tem;
19380 /* If AELT has the wrong props, it is useless.
19381 so get rid of it. */
19382 if (! NILP (aelt))
19383 mode_line_proptrans_alist
19384 = Fdelq (aelt, mode_line_proptrans_alist);
19386 elt = Fcopy_sequence (elt);
19387 Fset_text_properties (make_number (0), Flength (elt),
19388 props, elt);
19389 /* Add this item to mode_line_proptrans_alist. */
19390 mode_line_proptrans_alist
19391 = Fcons (Fcons (elt, props),
19392 mode_line_proptrans_alist);
19393 /* Truncate mode_line_proptrans_alist
19394 to at most 50 elements. */
19395 tem = Fnthcdr (make_number (50),
19396 mode_line_proptrans_alist);
19397 if (! NILP (tem))
19398 XSETCDR (tem, Qnil);
19403 offset = 0;
19405 if (literal)
19407 prec = precision - n;
19408 switch (mode_line_target)
19410 case MODE_LINE_NOPROP:
19411 case MODE_LINE_TITLE:
19412 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
19413 break;
19414 case MODE_LINE_STRING:
19415 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
19416 break;
19417 case MODE_LINE_DISPLAY:
19418 n += display_string (NULL, elt, Qnil, 0, 0, it,
19419 0, prec, 0, STRING_MULTIBYTE (elt));
19420 break;
19423 break;
19426 /* Handle the non-literal case. */
19428 while ((precision <= 0 || n < precision)
19429 && SREF (elt, offset) != 0
19430 && (mode_line_target != MODE_LINE_DISPLAY
19431 || it->current_x < it->last_visible_x))
19433 EMACS_INT last_offset = offset;
19435 /* Advance to end of string or next format specifier. */
19436 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
19439 if (offset - 1 != last_offset)
19441 EMACS_INT nchars, nbytes;
19443 /* Output to end of string or up to '%'. Field width
19444 is length of string. Don't output more than
19445 PRECISION allows us. */
19446 offset--;
19448 prec = c_string_width (SDATA (elt) + last_offset,
19449 offset - last_offset, precision - n,
19450 &nchars, &nbytes);
19452 switch (mode_line_target)
19454 case MODE_LINE_NOPROP:
19455 case MODE_LINE_TITLE:
19456 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
19457 break;
19458 case MODE_LINE_STRING:
19460 EMACS_INT bytepos = last_offset;
19461 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
19462 EMACS_INT endpos = (precision <= 0
19463 ? string_byte_to_char (elt, offset)
19464 : charpos + nchars);
19466 n += store_mode_line_string (NULL,
19467 Fsubstring (elt, make_number (charpos),
19468 make_number (endpos)),
19469 0, 0, 0, Qnil);
19471 break;
19472 case MODE_LINE_DISPLAY:
19474 EMACS_INT bytepos = last_offset;
19475 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
19477 if (precision <= 0)
19478 nchars = string_byte_to_char (elt, offset) - charpos;
19479 n += display_string (NULL, elt, Qnil, 0, charpos,
19480 it, 0, nchars, 0,
19481 STRING_MULTIBYTE (elt));
19483 break;
19486 else /* c == '%' */
19488 EMACS_INT percent_position = offset;
19490 /* Get the specified minimum width. Zero means
19491 don't pad. */
19492 field = 0;
19493 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
19494 field = field * 10 + c - '0';
19496 /* Don't pad beyond the total padding allowed. */
19497 if (field_width - n > 0 && field > field_width - n)
19498 field = field_width - n;
19500 /* Note that either PRECISION <= 0 or N < PRECISION. */
19501 prec = precision - n;
19503 if (c == 'M')
19504 n += display_mode_element (it, depth, field, prec,
19505 Vglobal_mode_string, props,
19506 risky);
19507 else if (c != 0)
19509 int multibyte;
19510 EMACS_INT bytepos, charpos;
19511 const char *spec;
19512 Lisp_Object string;
19514 bytepos = percent_position;
19515 charpos = (STRING_MULTIBYTE (elt)
19516 ? string_byte_to_char (elt, bytepos)
19517 : bytepos);
19518 spec = decode_mode_spec (it->w, c, field, &string);
19519 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
19521 switch (mode_line_target)
19523 case MODE_LINE_NOPROP:
19524 case MODE_LINE_TITLE:
19525 n += store_mode_line_noprop (spec, field, prec);
19526 break;
19527 case MODE_LINE_STRING:
19529 Lisp_Object tem = build_string (spec);
19530 props = Ftext_properties_at (make_number (charpos), elt);
19531 /* Should only keep face property in props */
19532 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
19534 break;
19535 case MODE_LINE_DISPLAY:
19537 int nglyphs_before, nwritten;
19539 nglyphs_before = it->glyph_row->used[TEXT_AREA];
19540 nwritten = display_string (spec, string, elt,
19541 charpos, 0, it,
19542 field, prec, 0,
19543 multibyte);
19545 /* Assign to the glyphs written above the
19546 string where the `%x' came from, position
19547 of the `%'. */
19548 if (nwritten > 0)
19550 struct glyph *glyph
19551 = (it->glyph_row->glyphs[TEXT_AREA]
19552 + nglyphs_before);
19553 int i;
19555 for (i = 0; i < nwritten; ++i)
19557 glyph[i].object = elt;
19558 glyph[i].charpos = charpos;
19561 n += nwritten;
19564 break;
19567 else /* c == 0 */
19568 break;
19572 break;
19574 case Lisp_Symbol:
19575 /* A symbol: process the value of the symbol recursively
19576 as if it appeared here directly. Avoid error if symbol void.
19577 Special case: if value of symbol is a string, output the string
19578 literally. */
19580 register Lisp_Object tem;
19582 /* If the variable is not marked as risky to set
19583 then its contents are risky to use. */
19584 if (NILP (Fget (elt, Qrisky_local_variable)))
19585 risky = 1;
19587 tem = Fboundp (elt);
19588 if (!NILP (tem))
19590 tem = Fsymbol_value (elt);
19591 /* If value is a string, output that string literally:
19592 don't check for % within it. */
19593 if (STRINGP (tem))
19594 literal = 1;
19596 if (!EQ (tem, elt))
19598 /* Give up right away for nil or t. */
19599 elt = tem;
19600 goto tail_recurse;
19604 break;
19606 case Lisp_Cons:
19608 register Lisp_Object car, tem;
19610 /* A cons cell: five distinct cases.
19611 If first element is :eval or :propertize, do something special.
19612 If first element is a string or a cons, process all the elements
19613 and effectively concatenate them.
19614 If first element is a negative number, truncate displaying cdr to
19615 at most that many characters. If positive, pad (with spaces)
19616 to at least that many characters.
19617 If first element is a symbol, process the cadr or caddr recursively
19618 according to whether the symbol's value is non-nil or nil. */
19619 car = XCAR (elt);
19620 if (EQ (car, QCeval))
19622 /* An element of the form (:eval FORM) means evaluate FORM
19623 and use the result as mode line elements. */
19625 if (risky)
19626 break;
19628 if (CONSP (XCDR (elt)))
19630 Lisp_Object spec;
19631 spec = safe_eval (XCAR (XCDR (elt)));
19632 n += display_mode_element (it, depth, field_width - n,
19633 precision - n, spec, props,
19634 risky);
19637 else if (EQ (car, QCpropertize))
19639 /* An element of the form (:propertize ELT PROPS...)
19640 means display ELT but applying properties PROPS. */
19642 if (risky)
19643 break;
19645 if (CONSP (XCDR (elt)))
19646 n += display_mode_element (it, depth, field_width - n,
19647 precision - n, XCAR (XCDR (elt)),
19648 XCDR (XCDR (elt)), risky);
19650 else if (SYMBOLP (car))
19652 tem = Fboundp (car);
19653 elt = XCDR (elt);
19654 if (!CONSP (elt))
19655 goto invalid;
19656 /* elt is now the cdr, and we know it is a cons cell.
19657 Use its car if CAR has a non-nil value. */
19658 if (!NILP (tem))
19660 tem = Fsymbol_value (car);
19661 if (!NILP (tem))
19663 elt = XCAR (elt);
19664 goto tail_recurse;
19667 /* Symbol's value is nil (or symbol is unbound)
19668 Get the cddr of the original list
19669 and if possible find the caddr and use that. */
19670 elt = XCDR (elt);
19671 if (NILP (elt))
19672 break;
19673 else if (!CONSP (elt))
19674 goto invalid;
19675 elt = XCAR (elt);
19676 goto tail_recurse;
19678 else if (INTEGERP (car))
19680 register int lim = XINT (car);
19681 elt = XCDR (elt);
19682 if (lim < 0)
19684 /* Negative int means reduce maximum width. */
19685 if (precision <= 0)
19686 precision = -lim;
19687 else
19688 precision = min (precision, -lim);
19690 else if (lim > 0)
19692 /* Padding specified. Don't let it be more than
19693 current maximum. */
19694 if (precision > 0)
19695 lim = min (precision, lim);
19697 /* If that's more padding than already wanted, queue it.
19698 But don't reduce padding already specified even if
19699 that is beyond the current truncation point. */
19700 field_width = max (lim, field_width);
19702 goto tail_recurse;
19704 else if (STRINGP (car) || CONSP (car))
19706 Lisp_Object halftail = elt;
19707 int len = 0;
19709 while (CONSP (elt)
19710 && (precision <= 0 || n < precision))
19712 n += display_mode_element (it, depth,
19713 /* Do padding only after the last
19714 element in the list. */
19715 (! CONSP (XCDR (elt))
19716 ? field_width - n
19717 : 0),
19718 precision - n, XCAR (elt),
19719 props, risky);
19720 elt = XCDR (elt);
19721 len++;
19722 if ((len & 1) == 0)
19723 halftail = XCDR (halftail);
19724 /* Check for cycle. */
19725 if (EQ (halftail, elt))
19726 break;
19730 break;
19732 default:
19733 invalid:
19734 elt = build_string ("*invalid*");
19735 goto tail_recurse;
19738 /* Pad to FIELD_WIDTH. */
19739 if (field_width > 0 && n < field_width)
19741 switch (mode_line_target)
19743 case MODE_LINE_NOPROP:
19744 case MODE_LINE_TITLE:
19745 n += store_mode_line_noprop ("", field_width - n, 0);
19746 break;
19747 case MODE_LINE_STRING:
19748 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
19749 break;
19750 case MODE_LINE_DISPLAY:
19751 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
19752 0, 0, 0);
19753 break;
19757 return n;
19760 /* Store a mode-line string element in mode_line_string_list.
19762 If STRING is non-null, display that C string. Otherwise, the Lisp
19763 string LISP_STRING is displayed.
19765 FIELD_WIDTH is the minimum number of output glyphs to produce.
19766 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19767 with spaces. FIELD_WIDTH <= 0 means don't pad.
19769 PRECISION is the maximum number of characters to output from
19770 STRING. PRECISION <= 0 means don't truncate the string.
19772 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
19773 properties to the string.
19775 PROPS are the properties to add to the string.
19776 The mode_line_string_face face property is always added to the string.
19779 static int
19780 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
19781 int field_width, int precision, Lisp_Object props)
19783 EMACS_INT len;
19784 int n = 0;
19786 if (string != NULL)
19788 len = strlen (string);
19789 if (precision > 0 && len > precision)
19790 len = precision;
19791 lisp_string = make_string (string, len);
19792 if (NILP (props))
19793 props = mode_line_string_face_prop;
19794 else if (!NILP (mode_line_string_face))
19796 Lisp_Object face = Fplist_get (props, Qface);
19797 props = Fcopy_sequence (props);
19798 if (NILP (face))
19799 face = mode_line_string_face;
19800 else
19801 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19802 props = Fplist_put (props, Qface, face);
19804 Fadd_text_properties (make_number (0), make_number (len),
19805 props, lisp_string);
19807 else
19809 len = XFASTINT (Flength (lisp_string));
19810 if (precision > 0 && len > precision)
19812 len = precision;
19813 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
19814 precision = -1;
19816 if (!NILP (mode_line_string_face))
19818 Lisp_Object face;
19819 if (NILP (props))
19820 props = Ftext_properties_at (make_number (0), lisp_string);
19821 face = Fplist_get (props, Qface);
19822 if (NILP (face))
19823 face = mode_line_string_face;
19824 else
19825 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19826 props = Fcons (Qface, Fcons (face, Qnil));
19827 if (copy_string)
19828 lisp_string = Fcopy_sequence (lisp_string);
19830 if (!NILP (props))
19831 Fadd_text_properties (make_number (0), make_number (len),
19832 props, lisp_string);
19835 if (len > 0)
19837 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19838 n += len;
19841 if (field_width > len)
19843 field_width -= len;
19844 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
19845 if (!NILP (props))
19846 Fadd_text_properties (make_number (0), make_number (field_width),
19847 props, lisp_string);
19848 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19849 n += field_width;
19852 return n;
19856 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
19857 1, 4, 0,
19858 doc: /* Format a string out of a mode line format specification.
19859 First arg FORMAT specifies the mode line format (see `mode-line-format'
19860 for details) to use.
19862 By default, the format is evaluated for the currently selected window.
19864 Optional second arg FACE specifies the face property to put on all
19865 characters for which no face is specified. The value nil means the
19866 default face. The value t means whatever face the window's mode line
19867 currently uses (either `mode-line' or `mode-line-inactive',
19868 depending on whether the window is the selected window or not).
19869 An integer value means the value string has no text
19870 properties.
19872 Optional third and fourth args WINDOW and BUFFER specify the window
19873 and buffer to use as the context for the formatting (defaults
19874 are the selected window and the WINDOW's buffer). */)
19875 (Lisp_Object format, Lisp_Object face,
19876 Lisp_Object window, Lisp_Object buffer)
19878 struct it it;
19879 int len;
19880 struct window *w;
19881 struct buffer *old_buffer = NULL;
19882 int face_id;
19883 int no_props = INTEGERP (face);
19884 int count = SPECPDL_INDEX ();
19885 Lisp_Object str;
19886 int string_start = 0;
19888 if (NILP (window))
19889 window = selected_window;
19890 CHECK_WINDOW (window);
19891 w = XWINDOW (window);
19893 if (NILP (buffer))
19894 buffer = w->buffer;
19895 CHECK_BUFFER (buffer);
19897 /* Make formatting the modeline a non-op when noninteractive, otherwise
19898 there will be problems later caused by a partially initialized frame. */
19899 if (NILP (format) || noninteractive)
19900 return empty_unibyte_string;
19902 if (no_props)
19903 face = Qnil;
19905 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
19906 : EQ (face, Qt) ? (EQ (window, selected_window)
19907 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
19908 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
19909 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
19910 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
19911 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
19912 : DEFAULT_FACE_ID;
19914 if (XBUFFER (buffer) != current_buffer)
19915 old_buffer = current_buffer;
19917 /* Save things including mode_line_proptrans_alist,
19918 and set that to nil so that we don't alter the outer value. */
19919 record_unwind_protect (unwind_format_mode_line,
19920 format_mode_line_unwind_data
19921 (old_buffer, selected_window, 1));
19922 mode_line_proptrans_alist = Qnil;
19924 Fselect_window (window, Qt);
19925 if (old_buffer)
19926 set_buffer_internal_1 (XBUFFER (buffer));
19928 init_iterator (&it, w, -1, -1, NULL, face_id);
19930 if (no_props)
19932 mode_line_target = MODE_LINE_NOPROP;
19933 mode_line_string_face_prop = Qnil;
19934 mode_line_string_list = Qnil;
19935 string_start = MODE_LINE_NOPROP_LEN (0);
19937 else
19939 mode_line_target = MODE_LINE_STRING;
19940 mode_line_string_list = Qnil;
19941 mode_line_string_face = face;
19942 mode_line_string_face_prop
19943 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
19946 push_kboard (FRAME_KBOARD (it.f));
19947 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19948 pop_kboard ();
19950 if (no_props)
19952 len = MODE_LINE_NOPROP_LEN (string_start);
19953 str = make_string (mode_line_noprop_buf + string_start, len);
19955 else
19957 mode_line_string_list = Fnreverse (mode_line_string_list);
19958 str = Fmapconcat (intern ("identity"), mode_line_string_list,
19959 empty_unibyte_string);
19962 unbind_to (count, Qnil);
19963 return str;
19966 /* Write a null-terminated, right justified decimal representation of
19967 the positive integer D to BUF using a minimal field width WIDTH. */
19969 static void
19970 pint2str (register char *buf, register int width, register EMACS_INT d)
19972 register char *p = buf;
19974 if (d <= 0)
19975 *p++ = '0';
19976 else
19978 while (d > 0)
19980 *p++ = d % 10 + '0';
19981 d /= 10;
19985 for (width -= (int) (p - buf); width > 0; --width)
19986 *p++ = ' ';
19987 *p-- = '\0';
19988 while (p > buf)
19990 d = *buf;
19991 *buf++ = *p;
19992 *p-- = d;
19996 /* Write a null-terminated, right justified decimal and "human
19997 readable" representation of the nonnegative integer D to BUF using
19998 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20000 static const char power_letter[] =
20002 0, /* no letter */
20003 'k', /* kilo */
20004 'M', /* mega */
20005 'G', /* giga */
20006 'T', /* tera */
20007 'P', /* peta */
20008 'E', /* exa */
20009 'Z', /* zetta */
20010 'Y' /* yotta */
20013 static void
20014 pint2hrstr (char *buf, int width, EMACS_INT d)
20016 /* We aim to represent the nonnegative integer D as
20017 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
20018 EMACS_INT quotient = d;
20019 int remainder = 0;
20020 /* -1 means: do not use TENTHS. */
20021 int tenths = -1;
20022 int exponent = 0;
20024 /* Length of QUOTIENT.TENTHS as a string. */
20025 int length;
20027 char * psuffix;
20028 char * p;
20030 if (1000 <= quotient)
20032 /* Scale to the appropriate EXPONENT. */
20035 remainder = quotient % 1000;
20036 quotient /= 1000;
20037 exponent++;
20039 while (1000 <= quotient);
20041 /* Round to nearest and decide whether to use TENTHS or not. */
20042 if (quotient <= 9)
20044 tenths = remainder / 100;
20045 if (50 <= remainder % 100)
20047 if (tenths < 9)
20048 tenths++;
20049 else
20051 quotient++;
20052 if (quotient == 10)
20053 tenths = -1;
20054 else
20055 tenths = 0;
20059 else
20060 if (500 <= remainder)
20062 if (quotient < 999)
20063 quotient++;
20064 else
20066 quotient = 1;
20067 exponent++;
20068 tenths = 0;
20073 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
20074 if (tenths == -1 && quotient <= 99)
20075 if (quotient <= 9)
20076 length = 1;
20077 else
20078 length = 2;
20079 else
20080 length = 3;
20081 p = psuffix = buf + max (width, length);
20083 /* Print EXPONENT. */
20084 *psuffix++ = power_letter[exponent];
20085 *psuffix = '\0';
20087 /* Print TENTHS. */
20088 if (tenths >= 0)
20090 *--p = '0' + tenths;
20091 *--p = '.';
20094 /* Print QUOTIENT. */
20097 int digit = quotient % 10;
20098 *--p = '0' + digit;
20100 while ((quotient /= 10) != 0);
20102 /* Print leading spaces. */
20103 while (buf < p)
20104 *--p = ' ';
20107 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
20108 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
20109 type of CODING_SYSTEM. Return updated pointer into BUF. */
20111 static unsigned char invalid_eol_type[] = "(*invalid*)";
20113 static char *
20114 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
20116 Lisp_Object val;
20117 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
20118 const unsigned char *eol_str;
20119 int eol_str_len;
20120 /* The EOL conversion we are using. */
20121 Lisp_Object eoltype;
20123 val = CODING_SYSTEM_SPEC (coding_system);
20124 eoltype = Qnil;
20126 if (!VECTORP (val)) /* Not yet decided. */
20128 if (multibyte)
20129 *buf++ = '-';
20130 if (eol_flag)
20131 eoltype = eol_mnemonic_undecided;
20132 /* Don't mention EOL conversion if it isn't decided. */
20134 else
20136 Lisp_Object attrs;
20137 Lisp_Object eolvalue;
20139 attrs = AREF (val, 0);
20140 eolvalue = AREF (val, 2);
20142 if (multibyte)
20143 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
20145 if (eol_flag)
20147 /* The EOL conversion that is normal on this system. */
20149 if (NILP (eolvalue)) /* Not yet decided. */
20150 eoltype = eol_mnemonic_undecided;
20151 else if (VECTORP (eolvalue)) /* Not yet decided. */
20152 eoltype = eol_mnemonic_undecided;
20153 else /* eolvalue is Qunix, Qdos, or Qmac. */
20154 eoltype = (EQ (eolvalue, Qunix)
20155 ? eol_mnemonic_unix
20156 : (EQ (eolvalue, Qdos) == 1
20157 ? eol_mnemonic_dos : eol_mnemonic_mac));
20161 if (eol_flag)
20163 /* Mention the EOL conversion if it is not the usual one. */
20164 if (STRINGP (eoltype))
20166 eol_str = SDATA (eoltype);
20167 eol_str_len = SBYTES (eoltype);
20169 else if (CHARACTERP (eoltype))
20171 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
20172 int c = XFASTINT (eoltype);
20173 eol_str_len = CHAR_STRING (c, tmp);
20174 eol_str = tmp;
20176 else
20178 eol_str = invalid_eol_type;
20179 eol_str_len = sizeof (invalid_eol_type) - 1;
20181 memcpy (buf, eol_str, eol_str_len);
20182 buf += eol_str_len;
20185 return buf;
20188 /* Return a string for the output of a mode line %-spec for window W,
20189 generated by character C. FIELD_WIDTH > 0 means pad the string
20190 returned with spaces to that value. Return a Lisp string in
20191 *STRING if the resulting string is taken from that Lisp string.
20193 Note we operate on the current buffer for most purposes,
20194 the exception being w->base_line_pos. */
20196 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
20198 static const char *
20199 decode_mode_spec (struct window *w, register int c, int field_width,
20200 Lisp_Object *string)
20202 Lisp_Object obj;
20203 struct frame *f = XFRAME (WINDOW_FRAME (w));
20204 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
20205 struct buffer *b = current_buffer;
20207 obj = Qnil;
20208 *string = Qnil;
20210 switch (c)
20212 case '*':
20213 if (!NILP (BVAR (b, read_only)))
20214 return "%";
20215 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20216 return "*";
20217 return "-";
20219 case '+':
20220 /* This differs from %* only for a modified read-only buffer. */
20221 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20222 return "*";
20223 if (!NILP (BVAR (b, read_only)))
20224 return "%";
20225 return "-";
20227 case '&':
20228 /* This differs from %* in ignoring read-only-ness. */
20229 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20230 return "*";
20231 return "-";
20233 case '%':
20234 return "%";
20236 case '[':
20238 int i;
20239 char *p;
20241 if (command_loop_level > 5)
20242 return "[[[... ";
20243 p = decode_mode_spec_buf;
20244 for (i = 0; i < command_loop_level; i++)
20245 *p++ = '[';
20246 *p = 0;
20247 return decode_mode_spec_buf;
20250 case ']':
20252 int i;
20253 char *p;
20255 if (command_loop_level > 5)
20256 return " ...]]]";
20257 p = decode_mode_spec_buf;
20258 for (i = 0; i < command_loop_level; i++)
20259 *p++ = ']';
20260 *p = 0;
20261 return decode_mode_spec_buf;
20264 case '-':
20266 register int i;
20268 /* Let lots_of_dashes be a string of infinite length. */
20269 if (mode_line_target == MODE_LINE_NOPROP ||
20270 mode_line_target == MODE_LINE_STRING)
20271 return "--";
20272 if (field_width <= 0
20273 || field_width > sizeof (lots_of_dashes))
20275 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
20276 decode_mode_spec_buf[i] = '-';
20277 decode_mode_spec_buf[i] = '\0';
20278 return decode_mode_spec_buf;
20280 else
20281 return lots_of_dashes;
20284 case 'b':
20285 obj = BVAR (b, name);
20286 break;
20288 case 'c':
20289 /* %c and %l are ignored in `frame-title-format'.
20290 (In redisplay_internal, the frame title is drawn _before_ the
20291 windows are updated, so the stuff which depends on actual
20292 window contents (such as %l) may fail to render properly, or
20293 even crash emacs.) */
20294 if (mode_line_target == MODE_LINE_TITLE)
20295 return "";
20296 else
20298 EMACS_INT col = current_column ();
20299 w->column_number_displayed = make_number (col);
20300 pint2str (decode_mode_spec_buf, field_width, col);
20301 return decode_mode_spec_buf;
20304 case 'e':
20305 #ifndef SYSTEM_MALLOC
20307 if (NILP (Vmemory_full))
20308 return "";
20309 else
20310 return "!MEM FULL! ";
20312 #else
20313 return "";
20314 #endif
20316 case 'F':
20317 /* %F displays the frame name. */
20318 if (!NILP (f->title))
20319 return SSDATA (f->title);
20320 if (f->explicit_name || ! FRAME_WINDOW_P (f))
20321 return SSDATA (f->name);
20322 return "Emacs";
20324 case 'f':
20325 obj = BVAR (b, filename);
20326 break;
20328 case 'i':
20330 EMACS_INT size = ZV - BEGV;
20331 pint2str (decode_mode_spec_buf, field_width, size);
20332 return decode_mode_spec_buf;
20335 case 'I':
20337 EMACS_INT size = ZV - BEGV;
20338 pint2hrstr (decode_mode_spec_buf, field_width, size);
20339 return decode_mode_spec_buf;
20342 case 'l':
20344 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
20345 EMACS_INT topline, nlines, height;
20346 EMACS_INT junk;
20348 /* %c and %l are ignored in `frame-title-format'. */
20349 if (mode_line_target == MODE_LINE_TITLE)
20350 return "";
20352 startpos = XMARKER (w->start)->charpos;
20353 startpos_byte = marker_byte_position (w->start);
20354 height = WINDOW_TOTAL_LINES (w);
20356 /* If we decided that this buffer isn't suitable for line numbers,
20357 don't forget that too fast. */
20358 if (EQ (w->base_line_pos, w->buffer))
20359 goto no_value;
20360 /* But do forget it, if the window shows a different buffer now. */
20361 else if (BUFFERP (w->base_line_pos))
20362 w->base_line_pos = Qnil;
20364 /* If the buffer is very big, don't waste time. */
20365 if (INTEGERP (Vline_number_display_limit)
20366 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
20368 w->base_line_pos = Qnil;
20369 w->base_line_number = Qnil;
20370 goto no_value;
20373 if (INTEGERP (w->base_line_number)
20374 && INTEGERP (w->base_line_pos)
20375 && XFASTINT (w->base_line_pos) <= startpos)
20377 line = XFASTINT (w->base_line_number);
20378 linepos = XFASTINT (w->base_line_pos);
20379 linepos_byte = buf_charpos_to_bytepos (b, linepos);
20381 else
20383 line = 1;
20384 linepos = BUF_BEGV (b);
20385 linepos_byte = BUF_BEGV_BYTE (b);
20388 /* Count lines from base line to window start position. */
20389 nlines = display_count_lines (linepos_byte,
20390 startpos_byte,
20391 startpos, &junk);
20393 topline = nlines + line;
20395 /* Determine a new base line, if the old one is too close
20396 or too far away, or if we did not have one.
20397 "Too close" means it's plausible a scroll-down would
20398 go back past it. */
20399 if (startpos == BUF_BEGV (b))
20401 w->base_line_number = make_number (topline);
20402 w->base_line_pos = make_number (BUF_BEGV (b));
20404 else if (nlines < height + 25 || nlines > height * 3 + 50
20405 || linepos == BUF_BEGV (b))
20407 EMACS_INT limit = BUF_BEGV (b);
20408 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
20409 EMACS_INT position;
20410 EMACS_INT distance =
20411 (height * 2 + 30) * line_number_display_limit_width;
20413 if (startpos - distance > limit)
20415 limit = startpos - distance;
20416 limit_byte = CHAR_TO_BYTE (limit);
20419 nlines = display_count_lines (startpos_byte,
20420 limit_byte,
20421 - (height * 2 + 30),
20422 &position);
20423 /* If we couldn't find the lines we wanted within
20424 line_number_display_limit_width chars per line,
20425 give up on line numbers for this window. */
20426 if (position == limit_byte && limit == startpos - distance)
20428 w->base_line_pos = w->buffer;
20429 w->base_line_number = Qnil;
20430 goto no_value;
20433 w->base_line_number = make_number (topline - nlines);
20434 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
20437 /* Now count lines from the start pos to point. */
20438 nlines = display_count_lines (startpos_byte,
20439 PT_BYTE, PT, &junk);
20441 /* Record that we did display the line number. */
20442 line_number_displayed = 1;
20444 /* Make the string to show. */
20445 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
20446 return decode_mode_spec_buf;
20447 no_value:
20449 char* p = decode_mode_spec_buf;
20450 int pad = field_width - 2;
20451 while (pad-- > 0)
20452 *p++ = ' ';
20453 *p++ = '?';
20454 *p++ = '?';
20455 *p = '\0';
20456 return decode_mode_spec_buf;
20459 break;
20461 case 'm':
20462 obj = BVAR (b, mode_name);
20463 break;
20465 case 'n':
20466 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
20467 return " Narrow";
20468 break;
20470 case 'p':
20472 EMACS_INT pos = marker_position (w->start);
20473 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
20475 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
20477 if (pos <= BUF_BEGV (b))
20478 return "All";
20479 else
20480 return "Bottom";
20482 else if (pos <= BUF_BEGV (b))
20483 return "Top";
20484 else
20486 if (total > 1000000)
20487 /* Do it differently for a large value, to avoid overflow. */
20488 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
20489 else
20490 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
20491 /* We can't normally display a 3-digit number,
20492 so get us a 2-digit number that is close. */
20493 if (total == 100)
20494 total = 99;
20495 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
20496 return decode_mode_spec_buf;
20500 /* Display percentage of size above the bottom of the screen. */
20501 case 'P':
20503 EMACS_INT toppos = marker_position (w->start);
20504 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
20505 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
20507 if (botpos >= BUF_ZV (b))
20509 if (toppos <= BUF_BEGV (b))
20510 return "All";
20511 else
20512 return "Bottom";
20514 else
20516 if (total > 1000000)
20517 /* Do it differently for a large value, to avoid overflow. */
20518 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
20519 else
20520 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
20521 /* We can't normally display a 3-digit number,
20522 so get us a 2-digit number that is close. */
20523 if (total == 100)
20524 total = 99;
20525 if (toppos <= BUF_BEGV (b))
20526 sprintf (decode_mode_spec_buf, "Top%2"pI"d%%", total);
20527 else
20528 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
20529 return decode_mode_spec_buf;
20533 case 's':
20534 /* status of process */
20535 obj = Fget_buffer_process (Fcurrent_buffer ());
20536 if (NILP (obj))
20537 return "no process";
20538 #ifndef MSDOS
20539 obj = Fsymbol_name (Fprocess_status (obj));
20540 #endif
20541 break;
20543 case '@':
20545 int count = inhibit_garbage_collection ();
20546 Lisp_Object val = call1 (intern ("file-remote-p"),
20547 BVAR (current_buffer, directory));
20548 unbind_to (count, Qnil);
20550 if (NILP (val))
20551 return "-";
20552 else
20553 return "@";
20556 case 't': /* indicate TEXT or BINARY */
20557 return "T";
20559 case 'z':
20560 /* coding-system (not including end-of-line format) */
20561 case 'Z':
20562 /* coding-system (including end-of-line type) */
20564 int eol_flag = (c == 'Z');
20565 char *p = decode_mode_spec_buf;
20567 if (! FRAME_WINDOW_P (f))
20569 /* No need to mention EOL here--the terminal never needs
20570 to do EOL conversion. */
20571 p = decode_mode_spec_coding (CODING_ID_NAME
20572 (FRAME_KEYBOARD_CODING (f)->id),
20573 p, 0);
20574 p = decode_mode_spec_coding (CODING_ID_NAME
20575 (FRAME_TERMINAL_CODING (f)->id),
20576 p, 0);
20578 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
20579 p, eol_flag);
20581 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
20582 #ifdef subprocesses
20583 obj = Fget_buffer_process (Fcurrent_buffer ());
20584 if (PROCESSP (obj))
20586 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
20587 p, eol_flag);
20588 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
20589 p, eol_flag);
20591 #endif /* subprocesses */
20592 #endif /* 0 */
20593 *p = 0;
20594 return decode_mode_spec_buf;
20598 if (STRINGP (obj))
20600 *string = obj;
20601 return SSDATA (obj);
20603 else
20604 return "";
20608 /* Count up to COUNT lines starting from START_BYTE.
20609 But don't go beyond LIMIT_BYTE.
20610 Return the number of lines thus found (always nonnegative).
20612 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
20614 static EMACS_INT
20615 display_count_lines (EMACS_INT start_byte,
20616 EMACS_INT limit_byte, EMACS_INT count,
20617 EMACS_INT *byte_pos_ptr)
20619 register unsigned char *cursor;
20620 unsigned char *base;
20622 register EMACS_INT ceiling;
20623 register unsigned char *ceiling_addr;
20624 EMACS_INT orig_count = count;
20626 /* If we are not in selective display mode,
20627 check only for newlines. */
20628 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
20629 && !INTEGERP (BVAR (current_buffer, selective_display)));
20631 if (count > 0)
20633 while (start_byte < limit_byte)
20635 ceiling = BUFFER_CEILING_OF (start_byte);
20636 ceiling = min (limit_byte - 1, ceiling);
20637 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
20638 base = (cursor = BYTE_POS_ADDR (start_byte));
20639 while (1)
20641 if (selective_display)
20642 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
20644 else
20645 while (*cursor != '\n' && ++cursor != ceiling_addr)
20648 if (cursor != ceiling_addr)
20650 if (--count == 0)
20652 start_byte += cursor - base + 1;
20653 *byte_pos_ptr = start_byte;
20654 return orig_count;
20656 else
20657 if (++cursor == ceiling_addr)
20658 break;
20660 else
20661 break;
20663 start_byte += cursor - base;
20666 else
20668 while (start_byte > limit_byte)
20670 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
20671 ceiling = max (limit_byte, ceiling);
20672 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
20673 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
20674 while (1)
20676 if (selective_display)
20677 while (--cursor != ceiling_addr
20678 && *cursor != '\n' && *cursor != 015)
20680 else
20681 while (--cursor != ceiling_addr && *cursor != '\n')
20684 if (cursor != ceiling_addr)
20686 if (++count == 0)
20688 start_byte += cursor - base + 1;
20689 *byte_pos_ptr = start_byte;
20690 /* When scanning backwards, we should
20691 not count the newline posterior to which we stop. */
20692 return - orig_count - 1;
20695 else
20696 break;
20698 /* Here we add 1 to compensate for the last decrement
20699 of CURSOR, which took it past the valid range. */
20700 start_byte += cursor - base + 1;
20704 *byte_pos_ptr = limit_byte;
20706 if (count < 0)
20707 return - orig_count + count;
20708 return orig_count - count;
20714 /***********************************************************************
20715 Displaying strings
20716 ***********************************************************************/
20718 /* Display a NUL-terminated string, starting with index START.
20720 If STRING is non-null, display that C string. Otherwise, the Lisp
20721 string LISP_STRING is displayed. There's a case that STRING is
20722 non-null and LISP_STRING is not nil. It means STRING is a string
20723 data of LISP_STRING. In that case, we display LISP_STRING while
20724 ignoring its text properties.
20726 If FACE_STRING is not nil, FACE_STRING_POS is a position in
20727 FACE_STRING. Display STRING or LISP_STRING with the face at
20728 FACE_STRING_POS in FACE_STRING:
20730 Display the string in the environment given by IT, but use the
20731 standard display table, temporarily.
20733 FIELD_WIDTH is the minimum number of output glyphs to produce.
20734 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20735 with spaces. If STRING has more characters, more than FIELD_WIDTH
20736 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
20738 PRECISION is the maximum number of characters to output from
20739 STRING. PRECISION < 0 means don't truncate the string.
20741 This is roughly equivalent to printf format specifiers:
20743 FIELD_WIDTH PRECISION PRINTF
20744 ----------------------------------------
20745 -1 -1 %s
20746 -1 10 %.10s
20747 10 -1 %10s
20748 20 10 %20.10s
20750 MULTIBYTE zero means do not display multibyte chars, > 0 means do
20751 display them, and < 0 means obey the current buffer's value of
20752 enable_multibyte_characters.
20754 Value is the number of columns displayed. */
20756 static int
20757 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
20758 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
20759 int field_width, int precision, int max_x, int multibyte)
20761 int hpos_at_start = it->hpos;
20762 int saved_face_id = it->face_id;
20763 struct glyph_row *row = it->glyph_row;
20764 EMACS_INT it_charpos;
20766 /* Initialize the iterator IT for iteration over STRING beginning
20767 with index START. */
20768 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
20769 precision, field_width, multibyte);
20770 if (string && STRINGP (lisp_string))
20771 /* LISP_STRING is the one returned by decode_mode_spec. We should
20772 ignore its text properties. */
20773 it->stop_charpos = it->end_charpos;
20775 /* If displaying STRING, set up the face of the iterator from
20776 FACE_STRING, if that's given. */
20777 if (STRINGP (face_string))
20779 EMACS_INT endptr;
20780 struct face *face;
20782 it->face_id
20783 = face_at_string_position (it->w, face_string, face_string_pos,
20784 0, it->region_beg_charpos,
20785 it->region_end_charpos,
20786 &endptr, it->base_face_id, 0);
20787 face = FACE_FROM_ID (it->f, it->face_id);
20788 it->face_box_p = face->box != FACE_NO_BOX;
20791 /* Set max_x to the maximum allowed X position. Don't let it go
20792 beyond the right edge of the window. */
20793 if (max_x <= 0)
20794 max_x = it->last_visible_x;
20795 else
20796 max_x = min (max_x, it->last_visible_x);
20798 /* Skip over display elements that are not visible. because IT->w is
20799 hscrolled. */
20800 if (it->current_x < it->first_visible_x)
20801 move_it_in_display_line_to (it, 100000, it->first_visible_x,
20802 MOVE_TO_POS | MOVE_TO_X);
20804 row->ascent = it->max_ascent;
20805 row->height = it->max_ascent + it->max_descent;
20806 row->phys_ascent = it->max_phys_ascent;
20807 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20808 row->extra_line_spacing = it->max_extra_line_spacing;
20810 if (STRINGP (it->string))
20811 it_charpos = IT_STRING_CHARPOS (*it);
20812 else
20813 it_charpos = IT_CHARPOS (*it);
20815 /* This condition is for the case that we are called with current_x
20816 past last_visible_x. */
20817 while (it->current_x < max_x)
20819 int x_before, x, n_glyphs_before, i, nglyphs;
20821 /* Get the next display element. */
20822 if (!get_next_display_element (it))
20823 break;
20825 /* Produce glyphs. */
20826 x_before = it->current_x;
20827 n_glyphs_before = row->used[TEXT_AREA];
20828 PRODUCE_GLYPHS (it);
20830 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20831 i = 0;
20832 x = x_before;
20833 while (i < nglyphs)
20835 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20837 if (it->line_wrap != TRUNCATE
20838 && x + glyph->pixel_width > max_x)
20840 /* End of continued line or max_x reached. */
20841 if (CHAR_GLYPH_PADDING_P (*glyph))
20843 /* A wide character is unbreakable. */
20844 if (row->reversed_p)
20845 unproduce_glyphs (it, row->used[TEXT_AREA]
20846 - n_glyphs_before);
20847 row->used[TEXT_AREA] = n_glyphs_before;
20848 it->current_x = x_before;
20850 else
20852 if (row->reversed_p)
20853 unproduce_glyphs (it, row->used[TEXT_AREA]
20854 - (n_glyphs_before + i));
20855 row->used[TEXT_AREA] = n_glyphs_before + i;
20856 it->current_x = x;
20858 break;
20860 else if (x + glyph->pixel_width >= it->first_visible_x)
20862 /* Glyph is at least partially visible. */
20863 ++it->hpos;
20864 if (x < it->first_visible_x)
20865 row->x = x - it->first_visible_x;
20867 else
20869 /* Glyph is off the left margin of the display area.
20870 Should not happen. */
20871 abort ();
20874 row->ascent = max (row->ascent, it->max_ascent);
20875 row->height = max (row->height, it->max_ascent + it->max_descent);
20876 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20877 row->phys_height = max (row->phys_height,
20878 it->max_phys_ascent + it->max_phys_descent);
20879 row->extra_line_spacing = max (row->extra_line_spacing,
20880 it->max_extra_line_spacing);
20881 x += glyph->pixel_width;
20882 ++i;
20885 /* Stop if max_x reached. */
20886 if (i < nglyphs)
20887 break;
20889 /* Stop at line ends. */
20890 if (ITERATOR_AT_END_OF_LINE_P (it))
20892 it->continuation_lines_width = 0;
20893 break;
20896 set_iterator_to_next (it, 1);
20897 if (STRINGP (it->string))
20898 it_charpos = IT_STRING_CHARPOS (*it);
20899 else
20900 it_charpos = IT_CHARPOS (*it);
20902 /* Stop if truncating at the right edge. */
20903 if (it->line_wrap == TRUNCATE
20904 && it->current_x >= it->last_visible_x)
20906 /* Add truncation mark, but don't do it if the line is
20907 truncated at a padding space. */
20908 if (it_charpos < it->string_nchars)
20910 if (!FRAME_WINDOW_P (it->f))
20912 int ii, n;
20914 if (it->current_x > it->last_visible_x)
20916 if (!row->reversed_p)
20918 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
20919 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
20920 break;
20922 else
20924 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
20925 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
20926 break;
20927 unproduce_glyphs (it, ii + 1);
20928 ii = row->used[TEXT_AREA] - (ii + 1);
20930 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
20932 row->used[TEXT_AREA] = ii;
20933 produce_special_glyphs (it, IT_TRUNCATION);
20936 produce_special_glyphs (it, IT_TRUNCATION);
20938 row->truncated_on_right_p = 1;
20940 break;
20944 /* Maybe insert a truncation at the left. */
20945 if (it->first_visible_x
20946 && it_charpos > 0)
20948 if (!FRAME_WINDOW_P (it->f))
20949 insert_left_trunc_glyphs (it);
20950 row->truncated_on_left_p = 1;
20953 it->face_id = saved_face_id;
20955 /* Value is number of columns displayed. */
20956 return it->hpos - hpos_at_start;
20961 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
20962 appears as an element of LIST or as the car of an element of LIST.
20963 If PROPVAL is a list, compare each element against LIST in that
20964 way, and return 1/2 if any element of PROPVAL is found in LIST.
20965 Otherwise return 0. This function cannot quit.
20966 The return value is 2 if the text is invisible but with an ellipsis
20967 and 1 if it's invisible and without an ellipsis. */
20970 invisible_p (register Lisp_Object propval, Lisp_Object list)
20972 register Lisp_Object tail, proptail;
20974 for (tail = list; CONSP (tail); tail = XCDR (tail))
20976 register Lisp_Object tem;
20977 tem = XCAR (tail);
20978 if (EQ (propval, tem))
20979 return 1;
20980 if (CONSP (tem) && EQ (propval, XCAR (tem)))
20981 return NILP (XCDR (tem)) ? 1 : 2;
20984 if (CONSP (propval))
20986 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
20988 Lisp_Object propelt;
20989 propelt = XCAR (proptail);
20990 for (tail = list; CONSP (tail); tail = XCDR (tail))
20992 register Lisp_Object tem;
20993 tem = XCAR (tail);
20994 if (EQ (propelt, tem))
20995 return 1;
20996 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
20997 return NILP (XCDR (tem)) ? 1 : 2;
21002 return 0;
21005 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
21006 doc: /* Non-nil if the property makes the text invisible.
21007 POS-OR-PROP can be a marker or number, in which case it is taken to be
21008 a position in the current buffer and the value of the `invisible' property
21009 is checked; or it can be some other value, which is then presumed to be the
21010 value of the `invisible' property of the text of interest.
21011 The non-nil value returned can be t for truly invisible text or something
21012 else if the text is replaced by an ellipsis. */)
21013 (Lisp_Object pos_or_prop)
21015 Lisp_Object prop
21016 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
21017 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
21018 : pos_or_prop);
21019 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
21020 return (invis == 0 ? Qnil
21021 : invis == 1 ? Qt
21022 : make_number (invis));
21025 /* Calculate a width or height in pixels from a specification using
21026 the following elements:
21028 SPEC ::=
21029 NUM - a (fractional) multiple of the default font width/height
21030 (NUM) - specifies exactly NUM pixels
21031 UNIT - a fixed number of pixels, see below.
21032 ELEMENT - size of a display element in pixels, see below.
21033 (NUM . SPEC) - equals NUM * SPEC
21034 (+ SPEC SPEC ...) - add pixel values
21035 (- SPEC SPEC ...) - subtract pixel values
21036 (- SPEC) - negate pixel value
21038 NUM ::=
21039 INT or FLOAT - a number constant
21040 SYMBOL - use symbol's (buffer local) variable binding.
21042 UNIT ::=
21043 in - pixels per inch *)
21044 mm - pixels per 1/1000 meter *)
21045 cm - pixels per 1/100 meter *)
21046 width - width of current font in pixels.
21047 height - height of current font in pixels.
21049 *) using the ratio(s) defined in display-pixels-per-inch.
21051 ELEMENT ::=
21053 left-fringe - left fringe width in pixels
21054 right-fringe - right fringe width in pixels
21056 left-margin - left margin width in pixels
21057 right-margin - right margin width in pixels
21059 scroll-bar - scroll-bar area width in pixels
21061 Examples:
21063 Pixels corresponding to 5 inches:
21064 (5 . in)
21066 Total width of non-text areas on left side of window (if scroll-bar is on left):
21067 '(space :width (+ left-fringe left-margin scroll-bar))
21069 Align to first text column (in header line):
21070 '(space :align-to 0)
21072 Align to middle of text area minus half the width of variable `my-image'
21073 containing a loaded image:
21074 '(space :align-to (0.5 . (- text my-image)))
21076 Width of left margin minus width of 1 character in the default font:
21077 '(space :width (- left-margin 1))
21079 Width of left margin minus width of 2 characters in the current font:
21080 '(space :width (- left-margin (2 . width)))
21082 Center 1 character over left-margin (in header line):
21083 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
21085 Different ways to express width of left fringe plus left margin minus one pixel:
21086 '(space :width (- (+ left-fringe left-margin) (1)))
21087 '(space :width (+ left-fringe left-margin (- (1))))
21088 '(space :width (+ left-fringe left-margin (-1)))
21092 #define NUMVAL(X) \
21093 ((INTEGERP (X) || FLOATP (X)) \
21094 ? XFLOATINT (X) \
21095 : - 1)
21098 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
21099 struct font *font, int width_p, int *align_to)
21101 double pixels;
21103 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
21104 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
21106 if (NILP (prop))
21107 return OK_PIXELS (0);
21109 xassert (FRAME_LIVE_P (it->f));
21111 if (SYMBOLP (prop))
21113 if (SCHARS (SYMBOL_NAME (prop)) == 2)
21115 char *unit = SSDATA (SYMBOL_NAME (prop));
21117 if (unit[0] == 'i' && unit[1] == 'n')
21118 pixels = 1.0;
21119 else if (unit[0] == 'm' && unit[1] == 'm')
21120 pixels = 25.4;
21121 else if (unit[0] == 'c' && unit[1] == 'm')
21122 pixels = 2.54;
21123 else
21124 pixels = 0;
21125 if (pixels > 0)
21127 double ppi;
21128 #ifdef HAVE_WINDOW_SYSTEM
21129 if (FRAME_WINDOW_P (it->f)
21130 && (ppi = (width_p
21131 ? FRAME_X_DISPLAY_INFO (it->f)->resx
21132 : FRAME_X_DISPLAY_INFO (it->f)->resy),
21133 ppi > 0))
21134 return OK_PIXELS (ppi / pixels);
21135 #endif
21137 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
21138 || (CONSP (Vdisplay_pixels_per_inch)
21139 && (ppi = (width_p
21140 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
21141 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
21142 ppi > 0)))
21143 return OK_PIXELS (ppi / pixels);
21145 return 0;
21149 #ifdef HAVE_WINDOW_SYSTEM
21150 if (EQ (prop, Qheight))
21151 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
21152 if (EQ (prop, Qwidth))
21153 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
21154 #else
21155 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
21156 return OK_PIXELS (1);
21157 #endif
21159 if (EQ (prop, Qtext))
21160 return OK_PIXELS (width_p
21161 ? window_box_width (it->w, TEXT_AREA)
21162 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
21164 if (align_to && *align_to < 0)
21166 *res = 0;
21167 if (EQ (prop, Qleft))
21168 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
21169 if (EQ (prop, Qright))
21170 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
21171 if (EQ (prop, Qcenter))
21172 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
21173 + window_box_width (it->w, TEXT_AREA) / 2);
21174 if (EQ (prop, Qleft_fringe))
21175 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21176 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
21177 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
21178 if (EQ (prop, Qright_fringe))
21179 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21180 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21181 : window_box_right_offset (it->w, TEXT_AREA));
21182 if (EQ (prop, Qleft_margin))
21183 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
21184 if (EQ (prop, Qright_margin))
21185 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
21186 if (EQ (prop, Qscroll_bar))
21187 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
21189 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21190 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21191 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21192 : 0)));
21194 else
21196 if (EQ (prop, Qleft_fringe))
21197 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
21198 if (EQ (prop, Qright_fringe))
21199 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
21200 if (EQ (prop, Qleft_margin))
21201 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
21202 if (EQ (prop, Qright_margin))
21203 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
21204 if (EQ (prop, Qscroll_bar))
21205 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
21208 prop = Fbuffer_local_value (prop, it->w->buffer);
21211 if (INTEGERP (prop) || FLOATP (prop))
21213 int base_unit = (width_p
21214 ? FRAME_COLUMN_WIDTH (it->f)
21215 : FRAME_LINE_HEIGHT (it->f));
21216 return OK_PIXELS (XFLOATINT (prop) * base_unit);
21219 if (CONSP (prop))
21221 Lisp_Object car = XCAR (prop);
21222 Lisp_Object cdr = XCDR (prop);
21224 if (SYMBOLP (car))
21226 #ifdef HAVE_WINDOW_SYSTEM
21227 if (FRAME_WINDOW_P (it->f)
21228 && valid_image_p (prop))
21230 int id = lookup_image (it->f, prop);
21231 struct image *img = IMAGE_FROM_ID (it->f, id);
21233 return OK_PIXELS (width_p ? img->width : img->height);
21235 #endif
21236 if (EQ (car, Qplus) || EQ (car, Qminus))
21238 int first = 1;
21239 double px;
21241 pixels = 0;
21242 while (CONSP (cdr))
21244 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
21245 font, width_p, align_to))
21246 return 0;
21247 if (first)
21248 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
21249 else
21250 pixels += px;
21251 cdr = XCDR (cdr);
21253 if (EQ (car, Qminus))
21254 pixels = -pixels;
21255 return OK_PIXELS (pixels);
21258 car = Fbuffer_local_value (car, it->w->buffer);
21261 if (INTEGERP (car) || FLOATP (car))
21263 double fact;
21264 pixels = XFLOATINT (car);
21265 if (NILP (cdr))
21266 return OK_PIXELS (pixels);
21267 if (calc_pixel_width_or_height (&fact, it, cdr,
21268 font, width_p, align_to))
21269 return OK_PIXELS (pixels * fact);
21270 return 0;
21273 return 0;
21276 return 0;
21280 /***********************************************************************
21281 Glyph Display
21282 ***********************************************************************/
21284 #ifdef HAVE_WINDOW_SYSTEM
21286 #if GLYPH_DEBUG
21288 void
21289 dump_glyph_string (struct glyph_string *s)
21291 fprintf (stderr, "glyph string\n");
21292 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
21293 s->x, s->y, s->width, s->height);
21294 fprintf (stderr, " ybase = %d\n", s->ybase);
21295 fprintf (stderr, " hl = %d\n", s->hl);
21296 fprintf (stderr, " left overhang = %d, right = %d\n",
21297 s->left_overhang, s->right_overhang);
21298 fprintf (stderr, " nchars = %d\n", s->nchars);
21299 fprintf (stderr, " extends to end of line = %d\n",
21300 s->extends_to_end_of_line_p);
21301 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
21302 fprintf (stderr, " bg width = %d\n", s->background_width);
21305 #endif /* GLYPH_DEBUG */
21307 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
21308 of XChar2b structures for S; it can't be allocated in
21309 init_glyph_string because it must be allocated via `alloca'. W
21310 is the window on which S is drawn. ROW and AREA are the glyph row
21311 and area within the row from which S is constructed. START is the
21312 index of the first glyph structure covered by S. HL is a
21313 face-override for drawing S. */
21315 #ifdef HAVE_NTGUI
21316 #define OPTIONAL_HDC(hdc) HDC hdc,
21317 #define DECLARE_HDC(hdc) HDC hdc;
21318 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
21319 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
21320 #endif
21322 #ifndef OPTIONAL_HDC
21323 #define OPTIONAL_HDC(hdc)
21324 #define DECLARE_HDC(hdc)
21325 #define ALLOCATE_HDC(hdc, f)
21326 #define RELEASE_HDC(hdc, f)
21327 #endif
21329 static void
21330 init_glyph_string (struct glyph_string *s,
21331 OPTIONAL_HDC (hdc)
21332 XChar2b *char2b, struct window *w, struct glyph_row *row,
21333 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
21335 memset (s, 0, sizeof *s);
21336 s->w = w;
21337 s->f = XFRAME (w->frame);
21338 #ifdef HAVE_NTGUI
21339 s->hdc = hdc;
21340 #endif
21341 s->display = FRAME_X_DISPLAY (s->f);
21342 s->window = FRAME_X_WINDOW (s->f);
21343 s->char2b = char2b;
21344 s->hl = hl;
21345 s->row = row;
21346 s->area = area;
21347 s->first_glyph = row->glyphs[area] + start;
21348 s->height = row->height;
21349 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
21350 s->ybase = s->y + row->ascent;
21354 /* Append the list of glyph strings with head H and tail T to the list
21355 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
21357 static inline void
21358 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
21359 struct glyph_string *h, struct glyph_string *t)
21361 if (h)
21363 if (*head)
21364 (*tail)->next = h;
21365 else
21366 *head = h;
21367 h->prev = *tail;
21368 *tail = t;
21373 /* Prepend the list of glyph strings with head H and tail T to the
21374 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
21375 result. */
21377 static inline void
21378 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
21379 struct glyph_string *h, struct glyph_string *t)
21381 if (h)
21383 if (*head)
21384 (*head)->prev = t;
21385 else
21386 *tail = t;
21387 t->next = *head;
21388 *head = h;
21393 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
21394 Set *HEAD and *TAIL to the resulting list. */
21396 static inline void
21397 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
21398 struct glyph_string *s)
21400 s->next = s->prev = NULL;
21401 append_glyph_string_lists (head, tail, s, s);
21405 /* Get face and two-byte form of character C in face FACE_ID on frame F.
21406 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
21407 make sure that X resources for the face returned are allocated.
21408 Value is a pointer to a realized face that is ready for display if
21409 DISPLAY_P is non-zero. */
21411 static inline struct face *
21412 get_char_face_and_encoding (struct frame *f, int c, int face_id,
21413 XChar2b *char2b, int display_p)
21415 struct face *face = FACE_FROM_ID (f, face_id);
21417 if (face->font)
21419 unsigned code = face->font->driver->encode_char (face->font, c);
21421 if (code != FONT_INVALID_CODE)
21422 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21423 else
21424 STORE_XCHAR2B (char2b, 0, 0);
21427 /* Make sure X resources of the face are allocated. */
21428 #ifdef HAVE_X_WINDOWS
21429 if (display_p)
21430 #endif
21432 xassert (face != NULL);
21433 PREPARE_FACE_FOR_DISPLAY (f, face);
21436 return face;
21440 /* Get face and two-byte form of character glyph GLYPH on frame F.
21441 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
21442 a pointer to a realized face that is ready for display. */
21444 static inline struct face *
21445 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
21446 XChar2b *char2b, int *two_byte_p)
21448 struct face *face;
21450 xassert (glyph->type == CHAR_GLYPH);
21451 face = FACE_FROM_ID (f, glyph->face_id);
21453 if (two_byte_p)
21454 *two_byte_p = 0;
21456 if (face->font)
21458 unsigned code;
21460 if (CHAR_BYTE8_P (glyph->u.ch))
21461 code = CHAR_TO_BYTE8 (glyph->u.ch);
21462 else
21463 code = face->font->driver->encode_char (face->font, glyph->u.ch);
21465 if (code != FONT_INVALID_CODE)
21466 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21467 else
21468 STORE_XCHAR2B (char2b, 0, 0);
21471 /* Make sure X resources of the face are allocated. */
21472 xassert (face != NULL);
21473 PREPARE_FACE_FOR_DISPLAY (f, face);
21474 return face;
21478 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
21479 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
21481 static inline int
21482 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
21484 unsigned code;
21486 if (CHAR_BYTE8_P (c))
21487 code = CHAR_TO_BYTE8 (c);
21488 else
21489 code = font->driver->encode_char (font, c);
21491 if (code == FONT_INVALID_CODE)
21492 return 0;
21493 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21494 return 1;
21498 /* Fill glyph string S with composition components specified by S->cmp.
21500 BASE_FACE is the base face of the composition.
21501 S->cmp_from is the index of the first component for S.
21503 OVERLAPS non-zero means S should draw the foreground only, and use
21504 its physical height for clipping. See also draw_glyphs.
21506 Value is the index of a component not in S. */
21508 static int
21509 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
21510 int overlaps)
21512 int i;
21513 /* For all glyphs of this composition, starting at the offset
21514 S->cmp_from, until we reach the end of the definition or encounter a
21515 glyph that requires the different face, add it to S. */
21516 struct face *face;
21518 xassert (s);
21520 s->for_overlaps = overlaps;
21521 s->face = NULL;
21522 s->font = NULL;
21523 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
21525 int c = COMPOSITION_GLYPH (s->cmp, i);
21527 if (c != '\t')
21529 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
21530 -1, Qnil);
21532 face = get_char_face_and_encoding (s->f, c, face_id,
21533 s->char2b + i, 1);
21534 if (face)
21536 if (! s->face)
21538 s->face = face;
21539 s->font = s->face->font;
21541 else if (s->face != face)
21542 break;
21545 ++s->nchars;
21547 s->cmp_to = i;
21549 /* All glyph strings for the same composition has the same width,
21550 i.e. the width set for the first component of the composition. */
21551 s->width = s->first_glyph->pixel_width;
21553 /* If the specified font could not be loaded, use the frame's
21554 default font, but record the fact that we couldn't load it in
21555 the glyph string so that we can draw rectangles for the
21556 characters of the glyph string. */
21557 if (s->font == NULL)
21559 s->font_not_found_p = 1;
21560 s->font = FRAME_FONT (s->f);
21563 /* Adjust base line for subscript/superscript text. */
21564 s->ybase += s->first_glyph->voffset;
21566 /* This glyph string must always be drawn with 16-bit functions. */
21567 s->two_byte_p = 1;
21569 return s->cmp_to;
21572 static int
21573 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
21574 int start, int end, int overlaps)
21576 struct glyph *glyph, *last;
21577 Lisp_Object lgstring;
21578 int i;
21580 s->for_overlaps = overlaps;
21581 glyph = s->row->glyphs[s->area] + start;
21582 last = s->row->glyphs[s->area] + end;
21583 s->cmp_id = glyph->u.cmp.id;
21584 s->cmp_from = glyph->slice.cmp.from;
21585 s->cmp_to = glyph->slice.cmp.to + 1;
21586 s->face = FACE_FROM_ID (s->f, face_id);
21587 lgstring = composition_gstring_from_id (s->cmp_id);
21588 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
21589 glyph++;
21590 while (glyph < last
21591 && glyph->u.cmp.automatic
21592 && glyph->u.cmp.id == s->cmp_id
21593 && s->cmp_to == glyph->slice.cmp.from)
21594 s->cmp_to = (glyph++)->slice.cmp.to + 1;
21596 for (i = s->cmp_from; i < s->cmp_to; i++)
21598 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
21599 unsigned code = LGLYPH_CODE (lglyph);
21601 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
21603 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
21604 return glyph - s->row->glyphs[s->area];
21608 /* Fill glyph string S from a sequence glyphs for glyphless characters.
21609 See the comment of fill_glyph_string for arguments.
21610 Value is the index of the first glyph not in S. */
21613 static int
21614 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
21615 int start, int end, int overlaps)
21617 struct glyph *glyph, *last;
21618 int voffset;
21620 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
21621 s->for_overlaps = overlaps;
21622 glyph = s->row->glyphs[s->area] + start;
21623 last = s->row->glyphs[s->area] + end;
21624 voffset = glyph->voffset;
21625 s->face = FACE_FROM_ID (s->f, face_id);
21626 s->font = s->face->font;
21627 s->nchars = 1;
21628 s->width = glyph->pixel_width;
21629 glyph++;
21630 while (glyph < last
21631 && glyph->type == GLYPHLESS_GLYPH
21632 && glyph->voffset == voffset
21633 && glyph->face_id == face_id)
21635 s->nchars++;
21636 s->width += glyph->pixel_width;
21637 glyph++;
21639 s->ybase += voffset;
21640 return glyph - s->row->glyphs[s->area];
21644 /* Fill glyph string S from a sequence of character glyphs.
21646 FACE_ID is the face id of the string. START is the index of the
21647 first glyph to consider, END is the index of the last + 1.
21648 OVERLAPS non-zero means S should draw the foreground only, and use
21649 its physical height for clipping. See also draw_glyphs.
21651 Value is the index of the first glyph not in S. */
21653 static int
21654 fill_glyph_string (struct glyph_string *s, int face_id,
21655 int start, int end, int overlaps)
21657 struct glyph *glyph, *last;
21658 int voffset;
21659 int glyph_not_available_p;
21661 xassert (s->f == XFRAME (s->w->frame));
21662 xassert (s->nchars == 0);
21663 xassert (start >= 0 && end > start);
21665 s->for_overlaps = overlaps;
21666 glyph = s->row->glyphs[s->area] + start;
21667 last = s->row->glyphs[s->area] + end;
21668 voffset = glyph->voffset;
21669 s->padding_p = glyph->padding_p;
21670 glyph_not_available_p = glyph->glyph_not_available_p;
21672 while (glyph < last
21673 && glyph->type == CHAR_GLYPH
21674 && glyph->voffset == voffset
21675 /* Same face id implies same font, nowadays. */
21676 && glyph->face_id == face_id
21677 && glyph->glyph_not_available_p == glyph_not_available_p)
21679 int two_byte_p;
21681 s->face = get_glyph_face_and_encoding (s->f, glyph,
21682 s->char2b + s->nchars,
21683 &two_byte_p);
21684 s->two_byte_p = two_byte_p;
21685 ++s->nchars;
21686 xassert (s->nchars <= end - start);
21687 s->width += glyph->pixel_width;
21688 if (glyph++->padding_p != s->padding_p)
21689 break;
21692 s->font = s->face->font;
21694 /* If the specified font could not be loaded, use the frame's font,
21695 but record the fact that we couldn't load it in
21696 S->font_not_found_p so that we can draw rectangles for the
21697 characters of the glyph string. */
21698 if (s->font == NULL || glyph_not_available_p)
21700 s->font_not_found_p = 1;
21701 s->font = FRAME_FONT (s->f);
21704 /* Adjust base line for subscript/superscript text. */
21705 s->ybase += voffset;
21707 xassert (s->face && s->face->gc);
21708 return glyph - s->row->glyphs[s->area];
21712 /* Fill glyph string S from image glyph S->first_glyph. */
21714 static void
21715 fill_image_glyph_string (struct glyph_string *s)
21717 xassert (s->first_glyph->type == IMAGE_GLYPH);
21718 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
21719 xassert (s->img);
21720 s->slice = s->first_glyph->slice.img;
21721 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
21722 s->font = s->face->font;
21723 s->width = s->first_glyph->pixel_width;
21725 /* Adjust base line for subscript/superscript text. */
21726 s->ybase += s->first_glyph->voffset;
21730 /* Fill glyph string S from a sequence of stretch glyphs.
21732 START is the index of the first glyph to consider,
21733 END is the index of the last + 1.
21735 Value is the index of the first glyph not in S. */
21737 static int
21738 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
21740 struct glyph *glyph, *last;
21741 int voffset, face_id;
21743 xassert (s->first_glyph->type == STRETCH_GLYPH);
21745 glyph = s->row->glyphs[s->area] + start;
21746 last = s->row->glyphs[s->area] + end;
21747 face_id = glyph->face_id;
21748 s->face = FACE_FROM_ID (s->f, face_id);
21749 s->font = s->face->font;
21750 s->width = glyph->pixel_width;
21751 s->nchars = 1;
21752 voffset = glyph->voffset;
21754 for (++glyph;
21755 (glyph < last
21756 && glyph->type == STRETCH_GLYPH
21757 && glyph->voffset == voffset
21758 && glyph->face_id == face_id);
21759 ++glyph)
21760 s->width += glyph->pixel_width;
21762 /* Adjust base line for subscript/superscript text. */
21763 s->ybase += voffset;
21765 /* The case that face->gc == 0 is handled when drawing the glyph
21766 string by calling PREPARE_FACE_FOR_DISPLAY. */
21767 xassert (s->face);
21768 return glyph - s->row->glyphs[s->area];
21771 static struct font_metrics *
21772 get_per_char_metric (struct font *font, XChar2b *char2b)
21774 static struct font_metrics metrics;
21775 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
21777 if (! font || code == FONT_INVALID_CODE)
21778 return NULL;
21779 font->driver->text_extents (font, &code, 1, &metrics);
21780 return &metrics;
21783 /* EXPORT for RIF:
21784 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
21785 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
21786 assumed to be zero. */
21788 void
21789 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
21791 *left = *right = 0;
21793 if (glyph->type == CHAR_GLYPH)
21795 struct face *face;
21796 XChar2b char2b;
21797 struct font_metrics *pcm;
21799 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
21800 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
21802 if (pcm->rbearing > pcm->width)
21803 *right = pcm->rbearing - pcm->width;
21804 if (pcm->lbearing < 0)
21805 *left = -pcm->lbearing;
21808 else if (glyph->type == COMPOSITE_GLYPH)
21810 if (! glyph->u.cmp.automatic)
21812 struct composition *cmp = composition_table[glyph->u.cmp.id];
21814 if (cmp->rbearing > cmp->pixel_width)
21815 *right = cmp->rbearing - cmp->pixel_width;
21816 if (cmp->lbearing < 0)
21817 *left = - cmp->lbearing;
21819 else
21821 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
21822 struct font_metrics metrics;
21824 composition_gstring_width (gstring, glyph->slice.cmp.from,
21825 glyph->slice.cmp.to + 1, &metrics);
21826 if (metrics.rbearing > metrics.width)
21827 *right = metrics.rbearing - metrics.width;
21828 if (metrics.lbearing < 0)
21829 *left = - metrics.lbearing;
21835 /* Return the index of the first glyph preceding glyph string S that
21836 is overwritten by S because of S's left overhang. Value is -1
21837 if no glyphs are overwritten. */
21839 static int
21840 left_overwritten (struct glyph_string *s)
21842 int k;
21844 if (s->left_overhang)
21846 int x = 0, i;
21847 struct glyph *glyphs = s->row->glyphs[s->area];
21848 int first = s->first_glyph - glyphs;
21850 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
21851 x -= glyphs[i].pixel_width;
21853 k = i + 1;
21855 else
21856 k = -1;
21858 return k;
21862 /* Return the index of the first glyph preceding glyph string S that
21863 is overwriting S because of its right overhang. Value is -1 if no
21864 glyph in front of S overwrites S. */
21866 static int
21867 left_overwriting (struct glyph_string *s)
21869 int i, k, x;
21870 struct glyph *glyphs = s->row->glyphs[s->area];
21871 int first = s->first_glyph - glyphs;
21873 k = -1;
21874 x = 0;
21875 for (i = first - 1; i >= 0; --i)
21877 int left, right;
21878 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21879 if (x + right > 0)
21880 k = i;
21881 x -= glyphs[i].pixel_width;
21884 return k;
21888 /* Return the index of the last glyph following glyph string S that is
21889 overwritten by S because of S's right overhang. Value is -1 if
21890 no such glyph is found. */
21892 static int
21893 right_overwritten (struct glyph_string *s)
21895 int k = -1;
21897 if (s->right_overhang)
21899 int x = 0, i;
21900 struct glyph *glyphs = s->row->glyphs[s->area];
21901 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21902 int end = s->row->used[s->area];
21904 for (i = first; i < end && s->right_overhang > x; ++i)
21905 x += glyphs[i].pixel_width;
21907 k = i;
21910 return k;
21914 /* Return the index of the last glyph following glyph string S that
21915 overwrites S because of its left overhang. Value is negative
21916 if no such glyph is found. */
21918 static int
21919 right_overwriting (struct glyph_string *s)
21921 int i, k, x;
21922 int end = s->row->used[s->area];
21923 struct glyph *glyphs = s->row->glyphs[s->area];
21924 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21926 k = -1;
21927 x = 0;
21928 for (i = first; i < end; ++i)
21930 int left, right;
21931 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21932 if (x - left < 0)
21933 k = i;
21934 x += glyphs[i].pixel_width;
21937 return k;
21941 /* Set background width of glyph string S. START is the index of the
21942 first glyph following S. LAST_X is the right-most x-position + 1
21943 in the drawing area. */
21945 static inline void
21946 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
21948 /* If the face of this glyph string has to be drawn to the end of
21949 the drawing area, set S->extends_to_end_of_line_p. */
21951 if (start == s->row->used[s->area]
21952 && s->area == TEXT_AREA
21953 && ((s->row->fill_line_p
21954 && (s->hl == DRAW_NORMAL_TEXT
21955 || s->hl == DRAW_IMAGE_RAISED
21956 || s->hl == DRAW_IMAGE_SUNKEN))
21957 || s->hl == DRAW_MOUSE_FACE))
21958 s->extends_to_end_of_line_p = 1;
21960 /* If S extends its face to the end of the line, set its
21961 background_width to the distance to the right edge of the drawing
21962 area. */
21963 if (s->extends_to_end_of_line_p)
21964 s->background_width = last_x - s->x + 1;
21965 else
21966 s->background_width = s->width;
21970 /* Compute overhangs and x-positions for glyph string S and its
21971 predecessors, or successors. X is the starting x-position for S.
21972 BACKWARD_P non-zero means process predecessors. */
21974 static void
21975 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
21977 if (backward_p)
21979 while (s)
21981 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21982 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21983 x -= s->width;
21984 s->x = x;
21985 s = s->prev;
21988 else
21990 while (s)
21992 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21993 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21994 s->x = x;
21995 x += s->width;
21996 s = s->next;
22003 /* The following macros are only called from draw_glyphs below.
22004 They reference the following parameters of that function directly:
22005 `w', `row', `area', and `overlap_p'
22006 as well as the following local variables:
22007 `s', `f', and `hdc' (in W32) */
22009 #ifdef HAVE_NTGUI
22010 /* On W32, silently add local `hdc' variable to argument list of
22011 init_glyph_string. */
22012 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22013 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
22014 #else
22015 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22016 init_glyph_string (s, char2b, w, row, area, start, hl)
22017 #endif
22019 /* Add a glyph string for a stretch glyph to the list of strings
22020 between HEAD and TAIL. START is the index of the stretch glyph in
22021 row area AREA of glyph row ROW. END is the index of the last glyph
22022 in that glyph row area. X is the current output position assigned
22023 to the new glyph string constructed. HL overrides that face of the
22024 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22025 is the right-most x-position of the drawing area. */
22027 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
22028 and below -- keep them on one line. */
22029 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22030 do \
22032 s = (struct glyph_string *) alloca (sizeof *s); \
22033 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22034 START = fill_stretch_glyph_string (s, START, END); \
22035 append_glyph_string (&HEAD, &TAIL, s); \
22036 s->x = (X); \
22038 while (0)
22041 /* Add a glyph string for an image glyph to the list of strings
22042 between HEAD and TAIL. START is the index of the image glyph in
22043 row area AREA of glyph row ROW. END is the index of the last glyph
22044 in that glyph row area. X is the current output position assigned
22045 to the new glyph string constructed. HL overrides that face of the
22046 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22047 is the right-most x-position of the drawing area. */
22049 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22050 do \
22052 s = (struct glyph_string *) alloca (sizeof *s); \
22053 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22054 fill_image_glyph_string (s); \
22055 append_glyph_string (&HEAD, &TAIL, s); \
22056 ++START; \
22057 s->x = (X); \
22059 while (0)
22062 /* Add a glyph string for a sequence of character glyphs to the list
22063 of strings between HEAD and TAIL. START is the index of the first
22064 glyph in row area AREA of glyph row ROW that is part of the new
22065 glyph string. END is the index of the last glyph in that glyph row
22066 area. X is the current output position assigned to the new glyph
22067 string constructed. HL overrides that face of the glyph; e.g. it
22068 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
22069 right-most x-position of the drawing area. */
22071 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22072 do \
22074 int face_id; \
22075 XChar2b *char2b; \
22077 face_id = (row)->glyphs[area][START].face_id; \
22079 s = (struct glyph_string *) alloca (sizeof *s); \
22080 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
22081 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22082 append_glyph_string (&HEAD, &TAIL, s); \
22083 s->x = (X); \
22084 START = fill_glyph_string (s, face_id, START, END, overlaps); \
22086 while (0)
22089 /* Add a glyph string for a composite sequence to the list of strings
22090 between HEAD and TAIL. START is the index of the first glyph in
22091 row area AREA of glyph row ROW that is part of the new glyph
22092 string. END is the index of the last glyph in that glyph row area.
22093 X is the current output position assigned to the new glyph string
22094 constructed. HL overrides that face of the glyph; e.g. it is
22095 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
22096 x-position of the drawing area. */
22098 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22099 do { \
22100 int face_id = (row)->glyphs[area][START].face_id; \
22101 struct face *base_face = FACE_FROM_ID (f, face_id); \
22102 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
22103 struct composition *cmp = composition_table[cmp_id]; \
22104 XChar2b *char2b; \
22105 struct glyph_string *first_s IF_LINT (= NULL); \
22106 int n; \
22108 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
22110 /* Make glyph_strings for each glyph sequence that is drawable by \
22111 the same face, and append them to HEAD/TAIL. */ \
22112 for (n = 0; n < cmp->glyph_len;) \
22114 s = (struct glyph_string *) alloca (sizeof *s); \
22115 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22116 append_glyph_string (&(HEAD), &(TAIL), s); \
22117 s->cmp = cmp; \
22118 s->cmp_from = n; \
22119 s->x = (X); \
22120 if (n == 0) \
22121 first_s = s; \
22122 n = fill_composite_glyph_string (s, base_face, overlaps); \
22125 ++START; \
22126 s = first_s; \
22127 } while (0)
22130 /* Add a glyph string for a glyph-string sequence to the list of strings
22131 between HEAD and TAIL. */
22133 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22134 do { \
22135 int face_id; \
22136 XChar2b *char2b; \
22137 Lisp_Object gstring; \
22139 face_id = (row)->glyphs[area][START].face_id; \
22140 gstring = (composition_gstring_from_id \
22141 ((row)->glyphs[area][START].u.cmp.id)); \
22142 s = (struct glyph_string *) alloca (sizeof *s); \
22143 char2b = (XChar2b *) alloca ((sizeof *char2b) \
22144 * LGSTRING_GLYPH_LEN (gstring)); \
22145 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22146 append_glyph_string (&(HEAD), &(TAIL), s); \
22147 s->x = (X); \
22148 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
22149 } while (0)
22152 /* Add a glyph string for a sequence of glyphless character's glyphs
22153 to the list of strings between HEAD and TAIL. The meanings of
22154 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
22156 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22157 do \
22159 int face_id; \
22161 face_id = (row)->glyphs[area][START].face_id; \
22163 s = (struct glyph_string *) alloca (sizeof *s); \
22164 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22165 append_glyph_string (&HEAD, &TAIL, s); \
22166 s->x = (X); \
22167 START = fill_glyphless_glyph_string (s, face_id, START, END, \
22168 overlaps); \
22170 while (0)
22173 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
22174 of AREA of glyph row ROW on window W between indices START and END.
22175 HL overrides the face for drawing glyph strings, e.g. it is
22176 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
22177 x-positions of the drawing area.
22179 This is an ugly monster macro construct because we must use alloca
22180 to allocate glyph strings (because draw_glyphs can be called
22181 asynchronously). */
22183 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22184 do \
22186 HEAD = TAIL = NULL; \
22187 while (START < END) \
22189 struct glyph *first_glyph = (row)->glyphs[area] + START; \
22190 switch (first_glyph->type) \
22192 case CHAR_GLYPH: \
22193 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
22194 HL, X, LAST_X); \
22195 break; \
22197 case COMPOSITE_GLYPH: \
22198 if (first_glyph->u.cmp.automatic) \
22199 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
22200 HL, X, LAST_X); \
22201 else \
22202 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
22203 HL, X, LAST_X); \
22204 break; \
22206 case STRETCH_GLYPH: \
22207 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
22208 HL, X, LAST_X); \
22209 break; \
22211 case IMAGE_GLYPH: \
22212 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
22213 HL, X, LAST_X); \
22214 break; \
22216 case GLYPHLESS_GLYPH: \
22217 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
22218 HL, X, LAST_X); \
22219 break; \
22221 default: \
22222 abort (); \
22225 if (s) \
22227 set_glyph_string_background_width (s, START, LAST_X); \
22228 (X) += s->width; \
22231 } while (0)
22234 /* Draw glyphs between START and END in AREA of ROW on window W,
22235 starting at x-position X. X is relative to AREA in W. HL is a
22236 face-override with the following meaning:
22238 DRAW_NORMAL_TEXT draw normally
22239 DRAW_CURSOR draw in cursor face
22240 DRAW_MOUSE_FACE draw in mouse face.
22241 DRAW_INVERSE_VIDEO draw in mode line face
22242 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
22243 DRAW_IMAGE_RAISED draw an image with a raised relief around it
22245 If OVERLAPS is non-zero, draw only the foreground of characters and
22246 clip to the physical height of ROW. Non-zero value also defines
22247 the overlapping part to be drawn:
22249 OVERLAPS_PRED overlap with preceding rows
22250 OVERLAPS_SUCC overlap with succeeding rows
22251 OVERLAPS_BOTH overlap with both preceding/succeeding rows
22252 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
22254 Value is the x-position reached, relative to AREA of W. */
22256 static int
22257 draw_glyphs (struct window *w, int x, struct glyph_row *row,
22258 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
22259 enum draw_glyphs_face hl, int overlaps)
22261 struct glyph_string *head, *tail;
22262 struct glyph_string *s;
22263 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
22264 int i, j, x_reached, last_x, area_left = 0;
22265 struct frame *f = XFRAME (WINDOW_FRAME (w));
22266 DECLARE_HDC (hdc);
22268 ALLOCATE_HDC (hdc, f);
22270 /* Let's rather be paranoid than getting a SEGV. */
22271 end = min (end, row->used[area]);
22272 start = max (0, start);
22273 start = min (end, start);
22275 /* Translate X to frame coordinates. Set last_x to the right
22276 end of the drawing area. */
22277 if (row->full_width_p)
22279 /* X is relative to the left edge of W, without scroll bars
22280 or fringes. */
22281 area_left = WINDOW_LEFT_EDGE_X (w);
22282 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
22284 else
22286 area_left = window_box_left (w, area);
22287 last_x = area_left + window_box_width (w, area);
22289 x += area_left;
22291 /* Build a doubly-linked list of glyph_string structures between
22292 head and tail from what we have to draw. Note that the macro
22293 BUILD_GLYPH_STRINGS will modify its start parameter. That's
22294 the reason we use a separate variable `i'. */
22295 i = start;
22296 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
22297 if (tail)
22298 x_reached = tail->x + tail->background_width;
22299 else
22300 x_reached = x;
22302 /* If there are any glyphs with lbearing < 0 or rbearing > width in
22303 the row, redraw some glyphs in front or following the glyph
22304 strings built above. */
22305 if (head && !overlaps && row->contains_overlapping_glyphs_p)
22307 struct glyph_string *h, *t;
22308 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
22309 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
22310 int check_mouse_face = 0;
22311 int dummy_x = 0;
22313 /* If mouse highlighting is on, we may need to draw adjacent
22314 glyphs using mouse-face highlighting. */
22315 if (area == TEXT_AREA && row->mouse_face_p)
22317 struct glyph_row *mouse_beg_row, *mouse_end_row;
22319 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
22320 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
22322 if (row >= mouse_beg_row && row <= mouse_end_row)
22324 check_mouse_face = 1;
22325 mouse_beg_col = (row == mouse_beg_row)
22326 ? hlinfo->mouse_face_beg_col : 0;
22327 mouse_end_col = (row == mouse_end_row)
22328 ? hlinfo->mouse_face_end_col
22329 : row->used[TEXT_AREA];
22333 /* Compute overhangs for all glyph strings. */
22334 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
22335 for (s = head; s; s = s->next)
22336 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
22338 /* Prepend glyph strings for glyphs in front of the first glyph
22339 string that are overwritten because of the first glyph
22340 string's left overhang. The background of all strings
22341 prepended must be drawn because the first glyph string
22342 draws over it. */
22343 i = left_overwritten (head);
22344 if (i >= 0)
22346 enum draw_glyphs_face overlap_hl;
22348 /* If this row contains mouse highlighting, attempt to draw
22349 the overlapped glyphs with the correct highlight. This
22350 code fails if the overlap encompasses more than one glyph
22351 and mouse-highlight spans only some of these glyphs.
22352 However, making it work perfectly involves a lot more
22353 code, and I don't know if the pathological case occurs in
22354 practice, so we'll stick to this for now. --- cyd */
22355 if (check_mouse_face
22356 && mouse_beg_col < start && mouse_end_col > i)
22357 overlap_hl = DRAW_MOUSE_FACE;
22358 else
22359 overlap_hl = DRAW_NORMAL_TEXT;
22361 j = i;
22362 BUILD_GLYPH_STRINGS (j, start, h, t,
22363 overlap_hl, dummy_x, last_x);
22364 start = i;
22365 compute_overhangs_and_x (t, head->x, 1);
22366 prepend_glyph_string_lists (&head, &tail, h, t);
22367 clip_head = head;
22370 /* Prepend glyph strings for glyphs in front of the first glyph
22371 string that overwrite that glyph string because of their
22372 right overhang. For these strings, only the foreground must
22373 be drawn, because it draws over the glyph string at `head'.
22374 The background must not be drawn because this would overwrite
22375 right overhangs of preceding glyphs for which no glyph
22376 strings exist. */
22377 i = left_overwriting (head);
22378 if (i >= 0)
22380 enum draw_glyphs_face overlap_hl;
22382 if (check_mouse_face
22383 && mouse_beg_col < start && mouse_end_col > i)
22384 overlap_hl = DRAW_MOUSE_FACE;
22385 else
22386 overlap_hl = DRAW_NORMAL_TEXT;
22388 clip_head = head;
22389 BUILD_GLYPH_STRINGS (i, start, h, t,
22390 overlap_hl, dummy_x, last_x);
22391 for (s = h; s; s = s->next)
22392 s->background_filled_p = 1;
22393 compute_overhangs_and_x (t, head->x, 1);
22394 prepend_glyph_string_lists (&head, &tail, h, t);
22397 /* Append glyphs strings for glyphs following the last glyph
22398 string tail that are overwritten by tail. The background of
22399 these strings has to be drawn because tail's foreground draws
22400 over it. */
22401 i = right_overwritten (tail);
22402 if (i >= 0)
22404 enum draw_glyphs_face overlap_hl;
22406 if (check_mouse_face
22407 && mouse_beg_col < i && mouse_end_col > end)
22408 overlap_hl = DRAW_MOUSE_FACE;
22409 else
22410 overlap_hl = DRAW_NORMAL_TEXT;
22412 BUILD_GLYPH_STRINGS (end, i, h, t,
22413 overlap_hl, x, last_x);
22414 /* Because BUILD_GLYPH_STRINGS updates the first argument,
22415 we don't have `end = i;' here. */
22416 compute_overhangs_and_x (h, tail->x + tail->width, 0);
22417 append_glyph_string_lists (&head, &tail, h, t);
22418 clip_tail = tail;
22421 /* Append glyph strings for glyphs following the last glyph
22422 string tail that overwrite tail. The foreground of such
22423 glyphs has to be drawn because it writes into the background
22424 of tail. The background must not be drawn because it could
22425 paint over the foreground of following glyphs. */
22426 i = right_overwriting (tail);
22427 if (i >= 0)
22429 enum draw_glyphs_face overlap_hl;
22430 if (check_mouse_face
22431 && mouse_beg_col < i && mouse_end_col > end)
22432 overlap_hl = DRAW_MOUSE_FACE;
22433 else
22434 overlap_hl = DRAW_NORMAL_TEXT;
22436 clip_tail = tail;
22437 i++; /* We must include the Ith glyph. */
22438 BUILD_GLYPH_STRINGS (end, i, h, t,
22439 overlap_hl, x, last_x);
22440 for (s = h; s; s = s->next)
22441 s->background_filled_p = 1;
22442 compute_overhangs_and_x (h, tail->x + tail->width, 0);
22443 append_glyph_string_lists (&head, &tail, h, t);
22445 if (clip_head || clip_tail)
22446 for (s = head; s; s = s->next)
22448 s->clip_head = clip_head;
22449 s->clip_tail = clip_tail;
22453 /* Draw all strings. */
22454 for (s = head; s; s = s->next)
22455 FRAME_RIF (f)->draw_glyph_string (s);
22457 #ifndef HAVE_NS
22458 /* When focus a sole frame and move horizontally, this sets on_p to 0
22459 causing a failure to erase prev cursor position. */
22460 if (area == TEXT_AREA
22461 && !row->full_width_p
22462 /* When drawing overlapping rows, only the glyph strings'
22463 foreground is drawn, which doesn't erase a cursor
22464 completely. */
22465 && !overlaps)
22467 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
22468 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
22469 : (tail ? tail->x + tail->background_width : x));
22470 x0 -= area_left;
22471 x1 -= area_left;
22473 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
22474 row->y, MATRIX_ROW_BOTTOM_Y (row));
22476 #endif
22478 /* Value is the x-position up to which drawn, relative to AREA of W.
22479 This doesn't include parts drawn because of overhangs. */
22480 if (row->full_width_p)
22481 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
22482 else
22483 x_reached -= area_left;
22485 RELEASE_HDC (hdc, f);
22487 return x_reached;
22490 /* Expand row matrix if too narrow. Don't expand if area
22491 is not present. */
22493 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
22495 if (!fonts_changed_p \
22496 && (it->glyph_row->glyphs[area] \
22497 < it->glyph_row->glyphs[area + 1])) \
22499 it->w->ncols_scale_factor++; \
22500 fonts_changed_p = 1; \
22504 /* Store one glyph for IT->char_to_display in IT->glyph_row.
22505 Called from x_produce_glyphs when IT->glyph_row is non-null. */
22507 static inline void
22508 append_glyph (struct it *it)
22510 struct glyph *glyph;
22511 enum glyph_row_area area = it->area;
22513 xassert (it->glyph_row);
22514 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
22516 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22517 if (glyph < it->glyph_row->glyphs[area + 1])
22519 /* If the glyph row is reversed, we need to prepend the glyph
22520 rather than append it. */
22521 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22523 struct glyph *g;
22525 /* Make room for the additional glyph. */
22526 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22527 g[1] = *g;
22528 glyph = it->glyph_row->glyphs[area];
22530 glyph->charpos = CHARPOS (it->position);
22531 glyph->object = it->object;
22532 if (it->pixel_width > 0)
22534 glyph->pixel_width = it->pixel_width;
22535 glyph->padding_p = 0;
22537 else
22539 /* Assure at least 1-pixel width. Otherwise, cursor can't
22540 be displayed correctly. */
22541 glyph->pixel_width = 1;
22542 glyph->padding_p = 1;
22544 glyph->ascent = it->ascent;
22545 glyph->descent = it->descent;
22546 glyph->voffset = it->voffset;
22547 glyph->type = CHAR_GLYPH;
22548 glyph->avoid_cursor_p = it->avoid_cursor_p;
22549 glyph->multibyte_p = it->multibyte_p;
22550 glyph->left_box_line_p = it->start_of_box_run_p;
22551 glyph->right_box_line_p = it->end_of_box_run_p;
22552 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22553 || it->phys_descent > it->descent);
22554 glyph->glyph_not_available_p = it->glyph_not_available_p;
22555 glyph->face_id = it->face_id;
22556 glyph->u.ch = it->char_to_display;
22557 glyph->slice.img = null_glyph_slice;
22558 glyph->font_type = FONT_TYPE_UNKNOWN;
22559 if (it->bidi_p)
22561 glyph->resolved_level = it->bidi_it.resolved_level;
22562 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22563 abort ();
22564 glyph->bidi_type = it->bidi_it.type;
22566 else
22568 glyph->resolved_level = 0;
22569 glyph->bidi_type = UNKNOWN_BT;
22571 ++it->glyph_row->used[area];
22573 else
22574 IT_EXPAND_MATRIX_WIDTH (it, area);
22577 /* Store one glyph for the composition IT->cmp_it.id in
22578 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
22579 non-null. */
22581 static inline void
22582 append_composite_glyph (struct it *it)
22584 struct glyph *glyph;
22585 enum glyph_row_area area = it->area;
22587 xassert (it->glyph_row);
22589 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22590 if (glyph < it->glyph_row->glyphs[area + 1])
22592 /* If the glyph row is reversed, we need to prepend the glyph
22593 rather than append it. */
22594 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
22596 struct glyph *g;
22598 /* Make room for the new glyph. */
22599 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
22600 g[1] = *g;
22601 glyph = it->glyph_row->glyphs[it->area];
22603 glyph->charpos = it->cmp_it.charpos;
22604 glyph->object = it->object;
22605 glyph->pixel_width = it->pixel_width;
22606 glyph->ascent = it->ascent;
22607 glyph->descent = it->descent;
22608 glyph->voffset = it->voffset;
22609 glyph->type = COMPOSITE_GLYPH;
22610 if (it->cmp_it.ch < 0)
22612 glyph->u.cmp.automatic = 0;
22613 glyph->u.cmp.id = it->cmp_it.id;
22614 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
22616 else
22618 glyph->u.cmp.automatic = 1;
22619 glyph->u.cmp.id = it->cmp_it.id;
22620 glyph->slice.cmp.from = it->cmp_it.from;
22621 glyph->slice.cmp.to = it->cmp_it.to - 1;
22623 glyph->avoid_cursor_p = it->avoid_cursor_p;
22624 glyph->multibyte_p = it->multibyte_p;
22625 glyph->left_box_line_p = it->start_of_box_run_p;
22626 glyph->right_box_line_p = it->end_of_box_run_p;
22627 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22628 || it->phys_descent > it->descent);
22629 glyph->padding_p = 0;
22630 glyph->glyph_not_available_p = 0;
22631 glyph->face_id = it->face_id;
22632 glyph->font_type = FONT_TYPE_UNKNOWN;
22633 if (it->bidi_p)
22635 glyph->resolved_level = it->bidi_it.resolved_level;
22636 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22637 abort ();
22638 glyph->bidi_type = it->bidi_it.type;
22640 ++it->glyph_row->used[area];
22642 else
22643 IT_EXPAND_MATRIX_WIDTH (it, area);
22647 /* Change IT->ascent and IT->height according to the setting of
22648 IT->voffset. */
22650 static inline void
22651 take_vertical_position_into_account (struct it *it)
22653 if (it->voffset)
22655 if (it->voffset < 0)
22656 /* Increase the ascent so that we can display the text higher
22657 in the line. */
22658 it->ascent -= it->voffset;
22659 else
22660 /* Increase the descent so that we can display the text lower
22661 in the line. */
22662 it->descent += it->voffset;
22667 /* Produce glyphs/get display metrics for the image IT is loaded with.
22668 See the description of struct display_iterator in dispextern.h for
22669 an overview of struct display_iterator. */
22671 static void
22672 produce_image_glyph (struct it *it)
22674 struct image *img;
22675 struct face *face;
22676 int glyph_ascent, crop;
22677 struct glyph_slice slice;
22679 xassert (it->what == IT_IMAGE);
22681 face = FACE_FROM_ID (it->f, it->face_id);
22682 xassert (face);
22683 /* Make sure X resources of the face is loaded. */
22684 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22686 if (it->image_id < 0)
22688 /* Fringe bitmap. */
22689 it->ascent = it->phys_ascent = 0;
22690 it->descent = it->phys_descent = 0;
22691 it->pixel_width = 0;
22692 it->nglyphs = 0;
22693 return;
22696 img = IMAGE_FROM_ID (it->f, it->image_id);
22697 xassert (img);
22698 /* Make sure X resources of the image is loaded. */
22699 prepare_image_for_display (it->f, img);
22701 slice.x = slice.y = 0;
22702 slice.width = img->width;
22703 slice.height = img->height;
22705 if (INTEGERP (it->slice.x))
22706 slice.x = XINT (it->slice.x);
22707 else if (FLOATP (it->slice.x))
22708 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
22710 if (INTEGERP (it->slice.y))
22711 slice.y = XINT (it->slice.y);
22712 else if (FLOATP (it->slice.y))
22713 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
22715 if (INTEGERP (it->slice.width))
22716 slice.width = XINT (it->slice.width);
22717 else if (FLOATP (it->slice.width))
22718 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
22720 if (INTEGERP (it->slice.height))
22721 slice.height = XINT (it->slice.height);
22722 else if (FLOATP (it->slice.height))
22723 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
22725 if (slice.x >= img->width)
22726 slice.x = img->width;
22727 if (slice.y >= img->height)
22728 slice.y = img->height;
22729 if (slice.x + slice.width >= img->width)
22730 slice.width = img->width - slice.x;
22731 if (slice.y + slice.height > img->height)
22732 slice.height = img->height - slice.y;
22734 if (slice.width == 0 || slice.height == 0)
22735 return;
22737 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
22739 it->descent = slice.height - glyph_ascent;
22740 if (slice.y == 0)
22741 it->descent += img->vmargin;
22742 if (slice.y + slice.height == img->height)
22743 it->descent += img->vmargin;
22744 it->phys_descent = it->descent;
22746 it->pixel_width = slice.width;
22747 if (slice.x == 0)
22748 it->pixel_width += img->hmargin;
22749 if (slice.x + slice.width == img->width)
22750 it->pixel_width += img->hmargin;
22752 /* It's quite possible for images to have an ascent greater than
22753 their height, so don't get confused in that case. */
22754 if (it->descent < 0)
22755 it->descent = 0;
22757 it->nglyphs = 1;
22759 if (face->box != FACE_NO_BOX)
22761 if (face->box_line_width > 0)
22763 if (slice.y == 0)
22764 it->ascent += face->box_line_width;
22765 if (slice.y + slice.height == img->height)
22766 it->descent += face->box_line_width;
22769 if (it->start_of_box_run_p && slice.x == 0)
22770 it->pixel_width += eabs (face->box_line_width);
22771 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
22772 it->pixel_width += eabs (face->box_line_width);
22775 take_vertical_position_into_account (it);
22777 /* Automatically crop wide image glyphs at right edge so we can
22778 draw the cursor on same display row. */
22779 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
22780 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
22782 it->pixel_width -= crop;
22783 slice.width -= crop;
22786 if (it->glyph_row)
22788 struct glyph *glyph;
22789 enum glyph_row_area area = it->area;
22791 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22792 if (glyph < it->glyph_row->glyphs[area + 1])
22794 glyph->charpos = CHARPOS (it->position);
22795 glyph->object = it->object;
22796 glyph->pixel_width = it->pixel_width;
22797 glyph->ascent = glyph_ascent;
22798 glyph->descent = it->descent;
22799 glyph->voffset = it->voffset;
22800 glyph->type = IMAGE_GLYPH;
22801 glyph->avoid_cursor_p = it->avoid_cursor_p;
22802 glyph->multibyte_p = it->multibyte_p;
22803 glyph->left_box_line_p = it->start_of_box_run_p;
22804 glyph->right_box_line_p = it->end_of_box_run_p;
22805 glyph->overlaps_vertically_p = 0;
22806 glyph->padding_p = 0;
22807 glyph->glyph_not_available_p = 0;
22808 glyph->face_id = it->face_id;
22809 glyph->u.img_id = img->id;
22810 glyph->slice.img = slice;
22811 glyph->font_type = FONT_TYPE_UNKNOWN;
22812 if (it->bidi_p)
22814 glyph->resolved_level = it->bidi_it.resolved_level;
22815 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22816 abort ();
22817 glyph->bidi_type = it->bidi_it.type;
22819 ++it->glyph_row->used[area];
22821 else
22822 IT_EXPAND_MATRIX_WIDTH (it, area);
22827 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
22828 of the glyph, WIDTH and HEIGHT are the width and height of the
22829 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
22831 static void
22832 append_stretch_glyph (struct it *it, Lisp_Object object,
22833 int width, int height, int ascent)
22835 struct glyph *glyph;
22836 enum glyph_row_area area = it->area;
22838 xassert (ascent >= 0 && ascent <= height);
22840 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22841 if (glyph < it->glyph_row->glyphs[area + 1])
22843 /* If the glyph row is reversed, we need to prepend the glyph
22844 rather than append it. */
22845 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22847 struct glyph *g;
22849 /* Make room for the additional glyph. */
22850 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22851 g[1] = *g;
22852 glyph = it->glyph_row->glyphs[area];
22854 glyph->charpos = CHARPOS (it->position);
22855 glyph->object = object;
22856 glyph->pixel_width = width;
22857 glyph->ascent = ascent;
22858 glyph->descent = height - ascent;
22859 glyph->voffset = it->voffset;
22860 glyph->type = STRETCH_GLYPH;
22861 glyph->avoid_cursor_p = it->avoid_cursor_p;
22862 glyph->multibyte_p = it->multibyte_p;
22863 glyph->left_box_line_p = it->start_of_box_run_p;
22864 glyph->right_box_line_p = it->end_of_box_run_p;
22865 glyph->overlaps_vertically_p = 0;
22866 glyph->padding_p = 0;
22867 glyph->glyph_not_available_p = 0;
22868 glyph->face_id = it->face_id;
22869 glyph->u.stretch.ascent = ascent;
22870 glyph->u.stretch.height = height;
22871 glyph->slice.img = null_glyph_slice;
22872 glyph->font_type = FONT_TYPE_UNKNOWN;
22873 if (it->bidi_p)
22875 glyph->resolved_level = it->bidi_it.resolved_level;
22876 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22877 abort ();
22878 glyph->bidi_type = it->bidi_it.type;
22880 else
22882 glyph->resolved_level = 0;
22883 glyph->bidi_type = UNKNOWN_BT;
22885 ++it->glyph_row->used[area];
22887 else
22888 IT_EXPAND_MATRIX_WIDTH (it, area);
22892 /* Produce a stretch glyph for iterator IT. IT->object is the value
22893 of the glyph property displayed. The value must be a list
22894 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
22895 being recognized:
22897 1. `:width WIDTH' specifies that the space should be WIDTH *
22898 canonical char width wide. WIDTH may be an integer or floating
22899 point number.
22901 2. `:relative-width FACTOR' specifies that the width of the stretch
22902 should be computed from the width of the first character having the
22903 `glyph' property, and should be FACTOR times that width.
22905 3. `:align-to HPOS' specifies that the space should be wide enough
22906 to reach HPOS, a value in canonical character units.
22908 Exactly one of the above pairs must be present.
22910 4. `:height HEIGHT' specifies that the height of the stretch produced
22911 should be HEIGHT, measured in canonical character units.
22913 5. `:relative-height FACTOR' specifies that the height of the
22914 stretch should be FACTOR times the height of the characters having
22915 the glyph property.
22917 Either none or exactly one of 4 or 5 must be present.
22919 6. `:ascent ASCENT' specifies that ASCENT percent of the height
22920 of the stretch should be used for the ascent of the stretch.
22921 ASCENT must be in the range 0 <= ASCENT <= 100. */
22923 static void
22924 produce_stretch_glyph (struct it *it)
22926 /* (space :width WIDTH :height HEIGHT ...) */
22927 Lisp_Object prop, plist;
22928 int width = 0, height = 0, align_to = -1;
22929 int zero_width_ok_p = 0, zero_height_ok_p = 0;
22930 int ascent = 0;
22931 double tem;
22932 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22933 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
22935 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22937 /* List should start with `space'. */
22938 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
22939 plist = XCDR (it->object);
22941 /* Compute the width of the stretch. */
22942 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
22943 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
22945 /* Absolute width `:width WIDTH' specified and valid. */
22946 zero_width_ok_p = 1;
22947 width = (int)tem;
22949 else if (prop = Fplist_get (plist, QCrelative_width),
22950 NUMVAL (prop) > 0)
22952 /* Relative width `:relative-width FACTOR' specified and valid.
22953 Compute the width of the characters having the `glyph'
22954 property. */
22955 struct it it2;
22956 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
22958 it2 = *it;
22959 if (it->multibyte_p)
22960 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
22961 else
22963 it2.c = it2.char_to_display = *p, it2.len = 1;
22964 if (! ASCII_CHAR_P (it2.c))
22965 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
22968 it2.glyph_row = NULL;
22969 it2.what = IT_CHARACTER;
22970 x_produce_glyphs (&it2);
22971 width = NUMVAL (prop) * it2.pixel_width;
22973 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
22974 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
22976 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
22977 align_to = (align_to < 0
22979 : align_to - window_box_left_offset (it->w, TEXT_AREA));
22980 else if (align_to < 0)
22981 align_to = window_box_left_offset (it->w, TEXT_AREA);
22982 width = max (0, (int)tem + align_to - it->current_x);
22983 zero_width_ok_p = 1;
22985 else
22986 /* Nothing specified -> width defaults to canonical char width. */
22987 width = FRAME_COLUMN_WIDTH (it->f);
22989 if (width <= 0 && (width < 0 || !zero_width_ok_p))
22990 width = 1;
22992 /* Compute height. */
22993 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
22994 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22996 height = (int)tem;
22997 zero_height_ok_p = 1;
22999 else if (prop = Fplist_get (plist, QCrelative_height),
23000 NUMVAL (prop) > 0)
23001 height = FONT_HEIGHT (font) * NUMVAL (prop);
23002 else
23003 height = FONT_HEIGHT (font);
23005 if (height <= 0 && (height < 0 || !zero_height_ok_p))
23006 height = 1;
23008 /* Compute percentage of height used for ascent. If
23009 `:ascent ASCENT' is present and valid, use that. Otherwise,
23010 derive the ascent from the font in use. */
23011 if (prop = Fplist_get (plist, QCascent),
23012 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
23013 ascent = height * NUMVAL (prop) / 100.0;
23014 else if (!NILP (prop)
23015 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23016 ascent = min (max (0, (int)tem), height);
23017 else
23018 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
23020 if (width > 0 && it->line_wrap != TRUNCATE
23021 && it->current_x + width > it->last_visible_x)
23022 width = it->last_visible_x - it->current_x - 1;
23024 if (width > 0 && height > 0 && it->glyph_row)
23026 Lisp_Object object = it->stack[it->sp - 1].string;
23027 if (!STRINGP (object))
23028 object = it->w->buffer;
23029 append_stretch_glyph (it, object, width, height, ascent);
23032 it->pixel_width = width;
23033 it->ascent = it->phys_ascent = ascent;
23034 it->descent = it->phys_descent = height - it->ascent;
23035 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
23037 take_vertical_position_into_account (it);
23040 /* Calculate line-height and line-spacing properties.
23041 An integer value specifies explicit pixel value.
23042 A float value specifies relative value to current face height.
23043 A cons (float . face-name) specifies relative value to
23044 height of specified face font.
23046 Returns height in pixels, or nil. */
23049 static Lisp_Object
23050 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
23051 int boff, int override)
23053 Lisp_Object face_name = Qnil;
23054 int ascent, descent, height;
23056 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
23057 return val;
23059 if (CONSP (val))
23061 face_name = XCAR (val);
23062 val = XCDR (val);
23063 if (!NUMBERP (val))
23064 val = make_number (1);
23065 if (NILP (face_name))
23067 height = it->ascent + it->descent;
23068 goto scale;
23072 if (NILP (face_name))
23074 font = FRAME_FONT (it->f);
23075 boff = FRAME_BASELINE_OFFSET (it->f);
23077 else if (EQ (face_name, Qt))
23079 override = 0;
23081 else
23083 int face_id;
23084 struct face *face;
23086 face_id = lookup_named_face (it->f, face_name, 0);
23087 if (face_id < 0)
23088 return make_number (-1);
23090 face = FACE_FROM_ID (it->f, face_id);
23091 font = face->font;
23092 if (font == NULL)
23093 return make_number (-1);
23094 boff = font->baseline_offset;
23095 if (font->vertical_centering)
23096 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23099 ascent = FONT_BASE (font) + boff;
23100 descent = FONT_DESCENT (font) - boff;
23102 if (override)
23104 it->override_ascent = ascent;
23105 it->override_descent = descent;
23106 it->override_boff = boff;
23109 height = ascent + descent;
23111 scale:
23112 if (FLOATP (val))
23113 height = (int)(XFLOAT_DATA (val) * height);
23114 else if (INTEGERP (val))
23115 height *= XINT (val);
23117 return make_number (height);
23121 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
23122 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
23123 and only if this is for a character for which no font was found.
23125 If the display method (it->glyphless_method) is
23126 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
23127 length of the acronym or the hexadecimal string, UPPER_XOFF and
23128 UPPER_YOFF are pixel offsets for the upper part of the string,
23129 LOWER_XOFF and LOWER_YOFF are for the lower part.
23131 For the other display methods, LEN through LOWER_YOFF are zero. */
23133 static void
23134 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
23135 short upper_xoff, short upper_yoff,
23136 short lower_xoff, short lower_yoff)
23138 struct glyph *glyph;
23139 enum glyph_row_area area = it->area;
23141 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23142 if (glyph < it->glyph_row->glyphs[area + 1])
23144 /* If the glyph row is reversed, we need to prepend the glyph
23145 rather than append it. */
23146 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23148 struct glyph *g;
23150 /* Make room for the additional glyph. */
23151 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23152 g[1] = *g;
23153 glyph = it->glyph_row->glyphs[area];
23155 glyph->charpos = CHARPOS (it->position);
23156 glyph->object = it->object;
23157 glyph->pixel_width = it->pixel_width;
23158 glyph->ascent = it->ascent;
23159 glyph->descent = it->descent;
23160 glyph->voffset = it->voffset;
23161 glyph->type = GLYPHLESS_GLYPH;
23162 glyph->u.glyphless.method = it->glyphless_method;
23163 glyph->u.glyphless.for_no_font = for_no_font;
23164 glyph->u.glyphless.len = len;
23165 glyph->u.glyphless.ch = it->c;
23166 glyph->slice.glyphless.upper_xoff = upper_xoff;
23167 glyph->slice.glyphless.upper_yoff = upper_yoff;
23168 glyph->slice.glyphless.lower_xoff = lower_xoff;
23169 glyph->slice.glyphless.lower_yoff = lower_yoff;
23170 glyph->avoid_cursor_p = it->avoid_cursor_p;
23171 glyph->multibyte_p = it->multibyte_p;
23172 glyph->left_box_line_p = it->start_of_box_run_p;
23173 glyph->right_box_line_p = it->end_of_box_run_p;
23174 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23175 || it->phys_descent > it->descent);
23176 glyph->padding_p = 0;
23177 glyph->glyph_not_available_p = 0;
23178 glyph->face_id = face_id;
23179 glyph->font_type = FONT_TYPE_UNKNOWN;
23180 if (it->bidi_p)
23182 glyph->resolved_level = it->bidi_it.resolved_level;
23183 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23184 abort ();
23185 glyph->bidi_type = it->bidi_it.type;
23187 ++it->glyph_row->used[area];
23189 else
23190 IT_EXPAND_MATRIX_WIDTH (it, area);
23194 /* Produce a glyph for a glyphless character for iterator IT.
23195 IT->glyphless_method specifies which method to use for displaying
23196 the character. See the description of enum
23197 glyphless_display_method in dispextern.h for the detail.
23199 FOR_NO_FONT is nonzero if and only if this is for a character for
23200 which no font was found. ACRONYM, if non-nil, is an acronym string
23201 for the character. */
23203 static void
23204 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
23206 int face_id;
23207 struct face *face;
23208 struct font *font;
23209 int base_width, base_height, width, height;
23210 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
23211 int len;
23213 /* Get the metrics of the base font. We always refer to the current
23214 ASCII face. */
23215 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
23216 font = face->font ? face->font : FRAME_FONT (it->f);
23217 it->ascent = FONT_BASE (font) + font->baseline_offset;
23218 it->descent = FONT_DESCENT (font) - font->baseline_offset;
23219 base_height = it->ascent + it->descent;
23220 base_width = font->average_width;
23222 /* Get a face ID for the glyph by utilizing a cache (the same way as
23223 doen for `escape-glyph' in get_next_display_element). */
23224 if (it->f == last_glyphless_glyph_frame
23225 && it->face_id == last_glyphless_glyph_face_id)
23227 face_id = last_glyphless_glyph_merged_face_id;
23229 else
23231 /* Merge the `glyphless-char' face into the current face. */
23232 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
23233 last_glyphless_glyph_frame = it->f;
23234 last_glyphless_glyph_face_id = it->face_id;
23235 last_glyphless_glyph_merged_face_id = face_id;
23238 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
23240 it->pixel_width = THIN_SPACE_WIDTH;
23241 len = 0;
23242 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23244 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
23246 width = CHAR_WIDTH (it->c);
23247 if (width == 0)
23248 width = 1;
23249 else if (width > 4)
23250 width = 4;
23251 it->pixel_width = base_width * width;
23252 len = 0;
23253 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23255 else
23257 char buf[7];
23258 const char *str;
23259 unsigned int code[6];
23260 int upper_len;
23261 int ascent, descent;
23262 struct font_metrics metrics_upper, metrics_lower;
23264 face = FACE_FROM_ID (it->f, face_id);
23265 font = face->font ? face->font : FRAME_FONT (it->f);
23266 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23268 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
23270 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
23271 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
23272 if (CONSP (acronym))
23273 acronym = XCAR (acronym);
23274 str = STRINGP (acronym) ? SSDATA (acronym) : "";
23276 else
23278 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
23279 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
23280 str = buf;
23282 for (len = 0; str[len] && ASCII_BYTE_P (str[len]); len++)
23283 code[len] = font->driver->encode_char (font, str[len]);
23284 upper_len = (len + 1) / 2;
23285 font->driver->text_extents (font, code, upper_len,
23286 &metrics_upper);
23287 font->driver->text_extents (font, code + upper_len, len - upper_len,
23288 &metrics_lower);
23292 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
23293 width = max (metrics_upper.width, metrics_lower.width) + 4;
23294 upper_xoff = upper_yoff = 2; /* the typical case */
23295 if (base_width >= width)
23297 /* Align the upper to the left, the lower to the right. */
23298 it->pixel_width = base_width;
23299 lower_xoff = base_width - 2 - metrics_lower.width;
23301 else
23303 /* Center the shorter one. */
23304 it->pixel_width = width;
23305 if (metrics_upper.width >= metrics_lower.width)
23306 lower_xoff = (width - metrics_lower.width) / 2;
23307 else
23309 /* FIXME: This code doesn't look right. It formerly was
23310 missing the "lower_xoff = 0;", which couldn't have
23311 been right since it left lower_xoff uninitialized. */
23312 lower_xoff = 0;
23313 upper_xoff = (width - metrics_upper.width) / 2;
23317 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
23318 top, bottom, and between upper and lower strings. */
23319 height = (metrics_upper.ascent + metrics_upper.descent
23320 + metrics_lower.ascent + metrics_lower.descent) + 5;
23321 /* Center vertically.
23322 H:base_height, D:base_descent
23323 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
23325 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
23326 descent = D - H/2 + h/2;
23327 lower_yoff = descent - 2 - ld;
23328 upper_yoff = lower_yoff - la - 1 - ud; */
23329 ascent = - (it->descent - (base_height + height + 1) / 2);
23330 descent = it->descent - (base_height - height) / 2;
23331 lower_yoff = descent - 2 - metrics_lower.descent;
23332 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
23333 - metrics_upper.descent);
23334 /* Don't make the height shorter than the base height. */
23335 if (height > base_height)
23337 it->ascent = ascent;
23338 it->descent = descent;
23342 it->phys_ascent = it->ascent;
23343 it->phys_descent = it->descent;
23344 if (it->glyph_row)
23345 append_glyphless_glyph (it, face_id, for_no_font, len,
23346 upper_xoff, upper_yoff,
23347 lower_xoff, lower_yoff);
23348 it->nglyphs = 1;
23349 take_vertical_position_into_account (it);
23353 /* RIF:
23354 Produce glyphs/get display metrics for the display element IT is
23355 loaded with. See the description of struct it in dispextern.h
23356 for an overview of struct it. */
23358 void
23359 x_produce_glyphs (struct it *it)
23361 int extra_line_spacing = it->extra_line_spacing;
23363 it->glyph_not_available_p = 0;
23365 if (it->what == IT_CHARACTER)
23367 XChar2b char2b;
23368 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23369 struct font *font = face->font;
23370 struct font_metrics *pcm = NULL;
23371 int boff; /* baseline offset */
23373 if (font == NULL)
23375 /* When no suitable font is found, display this character by
23376 the method specified in the first extra slot of
23377 Vglyphless_char_display. */
23378 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
23380 xassert (it->what == IT_GLYPHLESS);
23381 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
23382 goto done;
23385 boff = font->baseline_offset;
23386 if (font->vertical_centering)
23387 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23389 if (it->char_to_display != '\n' && it->char_to_display != '\t')
23391 int stretched_p;
23393 it->nglyphs = 1;
23395 if (it->override_ascent >= 0)
23397 it->ascent = it->override_ascent;
23398 it->descent = it->override_descent;
23399 boff = it->override_boff;
23401 else
23403 it->ascent = FONT_BASE (font) + boff;
23404 it->descent = FONT_DESCENT (font) - boff;
23407 if (get_char_glyph_code (it->char_to_display, font, &char2b))
23409 pcm = get_per_char_metric (font, &char2b);
23410 if (pcm->width == 0
23411 && pcm->rbearing == 0 && pcm->lbearing == 0)
23412 pcm = NULL;
23415 if (pcm)
23417 it->phys_ascent = pcm->ascent + boff;
23418 it->phys_descent = pcm->descent - boff;
23419 it->pixel_width = pcm->width;
23421 else
23423 it->glyph_not_available_p = 1;
23424 it->phys_ascent = it->ascent;
23425 it->phys_descent = it->descent;
23426 it->pixel_width = font->space_width;
23429 if (it->constrain_row_ascent_descent_p)
23431 if (it->descent > it->max_descent)
23433 it->ascent += it->descent - it->max_descent;
23434 it->descent = it->max_descent;
23436 if (it->ascent > it->max_ascent)
23438 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
23439 it->ascent = it->max_ascent;
23441 it->phys_ascent = min (it->phys_ascent, it->ascent);
23442 it->phys_descent = min (it->phys_descent, it->descent);
23443 extra_line_spacing = 0;
23446 /* If this is a space inside a region of text with
23447 `space-width' property, change its width. */
23448 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
23449 if (stretched_p)
23450 it->pixel_width *= XFLOATINT (it->space_width);
23452 /* If face has a box, add the box thickness to the character
23453 height. If character has a box line to the left and/or
23454 right, add the box line width to the character's width. */
23455 if (face->box != FACE_NO_BOX)
23457 int thick = face->box_line_width;
23459 if (thick > 0)
23461 it->ascent += thick;
23462 it->descent += thick;
23464 else
23465 thick = -thick;
23467 if (it->start_of_box_run_p)
23468 it->pixel_width += thick;
23469 if (it->end_of_box_run_p)
23470 it->pixel_width += thick;
23473 /* If face has an overline, add the height of the overline
23474 (1 pixel) and a 1 pixel margin to the character height. */
23475 if (face->overline_p)
23476 it->ascent += overline_margin;
23478 if (it->constrain_row_ascent_descent_p)
23480 if (it->ascent > it->max_ascent)
23481 it->ascent = it->max_ascent;
23482 if (it->descent > it->max_descent)
23483 it->descent = it->max_descent;
23486 take_vertical_position_into_account (it);
23488 /* If we have to actually produce glyphs, do it. */
23489 if (it->glyph_row)
23491 if (stretched_p)
23493 /* Translate a space with a `space-width' property
23494 into a stretch glyph. */
23495 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
23496 / FONT_HEIGHT (font));
23497 append_stretch_glyph (it, it->object, it->pixel_width,
23498 it->ascent + it->descent, ascent);
23500 else
23501 append_glyph (it);
23503 /* If characters with lbearing or rbearing are displayed
23504 in this line, record that fact in a flag of the
23505 glyph row. This is used to optimize X output code. */
23506 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
23507 it->glyph_row->contains_overlapping_glyphs_p = 1;
23509 if (! stretched_p && it->pixel_width == 0)
23510 /* We assure that all visible glyphs have at least 1-pixel
23511 width. */
23512 it->pixel_width = 1;
23514 else if (it->char_to_display == '\n')
23516 /* A newline has no width, but we need the height of the
23517 line. But if previous part of the line sets a height,
23518 don't increase that height */
23520 Lisp_Object height;
23521 Lisp_Object total_height = Qnil;
23523 it->override_ascent = -1;
23524 it->pixel_width = 0;
23525 it->nglyphs = 0;
23527 height = get_it_property (it, Qline_height);
23528 /* Split (line-height total-height) list */
23529 if (CONSP (height)
23530 && CONSP (XCDR (height))
23531 && NILP (XCDR (XCDR (height))))
23533 total_height = XCAR (XCDR (height));
23534 height = XCAR (height);
23536 height = calc_line_height_property (it, height, font, boff, 1);
23538 if (it->override_ascent >= 0)
23540 it->ascent = it->override_ascent;
23541 it->descent = it->override_descent;
23542 boff = it->override_boff;
23544 else
23546 it->ascent = FONT_BASE (font) + boff;
23547 it->descent = FONT_DESCENT (font) - boff;
23550 if (EQ (height, Qt))
23552 if (it->descent > it->max_descent)
23554 it->ascent += it->descent - it->max_descent;
23555 it->descent = it->max_descent;
23557 if (it->ascent > it->max_ascent)
23559 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
23560 it->ascent = it->max_ascent;
23562 it->phys_ascent = min (it->phys_ascent, it->ascent);
23563 it->phys_descent = min (it->phys_descent, it->descent);
23564 it->constrain_row_ascent_descent_p = 1;
23565 extra_line_spacing = 0;
23567 else
23569 Lisp_Object spacing;
23571 it->phys_ascent = it->ascent;
23572 it->phys_descent = it->descent;
23574 if ((it->max_ascent > 0 || it->max_descent > 0)
23575 && face->box != FACE_NO_BOX
23576 && face->box_line_width > 0)
23578 it->ascent += face->box_line_width;
23579 it->descent += face->box_line_width;
23581 if (!NILP (height)
23582 && XINT (height) > it->ascent + it->descent)
23583 it->ascent = XINT (height) - it->descent;
23585 if (!NILP (total_height))
23586 spacing = calc_line_height_property (it, total_height, font, boff, 0);
23587 else
23589 spacing = get_it_property (it, Qline_spacing);
23590 spacing = calc_line_height_property (it, spacing, font, boff, 0);
23592 if (INTEGERP (spacing))
23594 extra_line_spacing = XINT (spacing);
23595 if (!NILP (total_height))
23596 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
23600 else /* i.e. (it->char_to_display == '\t') */
23602 if (font->space_width > 0)
23604 int tab_width = it->tab_width * font->space_width;
23605 int x = it->current_x + it->continuation_lines_width;
23606 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
23608 /* If the distance from the current position to the next tab
23609 stop is less than a space character width, use the
23610 tab stop after that. */
23611 if (next_tab_x - x < font->space_width)
23612 next_tab_x += tab_width;
23614 it->pixel_width = next_tab_x - x;
23615 it->nglyphs = 1;
23616 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
23617 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
23619 if (it->glyph_row)
23621 append_stretch_glyph (it, it->object, it->pixel_width,
23622 it->ascent + it->descent, it->ascent);
23625 else
23627 it->pixel_width = 0;
23628 it->nglyphs = 1;
23632 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
23634 /* A static composition.
23636 Note: A composition is represented as one glyph in the
23637 glyph matrix. There are no padding glyphs.
23639 Important note: pixel_width, ascent, and descent are the
23640 values of what is drawn by draw_glyphs (i.e. the values of
23641 the overall glyphs composed). */
23642 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23643 int boff; /* baseline offset */
23644 struct composition *cmp = composition_table[it->cmp_it.id];
23645 int glyph_len = cmp->glyph_len;
23646 struct font *font = face->font;
23648 it->nglyphs = 1;
23650 /* If we have not yet calculated pixel size data of glyphs of
23651 the composition for the current face font, calculate them
23652 now. Theoretically, we have to check all fonts for the
23653 glyphs, but that requires much time and memory space. So,
23654 here we check only the font of the first glyph. This may
23655 lead to incorrect display, but it's very rare, and C-l
23656 (recenter-top-bottom) can correct the display anyway. */
23657 if (! cmp->font || cmp->font != font)
23659 /* Ascent and descent of the font of the first character
23660 of this composition (adjusted by baseline offset).
23661 Ascent and descent of overall glyphs should not be less
23662 than these, respectively. */
23663 int font_ascent, font_descent, font_height;
23664 /* Bounding box of the overall glyphs. */
23665 int leftmost, rightmost, lowest, highest;
23666 int lbearing, rbearing;
23667 int i, width, ascent, descent;
23668 int left_padded = 0, right_padded = 0;
23669 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
23670 XChar2b char2b;
23671 struct font_metrics *pcm;
23672 int font_not_found_p;
23673 EMACS_INT pos;
23675 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
23676 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
23677 break;
23678 if (glyph_len < cmp->glyph_len)
23679 right_padded = 1;
23680 for (i = 0; i < glyph_len; i++)
23682 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
23683 break;
23684 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
23686 if (i > 0)
23687 left_padded = 1;
23689 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
23690 : IT_CHARPOS (*it));
23691 /* If no suitable font is found, use the default font. */
23692 font_not_found_p = font == NULL;
23693 if (font_not_found_p)
23695 face = face->ascii_face;
23696 font = face->font;
23698 boff = font->baseline_offset;
23699 if (font->vertical_centering)
23700 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23701 font_ascent = FONT_BASE (font) + boff;
23702 font_descent = FONT_DESCENT (font) - boff;
23703 font_height = FONT_HEIGHT (font);
23705 cmp->font = (void *) font;
23707 pcm = NULL;
23708 if (! font_not_found_p)
23710 get_char_face_and_encoding (it->f, c, it->face_id,
23711 &char2b, 0);
23712 pcm = get_per_char_metric (font, &char2b);
23715 /* Initialize the bounding box. */
23716 if (pcm)
23718 width = pcm->width;
23719 ascent = pcm->ascent;
23720 descent = pcm->descent;
23721 lbearing = pcm->lbearing;
23722 rbearing = pcm->rbearing;
23724 else
23726 width = font->space_width;
23727 ascent = FONT_BASE (font);
23728 descent = FONT_DESCENT (font);
23729 lbearing = 0;
23730 rbearing = width;
23733 rightmost = width;
23734 leftmost = 0;
23735 lowest = - descent + boff;
23736 highest = ascent + boff;
23738 if (! font_not_found_p
23739 && font->default_ascent
23740 && CHAR_TABLE_P (Vuse_default_ascent)
23741 && !NILP (Faref (Vuse_default_ascent,
23742 make_number (it->char_to_display))))
23743 highest = font->default_ascent + boff;
23745 /* Draw the first glyph at the normal position. It may be
23746 shifted to right later if some other glyphs are drawn
23747 at the left. */
23748 cmp->offsets[i * 2] = 0;
23749 cmp->offsets[i * 2 + 1] = boff;
23750 cmp->lbearing = lbearing;
23751 cmp->rbearing = rbearing;
23753 /* Set cmp->offsets for the remaining glyphs. */
23754 for (i++; i < glyph_len; i++)
23756 int left, right, btm, top;
23757 int ch = COMPOSITION_GLYPH (cmp, i);
23758 int face_id;
23759 struct face *this_face;
23761 if (ch == '\t')
23762 ch = ' ';
23763 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
23764 this_face = FACE_FROM_ID (it->f, face_id);
23765 font = this_face->font;
23767 if (font == NULL)
23768 pcm = NULL;
23769 else
23771 get_char_face_and_encoding (it->f, ch, face_id,
23772 &char2b, 0);
23773 pcm = get_per_char_metric (font, &char2b);
23775 if (! pcm)
23776 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
23777 else
23779 width = pcm->width;
23780 ascent = pcm->ascent;
23781 descent = pcm->descent;
23782 lbearing = pcm->lbearing;
23783 rbearing = pcm->rbearing;
23784 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
23786 /* Relative composition with or without
23787 alternate chars. */
23788 left = (leftmost + rightmost - width) / 2;
23789 btm = - descent + boff;
23790 if (font->relative_compose
23791 && (! CHAR_TABLE_P (Vignore_relative_composition)
23792 || NILP (Faref (Vignore_relative_composition,
23793 make_number (ch)))))
23796 if (- descent >= font->relative_compose)
23797 /* One extra pixel between two glyphs. */
23798 btm = highest + 1;
23799 else if (ascent <= 0)
23800 /* One extra pixel between two glyphs. */
23801 btm = lowest - 1 - ascent - descent;
23804 else
23806 /* A composition rule is specified by an integer
23807 value that encodes global and new reference
23808 points (GREF and NREF). GREF and NREF are
23809 specified by numbers as below:
23811 0---1---2 -- ascent
23815 9--10--11 -- center
23817 ---3---4---5--- baseline
23819 6---7---8 -- descent
23821 int rule = COMPOSITION_RULE (cmp, i);
23822 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
23824 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
23825 grefx = gref % 3, nrefx = nref % 3;
23826 grefy = gref / 3, nrefy = nref / 3;
23827 if (xoff)
23828 xoff = font_height * (xoff - 128) / 256;
23829 if (yoff)
23830 yoff = font_height * (yoff - 128) / 256;
23832 left = (leftmost
23833 + grefx * (rightmost - leftmost) / 2
23834 - nrefx * width / 2
23835 + xoff);
23837 btm = ((grefy == 0 ? highest
23838 : grefy == 1 ? 0
23839 : grefy == 2 ? lowest
23840 : (highest + lowest) / 2)
23841 - (nrefy == 0 ? ascent + descent
23842 : nrefy == 1 ? descent - boff
23843 : nrefy == 2 ? 0
23844 : (ascent + descent) / 2)
23845 + yoff);
23848 cmp->offsets[i * 2] = left;
23849 cmp->offsets[i * 2 + 1] = btm + descent;
23851 /* Update the bounding box of the overall glyphs. */
23852 if (width > 0)
23854 right = left + width;
23855 if (left < leftmost)
23856 leftmost = left;
23857 if (right > rightmost)
23858 rightmost = right;
23860 top = btm + descent + ascent;
23861 if (top > highest)
23862 highest = top;
23863 if (btm < lowest)
23864 lowest = btm;
23866 if (cmp->lbearing > left + lbearing)
23867 cmp->lbearing = left + lbearing;
23868 if (cmp->rbearing < left + rbearing)
23869 cmp->rbearing = left + rbearing;
23873 /* If there are glyphs whose x-offsets are negative,
23874 shift all glyphs to the right and make all x-offsets
23875 non-negative. */
23876 if (leftmost < 0)
23878 for (i = 0; i < cmp->glyph_len; i++)
23879 cmp->offsets[i * 2] -= leftmost;
23880 rightmost -= leftmost;
23881 cmp->lbearing -= leftmost;
23882 cmp->rbearing -= leftmost;
23885 if (left_padded && cmp->lbearing < 0)
23887 for (i = 0; i < cmp->glyph_len; i++)
23888 cmp->offsets[i * 2] -= cmp->lbearing;
23889 rightmost -= cmp->lbearing;
23890 cmp->rbearing -= cmp->lbearing;
23891 cmp->lbearing = 0;
23893 if (right_padded && rightmost < cmp->rbearing)
23895 rightmost = cmp->rbearing;
23898 cmp->pixel_width = rightmost;
23899 cmp->ascent = highest;
23900 cmp->descent = - lowest;
23901 if (cmp->ascent < font_ascent)
23902 cmp->ascent = font_ascent;
23903 if (cmp->descent < font_descent)
23904 cmp->descent = font_descent;
23907 if (it->glyph_row
23908 && (cmp->lbearing < 0
23909 || cmp->rbearing > cmp->pixel_width))
23910 it->glyph_row->contains_overlapping_glyphs_p = 1;
23912 it->pixel_width = cmp->pixel_width;
23913 it->ascent = it->phys_ascent = cmp->ascent;
23914 it->descent = it->phys_descent = cmp->descent;
23915 if (face->box != FACE_NO_BOX)
23917 int thick = face->box_line_width;
23919 if (thick > 0)
23921 it->ascent += thick;
23922 it->descent += thick;
23924 else
23925 thick = - thick;
23927 if (it->start_of_box_run_p)
23928 it->pixel_width += thick;
23929 if (it->end_of_box_run_p)
23930 it->pixel_width += thick;
23933 /* If face has an overline, add the height of the overline
23934 (1 pixel) and a 1 pixel margin to the character height. */
23935 if (face->overline_p)
23936 it->ascent += overline_margin;
23938 take_vertical_position_into_account (it);
23939 if (it->ascent < 0)
23940 it->ascent = 0;
23941 if (it->descent < 0)
23942 it->descent = 0;
23944 if (it->glyph_row)
23945 append_composite_glyph (it);
23947 else if (it->what == IT_COMPOSITION)
23949 /* A dynamic (automatic) composition. */
23950 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23951 Lisp_Object gstring;
23952 struct font_metrics metrics;
23954 gstring = composition_gstring_from_id (it->cmp_it.id);
23955 it->pixel_width
23956 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
23957 &metrics);
23958 if (it->glyph_row
23959 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
23960 it->glyph_row->contains_overlapping_glyphs_p = 1;
23961 it->ascent = it->phys_ascent = metrics.ascent;
23962 it->descent = it->phys_descent = metrics.descent;
23963 if (face->box != FACE_NO_BOX)
23965 int thick = face->box_line_width;
23967 if (thick > 0)
23969 it->ascent += thick;
23970 it->descent += thick;
23972 else
23973 thick = - thick;
23975 if (it->start_of_box_run_p)
23976 it->pixel_width += thick;
23977 if (it->end_of_box_run_p)
23978 it->pixel_width += thick;
23980 /* If face has an overline, add the height of the overline
23981 (1 pixel) and a 1 pixel margin to the character height. */
23982 if (face->overline_p)
23983 it->ascent += overline_margin;
23984 take_vertical_position_into_account (it);
23985 if (it->ascent < 0)
23986 it->ascent = 0;
23987 if (it->descent < 0)
23988 it->descent = 0;
23990 if (it->glyph_row)
23991 append_composite_glyph (it);
23993 else if (it->what == IT_GLYPHLESS)
23994 produce_glyphless_glyph (it, 0, Qnil);
23995 else if (it->what == IT_IMAGE)
23996 produce_image_glyph (it);
23997 else if (it->what == IT_STRETCH)
23998 produce_stretch_glyph (it);
24000 done:
24001 /* Accumulate dimensions. Note: can't assume that it->descent > 0
24002 because this isn't true for images with `:ascent 100'. */
24003 xassert (it->ascent >= 0 && it->descent >= 0);
24004 if (it->area == TEXT_AREA)
24005 it->current_x += it->pixel_width;
24007 if (extra_line_spacing > 0)
24009 it->descent += extra_line_spacing;
24010 if (extra_line_spacing > it->max_extra_line_spacing)
24011 it->max_extra_line_spacing = extra_line_spacing;
24014 it->max_ascent = max (it->max_ascent, it->ascent);
24015 it->max_descent = max (it->max_descent, it->descent);
24016 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
24017 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
24020 /* EXPORT for RIF:
24021 Output LEN glyphs starting at START at the nominal cursor position.
24022 Advance the nominal cursor over the text. The global variable
24023 updated_window contains the window being updated, updated_row is
24024 the glyph row being updated, and updated_area is the area of that
24025 row being updated. */
24027 void
24028 x_write_glyphs (struct glyph *start, int len)
24030 int x, hpos;
24032 xassert (updated_window && updated_row);
24033 BLOCK_INPUT;
24035 /* Write glyphs. */
24037 hpos = start - updated_row->glyphs[updated_area];
24038 x = draw_glyphs (updated_window, output_cursor.x,
24039 updated_row, updated_area,
24040 hpos, hpos + len,
24041 DRAW_NORMAL_TEXT, 0);
24043 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
24044 if (updated_area == TEXT_AREA
24045 && updated_window->phys_cursor_on_p
24046 && updated_window->phys_cursor.vpos == output_cursor.vpos
24047 && updated_window->phys_cursor.hpos >= hpos
24048 && updated_window->phys_cursor.hpos < hpos + len)
24049 updated_window->phys_cursor_on_p = 0;
24051 UNBLOCK_INPUT;
24053 /* Advance the output cursor. */
24054 output_cursor.hpos += len;
24055 output_cursor.x = x;
24059 /* EXPORT for RIF:
24060 Insert LEN glyphs from START at the nominal cursor position. */
24062 void
24063 x_insert_glyphs (struct glyph *start, int len)
24065 struct frame *f;
24066 struct window *w;
24067 int line_height, shift_by_width, shifted_region_width;
24068 struct glyph_row *row;
24069 struct glyph *glyph;
24070 int frame_x, frame_y;
24071 EMACS_INT hpos;
24073 xassert (updated_window && updated_row);
24074 BLOCK_INPUT;
24075 w = updated_window;
24076 f = XFRAME (WINDOW_FRAME (w));
24078 /* Get the height of the line we are in. */
24079 row = updated_row;
24080 line_height = row->height;
24082 /* Get the width of the glyphs to insert. */
24083 shift_by_width = 0;
24084 for (glyph = start; glyph < start + len; ++glyph)
24085 shift_by_width += glyph->pixel_width;
24087 /* Get the width of the region to shift right. */
24088 shifted_region_width = (window_box_width (w, updated_area)
24089 - output_cursor.x
24090 - shift_by_width);
24092 /* Shift right. */
24093 frame_x = window_box_left (w, updated_area) + output_cursor.x;
24094 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
24096 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
24097 line_height, shift_by_width);
24099 /* Write the glyphs. */
24100 hpos = start - row->glyphs[updated_area];
24101 draw_glyphs (w, output_cursor.x, row, updated_area,
24102 hpos, hpos + len,
24103 DRAW_NORMAL_TEXT, 0);
24105 /* Advance the output cursor. */
24106 output_cursor.hpos += len;
24107 output_cursor.x += shift_by_width;
24108 UNBLOCK_INPUT;
24112 /* EXPORT for RIF:
24113 Erase the current text line from the nominal cursor position
24114 (inclusive) to pixel column TO_X (exclusive). The idea is that
24115 everything from TO_X onward is already erased.
24117 TO_X is a pixel position relative to updated_area of
24118 updated_window. TO_X == -1 means clear to the end of this area. */
24120 void
24121 x_clear_end_of_line (int to_x)
24123 struct frame *f;
24124 struct window *w = updated_window;
24125 int max_x, min_y, max_y;
24126 int from_x, from_y, to_y;
24128 xassert (updated_window && updated_row);
24129 f = XFRAME (w->frame);
24131 if (updated_row->full_width_p)
24132 max_x = WINDOW_TOTAL_WIDTH (w);
24133 else
24134 max_x = window_box_width (w, updated_area);
24135 max_y = window_text_bottom_y (w);
24137 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
24138 of window. For TO_X > 0, truncate to end of drawing area. */
24139 if (to_x == 0)
24140 return;
24141 else if (to_x < 0)
24142 to_x = max_x;
24143 else
24144 to_x = min (to_x, max_x);
24146 to_y = min (max_y, output_cursor.y + updated_row->height);
24148 /* Notice if the cursor will be cleared by this operation. */
24149 if (!updated_row->full_width_p)
24150 notice_overwritten_cursor (w, updated_area,
24151 output_cursor.x, -1,
24152 updated_row->y,
24153 MATRIX_ROW_BOTTOM_Y (updated_row));
24155 from_x = output_cursor.x;
24157 /* Translate to frame coordinates. */
24158 if (updated_row->full_width_p)
24160 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
24161 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
24163 else
24165 int area_left = window_box_left (w, updated_area);
24166 from_x += area_left;
24167 to_x += area_left;
24170 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
24171 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
24172 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
24174 /* Prevent inadvertently clearing to end of the X window. */
24175 if (to_x > from_x && to_y > from_y)
24177 BLOCK_INPUT;
24178 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
24179 to_x - from_x, to_y - from_y);
24180 UNBLOCK_INPUT;
24184 #endif /* HAVE_WINDOW_SYSTEM */
24188 /***********************************************************************
24189 Cursor types
24190 ***********************************************************************/
24192 /* Value is the internal representation of the specified cursor type
24193 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
24194 of the bar cursor. */
24196 static enum text_cursor_kinds
24197 get_specified_cursor_type (Lisp_Object arg, int *width)
24199 enum text_cursor_kinds type;
24201 if (NILP (arg))
24202 return NO_CURSOR;
24204 if (EQ (arg, Qbox))
24205 return FILLED_BOX_CURSOR;
24207 if (EQ (arg, Qhollow))
24208 return HOLLOW_BOX_CURSOR;
24210 if (EQ (arg, Qbar))
24212 *width = 2;
24213 return BAR_CURSOR;
24216 if (CONSP (arg)
24217 && EQ (XCAR (arg), Qbar)
24218 && INTEGERP (XCDR (arg))
24219 && XINT (XCDR (arg)) >= 0)
24221 *width = XINT (XCDR (arg));
24222 return BAR_CURSOR;
24225 if (EQ (arg, Qhbar))
24227 *width = 2;
24228 return HBAR_CURSOR;
24231 if (CONSP (arg)
24232 && EQ (XCAR (arg), Qhbar)
24233 && INTEGERP (XCDR (arg))
24234 && XINT (XCDR (arg)) >= 0)
24236 *width = XINT (XCDR (arg));
24237 return HBAR_CURSOR;
24240 /* Treat anything unknown as "hollow box cursor".
24241 It was bad to signal an error; people have trouble fixing
24242 .Xdefaults with Emacs, when it has something bad in it. */
24243 type = HOLLOW_BOX_CURSOR;
24245 return type;
24248 /* Set the default cursor types for specified frame. */
24249 void
24250 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
24252 int width = 1;
24253 Lisp_Object tem;
24255 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
24256 FRAME_CURSOR_WIDTH (f) = width;
24258 /* By default, set up the blink-off state depending on the on-state. */
24260 tem = Fassoc (arg, Vblink_cursor_alist);
24261 if (!NILP (tem))
24263 FRAME_BLINK_OFF_CURSOR (f)
24264 = get_specified_cursor_type (XCDR (tem), &width);
24265 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
24267 else
24268 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
24272 #ifdef HAVE_WINDOW_SYSTEM
24274 /* Return the cursor we want to be displayed in window W. Return
24275 width of bar/hbar cursor through WIDTH arg. Return with
24276 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
24277 (i.e. if the `system caret' should track this cursor).
24279 In a mini-buffer window, we want the cursor only to appear if we
24280 are reading input from this window. For the selected window, we
24281 want the cursor type given by the frame parameter or buffer local
24282 setting of cursor-type. If explicitly marked off, draw no cursor.
24283 In all other cases, we want a hollow box cursor. */
24285 static enum text_cursor_kinds
24286 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
24287 int *active_cursor)
24289 struct frame *f = XFRAME (w->frame);
24290 struct buffer *b = XBUFFER (w->buffer);
24291 int cursor_type = DEFAULT_CURSOR;
24292 Lisp_Object alt_cursor;
24293 int non_selected = 0;
24295 *active_cursor = 1;
24297 /* Echo area */
24298 if (cursor_in_echo_area
24299 && FRAME_HAS_MINIBUF_P (f)
24300 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
24302 if (w == XWINDOW (echo_area_window))
24304 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
24306 *width = FRAME_CURSOR_WIDTH (f);
24307 return FRAME_DESIRED_CURSOR (f);
24309 else
24310 return get_specified_cursor_type (BVAR (b, cursor_type), width);
24313 *active_cursor = 0;
24314 non_selected = 1;
24317 /* Detect a nonselected window or nonselected frame. */
24318 else if (w != XWINDOW (f->selected_window)
24319 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
24321 *active_cursor = 0;
24323 if (MINI_WINDOW_P (w) && minibuf_level == 0)
24324 return NO_CURSOR;
24326 non_selected = 1;
24329 /* Never display a cursor in a window in which cursor-type is nil. */
24330 if (NILP (BVAR (b, cursor_type)))
24331 return NO_CURSOR;
24333 /* Get the normal cursor type for this window. */
24334 if (EQ (BVAR (b, cursor_type), Qt))
24336 cursor_type = FRAME_DESIRED_CURSOR (f);
24337 *width = FRAME_CURSOR_WIDTH (f);
24339 else
24340 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
24342 /* Use cursor-in-non-selected-windows instead
24343 for non-selected window or frame. */
24344 if (non_selected)
24346 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
24347 if (!EQ (Qt, alt_cursor))
24348 return get_specified_cursor_type (alt_cursor, width);
24349 /* t means modify the normal cursor type. */
24350 if (cursor_type == FILLED_BOX_CURSOR)
24351 cursor_type = HOLLOW_BOX_CURSOR;
24352 else if (cursor_type == BAR_CURSOR && *width > 1)
24353 --*width;
24354 return cursor_type;
24357 /* Use normal cursor if not blinked off. */
24358 if (!w->cursor_off_p)
24360 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
24362 if (cursor_type == FILLED_BOX_CURSOR)
24364 /* Using a block cursor on large images can be very annoying.
24365 So use a hollow cursor for "large" images.
24366 If image is not transparent (no mask), also use hollow cursor. */
24367 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
24368 if (img != NULL && IMAGEP (img->spec))
24370 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
24371 where N = size of default frame font size.
24372 This should cover most of the "tiny" icons people may use. */
24373 if (!img->mask
24374 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
24375 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
24376 cursor_type = HOLLOW_BOX_CURSOR;
24379 else if (cursor_type != NO_CURSOR)
24381 /* Display current only supports BOX and HOLLOW cursors for images.
24382 So for now, unconditionally use a HOLLOW cursor when cursor is
24383 not a solid box cursor. */
24384 cursor_type = HOLLOW_BOX_CURSOR;
24387 return cursor_type;
24390 /* Cursor is blinked off, so determine how to "toggle" it. */
24392 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
24393 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
24394 return get_specified_cursor_type (XCDR (alt_cursor), width);
24396 /* Then see if frame has specified a specific blink off cursor type. */
24397 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
24399 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
24400 return FRAME_BLINK_OFF_CURSOR (f);
24403 #if 0
24404 /* Some people liked having a permanently visible blinking cursor,
24405 while others had very strong opinions against it. So it was
24406 decided to remove it. KFS 2003-09-03 */
24408 /* Finally perform built-in cursor blinking:
24409 filled box <-> hollow box
24410 wide [h]bar <-> narrow [h]bar
24411 narrow [h]bar <-> no cursor
24412 other type <-> no cursor */
24414 if (cursor_type == FILLED_BOX_CURSOR)
24415 return HOLLOW_BOX_CURSOR;
24417 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
24419 *width = 1;
24420 return cursor_type;
24422 #endif
24424 return NO_CURSOR;
24428 /* Notice when the text cursor of window W has been completely
24429 overwritten by a drawing operation that outputs glyphs in AREA
24430 starting at X0 and ending at X1 in the line starting at Y0 and
24431 ending at Y1. X coordinates are area-relative. X1 < 0 means all
24432 the rest of the line after X0 has been written. Y coordinates
24433 are window-relative. */
24435 static void
24436 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
24437 int x0, int x1, int y0, int y1)
24439 int cx0, cx1, cy0, cy1;
24440 struct glyph_row *row;
24442 if (!w->phys_cursor_on_p)
24443 return;
24444 if (area != TEXT_AREA)
24445 return;
24447 if (w->phys_cursor.vpos < 0
24448 || w->phys_cursor.vpos >= w->current_matrix->nrows
24449 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
24450 !(row->enabled_p && row->displays_text_p)))
24451 return;
24453 if (row->cursor_in_fringe_p)
24455 row->cursor_in_fringe_p = 0;
24456 draw_fringe_bitmap (w, row, row->reversed_p);
24457 w->phys_cursor_on_p = 0;
24458 return;
24461 cx0 = w->phys_cursor.x;
24462 cx1 = cx0 + w->phys_cursor_width;
24463 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
24464 return;
24466 /* The cursor image will be completely removed from the
24467 screen if the output area intersects the cursor area in
24468 y-direction. When we draw in [y0 y1[, and some part of
24469 the cursor is at y < y0, that part must have been drawn
24470 before. When scrolling, the cursor is erased before
24471 actually scrolling, so we don't come here. When not
24472 scrolling, the rows above the old cursor row must have
24473 changed, and in this case these rows must have written
24474 over the cursor image.
24476 Likewise if part of the cursor is below y1, with the
24477 exception of the cursor being in the first blank row at
24478 the buffer and window end because update_text_area
24479 doesn't draw that row. (Except when it does, but
24480 that's handled in update_text_area.) */
24482 cy0 = w->phys_cursor.y;
24483 cy1 = cy0 + w->phys_cursor_height;
24484 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
24485 return;
24487 w->phys_cursor_on_p = 0;
24490 #endif /* HAVE_WINDOW_SYSTEM */
24493 /************************************************************************
24494 Mouse Face
24495 ************************************************************************/
24497 #ifdef HAVE_WINDOW_SYSTEM
24499 /* EXPORT for RIF:
24500 Fix the display of area AREA of overlapping row ROW in window W
24501 with respect to the overlapping part OVERLAPS. */
24503 void
24504 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
24505 enum glyph_row_area area, int overlaps)
24507 int i, x;
24509 BLOCK_INPUT;
24511 x = 0;
24512 for (i = 0; i < row->used[area];)
24514 if (row->glyphs[area][i].overlaps_vertically_p)
24516 int start = i, start_x = x;
24520 x += row->glyphs[area][i].pixel_width;
24521 ++i;
24523 while (i < row->used[area]
24524 && row->glyphs[area][i].overlaps_vertically_p);
24526 draw_glyphs (w, start_x, row, area,
24527 start, i,
24528 DRAW_NORMAL_TEXT, overlaps);
24530 else
24532 x += row->glyphs[area][i].pixel_width;
24533 ++i;
24537 UNBLOCK_INPUT;
24541 /* EXPORT:
24542 Draw the cursor glyph of window W in glyph row ROW. See the
24543 comment of draw_glyphs for the meaning of HL. */
24545 void
24546 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
24547 enum draw_glyphs_face hl)
24549 /* If cursor hpos is out of bounds, don't draw garbage. This can
24550 happen in mini-buffer windows when switching between echo area
24551 glyphs and mini-buffer. */
24552 if ((row->reversed_p
24553 ? (w->phys_cursor.hpos >= 0)
24554 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
24556 int on_p = w->phys_cursor_on_p;
24557 int x1;
24558 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
24559 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
24560 hl, 0);
24561 w->phys_cursor_on_p = on_p;
24563 if (hl == DRAW_CURSOR)
24564 w->phys_cursor_width = x1 - w->phys_cursor.x;
24565 /* When we erase the cursor, and ROW is overlapped by other
24566 rows, make sure that these overlapping parts of other rows
24567 are redrawn. */
24568 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
24570 w->phys_cursor_width = x1 - w->phys_cursor.x;
24572 if (row > w->current_matrix->rows
24573 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
24574 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
24575 OVERLAPS_ERASED_CURSOR);
24577 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
24578 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
24579 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
24580 OVERLAPS_ERASED_CURSOR);
24586 /* EXPORT:
24587 Erase the image of a cursor of window W from the screen. */
24589 void
24590 erase_phys_cursor (struct window *w)
24592 struct frame *f = XFRAME (w->frame);
24593 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24594 int hpos = w->phys_cursor.hpos;
24595 int vpos = w->phys_cursor.vpos;
24596 int mouse_face_here_p = 0;
24597 struct glyph_matrix *active_glyphs = w->current_matrix;
24598 struct glyph_row *cursor_row;
24599 struct glyph *cursor_glyph;
24600 enum draw_glyphs_face hl;
24602 /* No cursor displayed or row invalidated => nothing to do on the
24603 screen. */
24604 if (w->phys_cursor_type == NO_CURSOR)
24605 goto mark_cursor_off;
24607 /* VPOS >= active_glyphs->nrows means that window has been resized.
24608 Don't bother to erase the cursor. */
24609 if (vpos >= active_glyphs->nrows)
24610 goto mark_cursor_off;
24612 /* If row containing cursor is marked invalid, there is nothing we
24613 can do. */
24614 cursor_row = MATRIX_ROW (active_glyphs, vpos);
24615 if (!cursor_row->enabled_p)
24616 goto mark_cursor_off;
24618 /* If line spacing is > 0, old cursor may only be partially visible in
24619 window after split-window. So adjust visible height. */
24620 cursor_row->visible_height = min (cursor_row->visible_height,
24621 window_text_bottom_y (w) - cursor_row->y);
24623 /* If row is completely invisible, don't attempt to delete a cursor which
24624 isn't there. This can happen if cursor is at top of a window, and
24625 we switch to a buffer with a header line in that window. */
24626 if (cursor_row->visible_height <= 0)
24627 goto mark_cursor_off;
24629 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
24630 if (cursor_row->cursor_in_fringe_p)
24632 cursor_row->cursor_in_fringe_p = 0;
24633 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
24634 goto mark_cursor_off;
24637 /* This can happen when the new row is shorter than the old one.
24638 In this case, either draw_glyphs or clear_end_of_line
24639 should have cleared the cursor. Note that we wouldn't be
24640 able to erase the cursor in this case because we don't have a
24641 cursor glyph at hand. */
24642 if ((cursor_row->reversed_p
24643 ? (w->phys_cursor.hpos < 0)
24644 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
24645 goto mark_cursor_off;
24647 /* If the cursor is in the mouse face area, redisplay that when
24648 we clear the cursor. */
24649 if (! NILP (hlinfo->mouse_face_window)
24650 && coords_in_mouse_face_p (w, hpos, vpos)
24651 /* Don't redraw the cursor's spot in mouse face if it is at the
24652 end of a line (on a newline). The cursor appears there, but
24653 mouse highlighting does not. */
24654 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
24655 mouse_face_here_p = 1;
24657 /* Maybe clear the display under the cursor. */
24658 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
24660 int x, y, left_x;
24661 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
24662 int width;
24664 cursor_glyph = get_phys_cursor_glyph (w);
24665 if (cursor_glyph == NULL)
24666 goto mark_cursor_off;
24668 width = cursor_glyph->pixel_width;
24669 left_x = window_box_left_offset (w, TEXT_AREA);
24670 x = w->phys_cursor.x;
24671 if (x < left_x)
24672 width -= left_x - x;
24673 width = min (width, window_box_width (w, TEXT_AREA) - x);
24674 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
24675 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
24677 if (width > 0)
24678 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
24681 /* Erase the cursor by redrawing the character underneath it. */
24682 if (mouse_face_here_p)
24683 hl = DRAW_MOUSE_FACE;
24684 else
24685 hl = DRAW_NORMAL_TEXT;
24686 draw_phys_cursor_glyph (w, cursor_row, hl);
24688 mark_cursor_off:
24689 w->phys_cursor_on_p = 0;
24690 w->phys_cursor_type = NO_CURSOR;
24694 /* EXPORT:
24695 Display or clear cursor of window W. If ON is zero, clear the
24696 cursor. If it is non-zero, display the cursor. If ON is nonzero,
24697 where to put the cursor is specified by HPOS, VPOS, X and Y. */
24699 void
24700 display_and_set_cursor (struct window *w, int on,
24701 int hpos, int vpos, int x, int y)
24703 struct frame *f = XFRAME (w->frame);
24704 int new_cursor_type;
24705 int new_cursor_width;
24706 int active_cursor;
24707 struct glyph_row *glyph_row;
24708 struct glyph *glyph;
24710 /* This is pointless on invisible frames, and dangerous on garbaged
24711 windows and frames; in the latter case, the frame or window may
24712 be in the midst of changing its size, and x and y may be off the
24713 window. */
24714 if (! FRAME_VISIBLE_P (f)
24715 || FRAME_GARBAGED_P (f)
24716 || vpos >= w->current_matrix->nrows
24717 || hpos >= w->current_matrix->matrix_w)
24718 return;
24720 /* If cursor is off and we want it off, return quickly. */
24721 if (!on && !w->phys_cursor_on_p)
24722 return;
24724 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
24725 /* If cursor row is not enabled, we don't really know where to
24726 display the cursor. */
24727 if (!glyph_row->enabled_p)
24729 w->phys_cursor_on_p = 0;
24730 return;
24733 glyph = NULL;
24734 if (!glyph_row->exact_window_width_line_p
24735 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
24736 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
24738 xassert (interrupt_input_blocked);
24740 /* Set new_cursor_type to the cursor we want to be displayed. */
24741 new_cursor_type = get_window_cursor_type (w, glyph,
24742 &new_cursor_width, &active_cursor);
24744 /* If cursor is currently being shown and we don't want it to be or
24745 it is in the wrong place, or the cursor type is not what we want,
24746 erase it. */
24747 if (w->phys_cursor_on_p
24748 && (!on
24749 || w->phys_cursor.x != x
24750 || w->phys_cursor.y != y
24751 || new_cursor_type != w->phys_cursor_type
24752 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
24753 && new_cursor_width != w->phys_cursor_width)))
24754 erase_phys_cursor (w);
24756 /* Don't check phys_cursor_on_p here because that flag is only set
24757 to zero in some cases where we know that the cursor has been
24758 completely erased, to avoid the extra work of erasing the cursor
24759 twice. In other words, phys_cursor_on_p can be 1 and the cursor
24760 still not be visible, or it has only been partly erased. */
24761 if (on)
24763 w->phys_cursor_ascent = glyph_row->ascent;
24764 w->phys_cursor_height = glyph_row->height;
24766 /* Set phys_cursor_.* before x_draw_.* is called because some
24767 of them may need the information. */
24768 w->phys_cursor.x = x;
24769 w->phys_cursor.y = glyph_row->y;
24770 w->phys_cursor.hpos = hpos;
24771 w->phys_cursor.vpos = vpos;
24774 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
24775 new_cursor_type, new_cursor_width,
24776 on, active_cursor);
24780 /* Switch the display of W's cursor on or off, according to the value
24781 of ON. */
24783 static void
24784 update_window_cursor (struct window *w, int on)
24786 /* Don't update cursor in windows whose frame is in the process
24787 of being deleted. */
24788 if (w->current_matrix)
24790 BLOCK_INPUT;
24791 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
24792 w->phys_cursor.x, w->phys_cursor.y);
24793 UNBLOCK_INPUT;
24798 /* Call update_window_cursor with parameter ON_P on all leaf windows
24799 in the window tree rooted at W. */
24801 static void
24802 update_cursor_in_window_tree (struct window *w, int on_p)
24804 while (w)
24806 if (!NILP (w->hchild))
24807 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
24808 else if (!NILP (w->vchild))
24809 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
24810 else
24811 update_window_cursor (w, on_p);
24813 w = NILP (w->next) ? 0 : XWINDOW (w->next);
24818 /* EXPORT:
24819 Display the cursor on window W, or clear it, according to ON_P.
24820 Don't change the cursor's position. */
24822 void
24823 x_update_cursor (struct frame *f, int on_p)
24825 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
24829 /* EXPORT:
24830 Clear the cursor of window W to background color, and mark the
24831 cursor as not shown. This is used when the text where the cursor
24832 is about to be rewritten. */
24834 void
24835 x_clear_cursor (struct window *w)
24837 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
24838 update_window_cursor (w, 0);
24841 #endif /* HAVE_WINDOW_SYSTEM */
24843 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
24844 and MSDOS. */
24845 static void
24846 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
24847 int start_hpos, int end_hpos,
24848 enum draw_glyphs_face draw)
24850 #ifdef HAVE_WINDOW_SYSTEM
24851 if (FRAME_WINDOW_P (XFRAME (w->frame)))
24853 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
24854 return;
24856 #endif
24857 #if defined (HAVE_GPM) || defined (MSDOS)
24858 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
24859 #endif
24862 /* Display the active region described by mouse_face_* according to DRAW. */
24864 static void
24865 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
24867 struct window *w = XWINDOW (hlinfo->mouse_face_window);
24868 struct frame *f = XFRAME (WINDOW_FRAME (w));
24870 if (/* If window is in the process of being destroyed, don't bother
24871 to do anything. */
24872 w->current_matrix != NULL
24873 /* Don't update mouse highlight if hidden */
24874 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
24875 /* Recognize when we are called to operate on rows that don't exist
24876 anymore. This can happen when a window is split. */
24877 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
24879 int phys_cursor_on_p = w->phys_cursor_on_p;
24880 struct glyph_row *row, *first, *last;
24882 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
24883 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
24885 for (row = first; row <= last && row->enabled_p; ++row)
24887 int start_hpos, end_hpos, start_x;
24889 /* For all but the first row, the highlight starts at column 0. */
24890 if (row == first)
24892 /* R2L rows have BEG and END in reversed order, but the
24893 screen drawing geometry is always left to right. So
24894 we need to mirror the beginning and end of the
24895 highlighted area in R2L rows. */
24896 if (!row->reversed_p)
24898 start_hpos = hlinfo->mouse_face_beg_col;
24899 start_x = hlinfo->mouse_face_beg_x;
24901 else if (row == last)
24903 start_hpos = hlinfo->mouse_face_end_col;
24904 start_x = hlinfo->mouse_face_end_x;
24906 else
24908 start_hpos = 0;
24909 start_x = 0;
24912 else if (row->reversed_p && row == last)
24914 start_hpos = hlinfo->mouse_face_end_col;
24915 start_x = hlinfo->mouse_face_end_x;
24917 else
24919 start_hpos = 0;
24920 start_x = 0;
24923 if (row == last)
24925 if (!row->reversed_p)
24926 end_hpos = hlinfo->mouse_face_end_col;
24927 else if (row == first)
24928 end_hpos = hlinfo->mouse_face_beg_col;
24929 else
24931 end_hpos = row->used[TEXT_AREA];
24932 if (draw == DRAW_NORMAL_TEXT)
24933 row->fill_line_p = 1; /* Clear to end of line */
24936 else if (row->reversed_p && row == first)
24937 end_hpos = hlinfo->mouse_face_beg_col;
24938 else
24940 end_hpos = row->used[TEXT_AREA];
24941 if (draw == DRAW_NORMAL_TEXT)
24942 row->fill_line_p = 1; /* Clear to end of line */
24945 if (end_hpos > start_hpos)
24947 draw_row_with_mouse_face (w, start_x, row,
24948 start_hpos, end_hpos, draw);
24950 row->mouse_face_p
24951 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
24955 #ifdef HAVE_WINDOW_SYSTEM
24956 /* When we've written over the cursor, arrange for it to
24957 be displayed again. */
24958 if (FRAME_WINDOW_P (f)
24959 && phys_cursor_on_p && !w->phys_cursor_on_p)
24961 BLOCK_INPUT;
24962 display_and_set_cursor (w, 1,
24963 w->phys_cursor.hpos, w->phys_cursor.vpos,
24964 w->phys_cursor.x, w->phys_cursor.y);
24965 UNBLOCK_INPUT;
24967 #endif /* HAVE_WINDOW_SYSTEM */
24970 #ifdef HAVE_WINDOW_SYSTEM
24971 /* Change the mouse cursor. */
24972 if (FRAME_WINDOW_P (f))
24974 if (draw == DRAW_NORMAL_TEXT
24975 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
24976 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
24977 else if (draw == DRAW_MOUSE_FACE)
24978 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
24979 else
24980 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
24982 #endif /* HAVE_WINDOW_SYSTEM */
24985 /* EXPORT:
24986 Clear out the mouse-highlighted active region.
24987 Redraw it un-highlighted first. Value is non-zero if mouse
24988 face was actually drawn unhighlighted. */
24991 clear_mouse_face (Mouse_HLInfo *hlinfo)
24993 int cleared = 0;
24995 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
24997 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
24998 cleared = 1;
25001 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25002 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25003 hlinfo->mouse_face_window = Qnil;
25004 hlinfo->mouse_face_overlay = Qnil;
25005 return cleared;
25008 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
25009 within the mouse face on that window. */
25010 static int
25011 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
25013 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25015 /* Quickly resolve the easy cases. */
25016 if (!(WINDOWP (hlinfo->mouse_face_window)
25017 && XWINDOW (hlinfo->mouse_face_window) == w))
25018 return 0;
25019 if (vpos < hlinfo->mouse_face_beg_row
25020 || vpos > hlinfo->mouse_face_end_row)
25021 return 0;
25022 if (vpos > hlinfo->mouse_face_beg_row
25023 && vpos < hlinfo->mouse_face_end_row)
25024 return 1;
25026 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
25028 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25030 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
25031 return 1;
25033 else if ((vpos == hlinfo->mouse_face_beg_row
25034 && hpos >= hlinfo->mouse_face_beg_col)
25035 || (vpos == hlinfo->mouse_face_end_row
25036 && hpos < hlinfo->mouse_face_end_col))
25037 return 1;
25039 else
25041 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25043 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
25044 return 1;
25046 else if ((vpos == hlinfo->mouse_face_beg_row
25047 && hpos <= hlinfo->mouse_face_beg_col)
25048 || (vpos == hlinfo->mouse_face_end_row
25049 && hpos > hlinfo->mouse_face_end_col))
25050 return 1;
25052 return 0;
25056 /* EXPORT:
25057 Non-zero if physical cursor of window W is within mouse face. */
25060 cursor_in_mouse_face_p (struct window *w)
25062 return coords_in_mouse_face_p (w, w->phys_cursor.hpos, w->phys_cursor.vpos);
25067 /* Find the glyph rows START_ROW and END_ROW of window W that display
25068 characters between buffer positions START_CHARPOS and END_CHARPOS
25069 (excluding END_CHARPOS). This is similar to row_containing_pos,
25070 but is more accurate when bidi reordering makes buffer positions
25071 change non-linearly with glyph rows. */
25072 static void
25073 rows_from_pos_range (struct window *w,
25074 EMACS_INT start_charpos, EMACS_INT end_charpos,
25075 struct glyph_row **start, struct glyph_row **end)
25077 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25078 int last_y = window_text_bottom_y (w);
25079 struct glyph_row *row;
25081 *start = NULL;
25082 *end = NULL;
25084 while (!first->enabled_p
25085 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
25086 first++;
25088 /* Find the START row. */
25089 for (row = first;
25090 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
25091 row++)
25093 /* A row can potentially be the START row if the range of the
25094 characters it displays intersects the range
25095 [START_CHARPOS..END_CHARPOS). */
25096 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
25097 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
25098 /* See the commentary in row_containing_pos, for the
25099 explanation of the complicated way to check whether
25100 some position is beyond the end of the characters
25101 displayed by a row. */
25102 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
25103 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
25104 && !row->ends_at_zv_p
25105 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
25106 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
25107 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
25108 && !row->ends_at_zv_p
25109 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
25111 /* Found a candidate row. Now make sure at least one of the
25112 glyphs it displays has a charpos from the range
25113 [START_CHARPOS..END_CHARPOS).
25115 This is not obvious because bidi reordering could make
25116 buffer positions of a row be 1,2,3,102,101,100, and if we
25117 want to highlight characters in [50..60), we don't want
25118 this row, even though [50..60) does intersect [1..103),
25119 the range of character positions given by the row's start
25120 and end positions. */
25121 struct glyph *g = row->glyphs[TEXT_AREA];
25122 struct glyph *e = g + row->used[TEXT_AREA];
25124 while (g < e)
25126 if ((BUFFERP (g->object) || INTEGERP (g->object))
25127 && start_charpos <= g->charpos && g->charpos < end_charpos)
25128 *start = row;
25129 g++;
25131 if (*start)
25132 break;
25136 /* Find the END row. */
25137 if (!*start
25138 /* If the last row is partially visible, start looking for END
25139 from that row, instead of starting from FIRST. */
25140 && !(row->enabled_p
25141 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
25142 row = first;
25143 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
25145 struct glyph_row *next = row + 1;
25147 if (!next->enabled_p
25148 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
25149 /* The first row >= START whose range of displayed characters
25150 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
25151 is the row END + 1. */
25152 || (start_charpos < MATRIX_ROW_START_CHARPOS (next)
25153 && end_charpos < MATRIX_ROW_START_CHARPOS (next))
25154 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
25155 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
25156 && !next->ends_at_zv_p
25157 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
25158 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
25159 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
25160 && !next->ends_at_zv_p
25161 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
25163 *end = row;
25164 break;
25166 else
25168 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
25169 but none of the characters it displays are in the range, it is
25170 also END + 1. */
25171 struct glyph *g = next->glyphs[TEXT_AREA];
25172 struct glyph *e = g + next->used[TEXT_AREA];
25174 while (g < e)
25176 if ((BUFFERP (g->object) || INTEGERP (g->object))
25177 && start_charpos <= g->charpos && g->charpos < end_charpos)
25178 break;
25179 g++;
25181 if (g == e)
25183 *end = row;
25184 break;
25190 /* This function sets the mouse_face_* elements of HLINFO, assuming
25191 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
25192 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
25193 for the overlay or run of text properties specifying the mouse
25194 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
25195 before-string and after-string that must also be highlighted.
25196 COVER_STRING, if non-nil, is a display string that may cover some
25197 or all of the highlighted text. */
25199 static void
25200 mouse_face_from_buffer_pos (Lisp_Object window,
25201 Mouse_HLInfo *hlinfo,
25202 EMACS_INT mouse_charpos,
25203 EMACS_INT start_charpos,
25204 EMACS_INT end_charpos,
25205 Lisp_Object before_string,
25206 Lisp_Object after_string,
25207 Lisp_Object cover_string)
25209 struct window *w = XWINDOW (window);
25210 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25211 struct glyph_row *r1, *r2;
25212 struct glyph *glyph, *end;
25213 EMACS_INT ignore, pos;
25214 int x;
25216 xassert (NILP (cover_string) || STRINGP (cover_string));
25217 xassert (NILP (before_string) || STRINGP (before_string));
25218 xassert (NILP (after_string) || STRINGP (after_string));
25220 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
25221 rows_from_pos_range (w, start_charpos, end_charpos, &r1, &r2);
25222 if (r1 == NULL)
25223 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25224 /* If the before-string or display-string contains newlines,
25225 rows_from_pos_range skips to its last row. Move back. */
25226 if (!NILP (before_string) || !NILP (cover_string))
25228 struct glyph_row *prev;
25229 while ((prev = r1 - 1, prev >= first)
25230 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
25231 && prev->used[TEXT_AREA] > 0)
25233 struct glyph *beg = prev->glyphs[TEXT_AREA];
25234 glyph = beg + prev->used[TEXT_AREA];
25235 while (--glyph >= beg && INTEGERP (glyph->object));
25236 if (glyph < beg
25237 || !(EQ (glyph->object, before_string)
25238 || EQ (glyph->object, cover_string)))
25239 break;
25240 r1 = prev;
25243 if (r2 == NULL)
25245 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25246 hlinfo->mouse_face_past_end = 1;
25248 else if (!NILP (after_string))
25250 /* If the after-string has newlines, advance to its last row. */
25251 struct glyph_row *next;
25252 struct glyph_row *last
25253 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25255 for (next = r2 + 1;
25256 next <= last
25257 && next->used[TEXT_AREA] > 0
25258 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
25259 ++next)
25260 r2 = next;
25262 /* The rest of the display engine assumes that mouse_face_beg_row is
25263 either above below mouse_face_end_row or identical to it. But
25264 with bidi-reordered continued lines, the row for START_CHARPOS
25265 could be below the row for END_CHARPOS. If so, swap the rows and
25266 store them in correct order. */
25267 if (r1->y > r2->y)
25269 struct glyph_row *tem = r2;
25271 r2 = r1;
25272 r1 = tem;
25275 hlinfo->mouse_face_beg_y = r1->y;
25276 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
25277 hlinfo->mouse_face_end_y = r2->y;
25278 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
25280 /* For a bidi-reordered row, the positions of BEFORE_STRING,
25281 AFTER_STRING, COVER_STRING, START_CHARPOS, and END_CHARPOS
25282 could be anywhere in the row and in any order. The strategy
25283 below is to find the leftmost and the rightmost glyph that
25284 belongs to either of these 3 strings, or whose position is
25285 between START_CHARPOS and END_CHARPOS, and highlight all the
25286 glyphs between those two. This may cover more than just the text
25287 between START_CHARPOS and END_CHARPOS if the range of characters
25288 strides the bidi level boundary, e.g. if the beginning is in R2L
25289 text while the end is in L2R text or vice versa. */
25290 if (!r1->reversed_p)
25292 /* This row is in a left to right paragraph. Scan it left to
25293 right. */
25294 glyph = r1->glyphs[TEXT_AREA];
25295 end = glyph + r1->used[TEXT_AREA];
25296 x = r1->x;
25298 /* Skip truncation glyphs at the start of the glyph row. */
25299 if (r1->displays_text_p)
25300 for (; glyph < end
25301 && INTEGERP (glyph->object)
25302 && glyph->charpos < 0;
25303 ++glyph)
25304 x += glyph->pixel_width;
25306 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
25307 or COVER_STRING, and the first glyph from buffer whose
25308 position is between START_CHARPOS and END_CHARPOS. */
25309 for (; glyph < end
25310 && !INTEGERP (glyph->object)
25311 && !EQ (glyph->object, cover_string)
25312 && !(BUFFERP (glyph->object)
25313 && (glyph->charpos >= start_charpos
25314 && glyph->charpos < end_charpos));
25315 ++glyph)
25317 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25318 are present at buffer positions between START_CHARPOS and
25319 END_CHARPOS, or if they come from an overlay. */
25320 if (EQ (glyph->object, before_string))
25322 pos = string_buffer_position (before_string,
25323 start_charpos);
25324 /* If pos == 0, it means before_string came from an
25325 overlay, not from a buffer position. */
25326 if (!pos || (pos >= start_charpos && pos < end_charpos))
25327 break;
25329 else if (EQ (glyph->object, after_string))
25331 pos = string_buffer_position (after_string, end_charpos);
25332 if (!pos || (pos >= start_charpos && pos < end_charpos))
25333 break;
25335 x += glyph->pixel_width;
25337 hlinfo->mouse_face_beg_x = x;
25338 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
25340 else
25342 /* This row is in a right to left paragraph. Scan it right to
25343 left. */
25344 struct glyph *g;
25346 end = r1->glyphs[TEXT_AREA] - 1;
25347 glyph = end + r1->used[TEXT_AREA];
25349 /* Skip truncation glyphs at the start of the glyph row. */
25350 if (r1->displays_text_p)
25351 for (; glyph > end
25352 && INTEGERP (glyph->object)
25353 && glyph->charpos < 0;
25354 --glyph)
25357 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
25358 or COVER_STRING, and the first glyph from buffer whose
25359 position is between START_CHARPOS and END_CHARPOS. */
25360 for (; glyph > end
25361 && !INTEGERP (glyph->object)
25362 && !EQ (glyph->object, cover_string)
25363 && !(BUFFERP (glyph->object)
25364 && (glyph->charpos >= start_charpos
25365 && glyph->charpos < end_charpos));
25366 --glyph)
25368 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25369 are present at buffer positions between START_CHARPOS and
25370 END_CHARPOS, or if they come from an overlay. */
25371 if (EQ (glyph->object, before_string))
25373 pos = string_buffer_position (before_string, start_charpos);
25374 /* If pos == 0, it means before_string came from an
25375 overlay, not from a buffer position. */
25376 if (!pos || (pos >= start_charpos && pos < end_charpos))
25377 break;
25379 else if (EQ (glyph->object, after_string))
25381 pos = string_buffer_position (after_string, end_charpos);
25382 if (!pos || (pos >= start_charpos && pos < end_charpos))
25383 break;
25387 glyph++; /* first glyph to the right of the highlighted area */
25388 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
25389 x += g->pixel_width;
25390 hlinfo->mouse_face_beg_x = x;
25391 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
25394 /* If the highlight ends in a different row, compute GLYPH and END
25395 for the end row. Otherwise, reuse the values computed above for
25396 the row where the highlight begins. */
25397 if (r2 != r1)
25399 if (!r2->reversed_p)
25401 glyph = r2->glyphs[TEXT_AREA];
25402 end = glyph + r2->used[TEXT_AREA];
25403 x = r2->x;
25405 else
25407 end = r2->glyphs[TEXT_AREA] - 1;
25408 glyph = end + r2->used[TEXT_AREA];
25412 if (!r2->reversed_p)
25414 /* Skip truncation and continuation glyphs near the end of the
25415 row, and also blanks and stretch glyphs inserted by
25416 extend_face_to_end_of_line. */
25417 while (end > glyph
25418 && INTEGERP ((end - 1)->object)
25419 && (end - 1)->charpos <= 0)
25420 --end;
25421 /* Scan the rest of the glyph row from the end, looking for the
25422 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
25423 COVER_STRING, or whose position is between START_CHARPOS
25424 and END_CHARPOS */
25425 for (--end;
25426 end > glyph
25427 && !INTEGERP (end->object)
25428 && !EQ (end->object, cover_string)
25429 && !(BUFFERP (end->object)
25430 && (end->charpos >= start_charpos
25431 && end->charpos < end_charpos));
25432 --end)
25434 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25435 are present at buffer positions between START_CHARPOS and
25436 END_CHARPOS, or if they come from an overlay. */
25437 if (EQ (end->object, before_string))
25439 pos = string_buffer_position (before_string, start_charpos);
25440 if (!pos || (pos >= start_charpos && pos < end_charpos))
25441 break;
25443 else if (EQ (end->object, after_string))
25445 pos = string_buffer_position (after_string, end_charpos);
25446 if (!pos || (pos >= start_charpos && pos < end_charpos))
25447 break;
25450 /* Find the X coordinate of the last glyph to be highlighted. */
25451 for (; glyph <= end; ++glyph)
25452 x += glyph->pixel_width;
25454 hlinfo->mouse_face_end_x = x;
25455 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
25457 else
25459 /* Skip truncation and continuation glyphs near the end of the
25460 row, and also blanks and stretch glyphs inserted by
25461 extend_face_to_end_of_line. */
25462 x = r2->x;
25463 end++;
25464 while (end < glyph
25465 && INTEGERP (end->object)
25466 && end->charpos <= 0)
25468 x += end->pixel_width;
25469 ++end;
25471 /* Scan the rest of the glyph row from the end, looking for the
25472 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
25473 COVER_STRING, or whose position is between START_CHARPOS
25474 and END_CHARPOS */
25475 for ( ;
25476 end < glyph
25477 && !INTEGERP (end->object)
25478 && !EQ (end->object, cover_string)
25479 && !(BUFFERP (end->object)
25480 && (end->charpos >= start_charpos
25481 && end->charpos < end_charpos));
25482 ++end)
25484 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25485 are present at buffer positions between START_CHARPOS and
25486 END_CHARPOS, or if they come from an overlay. */
25487 if (EQ (end->object, before_string))
25489 pos = string_buffer_position (before_string, start_charpos);
25490 if (!pos || (pos >= start_charpos && pos < end_charpos))
25491 break;
25493 else if (EQ (end->object, after_string))
25495 pos = string_buffer_position (after_string, end_charpos);
25496 if (!pos || (pos >= start_charpos && pos < end_charpos))
25497 break;
25499 x += end->pixel_width;
25501 hlinfo->mouse_face_end_x = x;
25502 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
25505 hlinfo->mouse_face_window = window;
25506 hlinfo->mouse_face_face_id
25507 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
25508 mouse_charpos + 1,
25509 !hlinfo->mouse_face_hidden, -1);
25510 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25513 /* The following function is not used anymore (replaced with
25514 mouse_face_from_string_pos), but I leave it here for the time
25515 being, in case someone would. */
25517 #if 0 /* not used */
25519 /* Find the position of the glyph for position POS in OBJECT in
25520 window W's current matrix, and return in *X, *Y the pixel
25521 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
25523 RIGHT_P non-zero means return the position of the right edge of the
25524 glyph, RIGHT_P zero means return the left edge position.
25526 If no glyph for POS exists in the matrix, return the position of
25527 the glyph with the next smaller position that is in the matrix, if
25528 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
25529 exists in the matrix, return the position of the glyph with the
25530 next larger position in OBJECT.
25532 Value is non-zero if a glyph was found. */
25534 static int
25535 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
25536 int *hpos, int *vpos, int *x, int *y, int right_p)
25538 int yb = window_text_bottom_y (w);
25539 struct glyph_row *r;
25540 struct glyph *best_glyph = NULL;
25541 struct glyph_row *best_row = NULL;
25542 int best_x = 0;
25544 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25545 r->enabled_p && r->y < yb;
25546 ++r)
25548 struct glyph *g = r->glyphs[TEXT_AREA];
25549 struct glyph *e = g + r->used[TEXT_AREA];
25550 int gx;
25552 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
25553 if (EQ (g->object, object))
25555 if (g->charpos == pos)
25557 best_glyph = g;
25558 best_x = gx;
25559 best_row = r;
25560 goto found;
25562 else if (best_glyph == NULL
25563 || ((eabs (g->charpos - pos)
25564 < eabs (best_glyph->charpos - pos))
25565 && (right_p
25566 ? g->charpos < pos
25567 : g->charpos > pos)))
25569 best_glyph = g;
25570 best_x = gx;
25571 best_row = r;
25576 found:
25578 if (best_glyph)
25580 *x = best_x;
25581 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
25583 if (right_p)
25585 *x += best_glyph->pixel_width;
25586 ++*hpos;
25589 *y = best_row->y;
25590 *vpos = best_row - w->current_matrix->rows;
25593 return best_glyph != NULL;
25595 #endif /* not used */
25597 /* Find the positions of the first and the last glyphs in window W's
25598 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
25599 (assumed to be a string), and return in HLINFO's mouse_face_*
25600 members the pixel and column/row coordinates of those glyphs. */
25602 static void
25603 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
25604 Lisp_Object object,
25605 EMACS_INT startpos, EMACS_INT endpos)
25607 int yb = window_text_bottom_y (w);
25608 struct glyph_row *r;
25609 struct glyph *g, *e;
25610 int gx;
25611 int found = 0;
25613 /* Find the glyph row with at least one position in the range
25614 [STARTPOS..ENDPOS], and the first glyph in that row whose
25615 position belongs to that range. */
25616 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25617 r->enabled_p && r->y < yb;
25618 ++r)
25620 if (!r->reversed_p)
25622 g = r->glyphs[TEXT_AREA];
25623 e = g + r->used[TEXT_AREA];
25624 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
25625 if (EQ (g->object, object)
25626 && startpos <= g->charpos && g->charpos <= endpos)
25628 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
25629 hlinfo->mouse_face_beg_y = r->y;
25630 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
25631 hlinfo->mouse_face_beg_x = gx;
25632 found = 1;
25633 break;
25636 else
25638 struct glyph *g1;
25640 e = r->glyphs[TEXT_AREA];
25641 g = e + r->used[TEXT_AREA];
25642 for ( ; g > e; --g)
25643 if (EQ ((g-1)->object, object)
25644 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
25646 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
25647 hlinfo->mouse_face_beg_y = r->y;
25648 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
25649 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
25650 gx += g1->pixel_width;
25651 hlinfo->mouse_face_beg_x = gx;
25652 found = 1;
25653 break;
25656 if (found)
25657 break;
25660 if (!found)
25661 return;
25663 /* Starting with the next row, look for the first row which does NOT
25664 include any glyphs whose positions are in the range. */
25665 for (++r; r->enabled_p && r->y < yb; ++r)
25667 g = r->glyphs[TEXT_AREA];
25668 e = g + r->used[TEXT_AREA];
25669 found = 0;
25670 for ( ; g < e; ++g)
25671 if (EQ (g->object, object)
25672 && startpos <= g->charpos && g->charpos <= endpos)
25674 found = 1;
25675 break;
25677 if (!found)
25678 break;
25681 /* The highlighted region ends on the previous row. */
25682 r--;
25684 /* Set the end row and its vertical pixel coordinate. */
25685 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
25686 hlinfo->mouse_face_end_y = r->y;
25688 /* Compute and set the end column and the end column's horizontal
25689 pixel coordinate. */
25690 if (!r->reversed_p)
25692 g = r->glyphs[TEXT_AREA];
25693 e = g + r->used[TEXT_AREA];
25694 for ( ; e > g; --e)
25695 if (EQ ((e-1)->object, object)
25696 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
25697 break;
25698 hlinfo->mouse_face_end_col = e - g;
25700 for (gx = r->x; g < e; ++g)
25701 gx += g->pixel_width;
25702 hlinfo->mouse_face_end_x = gx;
25704 else
25706 e = r->glyphs[TEXT_AREA];
25707 g = e + r->used[TEXT_AREA];
25708 for (gx = r->x ; e < g; ++e)
25710 if (EQ (e->object, object)
25711 && startpos <= e->charpos && e->charpos <= endpos)
25712 break;
25713 gx += e->pixel_width;
25715 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
25716 hlinfo->mouse_face_end_x = gx;
25720 #ifdef HAVE_WINDOW_SYSTEM
25722 /* See if position X, Y is within a hot-spot of an image. */
25724 static int
25725 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
25727 if (!CONSP (hot_spot))
25728 return 0;
25730 if (EQ (XCAR (hot_spot), Qrect))
25732 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
25733 Lisp_Object rect = XCDR (hot_spot);
25734 Lisp_Object tem;
25735 if (!CONSP (rect))
25736 return 0;
25737 if (!CONSP (XCAR (rect)))
25738 return 0;
25739 if (!CONSP (XCDR (rect)))
25740 return 0;
25741 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
25742 return 0;
25743 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
25744 return 0;
25745 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
25746 return 0;
25747 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
25748 return 0;
25749 return 1;
25751 else if (EQ (XCAR (hot_spot), Qcircle))
25753 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
25754 Lisp_Object circ = XCDR (hot_spot);
25755 Lisp_Object lr, lx0, ly0;
25756 if (CONSP (circ)
25757 && CONSP (XCAR (circ))
25758 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
25759 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
25760 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
25762 double r = XFLOATINT (lr);
25763 double dx = XINT (lx0) - x;
25764 double dy = XINT (ly0) - y;
25765 return (dx * dx + dy * dy <= r * r);
25768 else if (EQ (XCAR (hot_spot), Qpoly))
25770 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
25771 if (VECTORP (XCDR (hot_spot)))
25773 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
25774 Lisp_Object *poly = v->contents;
25775 int n = v->header.size;
25776 int i;
25777 int inside = 0;
25778 Lisp_Object lx, ly;
25779 int x0, y0;
25781 /* Need an even number of coordinates, and at least 3 edges. */
25782 if (n < 6 || n & 1)
25783 return 0;
25785 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
25786 If count is odd, we are inside polygon. Pixels on edges
25787 may or may not be included depending on actual geometry of the
25788 polygon. */
25789 if ((lx = poly[n-2], !INTEGERP (lx))
25790 || (ly = poly[n-1], !INTEGERP (lx)))
25791 return 0;
25792 x0 = XINT (lx), y0 = XINT (ly);
25793 for (i = 0; i < n; i += 2)
25795 int x1 = x0, y1 = y0;
25796 if ((lx = poly[i], !INTEGERP (lx))
25797 || (ly = poly[i+1], !INTEGERP (ly)))
25798 return 0;
25799 x0 = XINT (lx), y0 = XINT (ly);
25801 /* Does this segment cross the X line? */
25802 if (x0 >= x)
25804 if (x1 >= x)
25805 continue;
25807 else if (x1 < x)
25808 continue;
25809 if (y > y0 && y > y1)
25810 continue;
25811 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
25812 inside = !inside;
25814 return inside;
25817 return 0;
25820 Lisp_Object
25821 find_hot_spot (Lisp_Object map, int x, int y)
25823 while (CONSP (map))
25825 if (CONSP (XCAR (map))
25826 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
25827 return XCAR (map);
25828 map = XCDR (map);
25831 return Qnil;
25834 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
25835 3, 3, 0,
25836 doc: /* Lookup in image map MAP coordinates X and Y.
25837 An image map is an alist where each element has the format (AREA ID PLIST).
25838 An AREA is specified as either a rectangle, a circle, or a polygon:
25839 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
25840 pixel coordinates of the upper left and bottom right corners.
25841 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
25842 and the radius of the circle; r may be a float or integer.
25843 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
25844 vector describes one corner in the polygon.
25845 Returns the alist element for the first matching AREA in MAP. */)
25846 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
25848 if (NILP (map))
25849 return Qnil;
25851 CHECK_NUMBER (x);
25852 CHECK_NUMBER (y);
25854 return find_hot_spot (map, XINT (x), XINT (y));
25858 /* Display frame CURSOR, optionally using shape defined by POINTER. */
25859 static void
25860 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
25862 /* Do not change cursor shape while dragging mouse. */
25863 if (!NILP (do_mouse_tracking))
25864 return;
25866 if (!NILP (pointer))
25868 if (EQ (pointer, Qarrow))
25869 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25870 else if (EQ (pointer, Qhand))
25871 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
25872 else if (EQ (pointer, Qtext))
25873 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25874 else if (EQ (pointer, intern ("hdrag")))
25875 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25876 #ifdef HAVE_X_WINDOWS
25877 else if (EQ (pointer, intern ("vdrag")))
25878 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
25879 #endif
25880 else if (EQ (pointer, intern ("hourglass")))
25881 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
25882 else if (EQ (pointer, Qmodeline))
25883 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
25884 else
25885 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25888 if (cursor != No_Cursor)
25889 FRAME_RIF (f)->define_frame_cursor (f, cursor);
25892 #endif /* HAVE_WINDOW_SYSTEM */
25894 /* Take proper action when mouse has moved to the mode or header line
25895 or marginal area AREA of window W, x-position X and y-position Y.
25896 X is relative to the start of the text display area of W, so the
25897 width of bitmap areas and scroll bars must be subtracted to get a
25898 position relative to the start of the mode line. */
25900 static void
25901 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
25902 enum window_part area)
25904 struct window *w = XWINDOW (window);
25905 struct frame *f = XFRAME (w->frame);
25906 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25907 #ifdef HAVE_WINDOW_SYSTEM
25908 Display_Info *dpyinfo;
25909 #endif
25910 Cursor cursor = No_Cursor;
25911 Lisp_Object pointer = Qnil;
25912 int dx, dy, width, height;
25913 EMACS_INT charpos;
25914 Lisp_Object string, object = Qnil;
25915 Lisp_Object pos, help;
25917 Lisp_Object mouse_face;
25918 int original_x_pixel = x;
25919 struct glyph * glyph = NULL, * row_start_glyph = NULL;
25920 struct glyph_row *row;
25922 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
25924 int x0;
25925 struct glyph *end;
25927 /* Kludge alert: mode_line_string takes X/Y in pixels, but
25928 returns them in row/column units! */
25929 string = mode_line_string (w, area, &x, &y, &charpos,
25930 &object, &dx, &dy, &width, &height);
25932 row = (area == ON_MODE_LINE
25933 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
25934 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
25936 /* Find the glyph under the mouse pointer. */
25937 if (row->mode_line_p && row->enabled_p)
25939 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
25940 end = glyph + row->used[TEXT_AREA];
25942 for (x0 = original_x_pixel;
25943 glyph < end && x0 >= glyph->pixel_width;
25944 ++glyph)
25945 x0 -= glyph->pixel_width;
25947 if (glyph >= end)
25948 glyph = NULL;
25951 else
25953 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
25954 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
25955 returns them in row/column units! */
25956 string = marginal_area_string (w, area, &x, &y, &charpos,
25957 &object, &dx, &dy, &width, &height);
25960 help = Qnil;
25962 #ifdef HAVE_WINDOW_SYSTEM
25963 if (IMAGEP (object))
25965 Lisp_Object image_map, hotspot;
25966 if ((image_map = Fplist_get (XCDR (object), QCmap),
25967 !NILP (image_map))
25968 && (hotspot = find_hot_spot (image_map, dx, dy),
25969 CONSP (hotspot))
25970 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25972 Lisp_Object plist;
25974 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
25975 If so, we could look for mouse-enter, mouse-leave
25976 properties in PLIST (and do something...). */
25977 hotspot = XCDR (hotspot);
25978 if (CONSP (hotspot)
25979 && (plist = XCAR (hotspot), CONSP (plist)))
25981 pointer = Fplist_get (plist, Qpointer);
25982 if (NILP (pointer))
25983 pointer = Qhand;
25984 help = Fplist_get (plist, Qhelp_echo);
25985 if (!NILP (help))
25987 help_echo_string = help;
25988 /* Is this correct? ++kfs */
25989 XSETWINDOW (help_echo_window, w);
25990 help_echo_object = w->buffer;
25991 help_echo_pos = charpos;
25995 if (NILP (pointer))
25996 pointer = Fplist_get (XCDR (object), QCpointer);
25998 #endif /* HAVE_WINDOW_SYSTEM */
26000 if (STRINGP (string))
26002 pos = make_number (charpos);
26003 /* If we're on a string with `help-echo' text property, arrange
26004 for the help to be displayed. This is done by setting the
26005 global variable help_echo_string to the help string. */
26006 if (NILP (help))
26008 help = Fget_text_property (pos, Qhelp_echo, string);
26009 if (!NILP (help))
26011 help_echo_string = help;
26012 XSETWINDOW (help_echo_window, w);
26013 help_echo_object = string;
26014 help_echo_pos = charpos;
26018 #ifdef HAVE_WINDOW_SYSTEM
26019 if (FRAME_WINDOW_P (f))
26021 dpyinfo = FRAME_X_DISPLAY_INFO (f);
26022 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26023 if (NILP (pointer))
26024 pointer = Fget_text_property (pos, Qpointer, string);
26026 /* Change the mouse pointer according to what is under X/Y. */
26027 if (NILP (pointer)
26028 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
26030 Lisp_Object map;
26031 map = Fget_text_property (pos, Qlocal_map, string);
26032 if (!KEYMAPP (map))
26033 map = Fget_text_property (pos, Qkeymap, string);
26034 if (!KEYMAPP (map))
26035 cursor = dpyinfo->vertical_scroll_bar_cursor;
26038 #endif
26040 /* Change the mouse face according to what is under X/Y. */
26041 mouse_face = Fget_text_property (pos, Qmouse_face, string);
26042 if (!NILP (mouse_face)
26043 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
26044 && glyph)
26046 Lisp_Object b, e;
26048 struct glyph * tmp_glyph;
26050 int gpos;
26051 int gseq_length;
26052 int total_pixel_width;
26053 EMACS_INT begpos, endpos, ignore;
26055 int vpos, hpos;
26057 b = Fprevious_single_property_change (make_number (charpos + 1),
26058 Qmouse_face, string, Qnil);
26059 if (NILP (b))
26060 begpos = 0;
26061 else
26062 begpos = XINT (b);
26064 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
26065 if (NILP (e))
26066 endpos = SCHARS (string);
26067 else
26068 endpos = XINT (e);
26070 /* Calculate the glyph position GPOS of GLYPH in the
26071 displayed string, relative to the beginning of the
26072 highlighted part of the string.
26074 Note: GPOS is different from CHARPOS. CHARPOS is the
26075 position of GLYPH in the internal string object. A mode
26076 line string format has structures which are converted to
26077 a flattened string by the Emacs Lisp interpreter. The
26078 internal string is an element of those structures. The
26079 displayed string is the flattened string. */
26080 tmp_glyph = row_start_glyph;
26081 while (tmp_glyph < glyph
26082 && (!(EQ (tmp_glyph->object, glyph->object)
26083 && begpos <= tmp_glyph->charpos
26084 && tmp_glyph->charpos < endpos)))
26085 tmp_glyph++;
26086 gpos = glyph - tmp_glyph;
26088 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
26089 the highlighted part of the displayed string to which
26090 GLYPH belongs. Note: GSEQ_LENGTH is different from
26091 SCHARS (STRING), because the latter returns the length of
26092 the internal string. */
26093 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
26094 tmp_glyph > glyph
26095 && (!(EQ (tmp_glyph->object, glyph->object)
26096 && begpos <= tmp_glyph->charpos
26097 && tmp_glyph->charpos < endpos));
26098 tmp_glyph--)
26100 gseq_length = gpos + (tmp_glyph - glyph) + 1;
26102 /* Calculate the total pixel width of all the glyphs between
26103 the beginning of the highlighted area and GLYPH. */
26104 total_pixel_width = 0;
26105 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
26106 total_pixel_width += tmp_glyph->pixel_width;
26108 /* Pre calculation of re-rendering position. Note: X is in
26109 column units here, after the call to mode_line_string or
26110 marginal_area_string. */
26111 hpos = x - gpos;
26112 vpos = (area == ON_MODE_LINE
26113 ? (w->current_matrix)->nrows - 1
26114 : 0);
26116 /* If GLYPH's position is included in the region that is
26117 already drawn in mouse face, we have nothing to do. */
26118 if ( EQ (window, hlinfo->mouse_face_window)
26119 && (!row->reversed_p
26120 ? (hlinfo->mouse_face_beg_col <= hpos
26121 && hpos < hlinfo->mouse_face_end_col)
26122 /* In R2L rows we swap BEG and END, see below. */
26123 : (hlinfo->mouse_face_end_col <= hpos
26124 && hpos < hlinfo->mouse_face_beg_col))
26125 && hlinfo->mouse_face_beg_row == vpos )
26126 return;
26128 if (clear_mouse_face (hlinfo))
26129 cursor = No_Cursor;
26131 if (!row->reversed_p)
26133 hlinfo->mouse_face_beg_col = hpos;
26134 hlinfo->mouse_face_beg_x = original_x_pixel
26135 - (total_pixel_width + dx);
26136 hlinfo->mouse_face_end_col = hpos + gseq_length;
26137 hlinfo->mouse_face_end_x = 0;
26139 else
26141 /* In R2L rows, show_mouse_face expects BEG and END
26142 coordinates to be swapped. */
26143 hlinfo->mouse_face_end_col = hpos;
26144 hlinfo->mouse_face_end_x = original_x_pixel
26145 - (total_pixel_width + dx);
26146 hlinfo->mouse_face_beg_col = hpos + gseq_length;
26147 hlinfo->mouse_face_beg_x = 0;
26150 hlinfo->mouse_face_beg_row = vpos;
26151 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
26152 hlinfo->mouse_face_beg_y = 0;
26153 hlinfo->mouse_face_end_y = 0;
26154 hlinfo->mouse_face_past_end = 0;
26155 hlinfo->mouse_face_window = window;
26157 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
26158 charpos,
26159 0, 0, 0,
26160 &ignore,
26161 glyph->face_id,
26163 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26165 if (NILP (pointer))
26166 pointer = Qhand;
26168 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
26169 clear_mouse_face (hlinfo);
26171 #ifdef HAVE_WINDOW_SYSTEM
26172 if (FRAME_WINDOW_P (f))
26173 define_frame_cursor1 (f, cursor, pointer);
26174 #endif
26178 /* EXPORT:
26179 Take proper action when the mouse has moved to position X, Y on
26180 frame F as regards highlighting characters that have mouse-face
26181 properties. Also de-highlighting chars where the mouse was before.
26182 X and Y can be negative or out of range. */
26184 void
26185 note_mouse_highlight (struct frame *f, int x, int y)
26187 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26188 enum window_part part;
26189 Lisp_Object window;
26190 struct window *w;
26191 Cursor cursor = No_Cursor;
26192 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
26193 struct buffer *b;
26195 /* When a menu is active, don't highlight because this looks odd. */
26196 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
26197 if (popup_activated ())
26198 return;
26199 #endif
26201 if (NILP (Vmouse_highlight)
26202 || !f->glyphs_initialized_p
26203 || f->pointer_invisible)
26204 return;
26206 hlinfo->mouse_face_mouse_x = x;
26207 hlinfo->mouse_face_mouse_y = y;
26208 hlinfo->mouse_face_mouse_frame = f;
26210 if (hlinfo->mouse_face_defer)
26211 return;
26213 if (gc_in_progress)
26215 hlinfo->mouse_face_deferred_gc = 1;
26216 return;
26219 /* Which window is that in? */
26220 window = window_from_coordinates (f, x, y, &part, 1);
26222 /* If we were displaying active text in another window, clear that.
26223 Also clear if we move out of text area in same window. */
26224 if (! EQ (window, hlinfo->mouse_face_window)
26225 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
26226 && !NILP (hlinfo->mouse_face_window)))
26227 clear_mouse_face (hlinfo);
26229 /* Not on a window -> return. */
26230 if (!WINDOWP (window))
26231 return;
26233 /* Reset help_echo_string. It will get recomputed below. */
26234 help_echo_string = Qnil;
26236 /* Convert to window-relative pixel coordinates. */
26237 w = XWINDOW (window);
26238 frame_to_window_pixel_xy (w, &x, &y);
26240 #ifdef HAVE_WINDOW_SYSTEM
26241 /* Handle tool-bar window differently since it doesn't display a
26242 buffer. */
26243 if (EQ (window, f->tool_bar_window))
26245 note_tool_bar_highlight (f, x, y);
26246 return;
26248 #endif
26250 /* Mouse is on the mode, header line or margin? */
26251 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
26252 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
26254 note_mode_line_or_margin_highlight (window, x, y, part);
26255 return;
26258 #ifdef HAVE_WINDOW_SYSTEM
26259 if (part == ON_VERTICAL_BORDER)
26261 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26262 help_echo_string = build_string ("drag-mouse-1: resize");
26264 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
26265 || part == ON_SCROLL_BAR)
26266 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26267 else
26268 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26269 #endif
26271 /* Are we in a window whose display is up to date?
26272 And verify the buffer's text has not changed. */
26273 b = XBUFFER (w->buffer);
26274 if (part == ON_TEXT
26275 && EQ (w->window_end_valid, w->buffer)
26276 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
26277 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
26279 int hpos, vpos, dx, dy, area;
26280 EMACS_INT pos;
26281 struct glyph *glyph;
26282 Lisp_Object object;
26283 Lisp_Object mouse_face = Qnil, position;
26284 Lisp_Object *overlay_vec = NULL;
26285 ptrdiff_t i, noverlays;
26286 struct buffer *obuf;
26287 EMACS_INT obegv, ozv;
26288 int same_region;
26290 /* Find the glyph under X/Y. */
26291 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
26293 #ifdef HAVE_WINDOW_SYSTEM
26294 /* Look for :pointer property on image. */
26295 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26297 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26298 if (img != NULL && IMAGEP (img->spec))
26300 Lisp_Object image_map, hotspot;
26301 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
26302 !NILP (image_map))
26303 && (hotspot = find_hot_spot (image_map,
26304 glyph->slice.img.x + dx,
26305 glyph->slice.img.y + dy),
26306 CONSP (hotspot))
26307 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
26309 Lisp_Object plist;
26311 /* Could check XCAR (hotspot) to see if we enter/leave
26312 this hot-spot.
26313 If so, we could look for mouse-enter, mouse-leave
26314 properties in PLIST (and do something...). */
26315 hotspot = XCDR (hotspot);
26316 if (CONSP (hotspot)
26317 && (plist = XCAR (hotspot), CONSP (plist)))
26319 pointer = Fplist_get (plist, Qpointer);
26320 if (NILP (pointer))
26321 pointer = Qhand;
26322 help_echo_string = Fplist_get (plist, Qhelp_echo);
26323 if (!NILP (help_echo_string))
26325 help_echo_window = window;
26326 help_echo_object = glyph->object;
26327 help_echo_pos = glyph->charpos;
26331 if (NILP (pointer))
26332 pointer = Fplist_get (XCDR (img->spec), QCpointer);
26335 #endif /* HAVE_WINDOW_SYSTEM */
26337 /* Clear mouse face if X/Y not over text. */
26338 if (glyph == NULL
26339 || area != TEXT_AREA
26340 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
26341 /* Glyph's OBJECT is an integer for glyphs inserted by the
26342 display engine for its internal purposes, like truncation
26343 and continuation glyphs and blanks beyond the end of
26344 line's text on text terminals. If we are over such a
26345 glyph, we are not over any text. */
26346 || INTEGERP (glyph->object)
26347 /* R2L rows have a stretch glyph at their front, which
26348 stands for no text, whereas L2R rows have no glyphs at
26349 all beyond the end of text. Treat such stretch glyphs
26350 like we do with NULL glyphs in L2R rows. */
26351 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
26352 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
26353 && glyph->type == STRETCH_GLYPH
26354 && glyph->avoid_cursor_p))
26356 if (clear_mouse_face (hlinfo))
26357 cursor = No_Cursor;
26358 #ifdef HAVE_WINDOW_SYSTEM
26359 if (FRAME_WINDOW_P (f) && NILP (pointer))
26361 if (area != TEXT_AREA)
26362 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26363 else
26364 pointer = Vvoid_text_area_pointer;
26366 #endif
26367 goto set_cursor;
26370 pos = glyph->charpos;
26371 object = glyph->object;
26372 if (!STRINGP (object) && !BUFFERP (object))
26373 goto set_cursor;
26375 /* If we get an out-of-range value, return now; avoid an error. */
26376 if (BUFFERP (object) && pos > BUF_Z (b))
26377 goto set_cursor;
26379 /* Make the window's buffer temporarily current for
26380 overlays_at and compute_char_face. */
26381 obuf = current_buffer;
26382 current_buffer = b;
26383 obegv = BEGV;
26384 ozv = ZV;
26385 BEGV = BEG;
26386 ZV = Z;
26388 /* Is this char mouse-active or does it have help-echo? */
26389 position = make_number (pos);
26391 if (BUFFERP (object))
26393 /* Put all the overlays we want in a vector in overlay_vec. */
26394 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
26395 /* Sort overlays into increasing priority order. */
26396 noverlays = sort_overlays (overlay_vec, noverlays, w);
26398 else
26399 noverlays = 0;
26401 same_region = coords_in_mouse_face_p (w, hpos, vpos);
26403 if (same_region)
26404 cursor = No_Cursor;
26406 /* Check mouse-face highlighting. */
26407 if (! same_region
26408 /* If there exists an overlay with mouse-face overlapping
26409 the one we are currently highlighting, we have to
26410 check if we enter the overlapping overlay, and then
26411 highlight only that. */
26412 || (OVERLAYP (hlinfo->mouse_face_overlay)
26413 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
26415 /* Find the highest priority overlay with a mouse-face. */
26416 Lisp_Object overlay = Qnil;
26417 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
26419 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
26420 if (!NILP (mouse_face))
26421 overlay = overlay_vec[i];
26424 /* If we're highlighting the same overlay as before, there's
26425 no need to do that again. */
26426 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
26427 goto check_help_echo;
26428 hlinfo->mouse_face_overlay = overlay;
26430 /* Clear the display of the old active region, if any. */
26431 if (clear_mouse_face (hlinfo))
26432 cursor = No_Cursor;
26434 /* If no overlay applies, get a text property. */
26435 if (NILP (overlay))
26436 mouse_face = Fget_text_property (position, Qmouse_face, object);
26438 /* Next, compute the bounds of the mouse highlighting and
26439 display it. */
26440 if (!NILP (mouse_face) && STRINGP (object))
26442 /* The mouse-highlighting comes from a display string
26443 with a mouse-face. */
26444 Lisp_Object s, e;
26445 EMACS_INT ignore;
26447 s = Fprevious_single_property_change
26448 (make_number (pos + 1), Qmouse_face, object, Qnil);
26449 e = Fnext_single_property_change
26450 (position, Qmouse_face, object, Qnil);
26451 if (NILP (s))
26452 s = make_number (0);
26453 if (NILP (e))
26454 e = make_number (SCHARS (object) - 1);
26455 mouse_face_from_string_pos (w, hlinfo, object,
26456 XINT (s), XINT (e));
26457 hlinfo->mouse_face_past_end = 0;
26458 hlinfo->mouse_face_window = window;
26459 hlinfo->mouse_face_face_id
26460 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
26461 glyph->face_id, 1);
26462 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26463 cursor = No_Cursor;
26465 else
26467 /* The mouse-highlighting, if any, comes from an overlay
26468 or text property in the buffer. */
26469 Lisp_Object buffer IF_LINT (= Qnil);
26470 Lisp_Object cover_string IF_LINT (= Qnil);
26472 if (STRINGP (object))
26474 /* If we are on a display string with no mouse-face,
26475 check if the text under it has one. */
26476 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
26477 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26478 pos = string_buffer_position (object, start);
26479 if (pos > 0)
26481 mouse_face = get_char_property_and_overlay
26482 (make_number (pos), Qmouse_face, w->buffer, &overlay);
26483 buffer = w->buffer;
26484 cover_string = object;
26487 else
26489 buffer = object;
26490 cover_string = Qnil;
26493 if (!NILP (mouse_face))
26495 Lisp_Object before, after;
26496 Lisp_Object before_string, after_string;
26497 /* To correctly find the limits of mouse highlight
26498 in a bidi-reordered buffer, we must not use the
26499 optimization of limiting the search in
26500 previous-single-property-change and
26501 next-single-property-change, because
26502 rows_from_pos_range needs the real start and end
26503 positions to DTRT in this case. That's because
26504 the first row visible in a window does not
26505 necessarily display the character whose position
26506 is the smallest. */
26507 Lisp_Object lim1 =
26508 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
26509 ? Fmarker_position (w->start)
26510 : Qnil;
26511 Lisp_Object lim2 =
26512 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
26513 ? make_number (BUF_Z (XBUFFER (buffer))
26514 - XFASTINT (w->window_end_pos))
26515 : Qnil;
26517 if (NILP (overlay))
26519 /* Handle the text property case. */
26520 before = Fprevious_single_property_change
26521 (make_number (pos + 1), Qmouse_face, buffer, lim1);
26522 after = Fnext_single_property_change
26523 (make_number (pos), Qmouse_face, buffer, lim2);
26524 before_string = after_string = Qnil;
26526 else
26528 /* Handle the overlay case. */
26529 before = Foverlay_start (overlay);
26530 after = Foverlay_end (overlay);
26531 before_string = Foverlay_get (overlay, Qbefore_string);
26532 after_string = Foverlay_get (overlay, Qafter_string);
26534 if (!STRINGP (before_string)) before_string = Qnil;
26535 if (!STRINGP (after_string)) after_string = Qnil;
26538 mouse_face_from_buffer_pos (window, hlinfo, pos,
26539 XFASTINT (before),
26540 XFASTINT (after),
26541 before_string, after_string,
26542 cover_string);
26543 cursor = No_Cursor;
26548 check_help_echo:
26550 /* Look for a `help-echo' property. */
26551 if (NILP (help_echo_string)) {
26552 Lisp_Object help, overlay;
26554 /* Check overlays first. */
26555 help = overlay = Qnil;
26556 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
26558 overlay = overlay_vec[i];
26559 help = Foverlay_get (overlay, Qhelp_echo);
26562 if (!NILP (help))
26564 help_echo_string = help;
26565 help_echo_window = window;
26566 help_echo_object = overlay;
26567 help_echo_pos = pos;
26569 else
26571 Lisp_Object obj = glyph->object;
26572 EMACS_INT charpos = glyph->charpos;
26574 /* Try text properties. */
26575 if (STRINGP (obj)
26576 && charpos >= 0
26577 && charpos < SCHARS (obj))
26579 help = Fget_text_property (make_number (charpos),
26580 Qhelp_echo, obj);
26581 if (NILP (help))
26583 /* If the string itself doesn't specify a help-echo,
26584 see if the buffer text ``under'' it does. */
26585 struct glyph_row *r
26586 = MATRIX_ROW (w->current_matrix, vpos);
26587 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26588 EMACS_INT p = string_buffer_position (obj, start);
26589 if (p > 0)
26591 help = Fget_char_property (make_number (p),
26592 Qhelp_echo, w->buffer);
26593 if (!NILP (help))
26595 charpos = p;
26596 obj = w->buffer;
26601 else if (BUFFERP (obj)
26602 && charpos >= BEGV
26603 && charpos < ZV)
26604 help = Fget_text_property (make_number (charpos), Qhelp_echo,
26605 obj);
26607 if (!NILP (help))
26609 help_echo_string = help;
26610 help_echo_window = window;
26611 help_echo_object = obj;
26612 help_echo_pos = charpos;
26617 #ifdef HAVE_WINDOW_SYSTEM
26618 /* Look for a `pointer' property. */
26619 if (FRAME_WINDOW_P (f) && NILP (pointer))
26621 /* Check overlays first. */
26622 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
26623 pointer = Foverlay_get (overlay_vec[i], Qpointer);
26625 if (NILP (pointer))
26627 Lisp_Object obj = glyph->object;
26628 EMACS_INT charpos = glyph->charpos;
26630 /* Try text properties. */
26631 if (STRINGP (obj)
26632 && charpos >= 0
26633 && charpos < SCHARS (obj))
26635 pointer = Fget_text_property (make_number (charpos),
26636 Qpointer, obj);
26637 if (NILP (pointer))
26639 /* If the string itself doesn't specify a pointer,
26640 see if the buffer text ``under'' it does. */
26641 struct glyph_row *r
26642 = MATRIX_ROW (w->current_matrix, vpos);
26643 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26644 EMACS_INT p = string_buffer_position (obj, start);
26645 if (p > 0)
26646 pointer = Fget_char_property (make_number (p),
26647 Qpointer, w->buffer);
26650 else if (BUFFERP (obj)
26651 && charpos >= BEGV
26652 && charpos < ZV)
26653 pointer = Fget_text_property (make_number (charpos),
26654 Qpointer, obj);
26657 #endif /* HAVE_WINDOW_SYSTEM */
26659 BEGV = obegv;
26660 ZV = ozv;
26661 current_buffer = obuf;
26664 set_cursor:
26666 #ifdef HAVE_WINDOW_SYSTEM
26667 if (FRAME_WINDOW_P (f))
26668 define_frame_cursor1 (f, cursor, pointer);
26669 #else
26670 /* This is here to prevent a compiler error, about "label at end of
26671 compound statement". */
26672 return;
26673 #endif
26677 /* EXPORT for RIF:
26678 Clear any mouse-face on window W. This function is part of the
26679 redisplay interface, and is called from try_window_id and similar
26680 functions to ensure the mouse-highlight is off. */
26682 void
26683 x_clear_window_mouse_face (struct window *w)
26685 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26686 Lisp_Object window;
26688 BLOCK_INPUT;
26689 XSETWINDOW (window, w);
26690 if (EQ (window, hlinfo->mouse_face_window))
26691 clear_mouse_face (hlinfo);
26692 UNBLOCK_INPUT;
26696 /* EXPORT:
26697 Just discard the mouse face information for frame F, if any.
26698 This is used when the size of F is changed. */
26700 void
26701 cancel_mouse_face (struct frame *f)
26703 Lisp_Object window;
26704 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26706 window = hlinfo->mouse_face_window;
26707 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
26709 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26710 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26711 hlinfo->mouse_face_window = Qnil;
26717 /***********************************************************************
26718 Exposure Events
26719 ***********************************************************************/
26721 #ifdef HAVE_WINDOW_SYSTEM
26723 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
26724 which intersects rectangle R. R is in window-relative coordinates. */
26726 static void
26727 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
26728 enum glyph_row_area area)
26730 struct glyph *first = row->glyphs[area];
26731 struct glyph *end = row->glyphs[area] + row->used[area];
26732 struct glyph *last;
26733 int first_x, start_x, x;
26735 if (area == TEXT_AREA && row->fill_line_p)
26736 /* If row extends face to end of line write the whole line. */
26737 draw_glyphs (w, 0, row, area,
26738 0, row->used[area],
26739 DRAW_NORMAL_TEXT, 0);
26740 else
26742 /* Set START_X to the window-relative start position for drawing glyphs of
26743 AREA. The first glyph of the text area can be partially visible.
26744 The first glyphs of other areas cannot. */
26745 start_x = window_box_left_offset (w, area);
26746 x = start_x;
26747 if (area == TEXT_AREA)
26748 x += row->x;
26750 /* Find the first glyph that must be redrawn. */
26751 while (first < end
26752 && x + first->pixel_width < r->x)
26754 x += first->pixel_width;
26755 ++first;
26758 /* Find the last one. */
26759 last = first;
26760 first_x = x;
26761 while (last < end
26762 && x < r->x + r->width)
26764 x += last->pixel_width;
26765 ++last;
26768 /* Repaint. */
26769 if (last > first)
26770 draw_glyphs (w, first_x - start_x, row, area,
26771 first - row->glyphs[area], last - row->glyphs[area],
26772 DRAW_NORMAL_TEXT, 0);
26777 /* Redraw the parts of the glyph row ROW on window W intersecting
26778 rectangle R. R is in window-relative coordinates. Value is
26779 non-zero if mouse-face was overwritten. */
26781 static int
26782 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
26784 xassert (row->enabled_p);
26786 if (row->mode_line_p || w->pseudo_window_p)
26787 draw_glyphs (w, 0, row, TEXT_AREA,
26788 0, row->used[TEXT_AREA],
26789 DRAW_NORMAL_TEXT, 0);
26790 else
26792 if (row->used[LEFT_MARGIN_AREA])
26793 expose_area (w, row, r, LEFT_MARGIN_AREA);
26794 if (row->used[TEXT_AREA])
26795 expose_area (w, row, r, TEXT_AREA);
26796 if (row->used[RIGHT_MARGIN_AREA])
26797 expose_area (w, row, r, RIGHT_MARGIN_AREA);
26798 draw_row_fringe_bitmaps (w, row);
26801 return row->mouse_face_p;
26805 /* Redraw those parts of glyphs rows during expose event handling that
26806 overlap other rows. Redrawing of an exposed line writes over parts
26807 of lines overlapping that exposed line; this function fixes that.
26809 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
26810 row in W's current matrix that is exposed and overlaps other rows.
26811 LAST_OVERLAPPING_ROW is the last such row. */
26813 static void
26814 expose_overlaps (struct window *w,
26815 struct glyph_row *first_overlapping_row,
26816 struct glyph_row *last_overlapping_row,
26817 XRectangle *r)
26819 struct glyph_row *row;
26821 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
26822 if (row->overlapping_p)
26824 xassert (row->enabled_p && !row->mode_line_p);
26826 row->clip = r;
26827 if (row->used[LEFT_MARGIN_AREA])
26828 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
26830 if (row->used[TEXT_AREA])
26831 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
26833 if (row->used[RIGHT_MARGIN_AREA])
26834 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
26835 row->clip = NULL;
26840 /* Return non-zero if W's cursor intersects rectangle R. */
26842 static int
26843 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
26845 XRectangle cr, result;
26846 struct glyph *cursor_glyph;
26847 struct glyph_row *row;
26849 if (w->phys_cursor.vpos >= 0
26850 && w->phys_cursor.vpos < w->current_matrix->nrows
26851 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
26852 row->enabled_p)
26853 && row->cursor_in_fringe_p)
26855 /* Cursor is in the fringe. */
26856 cr.x = window_box_right_offset (w,
26857 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
26858 ? RIGHT_MARGIN_AREA
26859 : TEXT_AREA));
26860 cr.y = row->y;
26861 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
26862 cr.height = row->height;
26863 return x_intersect_rectangles (&cr, r, &result);
26866 cursor_glyph = get_phys_cursor_glyph (w);
26867 if (cursor_glyph)
26869 /* r is relative to W's box, but w->phys_cursor.x is relative
26870 to left edge of W's TEXT area. Adjust it. */
26871 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
26872 cr.y = w->phys_cursor.y;
26873 cr.width = cursor_glyph->pixel_width;
26874 cr.height = w->phys_cursor_height;
26875 /* ++KFS: W32 version used W32-specific IntersectRect here, but
26876 I assume the effect is the same -- and this is portable. */
26877 return x_intersect_rectangles (&cr, r, &result);
26879 /* If we don't understand the format, pretend we're not in the hot-spot. */
26880 return 0;
26884 /* EXPORT:
26885 Draw a vertical window border to the right of window W if W doesn't
26886 have vertical scroll bars. */
26888 void
26889 x_draw_vertical_border (struct window *w)
26891 struct frame *f = XFRAME (WINDOW_FRAME (w));
26893 /* We could do better, if we knew what type of scroll-bar the adjacent
26894 windows (on either side) have... But we don't :-(
26895 However, I think this works ok. ++KFS 2003-04-25 */
26897 /* Redraw borders between horizontally adjacent windows. Don't
26898 do it for frames with vertical scroll bars because either the
26899 right scroll bar of a window, or the left scroll bar of its
26900 neighbor will suffice as a border. */
26901 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
26902 return;
26904 if (!WINDOW_RIGHTMOST_P (w)
26905 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
26907 int x0, x1, y0, y1;
26909 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26910 y1 -= 1;
26912 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26913 x1 -= 1;
26915 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
26917 else if (!WINDOW_LEFTMOST_P (w)
26918 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
26920 int x0, x1, y0, y1;
26922 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26923 y1 -= 1;
26925 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26926 x0 -= 1;
26928 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
26933 /* Redraw the part of window W intersection rectangle FR. Pixel
26934 coordinates in FR are frame-relative. Call this function with
26935 input blocked. Value is non-zero if the exposure overwrites
26936 mouse-face. */
26938 static int
26939 expose_window (struct window *w, XRectangle *fr)
26941 struct frame *f = XFRAME (w->frame);
26942 XRectangle wr, r;
26943 int mouse_face_overwritten_p = 0;
26945 /* If window is not yet fully initialized, do nothing. This can
26946 happen when toolkit scroll bars are used and a window is split.
26947 Reconfiguring the scroll bar will generate an expose for a newly
26948 created window. */
26949 if (w->current_matrix == NULL)
26950 return 0;
26952 /* When we're currently updating the window, display and current
26953 matrix usually don't agree. Arrange for a thorough display
26954 later. */
26955 if (w == updated_window)
26957 SET_FRAME_GARBAGED (f);
26958 return 0;
26961 /* Frame-relative pixel rectangle of W. */
26962 wr.x = WINDOW_LEFT_EDGE_X (w);
26963 wr.y = WINDOW_TOP_EDGE_Y (w);
26964 wr.width = WINDOW_TOTAL_WIDTH (w);
26965 wr.height = WINDOW_TOTAL_HEIGHT (w);
26967 if (x_intersect_rectangles (fr, &wr, &r))
26969 int yb = window_text_bottom_y (w);
26970 struct glyph_row *row;
26971 int cursor_cleared_p;
26972 struct glyph_row *first_overlapping_row, *last_overlapping_row;
26974 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
26975 r.x, r.y, r.width, r.height));
26977 /* Convert to window coordinates. */
26978 r.x -= WINDOW_LEFT_EDGE_X (w);
26979 r.y -= WINDOW_TOP_EDGE_Y (w);
26981 /* Turn off the cursor. */
26982 if (!w->pseudo_window_p
26983 && phys_cursor_in_rect_p (w, &r))
26985 x_clear_cursor (w);
26986 cursor_cleared_p = 1;
26988 else
26989 cursor_cleared_p = 0;
26991 /* Update lines intersecting rectangle R. */
26992 first_overlapping_row = last_overlapping_row = NULL;
26993 for (row = w->current_matrix->rows;
26994 row->enabled_p;
26995 ++row)
26997 int y0 = row->y;
26998 int y1 = MATRIX_ROW_BOTTOM_Y (row);
27000 if ((y0 >= r.y && y0 < r.y + r.height)
27001 || (y1 > r.y && y1 < r.y + r.height)
27002 || (r.y >= y0 && r.y < y1)
27003 || (r.y + r.height > y0 && r.y + r.height < y1))
27005 /* A header line may be overlapping, but there is no need
27006 to fix overlapping areas for them. KFS 2005-02-12 */
27007 if (row->overlapping_p && !row->mode_line_p)
27009 if (first_overlapping_row == NULL)
27010 first_overlapping_row = row;
27011 last_overlapping_row = row;
27014 row->clip = fr;
27015 if (expose_line (w, row, &r))
27016 mouse_face_overwritten_p = 1;
27017 row->clip = NULL;
27019 else if (row->overlapping_p)
27021 /* We must redraw a row overlapping the exposed area. */
27022 if (y0 < r.y
27023 ? y0 + row->phys_height > r.y
27024 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
27026 if (first_overlapping_row == NULL)
27027 first_overlapping_row = row;
27028 last_overlapping_row = row;
27032 if (y1 >= yb)
27033 break;
27036 /* Display the mode line if there is one. */
27037 if (WINDOW_WANTS_MODELINE_P (w)
27038 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
27039 row->enabled_p)
27040 && row->y < r.y + r.height)
27042 if (expose_line (w, row, &r))
27043 mouse_face_overwritten_p = 1;
27046 if (!w->pseudo_window_p)
27048 /* Fix the display of overlapping rows. */
27049 if (first_overlapping_row)
27050 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
27051 fr);
27053 /* Draw border between windows. */
27054 x_draw_vertical_border (w);
27056 /* Turn the cursor on again. */
27057 if (cursor_cleared_p)
27058 update_window_cursor (w, 1);
27062 return mouse_face_overwritten_p;
27067 /* Redraw (parts) of all windows in the window tree rooted at W that
27068 intersect R. R contains frame pixel coordinates. Value is
27069 non-zero if the exposure overwrites mouse-face. */
27071 static int
27072 expose_window_tree (struct window *w, XRectangle *r)
27074 struct frame *f = XFRAME (w->frame);
27075 int mouse_face_overwritten_p = 0;
27077 while (w && !FRAME_GARBAGED_P (f))
27079 if (!NILP (w->hchild))
27080 mouse_face_overwritten_p
27081 |= expose_window_tree (XWINDOW (w->hchild), r);
27082 else if (!NILP (w->vchild))
27083 mouse_face_overwritten_p
27084 |= expose_window_tree (XWINDOW (w->vchild), r);
27085 else
27086 mouse_face_overwritten_p |= expose_window (w, r);
27088 w = NILP (w->next) ? NULL : XWINDOW (w->next);
27091 return mouse_face_overwritten_p;
27095 /* EXPORT:
27096 Redisplay an exposed area of frame F. X and Y are the upper-left
27097 corner of the exposed rectangle. W and H are width and height of
27098 the exposed area. All are pixel values. W or H zero means redraw
27099 the entire frame. */
27101 void
27102 expose_frame (struct frame *f, int x, int y, int w, int h)
27104 XRectangle r;
27105 int mouse_face_overwritten_p = 0;
27107 TRACE ((stderr, "expose_frame "));
27109 /* No need to redraw if frame will be redrawn soon. */
27110 if (FRAME_GARBAGED_P (f))
27112 TRACE ((stderr, " garbaged\n"));
27113 return;
27116 /* If basic faces haven't been realized yet, there is no point in
27117 trying to redraw anything. This can happen when we get an expose
27118 event while Emacs is starting, e.g. by moving another window. */
27119 if (FRAME_FACE_CACHE (f) == NULL
27120 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
27122 TRACE ((stderr, " no faces\n"));
27123 return;
27126 if (w == 0 || h == 0)
27128 r.x = r.y = 0;
27129 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
27130 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
27132 else
27134 r.x = x;
27135 r.y = y;
27136 r.width = w;
27137 r.height = h;
27140 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
27141 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
27143 if (WINDOWP (f->tool_bar_window))
27144 mouse_face_overwritten_p
27145 |= expose_window (XWINDOW (f->tool_bar_window), &r);
27147 #ifdef HAVE_X_WINDOWS
27148 #ifndef MSDOS
27149 #ifndef USE_X_TOOLKIT
27150 if (WINDOWP (f->menu_bar_window))
27151 mouse_face_overwritten_p
27152 |= expose_window (XWINDOW (f->menu_bar_window), &r);
27153 #endif /* not USE_X_TOOLKIT */
27154 #endif
27155 #endif
27157 /* Some window managers support a focus-follows-mouse style with
27158 delayed raising of frames. Imagine a partially obscured frame,
27159 and moving the mouse into partially obscured mouse-face on that
27160 frame. The visible part of the mouse-face will be highlighted,
27161 then the WM raises the obscured frame. With at least one WM, KDE
27162 2.1, Emacs is not getting any event for the raising of the frame
27163 (even tried with SubstructureRedirectMask), only Expose events.
27164 These expose events will draw text normally, i.e. not
27165 highlighted. Which means we must redo the highlight here.
27166 Subsume it under ``we love X''. --gerd 2001-08-15 */
27167 /* Included in Windows version because Windows most likely does not
27168 do the right thing if any third party tool offers
27169 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
27170 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
27172 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27173 if (f == hlinfo->mouse_face_mouse_frame)
27175 int mouse_x = hlinfo->mouse_face_mouse_x;
27176 int mouse_y = hlinfo->mouse_face_mouse_y;
27177 clear_mouse_face (hlinfo);
27178 note_mouse_highlight (f, mouse_x, mouse_y);
27184 /* EXPORT:
27185 Determine the intersection of two rectangles R1 and R2. Return
27186 the intersection in *RESULT. Value is non-zero if RESULT is not
27187 empty. */
27190 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
27192 XRectangle *left, *right;
27193 XRectangle *upper, *lower;
27194 int intersection_p = 0;
27196 /* Rearrange so that R1 is the left-most rectangle. */
27197 if (r1->x < r2->x)
27198 left = r1, right = r2;
27199 else
27200 left = r2, right = r1;
27202 /* X0 of the intersection is right.x0, if this is inside R1,
27203 otherwise there is no intersection. */
27204 if (right->x <= left->x + left->width)
27206 result->x = right->x;
27208 /* The right end of the intersection is the minimum of
27209 the right ends of left and right. */
27210 result->width = (min (left->x + left->width, right->x + right->width)
27211 - result->x);
27213 /* Same game for Y. */
27214 if (r1->y < r2->y)
27215 upper = r1, lower = r2;
27216 else
27217 upper = r2, lower = r1;
27219 /* The upper end of the intersection is lower.y0, if this is inside
27220 of upper. Otherwise, there is no intersection. */
27221 if (lower->y <= upper->y + upper->height)
27223 result->y = lower->y;
27225 /* The lower end of the intersection is the minimum of the lower
27226 ends of upper and lower. */
27227 result->height = (min (lower->y + lower->height,
27228 upper->y + upper->height)
27229 - result->y);
27230 intersection_p = 1;
27234 return intersection_p;
27237 #endif /* HAVE_WINDOW_SYSTEM */
27240 /***********************************************************************
27241 Initialization
27242 ***********************************************************************/
27244 void
27245 syms_of_xdisp (void)
27247 Vwith_echo_area_save_vector = Qnil;
27248 staticpro (&Vwith_echo_area_save_vector);
27250 Vmessage_stack = Qnil;
27251 staticpro (&Vmessage_stack);
27253 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
27255 message_dolog_marker1 = Fmake_marker ();
27256 staticpro (&message_dolog_marker1);
27257 message_dolog_marker2 = Fmake_marker ();
27258 staticpro (&message_dolog_marker2);
27259 message_dolog_marker3 = Fmake_marker ();
27260 staticpro (&message_dolog_marker3);
27262 #if GLYPH_DEBUG
27263 defsubr (&Sdump_frame_glyph_matrix);
27264 defsubr (&Sdump_glyph_matrix);
27265 defsubr (&Sdump_glyph_row);
27266 defsubr (&Sdump_tool_bar_row);
27267 defsubr (&Strace_redisplay);
27268 defsubr (&Strace_to_stderr);
27269 #endif
27270 #ifdef HAVE_WINDOW_SYSTEM
27271 defsubr (&Stool_bar_lines_needed);
27272 defsubr (&Slookup_image_map);
27273 #endif
27274 defsubr (&Sformat_mode_line);
27275 defsubr (&Sinvisible_p);
27276 defsubr (&Scurrent_bidi_paragraph_direction);
27278 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
27279 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
27280 DEFSYM (Qoverriding_local_map, "overriding-local-map");
27281 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
27282 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
27283 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
27284 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
27285 DEFSYM (Qeval, "eval");
27286 DEFSYM (QCdata, ":data");
27287 DEFSYM (Qdisplay, "display");
27288 DEFSYM (Qspace_width, "space-width");
27289 DEFSYM (Qraise, "raise");
27290 DEFSYM (Qslice, "slice");
27291 DEFSYM (Qspace, "space");
27292 DEFSYM (Qmargin, "margin");
27293 DEFSYM (Qpointer, "pointer");
27294 DEFSYM (Qleft_margin, "left-margin");
27295 DEFSYM (Qright_margin, "right-margin");
27296 DEFSYM (Qcenter, "center");
27297 DEFSYM (Qline_height, "line-height");
27298 DEFSYM (QCalign_to, ":align-to");
27299 DEFSYM (QCrelative_width, ":relative-width");
27300 DEFSYM (QCrelative_height, ":relative-height");
27301 DEFSYM (QCeval, ":eval");
27302 DEFSYM (QCpropertize, ":propertize");
27303 DEFSYM (QCfile, ":file");
27304 DEFSYM (Qfontified, "fontified");
27305 DEFSYM (Qfontification_functions, "fontification-functions");
27306 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
27307 DEFSYM (Qescape_glyph, "escape-glyph");
27308 DEFSYM (Qnobreak_space, "nobreak-space");
27309 DEFSYM (Qimage, "image");
27310 DEFSYM (Qtext, "text");
27311 DEFSYM (Qboth, "both");
27312 DEFSYM (Qboth_horiz, "both-horiz");
27313 DEFSYM (Qtext_image_horiz, "text-image-horiz");
27314 DEFSYM (QCmap, ":map");
27315 DEFSYM (QCpointer, ":pointer");
27316 DEFSYM (Qrect, "rect");
27317 DEFSYM (Qcircle, "circle");
27318 DEFSYM (Qpoly, "poly");
27319 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
27320 DEFSYM (Qgrow_only, "grow-only");
27321 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
27322 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
27323 DEFSYM (Qposition, "position");
27324 DEFSYM (Qbuffer_position, "buffer-position");
27325 DEFSYM (Qobject, "object");
27326 DEFSYM (Qbar, "bar");
27327 DEFSYM (Qhbar, "hbar");
27328 DEFSYM (Qbox, "box");
27329 DEFSYM (Qhollow, "hollow");
27330 DEFSYM (Qhand, "hand");
27331 DEFSYM (Qarrow, "arrow");
27332 DEFSYM (Qtext, "text");
27333 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
27335 list_of_error = Fcons (Fcons (intern_c_string ("error"),
27336 Fcons (intern_c_string ("void-variable"), Qnil)),
27337 Qnil);
27338 staticpro (&list_of_error);
27340 DEFSYM (Qlast_arrow_position, "last-arrow-position");
27341 DEFSYM (Qlast_arrow_string, "last-arrow-string");
27342 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
27343 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
27345 echo_buffer[0] = echo_buffer[1] = Qnil;
27346 staticpro (&echo_buffer[0]);
27347 staticpro (&echo_buffer[1]);
27349 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
27350 staticpro (&echo_area_buffer[0]);
27351 staticpro (&echo_area_buffer[1]);
27353 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
27354 staticpro (&Vmessages_buffer_name);
27356 mode_line_proptrans_alist = Qnil;
27357 staticpro (&mode_line_proptrans_alist);
27358 mode_line_string_list = Qnil;
27359 staticpro (&mode_line_string_list);
27360 mode_line_string_face = Qnil;
27361 staticpro (&mode_line_string_face);
27362 mode_line_string_face_prop = Qnil;
27363 staticpro (&mode_line_string_face_prop);
27364 Vmode_line_unwind_vector = Qnil;
27365 staticpro (&Vmode_line_unwind_vector);
27367 help_echo_string = Qnil;
27368 staticpro (&help_echo_string);
27369 help_echo_object = Qnil;
27370 staticpro (&help_echo_object);
27371 help_echo_window = Qnil;
27372 staticpro (&help_echo_window);
27373 previous_help_echo_string = Qnil;
27374 staticpro (&previous_help_echo_string);
27375 help_echo_pos = -1;
27377 DEFSYM (Qright_to_left, "right-to-left");
27378 DEFSYM (Qleft_to_right, "left-to-right");
27380 #ifdef HAVE_WINDOW_SYSTEM
27381 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
27382 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
27383 For example, if a block cursor is over a tab, it will be drawn as
27384 wide as that tab on the display. */);
27385 x_stretch_cursor_p = 0;
27386 #endif
27388 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
27389 doc: /* *Non-nil means highlight trailing whitespace.
27390 The face used for trailing whitespace is `trailing-whitespace'. */);
27391 Vshow_trailing_whitespace = Qnil;
27393 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
27394 doc: /* *Control highlighting of nobreak space and soft hyphen.
27395 A value of t means highlight the character itself (for nobreak space,
27396 use face `nobreak-space').
27397 A value of nil means no highlighting.
27398 Other values mean display the escape glyph followed by an ordinary
27399 space or ordinary hyphen. */);
27400 Vnobreak_char_display = Qt;
27402 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
27403 doc: /* *The pointer shape to show in void text areas.
27404 A value of nil means to show the text pointer. Other options are `arrow',
27405 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
27406 Vvoid_text_area_pointer = Qarrow;
27408 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
27409 doc: /* Non-nil means don't actually do any redisplay.
27410 This is used for internal purposes. */);
27411 Vinhibit_redisplay = Qnil;
27413 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
27414 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
27415 Vglobal_mode_string = Qnil;
27417 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
27418 doc: /* Marker for where to display an arrow on top of the buffer text.
27419 This must be the beginning of a line in order to work.
27420 See also `overlay-arrow-string'. */);
27421 Voverlay_arrow_position = Qnil;
27423 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
27424 doc: /* String to display as an arrow in non-window frames.
27425 See also `overlay-arrow-position'. */);
27426 Voverlay_arrow_string = make_pure_c_string ("=>");
27428 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
27429 doc: /* List of variables (symbols) which hold markers for overlay arrows.
27430 The symbols on this list are examined during redisplay to determine
27431 where to display overlay arrows. */);
27432 Voverlay_arrow_variable_list
27433 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
27435 DEFVAR_INT ("scroll-step", emacs_scroll_step,
27436 doc: /* *The number of lines to try scrolling a window by when point moves out.
27437 If that fails to bring point back on frame, point is centered instead.
27438 If this is zero, point is always centered after it moves off frame.
27439 If you want scrolling to always be a line at a time, you should set
27440 `scroll-conservatively' to a large value rather than set this to 1. */);
27442 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
27443 doc: /* *Scroll up to this many lines, to bring point back on screen.
27444 If point moves off-screen, redisplay will scroll by up to
27445 `scroll-conservatively' lines in order to bring point just barely
27446 onto the screen again. If that cannot be done, then redisplay
27447 recenters point as usual.
27449 If the value is greater than 100, redisplay will never recenter point,
27450 but will always scroll just enough text to bring point into view, even
27451 if you move far away.
27453 A value of zero means always recenter point if it moves off screen. */);
27454 scroll_conservatively = 0;
27456 DEFVAR_INT ("scroll-margin", scroll_margin,
27457 doc: /* *Number of lines of margin at the top and bottom of a window.
27458 Recenter the window whenever point gets within this many lines
27459 of the top or bottom of the window. */);
27460 scroll_margin = 0;
27462 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
27463 doc: /* Pixels per inch value for non-window system displays.
27464 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
27465 Vdisplay_pixels_per_inch = make_float (72.0);
27467 #if GLYPH_DEBUG
27468 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
27469 #endif
27471 DEFVAR_LISP ("truncate-partial-width-windows",
27472 Vtruncate_partial_width_windows,
27473 doc: /* Non-nil means truncate lines in windows narrower than the frame.
27474 For an integer value, truncate lines in each window narrower than the
27475 full frame width, provided the window width is less than that integer;
27476 otherwise, respect the value of `truncate-lines'.
27478 For any other non-nil value, truncate lines in all windows that do
27479 not span the full frame width.
27481 A value of nil means to respect the value of `truncate-lines'.
27483 If `word-wrap' is enabled, you might want to reduce this. */);
27484 Vtruncate_partial_width_windows = make_number (50);
27486 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
27487 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
27488 Any other value means to use the appropriate face, `mode-line',
27489 `header-line', or `menu' respectively. */);
27490 mode_line_inverse_video = 1;
27492 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
27493 doc: /* *Maximum buffer size for which line number should be displayed.
27494 If the buffer is bigger than this, the line number does not appear
27495 in the mode line. A value of nil means no limit. */);
27496 Vline_number_display_limit = Qnil;
27498 DEFVAR_INT ("line-number-display-limit-width",
27499 line_number_display_limit_width,
27500 doc: /* *Maximum line width (in characters) for line number display.
27501 If the average length of the lines near point is bigger than this, then the
27502 line number may be omitted from the mode line. */);
27503 line_number_display_limit_width = 200;
27505 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
27506 doc: /* *Non-nil means highlight region even in nonselected windows. */);
27507 highlight_nonselected_windows = 0;
27509 DEFVAR_BOOL ("multiple-frames", multiple_frames,
27510 doc: /* Non-nil if more than one frame is visible on this display.
27511 Minibuffer-only frames don't count, but iconified frames do.
27512 This variable is not guaranteed to be accurate except while processing
27513 `frame-title-format' and `icon-title-format'. */);
27515 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
27516 doc: /* Template for displaying the title bar of visible frames.
27517 \(Assuming the window manager supports this feature.)
27519 This variable has the same structure as `mode-line-format', except that
27520 the %c and %l constructs are ignored. It is used only on frames for
27521 which no explicit name has been set \(see `modify-frame-parameters'). */);
27523 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
27524 doc: /* Template for displaying the title bar of an iconified frame.
27525 \(Assuming the window manager supports this feature.)
27526 This variable has the same structure as `mode-line-format' (which see),
27527 and is used only on frames for which no explicit name has been set
27528 \(see `modify-frame-parameters'). */);
27529 Vicon_title_format
27530 = Vframe_title_format
27531 = pure_cons (intern_c_string ("multiple-frames"),
27532 pure_cons (make_pure_c_string ("%b"),
27533 pure_cons (pure_cons (empty_unibyte_string,
27534 pure_cons (intern_c_string ("invocation-name"),
27535 pure_cons (make_pure_c_string ("@"),
27536 pure_cons (intern_c_string ("system-name"),
27537 Qnil)))),
27538 Qnil)));
27540 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
27541 doc: /* Maximum number of lines to keep in the message log buffer.
27542 If nil, disable message logging. If t, log messages but don't truncate
27543 the buffer when it becomes large. */);
27544 Vmessage_log_max = make_number (100);
27546 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
27547 doc: /* Functions called before redisplay, if window sizes have changed.
27548 The value should be a list of functions that take one argument.
27549 Just before redisplay, for each frame, if any of its windows have changed
27550 size since the last redisplay, or have been split or deleted,
27551 all the functions in the list are called, with the frame as argument. */);
27552 Vwindow_size_change_functions = Qnil;
27554 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
27555 doc: /* List of functions to call before redisplaying a window with scrolling.
27556 Each function is called with two arguments, the window and its new
27557 display-start position. Note that these functions are also called by
27558 `set-window-buffer'. Also note that the value of `window-end' is not
27559 valid when these functions are called. */);
27560 Vwindow_scroll_functions = Qnil;
27562 DEFVAR_LISP ("window-text-change-functions",
27563 Vwindow_text_change_functions,
27564 doc: /* Functions to call in redisplay when text in the window might change. */);
27565 Vwindow_text_change_functions = Qnil;
27567 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
27568 doc: /* Functions called when redisplay of a window reaches the end trigger.
27569 Each function is called with two arguments, the window and the end trigger value.
27570 See `set-window-redisplay-end-trigger'. */);
27571 Vredisplay_end_trigger_functions = Qnil;
27573 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
27574 doc: /* *Non-nil means autoselect window with mouse pointer.
27575 If nil, do not autoselect windows.
27576 A positive number means delay autoselection by that many seconds: a
27577 window is autoselected only after the mouse has remained in that
27578 window for the duration of the delay.
27579 A negative number has a similar effect, but causes windows to be
27580 autoselected only after the mouse has stopped moving. \(Because of
27581 the way Emacs compares mouse events, you will occasionally wait twice
27582 that time before the window gets selected.\)
27583 Any other value means to autoselect window instantaneously when the
27584 mouse pointer enters it.
27586 Autoselection selects the minibuffer only if it is active, and never
27587 unselects the minibuffer if it is active.
27589 When customizing this variable make sure that the actual value of
27590 `focus-follows-mouse' matches the behavior of your window manager. */);
27591 Vmouse_autoselect_window = Qnil;
27593 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
27594 doc: /* *Non-nil means automatically resize tool-bars.
27595 This dynamically changes the tool-bar's height to the minimum height
27596 that is needed to make all tool-bar items visible.
27597 If value is `grow-only', the tool-bar's height is only increased
27598 automatically; to decrease the tool-bar height, use \\[recenter]. */);
27599 Vauto_resize_tool_bars = Qt;
27601 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
27602 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
27603 auto_raise_tool_bar_buttons_p = 1;
27605 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
27606 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
27607 make_cursor_line_fully_visible_p = 1;
27609 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
27610 doc: /* *Border below tool-bar in pixels.
27611 If an integer, use it as the height of the border.
27612 If it is one of `internal-border-width' or `border-width', use the
27613 value of the corresponding frame parameter.
27614 Otherwise, no border is added below the tool-bar. */);
27615 Vtool_bar_border = Qinternal_border_width;
27617 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
27618 doc: /* *Margin around tool-bar buttons in pixels.
27619 If an integer, use that for both horizontal and vertical margins.
27620 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
27621 HORZ specifying the horizontal margin, and VERT specifying the
27622 vertical margin. */);
27623 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
27625 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
27626 doc: /* *Relief thickness of tool-bar buttons. */);
27627 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
27629 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
27630 doc: /* Tool bar style to use.
27631 It can be one of
27632 image - show images only
27633 text - show text only
27634 both - show both, text below image
27635 both-horiz - show text to the right of the image
27636 text-image-horiz - show text to the left of the image
27637 any other - use system default or image if no system default. */);
27638 Vtool_bar_style = Qnil;
27640 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
27641 doc: /* *Maximum number of characters a label can have to be shown.
27642 The tool bar style must also show labels for this to have any effect, see
27643 `tool-bar-style'. */);
27644 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
27646 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
27647 doc: /* List of functions to call to fontify regions of text.
27648 Each function is called with one argument POS. Functions must
27649 fontify a region starting at POS in the current buffer, and give
27650 fontified regions the property `fontified'. */);
27651 Vfontification_functions = Qnil;
27652 Fmake_variable_buffer_local (Qfontification_functions);
27654 DEFVAR_BOOL ("unibyte-display-via-language-environment",
27655 unibyte_display_via_language_environment,
27656 doc: /* *Non-nil means display unibyte text according to language environment.
27657 Specifically, this means that raw bytes in the range 160-255 decimal
27658 are displayed by converting them to the equivalent multibyte characters
27659 according to the current language environment. As a result, they are
27660 displayed according to the current fontset.
27662 Note that this variable affects only how these bytes are displayed,
27663 but does not change the fact they are interpreted as raw bytes. */);
27664 unibyte_display_via_language_environment = 0;
27666 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
27667 doc: /* *Maximum height for resizing mini-windows (the minibuffer and the echo area).
27668 If a float, it specifies a fraction of the mini-window frame's height.
27669 If an integer, it specifies a number of lines. */);
27670 Vmax_mini_window_height = make_float (0.25);
27672 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
27673 doc: /* How to resize mini-windows (the minibuffer and the echo area).
27674 A value of nil means don't automatically resize mini-windows.
27675 A value of t means resize them to fit the text displayed in them.
27676 A value of `grow-only', the default, means let mini-windows grow only;
27677 they return to their normal size when the minibuffer is closed, or the
27678 echo area becomes empty. */);
27679 Vresize_mini_windows = Qgrow_only;
27681 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
27682 doc: /* Alist specifying how to blink the cursor off.
27683 Each element has the form (ON-STATE . OFF-STATE). Whenever the
27684 `cursor-type' frame-parameter or variable equals ON-STATE,
27685 comparing using `equal', Emacs uses OFF-STATE to specify
27686 how to blink it off. ON-STATE and OFF-STATE are values for
27687 the `cursor-type' frame parameter.
27689 If a frame's ON-STATE has no entry in this list,
27690 the frame's other specifications determine how to blink the cursor off. */);
27691 Vblink_cursor_alist = Qnil;
27693 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
27694 doc: /* Allow or disallow automatic horizontal scrolling of windows.
27695 If non-nil, windows are automatically scrolled horizontally to make
27696 point visible. */);
27697 automatic_hscrolling_p = 1;
27698 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
27700 DEFVAR_INT ("hscroll-margin", hscroll_margin,
27701 doc: /* *How many columns away from the window edge point is allowed to get
27702 before automatic hscrolling will horizontally scroll the window. */);
27703 hscroll_margin = 5;
27705 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
27706 doc: /* *How many columns to scroll the window when point gets too close to the edge.
27707 When point is less than `hscroll-margin' columns from the window
27708 edge, automatic hscrolling will scroll the window by the amount of columns
27709 determined by this variable. If its value is a positive integer, scroll that
27710 many columns. If it's a positive floating-point number, it specifies the
27711 fraction of the window's width to scroll. If it's nil or zero, point will be
27712 centered horizontally after the scroll. Any other value, including negative
27713 numbers, are treated as if the value were zero.
27715 Automatic hscrolling always moves point outside the scroll margin, so if
27716 point was more than scroll step columns inside the margin, the window will
27717 scroll more than the value given by the scroll step.
27719 Note that the lower bound for automatic hscrolling specified by `scroll-left'
27720 and `scroll-right' overrides this variable's effect. */);
27721 Vhscroll_step = make_number (0);
27723 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
27724 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
27725 Bind this around calls to `message' to let it take effect. */);
27726 message_truncate_lines = 0;
27728 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
27729 doc: /* Normal hook run to update the menu bar definitions.
27730 Redisplay runs this hook before it redisplays the menu bar.
27731 This is used to update submenus such as Buffers,
27732 whose contents depend on various data. */);
27733 Vmenu_bar_update_hook = Qnil;
27735 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
27736 doc: /* Frame for which we are updating a menu.
27737 The enable predicate for a menu binding should check this variable. */);
27738 Vmenu_updating_frame = Qnil;
27740 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
27741 doc: /* Non-nil means don't update menu bars. Internal use only. */);
27742 inhibit_menubar_update = 0;
27744 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
27745 doc: /* Prefix prepended to all continuation lines at display time.
27746 The value may be a string, an image, or a stretch-glyph; it is
27747 interpreted in the same way as the value of a `display' text property.
27749 This variable is overridden by any `wrap-prefix' text or overlay
27750 property.
27752 To add a prefix to non-continuation lines, use `line-prefix'. */);
27753 Vwrap_prefix = Qnil;
27754 DEFSYM (Qwrap_prefix, "wrap-prefix");
27755 Fmake_variable_buffer_local (Qwrap_prefix);
27757 DEFVAR_LISP ("line-prefix", Vline_prefix,
27758 doc: /* Prefix prepended to all non-continuation lines at display time.
27759 The value may be a string, an image, or a stretch-glyph; it is
27760 interpreted in the same way as the value of a `display' text property.
27762 This variable is overridden by any `line-prefix' text or overlay
27763 property.
27765 To add a prefix to continuation lines, use `wrap-prefix'. */);
27766 Vline_prefix = Qnil;
27767 DEFSYM (Qline_prefix, "line-prefix");
27768 Fmake_variable_buffer_local (Qline_prefix);
27770 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
27771 doc: /* Non-nil means don't eval Lisp during redisplay. */);
27772 inhibit_eval_during_redisplay = 0;
27774 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
27775 doc: /* Non-nil means don't free realized faces. Internal use only. */);
27776 inhibit_free_realized_faces = 0;
27778 #if GLYPH_DEBUG
27779 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
27780 doc: /* Inhibit try_window_id display optimization. */);
27781 inhibit_try_window_id = 0;
27783 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
27784 doc: /* Inhibit try_window_reusing display optimization. */);
27785 inhibit_try_window_reusing = 0;
27787 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
27788 doc: /* Inhibit try_cursor_movement display optimization. */);
27789 inhibit_try_cursor_movement = 0;
27790 #endif /* GLYPH_DEBUG */
27792 DEFVAR_INT ("overline-margin", overline_margin,
27793 doc: /* *Space between overline and text, in pixels.
27794 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
27795 margin to the caracter height. */);
27796 overline_margin = 2;
27798 DEFVAR_INT ("underline-minimum-offset",
27799 underline_minimum_offset,
27800 doc: /* Minimum distance between baseline and underline.
27801 This can improve legibility of underlined text at small font sizes,
27802 particularly when using variable `x-use-underline-position-properties'
27803 with fonts that specify an UNDERLINE_POSITION relatively close to the
27804 baseline. The default value is 1. */);
27805 underline_minimum_offset = 1;
27807 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
27808 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
27809 This feature only works when on a window system that can change
27810 cursor shapes. */);
27811 display_hourglass_p = 1;
27813 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
27814 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
27815 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
27817 hourglass_atimer = NULL;
27818 hourglass_shown_p = 0;
27820 DEFSYM (Qglyphless_char, "glyphless-char");
27821 DEFSYM (Qhex_code, "hex-code");
27822 DEFSYM (Qempty_box, "empty-box");
27823 DEFSYM (Qthin_space, "thin-space");
27824 DEFSYM (Qzero_width, "zero-width");
27826 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
27827 /* Intern this now in case it isn't already done.
27828 Setting this variable twice is harmless.
27829 But don't staticpro it here--that is done in alloc.c. */
27830 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
27831 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
27833 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
27834 doc: /* Char-table defining glyphless characters.
27835 Each element, if non-nil, should be one of the following:
27836 an ASCII acronym string: display this string in a box
27837 `hex-code': display the hexadecimal code of a character in a box
27838 `empty-box': display as an empty box
27839 `thin-space': display as 1-pixel width space
27840 `zero-width': don't display
27841 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
27842 display method for graphical terminals and text terminals respectively.
27843 GRAPHICAL and TEXT should each have one of the values listed above.
27845 The char-table has one extra slot to control the display of a character for
27846 which no font is found. This slot only takes effect on graphical terminals.
27847 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
27848 `thin-space'. The default is `empty-box'. */);
27849 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
27850 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
27851 Qempty_box);
27855 /* Initialize this module when Emacs starts. */
27857 void
27858 init_xdisp (void)
27860 current_header_line_height = current_mode_line_height = -1;
27862 CHARPOS (this_line_start_pos) = 0;
27864 if (!noninteractive)
27866 struct window *m = XWINDOW (minibuf_window);
27867 Lisp_Object frame = m->frame;
27868 struct frame *f = XFRAME (frame);
27869 Lisp_Object root = FRAME_ROOT_WINDOW (f);
27870 struct window *r = XWINDOW (root);
27871 int i;
27873 echo_area_window = minibuf_window;
27875 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
27876 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
27877 XSETFASTINT (r->total_cols, FRAME_COLS (f));
27878 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
27879 XSETFASTINT (m->total_lines, 1);
27880 XSETFASTINT (m->total_cols, FRAME_COLS (f));
27882 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
27883 scratch_glyph_row.glyphs[TEXT_AREA + 1]
27884 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
27886 /* The default ellipsis glyphs `...'. */
27887 for (i = 0; i < 3; ++i)
27888 default_invis_vector[i] = make_number ('.');
27892 /* Allocate the buffer for frame titles.
27893 Also used for `format-mode-line'. */
27894 int size = 100;
27895 mode_line_noprop_buf = (char *) xmalloc (size);
27896 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
27897 mode_line_noprop_ptr = mode_line_noprop_buf;
27898 mode_line_target = MODE_LINE_DISPLAY;
27901 help_echo_showing_p = 0;
27904 /* Since w32 does not support atimers, it defines its own implementation of
27905 the following three functions in w32fns.c. */
27906 #ifndef WINDOWSNT
27908 /* Platform-independent portion of hourglass implementation. */
27910 /* Return non-zero if houglass timer has been started or hourglass is shown. */
27912 hourglass_started (void)
27914 return hourglass_shown_p || hourglass_atimer != NULL;
27917 /* Cancel a currently active hourglass timer, and start a new one. */
27918 void
27919 start_hourglass (void)
27921 #if defined (HAVE_WINDOW_SYSTEM)
27922 EMACS_TIME delay;
27923 int secs, usecs = 0;
27925 cancel_hourglass ();
27927 if (INTEGERP (Vhourglass_delay)
27928 && XINT (Vhourglass_delay) > 0)
27929 secs = XFASTINT (Vhourglass_delay);
27930 else if (FLOATP (Vhourglass_delay)
27931 && XFLOAT_DATA (Vhourglass_delay) > 0)
27933 Lisp_Object tem;
27934 tem = Ftruncate (Vhourglass_delay, Qnil);
27935 secs = XFASTINT (tem);
27936 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
27938 else
27939 secs = DEFAULT_HOURGLASS_DELAY;
27941 EMACS_SET_SECS_USECS (delay, secs, usecs);
27942 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
27943 show_hourglass, NULL);
27944 #endif
27948 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
27949 shown. */
27950 void
27951 cancel_hourglass (void)
27953 #if defined (HAVE_WINDOW_SYSTEM)
27954 if (hourglass_atimer)
27956 cancel_atimer (hourglass_atimer);
27957 hourglass_atimer = NULL;
27960 if (hourglass_shown_p)
27961 hide_hourglass ();
27962 #endif
27964 #endif /* ! WINDOWSNT */