Fix a typo that caused crashes in redisplay.
[emacs.git] / src / xdisp.c
blobc5962541cb674e79b453ebef67bf7e93a759dc3b
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2012 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22 Redisplay.
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
53 expose_window (asynchronous) |
55 X expose events -----+
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
84 . try_cursor_movement
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
90 . try_window_reusing_current_matrix
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
96 . try_window_id
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
102 . try_window
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
115 Desired matrices.
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
160 Frame matrices.
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
181 Bidirectional display.
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
224 Bidirectional display and character compositions
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
251 Moving an iterator in bidirectional text
252 without producing glyphs
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
276 #include <setjmp.h>
278 #include "lisp.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "buffer.h"
285 #include "character.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
302 #ifdef HAVE_X_WINDOWS
303 #include "xterm.h"
304 #endif
305 #ifdef WINDOWSNT
306 #include "w32term.h"
307 #endif
308 #ifdef HAVE_NS
309 #include "nsterm.h"
310 #endif
311 #ifdef USE_GTK
312 #include "gtkutil.h"
313 #endif
315 #include "font.h"
317 #ifndef FRAME_X_OUTPUT
318 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
319 #endif
321 #define INFINITY 10000000
323 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
324 Lisp_Object Qwindow_scroll_functions;
325 static Lisp_Object Qwindow_text_change_functions;
326 static Lisp_Object Qredisplay_end_trigger_functions;
327 Lisp_Object Qinhibit_point_motion_hooks;
328 static Lisp_Object QCeval, QCpropertize;
329 Lisp_Object QCfile, QCdata;
330 static Lisp_Object Qfontified;
331 static Lisp_Object Qgrow_only;
332 static Lisp_Object Qinhibit_eval_during_redisplay;
333 static Lisp_Object Qbuffer_position, Qposition, Qobject;
334 static Lisp_Object Qright_to_left, Qleft_to_right;
336 /* Cursor shapes */
337 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
339 /* Pointer shapes */
340 static Lisp_Object Qarrow, Qhand;
341 Lisp_Object Qtext;
343 /* Holds the list (error). */
344 static Lisp_Object list_of_error;
346 static Lisp_Object Qfontification_functions;
348 static Lisp_Object Qwrap_prefix;
349 static Lisp_Object Qline_prefix;
351 /* Non-nil means don't actually do any redisplay. */
353 Lisp_Object Qinhibit_redisplay;
355 /* Names of text properties relevant for redisplay. */
357 Lisp_Object Qdisplay;
359 Lisp_Object Qspace, QCalign_to;
360 static Lisp_Object QCrelative_width, QCrelative_height;
361 Lisp_Object Qleft_margin, Qright_margin;
362 static Lisp_Object Qspace_width, Qraise;
363 static Lisp_Object Qslice;
364 Lisp_Object Qcenter;
365 static Lisp_Object Qmargin, Qpointer;
366 static Lisp_Object Qline_height;
368 #ifdef HAVE_WINDOW_SYSTEM
370 /* Test if overflow newline into fringe. Called with iterator IT
371 at or past right window margin, and with IT->current_x set. */
373 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
374 (!NILP (Voverflow_newline_into_fringe) \
375 && FRAME_WINDOW_P ((IT)->f) \
376 && ((IT)->bidi_it.paragraph_dir == R2L \
377 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
378 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
379 && (IT)->current_x == (IT)->last_visible_x \
380 && (IT)->line_wrap != WORD_WRAP)
382 #else /* !HAVE_WINDOW_SYSTEM */
383 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
384 #endif /* HAVE_WINDOW_SYSTEM */
386 /* Test if the display element loaded in IT is a space or tab
387 character. This is used to determine word wrapping. */
389 #define IT_DISPLAYING_WHITESPACE(it) \
390 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
392 /* Name of the face used to highlight trailing whitespace. */
394 static Lisp_Object Qtrailing_whitespace;
396 /* Name and number of the face used to highlight escape glyphs. */
398 static Lisp_Object Qescape_glyph;
400 /* Name and number of the face used to highlight non-breaking spaces. */
402 static Lisp_Object Qnobreak_space;
404 /* The symbol `image' which is the car of the lists used to represent
405 images in Lisp. Also a tool bar style. */
407 Lisp_Object Qimage;
409 /* The image map types. */
410 Lisp_Object QCmap;
411 static Lisp_Object QCpointer;
412 static Lisp_Object Qrect, Qcircle, Qpoly;
414 /* Tool bar styles */
415 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
417 /* Non-zero means print newline to stdout before next mini-buffer
418 message. */
420 int noninteractive_need_newline;
422 /* Non-zero means print newline to message log before next message. */
424 static int message_log_need_newline;
426 /* Three markers that message_dolog uses.
427 It could allocate them itself, but that causes trouble
428 in handling memory-full errors. */
429 static Lisp_Object message_dolog_marker1;
430 static Lisp_Object message_dolog_marker2;
431 static Lisp_Object message_dolog_marker3;
433 /* The buffer position of the first character appearing entirely or
434 partially on the line of the selected window which contains the
435 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
436 redisplay optimization in redisplay_internal. */
438 static struct text_pos this_line_start_pos;
440 /* Number of characters past the end of the line above, including the
441 terminating newline. */
443 static struct text_pos this_line_end_pos;
445 /* The vertical positions and the height of this line. */
447 static int this_line_vpos;
448 static int this_line_y;
449 static int this_line_pixel_height;
451 /* X position at which this display line starts. Usually zero;
452 negative if first character is partially visible. */
454 static int this_line_start_x;
456 /* The smallest character position seen by move_it_* functions as they
457 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
458 hscrolled lines, see display_line. */
460 static struct text_pos this_line_min_pos;
462 /* Buffer that this_line_.* variables are referring to. */
464 static struct buffer *this_line_buffer;
467 /* Values of those variables at last redisplay are stored as
468 properties on `overlay-arrow-position' symbol. However, if
469 Voverlay_arrow_position is a marker, last-arrow-position is its
470 numerical position. */
472 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
474 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
475 properties on a symbol in overlay-arrow-variable-list. */
477 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
479 Lisp_Object Qmenu_bar_update_hook;
481 /* Nonzero if an overlay arrow has been displayed in this window. */
483 static int overlay_arrow_seen;
485 /* Number of windows showing the buffer of the selected window (or
486 another buffer with the same base buffer). keyboard.c refers to
487 this. */
489 int buffer_shared;
491 /* Vector containing glyphs for an ellipsis `...'. */
493 static Lisp_Object default_invis_vector[3];
495 /* This is the window where the echo area message was displayed. It
496 is always a mini-buffer window, but it may not be the same window
497 currently active as a mini-buffer. */
499 Lisp_Object echo_area_window;
501 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
502 pushes the current message and the value of
503 message_enable_multibyte on the stack, the function restore_message
504 pops the stack and displays MESSAGE again. */
506 static Lisp_Object Vmessage_stack;
508 /* Nonzero means multibyte characters were enabled when the echo area
509 message was specified. */
511 static int message_enable_multibyte;
513 /* Nonzero if we should redraw the mode lines on the next redisplay. */
515 int update_mode_lines;
517 /* Nonzero if window sizes or contents have changed since last
518 redisplay that finished. */
520 int windows_or_buffers_changed;
522 /* Nonzero means a frame's cursor type has been changed. */
524 int cursor_type_changed;
526 /* Nonzero after display_mode_line if %l was used and it displayed a
527 line number. */
529 static int line_number_displayed;
531 /* The name of the *Messages* buffer, a string. */
533 static Lisp_Object Vmessages_buffer_name;
535 /* Current, index 0, and last displayed echo area message. Either
536 buffers from echo_buffers, or nil to indicate no message. */
538 Lisp_Object echo_area_buffer[2];
540 /* The buffers referenced from echo_area_buffer. */
542 static Lisp_Object echo_buffer[2];
544 /* A vector saved used in with_area_buffer to reduce consing. */
546 static Lisp_Object Vwith_echo_area_save_vector;
548 /* Non-zero means display_echo_area should display the last echo area
549 message again. Set by redisplay_preserve_echo_area. */
551 static int display_last_displayed_message_p;
553 /* Nonzero if echo area is being used by print; zero if being used by
554 message. */
556 static int message_buf_print;
558 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
560 static Lisp_Object Qinhibit_menubar_update;
561 static Lisp_Object Qmessage_truncate_lines;
563 /* Set to 1 in clear_message to make redisplay_internal aware
564 of an emptied echo area. */
566 static int message_cleared_p;
568 /* A scratch glyph row with contents used for generating truncation
569 glyphs. Also used in direct_output_for_insert. */
571 #define MAX_SCRATCH_GLYPHS 100
572 static struct glyph_row scratch_glyph_row;
573 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
575 /* Ascent and height of the last line processed by move_it_to. */
577 static int last_max_ascent, last_height;
579 /* Non-zero if there's a help-echo in the echo area. */
581 int help_echo_showing_p;
583 /* If >= 0, computed, exact values of mode-line and header-line height
584 to use in the macros CURRENT_MODE_LINE_HEIGHT and
585 CURRENT_HEADER_LINE_HEIGHT. */
587 int current_mode_line_height, current_header_line_height;
589 /* The maximum distance to look ahead for text properties. Values
590 that are too small let us call compute_char_face and similar
591 functions too often which is expensive. Values that are too large
592 let us call compute_char_face and alike too often because we
593 might not be interested in text properties that far away. */
595 #define TEXT_PROP_DISTANCE_LIMIT 100
597 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
598 iterator state and later restore it. This is needed because the
599 bidi iterator on bidi.c keeps a stacked cache of its states, which
600 is really a singleton. When we use scratch iterator objects to
601 move around the buffer, we can cause the bidi cache to be pushed or
602 popped, and therefore we need to restore the cache state when we
603 return to the original iterator. */
604 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
605 do { \
606 if (CACHE) \
607 bidi_unshelve_cache (CACHE, 1); \
608 ITCOPY = ITORIG; \
609 CACHE = bidi_shelve_cache (); \
610 } while (0)
612 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
613 do { \
614 if (pITORIG != pITCOPY) \
615 *(pITORIG) = *(pITCOPY); \
616 bidi_unshelve_cache (CACHE, 0); \
617 CACHE = NULL; \
618 } while (0)
620 #if GLYPH_DEBUG
622 /* Non-zero means print traces of redisplay if compiled with
623 GLYPH_DEBUG != 0. */
625 int trace_redisplay_p;
627 #endif /* GLYPH_DEBUG */
629 #ifdef DEBUG_TRACE_MOVE
630 /* Non-zero means trace with TRACE_MOVE to stderr. */
631 int trace_move;
633 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
634 #else
635 #define TRACE_MOVE(x) (void) 0
636 #endif
638 static Lisp_Object Qauto_hscroll_mode;
640 /* Buffer being redisplayed -- for redisplay_window_error. */
642 static struct buffer *displayed_buffer;
644 /* Value returned from text property handlers (see below). */
646 enum prop_handled
648 HANDLED_NORMALLY,
649 HANDLED_RECOMPUTE_PROPS,
650 HANDLED_OVERLAY_STRING_CONSUMED,
651 HANDLED_RETURN
654 /* A description of text properties that redisplay is interested
655 in. */
657 struct props
659 /* The name of the property. */
660 Lisp_Object *name;
662 /* A unique index for the property. */
663 enum prop_idx idx;
665 /* A handler function called to set up iterator IT from the property
666 at IT's current position. Value is used to steer handle_stop. */
667 enum prop_handled (*handler) (struct it *it);
670 static enum prop_handled handle_face_prop (struct it *);
671 static enum prop_handled handle_invisible_prop (struct it *);
672 static enum prop_handled handle_display_prop (struct it *);
673 static enum prop_handled handle_composition_prop (struct it *);
674 static enum prop_handled handle_overlay_change (struct it *);
675 static enum prop_handled handle_fontified_prop (struct it *);
677 /* Properties handled by iterators. */
679 static struct props it_props[] =
681 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
682 /* Handle `face' before `display' because some sub-properties of
683 `display' need to know the face. */
684 {&Qface, FACE_PROP_IDX, handle_face_prop},
685 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
686 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
687 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
688 {NULL, 0, NULL}
691 /* Value is the position described by X. If X is a marker, value is
692 the marker_position of X. Otherwise, value is X. */
694 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
696 /* Enumeration returned by some move_it_.* functions internally. */
698 enum move_it_result
700 /* Not used. Undefined value. */
701 MOVE_UNDEFINED,
703 /* Move ended at the requested buffer position or ZV. */
704 MOVE_POS_MATCH_OR_ZV,
706 /* Move ended at the requested X pixel position. */
707 MOVE_X_REACHED,
709 /* Move within a line ended at the end of a line that must be
710 continued. */
711 MOVE_LINE_CONTINUED,
713 /* Move within a line ended at the end of a line that would
714 be displayed truncated. */
715 MOVE_LINE_TRUNCATED,
717 /* Move within a line ended at a line end. */
718 MOVE_NEWLINE_OR_CR
721 /* This counter is used to clear the face cache every once in a while
722 in redisplay_internal. It is incremented for each redisplay.
723 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
724 cleared. */
726 #define CLEAR_FACE_CACHE_COUNT 500
727 static int clear_face_cache_count;
729 /* Similarly for the image cache. */
731 #ifdef HAVE_WINDOW_SYSTEM
732 #define CLEAR_IMAGE_CACHE_COUNT 101
733 static int clear_image_cache_count;
735 /* Null glyph slice */
736 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
737 #endif
739 /* Non-zero while redisplay_internal is in progress. */
741 int redisplaying_p;
743 static Lisp_Object Qinhibit_free_realized_faces;
745 /* If a string, XTread_socket generates an event to display that string.
746 (The display is done in read_char.) */
748 Lisp_Object help_echo_string;
749 Lisp_Object help_echo_window;
750 Lisp_Object help_echo_object;
751 EMACS_INT help_echo_pos;
753 /* Temporary variable for XTread_socket. */
755 Lisp_Object previous_help_echo_string;
757 /* Platform-independent portion of hourglass implementation. */
759 /* Non-zero means an hourglass cursor is currently shown. */
760 int hourglass_shown_p;
762 /* If non-null, an asynchronous timer that, when it expires, displays
763 an hourglass cursor on all frames. */
764 struct atimer *hourglass_atimer;
766 /* Name of the face used to display glyphless characters. */
767 Lisp_Object Qglyphless_char;
769 /* Symbol for the purpose of Vglyphless_char_display. */
770 static Lisp_Object Qglyphless_char_display;
772 /* Method symbols for Vglyphless_char_display. */
773 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
775 /* Default pixel width of `thin-space' display method. */
776 #define THIN_SPACE_WIDTH 1
778 /* Default number of seconds to wait before displaying an hourglass
779 cursor. */
780 #define DEFAULT_HOURGLASS_DELAY 1
783 /* Function prototypes. */
785 static void setup_for_ellipsis (struct it *, int);
786 static void set_iterator_to_next (struct it *, int);
787 static void mark_window_display_accurate_1 (struct window *, int);
788 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
789 static int display_prop_string_p (Lisp_Object, Lisp_Object);
790 static int cursor_row_p (struct glyph_row *);
791 static int redisplay_mode_lines (Lisp_Object, int);
792 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
794 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
796 static void handle_line_prefix (struct it *);
798 static void pint2str (char *, int, EMACS_INT);
799 static void pint2hrstr (char *, int, EMACS_INT);
800 static struct text_pos run_window_scroll_functions (Lisp_Object,
801 struct text_pos);
802 static void reconsider_clip_changes (struct window *, struct buffer *);
803 static int text_outside_line_unchanged_p (struct window *,
804 EMACS_INT, EMACS_INT);
805 static void store_mode_line_noprop_char (char);
806 static int store_mode_line_noprop (const char *, int, int);
807 static void handle_stop (struct it *);
808 static void handle_stop_backwards (struct it *, EMACS_INT);
809 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
810 static void ensure_echo_area_buffers (void);
811 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
812 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
813 static int with_echo_area_buffer (struct window *, int,
814 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
815 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
816 static void clear_garbaged_frames (void);
817 static int current_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
818 static void pop_message (void);
819 static int truncate_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
820 static void set_message (const char *, Lisp_Object, EMACS_INT, int);
821 static int set_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
822 static int display_echo_area (struct window *);
823 static int display_echo_area_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
824 static int resize_mini_window_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
825 static Lisp_Object unwind_redisplay (Lisp_Object);
826 static int string_char_and_length (const unsigned char *, int *);
827 static struct text_pos display_prop_end (struct it *, Lisp_Object,
828 struct text_pos);
829 static int compute_window_start_on_continuation_line (struct window *);
830 static Lisp_Object safe_eval_handler (Lisp_Object);
831 static void insert_left_trunc_glyphs (struct it *);
832 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
833 Lisp_Object);
834 static void extend_face_to_end_of_line (struct it *);
835 static int append_space_for_newline (struct it *, int);
836 static int cursor_row_fully_visible_p (struct window *, int, int);
837 static int try_scrolling (Lisp_Object, int, EMACS_INT, EMACS_INT, int, int);
838 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
839 static int trailing_whitespace_p (EMACS_INT);
840 static intmax_t message_log_check_duplicate (EMACS_INT, EMACS_INT);
841 static void push_it (struct it *, struct text_pos *);
842 static void iterate_out_of_display_property (struct it *);
843 static void pop_it (struct it *);
844 static void sync_frame_with_window_matrix_rows (struct window *);
845 static void select_frame_for_redisplay (Lisp_Object);
846 static void redisplay_internal (void);
847 static int echo_area_display (int);
848 static void redisplay_windows (Lisp_Object);
849 static void redisplay_window (Lisp_Object, int);
850 static Lisp_Object redisplay_window_error (Lisp_Object);
851 static Lisp_Object redisplay_window_0 (Lisp_Object);
852 static Lisp_Object redisplay_window_1 (Lisp_Object);
853 static int set_cursor_from_row (struct window *, struct glyph_row *,
854 struct glyph_matrix *, EMACS_INT, EMACS_INT,
855 int, int);
856 static int update_menu_bar (struct frame *, int, int);
857 static int try_window_reusing_current_matrix (struct window *);
858 static int try_window_id (struct window *);
859 static int display_line (struct it *);
860 static int display_mode_lines (struct window *);
861 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
862 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
863 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
864 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
865 static void display_menu_bar (struct window *);
866 static EMACS_INT display_count_lines (EMACS_INT, EMACS_INT, EMACS_INT,
867 EMACS_INT *);
868 static int display_string (const char *, Lisp_Object, Lisp_Object,
869 EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
870 static void compute_line_metrics (struct it *);
871 static void run_redisplay_end_trigger_hook (struct it *);
872 static int get_overlay_strings (struct it *, EMACS_INT);
873 static int get_overlay_strings_1 (struct it *, EMACS_INT, int);
874 static void next_overlay_string (struct it *);
875 static void reseat (struct it *, struct text_pos, int);
876 static void reseat_1 (struct it *, struct text_pos, int);
877 static void back_to_previous_visible_line_start (struct it *);
878 void reseat_at_previous_visible_line_start (struct it *);
879 static void reseat_at_next_visible_line_start (struct it *, int);
880 static int next_element_from_ellipsis (struct it *);
881 static int next_element_from_display_vector (struct it *);
882 static int next_element_from_string (struct it *);
883 static int next_element_from_c_string (struct it *);
884 static int next_element_from_buffer (struct it *);
885 static int next_element_from_composition (struct it *);
886 static int next_element_from_image (struct it *);
887 static int next_element_from_stretch (struct it *);
888 static void load_overlay_strings (struct it *, EMACS_INT);
889 static int init_from_display_pos (struct it *, struct window *,
890 struct display_pos *);
891 static void reseat_to_string (struct it *, const char *,
892 Lisp_Object, EMACS_INT, EMACS_INT, int, int);
893 static int get_next_display_element (struct it *);
894 static enum move_it_result
895 move_it_in_display_line_to (struct it *, EMACS_INT, int,
896 enum move_operation_enum);
897 void move_it_vertically_backward (struct it *, int);
898 static void init_to_row_start (struct it *, struct window *,
899 struct glyph_row *);
900 static int init_to_row_end (struct it *, struct window *,
901 struct glyph_row *);
902 static void back_to_previous_line_start (struct it *);
903 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
904 static struct text_pos string_pos_nchars_ahead (struct text_pos,
905 Lisp_Object, EMACS_INT);
906 static struct text_pos string_pos (EMACS_INT, Lisp_Object);
907 static struct text_pos c_string_pos (EMACS_INT, const char *, int);
908 static EMACS_INT number_of_chars (const char *, int);
909 static void compute_stop_pos (struct it *);
910 static void compute_string_pos (struct text_pos *, struct text_pos,
911 Lisp_Object);
912 static int face_before_or_after_it_pos (struct it *, int);
913 static EMACS_INT next_overlay_change (EMACS_INT);
914 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
915 Lisp_Object, struct text_pos *, EMACS_INT, int);
916 static int handle_single_display_spec (struct it *, Lisp_Object,
917 Lisp_Object, Lisp_Object,
918 struct text_pos *, EMACS_INT, int, int);
919 static int underlying_face_id (struct it *);
920 static int in_ellipses_for_invisible_text_p (struct display_pos *,
921 struct window *);
923 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
924 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
926 #ifdef HAVE_WINDOW_SYSTEM
928 static void x_consider_frame_title (Lisp_Object);
929 static int tool_bar_lines_needed (struct frame *, int *);
930 static void update_tool_bar (struct frame *, int);
931 static void build_desired_tool_bar_string (struct frame *f);
932 static int redisplay_tool_bar (struct frame *);
933 static void display_tool_bar_line (struct it *, int);
934 static void notice_overwritten_cursor (struct window *,
935 enum glyph_row_area,
936 int, int, int, int);
937 static void append_stretch_glyph (struct it *, Lisp_Object,
938 int, int, int);
941 #endif /* HAVE_WINDOW_SYSTEM */
943 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
944 static int coords_in_mouse_face_p (struct window *, int, int);
948 /***********************************************************************
949 Window display dimensions
950 ***********************************************************************/
952 /* Return the bottom boundary y-position for text lines in window W.
953 This is the first y position at which a line cannot start.
954 It is relative to the top of the window.
956 This is the height of W minus the height of a mode line, if any. */
959 window_text_bottom_y (struct window *w)
961 int height = WINDOW_TOTAL_HEIGHT (w);
963 if (WINDOW_WANTS_MODELINE_P (w))
964 height -= CURRENT_MODE_LINE_HEIGHT (w);
965 return height;
968 /* Return the pixel width of display area AREA of window W. AREA < 0
969 means return the total width of W, not including fringes to
970 the left and right of the window. */
973 window_box_width (struct window *w, int area)
975 int cols = XFASTINT (w->total_cols);
976 int pixels = 0;
978 if (!w->pseudo_window_p)
980 cols -= WINDOW_SCROLL_BAR_COLS (w);
982 if (area == TEXT_AREA)
984 if (INTEGERP (w->left_margin_cols))
985 cols -= XFASTINT (w->left_margin_cols);
986 if (INTEGERP (w->right_margin_cols))
987 cols -= XFASTINT (w->right_margin_cols);
988 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
990 else if (area == LEFT_MARGIN_AREA)
992 cols = (INTEGERP (w->left_margin_cols)
993 ? XFASTINT (w->left_margin_cols) : 0);
994 pixels = 0;
996 else if (area == RIGHT_MARGIN_AREA)
998 cols = (INTEGERP (w->right_margin_cols)
999 ? XFASTINT (w->right_margin_cols) : 0);
1000 pixels = 0;
1004 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1008 /* Return the pixel height of the display area of window W, not
1009 including mode lines of W, if any. */
1012 window_box_height (struct window *w)
1014 struct frame *f = XFRAME (w->frame);
1015 int height = WINDOW_TOTAL_HEIGHT (w);
1017 xassert (height >= 0);
1019 /* Note: the code below that determines the mode-line/header-line
1020 height is essentially the same as that contained in the macro
1021 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1022 the appropriate glyph row has its `mode_line_p' flag set,
1023 and if it doesn't, uses estimate_mode_line_height instead. */
1025 if (WINDOW_WANTS_MODELINE_P (w))
1027 struct glyph_row *ml_row
1028 = (w->current_matrix && w->current_matrix->rows
1029 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1030 : 0);
1031 if (ml_row && ml_row->mode_line_p)
1032 height -= ml_row->height;
1033 else
1034 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1037 if (WINDOW_WANTS_HEADER_LINE_P (w))
1039 struct glyph_row *hl_row
1040 = (w->current_matrix && w->current_matrix->rows
1041 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1042 : 0);
1043 if (hl_row && hl_row->mode_line_p)
1044 height -= hl_row->height;
1045 else
1046 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1049 /* With a very small font and a mode-line that's taller than
1050 default, we might end up with a negative height. */
1051 return max (0, height);
1054 /* Return the window-relative coordinate of the left edge of display
1055 area AREA of window W. AREA < 0 means return the left edge of the
1056 whole window, to the right of the left fringe of W. */
1059 window_box_left_offset (struct window *w, int area)
1061 int x;
1063 if (w->pseudo_window_p)
1064 return 0;
1066 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1068 if (area == TEXT_AREA)
1069 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1070 + window_box_width (w, LEFT_MARGIN_AREA));
1071 else if (area == RIGHT_MARGIN_AREA)
1072 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1073 + window_box_width (w, LEFT_MARGIN_AREA)
1074 + window_box_width (w, TEXT_AREA)
1075 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1077 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1078 else if (area == LEFT_MARGIN_AREA
1079 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1080 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1082 return x;
1086 /* Return the window-relative coordinate of the right edge of display
1087 area AREA of window W. AREA < 0 means return the right edge of the
1088 whole window, to the left of the right fringe of W. */
1091 window_box_right_offset (struct window *w, int area)
1093 return window_box_left_offset (w, area) + window_box_width (w, area);
1096 /* Return the frame-relative coordinate of the left edge of display
1097 area AREA of window W. AREA < 0 means return the left edge of the
1098 whole window, to the right of the left fringe of W. */
1101 window_box_left (struct window *w, int area)
1103 struct frame *f = XFRAME (w->frame);
1104 int x;
1106 if (w->pseudo_window_p)
1107 return FRAME_INTERNAL_BORDER_WIDTH (f);
1109 x = (WINDOW_LEFT_EDGE_X (w)
1110 + window_box_left_offset (w, area));
1112 return x;
1116 /* Return the frame-relative coordinate of the right edge of display
1117 area AREA of window W. AREA < 0 means return the right edge of the
1118 whole window, to the left of the right fringe of W. */
1121 window_box_right (struct window *w, int area)
1123 return window_box_left (w, area) + window_box_width (w, area);
1126 /* Get the bounding box of the display area AREA of window W, without
1127 mode lines, in frame-relative coordinates. AREA < 0 means the
1128 whole window, not including the left and right fringes of
1129 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1130 coordinates of the upper-left corner of the box. Return in
1131 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1133 void
1134 window_box (struct window *w, int area, int *box_x, int *box_y,
1135 int *box_width, int *box_height)
1137 if (box_width)
1138 *box_width = window_box_width (w, area);
1139 if (box_height)
1140 *box_height = window_box_height (w);
1141 if (box_x)
1142 *box_x = window_box_left (w, area);
1143 if (box_y)
1145 *box_y = WINDOW_TOP_EDGE_Y (w);
1146 if (WINDOW_WANTS_HEADER_LINE_P (w))
1147 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1152 /* Get the bounding box of the display area AREA of window W, without
1153 mode lines. AREA < 0 means the whole window, not including the
1154 left and right fringe of the window. Return in *TOP_LEFT_X
1155 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1156 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1157 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1158 box. */
1160 static inline void
1161 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1162 int *bottom_right_x, int *bottom_right_y)
1164 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1165 bottom_right_y);
1166 *bottom_right_x += *top_left_x;
1167 *bottom_right_y += *top_left_y;
1172 /***********************************************************************
1173 Utilities
1174 ***********************************************************************/
1176 /* Return the bottom y-position of the line the iterator IT is in.
1177 This can modify IT's settings. */
1180 line_bottom_y (struct it *it)
1182 int line_height = it->max_ascent + it->max_descent;
1183 int line_top_y = it->current_y;
1185 if (line_height == 0)
1187 if (last_height)
1188 line_height = last_height;
1189 else if (IT_CHARPOS (*it) < ZV)
1191 move_it_by_lines (it, 1);
1192 line_height = (it->max_ascent || it->max_descent
1193 ? it->max_ascent + it->max_descent
1194 : last_height);
1196 else
1198 struct glyph_row *row = it->glyph_row;
1200 /* Use the default character height. */
1201 it->glyph_row = NULL;
1202 it->what = IT_CHARACTER;
1203 it->c = ' ';
1204 it->len = 1;
1205 PRODUCE_GLYPHS (it);
1206 line_height = it->ascent + it->descent;
1207 it->glyph_row = row;
1211 return line_top_y + line_height;
1214 /* Subroutine of pos_visible_p below. Extracts a display string, if
1215 any, from the display spec given as its argument. */
1216 static Lisp_Object
1217 string_from_display_spec (Lisp_Object spec)
1219 if (CONSP (spec))
1221 while (CONSP (spec))
1223 if (STRINGP (XCAR (spec)))
1224 return XCAR (spec);
1225 spec = XCDR (spec);
1228 else if (VECTORP (spec))
1230 ptrdiff_t i;
1232 for (i = 0; i < ASIZE (spec); i++)
1234 if (STRINGP (AREF (spec, i)))
1235 return AREF (spec, i);
1237 return Qnil;
1240 return spec;
1243 /* Return 1 if position CHARPOS is visible in window W.
1244 CHARPOS < 0 means return info about WINDOW_END position.
1245 If visible, set *X and *Y to pixel coordinates of top left corner.
1246 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1247 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1250 pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1251 int *rtop, int *rbot, int *rowh, int *vpos)
1253 struct it it;
1254 void *itdata = bidi_shelve_cache ();
1255 struct text_pos top;
1256 int visible_p = 0;
1257 struct buffer *old_buffer = NULL;
1259 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1260 return visible_p;
1262 if (XBUFFER (w->buffer) != current_buffer)
1264 old_buffer = current_buffer;
1265 set_buffer_internal_1 (XBUFFER (w->buffer));
1268 SET_TEXT_POS_FROM_MARKER (top, w->start);
1269 /* Scrolling a minibuffer window via scroll bar when the echo area
1270 shows long text sometimes resets the minibuffer contents behind
1271 our backs. */
1272 if (CHARPOS (top) > ZV)
1273 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1275 /* Compute exact mode line heights. */
1276 if (WINDOW_WANTS_MODELINE_P (w))
1277 current_mode_line_height
1278 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1279 BVAR (current_buffer, mode_line_format));
1281 if (WINDOW_WANTS_HEADER_LINE_P (w))
1282 current_header_line_height
1283 = display_mode_line (w, HEADER_LINE_FACE_ID,
1284 BVAR (current_buffer, header_line_format));
1286 start_display (&it, w, top);
1287 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1288 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1290 if (charpos >= 0
1291 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1292 && IT_CHARPOS (it) >= charpos)
1293 /* When scanning backwards under bidi iteration, move_it_to
1294 stops at or _before_ CHARPOS, because it stops at or to
1295 the _right_ of the character at CHARPOS. */
1296 || (it.bidi_p && it.bidi_it.scan_dir == -1
1297 && IT_CHARPOS (it) <= charpos)))
1299 /* We have reached CHARPOS, or passed it. How the call to
1300 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1301 or covered by a display property, move_it_to stops at the end
1302 of the invisible text, to the right of CHARPOS. (ii) If
1303 CHARPOS is in a display vector, move_it_to stops on its last
1304 glyph. */
1305 int top_x = it.current_x;
1306 int top_y = it.current_y;
1307 /* Calling line_bottom_y may change it.method, it.position, etc. */
1308 enum it_method it_method = it.method;
1309 int bottom_y = (last_height = 0, line_bottom_y (&it));
1310 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1312 if (top_y < window_top_y)
1313 visible_p = bottom_y > window_top_y;
1314 else if (top_y < it.last_visible_y)
1315 visible_p = 1;
1316 if (bottom_y >= it.last_visible_y
1317 && it.bidi_p && it.bidi_it.scan_dir == -1
1318 && IT_CHARPOS (it) < charpos)
1320 /* When the last line of the window is scanned backwards
1321 under bidi iteration, we could be duped into thinking
1322 that we have passed CHARPOS, when in fact move_it_to
1323 simply stopped short of CHARPOS because it reached
1324 last_visible_y. To see if that's what happened, we call
1325 move_it_to again with a slightly larger vertical limit,
1326 and see if it actually moved vertically; if it did, we
1327 didn't really reach CHARPOS, which is beyond window end. */
1328 struct it save_it = it;
1329 /* Why 10? because we don't know how many canonical lines
1330 will the height of the next line(s) be. So we guess. */
1331 int ten_more_lines =
1332 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1334 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1335 MOVE_TO_POS | MOVE_TO_Y);
1336 if (it.current_y > top_y)
1337 visible_p = 0;
1339 it = save_it;
1341 if (visible_p)
1343 if (it_method == GET_FROM_DISPLAY_VECTOR)
1345 /* We stopped on the last glyph of a display vector.
1346 Try and recompute. Hack alert! */
1347 if (charpos < 2 || top.charpos >= charpos)
1348 top_x = it.glyph_row->x;
1349 else
1351 struct it it2;
1352 start_display (&it2, w, top);
1353 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1354 get_next_display_element (&it2);
1355 PRODUCE_GLYPHS (&it2);
1356 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1357 || it2.current_x > it2.last_visible_x)
1358 top_x = it.glyph_row->x;
1359 else
1361 top_x = it2.current_x;
1362 top_y = it2.current_y;
1366 else if (IT_CHARPOS (it) != charpos)
1368 Lisp_Object cpos = make_number (charpos);
1369 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1370 Lisp_Object string = string_from_display_spec (spec);
1371 int newline_in_string = 0;
1373 if (STRINGP (string))
1375 const char *s = SSDATA (string);
1376 const char *e = s + SBYTES (string);
1377 while (s < e)
1379 if (*s++ == '\n')
1381 newline_in_string = 1;
1382 break;
1386 /* The tricky code below is needed because there's a
1387 discrepancy between move_it_to and how we set cursor
1388 when the display line ends in a newline from a
1389 display string. move_it_to will stop _after_ such
1390 display strings, whereas set_cursor_from_row
1391 conspires with cursor_row_p to place the cursor on
1392 the first glyph produced from the display string. */
1394 /* We have overshoot PT because it is covered by a
1395 display property whose value is a string. If the
1396 string includes embedded newlines, we are also in the
1397 wrong display line. Backtrack to the correct line,
1398 where the display string begins. */
1399 if (newline_in_string)
1401 Lisp_Object startpos, endpos;
1402 EMACS_INT start, end;
1403 struct it it3;
1404 int it3_moved;
1406 /* Find the first and the last buffer positions
1407 covered by the display string. */
1408 endpos =
1409 Fnext_single_char_property_change (cpos, Qdisplay,
1410 Qnil, Qnil);
1411 startpos =
1412 Fprevious_single_char_property_change (endpos, Qdisplay,
1413 Qnil, Qnil);
1414 start = XFASTINT (startpos);
1415 end = XFASTINT (endpos);
1416 /* Move to the last buffer position before the
1417 display property. */
1418 start_display (&it3, w, top);
1419 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1420 /* Move forward one more line if the position before
1421 the display string is a newline or if it is the
1422 rightmost character on a line that is
1423 continued or word-wrapped. */
1424 if (it3.method == GET_FROM_BUFFER
1425 && it3.c == '\n')
1426 move_it_by_lines (&it3, 1);
1427 else if (move_it_in_display_line_to (&it3, -1,
1428 it3.current_x
1429 + it3.pixel_width,
1430 MOVE_TO_X)
1431 == MOVE_LINE_CONTINUED)
1433 move_it_by_lines (&it3, 1);
1434 /* When we are under word-wrap, the #$@%!
1435 move_it_by_lines moves 2 lines, so we need to
1436 fix that up. */
1437 if (it3.line_wrap == WORD_WRAP)
1438 move_it_by_lines (&it3, -1);
1441 /* Record the vertical coordinate of the display
1442 line where we wound up. */
1443 top_y = it3.current_y;
1444 if (it3.bidi_p)
1446 /* When characters are reordered for display,
1447 the character displayed to the left of the
1448 display string could be _after_ the display
1449 property in the logical order. Use the
1450 smallest vertical position of these two. */
1451 start_display (&it3, w, top);
1452 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1453 if (it3.current_y < top_y)
1454 top_y = it3.current_y;
1456 /* Move from the top of the window to the beginning
1457 of the display line where the display string
1458 begins. */
1459 start_display (&it3, w, top);
1460 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1461 /* If it3_moved stays zero after the 'while' loop
1462 below, that means we already were at a newline
1463 before the loop (e.g., the display string begins
1464 with a newline), so we don't need to (and cannot)
1465 inspect the glyphs of it3.glyph_row, because
1466 PRODUCE_GLYPHS will not produce anything for a
1467 newline, and thus it3.glyph_row stays at its
1468 stale content it got at top of the window. */
1469 it3_moved = 0;
1470 /* Finally, advance the iterator until we hit the
1471 first display element whose character position is
1472 CHARPOS, or until the first newline from the
1473 display string, which signals the end of the
1474 display line. */
1475 while (get_next_display_element (&it3))
1477 PRODUCE_GLYPHS (&it3);
1478 if (IT_CHARPOS (it3) == charpos
1479 || ITERATOR_AT_END_OF_LINE_P (&it3))
1480 break;
1481 it3_moved = 1;
1482 set_iterator_to_next (&it3, 0);
1484 top_x = it3.current_x - it3.pixel_width;
1485 /* Normally, we would exit the above loop because we
1486 found the display element whose character
1487 position is CHARPOS. For the contingency that we
1488 didn't, and stopped at the first newline from the
1489 display string, move back over the glyphs
1490 produced from the string, until we find the
1491 rightmost glyph not from the string. */
1492 if (it3_moved
1493 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1495 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1496 + it3.glyph_row->used[TEXT_AREA];
1498 while (EQ ((g - 1)->object, string))
1500 --g;
1501 top_x -= g->pixel_width;
1503 xassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1504 + it3.glyph_row->used[TEXT_AREA]);
1509 *x = top_x;
1510 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1511 *rtop = max (0, window_top_y - top_y);
1512 *rbot = max (0, bottom_y - it.last_visible_y);
1513 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1514 - max (top_y, window_top_y)));
1515 *vpos = it.vpos;
1518 else
1520 /* We were asked to provide info about WINDOW_END. */
1521 struct it it2;
1522 void *it2data = NULL;
1524 SAVE_IT (it2, it, it2data);
1525 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1526 move_it_by_lines (&it, 1);
1527 if (charpos < IT_CHARPOS (it)
1528 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1530 visible_p = 1;
1531 RESTORE_IT (&it2, &it2, it2data);
1532 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1533 *x = it2.current_x;
1534 *y = it2.current_y + it2.max_ascent - it2.ascent;
1535 *rtop = max (0, -it2.current_y);
1536 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1537 - it.last_visible_y));
1538 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1539 it.last_visible_y)
1540 - max (it2.current_y,
1541 WINDOW_HEADER_LINE_HEIGHT (w))));
1542 *vpos = it2.vpos;
1544 else
1545 bidi_unshelve_cache (it2data, 1);
1547 bidi_unshelve_cache (itdata, 0);
1549 if (old_buffer)
1550 set_buffer_internal_1 (old_buffer);
1552 current_header_line_height = current_mode_line_height = -1;
1554 if (visible_p && XFASTINT (w->hscroll) > 0)
1555 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1557 #if 0
1558 /* Debugging code. */
1559 if (visible_p)
1560 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1561 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1562 else
1563 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1564 #endif
1566 return visible_p;
1570 /* Return the next character from STR. Return in *LEN the length of
1571 the character. This is like STRING_CHAR_AND_LENGTH but never
1572 returns an invalid character. If we find one, we return a `?', but
1573 with the length of the invalid character. */
1575 static inline int
1576 string_char_and_length (const unsigned char *str, int *len)
1578 int c;
1580 c = STRING_CHAR_AND_LENGTH (str, *len);
1581 if (!CHAR_VALID_P (c))
1582 /* We may not change the length here because other places in Emacs
1583 don't use this function, i.e. they silently accept invalid
1584 characters. */
1585 c = '?';
1587 return c;
1592 /* Given a position POS containing a valid character and byte position
1593 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1595 static struct text_pos
1596 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1598 xassert (STRINGP (string) && nchars >= 0);
1600 if (STRING_MULTIBYTE (string))
1602 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1603 int len;
1605 while (nchars--)
1607 string_char_and_length (p, &len);
1608 p += len;
1609 CHARPOS (pos) += 1;
1610 BYTEPOS (pos) += len;
1613 else
1614 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1616 return pos;
1620 /* Value is the text position, i.e. character and byte position,
1621 for character position CHARPOS in STRING. */
1623 static inline struct text_pos
1624 string_pos (EMACS_INT charpos, Lisp_Object string)
1626 struct text_pos pos;
1627 xassert (STRINGP (string));
1628 xassert (charpos >= 0);
1629 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1630 return pos;
1634 /* Value is a text position, i.e. character and byte position, for
1635 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1636 means recognize multibyte characters. */
1638 static struct text_pos
1639 c_string_pos (EMACS_INT charpos, const char *s, int multibyte_p)
1641 struct text_pos pos;
1643 xassert (s != NULL);
1644 xassert (charpos >= 0);
1646 if (multibyte_p)
1648 int len;
1650 SET_TEXT_POS (pos, 0, 0);
1651 while (charpos--)
1653 string_char_and_length ((const unsigned char *) s, &len);
1654 s += len;
1655 CHARPOS (pos) += 1;
1656 BYTEPOS (pos) += len;
1659 else
1660 SET_TEXT_POS (pos, charpos, charpos);
1662 return pos;
1666 /* Value is the number of characters in C string S. MULTIBYTE_P
1667 non-zero means recognize multibyte characters. */
1669 static EMACS_INT
1670 number_of_chars (const char *s, int multibyte_p)
1672 EMACS_INT nchars;
1674 if (multibyte_p)
1676 EMACS_INT rest = strlen (s);
1677 int len;
1678 const unsigned char *p = (const unsigned char *) s;
1680 for (nchars = 0; rest > 0; ++nchars)
1682 string_char_and_length (p, &len);
1683 rest -= len, p += len;
1686 else
1687 nchars = strlen (s);
1689 return nchars;
1693 /* Compute byte position NEWPOS->bytepos corresponding to
1694 NEWPOS->charpos. POS is a known position in string STRING.
1695 NEWPOS->charpos must be >= POS.charpos. */
1697 static void
1698 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1700 xassert (STRINGP (string));
1701 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1703 if (STRING_MULTIBYTE (string))
1704 *newpos = string_pos_nchars_ahead (pos, string,
1705 CHARPOS (*newpos) - CHARPOS (pos));
1706 else
1707 BYTEPOS (*newpos) = CHARPOS (*newpos);
1710 /* EXPORT:
1711 Return an estimation of the pixel height of mode or header lines on
1712 frame F. FACE_ID specifies what line's height to estimate. */
1715 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1717 #ifdef HAVE_WINDOW_SYSTEM
1718 if (FRAME_WINDOW_P (f))
1720 int height = FONT_HEIGHT (FRAME_FONT (f));
1722 /* This function is called so early when Emacs starts that the face
1723 cache and mode line face are not yet initialized. */
1724 if (FRAME_FACE_CACHE (f))
1726 struct face *face = FACE_FROM_ID (f, face_id);
1727 if (face)
1729 if (face->font)
1730 height = FONT_HEIGHT (face->font);
1731 if (face->box_line_width > 0)
1732 height += 2 * face->box_line_width;
1736 return height;
1738 #endif
1740 return 1;
1743 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1744 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1745 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1746 not force the value into range. */
1748 void
1749 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1750 int *x, int *y, NativeRectangle *bounds, int noclip)
1753 #ifdef HAVE_WINDOW_SYSTEM
1754 if (FRAME_WINDOW_P (f))
1756 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1757 even for negative values. */
1758 if (pix_x < 0)
1759 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1760 if (pix_y < 0)
1761 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1763 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1764 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1766 if (bounds)
1767 STORE_NATIVE_RECT (*bounds,
1768 FRAME_COL_TO_PIXEL_X (f, pix_x),
1769 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1770 FRAME_COLUMN_WIDTH (f) - 1,
1771 FRAME_LINE_HEIGHT (f) - 1);
1773 if (!noclip)
1775 if (pix_x < 0)
1776 pix_x = 0;
1777 else if (pix_x > FRAME_TOTAL_COLS (f))
1778 pix_x = FRAME_TOTAL_COLS (f);
1780 if (pix_y < 0)
1781 pix_y = 0;
1782 else if (pix_y > FRAME_LINES (f))
1783 pix_y = FRAME_LINES (f);
1786 #endif
1788 *x = pix_x;
1789 *y = pix_y;
1793 /* Find the glyph under window-relative coordinates X/Y in window W.
1794 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1795 strings. Return in *HPOS and *VPOS the row and column number of
1796 the glyph found. Return in *AREA the glyph area containing X.
1797 Value is a pointer to the glyph found or null if X/Y is not on
1798 text, or we can't tell because W's current matrix is not up to
1799 date. */
1801 static
1802 struct glyph *
1803 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1804 int *dx, int *dy, int *area)
1806 struct glyph *glyph, *end;
1807 struct glyph_row *row = NULL;
1808 int x0, i;
1810 /* Find row containing Y. Give up if some row is not enabled. */
1811 for (i = 0; i < w->current_matrix->nrows; ++i)
1813 row = MATRIX_ROW (w->current_matrix, i);
1814 if (!row->enabled_p)
1815 return NULL;
1816 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1817 break;
1820 *vpos = i;
1821 *hpos = 0;
1823 /* Give up if Y is not in the window. */
1824 if (i == w->current_matrix->nrows)
1825 return NULL;
1827 /* Get the glyph area containing X. */
1828 if (w->pseudo_window_p)
1830 *area = TEXT_AREA;
1831 x0 = 0;
1833 else
1835 if (x < window_box_left_offset (w, TEXT_AREA))
1837 *area = LEFT_MARGIN_AREA;
1838 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1840 else if (x < window_box_right_offset (w, TEXT_AREA))
1842 *area = TEXT_AREA;
1843 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1845 else
1847 *area = RIGHT_MARGIN_AREA;
1848 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1852 /* Find glyph containing X. */
1853 glyph = row->glyphs[*area];
1854 end = glyph + row->used[*area];
1855 x -= x0;
1856 while (glyph < end && x >= glyph->pixel_width)
1858 x -= glyph->pixel_width;
1859 ++glyph;
1862 if (glyph == end)
1863 return NULL;
1865 if (dx)
1867 *dx = x;
1868 *dy = y - (row->y + row->ascent - glyph->ascent);
1871 *hpos = glyph - row->glyphs[*area];
1872 return glyph;
1875 /* Convert frame-relative x/y to coordinates relative to window W.
1876 Takes pseudo-windows into account. */
1878 static void
1879 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1881 if (w->pseudo_window_p)
1883 /* A pseudo-window is always full-width, and starts at the
1884 left edge of the frame, plus a frame border. */
1885 struct frame *f = XFRAME (w->frame);
1886 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1887 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1889 else
1891 *x -= WINDOW_LEFT_EDGE_X (w);
1892 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1896 #ifdef HAVE_WINDOW_SYSTEM
1898 /* EXPORT:
1899 Return in RECTS[] at most N clipping rectangles for glyph string S.
1900 Return the number of stored rectangles. */
1903 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1905 XRectangle r;
1907 if (n <= 0)
1908 return 0;
1910 if (s->row->full_width_p)
1912 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1913 r.x = WINDOW_LEFT_EDGE_X (s->w);
1914 r.width = WINDOW_TOTAL_WIDTH (s->w);
1916 /* Unless displaying a mode or menu bar line, which are always
1917 fully visible, clip to the visible part of the row. */
1918 if (s->w->pseudo_window_p)
1919 r.height = s->row->visible_height;
1920 else
1921 r.height = s->height;
1923 else
1925 /* This is a text line that may be partially visible. */
1926 r.x = window_box_left (s->w, s->area);
1927 r.width = window_box_width (s->w, s->area);
1928 r.height = s->row->visible_height;
1931 if (s->clip_head)
1932 if (r.x < s->clip_head->x)
1934 if (r.width >= s->clip_head->x - r.x)
1935 r.width -= s->clip_head->x - r.x;
1936 else
1937 r.width = 0;
1938 r.x = s->clip_head->x;
1940 if (s->clip_tail)
1941 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1943 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1944 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1945 else
1946 r.width = 0;
1949 /* If S draws overlapping rows, it's sufficient to use the top and
1950 bottom of the window for clipping because this glyph string
1951 intentionally draws over other lines. */
1952 if (s->for_overlaps)
1954 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1955 r.height = window_text_bottom_y (s->w) - r.y;
1957 /* Alas, the above simple strategy does not work for the
1958 environments with anti-aliased text: if the same text is
1959 drawn onto the same place multiple times, it gets thicker.
1960 If the overlap we are processing is for the erased cursor, we
1961 take the intersection with the rectangle of the cursor. */
1962 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1964 XRectangle rc, r_save = r;
1966 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1967 rc.y = s->w->phys_cursor.y;
1968 rc.width = s->w->phys_cursor_width;
1969 rc.height = s->w->phys_cursor_height;
1971 x_intersect_rectangles (&r_save, &rc, &r);
1974 else
1976 /* Don't use S->y for clipping because it doesn't take partially
1977 visible lines into account. For example, it can be negative for
1978 partially visible lines at the top of a window. */
1979 if (!s->row->full_width_p
1980 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1981 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1982 else
1983 r.y = max (0, s->row->y);
1986 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1988 /* If drawing the cursor, don't let glyph draw outside its
1989 advertised boundaries. Cleartype does this under some circumstances. */
1990 if (s->hl == DRAW_CURSOR)
1992 struct glyph *glyph = s->first_glyph;
1993 int height, max_y;
1995 if (s->x > r.x)
1997 r.width -= s->x - r.x;
1998 r.x = s->x;
2000 r.width = min (r.width, glyph->pixel_width);
2002 /* If r.y is below window bottom, ensure that we still see a cursor. */
2003 height = min (glyph->ascent + glyph->descent,
2004 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2005 max_y = window_text_bottom_y (s->w) - height;
2006 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2007 if (s->ybase - glyph->ascent > max_y)
2009 r.y = max_y;
2010 r.height = height;
2012 else
2014 /* Don't draw cursor glyph taller than our actual glyph. */
2015 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2016 if (height < r.height)
2018 max_y = r.y + r.height;
2019 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2020 r.height = min (max_y - r.y, height);
2025 if (s->row->clip)
2027 XRectangle r_save = r;
2029 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2030 r.width = 0;
2033 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2034 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2036 #ifdef CONVERT_FROM_XRECT
2037 CONVERT_FROM_XRECT (r, *rects);
2038 #else
2039 *rects = r;
2040 #endif
2041 return 1;
2043 else
2045 /* If we are processing overlapping and allowed to return
2046 multiple clipping rectangles, we exclude the row of the glyph
2047 string from the clipping rectangle. This is to avoid drawing
2048 the same text on the environment with anti-aliasing. */
2049 #ifdef CONVERT_FROM_XRECT
2050 XRectangle rs[2];
2051 #else
2052 XRectangle *rs = rects;
2053 #endif
2054 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2056 if (s->for_overlaps & OVERLAPS_PRED)
2058 rs[i] = r;
2059 if (r.y + r.height > row_y)
2061 if (r.y < row_y)
2062 rs[i].height = row_y - r.y;
2063 else
2064 rs[i].height = 0;
2066 i++;
2068 if (s->for_overlaps & OVERLAPS_SUCC)
2070 rs[i] = r;
2071 if (r.y < row_y + s->row->visible_height)
2073 if (r.y + r.height > row_y + s->row->visible_height)
2075 rs[i].y = row_y + s->row->visible_height;
2076 rs[i].height = r.y + r.height - rs[i].y;
2078 else
2079 rs[i].height = 0;
2081 i++;
2084 n = i;
2085 #ifdef CONVERT_FROM_XRECT
2086 for (i = 0; i < n; i++)
2087 CONVERT_FROM_XRECT (rs[i], rects[i]);
2088 #endif
2089 return n;
2093 /* EXPORT:
2094 Return in *NR the clipping rectangle for glyph string S. */
2096 void
2097 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2099 get_glyph_string_clip_rects (s, nr, 1);
2103 /* EXPORT:
2104 Return the position and height of the phys cursor in window W.
2105 Set w->phys_cursor_width to width of phys cursor.
2108 void
2109 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2110 struct glyph *glyph, int *xp, int *yp, int *heightp)
2112 struct frame *f = XFRAME (WINDOW_FRAME (w));
2113 int x, y, wd, h, h0, y0;
2115 /* Compute the width of the rectangle to draw. If on a stretch
2116 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2117 rectangle as wide as the glyph, but use a canonical character
2118 width instead. */
2119 wd = glyph->pixel_width - 1;
2120 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2121 wd++; /* Why? */
2122 #endif
2124 x = w->phys_cursor.x;
2125 if (x < 0)
2127 wd += x;
2128 x = 0;
2131 if (glyph->type == STRETCH_GLYPH
2132 && !x_stretch_cursor_p)
2133 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2134 w->phys_cursor_width = wd;
2136 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2138 /* If y is below window bottom, ensure that we still see a cursor. */
2139 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2141 h = max (h0, glyph->ascent + glyph->descent);
2142 h0 = min (h0, glyph->ascent + glyph->descent);
2144 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2145 if (y < y0)
2147 h = max (h - (y0 - y) + 1, h0);
2148 y = y0 - 1;
2150 else
2152 y0 = window_text_bottom_y (w) - h0;
2153 if (y > y0)
2155 h += y - y0;
2156 y = y0;
2160 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2161 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2162 *heightp = h;
2166 * Remember which glyph the mouse is over.
2169 void
2170 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2172 Lisp_Object window;
2173 struct window *w;
2174 struct glyph_row *r, *gr, *end_row;
2175 enum window_part part;
2176 enum glyph_row_area area;
2177 int x, y, width, height;
2179 /* Try to determine frame pixel position and size of the glyph under
2180 frame pixel coordinates X/Y on frame F. */
2182 if (!f->glyphs_initialized_p
2183 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2184 NILP (window)))
2186 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2187 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2188 goto virtual_glyph;
2191 w = XWINDOW (window);
2192 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2193 height = WINDOW_FRAME_LINE_HEIGHT (w);
2195 x = window_relative_x_coord (w, part, gx);
2196 y = gy - WINDOW_TOP_EDGE_Y (w);
2198 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2199 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2201 if (w->pseudo_window_p)
2203 area = TEXT_AREA;
2204 part = ON_MODE_LINE; /* Don't adjust margin. */
2205 goto text_glyph;
2208 switch (part)
2210 case ON_LEFT_MARGIN:
2211 area = LEFT_MARGIN_AREA;
2212 goto text_glyph;
2214 case ON_RIGHT_MARGIN:
2215 area = RIGHT_MARGIN_AREA;
2216 goto text_glyph;
2218 case ON_HEADER_LINE:
2219 case ON_MODE_LINE:
2220 gr = (part == ON_HEADER_LINE
2221 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2222 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2223 gy = gr->y;
2224 area = TEXT_AREA;
2225 goto text_glyph_row_found;
2227 case ON_TEXT:
2228 area = TEXT_AREA;
2230 text_glyph:
2231 gr = 0; gy = 0;
2232 for (; r <= end_row && r->enabled_p; ++r)
2233 if (r->y + r->height > y)
2235 gr = r; gy = r->y;
2236 break;
2239 text_glyph_row_found:
2240 if (gr && gy <= y)
2242 struct glyph *g = gr->glyphs[area];
2243 struct glyph *end = g + gr->used[area];
2245 height = gr->height;
2246 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2247 if (gx + g->pixel_width > x)
2248 break;
2250 if (g < end)
2252 if (g->type == IMAGE_GLYPH)
2254 /* Don't remember when mouse is over image, as
2255 image may have hot-spots. */
2256 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2257 return;
2259 width = g->pixel_width;
2261 else
2263 /* Use nominal char spacing at end of line. */
2264 x -= gx;
2265 gx += (x / width) * width;
2268 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2269 gx += window_box_left_offset (w, area);
2271 else
2273 /* Use nominal line height at end of window. */
2274 gx = (x / width) * width;
2275 y -= gy;
2276 gy += (y / height) * height;
2278 break;
2280 case ON_LEFT_FRINGE:
2281 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2282 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2283 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2284 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2285 goto row_glyph;
2287 case ON_RIGHT_FRINGE:
2288 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2289 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2290 : window_box_right_offset (w, TEXT_AREA));
2291 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2292 goto row_glyph;
2294 case ON_SCROLL_BAR:
2295 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2297 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2298 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2299 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2300 : 0)));
2301 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2303 row_glyph:
2304 gr = 0, gy = 0;
2305 for (; r <= end_row && r->enabled_p; ++r)
2306 if (r->y + r->height > y)
2308 gr = r; gy = r->y;
2309 break;
2312 if (gr && gy <= y)
2313 height = gr->height;
2314 else
2316 /* Use nominal line height at end of window. */
2317 y -= gy;
2318 gy += (y / height) * height;
2320 break;
2322 default:
2324 virtual_glyph:
2325 /* If there is no glyph under the mouse, then we divide the screen
2326 into a grid of the smallest glyph in the frame, and use that
2327 as our "glyph". */
2329 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2330 round down even for negative values. */
2331 if (gx < 0)
2332 gx -= width - 1;
2333 if (gy < 0)
2334 gy -= height - 1;
2336 gx = (gx / width) * width;
2337 gy = (gy / height) * height;
2339 goto store_rect;
2342 gx += WINDOW_LEFT_EDGE_X (w);
2343 gy += WINDOW_TOP_EDGE_Y (w);
2345 store_rect:
2346 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2348 /* Visible feedback for debugging. */
2349 #if 0
2350 #if HAVE_X_WINDOWS
2351 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2352 f->output_data.x->normal_gc,
2353 gx, gy, width, height);
2354 #endif
2355 #endif
2359 #endif /* HAVE_WINDOW_SYSTEM */
2362 /***********************************************************************
2363 Lisp form evaluation
2364 ***********************************************************************/
2366 /* Error handler for safe_eval and safe_call. */
2368 static Lisp_Object
2369 safe_eval_handler (Lisp_Object arg)
2371 add_to_log ("Error during redisplay: %S", arg, Qnil);
2372 return Qnil;
2376 /* Evaluate SEXPR and return the result, or nil if something went
2377 wrong. Prevent redisplay during the evaluation. */
2379 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2380 Return the result, or nil if something went wrong. Prevent
2381 redisplay during the evaluation. */
2383 Lisp_Object
2384 safe_call (ptrdiff_t nargs, Lisp_Object *args)
2386 Lisp_Object val;
2388 if (inhibit_eval_during_redisplay)
2389 val = Qnil;
2390 else
2392 int count = SPECPDL_INDEX ();
2393 struct gcpro gcpro1;
2395 GCPRO1 (args[0]);
2396 gcpro1.nvars = nargs;
2397 specbind (Qinhibit_redisplay, Qt);
2398 /* Use Qt to ensure debugger does not run,
2399 so there is no possibility of wanting to redisplay. */
2400 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2401 safe_eval_handler);
2402 UNGCPRO;
2403 val = unbind_to (count, val);
2406 return val;
2410 /* Call function FN with one argument ARG.
2411 Return the result, or nil if something went wrong. */
2413 Lisp_Object
2414 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2416 Lisp_Object args[2];
2417 args[0] = fn;
2418 args[1] = arg;
2419 return safe_call (2, args);
2422 static Lisp_Object Qeval;
2424 Lisp_Object
2425 safe_eval (Lisp_Object sexpr)
2427 return safe_call1 (Qeval, sexpr);
2430 /* Call function FN with one argument ARG.
2431 Return the result, or nil if something went wrong. */
2433 Lisp_Object
2434 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2436 Lisp_Object args[3];
2437 args[0] = fn;
2438 args[1] = arg1;
2439 args[2] = arg2;
2440 return safe_call (3, args);
2445 /***********************************************************************
2446 Debugging
2447 ***********************************************************************/
2449 #if 0
2451 /* Define CHECK_IT to perform sanity checks on iterators.
2452 This is for debugging. It is too slow to do unconditionally. */
2454 static void
2455 check_it (struct it *it)
2457 if (it->method == GET_FROM_STRING)
2459 xassert (STRINGP (it->string));
2460 xassert (IT_STRING_CHARPOS (*it) >= 0);
2462 else
2464 xassert (IT_STRING_CHARPOS (*it) < 0);
2465 if (it->method == GET_FROM_BUFFER)
2467 /* Check that character and byte positions agree. */
2468 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2472 if (it->dpvec)
2473 xassert (it->current.dpvec_index >= 0);
2474 else
2475 xassert (it->current.dpvec_index < 0);
2478 #define CHECK_IT(IT) check_it ((IT))
2480 #else /* not 0 */
2482 #define CHECK_IT(IT) (void) 0
2484 #endif /* not 0 */
2487 #if GLYPH_DEBUG && XASSERTS
2489 /* Check that the window end of window W is what we expect it
2490 to be---the last row in the current matrix displaying text. */
2492 static void
2493 check_window_end (struct window *w)
2495 if (!MINI_WINDOW_P (w)
2496 && !NILP (w->window_end_valid))
2498 struct glyph_row *row;
2499 xassert ((row = MATRIX_ROW (w->current_matrix,
2500 XFASTINT (w->window_end_vpos)),
2501 !row->enabled_p
2502 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2503 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2507 #define CHECK_WINDOW_END(W) check_window_end ((W))
2509 #else
2511 #define CHECK_WINDOW_END(W) (void) 0
2513 #endif
2517 /***********************************************************************
2518 Iterator initialization
2519 ***********************************************************************/
2521 /* Initialize IT for displaying current_buffer in window W, starting
2522 at character position CHARPOS. CHARPOS < 0 means that no buffer
2523 position is specified which is useful when the iterator is assigned
2524 a position later. BYTEPOS is the byte position corresponding to
2525 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2527 If ROW is not null, calls to produce_glyphs with IT as parameter
2528 will produce glyphs in that row.
2530 BASE_FACE_ID is the id of a base face to use. It must be one of
2531 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2532 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2533 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2535 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2536 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2537 will be initialized to use the corresponding mode line glyph row of
2538 the desired matrix of W. */
2540 void
2541 init_iterator (struct it *it, struct window *w,
2542 EMACS_INT charpos, EMACS_INT bytepos,
2543 struct glyph_row *row, enum face_id base_face_id)
2545 int highlight_region_p;
2546 enum face_id remapped_base_face_id = base_face_id;
2548 /* Some precondition checks. */
2549 xassert (w != NULL && it != NULL);
2550 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2551 && charpos <= ZV));
2553 /* If face attributes have been changed since the last redisplay,
2554 free realized faces now because they depend on face definitions
2555 that might have changed. Don't free faces while there might be
2556 desired matrices pending which reference these faces. */
2557 if (face_change_count && !inhibit_free_realized_faces)
2559 face_change_count = 0;
2560 free_all_realized_faces (Qnil);
2563 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2564 if (! NILP (Vface_remapping_alist))
2565 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2567 /* Use one of the mode line rows of W's desired matrix if
2568 appropriate. */
2569 if (row == NULL)
2571 if (base_face_id == MODE_LINE_FACE_ID
2572 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2573 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2574 else if (base_face_id == HEADER_LINE_FACE_ID)
2575 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2578 /* Clear IT. */
2579 memset (it, 0, sizeof *it);
2580 it->current.overlay_string_index = -1;
2581 it->current.dpvec_index = -1;
2582 it->base_face_id = remapped_base_face_id;
2583 it->string = Qnil;
2584 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2585 it->paragraph_embedding = L2R;
2586 it->bidi_it.string.lstring = Qnil;
2587 it->bidi_it.string.s = NULL;
2588 it->bidi_it.string.bufpos = 0;
2590 /* The window in which we iterate over current_buffer: */
2591 XSETWINDOW (it->window, w);
2592 it->w = w;
2593 it->f = XFRAME (w->frame);
2595 it->cmp_it.id = -1;
2597 /* Extra space between lines (on window systems only). */
2598 if (base_face_id == DEFAULT_FACE_ID
2599 && FRAME_WINDOW_P (it->f))
2601 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2602 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2603 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2604 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2605 * FRAME_LINE_HEIGHT (it->f));
2606 else if (it->f->extra_line_spacing > 0)
2607 it->extra_line_spacing = it->f->extra_line_spacing;
2608 it->max_extra_line_spacing = 0;
2611 /* If realized faces have been removed, e.g. because of face
2612 attribute changes of named faces, recompute them. When running
2613 in batch mode, the face cache of the initial frame is null. If
2614 we happen to get called, make a dummy face cache. */
2615 if (FRAME_FACE_CACHE (it->f) == NULL)
2616 init_frame_faces (it->f);
2617 if (FRAME_FACE_CACHE (it->f)->used == 0)
2618 recompute_basic_faces (it->f);
2620 /* Current value of the `slice', `space-width', and 'height' properties. */
2621 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2622 it->space_width = Qnil;
2623 it->font_height = Qnil;
2624 it->override_ascent = -1;
2626 /* Are control characters displayed as `^C'? */
2627 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2629 /* -1 means everything between a CR and the following line end
2630 is invisible. >0 means lines indented more than this value are
2631 invisible. */
2632 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2633 ? XINT (BVAR (current_buffer, selective_display))
2634 : (!NILP (BVAR (current_buffer, selective_display))
2635 ? -1 : 0));
2636 it->selective_display_ellipsis_p
2637 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2639 /* Display table to use. */
2640 it->dp = window_display_table (w);
2642 /* Are multibyte characters enabled in current_buffer? */
2643 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2645 /* Non-zero if we should highlight the region. */
2646 highlight_region_p
2647 = (!NILP (Vtransient_mark_mode)
2648 && !NILP (BVAR (current_buffer, mark_active))
2649 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2651 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2652 start and end of a visible region in window IT->w. Set both to
2653 -1 to indicate no region. */
2654 if (highlight_region_p
2655 /* Maybe highlight only in selected window. */
2656 && (/* Either show region everywhere. */
2657 highlight_nonselected_windows
2658 /* Or show region in the selected window. */
2659 || w == XWINDOW (selected_window)
2660 /* Or show the region if we are in the mini-buffer and W is
2661 the window the mini-buffer refers to. */
2662 || (MINI_WINDOW_P (XWINDOW (selected_window))
2663 && WINDOWP (minibuf_selected_window)
2664 && w == XWINDOW (minibuf_selected_window))))
2666 EMACS_INT markpos = marker_position (BVAR (current_buffer, mark));
2667 it->region_beg_charpos = min (PT, markpos);
2668 it->region_end_charpos = max (PT, markpos);
2670 else
2671 it->region_beg_charpos = it->region_end_charpos = -1;
2673 /* Get the position at which the redisplay_end_trigger hook should
2674 be run, if it is to be run at all. */
2675 if (MARKERP (w->redisplay_end_trigger)
2676 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2677 it->redisplay_end_trigger_charpos
2678 = marker_position (w->redisplay_end_trigger);
2679 else if (INTEGERP (w->redisplay_end_trigger))
2680 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2682 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2684 /* Are lines in the display truncated? */
2685 if (base_face_id != DEFAULT_FACE_ID
2686 || XINT (it->w->hscroll)
2687 || (! WINDOW_FULL_WIDTH_P (it->w)
2688 && ((!NILP (Vtruncate_partial_width_windows)
2689 && !INTEGERP (Vtruncate_partial_width_windows))
2690 || (INTEGERP (Vtruncate_partial_width_windows)
2691 && (WINDOW_TOTAL_COLS (it->w)
2692 < XINT (Vtruncate_partial_width_windows))))))
2693 it->line_wrap = TRUNCATE;
2694 else if (NILP (BVAR (current_buffer, truncate_lines)))
2695 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2696 ? WINDOW_WRAP : WORD_WRAP;
2697 else
2698 it->line_wrap = TRUNCATE;
2700 /* Get dimensions of truncation and continuation glyphs. These are
2701 displayed as fringe bitmaps under X, so we don't need them for such
2702 frames. */
2703 if (!FRAME_WINDOW_P (it->f))
2705 if (it->line_wrap == TRUNCATE)
2707 /* We will need the truncation glyph. */
2708 xassert (it->glyph_row == NULL);
2709 produce_special_glyphs (it, IT_TRUNCATION);
2710 it->truncation_pixel_width = it->pixel_width;
2712 else
2714 /* We will need the continuation glyph. */
2715 xassert (it->glyph_row == NULL);
2716 produce_special_glyphs (it, IT_CONTINUATION);
2717 it->continuation_pixel_width = it->pixel_width;
2720 /* Reset these values to zero because the produce_special_glyphs
2721 above has changed them. */
2722 it->pixel_width = it->ascent = it->descent = 0;
2723 it->phys_ascent = it->phys_descent = 0;
2726 /* Set this after getting the dimensions of truncation and
2727 continuation glyphs, so that we don't produce glyphs when calling
2728 produce_special_glyphs, above. */
2729 it->glyph_row = row;
2730 it->area = TEXT_AREA;
2732 /* Forget any previous info about this row being reversed. */
2733 if (it->glyph_row)
2734 it->glyph_row->reversed_p = 0;
2736 /* Get the dimensions of the display area. The display area
2737 consists of the visible window area plus a horizontally scrolled
2738 part to the left of the window. All x-values are relative to the
2739 start of this total display area. */
2740 if (base_face_id != DEFAULT_FACE_ID)
2742 /* Mode lines, menu bar in terminal frames. */
2743 it->first_visible_x = 0;
2744 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2746 else
2748 it->first_visible_x
2749 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2750 it->last_visible_x = (it->first_visible_x
2751 + window_box_width (w, TEXT_AREA));
2753 /* If we truncate lines, leave room for the truncator glyph(s) at
2754 the right margin. Otherwise, leave room for the continuation
2755 glyph(s). Truncation and continuation glyphs are not inserted
2756 for window-based redisplay. */
2757 if (!FRAME_WINDOW_P (it->f))
2759 if (it->line_wrap == TRUNCATE)
2760 it->last_visible_x -= it->truncation_pixel_width;
2761 else
2762 it->last_visible_x -= it->continuation_pixel_width;
2765 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2766 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2769 /* Leave room for a border glyph. */
2770 if (!FRAME_WINDOW_P (it->f)
2771 && !WINDOW_RIGHTMOST_P (it->w))
2772 it->last_visible_x -= 1;
2774 it->last_visible_y = window_text_bottom_y (w);
2776 /* For mode lines and alike, arrange for the first glyph having a
2777 left box line if the face specifies a box. */
2778 if (base_face_id != DEFAULT_FACE_ID)
2780 struct face *face;
2782 it->face_id = remapped_base_face_id;
2784 /* If we have a boxed mode line, make the first character appear
2785 with a left box line. */
2786 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2787 if (face->box != FACE_NO_BOX)
2788 it->start_of_box_run_p = 1;
2791 /* If a buffer position was specified, set the iterator there,
2792 getting overlays and face properties from that position. */
2793 if (charpos >= BUF_BEG (current_buffer))
2795 it->end_charpos = ZV;
2796 IT_CHARPOS (*it) = charpos;
2798 /* We will rely on `reseat' to set this up properly, via
2799 handle_face_prop. */
2800 it->face_id = it->base_face_id;
2802 /* Compute byte position if not specified. */
2803 if (bytepos < charpos)
2804 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2805 else
2806 IT_BYTEPOS (*it) = bytepos;
2808 it->start = it->current;
2809 /* Do we need to reorder bidirectional text? Not if this is a
2810 unibyte buffer: by definition, none of the single-byte
2811 characters are strong R2L, so no reordering is needed. And
2812 bidi.c doesn't support unibyte buffers anyway. Also, don't
2813 reorder while we are loading loadup.el, since the tables of
2814 character properties needed for reordering are not yet
2815 available. */
2816 it->bidi_p =
2817 NILP (Vpurify_flag)
2818 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2819 && it->multibyte_p;
2821 /* If we are to reorder bidirectional text, init the bidi
2822 iterator. */
2823 if (it->bidi_p)
2825 /* Note the paragraph direction that this buffer wants to
2826 use. */
2827 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2828 Qleft_to_right))
2829 it->paragraph_embedding = L2R;
2830 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2831 Qright_to_left))
2832 it->paragraph_embedding = R2L;
2833 else
2834 it->paragraph_embedding = NEUTRAL_DIR;
2835 bidi_unshelve_cache (NULL, 0);
2836 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2837 &it->bidi_it);
2840 /* Compute faces etc. */
2841 reseat (it, it->current.pos, 1);
2844 CHECK_IT (it);
2848 /* Initialize IT for the display of window W with window start POS. */
2850 void
2851 start_display (struct it *it, struct window *w, struct text_pos pos)
2853 struct glyph_row *row;
2854 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2856 row = w->desired_matrix->rows + first_vpos;
2857 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2858 it->first_vpos = first_vpos;
2860 /* Don't reseat to previous visible line start if current start
2861 position is in a string or image. */
2862 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2864 int start_at_line_beg_p;
2865 int first_y = it->current_y;
2867 /* If window start is not at a line start, skip forward to POS to
2868 get the correct continuation lines width. */
2869 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2870 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2871 if (!start_at_line_beg_p)
2873 int new_x;
2875 reseat_at_previous_visible_line_start (it);
2876 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2878 new_x = it->current_x + it->pixel_width;
2880 /* If lines are continued, this line may end in the middle
2881 of a multi-glyph character (e.g. a control character
2882 displayed as \003, or in the middle of an overlay
2883 string). In this case move_it_to above will not have
2884 taken us to the start of the continuation line but to the
2885 end of the continued line. */
2886 if (it->current_x > 0
2887 && it->line_wrap != TRUNCATE /* Lines are continued. */
2888 && (/* And glyph doesn't fit on the line. */
2889 new_x > it->last_visible_x
2890 /* Or it fits exactly and we're on a window
2891 system frame. */
2892 || (new_x == it->last_visible_x
2893 && FRAME_WINDOW_P (it->f))))
2895 if ((it->current.dpvec_index >= 0
2896 || it->current.overlay_string_index >= 0)
2897 /* If we are on a newline from a display vector or
2898 overlay string, then we are already at the end of
2899 a screen line; no need to go to the next line in
2900 that case, as this line is not really continued.
2901 (If we do go to the next line, C-e will not DTRT.) */
2902 && it->c != '\n')
2904 set_iterator_to_next (it, 1);
2905 move_it_in_display_line_to (it, -1, -1, 0);
2908 it->continuation_lines_width += it->current_x;
2910 /* If the character at POS is displayed via a display
2911 vector, move_it_to above stops at the final glyph of
2912 IT->dpvec. To make the caller redisplay that character
2913 again (a.k.a. start at POS), we need to reset the
2914 dpvec_index to the beginning of IT->dpvec. */
2915 else if (it->current.dpvec_index >= 0)
2916 it->current.dpvec_index = 0;
2918 /* We're starting a new display line, not affected by the
2919 height of the continued line, so clear the appropriate
2920 fields in the iterator structure. */
2921 it->max_ascent = it->max_descent = 0;
2922 it->max_phys_ascent = it->max_phys_descent = 0;
2924 it->current_y = first_y;
2925 it->vpos = 0;
2926 it->current_x = it->hpos = 0;
2932 /* Return 1 if POS is a position in ellipses displayed for invisible
2933 text. W is the window we display, for text property lookup. */
2935 static int
2936 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2938 Lisp_Object prop, window;
2939 int ellipses_p = 0;
2940 EMACS_INT charpos = CHARPOS (pos->pos);
2942 /* If POS specifies a position in a display vector, this might
2943 be for an ellipsis displayed for invisible text. We won't
2944 get the iterator set up for delivering that ellipsis unless
2945 we make sure that it gets aware of the invisible text. */
2946 if (pos->dpvec_index >= 0
2947 && pos->overlay_string_index < 0
2948 && CHARPOS (pos->string_pos) < 0
2949 && charpos > BEGV
2950 && (XSETWINDOW (window, w),
2951 prop = Fget_char_property (make_number (charpos),
2952 Qinvisible, window),
2953 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2955 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2956 window);
2957 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2960 return ellipses_p;
2964 /* Initialize IT for stepping through current_buffer in window W,
2965 starting at position POS that includes overlay string and display
2966 vector/ control character translation position information. Value
2967 is zero if there are overlay strings with newlines at POS. */
2969 static int
2970 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2972 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2973 int i, overlay_strings_with_newlines = 0;
2975 /* If POS specifies a position in a display vector, this might
2976 be for an ellipsis displayed for invisible text. We won't
2977 get the iterator set up for delivering that ellipsis unless
2978 we make sure that it gets aware of the invisible text. */
2979 if (in_ellipses_for_invisible_text_p (pos, w))
2981 --charpos;
2982 bytepos = 0;
2985 /* Keep in mind: the call to reseat in init_iterator skips invisible
2986 text, so we might end up at a position different from POS. This
2987 is only a problem when POS is a row start after a newline and an
2988 overlay starts there with an after-string, and the overlay has an
2989 invisible property. Since we don't skip invisible text in
2990 display_line and elsewhere immediately after consuming the
2991 newline before the row start, such a POS will not be in a string,
2992 but the call to init_iterator below will move us to the
2993 after-string. */
2994 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2996 /* This only scans the current chunk -- it should scan all chunks.
2997 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2998 to 16 in 22.1 to make this a lesser problem. */
2999 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3001 const char *s = SSDATA (it->overlay_strings[i]);
3002 const char *e = s + SBYTES (it->overlay_strings[i]);
3004 while (s < e && *s != '\n')
3005 ++s;
3007 if (s < e)
3009 overlay_strings_with_newlines = 1;
3010 break;
3014 /* If position is within an overlay string, set up IT to the right
3015 overlay string. */
3016 if (pos->overlay_string_index >= 0)
3018 int relative_index;
3020 /* If the first overlay string happens to have a `display'
3021 property for an image, the iterator will be set up for that
3022 image, and we have to undo that setup first before we can
3023 correct the overlay string index. */
3024 if (it->method == GET_FROM_IMAGE)
3025 pop_it (it);
3027 /* We already have the first chunk of overlay strings in
3028 IT->overlay_strings. Load more until the one for
3029 pos->overlay_string_index is in IT->overlay_strings. */
3030 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3032 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3033 it->current.overlay_string_index = 0;
3034 while (n--)
3036 load_overlay_strings (it, 0);
3037 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3041 it->current.overlay_string_index = pos->overlay_string_index;
3042 relative_index = (it->current.overlay_string_index
3043 % OVERLAY_STRING_CHUNK_SIZE);
3044 it->string = it->overlay_strings[relative_index];
3045 xassert (STRINGP (it->string));
3046 it->current.string_pos = pos->string_pos;
3047 it->method = GET_FROM_STRING;
3050 if (CHARPOS (pos->string_pos) >= 0)
3052 /* Recorded position is not in an overlay string, but in another
3053 string. This can only be a string from a `display' property.
3054 IT should already be filled with that string. */
3055 it->current.string_pos = pos->string_pos;
3056 xassert (STRINGP (it->string));
3059 /* Restore position in display vector translations, control
3060 character translations or ellipses. */
3061 if (pos->dpvec_index >= 0)
3063 if (it->dpvec == NULL)
3064 get_next_display_element (it);
3065 xassert (it->dpvec && it->current.dpvec_index == 0);
3066 it->current.dpvec_index = pos->dpvec_index;
3069 CHECK_IT (it);
3070 return !overlay_strings_with_newlines;
3074 /* Initialize IT for stepping through current_buffer in window W
3075 starting at ROW->start. */
3077 static void
3078 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3080 init_from_display_pos (it, w, &row->start);
3081 it->start = row->start;
3082 it->continuation_lines_width = row->continuation_lines_width;
3083 CHECK_IT (it);
3087 /* Initialize IT for stepping through current_buffer in window W
3088 starting in the line following ROW, i.e. starting at ROW->end.
3089 Value is zero if there are overlay strings with newlines at ROW's
3090 end position. */
3092 static int
3093 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3095 int success = 0;
3097 if (init_from_display_pos (it, w, &row->end))
3099 if (row->continued_p)
3100 it->continuation_lines_width
3101 = row->continuation_lines_width + row->pixel_width;
3102 CHECK_IT (it);
3103 success = 1;
3106 return success;
3112 /***********************************************************************
3113 Text properties
3114 ***********************************************************************/
3116 /* Called when IT reaches IT->stop_charpos. Handle text property and
3117 overlay changes. Set IT->stop_charpos to the next position where
3118 to stop. */
3120 static void
3121 handle_stop (struct it *it)
3123 enum prop_handled handled;
3124 int handle_overlay_change_p;
3125 struct props *p;
3127 it->dpvec = NULL;
3128 it->current.dpvec_index = -1;
3129 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3130 it->ignore_overlay_strings_at_pos_p = 0;
3131 it->ellipsis_p = 0;
3133 /* Use face of preceding text for ellipsis (if invisible) */
3134 if (it->selective_display_ellipsis_p)
3135 it->saved_face_id = it->face_id;
3139 handled = HANDLED_NORMALLY;
3141 /* Call text property handlers. */
3142 for (p = it_props; p->handler; ++p)
3144 handled = p->handler (it);
3146 if (handled == HANDLED_RECOMPUTE_PROPS)
3147 break;
3148 else if (handled == HANDLED_RETURN)
3150 /* We still want to show before and after strings from
3151 overlays even if the actual buffer text is replaced. */
3152 if (!handle_overlay_change_p
3153 || it->sp > 1
3154 /* Don't call get_overlay_strings_1 if we already
3155 have overlay strings loaded, because doing so
3156 will load them again and push the iterator state
3157 onto the stack one more time, which is not
3158 expected by the rest of the code that processes
3159 overlay strings. */
3160 || (it->current.overlay_string_index < 0
3161 ? !get_overlay_strings_1 (it, 0, 0)
3162 : 0))
3164 if (it->ellipsis_p)
3165 setup_for_ellipsis (it, 0);
3166 /* When handling a display spec, we might load an
3167 empty string. In that case, discard it here. We
3168 used to discard it in handle_single_display_spec,
3169 but that causes get_overlay_strings_1, above, to
3170 ignore overlay strings that we must check. */
3171 if (STRINGP (it->string) && !SCHARS (it->string))
3172 pop_it (it);
3173 return;
3175 else if (STRINGP (it->string) && !SCHARS (it->string))
3176 pop_it (it);
3177 else
3179 it->ignore_overlay_strings_at_pos_p = 1;
3180 it->string_from_display_prop_p = 0;
3181 it->from_disp_prop_p = 0;
3182 handle_overlay_change_p = 0;
3184 handled = HANDLED_RECOMPUTE_PROPS;
3185 break;
3187 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3188 handle_overlay_change_p = 0;
3191 if (handled != HANDLED_RECOMPUTE_PROPS)
3193 /* Don't check for overlay strings below when set to deliver
3194 characters from a display vector. */
3195 if (it->method == GET_FROM_DISPLAY_VECTOR)
3196 handle_overlay_change_p = 0;
3198 /* Handle overlay changes.
3199 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3200 if it finds overlays. */
3201 if (handle_overlay_change_p)
3202 handled = handle_overlay_change (it);
3205 if (it->ellipsis_p)
3207 setup_for_ellipsis (it, 0);
3208 break;
3211 while (handled == HANDLED_RECOMPUTE_PROPS);
3213 /* Determine where to stop next. */
3214 if (handled == HANDLED_NORMALLY)
3215 compute_stop_pos (it);
3219 /* Compute IT->stop_charpos from text property and overlay change
3220 information for IT's current position. */
3222 static void
3223 compute_stop_pos (struct it *it)
3225 register INTERVAL iv, next_iv;
3226 Lisp_Object object, limit, position;
3227 EMACS_INT charpos, bytepos;
3229 if (STRINGP (it->string))
3231 /* Strings are usually short, so don't limit the search for
3232 properties. */
3233 it->stop_charpos = it->end_charpos;
3234 object = it->string;
3235 limit = Qnil;
3236 charpos = IT_STRING_CHARPOS (*it);
3237 bytepos = IT_STRING_BYTEPOS (*it);
3239 else
3241 EMACS_INT pos;
3243 /* If end_charpos is out of range for some reason, such as a
3244 misbehaving display function, rationalize it (Bug#5984). */
3245 if (it->end_charpos > ZV)
3246 it->end_charpos = ZV;
3247 it->stop_charpos = it->end_charpos;
3249 /* If next overlay change is in front of the current stop pos
3250 (which is IT->end_charpos), stop there. Note: value of
3251 next_overlay_change is point-max if no overlay change
3252 follows. */
3253 charpos = IT_CHARPOS (*it);
3254 bytepos = IT_BYTEPOS (*it);
3255 pos = next_overlay_change (charpos);
3256 if (pos < it->stop_charpos)
3257 it->stop_charpos = pos;
3259 /* If showing the region, we have to stop at the region
3260 start or end because the face might change there. */
3261 if (it->region_beg_charpos > 0)
3263 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3264 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3265 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3266 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3269 /* Set up variables for computing the stop position from text
3270 property changes. */
3271 XSETBUFFER (object, current_buffer);
3272 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3275 /* Get the interval containing IT's position. Value is a null
3276 interval if there isn't such an interval. */
3277 position = make_number (charpos);
3278 iv = validate_interval_range (object, &position, &position, 0);
3279 if (!NULL_INTERVAL_P (iv))
3281 Lisp_Object values_here[LAST_PROP_IDX];
3282 struct props *p;
3284 /* Get properties here. */
3285 for (p = it_props; p->handler; ++p)
3286 values_here[p->idx] = textget (iv->plist, *p->name);
3288 /* Look for an interval following iv that has different
3289 properties. */
3290 for (next_iv = next_interval (iv);
3291 (!NULL_INTERVAL_P (next_iv)
3292 && (NILP (limit)
3293 || XFASTINT (limit) > next_iv->position));
3294 next_iv = next_interval (next_iv))
3296 for (p = it_props; p->handler; ++p)
3298 Lisp_Object new_value;
3300 new_value = textget (next_iv->plist, *p->name);
3301 if (!EQ (values_here[p->idx], new_value))
3302 break;
3305 if (p->handler)
3306 break;
3309 if (!NULL_INTERVAL_P (next_iv))
3311 if (INTEGERP (limit)
3312 && next_iv->position >= XFASTINT (limit))
3313 /* No text property change up to limit. */
3314 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3315 else
3316 /* Text properties change in next_iv. */
3317 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3321 if (it->cmp_it.id < 0)
3323 EMACS_INT stoppos = it->end_charpos;
3325 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3326 stoppos = -1;
3327 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3328 stoppos, it->string);
3331 xassert (STRINGP (it->string)
3332 || (it->stop_charpos >= BEGV
3333 && it->stop_charpos >= IT_CHARPOS (*it)));
3337 /* Return the position of the next overlay change after POS in
3338 current_buffer. Value is point-max if no overlay change
3339 follows. This is like `next-overlay-change' but doesn't use
3340 xmalloc. */
3342 static EMACS_INT
3343 next_overlay_change (EMACS_INT pos)
3345 ptrdiff_t i, noverlays;
3346 EMACS_INT endpos;
3347 Lisp_Object *overlays;
3349 /* Get all overlays at the given position. */
3350 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3352 /* If any of these overlays ends before endpos,
3353 use its ending point instead. */
3354 for (i = 0; i < noverlays; ++i)
3356 Lisp_Object oend;
3357 EMACS_INT oendpos;
3359 oend = OVERLAY_END (overlays[i]);
3360 oendpos = OVERLAY_POSITION (oend);
3361 endpos = min (endpos, oendpos);
3364 return endpos;
3367 /* How many characters forward to search for a display property or
3368 display string. Searching too far forward makes the bidi display
3369 sluggish, especially in small windows. */
3370 #define MAX_DISP_SCAN 250
3372 /* Return the character position of a display string at or after
3373 position specified by POSITION. If no display string exists at or
3374 after POSITION, return ZV. A display string is either an overlay
3375 with `display' property whose value is a string, or a `display'
3376 text property whose value is a string. STRING is data about the
3377 string to iterate; if STRING->lstring is nil, we are iterating a
3378 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3379 on a GUI frame. DISP_PROP is set to zero if we searched
3380 MAX_DISP_SCAN characters forward without finding any display
3381 strings, non-zero otherwise. It is set to 2 if the display string
3382 uses any kind of `(space ...)' spec that will produce a stretch of
3383 white space in the text area. */
3384 EMACS_INT
3385 compute_display_string_pos (struct text_pos *position,
3386 struct bidi_string_data *string,
3387 int frame_window_p, int *disp_prop)
3389 /* OBJECT = nil means current buffer. */
3390 Lisp_Object object =
3391 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3392 Lisp_Object pos, spec, limpos;
3393 int string_p = (string && (STRINGP (string->lstring) || string->s));
3394 EMACS_INT eob = string_p ? string->schars : ZV;
3395 EMACS_INT begb = string_p ? 0 : BEGV;
3396 EMACS_INT bufpos, charpos = CHARPOS (*position);
3397 EMACS_INT lim =
3398 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3399 struct text_pos tpos;
3400 int rv = 0;
3402 *disp_prop = 1;
3404 if (charpos >= eob
3405 /* We don't support display properties whose values are strings
3406 that have display string properties. */
3407 || string->from_disp_str
3408 /* C strings cannot have display properties. */
3409 || (string->s && !STRINGP (object)))
3411 *disp_prop = 0;
3412 return eob;
3415 /* If the character at CHARPOS is where the display string begins,
3416 return CHARPOS. */
3417 pos = make_number (charpos);
3418 if (STRINGP (object))
3419 bufpos = string->bufpos;
3420 else
3421 bufpos = charpos;
3422 tpos = *position;
3423 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3424 && (charpos <= begb
3425 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3426 object),
3427 spec))
3428 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3429 frame_window_p)))
3431 if (rv == 2)
3432 *disp_prop = 2;
3433 return charpos;
3436 /* Look forward for the first character with a `display' property
3437 that will replace the underlying text when displayed. */
3438 limpos = make_number (lim);
3439 do {
3440 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3441 CHARPOS (tpos) = XFASTINT (pos);
3442 if (CHARPOS (tpos) >= lim)
3444 *disp_prop = 0;
3445 break;
3447 if (STRINGP (object))
3448 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3449 else
3450 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3451 spec = Fget_char_property (pos, Qdisplay, object);
3452 if (!STRINGP (object))
3453 bufpos = CHARPOS (tpos);
3454 } while (NILP (spec)
3455 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3456 bufpos, frame_window_p)));
3457 if (rv == 2)
3458 *disp_prop = 2;
3460 return CHARPOS (tpos);
3463 /* Return the character position of the end of the display string that
3464 started at CHARPOS. If there's no display string at CHARPOS,
3465 return -1. A display string is either an overlay with `display'
3466 property whose value is a string or a `display' text property whose
3467 value is a string. */
3468 EMACS_INT
3469 compute_display_string_end (EMACS_INT charpos, struct bidi_string_data *string)
3471 /* OBJECT = nil means current buffer. */
3472 Lisp_Object object =
3473 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3474 Lisp_Object pos = make_number (charpos);
3475 EMACS_INT eob =
3476 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3478 if (charpos >= eob || (string->s && !STRINGP (object)))
3479 return eob;
3481 /* It could happen that the display property or overlay was removed
3482 since we found it in compute_display_string_pos above. One way
3483 this can happen is if JIT font-lock was called (through
3484 handle_fontified_prop), and jit-lock-functions remove text
3485 properties or overlays from the portion of buffer that includes
3486 CHARPOS. Muse mode is known to do that, for example. In this
3487 case, we return -1 to the caller, to signal that no display
3488 string is actually present at CHARPOS. See bidi_fetch_char for
3489 how this is handled.
3491 An alternative would be to never look for display properties past
3492 it->stop_charpos. But neither compute_display_string_pos nor
3493 bidi_fetch_char that calls it know or care where the next
3494 stop_charpos is. */
3495 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3496 return -1;
3498 /* Look forward for the first character where the `display' property
3499 changes. */
3500 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3502 return XFASTINT (pos);
3507 /***********************************************************************
3508 Fontification
3509 ***********************************************************************/
3511 /* Handle changes in the `fontified' property of the current buffer by
3512 calling hook functions from Qfontification_functions to fontify
3513 regions of text. */
3515 static enum prop_handled
3516 handle_fontified_prop (struct it *it)
3518 Lisp_Object prop, pos;
3519 enum prop_handled handled = HANDLED_NORMALLY;
3521 if (!NILP (Vmemory_full))
3522 return handled;
3524 /* Get the value of the `fontified' property at IT's current buffer
3525 position. (The `fontified' property doesn't have a special
3526 meaning in strings.) If the value is nil, call functions from
3527 Qfontification_functions. */
3528 if (!STRINGP (it->string)
3529 && it->s == NULL
3530 && !NILP (Vfontification_functions)
3531 && !NILP (Vrun_hooks)
3532 && (pos = make_number (IT_CHARPOS (*it)),
3533 prop = Fget_char_property (pos, Qfontified, Qnil),
3534 /* Ignore the special cased nil value always present at EOB since
3535 no amount of fontifying will be able to change it. */
3536 NILP (prop) && IT_CHARPOS (*it) < Z))
3538 int count = SPECPDL_INDEX ();
3539 Lisp_Object val;
3540 struct buffer *obuf = current_buffer;
3541 int begv = BEGV, zv = ZV;
3542 int old_clip_changed = current_buffer->clip_changed;
3544 val = Vfontification_functions;
3545 specbind (Qfontification_functions, Qnil);
3547 xassert (it->end_charpos == ZV);
3549 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3550 safe_call1 (val, pos);
3551 else
3553 Lisp_Object fns, fn;
3554 struct gcpro gcpro1, gcpro2;
3556 fns = Qnil;
3557 GCPRO2 (val, fns);
3559 for (; CONSP (val); val = XCDR (val))
3561 fn = XCAR (val);
3563 if (EQ (fn, Qt))
3565 /* A value of t indicates this hook has a local
3566 binding; it means to run the global binding too.
3567 In a global value, t should not occur. If it
3568 does, we must ignore it to avoid an endless
3569 loop. */
3570 for (fns = Fdefault_value (Qfontification_functions);
3571 CONSP (fns);
3572 fns = XCDR (fns))
3574 fn = XCAR (fns);
3575 if (!EQ (fn, Qt))
3576 safe_call1 (fn, pos);
3579 else
3580 safe_call1 (fn, pos);
3583 UNGCPRO;
3586 unbind_to (count, Qnil);
3588 /* Fontification functions routinely call `save-restriction'.
3589 Normally, this tags clip_changed, which can confuse redisplay
3590 (see discussion in Bug#6671). Since we don't perform any
3591 special handling of fontification changes in the case where
3592 `save-restriction' isn't called, there's no point doing so in
3593 this case either. So, if the buffer's restrictions are
3594 actually left unchanged, reset clip_changed. */
3595 if (obuf == current_buffer)
3597 if (begv == BEGV && zv == ZV)
3598 current_buffer->clip_changed = old_clip_changed;
3600 /* There isn't much we can reasonably do to protect against
3601 misbehaving fontification, but here's a fig leaf. */
3602 else if (!NILP (BVAR (obuf, name)))
3603 set_buffer_internal_1 (obuf);
3605 /* The fontification code may have added/removed text.
3606 It could do even a lot worse, but let's at least protect against
3607 the most obvious case where only the text past `pos' gets changed',
3608 as is/was done in grep.el where some escapes sequences are turned
3609 into face properties (bug#7876). */
3610 it->end_charpos = ZV;
3612 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3613 something. This avoids an endless loop if they failed to
3614 fontify the text for which reason ever. */
3615 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3616 handled = HANDLED_RECOMPUTE_PROPS;
3619 return handled;
3624 /***********************************************************************
3625 Faces
3626 ***********************************************************************/
3628 /* Set up iterator IT from face properties at its current position.
3629 Called from handle_stop. */
3631 static enum prop_handled
3632 handle_face_prop (struct it *it)
3634 int new_face_id;
3635 EMACS_INT next_stop;
3637 if (!STRINGP (it->string))
3639 new_face_id
3640 = face_at_buffer_position (it->w,
3641 IT_CHARPOS (*it),
3642 it->region_beg_charpos,
3643 it->region_end_charpos,
3644 &next_stop,
3645 (IT_CHARPOS (*it)
3646 + TEXT_PROP_DISTANCE_LIMIT),
3647 0, it->base_face_id);
3649 /* Is this a start of a run of characters with box face?
3650 Caveat: this can be called for a freshly initialized
3651 iterator; face_id is -1 in this case. We know that the new
3652 face will not change until limit, i.e. if the new face has a
3653 box, all characters up to limit will have one. But, as
3654 usual, we don't know whether limit is really the end. */
3655 if (new_face_id != it->face_id)
3657 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3659 /* If new face has a box but old face has not, this is
3660 the start of a run of characters with box, i.e. it has
3661 a shadow on the left side. The value of face_id of the
3662 iterator will be -1 if this is the initial call that gets
3663 the face. In this case, we have to look in front of IT's
3664 position and see whether there is a face != new_face_id. */
3665 it->start_of_box_run_p
3666 = (new_face->box != FACE_NO_BOX
3667 && (it->face_id >= 0
3668 || IT_CHARPOS (*it) == BEG
3669 || new_face_id != face_before_it_pos (it)));
3670 it->face_box_p = new_face->box != FACE_NO_BOX;
3673 else
3675 int base_face_id;
3676 EMACS_INT bufpos;
3677 int i;
3678 Lisp_Object from_overlay
3679 = (it->current.overlay_string_index >= 0
3680 ? it->string_overlays[it->current.overlay_string_index]
3681 : Qnil);
3683 /* See if we got to this string directly or indirectly from
3684 an overlay property. That includes the before-string or
3685 after-string of an overlay, strings in display properties
3686 provided by an overlay, their text properties, etc.
3688 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3689 if (! NILP (from_overlay))
3690 for (i = it->sp - 1; i >= 0; i--)
3692 if (it->stack[i].current.overlay_string_index >= 0)
3693 from_overlay
3694 = it->string_overlays[it->stack[i].current.overlay_string_index];
3695 else if (! NILP (it->stack[i].from_overlay))
3696 from_overlay = it->stack[i].from_overlay;
3698 if (!NILP (from_overlay))
3699 break;
3702 if (! NILP (from_overlay))
3704 bufpos = IT_CHARPOS (*it);
3705 /* For a string from an overlay, the base face depends
3706 only on text properties and ignores overlays. */
3707 base_face_id
3708 = face_for_overlay_string (it->w,
3709 IT_CHARPOS (*it),
3710 it->region_beg_charpos,
3711 it->region_end_charpos,
3712 &next_stop,
3713 (IT_CHARPOS (*it)
3714 + TEXT_PROP_DISTANCE_LIMIT),
3716 from_overlay);
3718 else
3720 bufpos = 0;
3722 /* For strings from a `display' property, use the face at
3723 IT's current buffer position as the base face to merge
3724 with, so that overlay strings appear in the same face as
3725 surrounding text, unless they specify their own
3726 faces. */
3727 base_face_id = it->string_from_prefix_prop_p
3728 ? DEFAULT_FACE_ID
3729 : underlying_face_id (it);
3732 new_face_id = face_at_string_position (it->w,
3733 it->string,
3734 IT_STRING_CHARPOS (*it),
3735 bufpos,
3736 it->region_beg_charpos,
3737 it->region_end_charpos,
3738 &next_stop,
3739 base_face_id, 0);
3741 /* Is this a start of a run of characters with box? Caveat:
3742 this can be called for a freshly allocated iterator; face_id
3743 is -1 is this case. We know that the new face will not
3744 change until the next check pos, i.e. if the new face has a
3745 box, all characters up to that position will have a
3746 box. But, as usual, we don't know whether that position
3747 is really the end. */
3748 if (new_face_id != it->face_id)
3750 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3751 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3753 /* If new face has a box but old face hasn't, this is the
3754 start of a run of characters with box, i.e. it has a
3755 shadow on the left side. */
3756 it->start_of_box_run_p
3757 = new_face->box && (old_face == NULL || !old_face->box);
3758 it->face_box_p = new_face->box != FACE_NO_BOX;
3762 it->face_id = new_face_id;
3763 return HANDLED_NORMALLY;
3767 /* Return the ID of the face ``underlying'' IT's current position,
3768 which is in a string. If the iterator is associated with a
3769 buffer, return the face at IT's current buffer position.
3770 Otherwise, use the iterator's base_face_id. */
3772 static int
3773 underlying_face_id (struct it *it)
3775 int face_id = it->base_face_id, i;
3777 xassert (STRINGP (it->string));
3779 for (i = it->sp - 1; i >= 0; --i)
3780 if (NILP (it->stack[i].string))
3781 face_id = it->stack[i].face_id;
3783 return face_id;
3787 /* Compute the face one character before or after the current position
3788 of IT, in the visual order. BEFORE_P non-zero means get the face
3789 in front (to the left in L2R paragraphs, to the right in R2L
3790 paragraphs) of IT's screen position. Value is the ID of the face. */
3792 static int
3793 face_before_or_after_it_pos (struct it *it, int before_p)
3795 int face_id, limit;
3796 EMACS_INT next_check_charpos;
3797 struct it it_copy;
3798 void *it_copy_data = NULL;
3800 xassert (it->s == NULL);
3802 if (STRINGP (it->string))
3804 EMACS_INT bufpos, charpos;
3805 int base_face_id;
3807 /* No face change past the end of the string (for the case
3808 we are padding with spaces). No face change before the
3809 string start. */
3810 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3811 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3812 return it->face_id;
3814 if (!it->bidi_p)
3816 /* Set charpos to the position before or after IT's current
3817 position, in the logical order, which in the non-bidi
3818 case is the same as the visual order. */
3819 if (before_p)
3820 charpos = IT_STRING_CHARPOS (*it) - 1;
3821 else if (it->what == IT_COMPOSITION)
3822 /* For composition, we must check the character after the
3823 composition. */
3824 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3825 else
3826 charpos = IT_STRING_CHARPOS (*it) + 1;
3828 else
3830 if (before_p)
3832 /* With bidi iteration, the character before the current
3833 in the visual order cannot be found by simple
3834 iteration, because "reverse" reordering is not
3835 supported. Instead, we need to use the move_it_*
3836 family of functions. */
3837 /* Ignore face changes before the first visible
3838 character on this display line. */
3839 if (it->current_x <= it->first_visible_x)
3840 return it->face_id;
3841 SAVE_IT (it_copy, *it, it_copy_data);
3842 /* Implementation note: Since move_it_in_display_line
3843 works in the iterator geometry, and thinks the first
3844 character is always the leftmost, even in R2L lines,
3845 we don't need to distinguish between the R2L and L2R
3846 cases here. */
3847 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3848 it_copy.current_x - 1, MOVE_TO_X);
3849 charpos = IT_STRING_CHARPOS (it_copy);
3850 RESTORE_IT (it, it, it_copy_data);
3852 else
3854 /* Set charpos to the string position of the character
3855 that comes after IT's current position in the visual
3856 order. */
3857 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3859 it_copy = *it;
3860 while (n--)
3861 bidi_move_to_visually_next (&it_copy.bidi_it);
3863 charpos = it_copy.bidi_it.charpos;
3866 xassert (0 <= charpos && charpos <= SCHARS (it->string));
3868 if (it->current.overlay_string_index >= 0)
3869 bufpos = IT_CHARPOS (*it);
3870 else
3871 bufpos = 0;
3873 base_face_id = underlying_face_id (it);
3875 /* Get the face for ASCII, or unibyte. */
3876 face_id = face_at_string_position (it->w,
3877 it->string,
3878 charpos,
3879 bufpos,
3880 it->region_beg_charpos,
3881 it->region_end_charpos,
3882 &next_check_charpos,
3883 base_face_id, 0);
3885 /* Correct the face for charsets different from ASCII. Do it
3886 for the multibyte case only. The face returned above is
3887 suitable for unibyte text if IT->string is unibyte. */
3888 if (STRING_MULTIBYTE (it->string))
3890 struct text_pos pos1 = string_pos (charpos, it->string);
3891 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3892 int c, len;
3893 struct face *face = FACE_FROM_ID (it->f, face_id);
3895 c = string_char_and_length (p, &len);
3896 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3899 else
3901 struct text_pos pos;
3903 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3904 || (IT_CHARPOS (*it) <= BEGV && before_p))
3905 return it->face_id;
3907 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3908 pos = it->current.pos;
3910 if (!it->bidi_p)
3912 if (before_p)
3913 DEC_TEXT_POS (pos, it->multibyte_p);
3914 else
3916 if (it->what == IT_COMPOSITION)
3918 /* For composition, we must check the position after
3919 the composition. */
3920 pos.charpos += it->cmp_it.nchars;
3921 pos.bytepos += it->len;
3923 else
3924 INC_TEXT_POS (pos, it->multibyte_p);
3927 else
3929 if (before_p)
3931 /* With bidi iteration, the character before the current
3932 in the visual order cannot be found by simple
3933 iteration, because "reverse" reordering is not
3934 supported. Instead, we need to use the move_it_*
3935 family of functions. */
3936 /* Ignore face changes before the first visible
3937 character on this display line. */
3938 if (it->current_x <= it->first_visible_x)
3939 return it->face_id;
3940 SAVE_IT (it_copy, *it, it_copy_data);
3941 /* Implementation note: Since move_it_in_display_line
3942 works in the iterator geometry, and thinks the first
3943 character is always the leftmost, even in R2L lines,
3944 we don't need to distinguish between the R2L and L2R
3945 cases here. */
3946 move_it_in_display_line (&it_copy, ZV,
3947 it_copy.current_x - 1, MOVE_TO_X);
3948 pos = it_copy.current.pos;
3949 RESTORE_IT (it, it, it_copy_data);
3951 else
3953 /* Set charpos to the buffer position of the character
3954 that comes after IT's current position in the visual
3955 order. */
3956 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3958 it_copy = *it;
3959 while (n--)
3960 bidi_move_to_visually_next (&it_copy.bidi_it);
3962 SET_TEXT_POS (pos,
3963 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3966 xassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
3968 /* Determine face for CHARSET_ASCII, or unibyte. */
3969 face_id = face_at_buffer_position (it->w,
3970 CHARPOS (pos),
3971 it->region_beg_charpos,
3972 it->region_end_charpos,
3973 &next_check_charpos,
3974 limit, 0, -1);
3976 /* Correct the face for charsets different from ASCII. Do it
3977 for the multibyte case only. The face returned above is
3978 suitable for unibyte text if current_buffer is unibyte. */
3979 if (it->multibyte_p)
3981 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3982 struct face *face = FACE_FROM_ID (it->f, face_id);
3983 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3987 return face_id;
3992 /***********************************************************************
3993 Invisible text
3994 ***********************************************************************/
3996 /* Set up iterator IT from invisible properties at its current
3997 position. Called from handle_stop. */
3999 static enum prop_handled
4000 handle_invisible_prop (struct it *it)
4002 enum prop_handled handled = HANDLED_NORMALLY;
4004 if (STRINGP (it->string))
4006 Lisp_Object prop, end_charpos, limit, charpos;
4008 /* Get the value of the invisible text property at the
4009 current position. Value will be nil if there is no such
4010 property. */
4011 charpos = make_number (IT_STRING_CHARPOS (*it));
4012 prop = Fget_text_property (charpos, Qinvisible, it->string);
4014 if (!NILP (prop)
4015 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4017 EMACS_INT endpos;
4019 handled = HANDLED_RECOMPUTE_PROPS;
4021 /* Get the position at which the next change of the
4022 invisible text property can be found in IT->string.
4023 Value will be nil if the property value is the same for
4024 all the rest of IT->string. */
4025 XSETINT (limit, SCHARS (it->string));
4026 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4027 it->string, limit);
4029 /* Text at current position is invisible. The next
4030 change in the property is at position end_charpos.
4031 Move IT's current position to that position. */
4032 if (INTEGERP (end_charpos)
4033 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
4035 struct text_pos old;
4036 EMACS_INT oldpos;
4038 old = it->current.string_pos;
4039 oldpos = CHARPOS (old);
4040 if (it->bidi_p)
4042 if (it->bidi_it.first_elt
4043 && it->bidi_it.charpos < SCHARS (it->string))
4044 bidi_paragraph_init (it->paragraph_embedding,
4045 &it->bidi_it, 1);
4046 /* Bidi-iterate out of the invisible text. */
4049 bidi_move_to_visually_next (&it->bidi_it);
4051 while (oldpos <= it->bidi_it.charpos
4052 && it->bidi_it.charpos < endpos);
4054 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4055 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4056 if (IT_CHARPOS (*it) >= endpos)
4057 it->prev_stop = endpos;
4059 else
4061 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4062 compute_string_pos (&it->current.string_pos, old, it->string);
4065 else
4067 /* The rest of the string is invisible. If this is an
4068 overlay string, proceed with the next overlay string
4069 or whatever comes and return a character from there. */
4070 if (it->current.overlay_string_index >= 0)
4072 next_overlay_string (it);
4073 /* Don't check for overlay strings when we just
4074 finished processing them. */
4075 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4077 else
4079 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4080 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4085 else
4087 int invis_p;
4088 EMACS_INT newpos, next_stop, start_charpos, tem;
4089 Lisp_Object pos, prop, overlay;
4091 /* First of all, is there invisible text at this position? */
4092 tem = start_charpos = IT_CHARPOS (*it);
4093 pos = make_number (tem);
4094 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4095 &overlay);
4096 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4098 /* If we are on invisible text, skip over it. */
4099 if (invis_p && start_charpos < it->end_charpos)
4101 /* Record whether we have to display an ellipsis for the
4102 invisible text. */
4103 int display_ellipsis_p = invis_p == 2;
4105 handled = HANDLED_RECOMPUTE_PROPS;
4107 /* Loop skipping over invisible text. The loop is left at
4108 ZV or with IT on the first char being visible again. */
4111 /* Try to skip some invisible text. Return value is the
4112 position reached which can be equal to where we start
4113 if there is nothing invisible there. This skips both
4114 over invisible text properties and overlays with
4115 invisible property. */
4116 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4118 /* If we skipped nothing at all we weren't at invisible
4119 text in the first place. If everything to the end of
4120 the buffer was skipped, end the loop. */
4121 if (newpos == tem || newpos >= ZV)
4122 invis_p = 0;
4123 else
4125 /* We skipped some characters but not necessarily
4126 all there are. Check if we ended up on visible
4127 text. Fget_char_property returns the property of
4128 the char before the given position, i.e. if we
4129 get invis_p = 0, this means that the char at
4130 newpos is visible. */
4131 pos = make_number (newpos);
4132 prop = Fget_char_property (pos, Qinvisible, it->window);
4133 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4136 /* If we ended up on invisible text, proceed to
4137 skip starting with next_stop. */
4138 if (invis_p)
4139 tem = next_stop;
4141 /* If there are adjacent invisible texts, don't lose the
4142 second one's ellipsis. */
4143 if (invis_p == 2)
4144 display_ellipsis_p = 1;
4146 while (invis_p);
4148 /* The position newpos is now either ZV or on visible text. */
4149 if (it->bidi_p)
4151 EMACS_INT bpos = CHAR_TO_BYTE (newpos);
4152 int on_newline =
4153 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4154 int after_newline =
4155 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4157 /* If the invisible text ends on a newline or on a
4158 character after a newline, we can avoid the costly,
4159 character by character, bidi iteration to NEWPOS, and
4160 instead simply reseat the iterator there. That's
4161 because all bidi reordering information is tossed at
4162 the newline. This is a big win for modes that hide
4163 complete lines, like Outline, Org, etc. */
4164 if (on_newline || after_newline)
4166 struct text_pos tpos;
4167 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4169 SET_TEXT_POS (tpos, newpos, bpos);
4170 reseat_1 (it, tpos, 0);
4171 /* If we reseat on a newline/ZV, we need to prep the
4172 bidi iterator for advancing to the next character
4173 after the newline/EOB, keeping the current paragraph
4174 direction (so that PRODUCE_GLYPHS does TRT wrt
4175 prepending/appending glyphs to a glyph row). */
4176 if (on_newline)
4178 it->bidi_it.first_elt = 0;
4179 it->bidi_it.paragraph_dir = pdir;
4180 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4181 it->bidi_it.nchars = 1;
4182 it->bidi_it.ch_len = 1;
4185 else /* Must use the slow method. */
4187 /* With bidi iteration, the region of invisible text
4188 could start and/or end in the middle of a
4189 non-base embedding level. Therefore, we need to
4190 skip invisible text using the bidi iterator,
4191 starting at IT's current position, until we find
4192 ourselves outside of the invisible text.
4193 Skipping invisible text _after_ bidi iteration
4194 avoids affecting the visual order of the
4195 displayed text when invisible properties are
4196 added or removed. */
4197 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4199 /* If we were `reseat'ed to a new paragraph,
4200 determine the paragraph base direction. We
4201 need to do it now because
4202 next_element_from_buffer may not have a
4203 chance to do it, if we are going to skip any
4204 text at the beginning, which resets the
4205 FIRST_ELT flag. */
4206 bidi_paragraph_init (it->paragraph_embedding,
4207 &it->bidi_it, 1);
4211 bidi_move_to_visually_next (&it->bidi_it);
4213 while (it->stop_charpos <= it->bidi_it.charpos
4214 && it->bidi_it.charpos < newpos);
4215 IT_CHARPOS (*it) = it->bidi_it.charpos;
4216 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4217 /* If we overstepped NEWPOS, record its position in
4218 the iterator, so that we skip invisible text if
4219 later the bidi iteration lands us in the
4220 invisible region again. */
4221 if (IT_CHARPOS (*it) >= newpos)
4222 it->prev_stop = newpos;
4225 else
4227 IT_CHARPOS (*it) = newpos;
4228 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4231 /* If there are before-strings at the start of invisible
4232 text, and the text is invisible because of a text
4233 property, arrange to show before-strings because 20.x did
4234 it that way. (If the text is invisible because of an
4235 overlay property instead of a text property, this is
4236 already handled in the overlay code.) */
4237 if (NILP (overlay)
4238 && get_overlay_strings (it, it->stop_charpos))
4240 handled = HANDLED_RECOMPUTE_PROPS;
4241 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4243 else if (display_ellipsis_p)
4245 /* Make sure that the glyphs of the ellipsis will get
4246 correct `charpos' values. If we would not update
4247 it->position here, the glyphs would belong to the
4248 last visible character _before_ the invisible
4249 text, which confuses `set_cursor_from_row'.
4251 We use the last invisible position instead of the
4252 first because this way the cursor is always drawn on
4253 the first "." of the ellipsis, whenever PT is inside
4254 the invisible text. Otherwise the cursor would be
4255 placed _after_ the ellipsis when the point is after the
4256 first invisible character. */
4257 if (!STRINGP (it->object))
4259 it->position.charpos = newpos - 1;
4260 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4262 it->ellipsis_p = 1;
4263 /* Let the ellipsis display before
4264 considering any properties of the following char.
4265 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4266 handled = HANDLED_RETURN;
4271 return handled;
4275 /* Make iterator IT return `...' next.
4276 Replaces LEN characters from buffer. */
4278 static void
4279 setup_for_ellipsis (struct it *it, int len)
4281 /* Use the display table definition for `...'. Invalid glyphs
4282 will be handled by the method returning elements from dpvec. */
4283 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4285 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4286 it->dpvec = v->contents;
4287 it->dpend = v->contents + v->header.size;
4289 else
4291 /* Default `...'. */
4292 it->dpvec = default_invis_vector;
4293 it->dpend = default_invis_vector + 3;
4296 it->dpvec_char_len = len;
4297 it->current.dpvec_index = 0;
4298 it->dpvec_face_id = -1;
4300 /* Remember the current face id in case glyphs specify faces.
4301 IT's face is restored in set_iterator_to_next.
4302 saved_face_id was set to preceding char's face in handle_stop. */
4303 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4304 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4306 it->method = GET_FROM_DISPLAY_VECTOR;
4307 it->ellipsis_p = 1;
4312 /***********************************************************************
4313 'display' property
4314 ***********************************************************************/
4316 /* Set up iterator IT from `display' property at its current position.
4317 Called from handle_stop.
4318 We return HANDLED_RETURN if some part of the display property
4319 overrides the display of the buffer text itself.
4320 Otherwise we return HANDLED_NORMALLY. */
4322 static enum prop_handled
4323 handle_display_prop (struct it *it)
4325 Lisp_Object propval, object, overlay;
4326 struct text_pos *position;
4327 EMACS_INT bufpos;
4328 /* Nonzero if some property replaces the display of the text itself. */
4329 int display_replaced_p = 0;
4331 if (STRINGP (it->string))
4333 object = it->string;
4334 position = &it->current.string_pos;
4335 bufpos = CHARPOS (it->current.pos);
4337 else
4339 XSETWINDOW (object, it->w);
4340 position = &it->current.pos;
4341 bufpos = CHARPOS (*position);
4344 /* Reset those iterator values set from display property values. */
4345 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4346 it->space_width = Qnil;
4347 it->font_height = Qnil;
4348 it->voffset = 0;
4350 /* We don't support recursive `display' properties, i.e. string
4351 values that have a string `display' property, that have a string
4352 `display' property etc. */
4353 if (!it->string_from_display_prop_p)
4354 it->area = TEXT_AREA;
4356 propval = get_char_property_and_overlay (make_number (position->charpos),
4357 Qdisplay, object, &overlay);
4358 if (NILP (propval))
4359 return HANDLED_NORMALLY;
4360 /* Now OVERLAY is the overlay that gave us this property, or nil
4361 if it was a text property. */
4363 if (!STRINGP (it->string))
4364 object = it->w->buffer;
4366 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4367 position, bufpos,
4368 FRAME_WINDOW_P (it->f));
4370 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4373 /* Subroutine of handle_display_prop. Returns non-zero if the display
4374 specification in SPEC is a replacing specification, i.e. it would
4375 replace the text covered by `display' property with something else,
4376 such as an image or a display string. If SPEC includes any kind or
4377 `(space ...) specification, the value is 2; this is used by
4378 compute_display_string_pos, which see.
4380 See handle_single_display_spec for documentation of arguments.
4381 frame_window_p is non-zero if the window being redisplayed is on a
4382 GUI frame; this argument is used only if IT is NULL, see below.
4384 IT can be NULL, if this is called by the bidi reordering code
4385 through compute_display_string_pos, which see. In that case, this
4386 function only examines SPEC, but does not otherwise "handle" it, in
4387 the sense that it doesn't set up members of IT from the display
4388 spec. */
4389 static int
4390 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4391 Lisp_Object overlay, struct text_pos *position,
4392 EMACS_INT bufpos, int frame_window_p)
4394 int replacing_p = 0;
4395 int rv;
4397 if (CONSP (spec)
4398 /* Simple specifications. */
4399 && !EQ (XCAR (spec), Qimage)
4400 && !EQ (XCAR (spec), Qspace)
4401 && !EQ (XCAR (spec), Qwhen)
4402 && !EQ (XCAR (spec), Qslice)
4403 && !EQ (XCAR (spec), Qspace_width)
4404 && !EQ (XCAR (spec), Qheight)
4405 && !EQ (XCAR (spec), Qraise)
4406 /* Marginal area specifications. */
4407 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4408 && !EQ (XCAR (spec), Qleft_fringe)
4409 && !EQ (XCAR (spec), Qright_fringe)
4410 && !NILP (XCAR (spec)))
4412 for (; CONSP (spec); spec = XCDR (spec))
4414 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4415 overlay, position, bufpos,
4416 replacing_p, frame_window_p)))
4418 replacing_p = rv;
4419 /* If some text in a string is replaced, `position' no
4420 longer points to the position of `object'. */
4421 if (!it || STRINGP (object))
4422 break;
4426 else if (VECTORP (spec))
4428 int i;
4429 for (i = 0; i < ASIZE (spec); ++i)
4430 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4431 overlay, position, bufpos,
4432 replacing_p, frame_window_p)))
4434 replacing_p = rv;
4435 /* If some text in a string is replaced, `position' no
4436 longer points to the position of `object'. */
4437 if (!it || STRINGP (object))
4438 break;
4441 else
4443 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4444 position, bufpos, 0,
4445 frame_window_p)))
4446 replacing_p = rv;
4449 return replacing_p;
4452 /* Value is the position of the end of the `display' property starting
4453 at START_POS in OBJECT. */
4455 static struct text_pos
4456 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4458 Lisp_Object end;
4459 struct text_pos end_pos;
4461 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4462 Qdisplay, object, Qnil);
4463 CHARPOS (end_pos) = XFASTINT (end);
4464 if (STRINGP (object))
4465 compute_string_pos (&end_pos, start_pos, it->string);
4466 else
4467 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4469 return end_pos;
4473 /* Set up IT from a single `display' property specification SPEC. OBJECT
4474 is the object in which the `display' property was found. *POSITION
4475 is the position in OBJECT at which the `display' property was found.
4476 BUFPOS is the buffer position of OBJECT (different from POSITION if
4477 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4478 previously saw a display specification which already replaced text
4479 display with something else, for example an image; we ignore such
4480 properties after the first one has been processed.
4482 OVERLAY is the overlay this `display' property came from,
4483 or nil if it was a text property.
4485 If SPEC is a `space' or `image' specification, and in some other
4486 cases too, set *POSITION to the position where the `display'
4487 property ends.
4489 If IT is NULL, only examine the property specification in SPEC, but
4490 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4491 is intended to be displayed in a window on a GUI frame.
4493 Value is non-zero if something was found which replaces the display
4494 of buffer or string text. */
4496 static int
4497 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4498 Lisp_Object overlay, struct text_pos *position,
4499 EMACS_INT bufpos, int display_replaced_p,
4500 int frame_window_p)
4502 Lisp_Object form;
4503 Lisp_Object location, value;
4504 struct text_pos start_pos = *position;
4505 int valid_p;
4507 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4508 If the result is non-nil, use VALUE instead of SPEC. */
4509 form = Qt;
4510 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4512 spec = XCDR (spec);
4513 if (!CONSP (spec))
4514 return 0;
4515 form = XCAR (spec);
4516 spec = XCDR (spec);
4519 if (!NILP (form) && !EQ (form, Qt))
4521 int count = SPECPDL_INDEX ();
4522 struct gcpro gcpro1;
4524 /* Bind `object' to the object having the `display' property, a
4525 buffer or string. Bind `position' to the position in the
4526 object where the property was found, and `buffer-position'
4527 to the current position in the buffer. */
4529 if (NILP (object))
4530 XSETBUFFER (object, current_buffer);
4531 specbind (Qobject, object);
4532 specbind (Qposition, make_number (CHARPOS (*position)));
4533 specbind (Qbuffer_position, make_number (bufpos));
4534 GCPRO1 (form);
4535 form = safe_eval (form);
4536 UNGCPRO;
4537 unbind_to (count, Qnil);
4540 if (NILP (form))
4541 return 0;
4543 /* Handle `(height HEIGHT)' specifications. */
4544 if (CONSP (spec)
4545 && EQ (XCAR (spec), Qheight)
4546 && CONSP (XCDR (spec)))
4548 if (it)
4550 if (!FRAME_WINDOW_P (it->f))
4551 return 0;
4553 it->font_height = XCAR (XCDR (spec));
4554 if (!NILP (it->font_height))
4556 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4557 int new_height = -1;
4559 if (CONSP (it->font_height)
4560 && (EQ (XCAR (it->font_height), Qplus)
4561 || EQ (XCAR (it->font_height), Qminus))
4562 && CONSP (XCDR (it->font_height))
4563 && INTEGERP (XCAR (XCDR (it->font_height))))
4565 /* `(+ N)' or `(- N)' where N is an integer. */
4566 int steps = XINT (XCAR (XCDR (it->font_height)));
4567 if (EQ (XCAR (it->font_height), Qplus))
4568 steps = - steps;
4569 it->face_id = smaller_face (it->f, it->face_id, steps);
4571 else if (FUNCTIONP (it->font_height))
4573 /* Call function with current height as argument.
4574 Value is the new height. */
4575 Lisp_Object height;
4576 height = safe_call1 (it->font_height,
4577 face->lface[LFACE_HEIGHT_INDEX]);
4578 if (NUMBERP (height))
4579 new_height = XFLOATINT (height);
4581 else if (NUMBERP (it->font_height))
4583 /* Value is a multiple of the canonical char height. */
4584 struct face *f;
4586 f = FACE_FROM_ID (it->f,
4587 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4588 new_height = (XFLOATINT (it->font_height)
4589 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4591 else
4593 /* Evaluate IT->font_height with `height' bound to the
4594 current specified height to get the new height. */
4595 int count = SPECPDL_INDEX ();
4597 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4598 value = safe_eval (it->font_height);
4599 unbind_to (count, Qnil);
4601 if (NUMBERP (value))
4602 new_height = XFLOATINT (value);
4605 if (new_height > 0)
4606 it->face_id = face_with_height (it->f, it->face_id, new_height);
4610 return 0;
4613 /* Handle `(space-width WIDTH)'. */
4614 if (CONSP (spec)
4615 && EQ (XCAR (spec), Qspace_width)
4616 && CONSP (XCDR (spec)))
4618 if (it)
4620 if (!FRAME_WINDOW_P (it->f))
4621 return 0;
4623 value = XCAR (XCDR (spec));
4624 if (NUMBERP (value) && XFLOATINT (value) > 0)
4625 it->space_width = value;
4628 return 0;
4631 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4632 if (CONSP (spec)
4633 && EQ (XCAR (spec), Qslice))
4635 Lisp_Object tem;
4637 if (it)
4639 if (!FRAME_WINDOW_P (it->f))
4640 return 0;
4642 if (tem = XCDR (spec), CONSP (tem))
4644 it->slice.x = XCAR (tem);
4645 if (tem = XCDR (tem), CONSP (tem))
4647 it->slice.y = XCAR (tem);
4648 if (tem = XCDR (tem), CONSP (tem))
4650 it->slice.width = XCAR (tem);
4651 if (tem = XCDR (tem), CONSP (tem))
4652 it->slice.height = XCAR (tem);
4658 return 0;
4661 /* Handle `(raise FACTOR)'. */
4662 if (CONSP (spec)
4663 && EQ (XCAR (spec), Qraise)
4664 && CONSP (XCDR (spec)))
4666 if (it)
4668 if (!FRAME_WINDOW_P (it->f))
4669 return 0;
4671 #ifdef HAVE_WINDOW_SYSTEM
4672 value = XCAR (XCDR (spec));
4673 if (NUMBERP (value))
4675 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4676 it->voffset = - (XFLOATINT (value)
4677 * (FONT_HEIGHT (face->font)));
4679 #endif /* HAVE_WINDOW_SYSTEM */
4682 return 0;
4685 /* Don't handle the other kinds of display specifications
4686 inside a string that we got from a `display' property. */
4687 if (it && it->string_from_display_prop_p)
4688 return 0;
4690 /* Characters having this form of property are not displayed, so
4691 we have to find the end of the property. */
4692 if (it)
4694 start_pos = *position;
4695 *position = display_prop_end (it, object, start_pos);
4697 value = Qnil;
4699 /* Stop the scan at that end position--we assume that all
4700 text properties change there. */
4701 if (it)
4702 it->stop_charpos = position->charpos;
4704 /* Handle `(left-fringe BITMAP [FACE])'
4705 and `(right-fringe BITMAP [FACE])'. */
4706 if (CONSP (spec)
4707 && (EQ (XCAR (spec), Qleft_fringe)
4708 || EQ (XCAR (spec), Qright_fringe))
4709 && CONSP (XCDR (spec)))
4711 int fringe_bitmap;
4713 if (it)
4715 if (!FRAME_WINDOW_P (it->f))
4716 /* If we return here, POSITION has been advanced
4717 across the text with this property. */
4719 /* Synchronize the bidi iterator with POSITION. This is
4720 needed because we are not going to push the iterator
4721 on behalf of this display property, so there will be
4722 no pop_it call to do this synchronization for us. */
4723 if (it->bidi_p)
4725 it->position = *position;
4726 iterate_out_of_display_property (it);
4727 *position = it->position;
4729 return 1;
4732 else if (!frame_window_p)
4733 return 1;
4735 #ifdef HAVE_WINDOW_SYSTEM
4736 value = XCAR (XCDR (spec));
4737 if (!SYMBOLP (value)
4738 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4739 /* If we return here, POSITION has been advanced
4740 across the text with this property. */
4742 if (it && it->bidi_p)
4744 it->position = *position;
4745 iterate_out_of_display_property (it);
4746 *position = it->position;
4748 return 1;
4751 if (it)
4753 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4755 if (CONSP (XCDR (XCDR (spec))))
4757 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4758 int face_id2 = lookup_derived_face (it->f, face_name,
4759 FRINGE_FACE_ID, 0);
4760 if (face_id2 >= 0)
4761 face_id = face_id2;
4764 /* Save current settings of IT so that we can restore them
4765 when we are finished with the glyph property value. */
4766 push_it (it, position);
4768 it->area = TEXT_AREA;
4769 it->what = IT_IMAGE;
4770 it->image_id = -1; /* no image */
4771 it->position = start_pos;
4772 it->object = NILP (object) ? it->w->buffer : object;
4773 it->method = GET_FROM_IMAGE;
4774 it->from_overlay = Qnil;
4775 it->face_id = face_id;
4776 it->from_disp_prop_p = 1;
4778 /* Say that we haven't consumed the characters with
4779 `display' property yet. The call to pop_it in
4780 set_iterator_to_next will clean this up. */
4781 *position = start_pos;
4783 if (EQ (XCAR (spec), Qleft_fringe))
4785 it->left_user_fringe_bitmap = fringe_bitmap;
4786 it->left_user_fringe_face_id = face_id;
4788 else
4790 it->right_user_fringe_bitmap = fringe_bitmap;
4791 it->right_user_fringe_face_id = face_id;
4794 #endif /* HAVE_WINDOW_SYSTEM */
4795 return 1;
4798 /* Prepare to handle `((margin left-margin) ...)',
4799 `((margin right-margin) ...)' and `((margin nil) ...)'
4800 prefixes for display specifications. */
4801 location = Qunbound;
4802 if (CONSP (spec) && CONSP (XCAR (spec)))
4804 Lisp_Object tem;
4806 value = XCDR (spec);
4807 if (CONSP (value))
4808 value = XCAR (value);
4810 tem = XCAR (spec);
4811 if (EQ (XCAR (tem), Qmargin)
4812 && (tem = XCDR (tem),
4813 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4814 (NILP (tem)
4815 || EQ (tem, Qleft_margin)
4816 || EQ (tem, Qright_margin))))
4817 location = tem;
4820 if (EQ (location, Qunbound))
4822 location = Qnil;
4823 value = spec;
4826 /* After this point, VALUE is the property after any
4827 margin prefix has been stripped. It must be a string,
4828 an image specification, or `(space ...)'.
4830 LOCATION specifies where to display: `left-margin',
4831 `right-margin' or nil. */
4833 valid_p = (STRINGP (value)
4834 #ifdef HAVE_WINDOW_SYSTEM
4835 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4836 && valid_image_p (value))
4837 #endif /* not HAVE_WINDOW_SYSTEM */
4838 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4840 if (valid_p && !display_replaced_p)
4842 int retval = 1;
4844 if (!it)
4846 /* Callers need to know whether the display spec is any kind
4847 of `(space ...)' spec that is about to affect text-area
4848 display. */
4849 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4850 retval = 2;
4851 return retval;
4854 /* Save current settings of IT so that we can restore them
4855 when we are finished with the glyph property value. */
4856 push_it (it, position);
4857 it->from_overlay = overlay;
4858 it->from_disp_prop_p = 1;
4860 if (NILP (location))
4861 it->area = TEXT_AREA;
4862 else if (EQ (location, Qleft_margin))
4863 it->area = LEFT_MARGIN_AREA;
4864 else
4865 it->area = RIGHT_MARGIN_AREA;
4867 if (STRINGP (value))
4869 it->string = value;
4870 it->multibyte_p = STRING_MULTIBYTE (it->string);
4871 it->current.overlay_string_index = -1;
4872 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4873 it->end_charpos = it->string_nchars = SCHARS (it->string);
4874 it->method = GET_FROM_STRING;
4875 it->stop_charpos = 0;
4876 it->prev_stop = 0;
4877 it->base_level_stop = 0;
4878 it->string_from_display_prop_p = 1;
4879 /* Say that we haven't consumed the characters with
4880 `display' property yet. The call to pop_it in
4881 set_iterator_to_next will clean this up. */
4882 if (BUFFERP (object))
4883 *position = start_pos;
4885 /* Force paragraph direction to be that of the parent
4886 object. If the parent object's paragraph direction is
4887 not yet determined, default to L2R. */
4888 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4889 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4890 else
4891 it->paragraph_embedding = L2R;
4893 /* Set up the bidi iterator for this display string. */
4894 if (it->bidi_p)
4896 it->bidi_it.string.lstring = it->string;
4897 it->bidi_it.string.s = NULL;
4898 it->bidi_it.string.schars = it->end_charpos;
4899 it->bidi_it.string.bufpos = bufpos;
4900 it->bidi_it.string.from_disp_str = 1;
4901 it->bidi_it.string.unibyte = !it->multibyte_p;
4902 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4905 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4907 it->method = GET_FROM_STRETCH;
4908 it->object = value;
4909 *position = it->position = start_pos;
4910 retval = 1 + (it->area == TEXT_AREA);
4912 #ifdef HAVE_WINDOW_SYSTEM
4913 else
4915 it->what = IT_IMAGE;
4916 it->image_id = lookup_image (it->f, value);
4917 it->position = start_pos;
4918 it->object = NILP (object) ? it->w->buffer : object;
4919 it->method = GET_FROM_IMAGE;
4921 /* Say that we haven't consumed the characters with
4922 `display' property yet. The call to pop_it in
4923 set_iterator_to_next will clean this up. */
4924 *position = start_pos;
4926 #endif /* HAVE_WINDOW_SYSTEM */
4928 return retval;
4931 /* Invalid property or property not supported. Restore
4932 POSITION to what it was before. */
4933 *position = start_pos;
4934 return 0;
4937 /* Check if PROP is a display property value whose text should be
4938 treated as intangible. OVERLAY is the overlay from which PROP
4939 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4940 specify the buffer position covered by PROP. */
4943 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4944 EMACS_INT charpos, EMACS_INT bytepos)
4946 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4947 struct text_pos position;
4949 SET_TEXT_POS (position, charpos, bytepos);
4950 return handle_display_spec (NULL, prop, Qnil, overlay,
4951 &position, charpos, frame_window_p);
4955 /* Return 1 if PROP is a display sub-property value containing STRING.
4957 Implementation note: this and the following function are really
4958 special cases of handle_display_spec and
4959 handle_single_display_spec, and should ideally use the same code.
4960 Until they do, these two pairs must be consistent and must be
4961 modified in sync. */
4963 static int
4964 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4966 if (EQ (string, prop))
4967 return 1;
4969 /* Skip over `when FORM'. */
4970 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4972 prop = XCDR (prop);
4973 if (!CONSP (prop))
4974 return 0;
4975 /* Actually, the condition following `when' should be eval'ed,
4976 like handle_single_display_spec does, and we should return
4977 zero if it evaluates to nil. However, this function is
4978 called only when the buffer was already displayed and some
4979 glyph in the glyph matrix was found to come from a display
4980 string. Therefore, the condition was already evaluated, and
4981 the result was non-nil, otherwise the display string wouldn't
4982 have been displayed and we would have never been called for
4983 this property. Thus, we can skip the evaluation and assume
4984 its result is non-nil. */
4985 prop = XCDR (prop);
4988 if (CONSP (prop))
4989 /* Skip over `margin LOCATION'. */
4990 if (EQ (XCAR (prop), Qmargin))
4992 prop = XCDR (prop);
4993 if (!CONSP (prop))
4994 return 0;
4996 prop = XCDR (prop);
4997 if (!CONSP (prop))
4998 return 0;
5001 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5005 /* Return 1 if STRING appears in the `display' property PROP. */
5007 static int
5008 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5010 if (CONSP (prop)
5011 && !EQ (XCAR (prop), Qwhen)
5012 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5014 /* A list of sub-properties. */
5015 while (CONSP (prop))
5017 if (single_display_spec_string_p (XCAR (prop), string))
5018 return 1;
5019 prop = XCDR (prop);
5022 else if (VECTORP (prop))
5024 /* A vector of sub-properties. */
5025 int i;
5026 for (i = 0; i < ASIZE (prop); ++i)
5027 if (single_display_spec_string_p (AREF (prop, i), string))
5028 return 1;
5030 else
5031 return single_display_spec_string_p (prop, string);
5033 return 0;
5036 /* Look for STRING in overlays and text properties in the current
5037 buffer, between character positions FROM and TO (excluding TO).
5038 BACK_P non-zero means look back (in this case, TO is supposed to be
5039 less than FROM).
5040 Value is the first character position where STRING was found, or
5041 zero if it wasn't found before hitting TO.
5043 This function may only use code that doesn't eval because it is
5044 called asynchronously from note_mouse_highlight. */
5046 static EMACS_INT
5047 string_buffer_position_lim (Lisp_Object string,
5048 EMACS_INT from, EMACS_INT to, int back_p)
5050 Lisp_Object limit, prop, pos;
5051 int found = 0;
5053 pos = make_number (max (from, BEGV));
5055 if (!back_p) /* looking forward */
5057 limit = make_number (min (to, ZV));
5058 while (!found && !EQ (pos, limit))
5060 prop = Fget_char_property (pos, Qdisplay, Qnil);
5061 if (!NILP (prop) && display_prop_string_p (prop, string))
5062 found = 1;
5063 else
5064 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5065 limit);
5068 else /* looking back */
5070 limit = make_number (max (to, BEGV));
5071 while (!found && !EQ (pos, limit))
5073 prop = Fget_char_property (pos, Qdisplay, Qnil);
5074 if (!NILP (prop) && display_prop_string_p (prop, string))
5075 found = 1;
5076 else
5077 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5078 limit);
5082 return found ? XINT (pos) : 0;
5085 /* Determine which buffer position in current buffer STRING comes from.
5086 AROUND_CHARPOS is an approximate position where it could come from.
5087 Value is the buffer position or 0 if it couldn't be determined.
5089 This function is necessary because we don't record buffer positions
5090 in glyphs generated from strings (to keep struct glyph small).
5091 This function may only use code that doesn't eval because it is
5092 called asynchronously from note_mouse_highlight. */
5094 static EMACS_INT
5095 string_buffer_position (Lisp_Object string, EMACS_INT around_charpos)
5097 const int MAX_DISTANCE = 1000;
5098 EMACS_INT found = string_buffer_position_lim (string, around_charpos,
5099 around_charpos + MAX_DISTANCE,
5102 if (!found)
5103 found = string_buffer_position_lim (string, around_charpos,
5104 around_charpos - MAX_DISTANCE, 1);
5105 return found;
5110 /***********************************************************************
5111 `composition' property
5112 ***********************************************************************/
5114 /* Set up iterator IT from `composition' property at its current
5115 position. Called from handle_stop. */
5117 static enum prop_handled
5118 handle_composition_prop (struct it *it)
5120 Lisp_Object prop, string;
5121 EMACS_INT pos, pos_byte, start, end;
5123 if (STRINGP (it->string))
5125 unsigned char *s;
5127 pos = IT_STRING_CHARPOS (*it);
5128 pos_byte = IT_STRING_BYTEPOS (*it);
5129 string = it->string;
5130 s = SDATA (string) + pos_byte;
5131 it->c = STRING_CHAR (s);
5133 else
5135 pos = IT_CHARPOS (*it);
5136 pos_byte = IT_BYTEPOS (*it);
5137 string = Qnil;
5138 it->c = FETCH_CHAR (pos_byte);
5141 /* If there's a valid composition and point is not inside of the
5142 composition (in the case that the composition is from the current
5143 buffer), draw a glyph composed from the composition components. */
5144 if (find_composition (pos, -1, &start, &end, &prop, string)
5145 && COMPOSITION_VALID_P (start, end, prop)
5146 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5148 if (start < pos)
5149 /* As we can't handle this situation (perhaps font-lock added
5150 a new composition), we just return here hoping that next
5151 redisplay will detect this composition much earlier. */
5152 return HANDLED_NORMALLY;
5153 if (start != pos)
5155 if (STRINGP (it->string))
5156 pos_byte = string_char_to_byte (it->string, start);
5157 else
5158 pos_byte = CHAR_TO_BYTE (start);
5160 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5161 prop, string);
5163 if (it->cmp_it.id >= 0)
5165 it->cmp_it.ch = -1;
5166 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5167 it->cmp_it.nglyphs = -1;
5171 return HANDLED_NORMALLY;
5176 /***********************************************************************
5177 Overlay strings
5178 ***********************************************************************/
5180 /* The following structure is used to record overlay strings for
5181 later sorting in load_overlay_strings. */
5183 struct overlay_entry
5185 Lisp_Object overlay;
5186 Lisp_Object string;
5187 int priority;
5188 int after_string_p;
5192 /* Set up iterator IT from overlay strings at its current position.
5193 Called from handle_stop. */
5195 static enum prop_handled
5196 handle_overlay_change (struct it *it)
5198 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5199 return HANDLED_RECOMPUTE_PROPS;
5200 else
5201 return HANDLED_NORMALLY;
5205 /* Set up the next overlay string for delivery by IT, if there is an
5206 overlay string to deliver. Called by set_iterator_to_next when the
5207 end of the current overlay string is reached. If there are more
5208 overlay strings to display, IT->string and
5209 IT->current.overlay_string_index are set appropriately here.
5210 Otherwise IT->string is set to nil. */
5212 static void
5213 next_overlay_string (struct it *it)
5215 ++it->current.overlay_string_index;
5216 if (it->current.overlay_string_index == it->n_overlay_strings)
5218 /* No more overlay strings. Restore IT's settings to what
5219 they were before overlay strings were processed, and
5220 continue to deliver from current_buffer. */
5222 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5223 pop_it (it);
5224 xassert (it->sp > 0
5225 || (NILP (it->string)
5226 && it->method == GET_FROM_BUFFER
5227 && it->stop_charpos >= BEGV
5228 && it->stop_charpos <= it->end_charpos));
5229 it->current.overlay_string_index = -1;
5230 it->n_overlay_strings = 0;
5231 it->overlay_strings_charpos = -1;
5232 /* If there's an empty display string on the stack, pop the
5233 stack, to resync the bidi iterator with IT's position. Such
5234 empty strings are pushed onto the stack in
5235 get_overlay_strings_1. */
5236 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5237 pop_it (it);
5239 /* If we're at the end of the buffer, record that we have
5240 processed the overlay strings there already, so that
5241 next_element_from_buffer doesn't try it again. */
5242 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5243 it->overlay_strings_at_end_processed_p = 1;
5245 else
5247 /* There are more overlay strings to process. If
5248 IT->current.overlay_string_index has advanced to a position
5249 where we must load IT->overlay_strings with more strings, do
5250 it. We must load at the IT->overlay_strings_charpos where
5251 IT->n_overlay_strings was originally computed; when invisible
5252 text is present, this might not be IT_CHARPOS (Bug#7016). */
5253 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5255 if (it->current.overlay_string_index && i == 0)
5256 load_overlay_strings (it, it->overlay_strings_charpos);
5258 /* Initialize IT to deliver display elements from the overlay
5259 string. */
5260 it->string = it->overlay_strings[i];
5261 it->multibyte_p = STRING_MULTIBYTE (it->string);
5262 SET_TEXT_POS (it->current.string_pos, 0, 0);
5263 it->method = GET_FROM_STRING;
5264 it->stop_charpos = 0;
5265 if (it->cmp_it.stop_pos >= 0)
5266 it->cmp_it.stop_pos = 0;
5267 it->prev_stop = 0;
5268 it->base_level_stop = 0;
5270 /* Set up the bidi iterator for this overlay string. */
5271 if (it->bidi_p)
5273 it->bidi_it.string.lstring = it->string;
5274 it->bidi_it.string.s = NULL;
5275 it->bidi_it.string.schars = SCHARS (it->string);
5276 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5277 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5278 it->bidi_it.string.unibyte = !it->multibyte_p;
5279 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5283 CHECK_IT (it);
5287 /* Compare two overlay_entry structures E1 and E2. Used as a
5288 comparison function for qsort in load_overlay_strings. Overlay
5289 strings for the same position are sorted so that
5291 1. All after-strings come in front of before-strings, except
5292 when they come from the same overlay.
5294 2. Within after-strings, strings are sorted so that overlay strings
5295 from overlays with higher priorities come first.
5297 2. Within before-strings, strings are sorted so that overlay
5298 strings from overlays with higher priorities come last.
5300 Value is analogous to strcmp. */
5303 static int
5304 compare_overlay_entries (const void *e1, const void *e2)
5306 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5307 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5308 int result;
5310 if (entry1->after_string_p != entry2->after_string_p)
5312 /* Let after-strings appear in front of before-strings if
5313 they come from different overlays. */
5314 if (EQ (entry1->overlay, entry2->overlay))
5315 result = entry1->after_string_p ? 1 : -1;
5316 else
5317 result = entry1->after_string_p ? -1 : 1;
5319 else if (entry1->after_string_p)
5320 /* After-strings sorted in order of decreasing priority. */
5321 result = entry2->priority - entry1->priority;
5322 else
5323 /* Before-strings sorted in order of increasing priority. */
5324 result = entry1->priority - entry2->priority;
5326 return result;
5330 /* Load the vector IT->overlay_strings with overlay strings from IT's
5331 current buffer position, or from CHARPOS if that is > 0. Set
5332 IT->n_overlays to the total number of overlay strings found.
5334 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5335 a time. On entry into load_overlay_strings,
5336 IT->current.overlay_string_index gives the number of overlay
5337 strings that have already been loaded by previous calls to this
5338 function.
5340 IT->add_overlay_start contains an additional overlay start
5341 position to consider for taking overlay strings from, if non-zero.
5342 This position comes into play when the overlay has an `invisible'
5343 property, and both before and after-strings. When we've skipped to
5344 the end of the overlay, because of its `invisible' property, we
5345 nevertheless want its before-string to appear.
5346 IT->add_overlay_start will contain the overlay start position
5347 in this case.
5349 Overlay strings are sorted so that after-string strings come in
5350 front of before-string strings. Within before and after-strings,
5351 strings are sorted by overlay priority. See also function
5352 compare_overlay_entries. */
5354 static void
5355 load_overlay_strings (struct it *it, EMACS_INT charpos)
5357 Lisp_Object overlay, window, str, invisible;
5358 struct Lisp_Overlay *ov;
5359 EMACS_INT start, end;
5360 int size = 20;
5361 int n = 0, i, j, invis_p;
5362 struct overlay_entry *entries
5363 = (struct overlay_entry *) alloca (size * sizeof *entries);
5365 if (charpos <= 0)
5366 charpos = IT_CHARPOS (*it);
5368 /* Append the overlay string STRING of overlay OVERLAY to vector
5369 `entries' which has size `size' and currently contains `n'
5370 elements. AFTER_P non-zero means STRING is an after-string of
5371 OVERLAY. */
5372 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5373 do \
5375 Lisp_Object priority; \
5377 if (n == size) \
5379 int new_size = 2 * size; \
5380 struct overlay_entry *old = entries; \
5381 entries = \
5382 (struct overlay_entry *) alloca (new_size \
5383 * sizeof *entries); \
5384 memcpy (entries, old, size * sizeof *entries); \
5385 size = new_size; \
5388 entries[n].string = (STRING); \
5389 entries[n].overlay = (OVERLAY); \
5390 priority = Foverlay_get ((OVERLAY), Qpriority); \
5391 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5392 entries[n].after_string_p = (AFTER_P); \
5393 ++n; \
5395 while (0)
5397 /* Process overlay before the overlay center. */
5398 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5400 XSETMISC (overlay, ov);
5401 xassert (OVERLAYP (overlay));
5402 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5403 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5405 if (end < charpos)
5406 break;
5408 /* Skip this overlay if it doesn't start or end at IT's current
5409 position. */
5410 if (end != charpos && start != charpos)
5411 continue;
5413 /* Skip this overlay if it doesn't apply to IT->w. */
5414 window = Foverlay_get (overlay, Qwindow);
5415 if (WINDOWP (window) && XWINDOW (window) != it->w)
5416 continue;
5418 /* If the text ``under'' the overlay is invisible, both before-
5419 and after-strings from this overlay are visible; start and
5420 end position are indistinguishable. */
5421 invisible = Foverlay_get (overlay, Qinvisible);
5422 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5424 /* If overlay has a non-empty before-string, record it. */
5425 if ((start == charpos || (end == charpos && invis_p))
5426 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5427 && SCHARS (str))
5428 RECORD_OVERLAY_STRING (overlay, str, 0);
5430 /* If overlay has a non-empty after-string, record it. */
5431 if ((end == charpos || (start == charpos && invis_p))
5432 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5433 && SCHARS (str))
5434 RECORD_OVERLAY_STRING (overlay, str, 1);
5437 /* Process overlays after the overlay center. */
5438 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5440 XSETMISC (overlay, ov);
5441 xassert (OVERLAYP (overlay));
5442 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5443 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5445 if (start > charpos)
5446 break;
5448 /* Skip this overlay if it doesn't start or end at IT's current
5449 position. */
5450 if (end != charpos && start != charpos)
5451 continue;
5453 /* Skip this overlay if it doesn't apply to IT->w. */
5454 window = Foverlay_get (overlay, Qwindow);
5455 if (WINDOWP (window) && XWINDOW (window) != it->w)
5456 continue;
5458 /* If the text ``under'' the overlay is invisible, it has a zero
5459 dimension, and both before- and after-strings apply. */
5460 invisible = Foverlay_get (overlay, Qinvisible);
5461 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5463 /* If overlay has a non-empty before-string, record it. */
5464 if ((start == charpos || (end == charpos && invis_p))
5465 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5466 && SCHARS (str))
5467 RECORD_OVERLAY_STRING (overlay, str, 0);
5469 /* If overlay has a non-empty after-string, record it. */
5470 if ((end == charpos || (start == charpos && invis_p))
5471 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5472 && SCHARS (str))
5473 RECORD_OVERLAY_STRING (overlay, str, 1);
5476 #undef RECORD_OVERLAY_STRING
5478 /* Sort entries. */
5479 if (n > 1)
5480 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5482 /* Record number of overlay strings, and where we computed it. */
5483 it->n_overlay_strings = n;
5484 it->overlay_strings_charpos = charpos;
5486 /* IT->current.overlay_string_index is the number of overlay strings
5487 that have already been consumed by IT. Copy some of the
5488 remaining overlay strings to IT->overlay_strings. */
5489 i = 0;
5490 j = it->current.overlay_string_index;
5491 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5493 it->overlay_strings[i] = entries[j].string;
5494 it->string_overlays[i++] = entries[j++].overlay;
5497 CHECK_IT (it);
5501 /* Get the first chunk of overlay strings at IT's current buffer
5502 position, or at CHARPOS if that is > 0. Value is non-zero if at
5503 least one overlay string was found. */
5505 static int
5506 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
5508 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5509 process. This fills IT->overlay_strings with strings, and sets
5510 IT->n_overlay_strings to the total number of strings to process.
5511 IT->pos.overlay_string_index has to be set temporarily to zero
5512 because load_overlay_strings needs this; it must be set to -1
5513 when no overlay strings are found because a zero value would
5514 indicate a position in the first overlay string. */
5515 it->current.overlay_string_index = 0;
5516 load_overlay_strings (it, charpos);
5518 /* If we found overlay strings, set up IT to deliver display
5519 elements from the first one. Otherwise set up IT to deliver
5520 from current_buffer. */
5521 if (it->n_overlay_strings)
5523 /* Make sure we know settings in current_buffer, so that we can
5524 restore meaningful values when we're done with the overlay
5525 strings. */
5526 if (compute_stop_p)
5527 compute_stop_pos (it);
5528 xassert (it->face_id >= 0);
5530 /* Save IT's settings. They are restored after all overlay
5531 strings have been processed. */
5532 xassert (!compute_stop_p || it->sp == 0);
5534 /* When called from handle_stop, there might be an empty display
5535 string loaded. In that case, don't bother saving it. But
5536 don't use this optimization with the bidi iterator, since we
5537 need the corresponding pop_it call to resync the bidi
5538 iterator's position with IT's position, after we are done
5539 with the overlay strings. (The corresponding call to pop_it
5540 in case of an empty display string is in
5541 next_overlay_string.) */
5542 if (!(!it->bidi_p
5543 && STRINGP (it->string) && !SCHARS (it->string)))
5544 push_it (it, NULL);
5546 /* Set up IT to deliver display elements from the first overlay
5547 string. */
5548 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5549 it->string = it->overlay_strings[0];
5550 it->from_overlay = Qnil;
5551 it->stop_charpos = 0;
5552 xassert (STRINGP (it->string));
5553 it->end_charpos = SCHARS (it->string);
5554 it->prev_stop = 0;
5555 it->base_level_stop = 0;
5556 it->multibyte_p = STRING_MULTIBYTE (it->string);
5557 it->method = GET_FROM_STRING;
5558 it->from_disp_prop_p = 0;
5560 /* Force paragraph direction to be that of the parent
5561 buffer. */
5562 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5563 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5564 else
5565 it->paragraph_embedding = L2R;
5567 /* Set up the bidi iterator for this overlay string. */
5568 if (it->bidi_p)
5570 EMACS_INT pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5572 it->bidi_it.string.lstring = it->string;
5573 it->bidi_it.string.s = NULL;
5574 it->bidi_it.string.schars = SCHARS (it->string);
5575 it->bidi_it.string.bufpos = pos;
5576 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5577 it->bidi_it.string.unibyte = !it->multibyte_p;
5578 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5580 return 1;
5583 it->current.overlay_string_index = -1;
5584 return 0;
5587 static int
5588 get_overlay_strings (struct it *it, EMACS_INT charpos)
5590 it->string = Qnil;
5591 it->method = GET_FROM_BUFFER;
5593 (void) get_overlay_strings_1 (it, charpos, 1);
5595 CHECK_IT (it);
5597 /* Value is non-zero if we found at least one overlay string. */
5598 return STRINGP (it->string);
5603 /***********************************************************************
5604 Saving and restoring state
5605 ***********************************************************************/
5607 /* Save current settings of IT on IT->stack. Called, for example,
5608 before setting up IT for an overlay string, to be able to restore
5609 IT's settings to what they were after the overlay string has been
5610 processed. If POSITION is non-NULL, it is the position to save on
5611 the stack instead of IT->position. */
5613 static void
5614 push_it (struct it *it, struct text_pos *position)
5616 struct iterator_stack_entry *p;
5618 xassert (it->sp < IT_STACK_SIZE);
5619 p = it->stack + it->sp;
5621 p->stop_charpos = it->stop_charpos;
5622 p->prev_stop = it->prev_stop;
5623 p->base_level_stop = it->base_level_stop;
5624 p->cmp_it = it->cmp_it;
5625 xassert (it->face_id >= 0);
5626 p->face_id = it->face_id;
5627 p->string = it->string;
5628 p->method = it->method;
5629 p->from_overlay = it->from_overlay;
5630 switch (p->method)
5632 case GET_FROM_IMAGE:
5633 p->u.image.object = it->object;
5634 p->u.image.image_id = it->image_id;
5635 p->u.image.slice = it->slice;
5636 break;
5637 case GET_FROM_STRETCH:
5638 p->u.stretch.object = it->object;
5639 break;
5641 p->position = position ? *position : it->position;
5642 p->current = it->current;
5643 p->end_charpos = it->end_charpos;
5644 p->string_nchars = it->string_nchars;
5645 p->area = it->area;
5646 p->multibyte_p = it->multibyte_p;
5647 p->avoid_cursor_p = it->avoid_cursor_p;
5648 p->space_width = it->space_width;
5649 p->font_height = it->font_height;
5650 p->voffset = it->voffset;
5651 p->string_from_display_prop_p = it->string_from_display_prop_p;
5652 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5653 p->display_ellipsis_p = 0;
5654 p->line_wrap = it->line_wrap;
5655 p->bidi_p = it->bidi_p;
5656 p->paragraph_embedding = it->paragraph_embedding;
5657 p->from_disp_prop_p = it->from_disp_prop_p;
5658 ++it->sp;
5660 /* Save the state of the bidi iterator as well. */
5661 if (it->bidi_p)
5662 bidi_push_it (&it->bidi_it);
5665 static void
5666 iterate_out_of_display_property (struct it *it)
5668 int buffer_p = !STRINGP (it->string);
5669 EMACS_INT eob = (buffer_p ? ZV : it->end_charpos);
5670 EMACS_INT bob = (buffer_p ? BEGV : 0);
5672 xassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5674 /* Maybe initialize paragraph direction. If we are at the beginning
5675 of a new paragraph, next_element_from_buffer may not have a
5676 chance to do that. */
5677 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5678 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5679 /* prev_stop can be zero, so check against BEGV as well. */
5680 while (it->bidi_it.charpos >= bob
5681 && it->prev_stop <= it->bidi_it.charpos
5682 && it->bidi_it.charpos < CHARPOS (it->position)
5683 && it->bidi_it.charpos < eob)
5684 bidi_move_to_visually_next (&it->bidi_it);
5685 /* Record the stop_pos we just crossed, for when we cross it
5686 back, maybe. */
5687 if (it->bidi_it.charpos > CHARPOS (it->position))
5688 it->prev_stop = CHARPOS (it->position);
5689 /* If we ended up not where pop_it put us, resync IT's
5690 positional members with the bidi iterator. */
5691 if (it->bidi_it.charpos != CHARPOS (it->position))
5692 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5693 if (buffer_p)
5694 it->current.pos = it->position;
5695 else
5696 it->current.string_pos = it->position;
5699 /* Restore IT's settings from IT->stack. Called, for example, when no
5700 more overlay strings must be processed, and we return to delivering
5701 display elements from a buffer, or when the end of a string from a
5702 `display' property is reached and we return to delivering display
5703 elements from an overlay string, or from a buffer. */
5705 static void
5706 pop_it (struct it *it)
5708 struct iterator_stack_entry *p;
5709 int from_display_prop = it->from_disp_prop_p;
5711 xassert (it->sp > 0);
5712 --it->sp;
5713 p = it->stack + it->sp;
5714 it->stop_charpos = p->stop_charpos;
5715 it->prev_stop = p->prev_stop;
5716 it->base_level_stop = p->base_level_stop;
5717 it->cmp_it = p->cmp_it;
5718 it->face_id = p->face_id;
5719 it->current = p->current;
5720 it->position = p->position;
5721 it->string = p->string;
5722 it->from_overlay = p->from_overlay;
5723 if (NILP (it->string))
5724 SET_TEXT_POS (it->current.string_pos, -1, -1);
5725 it->method = p->method;
5726 switch (it->method)
5728 case GET_FROM_IMAGE:
5729 it->image_id = p->u.image.image_id;
5730 it->object = p->u.image.object;
5731 it->slice = p->u.image.slice;
5732 break;
5733 case GET_FROM_STRETCH:
5734 it->object = p->u.stretch.object;
5735 break;
5736 case GET_FROM_BUFFER:
5737 it->object = it->w->buffer;
5738 break;
5739 case GET_FROM_STRING:
5740 it->object = it->string;
5741 break;
5742 case GET_FROM_DISPLAY_VECTOR:
5743 if (it->s)
5744 it->method = GET_FROM_C_STRING;
5745 else if (STRINGP (it->string))
5746 it->method = GET_FROM_STRING;
5747 else
5749 it->method = GET_FROM_BUFFER;
5750 it->object = it->w->buffer;
5753 it->end_charpos = p->end_charpos;
5754 it->string_nchars = p->string_nchars;
5755 it->area = p->area;
5756 it->multibyte_p = p->multibyte_p;
5757 it->avoid_cursor_p = p->avoid_cursor_p;
5758 it->space_width = p->space_width;
5759 it->font_height = p->font_height;
5760 it->voffset = p->voffset;
5761 it->string_from_display_prop_p = p->string_from_display_prop_p;
5762 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5763 it->line_wrap = p->line_wrap;
5764 it->bidi_p = p->bidi_p;
5765 it->paragraph_embedding = p->paragraph_embedding;
5766 it->from_disp_prop_p = p->from_disp_prop_p;
5767 if (it->bidi_p)
5769 bidi_pop_it (&it->bidi_it);
5770 /* Bidi-iterate until we get out of the portion of text, if any,
5771 covered by a `display' text property or by an overlay with
5772 `display' property. (We cannot just jump there, because the
5773 internal coherency of the bidi iterator state can not be
5774 preserved across such jumps.) We also must determine the
5775 paragraph base direction if the overlay we just processed is
5776 at the beginning of a new paragraph. */
5777 if (from_display_prop
5778 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5779 iterate_out_of_display_property (it);
5781 xassert ((BUFFERP (it->object)
5782 && IT_CHARPOS (*it) == it->bidi_it.charpos
5783 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5784 || (STRINGP (it->object)
5785 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5786 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5787 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5793 /***********************************************************************
5794 Moving over lines
5795 ***********************************************************************/
5797 /* Set IT's current position to the previous line start. */
5799 static void
5800 back_to_previous_line_start (struct it *it)
5802 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5803 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5807 /* Move IT to the next line start.
5809 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5810 we skipped over part of the text (as opposed to moving the iterator
5811 continuously over the text). Otherwise, don't change the value
5812 of *SKIPPED_P.
5814 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5815 iterator on the newline, if it was found.
5817 Newlines may come from buffer text, overlay strings, or strings
5818 displayed via the `display' property. That's the reason we can't
5819 simply use find_next_newline_no_quit.
5821 Note that this function may not skip over invisible text that is so
5822 because of text properties and immediately follows a newline. If
5823 it would, function reseat_at_next_visible_line_start, when called
5824 from set_iterator_to_next, would effectively make invisible
5825 characters following a newline part of the wrong glyph row, which
5826 leads to wrong cursor motion. */
5828 static int
5829 forward_to_next_line_start (struct it *it, int *skipped_p,
5830 struct bidi_it *bidi_it_prev)
5832 EMACS_INT old_selective;
5833 int newline_found_p, n;
5834 const int MAX_NEWLINE_DISTANCE = 500;
5836 /* If already on a newline, just consume it to avoid unintended
5837 skipping over invisible text below. */
5838 if (it->what == IT_CHARACTER
5839 && it->c == '\n'
5840 && CHARPOS (it->position) == IT_CHARPOS (*it))
5842 if (it->bidi_p && bidi_it_prev)
5843 *bidi_it_prev = it->bidi_it;
5844 set_iterator_to_next (it, 0);
5845 it->c = 0;
5846 return 1;
5849 /* Don't handle selective display in the following. It's (a)
5850 unnecessary because it's done by the caller, and (b) leads to an
5851 infinite recursion because next_element_from_ellipsis indirectly
5852 calls this function. */
5853 old_selective = it->selective;
5854 it->selective = 0;
5856 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5857 from buffer text. */
5858 for (n = newline_found_p = 0;
5859 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5860 n += STRINGP (it->string) ? 0 : 1)
5862 if (!get_next_display_element (it))
5863 return 0;
5864 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5865 if (newline_found_p && it->bidi_p && bidi_it_prev)
5866 *bidi_it_prev = it->bidi_it;
5867 set_iterator_to_next (it, 0);
5870 /* If we didn't find a newline near enough, see if we can use a
5871 short-cut. */
5872 if (!newline_found_p)
5874 EMACS_INT start = IT_CHARPOS (*it);
5875 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5876 Lisp_Object pos;
5878 xassert (!STRINGP (it->string));
5880 /* If there isn't any `display' property in sight, and no
5881 overlays, we can just use the position of the newline in
5882 buffer text. */
5883 if (it->stop_charpos >= limit
5884 || ((pos = Fnext_single_property_change (make_number (start),
5885 Qdisplay, Qnil,
5886 make_number (limit)),
5887 NILP (pos))
5888 && next_overlay_change (start) == ZV))
5890 if (!it->bidi_p)
5892 IT_CHARPOS (*it) = limit;
5893 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5895 else
5897 struct bidi_it bprev;
5899 /* Help bidi.c avoid expensive searches for display
5900 properties and overlays, by telling it that there are
5901 none up to `limit'. */
5902 if (it->bidi_it.disp_pos < limit)
5904 it->bidi_it.disp_pos = limit;
5905 it->bidi_it.disp_prop = 0;
5907 do {
5908 bprev = it->bidi_it;
5909 bidi_move_to_visually_next (&it->bidi_it);
5910 } while (it->bidi_it.charpos != limit);
5911 IT_CHARPOS (*it) = limit;
5912 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5913 if (bidi_it_prev)
5914 *bidi_it_prev = bprev;
5916 *skipped_p = newline_found_p = 1;
5918 else
5920 while (get_next_display_element (it)
5921 && !newline_found_p)
5923 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5924 if (newline_found_p && it->bidi_p && bidi_it_prev)
5925 *bidi_it_prev = it->bidi_it;
5926 set_iterator_to_next (it, 0);
5931 it->selective = old_selective;
5932 return newline_found_p;
5936 /* Set IT's current position to the previous visible line start. Skip
5937 invisible text that is so either due to text properties or due to
5938 selective display. Caution: this does not change IT->current_x and
5939 IT->hpos. */
5941 static void
5942 back_to_previous_visible_line_start (struct it *it)
5944 while (IT_CHARPOS (*it) > BEGV)
5946 back_to_previous_line_start (it);
5948 if (IT_CHARPOS (*it) <= BEGV)
5949 break;
5951 /* If selective > 0, then lines indented more than its value are
5952 invisible. */
5953 if (it->selective > 0
5954 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5955 it->selective))
5956 continue;
5958 /* Check the newline before point for invisibility. */
5960 Lisp_Object prop;
5961 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5962 Qinvisible, it->window);
5963 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5964 continue;
5967 if (IT_CHARPOS (*it) <= BEGV)
5968 break;
5971 struct it it2;
5972 void *it2data = NULL;
5973 EMACS_INT pos;
5974 EMACS_INT beg, end;
5975 Lisp_Object val, overlay;
5977 SAVE_IT (it2, *it, it2data);
5979 /* If newline is part of a composition, continue from start of composition */
5980 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5981 && beg < IT_CHARPOS (*it))
5982 goto replaced;
5984 /* If newline is replaced by a display property, find start of overlay
5985 or interval and continue search from that point. */
5986 pos = --IT_CHARPOS (it2);
5987 --IT_BYTEPOS (it2);
5988 it2.sp = 0;
5989 bidi_unshelve_cache (NULL, 0);
5990 it2.string_from_display_prop_p = 0;
5991 it2.from_disp_prop_p = 0;
5992 if (handle_display_prop (&it2) == HANDLED_RETURN
5993 && !NILP (val = get_char_property_and_overlay
5994 (make_number (pos), Qdisplay, Qnil, &overlay))
5995 && (OVERLAYP (overlay)
5996 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5997 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5999 RESTORE_IT (it, it, it2data);
6000 goto replaced;
6003 /* Newline is not replaced by anything -- so we are done. */
6004 RESTORE_IT (it, it, it2data);
6005 break;
6007 replaced:
6008 if (beg < BEGV)
6009 beg = BEGV;
6010 IT_CHARPOS (*it) = beg;
6011 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6015 it->continuation_lines_width = 0;
6017 xassert (IT_CHARPOS (*it) >= BEGV);
6018 xassert (IT_CHARPOS (*it) == BEGV
6019 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6020 CHECK_IT (it);
6024 /* Reseat iterator IT at the previous visible line start. Skip
6025 invisible text that is so either due to text properties or due to
6026 selective display. At the end, update IT's overlay information,
6027 face information etc. */
6029 void
6030 reseat_at_previous_visible_line_start (struct it *it)
6032 back_to_previous_visible_line_start (it);
6033 reseat (it, it->current.pos, 1);
6034 CHECK_IT (it);
6038 /* Reseat iterator IT on the next visible line start in the current
6039 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6040 preceding the line start. Skip over invisible text that is so
6041 because of selective display. Compute faces, overlays etc at the
6042 new position. Note that this function does not skip over text that
6043 is invisible because of text properties. */
6045 static void
6046 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6048 int newline_found_p, skipped_p = 0;
6049 struct bidi_it bidi_it_prev;
6051 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6053 /* Skip over lines that are invisible because they are indented
6054 more than the value of IT->selective. */
6055 if (it->selective > 0)
6056 while (IT_CHARPOS (*it) < ZV
6057 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6058 it->selective))
6060 xassert (IT_BYTEPOS (*it) == BEGV
6061 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6062 newline_found_p =
6063 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6066 /* Position on the newline if that's what's requested. */
6067 if (on_newline_p && newline_found_p)
6069 if (STRINGP (it->string))
6071 if (IT_STRING_CHARPOS (*it) > 0)
6073 if (!it->bidi_p)
6075 --IT_STRING_CHARPOS (*it);
6076 --IT_STRING_BYTEPOS (*it);
6078 else
6080 /* We need to restore the bidi iterator to the state
6081 it had on the newline, and resync the IT's
6082 position with that. */
6083 it->bidi_it = bidi_it_prev;
6084 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6085 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6089 else if (IT_CHARPOS (*it) > BEGV)
6091 if (!it->bidi_p)
6093 --IT_CHARPOS (*it);
6094 --IT_BYTEPOS (*it);
6096 else
6098 /* We need to restore the bidi iterator to the state it
6099 had on the newline and resync IT with that. */
6100 it->bidi_it = bidi_it_prev;
6101 IT_CHARPOS (*it) = it->bidi_it.charpos;
6102 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6104 reseat (it, it->current.pos, 0);
6107 else if (skipped_p)
6108 reseat (it, it->current.pos, 0);
6110 CHECK_IT (it);
6115 /***********************************************************************
6116 Changing an iterator's position
6117 ***********************************************************************/
6119 /* Change IT's current position to POS in current_buffer. If FORCE_P
6120 is non-zero, always check for text properties at the new position.
6121 Otherwise, text properties are only looked up if POS >=
6122 IT->check_charpos of a property. */
6124 static void
6125 reseat (struct it *it, struct text_pos pos, int force_p)
6127 EMACS_INT original_pos = IT_CHARPOS (*it);
6129 reseat_1 (it, pos, 0);
6131 /* Determine where to check text properties. Avoid doing it
6132 where possible because text property lookup is very expensive. */
6133 if (force_p
6134 || CHARPOS (pos) > it->stop_charpos
6135 || CHARPOS (pos) < original_pos)
6137 if (it->bidi_p)
6139 /* For bidi iteration, we need to prime prev_stop and
6140 base_level_stop with our best estimations. */
6141 /* Implementation note: Of course, POS is not necessarily a
6142 stop position, so assigning prev_pos to it is a lie; we
6143 should have called compute_stop_backwards. However, if
6144 the current buffer does not include any R2L characters,
6145 that call would be a waste of cycles, because the
6146 iterator will never move back, and thus never cross this
6147 "fake" stop position. So we delay that backward search
6148 until the time we really need it, in next_element_from_buffer. */
6149 if (CHARPOS (pos) != it->prev_stop)
6150 it->prev_stop = CHARPOS (pos);
6151 if (CHARPOS (pos) < it->base_level_stop)
6152 it->base_level_stop = 0; /* meaning it's unknown */
6153 handle_stop (it);
6155 else
6157 handle_stop (it);
6158 it->prev_stop = it->base_level_stop = 0;
6163 CHECK_IT (it);
6167 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6168 IT->stop_pos to POS, also. */
6170 static void
6171 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6173 /* Don't call this function when scanning a C string. */
6174 xassert (it->s == NULL);
6176 /* POS must be a reasonable value. */
6177 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6179 it->current.pos = it->position = pos;
6180 it->end_charpos = ZV;
6181 it->dpvec = NULL;
6182 it->current.dpvec_index = -1;
6183 it->current.overlay_string_index = -1;
6184 IT_STRING_CHARPOS (*it) = -1;
6185 IT_STRING_BYTEPOS (*it) = -1;
6186 it->string = Qnil;
6187 it->method = GET_FROM_BUFFER;
6188 it->object = it->w->buffer;
6189 it->area = TEXT_AREA;
6190 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6191 it->sp = 0;
6192 it->string_from_display_prop_p = 0;
6193 it->string_from_prefix_prop_p = 0;
6195 it->from_disp_prop_p = 0;
6196 it->face_before_selective_p = 0;
6197 if (it->bidi_p)
6199 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6200 &it->bidi_it);
6201 bidi_unshelve_cache (NULL, 0);
6202 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6203 it->bidi_it.string.s = NULL;
6204 it->bidi_it.string.lstring = Qnil;
6205 it->bidi_it.string.bufpos = 0;
6206 it->bidi_it.string.unibyte = 0;
6209 if (set_stop_p)
6211 it->stop_charpos = CHARPOS (pos);
6212 it->base_level_stop = CHARPOS (pos);
6217 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6218 If S is non-null, it is a C string to iterate over. Otherwise,
6219 STRING gives a Lisp string to iterate over.
6221 If PRECISION > 0, don't return more then PRECISION number of
6222 characters from the string.
6224 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6225 characters have been returned. FIELD_WIDTH < 0 means an infinite
6226 field width.
6228 MULTIBYTE = 0 means disable processing of multibyte characters,
6229 MULTIBYTE > 0 means enable it,
6230 MULTIBYTE < 0 means use IT->multibyte_p.
6232 IT must be initialized via a prior call to init_iterator before
6233 calling this function. */
6235 static void
6236 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6237 EMACS_INT charpos, EMACS_INT precision, int field_width,
6238 int multibyte)
6240 /* No region in strings. */
6241 it->region_beg_charpos = it->region_end_charpos = -1;
6243 /* No text property checks performed by default, but see below. */
6244 it->stop_charpos = -1;
6246 /* Set iterator position and end position. */
6247 memset (&it->current, 0, sizeof it->current);
6248 it->current.overlay_string_index = -1;
6249 it->current.dpvec_index = -1;
6250 xassert (charpos >= 0);
6252 /* If STRING is specified, use its multibyteness, otherwise use the
6253 setting of MULTIBYTE, if specified. */
6254 if (multibyte >= 0)
6255 it->multibyte_p = multibyte > 0;
6257 /* Bidirectional reordering of strings is controlled by the default
6258 value of bidi-display-reordering. Don't try to reorder while
6259 loading loadup.el, as the necessary character property tables are
6260 not yet available. */
6261 it->bidi_p =
6262 NILP (Vpurify_flag)
6263 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6265 if (s == NULL)
6267 xassert (STRINGP (string));
6268 it->string = string;
6269 it->s = NULL;
6270 it->end_charpos = it->string_nchars = SCHARS (string);
6271 it->method = GET_FROM_STRING;
6272 it->current.string_pos = string_pos (charpos, string);
6274 if (it->bidi_p)
6276 it->bidi_it.string.lstring = string;
6277 it->bidi_it.string.s = NULL;
6278 it->bidi_it.string.schars = it->end_charpos;
6279 it->bidi_it.string.bufpos = 0;
6280 it->bidi_it.string.from_disp_str = 0;
6281 it->bidi_it.string.unibyte = !it->multibyte_p;
6282 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6283 FRAME_WINDOW_P (it->f), &it->bidi_it);
6286 else
6288 it->s = (const unsigned char *) s;
6289 it->string = Qnil;
6291 /* Note that we use IT->current.pos, not it->current.string_pos,
6292 for displaying C strings. */
6293 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6294 if (it->multibyte_p)
6296 it->current.pos = c_string_pos (charpos, s, 1);
6297 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6299 else
6301 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6302 it->end_charpos = it->string_nchars = strlen (s);
6305 if (it->bidi_p)
6307 it->bidi_it.string.lstring = Qnil;
6308 it->bidi_it.string.s = (const unsigned char *) s;
6309 it->bidi_it.string.schars = it->end_charpos;
6310 it->bidi_it.string.bufpos = 0;
6311 it->bidi_it.string.from_disp_str = 0;
6312 it->bidi_it.string.unibyte = !it->multibyte_p;
6313 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6314 &it->bidi_it);
6316 it->method = GET_FROM_C_STRING;
6319 /* PRECISION > 0 means don't return more than PRECISION characters
6320 from the string. */
6321 if (precision > 0 && it->end_charpos - charpos > precision)
6323 it->end_charpos = it->string_nchars = charpos + precision;
6324 if (it->bidi_p)
6325 it->bidi_it.string.schars = it->end_charpos;
6328 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6329 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6330 FIELD_WIDTH < 0 means infinite field width. This is useful for
6331 padding with `-' at the end of a mode line. */
6332 if (field_width < 0)
6333 field_width = INFINITY;
6334 /* Implementation note: We deliberately don't enlarge
6335 it->bidi_it.string.schars here to fit it->end_charpos, because
6336 the bidi iterator cannot produce characters out of thin air. */
6337 if (field_width > it->end_charpos - charpos)
6338 it->end_charpos = charpos + field_width;
6340 /* Use the standard display table for displaying strings. */
6341 if (DISP_TABLE_P (Vstandard_display_table))
6342 it->dp = XCHAR_TABLE (Vstandard_display_table);
6344 it->stop_charpos = charpos;
6345 it->prev_stop = charpos;
6346 it->base_level_stop = 0;
6347 if (it->bidi_p)
6349 it->bidi_it.first_elt = 1;
6350 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6351 it->bidi_it.disp_pos = -1;
6353 if (s == NULL && it->multibyte_p)
6355 EMACS_INT endpos = SCHARS (it->string);
6356 if (endpos > it->end_charpos)
6357 endpos = it->end_charpos;
6358 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6359 it->string);
6361 CHECK_IT (it);
6366 /***********************************************************************
6367 Iteration
6368 ***********************************************************************/
6370 /* Map enum it_method value to corresponding next_element_from_* function. */
6372 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6374 next_element_from_buffer,
6375 next_element_from_display_vector,
6376 next_element_from_string,
6377 next_element_from_c_string,
6378 next_element_from_image,
6379 next_element_from_stretch
6382 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6385 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6386 (possibly with the following characters). */
6388 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6389 ((IT)->cmp_it.id >= 0 \
6390 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6391 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6392 END_CHARPOS, (IT)->w, \
6393 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6394 (IT)->string)))
6397 /* Lookup the char-table Vglyphless_char_display for character C (-1
6398 if we want information for no-font case), and return the display
6399 method symbol. By side-effect, update it->what and
6400 it->glyphless_method. This function is called from
6401 get_next_display_element for each character element, and from
6402 x_produce_glyphs when no suitable font was found. */
6404 Lisp_Object
6405 lookup_glyphless_char_display (int c, struct it *it)
6407 Lisp_Object glyphless_method = Qnil;
6409 if (CHAR_TABLE_P (Vglyphless_char_display)
6410 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6412 if (c >= 0)
6414 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6415 if (CONSP (glyphless_method))
6416 glyphless_method = FRAME_WINDOW_P (it->f)
6417 ? XCAR (glyphless_method)
6418 : XCDR (glyphless_method);
6420 else
6421 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6424 retry:
6425 if (NILP (glyphless_method))
6427 if (c >= 0)
6428 /* The default is to display the character by a proper font. */
6429 return Qnil;
6430 /* The default for the no-font case is to display an empty box. */
6431 glyphless_method = Qempty_box;
6433 if (EQ (glyphless_method, Qzero_width))
6435 if (c >= 0)
6436 return glyphless_method;
6437 /* This method can't be used for the no-font case. */
6438 glyphless_method = Qempty_box;
6440 if (EQ (glyphless_method, Qthin_space))
6441 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6442 else if (EQ (glyphless_method, Qempty_box))
6443 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6444 else if (EQ (glyphless_method, Qhex_code))
6445 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6446 else if (STRINGP (glyphless_method))
6447 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6448 else
6450 /* Invalid value. We use the default method. */
6451 glyphless_method = Qnil;
6452 goto retry;
6454 it->what = IT_GLYPHLESS;
6455 return glyphless_method;
6458 /* Load IT's display element fields with information about the next
6459 display element from the current position of IT. Value is zero if
6460 end of buffer (or C string) is reached. */
6462 static struct frame *last_escape_glyph_frame = NULL;
6463 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6464 static int last_escape_glyph_merged_face_id = 0;
6466 struct frame *last_glyphless_glyph_frame = NULL;
6467 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6468 int last_glyphless_glyph_merged_face_id = 0;
6470 static int
6471 get_next_display_element (struct it *it)
6473 /* Non-zero means that we found a display element. Zero means that
6474 we hit the end of what we iterate over. Performance note: the
6475 function pointer `method' used here turns out to be faster than
6476 using a sequence of if-statements. */
6477 int success_p;
6479 get_next:
6480 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6482 if (it->what == IT_CHARACTER)
6484 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6485 and only if (a) the resolved directionality of that character
6486 is R..." */
6487 /* FIXME: Do we need an exception for characters from display
6488 tables? */
6489 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6490 it->c = bidi_mirror_char (it->c);
6491 /* Map via display table or translate control characters.
6492 IT->c, IT->len etc. have been set to the next character by
6493 the function call above. If we have a display table, and it
6494 contains an entry for IT->c, translate it. Don't do this if
6495 IT->c itself comes from a display table, otherwise we could
6496 end up in an infinite recursion. (An alternative could be to
6497 count the recursion depth of this function and signal an
6498 error when a certain maximum depth is reached.) Is it worth
6499 it? */
6500 if (success_p && it->dpvec == NULL)
6502 Lisp_Object dv;
6503 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6504 int nonascii_space_p = 0;
6505 int nonascii_hyphen_p = 0;
6506 int c = it->c; /* This is the character to display. */
6508 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6510 xassert (SINGLE_BYTE_CHAR_P (c));
6511 if (unibyte_display_via_language_environment)
6513 c = DECODE_CHAR (unibyte, c);
6514 if (c < 0)
6515 c = BYTE8_TO_CHAR (it->c);
6517 else
6518 c = BYTE8_TO_CHAR (it->c);
6521 if (it->dp
6522 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6523 VECTORP (dv)))
6525 struct Lisp_Vector *v = XVECTOR (dv);
6527 /* Return the first character from the display table
6528 entry, if not empty. If empty, don't display the
6529 current character. */
6530 if (v->header.size)
6532 it->dpvec_char_len = it->len;
6533 it->dpvec = v->contents;
6534 it->dpend = v->contents + v->header.size;
6535 it->current.dpvec_index = 0;
6536 it->dpvec_face_id = -1;
6537 it->saved_face_id = it->face_id;
6538 it->method = GET_FROM_DISPLAY_VECTOR;
6539 it->ellipsis_p = 0;
6541 else
6543 set_iterator_to_next (it, 0);
6545 goto get_next;
6548 if (! NILP (lookup_glyphless_char_display (c, it)))
6550 if (it->what == IT_GLYPHLESS)
6551 goto done;
6552 /* Don't display this character. */
6553 set_iterator_to_next (it, 0);
6554 goto get_next;
6557 /* If `nobreak-char-display' is non-nil, we display
6558 non-ASCII spaces and hyphens specially. */
6559 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6561 if (c == 0xA0)
6562 nonascii_space_p = 1;
6563 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6564 nonascii_hyphen_p = 1;
6567 /* Translate control characters into `\003' or `^C' form.
6568 Control characters coming from a display table entry are
6569 currently not translated because we use IT->dpvec to hold
6570 the translation. This could easily be changed but I
6571 don't believe that it is worth doing.
6573 The characters handled by `nobreak-char-display' must be
6574 translated too.
6576 Non-printable characters and raw-byte characters are also
6577 translated to octal form. */
6578 if (((c < ' ' || c == 127) /* ASCII control chars */
6579 ? (it->area != TEXT_AREA
6580 /* In mode line, treat \n, \t like other crl chars. */
6581 || (c != '\t'
6582 && it->glyph_row
6583 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6584 || (c != '\n' && c != '\t'))
6585 : (nonascii_space_p
6586 || nonascii_hyphen_p
6587 || CHAR_BYTE8_P (c)
6588 || ! CHAR_PRINTABLE_P (c))))
6590 /* C is a control character, non-ASCII space/hyphen,
6591 raw-byte, or a non-printable character which must be
6592 displayed either as '\003' or as `^C' where the '\\'
6593 and '^' can be defined in the display table. Fill
6594 IT->ctl_chars with glyphs for what we have to
6595 display. Then, set IT->dpvec to these glyphs. */
6596 Lisp_Object gc;
6597 int ctl_len;
6598 int face_id;
6599 EMACS_INT lface_id = 0;
6600 int escape_glyph;
6602 /* Handle control characters with ^. */
6604 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6606 int g;
6608 g = '^'; /* default glyph for Control */
6609 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6610 if (it->dp
6611 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
6612 && GLYPH_CODE_CHAR_VALID_P (gc))
6614 g = GLYPH_CODE_CHAR (gc);
6615 lface_id = GLYPH_CODE_FACE (gc);
6617 if (lface_id)
6619 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6621 else if (it->f == last_escape_glyph_frame
6622 && it->face_id == last_escape_glyph_face_id)
6624 face_id = last_escape_glyph_merged_face_id;
6626 else
6628 /* Merge the escape-glyph face into the current face. */
6629 face_id = merge_faces (it->f, Qescape_glyph, 0,
6630 it->face_id);
6631 last_escape_glyph_frame = it->f;
6632 last_escape_glyph_face_id = it->face_id;
6633 last_escape_glyph_merged_face_id = face_id;
6636 XSETINT (it->ctl_chars[0], g);
6637 XSETINT (it->ctl_chars[1], c ^ 0100);
6638 ctl_len = 2;
6639 goto display_control;
6642 /* Handle non-ascii space in the mode where it only gets
6643 highlighting. */
6645 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6647 /* Merge `nobreak-space' into the current face. */
6648 face_id = merge_faces (it->f, Qnobreak_space, 0,
6649 it->face_id);
6650 XSETINT (it->ctl_chars[0], ' ');
6651 ctl_len = 1;
6652 goto display_control;
6655 /* Handle sequences that start with the "escape glyph". */
6657 /* the default escape glyph is \. */
6658 escape_glyph = '\\';
6660 if (it->dp
6661 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
6662 && GLYPH_CODE_CHAR_VALID_P (gc))
6664 escape_glyph = GLYPH_CODE_CHAR (gc);
6665 lface_id = GLYPH_CODE_FACE (gc);
6667 if (lface_id)
6669 /* The display table specified a face.
6670 Merge it into face_id and also into escape_glyph. */
6671 face_id = merge_faces (it->f, Qt, lface_id,
6672 it->face_id);
6674 else if (it->f == last_escape_glyph_frame
6675 && it->face_id == last_escape_glyph_face_id)
6677 face_id = last_escape_glyph_merged_face_id;
6679 else
6681 /* Merge the escape-glyph face into the current face. */
6682 face_id = merge_faces (it->f, Qescape_glyph, 0,
6683 it->face_id);
6684 last_escape_glyph_frame = it->f;
6685 last_escape_glyph_face_id = it->face_id;
6686 last_escape_glyph_merged_face_id = face_id;
6689 /* Draw non-ASCII hyphen with just highlighting: */
6691 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6693 XSETINT (it->ctl_chars[0], '-');
6694 ctl_len = 1;
6695 goto display_control;
6698 /* Draw non-ASCII space/hyphen with escape glyph: */
6700 if (nonascii_space_p || nonascii_hyphen_p)
6702 XSETINT (it->ctl_chars[0], escape_glyph);
6703 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6704 ctl_len = 2;
6705 goto display_control;
6709 char str[10];
6710 int len, i;
6712 if (CHAR_BYTE8_P (c))
6713 /* Display \200 instead of \17777600. */
6714 c = CHAR_TO_BYTE8 (c);
6715 len = sprintf (str, "%03o", c);
6717 XSETINT (it->ctl_chars[0], escape_glyph);
6718 for (i = 0; i < len; i++)
6719 XSETINT (it->ctl_chars[i + 1], str[i]);
6720 ctl_len = len + 1;
6723 display_control:
6724 /* Set up IT->dpvec and return first character from it. */
6725 it->dpvec_char_len = it->len;
6726 it->dpvec = it->ctl_chars;
6727 it->dpend = it->dpvec + ctl_len;
6728 it->current.dpvec_index = 0;
6729 it->dpvec_face_id = face_id;
6730 it->saved_face_id = it->face_id;
6731 it->method = GET_FROM_DISPLAY_VECTOR;
6732 it->ellipsis_p = 0;
6733 goto get_next;
6735 it->char_to_display = c;
6737 else if (success_p)
6739 it->char_to_display = it->c;
6743 /* Adjust face id for a multibyte character. There are no multibyte
6744 character in unibyte text. */
6745 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6746 && it->multibyte_p
6747 && success_p
6748 && FRAME_WINDOW_P (it->f))
6750 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6752 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6754 /* Automatic composition with glyph-string. */
6755 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6757 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6759 else
6761 EMACS_INT pos = (it->s ? -1
6762 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6763 : IT_CHARPOS (*it));
6764 int c;
6766 if (it->what == IT_CHARACTER)
6767 c = it->char_to_display;
6768 else
6770 struct composition *cmp = composition_table[it->cmp_it.id];
6771 int i;
6773 c = ' ';
6774 for (i = 0; i < cmp->glyph_len; i++)
6775 /* TAB in a composition means display glyphs with
6776 padding space on the left or right. */
6777 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6778 break;
6780 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6784 done:
6785 /* Is this character the last one of a run of characters with
6786 box? If yes, set IT->end_of_box_run_p to 1. */
6787 if (it->face_box_p
6788 && it->s == NULL)
6790 if (it->method == GET_FROM_STRING && it->sp)
6792 int face_id = underlying_face_id (it);
6793 struct face *face = FACE_FROM_ID (it->f, face_id);
6795 if (face)
6797 if (face->box == FACE_NO_BOX)
6799 /* If the box comes from face properties in a
6800 display string, check faces in that string. */
6801 int string_face_id = face_after_it_pos (it);
6802 it->end_of_box_run_p
6803 = (FACE_FROM_ID (it->f, string_face_id)->box
6804 == FACE_NO_BOX);
6806 /* Otherwise, the box comes from the underlying face.
6807 If this is the last string character displayed, check
6808 the next buffer location. */
6809 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6810 && (it->current.overlay_string_index
6811 == it->n_overlay_strings - 1))
6813 EMACS_INT ignore;
6814 int next_face_id;
6815 struct text_pos pos = it->current.pos;
6816 INC_TEXT_POS (pos, it->multibyte_p);
6818 next_face_id = face_at_buffer_position
6819 (it->w, CHARPOS (pos), it->region_beg_charpos,
6820 it->region_end_charpos, &ignore,
6821 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6822 -1);
6823 it->end_of_box_run_p
6824 = (FACE_FROM_ID (it->f, next_face_id)->box
6825 == FACE_NO_BOX);
6829 else
6831 int face_id = face_after_it_pos (it);
6832 it->end_of_box_run_p
6833 = (face_id != it->face_id
6834 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6837 /* If we reached the end of the object we've been iterating (e.g., a
6838 display string or an overlay string), and there's something on
6839 IT->stack, proceed with what's on the stack. It doesn't make
6840 sense to return zero if there's unprocessed stuff on the stack,
6841 because otherwise that stuff will never be displayed. */
6842 if (!success_p && it->sp > 0)
6844 set_iterator_to_next (it, 0);
6845 success_p = get_next_display_element (it);
6848 /* Value is 0 if end of buffer or string reached. */
6849 return success_p;
6853 /* Move IT to the next display element.
6855 RESEAT_P non-zero means if called on a newline in buffer text,
6856 skip to the next visible line start.
6858 Functions get_next_display_element and set_iterator_to_next are
6859 separate because I find this arrangement easier to handle than a
6860 get_next_display_element function that also increments IT's
6861 position. The way it is we can first look at an iterator's current
6862 display element, decide whether it fits on a line, and if it does,
6863 increment the iterator position. The other way around we probably
6864 would either need a flag indicating whether the iterator has to be
6865 incremented the next time, or we would have to implement a
6866 decrement position function which would not be easy to write. */
6868 void
6869 set_iterator_to_next (struct it *it, int reseat_p)
6871 /* Reset flags indicating start and end of a sequence of characters
6872 with box. Reset them at the start of this function because
6873 moving the iterator to a new position might set them. */
6874 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6876 switch (it->method)
6878 case GET_FROM_BUFFER:
6879 /* The current display element of IT is a character from
6880 current_buffer. Advance in the buffer, and maybe skip over
6881 invisible lines that are so because of selective display. */
6882 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6883 reseat_at_next_visible_line_start (it, 0);
6884 else if (it->cmp_it.id >= 0)
6886 /* We are currently getting glyphs from a composition. */
6887 int i;
6889 if (! it->bidi_p)
6891 IT_CHARPOS (*it) += it->cmp_it.nchars;
6892 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6893 if (it->cmp_it.to < it->cmp_it.nglyphs)
6895 it->cmp_it.from = it->cmp_it.to;
6897 else
6899 it->cmp_it.id = -1;
6900 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6901 IT_BYTEPOS (*it),
6902 it->end_charpos, Qnil);
6905 else if (! it->cmp_it.reversed_p)
6907 /* Composition created while scanning forward. */
6908 /* Update IT's char/byte positions to point to the first
6909 character of the next grapheme cluster, or to the
6910 character visually after the current composition. */
6911 for (i = 0; i < it->cmp_it.nchars; i++)
6912 bidi_move_to_visually_next (&it->bidi_it);
6913 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6914 IT_CHARPOS (*it) = it->bidi_it.charpos;
6916 if (it->cmp_it.to < it->cmp_it.nglyphs)
6918 /* Proceed to the next grapheme cluster. */
6919 it->cmp_it.from = it->cmp_it.to;
6921 else
6923 /* No more grapheme clusters in this composition.
6924 Find the next stop position. */
6925 EMACS_INT stop = it->end_charpos;
6926 if (it->bidi_it.scan_dir < 0)
6927 /* Now we are scanning backward and don't know
6928 where to stop. */
6929 stop = -1;
6930 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6931 IT_BYTEPOS (*it), stop, Qnil);
6934 else
6936 /* Composition created while scanning backward. */
6937 /* Update IT's char/byte positions to point to the last
6938 character of the previous grapheme cluster, or the
6939 character visually after the current composition. */
6940 for (i = 0; i < it->cmp_it.nchars; i++)
6941 bidi_move_to_visually_next (&it->bidi_it);
6942 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6943 IT_CHARPOS (*it) = it->bidi_it.charpos;
6944 if (it->cmp_it.from > 0)
6946 /* Proceed to the previous grapheme cluster. */
6947 it->cmp_it.to = it->cmp_it.from;
6949 else
6951 /* No more grapheme clusters in this composition.
6952 Find the next stop position. */
6953 EMACS_INT stop = it->end_charpos;
6954 if (it->bidi_it.scan_dir < 0)
6955 /* Now we are scanning backward and don't know
6956 where to stop. */
6957 stop = -1;
6958 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6959 IT_BYTEPOS (*it), stop, Qnil);
6963 else
6965 xassert (it->len != 0);
6967 if (!it->bidi_p)
6969 IT_BYTEPOS (*it) += it->len;
6970 IT_CHARPOS (*it) += 1;
6972 else
6974 int prev_scan_dir = it->bidi_it.scan_dir;
6975 /* If this is a new paragraph, determine its base
6976 direction (a.k.a. its base embedding level). */
6977 if (it->bidi_it.new_paragraph)
6978 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6979 bidi_move_to_visually_next (&it->bidi_it);
6980 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6981 IT_CHARPOS (*it) = it->bidi_it.charpos;
6982 if (prev_scan_dir != it->bidi_it.scan_dir)
6984 /* As the scan direction was changed, we must
6985 re-compute the stop position for composition. */
6986 EMACS_INT stop = it->end_charpos;
6987 if (it->bidi_it.scan_dir < 0)
6988 stop = -1;
6989 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6990 IT_BYTEPOS (*it), stop, Qnil);
6993 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6995 break;
6997 case GET_FROM_C_STRING:
6998 /* Current display element of IT is from a C string. */
6999 if (!it->bidi_p
7000 /* If the string position is beyond string's end, it means
7001 next_element_from_c_string is padding the string with
7002 blanks, in which case we bypass the bidi iterator,
7003 because it cannot deal with such virtual characters. */
7004 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7006 IT_BYTEPOS (*it) += it->len;
7007 IT_CHARPOS (*it) += 1;
7009 else
7011 bidi_move_to_visually_next (&it->bidi_it);
7012 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7013 IT_CHARPOS (*it) = it->bidi_it.charpos;
7015 break;
7017 case GET_FROM_DISPLAY_VECTOR:
7018 /* Current display element of IT is from a display table entry.
7019 Advance in the display table definition. Reset it to null if
7020 end reached, and continue with characters from buffers/
7021 strings. */
7022 ++it->current.dpvec_index;
7024 /* Restore face of the iterator to what they were before the
7025 display vector entry (these entries may contain faces). */
7026 it->face_id = it->saved_face_id;
7028 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7030 int recheck_faces = it->ellipsis_p;
7032 if (it->s)
7033 it->method = GET_FROM_C_STRING;
7034 else if (STRINGP (it->string))
7035 it->method = GET_FROM_STRING;
7036 else
7038 it->method = GET_FROM_BUFFER;
7039 it->object = it->w->buffer;
7042 it->dpvec = NULL;
7043 it->current.dpvec_index = -1;
7045 /* Skip over characters which were displayed via IT->dpvec. */
7046 if (it->dpvec_char_len < 0)
7047 reseat_at_next_visible_line_start (it, 1);
7048 else if (it->dpvec_char_len > 0)
7050 if (it->method == GET_FROM_STRING
7051 && it->n_overlay_strings > 0)
7052 it->ignore_overlay_strings_at_pos_p = 1;
7053 it->len = it->dpvec_char_len;
7054 set_iterator_to_next (it, reseat_p);
7057 /* Maybe recheck faces after display vector */
7058 if (recheck_faces)
7059 it->stop_charpos = IT_CHARPOS (*it);
7061 break;
7063 case GET_FROM_STRING:
7064 /* Current display element is a character from a Lisp string. */
7065 xassert (it->s == NULL && STRINGP (it->string));
7066 /* Don't advance past string end. These conditions are true
7067 when set_iterator_to_next is called at the end of
7068 get_next_display_element, in which case the Lisp string is
7069 already exhausted, and all we want is pop the iterator
7070 stack. */
7071 if (it->current.overlay_string_index >= 0)
7073 /* This is an overlay string, so there's no padding with
7074 spaces, and the number of characters in the string is
7075 where the string ends. */
7076 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7077 goto consider_string_end;
7079 else
7081 /* Not an overlay string. There could be padding, so test
7082 against it->end_charpos . */
7083 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7084 goto consider_string_end;
7086 if (it->cmp_it.id >= 0)
7088 int i;
7090 if (! it->bidi_p)
7092 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7093 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7094 if (it->cmp_it.to < it->cmp_it.nglyphs)
7095 it->cmp_it.from = it->cmp_it.to;
7096 else
7098 it->cmp_it.id = -1;
7099 composition_compute_stop_pos (&it->cmp_it,
7100 IT_STRING_CHARPOS (*it),
7101 IT_STRING_BYTEPOS (*it),
7102 it->end_charpos, it->string);
7105 else if (! it->cmp_it.reversed_p)
7107 for (i = 0; i < it->cmp_it.nchars; i++)
7108 bidi_move_to_visually_next (&it->bidi_it);
7109 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7110 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7112 if (it->cmp_it.to < it->cmp_it.nglyphs)
7113 it->cmp_it.from = it->cmp_it.to;
7114 else
7116 EMACS_INT stop = it->end_charpos;
7117 if (it->bidi_it.scan_dir < 0)
7118 stop = -1;
7119 composition_compute_stop_pos (&it->cmp_it,
7120 IT_STRING_CHARPOS (*it),
7121 IT_STRING_BYTEPOS (*it), stop,
7122 it->string);
7125 else
7127 for (i = 0; i < it->cmp_it.nchars; i++)
7128 bidi_move_to_visually_next (&it->bidi_it);
7129 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7130 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7131 if (it->cmp_it.from > 0)
7132 it->cmp_it.to = it->cmp_it.from;
7133 else
7135 EMACS_INT stop = it->end_charpos;
7136 if (it->bidi_it.scan_dir < 0)
7137 stop = -1;
7138 composition_compute_stop_pos (&it->cmp_it,
7139 IT_STRING_CHARPOS (*it),
7140 IT_STRING_BYTEPOS (*it), stop,
7141 it->string);
7145 else
7147 if (!it->bidi_p
7148 /* If the string position is beyond string's end, it
7149 means next_element_from_string is padding the string
7150 with blanks, in which case we bypass the bidi
7151 iterator, because it cannot deal with such virtual
7152 characters. */
7153 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7155 IT_STRING_BYTEPOS (*it) += it->len;
7156 IT_STRING_CHARPOS (*it) += 1;
7158 else
7160 int prev_scan_dir = it->bidi_it.scan_dir;
7162 bidi_move_to_visually_next (&it->bidi_it);
7163 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7164 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7165 if (prev_scan_dir != it->bidi_it.scan_dir)
7167 EMACS_INT stop = it->end_charpos;
7169 if (it->bidi_it.scan_dir < 0)
7170 stop = -1;
7171 composition_compute_stop_pos (&it->cmp_it,
7172 IT_STRING_CHARPOS (*it),
7173 IT_STRING_BYTEPOS (*it), stop,
7174 it->string);
7179 consider_string_end:
7181 if (it->current.overlay_string_index >= 0)
7183 /* IT->string is an overlay string. Advance to the
7184 next, if there is one. */
7185 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7187 it->ellipsis_p = 0;
7188 next_overlay_string (it);
7189 if (it->ellipsis_p)
7190 setup_for_ellipsis (it, 0);
7193 else
7195 /* IT->string is not an overlay string. If we reached
7196 its end, and there is something on IT->stack, proceed
7197 with what is on the stack. This can be either another
7198 string, this time an overlay string, or a buffer. */
7199 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7200 && it->sp > 0)
7202 pop_it (it);
7203 if (it->method == GET_FROM_STRING)
7204 goto consider_string_end;
7207 break;
7209 case GET_FROM_IMAGE:
7210 case GET_FROM_STRETCH:
7211 /* The position etc with which we have to proceed are on
7212 the stack. The position may be at the end of a string,
7213 if the `display' property takes up the whole string. */
7214 xassert (it->sp > 0);
7215 pop_it (it);
7216 if (it->method == GET_FROM_STRING)
7217 goto consider_string_end;
7218 break;
7220 default:
7221 /* There are no other methods defined, so this should be a bug. */
7222 abort ();
7225 xassert (it->method != GET_FROM_STRING
7226 || (STRINGP (it->string)
7227 && IT_STRING_CHARPOS (*it) >= 0));
7230 /* Load IT's display element fields with information about the next
7231 display element which comes from a display table entry or from the
7232 result of translating a control character to one of the forms `^C'
7233 or `\003'.
7235 IT->dpvec holds the glyphs to return as characters.
7236 IT->saved_face_id holds the face id before the display vector--it
7237 is restored into IT->face_id in set_iterator_to_next. */
7239 static int
7240 next_element_from_display_vector (struct it *it)
7242 Lisp_Object gc;
7244 /* Precondition. */
7245 xassert (it->dpvec && it->current.dpvec_index >= 0);
7247 it->face_id = it->saved_face_id;
7249 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7250 That seemed totally bogus - so I changed it... */
7251 gc = it->dpvec[it->current.dpvec_index];
7253 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
7255 it->c = GLYPH_CODE_CHAR (gc);
7256 it->len = CHAR_BYTES (it->c);
7258 /* The entry may contain a face id to use. Such a face id is
7259 the id of a Lisp face, not a realized face. A face id of
7260 zero means no face is specified. */
7261 if (it->dpvec_face_id >= 0)
7262 it->face_id = it->dpvec_face_id;
7263 else
7265 EMACS_INT lface_id = GLYPH_CODE_FACE (gc);
7266 if (lface_id > 0)
7267 it->face_id = merge_faces (it->f, Qt, lface_id,
7268 it->saved_face_id);
7271 else
7272 /* Display table entry is invalid. Return a space. */
7273 it->c = ' ', it->len = 1;
7275 /* Don't change position and object of the iterator here. They are
7276 still the values of the character that had this display table
7277 entry or was translated, and that's what we want. */
7278 it->what = IT_CHARACTER;
7279 return 1;
7282 /* Get the first element of string/buffer in the visual order, after
7283 being reseated to a new position in a string or a buffer. */
7284 static void
7285 get_visually_first_element (struct it *it)
7287 int string_p = STRINGP (it->string) || it->s;
7288 EMACS_INT eob = (string_p ? it->bidi_it.string.schars : ZV);
7289 EMACS_INT bob = (string_p ? 0 : BEGV);
7291 if (STRINGP (it->string))
7293 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7294 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7296 else
7298 it->bidi_it.charpos = IT_CHARPOS (*it);
7299 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7302 if (it->bidi_it.charpos == eob)
7304 /* Nothing to do, but reset the FIRST_ELT flag, like
7305 bidi_paragraph_init does, because we are not going to
7306 call it. */
7307 it->bidi_it.first_elt = 0;
7309 else if (it->bidi_it.charpos == bob
7310 || (!string_p
7311 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7312 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7314 /* If we are at the beginning of a line/string, we can produce
7315 the next element right away. */
7316 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7317 bidi_move_to_visually_next (&it->bidi_it);
7319 else
7321 EMACS_INT orig_bytepos = it->bidi_it.bytepos;
7323 /* We need to prime the bidi iterator starting at the line's or
7324 string's beginning, before we will be able to produce the
7325 next element. */
7326 if (string_p)
7327 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7328 else
7330 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7331 -1);
7332 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7334 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7337 /* Now return to buffer/string position where we were asked
7338 to get the next display element, and produce that. */
7339 bidi_move_to_visually_next (&it->bidi_it);
7341 while (it->bidi_it.bytepos != orig_bytepos
7342 && it->bidi_it.charpos < eob);
7345 /* Adjust IT's position information to where we ended up. */
7346 if (STRINGP (it->string))
7348 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7349 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7351 else
7353 IT_CHARPOS (*it) = it->bidi_it.charpos;
7354 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7357 if (STRINGP (it->string) || !it->s)
7359 EMACS_INT stop, charpos, bytepos;
7361 if (STRINGP (it->string))
7363 xassert (!it->s);
7364 stop = SCHARS (it->string);
7365 if (stop > it->end_charpos)
7366 stop = it->end_charpos;
7367 charpos = IT_STRING_CHARPOS (*it);
7368 bytepos = IT_STRING_BYTEPOS (*it);
7370 else
7372 stop = it->end_charpos;
7373 charpos = IT_CHARPOS (*it);
7374 bytepos = IT_BYTEPOS (*it);
7376 if (it->bidi_it.scan_dir < 0)
7377 stop = -1;
7378 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7379 it->string);
7383 /* Load IT with the next display element from Lisp string IT->string.
7384 IT->current.string_pos is the current position within the string.
7385 If IT->current.overlay_string_index >= 0, the Lisp string is an
7386 overlay string. */
7388 static int
7389 next_element_from_string (struct it *it)
7391 struct text_pos position;
7393 xassert (STRINGP (it->string));
7394 xassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7395 xassert (IT_STRING_CHARPOS (*it) >= 0);
7396 position = it->current.string_pos;
7398 /* With bidi reordering, the character to display might not be the
7399 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7400 that we were reseat()ed to a new string, whose paragraph
7401 direction is not known. */
7402 if (it->bidi_p && it->bidi_it.first_elt)
7404 get_visually_first_element (it);
7405 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7408 /* Time to check for invisible text? */
7409 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7411 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7413 if (!(!it->bidi_p
7414 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7415 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7417 /* With bidi non-linear iteration, we could find
7418 ourselves far beyond the last computed stop_charpos,
7419 with several other stop positions in between that we
7420 missed. Scan them all now, in buffer's logical
7421 order, until we find and handle the last stop_charpos
7422 that precedes our current position. */
7423 handle_stop_backwards (it, it->stop_charpos);
7424 return GET_NEXT_DISPLAY_ELEMENT (it);
7426 else
7428 if (it->bidi_p)
7430 /* Take note of the stop position we just moved
7431 across, for when we will move back across it. */
7432 it->prev_stop = it->stop_charpos;
7433 /* If we are at base paragraph embedding level, take
7434 note of the last stop position seen at this
7435 level. */
7436 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7437 it->base_level_stop = it->stop_charpos;
7439 handle_stop (it);
7441 /* Since a handler may have changed IT->method, we must
7442 recurse here. */
7443 return GET_NEXT_DISPLAY_ELEMENT (it);
7446 else if (it->bidi_p
7447 /* If we are before prev_stop, we may have overstepped
7448 on our way backwards a stop_pos, and if so, we need
7449 to handle that stop_pos. */
7450 && IT_STRING_CHARPOS (*it) < it->prev_stop
7451 /* We can sometimes back up for reasons that have nothing
7452 to do with bidi reordering. E.g., compositions. The
7453 code below is only needed when we are above the base
7454 embedding level, so test for that explicitly. */
7455 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7457 /* If we lost track of base_level_stop, we have no better
7458 place for handle_stop_backwards to start from than string
7459 beginning. This happens, e.g., when we were reseated to
7460 the previous screenful of text by vertical-motion. */
7461 if (it->base_level_stop <= 0
7462 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7463 it->base_level_stop = 0;
7464 handle_stop_backwards (it, it->base_level_stop);
7465 return GET_NEXT_DISPLAY_ELEMENT (it);
7469 if (it->current.overlay_string_index >= 0)
7471 /* Get the next character from an overlay string. In overlay
7472 strings, there is no field width or padding with spaces to
7473 do. */
7474 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7476 it->what = IT_EOB;
7477 return 0;
7479 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7480 IT_STRING_BYTEPOS (*it),
7481 it->bidi_it.scan_dir < 0
7482 ? -1
7483 : SCHARS (it->string))
7484 && next_element_from_composition (it))
7486 return 1;
7488 else if (STRING_MULTIBYTE (it->string))
7490 const unsigned char *s = (SDATA (it->string)
7491 + IT_STRING_BYTEPOS (*it));
7492 it->c = string_char_and_length (s, &it->len);
7494 else
7496 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7497 it->len = 1;
7500 else
7502 /* Get the next character from a Lisp string that is not an
7503 overlay string. Such strings come from the mode line, for
7504 example. We may have to pad with spaces, or truncate the
7505 string. See also next_element_from_c_string. */
7506 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7508 it->what = IT_EOB;
7509 return 0;
7511 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7513 /* Pad with spaces. */
7514 it->c = ' ', it->len = 1;
7515 CHARPOS (position) = BYTEPOS (position) = -1;
7517 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7518 IT_STRING_BYTEPOS (*it),
7519 it->bidi_it.scan_dir < 0
7520 ? -1
7521 : it->string_nchars)
7522 && next_element_from_composition (it))
7524 return 1;
7526 else if (STRING_MULTIBYTE (it->string))
7528 const unsigned char *s = (SDATA (it->string)
7529 + IT_STRING_BYTEPOS (*it));
7530 it->c = string_char_and_length (s, &it->len);
7532 else
7534 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7535 it->len = 1;
7539 /* Record what we have and where it came from. */
7540 it->what = IT_CHARACTER;
7541 it->object = it->string;
7542 it->position = position;
7543 return 1;
7547 /* Load IT with next display element from C string IT->s.
7548 IT->string_nchars is the maximum number of characters to return
7549 from the string. IT->end_charpos may be greater than
7550 IT->string_nchars when this function is called, in which case we
7551 may have to return padding spaces. Value is zero if end of string
7552 reached, including padding spaces. */
7554 static int
7555 next_element_from_c_string (struct it *it)
7557 int success_p = 1;
7559 xassert (it->s);
7560 xassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7561 it->what = IT_CHARACTER;
7562 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7563 it->object = Qnil;
7565 /* With bidi reordering, the character to display might not be the
7566 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7567 we were reseated to a new string, whose paragraph direction is
7568 not known. */
7569 if (it->bidi_p && it->bidi_it.first_elt)
7570 get_visually_first_element (it);
7572 /* IT's position can be greater than IT->string_nchars in case a
7573 field width or precision has been specified when the iterator was
7574 initialized. */
7575 if (IT_CHARPOS (*it) >= it->end_charpos)
7577 /* End of the game. */
7578 it->what = IT_EOB;
7579 success_p = 0;
7581 else if (IT_CHARPOS (*it) >= it->string_nchars)
7583 /* Pad with spaces. */
7584 it->c = ' ', it->len = 1;
7585 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7587 else if (it->multibyte_p)
7588 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7589 else
7590 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7592 return success_p;
7596 /* Set up IT to return characters from an ellipsis, if appropriate.
7597 The definition of the ellipsis glyphs may come from a display table
7598 entry. This function fills IT with the first glyph from the
7599 ellipsis if an ellipsis is to be displayed. */
7601 static int
7602 next_element_from_ellipsis (struct it *it)
7604 if (it->selective_display_ellipsis_p)
7605 setup_for_ellipsis (it, it->len);
7606 else
7608 /* The face at the current position may be different from the
7609 face we find after the invisible text. Remember what it
7610 was in IT->saved_face_id, and signal that it's there by
7611 setting face_before_selective_p. */
7612 it->saved_face_id = it->face_id;
7613 it->method = GET_FROM_BUFFER;
7614 it->object = it->w->buffer;
7615 reseat_at_next_visible_line_start (it, 1);
7616 it->face_before_selective_p = 1;
7619 return GET_NEXT_DISPLAY_ELEMENT (it);
7623 /* Deliver an image display element. The iterator IT is already
7624 filled with image information (done in handle_display_prop). Value
7625 is always 1. */
7628 static int
7629 next_element_from_image (struct it *it)
7631 it->what = IT_IMAGE;
7632 it->ignore_overlay_strings_at_pos_p = 0;
7633 return 1;
7637 /* Fill iterator IT with next display element from a stretch glyph
7638 property. IT->object is the value of the text property. Value is
7639 always 1. */
7641 static int
7642 next_element_from_stretch (struct it *it)
7644 it->what = IT_STRETCH;
7645 return 1;
7648 /* Scan backwards from IT's current position until we find a stop
7649 position, or until BEGV. This is called when we find ourself
7650 before both the last known prev_stop and base_level_stop while
7651 reordering bidirectional text. */
7653 static void
7654 compute_stop_pos_backwards (struct it *it)
7656 const int SCAN_BACK_LIMIT = 1000;
7657 struct text_pos pos;
7658 struct display_pos save_current = it->current;
7659 struct text_pos save_position = it->position;
7660 EMACS_INT charpos = IT_CHARPOS (*it);
7661 EMACS_INT where_we_are = charpos;
7662 EMACS_INT save_stop_pos = it->stop_charpos;
7663 EMACS_INT save_end_pos = it->end_charpos;
7665 xassert (NILP (it->string) && !it->s);
7666 xassert (it->bidi_p);
7667 it->bidi_p = 0;
7670 it->end_charpos = min (charpos + 1, ZV);
7671 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7672 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7673 reseat_1 (it, pos, 0);
7674 compute_stop_pos (it);
7675 /* We must advance forward, right? */
7676 if (it->stop_charpos <= charpos)
7677 abort ();
7679 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7681 if (it->stop_charpos <= where_we_are)
7682 it->prev_stop = it->stop_charpos;
7683 else
7684 it->prev_stop = BEGV;
7685 it->bidi_p = 1;
7686 it->current = save_current;
7687 it->position = save_position;
7688 it->stop_charpos = save_stop_pos;
7689 it->end_charpos = save_end_pos;
7692 /* Scan forward from CHARPOS in the current buffer/string, until we
7693 find a stop position > current IT's position. Then handle the stop
7694 position before that. This is called when we bump into a stop
7695 position while reordering bidirectional text. CHARPOS should be
7696 the last previously processed stop_pos (or BEGV/0, if none were
7697 processed yet) whose position is less that IT's current
7698 position. */
7700 static void
7701 handle_stop_backwards (struct it *it, EMACS_INT charpos)
7703 int bufp = !STRINGP (it->string);
7704 EMACS_INT where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7705 struct display_pos save_current = it->current;
7706 struct text_pos save_position = it->position;
7707 struct text_pos pos1;
7708 EMACS_INT next_stop;
7710 /* Scan in strict logical order. */
7711 xassert (it->bidi_p);
7712 it->bidi_p = 0;
7715 it->prev_stop = charpos;
7716 if (bufp)
7718 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7719 reseat_1 (it, pos1, 0);
7721 else
7722 it->current.string_pos = string_pos (charpos, it->string);
7723 compute_stop_pos (it);
7724 /* We must advance forward, right? */
7725 if (it->stop_charpos <= it->prev_stop)
7726 abort ();
7727 charpos = it->stop_charpos;
7729 while (charpos <= where_we_are);
7731 it->bidi_p = 1;
7732 it->current = save_current;
7733 it->position = save_position;
7734 next_stop = it->stop_charpos;
7735 it->stop_charpos = it->prev_stop;
7736 handle_stop (it);
7737 it->stop_charpos = next_stop;
7740 /* Load IT with the next display element from current_buffer. Value
7741 is zero if end of buffer reached. IT->stop_charpos is the next
7742 position at which to stop and check for text properties or buffer
7743 end. */
7745 static int
7746 next_element_from_buffer (struct it *it)
7748 int success_p = 1;
7750 xassert (IT_CHARPOS (*it) >= BEGV);
7751 xassert (NILP (it->string) && !it->s);
7752 xassert (!it->bidi_p
7753 || (EQ (it->bidi_it.string.lstring, Qnil)
7754 && it->bidi_it.string.s == NULL));
7756 /* With bidi reordering, the character to display might not be the
7757 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7758 we were reseat()ed to a new buffer position, which is potentially
7759 a different paragraph. */
7760 if (it->bidi_p && it->bidi_it.first_elt)
7762 get_visually_first_element (it);
7763 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7766 if (IT_CHARPOS (*it) >= it->stop_charpos)
7768 if (IT_CHARPOS (*it) >= it->end_charpos)
7770 int overlay_strings_follow_p;
7772 /* End of the game, except when overlay strings follow that
7773 haven't been returned yet. */
7774 if (it->overlay_strings_at_end_processed_p)
7775 overlay_strings_follow_p = 0;
7776 else
7778 it->overlay_strings_at_end_processed_p = 1;
7779 overlay_strings_follow_p = get_overlay_strings (it, 0);
7782 if (overlay_strings_follow_p)
7783 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7784 else
7786 it->what = IT_EOB;
7787 it->position = it->current.pos;
7788 success_p = 0;
7791 else if (!(!it->bidi_p
7792 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7793 || IT_CHARPOS (*it) == it->stop_charpos))
7795 /* With bidi non-linear iteration, we could find ourselves
7796 far beyond the last computed stop_charpos, with several
7797 other stop positions in between that we missed. Scan
7798 them all now, in buffer's logical order, until we find
7799 and handle the last stop_charpos that precedes our
7800 current position. */
7801 handle_stop_backwards (it, it->stop_charpos);
7802 return GET_NEXT_DISPLAY_ELEMENT (it);
7804 else
7806 if (it->bidi_p)
7808 /* Take note of the stop position we just moved across,
7809 for when we will move back across it. */
7810 it->prev_stop = it->stop_charpos;
7811 /* If we are at base paragraph embedding level, take
7812 note of the last stop position seen at this
7813 level. */
7814 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7815 it->base_level_stop = it->stop_charpos;
7817 handle_stop (it);
7818 return GET_NEXT_DISPLAY_ELEMENT (it);
7821 else if (it->bidi_p
7822 /* If we are before prev_stop, we may have overstepped on
7823 our way backwards a stop_pos, and if so, we need to
7824 handle that stop_pos. */
7825 && IT_CHARPOS (*it) < it->prev_stop
7826 /* We can sometimes back up for reasons that have nothing
7827 to do with bidi reordering. E.g., compositions. The
7828 code below is only needed when we are above the base
7829 embedding level, so test for that explicitly. */
7830 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7832 if (it->base_level_stop <= 0
7833 || IT_CHARPOS (*it) < it->base_level_stop)
7835 /* If we lost track of base_level_stop, we need to find
7836 prev_stop by looking backwards. This happens, e.g., when
7837 we were reseated to the previous screenful of text by
7838 vertical-motion. */
7839 it->base_level_stop = BEGV;
7840 compute_stop_pos_backwards (it);
7841 handle_stop_backwards (it, it->prev_stop);
7843 else
7844 handle_stop_backwards (it, it->base_level_stop);
7845 return GET_NEXT_DISPLAY_ELEMENT (it);
7847 else
7849 /* No face changes, overlays etc. in sight, so just return a
7850 character from current_buffer. */
7851 unsigned char *p;
7852 EMACS_INT stop;
7854 /* Maybe run the redisplay end trigger hook. Performance note:
7855 This doesn't seem to cost measurable time. */
7856 if (it->redisplay_end_trigger_charpos
7857 && it->glyph_row
7858 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7859 run_redisplay_end_trigger_hook (it);
7861 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7862 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7863 stop)
7864 && next_element_from_composition (it))
7866 return 1;
7869 /* Get the next character, maybe multibyte. */
7870 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7871 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7872 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7873 else
7874 it->c = *p, it->len = 1;
7876 /* Record what we have and where it came from. */
7877 it->what = IT_CHARACTER;
7878 it->object = it->w->buffer;
7879 it->position = it->current.pos;
7881 /* Normally we return the character found above, except when we
7882 really want to return an ellipsis for selective display. */
7883 if (it->selective)
7885 if (it->c == '\n')
7887 /* A value of selective > 0 means hide lines indented more
7888 than that number of columns. */
7889 if (it->selective > 0
7890 && IT_CHARPOS (*it) + 1 < ZV
7891 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7892 IT_BYTEPOS (*it) + 1,
7893 it->selective))
7895 success_p = next_element_from_ellipsis (it);
7896 it->dpvec_char_len = -1;
7899 else if (it->c == '\r' && it->selective == -1)
7901 /* A value of selective == -1 means that everything from the
7902 CR to the end of the line is invisible, with maybe an
7903 ellipsis displayed for it. */
7904 success_p = next_element_from_ellipsis (it);
7905 it->dpvec_char_len = -1;
7910 /* Value is zero if end of buffer reached. */
7911 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7912 return success_p;
7916 /* Run the redisplay end trigger hook for IT. */
7918 static void
7919 run_redisplay_end_trigger_hook (struct it *it)
7921 Lisp_Object args[3];
7923 /* IT->glyph_row should be non-null, i.e. we should be actually
7924 displaying something, or otherwise we should not run the hook. */
7925 xassert (it->glyph_row);
7927 /* Set up hook arguments. */
7928 args[0] = Qredisplay_end_trigger_functions;
7929 args[1] = it->window;
7930 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7931 it->redisplay_end_trigger_charpos = 0;
7933 /* Since we are *trying* to run these functions, don't try to run
7934 them again, even if they get an error. */
7935 it->w->redisplay_end_trigger = Qnil;
7936 Frun_hook_with_args (3, args);
7938 /* Notice if it changed the face of the character we are on. */
7939 handle_face_prop (it);
7943 /* Deliver a composition display element. Unlike the other
7944 next_element_from_XXX, this function is not registered in the array
7945 get_next_element[]. It is called from next_element_from_buffer and
7946 next_element_from_string when necessary. */
7948 static int
7949 next_element_from_composition (struct it *it)
7951 it->what = IT_COMPOSITION;
7952 it->len = it->cmp_it.nbytes;
7953 if (STRINGP (it->string))
7955 if (it->c < 0)
7957 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7958 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7959 return 0;
7961 it->position = it->current.string_pos;
7962 it->object = it->string;
7963 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7964 IT_STRING_BYTEPOS (*it), it->string);
7966 else
7968 if (it->c < 0)
7970 IT_CHARPOS (*it) += it->cmp_it.nchars;
7971 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7972 if (it->bidi_p)
7974 if (it->bidi_it.new_paragraph)
7975 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7976 /* Resync the bidi iterator with IT's new position.
7977 FIXME: this doesn't support bidirectional text. */
7978 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7979 bidi_move_to_visually_next (&it->bidi_it);
7981 return 0;
7983 it->position = it->current.pos;
7984 it->object = it->w->buffer;
7985 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7986 IT_BYTEPOS (*it), Qnil);
7988 return 1;
7993 /***********************************************************************
7994 Moving an iterator without producing glyphs
7995 ***********************************************************************/
7997 /* Check if iterator is at a position corresponding to a valid buffer
7998 position after some move_it_ call. */
8000 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8001 ((it)->method == GET_FROM_STRING \
8002 ? IT_STRING_CHARPOS (*it) == 0 \
8003 : 1)
8006 /* Move iterator IT to a specified buffer or X position within one
8007 line on the display without producing glyphs.
8009 OP should be a bit mask including some or all of these bits:
8010 MOVE_TO_X: Stop upon reaching x-position TO_X.
8011 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8012 Regardless of OP's value, stop upon reaching the end of the display line.
8014 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8015 This means, in particular, that TO_X includes window's horizontal
8016 scroll amount.
8018 The return value has several possible values that
8019 say what condition caused the scan to stop:
8021 MOVE_POS_MATCH_OR_ZV
8022 - when TO_POS or ZV was reached.
8024 MOVE_X_REACHED
8025 -when TO_X was reached before TO_POS or ZV were reached.
8027 MOVE_LINE_CONTINUED
8028 - when we reached the end of the display area and the line must
8029 be continued.
8031 MOVE_LINE_TRUNCATED
8032 - when we reached the end of the display area and the line is
8033 truncated.
8035 MOVE_NEWLINE_OR_CR
8036 - when we stopped at a line end, i.e. a newline or a CR and selective
8037 display is on. */
8039 static enum move_it_result
8040 move_it_in_display_line_to (struct it *it,
8041 EMACS_INT to_charpos, int to_x,
8042 enum move_operation_enum op)
8044 enum move_it_result result = MOVE_UNDEFINED;
8045 struct glyph_row *saved_glyph_row;
8046 struct it wrap_it, atpos_it, atx_it, ppos_it;
8047 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8048 void *ppos_data = NULL;
8049 int may_wrap = 0;
8050 enum it_method prev_method = it->method;
8051 EMACS_INT prev_pos = IT_CHARPOS (*it);
8052 int saw_smaller_pos = prev_pos < to_charpos;
8054 /* Don't produce glyphs in produce_glyphs. */
8055 saved_glyph_row = it->glyph_row;
8056 it->glyph_row = NULL;
8058 /* Use wrap_it to save a copy of IT wherever a word wrap could
8059 occur. Use atpos_it to save a copy of IT at the desired buffer
8060 position, if found, so that we can scan ahead and check if the
8061 word later overshoots the window edge. Use atx_it similarly, for
8062 pixel positions. */
8063 wrap_it.sp = -1;
8064 atpos_it.sp = -1;
8065 atx_it.sp = -1;
8067 /* Use ppos_it under bidi reordering to save a copy of IT for the
8068 position > CHARPOS that is the closest to CHARPOS. We restore
8069 that position in IT when we have scanned the entire display line
8070 without finding a match for CHARPOS and all the character
8071 positions are greater than CHARPOS. */
8072 if (it->bidi_p)
8074 SAVE_IT (ppos_it, *it, ppos_data);
8075 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8076 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8077 SAVE_IT (ppos_it, *it, ppos_data);
8080 #define BUFFER_POS_REACHED_P() \
8081 ((op & MOVE_TO_POS) != 0 \
8082 && BUFFERP (it->object) \
8083 && (IT_CHARPOS (*it) == to_charpos \
8084 || ((!it->bidi_p \
8085 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8086 && IT_CHARPOS (*it) > to_charpos) \
8087 || (it->what == IT_COMPOSITION \
8088 && ((IT_CHARPOS (*it) > to_charpos \
8089 && to_charpos >= it->cmp_it.charpos) \
8090 || (IT_CHARPOS (*it) < to_charpos \
8091 && to_charpos <= it->cmp_it.charpos)))) \
8092 && (it->method == GET_FROM_BUFFER \
8093 || (it->method == GET_FROM_DISPLAY_VECTOR \
8094 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8096 /* If there's a line-/wrap-prefix, handle it. */
8097 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8098 && it->current_y < it->last_visible_y)
8099 handle_line_prefix (it);
8101 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8102 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8104 while (1)
8106 int x, i, ascent = 0, descent = 0;
8108 /* Utility macro to reset an iterator with x, ascent, and descent. */
8109 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8110 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8111 (IT)->max_descent = descent)
8113 /* Stop if we move beyond TO_CHARPOS (after an image or a
8114 display string or stretch glyph). */
8115 if ((op & MOVE_TO_POS) != 0
8116 && BUFFERP (it->object)
8117 && it->method == GET_FROM_BUFFER
8118 && (((!it->bidi_p
8119 /* When the iterator is at base embedding level, we
8120 are guaranteed that characters are delivered for
8121 display in strictly increasing order of their
8122 buffer positions. */
8123 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8124 && IT_CHARPOS (*it) > to_charpos)
8125 || (it->bidi_p
8126 && (prev_method == GET_FROM_IMAGE
8127 || prev_method == GET_FROM_STRETCH
8128 || prev_method == GET_FROM_STRING)
8129 /* Passed TO_CHARPOS from left to right. */
8130 && ((prev_pos < to_charpos
8131 && IT_CHARPOS (*it) > to_charpos)
8132 /* Passed TO_CHARPOS from right to left. */
8133 || (prev_pos > to_charpos
8134 && IT_CHARPOS (*it) < to_charpos)))))
8136 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8138 result = MOVE_POS_MATCH_OR_ZV;
8139 break;
8141 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8142 /* If wrap_it is valid, the current position might be in a
8143 word that is wrapped. So, save the iterator in
8144 atpos_it and continue to see if wrapping happens. */
8145 SAVE_IT (atpos_it, *it, atpos_data);
8148 /* Stop when ZV reached.
8149 We used to stop here when TO_CHARPOS reached as well, but that is
8150 too soon if this glyph does not fit on this line. So we handle it
8151 explicitly below. */
8152 if (!get_next_display_element (it))
8154 result = MOVE_POS_MATCH_OR_ZV;
8155 break;
8158 if (it->line_wrap == TRUNCATE)
8160 if (BUFFER_POS_REACHED_P ())
8162 result = MOVE_POS_MATCH_OR_ZV;
8163 break;
8166 else
8168 if (it->line_wrap == WORD_WRAP)
8170 if (IT_DISPLAYING_WHITESPACE (it))
8171 may_wrap = 1;
8172 else if (may_wrap)
8174 /* We have reached a glyph that follows one or more
8175 whitespace characters. If the position is
8176 already found, we are done. */
8177 if (atpos_it.sp >= 0)
8179 RESTORE_IT (it, &atpos_it, atpos_data);
8180 result = MOVE_POS_MATCH_OR_ZV;
8181 goto done;
8183 if (atx_it.sp >= 0)
8185 RESTORE_IT (it, &atx_it, atx_data);
8186 result = MOVE_X_REACHED;
8187 goto done;
8189 /* Otherwise, we can wrap here. */
8190 SAVE_IT (wrap_it, *it, wrap_data);
8191 may_wrap = 0;
8196 /* Remember the line height for the current line, in case
8197 the next element doesn't fit on the line. */
8198 ascent = it->max_ascent;
8199 descent = it->max_descent;
8201 /* The call to produce_glyphs will get the metrics of the
8202 display element IT is loaded with. Record the x-position
8203 before this display element, in case it doesn't fit on the
8204 line. */
8205 x = it->current_x;
8207 PRODUCE_GLYPHS (it);
8209 if (it->area != TEXT_AREA)
8211 prev_method = it->method;
8212 if (it->method == GET_FROM_BUFFER)
8213 prev_pos = IT_CHARPOS (*it);
8214 set_iterator_to_next (it, 1);
8215 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8216 SET_TEXT_POS (this_line_min_pos,
8217 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8218 if (it->bidi_p
8219 && (op & MOVE_TO_POS)
8220 && IT_CHARPOS (*it) > to_charpos
8221 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8222 SAVE_IT (ppos_it, *it, ppos_data);
8223 continue;
8226 /* The number of glyphs we get back in IT->nglyphs will normally
8227 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8228 character on a terminal frame, or (iii) a line end. For the
8229 second case, IT->nglyphs - 1 padding glyphs will be present.
8230 (On X frames, there is only one glyph produced for a
8231 composite character.)
8233 The behavior implemented below means, for continuation lines,
8234 that as many spaces of a TAB as fit on the current line are
8235 displayed there. For terminal frames, as many glyphs of a
8236 multi-glyph character are displayed in the current line, too.
8237 This is what the old redisplay code did, and we keep it that
8238 way. Under X, the whole shape of a complex character must
8239 fit on the line or it will be completely displayed in the
8240 next line.
8242 Note that both for tabs and padding glyphs, all glyphs have
8243 the same width. */
8244 if (it->nglyphs)
8246 /* More than one glyph or glyph doesn't fit on line. All
8247 glyphs have the same width. */
8248 int single_glyph_width = it->pixel_width / it->nglyphs;
8249 int new_x;
8250 int x_before_this_char = x;
8251 int hpos_before_this_char = it->hpos;
8253 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8255 new_x = x + single_glyph_width;
8257 /* We want to leave anything reaching TO_X to the caller. */
8258 if ((op & MOVE_TO_X) && new_x > to_x)
8260 if (BUFFER_POS_REACHED_P ())
8262 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8263 goto buffer_pos_reached;
8264 if (atpos_it.sp < 0)
8266 SAVE_IT (atpos_it, *it, atpos_data);
8267 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8270 else
8272 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8274 it->current_x = x;
8275 result = MOVE_X_REACHED;
8276 break;
8278 if (atx_it.sp < 0)
8280 SAVE_IT (atx_it, *it, atx_data);
8281 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8286 if (/* Lines are continued. */
8287 it->line_wrap != TRUNCATE
8288 && (/* And glyph doesn't fit on the line. */
8289 new_x > it->last_visible_x
8290 /* Or it fits exactly and we're on a window
8291 system frame. */
8292 || (new_x == it->last_visible_x
8293 && FRAME_WINDOW_P (it->f))))
8295 if (/* IT->hpos == 0 means the very first glyph
8296 doesn't fit on the line, e.g. a wide image. */
8297 it->hpos == 0
8298 || (new_x == it->last_visible_x
8299 && FRAME_WINDOW_P (it->f)))
8301 ++it->hpos;
8302 it->current_x = new_x;
8304 /* The character's last glyph just barely fits
8305 in this row. */
8306 if (i == it->nglyphs - 1)
8308 /* If this is the destination position,
8309 return a position *before* it in this row,
8310 now that we know it fits in this row. */
8311 if (BUFFER_POS_REACHED_P ())
8313 if (it->line_wrap != WORD_WRAP
8314 || wrap_it.sp < 0)
8316 it->hpos = hpos_before_this_char;
8317 it->current_x = x_before_this_char;
8318 result = MOVE_POS_MATCH_OR_ZV;
8319 break;
8321 if (it->line_wrap == WORD_WRAP
8322 && atpos_it.sp < 0)
8324 SAVE_IT (atpos_it, *it, atpos_data);
8325 atpos_it.current_x = x_before_this_char;
8326 atpos_it.hpos = hpos_before_this_char;
8330 prev_method = it->method;
8331 if (it->method == GET_FROM_BUFFER)
8332 prev_pos = IT_CHARPOS (*it);
8333 set_iterator_to_next (it, 1);
8334 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8335 SET_TEXT_POS (this_line_min_pos,
8336 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8337 /* On graphical terminals, newlines may
8338 "overflow" into the fringe if
8339 overflow-newline-into-fringe is non-nil.
8340 On text-only terminals, newlines may
8341 overflow into the last glyph on the
8342 display line.*/
8343 if (!FRAME_WINDOW_P (it->f)
8344 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8346 if (!get_next_display_element (it))
8348 result = MOVE_POS_MATCH_OR_ZV;
8349 break;
8351 if (BUFFER_POS_REACHED_P ())
8353 if (ITERATOR_AT_END_OF_LINE_P (it))
8354 result = MOVE_POS_MATCH_OR_ZV;
8355 else
8356 result = MOVE_LINE_CONTINUED;
8357 break;
8359 if (ITERATOR_AT_END_OF_LINE_P (it))
8361 result = MOVE_NEWLINE_OR_CR;
8362 break;
8367 else
8368 IT_RESET_X_ASCENT_DESCENT (it);
8370 if (wrap_it.sp >= 0)
8372 RESTORE_IT (it, &wrap_it, wrap_data);
8373 atpos_it.sp = -1;
8374 atx_it.sp = -1;
8377 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8378 IT_CHARPOS (*it)));
8379 result = MOVE_LINE_CONTINUED;
8380 break;
8383 if (BUFFER_POS_REACHED_P ())
8385 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8386 goto buffer_pos_reached;
8387 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8389 SAVE_IT (atpos_it, *it, atpos_data);
8390 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8394 if (new_x > it->first_visible_x)
8396 /* Glyph is visible. Increment number of glyphs that
8397 would be displayed. */
8398 ++it->hpos;
8402 if (result != MOVE_UNDEFINED)
8403 break;
8405 else if (BUFFER_POS_REACHED_P ())
8407 buffer_pos_reached:
8408 IT_RESET_X_ASCENT_DESCENT (it);
8409 result = MOVE_POS_MATCH_OR_ZV;
8410 break;
8412 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8414 /* Stop when TO_X specified and reached. This check is
8415 necessary here because of lines consisting of a line end,
8416 only. The line end will not produce any glyphs and we
8417 would never get MOVE_X_REACHED. */
8418 xassert (it->nglyphs == 0);
8419 result = MOVE_X_REACHED;
8420 break;
8423 /* Is this a line end? If yes, we're done. */
8424 if (ITERATOR_AT_END_OF_LINE_P (it))
8426 /* If we are past TO_CHARPOS, but never saw any character
8427 positions smaller than TO_CHARPOS, return
8428 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8429 did. */
8430 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8432 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8434 if (IT_CHARPOS (ppos_it) < ZV)
8436 RESTORE_IT (it, &ppos_it, ppos_data);
8437 result = MOVE_POS_MATCH_OR_ZV;
8439 else
8440 goto buffer_pos_reached;
8442 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8443 && IT_CHARPOS (*it) > to_charpos)
8444 goto buffer_pos_reached;
8445 else
8446 result = MOVE_NEWLINE_OR_CR;
8448 else
8449 result = MOVE_NEWLINE_OR_CR;
8450 break;
8453 prev_method = it->method;
8454 if (it->method == GET_FROM_BUFFER)
8455 prev_pos = IT_CHARPOS (*it);
8456 /* The current display element has been consumed. Advance
8457 to the next. */
8458 set_iterator_to_next (it, 1);
8459 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8460 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8461 if (IT_CHARPOS (*it) < to_charpos)
8462 saw_smaller_pos = 1;
8463 if (it->bidi_p
8464 && (op & MOVE_TO_POS)
8465 && IT_CHARPOS (*it) >= to_charpos
8466 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8467 SAVE_IT (ppos_it, *it, ppos_data);
8469 /* Stop if lines are truncated and IT's current x-position is
8470 past the right edge of the window now. */
8471 if (it->line_wrap == TRUNCATE
8472 && it->current_x >= it->last_visible_x)
8474 if (!FRAME_WINDOW_P (it->f)
8475 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8477 int at_eob_p = 0;
8479 if ((at_eob_p = !get_next_display_element (it))
8480 || BUFFER_POS_REACHED_P ()
8481 /* If we are past TO_CHARPOS, but never saw any
8482 character positions smaller than TO_CHARPOS,
8483 return MOVE_POS_MATCH_OR_ZV, like the
8484 unidirectional display did. */
8485 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8486 && !saw_smaller_pos
8487 && IT_CHARPOS (*it) > to_charpos))
8489 if (it->bidi_p
8490 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8491 RESTORE_IT (it, &ppos_it, ppos_data);
8492 result = MOVE_POS_MATCH_OR_ZV;
8493 break;
8495 if (ITERATOR_AT_END_OF_LINE_P (it))
8497 result = MOVE_NEWLINE_OR_CR;
8498 break;
8501 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8502 && !saw_smaller_pos
8503 && IT_CHARPOS (*it) > to_charpos)
8505 if (IT_CHARPOS (ppos_it) < ZV)
8506 RESTORE_IT (it, &ppos_it, ppos_data);
8507 result = MOVE_POS_MATCH_OR_ZV;
8508 break;
8510 result = MOVE_LINE_TRUNCATED;
8511 break;
8513 #undef IT_RESET_X_ASCENT_DESCENT
8516 #undef BUFFER_POS_REACHED_P
8518 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8519 restore the saved iterator. */
8520 if (atpos_it.sp >= 0)
8521 RESTORE_IT (it, &atpos_it, atpos_data);
8522 else if (atx_it.sp >= 0)
8523 RESTORE_IT (it, &atx_it, atx_data);
8525 done:
8527 if (atpos_data)
8528 bidi_unshelve_cache (atpos_data, 1);
8529 if (atx_data)
8530 bidi_unshelve_cache (atx_data, 1);
8531 if (wrap_data)
8532 bidi_unshelve_cache (wrap_data, 1);
8533 if (ppos_data)
8534 bidi_unshelve_cache (ppos_data, 1);
8536 /* Restore the iterator settings altered at the beginning of this
8537 function. */
8538 it->glyph_row = saved_glyph_row;
8539 return result;
8542 /* For external use. */
8543 void
8544 move_it_in_display_line (struct it *it,
8545 EMACS_INT to_charpos, int to_x,
8546 enum move_operation_enum op)
8548 if (it->line_wrap == WORD_WRAP
8549 && (op & MOVE_TO_X))
8551 struct it save_it;
8552 void *save_data = NULL;
8553 int skip;
8555 SAVE_IT (save_it, *it, save_data);
8556 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8557 /* When word-wrap is on, TO_X may lie past the end
8558 of a wrapped line. Then it->current is the
8559 character on the next line, so backtrack to the
8560 space before the wrap point. */
8561 if (skip == MOVE_LINE_CONTINUED)
8563 int prev_x = max (it->current_x - 1, 0);
8564 RESTORE_IT (it, &save_it, save_data);
8565 move_it_in_display_line_to
8566 (it, -1, prev_x, MOVE_TO_X);
8568 else
8569 bidi_unshelve_cache (save_data, 1);
8571 else
8572 move_it_in_display_line_to (it, to_charpos, to_x, op);
8576 /* Move IT forward until it satisfies one or more of the criteria in
8577 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8579 OP is a bit-mask that specifies where to stop, and in particular,
8580 which of those four position arguments makes a difference. See the
8581 description of enum move_operation_enum.
8583 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8584 screen line, this function will set IT to the next position that is
8585 displayed to the right of TO_CHARPOS on the screen. */
8587 void
8588 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
8590 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8591 int line_height, line_start_x = 0, reached = 0;
8592 void *backup_data = NULL;
8594 for (;;)
8596 if (op & MOVE_TO_VPOS)
8598 /* If no TO_CHARPOS and no TO_X specified, stop at the
8599 start of the line TO_VPOS. */
8600 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8602 if (it->vpos == to_vpos)
8604 reached = 1;
8605 break;
8607 else
8608 skip = move_it_in_display_line_to (it, -1, -1, 0);
8610 else
8612 /* TO_VPOS >= 0 means stop at TO_X in the line at
8613 TO_VPOS, or at TO_POS, whichever comes first. */
8614 if (it->vpos == to_vpos)
8616 reached = 2;
8617 break;
8620 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8622 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8624 reached = 3;
8625 break;
8627 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8629 /* We have reached TO_X but not in the line we want. */
8630 skip = move_it_in_display_line_to (it, to_charpos,
8631 -1, MOVE_TO_POS);
8632 if (skip == MOVE_POS_MATCH_OR_ZV)
8634 reached = 4;
8635 break;
8640 else if (op & MOVE_TO_Y)
8642 struct it it_backup;
8644 if (it->line_wrap == WORD_WRAP)
8645 SAVE_IT (it_backup, *it, backup_data);
8647 /* TO_Y specified means stop at TO_X in the line containing
8648 TO_Y---or at TO_CHARPOS if this is reached first. The
8649 problem is that we can't really tell whether the line
8650 contains TO_Y before we have completely scanned it, and
8651 this may skip past TO_X. What we do is to first scan to
8652 TO_X.
8654 If TO_X is not specified, use a TO_X of zero. The reason
8655 is to make the outcome of this function more predictable.
8656 If we didn't use TO_X == 0, we would stop at the end of
8657 the line which is probably not what a caller would expect
8658 to happen. */
8659 skip = move_it_in_display_line_to
8660 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8661 (MOVE_TO_X | (op & MOVE_TO_POS)));
8663 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8664 if (skip == MOVE_POS_MATCH_OR_ZV)
8665 reached = 5;
8666 else if (skip == MOVE_X_REACHED)
8668 /* If TO_X was reached, we want to know whether TO_Y is
8669 in the line. We know this is the case if the already
8670 scanned glyphs make the line tall enough. Otherwise,
8671 we must check by scanning the rest of the line. */
8672 line_height = it->max_ascent + it->max_descent;
8673 if (to_y >= it->current_y
8674 && to_y < it->current_y + line_height)
8676 reached = 6;
8677 break;
8679 SAVE_IT (it_backup, *it, backup_data);
8680 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8681 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8682 op & MOVE_TO_POS);
8683 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8684 line_height = it->max_ascent + it->max_descent;
8685 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8687 if (to_y >= it->current_y
8688 && to_y < it->current_y + line_height)
8690 /* If TO_Y is in this line and TO_X was reached
8691 above, we scanned too far. We have to restore
8692 IT's settings to the ones before skipping. But
8693 keep the more accurate values of max_ascent and
8694 max_descent we've found while skipping the rest
8695 of the line, for the sake of callers, such as
8696 pos_visible_p, that need to know the line
8697 height. */
8698 int max_ascent = it->max_ascent;
8699 int max_descent = it->max_descent;
8701 RESTORE_IT (it, &it_backup, backup_data);
8702 it->max_ascent = max_ascent;
8703 it->max_descent = max_descent;
8704 reached = 6;
8706 else
8708 skip = skip2;
8709 if (skip == MOVE_POS_MATCH_OR_ZV)
8710 reached = 7;
8713 else
8715 /* Check whether TO_Y is in this line. */
8716 line_height = it->max_ascent + it->max_descent;
8717 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8719 if (to_y >= it->current_y
8720 && to_y < it->current_y + line_height)
8722 /* When word-wrap is on, TO_X may lie past the end
8723 of a wrapped line. Then it->current is the
8724 character on the next line, so backtrack to the
8725 space before the wrap point. */
8726 if (skip == MOVE_LINE_CONTINUED
8727 && it->line_wrap == WORD_WRAP)
8729 int prev_x = max (it->current_x - 1, 0);
8730 RESTORE_IT (it, &it_backup, backup_data);
8731 skip = move_it_in_display_line_to
8732 (it, -1, prev_x, MOVE_TO_X);
8734 reached = 6;
8738 if (reached)
8739 break;
8741 else if (BUFFERP (it->object)
8742 && (it->method == GET_FROM_BUFFER
8743 || it->method == GET_FROM_STRETCH)
8744 && IT_CHARPOS (*it) >= to_charpos
8745 /* Under bidi iteration, a call to set_iterator_to_next
8746 can scan far beyond to_charpos if the initial
8747 portion of the next line needs to be reordered. In
8748 that case, give move_it_in_display_line_to another
8749 chance below. */
8750 && !(it->bidi_p
8751 && it->bidi_it.scan_dir == -1))
8752 skip = MOVE_POS_MATCH_OR_ZV;
8753 else
8754 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8756 switch (skip)
8758 case MOVE_POS_MATCH_OR_ZV:
8759 reached = 8;
8760 goto out;
8762 case MOVE_NEWLINE_OR_CR:
8763 set_iterator_to_next (it, 1);
8764 it->continuation_lines_width = 0;
8765 break;
8767 case MOVE_LINE_TRUNCATED:
8768 it->continuation_lines_width = 0;
8769 reseat_at_next_visible_line_start (it, 0);
8770 if ((op & MOVE_TO_POS) != 0
8771 && IT_CHARPOS (*it) > to_charpos)
8773 reached = 9;
8774 goto out;
8776 break;
8778 case MOVE_LINE_CONTINUED:
8779 /* For continued lines ending in a tab, some of the glyphs
8780 associated with the tab are displayed on the current
8781 line. Since it->current_x does not include these glyphs,
8782 we use it->last_visible_x instead. */
8783 if (it->c == '\t')
8785 it->continuation_lines_width += it->last_visible_x;
8786 /* When moving by vpos, ensure that the iterator really
8787 advances to the next line (bug#847, bug#969). Fixme:
8788 do we need to do this in other circumstances? */
8789 if (it->current_x != it->last_visible_x
8790 && (op & MOVE_TO_VPOS)
8791 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8793 line_start_x = it->current_x + it->pixel_width
8794 - it->last_visible_x;
8795 set_iterator_to_next (it, 0);
8798 else
8799 it->continuation_lines_width += it->current_x;
8800 break;
8802 default:
8803 abort ();
8806 /* Reset/increment for the next run. */
8807 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8808 it->current_x = line_start_x;
8809 line_start_x = 0;
8810 it->hpos = 0;
8811 it->current_y += it->max_ascent + it->max_descent;
8812 ++it->vpos;
8813 last_height = it->max_ascent + it->max_descent;
8814 last_max_ascent = it->max_ascent;
8815 it->max_ascent = it->max_descent = 0;
8818 out:
8820 /* On text terminals, we may stop at the end of a line in the middle
8821 of a multi-character glyph. If the glyph itself is continued,
8822 i.e. it is actually displayed on the next line, don't treat this
8823 stopping point as valid; move to the next line instead (unless
8824 that brings us offscreen). */
8825 if (!FRAME_WINDOW_P (it->f)
8826 && op & MOVE_TO_POS
8827 && IT_CHARPOS (*it) == to_charpos
8828 && it->what == IT_CHARACTER
8829 && it->nglyphs > 1
8830 && it->line_wrap == WINDOW_WRAP
8831 && it->current_x == it->last_visible_x - 1
8832 && it->c != '\n'
8833 && it->c != '\t'
8834 && it->vpos < XFASTINT (it->w->window_end_vpos))
8836 it->continuation_lines_width += it->current_x;
8837 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8838 it->current_y += it->max_ascent + it->max_descent;
8839 ++it->vpos;
8840 last_height = it->max_ascent + it->max_descent;
8841 last_max_ascent = it->max_ascent;
8844 if (backup_data)
8845 bidi_unshelve_cache (backup_data, 1);
8847 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8851 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8853 If DY > 0, move IT backward at least that many pixels. DY = 0
8854 means move IT backward to the preceding line start or BEGV. This
8855 function may move over more than DY pixels if IT->current_y - DY
8856 ends up in the middle of a line; in this case IT->current_y will be
8857 set to the top of the line moved to. */
8859 void
8860 move_it_vertically_backward (struct it *it, int dy)
8862 int nlines, h;
8863 struct it it2, it3;
8864 void *it2data = NULL, *it3data = NULL;
8865 EMACS_INT start_pos;
8867 move_further_back:
8868 xassert (dy >= 0);
8870 start_pos = IT_CHARPOS (*it);
8872 /* Estimate how many newlines we must move back. */
8873 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8875 /* Set the iterator's position that many lines back. */
8876 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8877 back_to_previous_visible_line_start (it);
8879 /* Reseat the iterator here. When moving backward, we don't want
8880 reseat to skip forward over invisible text, set up the iterator
8881 to deliver from overlay strings at the new position etc. So,
8882 use reseat_1 here. */
8883 reseat_1 (it, it->current.pos, 1);
8885 /* We are now surely at a line start. */
8886 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8887 reordering is in effect. */
8888 it->continuation_lines_width = 0;
8890 /* Move forward and see what y-distance we moved. First move to the
8891 start of the next line so that we get its height. We need this
8892 height to be able to tell whether we reached the specified
8893 y-distance. */
8894 SAVE_IT (it2, *it, it2data);
8895 it2.max_ascent = it2.max_descent = 0;
8898 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8899 MOVE_TO_POS | MOVE_TO_VPOS);
8901 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8902 /* If we are in a display string which starts at START_POS,
8903 and that display string includes a newline, and we are
8904 right after that newline (i.e. at the beginning of a
8905 display line), exit the loop, because otherwise we will
8906 infloop, since move_it_to will see that it is already at
8907 START_POS and will not move. */
8908 || (it2.method == GET_FROM_STRING
8909 && IT_CHARPOS (it2) == start_pos
8910 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
8911 xassert (IT_CHARPOS (*it) >= BEGV);
8912 SAVE_IT (it3, it2, it3data);
8914 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8915 xassert (IT_CHARPOS (*it) >= BEGV);
8916 /* H is the actual vertical distance from the position in *IT
8917 and the starting position. */
8918 h = it2.current_y - it->current_y;
8919 /* NLINES is the distance in number of lines. */
8920 nlines = it2.vpos - it->vpos;
8922 /* Correct IT's y and vpos position
8923 so that they are relative to the starting point. */
8924 it->vpos -= nlines;
8925 it->current_y -= h;
8927 if (dy == 0)
8929 /* DY == 0 means move to the start of the screen line. The
8930 value of nlines is > 0 if continuation lines were involved,
8931 or if the original IT position was at start of a line. */
8932 RESTORE_IT (it, it, it2data);
8933 if (nlines > 0)
8934 move_it_by_lines (it, nlines);
8935 /* The above code moves us to some position NLINES down,
8936 usually to its first glyph (leftmost in an L2R line), but
8937 that's not necessarily the start of the line, under bidi
8938 reordering. We want to get to the character position
8939 that is immediately after the newline of the previous
8940 line. */
8941 if (it->bidi_p
8942 && !it->continuation_lines_width
8943 && !STRINGP (it->string)
8944 && IT_CHARPOS (*it) > BEGV
8945 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8947 EMACS_INT nl_pos =
8948 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
8950 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
8952 bidi_unshelve_cache (it3data, 1);
8954 else
8956 /* The y-position we try to reach, relative to *IT.
8957 Note that H has been subtracted in front of the if-statement. */
8958 int target_y = it->current_y + h - dy;
8959 int y0 = it3.current_y;
8960 int y1;
8961 int line_height;
8963 RESTORE_IT (&it3, &it3, it3data);
8964 y1 = line_bottom_y (&it3);
8965 line_height = y1 - y0;
8966 RESTORE_IT (it, it, it2data);
8967 /* If we did not reach target_y, try to move further backward if
8968 we can. If we moved too far backward, try to move forward. */
8969 if (target_y < it->current_y
8970 /* This is heuristic. In a window that's 3 lines high, with
8971 a line height of 13 pixels each, recentering with point
8972 on the bottom line will try to move -39/2 = 19 pixels
8973 backward. Try to avoid moving into the first line. */
8974 && (it->current_y - target_y
8975 > min (window_box_height (it->w), line_height * 2 / 3))
8976 && IT_CHARPOS (*it) > BEGV)
8978 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8979 target_y - it->current_y));
8980 dy = it->current_y - target_y;
8981 goto move_further_back;
8983 else if (target_y >= it->current_y + line_height
8984 && IT_CHARPOS (*it) < ZV)
8986 /* Should move forward by at least one line, maybe more.
8988 Note: Calling move_it_by_lines can be expensive on
8989 terminal frames, where compute_motion is used (via
8990 vmotion) to do the job, when there are very long lines
8991 and truncate-lines is nil. That's the reason for
8992 treating terminal frames specially here. */
8994 if (!FRAME_WINDOW_P (it->f))
8995 move_it_vertically (it, target_y - (it->current_y + line_height));
8996 else
9000 move_it_by_lines (it, 1);
9002 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9009 /* Move IT by a specified amount of pixel lines DY. DY negative means
9010 move backwards. DY = 0 means move to start of screen line. At the
9011 end, IT will be on the start of a screen line. */
9013 void
9014 move_it_vertically (struct it *it, int dy)
9016 if (dy <= 0)
9017 move_it_vertically_backward (it, -dy);
9018 else
9020 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9021 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9022 MOVE_TO_POS | MOVE_TO_Y);
9023 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9025 /* If buffer ends in ZV without a newline, move to the start of
9026 the line to satisfy the post-condition. */
9027 if (IT_CHARPOS (*it) == ZV
9028 && ZV > BEGV
9029 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9030 move_it_by_lines (it, 0);
9035 /* Move iterator IT past the end of the text line it is in. */
9037 void
9038 move_it_past_eol (struct it *it)
9040 enum move_it_result rc;
9042 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9043 if (rc == MOVE_NEWLINE_OR_CR)
9044 set_iterator_to_next (it, 0);
9048 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9049 negative means move up. DVPOS == 0 means move to the start of the
9050 screen line.
9052 Optimization idea: If we would know that IT->f doesn't use
9053 a face with proportional font, we could be faster for
9054 truncate-lines nil. */
9056 void
9057 move_it_by_lines (struct it *it, int dvpos)
9060 /* The commented-out optimization uses vmotion on terminals. This
9061 gives bad results, because elements like it->what, on which
9062 callers such as pos_visible_p rely, aren't updated. */
9063 /* struct position pos;
9064 if (!FRAME_WINDOW_P (it->f))
9066 struct text_pos textpos;
9068 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9069 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9070 reseat (it, textpos, 1);
9071 it->vpos += pos.vpos;
9072 it->current_y += pos.vpos;
9074 else */
9076 if (dvpos == 0)
9078 /* DVPOS == 0 means move to the start of the screen line. */
9079 move_it_vertically_backward (it, 0);
9080 /* Let next call to line_bottom_y calculate real line height */
9081 last_height = 0;
9083 else if (dvpos > 0)
9085 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9086 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9088 /* Only move to the next buffer position if we ended up in a
9089 string from display property, not in an overlay string
9090 (before-string or after-string). That is because the
9091 latter don't conceal the underlying buffer position, so
9092 we can ask to move the iterator to the exact position we
9093 are interested in. Note that, even if we are already at
9094 IT_CHARPOS (*it), the call below is not a no-op, as it
9095 will detect that we are at the end of the string, pop the
9096 iterator, and compute it->current_x and it->hpos
9097 correctly. */
9098 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9099 -1, -1, -1, MOVE_TO_POS);
9102 else
9104 struct it it2;
9105 void *it2data = NULL;
9106 EMACS_INT start_charpos, i;
9108 /* Start at the beginning of the screen line containing IT's
9109 position. This may actually move vertically backwards,
9110 in case of overlays, so adjust dvpos accordingly. */
9111 dvpos += it->vpos;
9112 move_it_vertically_backward (it, 0);
9113 dvpos -= it->vpos;
9115 /* Go back -DVPOS visible lines and reseat the iterator there. */
9116 start_charpos = IT_CHARPOS (*it);
9117 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9118 back_to_previous_visible_line_start (it);
9119 reseat (it, it->current.pos, 1);
9121 /* Move further back if we end up in a string or an image. */
9122 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9124 /* First try to move to start of display line. */
9125 dvpos += it->vpos;
9126 move_it_vertically_backward (it, 0);
9127 dvpos -= it->vpos;
9128 if (IT_POS_VALID_AFTER_MOVE_P (it))
9129 break;
9130 /* If start of line is still in string or image,
9131 move further back. */
9132 back_to_previous_visible_line_start (it);
9133 reseat (it, it->current.pos, 1);
9134 dvpos--;
9137 it->current_x = it->hpos = 0;
9139 /* Above call may have moved too far if continuation lines
9140 are involved. Scan forward and see if it did. */
9141 SAVE_IT (it2, *it, it2data);
9142 it2.vpos = it2.current_y = 0;
9143 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9144 it->vpos -= it2.vpos;
9145 it->current_y -= it2.current_y;
9146 it->current_x = it->hpos = 0;
9148 /* If we moved too far back, move IT some lines forward. */
9149 if (it2.vpos > -dvpos)
9151 int delta = it2.vpos + dvpos;
9153 RESTORE_IT (&it2, &it2, it2data);
9154 SAVE_IT (it2, *it, it2data);
9155 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9156 /* Move back again if we got too far ahead. */
9157 if (IT_CHARPOS (*it) >= start_charpos)
9158 RESTORE_IT (it, &it2, it2data);
9159 else
9160 bidi_unshelve_cache (it2data, 1);
9162 else
9163 RESTORE_IT (it, it, it2data);
9167 /* Return 1 if IT points into the middle of a display vector. */
9170 in_display_vector_p (struct it *it)
9172 return (it->method == GET_FROM_DISPLAY_VECTOR
9173 && it->current.dpvec_index > 0
9174 && it->dpvec + it->current.dpvec_index != it->dpend);
9178 /***********************************************************************
9179 Messages
9180 ***********************************************************************/
9183 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9184 to *Messages*. */
9186 void
9187 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9189 Lisp_Object args[3];
9190 Lisp_Object msg, fmt;
9191 char *buffer;
9192 EMACS_INT len;
9193 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9194 USE_SAFE_ALLOCA;
9196 /* Do nothing if called asynchronously. Inserting text into
9197 a buffer may call after-change-functions and alike and
9198 that would means running Lisp asynchronously. */
9199 if (handling_signal)
9200 return;
9202 fmt = msg = Qnil;
9203 GCPRO4 (fmt, msg, arg1, arg2);
9205 args[0] = fmt = build_string (format);
9206 args[1] = arg1;
9207 args[2] = arg2;
9208 msg = Fformat (3, args);
9210 len = SBYTES (msg) + 1;
9211 SAFE_ALLOCA (buffer, char *, len);
9212 memcpy (buffer, SDATA (msg), len);
9214 message_dolog (buffer, len - 1, 1, 0);
9215 SAFE_FREE ();
9217 UNGCPRO;
9221 /* Output a newline in the *Messages* buffer if "needs" one. */
9223 void
9224 message_log_maybe_newline (void)
9226 if (message_log_need_newline)
9227 message_dolog ("", 0, 1, 0);
9231 /* Add a string M of length NBYTES to the message log, optionally
9232 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9233 nonzero, means interpret the contents of M as multibyte. This
9234 function calls low-level routines in order to bypass text property
9235 hooks, etc. which might not be safe to run.
9237 This may GC (insert may run before/after change hooks),
9238 so the buffer M must NOT point to a Lisp string. */
9240 void
9241 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
9243 const unsigned char *msg = (const unsigned char *) m;
9245 if (!NILP (Vmemory_full))
9246 return;
9248 if (!NILP (Vmessage_log_max))
9250 struct buffer *oldbuf;
9251 Lisp_Object oldpoint, oldbegv, oldzv;
9252 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9253 EMACS_INT point_at_end = 0;
9254 EMACS_INT zv_at_end = 0;
9255 Lisp_Object old_deactivate_mark, tem;
9256 struct gcpro gcpro1;
9258 old_deactivate_mark = Vdeactivate_mark;
9259 oldbuf = current_buffer;
9260 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9261 BVAR (current_buffer, undo_list) = Qt;
9263 oldpoint = message_dolog_marker1;
9264 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9265 oldbegv = message_dolog_marker2;
9266 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9267 oldzv = message_dolog_marker3;
9268 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9269 GCPRO1 (old_deactivate_mark);
9271 if (PT == Z)
9272 point_at_end = 1;
9273 if (ZV == Z)
9274 zv_at_end = 1;
9276 BEGV = BEG;
9277 BEGV_BYTE = BEG_BYTE;
9278 ZV = Z;
9279 ZV_BYTE = Z_BYTE;
9280 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9282 /* Insert the string--maybe converting multibyte to single byte
9283 or vice versa, so that all the text fits the buffer. */
9284 if (multibyte
9285 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9287 EMACS_INT i;
9288 int c, char_bytes;
9289 char work[1];
9291 /* Convert a multibyte string to single-byte
9292 for the *Message* buffer. */
9293 for (i = 0; i < nbytes; i += char_bytes)
9295 c = string_char_and_length (msg + i, &char_bytes);
9296 work[0] = (ASCII_CHAR_P (c)
9298 : multibyte_char_to_unibyte (c));
9299 insert_1_both (work, 1, 1, 1, 0, 0);
9302 else if (! multibyte
9303 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9305 EMACS_INT i;
9306 int c, char_bytes;
9307 unsigned char str[MAX_MULTIBYTE_LENGTH];
9308 /* Convert a single-byte string to multibyte
9309 for the *Message* buffer. */
9310 for (i = 0; i < nbytes; i++)
9312 c = msg[i];
9313 MAKE_CHAR_MULTIBYTE (c);
9314 char_bytes = CHAR_STRING (c, str);
9315 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9318 else if (nbytes)
9319 insert_1 (m, nbytes, 1, 0, 0);
9321 if (nlflag)
9323 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9324 printmax_t dups;
9325 insert_1 ("\n", 1, 1, 0, 0);
9327 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9328 this_bol = PT;
9329 this_bol_byte = PT_BYTE;
9331 /* See if this line duplicates the previous one.
9332 If so, combine duplicates. */
9333 if (this_bol > BEG)
9335 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9336 prev_bol = PT;
9337 prev_bol_byte = PT_BYTE;
9339 dups = message_log_check_duplicate (prev_bol_byte,
9340 this_bol_byte);
9341 if (dups)
9343 del_range_both (prev_bol, prev_bol_byte,
9344 this_bol, this_bol_byte, 0);
9345 if (dups > 1)
9347 char dupstr[sizeof " [ times]"
9348 + INT_STRLEN_BOUND (printmax_t)];
9349 int duplen;
9351 /* If you change this format, don't forget to also
9352 change message_log_check_duplicate. */
9353 sprintf (dupstr, " [%"pMd" times]", dups);
9354 duplen = strlen (dupstr);
9355 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9356 insert_1 (dupstr, duplen, 1, 0, 1);
9361 /* If we have more than the desired maximum number of lines
9362 in the *Messages* buffer now, delete the oldest ones.
9363 This is safe because we don't have undo in this buffer. */
9365 if (NATNUMP (Vmessage_log_max))
9367 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9368 -XFASTINT (Vmessage_log_max) - 1, 0);
9369 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9372 BEGV = XMARKER (oldbegv)->charpos;
9373 BEGV_BYTE = marker_byte_position (oldbegv);
9375 if (zv_at_end)
9377 ZV = Z;
9378 ZV_BYTE = Z_BYTE;
9380 else
9382 ZV = XMARKER (oldzv)->charpos;
9383 ZV_BYTE = marker_byte_position (oldzv);
9386 if (point_at_end)
9387 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9388 else
9389 /* We can't do Fgoto_char (oldpoint) because it will run some
9390 Lisp code. */
9391 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9392 XMARKER (oldpoint)->bytepos);
9394 UNGCPRO;
9395 unchain_marker (XMARKER (oldpoint));
9396 unchain_marker (XMARKER (oldbegv));
9397 unchain_marker (XMARKER (oldzv));
9399 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9400 set_buffer_internal (oldbuf);
9401 if (NILP (tem))
9402 windows_or_buffers_changed = old_windows_or_buffers_changed;
9403 message_log_need_newline = !nlflag;
9404 Vdeactivate_mark = old_deactivate_mark;
9409 /* We are at the end of the buffer after just having inserted a newline.
9410 (Note: We depend on the fact we won't be crossing the gap.)
9411 Check to see if the most recent message looks a lot like the previous one.
9412 Return 0 if different, 1 if the new one should just replace it, or a
9413 value N > 1 if we should also append " [N times]". */
9415 static intmax_t
9416 message_log_check_duplicate (EMACS_INT prev_bol_byte, EMACS_INT this_bol_byte)
9418 EMACS_INT i;
9419 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
9420 int seen_dots = 0;
9421 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9422 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9424 for (i = 0; i < len; i++)
9426 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9427 seen_dots = 1;
9428 if (p1[i] != p2[i])
9429 return seen_dots;
9431 p1 += len;
9432 if (*p1 == '\n')
9433 return 2;
9434 if (*p1++ == ' ' && *p1++ == '[')
9436 char *pend;
9437 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9438 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9439 return n+1;
9441 return 0;
9445 /* Display an echo area message M with a specified length of NBYTES
9446 bytes. The string may include null characters. If M is 0, clear
9447 out any existing message, and let the mini-buffer text show
9448 through.
9450 This may GC, so the buffer M must NOT point to a Lisp string. */
9452 void
9453 message2 (const char *m, EMACS_INT nbytes, int multibyte)
9455 /* First flush out any partial line written with print. */
9456 message_log_maybe_newline ();
9457 if (m)
9458 message_dolog (m, nbytes, 1, multibyte);
9459 message2_nolog (m, nbytes, multibyte);
9463 /* The non-logging counterpart of message2. */
9465 void
9466 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
9468 struct frame *sf = SELECTED_FRAME ();
9469 message_enable_multibyte = multibyte;
9471 if (FRAME_INITIAL_P (sf))
9473 if (noninteractive_need_newline)
9474 putc ('\n', stderr);
9475 noninteractive_need_newline = 0;
9476 if (m)
9477 fwrite (m, nbytes, 1, stderr);
9478 if (cursor_in_echo_area == 0)
9479 fprintf (stderr, "\n");
9480 fflush (stderr);
9482 /* A null message buffer means that the frame hasn't really been
9483 initialized yet. Error messages get reported properly by
9484 cmd_error, so this must be just an informative message; toss it. */
9485 else if (INTERACTIVE
9486 && sf->glyphs_initialized_p
9487 && FRAME_MESSAGE_BUF (sf))
9489 Lisp_Object mini_window;
9490 struct frame *f;
9492 /* Get the frame containing the mini-buffer
9493 that the selected frame is using. */
9494 mini_window = FRAME_MINIBUF_WINDOW (sf);
9495 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9497 FRAME_SAMPLE_VISIBILITY (f);
9498 if (FRAME_VISIBLE_P (sf)
9499 && ! FRAME_VISIBLE_P (f))
9500 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9502 if (m)
9504 set_message (m, Qnil, nbytes, multibyte);
9505 if (minibuffer_auto_raise)
9506 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9508 else
9509 clear_message (1, 1);
9511 do_pending_window_change (0);
9512 echo_area_display (1);
9513 do_pending_window_change (0);
9514 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9515 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9520 /* Display an echo area message M with a specified length of NBYTES
9521 bytes. The string may include null characters. If M is not a
9522 string, clear out any existing message, and let the mini-buffer
9523 text show through.
9525 This function cancels echoing. */
9527 void
9528 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9530 struct gcpro gcpro1;
9532 GCPRO1 (m);
9533 clear_message (1,1);
9534 cancel_echoing ();
9536 /* First flush out any partial line written with print. */
9537 message_log_maybe_newline ();
9538 if (STRINGP (m))
9540 char *buffer;
9541 USE_SAFE_ALLOCA;
9543 SAFE_ALLOCA (buffer, char *, nbytes);
9544 memcpy (buffer, SDATA (m), nbytes);
9545 message_dolog (buffer, nbytes, 1, multibyte);
9546 SAFE_FREE ();
9548 message3_nolog (m, nbytes, multibyte);
9550 UNGCPRO;
9554 /* The non-logging version of message3.
9555 This does not cancel echoing, because it is used for echoing.
9556 Perhaps we need to make a separate function for echoing
9557 and make this cancel echoing. */
9559 void
9560 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9562 struct frame *sf = SELECTED_FRAME ();
9563 message_enable_multibyte = multibyte;
9565 if (FRAME_INITIAL_P (sf))
9567 if (noninteractive_need_newline)
9568 putc ('\n', stderr);
9569 noninteractive_need_newline = 0;
9570 if (STRINGP (m))
9571 fwrite (SDATA (m), nbytes, 1, stderr);
9572 if (cursor_in_echo_area == 0)
9573 fprintf (stderr, "\n");
9574 fflush (stderr);
9576 /* A null message buffer means that the frame hasn't really been
9577 initialized yet. Error messages get reported properly by
9578 cmd_error, so this must be just an informative message; toss it. */
9579 else if (INTERACTIVE
9580 && sf->glyphs_initialized_p
9581 && FRAME_MESSAGE_BUF (sf))
9583 Lisp_Object mini_window;
9584 Lisp_Object frame;
9585 struct frame *f;
9587 /* Get the frame containing the mini-buffer
9588 that the selected frame is using. */
9589 mini_window = FRAME_MINIBUF_WINDOW (sf);
9590 frame = XWINDOW (mini_window)->frame;
9591 f = XFRAME (frame);
9593 FRAME_SAMPLE_VISIBILITY (f);
9594 if (FRAME_VISIBLE_P (sf)
9595 && !FRAME_VISIBLE_P (f))
9596 Fmake_frame_visible (frame);
9598 if (STRINGP (m) && SCHARS (m) > 0)
9600 set_message (NULL, m, nbytes, multibyte);
9601 if (minibuffer_auto_raise)
9602 Fraise_frame (frame);
9603 /* Assume we are not echoing.
9604 (If we are, echo_now will override this.) */
9605 echo_message_buffer = Qnil;
9607 else
9608 clear_message (1, 1);
9610 do_pending_window_change (0);
9611 echo_area_display (1);
9612 do_pending_window_change (0);
9613 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9614 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9619 /* Display a null-terminated echo area message M. If M is 0, clear
9620 out any existing message, and let the mini-buffer text show through.
9622 The buffer M must continue to exist until after the echo area gets
9623 cleared or some other message gets displayed there. Do not pass
9624 text that is stored in a Lisp string. Do not pass text in a buffer
9625 that was alloca'd. */
9627 void
9628 message1 (const char *m)
9630 message2 (m, (m ? strlen (m) : 0), 0);
9634 /* The non-logging counterpart of message1. */
9636 void
9637 message1_nolog (const char *m)
9639 message2_nolog (m, (m ? strlen (m) : 0), 0);
9642 /* Display a message M which contains a single %s
9643 which gets replaced with STRING. */
9645 void
9646 message_with_string (const char *m, Lisp_Object string, int log)
9648 CHECK_STRING (string);
9650 if (noninteractive)
9652 if (m)
9654 if (noninteractive_need_newline)
9655 putc ('\n', stderr);
9656 noninteractive_need_newline = 0;
9657 fprintf (stderr, m, SDATA (string));
9658 if (!cursor_in_echo_area)
9659 fprintf (stderr, "\n");
9660 fflush (stderr);
9663 else if (INTERACTIVE)
9665 /* The frame whose minibuffer we're going to display the message on.
9666 It may be larger than the selected frame, so we need
9667 to use its buffer, not the selected frame's buffer. */
9668 Lisp_Object mini_window;
9669 struct frame *f, *sf = SELECTED_FRAME ();
9671 /* Get the frame containing the minibuffer
9672 that the selected frame is using. */
9673 mini_window = FRAME_MINIBUF_WINDOW (sf);
9674 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9676 /* A null message buffer means that the frame hasn't really been
9677 initialized yet. Error messages get reported properly by
9678 cmd_error, so this must be just an informative message; toss it. */
9679 if (FRAME_MESSAGE_BUF (f))
9681 Lisp_Object args[2], msg;
9682 struct gcpro gcpro1, gcpro2;
9684 args[0] = build_string (m);
9685 args[1] = msg = string;
9686 GCPRO2 (args[0], msg);
9687 gcpro1.nvars = 2;
9689 msg = Fformat (2, args);
9691 if (log)
9692 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9693 else
9694 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9696 UNGCPRO;
9698 /* Print should start at the beginning of the message
9699 buffer next time. */
9700 message_buf_print = 0;
9706 /* Dump an informative message to the minibuf. If M is 0, clear out
9707 any existing message, and let the mini-buffer text show through. */
9709 static void
9710 vmessage (const char *m, va_list ap)
9712 if (noninteractive)
9714 if (m)
9716 if (noninteractive_need_newline)
9717 putc ('\n', stderr);
9718 noninteractive_need_newline = 0;
9719 vfprintf (stderr, m, ap);
9720 if (cursor_in_echo_area == 0)
9721 fprintf (stderr, "\n");
9722 fflush (stderr);
9725 else if (INTERACTIVE)
9727 /* The frame whose mini-buffer we're going to display the message
9728 on. It may be larger than the selected frame, so we need to
9729 use its buffer, not the selected frame's buffer. */
9730 Lisp_Object mini_window;
9731 struct frame *f, *sf = SELECTED_FRAME ();
9733 /* Get the frame containing the mini-buffer
9734 that the selected frame is using. */
9735 mini_window = FRAME_MINIBUF_WINDOW (sf);
9736 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9738 /* A null message buffer means that the frame hasn't really been
9739 initialized yet. Error messages get reported properly by
9740 cmd_error, so this must be just an informative message; toss
9741 it. */
9742 if (FRAME_MESSAGE_BUF (f))
9744 if (m)
9746 ptrdiff_t len;
9748 len = doprnt (FRAME_MESSAGE_BUF (f),
9749 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9751 message2 (FRAME_MESSAGE_BUF (f), len, 0);
9753 else
9754 message1 (0);
9756 /* Print should start at the beginning of the message
9757 buffer next time. */
9758 message_buf_print = 0;
9763 void
9764 message (const char *m, ...)
9766 va_list ap;
9767 va_start (ap, m);
9768 vmessage (m, ap);
9769 va_end (ap);
9773 #if 0
9774 /* The non-logging version of message. */
9776 void
9777 message_nolog (const char *m, ...)
9779 Lisp_Object old_log_max;
9780 va_list ap;
9781 va_start (ap, m);
9782 old_log_max = Vmessage_log_max;
9783 Vmessage_log_max = Qnil;
9784 vmessage (m, ap);
9785 Vmessage_log_max = old_log_max;
9786 va_end (ap);
9788 #endif
9791 /* Display the current message in the current mini-buffer. This is
9792 only called from error handlers in process.c, and is not time
9793 critical. */
9795 void
9796 update_echo_area (void)
9798 if (!NILP (echo_area_buffer[0]))
9800 Lisp_Object string;
9801 string = Fcurrent_message ();
9802 message3 (string, SBYTES (string),
9803 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9808 /* Make sure echo area buffers in `echo_buffers' are live.
9809 If they aren't, make new ones. */
9811 static void
9812 ensure_echo_area_buffers (void)
9814 int i;
9816 for (i = 0; i < 2; ++i)
9817 if (!BUFFERP (echo_buffer[i])
9818 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9820 char name[30];
9821 Lisp_Object old_buffer;
9822 int j;
9824 old_buffer = echo_buffer[i];
9825 sprintf (name, " *Echo Area %d*", i);
9826 echo_buffer[i] = Fget_buffer_create (build_string (name));
9827 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9828 /* to force word wrap in echo area -
9829 it was decided to postpone this*/
9830 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9832 for (j = 0; j < 2; ++j)
9833 if (EQ (old_buffer, echo_area_buffer[j]))
9834 echo_area_buffer[j] = echo_buffer[i];
9839 /* Call FN with args A1..A4 with either the current or last displayed
9840 echo_area_buffer as current buffer.
9842 WHICH zero means use the current message buffer
9843 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9844 from echo_buffer[] and clear it.
9846 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9847 suitable buffer from echo_buffer[] and clear it.
9849 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9850 that the current message becomes the last displayed one, make
9851 choose a suitable buffer for echo_area_buffer[0], and clear it.
9853 Value is what FN returns. */
9855 static int
9856 with_echo_area_buffer (struct window *w, int which,
9857 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
9858 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9860 Lisp_Object buffer;
9861 int this_one, the_other, clear_buffer_p, rc;
9862 int count = SPECPDL_INDEX ();
9864 /* If buffers aren't live, make new ones. */
9865 ensure_echo_area_buffers ();
9867 clear_buffer_p = 0;
9869 if (which == 0)
9870 this_one = 0, the_other = 1;
9871 else if (which > 0)
9872 this_one = 1, the_other = 0;
9873 else
9875 this_one = 0, the_other = 1;
9876 clear_buffer_p = 1;
9878 /* We need a fresh one in case the current echo buffer equals
9879 the one containing the last displayed echo area message. */
9880 if (!NILP (echo_area_buffer[this_one])
9881 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9882 echo_area_buffer[this_one] = Qnil;
9885 /* Choose a suitable buffer from echo_buffer[] is we don't
9886 have one. */
9887 if (NILP (echo_area_buffer[this_one]))
9889 echo_area_buffer[this_one]
9890 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9891 ? echo_buffer[the_other]
9892 : echo_buffer[this_one]);
9893 clear_buffer_p = 1;
9896 buffer = echo_area_buffer[this_one];
9898 /* Don't get confused by reusing the buffer used for echoing
9899 for a different purpose. */
9900 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9901 cancel_echoing ();
9903 record_unwind_protect (unwind_with_echo_area_buffer,
9904 with_echo_area_buffer_unwind_data (w));
9906 /* Make the echo area buffer current. Note that for display
9907 purposes, it is not necessary that the displayed window's buffer
9908 == current_buffer, except for text property lookup. So, let's
9909 only set that buffer temporarily here without doing a full
9910 Fset_window_buffer. We must also change w->pointm, though,
9911 because otherwise an assertions in unshow_buffer fails, and Emacs
9912 aborts. */
9913 set_buffer_internal_1 (XBUFFER (buffer));
9914 if (w)
9916 w->buffer = buffer;
9917 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9920 BVAR (current_buffer, undo_list) = Qt;
9921 BVAR (current_buffer, read_only) = Qnil;
9922 specbind (Qinhibit_read_only, Qt);
9923 specbind (Qinhibit_modification_hooks, Qt);
9925 if (clear_buffer_p && Z > BEG)
9926 del_range (BEG, Z);
9928 xassert (BEGV >= BEG);
9929 xassert (ZV <= Z && ZV >= BEGV);
9931 rc = fn (a1, a2, a3, a4);
9933 xassert (BEGV >= BEG);
9934 xassert (ZV <= Z && ZV >= BEGV);
9936 unbind_to (count, Qnil);
9937 return rc;
9941 /* Save state that should be preserved around the call to the function
9942 FN called in with_echo_area_buffer. */
9944 static Lisp_Object
9945 with_echo_area_buffer_unwind_data (struct window *w)
9947 int i = 0;
9948 Lisp_Object vector, tmp;
9950 /* Reduce consing by keeping one vector in
9951 Vwith_echo_area_save_vector. */
9952 vector = Vwith_echo_area_save_vector;
9953 Vwith_echo_area_save_vector = Qnil;
9955 if (NILP (vector))
9956 vector = Fmake_vector (make_number (7), Qnil);
9958 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9959 ASET (vector, i, Vdeactivate_mark); ++i;
9960 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9962 if (w)
9964 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9965 ASET (vector, i, w->buffer); ++i;
9966 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9967 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9969 else
9971 int end = i + 4;
9972 for (; i < end; ++i)
9973 ASET (vector, i, Qnil);
9976 xassert (i == ASIZE (vector));
9977 return vector;
9981 /* Restore global state from VECTOR which was created by
9982 with_echo_area_buffer_unwind_data. */
9984 static Lisp_Object
9985 unwind_with_echo_area_buffer (Lisp_Object vector)
9987 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
9988 Vdeactivate_mark = AREF (vector, 1);
9989 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
9991 if (WINDOWP (AREF (vector, 3)))
9993 struct window *w;
9994 Lisp_Object buffer, charpos, bytepos;
9996 w = XWINDOW (AREF (vector, 3));
9997 buffer = AREF (vector, 4);
9998 charpos = AREF (vector, 5);
9999 bytepos = AREF (vector, 6);
10001 w->buffer = buffer;
10002 set_marker_both (w->pointm, buffer,
10003 XFASTINT (charpos), XFASTINT (bytepos));
10006 Vwith_echo_area_save_vector = vector;
10007 return Qnil;
10011 /* Set up the echo area for use by print functions. MULTIBYTE_P
10012 non-zero means we will print multibyte. */
10014 void
10015 setup_echo_area_for_printing (int multibyte_p)
10017 /* If we can't find an echo area any more, exit. */
10018 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10019 Fkill_emacs (Qnil);
10021 ensure_echo_area_buffers ();
10023 if (!message_buf_print)
10025 /* A message has been output since the last time we printed.
10026 Choose a fresh echo area buffer. */
10027 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10028 echo_area_buffer[0] = echo_buffer[1];
10029 else
10030 echo_area_buffer[0] = echo_buffer[0];
10032 /* Switch to that buffer and clear it. */
10033 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10034 BVAR (current_buffer, truncate_lines) = Qnil;
10036 if (Z > BEG)
10038 int count = SPECPDL_INDEX ();
10039 specbind (Qinhibit_read_only, Qt);
10040 /* Note that undo recording is always disabled. */
10041 del_range (BEG, Z);
10042 unbind_to (count, Qnil);
10044 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10046 /* Set up the buffer for the multibyteness we need. */
10047 if (multibyte_p
10048 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10049 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10051 /* Raise the frame containing the echo area. */
10052 if (minibuffer_auto_raise)
10054 struct frame *sf = SELECTED_FRAME ();
10055 Lisp_Object mini_window;
10056 mini_window = FRAME_MINIBUF_WINDOW (sf);
10057 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10060 message_log_maybe_newline ();
10061 message_buf_print = 1;
10063 else
10065 if (NILP (echo_area_buffer[0]))
10067 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10068 echo_area_buffer[0] = echo_buffer[1];
10069 else
10070 echo_area_buffer[0] = echo_buffer[0];
10073 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10075 /* Someone switched buffers between print requests. */
10076 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10077 BVAR (current_buffer, truncate_lines) = Qnil;
10083 /* Display an echo area message in window W. Value is non-zero if W's
10084 height is changed. If display_last_displayed_message_p is
10085 non-zero, display the message that was last displayed, otherwise
10086 display the current message. */
10088 static int
10089 display_echo_area (struct window *w)
10091 int i, no_message_p, window_height_changed_p, count;
10093 /* Temporarily disable garbage collections while displaying the echo
10094 area. This is done because a GC can print a message itself.
10095 That message would modify the echo area buffer's contents while a
10096 redisplay of the buffer is going on, and seriously confuse
10097 redisplay. */
10098 count = inhibit_garbage_collection ();
10100 /* If there is no message, we must call display_echo_area_1
10101 nevertheless because it resizes the window. But we will have to
10102 reset the echo_area_buffer in question to nil at the end because
10103 with_echo_area_buffer will sets it to an empty buffer. */
10104 i = display_last_displayed_message_p ? 1 : 0;
10105 no_message_p = NILP (echo_area_buffer[i]);
10107 window_height_changed_p
10108 = with_echo_area_buffer (w, display_last_displayed_message_p,
10109 display_echo_area_1,
10110 (intptr_t) w, Qnil, 0, 0);
10112 if (no_message_p)
10113 echo_area_buffer[i] = Qnil;
10115 unbind_to (count, Qnil);
10116 return window_height_changed_p;
10120 /* Helper for display_echo_area. Display the current buffer which
10121 contains the current echo area message in window W, a mini-window,
10122 a pointer to which is passed in A1. A2..A4 are currently not used.
10123 Change the height of W so that all of the message is displayed.
10124 Value is non-zero if height of W was changed. */
10126 static int
10127 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10129 intptr_t i1 = a1;
10130 struct window *w = (struct window *) i1;
10131 Lisp_Object window;
10132 struct text_pos start;
10133 int window_height_changed_p = 0;
10135 /* Do this before displaying, so that we have a large enough glyph
10136 matrix for the display. If we can't get enough space for the
10137 whole text, display the last N lines. That works by setting w->start. */
10138 window_height_changed_p = resize_mini_window (w, 0);
10140 /* Use the starting position chosen by resize_mini_window. */
10141 SET_TEXT_POS_FROM_MARKER (start, w->start);
10143 /* Display. */
10144 clear_glyph_matrix (w->desired_matrix);
10145 XSETWINDOW (window, w);
10146 try_window (window, start, 0);
10148 return window_height_changed_p;
10152 /* Resize the echo area window to exactly the size needed for the
10153 currently displayed message, if there is one. If a mini-buffer
10154 is active, don't shrink it. */
10156 void
10157 resize_echo_area_exactly (void)
10159 if (BUFFERP (echo_area_buffer[0])
10160 && WINDOWP (echo_area_window))
10162 struct window *w = XWINDOW (echo_area_window);
10163 int resized_p;
10164 Lisp_Object resize_exactly;
10166 if (minibuf_level == 0)
10167 resize_exactly = Qt;
10168 else
10169 resize_exactly = Qnil;
10171 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10172 (intptr_t) w, resize_exactly,
10173 0, 0);
10174 if (resized_p)
10176 ++windows_or_buffers_changed;
10177 ++update_mode_lines;
10178 redisplay_internal ();
10184 /* Callback function for with_echo_area_buffer, when used from
10185 resize_echo_area_exactly. A1 contains a pointer to the window to
10186 resize, EXACTLY non-nil means resize the mini-window exactly to the
10187 size of the text displayed. A3 and A4 are not used. Value is what
10188 resize_mini_window returns. */
10190 static int
10191 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
10193 intptr_t i1 = a1;
10194 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10198 /* Resize mini-window W to fit the size of its contents. EXACT_P
10199 means size the window exactly to the size needed. Otherwise, it's
10200 only enlarged until W's buffer is empty.
10202 Set W->start to the right place to begin display. If the whole
10203 contents fit, start at the beginning. Otherwise, start so as
10204 to make the end of the contents appear. This is particularly
10205 important for y-or-n-p, but seems desirable generally.
10207 Value is non-zero if the window height has been changed. */
10210 resize_mini_window (struct window *w, int exact_p)
10212 struct frame *f = XFRAME (w->frame);
10213 int window_height_changed_p = 0;
10215 xassert (MINI_WINDOW_P (w));
10217 /* By default, start display at the beginning. */
10218 set_marker_both (w->start, w->buffer,
10219 BUF_BEGV (XBUFFER (w->buffer)),
10220 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10222 /* Don't resize windows while redisplaying a window; it would
10223 confuse redisplay functions when the size of the window they are
10224 displaying changes from under them. Such a resizing can happen,
10225 for instance, when which-func prints a long message while
10226 we are running fontification-functions. We're running these
10227 functions with safe_call which binds inhibit-redisplay to t. */
10228 if (!NILP (Vinhibit_redisplay))
10229 return 0;
10231 /* Nil means don't try to resize. */
10232 if (NILP (Vresize_mini_windows)
10233 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10234 return 0;
10236 if (!FRAME_MINIBUF_ONLY_P (f))
10238 struct it it;
10239 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10240 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10241 int height, max_height;
10242 int unit = FRAME_LINE_HEIGHT (f);
10243 struct text_pos start;
10244 struct buffer *old_current_buffer = NULL;
10246 if (current_buffer != XBUFFER (w->buffer))
10248 old_current_buffer = current_buffer;
10249 set_buffer_internal (XBUFFER (w->buffer));
10252 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10254 /* Compute the max. number of lines specified by the user. */
10255 if (FLOATP (Vmax_mini_window_height))
10256 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10257 else if (INTEGERP (Vmax_mini_window_height))
10258 max_height = XINT (Vmax_mini_window_height);
10259 else
10260 max_height = total_height / 4;
10262 /* Correct that max. height if it's bogus. */
10263 max_height = max (1, max_height);
10264 max_height = min (total_height, max_height);
10266 /* Find out the height of the text in the window. */
10267 if (it.line_wrap == TRUNCATE)
10268 height = 1;
10269 else
10271 last_height = 0;
10272 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10273 if (it.max_ascent == 0 && it.max_descent == 0)
10274 height = it.current_y + last_height;
10275 else
10276 height = it.current_y + it.max_ascent + it.max_descent;
10277 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10278 height = (height + unit - 1) / unit;
10281 /* Compute a suitable window start. */
10282 if (height > max_height)
10284 height = max_height;
10285 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10286 move_it_vertically_backward (&it, (height - 1) * unit);
10287 start = it.current.pos;
10289 else
10290 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10291 SET_MARKER_FROM_TEXT_POS (w->start, start);
10293 if (EQ (Vresize_mini_windows, Qgrow_only))
10295 /* Let it grow only, until we display an empty message, in which
10296 case the window shrinks again. */
10297 if (height > WINDOW_TOTAL_LINES (w))
10299 int old_height = WINDOW_TOTAL_LINES (w);
10300 freeze_window_starts (f, 1);
10301 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10302 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10304 else if (height < WINDOW_TOTAL_LINES (w)
10305 && (exact_p || BEGV == ZV))
10307 int old_height = WINDOW_TOTAL_LINES (w);
10308 freeze_window_starts (f, 0);
10309 shrink_mini_window (w);
10310 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10313 else
10315 /* Always resize to exact size needed. */
10316 if (height > WINDOW_TOTAL_LINES (w))
10318 int old_height = WINDOW_TOTAL_LINES (w);
10319 freeze_window_starts (f, 1);
10320 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10321 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10323 else if (height < WINDOW_TOTAL_LINES (w))
10325 int old_height = WINDOW_TOTAL_LINES (w);
10326 freeze_window_starts (f, 0);
10327 shrink_mini_window (w);
10329 if (height)
10331 freeze_window_starts (f, 1);
10332 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10335 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10339 if (old_current_buffer)
10340 set_buffer_internal (old_current_buffer);
10343 return window_height_changed_p;
10347 /* Value is the current message, a string, or nil if there is no
10348 current message. */
10350 Lisp_Object
10351 current_message (void)
10353 Lisp_Object msg;
10355 if (!BUFFERP (echo_area_buffer[0]))
10356 msg = Qnil;
10357 else
10359 with_echo_area_buffer (0, 0, current_message_1,
10360 (intptr_t) &msg, Qnil, 0, 0);
10361 if (NILP (msg))
10362 echo_area_buffer[0] = Qnil;
10365 return msg;
10369 static int
10370 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10372 intptr_t i1 = a1;
10373 Lisp_Object *msg = (Lisp_Object *) i1;
10375 if (Z > BEG)
10376 *msg = make_buffer_string (BEG, Z, 1);
10377 else
10378 *msg = Qnil;
10379 return 0;
10383 /* Push the current message on Vmessage_stack for later restoration
10384 by restore_message. Value is non-zero if the current message isn't
10385 empty. This is a relatively infrequent operation, so it's not
10386 worth optimizing. */
10389 push_message (void)
10391 Lisp_Object msg;
10392 msg = current_message ();
10393 Vmessage_stack = Fcons (msg, Vmessage_stack);
10394 return STRINGP (msg);
10398 /* Restore message display from the top of Vmessage_stack. */
10400 void
10401 restore_message (void)
10403 Lisp_Object msg;
10405 xassert (CONSP (Vmessage_stack));
10406 msg = XCAR (Vmessage_stack);
10407 if (STRINGP (msg))
10408 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10409 else
10410 message3_nolog (msg, 0, 0);
10414 /* Handler for record_unwind_protect calling pop_message. */
10416 Lisp_Object
10417 pop_message_unwind (Lisp_Object dummy)
10419 pop_message ();
10420 return Qnil;
10423 /* Pop the top-most entry off Vmessage_stack. */
10425 static void
10426 pop_message (void)
10428 xassert (CONSP (Vmessage_stack));
10429 Vmessage_stack = XCDR (Vmessage_stack);
10433 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10434 exits. If the stack is not empty, we have a missing pop_message
10435 somewhere. */
10437 void
10438 check_message_stack (void)
10440 if (!NILP (Vmessage_stack))
10441 abort ();
10445 /* Truncate to NCHARS what will be displayed in the echo area the next
10446 time we display it---but don't redisplay it now. */
10448 void
10449 truncate_echo_area (EMACS_INT nchars)
10451 if (nchars == 0)
10452 echo_area_buffer[0] = Qnil;
10453 /* A null message buffer means that the frame hasn't really been
10454 initialized yet. Error messages get reported properly by
10455 cmd_error, so this must be just an informative message; toss it. */
10456 else if (!noninteractive
10457 && INTERACTIVE
10458 && !NILP (echo_area_buffer[0]))
10460 struct frame *sf = SELECTED_FRAME ();
10461 if (FRAME_MESSAGE_BUF (sf))
10462 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10467 /* Helper function for truncate_echo_area. Truncate the current
10468 message to at most NCHARS characters. */
10470 static int
10471 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10473 if (BEG + nchars < Z)
10474 del_range (BEG + nchars, Z);
10475 if (Z == BEG)
10476 echo_area_buffer[0] = Qnil;
10477 return 0;
10481 /* Set the current message to a substring of S or STRING.
10483 If STRING is a Lisp string, set the message to the first NBYTES
10484 bytes from STRING. NBYTES zero means use the whole string. If
10485 STRING is multibyte, the message will be displayed multibyte.
10487 If S is not null, set the message to the first LEN bytes of S. LEN
10488 zero means use the whole string. MULTIBYTE_P non-zero means S is
10489 multibyte. Display the message multibyte in that case.
10491 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10492 to t before calling set_message_1 (which calls insert).
10495 static void
10496 set_message (const char *s, Lisp_Object string,
10497 EMACS_INT nbytes, int multibyte_p)
10499 message_enable_multibyte
10500 = ((s && multibyte_p)
10501 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10503 with_echo_area_buffer (0, -1, set_message_1,
10504 (intptr_t) s, string, nbytes, multibyte_p);
10505 message_buf_print = 0;
10506 help_echo_showing_p = 0;
10510 /* Helper function for set_message. Arguments have the same meaning
10511 as there, with A1 corresponding to S and A2 corresponding to STRING
10512 This function is called with the echo area buffer being
10513 current. */
10515 static int
10516 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
10518 intptr_t i1 = a1;
10519 const char *s = (const char *) i1;
10520 const unsigned char *msg = (const unsigned char *) s;
10521 Lisp_Object string = a2;
10523 /* Change multibyteness of the echo buffer appropriately. */
10524 if (message_enable_multibyte
10525 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10526 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10528 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10529 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10530 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10532 /* Insert new message at BEG. */
10533 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10535 if (STRINGP (string))
10537 EMACS_INT nchars;
10539 if (nbytes == 0)
10540 nbytes = SBYTES (string);
10541 nchars = string_byte_to_char (string, nbytes);
10543 /* This function takes care of single/multibyte conversion. We
10544 just have to ensure that the echo area buffer has the right
10545 setting of enable_multibyte_characters. */
10546 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10548 else if (s)
10550 if (nbytes == 0)
10551 nbytes = strlen (s);
10553 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10555 /* Convert from multi-byte to single-byte. */
10556 EMACS_INT i;
10557 int c, n;
10558 char work[1];
10560 /* Convert a multibyte string to single-byte. */
10561 for (i = 0; i < nbytes; i += n)
10563 c = string_char_and_length (msg + i, &n);
10564 work[0] = (ASCII_CHAR_P (c)
10566 : multibyte_char_to_unibyte (c));
10567 insert_1_both (work, 1, 1, 1, 0, 0);
10570 else if (!multibyte_p
10571 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10573 /* Convert from single-byte to multi-byte. */
10574 EMACS_INT i;
10575 int c, n;
10576 unsigned char str[MAX_MULTIBYTE_LENGTH];
10578 /* Convert a single-byte string to multibyte. */
10579 for (i = 0; i < nbytes; i++)
10581 c = msg[i];
10582 MAKE_CHAR_MULTIBYTE (c);
10583 n = CHAR_STRING (c, str);
10584 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10587 else
10588 insert_1 (s, nbytes, 1, 0, 0);
10591 return 0;
10595 /* Clear messages. CURRENT_P non-zero means clear the current
10596 message. LAST_DISPLAYED_P non-zero means clear the message
10597 last displayed. */
10599 void
10600 clear_message (int current_p, int last_displayed_p)
10602 if (current_p)
10604 echo_area_buffer[0] = Qnil;
10605 message_cleared_p = 1;
10608 if (last_displayed_p)
10609 echo_area_buffer[1] = Qnil;
10611 message_buf_print = 0;
10614 /* Clear garbaged frames.
10616 This function is used where the old redisplay called
10617 redraw_garbaged_frames which in turn called redraw_frame which in
10618 turn called clear_frame. The call to clear_frame was a source of
10619 flickering. I believe a clear_frame is not necessary. It should
10620 suffice in the new redisplay to invalidate all current matrices,
10621 and ensure a complete redisplay of all windows. */
10623 static void
10624 clear_garbaged_frames (void)
10626 if (frame_garbaged)
10628 Lisp_Object tail, frame;
10629 int changed_count = 0;
10631 FOR_EACH_FRAME (tail, frame)
10633 struct frame *f = XFRAME (frame);
10635 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10637 if (f->resized_p)
10639 Fredraw_frame (frame);
10640 f->force_flush_display_p = 1;
10642 clear_current_matrices (f);
10643 changed_count++;
10644 f->garbaged = 0;
10645 f->resized_p = 0;
10649 frame_garbaged = 0;
10650 if (changed_count)
10651 ++windows_or_buffers_changed;
10656 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10657 is non-zero update selected_frame. Value is non-zero if the
10658 mini-windows height has been changed. */
10660 static int
10661 echo_area_display (int update_frame_p)
10663 Lisp_Object mini_window;
10664 struct window *w;
10665 struct frame *f;
10666 int window_height_changed_p = 0;
10667 struct frame *sf = SELECTED_FRAME ();
10669 mini_window = FRAME_MINIBUF_WINDOW (sf);
10670 w = XWINDOW (mini_window);
10671 f = XFRAME (WINDOW_FRAME (w));
10673 /* Don't display if frame is invisible or not yet initialized. */
10674 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10675 return 0;
10677 #ifdef HAVE_WINDOW_SYSTEM
10678 /* When Emacs starts, selected_frame may be the initial terminal
10679 frame. If we let this through, a message would be displayed on
10680 the terminal. */
10681 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10682 return 0;
10683 #endif /* HAVE_WINDOW_SYSTEM */
10685 /* Redraw garbaged frames. */
10686 if (frame_garbaged)
10687 clear_garbaged_frames ();
10689 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10691 echo_area_window = mini_window;
10692 window_height_changed_p = display_echo_area (w);
10693 w->must_be_updated_p = 1;
10695 /* Update the display, unless called from redisplay_internal.
10696 Also don't update the screen during redisplay itself. The
10697 update will happen at the end of redisplay, and an update
10698 here could cause confusion. */
10699 if (update_frame_p && !redisplaying_p)
10701 int n = 0;
10703 /* If the display update has been interrupted by pending
10704 input, update mode lines in the frame. Due to the
10705 pending input, it might have been that redisplay hasn't
10706 been called, so that mode lines above the echo area are
10707 garbaged. This looks odd, so we prevent it here. */
10708 if (!display_completed)
10709 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10711 if (window_height_changed_p
10712 /* Don't do this if Emacs is shutting down. Redisplay
10713 needs to run hooks. */
10714 && !NILP (Vrun_hooks))
10716 /* Must update other windows. Likewise as in other
10717 cases, don't let this update be interrupted by
10718 pending input. */
10719 int count = SPECPDL_INDEX ();
10720 specbind (Qredisplay_dont_pause, Qt);
10721 windows_or_buffers_changed = 1;
10722 redisplay_internal ();
10723 unbind_to (count, Qnil);
10725 else if (FRAME_WINDOW_P (f) && n == 0)
10727 /* Window configuration is the same as before.
10728 Can do with a display update of the echo area,
10729 unless we displayed some mode lines. */
10730 update_single_window (w, 1);
10731 FRAME_RIF (f)->flush_display (f);
10733 else
10734 update_frame (f, 1, 1);
10736 /* If cursor is in the echo area, make sure that the next
10737 redisplay displays the minibuffer, so that the cursor will
10738 be replaced with what the minibuffer wants. */
10739 if (cursor_in_echo_area)
10740 ++windows_or_buffers_changed;
10743 else if (!EQ (mini_window, selected_window))
10744 windows_or_buffers_changed++;
10746 /* Last displayed message is now the current message. */
10747 echo_area_buffer[1] = echo_area_buffer[0];
10748 /* Inform read_char that we're not echoing. */
10749 echo_message_buffer = Qnil;
10751 /* Prevent redisplay optimization in redisplay_internal by resetting
10752 this_line_start_pos. This is done because the mini-buffer now
10753 displays the message instead of its buffer text. */
10754 if (EQ (mini_window, selected_window))
10755 CHARPOS (this_line_start_pos) = 0;
10757 return window_height_changed_p;
10762 /***********************************************************************
10763 Mode Lines and Frame Titles
10764 ***********************************************************************/
10766 /* A buffer for constructing non-propertized mode-line strings and
10767 frame titles in it; allocated from the heap in init_xdisp and
10768 resized as needed in store_mode_line_noprop_char. */
10770 static char *mode_line_noprop_buf;
10772 /* The buffer's end, and a current output position in it. */
10774 static char *mode_line_noprop_buf_end;
10775 static char *mode_line_noprop_ptr;
10777 #define MODE_LINE_NOPROP_LEN(start) \
10778 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10780 static enum {
10781 MODE_LINE_DISPLAY = 0,
10782 MODE_LINE_TITLE,
10783 MODE_LINE_NOPROP,
10784 MODE_LINE_STRING
10785 } mode_line_target;
10787 /* Alist that caches the results of :propertize.
10788 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10789 static Lisp_Object mode_line_proptrans_alist;
10791 /* List of strings making up the mode-line. */
10792 static Lisp_Object mode_line_string_list;
10794 /* Base face property when building propertized mode line string. */
10795 static Lisp_Object mode_line_string_face;
10796 static Lisp_Object mode_line_string_face_prop;
10799 /* Unwind data for mode line strings */
10801 static Lisp_Object Vmode_line_unwind_vector;
10803 static Lisp_Object
10804 format_mode_line_unwind_data (struct buffer *obuf,
10805 Lisp_Object owin,
10806 int save_proptrans)
10808 Lisp_Object vector, tmp;
10810 /* Reduce consing by keeping one vector in
10811 Vwith_echo_area_save_vector. */
10812 vector = Vmode_line_unwind_vector;
10813 Vmode_line_unwind_vector = Qnil;
10815 if (NILP (vector))
10816 vector = Fmake_vector (make_number (8), Qnil);
10818 ASET (vector, 0, make_number (mode_line_target));
10819 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10820 ASET (vector, 2, mode_line_string_list);
10821 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10822 ASET (vector, 4, mode_line_string_face);
10823 ASET (vector, 5, mode_line_string_face_prop);
10825 if (obuf)
10826 XSETBUFFER (tmp, obuf);
10827 else
10828 tmp = Qnil;
10829 ASET (vector, 6, tmp);
10830 ASET (vector, 7, owin);
10832 return vector;
10835 static Lisp_Object
10836 unwind_format_mode_line (Lisp_Object vector)
10838 mode_line_target = XINT (AREF (vector, 0));
10839 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10840 mode_line_string_list = AREF (vector, 2);
10841 if (! EQ (AREF (vector, 3), Qt))
10842 mode_line_proptrans_alist = AREF (vector, 3);
10843 mode_line_string_face = AREF (vector, 4);
10844 mode_line_string_face_prop = AREF (vector, 5);
10846 if (!NILP (AREF (vector, 7)))
10847 /* Select window before buffer, since it may change the buffer. */
10848 Fselect_window (AREF (vector, 7), Qt);
10850 if (!NILP (AREF (vector, 6)))
10852 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10853 ASET (vector, 6, Qnil);
10856 Vmode_line_unwind_vector = vector;
10857 return Qnil;
10861 /* Store a single character C for the frame title in mode_line_noprop_buf.
10862 Re-allocate mode_line_noprop_buf if necessary. */
10864 static void
10865 store_mode_line_noprop_char (char c)
10867 /* If output position has reached the end of the allocated buffer,
10868 increase the buffer's size. */
10869 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10871 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10872 ptrdiff_t size = len;
10873 mode_line_noprop_buf =
10874 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10875 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10876 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10879 *mode_line_noprop_ptr++ = c;
10883 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10884 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10885 characters that yield more columns than PRECISION; PRECISION <= 0
10886 means copy the whole string. Pad with spaces until FIELD_WIDTH
10887 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10888 pad. Called from display_mode_element when it is used to build a
10889 frame title. */
10891 static int
10892 store_mode_line_noprop (const char *string, int field_width, int precision)
10894 const unsigned char *str = (const unsigned char *) string;
10895 int n = 0;
10896 EMACS_INT dummy, nbytes;
10898 /* Copy at most PRECISION chars from STR. */
10899 nbytes = strlen (string);
10900 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10901 while (nbytes--)
10902 store_mode_line_noprop_char (*str++);
10904 /* Fill up with spaces until FIELD_WIDTH reached. */
10905 while (field_width > 0
10906 && n < field_width)
10908 store_mode_line_noprop_char (' ');
10909 ++n;
10912 return n;
10915 /***********************************************************************
10916 Frame Titles
10917 ***********************************************************************/
10919 #ifdef HAVE_WINDOW_SYSTEM
10921 /* Set the title of FRAME, if it has changed. The title format is
10922 Vicon_title_format if FRAME is iconified, otherwise it is
10923 frame_title_format. */
10925 static void
10926 x_consider_frame_title (Lisp_Object frame)
10928 struct frame *f = XFRAME (frame);
10930 if (FRAME_WINDOW_P (f)
10931 || FRAME_MINIBUF_ONLY_P (f)
10932 || f->explicit_name)
10934 /* Do we have more than one visible frame on this X display? */
10935 Lisp_Object tail;
10936 Lisp_Object fmt;
10937 ptrdiff_t title_start;
10938 char *title;
10939 ptrdiff_t len;
10940 struct it it;
10941 int count = SPECPDL_INDEX ();
10943 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
10945 Lisp_Object other_frame = XCAR (tail);
10946 struct frame *tf = XFRAME (other_frame);
10948 if (tf != f
10949 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10950 && !FRAME_MINIBUF_ONLY_P (tf)
10951 && !EQ (other_frame, tip_frame)
10952 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10953 break;
10956 /* Set global variable indicating that multiple frames exist. */
10957 multiple_frames = CONSP (tail);
10959 /* Switch to the buffer of selected window of the frame. Set up
10960 mode_line_target so that display_mode_element will output into
10961 mode_line_noprop_buf; then display the title. */
10962 record_unwind_protect (unwind_format_mode_line,
10963 format_mode_line_unwind_data
10964 (current_buffer, selected_window, 0));
10966 Fselect_window (f->selected_window, Qt);
10967 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
10968 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
10970 mode_line_target = MODE_LINE_TITLE;
10971 title_start = MODE_LINE_NOPROP_LEN (0);
10972 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
10973 NULL, DEFAULT_FACE_ID);
10974 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
10975 len = MODE_LINE_NOPROP_LEN (title_start);
10976 title = mode_line_noprop_buf + title_start;
10977 unbind_to (count, Qnil);
10979 /* Set the title only if it's changed. This avoids consing in
10980 the common case where it hasn't. (If it turns out that we've
10981 already wasted too much time by walking through the list with
10982 display_mode_element, then we might need to optimize at a
10983 higher level than this.) */
10984 if (! STRINGP (f->name)
10985 || SBYTES (f->name) != len
10986 || memcmp (title, SDATA (f->name), len) != 0)
10987 x_implicitly_set_name (f, make_string (title, len), Qnil);
10991 #endif /* not HAVE_WINDOW_SYSTEM */
10996 /***********************************************************************
10997 Menu Bars
10998 ***********************************************************************/
11001 /* Prepare for redisplay by updating menu-bar item lists when
11002 appropriate. This can call eval. */
11004 void
11005 prepare_menu_bars (void)
11007 int all_windows;
11008 struct gcpro gcpro1, gcpro2;
11009 struct frame *f;
11010 Lisp_Object tooltip_frame;
11012 #ifdef HAVE_WINDOW_SYSTEM
11013 tooltip_frame = tip_frame;
11014 #else
11015 tooltip_frame = Qnil;
11016 #endif
11018 /* Update all frame titles based on their buffer names, etc. We do
11019 this before the menu bars so that the buffer-menu will show the
11020 up-to-date frame titles. */
11021 #ifdef HAVE_WINDOW_SYSTEM
11022 if (windows_or_buffers_changed || update_mode_lines)
11024 Lisp_Object tail, frame;
11026 FOR_EACH_FRAME (tail, frame)
11028 f = XFRAME (frame);
11029 if (!EQ (frame, tooltip_frame)
11030 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11031 x_consider_frame_title (frame);
11034 #endif /* HAVE_WINDOW_SYSTEM */
11036 /* Update the menu bar item lists, if appropriate. This has to be
11037 done before any actual redisplay or generation of display lines. */
11038 all_windows = (update_mode_lines
11039 || buffer_shared > 1
11040 || windows_or_buffers_changed);
11041 if (all_windows)
11043 Lisp_Object tail, frame;
11044 int count = SPECPDL_INDEX ();
11045 /* 1 means that update_menu_bar has run its hooks
11046 so any further calls to update_menu_bar shouldn't do so again. */
11047 int menu_bar_hooks_run = 0;
11049 record_unwind_save_match_data ();
11051 FOR_EACH_FRAME (tail, frame)
11053 f = XFRAME (frame);
11055 /* Ignore tooltip frame. */
11056 if (EQ (frame, tooltip_frame))
11057 continue;
11059 /* If a window on this frame changed size, report that to
11060 the user and clear the size-change flag. */
11061 if (FRAME_WINDOW_SIZES_CHANGED (f))
11063 Lisp_Object functions;
11065 /* Clear flag first in case we get an error below. */
11066 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11067 functions = Vwindow_size_change_functions;
11068 GCPRO2 (tail, functions);
11070 while (CONSP (functions))
11072 if (!EQ (XCAR (functions), Qt))
11073 call1 (XCAR (functions), frame);
11074 functions = XCDR (functions);
11076 UNGCPRO;
11079 GCPRO1 (tail);
11080 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11081 #ifdef HAVE_WINDOW_SYSTEM
11082 update_tool_bar (f, 0);
11083 #endif
11084 #ifdef HAVE_NS
11085 if (windows_or_buffers_changed
11086 && FRAME_NS_P (f))
11087 ns_set_doc_edited (f, Fbuffer_modified_p
11088 (XWINDOW (f->selected_window)->buffer));
11089 #endif
11090 UNGCPRO;
11093 unbind_to (count, Qnil);
11095 else
11097 struct frame *sf = SELECTED_FRAME ();
11098 update_menu_bar (sf, 1, 0);
11099 #ifdef HAVE_WINDOW_SYSTEM
11100 update_tool_bar (sf, 1);
11101 #endif
11106 /* Update the menu bar item list for frame F. This has to be done
11107 before we start to fill in any display lines, because it can call
11108 eval.
11110 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11112 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11113 already ran the menu bar hooks for this redisplay, so there
11114 is no need to run them again. The return value is the
11115 updated value of this flag, to pass to the next call. */
11117 static int
11118 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11120 Lisp_Object window;
11121 register struct window *w;
11123 /* If called recursively during a menu update, do nothing. This can
11124 happen when, for instance, an activate-menubar-hook causes a
11125 redisplay. */
11126 if (inhibit_menubar_update)
11127 return hooks_run;
11129 window = FRAME_SELECTED_WINDOW (f);
11130 w = XWINDOW (window);
11132 if (FRAME_WINDOW_P (f)
11134 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11135 || defined (HAVE_NS) || defined (USE_GTK)
11136 FRAME_EXTERNAL_MENU_BAR (f)
11137 #else
11138 FRAME_MENU_BAR_LINES (f) > 0
11139 #endif
11140 : FRAME_MENU_BAR_LINES (f) > 0)
11142 /* If the user has switched buffers or windows, we need to
11143 recompute to reflect the new bindings. But we'll
11144 recompute when update_mode_lines is set too; that means
11145 that people can use force-mode-line-update to request
11146 that the menu bar be recomputed. The adverse effect on
11147 the rest of the redisplay algorithm is about the same as
11148 windows_or_buffers_changed anyway. */
11149 if (windows_or_buffers_changed
11150 /* This used to test w->update_mode_line, but we believe
11151 there is no need to recompute the menu in that case. */
11152 || update_mode_lines
11153 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11154 < BUF_MODIFF (XBUFFER (w->buffer)))
11155 != !NILP (w->last_had_star))
11156 || ((!NILP (Vtransient_mark_mode)
11157 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11158 != !NILP (w->region_showing)))
11160 struct buffer *prev = current_buffer;
11161 int count = SPECPDL_INDEX ();
11163 specbind (Qinhibit_menubar_update, Qt);
11165 set_buffer_internal_1 (XBUFFER (w->buffer));
11166 if (save_match_data)
11167 record_unwind_save_match_data ();
11168 if (NILP (Voverriding_local_map_menu_flag))
11170 specbind (Qoverriding_terminal_local_map, Qnil);
11171 specbind (Qoverriding_local_map, Qnil);
11174 if (!hooks_run)
11176 /* Run the Lucid hook. */
11177 safe_run_hooks (Qactivate_menubar_hook);
11179 /* If it has changed current-menubar from previous value,
11180 really recompute the menu-bar from the value. */
11181 if (! NILP (Vlucid_menu_bar_dirty_flag))
11182 call0 (Qrecompute_lucid_menubar);
11184 safe_run_hooks (Qmenu_bar_update_hook);
11186 hooks_run = 1;
11189 XSETFRAME (Vmenu_updating_frame, f);
11190 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
11192 /* Redisplay the menu bar in case we changed it. */
11193 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11194 || defined (HAVE_NS) || defined (USE_GTK)
11195 if (FRAME_WINDOW_P (f))
11197 #if defined (HAVE_NS)
11198 /* All frames on Mac OS share the same menubar. So only
11199 the selected frame should be allowed to set it. */
11200 if (f == SELECTED_FRAME ())
11201 #endif
11202 set_frame_menubar (f, 0, 0);
11204 else
11205 /* On a terminal screen, the menu bar is an ordinary screen
11206 line, and this makes it get updated. */
11207 w->update_mode_line = Qt;
11208 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11209 /* In the non-toolkit version, the menu bar is an ordinary screen
11210 line, and this makes it get updated. */
11211 w->update_mode_line = Qt;
11212 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11214 unbind_to (count, Qnil);
11215 set_buffer_internal_1 (prev);
11219 return hooks_run;
11224 /***********************************************************************
11225 Output Cursor
11226 ***********************************************************************/
11228 #ifdef HAVE_WINDOW_SYSTEM
11230 /* EXPORT:
11231 Nominal cursor position -- where to draw output.
11232 HPOS and VPOS are window relative glyph matrix coordinates.
11233 X and Y are window relative pixel coordinates. */
11235 struct cursor_pos output_cursor;
11238 /* EXPORT:
11239 Set the global variable output_cursor to CURSOR. All cursor
11240 positions are relative to updated_window. */
11242 void
11243 set_output_cursor (struct cursor_pos *cursor)
11245 output_cursor.hpos = cursor->hpos;
11246 output_cursor.vpos = cursor->vpos;
11247 output_cursor.x = cursor->x;
11248 output_cursor.y = cursor->y;
11252 /* EXPORT for RIF:
11253 Set a nominal cursor position.
11255 HPOS and VPOS are column/row positions in a window glyph matrix. X
11256 and Y are window text area relative pixel positions.
11258 If this is done during an update, updated_window will contain the
11259 window that is being updated and the position is the future output
11260 cursor position for that window. If updated_window is null, use
11261 selected_window and display the cursor at the given position. */
11263 void
11264 x_cursor_to (int vpos, int hpos, int y, int x)
11266 struct window *w;
11268 /* If updated_window is not set, work on selected_window. */
11269 if (updated_window)
11270 w = updated_window;
11271 else
11272 w = XWINDOW (selected_window);
11274 /* Set the output cursor. */
11275 output_cursor.hpos = hpos;
11276 output_cursor.vpos = vpos;
11277 output_cursor.x = x;
11278 output_cursor.y = y;
11280 /* If not called as part of an update, really display the cursor.
11281 This will also set the cursor position of W. */
11282 if (updated_window == NULL)
11284 BLOCK_INPUT;
11285 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11286 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11287 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11288 UNBLOCK_INPUT;
11292 #endif /* HAVE_WINDOW_SYSTEM */
11295 /***********************************************************************
11296 Tool-bars
11297 ***********************************************************************/
11299 #ifdef HAVE_WINDOW_SYSTEM
11301 /* Where the mouse was last time we reported a mouse event. */
11303 FRAME_PTR last_mouse_frame;
11305 /* Tool-bar item index of the item on which a mouse button was pressed
11306 or -1. */
11308 int last_tool_bar_item;
11311 static Lisp_Object
11312 update_tool_bar_unwind (Lisp_Object frame)
11314 selected_frame = frame;
11315 return Qnil;
11318 /* Update the tool-bar item list for frame F. This has to be done
11319 before we start to fill in any display lines. Called from
11320 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11321 and restore it here. */
11323 static void
11324 update_tool_bar (struct frame *f, int save_match_data)
11326 #if defined (USE_GTK) || defined (HAVE_NS)
11327 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11328 #else
11329 int do_update = WINDOWP (f->tool_bar_window)
11330 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11331 #endif
11333 if (do_update)
11335 Lisp_Object window;
11336 struct window *w;
11338 window = FRAME_SELECTED_WINDOW (f);
11339 w = XWINDOW (window);
11341 /* If the user has switched buffers or windows, we need to
11342 recompute to reflect the new bindings. But we'll
11343 recompute when update_mode_lines is set too; that means
11344 that people can use force-mode-line-update to request
11345 that the menu bar be recomputed. The adverse effect on
11346 the rest of the redisplay algorithm is about the same as
11347 windows_or_buffers_changed anyway. */
11348 if (windows_or_buffers_changed
11349 || !NILP (w->update_mode_line)
11350 || update_mode_lines
11351 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11352 < BUF_MODIFF (XBUFFER (w->buffer)))
11353 != !NILP (w->last_had_star))
11354 || ((!NILP (Vtransient_mark_mode)
11355 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11356 != !NILP (w->region_showing)))
11358 struct buffer *prev = current_buffer;
11359 int count = SPECPDL_INDEX ();
11360 Lisp_Object frame, new_tool_bar;
11361 int new_n_tool_bar;
11362 struct gcpro gcpro1;
11364 /* Set current_buffer to the buffer of the selected
11365 window of the frame, so that we get the right local
11366 keymaps. */
11367 set_buffer_internal_1 (XBUFFER (w->buffer));
11369 /* Save match data, if we must. */
11370 if (save_match_data)
11371 record_unwind_save_match_data ();
11373 /* Make sure that we don't accidentally use bogus keymaps. */
11374 if (NILP (Voverriding_local_map_menu_flag))
11376 specbind (Qoverriding_terminal_local_map, Qnil);
11377 specbind (Qoverriding_local_map, Qnil);
11380 GCPRO1 (new_tool_bar);
11382 /* We must temporarily set the selected frame to this frame
11383 before calling tool_bar_items, because the calculation of
11384 the tool-bar keymap uses the selected frame (see
11385 `tool-bar-make-keymap' in tool-bar.el). */
11386 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11387 XSETFRAME (frame, f);
11388 selected_frame = frame;
11390 /* Build desired tool-bar items from keymaps. */
11391 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11392 &new_n_tool_bar);
11394 /* Redisplay the tool-bar if we changed it. */
11395 if (new_n_tool_bar != f->n_tool_bar_items
11396 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11398 /* Redisplay that happens asynchronously due to an expose event
11399 may access f->tool_bar_items. Make sure we update both
11400 variables within BLOCK_INPUT so no such event interrupts. */
11401 BLOCK_INPUT;
11402 f->tool_bar_items = new_tool_bar;
11403 f->n_tool_bar_items = new_n_tool_bar;
11404 w->update_mode_line = Qt;
11405 UNBLOCK_INPUT;
11408 UNGCPRO;
11410 unbind_to (count, Qnil);
11411 set_buffer_internal_1 (prev);
11417 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11418 F's desired tool-bar contents. F->tool_bar_items must have
11419 been set up previously by calling prepare_menu_bars. */
11421 static void
11422 build_desired_tool_bar_string (struct frame *f)
11424 int i, size, size_needed;
11425 struct gcpro gcpro1, gcpro2, gcpro3;
11426 Lisp_Object image, plist, props;
11428 image = plist = props = Qnil;
11429 GCPRO3 (image, plist, props);
11431 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11432 Otherwise, make a new string. */
11434 /* The size of the string we might be able to reuse. */
11435 size = (STRINGP (f->desired_tool_bar_string)
11436 ? SCHARS (f->desired_tool_bar_string)
11437 : 0);
11439 /* We need one space in the string for each image. */
11440 size_needed = f->n_tool_bar_items;
11442 /* Reuse f->desired_tool_bar_string, if possible. */
11443 if (size < size_needed || NILP (f->desired_tool_bar_string))
11444 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
11445 make_number (' '));
11446 else
11448 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11449 Fremove_text_properties (make_number (0), make_number (size),
11450 props, f->desired_tool_bar_string);
11453 /* Put a `display' property on the string for the images to display,
11454 put a `menu_item' property on tool-bar items with a value that
11455 is the index of the item in F's tool-bar item vector. */
11456 for (i = 0; i < f->n_tool_bar_items; ++i)
11458 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11460 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11461 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11462 int hmargin, vmargin, relief, idx, end;
11464 /* If image is a vector, choose the image according to the
11465 button state. */
11466 image = PROP (TOOL_BAR_ITEM_IMAGES);
11467 if (VECTORP (image))
11469 if (enabled_p)
11470 idx = (selected_p
11471 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11472 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11473 else
11474 idx = (selected_p
11475 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11476 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11478 xassert (ASIZE (image) >= idx);
11479 image = AREF (image, idx);
11481 else
11482 idx = -1;
11484 /* Ignore invalid image specifications. */
11485 if (!valid_image_p (image))
11486 continue;
11488 /* Display the tool-bar button pressed, or depressed. */
11489 plist = Fcopy_sequence (XCDR (image));
11491 /* Compute margin and relief to draw. */
11492 relief = (tool_bar_button_relief >= 0
11493 ? tool_bar_button_relief
11494 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11495 hmargin = vmargin = relief;
11497 if (INTEGERP (Vtool_bar_button_margin)
11498 && XINT (Vtool_bar_button_margin) > 0)
11500 hmargin += XFASTINT (Vtool_bar_button_margin);
11501 vmargin += XFASTINT (Vtool_bar_button_margin);
11503 else if (CONSP (Vtool_bar_button_margin))
11505 if (INTEGERP (XCAR (Vtool_bar_button_margin))
11506 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
11507 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11509 if (INTEGERP (XCDR (Vtool_bar_button_margin))
11510 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
11511 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11514 if (auto_raise_tool_bar_buttons_p)
11516 /* Add a `:relief' property to the image spec if the item is
11517 selected. */
11518 if (selected_p)
11520 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11521 hmargin -= relief;
11522 vmargin -= relief;
11525 else
11527 /* If image is selected, display it pressed, i.e. with a
11528 negative relief. If it's not selected, display it with a
11529 raised relief. */
11530 plist = Fplist_put (plist, QCrelief,
11531 (selected_p
11532 ? make_number (-relief)
11533 : make_number (relief)));
11534 hmargin -= relief;
11535 vmargin -= relief;
11538 /* Put a margin around the image. */
11539 if (hmargin || vmargin)
11541 if (hmargin == vmargin)
11542 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11543 else
11544 plist = Fplist_put (plist, QCmargin,
11545 Fcons (make_number (hmargin),
11546 make_number (vmargin)));
11549 /* If button is not enabled, and we don't have special images
11550 for the disabled state, make the image appear disabled by
11551 applying an appropriate algorithm to it. */
11552 if (!enabled_p && idx < 0)
11553 plist = Fplist_put (plist, QCconversion, Qdisabled);
11555 /* Put a `display' text property on the string for the image to
11556 display. Put a `menu-item' property on the string that gives
11557 the start of this item's properties in the tool-bar items
11558 vector. */
11559 image = Fcons (Qimage, plist);
11560 props = list4 (Qdisplay, image,
11561 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11563 /* Let the last image hide all remaining spaces in the tool bar
11564 string. The string can be longer than needed when we reuse a
11565 previous string. */
11566 if (i + 1 == f->n_tool_bar_items)
11567 end = SCHARS (f->desired_tool_bar_string);
11568 else
11569 end = i + 1;
11570 Fadd_text_properties (make_number (i), make_number (end),
11571 props, f->desired_tool_bar_string);
11572 #undef PROP
11575 UNGCPRO;
11579 /* Display one line of the tool-bar of frame IT->f.
11581 HEIGHT specifies the desired height of the tool-bar line.
11582 If the actual height of the glyph row is less than HEIGHT, the
11583 row's height is increased to HEIGHT, and the icons are centered
11584 vertically in the new height.
11586 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11587 count a final empty row in case the tool-bar width exactly matches
11588 the window width.
11591 static void
11592 display_tool_bar_line (struct it *it, int height)
11594 struct glyph_row *row = it->glyph_row;
11595 int max_x = it->last_visible_x;
11596 struct glyph *last;
11598 prepare_desired_row (row);
11599 row->y = it->current_y;
11601 /* Note that this isn't made use of if the face hasn't a box,
11602 so there's no need to check the face here. */
11603 it->start_of_box_run_p = 1;
11605 while (it->current_x < max_x)
11607 int x, n_glyphs_before, i, nglyphs;
11608 struct it it_before;
11610 /* Get the next display element. */
11611 if (!get_next_display_element (it))
11613 /* Don't count empty row if we are counting needed tool-bar lines. */
11614 if (height < 0 && !it->hpos)
11615 return;
11616 break;
11619 /* Produce glyphs. */
11620 n_glyphs_before = row->used[TEXT_AREA];
11621 it_before = *it;
11623 PRODUCE_GLYPHS (it);
11625 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11626 i = 0;
11627 x = it_before.current_x;
11628 while (i < nglyphs)
11630 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11632 if (x + glyph->pixel_width > max_x)
11634 /* Glyph doesn't fit on line. Backtrack. */
11635 row->used[TEXT_AREA] = n_glyphs_before;
11636 *it = it_before;
11637 /* If this is the only glyph on this line, it will never fit on the
11638 tool-bar, so skip it. But ensure there is at least one glyph,
11639 so we don't accidentally disable the tool-bar. */
11640 if (n_glyphs_before == 0
11641 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11642 break;
11643 goto out;
11646 ++it->hpos;
11647 x += glyph->pixel_width;
11648 ++i;
11651 /* Stop at line end. */
11652 if (ITERATOR_AT_END_OF_LINE_P (it))
11653 break;
11655 set_iterator_to_next (it, 1);
11658 out:;
11660 row->displays_text_p = row->used[TEXT_AREA] != 0;
11662 /* Use default face for the border below the tool bar.
11664 FIXME: When auto-resize-tool-bars is grow-only, there is
11665 no additional border below the possibly empty tool-bar lines.
11666 So to make the extra empty lines look "normal", we have to
11667 use the tool-bar face for the border too. */
11668 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11669 it->face_id = DEFAULT_FACE_ID;
11671 extend_face_to_end_of_line (it);
11672 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11673 last->right_box_line_p = 1;
11674 if (last == row->glyphs[TEXT_AREA])
11675 last->left_box_line_p = 1;
11677 /* Make line the desired height and center it vertically. */
11678 if ((height -= it->max_ascent + it->max_descent) > 0)
11680 /* Don't add more than one line height. */
11681 height %= FRAME_LINE_HEIGHT (it->f);
11682 it->max_ascent += height / 2;
11683 it->max_descent += (height + 1) / 2;
11686 compute_line_metrics (it);
11688 /* If line is empty, make it occupy the rest of the tool-bar. */
11689 if (!row->displays_text_p)
11691 row->height = row->phys_height = it->last_visible_y - row->y;
11692 row->visible_height = row->height;
11693 row->ascent = row->phys_ascent = 0;
11694 row->extra_line_spacing = 0;
11697 row->full_width_p = 1;
11698 row->continued_p = 0;
11699 row->truncated_on_left_p = 0;
11700 row->truncated_on_right_p = 0;
11702 it->current_x = it->hpos = 0;
11703 it->current_y += row->height;
11704 ++it->vpos;
11705 ++it->glyph_row;
11709 /* Max tool-bar height. */
11711 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11712 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11714 /* Value is the number of screen lines needed to make all tool-bar
11715 items of frame F visible. The number of actual rows needed is
11716 returned in *N_ROWS if non-NULL. */
11718 static int
11719 tool_bar_lines_needed (struct frame *f, int *n_rows)
11721 struct window *w = XWINDOW (f->tool_bar_window);
11722 struct it it;
11723 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11724 the desired matrix, so use (unused) mode-line row as temporary row to
11725 avoid destroying the first tool-bar row. */
11726 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11728 /* Initialize an iterator for iteration over
11729 F->desired_tool_bar_string in the tool-bar window of frame F. */
11730 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11731 it.first_visible_x = 0;
11732 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11733 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11734 it.paragraph_embedding = L2R;
11736 while (!ITERATOR_AT_END_P (&it))
11738 clear_glyph_row (temp_row);
11739 it.glyph_row = temp_row;
11740 display_tool_bar_line (&it, -1);
11742 clear_glyph_row (temp_row);
11744 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11745 if (n_rows)
11746 *n_rows = it.vpos > 0 ? it.vpos : -1;
11748 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11752 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11753 0, 1, 0,
11754 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11755 (Lisp_Object frame)
11757 struct frame *f;
11758 struct window *w;
11759 int nlines = 0;
11761 if (NILP (frame))
11762 frame = selected_frame;
11763 else
11764 CHECK_FRAME (frame);
11765 f = XFRAME (frame);
11767 if (WINDOWP (f->tool_bar_window)
11768 && (w = XWINDOW (f->tool_bar_window),
11769 WINDOW_TOTAL_LINES (w) > 0))
11771 update_tool_bar (f, 1);
11772 if (f->n_tool_bar_items)
11774 build_desired_tool_bar_string (f);
11775 nlines = tool_bar_lines_needed (f, NULL);
11779 return make_number (nlines);
11783 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11784 height should be changed. */
11786 static int
11787 redisplay_tool_bar (struct frame *f)
11789 struct window *w;
11790 struct it it;
11791 struct glyph_row *row;
11793 #if defined (USE_GTK) || defined (HAVE_NS)
11794 if (FRAME_EXTERNAL_TOOL_BAR (f))
11795 update_frame_tool_bar (f);
11796 return 0;
11797 #endif
11799 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11800 do anything. This means you must start with tool-bar-lines
11801 non-zero to get the auto-sizing effect. Or in other words, you
11802 can turn off tool-bars by specifying tool-bar-lines zero. */
11803 if (!WINDOWP (f->tool_bar_window)
11804 || (w = XWINDOW (f->tool_bar_window),
11805 WINDOW_TOTAL_LINES (w) == 0))
11806 return 0;
11808 /* Set up an iterator for the tool-bar window. */
11809 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11810 it.first_visible_x = 0;
11811 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11812 row = it.glyph_row;
11814 /* Build a string that represents the contents of the tool-bar. */
11815 build_desired_tool_bar_string (f);
11816 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11817 /* FIXME: This should be controlled by a user option. But it
11818 doesn't make sense to have an R2L tool bar if the menu bar cannot
11819 be drawn also R2L, and making the menu bar R2L is tricky due
11820 toolkit-specific code that implements it. If an R2L tool bar is
11821 ever supported, display_tool_bar_line should also be augmented to
11822 call unproduce_glyphs like display_line and display_string
11823 do. */
11824 it.paragraph_embedding = L2R;
11826 if (f->n_tool_bar_rows == 0)
11828 int nlines;
11830 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11831 nlines != WINDOW_TOTAL_LINES (w)))
11833 Lisp_Object frame;
11834 int old_height = WINDOW_TOTAL_LINES (w);
11836 XSETFRAME (frame, f);
11837 Fmodify_frame_parameters (frame,
11838 Fcons (Fcons (Qtool_bar_lines,
11839 make_number (nlines)),
11840 Qnil));
11841 if (WINDOW_TOTAL_LINES (w) != old_height)
11843 clear_glyph_matrix (w->desired_matrix);
11844 fonts_changed_p = 1;
11845 return 1;
11850 /* Display as many lines as needed to display all tool-bar items. */
11852 if (f->n_tool_bar_rows > 0)
11854 int border, rows, height, extra;
11856 if (INTEGERP (Vtool_bar_border))
11857 border = XINT (Vtool_bar_border);
11858 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11859 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11860 else if (EQ (Vtool_bar_border, Qborder_width))
11861 border = f->border_width;
11862 else
11863 border = 0;
11864 if (border < 0)
11865 border = 0;
11867 rows = f->n_tool_bar_rows;
11868 height = max (1, (it.last_visible_y - border) / rows);
11869 extra = it.last_visible_y - border - height * rows;
11871 while (it.current_y < it.last_visible_y)
11873 int h = 0;
11874 if (extra > 0 && rows-- > 0)
11876 h = (extra + rows - 1) / rows;
11877 extra -= h;
11879 display_tool_bar_line (&it, height + h);
11882 else
11884 while (it.current_y < it.last_visible_y)
11885 display_tool_bar_line (&it, 0);
11888 /* It doesn't make much sense to try scrolling in the tool-bar
11889 window, so don't do it. */
11890 w->desired_matrix->no_scrolling_p = 1;
11891 w->must_be_updated_p = 1;
11893 if (!NILP (Vauto_resize_tool_bars))
11895 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11896 int change_height_p = 0;
11898 /* If we couldn't display everything, change the tool-bar's
11899 height if there is room for more. */
11900 if (IT_STRING_CHARPOS (it) < it.end_charpos
11901 && it.current_y < max_tool_bar_height)
11902 change_height_p = 1;
11904 row = it.glyph_row - 1;
11906 /* If there are blank lines at the end, except for a partially
11907 visible blank line at the end that is smaller than
11908 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11909 if (!row->displays_text_p
11910 && row->height >= FRAME_LINE_HEIGHT (f))
11911 change_height_p = 1;
11913 /* If row displays tool-bar items, but is partially visible,
11914 change the tool-bar's height. */
11915 if (row->displays_text_p
11916 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11917 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11918 change_height_p = 1;
11920 /* Resize windows as needed by changing the `tool-bar-lines'
11921 frame parameter. */
11922 if (change_height_p)
11924 Lisp_Object frame;
11925 int old_height = WINDOW_TOTAL_LINES (w);
11926 int nrows;
11927 int nlines = tool_bar_lines_needed (f, &nrows);
11929 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11930 && !f->minimize_tool_bar_window_p)
11931 ? (nlines > old_height)
11932 : (nlines != old_height));
11933 f->minimize_tool_bar_window_p = 0;
11935 if (change_height_p)
11937 XSETFRAME (frame, f);
11938 Fmodify_frame_parameters (frame,
11939 Fcons (Fcons (Qtool_bar_lines,
11940 make_number (nlines)),
11941 Qnil));
11942 if (WINDOW_TOTAL_LINES (w) != old_height)
11944 clear_glyph_matrix (w->desired_matrix);
11945 f->n_tool_bar_rows = nrows;
11946 fonts_changed_p = 1;
11947 return 1;
11953 f->minimize_tool_bar_window_p = 0;
11954 return 0;
11958 /* Get information about the tool-bar item which is displayed in GLYPH
11959 on frame F. Return in *PROP_IDX the index where tool-bar item
11960 properties start in F->tool_bar_items. Value is zero if
11961 GLYPH doesn't display a tool-bar item. */
11963 static int
11964 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11966 Lisp_Object prop;
11967 int success_p;
11968 int charpos;
11970 /* This function can be called asynchronously, which means we must
11971 exclude any possibility that Fget_text_property signals an
11972 error. */
11973 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
11974 charpos = max (0, charpos);
11976 /* Get the text property `menu-item' at pos. The value of that
11977 property is the start index of this item's properties in
11978 F->tool_bar_items. */
11979 prop = Fget_text_property (make_number (charpos),
11980 Qmenu_item, f->current_tool_bar_string);
11981 if (INTEGERP (prop))
11983 *prop_idx = XINT (prop);
11984 success_p = 1;
11986 else
11987 success_p = 0;
11989 return success_p;
11993 /* Get information about the tool-bar item at position X/Y on frame F.
11994 Return in *GLYPH a pointer to the glyph of the tool-bar item in
11995 the current matrix of the tool-bar window of F, or NULL if not
11996 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
11997 item in F->tool_bar_items. Value is
11999 -1 if X/Y is not on a tool-bar item
12000 0 if X/Y is on the same item that was highlighted before.
12001 1 otherwise. */
12003 static int
12004 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12005 int *hpos, int *vpos, int *prop_idx)
12007 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12008 struct window *w = XWINDOW (f->tool_bar_window);
12009 int area;
12011 /* Find the glyph under X/Y. */
12012 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12013 if (*glyph == NULL)
12014 return -1;
12016 /* Get the start of this tool-bar item's properties in
12017 f->tool_bar_items. */
12018 if (!tool_bar_item_info (f, *glyph, prop_idx))
12019 return -1;
12021 /* Is mouse on the highlighted item? */
12022 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12023 && *vpos >= hlinfo->mouse_face_beg_row
12024 && *vpos <= hlinfo->mouse_face_end_row
12025 && (*vpos > hlinfo->mouse_face_beg_row
12026 || *hpos >= hlinfo->mouse_face_beg_col)
12027 && (*vpos < hlinfo->mouse_face_end_row
12028 || *hpos < hlinfo->mouse_face_end_col
12029 || hlinfo->mouse_face_past_end))
12030 return 0;
12032 return 1;
12036 /* EXPORT:
12037 Handle mouse button event on the tool-bar of frame F, at
12038 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12039 0 for button release. MODIFIERS is event modifiers for button
12040 release. */
12042 void
12043 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12044 unsigned int modifiers)
12046 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12047 struct window *w = XWINDOW (f->tool_bar_window);
12048 int hpos, vpos, prop_idx;
12049 struct glyph *glyph;
12050 Lisp_Object enabled_p;
12052 /* If not on the highlighted tool-bar item, return. */
12053 frame_to_window_pixel_xy (w, &x, &y);
12054 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12055 return;
12057 /* If item is disabled, do nothing. */
12058 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12059 if (NILP (enabled_p))
12060 return;
12062 if (down_p)
12064 /* Show item in pressed state. */
12065 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12066 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
12067 last_tool_bar_item = prop_idx;
12069 else
12071 Lisp_Object key, frame;
12072 struct input_event event;
12073 EVENT_INIT (event);
12075 /* Show item in released state. */
12076 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12077 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
12079 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12081 XSETFRAME (frame, f);
12082 event.kind = TOOL_BAR_EVENT;
12083 event.frame_or_window = frame;
12084 event.arg = frame;
12085 kbd_buffer_store_event (&event);
12087 event.kind = TOOL_BAR_EVENT;
12088 event.frame_or_window = frame;
12089 event.arg = key;
12090 event.modifiers = modifiers;
12091 kbd_buffer_store_event (&event);
12092 last_tool_bar_item = -1;
12097 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12098 tool-bar window-relative coordinates X/Y. Called from
12099 note_mouse_highlight. */
12101 static void
12102 note_tool_bar_highlight (struct frame *f, int x, int y)
12104 Lisp_Object window = f->tool_bar_window;
12105 struct window *w = XWINDOW (window);
12106 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12107 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12108 int hpos, vpos;
12109 struct glyph *glyph;
12110 struct glyph_row *row;
12111 int i;
12112 Lisp_Object enabled_p;
12113 int prop_idx;
12114 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12115 int mouse_down_p, rc;
12117 /* Function note_mouse_highlight is called with negative X/Y
12118 values when mouse moves outside of the frame. */
12119 if (x <= 0 || y <= 0)
12121 clear_mouse_face (hlinfo);
12122 return;
12125 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12126 if (rc < 0)
12128 /* Not on tool-bar item. */
12129 clear_mouse_face (hlinfo);
12130 return;
12132 else if (rc == 0)
12133 /* On same tool-bar item as before. */
12134 goto set_help_echo;
12136 clear_mouse_face (hlinfo);
12138 /* Mouse is down, but on different tool-bar item? */
12139 mouse_down_p = (dpyinfo->grabbed
12140 && f == last_mouse_frame
12141 && FRAME_LIVE_P (f));
12142 if (mouse_down_p
12143 && last_tool_bar_item != prop_idx)
12144 return;
12146 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12147 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12149 /* If tool-bar item is not enabled, don't highlight it. */
12150 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12151 if (!NILP (enabled_p))
12153 /* Compute the x-position of the glyph. In front and past the
12154 image is a space. We include this in the highlighted area. */
12155 row = MATRIX_ROW (w->current_matrix, vpos);
12156 for (i = x = 0; i < hpos; ++i)
12157 x += row->glyphs[TEXT_AREA][i].pixel_width;
12159 /* Record this as the current active region. */
12160 hlinfo->mouse_face_beg_col = hpos;
12161 hlinfo->mouse_face_beg_row = vpos;
12162 hlinfo->mouse_face_beg_x = x;
12163 hlinfo->mouse_face_beg_y = row->y;
12164 hlinfo->mouse_face_past_end = 0;
12166 hlinfo->mouse_face_end_col = hpos + 1;
12167 hlinfo->mouse_face_end_row = vpos;
12168 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12169 hlinfo->mouse_face_end_y = row->y;
12170 hlinfo->mouse_face_window = window;
12171 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12173 /* Display it as active. */
12174 show_mouse_face (hlinfo, draw);
12175 hlinfo->mouse_face_image_state = draw;
12178 set_help_echo:
12180 /* Set help_echo_string to a help string to display for this tool-bar item.
12181 XTread_socket does the rest. */
12182 help_echo_object = help_echo_window = Qnil;
12183 help_echo_pos = -1;
12184 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12185 if (NILP (help_echo_string))
12186 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12189 #endif /* HAVE_WINDOW_SYSTEM */
12193 /************************************************************************
12194 Horizontal scrolling
12195 ************************************************************************/
12197 static int hscroll_window_tree (Lisp_Object);
12198 static int hscroll_windows (Lisp_Object);
12200 /* For all leaf windows in the window tree rooted at WINDOW, set their
12201 hscroll value so that PT is (i) visible in the window, and (ii) so
12202 that it is not within a certain margin at the window's left and
12203 right border. Value is non-zero if any window's hscroll has been
12204 changed. */
12206 static int
12207 hscroll_window_tree (Lisp_Object window)
12209 int hscrolled_p = 0;
12210 int hscroll_relative_p = FLOATP (Vhscroll_step);
12211 int hscroll_step_abs = 0;
12212 double hscroll_step_rel = 0;
12214 if (hscroll_relative_p)
12216 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12217 if (hscroll_step_rel < 0)
12219 hscroll_relative_p = 0;
12220 hscroll_step_abs = 0;
12223 else if (INTEGERP (Vhscroll_step))
12225 hscroll_step_abs = XINT (Vhscroll_step);
12226 if (hscroll_step_abs < 0)
12227 hscroll_step_abs = 0;
12229 else
12230 hscroll_step_abs = 0;
12232 while (WINDOWP (window))
12234 struct window *w = XWINDOW (window);
12236 if (WINDOWP (w->hchild))
12237 hscrolled_p |= hscroll_window_tree (w->hchild);
12238 else if (WINDOWP (w->vchild))
12239 hscrolled_p |= hscroll_window_tree (w->vchild);
12240 else if (w->cursor.vpos >= 0)
12242 int h_margin;
12243 int text_area_width;
12244 struct glyph_row *current_cursor_row
12245 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12246 struct glyph_row *desired_cursor_row
12247 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12248 struct glyph_row *cursor_row
12249 = (desired_cursor_row->enabled_p
12250 ? desired_cursor_row
12251 : current_cursor_row);
12252 int row_r2l_p = cursor_row->reversed_p;
12254 text_area_width = window_box_width (w, TEXT_AREA);
12256 /* Scroll when cursor is inside this scroll margin. */
12257 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12259 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12260 /* For left-to-right rows, hscroll when cursor is either
12261 (i) inside the right hscroll margin, or (ii) if it is
12262 inside the left margin and the window is already
12263 hscrolled. */
12264 && ((!row_r2l_p
12265 && ((XFASTINT (w->hscroll)
12266 && w->cursor.x <= h_margin)
12267 || (cursor_row->enabled_p
12268 && cursor_row->truncated_on_right_p
12269 && (w->cursor.x >= text_area_width - h_margin))))
12270 /* For right-to-left rows, the logic is similar,
12271 except that rules for scrolling to left and right
12272 are reversed. E.g., if cursor.x <= h_margin, we
12273 need to hscroll "to the right" unconditionally,
12274 and that will scroll the screen to the left so as
12275 to reveal the next portion of the row. */
12276 || (row_r2l_p
12277 && ((cursor_row->enabled_p
12278 /* FIXME: It is confusing to set the
12279 truncated_on_right_p flag when R2L rows
12280 are actually truncated on the left. */
12281 && cursor_row->truncated_on_right_p
12282 && w->cursor.x <= h_margin)
12283 || (XFASTINT (w->hscroll)
12284 && (w->cursor.x >= text_area_width - h_margin))))))
12286 struct it it;
12287 int hscroll;
12288 struct buffer *saved_current_buffer;
12289 EMACS_INT pt;
12290 int wanted_x;
12292 /* Find point in a display of infinite width. */
12293 saved_current_buffer = current_buffer;
12294 current_buffer = XBUFFER (w->buffer);
12296 if (w == XWINDOW (selected_window))
12297 pt = PT;
12298 else
12300 pt = marker_position (w->pointm);
12301 pt = max (BEGV, pt);
12302 pt = min (ZV, pt);
12305 /* Move iterator to pt starting at cursor_row->start in
12306 a line with infinite width. */
12307 init_to_row_start (&it, w, cursor_row);
12308 it.last_visible_x = INFINITY;
12309 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12310 current_buffer = saved_current_buffer;
12312 /* Position cursor in window. */
12313 if (!hscroll_relative_p && hscroll_step_abs == 0)
12314 hscroll = max (0, (it.current_x
12315 - (ITERATOR_AT_END_OF_LINE_P (&it)
12316 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12317 : (text_area_width / 2))))
12318 / FRAME_COLUMN_WIDTH (it.f);
12319 else if ((!row_r2l_p
12320 && w->cursor.x >= text_area_width - h_margin)
12321 || (row_r2l_p && w->cursor.x <= h_margin))
12323 if (hscroll_relative_p)
12324 wanted_x = text_area_width * (1 - hscroll_step_rel)
12325 - h_margin;
12326 else
12327 wanted_x = text_area_width
12328 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12329 - h_margin;
12330 hscroll
12331 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12333 else
12335 if (hscroll_relative_p)
12336 wanted_x = text_area_width * hscroll_step_rel
12337 + h_margin;
12338 else
12339 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12340 + h_margin;
12341 hscroll
12342 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12344 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
12346 /* Don't prevent redisplay optimizations if hscroll
12347 hasn't changed, as it will unnecessarily slow down
12348 redisplay. */
12349 if (XFASTINT (w->hscroll) != hscroll)
12351 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12352 w->hscroll = make_number (hscroll);
12353 hscrolled_p = 1;
12358 window = w->next;
12361 /* Value is non-zero if hscroll of any leaf window has been changed. */
12362 return hscrolled_p;
12366 /* Set hscroll so that cursor is visible and not inside horizontal
12367 scroll margins for all windows in the tree rooted at WINDOW. See
12368 also hscroll_window_tree above. Value is non-zero if any window's
12369 hscroll has been changed. If it has, desired matrices on the frame
12370 of WINDOW are cleared. */
12372 static int
12373 hscroll_windows (Lisp_Object window)
12375 int hscrolled_p = hscroll_window_tree (window);
12376 if (hscrolled_p)
12377 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12378 return hscrolled_p;
12383 /************************************************************************
12384 Redisplay
12385 ************************************************************************/
12387 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12388 to a non-zero value. This is sometimes handy to have in a debugger
12389 session. */
12391 #if GLYPH_DEBUG
12393 /* First and last unchanged row for try_window_id. */
12395 static int debug_first_unchanged_at_end_vpos;
12396 static int debug_last_unchanged_at_beg_vpos;
12398 /* Delta vpos and y. */
12400 static int debug_dvpos, debug_dy;
12402 /* Delta in characters and bytes for try_window_id. */
12404 static EMACS_INT debug_delta, debug_delta_bytes;
12406 /* Values of window_end_pos and window_end_vpos at the end of
12407 try_window_id. */
12409 static EMACS_INT debug_end_vpos;
12411 /* Append a string to W->desired_matrix->method. FMT is a printf
12412 format string. If trace_redisplay_p is non-zero also printf the
12413 resulting string to stderr. */
12415 static void debug_method_add (struct window *, char const *, ...)
12416 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12418 static void
12419 debug_method_add (struct window *w, char const *fmt, ...)
12421 char buffer[512];
12422 char *method = w->desired_matrix->method;
12423 int len = strlen (method);
12424 int size = sizeof w->desired_matrix->method;
12425 int remaining = size - len - 1;
12426 va_list ap;
12428 va_start (ap, fmt);
12429 vsprintf (buffer, fmt, ap);
12430 va_end (ap);
12431 if (len && remaining)
12433 method[len] = '|';
12434 --remaining, ++len;
12437 strncpy (method + len, buffer, remaining);
12439 if (trace_redisplay_p)
12440 fprintf (stderr, "%p (%s): %s\n",
12442 ((BUFFERP (w->buffer)
12443 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12444 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12445 : "no buffer"),
12446 buffer);
12449 #endif /* GLYPH_DEBUG */
12452 /* Value is non-zero if all changes in window W, which displays
12453 current_buffer, are in the text between START and END. START is a
12454 buffer position, END is given as a distance from Z. Used in
12455 redisplay_internal for display optimization. */
12457 static inline int
12458 text_outside_line_unchanged_p (struct window *w,
12459 EMACS_INT start, EMACS_INT end)
12461 int unchanged_p = 1;
12463 /* If text or overlays have changed, see where. */
12464 if (XFASTINT (w->last_modified) < MODIFF
12465 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12467 /* Gap in the line? */
12468 if (GPT < start || Z - GPT < end)
12469 unchanged_p = 0;
12471 /* Changes start in front of the line, or end after it? */
12472 if (unchanged_p
12473 && (BEG_UNCHANGED < start - 1
12474 || END_UNCHANGED < end))
12475 unchanged_p = 0;
12477 /* If selective display, can't optimize if changes start at the
12478 beginning of the line. */
12479 if (unchanged_p
12480 && INTEGERP (BVAR (current_buffer, selective_display))
12481 && XINT (BVAR (current_buffer, selective_display)) > 0
12482 && (BEG_UNCHANGED < start || GPT <= start))
12483 unchanged_p = 0;
12485 /* If there are overlays at the start or end of the line, these
12486 may have overlay strings with newlines in them. A change at
12487 START, for instance, may actually concern the display of such
12488 overlay strings as well, and they are displayed on different
12489 lines. So, quickly rule out this case. (For the future, it
12490 might be desirable to implement something more telling than
12491 just BEG/END_UNCHANGED.) */
12492 if (unchanged_p)
12494 if (BEG + BEG_UNCHANGED == start
12495 && overlay_touches_p (start))
12496 unchanged_p = 0;
12497 if (END_UNCHANGED == end
12498 && overlay_touches_p (Z - end))
12499 unchanged_p = 0;
12502 /* Under bidi reordering, adding or deleting a character in the
12503 beginning of a paragraph, before the first strong directional
12504 character, can change the base direction of the paragraph (unless
12505 the buffer specifies a fixed paragraph direction), which will
12506 require to redisplay the whole paragraph. It might be worthwhile
12507 to find the paragraph limits and widen the range of redisplayed
12508 lines to that, but for now just give up this optimization. */
12509 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12510 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12511 unchanged_p = 0;
12514 return unchanged_p;
12518 /* Do a frame update, taking possible shortcuts into account. This is
12519 the main external entry point for redisplay.
12521 If the last redisplay displayed an echo area message and that message
12522 is no longer requested, we clear the echo area or bring back the
12523 mini-buffer if that is in use. */
12525 void
12526 redisplay (void)
12528 redisplay_internal ();
12532 static Lisp_Object
12533 overlay_arrow_string_or_property (Lisp_Object var)
12535 Lisp_Object val;
12537 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12538 return val;
12540 return Voverlay_arrow_string;
12543 /* Return 1 if there are any overlay-arrows in current_buffer. */
12544 static int
12545 overlay_arrow_in_current_buffer_p (void)
12547 Lisp_Object vlist;
12549 for (vlist = Voverlay_arrow_variable_list;
12550 CONSP (vlist);
12551 vlist = XCDR (vlist))
12553 Lisp_Object var = XCAR (vlist);
12554 Lisp_Object val;
12556 if (!SYMBOLP (var))
12557 continue;
12558 val = find_symbol_value (var);
12559 if (MARKERP (val)
12560 && current_buffer == XMARKER (val)->buffer)
12561 return 1;
12563 return 0;
12567 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12568 has changed. */
12570 static int
12571 overlay_arrows_changed_p (void)
12573 Lisp_Object vlist;
12575 for (vlist = Voverlay_arrow_variable_list;
12576 CONSP (vlist);
12577 vlist = XCDR (vlist))
12579 Lisp_Object var = XCAR (vlist);
12580 Lisp_Object val, pstr;
12582 if (!SYMBOLP (var))
12583 continue;
12584 val = find_symbol_value (var);
12585 if (!MARKERP (val))
12586 continue;
12587 if (! EQ (COERCE_MARKER (val),
12588 Fget (var, Qlast_arrow_position))
12589 || ! (pstr = overlay_arrow_string_or_property (var),
12590 EQ (pstr, Fget (var, Qlast_arrow_string))))
12591 return 1;
12593 return 0;
12596 /* Mark overlay arrows to be updated on next redisplay. */
12598 static void
12599 update_overlay_arrows (int up_to_date)
12601 Lisp_Object vlist;
12603 for (vlist = Voverlay_arrow_variable_list;
12604 CONSP (vlist);
12605 vlist = XCDR (vlist))
12607 Lisp_Object var = XCAR (vlist);
12609 if (!SYMBOLP (var))
12610 continue;
12612 if (up_to_date > 0)
12614 Lisp_Object val = find_symbol_value (var);
12615 Fput (var, Qlast_arrow_position,
12616 COERCE_MARKER (val));
12617 Fput (var, Qlast_arrow_string,
12618 overlay_arrow_string_or_property (var));
12620 else if (up_to_date < 0
12621 || !NILP (Fget (var, Qlast_arrow_position)))
12623 Fput (var, Qlast_arrow_position, Qt);
12624 Fput (var, Qlast_arrow_string, Qt);
12630 /* Return overlay arrow string to display at row.
12631 Return integer (bitmap number) for arrow bitmap in left fringe.
12632 Return nil if no overlay arrow. */
12634 static Lisp_Object
12635 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12637 Lisp_Object vlist;
12639 for (vlist = Voverlay_arrow_variable_list;
12640 CONSP (vlist);
12641 vlist = XCDR (vlist))
12643 Lisp_Object var = XCAR (vlist);
12644 Lisp_Object val;
12646 if (!SYMBOLP (var))
12647 continue;
12649 val = find_symbol_value (var);
12651 if (MARKERP (val)
12652 && current_buffer == XMARKER (val)->buffer
12653 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12655 if (FRAME_WINDOW_P (it->f)
12656 /* FIXME: if ROW->reversed_p is set, this should test
12657 the right fringe, not the left one. */
12658 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12660 #ifdef HAVE_WINDOW_SYSTEM
12661 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12663 int fringe_bitmap;
12664 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12665 return make_number (fringe_bitmap);
12667 #endif
12668 return make_number (-1); /* Use default arrow bitmap */
12670 return overlay_arrow_string_or_property (var);
12674 return Qnil;
12677 /* Return 1 if point moved out of or into a composition. Otherwise
12678 return 0. PREV_BUF and PREV_PT are the last point buffer and
12679 position. BUF and PT are the current point buffer and position. */
12681 static int
12682 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
12683 struct buffer *buf, EMACS_INT pt)
12685 EMACS_INT start, end;
12686 Lisp_Object prop;
12687 Lisp_Object buffer;
12689 XSETBUFFER (buffer, buf);
12690 /* Check a composition at the last point if point moved within the
12691 same buffer. */
12692 if (prev_buf == buf)
12694 if (prev_pt == pt)
12695 /* Point didn't move. */
12696 return 0;
12698 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12699 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12700 && COMPOSITION_VALID_P (start, end, prop)
12701 && start < prev_pt && end > prev_pt)
12702 /* The last point was within the composition. Return 1 iff
12703 point moved out of the composition. */
12704 return (pt <= start || pt >= end);
12707 /* Check a composition at the current point. */
12708 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12709 && find_composition (pt, -1, &start, &end, &prop, buffer)
12710 && COMPOSITION_VALID_P (start, end, prop)
12711 && start < pt && end > pt);
12715 /* Reconsider the setting of B->clip_changed which is displayed
12716 in window W. */
12718 static inline void
12719 reconsider_clip_changes (struct window *w, struct buffer *b)
12721 if (b->clip_changed
12722 && !NILP (w->window_end_valid)
12723 && w->current_matrix->buffer == b
12724 && w->current_matrix->zv == BUF_ZV (b)
12725 && w->current_matrix->begv == BUF_BEGV (b))
12726 b->clip_changed = 0;
12728 /* If display wasn't paused, and W is not a tool bar window, see if
12729 point has been moved into or out of a composition. In that case,
12730 we set b->clip_changed to 1 to force updating the screen. If
12731 b->clip_changed has already been set to 1, we can skip this
12732 check. */
12733 if (!b->clip_changed
12734 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12736 EMACS_INT pt;
12738 if (w == XWINDOW (selected_window))
12739 pt = PT;
12740 else
12741 pt = marker_position (w->pointm);
12743 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12744 || pt != XINT (w->last_point))
12745 && check_point_in_composition (w->current_matrix->buffer,
12746 XINT (w->last_point),
12747 XBUFFER (w->buffer), pt))
12748 b->clip_changed = 1;
12753 /* Select FRAME to forward the values of frame-local variables into C
12754 variables so that the redisplay routines can access those values
12755 directly. */
12757 static void
12758 select_frame_for_redisplay (Lisp_Object frame)
12760 Lisp_Object tail, tem;
12761 Lisp_Object old = selected_frame;
12762 struct Lisp_Symbol *sym;
12764 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12766 selected_frame = frame;
12768 do {
12769 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12770 if (CONSP (XCAR (tail))
12771 && (tem = XCAR (XCAR (tail)),
12772 SYMBOLP (tem))
12773 && (sym = indirect_variable (XSYMBOL (tem)),
12774 sym->redirect == SYMBOL_LOCALIZED)
12775 && sym->val.blv->frame_local)
12776 /* Use find_symbol_value rather than Fsymbol_value
12777 to avoid an error if it is void. */
12778 find_symbol_value (tem);
12779 } while (!EQ (frame, old) && (frame = old, 1));
12783 #define STOP_POLLING \
12784 do { if (! polling_stopped_here) stop_polling (); \
12785 polling_stopped_here = 1; } while (0)
12787 #define RESUME_POLLING \
12788 do { if (polling_stopped_here) start_polling (); \
12789 polling_stopped_here = 0; } while (0)
12792 /* Perhaps in the future avoid recentering windows if it
12793 is not necessary; currently that causes some problems. */
12795 static void
12796 redisplay_internal (void)
12798 struct window *w = XWINDOW (selected_window);
12799 struct window *sw;
12800 struct frame *fr;
12801 int pending;
12802 int must_finish = 0;
12803 struct text_pos tlbufpos, tlendpos;
12804 int number_of_visible_frames;
12805 int count, count1;
12806 struct frame *sf;
12807 int polling_stopped_here = 0;
12808 Lisp_Object old_frame = selected_frame;
12810 /* Non-zero means redisplay has to consider all windows on all
12811 frames. Zero means, only selected_window is considered. */
12812 int consider_all_windows_p;
12814 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12816 /* No redisplay if running in batch mode or frame is not yet fully
12817 initialized, or redisplay is explicitly turned off by setting
12818 Vinhibit_redisplay. */
12819 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12820 || !NILP (Vinhibit_redisplay))
12821 return;
12823 /* Don't examine these until after testing Vinhibit_redisplay.
12824 When Emacs is shutting down, perhaps because its connection to
12825 X has dropped, we should not look at them at all. */
12826 fr = XFRAME (w->frame);
12827 sf = SELECTED_FRAME ();
12829 if (!fr->glyphs_initialized_p)
12830 return;
12832 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12833 if (popup_activated ())
12834 return;
12835 #endif
12837 /* I don't think this happens but let's be paranoid. */
12838 if (redisplaying_p)
12839 return;
12841 /* Record a function that resets redisplaying_p to its old value
12842 when we leave this function. */
12843 count = SPECPDL_INDEX ();
12844 record_unwind_protect (unwind_redisplay,
12845 Fcons (make_number (redisplaying_p), selected_frame));
12846 ++redisplaying_p;
12847 specbind (Qinhibit_free_realized_faces, Qnil);
12850 Lisp_Object tail, frame;
12852 FOR_EACH_FRAME (tail, frame)
12854 struct frame *f = XFRAME (frame);
12855 f->already_hscrolled_p = 0;
12859 retry:
12860 /* Remember the currently selected window. */
12861 sw = w;
12863 if (!EQ (old_frame, selected_frame)
12864 && FRAME_LIVE_P (XFRAME (old_frame)))
12865 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12866 selected_frame and selected_window to be temporarily out-of-sync so
12867 when we come back here via `goto retry', we need to resync because we
12868 may need to run Elisp code (via prepare_menu_bars). */
12869 select_frame_for_redisplay (old_frame);
12871 pending = 0;
12872 reconsider_clip_changes (w, current_buffer);
12873 last_escape_glyph_frame = NULL;
12874 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12875 last_glyphless_glyph_frame = NULL;
12876 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12878 /* If new fonts have been loaded that make a glyph matrix adjustment
12879 necessary, do it. */
12880 if (fonts_changed_p)
12882 adjust_glyphs (NULL);
12883 ++windows_or_buffers_changed;
12884 fonts_changed_p = 0;
12887 /* If face_change_count is non-zero, init_iterator will free all
12888 realized faces, which includes the faces referenced from current
12889 matrices. So, we can't reuse current matrices in this case. */
12890 if (face_change_count)
12891 ++windows_or_buffers_changed;
12893 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12894 && FRAME_TTY (sf)->previous_frame != sf)
12896 /* Since frames on a single ASCII terminal share the same
12897 display area, displaying a different frame means redisplay
12898 the whole thing. */
12899 windows_or_buffers_changed++;
12900 SET_FRAME_GARBAGED (sf);
12901 #ifndef DOS_NT
12902 set_tty_color_mode (FRAME_TTY (sf), sf);
12903 #endif
12904 FRAME_TTY (sf)->previous_frame = sf;
12907 /* Set the visible flags for all frames. Do this before checking
12908 for resized or garbaged frames; they want to know if their frames
12909 are visible. See the comment in frame.h for
12910 FRAME_SAMPLE_VISIBILITY. */
12912 Lisp_Object tail, frame;
12914 number_of_visible_frames = 0;
12916 FOR_EACH_FRAME (tail, frame)
12918 struct frame *f = XFRAME (frame);
12920 FRAME_SAMPLE_VISIBILITY (f);
12921 if (FRAME_VISIBLE_P (f))
12922 ++number_of_visible_frames;
12923 clear_desired_matrices (f);
12927 /* Notice any pending interrupt request to change frame size. */
12928 do_pending_window_change (1);
12930 /* do_pending_window_change could change the selected_window due to
12931 frame resizing which makes the selected window too small. */
12932 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12934 sw = w;
12935 reconsider_clip_changes (w, current_buffer);
12938 /* Clear frames marked as garbaged. */
12939 if (frame_garbaged)
12940 clear_garbaged_frames ();
12942 /* Build menubar and tool-bar items. */
12943 if (NILP (Vmemory_full))
12944 prepare_menu_bars ();
12946 if (windows_or_buffers_changed)
12947 update_mode_lines++;
12949 /* Detect case that we need to write or remove a star in the mode line. */
12950 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
12952 w->update_mode_line = Qt;
12953 if (buffer_shared > 1)
12954 update_mode_lines++;
12957 /* Avoid invocation of point motion hooks by `current_column' below. */
12958 count1 = SPECPDL_INDEX ();
12959 specbind (Qinhibit_point_motion_hooks, Qt);
12961 /* If %c is in the mode line, update it if needed. */
12962 if (!NILP (w->column_number_displayed)
12963 /* This alternative quickly identifies a common case
12964 where no change is needed. */
12965 && !(PT == XFASTINT (w->last_point)
12966 && XFASTINT (w->last_modified) >= MODIFF
12967 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12968 && (XFASTINT (w->column_number_displayed) != current_column ()))
12969 w->update_mode_line = Qt;
12971 unbind_to (count1, Qnil);
12973 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12975 /* The variable buffer_shared is set in redisplay_window and
12976 indicates that we redisplay a buffer in different windows. See
12977 there. */
12978 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
12979 || cursor_type_changed);
12981 /* If specs for an arrow have changed, do thorough redisplay
12982 to ensure we remove any arrow that should no longer exist. */
12983 if (overlay_arrows_changed_p ())
12984 consider_all_windows_p = windows_or_buffers_changed = 1;
12986 /* Normally the message* functions will have already displayed and
12987 updated the echo area, but the frame may have been trashed, or
12988 the update may have been preempted, so display the echo area
12989 again here. Checking message_cleared_p captures the case that
12990 the echo area should be cleared. */
12991 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12992 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12993 || (message_cleared_p
12994 && minibuf_level == 0
12995 /* If the mini-window is currently selected, this means the
12996 echo-area doesn't show through. */
12997 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12999 int window_height_changed_p = echo_area_display (0);
13000 must_finish = 1;
13002 /* If we don't display the current message, don't clear the
13003 message_cleared_p flag, because, if we did, we wouldn't clear
13004 the echo area in the next redisplay which doesn't preserve
13005 the echo area. */
13006 if (!display_last_displayed_message_p)
13007 message_cleared_p = 0;
13009 if (fonts_changed_p)
13010 goto retry;
13011 else if (window_height_changed_p)
13013 consider_all_windows_p = 1;
13014 ++update_mode_lines;
13015 ++windows_or_buffers_changed;
13017 /* If window configuration was changed, frames may have been
13018 marked garbaged. Clear them or we will experience
13019 surprises wrt scrolling. */
13020 if (frame_garbaged)
13021 clear_garbaged_frames ();
13024 else if (EQ (selected_window, minibuf_window)
13025 && (current_buffer->clip_changed
13026 || XFASTINT (w->last_modified) < MODIFF
13027 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
13028 && resize_mini_window (w, 0))
13030 /* Resized active mini-window to fit the size of what it is
13031 showing if its contents might have changed. */
13032 must_finish = 1;
13033 /* FIXME: this causes all frames to be updated, which seems unnecessary
13034 since only the current frame needs to be considered. This function needs
13035 to be rewritten with two variables, consider_all_windows and
13036 consider_all_frames. */
13037 consider_all_windows_p = 1;
13038 ++windows_or_buffers_changed;
13039 ++update_mode_lines;
13041 /* If window configuration was changed, frames may have been
13042 marked garbaged. Clear them or we will experience
13043 surprises wrt scrolling. */
13044 if (frame_garbaged)
13045 clear_garbaged_frames ();
13049 /* If showing the region, and mark has changed, we must redisplay
13050 the whole window. The assignment to this_line_start_pos prevents
13051 the optimization directly below this if-statement. */
13052 if (((!NILP (Vtransient_mark_mode)
13053 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13054 != !NILP (w->region_showing))
13055 || (!NILP (w->region_showing)
13056 && !EQ (w->region_showing,
13057 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13058 CHARPOS (this_line_start_pos) = 0;
13060 /* Optimize the case that only the line containing the cursor in the
13061 selected window has changed. Variables starting with this_ are
13062 set in display_line and record information about the line
13063 containing the cursor. */
13064 tlbufpos = this_line_start_pos;
13065 tlendpos = this_line_end_pos;
13066 if (!consider_all_windows_p
13067 && CHARPOS (tlbufpos) > 0
13068 && NILP (w->update_mode_line)
13069 && !current_buffer->clip_changed
13070 && !current_buffer->prevent_redisplay_optimizations_p
13071 && FRAME_VISIBLE_P (XFRAME (w->frame))
13072 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13073 /* Make sure recorded data applies to current buffer, etc. */
13074 && this_line_buffer == current_buffer
13075 && current_buffer == XBUFFER (w->buffer)
13076 && NILP (w->force_start)
13077 && NILP (w->optional_new_start)
13078 /* Point must be on the line that we have info recorded about. */
13079 && PT >= CHARPOS (tlbufpos)
13080 && PT <= Z - CHARPOS (tlendpos)
13081 /* All text outside that line, including its final newline,
13082 must be unchanged. */
13083 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13084 CHARPOS (tlendpos)))
13086 if (CHARPOS (tlbufpos) > BEGV
13087 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13088 && (CHARPOS (tlbufpos) == ZV
13089 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13090 /* Former continuation line has disappeared by becoming empty. */
13091 goto cancel;
13092 else if (XFASTINT (w->last_modified) < MODIFF
13093 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
13094 || MINI_WINDOW_P (w))
13096 /* We have to handle the case of continuation around a
13097 wide-column character (see the comment in indent.c around
13098 line 1340).
13100 For instance, in the following case:
13102 -------- Insert --------
13103 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13104 J_I_ ==> J_I_ `^^' are cursors.
13105 ^^ ^^
13106 -------- --------
13108 As we have to redraw the line above, we cannot use this
13109 optimization. */
13111 struct it it;
13112 int line_height_before = this_line_pixel_height;
13114 /* Note that start_display will handle the case that the
13115 line starting at tlbufpos is a continuation line. */
13116 start_display (&it, w, tlbufpos);
13118 /* Implementation note: It this still necessary? */
13119 if (it.current_x != this_line_start_x)
13120 goto cancel;
13122 TRACE ((stderr, "trying display optimization 1\n"));
13123 w->cursor.vpos = -1;
13124 overlay_arrow_seen = 0;
13125 it.vpos = this_line_vpos;
13126 it.current_y = this_line_y;
13127 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13128 display_line (&it);
13130 /* If line contains point, is not continued,
13131 and ends at same distance from eob as before, we win. */
13132 if (w->cursor.vpos >= 0
13133 /* Line is not continued, otherwise this_line_start_pos
13134 would have been set to 0 in display_line. */
13135 && CHARPOS (this_line_start_pos)
13136 /* Line ends as before. */
13137 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13138 /* Line has same height as before. Otherwise other lines
13139 would have to be shifted up or down. */
13140 && this_line_pixel_height == line_height_before)
13142 /* If this is not the window's last line, we must adjust
13143 the charstarts of the lines below. */
13144 if (it.current_y < it.last_visible_y)
13146 struct glyph_row *row
13147 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13148 EMACS_INT delta, delta_bytes;
13150 /* We used to distinguish between two cases here,
13151 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13152 when the line ends in a newline or the end of the
13153 buffer's accessible portion. But both cases did
13154 the same, so they were collapsed. */
13155 delta = (Z
13156 - CHARPOS (tlendpos)
13157 - MATRIX_ROW_START_CHARPOS (row));
13158 delta_bytes = (Z_BYTE
13159 - BYTEPOS (tlendpos)
13160 - MATRIX_ROW_START_BYTEPOS (row));
13162 increment_matrix_positions (w->current_matrix,
13163 this_line_vpos + 1,
13164 w->current_matrix->nrows,
13165 delta, delta_bytes);
13168 /* If this row displays text now but previously didn't,
13169 or vice versa, w->window_end_vpos may have to be
13170 adjusted. */
13171 if ((it.glyph_row - 1)->displays_text_p)
13173 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13174 XSETINT (w->window_end_vpos, this_line_vpos);
13176 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13177 && this_line_vpos > 0)
13178 XSETINT (w->window_end_vpos, this_line_vpos - 1);
13179 w->window_end_valid = Qnil;
13181 /* Update hint: No need to try to scroll in update_window. */
13182 w->desired_matrix->no_scrolling_p = 1;
13184 #if GLYPH_DEBUG
13185 *w->desired_matrix->method = 0;
13186 debug_method_add (w, "optimization 1");
13187 #endif
13188 #ifdef HAVE_WINDOW_SYSTEM
13189 update_window_fringes (w, 0);
13190 #endif
13191 goto update;
13193 else
13194 goto cancel;
13196 else if (/* Cursor position hasn't changed. */
13197 PT == XFASTINT (w->last_point)
13198 /* Make sure the cursor was last displayed
13199 in this window. Otherwise we have to reposition it. */
13200 && 0 <= w->cursor.vpos
13201 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13203 if (!must_finish)
13205 do_pending_window_change (1);
13206 /* If selected_window changed, redisplay again. */
13207 if (WINDOWP (selected_window)
13208 && (w = XWINDOW (selected_window)) != sw)
13209 goto retry;
13211 /* We used to always goto end_of_redisplay here, but this
13212 isn't enough if we have a blinking cursor. */
13213 if (w->cursor_off_p == w->last_cursor_off_p)
13214 goto end_of_redisplay;
13216 goto update;
13218 /* If highlighting the region, or if the cursor is in the echo area,
13219 then we can't just move the cursor. */
13220 else if (! (!NILP (Vtransient_mark_mode)
13221 && !NILP (BVAR (current_buffer, mark_active)))
13222 && (EQ (selected_window, BVAR (current_buffer, last_selected_window))
13223 || highlight_nonselected_windows)
13224 && NILP (w->region_showing)
13225 && NILP (Vshow_trailing_whitespace)
13226 && !cursor_in_echo_area)
13228 struct it it;
13229 struct glyph_row *row;
13231 /* Skip from tlbufpos to PT and see where it is. Note that
13232 PT may be in invisible text. If so, we will end at the
13233 next visible position. */
13234 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13235 NULL, DEFAULT_FACE_ID);
13236 it.current_x = this_line_start_x;
13237 it.current_y = this_line_y;
13238 it.vpos = this_line_vpos;
13240 /* The call to move_it_to stops in front of PT, but
13241 moves over before-strings. */
13242 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13244 if (it.vpos == this_line_vpos
13245 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13246 row->enabled_p))
13248 xassert (this_line_vpos == it.vpos);
13249 xassert (this_line_y == it.current_y);
13250 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13251 #if GLYPH_DEBUG
13252 *w->desired_matrix->method = 0;
13253 debug_method_add (w, "optimization 3");
13254 #endif
13255 goto update;
13257 else
13258 goto cancel;
13261 cancel:
13262 /* Text changed drastically or point moved off of line. */
13263 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13266 CHARPOS (this_line_start_pos) = 0;
13267 consider_all_windows_p |= buffer_shared > 1;
13268 ++clear_face_cache_count;
13269 #ifdef HAVE_WINDOW_SYSTEM
13270 ++clear_image_cache_count;
13271 #endif
13273 /* Build desired matrices, and update the display. If
13274 consider_all_windows_p is non-zero, do it for all windows on all
13275 frames. Otherwise do it for selected_window, only. */
13277 if (consider_all_windows_p)
13279 Lisp_Object tail, frame;
13281 FOR_EACH_FRAME (tail, frame)
13282 XFRAME (frame)->updated_p = 0;
13284 /* Recompute # windows showing selected buffer. This will be
13285 incremented each time such a window is displayed. */
13286 buffer_shared = 0;
13288 FOR_EACH_FRAME (tail, frame)
13290 struct frame *f = XFRAME (frame);
13292 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13294 if (! EQ (frame, selected_frame))
13295 /* Select the frame, for the sake of frame-local
13296 variables. */
13297 select_frame_for_redisplay (frame);
13299 /* Mark all the scroll bars to be removed; we'll redeem
13300 the ones we want when we redisplay their windows. */
13301 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13302 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13304 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13305 redisplay_windows (FRAME_ROOT_WINDOW (f));
13307 /* The X error handler may have deleted that frame. */
13308 if (!FRAME_LIVE_P (f))
13309 continue;
13311 /* Any scroll bars which redisplay_windows should have
13312 nuked should now go away. */
13313 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13314 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13316 /* If fonts changed, display again. */
13317 /* ??? rms: I suspect it is a mistake to jump all the way
13318 back to retry here. It should just retry this frame. */
13319 if (fonts_changed_p)
13320 goto retry;
13322 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13324 /* See if we have to hscroll. */
13325 if (!f->already_hscrolled_p)
13327 f->already_hscrolled_p = 1;
13328 if (hscroll_windows (f->root_window))
13329 goto retry;
13332 /* Prevent various kinds of signals during display
13333 update. stdio is not robust about handling
13334 signals, which can cause an apparent I/O
13335 error. */
13336 if (interrupt_input)
13337 unrequest_sigio ();
13338 STOP_POLLING;
13340 /* Update the display. */
13341 set_window_update_flags (XWINDOW (f->root_window), 1);
13342 pending |= update_frame (f, 0, 0);
13343 f->updated_p = 1;
13348 if (!EQ (old_frame, selected_frame)
13349 && FRAME_LIVE_P (XFRAME (old_frame)))
13350 /* We played a bit fast-and-loose above and allowed selected_frame
13351 and selected_window to be temporarily out-of-sync but let's make
13352 sure this stays contained. */
13353 select_frame_for_redisplay (old_frame);
13354 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13356 if (!pending)
13358 /* Do the mark_window_display_accurate after all windows have
13359 been redisplayed because this call resets flags in buffers
13360 which are needed for proper redisplay. */
13361 FOR_EACH_FRAME (tail, frame)
13363 struct frame *f = XFRAME (frame);
13364 if (f->updated_p)
13366 mark_window_display_accurate (f->root_window, 1);
13367 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13368 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13373 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13375 Lisp_Object mini_window;
13376 struct frame *mini_frame;
13378 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13379 /* Use list_of_error, not Qerror, so that
13380 we catch only errors and don't run the debugger. */
13381 internal_condition_case_1 (redisplay_window_1, selected_window,
13382 list_of_error,
13383 redisplay_window_error);
13385 /* Compare desired and current matrices, perform output. */
13387 update:
13388 /* If fonts changed, display again. */
13389 if (fonts_changed_p)
13390 goto retry;
13392 /* Prevent various kinds of signals during display update.
13393 stdio is not robust about handling signals,
13394 which can cause an apparent I/O error. */
13395 if (interrupt_input)
13396 unrequest_sigio ();
13397 STOP_POLLING;
13399 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13401 if (hscroll_windows (selected_window))
13402 goto retry;
13404 XWINDOW (selected_window)->must_be_updated_p = 1;
13405 pending = update_frame (sf, 0, 0);
13408 /* We may have called echo_area_display at the top of this
13409 function. If the echo area is on another frame, that may
13410 have put text on a frame other than the selected one, so the
13411 above call to update_frame would not have caught it. Catch
13412 it here. */
13413 mini_window = FRAME_MINIBUF_WINDOW (sf);
13414 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13416 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13418 XWINDOW (mini_window)->must_be_updated_p = 1;
13419 pending |= update_frame (mini_frame, 0, 0);
13420 if (!pending && hscroll_windows (mini_window))
13421 goto retry;
13425 /* If display was paused because of pending input, make sure we do a
13426 thorough update the next time. */
13427 if (pending)
13429 /* Prevent the optimization at the beginning of
13430 redisplay_internal that tries a single-line update of the
13431 line containing the cursor in the selected window. */
13432 CHARPOS (this_line_start_pos) = 0;
13434 /* Let the overlay arrow be updated the next time. */
13435 update_overlay_arrows (0);
13437 /* If we pause after scrolling, some rows in the current
13438 matrices of some windows are not valid. */
13439 if (!WINDOW_FULL_WIDTH_P (w)
13440 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13441 update_mode_lines = 1;
13443 else
13445 if (!consider_all_windows_p)
13447 /* This has already been done above if
13448 consider_all_windows_p is set. */
13449 mark_window_display_accurate_1 (w, 1);
13451 /* Say overlay arrows are up to date. */
13452 update_overlay_arrows (1);
13454 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13455 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13458 update_mode_lines = 0;
13459 windows_or_buffers_changed = 0;
13460 cursor_type_changed = 0;
13463 /* Start SIGIO interrupts coming again. Having them off during the
13464 code above makes it less likely one will discard output, but not
13465 impossible, since there might be stuff in the system buffer here.
13466 But it is much hairier to try to do anything about that. */
13467 if (interrupt_input)
13468 request_sigio ();
13469 RESUME_POLLING;
13471 /* If a frame has become visible which was not before, redisplay
13472 again, so that we display it. Expose events for such a frame
13473 (which it gets when becoming visible) don't call the parts of
13474 redisplay constructing glyphs, so simply exposing a frame won't
13475 display anything in this case. So, we have to display these
13476 frames here explicitly. */
13477 if (!pending)
13479 Lisp_Object tail, frame;
13480 int new_count = 0;
13482 FOR_EACH_FRAME (tail, frame)
13484 int this_is_visible = 0;
13486 if (XFRAME (frame)->visible)
13487 this_is_visible = 1;
13488 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13489 if (XFRAME (frame)->visible)
13490 this_is_visible = 1;
13492 if (this_is_visible)
13493 new_count++;
13496 if (new_count != number_of_visible_frames)
13497 windows_or_buffers_changed++;
13500 /* Change frame size now if a change is pending. */
13501 do_pending_window_change (1);
13503 /* If we just did a pending size change, or have additional
13504 visible frames, or selected_window changed, redisplay again. */
13505 if ((windows_or_buffers_changed && !pending)
13506 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13507 goto retry;
13509 /* Clear the face and image caches.
13511 We used to do this only if consider_all_windows_p. But the cache
13512 needs to be cleared if a timer creates images in the current
13513 buffer (e.g. the test case in Bug#6230). */
13515 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13517 clear_face_cache (0);
13518 clear_face_cache_count = 0;
13521 #ifdef HAVE_WINDOW_SYSTEM
13522 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13524 clear_image_caches (Qnil);
13525 clear_image_cache_count = 0;
13527 #endif /* HAVE_WINDOW_SYSTEM */
13529 end_of_redisplay:
13530 unbind_to (count, Qnil);
13531 RESUME_POLLING;
13535 /* Redisplay, but leave alone any recent echo area message unless
13536 another message has been requested in its place.
13538 This is useful in situations where you need to redisplay but no
13539 user action has occurred, making it inappropriate for the message
13540 area to be cleared. See tracking_off and
13541 wait_reading_process_output for examples of these situations.
13543 FROM_WHERE is an integer saying from where this function was
13544 called. This is useful for debugging. */
13546 void
13547 redisplay_preserve_echo_area (int from_where)
13549 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13551 if (!NILP (echo_area_buffer[1]))
13553 /* We have a previously displayed message, but no current
13554 message. Redisplay the previous message. */
13555 display_last_displayed_message_p = 1;
13556 redisplay_internal ();
13557 display_last_displayed_message_p = 0;
13559 else
13560 redisplay_internal ();
13562 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13563 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13564 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13568 /* Function registered with record_unwind_protect in
13569 redisplay_internal. Reset redisplaying_p to the value it had
13570 before redisplay_internal was called, and clear
13571 prevent_freeing_realized_faces_p. It also selects the previously
13572 selected frame, unless it has been deleted (by an X connection
13573 failure during redisplay, for example). */
13575 static Lisp_Object
13576 unwind_redisplay (Lisp_Object val)
13578 Lisp_Object old_redisplaying_p, old_frame;
13580 old_redisplaying_p = XCAR (val);
13581 redisplaying_p = XFASTINT (old_redisplaying_p);
13582 old_frame = XCDR (val);
13583 if (! EQ (old_frame, selected_frame)
13584 && FRAME_LIVE_P (XFRAME (old_frame)))
13585 select_frame_for_redisplay (old_frame);
13586 return Qnil;
13590 /* Mark the display of window W as accurate or inaccurate. If
13591 ACCURATE_P is non-zero mark display of W as accurate. If
13592 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13593 redisplay_internal is called. */
13595 static void
13596 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13598 if (BUFFERP (w->buffer))
13600 struct buffer *b = XBUFFER (w->buffer);
13602 w->last_modified
13603 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
13604 w->last_overlay_modified
13605 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
13606 w->last_had_star
13607 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
13609 if (accurate_p)
13611 b->clip_changed = 0;
13612 b->prevent_redisplay_optimizations_p = 0;
13614 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13615 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13616 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13617 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13619 w->current_matrix->buffer = b;
13620 w->current_matrix->begv = BUF_BEGV (b);
13621 w->current_matrix->zv = BUF_ZV (b);
13623 w->last_cursor = w->cursor;
13624 w->last_cursor_off_p = w->cursor_off_p;
13626 if (w == XWINDOW (selected_window))
13627 w->last_point = make_number (BUF_PT (b));
13628 else
13629 w->last_point = make_number (XMARKER (w->pointm)->charpos);
13633 if (accurate_p)
13635 w->window_end_valid = w->buffer;
13636 w->update_mode_line = Qnil;
13641 /* Mark the display of windows in the window tree rooted at WINDOW as
13642 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13643 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13644 be redisplayed the next time redisplay_internal is called. */
13646 void
13647 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13649 struct window *w;
13651 for (; !NILP (window); window = w->next)
13653 w = XWINDOW (window);
13654 mark_window_display_accurate_1 (w, accurate_p);
13656 if (!NILP (w->vchild))
13657 mark_window_display_accurate (w->vchild, accurate_p);
13658 if (!NILP (w->hchild))
13659 mark_window_display_accurate (w->hchild, accurate_p);
13662 if (accurate_p)
13664 update_overlay_arrows (1);
13666 else
13668 /* Force a thorough redisplay the next time by setting
13669 last_arrow_position and last_arrow_string to t, which is
13670 unequal to any useful value of Voverlay_arrow_... */
13671 update_overlay_arrows (-1);
13676 /* Return value in display table DP (Lisp_Char_Table *) for character
13677 C. Since a display table doesn't have any parent, we don't have to
13678 follow parent. Do not call this function directly but use the
13679 macro DISP_CHAR_VECTOR. */
13681 Lisp_Object
13682 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13684 Lisp_Object val;
13686 if (ASCII_CHAR_P (c))
13688 val = dp->ascii;
13689 if (SUB_CHAR_TABLE_P (val))
13690 val = XSUB_CHAR_TABLE (val)->contents[c];
13692 else
13694 Lisp_Object table;
13696 XSETCHAR_TABLE (table, dp);
13697 val = char_table_ref (table, c);
13699 if (NILP (val))
13700 val = dp->defalt;
13701 return val;
13706 /***********************************************************************
13707 Window Redisplay
13708 ***********************************************************************/
13710 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13712 static void
13713 redisplay_windows (Lisp_Object window)
13715 while (!NILP (window))
13717 struct window *w = XWINDOW (window);
13719 if (!NILP (w->hchild))
13720 redisplay_windows (w->hchild);
13721 else if (!NILP (w->vchild))
13722 redisplay_windows (w->vchild);
13723 else if (!NILP (w->buffer))
13725 displayed_buffer = XBUFFER (w->buffer);
13726 /* Use list_of_error, not Qerror, so that
13727 we catch only errors and don't run the debugger. */
13728 internal_condition_case_1 (redisplay_window_0, window,
13729 list_of_error,
13730 redisplay_window_error);
13733 window = w->next;
13737 static Lisp_Object
13738 redisplay_window_error (Lisp_Object ignore)
13740 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13741 return Qnil;
13744 static Lisp_Object
13745 redisplay_window_0 (Lisp_Object window)
13747 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13748 redisplay_window (window, 0);
13749 return Qnil;
13752 static Lisp_Object
13753 redisplay_window_1 (Lisp_Object window)
13755 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13756 redisplay_window (window, 1);
13757 return Qnil;
13761 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13762 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13763 which positions recorded in ROW differ from current buffer
13764 positions.
13766 Return 0 if cursor is not on this row, 1 otherwise. */
13768 static int
13769 set_cursor_from_row (struct window *w, struct glyph_row *row,
13770 struct glyph_matrix *matrix,
13771 EMACS_INT delta, EMACS_INT delta_bytes,
13772 int dy, int dvpos)
13774 struct glyph *glyph = row->glyphs[TEXT_AREA];
13775 struct glyph *end = glyph + row->used[TEXT_AREA];
13776 struct glyph *cursor = NULL;
13777 /* The last known character position in row. */
13778 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13779 int x = row->x;
13780 EMACS_INT pt_old = PT - delta;
13781 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13782 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13783 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13784 /* A glyph beyond the edge of TEXT_AREA which we should never
13785 touch. */
13786 struct glyph *glyphs_end = end;
13787 /* Non-zero means we've found a match for cursor position, but that
13788 glyph has the avoid_cursor_p flag set. */
13789 int match_with_avoid_cursor = 0;
13790 /* Non-zero means we've seen at least one glyph that came from a
13791 display string. */
13792 int string_seen = 0;
13793 /* Largest and smallest buffer positions seen so far during scan of
13794 glyph row. */
13795 EMACS_INT bpos_max = pos_before;
13796 EMACS_INT bpos_min = pos_after;
13797 /* Last buffer position covered by an overlay string with an integer
13798 `cursor' property. */
13799 EMACS_INT bpos_covered = 0;
13800 /* Non-zero means the display string on which to display the cursor
13801 comes from a text property, not from an overlay. */
13802 int string_from_text_prop = 0;
13804 /* Don't even try doing anything if called for a mode-line or
13805 header-line row, since the rest of the code isn't prepared to
13806 deal with such calamities. */
13807 xassert (!row->mode_line_p);
13808 if (row->mode_line_p)
13809 return 0;
13811 /* Skip over glyphs not having an object at the start and the end of
13812 the row. These are special glyphs like truncation marks on
13813 terminal frames. */
13814 if (row->displays_text_p)
13816 if (!row->reversed_p)
13818 while (glyph < end
13819 && INTEGERP (glyph->object)
13820 && glyph->charpos < 0)
13822 x += glyph->pixel_width;
13823 ++glyph;
13825 while (end > glyph
13826 && INTEGERP ((end - 1)->object)
13827 /* CHARPOS is zero for blanks and stretch glyphs
13828 inserted by extend_face_to_end_of_line. */
13829 && (end - 1)->charpos <= 0)
13830 --end;
13831 glyph_before = glyph - 1;
13832 glyph_after = end;
13834 else
13836 struct glyph *g;
13838 /* If the glyph row is reversed, we need to process it from back
13839 to front, so swap the edge pointers. */
13840 glyphs_end = end = glyph - 1;
13841 glyph += row->used[TEXT_AREA] - 1;
13843 while (glyph > end + 1
13844 && INTEGERP (glyph->object)
13845 && glyph->charpos < 0)
13847 --glyph;
13848 x -= glyph->pixel_width;
13850 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13851 --glyph;
13852 /* By default, in reversed rows we put the cursor on the
13853 rightmost (first in the reading order) glyph. */
13854 for (g = end + 1; g < glyph; g++)
13855 x += g->pixel_width;
13856 while (end < glyph
13857 && INTEGERP ((end + 1)->object)
13858 && (end + 1)->charpos <= 0)
13859 ++end;
13860 glyph_before = glyph + 1;
13861 glyph_after = end;
13864 else if (row->reversed_p)
13866 /* In R2L rows that don't display text, put the cursor on the
13867 rightmost glyph. Case in point: an empty last line that is
13868 part of an R2L paragraph. */
13869 cursor = end - 1;
13870 /* Avoid placing the cursor on the last glyph of the row, where
13871 on terminal frames we hold the vertical border between
13872 adjacent windows. */
13873 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13874 && !WINDOW_RIGHTMOST_P (w)
13875 && cursor == row->glyphs[LAST_AREA] - 1)
13876 cursor--;
13877 x = -1; /* will be computed below, at label compute_x */
13880 /* Step 1: Try to find the glyph whose character position
13881 corresponds to point. If that's not possible, find 2 glyphs
13882 whose character positions are the closest to point, one before
13883 point, the other after it. */
13884 if (!row->reversed_p)
13885 while (/* not marched to end of glyph row */
13886 glyph < end
13887 /* glyph was not inserted by redisplay for internal purposes */
13888 && !INTEGERP (glyph->object))
13890 if (BUFFERP (glyph->object))
13892 EMACS_INT dpos = glyph->charpos - pt_old;
13894 if (glyph->charpos > bpos_max)
13895 bpos_max = glyph->charpos;
13896 if (glyph->charpos < bpos_min)
13897 bpos_min = glyph->charpos;
13898 if (!glyph->avoid_cursor_p)
13900 /* If we hit point, we've found the glyph on which to
13901 display the cursor. */
13902 if (dpos == 0)
13904 match_with_avoid_cursor = 0;
13905 break;
13907 /* See if we've found a better approximation to
13908 POS_BEFORE or to POS_AFTER. Note that we want the
13909 first (leftmost) glyph of all those that are the
13910 closest from below, and the last (rightmost) of all
13911 those from above. */
13912 if (0 > dpos && dpos > pos_before - pt_old)
13914 pos_before = glyph->charpos;
13915 glyph_before = glyph;
13917 else if (0 < dpos && dpos <= pos_after - pt_old)
13919 pos_after = glyph->charpos;
13920 glyph_after = glyph;
13923 else if (dpos == 0)
13924 match_with_avoid_cursor = 1;
13926 else if (STRINGP (glyph->object))
13928 Lisp_Object chprop;
13929 EMACS_INT glyph_pos = glyph->charpos;
13931 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13932 glyph->object);
13933 if (!NILP (chprop))
13935 /* If the string came from a `display' text property,
13936 look up the buffer position of that property and
13937 use that position to update bpos_max, as if we
13938 actually saw such a position in one of the row's
13939 glyphs. This helps with supporting integer values
13940 of `cursor' property on the display string in
13941 situations where most or all of the row's buffer
13942 text is completely covered by display properties,
13943 so that no glyph with valid buffer positions is
13944 ever seen in the row. */
13945 EMACS_INT prop_pos =
13946 string_buffer_position_lim (glyph->object, pos_before,
13947 pos_after, 0);
13949 if (prop_pos >= pos_before)
13950 bpos_max = prop_pos - 1;
13952 if (INTEGERP (chprop))
13954 bpos_covered = bpos_max + XINT (chprop);
13955 /* If the `cursor' property covers buffer positions up
13956 to and including point, we should display cursor on
13957 this glyph. Note that, if a `cursor' property on one
13958 of the string's characters has an integer value, we
13959 will break out of the loop below _before_ we get to
13960 the position match above. IOW, integer values of
13961 the `cursor' property override the "exact match for
13962 point" strategy of positioning the cursor. */
13963 /* Implementation note: bpos_max == pt_old when, e.g.,
13964 we are in an empty line, where bpos_max is set to
13965 MATRIX_ROW_START_CHARPOS, see above. */
13966 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13968 cursor = glyph;
13969 break;
13973 string_seen = 1;
13975 x += glyph->pixel_width;
13976 ++glyph;
13978 else if (glyph > end) /* row is reversed */
13979 while (!INTEGERP (glyph->object))
13981 if (BUFFERP (glyph->object))
13983 EMACS_INT dpos = glyph->charpos - pt_old;
13985 if (glyph->charpos > bpos_max)
13986 bpos_max = glyph->charpos;
13987 if (glyph->charpos < bpos_min)
13988 bpos_min = glyph->charpos;
13989 if (!glyph->avoid_cursor_p)
13991 if (dpos == 0)
13993 match_with_avoid_cursor = 0;
13994 break;
13996 if (0 > dpos && dpos > pos_before - pt_old)
13998 pos_before = glyph->charpos;
13999 glyph_before = glyph;
14001 else if (0 < dpos && dpos <= pos_after - pt_old)
14003 pos_after = glyph->charpos;
14004 glyph_after = glyph;
14007 else if (dpos == 0)
14008 match_with_avoid_cursor = 1;
14010 else if (STRINGP (glyph->object))
14012 Lisp_Object chprop;
14013 EMACS_INT glyph_pos = glyph->charpos;
14015 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14016 glyph->object);
14017 if (!NILP (chprop))
14019 EMACS_INT prop_pos =
14020 string_buffer_position_lim (glyph->object, pos_before,
14021 pos_after, 0);
14023 if (prop_pos >= pos_before)
14024 bpos_max = prop_pos - 1;
14026 if (INTEGERP (chprop))
14028 bpos_covered = bpos_max + XINT (chprop);
14029 /* If the `cursor' property covers buffer positions up
14030 to and including point, we should display cursor on
14031 this glyph. */
14032 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14034 cursor = glyph;
14035 break;
14038 string_seen = 1;
14040 --glyph;
14041 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14043 x--; /* can't use any pixel_width */
14044 break;
14046 x -= glyph->pixel_width;
14049 /* Step 2: If we didn't find an exact match for point, we need to
14050 look for a proper place to put the cursor among glyphs between
14051 GLYPH_BEFORE and GLYPH_AFTER. */
14052 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14053 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14054 && bpos_covered < pt_old)
14056 /* An empty line has a single glyph whose OBJECT is zero and
14057 whose CHARPOS is the position of a newline on that line.
14058 Note that on a TTY, there are more glyphs after that, which
14059 were produced by extend_face_to_end_of_line, but their
14060 CHARPOS is zero or negative. */
14061 int empty_line_p =
14062 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14063 && INTEGERP (glyph->object) && glyph->charpos > 0;
14065 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14067 EMACS_INT ellipsis_pos;
14069 /* Scan back over the ellipsis glyphs. */
14070 if (!row->reversed_p)
14072 ellipsis_pos = (glyph - 1)->charpos;
14073 while (glyph > row->glyphs[TEXT_AREA]
14074 && (glyph - 1)->charpos == ellipsis_pos)
14075 glyph--, x -= glyph->pixel_width;
14076 /* That loop always goes one position too far, including
14077 the glyph before the ellipsis. So scan forward over
14078 that one. */
14079 x += glyph->pixel_width;
14080 glyph++;
14082 else /* row is reversed */
14084 ellipsis_pos = (glyph + 1)->charpos;
14085 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14086 && (glyph + 1)->charpos == ellipsis_pos)
14087 glyph++, x += glyph->pixel_width;
14088 x -= glyph->pixel_width;
14089 glyph--;
14092 else if (match_with_avoid_cursor)
14094 cursor = glyph_after;
14095 x = -1;
14097 else if (string_seen)
14099 int incr = row->reversed_p ? -1 : +1;
14101 /* Need to find the glyph that came out of a string which is
14102 present at point. That glyph is somewhere between
14103 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14104 positioned between POS_BEFORE and POS_AFTER in the
14105 buffer. */
14106 struct glyph *start, *stop;
14107 EMACS_INT pos = pos_before;
14109 x = -1;
14111 /* If the row ends in a newline from a display string,
14112 reordering could have moved the glyphs belonging to the
14113 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14114 in this case we extend the search to the last glyph in
14115 the row that was not inserted by redisplay. */
14116 if (row->ends_in_newline_from_string_p)
14118 glyph_after = end;
14119 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14122 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14123 correspond to POS_BEFORE and POS_AFTER, respectively. We
14124 need START and STOP in the order that corresponds to the
14125 row's direction as given by its reversed_p flag. If the
14126 directionality of characters between POS_BEFORE and
14127 POS_AFTER is the opposite of the row's base direction,
14128 these characters will have been reordered for display,
14129 and we need to reverse START and STOP. */
14130 if (!row->reversed_p)
14132 start = min (glyph_before, glyph_after);
14133 stop = max (glyph_before, glyph_after);
14135 else
14137 start = max (glyph_before, glyph_after);
14138 stop = min (glyph_before, glyph_after);
14140 for (glyph = start + incr;
14141 row->reversed_p ? glyph > stop : glyph < stop; )
14144 /* Any glyphs that come from the buffer are here because
14145 of bidi reordering. Skip them, and only pay
14146 attention to glyphs that came from some string. */
14147 if (STRINGP (glyph->object))
14149 Lisp_Object str;
14150 EMACS_INT tem;
14151 /* If the display property covers the newline, we
14152 need to search for it one position farther. */
14153 EMACS_INT lim = pos_after
14154 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14156 string_from_text_prop = 0;
14157 str = glyph->object;
14158 tem = string_buffer_position_lim (str, pos, lim, 0);
14159 if (tem == 0 /* from overlay */
14160 || pos <= tem)
14162 /* If the string from which this glyph came is
14163 found in the buffer at point, then we've
14164 found the glyph we've been looking for. If
14165 it comes from an overlay (tem == 0), and it
14166 has the `cursor' property on one of its
14167 glyphs, record that glyph as a candidate for
14168 displaying the cursor. (As in the
14169 unidirectional version, we will display the
14170 cursor on the last candidate we find.) */
14171 if (tem == 0 || tem == pt_old)
14173 /* The glyphs from this string could have
14174 been reordered. Find the one with the
14175 smallest string position. Or there could
14176 be a character in the string with the
14177 `cursor' property, which means display
14178 cursor on that character's glyph. */
14179 EMACS_INT strpos = glyph->charpos;
14181 if (tem)
14183 cursor = glyph;
14184 string_from_text_prop = 1;
14186 for ( ;
14187 (row->reversed_p ? glyph > stop : glyph < stop)
14188 && EQ (glyph->object, str);
14189 glyph += incr)
14191 Lisp_Object cprop;
14192 EMACS_INT gpos = glyph->charpos;
14194 cprop = Fget_char_property (make_number (gpos),
14195 Qcursor,
14196 glyph->object);
14197 if (!NILP (cprop))
14199 cursor = glyph;
14200 break;
14202 if (tem && glyph->charpos < strpos)
14204 strpos = glyph->charpos;
14205 cursor = glyph;
14209 if (tem == pt_old)
14210 goto compute_x;
14212 if (tem)
14213 pos = tem + 1; /* don't find previous instances */
14215 /* This string is not what we want; skip all of the
14216 glyphs that came from it. */
14217 while ((row->reversed_p ? glyph > stop : glyph < stop)
14218 && EQ (glyph->object, str))
14219 glyph += incr;
14221 else
14222 glyph += incr;
14225 /* If we reached the end of the line, and END was from a string,
14226 the cursor is not on this line. */
14227 if (cursor == NULL
14228 && (row->reversed_p ? glyph <= end : glyph >= end)
14229 && STRINGP (end->object)
14230 && row->continued_p)
14231 return 0;
14233 /* A truncated row may not include PT among its character positions.
14234 Setting the cursor inside the scroll margin will trigger
14235 recalculation of hscroll in hscroll_window_tree. But if a
14236 display string covers point, defer to the string-handling
14237 code below to figure this out. */
14238 else if (row->truncated_on_left_p && pt_old < bpos_min)
14240 cursor = glyph_before;
14241 x = -1;
14243 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14244 /* Zero-width characters produce no glyphs. */
14245 || (!empty_line_p
14246 && (row->reversed_p
14247 ? glyph_after > glyphs_end
14248 : glyph_after < glyphs_end)))
14250 cursor = glyph_after;
14251 x = -1;
14255 compute_x:
14256 if (cursor != NULL)
14257 glyph = cursor;
14258 if (x < 0)
14260 struct glyph *g;
14262 /* Need to compute x that corresponds to GLYPH. */
14263 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14265 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14266 abort ();
14267 x += g->pixel_width;
14271 /* ROW could be part of a continued line, which, under bidi
14272 reordering, might have other rows whose start and end charpos
14273 occlude point. Only set w->cursor if we found a better
14274 approximation to the cursor position than we have from previously
14275 examined candidate rows belonging to the same continued line. */
14276 if (/* we already have a candidate row */
14277 w->cursor.vpos >= 0
14278 /* that candidate is not the row we are processing */
14279 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14280 /* Make sure cursor.vpos specifies a row whose start and end
14281 charpos occlude point, and it is valid candidate for being a
14282 cursor-row. This is because some callers of this function
14283 leave cursor.vpos at the row where the cursor was displayed
14284 during the last redisplay cycle. */
14285 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14286 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14287 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14289 struct glyph *g1 =
14290 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14292 /* Don't consider glyphs that are outside TEXT_AREA. */
14293 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14294 return 0;
14295 /* Keep the candidate whose buffer position is the closest to
14296 point or has the `cursor' property. */
14297 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14298 w->cursor.hpos >= 0
14299 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14300 && ((BUFFERP (g1->object)
14301 && (g1->charpos == pt_old /* an exact match always wins */
14302 || (BUFFERP (glyph->object)
14303 && eabs (g1->charpos - pt_old)
14304 < eabs (glyph->charpos - pt_old))))
14305 /* previous candidate is a glyph from a string that has
14306 a non-nil `cursor' property */
14307 || (STRINGP (g1->object)
14308 && (!NILP (Fget_char_property (make_number (g1->charpos),
14309 Qcursor, g1->object))
14310 /* previous candidate is from the same display
14311 string as this one, and the display string
14312 came from a text property */
14313 || (EQ (g1->object, glyph->object)
14314 && string_from_text_prop)
14315 /* this candidate is from newline and its
14316 position is not an exact match */
14317 || (INTEGERP (glyph->object)
14318 && glyph->charpos != pt_old)))))
14319 return 0;
14320 /* If this candidate gives an exact match, use that. */
14321 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14322 /* If this candidate is a glyph created for the
14323 terminating newline of a line, and point is on that
14324 newline, it wins because it's an exact match. */
14325 || (!row->continued_p
14326 && INTEGERP (glyph->object)
14327 && glyph->charpos == 0
14328 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14329 /* Otherwise, keep the candidate that comes from a row
14330 spanning less buffer positions. This may win when one or
14331 both candidate positions are on glyphs that came from
14332 display strings, for which we cannot compare buffer
14333 positions. */
14334 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14335 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14336 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14337 return 0;
14339 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14340 w->cursor.x = x;
14341 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14342 w->cursor.y = row->y + dy;
14344 if (w == XWINDOW (selected_window))
14346 if (!row->continued_p
14347 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14348 && row->x == 0)
14350 this_line_buffer = XBUFFER (w->buffer);
14352 CHARPOS (this_line_start_pos)
14353 = MATRIX_ROW_START_CHARPOS (row) + delta;
14354 BYTEPOS (this_line_start_pos)
14355 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14357 CHARPOS (this_line_end_pos)
14358 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14359 BYTEPOS (this_line_end_pos)
14360 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14362 this_line_y = w->cursor.y;
14363 this_line_pixel_height = row->height;
14364 this_line_vpos = w->cursor.vpos;
14365 this_line_start_x = row->x;
14367 else
14368 CHARPOS (this_line_start_pos) = 0;
14371 return 1;
14375 /* Run window scroll functions, if any, for WINDOW with new window
14376 start STARTP. Sets the window start of WINDOW to that position.
14378 We assume that the window's buffer is really current. */
14380 static inline struct text_pos
14381 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14383 struct window *w = XWINDOW (window);
14384 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14386 if (current_buffer != XBUFFER (w->buffer))
14387 abort ();
14389 if (!NILP (Vwindow_scroll_functions))
14391 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14392 make_number (CHARPOS (startp)));
14393 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14394 /* In case the hook functions switch buffers. */
14395 if (current_buffer != XBUFFER (w->buffer))
14396 set_buffer_internal_1 (XBUFFER (w->buffer));
14399 return startp;
14403 /* Make sure the line containing the cursor is fully visible.
14404 A value of 1 means there is nothing to be done.
14405 (Either the line is fully visible, or it cannot be made so,
14406 or we cannot tell.)
14408 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14409 is higher than window.
14411 A value of 0 means the caller should do scrolling
14412 as if point had gone off the screen. */
14414 static int
14415 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14417 struct glyph_matrix *matrix;
14418 struct glyph_row *row;
14419 int window_height;
14421 if (!make_cursor_line_fully_visible_p)
14422 return 1;
14424 /* It's not always possible to find the cursor, e.g, when a window
14425 is full of overlay strings. Don't do anything in that case. */
14426 if (w->cursor.vpos < 0)
14427 return 1;
14429 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14430 row = MATRIX_ROW (matrix, w->cursor.vpos);
14432 /* If the cursor row is not partially visible, there's nothing to do. */
14433 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14434 return 1;
14436 /* If the row the cursor is in is taller than the window's height,
14437 it's not clear what to do, so do nothing. */
14438 window_height = window_box_height (w);
14439 if (row->height >= window_height)
14441 if (!force_p || MINI_WINDOW_P (w)
14442 || w->vscroll || w->cursor.vpos == 0)
14443 return 1;
14445 return 0;
14449 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14450 non-zero means only WINDOW is redisplayed in redisplay_internal.
14451 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14452 in redisplay_window to bring a partially visible line into view in
14453 the case that only the cursor has moved.
14455 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14456 last screen line's vertical height extends past the end of the screen.
14458 Value is
14460 1 if scrolling succeeded
14462 0 if scrolling didn't find point.
14464 -1 if new fonts have been loaded so that we must interrupt
14465 redisplay, adjust glyph matrices, and try again. */
14467 enum
14469 SCROLLING_SUCCESS,
14470 SCROLLING_FAILED,
14471 SCROLLING_NEED_LARGER_MATRICES
14474 /* If scroll-conservatively is more than this, never recenter.
14476 If you change this, don't forget to update the doc string of
14477 `scroll-conservatively' and the Emacs manual. */
14478 #define SCROLL_LIMIT 100
14480 static int
14481 try_scrolling (Lisp_Object window, int just_this_one_p,
14482 EMACS_INT arg_scroll_conservatively, EMACS_INT scroll_step,
14483 int temp_scroll_step, int last_line_misfit)
14485 struct window *w = XWINDOW (window);
14486 struct frame *f = XFRAME (w->frame);
14487 struct text_pos pos, startp;
14488 struct it it;
14489 int this_scroll_margin, scroll_max, rc, height;
14490 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14491 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14492 Lisp_Object aggressive;
14493 /* We will never try scrolling more than this number of lines. */
14494 int scroll_limit = SCROLL_LIMIT;
14496 #if GLYPH_DEBUG
14497 debug_method_add (w, "try_scrolling");
14498 #endif
14500 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14502 /* Compute scroll margin height in pixels. We scroll when point is
14503 within this distance from the top or bottom of the window. */
14504 if (scroll_margin > 0)
14505 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14506 * FRAME_LINE_HEIGHT (f);
14507 else
14508 this_scroll_margin = 0;
14510 /* Force arg_scroll_conservatively to have a reasonable value, to
14511 avoid scrolling too far away with slow move_it_* functions. Note
14512 that the user can supply scroll-conservatively equal to
14513 `most-positive-fixnum', which can be larger than INT_MAX. */
14514 if (arg_scroll_conservatively > scroll_limit)
14516 arg_scroll_conservatively = scroll_limit + 1;
14517 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14519 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14520 /* Compute how much we should try to scroll maximally to bring
14521 point into view. */
14522 scroll_max = (max (scroll_step,
14523 max (arg_scroll_conservatively, temp_scroll_step))
14524 * FRAME_LINE_HEIGHT (f));
14525 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14526 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14527 /* We're trying to scroll because of aggressive scrolling but no
14528 scroll_step is set. Choose an arbitrary one. */
14529 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14530 else
14531 scroll_max = 0;
14533 too_near_end:
14535 /* Decide whether to scroll down. */
14536 if (PT > CHARPOS (startp))
14538 int scroll_margin_y;
14540 /* Compute the pixel ypos of the scroll margin, then move IT to
14541 either that ypos or PT, whichever comes first. */
14542 start_display (&it, w, startp);
14543 scroll_margin_y = it.last_visible_y - this_scroll_margin
14544 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14545 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14546 (MOVE_TO_POS | MOVE_TO_Y));
14548 if (PT > CHARPOS (it.current.pos))
14550 int y0 = line_bottom_y (&it);
14551 /* Compute how many pixels below window bottom to stop searching
14552 for PT. This avoids costly search for PT that is far away if
14553 the user limited scrolling by a small number of lines, but
14554 always finds PT if scroll_conservatively is set to a large
14555 number, such as most-positive-fixnum. */
14556 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14557 int y_to_move = it.last_visible_y + slack;
14559 /* Compute the distance from the scroll margin to PT or to
14560 the scroll limit, whichever comes first. This should
14561 include the height of the cursor line, to make that line
14562 fully visible. */
14563 move_it_to (&it, PT, -1, y_to_move,
14564 -1, MOVE_TO_POS | MOVE_TO_Y);
14565 dy = line_bottom_y (&it) - y0;
14567 if (dy > scroll_max)
14568 return SCROLLING_FAILED;
14570 if (dy > 0)
14571 scroll_down_p = 1;
14575 if (scroll_down_p)
14577 /* Point is in or below the bottom scroll margin, so move the
14578 window start down. If scrolling conservatively, move it just
14579 enough down to make point visible. If scroll_step is set,
14580 move it down by scroll_step. */
14581 if (arg_scroll_conservatively)
14582 amount_to_scroll
14583 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14584 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14585 else if (scroll_step || temp_scroll_step)
14586 amount_to_scroll = scroll_max;
14587 else
14589 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14590 height = WINDOW_BOX_TEXT_HEIGHT (w);
14591 if (NUMBERP (aggressive))
14593 double float_amount = XFLOATINT (aggressive) * height;
14594 amount_to_scroll = float_amount;
14595 if (amount_to_scroll == 0 && float_amount > 0)
14596 amount_to_scroll = 1;
14597 /* Don't let point enter the scroll margin near top of
14598 the window. */
14599 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14600 amount_to_scroll = height - 2*this_scroll_margin + dy;
14604 if (amount_to_scroll <= 0)
14605 return SCROLLING_FAILED;
14607 start_display (&it, w, startp);
14608 if (arg_scroll_conservatively <= scroll_limit)
14609 move_it_vertically (&it, amount_to_scroll);
14610 else
14612 /* Extra precision for users who set scroll-conservatively
14613 to a large number: make sure the amount we scroll
14614 the window start is never less than amount_to_scroll,
14615 which was computed as distance from window bottom to
14616 point. This matters when lines at window top and lines
14617 below window bottom have different height. */
14618 struct it it1;
14619 void *it1data = NULL;
14620 /* We use a temporary it1 because line_bottom_y can modify
14621 its argument, if it moves one line down; see there. */
14622 int start_y;
14624 SAVE_IT (it1, it, it1data);
14625 start_y = line_bottom_y (&it1);
14626 do {
14627 RESTORE_IT (&it, &it, it1data);
14628 move_it_by_lines (&it, 1);
14629 SAVE_IT (it1, it, it1data);
14630 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14633 /* If STARTP is unchanged, move it down another screen line. */
14634 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14635 move_it_by_lines (&it, 1);
14636 startp = it.current.pos;
14638 else
14640 struct text_pos scroll_margin_pos = startp;
14642 /* See if point is inside the scroll margin at the top of the
14643 window. */
14644 if (this_scroll_margin)
14646 start_display (&it, w, startp);
14647 move_it_vertically (&it, this_scroll_margin);
14648 scroll_margin_pos = it.current.pos;
14651 if (PT < CHARPOS (scroll_margin_pos))
14653 /* Point is in the scroll margin at the top of the window or
14654 above what is displayed in the window. */
14655 int y0, y_to_move;
14657 /* Compute the vertical distance from PT to the scroll
14658 margin position. Move as far as scroll_max allows, or
14659 one screenful, or 10 screen lines, whichever is largest.
14660 Give up if distance is greater than scroll_max. */
14661 SET_TEXT_POS (pos, PT, PT_BYTE);
14662 start_display (&it, w, pos);
14663 y0 = it.current_y;
14664 y_to_move = max (it.last_visible_y,
14665 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14666 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14667 y_to_move, -1,
14668 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14669 dy = it.current_y - y0;
14670 if (dy > scroll_max)
14671 return SCROLLING_FAILED;
14673 /* Compute new window start. */
14674 start_display (&it, w, startp);
14676 if (arg_scroll_conservatively)
14677 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14678 max (scroll_step, temp_scroll_step));
14679 else if (scroll_step || temp_scroll_step)
14680 amount_to_scroll = scroll_max;
14681 else
14683 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14684 height = WINDOW_BOX_TEXT_HEIGHT (w);
14685 if (NUMBERP (aggressive))
14687 double float_amount = XFLOATINT (aggressive) * height;
14688 amount_to_scroll = float_amount;
14689 if (amount_to_scroll == 0 && float_amount > 0)
14690 amount_to_scroll = 1;
14691 amount_to_scroll -=
14692 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14693 /* Don't let point enter the scroll margin near
14694 bottom of the window. */
14695 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14696 amount_to_scroll = height - 2*this_scroll_margin + dy;
14700 if (amount_to_scroll <= 0)
14701 return SCROLLING_FAILED;
14703 move_it_vertically_backward (&it, amount_to_scroll);
14704 startp = it.current.pos;
14708 /* Run window scroll functions. */
14709 startp = run_window_scroll_functions (window, startp);
14711 /* Display the window. Give up if new fonts are loaded, or if point
14712 doesn't appear. */
14713 if (!try_window (window, startp, 0))
14714 rc = SCROLLING_NEED_LARGER_MATRICES;
14715 else if (w->cursor.vpos < 0)
14717 clear_glyph_matrix (w->desired_matrix);
14718 rc = SCROLLING_FAILED;
14720 else
14722 /* Maybe forget recorded base line for line number display. */
14723 if (!just_this_one_p
14724 || current_buffer->clip_changed
14725 || BEG_UNCHANGED < CHARPOS (startp))
14726 w->base_line_number = Qnil;
14728 /* If cursor ends up on a partially visible line,
14729 treat that as being off the bottom of the screen. */
14730 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14731 /* It's possible that the cursor is on the first line of the
14732 buffer, which is partially obscured due to a vscroll
14733 (Bug#7537). In that case, avoid looping forever . */
14734 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14736 clear_glyph_matrix (w->desired_matrix);
14737 ++extra_scroll_margin_lines;
14738 goto too_near_end;
14740 rc = SCROLLING_SUCCESS;
14743 return rc;
14747 /* Compute a suitable window start for window W if display of W starts
14748 on a continuation line. Value is non-zero if a new window start
14749 was computed.
14751 The new window start will be computed, based on W's width, starting
14752 from the start of the continued line. It is the start of the
14753 screen line with the minimum distance from the old start W->start. */
14755 static int
14756 compute_window_start_on_continuation_line (struct window *w)
14758 struct text_pos pos, start_pos;
14759 int window_start_changed_p = 0;
14761 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14763 /* If window start is on a continuation line... Window start may be
14764 < BEGV in case there's invisible text at the start of the
14765 buffer (M-x rmail, for example). */
14766 if (CHARPOS (start_pos) > BEGV
14767 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14769 struct it it;
14770 struct glyph_row *row;
14772 /* Handle the case that the window start is out of range. */
14773 if (CHARPOS (start_pos) < BEGV)
14774 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14775 else if (CHARPOS (start_pos) > ZV)
14776 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14778 /* Find the start of the continued line. This should be fast
14779 because scan_buffer is fast (newline cache). */
14780 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14781 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14782 row, DEFAULT_FACE_ID);
14783 reseat_at_previous_visible_line_start (&it);
14785 /* If the line start is "too far" away from the window start,
14786 say it takes too much time to compute a new window start. */
14787 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14788 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14790 int min_distance, distance;
14792 /* Move forward by display lines to find the new window
14793 start. If window width was enlarged, the new start can
14794 be expected to be > the old start. If window width was
14795 decreased, the new window start will be < the old start.
14796 So, we're looking for the display line start with the
14797 minimum distance from the old window start. */
14798 pos = it.current.pos;
14799 min_distance = INFINITY;
14800 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14801 distance < min_distance)
14803 min_distance = distance;
14804 pos = it.current.pos;
14805 move_it_by_lines (&it, 1);
14808 /* Set the window start there. */
14809 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14810 window_start_changed_p = 1;
14814 return window_start_changed_p;
14818 /* Try cursor movement in case text has not changed in window WINDOW,
14819 with window start STARTP. Value is
14821 CURSOR_MOVEMENT_SUCCESS if successful
14823 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14825 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14826 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14827 we want to scroll as if scroll-step were set to 1. See the code.
14829 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14830 which case we have to abort this redisplay, and adjust matrices
14831 first. */
14833 enum
14835 CURSOR_MOVEMENT_SUCCESS,
14836 CURSOR_MOVEMENT_CANNOT_BE_USED,
14837 CURSOR_MOVEMENT_MUST_SCROLL,
14838 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14841 static int
14842 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14844 struct window *w = XWINDOW (window);
14845 struct frame *f = XFRAME (w->frame);
14846 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14848 #if GLYPH_DEBUG
14849 if (inhibit_try_cursor_movement)
14850 return rc;
14851 #endif
14853 /* Handle case where text has not changed, only point, and it has
14854 not moved off the frame. */
14855 if (/* Point may be in this window. */
14856 PT >= CHARPOS (startp)
14857 /* Selective display hasn't changed. */
14858 && !current_buffer->clip_changed
14859 /* Function force-mode-line-update is used to force a thorough
14860 redisplay. It sets either windows_or_buffers_changed or
14861 update_mode_lines. So don't take a shortcut here for these
14862 cases. */
14863 && !update_mode_lines
14864 && !windows_or_buffers_changed
14865 && !cursor_type_changed
14866 /* Can't use this case if highlighting a region. When a
14867 region exists, cursor movement has to do more than just
14868 set the cursor. */
14869 && !(!NILP (Vtransient_mark_mode)
14870 && !NILP (BVAR (current_buffer, mark_active)))
14871 && NILP (w->region_showing)
14872 && NILP (Vshow_trailing_whitespace)
14873 /* Right after splitting windows, last_point may be nil. */
14874 && INTEGERP (w->last_point)
14875 /* This code is not used for mini-buffer for the sake of the case
14876 of redisplaying to replace an echo area message; since in
14877 that case the mini-buffer contents per se are usually
14878 unchanged. This code is of no real use in the mini-buffer
14879 since the handling of this_line_start_pos, etc., in redisplay
14880 handles the same cases. */
14881 && !EQ (window, minibuf_window)
14882 /* When splitting windows or for new windows, it happens that
14883 redisplay is called with a nil window_end_vpos or one being
14884 larger than the window. This should really be fixed in
14885 window.c. I don't have this on my list, now, so we do
14886 approximately the same as the old redisplay code. --gerd. */
14887 && INTEGERP (w->window_end_vpos)
14888 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14889 && (FRAME_WINDOW_P (f)
14890 || !overlay_arrow_in_current_buffer_p ()))
14892 int this_scroll_margin, top_scroll_margin;
14893 struct glyph_row *row = NULL;
14895 #if GLYPH_DEBUG
14896 debug_method_add (w, "cursor movement");
14897 #endif
14899 /* Scroll if point within this distance from the top or bottom
14900 of the window. This is a pixel value. */
14901 if (scroll_margin > 0)
14903 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14904 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14906 else
14907 this_scroll_margin = 0;
14909 top_scroll_margin = this_scroll_margin;
14910 if (WINDOW_WANTS_HEADER_LINE_P (w))
14911 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14913 /* Start with the row the cursor was displayed during the last
14914 not paused redisplay. Give up if that row is not valid. */
14915 if (w->last_cursor.vpos < 0
14916 || w->last_cursor.vpos >= w->current_matrix->nrows)
14917 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14918 else
14920 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14921 if (row->mode_line_p)
14922 ++row;
14923 if (!row->enabled_p)
14924 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14927 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14929 int scroll_p = 0, must_scroll = 0;
14930 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14932 if (PT > XFASTINT (w->last_point))
14934 /* Point has moved forward. */
14935 while (MATRIX_ROW_END_CHARPOS (row) < PT
14936 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14938 xassert (row->enabled_p);
14939 ++row;
14942 /* If the end position of a row equals the start
14943 position of the next row, and PT is at that position,
14944 we would rather display cursor in the next line. */
14945 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14946 && MATRIX_ROW_END_CHARPOS (row) == PT
14947 && row < w->current_matrix->rows
14948 + w->current_matrix->nrows - 1
14949 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14950 && !cursor_row_p (row))
14951 ++row;
14953 /* If within the scroll margin, scroll. Note that
14954 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14955 the next line would be drawn, and that
14956 this_scroll_margin can be zero. */
14957 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14958 || PT > MATRIX_ROW_END_CHARPOS (row)
14959 /* Line is completely visible last line in window
14960 and PT is to be set in the next line. */
14961 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14962 && PT == MATRIX_ROW_END_CHARPOS (row)
14963 && !row->ends_at_zv_p
14964 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14965 scroll_p = 1;
14967 else if (PT < XFASTINT (w->last_point))
14969 /* Cursor has to be moved backward. Note that PT >=
14970 CHARPOS (startp) because of the outer if-statement. */
14971 while (!row->mode_line_p
14972 && (MATRIX_ROW_START_CHARPOS (row) > PT
14973 || (MATRIX_ROW_START_CHARPOS (row) == PT
14974 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14975 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14976 row > w->current_matrix->rows
14977 && (row-1)->ends_in_newline_from_string_p))))
14978 && (row->y > top_scroll_margin
14979 || CHARPOS (startp) == BEGV))
14981 xassert (row->enabled_p);
14982 --row;
14985 /* Consider the following case: Window starts at BEGV,
14986 there is invisible, intangible text at BEGV, so that
14987 display starts at some point START > BEGV. It can
14988 happen that we are called with PT somewhere between
14989 BEGV and START. Try to handle that case. */
14990 if (row < w->current_matrix->rows
14991 || row->mode_line_p)
14993 row = w->current_matrix->rows;
14994 if (row->mode_line_p)
14995 ++row;
14998 /* Due to newlines in overlay strings, we may have to
14999 skip forward over overlay strings. */
15000 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15001 && MATRIX_ROW_END_CHARPOS (row) == PT
15002 && !cursor_row_p (row))
15003 ++row;
15005 /* If within the scroll margin, scroll. */
15006 if (row->y < top_scroll_margin
15007 && CHARPOS (startp) != BEGV)
15008 scroll_p = 1;
15010 else
15012 /* Cursor did not move. So don't scroll even if cursor line
15013 is partially visible, as it was so before. */
15014 rc = CURSOR_MOVEMENT_SUCCESS;
15017 if (PT < MATRIX_ROW_START_CHARPOS (row)
15018 || PT > MATRIX_ROW_END_CHARPOS (row))
15020 /* if PT is not in the glyph row, give up. */
15021 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15022 must_scroll = 1;
15024 else if (rc != CURSOR_MOVEMENT_SUCCESS
15025 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15027 struct glyph_row *row1;
15029 /* If rows are bidi-reordered and point moved, back up
15030 until we find a row that does not belong to a
15031 continuation line. This is because we must consider
15032 all rows of a continued line as candidates for the
15033 new cursor positioning, since row start and end
15034 positions change non-linearly with vertical position
15035 in such rows. */
15036 /* FIXME: Revisit this when glyph ``spilling'' in
15037 continuation lines' rows is implemented for
15038 bidi-reordered rows. */
15039 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15040 MATRIX_ROW_CONTINUATION_LINE_P (row);
15041 --row)
15043 /* If we hit the beginning of the displayed portion
15044 without finding the first row of a continued
15045 line, give up. */
15046 if (row <= row1)
15048 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15049 break;
15051 xassert (row->enabled_p);
15054 if (must_scroll)
15056 else if (rc != CURSOR_MOVEMENT_SUCCESS
15057 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15058 /* Make sure this isn't a header line by any chance, since
15059 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15060 && !row->mode_line_p
15061 && make_cursor_line_fully_visible_p)
15063 if (PT == MATRIX_ROW_END_CHARPOS (row)
15064 && !row->ends_at_zv_p
15065 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15066 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15067 else if (row->height > window_box_height (w))
15069 /* If we end up in a partially visible line, let's
15070 make it fully visible, except when it's taller
15071 than the window, in which case we can't do much
15072 about it. */
15073 *scroll_step = 1;
15074 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15076 else
15078 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15079 if (!cursor_row_fully_visible_p (w, 0, 1))
15080 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15081 else
15082 rc = CURSOR_MOVEMENT_SUCCESS;
15085 else if (scroll_p)
15086 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15087 else if (rc != CURSOR_MOVEMENT_SUCCESS
15088 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15090 /* With bidi-reordered rows, there could be more than
15091 one candidate row whose start and end positions
15092 occlude point. We need to let set_cursor_from_row
15093 find the best candidate. */
15094 /* FIXME: Revisit this when glyph ``spilling'' in
15095 continuation lines' rows is implemented for
15096 bidi-reordered rows. */
15097 int rv = 0;
15101 int at_zv_p = 0, exact_match_p = 0;
15103 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15104 && PT <= MATRIX_ROW_END_CHARPOS (row)
15105 && cursor_row_p (row))
15106 rv |= set_cursor_from_row (w, row, w->current_matrix,
15107 0, 0, 0, 0);
15108 /* As soon as we've found the exact match for point,
15109 or the first suitable row whose ends_at_zv_p flag
15110 is set, we are done. */
15111 at_zv_p =
15112 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15113 if (rv && !at_zv_p
15114 && w->cursor.hpos >= 0
15115 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15116 w->cursor.vpos))
15118 struct glyph_row *candidate =
15119 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15120 struct glyph *g =
15121 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15122 EMACS_INT endpos = MATRIX_ROW_END_CHARPOS (candidate);
15124 exact_match_p =
15125 (BUFFERP (g->object) && g->charpos == PT)
15126 || (INTEGERP (g->object)
15127 && (g->charpos == PT
15128 || (g->charpos == 0 && endpos - 1 == PT)));
15130 if (rv && (at_zv_p || exact_match_p))
15132 rc = CURSOR_MOVEMENT_SUCCESS;
15133 break;
15135 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15136 break;
15137 ++row;
15139 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15140 || row->continued_p)
15141 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15142 || (MATRIX_ROW_START_CHARPOS (row) == PT
15143 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15144 /* If we didn't find any candidate rows, or exited the
15145 loop before all the candidates were examined, signal
15146 to the caller that this method failed. */
15147 if (rc != CURSOR_MOVEMENT_SUCCESS
15148 && !(rv
15149 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15150 && !row->continued_p))
15151 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15152 else if (rv)
15153 rc = CURSOR_MOVEMENT_SUCCESS;
15155 else
15159 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15161 rc = CURSOR_MOVEMENT_SUCCESS;
15162 break;
15164 ++row;
15166 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15167 && MATRIX_ROW_START_CHARPOS (row) == PT
15168 && cursor_row_p (row));
15173 return rc;
15176 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15177 static
15178 #endif
15179 void
15180 set_vertical_scroll_bar (struct window *w)
15182 EMACS_INT start, end, whole;
15184 /* Calculate the start and end positions for the current window.
15185 At some point, it would be nice to choose between scrollbars
15186 which reflect the whole buffer size, with special markers
15187 indicating narrowing, and scrollbars which reflect only the
15188 visible region.
15190 Note that mini-buffers sometimes aren't displaying any text. */
15191 if (!MINI_WINDOW_P (w)
15192 || (w == XWINDOW (minibuf_window)
15193 && NILP (echo_area_buffer[0])))
15195 struct buffer *buf = XBUFFER (w->buffer);
15196 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15197 start = marker_position (w->start) - BUF_BEGV (buf);
15198 /* I don't think this is guaranteed to be right. For the
15199 moment, we'll pretend it is. */
15200 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15202 if (end < start)
15203 end = start;
15204 if (whole < (end - start))
15205 whole = end - start;
15207 else
15208 start = end = whole = 0;
15210 /* Indicate what this scroll bar ought to be displaying now. */
15211 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15212 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15213 (w, end - start, whole, start);
15217 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15218 selected_window is redisplayed.
15220 We can return without actually redisplaying the window if
15221 fonts_changed_p is nonzero. In that case, redisplay_internal will
15222 retry. */
15224 static void
15225 redisplay_window (Lisp_Object window, int just_this_one_p)
15227 struct window *w = XWINDOW (window);
15228 struct frame *f = XFRAME (w->frame);
15229 struct buffer *buffer = XBUFFER (w->buffer);
15230 struct buffer *old = current_buffer;
15231 struct text_pos lpoint, opoint, startp;
15232 int update_mode_line;
15233 int tem;
15234 struct it it;
15235 /* Record it now because it's overwritten. */
15236 int current_matrix_up_to_date_p = 0;
15237 int used_current_matrix_p = 0;
15238 /* This is less strict than current_matrix_up_to_date_p.
15239 It indicates that the buffer contents and narrowing are unchanged. */
15240 int buffer_unchanged_p = 0;
15241 int temp_scroll_step = 0;
15242 int count = SPECPDL_INDEX ();
15243 int rc;
15244 int centering_position = -1;
15245 int last_line_misfit = 0;
15246 EMACS_INT beg_unchanged, end_unchanged;
15248 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15249 opoint = lpoint;
15251 /* W must be a leaf window here. */
15252 xassert (!NILP (w->buffer));
15253 #if GLYPH_DEBUG
15254 *w->desired_matrix->method = 0;
15255 #endif
15257 restart:
15258 reconsider_clip_changes (w, buffer);
15260 /* Has the mode line to be updated? */
15261 update_mode_line = (!NILP (w->update_mode_line)
15262 || update_mode_lines
15263 || buffer->clip_changed
15264 || buffer->prevent_redisplay_optimizations_p);
15266 if (MINI_WINDOW_P (w))
15268 if (w == XWINDOW (echo_area_window)
15269 && !NILP (echo_area_buffer[0]))
15271 if (update_mode_line)
15272 /* We may have to update a tty frame's menu bar or a
15273 tool-bar. Example `M-x C-h C-h C-g'. */
15274 goto finish_menu_bars;
15275 else
15276 /* We've already displayed the echo area glyphs in this window. */
15277 goto finish_scroll_bars;
15279 else if ((w != XWINDOW (minibuf_window)
15280 || minibuf_level == 0)
15281 /* When buffer is nonempty, redisplay window normally. */
15282 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15283 /* Quail displays non-mini buffers in minibuffer window.
15284 In that case, redisplay the window normally. */
15285 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15287 /* W is a mini-buffer window, but it's not active, so clear
15288 it. */
15289 int yb = window_text_bottom_y (w);
15290 struct glyph_row *row;
15291 int y;
15293 for (y = 0, row = w->desired_matrix->rows;
15294 y < yb;
15295 y += row->height, ++row)
15296 blank_row (w, row, y);
15297 goto finish_scroll_bars;
15300 clear_glyph_matrix (w->desired_matrix);
15303 /* Otherwise set up data on this window; select its buffer and point
15304 value. */
15305 /* Really select the buffer, for the sake of buffer-local
15306 variables. */
15307 set_buffer_internal_1 (XBUFFER (w->buffer));
15309 current_matrix_up_to_date_p
15310 = (!NILP (w->window_end_valid)
15311 && !current_buffer->clip_changed
15312 && !current_buffer->prevent_redisplay_optimizations_p
15313 && XFASTINT (w->last_modified) >= MODIFF
15314 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15316 /* Run the window-bottom-change-functions
15317 if it is possible that the text on the screen has changed
15318 (either due to modification of the text, or any other reason). */
15319 if (!current_matrix_up_to_date_p
15320 && !NILP (Vwindow_text_change_functions))
15322 safe_run_hooks (Qwindow_text_change_functions);
15323 goto restart;
15326 beg_unchanged = BEG_UNCHANGED;
15327 end_unchanged = END_UNCHANGED;
15329 SET_TEXT_POS (opoint, PT, PT_BYTE);
15331 specbind (Qinhibit_point_motion_hooks, Qt);
15333 buffer_unchanged_p
15334 = (!NILP (w->window_end_valid)
15335 && !current_buffer->clip_changed
15336 && XFASTINT (w->last_modified) >= MODIFF
15337 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15339 /* When windows_or_buffers_changed is non-zero, we can't rely on
15340 the window end being valid, so set it to nil there. */
15341 if (windows_or_buffers_changed)
15343 /* If window starts on a continuation line, maybe adjust the
15344 window start in case the window's width changed. */
15345 if (XMARKER (w->start)->buffer == current_buffer)
15346 compute_window_start_on_continuation_line (w);
15348 w->window_end_valid = Qnil;
15351 /* Some sanity checks. */
15352 CHECK_WINDOW_END (w);
15353 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15354 abort ();
15355 if (BYTEPOS (opoint) < CHARPOS (opoint))
15356 abort ();
15358 /* If %c is in mode line, update it if needed. */
15359 if (!NILP (w->column_number_displayed)
15360 /* This alternative quickly identifies a common case
15361 where no change is needed. */
15362 && !(PT == XFASTINT (w->last_point)
15363 && XFASTINT (w->last_modified) >= MODIFF
15364 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
15365 && (XFASTINT (w->column_number_displayed) != current_column ()))
15366 update_mode_line = 1;
15368 /* Count number of windows showing the selected buffer. An indirect
15369 buffer counts as its base buffer. */
15370 if (!just_this_one_p)
15372 struct buffer *current_base, *window_base;
15373 current_base = current_buffer;
15374 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15375 if (current_base->base_buffer)
15376 current_base = current_base->base_buffer;
15377 if (window_base->base_buffer)
15378 window_base = window_base->base_buffer;
15379 if (current_base == window_base)
15380 buffer_shared++;
15383 /* Point refers normally to the selected window. For any other
15384 window, set up appropriate value. */
15385 if (!EQ (window, selected_window))
15387 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
15388 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
15389 if (new_pt < BEGV)
15391 new_pt = BEGV;
15392 new_pt_byte = BEGV_BYTE;
15393 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15395 else if (new_pt > (ZV - 1))
15397 new_pt = ZV;
15398 new_pt_byte = ZV_BYTE;
15399 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15402 /* We don't use SET_PT so that the point-motion hooks don't run. */
15403 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15406 /* If any of the character widths specified in the display table
15407 have changed, invalidate the width run cache. It's true that
15408 this may be a bit late to catch such changes, but the rest of
15409 redisplay goes (non-fatally) haywire when the display table is
15410 changed, so why should we worry about doing any better? */
15411 if (current_buffer->width_run_cache)
15413 struct Lisp_Char_Table *disptab = buffer_display_table ();
15415 if (! disptab_matches_widthtab (disptab,
15416 XVECTOR (BVAR (current_buffer, width_table))))
15418 invalidate_region_cache (current_buffer,
15419 current_buffer->width_run_cache,
15420 BEG, Z);
15421 recompute_width_table (current_buffer, disptab);
15425 /* If window-start is screwed up, choose a new one. */
15426 if (XMARKER (w->start)->buffer != current_buffer)
15427 goto recenter;
15429 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15431 /* If someone specified a new starting point but did not insist,
15432 check whether it can be used. */
15433 if (!NILP (w->optional_new_start)
15434 && CHARPOS (startp) >= BEGV
15435 && CHARPOS (startp) <= ZV)
15437 w->optional_new_start = Qnil;
15438 start_display (&it, w, startp);
15439 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15440 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15441 if (IT_CHARPOS (it) == PT)
15442 w->force_start = Qt;
15443 /* IT may overshoot PT if text at PT is invisible. */
15444 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15445 w->force_start = Qt;
15448 force_start:
15450 /* Handle case where place to start displaying has been specified,
15451 unless the specified location is outside the accessible range. */
15452 if (!NILP (w->force_start)
15453 || w->frozen_window_start_p)
15455 /* We set this later on if we have to adjust point. */
15456 int new_vpos = -1;
15458 w->force_start = Qnil;
15459 w->vscroll = 0;
15460 w->window_end_valid = Qnil;
15462 /* Forget any recorded base line for line number display. */
15463 if (!buffer_unchanged_p)
15464 w->base_line_number = Qnil;
15466 /* Redisplay the mode line. Select the buffer properly for that.
15467 Also, run the hook window-scroll-functions
15468 because we have scrolled. */
15469 /* Note, we do this after clearing force_start because
15470 if there's an error, it is better to forget about force_start
15471 than to get into an infinite loop calling the hook functions
15472 and having them get more errors. */
15473 if (!update_mode_line
15474 || ! NILP (Vwindow_scroll_functions))
15476 update_mode_line = 1;
15477 w->update_mode_line = Qt;
15478 startp = run_window_scroll_functions (window, startp);
15481 w->last_modified = make_number (0);
15482 w->last_overlay_modified = make_number (0);
15483 if (CHARPOS (startp) < BEGV)
15484 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15485 else if (CHARPOS (startp) > ZV)
15486 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15488 /* Redisplay, then check if cursor has been set during the
15489 redisplay. Give up if new fonts were loaded. */
15490 /* We used to issue a CHECK_MARGINS argument to try_window here,
15491 but this causes scrolling to fail when point begins inside
15492 the scroll margin (bug#148) -- cyd */
15493 if (!try_window (window, startp, 0))
15495 w->force_start = Qt;
15496 clear_glyph_matrix (w->desired_matrix);
15497 goto need_larger_matrices;
15500 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15502 /* If point does not appear, try to move point so it does
15503 appear. The desired matrix has been built above, so we
15504 can use it here. */
15505 new_vpos = window_box_height (w) / 2;
15508 if (!cursor_row_fully_visible_p (w, 0, 0))
15510 /* Point does appear, but on a line partly visible at end of window.
15511 Move it back to a fully-visible line. */
15512 new_vpos = window_box_height (w);
15515 /* If we need to move point for either of the above reasons,
15516 now actually do it. */
15517 if (new_vpos >= 0)
15519 struct glyph_row *row;
15521 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15522 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15523 ++row;
15525 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15526 MATRIX_ROW_START_BYTEPOS (row));
15528 if (w != XWINDOW (selected_window))
15529 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15530 else if (current_buffer == old)
15531 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15533 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15535 /* If we are highlighting the region, then we just changed
15536 the region, so redisplay to show it. */
15537 if (!NILP (Vtransient_mark_mode)
15538 && !NILP (BVAR (current_buffer, mark_active)))
15540 clear_glyph_matrix (w->desired_matrix);
15541 if (!try_window (window, startp, 0))
15542 goto need_larger_matrices;
15546 #if GLYPH_DEBUG
15547 debug_method_add (w, "forced window start");
15548 #endif
15549 goto done;
15552 /* Handle case where text has not changed, only point, and it has
15553 not moved off the frame, and we are not retrying after hscroll.
15554 (current_matrix_up_to_date_p is nonzero when retrying.) */
15555 if (current_matrix_up_to_date_p
15556 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15557 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15559 switch (rc)
15561 case CURSOR_MOVEMENT_SUCCESS:
15562 used_current_matrix_p = 1;
15563 goto done;
15565 case CURSOR_MOVEMENT_MUST_SCROLL:
15566 goto try_to_scroll;
15568 default:
15569 abort ();
15572 /* If current starting point was originally the beginning of a line
15573 but no longer is, find a new starting point. */
15574 else if (!NILP (w->start_at_line_beg)
15575 && !(CHARPOS (startp) <= BEGV
15576 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15578 #if GLYPH_DEBUG
15579 debug_method_add (w, "recenter 1");
15580 #endif
15581 goto recenter;
15584 /* Try scrolling with try_window_id. Value is > 0 if update has
15585 been done, it is -1 if we know that the same window start will
15586 not work. It is 0 if unsuccessful for some other reason. */
15587 else if ((tem = try_window_id (w)) != 0)
15589 #if GLYPH_DEBUG
15590 debug_method_add (w, "try_window_id %d", tem);
15591 #endif
15593 if (fonts_changed_p)
15594 goto need_larger_matrices;
15595 if (tem > 0)
15596 goto done;
15598 /* Otherwise try_window_id has returned -1 which means that we
15599 don't want the alternative below this comment to execute. */
15601 else if (CHARPOS (startp) >= BEGV
15602 && CHARPOS (startp) <= ZV
15603 && PT >= CHARPOS (startp)
15604 && (CHARPOS (startp) < ZV
15605 /* Avoid starting at end of buffer. */
15606 || CHARPOS (startp) == BEGV
15607 || (XFASTINT (w->last_modified) >= MODIFF
15608 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
15610 int d1, d2, d3, d4, d5, d6;
15612 /* If first window line is a continuation line, and window start
15613 is inside the modified region, but the first change is before
15614 current window start, we must select a new window start.
15616 However, if this is the result of a down-mouse event (e.g. by
15617 extending the mouse-drag-overlay), we don't want to select a
15618 new window start, since that would change the position under
15619 the mouse, resulting in an unwanted mouse-movement rather
15620 than a simple mouse-click. */
15621 if (NILP (w->start_at_line_beg)
15622 && NILP (do_mouse_tracking)
15623 && CHARPOS (startp) > BEGV
15624 && CHARPOS (startp) > BEG + beg_unchanged
15625 && CHARPOS (startp) <= Z - end_unchanged
15626 /* Even if w->start_at_line_beg is nil, a new window may
15627 start at a line_beg, since that's how set_buffer_window
15628 sets it. So, we need to check the return value of
15629 compute_window_start_on_continuation_line. (See also
15630 bug#197). */
15631 && XMARKER (w->start)->buffer == current_buffer
15632 && compute_window_start_on_continuation_line (w)
15633 /* It doesn't make sense to force the window start like we
15634 do at label force_start if it is already known that point
15635 will not be visible in the resulting window, because
15636 doing so will move point from its correct position
15637 instead of scrolling the window to bring point into view.
15638 See bug#9324. */
15639 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15641 w->force_start = Qt;
15642 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15643 goto force_start;
15646 #if GLYPH_DEBUG
15647 debug_method_add (w, "same window start");
15648 #endif
15650 /* Try to redisplay starting at same place as before.
15651 If point has not moved off frame, accept the results. */
15652 if (!current_matrix_up_to_date_p
15653 /* Don't use try_window_reusing_current_matrix in this case
15654 because a window scroll function can have changed the
15655 buffer. */
15656 || !NILP (Vwindow_scroll_functions)
15657 || MINI_WINDOW_P (w)
15658 || !(used_current_matrix_p
15659 = try_window_reusing_current_matrix (w)))
15661 IF_DEBUG (debug_method_add (w, "1"));
15662 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15663 /* -1 means we need to scroll.
15664 0 means we need new matrices, but fonts_changed_p
15665 is set in that case, so we will detect it below. */
15666 goto try_to_scroll;
15669 if (fonts_changed_p)
15670 goto need_larger_matrices;
15672 if (w->cursor.vpos >= 0)
15674 if (!just_this_one_p
15675 || current_buffer->clip_changed
15676 || BEG_UNCHANGED < CHARPOS (startp))
15677 /* Forget any recorded base line for line number display. */
15678 w->base_line_number = Qnil;
15680 if (!cursor_row_fully_visible_p (w, 1, 0))
15682 clear_glyph_matrix (w->desired_matrix);
15683 last_line_misfit = 1;
15685 /* Drop through and scroll. */
15686 else
15687 goto done;
15689 else
15690 clear_glyph_matrix (w->desired_matrix);
15693 try_to_scroll:
15695 w->last_modified = make_number (0);
15696 w->last_overlay_modified = make_number (0);
15698 /* Redisplay the mode line. Select the buffer properly for that. */
15699 if (!update_mode_line)
15701 update_mode_line = 1;
15702 w->update_mode_line = Qt;
15705 /* Try to scroll by specified few lines. */
15706 if ((scroll_conservatively
15707 || emacs_scroll_step
15708 || temp_scroll_step
15709 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15710 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15711 && CHARPOS (startp) >= BEGV
15712 && CHARPOS (startp) <= ZV)
15714 /* The function returns -1 if new fonts were loaded, 1 if
15715 successful, 0 if not successful. */
15716 int ss = try_scrolling (window, just_this_one_p,
15717 scroll_conservatively,
15718 emacs_scroll_step,
15719 temp_scroll_step, last_line_misfit);
15720 switch (ss)
15722 case SCROLLING_SUCCESS:
15723 goto done;
15725 case SCROLLING_NEED_LARGER_MATRICES:
15726 goto need_larger_matrices;
15728 case SCROLLING_FAILED:
15729 break;
15731 default:
15732 abort ();
15736 /* Finally, just choose a place to start which positions point
15737 according to user preferences. */
15739 recenter:
15741 #if GLYPH_DEBUG
15742 debug_method_add (w, "recenter");
15743 #endif
15745 /* w->vscroll = 0; */
15747 /* Forget any previously recorded base line for line number display. */
15748 if (!buffer_unchanged_p)
15749 w->base_line_number = Qnil;
15751 /* Determine the window start relative to point. */
15752 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15753 it.current_y = it.last_visible_y;
15754 if (centering_position < 0)
15756 int margin =
15757 scroll_margin > 0
15758 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15759 : 0;
15760 EMACS_INT margin_pos = CHARPOS (startp);
15761 Lisp_Object aggressive;
15762 int scrolling_up;
15764 /* If there is a scroll margin at the top of the window, find
15765 its character position. */
15766 if (margin
15767 /* Cannot call start_display if startp is not in the
15768 accessible region of the buffer. This can happen when we
15769 have just switched to a different buffer and/or changed
15770 its restriction. In that case, startp is initialized to
15771 the character position 1 (BEGV) because we did not yet
15772 have chance to display the buffer even once. */
15773 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15775 struct it it1;
15776 void *it1data = NULL;
15778 SAVE_IT (it1, it, it1data);
15779 start_display (&it1, w, startp);
15780 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15781 margin_pos = IT_CHARPOS (it1);
15782 RESTORE_IT (&it, &it, it1data);
15784 scrolling_up = PT > margin_pos;
15785 aggressive =
15786 scrolling_up
15787 ? BVAR (current_buffer, scroll_up_aggressively)
15788 : BVAR (current_buffer, scroll_down_aggressively);
15790 if (!MINI_WINDOW_P (w)
15791 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15793 int pt_offset = 0;
15795 /* Setting scroll-conservatively overrides
15796 scroll-*-aggressively. */
15797 if (!scroll_conservatively && NUMBERP (aggressive))
15799 double float_amount = XFLOATINT (aggressive);
15801 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15802 if (pt_offset == 0 && float_amount > 0)
15803 pt_offset = 1;
15804 if (pt_offset && margin > 0)
15805 margin -= 1;
15807 /* Compute how much to move the window start backward from
15808 point so that point will be displayed where the user
15809 wants it. */
15810 if (scrolling_up)
15812 centering_position = it.last_visible_y;
15813 if (pt_offset)
15814 centering_position -= pt_offset;
15815 centering_position -=
15816 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15817 + WINDOW_HEADER_LINE_HEIGHT (w);
15818 /* Don't let point enter the scroll margin near top of
15819 the window. */
15820 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15821 centering_position = margin * FRAME_LINE_HEIGHT (f);
15823 else
15824 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15826 else
15827 /* Set the window start half the height of the window backward
15828 from point. */
15829 centering_position = window_box_height (w) / 2;
15831 move_it_vertically_backward (&it, centering_position);
15833 xassert (IT_CHARPOS (it) >= BEGV);
15835 /* The function move_it_vertically_backward may move over more
15836 than the specified y-distance. If it->w is small, e.g. a
15837 mini-buffer window, we may end up in front of the window's
15838 display area. Start displaying at the start of the line
15839 containing PT in this case. */
15840 if (it.current_y <= 0)
15842 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15843 move_it_vertically_backward (&it, 0);
15844 it.current_y = 0;
15847 it.current_x = it.hpos = 0;
15849 /* Set the window start position here explicitly, to avoid an
15850 infinite loop in case the functions in window-scroll-functions
15851 get errors. */
15852 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15854 /* Run scroll hooks. */
15855 startp = run_window_scroll_functions (window, it.current.pos);
15857 /* Redisplay the window. */
15858 if (!current_matrix_up_to_date_p
15859 || windows_or_buffers_changed
15860 || cursor_type_changed
15861 /* Don't use try_window_reusing_current_matrix in this case
15862 because it can have changed the buffer. */
15863 || !NILP (Vwindow_scroll_functions)
15864 || !just_this_one_p
15865 || MINI_WINDOW_P (w)
15866 || !(used_current_matrix_p
15867 = try_window_reusing_current_matrix (w)))
15868 try_window (window, startp, 0);
15870 /* If new fonts have been loaded (due to fontsets), give up. We
15871 have to start a new redisplay since we need to re-adjust glyph
15872 matrices. */
15873 if (fonts_changed_p)
15874 goto need_larger_matrices;
15876 /* If cursor did not appear assume that the middle of the window is
15877 in the first line of the window. Do it again with the next line.
15878 (Imagine a window of height 100, displaying two lines of height
15879 60. Moving back 50 from it->last_visible_y will end in the first
15880 line.) */
15881 if (w->cursor.vpos < 0)
15883 if (!NILP (w->window_end_valid)
15884 && PT >= Z - XFASTINT (w->window_end_pos))
15886 clear_glyph_matrix (w->desired_matrix);
15887 move_it_by_lines (&it, 1);
15888 try_window (window, it.current.pos, 0);
15890 else if (PT < IT_CHARPOS (it))
15892 clear_glyph_matrix (w->desired_matrix);
15893 move_it_by_lines (&it, -1);
15894 try_window (window, it.current.pos, 0);
15896 else
15898 /* Not much we can do about it. */
15902 /* Consider the following case: Window starts at BEGV, there is
15903 invisible, intangible text at BEGV, so that display starts at
15904 some point START > BEGV. It can happen that we are called with
15905 PT somewhere between BEGV and START. Try to handle that case. */
15906 if (w->cursor.vpos < 0)
15908 struct glyph_row *row = w->current_matrix->rows;
15909 if (row->mode_line_p)
15910 ++row;
15911 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15914 if (!cursor_row_fully_visible_p (w, 0, 0))
15916 /* If vscroll is enabled, disable it and try again. */
15917 if (w->vscroll)
15919 w->vscroll = 0;
15920 clear_glyph_matrix (w->desired_matrix);
15921 goto recenter;
15924 /* Users who set scroll-conservatively to a large number want
15925 point just above/below the scroll margin. If we ended up
15926 with point's row partially visible, move the window start to
15927 make that row fully visible and out of the margin. */
15928 if (scroll_conservatively > SCROLL_LIMIT)
15930 int margin =
15931 scroll_margin > 0
15932 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15933 : 0;
15934 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
15936 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
15937 clear_glyph_matrix (w->desired_matrix);
15938 if (1 == try_window (window, it.current.pos,
15939 TRY_WINDOW_CHECK_MARGINS))
15940 goto done;
15943 /* If centering point failed to make the whole line visible,
15944 put point at the top instead. That has to make the whole line
15945 visible, if it can be done. */
15946 if (centering_position == 0)
15947 goto done;
15949 clear_glyph_matrix (w->desired_matrix);
15950 centering_position = 0;
15951 goto recenter;
15954 done:
15956 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15957 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
15958 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
15959 ? Qt : Qnil);
15961 /* Display the mode line, if we must. */
15962 if ((update_mode_line
15963 /* If window not full width, must redo its mode line
15964 if (a) the window to its side is being redone and
15965 (b) we do a frame-based redisplay. This is a consequence
15966 of how inverted lines are drawn in frame-based redisplay. */
15967 || (!just_this_one_p
15968 && !FRAME_WINDOW_P (f)
15969 && !WINDOW_FULL_WIDTH_P (w))
15970 /* Line number to display. */
15971 || INTEGERP (w->base_line_pos)
15972 /* Column number is displayed and different from the one displayed. */
15973 || (!NILP (w->column_number_displayed)
15974 && (XFASTINT (w->column_number_displayed) != current_column ())))
15975 /* This means that the window has a mode line. */
15976 && (WINDOW_WANTS_MODELINE_P (w)
15977 || WINDOW_WANTS_HEADER_LINE_P (w)))
15979 display_mode_lines (w);
15981 /* If mode line height has changed, arrange for a thorough
15982 immediate redisplay using the correct mode line height. */
15983 if (WINDOW_WANTS_MODELINE_P (w)
15984 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
15986 fonts_changed_p = 1;
15987 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
15988 = DESIRED_MODE_LINE_HEIGHT (w);
15991 /* If header line height has changed, arrange for a thorough
15992 immediate redisplay using the correct header line height. */
15993 if (WINDOW_WANTS_HEADER_LINE_P (w)
15994 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
15996 fonts_changed_p = 1;
15997 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
15998 = DESIRED_HEADER_LINE_HEIGHT (w);
16001 if (fonts_changed_p)
16002 goto need_larger_matrices;
16005 if (!line_number_displayed
16006 && !BUFFERP (w->base_line_pos))
16008 w->base_line_pos = Qnil;
16009 w->base_line_number = Qnil;
16012 finish_menu_bars:
16014 /* When we reach a frame's selected window, redo the frame's menu bar. */
16015 if (update_mode_line
16016 && EQ (FRAME_SELECTED_WINDOW (f), window))
16018 int redisplay_menu_p = 0;
16020 if (FRAME_WINDOW_P (f))
16022 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16023 || defined (HAVE_NS) || defined (USE_GTK)
16024 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16025 #else
16026 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16027 #endif
16029 else
16030 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16032 if (redisplay_menu_p)
16033 display_menu_bar (w);
16035 #ifdef HAVE_WINDOW_SYSTEM
16036 if (FRAME_WINDOW_P (f))
16038 #if defined (USE_GTK) || defined (HAVE_NS)
16039 if (FRAME_EXTERNAL_TOOL_BAR (f))
16040 redisplay_tool_bar (f);
16041 #else
16042 if (WINDOWP (f->tool_bar_window)
16043 && (FRAME_TOOL_BAR_LINES (f) > 0
16044 || !NILP (Vauto_resize_tool_bars))
16045 && redisplay_tool_bar (f))
16046 ignore_mouse_drag_p = 1;
16047 #endif
16049 #endif
16052 #ifdef HAVE_WINDOW_SYSTEM
16053 if (FRAME_WINDOW_P (f)
16054 && update_window_fringes (w, (just_this_one_p
16055 || (!used_current_matrix_p && !overlay_arrow_seen)
16056 || w->pseudo_window_p)))
16058 update_begin (f);
16059 BLOCK_INPUT;
16060 if (draw_window_fringes (w, 1))
16061 x_draw_vertical_border (w);
16062 UNBLOCK_INPUT;
16063 update_end (f);
16065 #endif /* HAVE_WINDOW_SYSTEM */
16067 /* We go to this label, with fonts_changed_p nonzero,
16068 if it is necessary to try again using larger glyph matrices.
16069 We have to redeem the scroll bar even in this case,
16070 because the loop in redisplay_internal expects that. */
16071 need_larger_matrices:
16073 finish_scroll_bars:
16075 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16077 /* Set the thumb's position and size. */
16078 set_vertical_scroll_bar (w);
16080 /* Note that we actually used the scroll bar attached to this
16081 window, so it shouldn't be deleted at the end of redisplay. */
16082 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16083 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16086 /* Restore current_buffer and value of point in it. The window
16087 update may have changed the buffer, so first make sure `opoint'
16088 is still valid (Bug#6177). */
16089 if (CHARPOS (opoint) < BEGV)
16090 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16091 else if (CHARPOS (opoint) > ZV)
16092 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16093 else
16094 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16096 set_buffer_internal_1 (old);
16097 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16098 shorter. This can be caused by log truncation in *Messages*. */
16099 if (CHARPOS (lpoint) <= ZV)
16100 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16102 unbind_to (count, Qnil);
16106 /* Build the complete desired matrix of WINDOW with a window start
16107 buffer position POS.
16109 Value is 1 if successful. It is zero if fonts were loaded during
16110 redisplay which makes re-adjusting glyph matrices necessary, and -1
16111 if point would appear in the scroll margins.
16112 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16113 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16114 set in FLAGS.) */
16117 try_window (Lisp_Object window, struct text_pos pos, int flags)
16119 struct window *w = XWINDOW (window);
16120 struct it it;
16121 struct glyph_row *last_text_row = NULL;
16122 struct frame *f = XFRAME (w->frame);
16124 /* Make POS the new window start. */
16125 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16127 /* Mark cursor position as unknown. No overlay arrow seen. */
16128 w->cursor.vpos = -1;
16129 overlay_arrow_seen = 0;
16131 /* Initialize iterator and info to start at POS. */
16132 start_display (&it, w, pos);
16134 /* Display all lines of W. */
16135 while (it.current_y < it.last_visible_y)
16137 if (display_line (&it))
16138 last_text_row = it.glyph_row - 1;
16139 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16140 return 0;
16143 /* Don't let the cursor end in the scroll margins. */
16144 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16145 && !MINI_WINDOW_P (w))
16147 int this_scroll_margin;
16149 if (scroll_margin > 0)
16151 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16152 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16154 else
16155 this_scroll_margin = 0;
16157 if ((w->cursor.y >= 0 /* not vscrolled */
16158 && w->cursor.y < this_scroll_margin
16159 && CHARPOS (pos) > BEGV
16160 && IT_CHARPOS (it) < ZV)
16161 /* rms: considering make_cursor_line_fully_visible_p here
16162 seems to give wrong results. We don't want to recenter
16163 when the last line is partly visible, we want to allow
16164 that case to be handled in the usual way. */
16165 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16167 w->cursor.vpos = -1;
16168 clear_glyph_matrix (w->desired_matrix);
16169 return -1;
16173 /* If bottom moved off end of frame, change mode line percentage. */
16174 if (XFASTINT (w->window_end_pos) <= 0
16175 && Z != IT_CHARPOS (it))
16176 w->update_mode_line = Qt;
16178 /* Set window_end_pos to the offset of the last character displayed
16179 on the window from the end of current_buffer. Set
16180 window_end_vpos to its row number. */
16181 if (last_text_row)
16183 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16184 w->window_end_bytepos
16185 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16186 w->window_end_pos
16187 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16188 w->window_end_vpos
16189 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16190 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
16191 ->displays_text_p);
16193 else
16195 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16196 w->window_end_pos = make_number (Z - ZV);
16197 w->window_end_vpos = make_number (0);
16200 /* But that is not valid info until redisplay finishes. */
16201 w->window_end_valid = Qnil;
16202 return 1;
16207 /************************************************************************
16208 Window redisplay reusing current matrix when buffer has not changed
16209 ************************************************************************/
16211 /* Try redisplay of window W showing an unchanged buffer with a
16212 different window start than the last time it was displayed by
16213 reusing its current matrix. Value is non-zero if successful.
16214 W->start is the new window start. */
16216 static int
16217 try_window_reusing_current_matrix (struct window *w)
16219 struct frame *f = XFRAME (w->frame);
16220 struct glyph_row *bottom_row;
16221 struct it it;
16222 struct run run;
16223 struct text_pos start, new_start;
16224 int nrows_scrolled, i;
16225 struct glyph_row *last_text_row;
16226 struct glyph_row *last_reused_text_row;
16227 struct glyph_row *start_row;
16228 int start_vpos, min_y, max_y;
16230 #if GLYPH_DEBUG
16231 if (inhibit_try_window_reusing)
16232 return 0;
16233 #endif
16235 if (/* This function doesn't handle terminal frames. */
16236 !FRAME_WINDOW_P (f)
16237 /* Don't try to reuse the display if windows have been split
16238 or such. */
16239 || windows_or_buffers_changed
16240 || cursor_type_changed)
16241 return 0;
16243 /* Can't do this if region may have changed. */
16244 if ((!NILP (Vtransient_mark_mode)
16245 && !NILP (BVAR (current_buffer, mark_active)))
16246 || !NILP (w->region_showing)
16247 || !NILP (Vshow_trailing_whitespace))
16248 return 0;
16250 /* If top-line visibility has changed, give up. */
16251 if (WINDOW_WANTS_HEADER_LINE_P (w)
16252 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16253 return 0;
16255 /* Give up if old or new display is scrolled vertically. We could
16256 make this function handle this, but right now it doesn't. */
16257 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16258 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16259 return 0;
16261 /* The variable new_start now holds the new window start. The old
16262 start `start' can be determined from the current matrix. */
16263 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16264 start = start_row->minpos;
16265 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16267 /* Clear the desired matrix for the display below. */
16268 clear_glyph_matrix (w->desired_matrix);
16270 if (CHARPOS (new_start) <= CHARPOS (start))
16272 /* Don't use this method if the display starts with an ellipsis
16273 displayed for invisible text. It's not easy to handle that case
16274 below, and it's certainly not worth the effort since this is
16275 not a frequent case. */
16276 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16277 return 0;
16279 IF_DEBUG (debug_method_add (w, "twu1"));
16281 /* Display up to a row that can be reused. The variable
16282 last_text_row is set to the last row displayed that displays
16283 text. Note that it.vpos == 0 if or if not there is a
16284 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16285 start_display (&it, w, new_start);
16286 w->cursor.vpos = -1;
16287 last_text_row = last_reused_text_row = NULL;
16289 while (it.current_y < it.last_visible_y
16290 && !fonts_changed_p)
16292 /* If we have reached into the characters in the START row,
16293 that means the line boundaries have changed. So we
16294 can't start copying with the row START. Maybe it will
16295 work to start copying with the following row. */
16296 while (IT_CHARPOS (it) > CHARPOS (start))
16298 /* Advance to the next row as the "start". */
16299 start_row++;
16300 start = start_row->minpos;
16301 /* If there are no more rows to try, or just one, give up. */
16302 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16303 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16304 || CHARPOS (start) == ZV)
16306 clear_glyph_matrix (w->desired_matrix);
16307 return 0;
16310 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16312 /* If we have reached alignment, we can copy the rest of the
16313 rows. */
16314 if (IT_CHARPOS (it) == CHARPOS (start)
16315 /* Don't accept "alignment" inside a display vector,
16316 since start_row could have started in the middle of
16317 that same display vector (thus their character
16318 positions match), and we have no way of telling if
16319 that is the case. */
16320 && it.current.dpvec_index < 0)
16321 break;
16323 if (display_line (&it))
16324 last_text_row = it.glyph_row - 1;
16328 /* A value of current_y < last_visible_y means that we stopped
16329 at the previous window start, which in turn means that we
16330 have at least one reusable row. */
16331 if (it.current_y < it.last_visible_y)
16333 struct glyph_row *row;
16335 /* IT.vpos always starts from 0; it counts text lines. */
16336 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16338 /* Find PT if not already found in the lines displayed. */
16339 if (w->cursor.vpos < 0)
16341 int dy = it.current_y - start_row->y;
16343 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16344 row = row_containing_pos (w, PT, row, NULL, dy);
16345 if (row)
16346 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16347 dy, nrows_scrolled);
16348 else
16350 clear_glyph_matrix (w->desired_matrix);
16351 return 0;
16355 /* Scroll the display. Do it before the current matrix is
16356 changed. The problem here is that update has not yet
16357 run, i.e. part of the current matrix is not up to date.
16358 scroll_run_hook will clear the cursor, and use the
16359 current matrix to get the height of the row the cursor is
16360 in. */
16361 run.current_y = start_row->y;
16362 run.desired_y = it.current_y;
16363 run.height = it.last_visible_y - it.current_y;
16365 if (run.height > 0 && run.current_y != run.desired_y)
16367 update_begin (f);
16368 FRAME_RIF (f)->update_window_begin_hook (w);
16369 FRAME_RIF (f)->clear_window_mouse_face (w);
16370 FRAME_RIF (f)->scroll_run_hook (w, &run);
16371 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16372 update_end (f);
16375 /* Shift current matrix down by nrows_scrolled lines. */
16376 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16377 rotate_matrix (w->current_matrix,
16378 start_vpos,
16379 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16380 nrows_scrolled);
16382 /* Disable lines that must be updated. */
16383 for (i = 0; i < nrows_scrolled; ++i)
16384 (start_row + i)->enabled_p = 0;
16386 /* Re-compute Y positions. */
16387 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16388 max_y = it.last_visible_y;
16389 for (row = start_row + nrows_scrolled;
16390 row < bottom_row;
16391 ++row)
16393 row->y = it.current_y;
16394 row->visible_height = row->height;
16396 if (row->y < min_y)
16397 row->visible_height -= min_y - row->y;
16398 if (row->y + row->height > max_y)
16399 row->visible_height -= row->y + row->height - max_y;
16400 if (row->fringe_bitmap_periodic_p)
16401 row->redraw_fringe_bitmaps_p = 1;
16403 it.current_y += row->height;
16405 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16406 last_reused_text_row = row;
16407 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16408 break;
16411 /* Disable lines in the current matrix which are now
16412 below the window. */
16413 for (++row; row < bottom_row; ++row)
16414 row->enabled_p = row->mode_line_p = 0;
16417 /* Update window_end_pos etc.; last_reused_text_row is the last
16418 reused row from the current matrix containing text, if any.
16419 The value of last_text_row is the last displayed line
16420 containing text. */
16421 if (last_reused_text_row)
16423 w->window_end_bytepos
16424 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16425 w->window_end_pos
16426 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
16427 w->window_end_vpos
16428 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16429 w->current_matrix));
16431 else if (last_text_row)
16433 w->window_end_bytepos
16434 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16435 w->window_end_pos
16436 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16437 w->window_end_vpos
16438 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16440 else
16442 /* This window must be completely empty. */
16443 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16444 w->window_end_pos = make_number (Z - ZV);
16445 w->window_end_vpos = make_number (0);
16447 w->window_end_valid = Qnil;
16449 /* Update hint: don't try scrolling again in update_window. */
16450 w->desired_matrix->no_scrolling_p = 1;
16452 #if GLYPH_DEBUG
16453 debug_method_add (w, "try_window_reusing_current_matrix 1");
16454 #endif
16455 return 1;
16457 else if (CHARPOS (new_start) > CHARPOS (start))
16459 struct glyph_row *pt_row, *row;
16460 struct glyph_row *first_reusable_row;
16461 struct glyph_row *first_row_to_display;
16462 int dy;
16463 int yb = window_text_bottom_y (w);
16465 /* Find the row starting at new_start, if there is one. Don't
16466 reuse a partially visible line at the end. */
16467 first_reusable_row = start_row;
16468 while (first_reusable_row->enabled_p
16469 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16470 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16471 < CHARPOS (new_start)))
16472 ++first_reusable_row;
16474 /* Give up if there is no row to reuse. */
16475 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16476 || !first_reusable_row->enabled_p
16477 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16478 != CHARPOS (new_start)))
16479 return 0;
16481 /* We can reuse fully visible rows beginning with
16482 first_reusable_row to the end of the window. Set
16483 first_row_to_display to the first row that cannot be reused.
16484 Set pt_row to the row containing point, if there is any. */
16485 pt_row = NULL;
16486 for (first_row_to_display = first_reusable_row;
16487 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16488 ++first_row_to_display)
16490 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16491 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16492 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16493 && first_row_to_display->ends_at_zv_p
16494 && pt_row == NULL)))
16495 pt_row = first_row_to_display;
16498 /* Start displaying at the start of first_row_to_display. */
16499 xassert (first_row_to_display->y < yb);
16500 init_to_row_start (&it, w, first_row_to_display);
16502 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16503 - start_vpos);
16504 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16505 - nrows_scrolled);
16506 it.current_y = (first_row_to_display->y - first_reusable_row->y
16507 + WINDOW_HEADER_LINE_HEIGHT (w));
16509 /* Display lines beginning with first_row_to_display in the
16510 desired matrix. Set last_text_row to the last row displayed
16511 that displays text. */
16512 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16513 if (pt_row == NULL)
16514 w->cursor.vpos = -1;
16515 last_text_row = NULL;
16516 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16517 if (display_line (&it))
16518 last_text_row = it.glyph_row - 1;
16520 /* If point is in a reused row, adjust y and vpos of the cursor
16521 position. */
16522 if (pt_row)
16524 w->cursor.vpos -= nrows_scrolled;
16525 w->cursor.y -= first_reusable_row->y - start_row->y;
16528 /* Give up if point isn't in a row displayed or reused. (This
16529 also handles the case where w->cursor.vpos < nrows_scrolled
16530 after the calls to display_line, which can happen with scroll
16531 margins. See bug#1295.) */
16532 if (w->cursor.vpos < 0)
16534 clear_glyph_matrix (w->desired_matrix);
16535 return 0;
16538 /* Scroll the display. */
16539 run.current_y = first_reusable_row->y;
16540 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16541 run.height = it.last_visible_y - run.current_y;
16542 dy = run.current_y - run.desired_y;
16544 if (run.height)
16546 update_begin (f);
16547 FRAME_RIF (f)->update_window_begin_hook (w);
16548 FRAME_RIF (f)->clear_window_mouse_face (w);
16549 FRAME_RIF (f)->scroll_run_hook (w, &run);
16550 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16551 update_end (f);
16554 /* Adjust Y positions of reused rows. */
16555 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16556 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16557 max_y = it.last_visible_y;
16558 for (row = first_reusable_row; row < first_row_to_display; ++row)
16560 row->y -= dy;
16561 row->visible_height = row->height;
16562 if (row->y < min_y)
16563 row->visible_height -= min_y - row->y;
16564 if (row->y + row->height > max_y)
16565 row->visible_height -= row->y + row->height - max_y;
16566 if (row->fringe_bitmap_periodic_p)
16567 row->redraw_fringe_bitmaps_p = 1;
16570 /* Scroll the current matrix. */
16571 xassert (nrows_scrolled > 0);
16572 rotate_matrix (w->current_matrix,
16573 start_vpos,
16574 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16575 -nrows_scrolled);
16577 /* Disable rows not reused. */
16578 for (row -= nrows_scrolled; row < bottom_row; ++row)
16579 row->enabled_p = 0;
16581 /* Point may have moved to a different line, so we cannot assume that
16582 the previous cursor position is valid; locate the correct row. */
16583 if (pt_row)
16585 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16586 row < bottom_row
16587 && PT >= MATRIX_ROW_END_CHARPOS (row)
16588 && !row->ends_at_zv_p;
16589 row++)
16591 w->cursor.vpos++;
16592 w->cursor.y = row->y;
16594 if (row < bottom_row)
16596 /* Can't simply scan the row for point with
16597 bidi-reordered glyph rows. Let set_cursor_from_row
16598 figure out where to put the cursor, and if it fails,
16599 give up. */
16600 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16602 if (!set_cursor_from_row (w, row, w->current_matrix,
16603 0, 0, 0, 0))
16605 clear_glyph_matrix (w->desired_matrix);
16606 return 0;
16609 else
16611 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16612 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16614 for (; glyph < end
16615 && (!BUFFERP (glyph->object)
16616 || glyph->charpos < PT);
16617 glyph++)
16619 w->cursor.hpos++;
16620 w->cursor.x += glyph->pixel_width;
16626 /* Adjust window end. A null value of last_text_row means that
16627 the window end is in reused rows which in turn means that
16628 only its vpos can have changed. */
16629 if (last_text_row)
16631 w->window_end_bytepos
16632 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16633 w->window_end_pos
16634 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16635 w->window_end_vpos
16636 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16638 else
16640 w->window_end_vpos
16641 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16644 w->window_end_valid = Qnil;
16645 w->desired_matrix->no_scrolling_p = 1;
16647 #if GLYPH_DEBUG
16648 debug_method_add (w, "try_window_reusing_current_matrix 2");
16649 #endif
16650 return 1;
16653 return 0;
16658 /************************************************************************
16659 Window redisplay reusing current matrix when buffer has changed
16660 ************************************************************************/
16662 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16663 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16664 EMACS_INT *, EMACS_INT *);
16665 static struct glyph_row *
16666 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16667 struct glyph_row *);
16670 /* Return the last row in MATRIX displaying text. If row START is
16671 non-null, start searching with that row. IT gives the dimensions
16672 of the display. Value is null if matrix is empty; otherwise it is
16673 a pointer to the row found. */
16675 static struct glyph_row *
16676 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16677 struct glyph_row *start)
16679 struct glyph_row *row, *row_found;
16681 /* Set row_found to the last row in IT->w's current matrix
16682 displaying text. The loop looks funny but think of partially
16683 visible lines. */
16684 row_found = NULL;
16685 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16686 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16688 xassert (row->enabled_p);
16689 row_found = row;
16690 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16691 break;
16692 ++row;
16695 return row_found;
16699 /* Return the last row in the current matrix of W that is not affected
16700 by changes at the start of current_buffer that occurred since W's
16701 current matrix was built. Value is null if no such row exists.
16703 BEG_UNCHANGED us the number of characters unchanged at the start of
16704 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16705 first changed character in current_buffer. Characters at positions <
16706 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16707 when the current matrix was built. */
16709 static struct glyph_row *
16710 find_last_unchanged_at_beg_row (struct window *w)
16712 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
16713 struct glyph_row *row;
16714 struct glyph_row *row_found = NULL;
16715 int yb = window_text_bottom_y (w);
16717 /* Find the last row displaying unchanged text. */
16718 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16719 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16720 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16721 ++row)
16723 if (/* If row ends before first_changed_pos, it is unchanged,
16724 except in some case. */
16725 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16726 /* When row ends in ZV and we write at ZV it is not
16727 unchanged. */
16728 && !row->ends_at_zv_p
16729 /* When first_changed_pos is the end of a continued line,
16730 row is not unchanged because it may be no longer
16731 continued. */
16732 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16733 && (row->continued_p
16734 || row->exact_window_width_line_p))
16735 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16736 needs to be recomputed, so don't consider this row as
16737 unchanged. This happens when the last line was
16738 bidi-reordered and was killed immediately before this
16739 redisplay cycle. In that case, ROW->end stores the
16740 buffer position of the first visual-order character of
16741 the killed text, which is now beyond ZV. */
16742 && CHARPOS (row->end.pos) <= ZV)
16743 row_found = row;
16745 /* Stop if last visible row. */
16746 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16747 break;
16750 return row_found;
16754 /* Find the first glyph row in the current matrix of W that is not
16755 affected by changes at the end of current_buffer since the
16756 time W's current matrix was built.
16758 Return in *DELTA the number of chars by which buffer positions in
16759 unchanged text at the end of current_buffer must be adjusted.
16761 Return in *DELTA_BYTES the corresponding number of bytes.
16763 Value is null if no such row exists, i.e. all rows are affected by
16764 changes. */
16766 static struct glyph_row *
16767 find_first_unchanged_at_end_row (struct window *w,
16768 EMACS_INT *delta, EMACS_INT *delta_bytes)
16770 struct glyph_row *row;
16771 struct glyph_row *row_found = NULL;
16773 *delta = *delta_bytes = 0;
16775 /* Display must not have been paused, otherwise the current matrix
16776 is not up to date. */
16777 eassert (!NILP (w->window_end_valid));
16779 /* A value of window_end_pos >= END_UNCHANGED means that the window
16780 end is in the range of changed text. If so, there is no
16781 unchanged row at the end of W's current matrix. */
16782 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16783 return NULL;
16785 /* Set row to the last row in W's current matrix displaying text. */
16786 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16788 /* If matrix is entirely empty, no unchanged row exists. */
16789 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16791 /* The value of row is the last glyph row in the matrix having a
16792 meaningful buffer position in it. The end position of row
16793 corresponds to window_end_pos. This allows us to translate
16794 buffer positions in the current matrix to current buffer
16795 positions for characters not in changed text. */
16796 EMACS_INT Z_old =
16797 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16798 EMACS_INT Z_BYTE_old =
16799 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16800 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
16801 struct glyph_row *first_text_row
16802 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16804 *delta = Z - Z_old;
16805 *delta_bytes = Z_BYTE - Z_BYTE_old;
16807 /* Set last_unchanged_pos to the buffer position of the last
16808 character in the buffer that has not been changed. Z is the
16809 index + 1 of the last character in current_buffer, i.e. by
16810 subtracting END_UNCHANGED we get the index of the last
16811 unchanged character, and we have to add BEG to get its buffer
16812 position. */
16813 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16814 last_unchanged_pos_old = last_unchanged_pos - *delta;
16816 /* Search backward from ROW for a row displaying a line that
16817 starts at a minimum position >= last_unchanged_pos_old. */
16818 for (; row > first_text_row; --row)
16820 /* This used to abort, but it can happen.
16821 It is ok to just stop the search instead here. KFS. */
16822 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16823 break;
16825 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16826 row_found = row;
16830 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16832 return row_found;
16836 /* Make sure that glyph rows in the current matrix of window W
16837 reference the same glyph memory as corresponding rows in the
16838 frame's frame matrix. This function is called after scrolling W's
16839 current matrix on a terminal frame in try_window_id and
16840 try_window_reusing_current_matrix. */
16842 static void
16843 sync_frame_with_window_matrix_rows (struct window *w)
16845 struct frame *f = XFRAME (w->frame);
16846 struct glyph_row *window_row, *window_row_end, *frame_row;
16848 /* Preconditions: W must be a leaf window and full-width. Its frame
16849 must have a frame matrix. */
16850 xassert (NILP (w->hchild) && NILP (w->vchild));
16851 xassert (WINDOW_FULL_WIDTH_P (w));
16852 xassert (!FRAME_WINDOW_P (f));
16854 /* If W is a full-width window, glyph pointers in W's current matrix
16855 have, by definition, to be the same as glyph pointers in the
16856 corresponding frame matrix. Note that frame matrices have no
16857 marginal areas (see build_frame_matrix). */
16858 window_row = w->current_matrix->rows;
16859 window_row_end = window_row + w->current_matrix->nrows;
16860 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16861 while (window_row < window_row_end)
16863 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16864 struct glyph *end = window_row->glyphs[LAST_AREA];
16866 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16867 frame_row->glyphs[TEXT_AREA] = start;
16868 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16869 frame_row->glyphs[LAST_AREA] = end;
16871 /* Disable frame rows whose corresponding window rows have
16872 been disabled in try_window_id. */
16873 if (!window_row->enabled_p)
16874 frame_row->enabled_p = 0;
16876 ++window_row, ++frame_row;
16881 /* Find the glyph row in window W containing CHARPOS. Consider all
16882 rows between START and END (not inclusive). END null means search
16883 all rows to the end of the display area of W. Value is the row
16884 containing CHARPOS or null. */
16886 struct glyph_row *
16887 row_containing_pos (struct window *w, EMACS_INT charpos,
16888 struct glyph_row *start, struct glyph_row *end, int dy)
16890 struct glyph_row *row = start;
16891 struct glyph_row *best_row = NULL;
16892 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16893 int last_y;
16895 /* If we happen to start on a header-line, skip that. */
16896 if (row->mode_line_p)
16897 ++row;
16899 if ((end && row >= end) || !row->enabled_p)
16900 return NULL;
16902 last_y = window_text_bottom_y (w) - dy;
16904 while (1)
16906 /* Give up if we have gone too far. */
16907 if (end && row >= end)
16908 return NULL;
16909 /* This formerly returned if they were equal.
16910 I think that both quantities are of a "last plus one" type;
16911 if so, when they are equal, the row is within the screen. -- rms. */
16912 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16913 return NULL;
16915 /* If it is in this row, return this row. */
16916 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16917 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16918 /* The end position of a row equals the start
16919 position of the next row. If CHARPOS is there, we
16920 would rather display it in the next line, except
16921 when this line ends in ZV. */
16922 && !row->ends_at_zv_p
16923 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16924 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16926 struct glyph *g;
16928 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16929 || (!best_row && !row->continued_p))
16930 return row;
16931 /* In bidi-reordered rows, there could be several rows
16932 occluding point, all of them belonging to the same
16933 continued line. We need to find the row which fits
16934 CHARPOS the best. */
16935 for (g = row->glyphs[TEXT_AREA];
16936 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16937 g++)
16939 if (!STRINGP (g->object))
16941 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16943 mindif = eabs (g->charpos - charpos);
16944 best_row = row;
16945 /* Exact match always wins. */
16946 if (mindif == 0)
16947 return best_row;
16952 else if (best_row && !row->continued_p)
16953 return best_row;
16954 ++row;
16959 /* Try to redisplay window W by reusing its existing display. W's
16960 current matrix must be up to date when this function is called,
16961 i.e. window_end_valid must not be nil.
16963 Value is
16965 1 if display has been updated
16966 0 if otherwise unsuccessful
16967 -1 if redisplay with same window start is known not to succeed
16969 The following steps are performed:
16971 1. Find the last row in the current matrix of W that is not
16972 affected by changes at the start of current_buffer. If no such row
16973 is found, give up.
16975 2. Find the first row in W's current matrix that is not affected by
16976 changes at the end of current_buffer. Maybe there is no such row.
16978 3. Display lines beginning with the row + 1 found in step 1 to the
16979 row found in step 2 or, if step 2 didn't find a row, to the end of
16980 the window.
16982 4. If cursor is not known to appear on the window, give up.
16984 5. If display stopped at the row found in step 2, scroll the
16985 display and current matrix as needed.
16987 6. Maybe display some lines at the end of W, if we must. This can
16988 happen under various circumstances, like a partially visible line
16989 becoming fully visible, or because newly displayed lines are displayed
16990 in smaller font sizes.
16992 7. Update W's window end information. */
16994 static int
16995 try_window_id (struct window *w)
16997 struct frame *f = XFRAME (w->frame);
16998 struct glyph_matrix *current_matrix = w->current_matrix;
16999 struct glyph_matrix *desired_matrix = w->desired_matrix;
17000 struct glyph_row *last_unchanged_at_beg_row;
17001 struct glyph_row *first_unchanged_at_end_row;
17002 struct glyph_row *row;
17003 struct glyph_row *bottom_row;
17004 int bottom_vpos;
17005 struct it it;
17006 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
17007 int dvpos, dy;
17008 struct text_pos start_pos;
17009 struct run run;
17010 int first_unchanged_at_end_vpos = 0;
17011 struct glyph_row *last_text_row, *last_text_row_at_end;
17012 struct text_pos start;
17013 EMACS_INT first_changed_charpos, last_changed_charpos;
17015 #if GLYPH_DEBUG
17016 if (inhibit_try_window_id)
17017 return 0;
17018 #endif
17020 /* This is handy for debugging. */
17021 #if 0
17022 #define GIVE_UP(X) \
17023 do { \
17024 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17025 return 0; \
17026 } while (0)
17027 #else
17028 #define GIVE_UP(X) return 0
17029 #endif
17031 SET_TEXT_POS_FROM_MARKER (start, w->start);
17033 /* Don't use this for mini-windows because these can show
17034 messages and mini-buffers, and we don't handle that here. */
17035 if (MINI_WINDOW_P (w))
17036 GIVE_UP (1);
17038 /* This flag is used to prevent redisplay optimizations. */
17039 if (windows_or_buffers_changed || cursor_type_changed)
17040 GIVE_UP (2);
17042 /* Verify that narrowing has not changed.
17043 Also verify that we were not told to prevent redisplay optimizations.
17044 It would be nice to further
17045 reduce the number of cases where this prevents try_window_id. */
17046 if (current_buffer->clip_changed
17047 || current_buffer->prevent_redisplay_optimizations_p)
17048 GIVE_UP (3);
17050 /* Window must either use window-based redisplay or be full width. */
17051 if (!FRAME_WINDOW_P (f)
17052 && (!FRAME_LINE_INS_DEL_OK (f)
17053 || !WINDOW_FULL_WIDTH_P (w)))
17054 GIVE_UP (4);
17056 /* Give up if point is known NOT to appear in W. */
17057 if (PT < CHARPOS (start))
17058 GIVE_UP (5);
17060 /* Another way to prevent redisplay optimizations. */
17061 if (XFASTINT (w->last_modified) == 0)
17062 GIVE_UP (6);
17064 /* Verify that window is not hscrolled. */
17065 if (XFASTINT (w->hscroll) != 0)
17066 GIVE_UP (7);
17068 /* Verify that display wasn't paused. */
17069 if (NILP (w->window_end_valid))
17070 GIVE_UP (8);
17072 /* Can't use this if highlighting a region because a cursor movement
17073 will do more than just set the cursor. */
17074 if (!NILP (Vtransient_mark_mode)
17075 && !NILP (BVAR (current_buffer, mark_active)))
17076 GIVE_UP (9);
17078 /* Likewise if highlighting trailing whitespace. */
17079 if (!NILP (Vshow_trailing_whitespace))
17080 GIVE_UP (11);
17082 /* Likewise if showing a region. */
17083 if (!NILP (w->region_showing))
17084 GIVE_UP (10);
17086 /* Can't use this if overlay arrow position and/or string have
17087 changed. */
17088 if (overlay_arrows_changed_p ())
17089 GIVE_UP (12);
17091 /* When word-wrap is on, adding a space to the first word of a
17092 wrapped line can change the wrap position, altering the line
17093 above it. It might be worthwhile to handle this more
17094 intelligently, but for now just redisplay from scratch. */
17095 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17096 GIVE_UP (21);
17098 /* Under bidi reordering, adding or deleting a character in the
17099 beginning of a paragraph, before the first strong directional
17100 character, can change the base direction of the paragraph (unless
17101 the buffer specifies a fixed paragraph direction), which will
17102 require to redisplay the whole paragraph. It might be worthwhile
17103 to find the paragraph limits and widen the range of redisplayed
17104 lines to that, but for now just give up this optimization and
17105 redisplay from scratch. */
17106 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17107 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17108 GIVE_UP (22);
17110 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17111 only if buffer has really changed. The reason is that the gap is
17112 initially at Z for freshly visited files. The code below would
17113 set end_unchanged to 0 in that case. */
17114 if (MODIFF > SAVE_MODIFF
17115 /* This seems to happen sometimes after saving a buffer. */
17116 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17118 if (GPT - BEG < BEG_UNCHANGED)
17119 BEG_UNCHANGED = GPT - BEG;
17120 if (Z - GPT < END_UNCHANGED)
17121 END_UNCHANGED = Z - GPT;
17124 /* The position of the first and last character that has been changed. */
17125 first_changed_charpos = BEG + BEG_UNCHANGED;
17126 last_changed_charpos = Z - END_UNCHANGED;
17128 /* If window starts after a line end, and the last change is in
17129 front of that newline, then changes don't affect the display.
17130 This case happens with stealth-fontification. Note that although
17131 the display is unchanged, glyph positions in the matrix have to
17132 be adjusted, of course. */
17133 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17134 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17135 && ((last_changed_charpos < CHARPOS (start)
17136 && CHARPOS (start) == BEGV)
17137 || (last_changed_charpos < CHARPOS (start) - 1
17138 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17140 EMACS_INT Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17141 struct glyph_row *r0;
17143 /* Compute how many chars/bytes have been added to or removed
17144 from the buffer. */
17145 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17146 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17147 Z_delta = Z - Z_old;
17148 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17150 /* Give up if PT is not in the window. Note that it already has
17151 been checked at the start of try_window_id that PT is not in
17152 front of the window start. */
17153 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17154 GIVE_UP (13);
17156 /* If window start is unchanged, we can reuse the whole matrix
17157 as is, after adjusting glyph positions. No need to compute
17158 the window end again, since its offset from Z hasn't changed. */
17159 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17160 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17161 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17162 /* PT must not be in a partially visible line. */
17163 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17164 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17166 /* Adjust positions in the glyph matrix. */
17167 if (Z_delta || Z_delta_bytes)
17169 struct glyph_row *r1
17170 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17171 increment_matrix_positions (w->current_matrix,
17172 MATRIX_ROW_VPOS (r0, current_matrix),
17173 MATRIX_ROW_VPOS (r1, current_matrix),
17174 Z_delta, Z_delta_bytes);
17177 /* Set the cursor. */
17178 row = row_containing_pos (w, PT, r0, NULL, 0);
17179 if (row)
17180 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17181 else
17182 abort ();
17183 return 1;
17187 /* Handle the case that changes are all below what is displayed in
17188 the window, and that PT is in the window. This shortcut cannot
17189 be taken if ZV is visible in the window, and text has been added
17190 there that is visible in the window. */
17191 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17192 /* ZV is not visible in the window, or there are no
17193 changes at ZV, actually. */
17194 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17195 || first_changed_charpos == last_changed_charpos))
17197 struct glyph_row *r0;
17199 /* Give up if PT is not in the window. Note that it already has
17200 been checked at the start of try_window_id that PT is not in
17201 front of the window start. */
17202 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17203 GIVE_UP (14);
17205 /* If window start is unchanged, we can reuse the whole matrix
17206 as is, without changing glyph positions since no text has
17207 been added/removed in front of the window end. */
17208 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17209 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17210 /* PT must not be in a partially visible line. */
17211 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17212 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17214 /* We have to compute the window end anew since text
17215 could have been added/removed after it. */
17216 w->window_end_pos
17217 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17218 w->window_end_bytepos
17219 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17221 /* Set the cursor. */
17222 row = row_containing_pos (w, PT, r0, NULL, 0);
17223 if (row)
17224 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17225 else
17226 abort ();
17227 return 2;
17231 /* Give up if window start is in the changed area.
17233 The condition used to read
17235 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17237 but why that was tested escapes me at the moment. */
17238 if (CHARPOS (start) >= first_changed_charpos
17239 && CHARPOS (start) <= last_changed_charpos)
17240 GIVE_UP (15);
17242 /* Check that window start agrees with the start of the first glyph
17243 row in its current matrix. Check this after we know the window
17244 start is not in changed text, otherwise positions would not be
17245 comparable. */
17246 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17247 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17248 GIVE_UP (16);
17250 /* Give up if the window ends in strings. Overlay strings
17251 at the end are difficult to handle, so don't try. */
17252 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17253 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17254 GIVE_UP (20);
17256 /* Compute the position at which we have to start displaying new
17257 lines. Some of the lines at the top of the window might be
17258 reusable because they are not displaying changed text. Find the
17259 last row in W's current matrix not affected by changes at the
17260 start of current_buffer. Value is null if changes start in the
17261 first line of window. */
17262 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17263 if (last_unchanged_at_beg_row)
17265 /* Avoid starting to display in the middle of a character, a TAB
17266 for instance. This is easier than to set up the iterator
17267 exactly, and it's not a frequent case, so the additional
17268 effort wouldn't really pay off. */
17269 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17270 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17271 && last_unchanged_at_beg_row > w->current_matrix->rows)
17272 --last_unchanged_at_beg_row;
17274 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17275 GIVE_UP (17);
17277 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17278 GIVE_UP (18);
17279 start_pos = it.current.pos;
17281 /* Start displaying new lines in the desired matrix at the same
17282 vpos we would use in the current matrix, i.e. below
17283 last_unchanged_at_beg_row. */
17284 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17285 current_matrix);
17286 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17287 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17289 xassert (it.hpos == 0 && it.current_x == 0);
17291 else
17293 /* There are no reusable lines at the start of the window.
17294 Start displaying in the first text line. */
17295 start_display (&it, w, start);
17296 it.vpos = it.first_vpos;
17297 start_pos = it.current.pos;
17300 /* Find the first row that is not affected by changes at the end of
17301 the buffer. Value will be null if there is no unchanged row, in
17302 which case we must redisplay to the end of the window. delta
17303 will be set to the value by which buffer positions beginning with
17304 first_unchanged_at_end_row have to be adjusted due to text
17305 changes. */
17306 first_unchanged_at_end_row
17307 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17308 IF_DEBUG (debug_delta = delta);
17309 IF_DEBUG (debug_delta_bytes = delta_bytes);
17311 /* Set stop_pos to the buffer position up to which we will have to
17312 display new lines. If first_unchanged_at_end_row != NULL, this
17313 is the buffer position of the start of the line displayed in that
17314 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17315 that we don't stop at a buffer position. */
17316 stop_pos = 0;
17317 if (first_unchanged_at_end_row)
17319 xassert (last_unchanged_at_beg_row == NULL
17320 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17322 /* If this is a continuation line, move forward to the next one
17323 that isn't. Changes in lines above affect this line.
17324 Caution: this may move first_unchanged_at_end_row to a row
17325 not displaying text. */
17326 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17327 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17328 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17329 < it.last_visible_y))
17330 ++first_unchanged_at_end_row;
17332 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17333 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17334 >= it.last_visible_y))
17335 first_unchanged_at_end_row = NULL;
17336 else
17338 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17339 + delta);
17340 first_unchanged_at_end_vpos
17341 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17342 xassert (stop_pos >= Z - END_UNCHANGED);
17345 else if (last_unchanged_at_beg_row == NULL)
17346 GIVE_UP (19);
17349 #if GLYPH_DEBUG
17351 /* Either there is no unchanged row at the end, or the one we have
17352 now displays text. This is a necessary condition for the window
17353 end pos calculation at the end of this function. */
17354 xassert (first_unchanged_at_end_row == NULL
17355 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17357 debug_last_unchanged_at_beg_vpos
17358 = (last_unchanged_at_beg_row
17359 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17360 : -1);
17361 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17363 #endif /* GLYPH_DEBUG != 0 */
17366 /* Display new lines. Set last_text_row to the last new line
17367 displayed which has text on it, i.e. might end up as being the
17368 line where the window_end_vpos is. */
17369 w->cursor.vpos = -1;
17370 last_text_row = NULL;
17371 overlay_arrow_seen = 0;
17372 while (it.current_y < it.last_visible_y
17373 && !fonts_changed_p
17374 && (first_unchanged_at_end_row == NULL
17375 || IT_CHARPOS (it) < stop_pos))
17377 if (display_line (&it))
17378 last_text_row = it.glyph_row - 1;
17381 if (fonts_changed_p)
17382 return -1;
17385 /* Compute differences in buffer positions, y-positions etc. for
17386 lines reused at the bottom of the window. Compute what we can
17387 scroll. */
17388 if (first_unchanged_at_end_row
17389 /* No lines reused because we displayed everything up to the
17390 bottom of the window. */
17391 && it.current_y < it.last_visible_y)
17393 dvpos = (it.vpos
17394 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17395 current_matrix));
17396 dy = it.current_y - first_unchanged_at_end_row->y;
17397 run.current_y = first_unchanged_at_end_row->y;
17398 run.desired_y = run.current_y + dy;
17399 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17401 else
17403 delta = delta_bytes = dvpos = dy
17404 = run.current_y = run.desired_y = run.height = 0;
17405 first_unchanged_at_end_row = NULL;
17407 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17410 /* Find the cursor if not already found. We have to decide whether
17411 PT will appear on this window (it sometimes doesn't, but this is
17412 not a very frequent case.) This decision has to be made before
17413 the current matrix is altered. A value of cursor.vpos < 0 means
17414 that PT is either in one of the lines beginning at
17415 first_unchanged_at_end_row or below the window. Don't care for
17416 lines that might be displayed later at the window end; as
17417 mentioned, this is not a frequent case. */
17418 if (w->cursor.vpos < 0)
17420 /* Cursor in unchanged rows at the top? */
17421 if (PT < CHARPOS (start_pos)
17422 && last_unchanged_at_beg_row)
17424 row = row_containing_pos (w, PT,
17425 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17426 last_unchanged_at_beg_row + 1, 0);
17427 if (row)
17428 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17431 /* Start from first_unchanged_at_end_row looking for PT. */
17432 else if (first_unchanged_at_end_row)
17434 row = row_containing_pos (w, PT - delta,
17435 first_unchanged_at_end_row, NULL, 0);
17436 if (row)
17437 set_cursor_from_row (w, row, w->current_matrix, delta,
17438 delta_bytes, dy, dvpos);
17441 /* Give up if cursor was not found. */
17442 if (w->cursor.vpos < 0)
17444 clear_glyph_matrix (w->desired_matrix);
17445 return -1;
17449 /* Don't let the cursor end in the scroll margins. */
17451 int this_scroll_margin, cursor_height;
17453 this_scroll_margin =
17454 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17455 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17456 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17458 if ((w->cursor.y < this_scroll_margin
17459 && CHARPOS (start) > BEGV)
17460 /* Old redisplay didn't take scroll margin into account at the bottom,
17461 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17462 || (w->cursor.y + (make_cursor_line_fully_visible_p
17463 ? cursor_height + this_scroll_margin
17464 : 1)) > it.last_visible_y)
17466 w->cursor.vpos = -1;
17467 clear_glyph_matrix (w->desired_matrix);
17468 return -1;
17472 /* Scroll the display. Do it before changing the current matrix so
17473 that xterm.c doesn't get confused about where the cursor glyph is
17474 found. */
17475 if (dy && run.height)
17477 update_begin (f);
17479 if (FRAME_WINDOW_P (f))
17481 FRAME_RIF (f)->update_window_begin_hook (w);
17482 FRAME_RIF (f)->clear_window_mouse_face (w);
17483 FRAME_RIF (f)->scroll_run_hook (w, &run);
17484 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17486 else
17488 /* Terminal frame. In this case, dvpos gives the number of
17489 lines to scroll by; dvpos < 0 means scroll up. */
17490 int from_vpos
17491 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17492 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17493 int end = (WINDOW_TOP_EDGE_LINE (w)
17494 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17495 + window_internal_height (w));
17497 #if defined (HAVE_GPM) || defined (MSDOS)
17498 x_clear_window_mouse_face (w);
17499 #endif
17500 /* Perform the operation on the screen. */
17501 if (dvpos > 0)
17503 /* Scroll last_unchanged_at_beg_row to the end of the
17504 window down dvpos lines. */
17505 set_terminal_window (f, end);
17507 /* On dumb terminals delete dvpos lines at the end
17508 before inserting dvpos empty lines. */
17509 if (!FRAME_SCROLL_REGION_OK (f))
17510 ins_del_lines (f, end - dvpos, -dvpos);
17512 /* Insert dvpos empty lines in front of
17513 last_unchanged_at_beg_row. */
17514 ins_del_lines (f, from, dvpos);
17516 else if (dvpos < 0)
17518 /* Scroll up last_unchanged_at_beg_vpos to the end of
17519 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17520 set_terminal_window (f, end);
17522 /* Delete dvpos lines in front of
17523 last_unchanged_at_beg_vpos. ins_del_lines will set
17524 the cursor to the given vpos and emit |dvpos| delete
17525 line sequences. */
17526 ins_del_lines (f, from + dvpos, dvpos);
17528 /* On a dumb terminal insert dvpos empty lines at the
17529 end. */
17530 if (!FRAME_SCROLL_REGION_OK (f))
17531 ins_del_lines (f, end + dvpos, -dvpos);
17534 set_terminal_window (f, 0);
17537 update_end (f);
17540 /* Shift reused rows of the current matrix to the right position.
17541 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17542 text. */
17543 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17544 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17545 if (dvpos < 0)
17547 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17548 bottom_vpos, dvpos);
17549 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17550 bottom_vpos, 0);
17552 else if (dvpos > 0)
17554 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17555 bottom_vpos, dvpos);
17556 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17557 first_unchanged_at_end_vpos + dvpos, 0);
17560 /* For frame-based redisplay, make sure that current frame and window
17561 matrix are in sync with respect to glyph memory. */
17562 if (!FRAME_WINDOW_P (f))
17563 sync_frame_with_window_matrix_rows (w);
17565 /* Adjust buffer positions in reused rows. */
17566 if (delta || delta_bytes)
17567 increment_matrix_positions (current_matrix,
17568 first_unchanged_at_end_vpos + dvpos,
17569 bottom_vpos, delta, delta_bytes);
17571 /* Adjust Y positions. */
17572 if (dy)
17573 shift_glyph_matrix (w, current_matrix,
17574 first_unchanged_at_end_vpos + dvpos,
17575 bottom_vpos, dy);
17577 if (first_unchanged_at_end_row)
17579 first_unchanged_at_end_row += dvpos;
17580 if (first_unchanged_at_end_row->y >= it.last_visible_y
17581 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17582 first_unchanged_at_end_row = NULL;
17585 /* If scrolling up, there may be some lines to display at the end of
17586 the window. */
17587 last_text_row_at_end = NULL;
17588 if (dy < 0)
17590 /* Scrolling up can leave for example a partially visible line
17591 at the end of the window to be redisplayed. */
17592 /* Set last_row to the glyph row in the current matrix where the
17593 window end line is found. It has been moved up or down in
17594 the matrix by dvpos. */
17595 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17596 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17598 /* If last_row is the window end line, it should display text. */
17599 xassert (last_row->displays_text_p);
17601 /* If window end line was partially visible before, begin
17602 displaying at that line. Otherwise begin displaying with the
17603 line following it. */
17604 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17606 init_to_row_start (&it, w, last_row);
17607 it.vpos = last_vpos;
17608 it.current_y = last_row->y;
17610 else
17612 init_to_row_end (&it, w, last_row);
17613 it.vpos = 1 + last_vpos;
17614 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17615 ++last_row;
17618 /* We may start in a continuation line. If so, we have to
17619 get the right continuation_lines_width and current_x. */
17620 it.continuation_lines_width = last_row->continuation_lines_width;
17621 it.hpos = it.current_x = 0;
17623 /* Display the rest of the lines at the window end. */
17624 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17625 while (it.current_y < it.last_visible_y
17626 && !fonts_changed_p)
17628 /* Is it always sure that the display agrees with lines in
17629 the current matrix? I don't think so, so we mark rows
17630 displayed invalid in the current matrix by setting their
17631 enabled_p flag to zero. */
17632 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17633 if (display_line (&it))
17634 last_text_row_at_end = it.glyph_row - 1;
17638 /* Update window_end_pos and window_end_vpos. */
17639 if (first_unchanged_at_end_row
17640 && !last_text_row_at_end)
17642 /* Window end line if one of the preserved rows from the current
17643 matrix. Set row to the last row displaying text in current
17644 matrix starting at first_unchanged_at_end_row, after
17645 scrolling. */
17646 xassert (first_unchanged_at_end_row->displays_text_p);
17647 row = find_last_row_displaying_text (w->current_matrix, &it,
17648 first_unchanged_at_end_row);
17649 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17651 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17652 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17653 w->window_end_vpos
17654 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17655 xassert (w->window_end_bytepos >= 0);
17656 IF_DEBUG (debug_method_add (w, "A"));
17658 else if (last_text_row_at_end)
17660 w->window_end_pos
17661 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17662 w->window_end_bytepos
17663 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17664 w->window_end_vpos
17665 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17666 xassert (w->window_end_bytepos >= 0);
17667 IF_DEBUG (debug_method_add (w, "B"));
17669 else if (last_text_row)
17671 /* We have displayed either to the end of the window or at the
17672 end of the window, i.e. the last row with text is to be found
17673 in the desired matrix. */
17674 w->window_end_pos
17675 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17676 w->window_end_bytepos
17677 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17678 w->window_end_vpos
17679 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17680 xassert (w->window_end_bytepos >= 0);
17682 else if (first_unchanged_at_end_row == NULL
17683 && last_text_row == NULL
17684 && last_text_row_at_end == NULL)
17686 /* Displayed to end of window, but no line containing text was
17687 displayed. Lines were deleted at the end of the window. */
17688 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17689 int vpos = XFASTINT (w->window_end_vpos);
17690 struct glyph_row *current_row = current_matrix->rows + vpos;
17691 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17693 for (row = NULL;
17694 row == NULL && vpos >= first_vpos;
17695 --vpos, --current_row, --desired_row)
17697 if (desired_row->enabled_p)
17699 if (desired_row->displays_text_p)
17700 row = desired_row;
17702 else if (current_row->displays_text_p)
17703 row = current_row;
17706 xassert (row != NULL);
17707 w->window_end_vpos = make_number (vpos + 1);
17708 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17709 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17710 xassert (w->window_end_bytepos >= 0);
17711 IF_DEBUG (debug_method_add (w, "C"));
17713 else
17714 abort ();
17716 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17717 debug_end_vpos = XFASTINT (w->window_end_vpos));
17719 /* Record that display has not been completed. */
17720 w->window_end_valid = Qnil;
17721 w->desired_matrix->no_scrolling_p = 1;
17722 return 3;
17724 #undef GIVE_UP
17729 /***********************************************************************
17730 More debugging support
17731 ***********************************************************************/
17733 #if GLYPH_DEBUG
17735 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17736 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17737 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17740 /* Dump the contents of glyph matrix MATRIX on stderr.
17742 GLYPHS 0 means don't show glyph contents.
17743 GLYPHS 1 means show glyphs in short form
17744 GLYPHS > 1 means show glyphs in long form. */
17746 void
17747 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17749 int i;
17750 for (i = 0; i < matrix->nrows; ++i)
17751 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17755 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17756 the glyph row and area where the glyph comes from. */
17758 void
17759 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17761 if (glyph->type == CHAR_GLYPH)
17763 fprintf (stderr,
17764 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17765 glyph - row->glyphs[TEXT_AREA],
17766 'C',
17767 glyph->charpos,
17768 (BUFFERP (glyph->object)
17769 ? 'B'
17770 : (STRINGP (glyph->object)
17771 ? 'S'
17772 : '-')),
17773 glyph->pixel_width,
17774 glyph->u.ch,
17775 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17776 ? glyph->u.ch
17777 : '.'),
17778 glyph->face_id,
17779 glyph->left_box_line_p,
17780 glyph->right_box_line_p);
17782 else if (glyph->type == STRETCH_GLYPH)
17784 fprintf (stderr,
17785 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17786 glyph - row->glyphs[TEXT_AREA],
17787 'S',
17788 glyph->charpos,
17789 (BUFFERP (glyph->object)
17790 ? 'B'
17791 : (STRINGP (glyph->object)
17792 ? 'S'
17793 : '-')),
17794 glyph->pixel_width,
17796 '.',
17797 glyph->face_id,
17798 glyph->left_box_line_p,
17799 glyph->right_box_line_p);
17801 else if (glyph->type == IMAGE_GLYPH)
17803 fprintf (stderr,
17804 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17805 glyph - row->glyphs[TEXT_AREA],
17806 'I',
17807 glyph->charpos,
17808 (BUFFERP (glyph->object)
17809 ? 'B'
17810 : (STRINGP (glyph->object)
17811 ? 'S'
17812 : '-')),
17813 glyph->pixel_width,
17814 glyph->u.img_id,
17815 '.',
17816 glyph->face_id,
17817 glyph->left_box_line_p,
17818 glyph->right_box_line_p);
17820 else if (glyph->type == COMPOSITE_GLYPH)
17822 fprintf (stderr,
17823 " %5td %4c %6"pI"d %c %3d 0x%05x",
17824 glyph - row->glyphs[TEXT_AREA],
17825 '+',
17826 glyph->charpos,
17827 (BUFFERP (glyph->object)
17828 ? 'B'
17829 : (STRINGP (glyph->object)
17830 ? 'S'
17831 : '-')),
17832 glyph->pixel_width,
17833 glyph->u.cmp.id);
17834 if (glyph->u.cmp.automatic)
17835 fprintf (stderr,
17836 "[%d-%d]",
17837 glyph->slice.cmp.from, glyph->slice.cmp.to);
17838 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17839 glyph->face_id,
17840 glyph->left_box_line_p,
17841 glyph->right_box_line_p);
17846 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17847 GLYPHS 0 means don't show glyph contents.
17848 GLYPHS 1 means show glyphs in short form
17849 GLYPHS > 1 means show glyphs in long form. */
17851 void
17852 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17854 if (glyphs != 1)
17856 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17857 fprintf (stderr, "======================================================================\n");
17859 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17860 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17861 vpos,
17862 MATRIX_ROW_START_CHARPOS (row),
17863 MATRIX_ROW_END_CHARPOS (row),
17864 row->used[TEXT_AREA],
17865 row->contains_overlapping_glyphs_p,
17866 row->enabled_p,
17867 row->truncated_on_left_p,
17868 row->truncated_on_right_p,
17869 row->continued_p,
17870 MATRIX_ROW_CONTINUATION_LINE_P (row),
17871 row->displays_text_p,
17872 row->ends_at_zv_p,
17873 row->fill_line_p,
17874 row->ends_in_middle_of_char_p,
17875 row->starts_in_middle_of_char_p,
17876 row->mouse_face_p,
17877 row->x,
17878 row->y,
17879 row->pixel_width,
17880 row->height,
17881 row->visible_height,
17882 row->ascent,
17883 row->phys_ascent);
17884 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
17885 row->end.overlay_string_index,
17886 row->continuation_lines_width);
17887 fprintf (stderr, "%9"pI"d %5"pI"d\n",
17888 CHARPOS (row->start.string_pos),
17889 CHARPOS (row->end.string_pos));
17890 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17891 row->end.dpvec_index);
17894 if (glyphs > 1)
17896 int area;
17898 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17900 struct glyph *glyph = row->glyphs[area];
17901 struct glyph *glyph_end = glyph + row->used[area];
17903 /* Glyph for a line end in text. */
17904 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17905 ++glyph_end;
17907 if (glyph < glyph_end)
17908 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
17910 for (; glyph < glyph_end; ++glyph)
17911 dump_glyph (row, glyph, area);
17914 else if (glyphs == 1)
17916 int area;
17918 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17920 char *s = (char *) alloca (row->used[area] + 1);
17921 int i;
17923 for (i = 0; i < row->used[area]; ++i)
17925 struct glyph *glyph = row->glyphs[area] + i;
17926 if (glyph->type == CHAR_GLYPH
17927 && glyph->u.ch < 0x80
17928 && glyph->u.ch >= ' ')
17929 s[i] = glyph->u.ch;
17930 else
17931 s[i] = '.';
17934 s[i] = '\0';
17935 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17941 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17942 Sdump_glyph_matrix, 0, 1, "p",
17943 doc: /* Dump the current matrix of the selected window to stderr.
17944 Shows contents of glyph row structures. With non-nil
17945 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17946 glyphs in short form, otherwise show glyphs in long form. */)
17947 (Lisp_Object glyphs)
17949 struct window *w = XWINDOW (selected_window);
17950 struct buffer *buffer = XBUFFER (w->buffer);
17952 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17953 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17954 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17955 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17956 fprintf (stderr, "=============================================\n");
17957 dump_glyph_matrix (w->current_matrix,
17958 NILP (glyphs) ? 0 : XINT (glyphs));
17959 return Qnil;
17963 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17964 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17965 (void)
17967 struct frame *f = XFRAME (selected_frame);
17968 dump_glyph_matrix (f->current_matrix, 1);
17969 return Qnil;
17973 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17974 doc: /* Dump glyph row ROW to stderr.
17975 GLYPH 0 means don't dump glyphs.
17976 GLYPH 1 means dump glyphs in short form.
17977 GLYPH > 1 or omitted means dump glyphs in long form. */)
17978 (Lisp_Object row, Lisp_Object glyphs)
17980 struct glyph_matrix *matrix;
17981 int vpos;
17983 CHECK_NUMBER (row);
17984 matrix = XWINDOW (selected_window)->current_matrix;
17985 vpos = XINT (row);
17986 if (vpos >= 0 && vpos < matrix->nrows)
17987 dump_glyph_row (MATRIX_ROW (matrix, vpos),
17988 vpos,
17989 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17990 return Qnil;
17994 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
17995 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
17996 GLYPH 0 means don't dump glyphs.
17997 GLYPH 1 means dump glyphs in short form.
17998 GLYPH > 1 or omitted means dump glyphs in long form. */)
17999 (Lisp_Object row, Lisp_Object glyphs)
18001 struct frame *sf = SELECTED_FRAME ();
18002 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18003 int vpos;
18005 CHECK_NUMBER (row);
18006 vpos = XINT (row);
18007 if (vpos >= 0 && vpos < m->nrows)
18008 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18009 INTEGERP (glyphs) ? XINT (glyphs) : 2);
18010 return Qnil;
18014 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18015 doc: /* Toggle tracing of redisplay.
18016 With ARG, turn tracing on if and only if ARG is positive. */)
18017 (Lisp_Object arg)
18019 if (NILP (arg))
18020 trace_redisplay_p = !trace_redisplay_p;
18021 else
18023 arg = Fprefix_numeric_value (arg);
18024 trace_redisplay_p = XINT (arg) > 0;
18027 return Qnil;
18031 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18032 doc: /* Like `format', but print result to stderr.
18033 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18034 (ptrdiff_t nargs, Lisp_Object *args)
18036 Lisp_Object s = Fformat (nargs, args);
18037 fprintf (stderr, "%s", SDATA (s));
18038 return Qnil;
18041 #endif /* GLYPH_DEBUG */
18045 /***********************************************************************
18046 Building Desired Matrix Rows
18047 ***********************************************************************/
18049 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18050 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18052 static struct glyph_row *
18053 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18055 struct frame *f = XFRAME (WINDOW_FRAME (w));
18056 struct buffer *buffer = XBUFFER (w->buffer);
18057 struct buffer *old = current_buffer;
18058 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18059 int arrow_len = SCHARS (overlay_arrow_string);
18060 const unsigned char *arrow_end = arrow_string + arrow_len;
18061 const unsigned char *p;
18062 struct it it;
18063 int multibyte_p;
18064 int n_glyphs_before;
18066 set_buffer_temp (buffer);
18067 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18068 it.glyph_row->used[TEXT_AREA] = 0;
18069 SET_TEXT_POS (it.position, 0, 0);
18071 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18072 p = arrow_string;
18073 while (p < arrow_end)
18075 Lisp_Object face, ilisp;
18077 /* Get the next character. */
18078 if (multibyte_p)
18079 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18080 else
18082 it.c = it.char_to_display = *p, it.len = 1;
18083 if (! ASCII_CHAR_P (it.c))
18084 it.char_to_display = BYTE8_TO_CHAR (it.c);
18086 p += it.len;
18088 /* Get its face. */
18089 ilisp = make_number (p - arrow_string);
18090 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18091 it.face_id = compute_char_face (f, it.char_to_display, face);
18093 /* Compute its width, get its glyphs. */
18094 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18095 SET_TEXT_POS (it.position, -1, -1);
18096 PRODUCE_GLYPHS (&it);
18098 /* If this character doesn't fit any more in the line, we have
18099 to remove some glyphs. */
18100 if (it.current_x > it.last_visible_x)
18102 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18103 break;
18107 set_buffer_temp (old);
18108 return it.glyph_row;
18112 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
18113 glyphs are only inserted for terminal frames since we can't really
18114 win with truncation glyphs when partially visible glyphs are
18115 involved. Which glyphs to insert is determined by
18116 produce_special_glyphs. */
18118 static void
18119 insert_left_trunc_glyphs (struct it *it)
18121 struct it truncate_it;
18122 struct glyph *from, *end, *to, *toend;
18124 xassert (!FRAME_WINDOW_P (it->f));
18126 /* Get the truncation glyphs. */
18127 truncate_it = *it;
18128 truncate_it.current_x = 0;
18129 truncate_it.face_id = DEFAULT_FACE_ID;
18130 truncate_it.glyph_row = &scratch_glyph_row;
18131 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18132 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18133 truncate_it.object = make_number (0);
18134 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18136 /* Overwrite glyphs from IT with truncation glyphs. */
18137 if (!it->glyph_row->reversed_p)
18139 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18140 end = from + truncate_it.glyph_row->used[TEXT_AREA];
18141 to = it->glyph_row->glyphs[TEXT_AREA];
18142 toend = to + it->glyph_row->used[TEXT_AREA];
18144 while (from < end)
18145 *to++ = *from++;
18147 /* There may be padding glyphs left over. Overwrite them too. */
18148 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18150 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18151 while (from < end)
18152 *to++ = *from++;
18155 if (to > toend)
18156 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18158 else
18160 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18161 that back to front. */
18162 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18163 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18164 toend = it->glyph_row->glyphs[TEXT_AREA];
18165 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18167 while (from >= end && to >= toend)
18168 *to-- = *from--;
18169 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18171 from =
18172 truncate_it.glyph_row->glyphs[TEXT_AREA]
18173 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18174 while (from >= end && to >= toend)
18175 *to-- = *from--;
18177 if (from >= end)
18179 /* Need to free some room before prepending additional
18180 glyphs. */
18181 int move_by = from - end + 1;
18182 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18183 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18185 for ( ; g >= g0; g--)
18186 g[move_by] = *g;
18187 while (from >= end)
18188 *to-- = *from--;
18189 it->glyph_row->used[TEXT_AREA] += move_by;
18194 /* Compute the hash code for ROW. */
18195 unsigned
18196 row_hash (struct glyph_row *row)
18198 int area, k;
18199 unsigned hashval = 0;
18201 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18202 for (k = 0; k < row->used[area]; ++k)
18203 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18204 + row->glyphs[area][k].u.val
18205 + row->glyphs[area][k].face_id
18206 + row->glyphs[area][k].padding_p
18207 + (row->glyphs[area][k].type << 2));
18209 return hashval;
18212 /* Compute the pixel height and width of IT->glyph_row.
18214 Most of the time, ascent and height of a display line will be equal
18215 to the max_ascent and max_height values of the display iterator
18216 structure. This is not the case if
18218 1. We hit ZV without displaying anything. In this case, max_ascent
18219 and max_height will be zero.
18221 2. We have some glyphs that don't contribute to the line height.
18222 (The glyph row flag contributes_to_line_height_p is for future
18223 pixmap extensions).
18225 The first case is easily covered by using default values because in
18226 these cases, the line height does not really matter, except that it
18227 must not be zero. */
18229 static void
18230 compute_line_metrics (struct it *it)
18232 struct glyph_row *row = it->glyph_row;
18234 if (FRAME_WINDOW_P (it->f))
18236 int i, min_y, max_y;
18238 /* The line may consist of one space only, that was added to
18239 place the cursor on it. If so, the row's height hasn't been
18240 computed yet. */
18241 if (row->height == 0)
18243 if (it->max_ascent + it->max_descent == 0)
18244 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18245 row->ascent = it->max_ascent;
18246 row->height = it->max_ascent + it->max_descent;
18247 row->phys_ascent = it->max_phys_ascent;
18248 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18249 row->extra_line_spacing = it->max_extra_line_spacing;
18252 /* Compute the width of this line. */
18253 row->pixel_width = row->x;
18254 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18255 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18257 xassert (row->pixel_width >= 0);
18258 xassert (row->ascent >= 0 && row->height > 0);
18260 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18261 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18263 /* If first line's physical ascent is larger than its logical
18264 ascent, use the physical ascent, and make the row taller.
18265 This makes accented characters fully visible. */
18266 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18267 && row->phys_ascent > row->ascent)
18269 row->height += row->phys_ascent - row->ascent;
18270 row->ascent = row->phys_ascent;
18273 /* Compute how much of the line is visible. */
18274 row->visible_height = row->height;
18276 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18277 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18279 if (row->y < min_y)
18280 row->visible_height -= min_y - row->y;
18281 if (row->y + row->height > max_y)
18282 row->visible_height -= row->y + row->height - max_y;
18284 else
18286 row->pixel_width = row->used[TEXT_AREA];
18287 if (row->continued_p)
18288 row->pixel_width -= it->continuation_pixel_width;
18289 else if (row->truncated_on_right_p)
18290 row->pixel_width -= it->truncation_pixel_width;
18291 row->ascent = row->phys_ascent = 0;
18292 row->height = row->phys_height = row->visible_height = 1;
18293 row->extra_line_spacing = 0;
18296 /* Compute a hash code for this row. */
18297 row->hash = row_hash (row);
18299 it->max_ascent = it->max_descent = 0;
18300 it->max_phys_ascent = it->max_phys_descent = 0;
18304 /* Append one space to the glyph row of iterator IT if doing a
18305 window-based redisplay. The space has the same face as
18306 IT->face_id. Value is non-zero if a space was added.
18308 This function is called to make sure that there is always one glyph
18309 at the end of a glyph row that the cursor can be set on under
18310 window-systems. (If there weren't such a glyph we would not know
18311 how wide and tall a box cursor should be displayed).
18313 At the same time this space let's a nicely handle clearing to the
18314 end of the line if the row ends in italic text. */
18316 static int
18317 append_space_for_newline (struct it *it, int default_face_p)
18319 if (FRAME_WINDOW_P (it->f))
18321 int n = it->glyph_row->used[TEXT_AREA];
18323 if (it->glyph_row->glyphs[TEXT_AREA] + n
18324 < it->glyph_row->glyphs[1 + TEXT_AREA])
18326 /* Save some values that must not be changed.
18327 Must save IT->c and IT->len because otherwise
18328 ITERATOR_AT_END_P wouldn't work anymore after
18329 append_space_for_newline has been called. */
18330 enum display_element_type saved_what = it->what;
18331 int saved_c = it->c, saved_len = it->len;
18332 int saved_char_to_display = it->char_to_display;
18333 int saved_x = it->current_x;
18334 int saved_face_id = it->face_id;
18335 struct text_pos saved_pos;
18336 Lisp_Object saved_object;
18337 struct face *face;
18339 saved_object = it->object;
18340 saved_pos = it->position;
18342 it->what = IT_CHARACTER;
18343 memset (&it->position, 0, sizeof it->position);
18344 it->object = make_number (0);
18345 it->c = it->char_to_display = ' ';
18346 it->len = 1;
18348 /* If the default face was remapped, be sure to use the
18349 remapped face for the appended newline. */
18350 if (default_face_p)
18351 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18352 else if (it->face_before_selective_p)
18353 it->face_id = it->saved_face_id;
18354 face = FACE_FROM_ID (it->f, it->face_id);
18355 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18357 PRODUCE_GLYPHS (it);
18359 it->override_ascent = -1;
18360 it->constrain_row_ascent_descent_p = 0;
18361 it->current_x = saved_x;
18362 it->object = saved_object;
18363 it->position = saved_pos;
18364 it->what = saved_what;
18365 it->face_id = saved_face_id;
18366 it->len = saved_len;
18367 it->c = saved_c;
18368 it->char_to_display = saved_char_to_display;
18369 return 1;
18373 return 0;
18377 /* Extend the face of the last glyph in the text area of IT->glyph_row
18378 to the end of the display line. Called from display_line. If the
18379 glyph row is empty, add a space glyph to it so that we know the
18380 face to draw. Set the glyph row flag fill_line_p. If the glyph
18381 row is R2L, prepend a stretch glyph to cover the empty space to the
18382 left of the leftmost glyph. */
18384 static void
18385 extend_face_to_end_of_line (struct it *it)
18387 struct face *face, *default_face;
18388 struct frame *f = it->f;
18390 /* If line is already filled, do nothing. Non window-system frames
18391 get a grace of one more ``pixel'' because their characters are
18392 1-``pixel'' wide, so they hit the equality too early. This grace
18393 is needed only for R2L rows that are not continued, to produce
18394 one extra blank where we could display the cursor. */
18395 if (it->current_x >= it->last_visible_x
18396 + (!FRAME_WINDOW_P (f)
18397 && it->glyph_row->reversed_p
18398 && !it->glyph_row->continued_p))
18399 return;
18401 /* The default face, possibly remapped. */
18402 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18404 /* Face extension extends the background and box of IT->face_id
18405 to the end of the line. If the background equals the background
18406 of the frame, we don't have to do anything. */
18407 if (it->face_before_selective_p)
18408 face = FACE_FROM_ID (f, it->saved_face_id);
18409 else
18410 face = FACE_FROM_ID (f, it->face_id);
18412 if (FRAME_WINDOW_P (f)
18413 && it->glyph_row->displays_text_p
18414 && face->box == FACE_NO_BOX
18415 && face->background == FRAME_BACKGROUND_PIXEL (f)
18416 && !face->stipple
18417 && !it->glyph_row->reversed_p)
18418 return;
18420 /* Set the glyph row flag indicating that the face of the last glyph
18421 in the text area has to be drawn to the end of the text area. */
18422 it->glyph_row->fill_line_p = 1;
18424 /* If current character of IT is not ASCII, make sure we have the
18425 ASCII face. This will be automatically undone the next time
18426 get_next_display_element returns a multibyte character. Note
18427 that the character will always be single byte in unibyte
18428 text. */
18429 if (!ASCII_CHAR_P (it->c))
18431 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18434 if (FRAME_WINDOW_P (f))
18436 /* If the row is empty, add a space with the current face of IT,
18437 so that we know which face to draw. */
18438 if (it->glyph_row->used[TEXT_AREA] == 0)
18440 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18441 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18442 it->glyph_row->used[TEXT_AREA] = 1;
18444 #ifdef HAVE_WINDOW_SYSTEM
18445 if (it->glyph_row->reversed_p)
18447 /* Prepend a stretch glyph to the row, such that the
18448 rightmost glyph will be drawn flushed all the way to the
18449 right margin of the window. The stretch glyph that will
18450 occupy the empty space, if any, to the left of the
18451 glyphs. */
18452 struct font *font = face->font ? face->font : FRAME_FONT (f);
18453 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18454 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18455 struct glyph *g;
18456 int row_width, stretch_ascent, stretch_width;
18457 struct text_pos saved_pos;
18458 int saved_face_id, saved_avoid_cursor;
18460 for (row_width = 0, g = row_start; g < row_end; g++)
18461 row_width += g->pixel_width;
18462 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18463 if (stretch_width > 0)
18465 stretch_ascent =
18466 (((it->ascent + it->descent)
18467 * FONT_BASE (font)) / FONT_HEIGHT (font));
18468 saved_pos = it->position;
18469 memset (&it->position, 0, sizeof it->position);
18470 saved_avoid_cursor = it->avoid_cursor_p;
18471 it->avoid_cursor_p = 1;
18472 saved_face_id = it->face_id;
18473 /* The last row's stretch glyph should get the default
18474 face, to avoid painting the rest of the window with
18475 the region face, if the region ends at ZV. */
18476 if (it->glyph_row->ends_at_zv_p)
18477 it->face_id = default_face->id;
18478 else
18479 it->face_id = face->id;
18480 append_stretch_glyph (it, make_number (0), stretch_width,
18481 it->ascent + it->descent, stretch_ascent);
18482 it->position = saved_pos;
18483 it->avoid_cursor_p = saved_avoid_cursor;
18484 it->face_id = saved_face_id;
18487 #endif /* HAVE_WINDOW_SYSTEM */
18489 else
18491 /* Save some values that must not be changed. */
18492 int saved_x = it->current_x;
18493 struct text_pos saved_pos;
18494 Lisp_Object saved_object;
18495 enum display_element_type saved_what = it->what;
18496 int saved_face_id = it->face_id;
18498 saved_object = it->object;
18499 saved_pos = it->position;
18501 it->what = IT_CHARACTER;
18502 memset (&it->position, 0, sizeof it->position);
18503 it->object = make_number (0);
18504 it->c = it->char_to_display = ' ';
18505 it->len = 1;
18506 /* The last row's blank glyphs should get the default face, to
18507 avoid painting the rest of the window with the region face,
18508 if the region ends at ZV. */
18509 if (it->glyph_row->ends_at_zv_p)
18510 it->face_id = default_face->id;
18511 else
18512 it->face_id = face->id;
18514 PRODUCE_GLYPHS (it);
18516 while (it->current_x <= it->last_visible_x)
18517 PRODUCE_GLYPHS (it);
18519 /* Don't count these blanks really. It would let us insert a left
18520 truncation glyph below and make us set the cursor on them, maybe. */
18521 it->current_x = saved_x;
18522 it->object = saved_object;
18523 it->position = saved_pos;
18524 it->what = saved_what;
18525 it->face_id = saved_face_id;
18530 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18531 trailing whitespace. */
18533 static int
18534 trailing_whitespace_p (EMACS_INT charpos)
18536 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
18537 int c = 0;
18539 while (bytepos < ZV_BYTE
18540 && (c = FETCH_CHAR (bytepos),
18541 c == ' ' || c == '\t'))
18542 ++bytepos;
18544 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18546 if (bytepos != PT_BYTE)
18547 return 1;
18549 return 0;
18553 /* Highlight trailing whitespace, if any, in ROW. */
18555 static void
18556 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18558 int used = row->used[TEXT_AREA];
18560 if (used)
18562 struct glyph *start = row->glyphs[TEXT_AREA];
18563 struct glyph *glyph = start + used - 1;
18565 if (row->reversed_p)
18567 /* Right-to-left rows need to be processed in the opposite
18568 direction, so swap the edge pointers. */
18569 glyph = start;
18570 start = row->glyphs[TEXT_AREA] + used - 1;
18573 /* Skip over glyphs inserted to display the cursor at the
18574 end of a line, for extending the face of the last glyph
18575 to the end of the line on terminals, and for truncation
18576 and continuation glyphs. */
18577 if (!row->reversed_p)
18579 while (glyph >= start
18580 && glyph->type == CHAR_GLYPH
18581 && INTEGERP (glyph->object))
18582 --glyph;
18584 else
18586 while (glyph <= start
18587 && glyph->type == CHAR_GLYPH
18588 && INTEGERP (glyph->object))
18589 ++glyph;
18592 /* If last glyph is a space or stretch, and it's trailing
18593 whitespace, set the face of all trailing whitespace glyphs in
18594 IT->glyph_row to `trailing-whitespace'. */
18595 if ((row->reversed_p ? glyph <= start : glyph >= start)
18596 && BUFFERP (glyph->object)
18597 && (glyph->type == STRETCH_GLYPH
18598 || (glyph->type == CHAR_GLYPH
18599 && glyph->u.ch == ' '))
18600 && trailing_whitespace_p (glyph->charpos))
18602 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18603 if (face_id < 0)
18604 return;
18606 if (!row->reversed_p)
18608 while (glyph >= start
18609 && BUFFERP (glyph->object)
18610 && (glyph->type == STRETCH_GLYPH
18611 || (glyph->type == CHAR_GLYPH
18612 && glyph->u.ch == ' ')))
18613 (glyph--)->face_id = face_id;
18615 else
18617 while (glyph <= start
18618 && BUFFERP (glyph->object)
18619 && (glyph->type == STRETCH_GLYPH
18620 || (glyph->type == CHAR_GLYPH
18621 && glyph->u.ch == ' ')))
18622 (glyph++)->face_id = face_id;
18629 /* Value is non-zero if glyph row ROW should be
18630 used to hold the cursor. */
18632 static int
18633 cursor_row_p (struct glyph_row *row)
18635 int result = 1;
18637 if (PT == CHARPOS (row->end.pos)
18638 || PT == MATRIX_ROW_END_CHARPOS (row))
18640 /* Suppose the row ends on a string.
18641 Unless the row is continued, that means it ends on a newline
18642 in the string. If it's anything other than a display string
18643 (e.g., a before-string from an overlay), we don't want the
18644 cursor there. (This heuristic seems to give the optimal
18645 behavior for the various types of multi-line strings.)
18646 One exception: if the string has `cursor' property on one of
18647 its characters, we _do_ want the cursor there. */
18648 if (CHARPOS (row->end.string_pos) >= 0)
18650 if (row->continued_p)
18651 result = 1;
18652 else
18654 /* Check for `display' property. */
18655 struct glyph *beg = row->glyphs[TEXT_AREA];
18656 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18657 struct glyph *glyph;
18659 result = 0;
18660 for (glyph = end; glyph >= beg; --glyph)
18661 if (STRINGP (glyph->object))
18663 Lisp_Object prop
18664 = Fget_char_property (make_number (PT),
18665 Qdisplay, Qnil);
18666 result =
18667 (!NILP (prop)
18668 && display_prop_string_p (prop, glyph->object));
18669 /* If there's a `cursor' property on one of the
18670 string's characters, this row is a cursor row,
18671 even though this is not a display string. */
18672 if (!result)
18674 Lisp_Object s = glyph->object;
18676 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18678 EMACS_INT gpos = glyph->charpos;
18680 if (!NILP (Fget_char_property (make_number (gpos),
18681 Qcursor, s)))
18683 result = 1;
18684 break;
18688 break;
18692 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18694 /* If the row ends in middle of a real character,
18695 and the line is continued, we want the cursor here.
18696 That's because CHARPOS (ROW->end.pos) would equal
18697 PT if PT is before the character. */
18698 if (!row->ends_in_ellipsis_p)
18699 result = row->continued_p;
18700 else
18701 /* If the row ends in an ellipsis, then
18702 CHARPOS (ROW->end.pos) will equal point after the
18703 invisible text. We want that position to be displayed
18704 after the ellipsis. */
18705 result = 0;
18707 /* If the row ends at ZV, display the cursor at the end of that
18708 row instead of at the start of the row below. */
18709 else if (row->ends_at_zv_p)
18710 result = 1;
18711 else
18712 result = 0;
18715 return result;
18720 /* Push the property PROP so that it will be rendered at the current
18721 position in IT. Return 1 if PROP was successfully pushed, 0
18722 otherwise. Called from handle_line_prefix to handle the
18723 `line-prefix' and `wrap-prefix' properties. */
18725 static int
18726 push_prefix_prop (struct it *it, Lisp_Object prop)
18728 struct text_pos pos =
18729 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18731 xassert (it->method == GET_FROM_BUFFER
18732 || it->method == GET_FROM_DISPLAY_VECTOR
18733 || it->method == GET_FROM_STRING);
18735 /* We need to save the current buffer/string position, so it will be
18736 restored by pop_it, because iterate_out_of_display_property
18737 depends on that being set correctly, but some situations leave
18738 it->position not yet set when this function is called. */
18739 push_it (it, &pos);
18741 if (STRINGP (prop))
18743 if (SCHARS (prop) == 0)
18745 pop_it (it);
18746 return 0;
18749 it->string = prop;
18750 it->string_from_prefix_prop_p = 1;
18751 it->multibyte_p = STRING_MULTIBYTE (it->string);
18752 it->current.overlay_string_index = -1;
18753 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18754 it->end_charpos = it->string_nchars = SCHARS (it->string);
18755 it->method = GET_FROM_STRING;
18756 it->stop_charpos = 0;
18757 it->prev_stop = 0;
18758 it->base_level_stop = 0;
18760 /* Force paragraph direction to be that of the parent
18761 buffer/string. */
18762 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18763 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18764 else
18765 it->paragraph_embedding = L2R;
18767 /* Set up the bidi iterator for this display string. */
18768 if (it->bidi_p)
18770 it->bidi_it.string.lstring = it->string;
18771 it->bidi_it.string.s = NULL;
18772 it->bidi_it.string.schars = it->end_charpos;
18773 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18774 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18775 it->bidi_it.string.unibyte = !it->multibyte_p;
18776 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18779 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18781 it->method = GET_FROM_STRETCH;
18782 it->object = prop;
18784 #ifdef HAVE_WINDOW_SYSTEM
18785 else if (IMAGEP (prop))
18787 it->what = IT_IMAGE;
18788 it->image_id = lookup_image (it->f, prop);
18789 it->method = GET_FROM_IMAGE;
18791 #endif /* HAVE_WINDOW_SYSTEM */
18792 else
18794 pop_it (it); /* bogus display property, give up */
18795 return 0;
18798 return 1;
18801 /* Return the character-property PROP at the current position in IT. */
18803 static Lisp_Object
18804 get_it_property (struct it *it, Lisp_Object prop)
18806 Lisp_Object position;
18808 if (STRINGP (it->object))
18809 position = make_number (IT_STRING_CHARPOS (*it));
18810 else if (BUFFERP (it->object))
18811 position = make_number (IT_CHARPOS (*it));
18812 else
18813 return Qnil;
18815 return Fget_char_property (position, prop, it->object);
18818 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18820 static void
18821 handle_line_prefix (struct it *it)
18823 Lisp_Object prefix;
18825 if (it->continuation_lines_width > 0)
18827 prefix = get_it_property (it, Qwrap_prefix);
18828 if (NILP (prefix))
18829 prefix = Vwrap_prefix;
18831 else
18833 prefix = get_it_property (it, Qline_prefix);
18834 if (NILP (prefix))
18835 prefix = Vline_prefix;
18837 if (! NILP (prefix) && push_prefix_prop (it, prefix))
18839 /* If the prefix is wider than the window, and we try to wrap
18840 it, it would acquire its own wrap prefix, and so on till the
18841 iterator stack overflows. So, don't wrap the prefix. */
18842 it->line_wrap = TRUNCATE;
18843 it->avoid_cursor_p = 1;
18849 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18850 only for R2L lines from display_line and display_string, when they
18851 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18852 the line/string needs to be continued on the next glyph row. */
18853 static void
18854 unproduce_glyphs (struct it *it, int n)
18856 struct glyph *glyph, *end;
18858 xassert (it->glyph_row);
18859 xassert (it->glyph_row->reversed_p);
18860 xassert (it->area == TEXT_AREA);
18861 xassert (n <= it->glyph_row->used[TEXT_AREA]);
18863 if (n > it->glyph_row->used[TEXT_AREA])
18864 n = it->glyph_row->used[TEXT_AREA];
18865 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18866 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18867 for ( ; glyph < end; glyph++)
18868 glyph[-n] = *glyph;
18871 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18872 and ROW->maxpos. */
18873 static void
18874 find_row_edges (struct it *it, struct glyph_row *row,
18875 EMACS_INT min_pos, EMACS_INT min_bpos,
18876 EMACS_INT max_pos, EMACS_INT max_bpos)
18878 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18879 lines' rows is implemented for bidi-reordered rows. */
18881 /* ROW->minpos is the value of min_pos, the minimal buffer position
18882 we have in ROW, or ROW->start.pos if that is smaller. */
18883 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18884 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18885 else
18886 /* We didn't find buffer positions smaller than ROW->start, or
18887 didn't find _any_ valid buffer positions in any of the glyphs,
18888 so we must trust the iterator's computed positions. */
18889 row->minpos = row->start.pos;
18890 if (max_pos <= 0)
18892 max_pos = CHARPOS (it->current.pos);
18893 max_bpos = BYTEPOS (it->current.pos);
18896 /* Here are the various use-cases for ending the row, and the
18897 corresponding values for ROW->maxpos:
18899 Line ends in a newline from buffer eol_pos + 1
18900 Line is continued from buffer max_pos + 1
18901 Line is truncated on right it->current.pos
18902 Line ends in a newline from string max_pos + 1(*)
18903 (*) + 1 only when line ends in a forward scan
18904 Line is continued from string max_pos
18905 Line is continued from display vector max_pos
18906 Line is entirely from a string min_pos == max_pos
18907 Line is entirely from a display vector min_pos == max_pos
18908 Line that ends at ZV ZV
18910 If you discover other use-cases, please add them here as
18911 appropriate. */
18912 if (row->ends_at_zv_p)
18913 row->maxpos = it->current.pos;
18914 else if (row->used[TEXT_AREA])
18916 int seen_this_string = 0;
18917 struct glyph_row *r1 = row - 1;
18919 /* Did we see the same display string on the previous row? */
18920 if (STRINGP (it->object)
18921 /* this is not the first row */
18922 && row > it->w->desired_matrix->rows
18923 /* previous row is not the header line */
18924 && !r1->mode_line_p
18925 /* previous row also ends in a newline from a string */
18926 && r1->ends_in_newline_from_string_p)
18928 struct glyph *start, *end;
18930 /* Search for the last glyph of the previous row that came
18931 from buffer or string. Depending on whether the row is
18932 L2R or R2L, we need to process it front to back or the
18933 other way round. */
18934 if (!r1->reversed_p)
18936 start = r1->glyphs[TEXT_AREA];
18937 end = start + r1->used[TEXT_AREA];
18938 /* Glyphs inserted by redisplay have an integer (zero)
18939 as their object. */
18940 while (end > start
18941 && INTEGERP ((end - 1)->object)
18942 && (end - 1)->charpos <= 0)
18943 --end;
18944 if (end > start)
18946 if (EQ ((end - 1)->object, it->object))
18947 seen_this_string = 1;
18949 else
18950 /* If all the glyphs of the previous row were inserted
18951 by redisplay, it means the previous row was
18952 produced from a single newline, which is only
18953 possible if that newline came from the same string
18954 as the one which produced this ROW. */
18955 seen_this_string = 1;
18957 else
18959 end = r1->glyphs[TEXT_AREA] - 1;
18960 start = end + r1->used[TEXT_AREA];
18961 while (end < start
18962 && INTEGERP ((end + 1)->object)
18963 && (end + 1)->charpos <= 0)
18964 ++end;
18965 if (end < start)
18967 if (EQ ((end + 1)->object, it->object))
18968 seen_this_string = 1;
18970 else
18971 seen_this_string = 1;
18974 /* Take note of each display string that covers a newline only
18975 once, the first time we see it. This is for when a display
18976 string includes more than one newline in it. */
18977 if (row->ends_in_newline_from_string_p && !seen_this_string)
18979 /* If we were scanning the buffer forward when we displayed
18980 the string, we want to account for at least one buffer
18981 position that belongs to this row (position covered by
18982 the display string), so that cursor positioning will
18983 consider this row as a candidate when point is at the end
18984 of the visual line represented by this row. This is not
18985 required when scanning back, because max_pos will already
18986 have a much larger value. */
18987 if (CHARPOS (row->end.pos) > max_pos)
18988 INC_BOTH (max_pos, max_bpos);
18989 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18991 else if (CHARPOS (it->eol_pos) > 0)
18992 SET_TEXT_POS (row->maxpos,
18993 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
18994 else if (row->continued_p)
18996 /* If max_pos is different from IT's current position, it
18997 means IT->method does not belong to the display element
18998 at max_pos. However, it also means that the display
18999 element at max_pos was displayed in its entirety on this
19000 line, which is equivalent to saying that the next line
19001 starts at the next buffer position. */
19002 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19003 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19004 else
19006 INC_BOTH (max_pos, max_bpos);
19007 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19010 else if (row->truncated_on_right_p)
19011 /* display_line already called reseat_at_next_visible_line_start,
19012 which puts the iterator at the beginning of the next line, in
19013 the logical order. */
19014 row->maxpos = it->current.pos;
19015 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19016 /* A line that is entirely from a string/image/stretch... */
19017 row->maxpos = row->minpos;
19018 else
19019 abort ();
19021 else
19022 row->maxpos = it->current.pos;
19025 /* Construct the glyph row IT->glyph_row in the desired matrix of
19026 IT->w from text at the current position of IT. See dispextern.h
19027 for an overview of struct it. Value is non-zero if
19028 IT->glyph_row displays text, as opposed to a line displaying ZV
19029 only. */
19031 static int
19032 display_line (struct it *it)
19034 struct glyph_row *row = it->glyph_row;
19035 Lisp_Object overlay_arrow_string;
19036 struct it wrap_it;
19037 void *wrap_data = NULL;
19038 int may_wrap = 0, wrap_x IF_LINT (= 0);
19039 int wrap_row_used = -1;
19040 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19041 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19042 int wrap_row_extra_line_spacing IF_LINT (= 0);
19043 EMACS_INT wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19044 EMACS_INT wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19045 int cvpos;
19046 EMACS_INT min_pos = ZV + 1, max_pos = 0;
19047 EMACS_INT min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19049 /* We always start displaying at hpos zero even if hscrolled. */
19050 xassert (it->hpos == 0 && it->current_x == 0);
19052 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19053 >= it->w->desired_matrix->nrows)
19055 it->w->nrows_scale_factor++;
19056 fonts_changed_p = 1;
19057 return 0;
19060 /* Is IT->w showing the region? */
19061 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
19063 /* Clear the result glyph row and enable it. */
19064 prepare_desired_row (row);
19066 row->y = it->current_y;
19067 row->start = it->start;
19068 row->continuation_lines_width = it->continuation_lines_width;
19069 row->displays_text_p = 1;
19070 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19071 it->starts_in_middle_of_char_p = 0;
19073 /* Arrange the overlays nicely for our purposes. Usually, we call
19074 display_line on only one line at a time, in which case this
19075 can't really hurt too much, or we call it on lines which appear
19076 one after another in the buffer, in which case all calls to
19077 recenter_overlay_lists but the first will be pretty cheap. */
19078 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19080 /* Move over display elements that are not visible because we are
19081 hscrolled. This may stop at an x-position < IT->first_visible_x
19082 if the first glyph is partially visible or if we hit a line end. */
19083 if (it->current_x < it->first_visible_x)
19085 this_line_min_pos = row->start.pos;
19086 move_it_in_display_line_to (it, ZV, it->first_visible_x,
19087 MOVE_TO_POS | MOVE_TO_X);
19088 /* Record the smallest positions seen while we moved over
19089 display elements that are not visible. This is needed by
19090 redisplay_internal for optimizing the case where the cursor
19091 stays inside the same line. The rest of this function only
19092 considers positions that are actually displayed, so
19093 RECORD_MAX_MIN_POS will not otherwise record positions that
19094 are hscrolled to the left of the left edge of the window. */
19095 min_pos = CHARPOS (this_line_min_pos);
19096 min_bpos = BYTEPOS (this_line_min_pos);
19098 else
19100 /* We only do this when not calling `move_it_in_display_line_to'
19101 above, because move_it_in_display_line_to calls
19102 handle_line_prefix itself. */
19103 handle_line_prefix (it);
19106 /* Get the initial row height. This is either the height of the
19107 text hscrolled, if there is any, or zero. */
19108 row->ascent = it->max_ascent;
19109 row->height = it->max_ascent + it->max_descent;
19110 row->phys_ascent = it->max_phys_ascent;
19111 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19112 row->extra_line_spacing = it->max_extra_line_spacing;
19114 /* Utility macro to record max and min buffer positions seen until now. */
19115 #define RECORD_MAX_MIN_POS(IT) \
19116 do \
19118 int composition_p = !STRINGP ((IT)->string) \
19119 && ((IT)->what == IT_COMPOSITION); \
19120 EMACS_INT current_pos = \
19121 composition_p ? (IT)->cmp_it.charpos \
19122 : IT_CHARPOS (*(IT)); \
19123 EMACS_INT current_bpos = \
19124 composition_p ? CHAR_TO_BYTE (current_pos) \
19125 : IT_BYTEPOS (*(IT)); \
19126 if (current_pos < min_pos) \
19128 min_pos = current_pos; \
19129 min_bpos = current_bpos; \
19131 if (IT_CHARPOS (*it) > max_pos) \
19133 max_pos = IT_CHARPOS (*it); \
19134 max_bpos = IT_BYTEPOS (*it); \
19137 while (0)
19139 /* Loop generating characters. The loop is left with IT on the next
19140 character to display. */
19141 while (1)
19143 int n_glyphs_before, hpos_before, x_before;
19144 int x, nglyphs;
19145 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19147 /* Retrieve the next thing to display. Value is zero if end of
19148 buffer reached. */
19149 if (!get_next_display_element (it))
19151 /* Maybe add a space at the end of this line that is used to
19152 display the cursor there under X. Set the charpos of the
19153 first glyph of blank lines not corresponding to any text
19154 to -1. */
19155 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19156 row->exact_window_width_line_p = 1;
19157 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19158 || row->used[TEXT_AREA] == 0)
19160 row->glyphs[TEXT_AREA]->charpos = -1;
19161 row->displays_text_p = 0;
19163 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19164 && (!MINI_WINDOW_P (it->w)
19165 || (minibuf_level && EQ (it->window, minibuf_window))))
19166 row->indicate_empty_line_p = 1;
19169 it->continuation_lines_width = 0;
19170 row->ends_at_zv_p = 1;
19171 /* A row that displays right-to-left text must always have
19172 its last face extended all the way to the end of line,
19173 even if this row ends in ZV, because we still write to
19174 the screen left to right. We also need to extend the
19175 last face if the default face is remapped to some
19176 different face, otherwise the functions that clear
19177 portions of the screen will clear with the default face's
19178 background color. */
19179 if (row->reversed_p
19180 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19181 extend_face_to_end_of_line (it);
19182 break;
19185 /* Now, get the metrics of what we want to display. This also
19186 generates glyphs in `row' (which is IT->glyph_row). */
19187 n_glyphs_before = row->used[TEXT_AREA];
19188 x = it->current_x;
19190 /* Remember the line height so far in case the next element doesn't
19191 fit on the line. */
19192 if (it->line_wrap != TRUNCATE)
19194 ascent = it->max_ascent;
19195 descent = it->max_descent;
19196 phys_ascent = it->max_phys_ascent;
19197 phys_descent = it->max_phys_descent;
19199 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19201 if (IT_DISPLAYING_WHITESPACE (it))
19202 may_wrap = 1;
19203 else if (may_wrap)
19205 SAVE_IT (wrap_it, *it, wrap_data);
19206 wrap_x = x;
19207 wrap_row_used = row->used[TEXT_AREA];
19208 wrap_row_ascent = row->ascent;
19209 wrap_row_height = row->height;
19210 wrap_row_phys_ascent = row->phys_ascent;
19211 wrap_row_phys_height = row->phys_height;
19212 wrap_row_extra_line_spacing = row->extra_line_spacing;
19213 wrap_row_min_pos = min_pos;
19214 wrap_row_min_bpos = min_bpos;
19215 wrap_row_max_pos = max_pos;
19216 wrap_row_max_bpos = max_bpos;
19217 may_wrap = 0;
19222 PRODUCE_GLYPHS (it);
19224 /* If this display element was in marginal areas, continue with
19225 the next one. */
19226 if (it->area != TEXT_AREA)
19228 row->ascent = max (row->ascent, it->max_ascent);
19229 row->height = max (row->height, it->max_ascent + it->max_descent);
19230 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19231 row->phys_height = max (row->phys_height,
19232 it->max_phys_ascent + it->max_phys_descent);
19233 row->extra_line_spacing = max (row->extra_line_spacing,
19234 it->max_extra_line_spacing);
19235 set_iterator_to_next (it, 1);
19236 continue;
19239 /* Does the display element fit on the line? If we truncate
19240 lines, we should draw past the right edge of the window. If
19241 we don't truncate, we want to stop so that we can display the
19242 continuation glyph before the right margin. If lines are
19243 continued, there are two possible strategies for characters
19244 resulting in more than 1 glyph (e.g. tabs): Display as many
19245 glyphs as possible in this line and leave the rest for the
19246 continuation line, or display the whole element in the next
19247 line. Original redisplay did the former, so we do it also. */
19248 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19249 hpos_before = it->hpos;
19250 x_before = x;
19252 if (/* Not a newline. */
19253 nglyphs > 0
19254 /* Glyphs produced fit entirely in the line. */
19255 && it->current_x < it->last_visible_x)
19257 it->hpos += nglyphs;
19258 row->ascent = max (row->ascent, it->max_ascent);
19259 row->height = max (row->height, it->max_ascent + it->max_descent);
19260 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19261 row->phys_height = max (row->phys_height,
19262 it->max_phys_ascent + it->max_phys_descent);
19263 row->extra_line_spacing = max (row->extra_line_spacing,
19264 it->max_extra_line_spacing);
19265 if (it->current_x - it->pixel_width < it->first_visible_x)
19266 row->x = x - it->first_visible_x;
19267 /* Record the maximum and minimum buffer positions seen so
19268 far in glyphs that will be displayed by this row. */
19269 if (it->bidi_p)
19270 RECORD_MAX_MIN_POS (it);
19272 else
19274 int i, new_x;
19275 struct glyph *glyph;
19277 for (i = 0; i < nglyphs; ++i, x = new_x)
19279 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19280 new_x = x + glyph->pixel_width;
19282 if (/* Lines are continued. */
19283 it->line_wrap != TRUNCATE
19284 && (/* Glyph doesn't fit on the line. */
19285 new_x > it->last_visible_x
19286 /* Or it fits exactly on a window system frame. */
19287 || (new_x == it->last_visible_x
19288 && FRAME_WINDOW_P (it->f))))
19290 /* End of a continued line. */
19292 if (it->hpos == 0
19293 || (new_x == it->last_visible_x
19294 && FRAME_WINDOW_P (it->f)))
19296 /* Current glyph is the only one on the line or
19297 fits exactly on the line. We must continue
19298 the line because we can't draw the cursor
19299 after the glyph. */
19300 row->continued_p = 1;
19301 it->current_x = new_x;
19302 it->continuation_lines_width += new_x;
19303 ++it->hpos;
19304 if (i == nglyphs - 1)
19306 /* If line-wrap is on, check if a previous
19307 wrap point was found. */
19308 if (wrap_row_used > 0
19309 /* Even if there is a previous wrap
19310 point, continue the line here as
19311 usual, if (i) the previous character
19312 was a space or tab AND (ii) the
19313 current character is not. */
19314 && (!may_wrap
19315 || IT_DISPLAYING_WHITESPACE (it)))
19316 goto back_to_wrap;
19318 /* Record the maximum and minimum buffer
19319 positions seen so far in glyphs that will be
19320 displayed by this row. */
19321 if (it->bidi_p)
19322 RECORD_MAX_MIN_POS (it);
19323 set_iterator_to_next (it, 1);
19324 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19326 if (!get_next_display_element (it))
19328 row->exact_window_width_line_p = 1;
19329 it->continuation_lines_width = 0;
19330 row->continued_p = 0;
19331 row->ends_at_zv_p = 1;
19333 else if (ITERATOR_AT_END_OF_LINE_P (it))
19335 row->continued_p = 0;
19336 row->exact_window_width_line_p = 1;
19340 else if (it->bidi_p)
19341 RECORD_MAX_MIN_POS (it);
19343 else if (CHAR_GLYPH_PADDING_P (*glyph)
19344 && !FRAME_WINDOW_P (it->f))
19346 /* A padding glyph that doesn't fit on this line.
19347 This means the whole character doesn't fit
19348 on the line. */
19349 if (row->reversed_p)
19350 unproduce_glyphs (it, row->used[TEXT_AREA]
19351 - n_glyphs_before);
19352 row->used[TEXT_AREA] = n_glyphs_before;
19354 /* Fill the rest of the row with continuation
19355 glyphs like in 20.x. */
19356 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19357 < row->glyphs[1 + TEXT_AREA])
19358 produce_special_glyphs (it, IT_CONTINUATION);
19360 row->continued_p = 1;
19361 it->current_x = x_before;
19362 it->continuation_lines_width += x_before;
19364 /* Restore the height to what it was before the
19365 element not fitting on the line. */
19366 it->max_ascent = ascent;
19367 it->max_descent = descent;
19368 it->max_phys_ascent = phys_ascent;
19369 it->max_phys_descent = phys_descent;
19371 else if (wrap_row_used > 0)
19373 back_to_wrap:
19374 if (row->reversed_p)
19375 unproduce_glyphs (it,
19376 row->used[TEXT_AREA] - wrap_row_used);
19377 RESTORE_IT (it, &wrap_it, wrap_data);
19378 it->continuation_lines_width += wrap_x;
19379 row->used[TEXT_AREA] = wrap_row_used;
19380 row->ascent = wrap_row_ascent;
19381 row->height = wrap_row_height;
19382 row->phys_ascent = wrap_row_phys_ascent;
19383 row->phys_height = wrap_row_phys_height;
19384 row->extra_line_spacing = wrap_row_extra_line_spacing;
19385 min_pos = wrap_row_min_pos;
19386 min_bpos = wrap_row_min_bpos;
19387 max_pos = wrap_row_max_pos;
19388 max_bpos = wrap_row_max_bpos;
19389 row->continued_p = 1;
19390 row->ends_at_zv_p = 0;
19391 row->exact_window_width_line_p = 0;
19392 it->continuation_lines_width += x;
19394 /* Make sure that a non-default face is extended
19395 up to the right margin of the window. */
19396 extend_face_to_end_of_line (it);
19398 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19400 /* A TAB that extends past the right edge of the
19401 window. This produces a single glyph on
19402 window system frames. We leave the glyph in
19403 this row and let it fill the row, but don't
19404 consume the TAB. */
19405 it->continuation_lines_width += it->last_visible_x;
19406 row->ends_in_middle_of_char_p = 1;
19407 row->continued_p = 1;
19408 glyph->pixel_width = it->last_visible_x - x;
19409 it->starts_in_middle_of_char_p = 1;
19411 else
19413 /* Something other than a TAB that draws past
19414 the right edge of the window. Restore
19415 positions to values before the element. */
19416 if (row->reversed_p)
19417 unproduce_glyphs (it, row->used[TEXT_AREA]
19418 - (n_glyphs_before + i));
19419 row->used[TEXT_AREA] = n_glyphs_before + i;
19421 /* Display continuation glyphs. */
19422 if (!FRAME_WINDOW_P (it->f))
19423 produce_special_glyphs (it, IT_CONTINUATION);
19424 row->continued_p = 1;
19426 it->current_x = x_before;
19427 it->continuation_lines_width += x;
19428 extend_face_to_end_of_line (it);
19430 if (nglyphs > 1 && i > 0)
19432 row->ends_in_middle_of_char_p = 1;
19433 it->starts_in_middle_of_char_p = 1;
19436 /* Restore the height to what it was before the
19437 element not fitting on the line. */
19438 it->max_ascent = ascent;
19439 it->max_descent = descent;
19440 it->max_phys_ascent = phys_ascent;
19441 it->max_phys_descent = phys_descent;
19444 break;
19446 else if (new_x > it->first_visible_x)
19448 /* Increment number of glyphs actually displayed. */
19449 ++it->hpos;
19451 /* Record the maximum and minimum buffer positions
19452 seen so far in glyphs that will be displayed by
19453 this row. */
19454 if (it->bidi_p)
19455 RECORD_MAX_MIN_POS (it);
19457 if (x < it->first_visible_x)
19458 /* Glyph is partially visible, i.e. row starts at
19459 negative X position. */
19460 row->x = x - it->first_visible_x;
19462 else
19464 /* Glyph is completely off the left margin of the
19465 window. This should not happen because of the
19466 move_it_in_display_line at the start of this
19467 function, unless the text display area of the
19468 window is empty. */
19469 xassert (it->first_visible_x <= it->last_visible_x);
19472 /* Even if this display element produced no glyphs at all,
19473 we want to record its position. */
19474 if (it->bidi_p && nglyphs == 0)
19475 RECORD_MAX_MIN_POS (it);
19477 row->ascent = max (row->ascent, it->max_ascent);
19478 row->height = max (row->height, it->max_ascent + it->max_descent);
19479 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19480 row->phys_height = max (row->phys_height,
19481 it->max_phys_ascent + it->max_phys_descent);
19482 row->extra_line_spacing = max (row->extra_line_spacing,
19483 it->max_extra_line_spacing);
19485 /* End of this display line if row is continued. */
19486 if (row->continued_p || row->ends_at_zv_p)
19487 break;
19490 at_end_of_line:
19491 /* Is this a line end? If yes, we're also done, after making
19492 sure that a non-default face is extended up to the right
19493 margin of the window. */
19494 if (ITERATOR_AT_END_OF_LINE_P (it))
19496 int used_before = row->used[TEXT_AREA];
19498 row->ends_in_newline_from_string_p = STRINGP (it->object);
19500 /* Add a space at the end of the line that is used to
19501 display the cursor there. */
19502 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19503 append_space_for_newline (it, 0);
19505 /* Extend the face to the end of the line. */
19506 extend_face_to_end_of_line (it);
19508 /* Make sure we have the position. */
19509 if (used_before == 0)
19510 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19512 /* Record the position of the newline, for use in
19513 find_row_edges. */
19514 it->eol_pos = it->current.pos;
19516 /* Consume the line end. This skips over invisible lines. */
19517 set_iterator_to_next (it, 1);
19518 it->continuation_lines_width = 0;
19519 break;
19522 /* Proceed with next display element. Note that this skips
19523 over lines invisible because of selective display. */
19524 set_iterator_to_next (it, 1);
19526 /* If we truncate lines, we are done when the last displayed
19527 glyphs reach past the right margin of the window. */
19528 if (it->line_wrap == TRUNCATE
19529 && (FRAME_WINDOW_P (it->f)
19530 ? (it->current_x >= it->last_visible_x)
19531 : (it->current_x > it->last_visible_x)))
19533 /* Maybe add truncation glyphs. */
19534 if (!FRAME_WINDOW_P (it->f))
19536 int i, n;
19538 if (!row->reversed_p)
19540 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19541 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19542 break;
19544 else
19546 for (i = 0; i < row->used[TEXT_AREA]; i++)
19547 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19548 break;
19549 /* Remove any padding glyphs at the front of ROW, to
19550 make room for the truncation glyphs we will be
19551 adding below. The loop below always inserts at
19552 least one truncation glyph, so also remove the
19553 last glyph added to ROW. */
19554 unproduce_glyphs (it, i + 1);
19555 /* Adjust i for the loop below. */
19556 i = row->used[TEXT_AREA] - (i + 1);
19559 for (n = row->used[TEXT_AREA]; i < n; ++i)
19561 row->used[TEXT_AREA] = i;
19562 produce_special_glyphs (it, IT_TRUNCATION);
19565 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19567 /* Don't truncate if we can overflow newline into fringe. */
19568 if (!get_next_display_element (it))
19570 it->continuation_lines_width = 0;
19571 row->ends_at_zv_p = 1;
19572 row->exact_window_width_line_p = 1;
19573 break;
19575 if (ITERATOR_AT_END_OF_LINE_P (it))
19577 row->exact_window_width_line_p = 1;
19578 goto at_end_of_line;
19582 row->truncated_on_right_p = 1;
19583 it->continuation_lines_width = 0;
19584 reseat_at_next_visible_line_start (it, 0);
19585 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19586 it->hpos = hpos_before;
19587 it->current_x = x_before;
19588 break;
19592 if (wrap_data)
19593 bidi_unshelve_cache (wrap_data, 1);
19595 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19596 at the left window margin. */
19597 if (it->first_visible_x
19598 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19600 if (!FRAME_WINDOW_P (it->f))
19601 insert_left_trunc_glyphs (it);
19602 row->truncated_on_left_p = 1;
19605 /* Remember the position at which this line ends.
19607 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19608 cannot be before the call to find_row_edges below, since that is
19609 where these positions are determined. */
19610 row->end = it->current;
19611 if (!it->bidi_p)
19613 row->minpos = row->start.pos;
19614 row->maxpos = row->end.pos;
19616 else
19618 /* ROW->minpos and ROW->maxpos must be the smallest and
19619 `1 + the largest' buffer positions in ROW. But if ROW was
19620 bidi-reordered, these two positions can be anywhere in the
19621 row, so we must determine them now. */
19622 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19625 /* If the start of this line is the overlay arrow-position, then
19626 mark this glyph row as the one containing the overlay arrow.
19627 This is clearly a mess with variable size fonts. It would be
19628 better to let it be displayed like cursors under X. */
19629 if ((row->displays_text_p || !overlay_arrow_seen)
19630 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19631 !NILP (overlay_arrow_string)))
19633 /* Overlay arrow in window redisplay is a fringe bitmap. */
19634 if (STRINGP (overlay_arrow_string))
19636 struct glyph_row *arrow_row
19637 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19638 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19639 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19640 struct glyph *p = row->glyphs[TEXT_AREA];
19641 struct glyph *p2, *end;
19643 /* Copy the arrow glyphs. */
19644 while (glyph < arrow_end)
19645 *p++ = *glyph++;
19647 /* Throw away padding glyphs. */
19648 p2 = p;
19649 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19650 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19651 ++p2;
19652 if (p2 > p)
19654 while (p2 < end)
19655 *p++ = *p2++;
19656 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19659 else
19661 xassert (INTEGERP (overlay_arrow_string));
19662 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19664 overlay_arrow_seen = 1;
19667 /* Highlight trailing whitespace. */
19668 if (!NILP (Vshow_trailing_whitespace))
19669 highlight_trailing_whitespace (it->f, it->glyph_row);
19671 /* Compute pixel dimensions of this line. */
19672 compute_line_metrics (it);
19674 /* Implementation note: No changes in the glyphs of ROW or in their
19675 faces can be done past this point, because compute_line_metrics
19676 computes ROW's hash value and stores it within the glyph_row
19677 structure. */
19679 /* Record whether this row ends inside an ellipsis. */
19680 row->ends_in_ellipsis_p
19681 = (it->method == GET_FROM_DISPLAY_VECTOR
19682 && it->ellipsis_p);
19684 /* Save fringe bitmaps in this row. */
19685 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19686 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19687 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19688 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19690 it->left_user_fringe_bitmap = 0;
19691 it->left_user_fringe_face_id = 0;
19692 it->right_user_fringe_bitmap = 0;
19693 it->right_user_fringe_face_id = 0;
19695 /* Maybe set the cursor. */
19696 cvpos = it->w->cursor.vpos;
19697 if ((cvpos < 0
19698 /* In bidi-reordered rows, keep checking for proper cursor
19699 position even if one has been found already, because buffer
19700 positions in such rows change non-linearly with ROW->VPOS,
19701 when a line is continued. One exception: when we are at ZV,
19702 display cursor on the first suitable glyph row, since all
19703 the empty rows after that also have their position set to ZV. */
19704 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19705 lines' rows is implemented for bidi-reordered rows. */
19706 || (it->bidi_p
19707 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19708 && PT >= MATRIX_ROW_START_CHARPOS (row)
19709 && PT <= MATRIX_ROW_END_CHARPOS (row)
19710 && cursor_row_p (row))
19711 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19713 /* Prepare for the next line. This line starts horizontally at (X
19714 HPOS) = (0 0). Vertical positions are incremented. As a
19715 convenience for the caller, IT->glyph_row is set to the next
19716 row to be used. */
19717 it->current_x = it->hpos = 0;
19718 it->current_y += row->height;
19719 SET_TEXT_POS (it->eol_pos, 0, 0);
19720 ++it->vpos;
19721 ++it->glyph_row;
19722 /* The next row should by default use the same value of the
19723 reversed_p flag as this one. set_iterator_to_next decides when
19724 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19725 the flag accordingly. */
19726 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19727 it->glyph_row->reversed_p = row->reversed_p;
19728 it->start = row->end;
19729 return row->displays_text_p;
19731 #undef RECORD_MAX_MIN_POS
19734 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19735 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19736 doc: /* Return paragraph direction at point in BUFFER.
19737 Value is either `left-to-right' or `right-to-left'.
19738 If BUFFER is omitted or nil, it defaults to the current buffer.
19740 Paragraph direction determines how the text in the paragraph is displayed.
19741 In left-to-right paragraphs, text begins at the left margin of the window
19742 and the reading direction is generally left to right. In right-to-left
19743 paragraphs, text begins at the right margin and is read from right to left.
19745 See also `bidi-paragraph-direction'. */)
19746 (Lisp_Object buffer)
19748 struct buffer *buf = current_buffer;
19749 struct buffer *old = buf;
19751 if (! NILP (buffer))
19753 CHECK_BUFFER (buffer);
19754 buf = XBUFFER (buffer);
19757 if (NILP (BVAR (buf, bidi_display_reordering))
19758 || NILP (BVAR (buf, enable_multibyte_characters))
19759 /* When we are loading loadup.el, the character property tables
19760 needed for bidi iteration are not yet available. */
19761 || !NILP (Vpurify_flag))
19762 return Qleft_to_right;
19763 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19764 return BVAR (buf, bidi_paragraph_direction);
19765 else
19767 /* Determine the direction from buffer text. We could try to
19768 use current_matrix if it is up to date, but this seems fast
19769 enough as it is. */
19770 struct bidi_it itb;
19771 EMACS_INT pos = BUF_PT (buf);
19772 EMACS_INT bytepos = BUF_PT_BYTE (buf);
19773 int c;
19774 void *itb_data = bidi_shelve_cache ();
19776 set_buffer_temp (buf);
19777 /* bidi_paragraph_init finds the base direction of the paragraph
19778 by searching forward from paragraph start. We need the base
19779 direction of the current or _previous_ paragraph, so we need
19780 to make sure we are within that paragraph. To that end, find
19781 the previous non-empty line. */
19782 if (pos >= ZV && pos > BEGV)
19784 pos--;
19785 bytepos = CHAR_TO_BYTE (pos);
19787 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19788 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19790 while ((c = FETCH_BYTE (bytepos)) == '\n'
19791 || c == ' ' || c == '\t' || c == '\f')
19793 if (bytepos <= BEGV_BYTE)
19794 break;
19795 bytepos--;
19796 pos--;
19798 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19799 bytepos--;
19801 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19802 itb.paragraph_dir = NEUTRAL_DIR;
19803 itb.string.s = NULL;
19804 itb.string.lstring = Qnil;
19805 itb.string.bufpos = 0;
19806 itb.string.unibyte = 0;
19807 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19808 bidi_unshelve_cache (itb_data, 0);
19809 set_buffer_temp (old);
19810 switch (itb.paragraph_dir)
19812 case L2R:
19813 return Qleft_to_right;
19814 break;
19815 case R2L:
19816 return Qright_to_left;
19817 break;
19818 default:
19819 abort ();
19826 /***********************************************************************
19827 Menu Bar
19828 ***********************************************************************/
19830 /* Redisplay the menu bar in the frame for window W.
19832 The menu bar of X frames that don't have X toolkit support is
19833 displayed in a special window W->frame->menu_bar_window.
19835 The menu bar of terminal frames is treated specially as far as
19836 glyph matrices are concerned. Menu bar lines are not part of
19837 windows, so the update is done directly on the frame matrix rows
19838 for the menu bar. */
19840 static void
19841 display_menu_bar (struct window *w)
19843 struct frame *f = XFRAME (WINDOW_FRAME (w));
19844 struct it it;
19845 Lisp_Object items;
19846 int i;
19848 /* Don't do all this for graphical frames. */
19849 #ifdef HAVE_NTGUI
19850 if (FRAME_W32_P (f))
19851 return;
19852 #endif
19853 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
19854 if (FRAME_X_P (f))
19855 return;
19856 #endif
19858 #ifdef HAVE_NS
19859 if (FRAME_NS_P (f))
19860 return;
19861 #endif /* HAVE_NS */
19863 #ifdef USE_X_TOOLKIT
19864 xassert (!FRAME_WINDOW_P (f));
19865 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
19866 it.first_visible_x = 0;
19867 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19868 #else /* not USE_X_TOOLKIT */
19869 if (FRAME_WINDOW_P (f))
19871 /* Menu bar lines are displayed in the desired matrix of the
19872 dummy window menu_bar_window. */
19873 struct window *menu_w;
19874 xassert (WINDOWP (f->menu_bar_window));
19875 menu_w = XWINDOW (f->menu_bar_window);
19876 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
19877 MENU_FACE_ID);
19878 it.first_visible_x = 0;
19879 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19881 else
19883 /* This is a TTY frame, i.e. character hpos/vpos are used as
19884 pixel x/y. */
19885 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
19886 MENU_FACE_ID);
19887 it.first_visible_x = 0;
19888 it.last_visible_x = FRAME_COLS (f);
19890 #endif /* not USE_X_TOOLKIT */
19892 /* FIXME: This should be controlled by a user option. See the
19893 comments in redisplay_tool_bar and display_mode_line about
19894 this. */
19895 it.paragraph_embedding = L2R;
19897 if (! mode_line_inverse_video)
19898 /* Force the menu-bar to be displayed in the default face. */
19899 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19901 /* Clear all rows of the menu bar. */
19902 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
19904 struct glyph_row *row = it.glyph_row + i;
19905 clear_glyph_row (row);
19906 row->enabled_p = 1;
19907 row->full_width_p = 1;
19910 /* Display all items of the menu bar. */
19911 items = FRAME_MENU_BAR_ITEMS (it.f);
19912 for (i = 0; i < ASIZE (items); i += 4)
19914 Lisp_Object string;
19916 /* Stop at nil string. */
19917 string = AREF (items, i + 1);
19918 if (NILP (string))
19919 break;
19921 /* Remember where item was displayed. */
19922 ASET (items, i + 3, make_number (it.hpos));
19924 /* Display the item, pad with one space. */
19925 if (it.current_x < it.last_visible_x)
19926 display_string (NULL, string, Qnil, 0, 0, &it,
19927 SCHARS (string) + 1, 0, 0, -1);
19930 /* Fill out the line with spaces. */
19931 if (it.current_x < it.last_visible_x)
19932 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
19934 /* Compute the total height of the lines. */
19935 compute_line_metrics (&it);
19940 /***********************************************************************
19941 Mode Line
19942 ***********************************************************************/
19944 /* Redisplay mode lines in the window tree whose root is WINDOW. If
19945 FORCE is non-zero, redisplay mode lines unconditionally.
19946 Otherwise, redisplay only mode lines that are garbaged. Value is
19947 the number of windows whose mode lines were redisplayed. */
19949 static int
19950 redisplay_mode_lines (Lisp_Object window, int force)
19952 int nwindows = 0;
19954 while (!NILP (window))
19956 struct window *w = XWINDOW (window);
19958 if (WINDOWP (w->hchild))
19959 nwindows += redisplay_mode_lines (w->hchild, force);
19960 else if (WINDOWP (w->vchild))
19961 nwindows += redisplay_mode_lines (w->vchild, force);
19962 else if (force
19963 || FRAME_GARBAGED_P (XFRAME (w->frame))
19964 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
19966 struct text_pos lpoint;
19967 struct buffer *old = current_buffer;
19969 /* Set the window's buffer for the mode line display. */
19970 SET_TEXT_POS (lpoint, PT, PT_BYTE);
19971 set_buffer_internal_1 (XBUFFER (w->buffer));
19973 /* Point refers normally to the selected window. For any
19974 other window, set up appropriate value. */
19975 if (!EQ (window, selected_window))
19977 struct text_pos pt;
19979 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
19980 if (CHARPOS (pt) < BEGV)
19981 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
19982 else if (CHARPOS (pt) > (ZV - 1))
19983 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
19984 else
19985 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
19988 /* Display mode lines. */
19989 clear_glyph_matrix (w->desired_matrix);
19990 if (display_mode_lines (w))
19992 ++nwindows;
19993 w->must_be_updated_p = 1;
19996 /* Restore old settings. */
19997 set_buffer_internal_1 (old);
19998 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20001 window = w->next;
20004 return nwindows;
20008 /* Display the mode and/or header line of window W. Value is the
20009 sum number of mode lines and header lines displayed. */
20011 static int
20012 display_mode_lines (struct window *w)
20014 Lisp_Object old_selected_window, old_selected_frame;
20015 int n = 0;
20017 old_selected_frame = selected_frame;
20018 selected_frame = w->frame;
20019 old_selected_window = selected_window;
20020 XSETWINDOW (selected_window, w);
20022 /* These will be set while the mode line specs are processed. */
20023 line_number_displayed = 0;
20024 w->column_number_displayed = Qnil;
20026 if (WINDOW_WANTS_MODELINE_P (w))
20028 struct window *sel_w = XWINDOW (old_selected_window);
20030 /* Select mode line face based on the real selected window. */
20031 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20032 BVAR (current_buffer, mode_line_format));
20033 ++n;
20036 if (WINDOW_WANTS_HEADER_LINE_P (w))
20038 display_mode_line (w, HEADER_LINE_FACE_ID,
20039 BVAR (current_buffer, header_line_format));
20040 ++n;
20043 selected_frame = old_selected_frame;
20044 selected_window = old_selected_window;
20045 return n;
20049 /* Display mode or header line of window W. FACE_ID specifies which
20050 line to display; it is either MODE_LINE_FACE_ID or
20051 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20052 display. Value is the pixel height of the mode/header line
20053 displayed. */
20055 static int
20056 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20058 struct it it;
20059 struct face *face;
20060 int count = SPECPDL_INDEX ();
20062 init_iterator (&it, w, -1, -1, NULL, face_id);
20063 /* Don't extend on a previously drawn mode-line.
20064 This may happen if called from pos_visible_p. */
20065 it.glyph_row->enabled_p = 0;
20066 prepare_desired_row (it.glyph_row);
20068 it.glyph_row->mode_line_p = 1;
20070 if (! mode_line_inverse_video)
20071 /* Force the mode-line to be displayed in the default face. */
20072 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20074 /* FIXME: This should be controlled by a user option. But
20075 supporting such an option is not trivial, since the mode line is
20076 made up of many separate strings. */
20077 it.paragraph_embedding = L2R;
20079 record_unwind_protect (unwind_format_mode_line,
20080 format_mode_line_unwind_data (NULL, Qnil, 0));
20082 mode_line_target = MODE_LINE_DISPLAY;
20084 /* Temporarily make frame's keyboard the current kboard so that
20085 kboard-local variables in the mode_line_format will get the right
20086 values. */
20087 push_kboard (FRAME_KBOARD (it.f));
20088 record_unwind_save_match_data ();
20089 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20090 pop_kboard ();
20092 unbind_to (count, Qnil);
20094 /* Fill up with spaces. */
20095 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20097 compute_line_metrics (&it);
20098 it.glyph_row->full_width_p = 1;
20099 it.glyph_row->continued_p = 0;
20100 it.glyph_row->truncated_on_left_p = 0;
20101 it.glyph_row->truncated_on_right_p = 0;
20103 /* Make a 3D mode-line have a shadow at its right end. */
20104 face = FACE_FROM_ID (it.f, face_id);
20105 extend_face_to_end_of_line (&it);
20106 if (face->box != FACE_NO_BOX)
20108 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20109 + it.glyph_row->used[TEXT_AREA] - 1);
20110 last->right_box_line_p = 1;
20113 return it.glyph_row->height;
20116 /* Move element ELT in LIST to the front of LIST.
20117 Return the updated list. */
20119 static Lisp_Object
20120 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20122 register Lisp_Object tail, prev;
20123 register Lisp_Object tem;
20125 tail = list;
20126 prev = Qnil;
20127 while (CONSP (tail))
20129 tem = XCAR (tail);
20131 if (EQ (elt, tem))
20133 /* Splice out the link TAIL. */
20134 if (NILP (prev))
20135 list = XCDR (tail);
20136 else
20137 Fsetcdr (prev, XCDR (tail));
20139 /* Now make it the first. */
20140 Fsetcdr (tail, list);
20141 return tail;
20143 else
20144 prev = tail;
20145 tail = XCDR (tail);
20146 QUIT;
20149 /* Not found--return unchanged LIST. */
20150 return list;
20153 /* Contribute ELT to the mode line for window IT->w. How it
20154 translates into text depends on its data type.
20156 IT describes the display environment in which we display, as usual.
20158 DEPTH is the depth in recursion. It is used to prevent
20159 infinite recursion here.
20161 FIELD_WIDTH is the number of characters the display of ELT should
20162 occupy in the mode line, and PRECISION is the maximum number of
20163 characters to display from ELT's representation. See
20164 display_string for details.
20166 Returns the hpos of the end of the text generated by ELT.
20168 PROPS is a property list to add to any string we encounter.
20170 If RISKY is nonzero, remove (disregard) any properties in any string
20171 we encounter, and ignore :eval and :propertize.
20173 The global variable `mode_line_target' determines whether the
20174 output is passed to `store_mode_line_noprop',
20175 `store_mode_line_string', or `display_string'. */
20177 static int
20178 display_mode_element (struct it *it, int depth, int field_width, int precision,
20179 Lisp_Object elt, Lisp_Object props, int risky)
20181 int n = 0, field, prec;
20182 int literal = 0;
20184 tail_recurse:
20185 if (depth > 100)
20186 elt = build_string ("*too-deep*");
20188 depth++;
20190 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
20192 case Lisp_String:
20194 /* A string: output it and check for %-constructs within it. */
20195 unsigned char c;
20196 EMACS_INT offset = 0;
20198 if (SCHARS (elt) > 0
20199 && (!NILP (props) || risky))
20201 Lisp_Object oprops, aelt;
20202 oprops = Ftext_properties_at (make_number (0), elt);
20204 /* If the starting string's properties are not what
20205 we want, translate the string. Also, if the string
20206 is risky, do that anyway. */
20208 if (NILP (Fequal (props, oprops)) || risky)
20210 /* If the starting string has properties,
20211 merge the specified ones onto the existing ones. */
20212 if (! NILP (oprops) && !risky)
20214 Lisp_Object tem;
20216 oprops = Fcopy_sequence (oprops);
20217 tem = props;
20218 while (CONSP (tem))
20220 oprops = Fplist_put (oprops, XCAR (tem),
20221 XCAR (XCDR (tem)));
20222 tem = XCDR (XCDR (tem));
20224 props = oprops;
20227 aelt = Fassoc (elt, mode_line_proptrans_alist);
20228 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20230 /* AELT is what we want. Move it to the front
20231 without consing. */
20232 elt = XCAR (aelt);
20233 mode_line_proptrans_alist
20234 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20236 else
20238 Lisp_Object tem;
20240 /* If AELT has the wrong props, it is useless.
20241 so get rid of it. */
20242 if (! NILP (aelt))
20243 mode_line_proptrans_alist
20244 = Fdelq (aelt, mode_line_proptrans_alist);
20246 elt = Fcopy_sequence (elt);
20247 Fset_text_properties (make_number (0), Flength (elt),
20248 props, elt);
20249 /* Add this item to mode_line_proptrans_alist. */
20250 mode_line_proptrans_alist
20251 = Fcons (Fcons (elt, props),
20252 mode_line_proptrans_alist);
20253 /* Truncate mode_line_proptrans_alist
20254 to at most 50 elements. */
20255 tem = Fnthcdr (make_number (50),
20256 mode_line_proptrans_alist);
20257 if (! NILP (tem))
20258 XSETCDR (tem, Qnil);
20263 offset = 0;
20265 if (literal)
20267 prec = precision - n;
20268 switch (mode_line_target)
20270 case MODE_LINE_NOPROP:
20271 case MODE_LINE_TITLE:
20272 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20273 break;
20274 case MODE_LINE_STRING:
20275 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20276 break;
20277 case MODE_LINE_DISPLAY:
20278 n += display_string (NULL, elt, Qnil, 0, 0, it,
20279 0, prec, 0, STRING_MULTIBYTE (elt));
20280 break;
20283 break;
20286 /* Handle the non-literal case. */
20288 while ((precision <= 0 || n < precision)
20289 && SREF (elt, offset) != 0
20290 && (mode_line_target != MODE_LINE_DISPLAY
20291 || it->current_x < it->last_visible_x))
20293 EMACS_INT last_offset = offset;
20295 /* Advance to end of string or next format specifier. */
20296 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20299 if (offset - 1 != last_offset)
20301 EMACS_INT nchars, nbytes;
20303 /* Output to end of string or up to '%'. Field width
20304 is length of string. Don't output more than
20305 PRECISION allows us. */
20306 offset--;
20308 prec = c_string_width (SDATA (elt) + last_offset,
20309 offset - last_offset, precision - n,
20310 &nchars, &nbytes);
20312 switch (mode_line_target)
20314 case MODE_LINE_NOPROP:
20315 case MODE_LINE_TITLE:
20316 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20317 break;
20318 case MODE_LINE_STRING:
20320 EMACS_INT bytepos = last_offset;
20321 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
20322 EMACS_INT endpos = (precision <= 0
20323 ? string_byte_to_char (elt, offset)
20324 : charpos + nchars);
20326 n += store_mode_line_string (NULL,
20327 Fsubstring (elt, make_number (charpos),
20328 make_number (endpos)),
20329 0, 0, 0, Qnil);
20331 break;
20332 case MODE_LINE_DISPLAY:
20334 EMACS_INT bytepos = last_offset;
20335 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
20337 if (precision <= 0)
20338 nchars = string_byte_to_char (elt, offset) - charpos;
20339 n += display_string (NULL, elt, Qnil, 0, charpos,
20340 it, 0, nchars, 0,
20341 STRING_MULTIBYTE (elt));
20343 break;
20346 else /* c == '%' */
20348 EMACS_INT percent_position = offset;
20350 /* Get the specified minimum width. Zero means
20351 don't pad. */
20352 field = 0;
20353 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20354 field = field * 10 + c - '0';
20356 /* Don't pad beyond the total padding allowed. */
20357 if (field_width - n > 0 && field > field_width - n)
20358 field = field_width - n;
20360 /* Note that either PRECISION <= 0 or N < PRECISION. */
20361 prec = precision - n;
20363 if (c == 'M')
20364 n += display_mode_element (it, depth, field, prec,
20365 Vglobal_mode_string, props,
20366 risky);
20367 else if (c != 0)
20369 int multibyte;
20370 EMACS_INT bytepos, charpos;
20371 const char *spec;
20372 Lisp_Object string;
20374 bytepos = percent_position;
20375 charpos = (STRING_MULTIBYTE (elt)
20376 ? string_byte_to_char (elt, bytepos)
20377 : bytepos);
20378 spec = decode_mode_spec (it->w, c, field, &string);
20379 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20381 switch (mode_line_target)
20383 case MODE_LINE_NOPROP:
20384 case MODE_LINE_TITLE:
20385 n += store_mode_line_noprop (spec, field, prec);
20386 break;
20387 case MODE_LINE_STRING:
20389 Lisp_Object tem = build_string (spec);
20390 props = Ftext_properties_at (make_number (charpos), elt);
20391 /* Should only keep face property in props */
20392 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20394 break;
20395 case MODE_LINE_DISPLAY:
20397 int nglyphs_before, nwritten;
20399 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20400 nwritten = display_string (spec, string, elt,
20401 charpos, 0, it,
20402 field, prec, 0,
20403 multibyte);
20405 /* Assign to the glyphs written above the
20406 string where the `%x' came from, position
20407 of the `%'. */
20408 if (nwritten > 0)
20410 struct glyph *glyph
20411 = (it->glyph_row->glyphs[TEXT_AREA]
20412 + nglyphs_before);
20413 int i;
20415 for (i = 0; i < nwritten; ++i)
20417 glyph[i].object = elt;
20418 glyph[i].charpos = charpos;
20421 n += nwritten;
20424 break;
20427 else /* c == 0 */
20428 break;
20432 break;
20434 case Lisp_Symbol:
20435 /* A symbol: process the value of the symbol recursively
20436 as if it appeared here directly. Avoid error if symbol void.
20437 Special case: if value of symbol is a string, output the string
20438 literally. */
20440 register Lisp_Object tem;
20442 /* If the variable is not marked as risky to set
20443 then its contents are risky to use. */
20444 if (NILP (Fget (elt, Qrisky_local_variable)))
20445 risky = 1;
20447 tem = Fboundp (elt);
20448 if (!NILP (tem))
20450 tem = Fsymbol_value (elt);
20451 /* If value is a string, output that string literally:
20452 don't check for % within it. */
20453 if (STRINGP (tem))
20454 literal = 1;
20456 if (!EQ (tem, elt))
20458 /* Give up right away for nil or t. */
20459 elt = tem;
20460 goto tail_recurse;
20464 break;
20466 case Lisp_Cons:
20468 register Lisp_Object car, tem;
20470 /* A cons cell: five distinct cases.
20471 If first element is :eval or :propertize, do something special.
20472 If first element is a string or a cons, process all the elements
20473 and effectively concatenate them.
20474 If first element is a negative number, truncate displaying cdr to
20475 at most that many characters. If positive, pad (with spaces)
20476 to at least that many characters.
20477 If first element is a symbol, process the cadr or caddr recursively
20478 according to whether the symbol's value is non-nil or nil. */
20479 car = XCAR (elt);
20480 if (EQ (car, QCeval))
20482 /* An element of the form (:eval FORM) means evaluate FORM
20483 and use the result as mode line elements. */
20485 if (risky)
20486 break;
20488 if (CONSP (XCDR (elt)))
20490 Lisp_Object spec;
20491 spec = safe_eval (XCAR (XCDR (elt)));
20492 n += display_mode_element (it, depth, field_width - n,
20493 precision - n, spec, props,
20494 risky);
20497 else if (EQ (car, QCpropertize))
20499 /* An element of the form (:propertize ELT PROPS...)
20500 means display ELT but applying properties PROPS. */
20502 if (risky)
20503 break;
20505 if (CONSP (XCDR (elt)))
20506 n += display_mode_element (it, depth, field_width - n,
20507 precision - n, XCAR (XCDR (elt)),
20508 XCDR (XCDR (elt)), risky);
20510 else if (SYMBOLP (car))
20512 tem = Fboundp (car);
20513 elt = XCDR (elt);
20514 if (!CONSP (elt))
20515 goto invalid;
20516 /* elt is now the cdr, and we know it is a cons cell.
20517 Use its car if CAR has a non-nil value. */
20518 if (!NILP (tem))
20520 tem = Fsymbol_value (car);
20521 if (!NILP (tem))
20523 elt = XCAR (elt);
20524 goto tail_recurse;
20527 /* Symbol's value is nil (or symbol is unbound)
20528 Get the cddr of the original list
20529 and if possible find the caddr and use that. */
20530 elt = XCDR (elt);
20531 if (NILP (elt))
20532 break;
20533 else if (!CONSP (elt))
20534 goto invalid;
20535 elt = XCAR (elt);
20536 goto tail_recurse;
20538 else if (INTEGERP (car))
20540 register int lim = XINT (car);
20541 elt = XCDR (elt);
20542 if (lim < 0)
20544 /* Negative int means reduce maximum width. */
20545 if (precision <= 0)
20546 precision = -lim;
20547 else
20548 precision = min (precision, -lim);
20550 else if (lim > 0)
20552 /* Padding specified. Don't let it be more than
20553 current maximum. */
20554 if (precision > 0)
20555 lim = min (precision, lim);
20557 /* If that's more padding than already wanted, queue it.
20558 But don't reduce padding already specified even if
20559 that is beyond the current truncation point. */
20560 field_width = max (lim, field_width);
20562 goto tail_recurse;
20564 else if (STRINGP (car) || CONSP (car))
20566 Lisp_Object halftail = elt;
20567 int len = 0;
20569 while (CONSP (elt)
20570 && (precision <= 0 || n < precision))
20572 n += display_mode_element (it, depth,
20573 /* Do padding only after the last
20574 element in the list. */
20575 (! CONSP (XCDR (elt))
20576 ? field_width - n
20577 : 0),
20578 precision - n, XCAR (elt),
20579 props, risky);
20580 elt = XCDR (elt);
20581 len++;
20582 if ((len & 1) == 0)
20583 halftail = XCDR (halftail);
20584 /* Check for cycle. */
20585 if (EQ (halftail, elt))
20586 break;
20590 break;
20592 default:
20593 invalid:
20594 elt = build_string ("*invalid*");
20595 goto tail_recurse;
20598 /* Pad to FIELD_WIDTH. */
20599 if (field_width > 0 && n < field_width)
20601 switch (mode_line_target)
20603 case MODE_LINE_NOPROP:
20604 case MODE_LINE_TITLE:
20605 n += store_mode_line_noprop ("", field_width - n, 0);
20606 break;
20607 case MODE_LINE_STRING:
20608 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20609 break;
20610 case MODE_LINE_DISPLAY:
20611 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20612 0, 0, 0);
20613 break;
20617 return n;
20620 /* Store a mode-line string element in mode_line_string_list.
20622 If STRING is non-null, display that C string. Otherwise, the Lisp
20623 string LISP_STRING is displayed.
20625 FIELD_WIDTH is the minimum number of output glyphs to produce.
20626 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20627 with spaces. FIELD_WIDTH <= 0 means don't pad.
20629 PRECISION is the maximum number of characters to output from
20630 STRING. PRECISION <= 0 means don't truncate the string.
20632 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20633 properties to the string.
20635 PROPS are the properties to add to the string.
20636 The mode_line_string_face face property is always added to the string.
20639 static int
20640 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20641 int field_width, int precision, Lisp_Object props)
20643 EMACS_INT len;
20644 int n = 0;
20646 if (string != NULL)
20648 len = strlen (string);
20649 if (precision > 0 && len > precision)
20650 len = precision;
20651 lisp_string = make_string (string, len);
20652 if (NILP (props))
20653 props = mode_line_string_face_prop;
20654 else if (!NILP (mode_line_string_face))
20656 Lisp_Object face = Fplist_get (props, Qface);
20657 props = Fcopy_sequence (props);
20658 if (NILP (face))
20659 face = mode_line_string_face;
20660 else
20661 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20662 props = Fplist_put (props, Qface, face);
20664 Fadd_text_properties (make_number (0), make_number (len),
20665 props, lisp_string);
20667 else
20669 len = XFASTINT (Flength (lisp_string));
20670 if (precision > 0 && len > precision)
20672 len = precision;
20673 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20674 precision = -1;
20676 if (!NILP (mode_line_string_face))
20678 Lisp_Object face;
20679 if (NILP (props))
20680 props = Ftext_properties_at (make_number (0), lisp_string);
20681 face = Fplist_get (props, Qface);
20682 if (NILP (face))
20683 face = mode_line_string_face;
20684 else
20685 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20686 props = Fcons (Qface, Fcons (face, Qnil));
20687 if (copy_string)
20688 lisp_string = Fcopy_sequence (lisp_string);
20690 if (!NILP (props))
20691 Fadd_text_properties (make_number (0), make_number (len),
20692 props, lisp_string);
20695 if (len > 0)
20697 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20698 n += len;
20701 if (field_width > len)
20703 field_width -= len;
20704 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20705 if (!NILP (props))
20706 Fadd_text_properties (make_number (0), make_number (field_width),
20707 props, lisp_string);
20708 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20709 n += field_width;
20712 return n;
20716 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20717 1, 4, 0,
20718 doc: /* Format a string out of a mode line format specification.
20719 First arg FORMAT specifies the mode line format (see `mode-line-format'
20720 for details) to use.
20722 By default, the format is evaluated for the currently selected window.
20724 Optional second arg FACE specifies the face property to put on all
20725 characters for which no face is specified. The value nil means the
20726 default face. The value t means whatever face the window's mode line
20727 currently uses (either `mode-line' or `mode-line-inactive',
20728 depending on whether the window is the selected window or not).
20729 An integer value means the value string has no text
20730 properties.
20732 Optional third and fourth args WINDOW and BUFFER specify the window
20733 and buffer to use as the context for the formatting (defaults
20734 are the selected window and the WINDOW's buffer). */)
20735 (Lisp_Object format, Lisp_Object face,
20736 Lisp_Object window, Lisp_Object buffer)
20738 struct it it;
20739 int len;
20740 struct window *w;
20741 struct buffer *old_buffer = NULL;
20742 int face_id;
20743 int no_props = INTEGERP (face);
20744 int count = SPECPDL_INDEX ();
20745 Lisp_Object str;
20746 int string_start = 0;
20748 if (NILP (window))
20749 window = selected_window;
20750 CHECK_WINDOW (window);
20751 w = XWINDOW (window);
20753 if (NILP (buffer))
20754 buffer = w->buffer;
20755 CHECK_BUFFER (buffer);
20757 /* Make formatting the modeline a non-op when noninteractive, otherwise
20758 there will be problems later caused by a partially initialized frame. */
20759 if (NILP (format) || noninteractive)
20760 return empty_unibyte_string;
20762 if (no_props)
20763 face = Qnil;
20765 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20766 : EQ (face, Qt) ? (EQ (window, selected_window)
20767 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20768 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20769 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20770 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20771 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20772 : DEFAULT_FACE_ID;
20774 if (XBUFFER (buffer) != current_buffer)
20775 old_buffer = current_buffer;
20777 /* Save things including mode_line_proptrans_alist,
20778 and set that to nil so that we don't alter the outer value. */
20779 record_unwind_protect (unwind_format_mode_line,
20780 format_mode_line_unwind_data
20781 (old_buffer, selected_window, 1));
20782 mode_line_proptrans_alist = Qnil;
20784 Fselect_window (window, Qt);
20785 if (old_buffer)
20786 set_buffer_internal_1 (XBUFFER (buffer));
20788 init_iterator (&it, w, -1, -1, NULL, face_id);
20790 if (no_props)
20792 mode_line_target = MODE_LINE_NOPROP;
20793 mode_line_string_face_prop = Qnil;
20794 mode_line_string_list = Qnil;
20795 string_start = MODE_LINE_NOPROP_LEN (0);
20797 else
20799 mode_line_target = MODE_LINE_STRING;
20800 mode_line_string_list = Qnil;
20801 mode_line_string_face = face;
20802 mode_line_string_face_prop
20803 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20806 push_kboard (FRAME_KBOARD (it.f));
20807 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20808 pop_kboard ();
20810 if (no_props)
20812 len = MODE_LINE_NOPROP_LEN (string_start);
20813 str = make_string (mode_line_noprop_buf + string_start, len);
20815 else
20817 mode_line_string_list = Fnreverse (mode_line_string_list);
20818 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20819 empty_unibyte_string);
20822 unbind_to (count, Qnil);
20823 return str;
20826 /* Write a null-terminated, right justified decimal representation of
20827 the positive integer D to BUF using a minimal field width WIDTH. */
20829 static void
20830 pint2str (register char *buf, register int width, register EMACS_INT d)
20832 register char *p = buf;
20834 if (d <= 0)
20835 *p++ = '0';
20836 else
20838 while (d > 0)
20840 *p++ = d % 10 + '0';
20841 d /= 10;
20845 for (width -= (int) (p - buf); width > 0; --width)
20846 *p++ = ' ';
20847 *p-- = '\0';
20848 while (p > buf)
20850 d = *buf;
20851 *buf++ = *p;
20852 *p-- = d;
20856 /* Write a null-terminated, right justified decimal and "human
20857 readable" representation of the nonnegative integer D to BUF using
20858 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20860 static const char power_letter[] =
20862 0, /* no letter */
20863 'k', /* kilo */
20864 'M', /* mega */
20865 'G', /* giga */
20866 'T', /* tera */
20867 'P', /* peta */
20868 'E', /* exa */
20869 'Z', /* zetta */
20870 'Y' /* yotta */
20873 static void
20874 pint2hrstr (char *buf, int width, EMACS_INT d)
20876 /* We aim to represent the nonnegative integer D as
20877 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
20878 EMACS_INT quotient = d;
20879 int remainder = 0;
20880 /* -1 means: do not use TENTHS. */
20881 int tenths = -1;
20882 int exponent = 0;
20884 /* Length of QUOTIENT.TENTHS as a string. */
20885 int length;
20887 char * psuffix;
20888 char * p;
20890 if (1000 <= quotient)
20892 /* Scale to the appropriate EXPONENT. */
20895 remainder = quotient % 1000;
20896 quotient /= 1000;
20897 exponent++;
20899 while (1000 <= quotient);
20901 /* Round to nearest and decide whether to use TENTHS or not. */
20902 if (quotient <= 9)
20904 tenths = remainder / 100;
20905 if (50 <= remainder % 100)
20907 if (tenths < 9)
20908 tenths++;
20909 else
20911 quotient++;
20912 if (quotient == 10)
20913 tenths = -1;
20914 else
20915 tenths = 0;
20919 else
20920 if (500 <= remainder)
20922 if (quotient < 999)
20923 quotient++;
20924 else
20926 quotient = 1;
20927 exponent++;
20928 tenths = 0;
20933 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
20934 if (tenths == -1 && quotient <= 99)
20935 if (quotient <= 9)
20936 length = 1;
20937 else
20938 length = 2;
20939 else
20940 length = 3;
20941 p = psuffix = buf + max (width, length);
20943 /* Print EXPONENT. */
20944 *psuffix++ = power_letter[exponent];
20945 *psuffix = '\0';
20947 /* Print TENTHS. */
20948 if (tenths >= 0)
20950 *--p = '0' + tenths;
20951 *--p = '.';
20954 /* Print QUOTIENT. */
20957 int digit = quotient % 10;
20958 *--p = '0' + digit;
20960 while ((quotient /= 10) != 0);
20962 /* Print leading spaces. */
20963 while (buf < p)
20964 *--p = ' ';
20967 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
20968 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
20969 type of CODING_SYSTEM. Return updated pointer into BUF. */
20971 static unsigned char invalid_eol_type[] = "(*invalid*)";
20973 static char *
20974 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
20976 Lisp_Object val;
20977 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
20978 const unsigned char *eol_str;
20979 int eol_str_len;
20980 /* The EOL conversion we are using. */
20981 Lisp_Object eoltype;
20983 val = CODING_SYSTEM_SPEC (coding_system);
20984 eoltype = Qnil;
20986 if (!VECTORP (val)) /* Not yet decided. */
20988 if (multibyte)
20989 *buf++ = '-';
20990 if (eol_flag)
20991 eoltype = eol_mnemonic_undecided;
20992 /* Don't mention EOL conversion if it isn't decided. */
20994 else
20996 Lisp_Object attrs;
20997 Lisp_Object eolvalue;
20999 attrs = AREF (val, 0);
21000 eolvalue = AREF (val, 2);
21002 if (multibyte)
21003 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
21005 if (eol_flag)
21007 /* The EOL conversion that is normal on this system. */
21009 if (NILP (eolvalue)) /* Not yet decided. */
21010 eoltype = eol_mnemonic_undecided;
21011 else if (VECTORP (eolvalue)) /* Not yet decided. */
21012 eoltype = eol_mnemonic_undecided;
21013 else /* eolvalue is Qunix, Qdos, or Qmac. */
21014 eoltype = (EQ (eolvalue, Qunix)
21015 ? eol_mnemonic_unix
21016 : (EQ (eolvalue, Qdos) == 1
21017 ? eol_mnemonic_dos : eol_mnemonic_mac));
21021 if (eol_flag)
21023 /* Mention the EOL conversion if it is not the usual one. */
21024 if (STRINGP (eoltype))
21026 eol_str = SDATA (eoltype);
21027 eol_str_len = SBYTES (eoltype);
21029 else if (CHARACTERP (eoltype))
21031 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
21032 int c = XFASTINT (eoltype);
21033 eol_str_len = CHAR_STRING (c, tmp);
21034 eol_str = tmp;
21036 else
21038 eol_str = invalid_eol_type;
21039 eol_str_len = sizeof (invalid_eol_type) - 1;
21041 memcpy (buf, eol_str, eol_str_len);
21042 buf += eol_str_len;
21045 return buf;
21048 /* Return a string for the output of a mode line %-spec for window W,
21049 generated by character C. FIELD_WIDTH > 0 means pad the string
21050 returned with spaces to that value. Return a Lisp string in
21051 *STRING if the resulting string is taken from that Lisp string.
21053 Note we operate on the current buffer for most purposes,
21054 the exception being w->base_line_pos. */
21056 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21058 static const char *
21059 decode_mode_spec (struct window *w, register int c, int field_width,
21060 Lisp_Object *string)
21062 Lisp_Object obj;
21063 struct frame *f = XFRAME (WINDOW_FRAME (w));
21064 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21065 struct buffer *b = current_buffer;
21067 obj = Qnil;
21068 *string = Qnil;
21070 switch (c)
21072 case '*':
21073 if (!NILP (BVAR (b, read_only)))
21074 return "%";
21075 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21076 return "*";
21077 return "-";
21079 case '+':
21080 /* This differs from %* only for a modified read-only buffer. */
21081 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21082 return "*";
21083 if (!NILP (BVAR (b, read_only)))
21084 return "%";
21085 return "-";
21087 case '&':
21088 /* This differs from %* in ignoring read-only-ness. */
21089 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21090 return "*";
21091 return "-";
21093 case '%':
21094 return "%";
21096 case '[':
21098 int i;
21099 char *p;
21101 if (command_loop_level > 5)
21102 return "[[[... ";
21103 p = decode_mode_spec_buf;
21104 for (i = 0; i < command_loop_level; i++)
21105 *p++ = '[';
21106 *p = 0;
21107 return decode_mode_spec_buf;
21110 case ']':
21112 int i;
21113 char *p;
21115 if (command_loop_level > 5)
21116 return " ...]]]";
21117 p = decode_mode_spec_buf;
21118 for (i = 0; i < command_loop_level; i++)
21119 *p++ = ']';
21120 *p = 0;
21121 return decode_mode_spec_buf;
21124 case '-':
21126 register int i;
21128 /* Let lots_of_dashes be a string of infinite length. */
21129 if (mode_line_target == MODE_LINE_NOPROP ||
21130 mode_line_target == MODE_LINE_STRING)
21131 return "--";
21132 if (field_width <= 0
21133 || field_width > sizeof (lots_of_dashes))
21135 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21136 decode_mode_spec_buf[i] = '-';
21137 decode_mode_spec_buf[i] = '\0';
21138 return decode_mode_spec_buf;
21140 else
21141 return lots_of_dashes;
21144 case 'b':
21145 obj = BVAR (b, name);
21146 break;
21148 case 'c':
21149 /* %c and %l are ignored in `frame-title-format'.
21150 (In redisplay_internal, the frame title is drawn _before_ the
21151 windows are updated, so the stuff which depends on actual
21152 window contents (such as %l) may fail to render properly, or
21153 even crash emacs.) */
21154 if (mode_line_target == MODE_LINE_TITLE)
21155 return "";
21156 else
21158 EMACS_INT col = current_column ();
21159 w->column_number_displayed = make_number (col);
21160 pint2str (decode_mode_spec_buf, field_width, col);
21161 return decode_mode_spec_buf;
21164 case 'e':
21165 #ifndef SYSTEM_MALLOC
21167 if (NILP (Vmemory_full))
21168 return "";
21169 else
21170 return "!MEM FULL! ";
21172 #else
21173 return "";
21174 #endif
21176 case 'F':
21177 /* %F displays the frame name. */
21178 if (!NILP (f->title))
21179 return SSDATA (f->title);
21180 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21181 return SSDATA (f->name);
21182 return "Emacs";
21184 case 'f':
21185 obj = BVAR (b, filename);
21186 break;
21188 case 'i':
21190 EMACS_INT size = ZV - BEGV;
21191 pint2str (decode_mode_spec_buf, field_width, size);
21192 return decode_mode_spec_buf;
21195 case 'I':
21197 EMACS_INT size = ZV - BEGV;
21198 pint2hrstr (decode_mode_spec_buf, field_width, size);
21199 return decode_mode_spec_buf;
21202 case 'l':
21204 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
21205 EMACS_INT topline, nlines, height;
21206 EMACS_INT junk;
21208 /* %c and %l are ignored in `frame-title-format'. */
21209 if (mode_line_target == MODE_LINE_TITLE)
21210 return "";
21212 startpos = XMARKER (w->start)->charpos;
21213 startpos_byte = marker_byte_position (w->start);
21214 height = WINDOW_TOTAL_LINES (w);
21216 /* If we decided that this buffer isn't suitable for line numbers,
21217 don't forget that too fast. */
21218 if (EQ (w->base_line_pos, w->buffer))
21219 goto no_value;
21220 /* But do forget it, if the window shows a different buffer now. */
21221 else if (BUFFERP (w->base_line_pos))
21222 w->base_line_pos = Qnil;
21224 /* If the buffer is very big, don't waste time. */
21225 if (INTEGERP (Vline_number_display_limit)
21226 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21228 w->base_line_pos = Qnil;
21229 w->base_line_number = Qnil;
21230 goto no_value;
21233 if (INTEGERP (w->base_line_number)
21234 && INTEGERP (w->base_line_pos)
21235 && XFASTINT (w->base_line_pos) <= startpos)
21237 line = XFASTINT (w->base_line_number);
21238 linepos = XFASTINT (w->base_line_pos);
21239 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21241 else
21243 line = 1;
21244 linepos = BUF_BEGV (b);
21245 linepos_byte = BUF_BEGV_BYTE (b);
21248 /* Count lines from base line to window start position. */
21249 nlines = display_count_lines (linepos_byte,
21250 startpos_byte,
21251 startpos, &junk);
21253 topline = nlines + line;
21255 /* Determine a new base line, if the old one is too close
21256 or too far away, or if we did not have one.
21257 "Too close" means it's plausible a scroll-down would
21258 go back past it. */
21259 if (startpos == BUF_BEGV (b))
21261 w->base_line_number = make_number (topline);
21262 w->base_line_pos = make_number (BUF_BEGV (b));
21264 else if (nlines < height + 25 || nlines > height * 3 + 50
21265 || linepos == BUF_BEGV (b))
21267 EMACS_INT limit = BUF_BEGV (b);
21268 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
21269 EMACS_INT position;
21270 EMACS_INT distance =
21271 (height * 2 + 30) * line_number_display_limit_width;
21273 if (startpos - distance > limit)
21275 limit = startpos - distance;
21276 limit_byte = CHAR_TO_BYTE (limit);
21279 nlines = display_count_lines (startpos_byte,
21280 limit_byte,
21281 - (height * 2 + 30),
21282 &position);
21283 /* If we couldn't find the lines we wanted within
21284 line_number_display_limit_width chars per line,
21285 give up on line numbers for this window. */
21286 if (position == limit_byte && limit == startpos - distance)
21288 w->base_line_pos = w->buffer;
21289 w->base_line_number = Qnil;
21290 goto no_value;
21293 w->base_line_number = make_number (topline - nlines);
21294 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
21297 /* Now count lines from the start pos to point. */
21298 nlines = display_count_lines (startpos_byte,
21299 PT_BYTE, PT, &junk);
21301 /* Record that we did display the line number. */
21302 line_number_displayed = 1;
21304 /* Make the string to show. */
21305 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21306 return decode_mode_spec_buf;
21307 no_value:
21309 char* p = decode_mode_spec_buf;
21310 int pad = field_width - 2;
21311 while (pad-- > 0)
21312 *p++ = ' ';
21313 *p++ = '?';
21314 *p++ = '?';
21315 *p = '\0';
21316 return decode_mode_spec_buf;
21319 break;
21321 case 'm':
21322 obj = BVAR (b, mode_name);
21323 break;
21325 case 'n':
21326 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21327 return " Narrow";
21328 break;
21330 case 'p':
21332 EMACS_INT pos = marker_position (w->start);
21333 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
21335 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21337 if (pos <= BUF_BEGV (b))
21338 return "All";
21339 else
21340 return "Bottom";
21342 else if (pos <= BUF_BEGV (b))
21343 return "Top";
21344 else
21346 if (total > 1000000)
21347 /* Do it differently for a large value, to avoid overflow. */
21348 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21349 else
21350 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21351 /* We can't normally display a 3-digit number,
21352 so get us a 2-digit number that is close. */
21353 if (total == 100)
21354 total = 99;
21355 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
21356 return decode_mode_spec_buf;
21360 /* Display percentage of size above the bottom of the screen. */
21361 case 'P':
21363 EMACS_INT toppos = marker_position (w->start);
21364 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21365 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
21367 if (botpos >= BUF_ZV (b))
21369 if (toppos <= BUF_BEGV (b))
21370 return "All";
21371 else
21372 return "Bottom";
21374 else
21376 if (total > 1000000)
21377 /* Do it differently for a large value, to avoid overflow. */
21378 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21379 else
21380 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21381 /* We can't normally display a 3-digit number,
21382 so get us a 2-digit number that is close. */
21383 if (total == 100)
21384 total = 99;
21385 if (toppos <= BUF_BEGV (b))
21386 sprintf (decode_mode_spec_buf, "Top%2"pI"d%%", total);
21387 else
21388 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
21389 return decode_mode_spec_buf;
21393 case 's':
21394 /* status of process */
21395 obj = Fget_buffer_process (Fcurrent_buffer ());
21396 if (NILP (obj))
21397 return "no process";
21398 #ifndef MSDOS
21399 obj = Fsymbol_name (Fprocess_status (obj));
21400 #endif
21401 break;
21403 case '@':
21405 int count = inhibit_garbage_collection ();
21406 Lisp_Object val = call1 (intern ("file-remote-p"),
21407 BVAR (current_buffer, directory));
21408 unbind_to (count, Qnil);
21410 if (NILP (val))
21411 return "-";
21412 else
21413 return "@";
21416 case 't': /* indicate TEXT or BINARY */
21417 return "T";
21419 case 'z':
21420 /* coding-system (not including end-of-line format) */
21421 case 'Z':
21422 /* coding-system (including end-of-line type) */
21424 int eol_flag = (c == 'Z');
21425 char *p = decode_mode_spec_buf;
21427 if (! FRAME_WINDOW_P (f))
21429 /* No need to mention EOL here--the terminal never needs
21430 to do EOL conversion. */
21431 p = decode_mode_spec_coding (CODING_ID_NAME
21432 (FRAME_KEYBOARD_CODING (f)->id),
21433 p, 0);
21434 p = decode_mode_spec_coding (CODING_ID_NAME
21435 (FRAME_TERMINAL_CODING (f)->id),
21436 p, 0);
21438 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21439 p, eol_flag);
21441 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21442 #ifdef subprocesses
21443 obj = Fget_buffer_process (Fcurrent_buffer ());
21444 if (PROCESSP (obj))
21446 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
21447 p, eol_flag);
21448 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
21449 p, eol_flag);
21451 #endif /* subprocesses */
21452 #endif /* 0 */
21453 *p = 0;
21454 return decode_mode_spec_buf;
21458 if (STRINGP (obj))
21460 *string = obj;
21461 return SSDATA (obj);
21463 else
21464 return "";
21468 /* Count up to COUNT lines starting from START_BYTE.
21469 But don't go beyond LIMIT_BYTE.
21470 Return the number of lines thus found (always nonnegative).
21472 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21474 static EMACS_INT
21475 display_count_lines (EMACS_INT start_byte,
21476 EMACS_INT limit_byte, EMACS_INT count,
21477 EMACS_INT *byte_pos_ptr)
21479 register unsigned char *cursor;
21480 unsigned char *base;
21482 register EMACS_INT ceiling;
21483 register unsigned char *ceiling_addr;
21484 EMACS_INT orig_count = count;
21486 /* If we are not in selective display mode,
21487 check only for newlines. */
21488 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21489 && !INTEGERP (BVAR (current_buffer, selective_display)));
21491 if (count > 0)
21493 while (start_byte < limit_byte)
21495 ceiling = BUFFER_CEILING_OF (start_byte);
21496 ceiling = min (limit_byte - 1, ceiling);
21497 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21498 base = (cursor = BYTE_POS_ADDR (start_byte));
21499 while (1)
21501 if (selective_display)
21502 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21504 else
21505 while (*cursor != '\n' && ++cursor != ceiling_addr)
21508 if (cursor != ceiling_addr)
21510 if (--count == 0)
21512 start_byte += cursor - base + 1;
21513 *byte_pos_ptr = start_byte;
21514 return orig_count;
21516 else
21517 if (++cursor == ceiling_addr)
21518 break;
21520 else
21521 break;
21523 start_byte += cursor - base;
21526 else
21528 while (start_byte > limit_byte)
21530 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21531 ceiling = max (limit_byte, ceiling);
21532 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21533 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21534 while (1)
21536 if (selective_display)
21537 while (--cursor != ceiling_addr
21538 && *cursor != '\n' && *cursor != 015)
21540 else
21541 while (--cursor != ceiling_addr && *cursor != '\n')
21544 if (cursor != ceiling_addr)
21546 if (++count == 0)
21548 start_byte += cursor - base + 1;
21549 *byte_pos_ptr = start_byte;
21550 /* When scanning backwards, we should
21551 not count the newline posterior to which we stop. */
21552 return - orig_count - 1;
21555 else
21556 break;
21558 /* Here we add 1 to compensate for the last decrement
21559 of CURSOR, which took it past the valid range. */
21560 start_byte += cursor - base + 1;
21564 *byte_pos_ptr = limit_byte;
21566 if (count < 0)
21567 return - orig_count + count;
21568 return orig_count - count;
21574 /***********************************************************************
21575 Displaying strings
21576 ***********************************************************************/
21578 /* Display a NUL-terminated string, starting with index START.
21580 If STRING is non-null, display that C string. Otherwise, the Lisp
21581 string LISP_STRING is displayed. There's a case that STRING is
21582 non-null and LISP_STRING is not nil. It means STRING is a string
21583 data of LISP_STRING. In that case, we display LISP_STRING while
21584 ignoring its text properties.
21586 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21587 FACE_STRING. Display STRING or LISP_STRING with the face at
21588 FACE_STRING_POS in FACE_STRING:
21590 Display the string in the environment given by IT, but use the
21591 standard display table, temporarily.
21593 FIELD_WIDTH is the minimum number of output glyphs to produce.
21594 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21595 with spaces. If STRING has more characters, more than FIELD_WIDTH
21596 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21598 PRECISION is the maximum number of characters to output from
21599 STRING. PRECISION < 0 means don't truncate the string.
21601 This is roughly equivalent to printf format specifiers:
21603 FIELD_WIDTH PRECISION PRINTF
21604 ----------------------------------------
21605 -1 -1 %s
21606 -1 10 %.10s
21607 10 -1 %10s
21608 20 10 %20.10s
21610 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21611 display them, and < 0 means obey the current buffer's value of
21612 enable_multibyte_characters.
21614 Value is the number of columns displayed. */
21616 static int
21617 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21618 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
21619 int field_width, int precision, int max_x, int multibyte)
21621 int hpos_at_start = it->hpos;
21622 int saved_face_id = it->face_id;
21623 struct glyph_row *row = it->glyph_row;
21624 EMACS_INT it_charpos;
21626 /* Initialize the iterator IT for iteration over STRING beginning
21627 with index START. */
21628 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21629 precision, field_width, multibyte);
21630 if (string && STRINGP (lisp_string))
21631 /* LISP_STRING is the one returned by decode_mode_spec. We should
21632 ignore its text properties. */
21633 it->stop_charpos = it->end_charpos;
21635 /* If displaying STRING, set up the face of the iterator from
21636 FACE_STRING, if that's given. */
21637 if (STRINGP (face_string))
21639 EMACS_INT endptr;
21640 struct face *face;
21642 it->face_id
21643 = face_at_string_position (it->w, face_string, face_string_pos,
21644 0, it->region_beg_charpos,
21645 it->region_end_charpos,
21646 &endptr, it->base_face_id, 0);
21647 face = FACE_FROM_ID (it->f, it->face_id);
21648 it->face_box_p = face->box != FACE_NO_BOX;
21651 /* Set max_x to the maximum allowed X position. Don't let it go
21652 beyond the right edge of the window. */
21653 if (max_x <= 0)
21654 max_x = it->last_visible_x;
21655 else
21656 max_x = min (max_x, it->last_visible_x);
21658 /* Skip over display elements that are not visible. because IT->w is
21659 hscrolled. */
21660 if (it->current_x < it->first_visible_x)
21661 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21662 MOVE_TO_POS | MOVE_TO_X);
21664 row->ascent = it->max_ascent;
21665 row->height = it->max_ascent + it->max_descent;
21666 row->phys_ascent = it->max_phys_ascent;
21667 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21668 row->extra_line_spacing = it->max_extra_line_spacing;
21670 if (STRINGP (it->string))
21671 it_charpos = IT_STRING_CHARPOS (*it);
21672 else
21673 it_charpos = IT_CHARPOS (*it);
21675 /* This condition is for the case that we are called with current_x
21676 past last_visible_x. */
21677 while (it->current_x < max_x)
21679 int x_before, x, n_glyphs_before, i, nglyphs;
21681 /* Get the next display element. */
21682 if (!get_next_display_element (it))
21683 break;
21685 /* Produce glyphs. */
21686 x_before = it->current_x;
21687 n_glyphs_before = row->used[TEXT_AREA];
21688 PRODUCE_GLYPHS (it);
21690 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21691 i = 0;
21692 x = x_before;
21693 while (i < nglyphs)
21695 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21697 if (it->line_wrap != TRUNCATE
21698 && x + glyph->pixel_width > max_x)
21700 /* End of continued line or max_x reached. */
21701 if (CHAR_GLYPH_PADDING_P (*glyph))
21703 /* A wide character is unbreakable. */
21704 if (row->reversed_p)
21705 unproduce_glyphs (it, row->used[TEXT_AREA]
21706 - n_glyphs_before);
21707 row->used[TEXT_AREA] = n_glyphs_before;
21708 it->current_x = x_before;
21710 else
21712 if (row->reversed_p)
21713 unproduce_glyphs (it, row->used[TEXT_AREA]
21714 - (n_glyphs_before + i));
21715 row->used[TEXT_AREA] = n_glyphs_before + i;
21716 it->current_x = x;
21718 break;
21720 else if (x + glyph->pixel_width >= it->first_visible_x)
21722 /* Glyph is at least partially visible. */
21723 ++it->hpos;
21724 if (x < it->first_visible_x)
21725 row->x = x - it->first_visible_x;
21727 else
21729 /* Glyph is off the left margin of the display area.
21730 Should not happen. */
21731 abort ();
21734 row->ascent = max (row->ascent, it->max_ascent);
21735 row->height = max (row->height, it->max_ascent + it->max_descent);
21736 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21737 row->phys_height = max (row->phys_height,
21738 it->max_phys_ascent + it->max_phys_descent);
21739 row->extra_line_spacing = max (row->extra_line_spacing,
21740 it->max_extra_line_spacing);
21741 x += glyph->pixel_width;
21742 ++i;
21745 /* Stop if max_x reached. */
21746 if (i < nglyphs)
21747 break;
21749 /* Stop at line ends. */
21750 if (ITERATOR_AT_END_OF_LINE_P (it))
21752 it->continuation_lines_width = 0;
21753 break;
21756 set_iterator_to_next (it, 1);
21757 if (STRINGP (it->string))
21758 it_charpos = IT_STRING_CHARPOS (*it);
21759 else
21760 it_charpos = IT_CHARPOS (*it);
21762 /* Stop if truncating at the right edge. */
21763 if (it->line_wrap == TRUNCATE
21764 && it->current_x >= it->last_visible_x)
21766 /* Add truncation mark, but don't do it if the line is
21767 truncated at a padding space. */
21768 if (it_charpos < it->string_nchars)
21770 if (!FRAME_WINDOW_P (it->f))
21772 int ii, n;
21774 if (it->current_x > it->last_visible_x)
21776 if (!row->reversed_p)
21778 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21779 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21780 break;
21782 else
21784 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21785 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21786 break;
21787 unproduce_glyphs (it, ii + 1);
21788 ii = row->used[TEXT_AREA] - (ii + 1);
21790 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21792 row->used[TEXT_AREA] = ii;
21793 produce_special_glyphs (it, IT_TRUNCATION);
21796 produce_special_glyphs (it, IT_TRUNCATION);
21798 row->truncated_on_right_p = 1;
21800 break;
21804 /* Maybe insert a truncation at the left. */
21805 if (it->first_visible_x
21806 && it_charpos > 0)
21808 if (!FRAME_WINDOW_P (it->f))
21809 insert_left_trunc_glyphs (it);
21810 row->truncated_on_left_p = 1;
21813 it->face_id = saved_face_id;
21815 /* Value is number of columns displayed. */
21816 return it->hpos - hpos_at_start;
21821 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21822 appears as an element of LIST or as the car of an element of LIST.
21823 If PROPVAL is a list, compare each element against LIST in that
21824 way, and return 1/2 if any element of PROPVAL is found in LIST.
21825 Otherwise return 0. This function cannot quit.
21826 The return value is 2 if the text is invisible but with an ellipsis
21827 and 1 if it's invisible and without an ellipsis. */
21830 invisible_p (register Lisp_Object propval, Lisp_Object list)
21832 register Lisp_Object tail, proptail;
21834 for (tail = list; CONSP (tail); tail = XCDR (tail))
21836 register Lisp_Object tem;
21837 tem = XCAR (tail);
21838 if (EQ (propval, tem))
21839 return 1;
21840 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21841 return NILP (XCDR (tem)) ? 1 : 2;
21844 if (CONSP (propval))
21846 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
21848 Lisp_Object propelt;
21849 propelt = XCAR (proptail);
21850 for (tail = list; CONSP (tail); tail = XCDR (tail))
21852 register Lisp_Object tem;
21853 tem = XCAR (tail);
21854 if (EQ (propelt, tem))
21855 return 1;
21856 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
21857 return NILP (XCDR (tem)) ? 1 : 2;
21862 return 0;
21865 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
21866 doc: /* Non-nil if the property makes the text invisible.
21867 POS-OR-PROP can be a marker or number, in which case it is taken to be
21868 a position in the current buffer and the value of the `invisible' property
21869 is checked; or it can be some other value, which is then presumed to be the
21870 value of the `invisible' property of the text of interest.
21871 The non-nil value returned can be t for truly invisible text or something
21872 else if the text is replaced by an ellipsis. */)
21873 (Lisp_Object pos_or_prop)
21875 Lisp_Object prop
21876 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
21877 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
21878 : pos_or_prop);
21879 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
21880 return (invis == 0 ? Qnil
21881 : invis == 1 ? Qt
21882 : make_number (invis));
21885 /* Calculate a width or height in pixels from a specification using
21886 the following elements:
21888 SPEC ::=
21889 NUM - a (fractional) multiple of the default font width/height
21890 (NUM) - specifies exactly NUM pixels
21891 UNIT - a fixed number of pixels, see below.
21892 ELEMENT - size of a display element in pixels, see below.
21893 (NUM . SPEC) - equals NUM * SPEC
21894 (+ SPEC SPEC ...) - add pixel values
21895 (- SPEC SPEC ...) - subtract pixel values
21896 (- SPEC) - negate pixel value
21898 NUM ::=
21899 INT or FLOAT - a number constant
21900 SYMBOL - use symbol's (buffer local) variable binding.
21902 UNIT ::=
21903 in - pixels per inch *)
21904 mm - pixels per 1/1000 meter *)
21905 cm - pixels per 1/100 meter *)
21906 width - width of current font in pixels.
21907 height - height of current font in pixels.
21909 *) using the ratio(s) defined in display-pixels-per-inch.
21911 ELEMENT ::=
21913 left-fringe - left fringe width in pixels
21914 right-fringe - right fringe width in pixels
21916 left-margin - left margin width in pixels
21917 right-margin - right margin width in pixels
21919 scroll-bar - scroll-bar area width in pixels
21921 Examples:
21923 Pixels corresponding to 5 inches:
21924 (5 . in)
21926 Total width of non-text areas on left side of window (if scroll-bar is on left):
21927 '(space :width (+ left-fringe left-margin scroll-bar))
21929 Align to first text column (in header line):
21930 '(space :align-to 0)
21932 Align to middle of text area minus half the width of variable `my-image'
21933 containing a loaded image:
21934 '(space :align-to (0.5 . (- text my-image)))
21936 Width of left margin minus width of 1 character in the default font:
21937 '(space :width (- left-margin 1))
21939 Width of left margin minus width of 2 characters in the current font:
21940 '(space :width (- left-margin (2 . width)))
21942 Center 1 character over left-margin (in header line):
21943 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
21945 Different ways to express width of left fringe plus left margin minus one pixel:
21946 '(space :width (- (+ left-fringe left-margin) (1)))
21947 '(space :width (+ left-fringe left-margin (- (1))))
21948 '(space :width (+ left-fringe left-margin (-1)))
21952 #define NUMVAL(X) \
21953 ((INTEGERP (X) || FLOATP (X)) \
21954 ? XFLOATINT (X) \
21955 : - 1)
21957 static int
21958 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
21959 struct font *font, int width_p, int *align_to)
21961 double pixels;
21963 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
21964 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
21966 if (NILP (prop))
21967 return OK_PIXELS (0);
21969 xassert (FRAME_LIVE_P (it->f));
21971 if (SYMBOLP (prop))
21973 if (SCHARS (SYMBOL_NAME (prop)) == 2)
21975 char *unit = SSDATA (SYMBOL_NAME (prop));
21977 if (unit[0] == 'i' && unit[1] == 'n')
21978 pixels = 1.0;
21979 else if (unit[0] == 'm' && unit[1] == 'm')
21980 pixels = 25.4;
21981 else if (unit[0] == 'c' && unit[1] == 'm')
21982 pixels = 2.54;
21983 else
21984 pixels = 0;
21985 if (pixels > 0)
21987 double ppi;
21988 #ifdef HAVE_WINDOW_SYSTEM
21989 if (FRAME_WINDOW_P (it->f)
21990 && (ppi = (width_p
21991 ? FRAME_X_DISPLAY_INFO (it->f)->resx
21992 : FRAME_X_DISPLAY_INFO (it->f)->resy),
21993 ppi > 0))
21994 return OK_PIXELS (ppi / pixels);
21995 #endif
21997 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
21998 || (CONSP (Vdisplay_pixels_per_inch)
21999 && (ppi = (width_p
22000 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22001 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22002 ppi > 0)))
22003 return OK_PIXELS (ppi / pixels);
22005 return 0;
22009 #ifdef HAVE_WINDOW_SYSTEM
22010 if (EQ (prop, Qheight))
22011 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22012 if (EQ (prop, Qwidth))
22013 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22014 #else
22015 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22016 return OK_PIXELS (1);
22017 #endif
22019 if (EQ (prop, Qtext))
22020 return OK_PIXELS (width_p
22021 ? window_box_width (it->w, TEXT_AREA)
22022 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22024 if (align_to && *align_to < 0)
22026 *res = 0;
22027 if (EQ (prop, Qleft))
22028 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22029 if (EQ (prop, Qright))
22030 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22031 if (EQ (prop, Qcenter))
22032 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22033 + window_box_width (it->w, TEXT_AREA) / 2);
22034 if (EQ (prop, Qleft_fringe))
22035 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22036 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22037 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22038 if (EQ (prop, Qright_fringe))
22039 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22040 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22041 : window_box_right_offset (it->w, TEXT_AREA));
22042 if (EQ (prop, Qleft_margin))
22043 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22044 if (EQ (prop, Qright_margin))
22045 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22046 if (EQ (prop, Qscroll_bar))
22047 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22049 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22050 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22051 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22052 : 0)));
22054 else
22056 if (EQ (prop, Qleft_fringe))
22057 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22058 if (EQ (prop, Qright_fringe))
22059 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22060 if (EQ (prop, Qleft_margin))
22061 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22062 if (EQ (prop, Qright_margin))
22063 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22064 if (EQ (prop, Qscroll_bar))
22065 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22068 prop = Fbuffer_local_value (prop, it->w->buffer);
22071 if (INTEGERP (prop) || FLOATP (prop))
22073 int base_unit = (width_p
22074 ? FRAME_COLUMN_WIDTH (it->f)
22075 : FRAME_LINE_HEIGHT (it->f));
22076 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22079 if (CONSP (prop))
22081 Lisp_Object car = XCAR (prop);
22082 Lisp_Object cdr = XCDR (prop);
22084 if (SYMBOLP (car))
22086 #ifdef HAVE_WINDOW_SYSTEM
22087 if (FRAME_WINDOW_P (it->f)
22088 && valid_image_p (prop))
22090 ptrdiff_t id = lookup_image (it->f, prop);
22091 struct image *img = IMAGE_FROM_ID (it->f, id);
22093 return OK_PIXELS (width_p ? img->width : img->height);
22095 #endif
22096 if (EQ (car, Qplus) || EQ (car, Qminus))
22098 int first = 1;
22099 double px;
22101 pixels = 0;
22102 while (CONSP (cdr))
22104 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22105 font, width_p, align_to))
22106 return 0;
22107 if (first)
22108 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22109 else
22110 pixels += px;
22111 cdr = XCDR (cdr);
22113 if (EQ (car, Qminus))
22114 pixels = -pixels;
22115 return OK_PIXELS (pixels);
22118 car = Fbuffer_local_value (car, it->w->buffer);
22121 if (INTEGERP (car) || FLOATP (car))
22123 double fact;
22124 pixels = XFLOATINT (car);
22125 if (NILP (cdr))
22126 return OK_PIXELS (pixels);
22127 if (calc_pixel_width_or_height (&fact, it, cdr,
22128 font, width_p, align_to))
22129 return OK_PIXELS (pixels * fact);
22130 return 0;
22133 return 0;
22136 return 0;
22140 /***********************************************************************
22141 Glyph Display
22142 ***********************************************************************/
22144 #ifdef HAVE_WINDOW_SYSTEM
22146 #if GLYPH_DEBUG
22148 void
22149 dump_glyph_string (struct glyph_string *s)
22151 fprintf (stderr, "glyph string\n");
22152 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22153 s->x, s->y, s->width, s->height);
22154 fprintf (stderr, " ybase = %d\n", s->ybase);
22155 fprintf (stderr, " hl = %d\n", s->hl);
22156 fprintf (stderr, " left overhang = %d, right = %d\n",
22157 s->left_overhang, s->right_overhang);
22158 fprintf (stderr, " nchars = %d\n", s->nchars);
22159 fprintf (stderr, " extends to end of line = %d\n",
22160 s->extends_to_end_of_line_p);
22161 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22162 fprintf (stderr, " bg width = %d\n", s->background_width);
22165 #endif /* GLYPH_DEBUG */
22167 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22168 of XChar2b structures for S; it can't be allocated in
22169 init_glyph_string because it must be allocated via `alloca'. W
22170 is the window on which S is drawn. ROW and AREA are the glyph row
22171 and area within the row from which S is constructed. START is the
22172 index of the first glyph structure covered by S. HL is a
22173 face-override for drawing S. */
22175 #ifdef HAVE_NTGUI
22176 #define OPTIONAL_HDC(hdc) HDC hdc,
22177 #define DECLARE_HDC(hdc) HDC hdc;
22178 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22179 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22180 #endif
22182 #ifndef OPTIONAL_HDC
22183 #define OPTIONAL_HDC(hdc)
22184 #define DECLARE_HDC(hdc)
22185 #define ALLOCATE_HDC(hdc, f)
22186 #define RELEASE_HDC(hdc, f)
22187 #endif
22189 static void
22190 init_glyph_string (struct glyph_string *s,
22191 OPTIONAL_HDC (hdc)
22192 XChar2b *char2b, struct window *w, struct glyph_row *row,
22193 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22195 memset (s, 0, sizeof *s);
22196 s->w = w;
22197 s->f = XFRAME (w->frame);
22198 #ifdef HAVE_NTGUI
22199 s->hdc = hdc;
22200 #endif
22201 s->display = FRAME_X_DISPLAY (s->f);
22202 s->window = FRAME_X_WINDOW (s->f);
22203 s->char2b = char2b;
22204 s->hl = hl;
22205 s->row = row;
22206 s->area = area;
22207 s->first_glyph = row->glyphs[area] + start;
22208 s->height = row->height;
22209 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22210 s->ybase = s->y + row->ascent;
22214 /* Append the list of glyph strings with head H and tail T to the list
22215 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22217 static inline void
22218 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22219 struct glyph_string *h, struct glyph_string *t)
22221 if (h)
22223 if (*head)
22224 (*tail)->next = h;
22225 else
22226 *head = h;
22227 h->prev = *tail;
22228 *tail = t;
22233 /* Prepend the list of glyph strings with head H and tail T to the
22234 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22235 result. */
22237 static inline void
22238 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22239 struct glyph_string *h, struct glyph_string *t)
22241 if (h)
22243 if (*head)
22244 (*head)->prev = t;
22245 else
22246 *tail = t;
22247 t->next = *head;
22248 *head = h;
22253 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22254 Set *HEAD and *TAIL to the resulting list. */
22256 static inline void
22257 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22258 struct glyph_string *s)
22260 s->next = s->prev = NULL;
22261 append_glyph_string_lists (head, tail, s, s);
22265 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22266 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22267 make sure that X resources for the face returned are allocated.
22268 Value is a pointer to a realized face that is ready for display if
22269 DISPLAY_P is non-zero. */
22271 static inline struct face *
22272 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22273 XChar2b *char2b, int display_p)
22275 struct face *face = FACE_FROM_ID (f, face_id);
22277 if (face->font)
22279 unsigned code = face->font->driver->encode_char (face->font, c);
22281 if (code != FONT_INVALID_CODE)
22282 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22283 else
22284 STORE_XCHAR2B (char2b, 0, 0);
22287 /* Make sure X resources of the face are allocated. */
22288 #ifdef HAVE_X_WINDOWS
22289 if (display_p)
22290 #endif
22292 xassert (face != NULL);
22293 PREPARE_FACE_FOR_DISPLAY (f, face);
22296 return face;
22300 /* Get face and two-byte form of character glyph GLYPH on frame F.
22301 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22302 a pointer to a realized face that is ready for display. */
22304 static inline struct face *
22305 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22306 XChar2b *char2b, int *two_byte_p)
22308 struct face *face;
22310 xassert (glyph->type == CHAR_GLYPH);
22311 face = FACE_FROM_ID (f, glyph->face_id);
22313 if (two_byte_p)
22314 *two_byte_p = 0;
22316 if (face->font)
22318 unsigned code;
22320 if (CHAR_BYTE8_P (glyph->u.ch))
22321 code = CHAR_TO_BYTE8 (glyph->u.ch);
22322 else
22323 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22325 if (code != FONT_INVALID_CODE)
22326 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22327 else
22328 STORE_XCHAR2B (char2b, 0, 0);
22331 /* Make sure X resources of the face are allocated. */
22332 xassert (face != NULL);
22333 PREPARE_FACE_FOR_DISPLAY (f, face);
22334 return face;
22338 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22339 Return 1 if FONT has a glyph for C, otherwise return 0. */
22341 static inline int
22342 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22344 unsigned code;
22346 if (CHAR_BYTE8_P (c))
22347 code = CHAR_TO_BYTE8 (c);
22348 else
22349 code = font->driver->encode_char (font, c);
22351 if (code == FONT_INVALID_CODE)
22352 return 0;
22353 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22354 return 1;
22358 /* Fill glyph string S with composition components specified by S->cmp.
22360 BASE_FACE is the base face of the composition.
22361 S->cmp_from is the index of the first component for S.
22363 OVERLAPS non-zero means S should draw the foreground only, and use
22364 its physical height for clipping. See also draw_glyphs.
22366 Value is the index of a component not in S. */
22368 static int
22369 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22370 int overlaps)
22372 int i;
22373 /* For all glyphs of this composition, starting at the offset
22374 S->cmp_from, until we reach the end of the definition or encounter a
22375 glyph that requires the different face, add it to S. */
22376 struct face *face;
22378 xassert (s);
22380 s->for_overlaps = overlaps;
22381 s->face = NULL;
22382 s->font = NULL;
22383 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22385 int c = COMPOSITION_GLYPH (s->cmp, i);
22387 /* TAB in a composition means display glyphs with padding space
22388 on the left or right. */
22389 if (c != '\t')
22391 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22392 -1, Qnil);
22394 face = get_char_face_and_encoding (s->f, c, face_id,
22395 s->char2b + i, 1);
22396 if (face)
22398 if (! s->face)
22400 s->face = face;
22401 s->font = s->face->font;
22403 else if (s->face != face)
22404 break;
22407 ++s->nchars;
22409 s->cmp_to = i;
22411 if (s->face == NULL)
22413 s->face = base_face->ascii_face;
22414 s->font = s->face->font;
22417 /* All glyph strings for the same composition has the same width,
22418 i.e. the width set for the first component of the composition. */
22419 s->width = s->first_glyph->pixel_width;
22421 /* If the specified font could not be loaded, use the frame's
22422 default font, but record the fact that we couldn't load it in
22423 the glyph string so that we can draw rectangles for the
22424 characters of the glyph string. */
22425 if (s->font == NULL)
22427 s->font_not_found_p = 1;
22428 s->font = FRAME_FONT (s->f);
22431 /* Adjust base line for subscript/superscript text. */
22432 s->ybase += s->first_glyph->voffset;
22434 /* This glyph string must always be drawn with 16-bit functions. */
22435 s->two_byte_p = 1;
22437 return s->cmp_to;
22440 static int
22441 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22442 int start, int end, int overlaps)
22444 struct glyph *glyph, *last;
22445 Lisp_Object lgstring;
22446 int i;
22448 s->for_overlaps = overlaps;
22449 glyph = s->row->glyphs[s->area] + start;
22450 last = s->row->glyphs[s->area] + end;
22451 s->cmp_id = glyph->u.cmp.id;
22452 s->cmp_from = glyph->slice.cmp.from;
22453 s->cmp_to = glyph->slice.cmp.to + 1;
22454 s->face = FACE_FROM_ID (s->f, face_id);
22455 lgstring = composition_gstring_from_id (s->cmp_id);
22456 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22457 glyph++;
22458 while (glyph < last
22459 && glyph->u.cmp.automatic
22460 && glyph->u.cmp.id == s->cmp_id
22461 && s->cmp_to == glyph->slice.cmp.from)
22462 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22464 for (i = s->cmp_from; i < s->cmp_to; i++)
22466 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22467 unsigned code = LGLYPH_CODE (lglyph);
22469 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22471 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22472 return glyph - s->row->glyphs[s->area];
22476 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22477 See the comment of fill_glyph_string for arguments.
22478 Value is the index of the first glyph not in S. */
22481 static int
22482 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22483 int start, int end, int overlaps)
22485 struct glyph *glyph, *last;
22486 int voffset;
22488 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22489 s->for_overlaps = overlaps;
22490 glyph = s->row->glyphs[s->area] + start;
22491 last = s->row->glyphs[s->area] + end;
22492 voffset = glyph->voffset;
22493 s->face = FACE_FROM_ID (s->f, face_id);
22494 s->font = s->face->font;
22495 s->nchars = 1;
22496 s->width = glyph->pixel_width;
22497 glyph++;
22498 while (glyph < last
22499 && glyph->type == GLYPHLESS_GLYPH
22500 && glyph->voffset == voffset
22501 && glyph->face_id == face_id)
22503 s->nchars++;
22504 s->width += glyph->pixel_width;
22505 glyph++;
22507 s->ybase += voffset;
22508 return glyph - s->row->glyphs[s->area];
22512 /* Fill glyph string S from a sequence of character glyphs.
22514 FACE_ID is the face id of the string. START is the index of the
22515 first glyph to consider, END is the index of the last + 1.
22516 OVERLAPS non-zero means S should draw the foreground only, and use
22517 its physical height for clipping. See also draw_glyphs.
22519 Value is the index of the first glyph not in S. */
22521 static int
22522 fill_glyph_string (struct glyph_string *s, int face_id,
22523 int start, int end, int overlaps)
22525 struct glyph *glyph, *last;
22526 int voffset;
22527 int glyph_not_available_p;
22529 xassert (s->f == XFRAME (s->w->frame));
22530 xassert (s->nchars == 0);
22531 xassert (start >= 0 && end > start);
22533 s->for_overlaps = overlaps;
22534 glyph = s->row->glyphs[s->area] + start;
22535 last = s->row->glyphs[s->area] + end;
22536 voffset = glyph->voffset;
22537 s->padding_p = glyph->padding_p;
22538 glyph_not_available_p = glyph->glyph_not_available_p;
22540 while (glyph < last
22541 && glyph->type == CHAR_GLYPH
22542 && glyph->voffset == voffset
22543 /* Same face id implies same font, nowadays. */
22544 && glyph->face_id == face_id
22545 && glyph->glyph_not_available_p == glyph_not_available_p)
22547 int two_byte_p;
22549 s->face = get_glyph_face_and_encoding (s->f, glyph,
22550 s->char2b + s->nchars,
22551 &two_byte_p);
22552 s->two_byte_p = two_byte_p;
22553 ++s->nchars;
22554 xassert (s->nchars <= end - start);
22555 s->width += glyph->pixel_width;
22556 if (glyph++->padding_p != s->padding_p)
22557 break;
22560 s->font = s->face->font;
22562 /* If the specified font could not be loaded, use the frame's font,
22563 but record the fact that we couldn't load it in
22564 S->font_not_found_p so that we can draw rectangles for the
22565 characters of the glyph string. */
22566 if (s->font == NULL || glyph_not_available_p)
22568 s->font_not_found_p = 1;
22569 s->font = FRAME_FONT (s->f);
22572 /* Adjust base line for subscript/superscript text. */
22573 s->ybase += voffset;
22575 xassert (s->face && s->face->gc);
22576 return glyph - s->row->glyphs[s->area];
22580 /* Fill glyph string S from image glyph S->first_glyph. */
22582 static void
22583 fill_image_glyph_string (struct glyph_string *s)
22585 xassert (s->first_glyph->type == IMAGE_GLYPH);
22586 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22587 xassert (s->img);
22588 s->slice = s->first_glyph->slice.img;
22589 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22590 s->font = s->face->font;
22591 s->width = s->first_glyph->pixel_width;
22593 /* Adjust base line for subscript/superscript text. */
22594 s->ybase += s->first_glyph->voffset;
22598 /* Fill glyph string S from a sequence of stretch glyphs.
22600 START is the index of the first glyph to consider,
22601 END is the index of the last + 1.
22603 Value is the index of the first glyph not in S. */
22605 static int
22606 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22608 struct glyph *glyph, *last;
22609 int voffset, face_id;
22611 xassert (s->first_glyph->type == STRETCH_GLYPH);
22613 glyph = s->row->glyphs[s->area] + start;
22614 last = s->row->glyphs[s->area] + end;
22615 face_id = glyph->face_id;
22616 s->face = FACE_FROM_ID (s->f, face_id);
22617 s->font = s->face->font;
22618 s->width = glyph->pixel_width;
22619 s->nchars = 1;
22620 voffset = glyph->voffset;
22622 for (++glyph;
22623 (glyph < last
22624 && glyph->type == STRETCH_GLYPH
22625 && glyph->voffset == voffset
22626 && glyph->face_id == face_id);
22627 ++glyph)
22628 s->width += glyph->pixel_width;
22630 /* Adjust base line for subscript/superscript text. */
22631 s->ybase += voffset;
22633 /* The case that face->gc == 0 is handled when drawing the glyph
22634 string by calling PREPARE_FACE_FOR_DISPLAY. */
22635 xassert (s->face);
22636 return glyph - s->row->glyphs[s->area];
22639 static struct font_metrics *
22640 get_per_char_metric (struct font *font, XChar2b *char2b)
22642 static struct font_metrics metrics;
22643 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22645 if (! font || code == FONT_INVALID_CODE)
22646 return NULL;
22647 font->driver->text_extents (font, &code, 1, &metrics);
22648 return &metrics;
22651 /* EXPORT for RIF:
22652 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22653 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22654 assumed to be zero. */
22656 void
22657 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22659 *left = *right = 0;
22661 if (glyph->type == CHAR_GLYPH)
22663 struct face *face;
22664 XChar2b char2b;
22665 struct font_metrics *pcm;
22667 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22668 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22670 if (pcm->rbearing > pcm->width)
22671 *right = pcm->rbearing - pcm->width;
22672 if (pcm->lbearing < 0)
22673 *left = -pcm->lbearing;
22676 else if (glyph->type == COMPOSITE_GLYPH)
22678 if (! glyph->u.cmp.automatic)
22680 struct composition *cmp = composition_table[glyph->u.cmp.id];
22682 if (cmp->rbearing > cmp->pixel_width)
22683 *right = cmp->rbearing - cmp->pixel_width;
22684 if (cmp->lbearing < 0)
22685 *left = - cmp->lbearing;
22687 else
22689 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22690 struct font_metrics metrics;
22692 composition_gstring_width (gstring, glyph->slice.cmp.from,
22693 glyph->slice.cmp.to + 1, &metrics);
22694 if (metrics.rbearing > metrics.width)
22695 *right = metrics.rbearing - metrics.width;
22696 if (metrics.lbearing < 0)
22697 *left = - metrics.lbearing;
22703 /* Return the index of the first glyph preceding glyph string S that
22704 is overwritten by S because of S's left overhang. Value is -1
22705 if no glyphs are overwritten. */
22707 static int
22708 left_overwritten (struct glyph_string *s)
22710 int k;
22712 if (s->left_overhang)
22714 int x = 0, i;
22715 struct glyph *glyphs = s->row->glyphs[s->area];
22716 int first = s->first_glyph - glyphs;
22718 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22719 x -= glyphs[i].pixel_width;
22721 k = i + 1;
22723 else
22724 k = -1;
22726 return k;
22730 /* Return the index of the first glyph preceding glyph string S that
22731 is overwriting S because of its right overhang. Value is -1 if no
22732 glyph in front of S overwrites S. */
22734 static int
22735 left_overwriting (struct glyph_string *s)
22737 int i, k, x;
22738 struct glyph *glyphs = s->row->glyphs[s->area];
22739 int first = s->first_glyph - glyphs;
22741 k = -1;
22742 x = 0;
22743 for (i = first - 1; i >= 0; --i)
22745 int left, right;
22746 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22747 if (x + right > 0)
22748 k = i;
22749 x -= glyphs[i].pixel_width;
22752 return k;
22756 /* Return the index of the last glyph following glyph string S that is
22757 overwritten by S because of S's right overhang. Value is -1 if
22758 no such glyph is found. */
22760 static int
22761 right_overwritten (struct glyph_string *s)
22763 int k = -1;
22765 if (s->right_overhang)
22767 int x = 0, i;
22768 struct glyph *glyphs = s->row->glyphs[s->area];
22769 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22770 int end = s->row->used[s->area];
22772 for (i = first; i < end && s->right_overhang > x; ++i)
22773 x += glyphs[i].pixel_width;
22775 k = i;
22778 return k;
22782 /* Return the index of the last glyph following glyph string S that
22783 overwrites S because of its left overhang. Value is negative
22784 if no such glyph is found. */
22786 static int
22787 right_overwriting (struct glyph_string *s)
22789 int i, k, x;
22790 int end = s->row->used[s->area];
22791 struct glyph *glyphs = s->row->glyphs[s->area];
22792 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22794 k = -1;
22795 x = 0;
22796 for (i = first; i < end; ++i)
22798 int left, right;
22799 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22800 if (x - left < 0)
22801 k = i;
22802 x += glyphs[i].pixel_width;
22805 return k;
22809 /* Set background width of glyph string S. START is the index of the
22810 first glyph following S. LAST_X is the right-most x-position + 1
22811 in the drawing area. */
22813 static inline void
22814 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22816 /* If the face of this glyph string has to be drawn to the end of
22817 the drawing area, set S->extends_to_end_of_line_p. */
22819 if (start == s->row->used[s->area]
22820 && s->area == TEXT_AREA
22821 && ((s->row->fill_line_p
22822 && (s->hl == DRAW_NORMAL_TEXT
22823 || s->hl == DRAW_IMAGE_RAISED
22824 || s->hl == DRAW_IMAGE_SUNKEN))
22825 || s->hl == DRAW_MOUSE_FACE))
22826 s->extends_to_end_of_line_p = 1;
22828 /* If S extends its face to the end of the line, set its
22829 background_width to the distance to the right edge of the drawing
22830 area. */
22831 if (s->extends_to_end_of_line_p)
22832 s->background_width = last_x - s->x + 1;
22833 else
22834 s->background_width = s->width;
22838 /* Compute overhangs and x-positions for glyph string S and its
22839 predecessors, or successors. X is the starting x-position for S.
22840 BACKWARD_P non-zero means process predecessors. */
22842 static void
22843 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22845 if (backward_p)
22847 while (s)
22849 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22850 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22851 x -= s->width;
22852 s->x = x;
22853 s = s->prev;
22856 else
22858 while (s)
22860 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22861 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22862 s->x = x;
22863 x += s->width;
22864 s = s->next;
22871 /* The following macros are only called from draw_glyphs below.
22872 They reference the following parameters of that function directly:
22873 `w', `row', `area', and `overlap_p'
22874 as well as the following local variables:
22875 `s', `f', and `hdc' (in W32) */
22877 #ifdef HAVE_NTGUI
22878 /* On W32, silently add local `hdc' variable to argument list of
22879 init_glyph_string. */
22880 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22881 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
22882 #else
22883 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22884 init_glyph_string (s, char2b, w, row, area, start, hl)
22885 #endif
22887 /* Add a glyph string for a stretch glyph to the list of strings
22888 between HEAD and TAIL. START is the index of the stretch glyph in
22889 row area AREA of glyph row ROW. END is the index of the last glyph
22890 in that glyph row area. X is the current output position assigned
22891 to the new glyph string constructed. HL overrides that face of the
22892 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22893 is the right-most x-position of the drawing area. */
22895 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
22896 and below -- keep them on one line. */
22897 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22898 do \
22900 s = (struct glyph_string *) alloca (sizeof *s); \
22901 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22902 START = fill_stretch_glyph_string (s, START, END); \
22903 append_glyph_string (&HEAD, &TAIL, s); \
22904 s->x = (X); \
22906 while (0)
22909 /* Add a glyph string for an image glyph to the list of strings
22910 between HEAD and TAIL. START is the index of the image glyph in
22911 row area AREA of glyph row ROW. END is the index of the last glyph
22912 in that glyph row area. X is the current output position assigned
22913 to the new glyph string constructed. HL overrides that face of the
22914 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22915 is the right-most x-position of the drawing area. */
22917 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22918 do \
22920 s = (struct glyph_string *) alloca (sizeof *s); \
22921 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22922 fill_image_glyph_string (s); \
22923 append_glyph_string (&HEAD, &TAIL, s); \
22924 ++START; \
22925 s->x = (X); \
22927 while (0)
22930 /* Add a glyph string for a sequence of character glyphs to the list
22931 of strings between HEAD and TAIL. START is the index of the first
22932 glyph in row area AREA of glyph row ROW that is part of the new
22933 glyph string. END is the index of the last glyph in that glyph row
22934 area. X is the current output position assigned to the new glyph
22935 string constructed. HL overrides that face of the glyph; e.g. it
22936 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
22937 right-most x-position of the drawing area. */
22939 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22940 do \
22942 int face_id; \
22943 XChar2b *char2b; \
22945 face_id = (row)->glyphs[area][START].face_id; \
22947 s = (struct glyph_string *) alloca (sizeof *s); \
22948 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
22949 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22950 append_glyph_string (&HEAD, &TAIL, s); \
22951 s->x = (X); \
22952 START = fill_glyph_string (s, face_id, START, END, overlaps); \
22954 while (0)
22957 /* Add a glyph string for a composite sequence to the list of strings
22958 between HEAD and TAIL. START is the index of the first glyph in
22959 row area AREA of glyph row ROW that is part of the new glyph
22960 string. END is the index of the last glyph in that glyph row area.
22961 X is the current output position assigned to the new glyph string
22962 constructed. HL overrides that face of the glyph; e.g. it is
22963 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
22964 x-position of the drawing area. */
22966 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22967 do { \
22968 int face_id = (row)->glyphs[area][START].face_id; \
22969 struct face *base_face = FACE_FROM_ID (f, face_id); \
22970 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
22971 struct composition *cmp = composition_table[cmp_id]; \
22972 XChar2b *char2b; \
22973 struct glyph_string *first_s = NULL; \
22974 int n; \
22976 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
22978 /* Make glyph_strings for each glyph sequence that is drawable by \
22979 the same face, and append them to HEAD/TAIL. */ \
22980 for (n = 0; n < cmp->glyph_len;) \
22982 s = (struct glyph_string *) alloca (sizeof *s); \
22983 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22984 append_glyph_string (&(HEAD), &(TAIL), s); \
22985 s->cmp = cmp; \
22986 s->cmp_from = n; \
22987 s->x = (X); \
22988 if (n == 0) \
22989 first_s = s; \
22990 n = fill_composite_glyph_string (s, base_face, overlaps); \
22993 ++START; \
22994 s = first_s; \
22995 } while (0)
22998 /* Add a glyph string for a glyph-string sequence to the list of strings
22999 between HEAD and TAIL. */
23001 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23002 do { \
23003 int face_id; \
23004 XChar2b *char2b; \
23005 Lisp_Object gstring; \
23007 face_id = (row)->glyphs[area][START].face_id; \
23008 gstring = (composition_gstring_from_id \
23009 ((row)->glyphs[area][START].u.cmp.id)); \
23010 s = (struct glyph_string *) alloca (sizeof *s); \
23011 char2b = (XChar2b *) alloca ((sizeof *char2b) \
23012 * LGSTRING_GLYPH_LEN (gstring)); \
23013 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23014 append_glyph_string (&(HEAD), &(TAIL), s); \
23015 s->x = (X); \
23016 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23017 } while (0)
23020 /* Add a glyph string for a sequence of glyphless character's glyphs
23021 to the list of strings between HEAD and TAIL. The meanings of
23022 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23024 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23025 do \
23027 int face_id; \
23029 face_id = (row)->glyphs[area][START].face_id; \
23031 s = (struct glyph_string *) alloca (sizeof *s); \
23032 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23033 append_glyph_string (&HEAD, &TAIL, s); \
23034 s->x = (X); \
23035 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23036 overlaps); \
23038 while (0)
23041 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23042 of AREA of glyph row ROW on window W between indices START and END.
23043 HL overrides the face for drawing glyph strings, e.g. it is
23044 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23045 x-positions of the drawing area.
23047 This is an ugly monster macro construct because we must use alloca
23048 to allocate glyph strings (because draw_glyphs can be called
23049 asynchronously). */
23051 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23052 do \
23054 HEAD = TAIL = NULL; \
23055 while (START < END) \
23057 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23058 switch (first_glyph->type) \
23060 case CHAR_GLYPH: \
23061 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23062 HL, X, LAST_X); \
23063 break; \
23065 case COMPOSITE_GLYPH: \
23066 if (first_glyph->u.cmp.automatic) \
23067 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23068 HL, X, LAST_X); \
23069 else \
23070 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23071 HL, X, LAST_X); \
23072 break; \
23074 case STRETCH_GLYPH: \
23075 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23076 HL, X, LAST_X); \
23077 break; \
23079 case IMAGE_GLYPH: \
23080 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23081 HL, X, LAST_X); \
23082 break; \
23084 case GLYPHLESS_GLYPH: \
23085 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23086 HL, X, LAST_X); \
23087 break; \
23089 default: \
23090 abort (); \
23093 if (s) \
23095 set_glyph_string_background_width (s, START, LAST_X); \
23096 (X) += s->width; \
23099 } while (0)
23102 /* Draw glyphs between START and END in AREA of ROW on window W,
23103 starting at x-position X. X is relative to AREA in W. HL is a
23104 face-override with the following meaning:
23106 DRAW_NORMAL_TEXT draw normally
23107 DRAW_CURSOR draw in cursor face
23108 DRAW_MOUSE_FACE draw in mouse face.
23109 DRAW_INVERSE_VIDEO draw in mode line face
23110 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23111 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23113 If OVERLAPS is non-zero, draw only the foreground of characters and
23114 clip to the physical height of ROW. Non-zero value also defines
23115 the overlapping part to be drawn:
23117 OVERLAPS_PRED overlap with preceding rows
23118 OVERLAPS_SUCC overlap with succeeding rows
23119 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23120 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23122 Value is the x-position reached, relative to AREA of W. */
23124 static int
23125 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23126 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
23127 enum draw_glyphs_face hl, int overlaps)
23129 struct glyph_string *head, *tail;
23130 struct glyph_string *s;
23131 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23132 int i, j, x_reached, last_x, area_left = 0;
23133 struct frame *f = XFRAME (WINDOW_FRAME (w));
23134 DECLARE_HDC (hdc);
23136 ALLOCATE_HDC (hdc, f);
23138 /* Let's rather be paranoid than getting a SEGV. */
23139 end = min (end, row->used[area]);
23140 start = max (0, start);
23141 start = min (end, start);
23143 /* Translate X to frame coordinates. Set last_x to the right
23144 end of the drawing area. */
23145 if (row->full_width_p)
23147 /* X is relative to the left edge of W, without scroll bars
23148 or fringes. */
23149 area_left = WINDOW_LEFT_EDGE_X (w);
23150 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23152 else
23154 area_left = window_box_left (w, area);
23155 last_x = area_left + window_box_width (w, area);
23157 x += area_left;
23159 /* Build a doubly-linked list of glyph_string structures between
23160 head and tail from what we have to draw. Note that the macro
23161 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23162 the reason we use a separate variable `i'. */
23163 i = start;
23164 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23165 if (tail)
23166 x_reached = tail->x + tail->background_width;
23167 else
23168 x_reached = x;
23170 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23171 the row, redraw some glyphs in front or following the glyph
23172 strings built above. */
23173 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23175 struct glyph_string *h, *t;
23176 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23177 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23178 int check_mouse_face = 0;
23179 int dummy_x = 0;
23181 /* If mouse highlighting is on, we may need to draw adjacent
23182 glyphs using mouse-face highlighting. */
23183 if (area == TEXT_AREA && row->mouse_face_p)
23185 struct glyph_row *mouse_beg_row, *mouse_end_row;
23187 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23188 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23190 if (row >= mouse_beg_row && row <= mouse_end_row)
23192 check_mouse_face = 1;
23193 mouse_beg_col = (row == mouse_beg_row)
23194 ? hlinfo->mouse_face_beg_col : 0;
23195 mouse_end_col = (row == mouse_end_row)
23196 ? hlinfo->mouse_face_end_col
23197 : row->used[TEXT_AREA];
23201 /* Compute overhangs for all glyph strings. */
23202 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23203 for (s = head; s; s = s->next)
23204 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23206 /* Prepend glyph strings for glyphs in front of the first glyph
23207 string that are overwritten because of the first glyph
23208 string's left overhang. The background of all strings
23209 prepended must be drawn because the first glyph string
23210 draws over it. */
23211 i = left_overwritten (head);
23212 if (i >= 0)
23214 enum draw_glyphs_face overlap_hl;
23216 /* If this row contains mouse highlighting, attempt to draw
23217 the overlapped glyphs with the correct highlight. This
23218 code fails if the overlap encompasses more than one glyph
23219 and mouse-highlight spans only some of these glyphs.
23220 However, making it work perfectly involves a lot more
23221 code, and I don't know if the pathological case occurs in
23222 practice, so we'll stick to this for now. --- cyd */
23223 if (check_mouse_face
23224 && mouse_beg_col < start && mouse_end_col > i)
23225 overlap_hl = DRAW_MOUSE_FACE;
23226 else
23227 overlap_hl = DRAW_NORMAL_TEXT;
23229 j = i;
23230 BUILD_GLYPH_STRINGS (j, start, h, t,
23231 overlap_hl, dummy_x, last_x);
23232 start = i;
23233 compute_overhangs_and_x (t, head->x, 1);
23234 prepend_glyph_string_lists (&head, &tail, h, t);
23235 clip_head = head;
23238 /* Prepend glyph strings for glyphs in front of the first glyph
23239 string that overwrite that glyph string because of their
23240 right overhang. For these strings, only the foreground must
23241 be drawn, because it draws over the glyph string at `head'.
23242 The background must not be drawn because this would overwrite
23243 right overhangs of preceding glyphs for which no glyph
23244 strings exist. */
23245 i = left_overwriting (head);
23246 if (i >= 0)
23248 enum draw_glyphs_face overlap_hl;
23250 if (check_mouse_face
23251 && mouse_beg_col < start && mouse_end_col > i)
23252 overlap_hl = DRAW_MOUSE_FACE;
23253 else
23254 overlap_hl = DRAW_NORMAL_TEXT;
23256 clip_head = head;
23257 BUILD_GLYPH_STRINGS (i, start, h, t,
23258 overlap_hl, dummy_x, last_x);
23259 for (s = h; s; s = s->next)
23260 s->background_filled_p = 1;
23261 compute_overhangs_and_x (t, head->x, 1);
23262 prepend_glyph_string_lists (&head, &tail, h, t);
23265 /* Append glyphs strings for glyphs following the last glyph
23266 string tail that are overwritten by tail. The background of
23267 these strings has to be drawn because tail's foreground draws
23268 over it. */
23269 i = right_overwritten (tail);
23270 if (i >= 0)
23272 enum draw_glyphs_face overlap_hl;
23274 if (check_mouse_face
23275 && mouse_beg_col < i && mouse_end_col > end)
23276 overlap_hl = DRAW_MOUSE_FACE;
23277 else
23278 overlap_hl = DRAW_NORMAL_TEXT;
23280 BUILD_GLYPH_STRINGS (end, i, h, t,
23281 overlap_hl, x, last_x);
23282 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23283 we don't have `end = i;' here. */
23284 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23285 append_glyph_string_lists (&head, &tail, h, t);
23286 clip_tail = tail;
23289 /* Append glyph strings for glyphs following the last glyph
23290 string tail that overwrite tail. The foreground of such
23291 glyphs has to be drawn because it writes into the background
23292 of tail. The background must not be drawn because it could
23293 paint over the foreground of following glyphs. */
23294 i = right_overwriting (tail);
23295 if (i >= 0)
23297 enum draw_glyphs_face overlap_hl;
23298 if (check_mouse_face
23299 && mouse_beg_col < i && mouse_end_col > end)
23300 overlap_hl = DRAW_MOUSE_FACE;
23301 else
23302 overlap_hl = DRAW_NORMAL_TEXT;
23304 clip_tail = tail;
23305 i++; /* We must include the Ith glyph. */
23306 BUILD_GLYPH_STRINGS (end, i, h, t,
23307 overlap_hl, x, last_x);
23308 for (s = h; s; s = s->next)
23309 s->background_filled_p = 1;
23310 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23311 append_glyph_string_lists (&head, &tail, h, t);
23313 if (clip_head || clip_tail)
23314 for (s = head; s; s = s->next)
23316 s->clip_head = clip_head;
23317 s->clip_tail = clip_tail;
23321 /* Draw all strings. */
23322 for (s = head; s; s = s->next)
23323 FRAME_RIF (f)->draw_glyph_string (s);
23325 #ifndef HAVE_NS
23326 /* When focus a sole frame and move horizontally, this sets on_p to 0
23327 causing a failure to erase prev cursor position. */
23328 if (area == TEXT_AREA
23329 && !row->full_width_p
23330 /* When drawing overlapping rows, only the glyph strings'
23331 foreground is drawn, which doesn't erase a cursor
23332 completely. */
23333 && !overlaps)
23335 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23336 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23337 : (tail ? tail->x + tail->background_width : x));
23338 x0 -= area_left;
23339 x1 -= area_left;
23341 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23342 row->y, MATRIX_ROW_BOTTOM_Y (row));
23344 #endif
23346 /* Value is the x-position up to which drawn, relative to AREA of W.
23347 This doesn't include parts drawn because of overhangs. */
23348 if (row->full_width_p)
23349 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23350 else
23351 x_reached -= area_left;
23353 RELEASE_HDC (hdc, f);
23355 return x_reached;
23358 /* Expand row matrix if too narrow. Don't expand if area
23359 is not present. */
23361 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23363 if (!fonts_changed_p \
23364 && (it->glyph_row->glyphs[area] \
23365 < it->glyph_row->glyphs[area + 1])) \
23367 it->w->ncols_scale_factor++; \
23368 fonts_changed_p = 1; \
23372 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23373 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23375 static inline void
23376 append_glyph (struct it *it)
23378 struct glyph *glyph;
23379 enum glyph_row_area area = it->area;
23381 xassert (it->glyph_row);
23382 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23384 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23385 if (glyph < it->glyph_row->glyphs[area + 1])
23387 /* If the glyph row is reversed, we need to prepend the glyph
23388 rather than append it. */
23389 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23391 struct glyph *g;
23393 /* Make room for the additional glyph. */
23394 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23395 g[1] = *g;
23396 glyph = it->glyph_row->glyphs[area];
23398 glyph->charpos = CHARPOS (it->position);
23399 glyph->object = it->object;
23400 if (it->pixel_width > 0)
23402 glyph->pixel_width = it->pixel_width;
23403 glyph->padding_p = 0;
23405 else
23407 /* Assure at least 1-pixel width. Otherwise, cursor can't
23408 be displayed correctly. */
23409 glyph->pixel_width = 1;
23410 glyph->padding_p = 1;
23412 glyph->ascent = it->ascent;
23413 glyph->descent = it->descent;
23414 glyph->voffset = it->voffset;
23415 glyph->type = CHAR_GLYPH;
23416 glyph->avoid_cursor_p = it->avoid_cursor_p;
23417 glyph->multibyte_p = it->multibyte_p;
23418 glyph->left_box_line_p = it->start_of_box_run_p;
23419 glyph->right_box_line_p = it->end_of_box_run_p;
23420 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23421 || it->phys_descent > it->descent);
23422 glyph->glyph_not_available_p = it->glyph_not_available_p;
23423 glyph->face_id = it->face_id;
23424 glyph->u.ch = it->char_to_display;
23425 glyph->slice.img = null_glyph_slice;
23426 glyph->font_type = FONT_TYPE_UNKNOWN;
23427 if (it->bidi_p)
23429 glyph->resolved_level = it->bidi_it.resolved_level;
23430 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23431 abort ();
23432 glyph->bidi_type = it->bidi_it.type;
23434 else
23436 glyph->resolved_level = 0;
23437 glyph->bidi_type = UNKNOWN_BT;
23439 ++it->glyph_row->used[area];
23441 else
23442 IT_EXPAND_MATRIX_WIDTH (it, area);
23445 /* Store one glyph for the composition IT->cmp_it.id in
23446 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23447 non-null. */
23449 static inline void
23450 append_composite_glyph (struct it *it)
23452 struct glyph *glyph;
23453 enum glyph_row_area area = it->area;
23455 xassert (it->glyph_row);
23457 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23458 if (glyph < it->glyph_row->glyphs[area + 1])
23460 /* If the glyph row is reversed, we need to prepend the glyph
23461 rather than append it. */
23462 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23464 struct glyph *g;
23466 /* Make room for the new glyph. */
23467 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23468 g[1] = *g;
23469 glyph = it->glyph_row->glyphs[it->area];
23471 glyph->charpos = it->cmp_it.charpos;
23472 glyph->object = it->object;
23473 glyph->pixel_width = it->pixel_width;
23474 glyph->ascent = it->ascent;
23475 glyph->descent = it->descent;
23476 glyph->voffset = it->voffset;
23477 glyph->type = COMPOSITE_GLYPH;
23478 if (it->cmp_it.ch < 0)
23480 glyph->u.cmp.automatic = 0;
23481 glyph->u.cmp.id = it->cmp_it.id;
23482 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23484 else
23486 glyph->u.cmp.automatic = 1;
23487 glyph->u.cmp.id = it->cmp_it.id;
23488 glyph->slice.cmp.from = it->cmp_it.from;
23489 glyph->slice.cmp.to = it->cmp_it.to - 1;
23491 glyph->avoid_cursor_p = it->avoid_cursor_p;
23492 glyph->multibyte_p = it->multibyte_p;
23493 glyph->left_box_line_p = it->start_of_box_run_p;
23494 glyph->right_box_line_p = it->end_of_box_run_p;
23495 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23496 || it->phys_descent > it->descent);
23497 glyph->padding_p = 0;
23498 glyph->glyph_not_available_p = 0;
23499 glyph->face_id = it->face_id;
23500 glyph->font_type = FONT_TYPE_UNKNOWN;
23501 if (it->bidi_p)
23503 glyph->resolved_level = it->bidi_it.resolved_level;
23504 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23505 abort ();
23506 glyph->bidi_type = it->bidi_it.type;
23508 ++it->glyph_row->used[area];
23510 else
23511 IT_EXPAND_MATRIX_WIDTH (it, area);
23515 /* Change IT->ascent and IT->height according to the setting of
23516 IT->voffset. */
23518 static inline void
23519 take_vertical_position_into_account (struct it *it)
23521 if (it->voffset)
23523 if (it->voffset < 0)
23524 /* Increase the ascent so that we can display the text higher
23525 in the line. */
23526 it->ascent -= it->voffset;
23527 else
23528 /* Increase the descent so that we can display the text lower
23529 in the line. */
23530 it->descent += it->voffset;
23535 /* Produce glyphs/get display metrics for the image IT is loaded with.
23536 See the description of struct display_iterator in dispextern.h for
23537 an overview of struct display_iterator. */
23539 static void
23540 produce_image_glyph (struct it *it)
23542 struct image *img;
23543 struct face *face;
23544 int glyph_ascent, crop;
23545 struct glyph_slice slice;
23547 xassert (it->what == IT_IMAGE);
23549 face = FACE_FROM_ID (it->f, it->face_id);
23550 xassert (face);
23551 /* Make sure X resources of the face is loaded. */
23552 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23554 if (it->image_id < 0)
23556 /* Fringe bitmap. */
23557 it->ascent = it->phys_ascent = 0;
23558 it->descent = it->phys_descent = 0;
23559 it->pixel_width = 0;
23560 it->nglyphs = 0;
23561 return;
23564 img = IMAGE_FROM_ID (it->f, it->image_id);
23565 xassert (img);
23566 /* Make sure X resources of the image is loaded. */
23567 prepare_image_for_display (it->f, img);
23569 slice.x = slice.y = 0;
23570 slice.width = img->width;
23571 slice.height = img->height;
23573 if (INTEGERP (it->slice.x))
23574 slice.x = XINT (it->slice.x);
23575 else if (FLOATP (it->slice.x))
23576 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23578 if (INTEGERP (it->slice.y))
23579 slice.y = XINT (it->slice.y);
23580 else if (FLOATP (it->slice.y))
23581 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23583 if (INTEGERP (it->slice.width))
23584 slice.width = XINT (it->slice.width);
23585 else if (FLOATP (it->slice.width))
23586 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23588 if (INTEGERP (it->slice.height))
23589 slice.height = XINT (it->slice.height);
23590 else if (FLOATP (it->slice.height))
23591 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23593 if (slice.x >= img->width)
23594 slice.x = img->width;
23595 if (slice.y >= img->height)
23596 slice.y = img->height;
23597 if (slice.x + slice.width >= img->width)
23598 slice.width = img->width - slice.x;
23599 if (slice.y + slice.height > img->height)
23600 slice.height = img->height - slice.y;
23602 if (slice.width == 0 || slice.height == 0)
23603 return;
23605 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23607 it->descent = slice.height - glyph_ascent;
23608 if (slice.y == 0)
23609 it->descent += img->vmargin;
23610 if (slice.y + slice.height == img->height)
23611 it->descent += img->vmargin;
23612 it->phys_descent = it->descent;
23614 it->pixel_width = slice.width;
23615 if (slice.x == 0)
23616 it->pixel_width += img->hmargin;
23617 if (slice.x + slice.width == img->width)
23618 it->pixel_width += img->hmargin;
23620 /* It's quite possible for images to have an ascent greater than
23621 their height, so don't get confused in that case. */
23622 if (it->descent < 0)
23623 it->descent = 0;
23625 it->nglyphs = 1;
23627 if (face->box != FACE_NO_BOX)
23629 if (face->box_line_width > 0)
23631 if (slice.y == 0)
23632 it->ascent += face->box_line_width;
23633 if (slice.y + slice.height == img->height)
23634 it->descent += face->box_line_width;
23637 if (it->start_of_box_run_p && slice.x == 0)
23638 it->pixel_width += eabs (face->box_line_width);
23639 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23640 it->pixel_width += eabs (face->box_line_width);
23643 take_vertical_position_into_account (it);
23645 /* Automatically crop wide image glyphs at right edge so we can
23646 draw the cursor on same display row. */
23647 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23648 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23650 it->pixel_width -= crop;
23651 slice.width -= crop;
23654 if (it->glyph_row)
23656 struct glyph *glyph;
23657 enum glyph_row_area area = it->area;
23659 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23660 if (glyph < it->glyph_row->glyphs[area + 1])
23662 glyph->charpos = CHARPOS (it->position);
23663 glyph->object = it->object;
23664 glyph->pixel_width = it->pixel_width;
23665 glyph->ascent = glyph_ascent;
23666 glyph->descent = it->descent;
23667 glyph->voffset = it->voffset;
23668 glyph->type = IMAGE_GLYPH;
23669 glyph->avoid_cursor_p = it->avoid_cursor_p;
23670 glyph->multibyte_p = it->multibyte_p;
23671 glyph->left_box_line_p = it->start_of_box_run_p;
23672 glyph->right_box_line_p = it->end_of_box_run_p;
23673 glyph->overlaps_vertically_p = 0;
23674 glyph->padding_p = 0;
23675 glyph->glyph_not_available_p = 0;
23676 glyph->face_id = it->face_id;
23677 glyph->u.img_id = img->id;
23678 glyph->slice.img = slice;
23679 glyph->font_type = FONT_TYPE_UNKNOWN;
23680 if (it->bidi_p)
23682 glyph->resolved_level = it->bidi_it.resolved_level;
23683 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23684 abort ();
23685 glyph->bidi_type = it->bidi_it.type;
23687 ++it->glyph_row->used[area];
23689 else
23690 IT_EXPAND_MATRIX_WIDTH (it, area);
23695 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23696 of the glyph, WIDTH and HEIGHT are the width and height of the
23697 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23699 static void
23700 append_stretch_glyph (struct it *it, Lisp_Object object,
23701 int width, int height, int ascent)
23703 struct glyph *glyph;
23704 enum glyph_row_area area = it->area;
23706 xassert (ascent >= 0 && ascent <= height);
23708 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23709 if (glyph < it->glyph_row->glyphs[area + 1])
23711 /* If the glyph row is reversed, we need to prepend the glyph
23712 rather than append it. */
23713 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23715 struct glyph *g;
23717 /* Make room for the additional glyph. */
23718 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23719 g[1] = *g;
23720 glyph = it->glyph_row->glyphs[area];
23722 glyph->charpos = CHARPOS (it->position);
23723 glyph->object = object;
23724 glyph->pixel_width = width;
23725 glyph->ascent = ascent;
23726 glyph->descent = height - ascent;
23727 glyph->voffset = it->voffset;
23728 glyph->type = STRETCH_GLYPH;
23729 glyph->avoid_cursor_p = it->avoid_cursor_p;
23730 glyph->multibyte_p = it->multibyte_p;
23731 glyph->left_box_line_p = it->start_of_box_run_p;
23732 glyph->right_box_line_p = it->end_of_box_run_p;
23733 glyph->overlaps_vertically_p = 0;
23734 glyph->padding_p = 0;
23735 glyph->glyph_not_available_p = 0;
23736 glyph->face_id = it->face_id;
23737 glyph->u.stretch.ascent = ascent;
23738 glyph->u.stretch.height = height;
23739 glyph->slice.img = null_glyph_slice;
23740 glyph->font_type = FONT_TYPE_UNKNOWN;
23741 if (it->bidi_p)
23743 glyph->resolved_level = it->bidi_it.resolved_level;
23744 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23745 abort ();
23746 glyph->bidi_type = it->bidi_it.type;
23748 else
23750 glyph->resolved_level = 0;
23751 glyph->bidi_type = UNKNOWN_BT;
23753 ++it->glyph_row->used[area];
23755 else
23756 IT_EXPAND_MATRIX_WIDTH (it, area);
23759 #endif /* HAVE_WINDOW_SYSTEM */
23761 /* Produce a stretch glyph for iterator IT. IT->object is the value
23762 of the glyph property displayed. The value must be a list
23763 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23764 being recognized:
23766 1. `:width WIDTH' specifies that the space should be WIDTH *
23767 canonical char width wide. WIDTH may be an integer or floating
23768 point number.
23770 2. `:relative-width FACTOR' specifies that the width of the stretch
23771 should be computed from the width of the first character having the
23772 `glyph' property, and should be FACTOR times that width.
23774 3. `:align-to HPOS' specifies that the space should be wide enough
23775 to reach HPOS, a value in canonical character units.
23777 Exactly one of the above pairs must be present.
23779 4. `:height HEIGHT' specifies that the height of the stretch produced
23780 should be HEIGHT, measured in canonical character units.
23782 5. `:relative-height FACTOR' specifies that the height of the
23783 stretch should be FACTOR times the height of the characters having
23784 the glyph property.
23786 Either none or exactly one of 4 or 5 must be present.
23788 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23789 of the stretch should be used for the ascent of the stretch.
23790 ASCENT must be in the range 0 <= ASCENT <= 100. */
23792 void
23793 produce_stretch_glyph (struct it *it)
23795 /* (space :width WIDTH :height HEIGHT ...) */
23796 Lisp_Object prop, plist;
23797 int width = 0, height = 0, align_to = -1;
23798 int zero_width_ok_p = 0;
23799 int ascent = 0;
23800 double tem;
23801 struct face *face = NULL;
23802 struct font *font = NULL;
23804 #ifdef HAVE_WINDOW_SYSTEM
23805 int zero_height_ok_p = 0;
23807 if (FRAME_WINDOW_P (it->f))
23809 face = FACE_FROM_ID (it->f, it->face_id);
23810 font = face->font ? face->font : FRAME_FONT (it->f);
23811 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23813 #endif
23815 /* List should start with `space'. */
23816 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
23817 plist = XCDR (it->object);
23819 /* Compute the width of the stretch. */
23820 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
23821 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
23823 /* Absolute width `:width WIDTH' specified and valid. */
23824 zero_width_ok_p = 1;
23825 width = (int)tem;
23827 #ifdef HAVE_WINDOW_SYSTEM
23828 else if (FRAME_WINDOW_P (it->f)
23829 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
23831 /* Relative width `:relative-width FACTOR' specified and valid.
23832 Compute the width of the characters having the `glyph'
23833 property. */
23834 struct it it2;
23835 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
23837 it2 = *it;
23838 if (it->multibyte_p)
23839 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
23840 else
23842 it2.c = it2.char_to_display = *p, it2.len = 1;
23843 if (! ASCII_CHAR_P (it2.c))
23844 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
23847 it2.glyph_row = NULL;
23848 it2.what = IT_CHARACTER;
23849 x_produce_glyphs (&it2);
23850 width = NUMVAL (prop) * it2.pixel_width;
23852 #endif /* HAVE_WINDOW_SYSTEM */
23853 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
23854 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
23856 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
23857 align_to = (align_to < 0
23859 : align_to - window_box_left_offset (it->w, TEXT_AREA));
23860 else if (align_to < 0)
23861 align_to = window_box_left_offset (it->w, TEXT_AREA);
23862 width = max (0, (int)tem + align_to - it->current_x);
23863 zero_width_ok_p = 1;
23865 else
23866 /* Nothing specified -> width defaults to canonical char width. */
23867 width = FRAME_COLUMN_WIDTH (it->f);
23869 if (width <= 0 && (width < 0 || !zero_width_ok_p))
23870 width = 1;
23872 #ifdef HAVE_WINDOW_SYSTEM
23873 /* Compute height. */
23874 if (FRAME_WINDOW_P (it->f))
23876 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
23877 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23879 height = (int)tem;
23880 zero_height_ok_p = 1;
23882 else if (prop = Fplist_get (plist, QCrelative_height),
23883 NUMVAL (prop) > 0)
23884 height = FONT_HEIGHT (font) * NUMVAL (prop);
23885 else
23886 height = FONT_HEIGHT (font);
23888 if (height <= 0 && (height < 0 || !zero_height_ok_p))
23889 height = 1;
23891 /* Compute percentage of height used for ascent. If
23892 `:ascent ASCENT' is present and valid, use that. Otherwise,
23893 derive the ascent from the font in use. */
23894 if (prop = Fplist_get (plist, QCascent),
23895 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
23896 ascent = height * NUMVAL (prop) / 100.0;
23897 else if (!NILP (prop)
23898 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23899 ascent = min (max (0, (int)tem), height);
23900 else
23901 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
23903 else
23904 #endif /* HAVE_WINDOW_SYSTEM */
23905 height = 1;
23907 if (width > 0 && it->line_wrap != TRUNCATE
23908 && it->current_x + width > it->last_visible_x)
23910 width = it->last_visible_x - it->current_x;
23911 #ifdef HAVE_WINDOW_SYSTEM
23912 /* Subtract one more pixel from the stretch width, but only on
23913 GUI frames, since on a TTY each glyph is one "pixel" wide. */
23914 width -= FRAME_WINDOW_P (it->f);
23915 #endif
23918 if (width > 0 && height > 0 && it->glyph_row)
23920 Lisp_Object o_object = it->object;
23921 Lisp_Object object = it->stack[it->sp - 1].string;
23922 int n = width;
23924 if (!STRINGP (object))
23925 object = it->w->buffer;
23926 #ifdef HAVE_WINDOW_SYSTEM
23927 if (FRAME_WINDOW_P (it->f))
23928 append_stretch_glyph (it, object, width, height, ascent);
23929 else
23930 #endif
23932 it->object = object;
23933 it->char_to_display = ' ';
23934 it->pixel_width = it->len = 1;
23935 while (n--)
23936 tty_append_glyph (it);
23937 it->object = o_object;
23941 it->pixel_width = width;
23942 #ifdef HAVE_WINDOW_SYSTEM
23943 if (FRAME_WINDOW_P (it->f))
23945 it->ascent = it->phys_ascent = ascent;
23946 it->descent = it->phys_descent = height - it->ascent;
23947 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
23948 take_vertical_position_into_account (it);
23950 else
23951 #endif
23952 it->nglyphs = width;
23955 #ifdef HAVE_WINDOW_SYSTEM
23957 /* Calculate line-height and line-spacing properties.
23958 An integer value specifies explicit pixel value.
23959 A float value specifies relative value to current face height.
23960 A cons (float . face-name) specifies relative value to
23961 height of specified face font.
23963 Returns height in pixels, or nil. */
23966 static Lisp_Object
23967 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
23968 int boff, int override)
23970 Lisp_Object face_name = Qnil;
23971 int ascent, descent, height;
23973 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
23974 return val;
23976 if (CONSP (val))
23978 face_name = XCAR (val);
23979 val = XCDR (val);
23980 if (!NUMBERP (val))
23981 val = make_number (1);
23982 if (NILP (face_name))
23984 height = it->ascent + it->descent;
23985 goto scale;
23989 if (NILP (face_name))
23991 font = FRAME_FONT (it->f);
23992 boff = FRAME_BASELINE_OFFSET (it->f);
23994 else if (EQ (face_name, Qt))
23996 override = 0;
23998 else
24000 int face_id;
24001 struct face *face;
24003 face_id = lookup_named_face (it->f, face_name, 0);
24004 if (face_id < 0)
24005 return make_number (-1);
24007 face = FACE_FROM_ID (it->f, face_id);
24008 font = face->font;
24009 if (font == NULL)
24010 return make_number (-1);
24011 boff = font->baseline_offset;
24012 if (font->vertical_centering)
24013 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24016 ascent = FONT_BASE (font) + boff;
24017 descent = FONT_DESCENT (font) - boff;
24019 if (override)
24021 it->override_ascent = ascent;
24022 it->override_descent = descent;
24023 it->override_boff = boff;
24026 height = ascent + descent;
24028 scale:
24029 if (FLOATP (val))
24030 height = (int)(XFLOAT_DATA (val) * height);
24031 else if (INTEGERP (val))
24032 height *= XINT (val);
24034 return make_number (height);
24038 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24039 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24040 and only if this is for a character for which no font was found.
24042 If the display method (it->glyphless_method) is
24043 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24044 length of the acronym or the hexadecimal string, UPPER_XOFF and
24045 UPPER_YOFF are pixel offsets for the upper part of the string,
24046 LOWER_XOFF and LOWER_YOFF are for the lower part.
24048 For the other display methods, LEN through LOWER_YOFF are zero. */
24050 static void
24051 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24052 short upper_xoff, short upper_yoff,
24053 short lower_xoff, short lower_yoff)
24055 struct glyph *glyph;
24056 enum glyph_row_area area = it->area;
24058 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24059 if (glyph < it->glyph_row->glyphs[area + 1])
24061 /* If the glyph row is reversed, we need to prepend the glyph
24062 rather than append it. */
24063 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24065 struct glyph *g;
24067 /* Make room for the additional glyph. */
24068 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24069 g[1] = *g;
24070 glyph = it->glyph_row->glyphs[area];
24072 glyph->charpos = CHARPOS (it->position);
24073 glyph->object = it->object;
24074 glyph->pixel_width = it->pixel_width;
24075 glyph->ascent = it->ascent;
24076 glyph->descent = it->descent;
24077 glyph->voffset = it->voffset;
24078 glyph->type = GLYPHLESS_GLYPH;
24079 glyph->u.glyphless.method = it->glyphless_method;
24080 glyph->u.glyphless.for_no_font = for_no_font;
24081 glyph->u.glyphless.len = len;
24082 glyph->u.glyphless.ch = it->c;
24083 glyph->slice.glyphless.upper_xoff = upper_xoff;
24084 glyph->slice.glyphless.upper_yoff = upper_yoff;
24085 glyph->slice.glyphless.lower_xoff = lower_xoff;
24086 glyph->slice.glyphless.lower_yoff = lower_yoff;
24087 glyph->avoid_cursor_p = it->avoid_cursor_p;
24088 glyph->multibyte_p = it->multibyte_p;
24089 glyph->left_box_line_p = it->start_of_box_run_p;
24090 glyph->right_box_line_p = it->end_of_box_run_p;
24091 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24092 || it->phys_descent > it->descent);
24093 glyph->padding_p = 0;
24094 glyph->glyph_not_available_p = 0;
24095 glyph->face_id = face_id;
24096 glyph->font_type = FONT_TYPE_UNKNOWN;
24097 if (it->bidi_p)
24099 glyph->resolved_level = it->bidi_it.resolved_level;
24100 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24101 abort ();
24102 glyph->bidi_type = it->bidi_it.type;
24104 ++it->glyph_row->used[area];
24106 else
24107 IT_EXPAND_MATRIX_WIDTH (it, area);
24111 /* Produce a glyph for a glyphless character for iterator IT.
24112 IT->glyphless_method specifies which method to use for displaying
24113 the character. See the description of enum
24114 glyphless_display_method in dispextern.h for the detail.
24116 FOR_NO_FONT is nonzero if and only if this is for a character for
24117 which no font was found. ACRONYM, if non-nil, is an acronym string
24118 for the character. */
24120 static void
24121 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24123 int face_id;
24124 struct face *face;
24125 struct font *font;
24126 int base_width, base_height, width, height;
24127 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24128 int len;
24130 /* Get the metrics of the base font. We always refer to the current
24131 ASCII face. */
24132 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24133 font = face->font ? face->font : FRAME_FONT (it->f);
24134 it->ascent = FONT_BASE (font) + font->baseline_offset;
24135 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24136 base_height = it->ascent + it->descent;
24137 base_width = font->average_width;
24139 /* Get a face ID for the glyph by utilizing a cache (the same way as
24140 done for `escape-glyph' in get_next_display_element). */
24141 if (it->f == last_glyphless_glyph_frame
24142 && it->face_id == last_glyphless_glyph_face_id)
24144 face_id = last_glyphless_glyph_merged_face_id;
24146 else
24148 /* Merge the `glyphless-char' face into the current face. */
24149 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24150 last_glyphless_glyph_frame = it->f;
24151 last_glyphless_glyph_face_id = it->face_id;
24152 last_glyphless_glyph_merged_face_id = face_id;
24155 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24157 it->pixel_width = THIN_SPACE_WIDTH;
24158 len = 0;
24159 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24161 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24163 width = CHAR_WIDTH (it->c);
24164 if (width == 0)
24165 width = 1;
24166 else if (width > 4)
24167 width = 4;
24168 it->pixel_width = base_width * width;
24169 len = 0;
24170 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24172 else
24174 char buf[7];
24175 const char *str;
24176 unsigned int code[6];
24177 int upper_len;
24178 int ascent, descent;
24179 struct font_metrics metrics_upper, metrics_lower;
24181 face = FACE_FROM_ID (it->f, face_id);
24182 font = face->font ? face->font : FRAME_FONT (it->f);
24183 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24185 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24187 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24188 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24189 if (CONSP (acronym))
24190 acronym = XCAR (acronym);
24191 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24193 else
24195 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24196 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24197 str = buf;
24199 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24200 code[len] = font->driver->encode_char (font, str[len]);
24201 upper_len = (len + 1) / 2;
24202 font->driver->text_extents (font, code, upper_len,
24203 &metrics_upper);
24204 font->driver->text_extents (font, code + upper_len, len - upper_len,
24205 &metrics_lower);
24209 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24210 width = max (metrics_upper.width, metrics_lower.width) + 4;
24211 upper_xoff = upper_yoff = 2; /* the typical case */
24212 if (base_width >= width)
24214 /* Align the upper to the left, the lower to the right. */
24215 it->pixel_width = base_width;
24216 lower_xoff = base_width - 2 - metrics_lower.width;
24218 else
24220 /* Center the shorter one. */
24221 it->pixel_width = width;
24222 if (metrics_upper.width >= metrics_lower.width)
24223 lower_xoff = (width - metrics_lower.width) / 2;
24224 else
24226 /* FIXME: This code doesn't look right. It formerly was
24227 missing the "lower_xoff = 0;", which couldn't have
24228 been right since it left lower_xoff uninitialized. */
24229 lower_xoff = 0;
24230 upper_xoff = (width - metrics_upper.width) / 2;
24234 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24235 top, bottom, and between upper and lower strings. */
24236 height = (metrics_upper.ascent + metrics_upper.descent
24237 + metrics_lower.ascent + metrics_lower.descent) + 5;
24238 /* Center vertically.
24239 H:base_height, D:base_descent
24240 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24242 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24243 descent = D - H/2 + h/2;
24244 lower_yoff = descent - 2 - ld;
24245 upper_yoff = lower_yoff - la - 1 - ud; */
24246 ascent = - (it->descent - (base_height + height + 1) / 2);
24247 descent = it->descent - (base_height - height) / 2;
24248 lower_yoff = descent - 2 - metrics_lower.descent;
24249 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24250 - metrics_upper.descent);
24251 /* Don't make the height shorter than the base height. */
24252 if (height > base_height)
24254 it->ascent = ascent;
24255 it->descent = descent;
24259 it->phys_ascent = it->ascent;
24260 it->phys_descent = it->descent;
24261 if (it->glyph_row)
24262 append_glyphless_glyph (it, face_id, for_no_font, len,
24263 upper_xoff, upper_yoff,
24264 lower_xoff, lower_yoff);
24265 it->nglyphs = 1;
24266 take_vertical_position_into_account (it);
24270 /* RIF:
24271 Produce glyphs/get display metrics for the display element IT is
24272 loaded with. See the description of struct it in dispextern.h
24273 for an overview of struct it. */
24275 void
24276 x_produce_glyphs (struct it *it)
24278 int extra_line_spacing = it->extra_line_spacing;
24280 it->glyph_not_available_p = 0;
24282 if (it->what == IT_CHARACTER)
24284 XChar2b char2b;
24285 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24286 struct font *font = face->font;
24287 struct font_metrics *pcm = NULL;
24288 int boff; /* baseline offset */
24290 if (font == NULL)
24292 /* When no suitable font is found, display this character by
24293 the method specified in the first extra slot of
24294 Vglyphless_char_display. */
24295 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24297 xassert (it->what == IT_GLYPHLESS);
24298 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24299 goto done;
24302 boff = font->baseline_offset;
24303 if (font->vertical_centering)
24304 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24306 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24308 int stretched_p;
24310 it->nglyphs = 1;
24312 if (it->override_ascent >= 0)
24314 it->ascent = it->override_ascent;
24315 it->descent = it->override_descent;
24316 boff = it->override_boff;
24318 else
24320 it->ascent = FONT_BASE (font) + boff;
24321 it->descent = FONT_DESCENT (font) - boff;
24324 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24326 pcm = get_per_char_metric (font, &char2b);
24327 if (pcm->width == 0
24328 && pcm->rbearing == 0 && pcm->lbearing == 0)
24329 pcm = NULL;
24332 if (pcm)
24334 it->phys_ascent = pcm->ascent + boff;
24335 it->phys_descent = pcm->descent - boff;
24336 it->pixel_width = pcm->width;
24338 else
24340 it->glyph_not_available_p = 1;
24341 it->phys_ascent = it->ascent;
24342 it->phys_descent = it->descent;
24343 it->pixel_width = font->space_width;
24346 if (it->constrain_row_ascent_descent_p)
24348 if (it->descent > it->max_descent)
24350 it->ascent += it->descent - it->max_descent;
24351 it->descent = it->max_descent;
24353 if (it->ascent > it->max_ascent)
24355 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24356 it->ascent = it->max_ascent;
24358 it->phys_ascent = min (it->phys_ascent, it->ascent);
24359 it->phys_descent = min (it->phys_descent, it->descent);
24360 extra_line_spacing = 0;
24363 /* If this is a space inside a region of text with
24364 `space-width' property, change its width. */
24365 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24366 if (stretched_p)
24367 it->pixel_width *= XFLOATINT (it->space_width);
24369 /* If face has a box, add the box thickness to the character
24370 height. If character has a box line to the left and/or
24371 right, add the box line width to the character's width. */
24372 if (face->box != FACE_NO_BOX)
24374 int thick = face->box_line_width;
24376 if (thick > 0)
24378 it->ascent += thick;
24379 it->descent += thick;
24381 else
24382 thick = -thick;
24384 if (it->start_of_box_run_p)
24385 it->pixel_width += thick;
24386 if (it->end_of_box_run_p)
24387 it->pixel_width += thick;
24390 /* If face has an overline, add the height of the overline
24391 (1 pixel) and a 1 pixel margin to the character height. */
24392 if (face->overline_p)
24393 it->ascent += overline_margin;
24395 if (it->constrain_row_ascent_descent_p)
24397 if (it->ascent > it->max_ascent)
24398 it->ascent = it->max_ascent;
24399 if (it->descent > it->max_descent)
24400 it->descent = it->max_descent;
24403 take_vertical_position_into_account (it);
24405 /* If we have to actually produce glyphs, do it. */
24406 if (it->glyph_row)
24408 if (stretched_p)
24410 /* Translate a space with a `space-width' property
24411 into a stretch glyph. */
24412 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24413 / FONT_HEIGHT (font));
24414 append_stretch_glyph (it, it->object, it->pixel_width,
24415 it->ascent + it->descent, ascent);
24417 else
24418 append_glyph (it);
24420 /* If characters with lbearing or rbearing are displayed
24421 in this line, record that fact in a flag of the
24422 glyph row. This is used to optimize X output code. */
24423 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24424 it->glyph_row->contains_overlapping_glyphs_p = 1;
24426 if (! stretched_p && it->pixel_width == 0)
24427 /* We assure that all visible glyphs have at least 1-pixel
24428 width. */
24429 it->pixel_width = 1;
24431 else if (it->char_to_display == '\n')
24433 /* A newline has no width, but we need the height of the
24434 line. But if previous part of the line sets a height,
24435 don't increase that height */
24437 Lisp_Object height;
24438 Lisp_Object total_height = Qnil;
24440 it->override_ascent = -1;
24441 it->pixel_width = 0;
24442 it->nglyphs = 0;
24444 height = get_it_property (it, Qline_height);
24445 /* Split (line-height total-height) list */
24446 if (CONSP (height)
24447 && CONSP (XCDR (height))
24448 && NILP (XCDR (XCDR (height))))
24450 total_height = XCAR (XCDR (height));
24451 height = XCAR (height);
24453 height = calc_line_height_property (it, height, font, boff, 1);
24455 if (it->override_ascent >= 0)
24457 it->ascent = it->override_ascent;
24458 it->descent = it->override_descent;
24459 boff = it->override_boff;
24461 else
24463 it->ascent = FONT_BASE (font) + boff;
24464 it->descent = FONT_DESCENT (font) - boff;
24467 if (EQ (height, Qt))
24469 if (it->descent > it->max_descent)
24471 it->ascent += it->descent - it->max_descent;
24472 it->descent = it->max_descent;
24474 if (it->ascent > it->max_ascent)
24476 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24477 it->ascent = it->max_ascent;
24479 it->phys_ascent = min (it->phys_ascent, it->ascent);
24480 it->phys_descent = min (it->phys_descent, it->descent);
24481 it->constrain_row_ascent_descent_p = 1;
24482 extra_line_spacing = 0;
24484 else
24486 Lisp_Object spacing;
24488 it->phys_ascent = it->ascent;
24489 it->phys_descent = it->descent;
24491 if ((it->max_ascent > 0 || it->max_descent > 0)
24492 && face->box != FACE_NO_BOX
24493 && face->box_line_width > 0)
24495 it->ascent += face->box_line_width;
24496 it->descent += face->box_line_width;
24498 if (!NILP (height)
24499 && XINT (height) > it->ascent + it->descent)
24500 it->ascent = XINT (height) - it->descent;
24502 if (!NILP (total_height))
24503 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24504 else
24506 spacing = get_it_property (it, Qline_spacing);
24507 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24509 if (INTEGERP (spacing))
24511 extra_line_spacing = XINT (spacing);
24512 if (!NILP (total_height))
24513 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24517 else /* i.e. (it->char_to_display == '\t') */
24519 if (font->space_width > 0)
24521 int tab_width = it->tab_width * font->space_width;
24522 int x = it->current_x + it->continuation_lines_width;
24523 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24525 /* If the distance from the current position to the next tab
24526 stop is less than a space character width, use the
24527 tab stop after that. */
24528 if (next_tab_x - x < font->space_width)
24529 next_tab_x += tab_width;
24531 it->pixel_width = next_tab_x - x;
24532 it->nglyphs = 1;
24533 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24534 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24536 if (it->glyph_row)
24538 append_stretch_glyph (it, it->object, it->pixel_width,
24539 it->ascent + it->descent, it->ascent);
24542 else
24544 it->pixel_width = 0;
24545 it->nglyphs = 1;
24549 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24551 /* A static composition.
24553 Note: A composition is represented as one glyph in the
24554 glyph matrix. There are no padding glyphs.
24556 Important note: pixel_width, ascent, and descent are the
24557 values of what is drawn by draw_glyphs (i.e. the values of
24558 the overall glyphs composed). */
24559 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24560 int boff; /* baseline offset */
24561 struct composition *cmp = composition_table[it->cmp_it.id];
24562 int glyph_len = cmp->glyph_len;
24563 struct font *font = face->font;
24565 it->nglyphs = 1;
24567 /* If we have not yet calculated pixel size data of glyphs of
24568 the composition for the current face font, calculate them
24569 now. Theoretically, we have to check all fonts for the
24570 glyphs, but that requires much time and memory space. So,
24571 here we check only the font of the first glyph. This may
24572 lead to incorrect display, but it's very rare, and C-l
24573 (recenter-top-bottom) can correct the display anyway. */
24574 if (! cmp->font || cmp->font != font)
24576 /* Ascent and descent of the font of the first character
24577 of this composition (adjusted by baseline offset).
24578 Ascent and descent of overall glyphs should not be less
24579 than these, respectively. */
24580 int font_ascent, font_descent, font_height;
24581 /* Bounding box of the overall glyphs. */
24582 int leftmost, rightmost, lowest, highest;
24583 int lbearing, rbearing;
24584 int i, width, ascent, descent;
24585 int left_padded = 0, right_padded = 0;
24586 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24587 XChar2b char2b;
24588 struct font_metrics *pcm;
24589 int font_not_found_p;
24590 EMACS_INT pos;
24592 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24593 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24594 break;
24595 if (glyph_len < cmp->glyph_len)
24596 right_padded = 1;
24597 for (i = 0; i < glyph_len; i++)
24599 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24600 break;
24601 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24603 if (i > 0)
24604 left_padded = 1;
24606 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24607 : IT_CHARPOS (*it));
24608 /* If no suitable font is found, use the default font. */
24609 font_not_found_p = font == NULL;
24610 if (font_not_found_p)
24612 face = face->ascii_face;
24613 font = face->font;
24615 boff = font->baseline_offset;
24616 if (font->vertical_centering)
24617 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24618 font_ascent = FONT_BASE (font) + boff;
24619 font_descent = FONT_DESCENT (font) - boff;
24620 font_height = FONT_HEIGHT (font);
24622 cmp->font = (void *) font;
24624 pcm = NULL;
24625 if (! font_not_found_p)
24627 get_char_face_and_encoding (it->f, c, it->face_id,
24628 &char2b, 0);
24629 pcm = get_per_char_metric (font, &char2b);
24632 /* Initialize the bounding box. */
24633 if (pcm)
24635 width = cmp->glyph_len > 0 ? pcm->width : 0;
24636 ascent = pcm->ascent;
24637 descent = pcm->descent;
24638 lbearing = pcm->lbearing;
24639 rbearing = pcm->rbearing;
24641 else
24643 width = cmp->glyph_len > 0 ? font->space_width : 0;
24644 ascent = FONT_BASE (font);
24645 descent = FONT_DESCENT (font);
24646 lbearing = 0;
24647 rbearing = width;
24650 rightmost = width;
24651 leftmost = 0;
24652 lowest = - descent + boff;
24653 highest = ascent + boff;
24655 if (! font_not_found_p
24656 && font->default_ascent
24657 && CHAR_TABLE_P (Vuse_default_ascent)
24658 && !NILP (Faref (Vuse_default_ascent,
24659 make_number (it->char_to_display))))
24660 highest = font->default_ascent + boff;
24662 /* Draw the first glyph at the normal position. It may be
24663 shifted to right later if some other glyphs are drawn
24664 at the left. */
24665 cmp->offsets[i * 2] = 0;
24666 cmp->offsets[i * 2 + 1] = boff;
24667 cmp->lbearing = lbearing;
24668 cmp->rbearing = rbearing;
24670 /* Set cmp->offsets for the remaining glyphs. */
24671 for (i++; i < glyph_len; i++)
24673 int left, right, btm, top;
24674 int ch = COMPOSITION_GLYPH (cmp, i);
24675 int face_id;
24676 struct face *this_face;
24678 if (ch == '\t')
24679 ch = ' ';
24680 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24681 this_face = FACE_FROM_ID (it->f, face_id);
24682 font = this_face->font;
24684 if (font == NULL)
24685 pcm = NULL;
24686 else
24688 get_char_face_and_encoding (it->f, ch, face_id,
24689 &char2b, 0);
24690 pcm = get_per_char_metric (font, &char2b);
24692 if (! pcm)
24693 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24694 else
24696 width = pcm->width;
24697 ascent = pcm->ascent;
24698 descent = pcm->descent;
24699 lbearing = pcm->lbearing;
24700 rbearing = pcm->rbearing;
24701 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
24703 /* Relative composition with or without
24704 alternate chars. */
24705 left = (leftmost + rightmost - width) / 2;
24706 btm = - descent + boff;
24707 if (font->relative_compose
24708 && (! CHAR_TABLE_P (Vignore_relative_composition)
24709 || NILP (Faref (Vignore_relative_composition,
24710 make_number (ch)))))
24713 if (- descent >= font->relative_compose)
24714 /* One extra pixel between two glyphs. */
24715 btm = highest + 1;
24716 else if (ascent <= 0)
24717 /* One extra pixel between two glyphs. */
24718 btm = lowest - 1 - ascent - descent;
24721 else
24723 /* A composition rule is specified by an integer
24724 value that encodes global and new reference
24725 points (GREF and NREF). GREF and NREF are
24726 specified by numbers as below:
24728 0---1---2 -- ascent
24732 9--10--11 -- center
24734 ---3---4---5--- baseline
24736 6---7---8 -- descent
24738 int rule = COMPOSITION_RULE (cmp, i);
24739 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
24741 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
24742 grefx = gref % 3, nrefx = nref % 3;
24743 grefy = gref / 3, nrefy = nref / 3;
24744 if (xoff)
24745 xoff = font_height * (xoff - 128) / 256;
24746 if (yoff)
24747 yoff = font_height * (yoff - 128) / 256;
24749 left = (leftmost
24750 + grefx * (rightmost - leftmost) / 2
24751 - nrefx * width / 2
24752 + xoff);
24754 btm = ((grefy == 0 ? highest
24755 : grefy == 1 ? 0
24756 : grefy == 2 ? lowest
24757 : (highest + lowest) / 2)
24758 - (nrefy == 0 ? ascent + descent
24759 : nrefy == 1 ? descent - boff
24760 : nrefy == 2 ? 0
24761 : (ascent + descent) / 2)
24762 + yoff);
24765 cmp->offsets[i * 2] = left;
24766 cmp->offsets[i * 2 + 1] = btm + descent;
24768 /* Update the bounding box of the overall glyphs. */
24769 if (width > 0)
24771 right = left + width;
24772 if (left < leftmost)
24773 leftmost = left;
24774 if (right > rightmost)
24775 rightmost = right;
24777 top = btm + descent + ascent;
24778 if (top > highest)
24779 highest = top;
24780 if (btm < lowest)
24781 lowest = btm;
24783 if (cmp->lbearing > left + lbearing)
24784 cmp->lbearing = left + lbearing;
24785 if (cmp->rbearing < left + rbearing)
24786 cmp->rbearing = left + rbearing;
24790 /* If there are glyphs whose x-offsets are negative,
24791 shift all glyphs to the right and make all x-offsets
24792 non-negative. */
24793 if (leftmost < 0)
24795 for (i = 0; i < cmp->glyph_len; i++)
24796 cmp->offsets[i * 2] -= leftmost;
24797 rightmost -= leftmost;
24798 cmp->lbearing -= leftmost;
24799 cmp->rbearing -= leftmost;
24802 if (left_padded && cmp->lbearing < 0)
24804 for (i = 0; i < cmp->glyph_len; i++)
24805 cmp->offsets[i * 2] -= cmp->lbearing;
24806 rightmost -= cmp->lbearing;
24807 cmp->rbearing -= cmp->lbearing;
24808 cmp->lbearing = 0;
24810 if (right_padded && rightmost < cmp->rbearing)
24812 rightmost = cmp->rbearing;
24815 cmp->pixel_width = rightmost;
24816 cmp->ascent = highest;
24817 cmp->descent = - lowest;
24818 if (cmp->ascent < font_ascent)
24819 cmp->ascent = font_ascent;
24820 if (cmp->descent < font_descent)
24821 cmp->descent = font_descent;
24824 if (it->glyph_row
24825 && (cmp->lbearing < 0
24826 || cmp->rbearing > cmp->pixel_width))
24827 it->glyph_row->contains_overlapping_glyphs_p = 1;
24829 it->pixel_width = cmp->pixel_width;
24830 it->ascent = it->phys_ascent = cmp->ascent;
24831 it->descent = it->phys_descent = cmp->descent;
24832 if (face->box != FACE_NO_BOX)
24834 int thick = face->box_line_width;
24836 if (thick > 0)
24838 it->ascent += thick;
24839 it->descent += thick;
24841 else
24842 thick = - thick;
24844 if (it->start_of_box_run_p)
24845 it->pixel_width += thick;
24846 if (it->end_of_box_run_p)
24847 it->pixel_width += thick;
24850 /* If face has an overline, add the height of the overline
24851 (1 pixel) and a 1 pixel margin to the character height. */
24852 if (face->overline_p)
24853 it->ascent += overline_margin;
24855 take_vertical_position_into_account (it);
24856 if (it->ascent < 0)
24857 it->ascent = 0;
24858 if (it->descent < 0)
24859 it->descent = 0;
24861 if (it->glyph_row && cmp->glyph_len > 0)
24862 append_composite_glyph (it);
24864 else if (it->what == IT_COMPOSITION)
24866 /* A dynamic (automatic) composition. */
24867 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24868 Lisp_Object gstring;
24869 struct font_metrics metrics;
24871 it->nglyphs = 1;
24873 gstring = composition_gstring_from_id (it->cmp_it.id);
24874 it->pixel_width
24875 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
24876 &metrics);
24877 if (it->glyph_row
24878 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
24879 it->glyph_row->contains_overlapping_glyphs_p = 1;
24880 it->ascent = it->phys_ascent = metrics.ascent;
24881 it->descent = it->phys_descent = metrics.descent;
24882 if (face->box != FACE_NO_BOX)
24884 int thick = face->box_line_width;
24886 if (thick > 0)
24888 it->ascent += thick;
24889 it->descent += thick;
24891 else
24892 thick = - thick;
24894 if (it->start_of_box_run_p)
24895 it->pixel_width += thick;
24896 if (it->end_of_box_run_p)
24897 it->pixel_width += thick;
24899 /* If face has an overline, add the height of the overline
24900 (1 pixel) and a 1 pixel margin to the character height. */
24901 if (face->overline_p)
24902 it->ascent += overline_margin;
24903 take_vertical_position_into_account (it);
24904 if (it->ascent < 0)
24905 it->ascent = 0;
24906 if (it->descent < 0)
24907 it->descent = 0;
24909 if (it->glyph_row)
24910 append_composite_glyph (it);
24912 else if (it->what == IT_GLYPHLESS)
24913 produce_glyphless_glyph (it, 0, Qnil);
24914 else if (it->what == IT_IMAGE)
24915 produce_image_glyph (it);
24916 else if (it->what == IT_STRETCH)
24917 produce_stretch_glyph (it);
24919 done:
24920 /* Accumulate dimensions. Note: can't assume that it->descent > 0
24921 because this isn't true for images with `:ascent 100'. */
24922 xassert (it->ascent >= 0 && it->descent >= 0);
24923 if (it->area == TEXT_AREA)
24924 it->current_x += it->pixel_width;
24926 if (extra_line_spacing > 0)
24928 it->descent += extra_line_spacing;
24929 if (extra_line_spacing > it->max_extra_line_spacing)
24930 it->max_extra_line_spacing = extra_line_spacing;
24933 it->max_ascent = max (it->max_ascent, it->ascent);
24934 it->max_descent = max (it->max_descent, it->descent);
24935 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
24936 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
24939 /* EXPORT for RIF:
24940 Output LEN glyphs starting at START at the nominal cursor position.
24941 Advance the nominal cursor over the text. The global variable
24942 updated_window contains the window being updated, updated_row is
24943 the glyph row being updated, and updated_area is the area of that
24944 row being updated. */
24946 void
24947 x_write_glyphs (struct glyph *start, int len)
24949 int x, hpos, chpos = updated_window->phys_cursor.hpos;
24951 xassert (updated_window && updated_row);
24952 /* When the window is hscrolled, cursor hpos can legitimately be out
24953 of bounds, but we draw the cursor at the corresponding window
24954 margin in that case. */
24955 if (!updated_row->reversed_p && chpos < 0)
24956 chpos = 0;
24957 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
24958 chpos = updated_row->used[TEXT_AREA] - 1;
24960 BLOCK_INPUT;
24962 /* Write glyphs. */
24964 hpos = start - updated_row->glyphs[updated_area];
24965 x = draw_glyphs (updated_window, output_cursor.x,
24966 updated_row, updated_area,
24967 hpos, hpos + len,
24968 DRAW_NORMAL_TEXT, 0);
24970 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
24971 if (updated_area == TEXT_AREA
24972 && updated_window->phys_cursor_on_p
24973 && updated_window->phys_cursor.vpos == output_cursor.vpos
24974 && chpos >= hpos
24975 && chpos < hpos + len)
24976 updated_window->phys_cursor_on_p = 0;
24978 UNBLOCK_INPUT;
24980 /* Advance the output cursor. */
24981 output_cursor.hpos += len;
24982 output_cursor.x = x;
24986 /* EXPORT for RIF:
24987 Insert LEN glyphs from START at the nominal cursor position. */
24989 void
24990 x_insert_glyphs (struct glyph *start, int len)
24992 struct frame *f;
24993 struct window *w;
24994 int line_height, shift_by_width, shifted_region_width;
24995 struct glyph_row *row;
24996 struct glyph *glyph;
24997 int frame_x, frame_y;
24998 EMACS_INT hpos;
25000 xassert (updated_window && updated_row);
25001 BLOCK_INPUT;
25002 w = updated_window;
25003 f = XFRAME (WINDOW_FRAME (w));
25005 /* Get the height of the line we are in. */
25006 row = updated_row;
25007 line_height = row->height;
25009 /* Get the width of the glyphs to insert. */
25010 shift_by_width = 0;
25011 for (glyph = start; glyph < start + len; ++glyph)
25012 shift_by_width += glyph->pixel_width;
25014 /* Get the width of the region to shift right. */
25015 shifted_region_width = (window_box_width (w, updated_area)
25016 - output_cursor.x
25017 - shift_by_width);
25019 /* Shift right. */
25020 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25021 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25023 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25024 line_height, shift_by_width);
25026 /* Write the glyphs. */
25027 hpos = start - row->glyphs[updated_area];
25028 draw_glyphs (w, output_cursor.x, row, updated_area,
25029 hpos, hpos + len,
25030 DRAW_NORMAL_TEXT, 0);
25032 /* Advance the output cursor. */
25033 output_cursor.hpos += len;
25034 output_cursor.x += shift_by_width;
25035 UNBLOCK_INPUT;
25039 /* EXPORT for RIF:
25040 Erase the current text line from the nominal cursor position
25041 (inclusive) to pixel column TO_X (exclusive). The idea is that
25042 everything from TO_X onward is already erased.
25044 TO_X is a pixel position relative to updated_area of
25045 updated_window. TO_X == -1 means clear to the end of this area. */
25047 void
25048 x_clear_end_of_line (int to_x)
25050 struct frame *f;
25051 struct window *w = updated_window;
25052 int max_x, min_y, max_y;
25053 int from_x, from_y, to_y;
25055 xassert (updated_window && updated_row);
25056 f = XFRAME (w->frame);
25058 if (updated_row->full_width_p)
25059 max_x = WINDOW_TOTAL_WIDTH (w);
25060 else
25061 max_x = window_box_width (w, updated_area);
25062 max_y = window_text_bottom_y (w);
25064 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25065 of window. For TO_X > 0, truncate to end of drawing area. */
25066 if (to_x == 0)
25067 return;
25068 else if (to_x < 0)
25069 to_x = max_x;
25070 else
25071 to_x = min (to_x, max_x);
25073 to_y = min (max_y, output_cursor.y + updated_row->height);
25075 /* Notice if the cursor will be cleared by this operation. */
25076 if (!updated_row->full_width_p)
25077 notice_overwritten_cursor (w, updated_area,
25078 output_cursor.x, -1,
25079 updated_row->y,
25080 MATRIX_ROW_BOTTOM_Y (updated_row));
25082 from_x = output_cursor.x;
25084 /* Translate to frame coordinates. */
25085 if (updated_row->full_width_p)
25087 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25088 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25090 else
25092 int area_left = window_box_left (w, updated_area);
25093 from_x += area_left;
25094 to_x += area_left;
25097 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25098 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25099 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25101 /* Prevent inadvertently clearing to end of the X window. */
25102 if (to_x > from_x && to_y > from_y)
25104 BLOCK_INPUT;
25105 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25106 to_x - from_x, to_y - from_y);
25107 UNBLOCK_INPUT;
25111 #endif /* HAVE_WINDOW_SYSTEM */
25115 /***********************************************************************
25116 Cursor types
25117 ***********************************************************************/
25119 /* Value is the internal representation of the specified cursor type
25120 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25121 of the bar cursor. */
25123 static enum text_cursor_kinds
25124 get_specified_cursor_type (Lisp_Object arg, int *width)
25126 enum text_cursor_kinds type;
25128 if (NILP (arg))
25129 return NO_CURSOR;
25131 if (EQ (arg, Qbox))
25132 return FILLED_BOX_CURSOR;
25134 if (EQ (arg, Qhollow))
25135 return HOLLOW_BOX_CURSOR;
25137 if (EQ (arg, Qbar))
25139 *width = 2;
25140 return BAR_CURSOR;
25143 if (CONSP (arg)
25144 && EQ (XCAR (arg), Qbar)
25145 && INTEGERP (XCDR (arg))
25146 && XINT (XCDR (arg)) >= 0)
25148 *width = XINT (XCDR (arg));
25149 return BAR_CURSOR;
25152 if (EQ (arg, Qhbar))
25154 *width = 2;
25155 return HBAR_CURSOR;
25158 if (CONSP (arg)
25159 && EQ (XCAR (arg), Qhbar)
25160 && INTEGERP (XCDR (arg))
25161 && XINT (XCDR (arg)) >= 0)
25163 *width = XINT (XCDR (arg));
25164 return HBAR_CURSOR;
25167 /* Treat anything unknown as "hollow box cursor".
25168 It was bad to signal an error; people have trouble fixing
25169 .Xdefaults with Emacs, when it has something bad in it. */
25170 type = HOLLOW_BOX_CURSOR;
25172 return type;
25175 /* Set the default cursor types for specified frame. */
25176 void
25177 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25179 int width = 1;
25180 Lisp_Object tem;
25182 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25183 FRAME_CURSOR_WIDTH (f) = width;
25185 /* By default, set up the blink-off state depending on the on-state. */
25187 tem = Fassoc (arg, Vblink_cursor_alist);
25188 if (!NILP (tem))
25190 FRAME_BLINK_OFF_CURSOR (f)
25191 = get_specified_cursor_type (XCDR (tem), &width);
25192 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25194 else
25195 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25199 #ifdef HAVE_WINDOW_SYSTEM
25201 /* Return the cursor we want to be displayed in window W. Return
25202 width of bar/hbar cursor through WIDTH arg. Return with
25203 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25204 (i.e. if the `system caret' should track this cursor).
25206 In a mini-buffer window, we want the cursor only to appear if we
25207 are reading input from this window. For the selected window, we
25208 want the cursor type given by the frame parameter or buffer local
25209 setting of cursor-type. If explicitly marked off, draw no cursor.
25210 In all other cases, we want a hollow box cursor. */
25212 static enum text_cursor_kinds
25213 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25214 int *active_cursor)
25216 struct frame *f = XFRAME (w->frame);
25217 struct buffer *b = XBUFFER (w->buffer);
25218 int cursor_type = DEFAULT_CURSOR;
25219 Lisp_Object alt_cursor;
25220 int non_selected = 0;
25222 *active_cursor = 1;
25224 /* Echo area */
25225 if (cursor_in_echo_area
25226 && FRAME_HAS_MINIBUF_P (f)
25227 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25229 if (w == XWINDOW (echo_area_window))
25231 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25233 *width = FRAME_CURSOR_WIDTH (f);
25234 return FRAME_DESIRED_CURSOR (f);
25236 else
25237 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25240 *active_cursor = 0;
25241 non_selected = 1;
25244 /* Detect a nonselected window or nonselected frame. */
25245 else if (w != XWINDOW (f->selected_window)
25246 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25248 *active_cursor = 0;
25250 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25251 return NO_CURSOR;
25253 non_selected = 1;
25256 /* Never display a cursor in a window in which cursor-type is nil. */
25257 if (NILP (BVAR (b, cursor_type)))
25258 return NO_CURSOR;
25260 /* Get the normal cursor type for this window. */
25261 if (EQ (BVAR (b, cursor_type), Qt))
25263 cursor_type = FRAME_DESIRED_CURSOR (f);
25264 *width = FRAME_CURSOR_WIDTH (f);
25266 else
25267 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25269 /* Use cursor-in-non-selected-windows instead
25270 for non-selected window or frame. */
25271 if (non_selected)
25273 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25274 if (!EQ (Qt, alt_cursor))
25275 return get_specified_cursor_type (alt_cursor, width);
25276 /* t means modify the normal cursor type. */
25277 if (cursor_type == FILLED_BOX_CURSOR)
25278 cursor_type = HOLLOW_BOX_CURSOR;
25279 else if (cursor_type == BAR_CURSOR && *width > 1)
25280 --*width;
25281 return cursor_type;
25284 /* Use normal cursor if not blinked off. */
25285 if (!w->cursor_off_p)
25287 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25289 if (cursor_type == FILLED_BOX_CURSOR)
25291 /* Using a block cursor on large images can be very annoying.
25292 So use a hollow cursor for "large" images.
25293 If image is not transparent (no mask), also use hollow cursor. */
25294 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25295 if (img != NULL && IMAGEP (img->spec))
25297 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25298 where N = size of default frame font size.
25299 This should cover most of the "tiny" icons people may use. */
25300 if (!img->mask
25301 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25302 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25303 cursor_type = HOLLOW_BOX_CURSOR;
25306 else if (cursor_type != NO_CURSOR)
25308 /* Display current only supports BOX and HOLLOW cursors for images.
25309 So for now, unconditionally use a HOLLOW cursor when cursor is
25310 not a solid box cursor. */
25311 cursor_type = HOLLOW_BOX_CURSOR;
25314 return cursor_type;
25317 /* Cursor is blinked off, so determine how to "toggle" it. */
25319 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25320 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25321 return get_specified_cursor_type (XCDR (alt_cursor), width);
25323 /* Then see if frame has specified a specific blink off cursor type. */
25324 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25326 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25327 return FRAME_BLINK_OFF_CURSOR (f);
25330 #if 0
25331 /* Some people liked having a permanently visible blinking cursor,
25332 while others had very strong opinions against it. So it was
25333 decided to remove it. KFS 2003-09-03 */
25335 /* Finally perform built-in cursor blinking:
25336 filled box <-> hollow box
25337 wide [h]bar <-> narrow [h]bar
25338 narrow [h]bar <-> no cursor
25339 other type <-> no cursor */
25341 if (cursor_type == FILLED_BOX_CURSOR)
25342 return HOLLOW_BOX_CURSOR;
25344 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25346 *width = 1;
25347 return cursor_type;
25349 #endif
25351 return NO_CURSOR;
25355 /* Notice when the text cursor of window W has been completely
25356 overwritten by a drawing operation that outputs glyphs in AREA
25357 starting at X0 and ending at X1 in the line starting at Y0 and
25358 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25359 the rest of the line after X0 has been written. Y coordinates
25360 are window-relative. */
25362 static void
25363 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25364 int x0, int x1, int y0, int y1)
25366 int cx0, cx1, cy0, cy1;
25367 struct glyph_row *row;
25369 if (!w->phys_cursor_on_p)
25370 return;
25371 if (area != TEXT_AREA)
25372 return;
25374 if (w->phys_cursor.vpos < 0
25375 || w->phys_cursor.vpos >= w->current_matrix->nrows
25376 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25377 !(row->enabled_p && row->displays_text_p)))
25378 return;
25380 if (row->cursor_in_fringe_p)
25382 row->cursor_in_fringe_p = 0;
25383 draw_fringe_bitmap (w, row, row->reversed_p);
25384 w->phys_cursor_on_p = 0;
25385 return;
25388 cx0 = w->phys_cursor.x;
25389 cx1 = cx0 + w->phys_cursor_width;
25390 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25391 return;
25393 /* The cursor image will be completely removed from the
25394 screen if the output area intersects the cursor area in
25395 y-direction. When we draw in [y0 y1[, and some part of
25396 the cursor is at y < y0, that part must have been drawn
25397 before. When scrolling, the cursor is erased before
25398 actually scrolling, so we don't come here. When not
25399 scrolling, the rows above the old cursor row must have
25400 changed, and in this case these rows must have written
25401 over the cursor image.
25403 Likewise if part of the cursor is below y1, with the
25404 exception of the cursor being in the first blank row at
25405 the buffer and window end because update_text_area
25406 doesn't draw that row. (Except when it does, but
25407 that's handled in update_text_area.) */
25409 cy0 = w->phys_cursor.y;
25410 cy1 = cy0 + w->phys_cursor_height;
25411 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25412 return;
25414 w->phys_cursor_on_p = 0;
25417 #endif /* HAVE_WINDOW_SYSTEM */
25420 /************************************************************************
25421 Mouse Face
25422 ************************************************************************/
25424 #ifdef HAVE_WINDOW_SYSTEM
25426 /* EXPORT for RIF:
25427 Fix the display of area AREA of overlapping row ROW in window W
25428 with respect to the overlapping part OVERLAPS. */
25430 void
25431 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25432 enum glyph_row_area area, int overlaps)
25434 int i, x;
25436 BLOCK_INPUT;
25438 x = 0;
25439 for (i = 0; i < row->used[area];)
25441 if (row->glyphs[area][i].overlaps_vertically_p)
25443 int start = i, start_x = x;
25447 x += row->glyphs[area][i].pixel_width;
25448 ++i;
25450 while (i < row->used[area]
25451 && row->glyphs[area][i].overlaps_vertically_p);
25453 draw_glyphs (w, start_x, row, area,
25454 start, i,
25455 DRAW_NORMAL_TEXT, overlaps);
25457 else
25459 x += row->glyphs[area][i].pixel_width;
25460 ++i;
25464 UNBLOCK_INPUT;
25468 /* EXPORT:
25469 Draw the cursor glyph of window W in glyph row ROW. See the
25470 comment of draw_glyphs for the meaning of HL. */
25472 void
25473 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25474 enum draw_glyphs_face hl)
25476 /* If cursor hpos is out of bounds, don't draw garbage. This can
25477 happen in mini-buffer windows when switching between echo area
25478 glyphs and mini-buffer. */
25479 if ((row->reversed_p
25480 ? (w->phys_cursor.hpos >= 0)
25481 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25483 int on_p = w->phys_cursor_on_p;
25484 int x1;
25485 int hpos = w->phys_cursor.hpos;
25487 /* When the window is hscrolled, cursor hpos can legitimately be
25488 out of bounds, but we draw the cursor at the corresponding
25489 window margin in that case. */
25490 if (!row->reversed_p && hpos < 0)
25491 hpos = 0;
25492 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25493 hpos = row->used[TEXT_AREA] - 1;
25495 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25496 hl, 0);
25497 w->phys_cursor_on_p = on_p;
25499 if (hl == DRAW_CURSOR)
25500 w->phys_cursor_width = x1 - w->phys_cursor.x;
25501 /* When we erase the cursor, and ROW is overlapped by other
25502 rows, make sure that these overlapping parts of other rows
25503 are redrawn. */
25504 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25506 w->phys_cursor_width = x1 - w->phys_cursor.x;
25508 if (row > w->current_matrix->rows
25509 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25510 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25511 OVERLAPS_ERASED_CURSOR);
25513 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25514 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25515 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25516 OVERLAPS_ERASED_CURSOR);
25522 /* EXPORT:
25523 Erase the image of a cursor of window W from the screen. */
25525 void
25526 erase_phys_cursor (struct window *w)
25528 struct frame *f = XFRAME (w->frame);
25529 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25530 int hpos = w->phys_cursor.hpos;
25531 int vpos = w->phys_cursor.vpos;
25532 int mouse_face_here_p = 0;
25533 struct glyph_matrix *active_glyphs = w->current_matrix;
25534 struct glyph_row *cursor_row;
25535 struct glyph *cursor_glyph;
25536 enum draw_glyphs_face hl;
25538 /* No cursor displayed or row invalidated => nothing to do on the
25539 screen. */
25540 if (w->phys_cursor_type == NO_CURSOR)
25541 goto mark_cursor_off;
25543 /* VPOS >= active_glyphs->nrows means that window has been resized.
25544 Don't bother to erase the cursor. */
25545 if (vpos >= active_glyphs->nrows)
25546 goto mark_cursor_off;
25548 /* If row containing cursor is marked invalid, there is nothing we
25549 can do. */
25550 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25551 if (!cursor_row->enabled_p)
25552 goto mark_cursor_off;
25554 /* If line spacing is > 0, old cursor may only be partially visible in
25555 window after split-window. So adjust visible height. */
25556 cursor_row->visible_height = min (cursor_row->visible_height,
25557 window_text_bottom_y (w) - cursor_row->y);
25559 /* If row is completely invisible, don't attempt to delete a cursor which
25560 isn't there. This can happen if cursor is at top of a window, and
25561 we switch to a buffer with a header line in that window. */
25562 if (cursor_row->visible_height <= 0)
25563 goto mark_cursor_off;
25565 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25566 if (cursor_row->cursor_in_fringe_p)
25568 cursor_row->cursor_in_fringe_p = 0;
25569 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25570 goto mark_cursor_off;
25573 /* This can happen when the new row is shorter than the old one.
25574 In this case, either draw_glyphs or clear_end_of_line
25575 should have cleared the cursor. Note that we wouldn't be
25576 able to erase the cursor in this case because we don't have a
25577 cursor glyph at hand. */
25578 if ((cursor_row->reversed_p
25579 ? (w->phys_cursor.hpos < 0)
25580 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25581 goto mark_cursor_off;
25583 /* When the window is hscrolled, cursor hpos can legitimately be out
25584 of bounds, but we draw the cursor at the corresponding window
25585 margin in that case. */
25586 if (!cursor_row->reversed_p && hpos < 0)
25587 hpos = 0;
25588 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25589 hpos = cursor_row->used[TEXT_AREA] - 1;
25591 /* If the cursor is in the mouse face area, redisplay that when
25592 we clear the cursor. */
25593 if (! NILP (hlinfo->mouse_face_window)
25594 && coords_in_mouse_face_p (w, hpos, vpos)
25595 /* Don't redraw the cursor's spot in mouse face if it is at the
25596 end of a line (on a newline). The cursor appears there, but
25597 mouse highlighting does not. */
25598 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25599 mouse_face_here_p = 1;
25601 /* Maybe clear the display under the cursor. */
25602 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25604 int x, y, left_x;
25605 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25606 int width;
25608 cursor_glyph = get_phys_cursor_glyph (w);
25609 if (cursor_glyph == NULL)
25610 goto mark_cursor_off;
25612 width = cursor_glyph->pixel_width;
25613 left_x = window_box_left_offset (w, TEXT_AREA);
25614 x = w->phys_cursor.x;
25615 if (x < left_x)
25616 width -= left_x - x;
25617 width = min (width, window_box_width (w, TEXT_AREA) - x);
25618 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25619 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25621 if (width > 0)
25622 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25625 /* Erase the cursor by redrawing the character underneath it. */
25626 if (mouse_face_here_p)
25627 hl = DRAW_MOUSE_FACE;
25628 else
25629 hl = DRAW_NORMAL_TEXT;
25630 draw_phys_cursor_glyph (w, cursor_row, hl);
25632 mark_cursor_off:
25633 w->phys_cursor_on_p = 0;
25634 w->phys_cursor_type = NO_CURSOR;
25638 /* EXPORT:
25639 Display or clear cursor of window W. If ON is zero, clear the
25640 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25641 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25643 void
25644 display_and_set_cursor (struct window *w, int on,
25645 int hpos, int vpos, int x, int y)
25647 struct frame *f = XFRAME (w->frame);
25648 int new_cursor_type;
25649 int new_cursor_width;
25650 int active_cursor;
25651 struct glyph_row *glyph_row;
25652 struct glyph *glyph;
25654 /* This is pointless on invisible frames, and dangerous on garbaged
25655 windows and frames; in the latter case, the frame or window may
25656 be in the midst of changing its size, and x and y may be off the
25657 window. */
25658 if (! FRAME_VISIBLE_P (f)
25659 || FRAME_GARBAGED_P (f)
25660 || vpos >= w->current_matrix->nrows
25661 || hpos >= w->current_matrix->matrix_w)
25662 return;
25664 /* If cursor is off and we want it off, return quickly. */
25665 if (!on && !w->phys_cursor_on_p)
25666 return;
25668 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25669 /* If cursor row is not enabled, we don't really know where to
25670 display the cursor. */
25671 if (!glyph_row->enabled_p)
25673 w->phys_cursor_on_p = 0;
25674 return;
25677 glyph = NULL;
25678 if (!glyph_row->exact_window_width_line_p
25679 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25680 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25682 xassert (interrupt_input_blocked);
25684 /* Set new_cursor_type to the cursor we want to be displayed. */
25685 new_cursor_type = get_window_cursor_type (w, glyph,
25686 &new_cursor_width, &active_cursor);
25688 /* If cursor is currently being shown and we don't want it to be or
25689 it is in the wrong place, or the cursor type is not what we want,
25690 erase it. */
25691 if (w->phys_cursor_on_p
25692 && (!on
25693 || w->phys_cursor.x != x
25694 || w->phys_cursor.y != y
25695 || new_cursor_type != w->phys_cursor_type
25696 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
25697 && new_cursor_width != w->phys_cursor_width)))
25698 erase_phys_cursor (w);
25700 /* Don't check phys_cursor_on_p here because that flag is only set
25701 to zero in some cases where we know that the cursor has been
25702 completely erased, to avoid the extra work of erasing the cursor
25703 twice. In other words, phys_cursor_on_p can be 1 and the cursor
25704 still not be visible, or it has only been partly erased. */
25705 if (on)
25707 w->phys_cursor_ascent = glyph_row->ascent;
25708 w->phys_cursor_height = glyph_row->height;
25710 /* Set phys_cursor_.* before x_draw_.* is called because some
25711 of them may need the information. */
25712 w->phys_cursor.x = x;
25713 w->phys_cursor.y = glyph_row->y;
25714 w->phys_cursor.hpos = hpos;
25715 w->phys_cursor.vpos = vpos;
25718 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
25719 new_cursor_type, new_cursor_width,
25720 on, active_cursor);
25724 /* Switch the display of W's cursor on or off, according to the value
25725 of ON. */
25727 static void
25728 update_window_cursor (struct window *w, int on)
25730 /* Don't update cursor in windows whose frame is in the process
25731 of being deleted. */
25732 if (w->current_matrix)
25734 int hpos = w->phys_cursor.hpos;
25735 int vpos = w->phys_cursor.vpos;
25736 struct glyph_row *row;
25738 if (vpos >= w->current_matrix->nrows
25739 || hpos >= w->current_matrix->matrix_w)
25740 return;
25742 row = MATRIX_ROW (w->current_matrix, vpos);
25744 /* When the window is hscrolled, cursor hpos can legitimately be
25745 out of bounds, but we draw the cursor at the corresponding
25746 window margin in that case. */
25747 if (!row->reversed_p && hpos < 0)
25748 hpos = 0;
25749 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25750 hpos = row->used[TEXT_AREA] - 1;
25752 BLOCK_INPUT;
25753 display_and_set_cursor (w, on, hpos, vpos,
25754 w->phys_cursor.x, w->phys_cursor.y);
25755 UNBLOCK_INPUT;
25760 /* Call update_window_cursor with parameter ON_P on all leaf windows
25761 in the window tree rooted at W. */
25763 static void
25764 update_cursor_in_window_tree (struct window *w, int on_p)
25766 while (w)
25768 if (!NILP (w->hchild))
25769 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
25770 else if (!NILP (w->vchild))
25771 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
25772 else
25773 update_window_cursor (w, on_p);
25775 w = NILP (w->next) ? 0 : XWINDOW (w->next);
25780 /* EXPORT:
25781 Display the cursor on window W, or clear it, according to ON_P.
25782 Don't change the cursor's position. */
25784 void
25785 x_update_cursor (struct frame *f, int on_p)
25787 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
25791 /* EXPORT:
25792 Clear the cursor of window W to background color, and mark the
25793 cursor as not shown. This is used when the text where the cursor
25794 is about to be rewritten. */
25796 void
25797 x_clear_cursor (struct window *w)
25799 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
25800 update_window_cursor (w, 0);
25803 #endif /* HAVE_WINDOW_SYSTEM */
25805 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
25806 and MSDOS. */
25807 static void
25808 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
25809 int start_hpos, int end_hpos,
25810 enum draw_glyphs_face draw)
25812 #ifdef HAVE_WINDOW_SYSTEM
25813 if (FRAME_WINDOW_P (XFRAME (w->frame)))
25815 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
25816 return;
25818 #endif
25819 #if defined (HAVE_GPM) || defined (MSDOS)
25820 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
25821 #endif
25824 /* Display the active region described by mouse_face_* according to DRAW. */
25826 static void
25827 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
25829 struct window *w = XWINDOW (hlinfo->mouse_face_window);
25830 struct frame *f = XFRAME (WINDOW_FRAME (w));
25832 if (/* If window is in the process of being destroyed, don't bother
25833 to do anything. */
25834 w->current_matrix != NULL
25835 /* Don't update mouse highlight if hidden */
25836 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
25837 /* Recognize when we are called to operate on rows that don't exist
25838 anymore. This can happen when a window is split. */
25839 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
25841 int phys_cursor_on_p = w->phys_cursor_on_p;
25842 struct glyph_row *row, *first, *last;
25844 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
25845 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
25847 for (row = first; row <= last && row->enabled_p; ++row)
25849 int start_hpos, end_hpos, start_x;
25851 /* For all but the first row, the highlight starts at column 0. */
25852 if (row == first)
25854 /* R2L rows have BEG and END in reversed order, but the
25855 screen drawing geometry is always left to right. So
25856 we need to mirror the beginning and end of the
25857 highlighted area in R2L rows. */
25858 if (!row->reversed_p)
25860 start_hpos = hlinfo->mouse_face_beg_col;
25861 start_x = hlinfo->mouse_face_beg_x;
25863 else if (row == last)
25865 start_hpos = hlinfo->mouse_face_end_col;
25866 start_x = hlinfo->mouse_face_end_x;
25868 else
25870 start_hpos = 0;
25871 start_x = 0;
25874 else if (row->reversed_p && row == last)
25876 start_hpos = hlinfo->mouse_face_end_col;
25877 start_x = hlinfo->mouse_face_end_x;
25879 else
25881 start_hpos = 0;
25882 start_x = 0;
25885 if (row == last)
25887 if (!row->reversed_p)
25888 end_hpos = hlinfo->mouse_face_end_col;
25889 else if (row == first)
25890 end_hpos = hlinfo->mouse_face_beg_col;
25891 else
25893 end_hpos = row->used[TEXT_AREA];
25894 if (draw == DRAW_NORMAL_TEXT)
25895 row->fill_line_p = 1; /* Clear to end of line */
25898 else if (row->reversed_p && row == first)
25899 end_hpos = hlinfo->mouse_face_beg_col;
25900 else
25902 end_hpos = row->used[TEXT_AREA];
25903 if (draw == DRAW_NORMAL_TEXT)
25904 row->fill_line_p = 1; /* Clear to end of line */
25907 if (end_hpos > start_hpos)
25909 draw_row_with_mouse_face (w, start_x, row,
25910 start_hpos, end_hpos, draw);
25912 row->mouse_face_p
25913 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
25917 #ifdef HAVE_WINDOW_SYSTEM
25918 /* When we've written over the cursor, arrange for it to
25919 be displayed again. */
25920 if (FRAME_WINDOW_P (f)
25921 && phys_cursor_on_p && !w->phys_cursor_on_p)
25923 int hpos = w->phys_cursor.hpos;
25925 /* When the window is hscrolled, cursor hpos can legitimately be
25926 out of bounds, but we draw the cursor at the corresponding
25927 window margin in that case. */
25928 if (!row->reversed_p && hpos < 0)
25929 hpos = 0;
25930 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25931 hpos = row->used[TEXT_AREA] - 1;
25933 BLOCK_INPUT;
25934 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
25935 w->phys_cursor.x, w->phys_cursor.y);
25936 UNBLOCK_INPUT;
25938 #endif /* HAVE_WINDOW_SYSTEM */
25941 #ifdef HAVE_WINDOW_SYSTEM
25942 /* Change the mouse cursor. */
25943 if (FRAME_WINDOW_P (f))
25945 if (draw == DRAW_NORMAL_TEXT
25946 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
25947 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
25948 else if (draw == DRAW_MOUSE_FACE)
25949 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
25950 else
25951 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
25953 #endif /* HAVE_WINDOW_SYSTEM */
25956 /* EXPORT:
25957 Clear out the mouse-highlighted active region.
25958 Redraw it un-highlighted first. Value is non-zero if mouse
25959 face was actually drawn unhighlighted. */
25962 clear_mouse_face (Mouse_HLInfo *hlinfo)
25964 int cleared = 0;
25966 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
25968 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
25969 cleared = 1;
25972 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25973 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25974 hlinfo->mouse_face_window = Qnil;
25975 hlinfo->mouse_face_overlay = Qnil;
25976 return cleared;
25979 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
25980 within the mouse face on that window. */
25981 static int
25982 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
25984 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25986 /* Quickly resolve the easy cases. */
25987 if (!(WINDOWP (hlinfo->mouse_face_window)
25988 && XWINDOW (hlinfo->mouse_face_window) == w))
25989 return 0;
25990 if (vpos < hlinfo->mouse_face_beg_row
25991 || vpos > hlinfo->mouse_face_end_row)
25992 return 0;
25993 if (vpos > hlinfo->mouse_face_beg_row
25994 && vpos < hlinfo->mouse_face_end_row)
25995 return 1;
25997 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
25999 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26001 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26002 return 1;
26004 else if ((vpos == hlinfo->mouse_face_beg_row
26005 && hpos >= hlinfo->mouse_face_beg_col)
26006 || (vpos == hlinfo->mouse_face_end_row
26007 && hpos < hlinfo->mouse_face_end_col))
26008 return 1;
26010 else
26012 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26014 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26015 return 1;
26017 else if ((vpos == hlinfo->mouse_face_beg_row
26018 && hpos <= hlinfo->mouse_face_beg_col)
26019 || (vpos == hlinfo->mouse_face_end_row
26020 && hpos > hlinfo->mouse_face_end_col))
26021 return 1;
26023 return 0;
26027 /* EXPORT:
26028 Non-zero if physical cursor of window W is within mouse face. */
26031 cursor_in_mouse_face_p (struct window *w)
26033 int hpos = w->phys_cursor.hpos;
26034 int vpos = w->phys_cursor.vpos;
26035 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26037 /* When the window is hscrolled, cursor hpos can legitimately be out
26038 of bounds, but we draw the cursor at the corresponding window
26039 margin in that case. */
26040 if (!row->reversed_p && hpos < 0)
26041 hpos = 0;
26042 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26043 hpos = row->used[TEXT_AREA] - 1;
26045 return coords_in_mouse_face_p (w, hpos, vpos);
26050 /* Find the glyph rows START_ROW and END_ROW of window W that display
26051 characters between buffer positions START_CHARPOS and END_CHARPOS
26052 (excluding END_CHARPOS). DISP_STRING is a display string that
26053 covers these buffer positions. This is similar to
26054 row_containing_pos, but is more accurate when bidi reordering makes
26055 buffer positions change non-linearly with glyph rows. */
26056 static void
26057 rows_from_pos_range (struct window *w,
26058 EMACS_INT start_charpos, EMACS_INT end_charpos,
26059 Lisp_Object disp_string,
26060 struct glyph_row **start, struct glyph_row **end)
26062 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26063 int last_y = window_text_bottom_y (w);
26064 struct glyph_row *row;
26066 *start = NULL;
26067 *end = NULL;
26069 while (!first->enabled_p
26070 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26071 first++;
26073 /* Find the START row. */
26074 for (row = first;
26075 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26076 row++)
26078 /* A row can potentially be the START row if the range of the
26079 characters it displays intersects the range
26080 [START_CHARPOS..END_CHARPOS). */
26081 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26082 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26083 /* See the commentary in row_containing_pos, for the
26084 explanation of the complicated way to check whether
26085 some position is beyond the end of the characters
26086 displayed by a row. */
26087 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26088 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26089 && !row->ends_at_zv_p
26090 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26091 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26092 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26093 && !row->ends_at_zv_p
26094 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26096 /* Found a candidate row. Now make sure at least one of the
26097 glyphs it displays has a charpos from the range
26098 [START_CHARPOS..END_CHARPOS).
26100 This is not obvious because bidi reordering could make
26101 buffer positions of a row be 1,2,3,102,101,100, and if we
26102 want to highlight characters in [50..60), we don't want
26103 this row, even though [50..60) does intersect [1..103),
26104 the range of character positions given by the row's start
26105 and end positions. */
26106 struct glyph *g = row->glyphs[TEXT_AREA];
26107 struct glyph *e = g + row->used[TEXT_AREA];
26109 while (g < e)
26111 if (((BUFFERP (g->object) || INTEGERP (g->object))
26112 && start_charpos <= g->charpos && g->charpos < end_charpos)
26113 /* A glyph that comes from DISP_STRING is by
26114 definition to be highlighted. */
26115 || EQ (g->object, disp_string))
26116 *start = row;
26117 g++;
26119 if (*start)
26120 break;
26124 /* Find the END row. */
26125 if (!*start
26126 /* If the last row is partially visible, start looking for END
26127 from that row, instead of starting from FIRST. */
26128 && !(row->enabled_p
26129 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26130 row = first;
26131 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26133 struct glyph_row *next = row + 1;
26134 EMACS_INT next_start = MATRIX_ROW_START_CHARPOS (next);
26136 if (!next->enabled_p
26137 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26138 /* The first row >= START whose range of displayed characters
26139 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26140 is the row END + 1. */
26141 || (start_charpos < next_start
26142 && end_charpos < next_start)
26143 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26144 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26145 && !next->ends_at_zv_p
26146 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26147 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26148 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26149 && !next->ends_at_zv_p
26150 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26152 *end = row;
26153 break;
26155 else
26157 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26158 but none of the characters it displays are in the range, it is
26159 also END + 1. */
26160 struct glyph *g = next->glyphs[TEXT_AREA];
26161 struct glyph *s = g;
26162 struct glyph *e = g + next->used[TEXT_AREA];
26164 while (g < e)
26166 if (((BUFFERP (g->object) || INTEGERP (g->object))
26167 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26168 /* If the buffer position of the first glyph in
26169 the row is equal to END_CHARPOS, it means
26170 the last character to be highlighted is the
26171 newline of ROW, and we must consider NEXT as
26172 END, not END+1. */
26173 || (((!next->reversed_p && g == s)
26174 || (next->reversed_p && g == e - 1))
26175 && (g->charpos == end_charpos
26176 /* Special case for when NEXT is an
26177 empty line at ZV. */
26178 || (g->charpos == -1
26179 && !row->ends_at_zv_p
26180 && next_start == end_charpos)))))
26181 /* A glyph that comes from DISP_STRING is by
26182 definition to be highlighted. */
26183 || EQ (g->object, disp_string))
26184 break;
26185 g++;
26187 if (g == e)
26189 *end = row;
26190 break;
26192 /* The first row that ends at ZV must be the last to be
26193 highlighted. */
26194 else if (next->ends_at_zv_p)
26196 *end = next;
26197 break;
26203 /* This function sets the mouse_face_* elements of HLINFO, assuming
26204 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26205 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26206 for the overlay or run of text properties specifying the mouse
26207 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26208 before-string and after-string that must also be highlighted.
26209 DISP_STRING, if non-nil, is a display string that may cover some
26210 or all of the highlighted text. */
26212 static void
26213 mouse_face_from_buffer_pos (Lisp_Object window,
26214 Mouse_HLInfo *hlinfo,
26215 EMACS_INT mouse_charpos,
26216 EMACS_INT start_charpos,
26217 EMACS_INT end_charpos,
26218 Lisp_Object before_string,
26219 Lisp_Object after_string,
26220 Lisp_Object disp_string)
26222 struct window *w = XWINDOW (window);
26223 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26224 struct glyph_row *r1, *r2;
26225 struct glyph *glyph, *end;
26226 EMACS_INT ignore, pos;
26227 int x;
26229 xassert (NILP (disp_string) || STRINGP (disp_string));
26230 xassert (NILP (before_string) || STRINGP (before_string));
26231 xassert (NILP (after_string) || STRINGP (after_string));
26233 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26234 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26235 if (r1 == NULL)
26236 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26237 /* If the before-string or display-string contains newlines,
26238 rows_from_pos_range skips to its last row. Move back. */
26239 if (!NILP (before_string) || !NILP (disp_string))
26241 struct glyph_row *prev;
26242 while ((prev = r1 - 1, prev >= first)
26243 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26244 && prev->used[TEXT_AREA] > 0)
26246 struct glyph *beg = prev->glyphs[TEXT_AREA];
26247 glyph = beg + prev->used[TEXT_AREA];
26248 while (--glyph >= beg && INTEGERP (glyph->object));
26249 if (glyph < beg
26250 || !(EQ (glyph->object, before_string)
26251 || EQ (glyph->object, disp_string)))
26252 break;
26253 r1 = prev;
26256 if (r2 == NULL)
26258 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26259 hlinfo->mouse_face_past_end = 1;
26261 else if (!NILP (after_string))
26263 /* If the after-string has newlines, advance to its last row. */
26264 struct glyph_row *next;
26265 struct glyph_row *last
26266 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26268 for (next = r2 + 1;
26269 next <= last
26270 && next->used[TEXT_AREA] > 0
26271 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26272 ++next)
26273 r2 = next;
26275 /* The rest of the display engine assumes that mouse_face_beg_row is
26276 either above mouse_face_end_row or identical to it. But with
26277 bidi-reordered continued lines, the row for START_CHARPOS could
26278 be below the row for END_CHARPOS. If so, swap the rows and store
26279 them in correct order. */
26280 if (r1->y > r2->y)
26282 struct glyph_row *tem = r2;
26284 r2 = r1;
26285 r1 = tem;
26288 hlinfo->mouse_face_beg_y = r1->y;
26289 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26290 hlinfo->mouse_face_end_y = r2->y;
26291 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26293 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26294 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26295 could be anywhere in the row and in any order. The strategy
26296 below is to find the leftmost and the rightmost glyph that
26297 belongs to either of these 3 strings, or whose position is
26298 between START_CHARPOS and END_CHARPOS, and highlight all the
26299 glyphs between those two. This may cover more than just the text
26300 between START_CHARPOS and END_CHARPOS if the range of characters
26301 strides the bidi level boundary, e.g. if the beginning is in R2L
26302 text while the end is in L2R text or vice versa. */
26303 if (!r1->reversed_p)
26305 /* This row is in a left to right paragraph. Scan it left to
26306 right. */
26307 glyph = r1->glyphs[TEXT_AREA];
26308 end = glyph + r1->used[TEXT_AREA];
26309 x = r1->x;
26311 /* Skip truncation glyphs at the start of the glyph row. */
26312 if (r1->displays_text_p)
26313 for (; glyph < end
26314 && INTEGERP (glyph->object)
26315 && glyph->charpos < 0;
26316 ++glyph)
26317 x += glyph->pixel_width;
26319 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26320 or DISP_STRING, and the first glyph from buffer whose
26321 position is between START_CHARPOS and END_CHARPOS. */
26322 for (; glyph < end
26323 && !INTEGERP (glyph->object)
26324 && !EQ (glyph->object, disp_string)
26325 && !(BUFFERP (glyph->object)
26326 && (glyph->charpos >= start_charpos
26327 && glyph->charpos < end_charpos));
26328 ++glyph)
26330 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26331 are present at buffer positions between START_CHARPOS and
26332 END_CHARPOS, or if they come from an overlay. */
26333 if (EQ (glyph->object, before_string))
26335 pos = string_buffer_position (before_string,
26336 start_charpos);
26337 /* If pos == 0, it means before_string came from an
26338 overlay, not from a buffer position. */
26339 if (!pos || (pos >= start_charpos && pos < end_charpos))
26340 break;
26342 else if (EQ (glyph->object, after_string))
26344 pos = string_buffer_position (after_string, end_charpos);
26345 if (!pos || (pos >= start_charpos && pos < end_charpos))
26346 break;
26348 x += glyph->pixel_width;
26350 hlinfo->mouse_face_beg_x = x;
26351 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26353 else
26355 /* This row is in a right to left paragraph. Scan it right to
26356 left. */
26357 struct glyph *g;
26359 end = r1->glyphs[TEXT_AREA] - 1;
26360 glyph = end + r1->used[TEXT_AREA];
26362 /* Skip truncation glyphs at the start of the glyph row. */
26363 if (r1->displays_text_p)
26364 for (; glyph > end
26365 && INTEGERP (glyph->object)
26366 && glyph->charpos < 0;
26367 --glyph)
26370 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26371 or DISP_STRING, and the first glyph from buffer whose
26372 position is between START_CHARPOS and END_CHARPOS. */
26373 for (; glyph > end
26374 && !INTEGERP (glyph->object)
26375 && !EQ (glyph->object, disp_string)
26376 && !(BUFFERP (glyph->object)
26377 && (glyph->charpos >= start_charpos
26378 && glyph->charpos < end_charpos));
26379 --glyph)
26381 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26382 are present at buffer positions between START_CHARPOS and
26383 END_CHARPOS, or if they come from an overlay. */
26384 if (EQ (glyph->object, before_string))
26386 pos = string_buffer_position (before_string, start_charpos);
26387 /* If pos == 0, it means before_string came from an
26388 overlay, not from a buffer position. */
26389 if (!pos || (pos >= start_charpos && pos < end_charpos))
26390 break;
26392 else if (EQ (glyph->object, after_string))
26394 pos = string_buffer_position (after_string, end_charpos);
26395 if (!pos || (pos >= start_charpos && pos < end_charpos))
26396 break;
26400 glyph++; /* first glyph to the right of the highlighted area */
26401 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26402 x += g->pixel_width;
26403 hlinfo->mouse_face_beg_x = x;
26404 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26407 /* If the highlight ends in a different row, compute GLYPH and END
26408 for the end row. Otherwise, reuse the values computed above for
26409 the row where the highlight begins. */
26410 if (r2 != r1)
26412 if (!r2->reversed_p)
26414 glyph = r2->glyphs[TEXT_AREA];
26415 end = glyph + r2->used[TEXT_AREA];
26416 x = r2->x;
26418 else
26420 end = r2->glyphs[TEXT_AREA] - 1;
26421 glyph = end + r2->used[TEXT_AREA];
26425 if (!r2->reversed_p)
26427 /* Skip truncation and continuation glyphs near the end of the
26428 row, and also blanks and stretch glyphs inserted by
26429 extend_face_to_end_of_line. */
26430 while (end > glyph
26431 && INTEGERP ((end - 1)->object))
26432 --end;
26433 /* Scan the rest of the glyph row from the end, looking for the
26434 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26435 DISP_STRING, or whose position is between START_CHARPOS
26436 and END_CHARPOS */
26437 for (--end;
26438 end > glyph
26439 && !INTEGERP (end->object)
26440 && !EQ (end->object, disp_string)
26441 && !(BUFFERP (end->object)
26442 && (end->charpos >= start_charpos
26443 && end->charpos < end_charpos));
26444 --end)
26446 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26447 are present at buffer positions between START_CHARPOS and
26448 END_CHARPOS, or if they come from an overlay. */
26449 if (EQ (end->object, before_string))
26451 pos = string_buffer_position (before_string, start_charpos);
26452 if (!pos || (pos >= start_charpos && pos < end_charpos))
26453 break;
26455 else if (EQ (end->object, after_string))
26457 pos = string_buffer_position (after_string, end_charpos);
26458 if (!pos || (pos >= start_charpos && pos < end_charpos))
26459 break;
26462 /* Find the X coordinate of the last glyph to be highlighted. */
26463 for (; glyph <= end; ++glyph)
26464 x += glyph->pixel_width;
26466 hlinfo->mouse_face_end_x = x;
26467 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26469 else
26471 /* Skip truncation and continuation glyphs near the end of the
26472 row, and also blanks and stretch glyphs inserted by
26473 extend_face_to_end_of_line. */
26474 x = r2->x;
26475 end++;
26476 while (end < glyph
26477 && INTEGERP (end->object))
26479 x += end->pixel_width;
26480 ++end;
26482 /* Scan the rest of the glyph row from the end, looking for the
26483 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26484 DISP_STRING, or whose position is between START_CHARPOS
26485 and END_CHARPOS */
26486 for ( ;
26487 end < glyph
26488 && !INTEGERP (end->object)
26489 && !EQ (end->object, disp_string)
26490 && !(BUFFERP (end->object)
26491 && (end->charpos >= start_charpos
26492 && end->charpos < end_charpos));
26493 ++end)
26495 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26496 are present at buffer positions between START_CHARPOS and
26497 END_CHARPOS, or if they come from an overlay. */
26498 if (EQ (end->object, before_string))
26500 pos = string_buffer_position (before_string, start_charpos);
26501 if (!pos || (pos >= start_charpos && pos < end_charpos))
26502 break;
26504 else if (EQ (end->object, after_string))
26506 pos = string_buffer_position (after_string, end_charpos);
26507 if (!pos || (pos >= start_charpos && pos < end_charpos))
26508 break;
26510 x += end->pixel_width;
26512 /* If we exited the above loop because we arrived at the last
26513 glyph of the row, and its buffer position is still not in
26514 range, it means the last character in range is the preceding
26515 newline. Bump the end column and x values to get past the
26516 last glyph. */
26517 if (end == glyph
26518 && BUFFERP (end->object)
26519 && (end->charpos < start_charpos
26520 || end->charpos >= end_charpos))
26522 x += end->pixel_width;
26523 ++end;
26525 hlinfo->mouse_face_end_x = x;
26526 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26529 hlinfo->mouse_face_window = window;
26530 hlinfo->mouse_face_face_id
26531 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26532 mouse_charpos + 1,
26533 !hlinfo->mouse_face_hidden, -1);
26534 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26537 /* The following function is not used anymore (replaced with
26538 mouse_face_from_string_pos), but I leave it here for the time
26539 being, in case someone would. */
26541 #if 0 /* not used */
26543 /* Find the position of the glyph for position POS in OBJECT in
26544 window W's current matrix, and return in *X, *Y the pixel
26545 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26547 RIGHT_P non-zero means return the position of the right edge of the
26548 glyph, RIGHT_P zero means return the left edge position.
26550 If no glyph for POS exists in the matrix, return the position of
26551 the glyph with the next smaller position that is in the matrix, if
26552 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26553 exists in the matrix, return the position of the glyph with the
26554 next larger position in OBJECT.
26556 Value is non-zero if a glyph was found. */
26558 static int
26559 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
26560 int *hpos, int *vpos, int *x, int *y, int right_p)
26562 int yb = window_text_bottom_y (w);
26563 struct glyph_row *r;
26564 struct glyph *best_glyph = NULL;
26565 struct glyph_row *best_row = NULL;
26566 int best_x = 0;
26568 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26569 r->enabled_p && r->y < yb;
26570 ++r)
26572 struct glyph *g = r->glyphs[TEXT_AREA];
26573 struct glyph *e = g + r->used[TEXT_AREA];
26574 int gx;
26576 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26577 if (EQ (g->object, object))
26579 if (g->charpos == pos)
26581 best_glyph = g;
26582 best_x = gx;
26583 best_row = r;
26584 goto found;
26586 else if (best_glyph == NULL
26587 || ((eabs (g->charpos - pos)
26588 < eabs (best_glyph->charpos - pos))
26589 && (right_p
26590 ? g->charpos < pos
26591 : g->charpos > pos)))
26593 best_glyph = g;
26594 best_x = gx;
26595 best_row = r;
26600 found:
26602 if (best_glyph)
26604 *x = best_x;
26605 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26607 if (right_p)
26609 *x += best_glyph->pixel_width;
26610 ++*hpos;
26613 *y = best_row->y;
26614 *vpos = best_row - w->current_matrix->rows;
26617 return best_glyph != NULL;
26619 #endif /* not used */
26621 /* Find the positions of the first and the last glyphs in window W's
26622 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26623 (assumed to be a string), and return in HLINFO's mouse_face_*
26624 members the pixel and column/row coordinates of those glyphs. */
26626 static void
26627 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26628 Lisp_Object object,
26629 EMACS_INT startpos, EMACS_INT endpos)
26631 int yb = window_text_bottom_y (w);
26632 struct glyph_row *r;
26633 struct glyph *g, *e;
26634 int gx;
26635 int found = 0;
26637 /* Find the glyph row with at least one position in the range
26638 [STARTPOS..ENDPOS], and the first glyph in that row whose
26639 position belongs to that range. */
26640 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26641 r->enabled_p && r->y < yb;
26642 ++r)
26644 if (!r->reversed_p)
26646 g = r->glyphs[TEXT_AREA];
26647 e = g + r->used[TEXT_AREA];
26648 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26649 if (EQ (g->object, object)
26650 && startpos <= g->charpos && g->charpos <= endpos)
26652 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26653 hlinfo->mouse_face_beg_y = r->y;
26654 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26655 hlinfo->mouse_face_beg_x = gx;
26656 found = 1;
26657 break;
26660 else
26662 struct glyph *g1;
26664 e = r->glyphs[TEXT_AREA];
26665 g = e + r->used[TEXT_AREA];
26666 for ( ; g > e; --g)
26667 if (EQ ((g-1)->object, object)
26668 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26670 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26671 hlinfo->mouse_face_beg_y = r->y;
26672 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26673 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26674 gx += g1->pixel_width;
26675 hlinfo->mouse_face_beg_x = gx;
26676 found = 1;
26677 break;
26680 if (found)
26681 break;
26684 if (!found)
26685 return;
26687 /* Starting with the next row, look for the first row which does NOT
26688 include any glyphs whose positions are in the range. */
26689 for (++r; r->enabled_p && r->y < yb; ++r)
26691 g = r->glyphs[TEXT_AREA];
26692 e = g + r->used[TEXT_AREA];
26693 found = 0;
26694 for ( ; g < e; ++g)
26695 if (EQ (g->object, object)
26696 && startpos <= g->charpos && g->charpos <= endpos)
26698 found = 1;
26699 break;
26701 if (!found)
26702 break;
26705 /* The highlighted region ends on the previous row. */
26706 r--;
26708 /* Set the end row and its vertical pixel coordinate. */
26709 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
26710 hlinfo->mouse_face_end_y = r->y;
26712 /* Compute and set the end column and the end column's horizontal
26713 pixel coordinate. */
26714 if (!r->reversed_p)
26716 g = r->glyphs[TEXT_AREA];
26717 e = g + r->used[TEXT_AREA];
26718 for ( ; e > g; --e)
26719 if (EQ ((e-1)->object, object)
26720 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
26721 break;
26722 hlinfo->mouse_face_end_col = e - g;
26724 for (gx = r->x; g < e; ++g)
26725 gx += g->pixel_width;
26726 hlinfo->mouse_face_end_x = gx;
26728 else
26730 e = r->glyphs[TEXT_AREA];
26731 g = e + r->used[TEXT_AREA];
26732 for (gx = r->x ; e < g; ++e)
26734 if (EQ (e->object, object)
26735 && startpos <= e->charpos && e->charpos <= endpos)
26736 break;
26737 gx += e->pixel_width;
26739 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
26740 hlinfo->mouse_face_end_x = gx;
26744 #ifdef HAVE_WINDOW_SYSTEM
26746 /* See if position X, Y is within a hot-spot of an image. */
26748 static int
26749 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
26751 if (!CONSP (hot_spot))
26752 return 0;
26754 if (EQ (XCAR (hot_spot), Qrect))
26756 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
26757 Lisp_Object rect = XCDR (hot_spot);
26758 Lisp_Object tem;
26759 if (!CONSP (rect))
26760 return 0;
26761 if (!CONSP (XCAR (rect)))
26762 return 0;
26763 if (!CONSP (XCDR (rect)))
26764 return 0;
26765 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
26766 return 0;
26767 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
26768 return 0;
26769 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
26770 return 0;
26771 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
26772 return 0;
26773 return 1;
26775 else if (EQ (XCAR (hot_spot), Qcircle))
26777 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
26778 Lisp_Object circ = XCDR (hot_spot);
26779 Lisp_Object lr, lx0, ly0;
26780 if (CONSP (circ)
26781 && CONSP (XCAR (circ))
26782 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
26783 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
26784 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
26786 double r = XFLOATINT (lr);
26787 double dx = XINT (lx0) - x;
26788 double dy = XINT (ly0) - y;
26789 return (dx * dx + dy * dy <= r * r);
26792 else if (EQ (XCAR (hot_spot), Qpoly))
26794 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
26795 if (VECTORP (XCDR (hot_spot)))
26797 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
26798 Lisp_Object *poly = v->contents;
26799 int n = v->header.size;
26800 int i;
26801 int inside = 0;
26802 Lisp_Object lx, ly;
26803 int x0, y0;
26805 /* Need an even number of coordinates, and at least 3 edges. */
26806 if (n < 6 || n & 1)
26807 return 0;
26809 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
26810 If count is odd, we are inside polygon. Pixels on edges
26811 may or may not be included depending on actual geometry of the
26812 polygon. */
26813 if ((lx = poly[n-2], !INTEGERP (lx))
26814 || (ly = poly[n-1], !INTEGERP (lx)))
26815 return 0;
26816 x0 = XINT (lx), y0 = XINT (ly);
26817 for (i = 0; i < n; i += 2)
26819 int x1 = x0, y1 = y0;
26820 if ((lx = poly[i], !INTEGERP (lx))
26821 || (ly = poly[i+1], !INTEGERP (ly)))
26822 return 0;
26823 x0 = XINT (lx), y0 = XINT (ly);
26825 /* Does this segment cross the X line? */
26826 if (x0 >= x)
26828 if (x1 >= x)
26829 continue;
26831 else if (x1 < x)
26832 continue;
26833 if (y > y0 && y > y1)
26834 continue;
26835 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
26836 inside = !inside;
26838 return inside;
26841 return 0;
26844 Lisp_Object
26845 find_hot_spot (Lisp_Object map, int x, int y)
26847 while (CONSP (map))
26849 if (CONSP (XCAR (map))
26850 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
26851 return XCAR (map);
26852 map = XCDR (map);
26855 return Qnil;
26858 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
26859 3, 3, 0,
26860 doc: /* Lookup in image map MAP coordinates X and Y.
26861 An image map is an alist where each element has the format (AREA ID PLIST).
26862 An AREA is specified as either a rectangle, a circle, or a polygon:
26863 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
26864 pixel coordinates of the upper left and bottom right corners.
26865 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
26866 and the radius of the circle; r may be a float or integer.
26867 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
26868 vector describes one corner in the polygon.
26869 Returns the alist element for the first matching AREA in MAP. */)
26870 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
26872 if (NILP (map))
26873 return Qnil;
26875 CHECK_NUMBER (x);
26876 CHECK_NUMBER (y);
26878 return find_hot_spot (map, XINT (x), XINT (y));
26882 /* Display frame CURSOR, optionally using shape defined by POINTER. */
26883 static void
26884 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
26886 /* Do not change cursor shape while dragging mouse. */
26887 if (!NILP (do_mouse_tracking))
26888 return;
26890 if (!NILP (pointer))
26892 if (EQ (pointer, Qarrow))
26893 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26894 else if (EQ (pointer, Qhand))
26895 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
26896 else if (EQ (pointer, Qtext))
26897 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26898 else if (EQ (pointer, intern ("hdrag")))
26899 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26900 #ifdef HAVE_X_WINDOWS
26901 else if (EQ (pointer, intern ("vdrag")))
26902 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
26903 #endif
26904 else if (EQ (pointer, intern ("hourglass")))
26905 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
26906 else if (EQ (pointer, Qmodeline))
26907 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
26908 else
26909 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26912 if (cursor != No_Cursor)
26913 FRAME_RIF (f)->define_frame_cursor (f, cursor);
26916 #endif /* HAVE_WINDOW_SYSTEM */
26918 /* Take proper action when mouse has moved to the mode or header line
26919 or marginal area AREA of window W, x-position X and y-position Y.
26920 X is relative to the start of the text display area of W, so the
26921 width of bitmap areas and scroll bars must be subtracted to get a
26922 position relative to the start of the mode line. */
26924 static void
26925 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
26926 enum window_part area)
26928 struct window *w = XWINDOW (window);
26929 struct frame *f = XFRAME (w->frame);
26930 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26931 #ifdef HAVE_WINDOW_SYSTEM
26932 Display_Info *dpyinfo;
26933 #endif
26934 Cursor cursor = No_Cursor;
26935 Lisp_Object pointer = Qnil;
26936 int dx, dy, width, height;
26937 EMACS_INT charpos;
26938 Lisp_Object string, object = Qnil;
26939 Lisp_Object pos, help;
26941 Lisp_Object mouse_face;
26942 int original_x_pixel = x;
26943 struct glyph * glyph = NULL, * row_start_glyph = NULL;
26944 struct glyph_row *row;
26946 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
26948 int x0;
26949 struct glyph *end;
26951 /* Kludge alert: mode_line_string takes X/Y in pixels, but
26952 returns them in row/column units! */
26953 string = mode_line_string (w, area, &x, &y, &charpos,
26954 &object, &dx, &dy, &width, &height);
26956 row = (area == ON_MODE_LINE
26957 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
26958 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
26960 /* Find the glyph under the mouse pointer. */
26961 if (row->mode_line_p && row->enabled_p)
26963 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
26964 end = glyph + row->used[TEXT_AREA];
26966 for (x0 = original_x_pixel;
26967 glyph < end && x0 >= glyph->pixel_width;
26968 ++glyph)
26969 x0 -= glyph->pixel_width;
26971 if (glyph >= end)
26972 glyph = NULL;
26975 else
26977 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
26978 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
26979 returns them in row/column units! */
26980 string = marginal_area_string (w, area, &x, &y, &charpos,
26981 &object, &dx, &dy, &width, &height);
26984 help = Qnil;
26986 #ifdef HAVE_WINDOW_SYSTEM
26987 if (IMAGEP (object))
26989 Lisp_Object image_map, hotspot;
26990 if ((image_map = Fplist_get (XCDR (object), QCmap),
26991 !NILP (image_map))
26992 && (hotspot = find_hot_spot (image_map, dx, dy),
26993 CONSP (hotspot))
26994 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
26996 Lisp_Object plist;
26998 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
26999 If so, we could look for mouse-enter, mouse-leave
27000 properties in PLIST (and do something...). */
27001 hotspot = XCDR (hotspot);
27002 if (CONSP (hotspot)
27003 && (plist = XCAR (hotspot), CONSP (plist)))
27005 pointer = Fplist_get (plist, Qpointer);
27006 if (NILP (pointer))
27007 pointer = Qhand;
27008 help = Fplist_get (plist, Qhelp_echo);
27009 if (!NILP (help))
27011 help_echo_string = help;
27012 /* Is this correct? ++kfs */
27013 XSETWINDOW (help_echo_window, w);
27014 help_echo_object = w->buffer;
27015 help_echo_pos = charpos;
27019 if (NILP (pointer))
27020 pointer = Fplist_get (XCDR (object), QCpointer);
27022 #endif /* HAVE_WINDOW_SYSTEM */
27024 if (STRINGP (string))
27026 pos = make_number (charpos);
27027 /* If we're on a string with `help-echo' text property, arrange
27028 for the help to be displayed. This is done by setting the
27029 global variable help_echo_string to the help string. */
27030 if (NILP (help))
27032 help = Fget_text_property (pos, Qhelp_echo, string);
27033 if (!NILP (help))
27035 help_echo_string = help;
27036 XSETWINDOW (help_echo_window, w);
27037 help_echo_object = string;
27038 help_echo_pos = charpos;
27042 #ifdef HAVE_WINDOW_SYSTEM
27043 if (FRAME_WINDOW_P (f))
27045 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27046 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27047 if (NILP (pointer))
27048 pointer = Fget_text_property (pos, Qpointer, string);
27050 /* Change the mouse pointer according to what is under X/Y. */
27051 if (NILP (pointer)
27052 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27054 Lisp_Object map;
27055 map = Fget_text_property (pos, Qlocal_map, string);
27056 if (!KEYMAPP (map))
27057 map = Fget_text_property (pos, Qkeymap, string);
27058 if (!KEYMAPP (map))
27059 cursor = dpyinfo->vertical_scroll_bar_cursor;
27062 #endif
27064 /* Change the mouse face according to what is under X/Y. */
27065 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27066 if (!NILP (mouse_face)
27067 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27068 && glyph)
27070 Lisp_Object b, e;
27072 struct glyph * tmp_glyph;
27074 int gpos;
27075 int gseq_length;
27076 int total_pixel_width;
27077 EMACS_INT begpos, endpos, ignore;
27079 int vpos, hpos;
27081 b = Fprevious_single_property_change (make_number (charpos + 1),
27082 Qmouse_face, string, Qnil);
27083 if (NILP (b))
27084 begpos = 0;
27085 else
27086 begpos = XINT (b);
27088 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27089 if (NILP (e))
27090 endpos = SCHARS (string);
27091 else
27092 endpos = XINT (e);
27094 /* Calculate the glyph position GPOS of GLYPH in the
27095 displayed string, relative to the beginning of the
27096 highlighted part of the string.
27098 Note: GPOS is different from CHARPOS. CHARPOS is the
27099 position of GLYPH in the internal string object. A mode
27100 line string format has structures which are converted to
27101 a flattened string by the Emacs Lisp interpreter. The
27102 internal string is an element of those structures. The
27103 displayed string is the flattened string. */
27104 tmp_glyph = row_start_glyph;
27105 while (tmp_glyph < glyph
27106 && (!(EQ (tmp_glyph->object, glyph->object)
27107 && begpos <= tmp_glyph->charpos
27108 && tmp_glyph->charpos < endpos)))
27109 tmp_glyph++;
27110 gpos = glyph - tmp_glyph;
27112 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27113 the highlighted part of the displayed string to which
27114 GLYPH belongs. Note: GSEQ_LENGTH is different from
27115 SCHARS (STRING), because the latter returns the length of
27116 the internal string. */
27117 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27118 tmp_glyph > glyph
27119 && (!(EQ (tmp_glyph->object, glyph->object)
27120 && begpos <= tmp_glyph->charpos
27121 && tmp_glyph->charpos < endpos));
27122 tmp_glyph--)
27124 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27126 /* Calculate the total pixel width of all the glyphs between
27127 the beginning of the highlighted area and GLYPH. */
27128 total_pixel_width = 0;
27129 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27130 total_pixel_width += tmp_glyph->pixel_width;
27132 /* Pre calculation of re-rendering position. Note: X is in
27133 column units here, after the call to mode_line_string or
27134 marginal_area_string. */
27135 hpos = x - gpos;
27136 vpos = (area == ON_MODE_LINE
27137 ? (w->current_matrix)->nrows - 1
27138 : 0);
27140 /* If GLYPH's position is included in the region that is
27141 already drawn in mouse face, we have nothing to do. */
27142 if ( EQ (window, hlinfo->mouse_face_window)
27143 && (!row->reversed_p
27144 ? (hlinfo->mouse_face_beg_col <= hpos
27145 && hpos < hlinfo->mouse_face_end_col)
27146 /* In R2L rows we swap BEG and END, see below. */
27147 : (hlinfo->mouse_face_end_col <= hpos
27148 && hpos < hlinfo->mouse_face_beg_col))
27149 && hlinfo->mouse_face_beg_row == vpos )
27150 return;
27152 if (clear_mouse_face (hlinfo))
27153 cursor = No_Cursor;
27155 if (!row->reversed_p)
27157 hlinfo->mouse_face_beg_col = hpos;
27158 hlinfo->mouse_face_beg_x = original_x_pixel
27159 - (total_pixel_width + dx);
27160 hlinfo->mouse_face_end_col = hpos + gseq_length;
27161 hlinfo->mouse_face_end_x = 0;
27163 else
27165 /* In R2L rows, show_mouse_face expects BEG and END
27166 coordinates to be swapped. */
27167 hlinfo->mouse_face_end_col = hpos;
27168 hlinfo->mouse_face_end_x = original_x_pixel
27169 - (total_pixel_width + dx);
27170 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27171 hlinfo->mouse_face_beg_x = 0;
27174 hlinfo->mouse_face_beg_row = vpos;
27175 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27176 hlinfo->mouse_face_beg_y = 0;
27177 hlinfo->mouse_face_end_y = 0;
27178 hlinfo->mouse_face_past_end = 0;
27179 hlinfo->mouse_face_window = window;
27181 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27182 charpos,
27183 0, 0, 0,
27184 &ignore,
27185 glyph->face_id,
27187 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27189 if (NILP (pointer))
27190 pointer = Qhand;
27192 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27193 clear_mouse_face (hlinfo);
27195 #ifdef HAVE_WINDOW_SYSTEM
27196 if (FRAME_WINDOW_P (f))
27197 define_frame_cursor1 (f, cursor, pointer);
27198 #endif
27202 /* EXPORT:
27203 Take proper action when the mouse has moved to position X, Y on
27204 frame F as regards highlighting characters that have mouse-face
27205 properties. Also de-highlighting chars where the mouse was before.
27206 X and Y can be negative or out of range. */
27208 void
27209 note_mouse_highlight (struct frame *f, int x, int y)
27211 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27212 enum window_part part = ON_NOTHING;
27213 Lisp_Object window;
27214 struct window *w;
27215 Cursor cursor = No_Cursor;
27216 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27217 struct buffer *b;
27219 /* When a menu is active, don't highlight because this looks odd. */
27220 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27221 if (popup_activated ())
27222 return;
27223 #endif
27225 if (NILP (Vmouse_highlight)
27226 || !f->glyphs_initialized_p
27227 || f->pointer_invisible)
27228 return;
27230 hlinfo->mouse_face_mouse_x = x;
27231 hlinfo->mouse_face_mouse_y = y;
27232 hlinfo->mouse_face_mouse_frame = f;
27234 if (hlinfo->mouse_face_defer)
27235 return;
27237 if (gc_in_progress)
27239 hlinfo->mouse_face_deferred_gc = 1;
27240 return;
27243 /* Which window is that in? */
27244 window = window_from_coordinates (f, x, y, &part, 1);
27246 /* If displaying active text in another window, clear that. */
27247 if (! EQ (window, hlinfo->mouse_face_window)
27248 /* Also clear if we move out of text area in same window. */
27249 || (!NILP (hlinfo->mouse_face_window)
27250 && !NILP (window)
27251 && part != ON_TEXT
27252 && part != ON_MODE_LINE
27253 && part != ON_HEADER_LINE))
27254 clear_mouse_face (hlinfo);
27256 /* Not on a window -> return. */
27257 if (!WINDOWP (window))
27258 return;
27260 /* Reset help_echo_string. It will get recomputed below. */
27261 help_echo_string = Qnil;
27263 /* Convert to window-relative pixel coordinates. */
27264 w = XWINDOW (window);
27265 frame_to_window_pixel_xy (w, &x, &y);
27267 #ifdef HAVE_WINDOW_SYSTEM
27268 /* Handle tool-bar window differently since it doesn't display a
27269 buffer. */
27270 if (EQ (window, f->tool_bar_window))
27272 note_tool_bar_highlight (f, x, y);
27273 return;
27275 #endif
27277 /* Mouse is on the mode, header line or margin? */
27278 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27279 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27281 note_mode_line_or_margin_highlight (window, x, y, part);
27282 return;
27285 #ifdef HAVE_WINDOW_SYSTEM
27286 if (part == ON_VERTICAL_BORDER)
27288 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27289 help_echo_string = build_string ("drag-mouse-1: resize");
27291 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27292 || part == ON_SCROLL_BAR)
27293 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27294 else
27295 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27296 #endif
27298 /* Are we in a window whose display is up to date?
27299 And verify the buffer's text has not changed. */
27300 b = XBUFFER (w->buffer);
27301 if (part == ON_TEXT
27302 && EQ (w->window_end_valid, w->buffer)
27303 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
27304 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
27306 int hpos, vpos, dx, dy, area = LAST_AREA;
27307 EMACS_INT pos;
27308 struct glyph *glyph;
27309 Lisp_Object object;
27310 Lisp_Object mouse_face = Qnil, position;
27311 Lisp_Object *overlay_vec = NULL;
27312 ptrdiff_t i, noverlays;
27313 struct buffer *obuf;
27314 EMACS_INT obegv, ozv;
27315 int same_region;
27317 /* Find the glyph under X/Y. */
27318 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27320 #ifdef HAVE_WINDOW_SYSTEM
27321 /* Look for :pointer property on image. */
27322 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27324 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27325 if (img != NULL && IMAGEP (img->spec))
27327 Lisp_Object image_map, hotspot;
27328 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27329 !NILP (image_map))
27330 && (hotspot = find_hot_spot (image_map,
27331 glyph->slice.img.x + dx,
27332 glyph->slice.img.y + dy),
27333 CONSP (hotspot))
27334 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27336 Lisp_Object plist;
27338 /* Could check XCAR (hotspot) to see if we enter/leave
27339 this hot-spot.
27340 If so, we could look for mouse-enter, mouse-leave
27341 properties in PLIST (and do something...). */
27342 hotspot = XCDR (hotspot);
27343 if (CONSP (hotspot)
27344 && (plist = XCAR (hotspot), CONSP (plist)))
27346 pointer = Fplist_get (plist, Qpointer);
27347 if (NILP (pointer))
27348 pointer = Qhand;
27349 help_echo_string = Fplist_get (plist, Qhelp_echo);
27350 if (!NILP (help_echo_string))
27352 help_echo_window = window;
27353 help_echo_object = glyph->object;
27354 help_echo_pos = glyph->charpos;
27358 if (NILP (pointer))
27359 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27362 #endif /* HAVE_WINDOW_SYSTEM */
27364 /* Clear mouse face if X/Y not over text. */
27365 if (glyph == NULL
27366 || area != TEXT_AREA
27367 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27368 /* Glyph's OBJECT is an integer for glyphs inserted by the
27369 display engine for its internal purposes, like truncation
27370 and continuation glyphs and blanks beyond the end of
27371 line's text on text terminals. If we are over such a
27372 glyph, we are not over any text. */
27373 || INTEGERP (glyph->object)
27374 /* R2L rows have a stretch glyph at their front, which
27375 stands for no text, whereas L2R rows have no glyphs at
27376 all beyond the end of text. Treat such stretch glyphs
27377 like we do with NULL glyphs in L2R rows. */
27378 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27379 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27380 && glyph->type == STRETCH_GLYPH
27381 && glyph->avoid_cursor_p))
27383 if (clear_mouse_face (hlinfo))
27384 cursor = No_Cursor;
27385 #ifdef HAVE_WINDOW_SYSTEM
27386 if (FRAME_WINDOW_P (f) && NILP (pointer))
27388 if (area != TEXT_AREA)
27389 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27390 else
27391 pointer = Vvoid_text_area_pointer;
27393 #endif
27394 goto set_cursor;
27397 pos = glyph->charpos;
27398 object = glyph->object;
27399 if (!STRINGP (object) && !BUFFERP (object))
27400 goto set_cursor;
27402 /* If we get an out-of-range value, return now; avoid an error. */
27403 if (BUFFERP (object) && pos > BUF_Z (b))
27404 goto set_cursor;
27406 /* Make the window's buffer temporarily current for
27407 overlays_at and compute_char_face. */
27408 obuf = current_buffer;
27409 current_buffer = b;
27410 obegv = BEGV;
27411 ozv = ZV;
27412 BEGV = BEG;
27413 ZV = Z;
27415 /* Is this char mouse-active or does it have help-echo? */
27416 position = make_number (pos);
27418 if (BUFFERP (object))
27420 /* Put all the overlays we want in a vector in overlay_vec. */
27421 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27422 /* Sort overlays into increasing priority order. */
27423 noverlays = sort_overlays (overlay_vec, noverlays, w);
27425 else
27426 noverlays = 0;
27428 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27430 if (same_region)
27431 cursor = No_Cursor;
27433 /* Check mouse-face highlighting. */
27434 if (! same_region
27435 /* If there exists an overlay with mouse-face overlapping
27436 the one we are currently highlighting, we have to
27437 check if we enter the overlapping overlay, and then
27438 highlight only that. */
27439 || (OVERLAYP (hlinfo->mouse_face_overlay)
27440 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27442 /* Find the highest priority overlay with a mouse-face. */
27443 Lisp_Object overlay = Qnil;
27444 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27446 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27447 if (!NILP (mouse_face))
27448 overlay = overlay_vec[i];
27451 /* If we're highlighting the same overlay as before, there's
27452 no need to do that again. */
27453 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27454 goto check_help_echo;
27455 hlinfo->mouse_face_overlay = overlay;
27457 /* Clear the display of the old active region, if any. */
27458 if (clear_mouse_face (hlinfo))
27459 cursor = No_Cursor;
27461 /* If no overlay applies, get a text property. */
27462 if (NILP (overlay))
27463 mouse_face = Fget_text_property (position, Qmouse_face, object);
27465 /* Next, compute the bounds of the mouse highlighting and
27466 display it. */
27467 if (!NILP (mouse_face) && STRINGP (object))
27469 /* The mouse-highlighting comes from a display string
27470 with a mouse-face. */
27471 Lisp_Object s, e;
27472 EMACS_INT ignore;
27474 s = Fprevious_single_property_change
27475 (make_number (pos + 1), Qmouse_face, object, Qnil);
27476 e = Fnext_single_property_change
27477 (position, Qmouse_face, object, Qnil);
27478 if (NILP (s))
27479 s = make_number (0);
27480 if (NILP (e))
27481 e = make_number (SCHARS (object) - 1);
27482 mouse_face_from_string_pos (w, hlinfo, object,
27483 XINT (s), XINT (e));
27484 hlinfo->mouse_face_past_end = 0;
27485 hlinfo->mouse_face_window = window;
27486 hlinfo->mouse_face_face_id
27487 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27488 glyph->face_id, 1);
27489 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27490 cursor = No_Cursor;
27492 else
27494 /* The mouse-highlighting, if any, comes from an overlay
27495 or text property in the buffer. */
27496 Lisp_Object buffer IF_LINT (= Qnil);
27497 Lisp_Object disp_string IF_LINT (= Qnil);
27499 if (STRINGP (object))
27501 /* If we are on a display string with no mouse-face,
27502 check if the text under it has one. */
27503 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27504 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27505 pos = string_buffer_position (object, start);
27506 if (pos > 0)
27508 mouse_face = get_char_property_and_overlay
27509 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27510 buffer = w->buffer;
27511 disp_string = object;
27514 else
27516 buffer = object;
27517 disp_string = Qnil;
27520 if (!NILP (mouse_face))
27522 Lisp_Object before, after;
27523 Lisp_Object before_string, after_string;
27524 /* To correctly find the limits of mouse highlight
27525 in a bidi-reordered buffer, we must not use the
27526 optimization of limiting the search in
27527 previous-single-property-change and
27528 next-single-property-change, because
27529 rows_from_pos_range needs the real start and end
27530 positions to DTRT in this case. That's because
27531 the first row visible in a window does not
27532 necessarily display the character whose position
27533 is the smallest. */
27534 Lisp_Object lim1 =
27535 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27536 ? Fmarker_position (w->start)
27537 : Qnil;
27538 Lisp_Object lim2 =
27539 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27540 ? make_number (BUF_Z (XBUFFER (buffer))
27541 - XFASTINT (w->window_end_pos))
27542 : Qnil;
27544 if (NILP (overlay))
27546 /* Handle the text property case. */
27547 before = Fprevious_single_property_change
27548 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27549 after = Fnext_single_property_change
27550 (make_number (pos), Qmouse_face, buffer, lim2);
27551 before_string = after_string = Qnil;
27553 else
27555 /* Handle the overlay case. */
27556 before = Foverlay_start (overlay);
27557 after = Foverlay_end (overlay);
27558 before_string = Foverlay_get (overlay, Qbefore_string);
27559 after_string = Foverlay_get (overlay, Qafter_string);
27561 if (!STRINGP (before_string)) before_string = Qnil;
27562 if (!STRINGP (after_string)) after_string = Qnil;
27565 mouse_face_from_buffer_pos (window, hlinfo, pos,
27566 NILP (before)
27568 : XFASTINT (before),
27569 NILP (after)
27570 ? BUF_Z (XBUFFER (buffer))
27571 : XFASTINT (after),
27572 before_string, after_string,
27573 disp_string);
27574 cursor = No_Cursor;
27579 check_help_echo:
27581 /* Look for a `help-echo' property. */
27582 if (NILP (help_echo_string)) {
27583 Lisp_Object help, overlay;
27585 /* Check overlays first. */
27586 help = overlay = Qnil;
27587 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27589 overlay = overlay_vec[i];
27590 help = Foverlay_get (overlay, Qhelp_echo);
27593 if (!NILP (help))
27595 help_echo_string = help;
27596 help_echo_window = window;
27597 help_echo_object = overlay;
27598 help_echo_pos = pos;
27600 else
27602 Lisp_Object obj = glyph->object;
27603 EMACS_INT charpos = glyph->charpos;
27605 /* Try text properties. */
27606 if (STRINGP (obj)
27607 && charpos >= 0
27608 && charpos < SCHARS (obj))
27610 help = Fget_text_property (make_number (charpos),
27611 Qhelp_echo, obj);
27612 if (NILP (help))
27614 /* If the string itself doesn't specify a help-echo,
27615 see if the buffer text ``under'' it does. */
27616 struct glyph_row *r
27617 = MATRIX_ROW (w->current_matrix, vpos);
27618 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27619 EMACS_INT p = string_buffer_position (obj, start);
27620 if (p > 0)
27622 help = Fget_char_property (make_number (p),
27623 Qhelp_echo, w->buffer);
27624 if (!NILP (help))
27626 charpos = p;
27627 obj = w->buffer;
27632 else if (BUFFERP (obj)
27633 && charpos >= BEGV
27634 && charpos < ZV)
27635 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27636 obj);
27638 if (!NILP (help))
27640 help_echo_string = help;
27641 help_echo_window = window;
27642 help_echo_object = obj;
27643 help_echo_pos = charpos;
27648 #ifdef HAVE_WINDOW_SYSTEM
27649 /* Look for a `pointer' property. */
27650 if (FRAME_WINDOW_P (f) && NILP (pointer))
27652 /* Check overlays first. */
27653 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27654 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27656 if (NILP (pointer))
27658 Lisp_Object obj = glyph->object;
27659 EMACS_INT charpos = glyph->charpos;
27661 /* Try text properties. */
27662 if (STRINGP (obj)
27663 && charpos >= 0
27664 && charpos < SCHARS (obj))
27666 pointer = Fget_text_property (make_number (charpos),
27667 Qpointer, obj);
27668 if (NILP (pointer))
27670 /* If the string itself doesn't specify a pointer,
27671 see if the buffer text ``under'' it does. */
27672 struct glyph_row *r
27673 = MATRIX_ROW (w->current_matrix, vpos);
27674 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27675 EMACS_INT p = string_buffer_position (obj, start);
27676 if (p > 0)
27677 pointer = Fget_char_property (make_number (p),
27678 Qpointer, w->buffer);
27681 else if (BUFFERP (obj)
27682 && charpos >= BEGV
27683 && charpos < ZV)
27684 pointer = Fget_text_property (make_number (charpos),
27685 Qpointer, obj);
27688 #endif /* HAVE_WINDOW_SYSTEM */
27690 BEGV = obegv;
27691 ZV = ozv;
27692 current_buffer = obuf;
27695 set_cursor:
27697 #ifdef HAVE_WINDOW_SYSTEM
27698 if (FRAME_WINDOW_P (f))
27699 define_frame_cursor1 (f, cursor, pointer);
27700 #else
27701 /* This is here to prevent a compiler error, about "label at end of
27702 compound statement". */
27703 return;
27704 #endif
27708 /* EXPORT for RIF:
27709 Clear any mouse-face on window W. This function is part of the
27710 redisplay interface, and is called from try_window_id and similar
27711 functions to ensure the mouse-highlight is off. */
27713 void
27714 x_clear_window_mouse_face (struct window *w)
27716 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27717 Lisp_Object window;
27719 BLOCK_INPUT;
27720 XSETWINDOW (window, w);
27721 if (EQ (window, hlinfo->mouse_face_window))
27722 clear_mouse_face (hlinfo);
27723 UNBLOCK_INPUT;
27727 /* EXPORT:
27728 Just discard the mouse face information for frame F, if any.
27729 This is used when the size of F is changed. */
27731 void
27732 cancel_mouse_face (struct frame *f)
27734 Lisp_Object window;
27735 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27737 window = hlinfo->mouse_face_window;
27738 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
27740 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27741 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27742 hlinfo->mouse_face_window = Qnil;
27748 /***********************************************************************
27749 Exposure Events
27750 ***********************************************************************/
27752 #ifdef HAVE_WINDOW_SYSTEM
27754 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
27755 which intersects rectangle R. R is in window-relative coordinates. */
27757 static void
27758 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
27759 enum glyph_row_area area)
27761 struct glyph *first = row->glyphs[area];
27762 struct glyph *end = row->glyphs[area] + row->used[area];
27763 struct glyph *last;
27764 int first_x, start_x, x;
27766 if (area == TEXT_AREA && row->fill_line_p)
27767 /* If row extends face to end of line write the whole line. */
27768 draw_glyphs (w, 0, row, area,
27769 0, row->used[area],
27770 DRAW_NORMAL_TEXT, 0);
27771 else
27773 /* Set START_X to the window-relative start position for drawing glyphs of
27774 AREA. The first glyph of the text area can be partially visible.
27775 The first glyphs of other areas cannot. */
27776 start_x = window_box_left_offset (w, area);
27777 x = start_x;
27778 if (area == TEXT_AREA)
27779 x += row->x;
27781 /* Find the first glyph that must be redrawn. */
27782 while (first < end
27783 && x + first->pixel_width < r->x)
27785 x += first->pixel_width;
27786 ++first;
27789 /* Find the last one. */
27790 last = first;
27791 first_x = x;
27792 while (last < end
27793 && x < r->x + r->width)
27795 x += last->pixel_width;
27796 ++last;
27799 /* Repaint. */
27800 if (last > first)
27801 draw_glyphs (w, first_x - start_x, row, area,
27802 first - row->glyphs[area], last - row->glyphs[area],
27803 DRAW_NORMAL_TEXT, 0);
27808 /* Redraw the parts of the glyph row ROW on window W intersecting
27809 rectangle R. R is in window-relative coordinates. Value is
27810 non-zero if mouse-face was overwritten. */
27812 static int
27813 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
27815 xassert (row->enabled_p);
27817 if (row->mode_line_p || w->pseudo_window_p)
27818 draw_glyphs (w, 0, row, TEXT_AREA,
27819 0, row->used[TEXT_AREA],
27820 DRAW_NORMAL_TEXT, 0);
27821 else
27823 if (row->used[LEFT_MARGIN_AREA])
27824 expose_area (w, row, r, LEFT_MARGIN_AREA);
27825 if (row->used[TEXT_AREA])
27826 expose_area (w, row, r, TEXT_AREA);
27827 if (row->used[RIGHT_MARGIN_AREA])
27828 expose_area (w, row, r, RIGHT_MARGIN_AREA);
27829 draw_row_fringe_bitmaps (w, row);
27832 return row->mouse_face_p;
27836 /* Redraw those parts of glyphs rows during expose event handling that
27837 overlap other rows. Redrawing of an exposed line writes over parts
27838 of lines overlapping that exposed line; this function fixes that.
27840 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
27841 row in W's current matrix that is exposed and overlaps other rows.
27842 LAST_OVERLAPPING_ROW is the last such row. */
27844 static void
27845 expose_overlaps (struct window *w,
27846 struct glyph_row *first_overlapping_row,
27847 struct glyph_row *last_overlapping_row,
27848 XRectangle *r)
27850 struct glyph_row *row;
27852 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
27853 if (row->overlapping_p)
27855 xassert (row->enabled_p && !row->mode_line_p);
27857 row->clip = r;
27858 if (row->used[LEFT_MARGIN_AREA])
27859 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
27861 if (row->used[TEXT_AREA])
27862 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
27864 if (row->used[RIGHT_MARGIN_AREA])
27865 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
27866 row->clip = NULL;
27871 /* Return non-zero if W's cursor intersects rectangle R. */
27873 static int
27874 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
27876 XRectangle cr, result;
27877 struct glyph *cursor_glyph;
27878 struct glyph_row *row;
27880 if (w->phys_cursor.vpos >= 0
27881 && w->phys_cursor.vpos < w->current_matrix->nrows
27882 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
27883 row->enabled_p)
27884 && row->cursor_in_fringe_p)
27886 /* Cursor is in the fringe. */
27887 cr.x = window_box_right_offset (w,
27888 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
27889 ? RIGHT_MARGIN_AREA
27890 : TEXT_AREA));
27891 cr.y = row->y;
27892 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
27893 cr.height = row->height;
27894 return x_intersect_rectangles (&cr, r, &result);
27897 cursor_glyph = get_phys_cursor_glyph (w);
27898 if (cursor_glyph)
27900 /* r is relative to W's box, but w->phys_cursor.x is relative
27901 to left edge of W's TEXT area. Adjust it. */
27902 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
27903 cr.y = w->phys_cursor.y;
27904 cr.width = cursor_glyph->pixel_width;
27905 cr.height = w->phys_cursor_height;
27906 /* ++KFS: W32 version used W32-specific IntersectRect here, but
27907 I assume the effect is the same -- and this is portable. */
27908 return x_intersect_rectangles (&cr, r, &result);
27910 /* If we don't understand the format, pretend we're not in the hot-spot. */
27911 return 0;
27915 /* EXPORT:
27916 Draw a vertical window border to the right of window W if W doesn't
27917 have vertical scroll bars. */
27919 void
27920 x_draw_vertical_border (struct window *w)
27922 struct frame *f = XFRAME (WINDOW_FRAME (w));
27924 /* We could do better, if we knew what type of scroll-bar the adjacent
27925 windows (on either side) have... But we don't :-(
27926 However, I think this works ok. ++KFS 2003-04-25 */
27928 /* Redraw borders between horizontally adjacent windows. Don't
27929 do it for frames with vertical scroll bars because either the
27930 right scroll bar of a window, or the left scroll bar of its
27931 neighbor will suffice as a border. */
27932 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
27933 return;
27935 if (!WINDOW_RIGHTMOST_P (w)
27936 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
27938 int x0, x1, y0, y1;
27940 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27941 y1 -= 1;
27943 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27944 x1 -= 1;
27946 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
27948 else if (!WINDOW_LEFTMOST_P (w)
27949 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
27951 int x0, x1, y0, y1;
27953 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27954 y1 -= 1;
27956 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27957 x0 -= 1;
27959 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
27964 /* Redraw the part of window W intersection rectangle FR. Pixel
27965 coordinates in FR are frame-relative. Call this function with
27966 input blocked. Value is non-zero if the exposure overwrites
27967 mouse-face. */
27969 static int
27970 expose_window (struct window *w, XRectangle *fr)
27972 struct frame *f = XFRAME (w->frame);
27973 XRectangle wr, r;
27974 int mouse_face_overwritten_p = 0;
27976 /* If window is not yet fully initialized, do nothing. This can
27977 happen when toolkit scroll bars are used and a window is split.
27978 Reconfiguring the scroll bar will generate an expose for a newly
27979 created window. */
27980 if (w->current_matrix == NULL)
27981 return 0;
27983 /* When we're currently updating the window, display and current
27984 matrix usually don't agree. Arrange for a thorough display
27985 later. */
27986 if (w == updated_window)
27988 SET_FRAME_GARBAGED (f);
27989 return 0;
27992 /* Frame-relative pixel rectangle of W. */
27993 wr.x = WINDOW_LEFT_EDGE_X (w);
27994 wr.y = WINDOW_TOP_EDGE_Y (w);
27995 wr.width = WINDOW_TOTAL_WIDTH (w);
27996 wr.height = WINDOW_TOTAL_HEIGHT (w);
27998 if (x_intersect_rectangles (fr, &wr, &r))
28000 int yb = window_text_bottom_y (w);
28001 struct glyph_row *row;
28002 int cursor_cleared_p, phys_cursor_on_p;
28003 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28005 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28006 r.x, r.y, r.width, r.height));
28008 /* Convert to window coordinates. */
28009 r.x -= WINDOW_LEFT_EDGE_X (w);
28010 r.y -= WINDOW_TOP_EDGE_Y (w);
28012 /* Turn off the cursor. */
28013 if (!w->pseudo_window_p
28014 && phys_cursor_in_rect_p (w, &r))
28016 x_clear_cursor (w);
28017 cursor_cleared_p = 1;
28019 else
28020 cursor_cleared_p = 0;
28022 /* If the row containing the cursor extends face to end of line,
28023 then expose_area might overwrite the cursor outside the
28024 rectangle and thus notice_overwritten_cursor might clear
28025 w->phys_cursor_on_p. We remember the original value and
28026 check later if it is changed. */
28027 phys_cursor_on_p = w->phys_cursor_on_p;
28029 /* Update lines intersecting rectangle R. */
28030 first_overlapping_row = last_overlapping_row = NULL;
28031 for (row = w->current_matrix->rows;
28032 row->enabled_p;
28033 ++row)
28035 int y0 = row->y;
28036 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28038 if ((y0 >= r.y && y0 < r.y + r.height)
28039 || (y1 > r.y && y1 < r.y + r.height)
28040 || (r.y >= y0 && r.y < y1)
28041 || (r.y + r.height > y0 && r.y + r.height < y1))
28043 /* A header line may be overlapping, but there is no need
28044 to fix overlapping areas for them. KFS 2005-02-12 */
28045 if (row->overlapping_p && !row->mode_line_p)
28047 if (first_overlapping_row == NULL)
28048 first_overlapping_row = row;
28049 last_overlapping_row = row;
28052 row->clip = fr;
28053 if (expose_line (w, row, &r))
28054 mouse_face_overwritten_p = 1;
28055 row->clip = NULL;
28057 else if (row->overlapping_p)
28059 /* We must redraw a row overlapping the exposed area. */
28060 if (y0 < r.y
28061 ? y0 + row->phys_height > r.y
28062 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28064 if (first_overlapping_row == NULL)
28065 first_overlapping_row = row;
28066 last_overlapping_row = row;
28070 if (y1 >= yb)
28071 break;
28074 /* Display the mode line if there is one. */
28075 if (WINDOW_WANTS_MODELINE_P (w)
28076 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28077 row->enabled_p)
28078 && row->y < r.y + r.height)
28080 if (expose_line (w, row, &r))
28081 mouse_face_overwritten_p = 1;
28084 if (!w->pseudo_window_p)
28086 /* Fix the display of overlapping rows. */
28087 if (first_overlapping_row)
28088 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28089 fr);
28091 /* Draw border between windows. */
28092 x_draw_vertical_border (w);
28094 /* Turn the cursor on again. */
28095 if (cursor_cleared_p
28096 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28097 update_window_cursor (w, 1);
28101 return mouse_face_overwritten_p;
28106 /* Redraw (parts) of all windows in the window tree rooted at W that
28107 intersect R. R contains frame pixel coordinates. Value is
28108 non-zero if the exposure overwrites mouse-face. */
28110 static int
28111 expose_window_tree (struct window *w, XRectangle *r)
28113 struct frame *f = XFRAME (w->frame);
28114 int mouse_face_overwritten_p = 0;
28116 while (w && !FRAME_GARBAGED_P (f))
28118 if (!NILP (w->hchild))
28119 mouse_face_overwritten_p
28120 |= expose_window_tree (XWINDOW (w->hchild), r);
28121 else if (!NILP (w->vchild))
28122 mouse_face_overwritten_p
28123 |= expose_window_tree (XWINDOW (w->vchild), r);
28124 else
28125 mouse_face_overwritten_p |= expose_window (w, r);
28127 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28130 return mouse_face_overwritten_p;
28134 /* EXPORT:
28135 Redisplay an exposed area of frame F. X and Y are the upper-left
28136 corner of the exposed rectangle. W and H are width and height of
28137 the exposed area. All are pixel values. W or H zero means redraw
28138 the entire frame. */
28140 void
28141 expose_frame (struct frame *f, int x, int y, int w, int h)
28143 XRectangle r;
28144 int mouse_face_overwritten_p = 0;
28146 TRACE ((stderr, "expose_frame "));
28148 /* No need to redraw if frame will be redrawn soon. */
28149 if (FRAME_GARBAGED_P (f))
28151 TRACE ((stderr, " garbaged\n"));
28152 return;
28155 /* If basic faces haven't been realized yet, there is no point in
28156 trying to redraw anything. This can happen when we get an expose
28157 event while Emacs is starting, e.g. by moving another window. */
28158 if (FRAME_FACE_CACHE (f) == NULL
28159 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28161 TRACE ((stderr, " no faces\n"));
28162 return;
28165 if (w == 0 || h == 0)
28167 r.x = r.y = 0;
28168 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28169 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28171 else
28173 r.x = x;
28174 r.y = y;
28175 r.width = w;
28176 r.height = h;
28179 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28180 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28182 if (WINDOWP (f->tool_bar_window))
28183 mouse_face_overwritten_p
28184 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28186 #ifdef HAVE_X_WINDOWS
28187 #ifndef MSDOS
28188 #ifndef USE_X_TOOLKIT
28189 if (WINDOWP (f->menu_bar_window))
28190 mouse_face_overwritten_p
28191 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28192 #endif /* not USE_X_TOOLKIT */
28193 #endif
28194 #endif
28196 /* Some window managers support a focus-follows-mouse style with
28197 delayed raising of frames. Imagine a partially obscured frame,
28198 and moving the mouse into partially obscured mouse-face on that
28199 frame. The visible part of the mouse-face will be highlighted,
28200 then the WM raises the obscured frame. With at least one WM, KDE
28201 2.1, Emacs is not getting any event for the raising of the frame
28202 (even tried with SubstructureRedirectMask), only Expose events.
28203 These expose events will draw text normally, i.e. not
28204 highlighted. Which means we must redo the highlight here.
28205 Subsume it under ``we love X''. --gerd 2001-08-15 */
28206 /* Included in Windows version because Windows most likely does not
28207 do the right thing if any third party tool offers
28208 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28209 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28211 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28212 if (f == hlinfo->mouse_face_mouse_frame)
28214 int mouse_x = hlinfo->mouse_face_mouse_x;
28215 int mouse_y = hlinfo->mouse_face_mouse_y;
28216 clear_mouse_face (hlinfo);
28217 note_mouse_highlight (f, mouse_x, mouse_y);
28223 /* EXPORT:
28224 Determine the intersection of two rectangles R1 and R2. Return
28225 the intersection in *RESULT. Value is non-zero if RESULT is not
28226 empty. */
28229 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28231 XRectangle *left, *right;
28232 XRectangle *upper, *lower;
28233 int intersection_p = 0;
28235 /* Rearrange so that R1 is the left-most rectangle. */
28236 if (r1->x < r2->x)
28237 left = r1, right = r2;
28238 else
28239 left = r2, right = r1;
28241 /* X0 of the intersection is right.x0, if this is inside R1,
28242 otherwise there is no intersection. */
28243 if (right->x <= left->x + left->width)
28245 result->x = right->x;
28247 /* The right end of the intersection is the minimum of
28248 the right ends of left and right. */
28249 result->width = (min (left->x + left->width, right->x + right->width)
28250 - result->x);
28252 /* Same game for Y. */
28253 if (r1->y < r2->y)
28254 upper = r1, lower = r2;
28255 else
28256 upper = r2, lower = r1;
28258 /* The upper end of the intersection is lower.y0, if this is inside
28259 of upper. Otherwise, there is no intersection. */
28260 if (lower->y <= upper->y + upper->height)
28262 result->y = lower->y;
28264 /* The lower end of the intersection is the minimum of the lower
28265 ends of upper and lower. */
28266 result->height = (min (lower->y + lower->height,
28267 upper->y + upper->height)
28268 - result->y);
28269 intersection_p = 1;
28273 return intersection_p;
28276 #endif /* HAVE_WINDOW_SYSTEM */
28279 /***********************************************************************
28280 Initialization
28281 ***********************************************************************/
28283 void
28284 syms_of_xdisp (void)
28286 Vwith_echo_area_save_vector = Qnil;
28287 staticpro (&Vwith_echo_area_save_vector);
28289 Vmessage_stack = Qnil;
28290 staticpro (&Vmessage_stack);
28292 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28294 message_dolog_marker1 = Fmake_marker ();
28295 staticpro (&message_dolog_marker1);
28296 message_dolog_marker2 = Fmake_marker ();
28297 staticpro (&message_dolog_marker2);
28298 message_dolog_marker3 = Fmake_marker ();
28299 staticpro (&message_dolog_marker3);
28301 #if GLYPH_DEBUG
28302 defsubr (&Sdump_frame_glyph_matrix);
28303 defsubr (&Sdump_glyph_matrix);
28304 defsubr (&Sdump_glyph_row);
28305 defsubr (&Sdump_tool_bar_row);
28306 defsubr (&Strace_redisplay);
28307 defsubr (&Strace_to_stderr);
28308 #endif
28309 #ifdef HAVE_WINDOW_SYSTEM
28310 defsubr (&Stool_bar_lines_needed);
28311 defsubr (&Slookup_image_map);
28312 #endif
28313 defsubr (&Sformat_mode_line);
28314 defsubr (&Sinvisible_p);
28315 defsubr (&Scurrent_bidi_paragraph_direction);
28317 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28318 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28319 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28320 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28321 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28322 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28323 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28324 DEFSYM (Qeval, "eval");
28325 DEFSYM (QCdata, ":data");
28326 DEFSYM (Qdisplay, "display");
28327 DEFSYM (Qspace_width, "space-width");
28328 DEFSYM (Qraise, "raise");
28329 DEFSYM (Qslice, "slice");
28330 DEFSYM (Qspace, "space");
28331 DEFSYM (Qmargin, "margin");
28332 DEFSYM (Qpointer, "pointer");
28333 DEFSYM (Qleft_margin, "left-margin");
28334 DEFSYM (Qright_margin, "right-margin");
28335 DEFSYM (Qcenter, "center");
28336 DEFSYM (Qline_height, "line-height");
28337 DEFSYM (QCalign_to, ":align-to");
28338 DEFSYM (QCrelative_width, ":relative-width");
28339 DEFSYM (QCrelative_height, ":relative-height");
28340 DEFSYM (QCeval, ":eval");
28341 DEFSYM (QCpropertize, ":propertize");
28342 DEFSYM (QCfile, ":file");
28343 DEFSYM (Qfontified, "fontified");
28344 DEFSYM (Qfontification_functions, "fontification-functions");
28345 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28346 DEFSYM (Qescape_glyph, "escape-glyph");
28347 DEFSYM (Qnobreak_space, "nobreak-space");
28348 DEFSYM (Qimage, "image");
28349 DEFSYM (Qtext, "text");
28350 DEFSYM (Qboth, "both");
28351 DEFSYM (Qboth_horiz, "both-horiz");
28352 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28353 DEFSYM (QCmap, ":map");
28354 DEFSYM (QCpointer, ":pointer");
28355 DEFSYM (Qrect, "rect");
28356 DEFSYM (Qcircle, "circle");
28357 DEFSYM (Qpoly, "poly");
28358 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28359 DEFSYM (Qgrow_only, "grow-only");
28360 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28361 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28362 DEFSYM (Qposition, "position");
28363 DEFSYM (Qbuffer_position, "buffer-position");
28364 DEFSYM (Qobject, "object");
28365 DEFSYM (Qbar, "bar");
28366 DEFSYM (Qhbar, "hbar");
28367 DEFSYM (Qbox, "box");
28368 DEFSYM (Qhollow, "hollow");
28369 DEFSYM (Qhand, "hand");
28370 DEFSYM (Qarrow, "arrow");
28371 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28373 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28374 Fcons (intern_c_string ("void-variable"), Qnil)),
28375 Qnil);
28376 staticpro (&list_of_error);
28378 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28379 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28380 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28381 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28383 echo_buffer[0] = echo_buffer[1] = Qnil;
28384 staticpro (&echo_buffer[0]);
28385 staticpro (&echo_buffer[1]);
28387 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28388 staticpro (&echo_area_buffer[0]);
28389 staticpro (&echo_area_buffer[1]);
28391 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
28392 staticpro (&Vmessages_buffer_name);
28394 mode_line_proptrans_alist = Qnil;
28395 staticpro (&mode_line_proptrans_alist);
28396 mode_line_string_list = Qnil;
28397 staticpro (&mode_line_string_list);
28398 mode_line_string_face = Qnil;
28399 staticpro (&mode_line_string_face);
28400 mode_line_string_face_prop = Qnil;
28401 staticpro (&mode_line_string_face_prop);
28402 Vmode_line_unwind_vector = Qnil;
28403 staticpro (&Vmode_line_unwind_vector);
28405 help_echo_string = Qnil;
28406 staticpro (&help_echo_string);
28407 help_echo_object = Qnil;
28408 staticpro (&help_echo_object);
28409 help_echo_window = Qnil;
28410 staticpro (&help_echo_window);
28411 previous_help_echo_string = Qnil;
28412 staticpro (&previous_help_echo_string);
28413 help_echo_pos = -1;
28415 DEFSYM (Qright_to_left, "right-to-left");
28416 DEFSYM (Qleft_to_right, "left-to-right");
28418 #ifdef HAVE_WINDOW_SYSTEM
28419 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28420 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
28421 For example, if a block cursor is over a tab, it will be drawn as
28422 wide as that tab on the display. */);
28423 x_stretch_cursor_p = 0;
28424 #endif
28426 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28427 doc: /* *Non-nil means highlight trailing whitespace.
28428 The face used for trailing whitespace is `trailing-whitespace'. */);
28429 Vshow_trailing_whitespace = Qnil;
28431 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28432 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28433 If the value is t, Emacs highlights non-ASCII chars which have the
28434 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28435 or `escape-glyph' face respectively.
28437 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28438 U+2011 (non-breaking hyphen) are affected.
28440 Any other non-nil value means to display these characters as a escape
28441 glyph followed by an ordinary space or hyphen.
28443 A value of nil means no special handling of these characters. */);
28444 Vnobreak_char_display = Qt;
28446 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28447 doc: /* *The pointer shape to show in void text areas.
28448 A value of nil means to show the text pointer. Other options are `arrow',
28449 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28450 Vvoid_text_area_pointer = Qarrow;
28452 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28453 doc: /* Non-nil means don't actually do any redisplay.
28454 This is used for internal purposes. */);
28455 Vinhibit_redisplay = Qnil;
28457 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28458 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28459 Vglobal_mode_string = Qnil;
28461 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28462 doc: /* Marker for where to display an arrow on top of the buffer text.
28463 This must be the beginning of a line in order to work.
28464 See also `overlay-arrow-string'. */);
28465 Voverlay_arrow_position = Qnil;
28467 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28468 doc: /* String to display as an arrow in non-window frames.
28469 See also `overlay-arrow-position'. */);
28470 Voverlay_arrow_string = make_pure_c_string ("=>");
28472 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28473 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28474 The symbols on this list are examined during redisplay to determine
28475 where to display overlay arrows. */);
28476 Voverlay_arrow_variable_list
28477 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28479 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28480 doc: /* *The number of lines to try scrolling a window by when point moves out.
28481 If that fails to bring point back on frame, point is centered instead.
28482 If this is zero, point is always centered after it moves off frame.
28483 If you want scrolling to always be a line at a time, you should set
28484 `scroll-conservatively' to a large value rather than set this to 1. */);
28486 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28487 doc: /* *Scroll up to this many lines, to bring point back on screen.
28488 If point moves off-screen, redisplay will scroll by up to
28489 `scroll-conservatively' lines in order to bring point just barely
28490 onto the screen again. If that cannot be done, then redisplay
28491 recenters point as usual.
28493 If the value is greater than 100, redisplay will never recenter point,
28494 but will always scroll just enough text to bring point into view, even
28495 if you move far away.
28497 A value of zero means always recenter point if it moves off screen. */);
28498 scroll_conservatively = 0;
28500 DEFVAR_INT ("scroll-margin", scroll_margin,
28501 doc: /* *Number of lines of margin at the top and bottom of a window.
28502 Recenter the window whenever point gets within this many lines
28503 of the top or bottom of the window. */);
28504 scroll_margin = 0;
28506 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28507 doc: /* Pixels per inch value for non-window system displays.
28508 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28509 Vdisplay_pixels_per_inch = make_float (72.0);
28511 #if GLYPH_DEBUG
28512 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28513 #endif
28515 DEFVAR_LISP ("truncate-partial-width-windows",
28516 Vtruncate_partial_width_windows,
28517 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28518 For an integer value, truncate lines in each window narrower than the
28519 full frame width, provided the window width is less than that integer;
28520 otherwise, respect the value of `truncate-lines'.
28522 For any other non-nil value, truncate lines in all windows that do
28523 not span the full frame width.
28525 A value of nil means to respect the value of `truncate-lines'.
28527 If `word-wrap' is enabled, you might want to reduce this. */);
28528 Vtruncate_partial_width_windows = make_number (50);
28530 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28531 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28532 Any other value means to use the appropriate face, `mode-line',
28533 `header-line', or `menu' respectively. */);
28534 mode_line_inverse_video = 1;
28536 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28537 doc: /* *Maximum buffer size for which line number should be displayed.
28538 If the buffer is bigger than this, the line number does not appear
28539 in the mode line. A value of nil means no limit. */);
28540 Vline_number_display_limit = Qnil;
28542 DEFVAR_INT ("line-number-display-limit-width",
28543 line_number_display_limit_width,
28544 doc: /* *Maximum line width (in characters) for line number display.
28545 If the average length of the lines near point is bigger than this, then the
28546 line number may be omitted from the mode line. */);
28547 line_number_display_limit_width = 200;
28549 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28550 doc: /* *Non-nil means highlight region even in nonselected windows. */);
28551 highlight_nonselected_windows = 0;
28553 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28554 doc: /* Non-nil if more than one frame is visible on this display.
28555 Minibuffer-only frames don't count, but iconified frames do.
28556 This variable is not guaranteed to be accurate except while processing
28557 `frame-title-format' and `icon-title-format'. */);
28559 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28560 doc: /* Template for displaying the title bar of visible frames.
28561 \(Assuming the window manager supports this feature.)
28563 This variable has the same structure as `mode-line-format', except that
28564 the %c and %l constructs are ignored. It is used only on frames for
28565 which no explicit name has been set \(see `modify-frame-parameters'). */);
28567 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28568 doc: /* Template for displaying the title bar of an iconified frame.
28569 \(Assuming the window manager supports this feature.)
28570 This variable has the same structure as `mode-line-format' (which see),
28571 and is used only on frames for which no explicit name has been set
28572 \(see `modify-frame-parameters'). */);
28573 Vicon_title_format
28574 = Vframe_title_format
28575 = pure_cons (intern_c_string ("multiple-frames"),
28576 pure_cons (make_pure_c_string ("%b"),
28577 pure_cons (pure_cons (empty_unibyte_string,
28578 pure_cons (intern_c_string ("invocation-name"),
28579 pure_cons (make_pure_c_string ("@"),
28580 pure_cons (intern_c_string ("system-name"),
28581 Qnil)))),
28582 Qnil)));
28584 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28585 doc: /* Maximum number of lines to keep in the message log buffer.
28586 If nil, disable message logging. If t, log messages but don't truncate
28587 the buffer when it becomes large. */);
28588 Vmessage_log_max = make_number (100);
28590 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28591 doc: /* Functions called before redisplay, if window sizes have changed.
28592 The value should be a list of functions that take one argument.
28593 Just before redisplay, for each frame, if any of its windows have changed
28594 size since the last redisplay, or have been split or deleted,
28595 all the functions in the list are called, with the frame as argument. */);
28596 Vwindow_size_change_functions = Qnil;
28598 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28599 doc: /* List of functions to call before redisplaying a window with scrolling.
28600 Each function is called with two arguments, the window and its new
28601 display-start position. Note that these functions are also called by
28602 `set-window-buffer'. Also note that the value of `window-end' is not
28603 valid when these functions are called.
28605 Warning: Do not use this feature to alter the way the window
28606 is scrolled. It is not designed for that, and such use probably won't
28607 work. */);
28608 Vwindow_scroll_functions = Qnil;
28610 DEFVAR_LISP ("window-text-change-functions",
28611 Vwindow_text_change_functions,
28612 doc: /* Functions to call in redisplay when text in the window might change. */);
28613 Vwindow_text_change_functions = Qnil;
28615 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28616 doc: /* Functions called when redisplay of a window reaches the end trigger.
28617 Each function is called with two arguments, the window and the end trigger value.
28618 See `set-window-redisplay-end-trigger'. */);
28619 Vredisplay_end_trigger_functions = Qnil;
28621 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28622 doc: /* *Non-nil means autoselect window with mouse pointer.
28623 If nil, do not autoselect windows.
28624 A positive number means delay autoselection by that many seconds: a
28625 window is autoselected only after the mouse has remained in that
28626 window for the duration of the delay.
28627 A negative number has a similar effect, but causes windows to be
28628 autoselected only after the mouse has stopped moving. \(Because of
28629 the way Emacs compares mouse events, you will occasionally wait twice
28630 that time before the window gets selected.\)
28631 Any other value means to autoselect window instantaneously when the
28632 mouse pointer enters it.
28634 Autoselection selects the minibuffer only if it is active, and never
28635 unselects the minibuffer if it is active.
28637 When customizing this variable make sure that the actual value of
28638 `focus-follows-mouse' matches the behavior of your window manager. */);
28639 Vmouse_autoselect_window = Qnil;
28641 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28642 doc: /* *Non-nil means automatically resize tool-bars.
28643 This dynamically changes the tool-bar's height to the minimum height
28644 that is needed to make all tool-bar items visible.
28645 If value is `grow-only', the tool-bar's height is only increased
28646 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28647 Vauto_resize_tool_bars = Qt;
28649 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28650 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28651 auto_raise_tool_bar_buttons_p = 1;
28653 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28654 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28655 make_cursor_line_fully_visible_p = 1;
28657 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
28658 doc: /* *Border below tool-bar in pixels.
28659 If an integer, use it as the height of the border.
28660 If it is one of `internal-border-width' or `border-width', use the
28661 value of the corresponding frame parameter.
28662 Otherwise, no border is added below the tool-bar. */);
28663 Vtool_bar_border = Qinternal_border_width;
28665 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
28666 doc: /* *Margin around tool-bar buttons in pixels.
28667 If an integer, use that for both horizontal and vertical margins.
28668 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
28669 HORZ specifying the horizontal margin, and VERT specifying the
28670 vertical margin. */);
28671 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
28673 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
28674 doc: /* *Relief thickness of tool-bar buttons. */);
28675 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
28677 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
28678 doc: /* Tool bar style to use.
28679 It can be one of
28680 image - show images only
28681 text - show text only
28682 both - show both, text below image
28683 both-horiz - show text to the right of the image
28684 text-image-horiz - show text to the left of the image
28685 any other - use system default or image if no system default. */);
28686 Vtool_bar_style = Qnil;
28688 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
28689 doc: /* *Maximum number of characters a label can have to be shown.
28690 The tool bar style must also show labels for this to have any effect, see
28691 `tool-bar-style'. */);
28692 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
28694 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
28695 doc: /* List of functions to call to fontify regions of text.
28696 Each function is called with one argument POS. Functions must
28697 fontify a region starting at POS in the current buffer, and give
28698 fontified regions the property `fontified'. */);
28699 Vfontification_functions = Qnil;
28700 Fmake_variable_buffer_local (Qfontification_functions);
28702 DEFVAR_BOOL ("unibyte-display-via-language-environment",
28703 unibyte_display_via_language_environment,
28704 doc: /* *Non-nil means display unibyte text according to language environment.
28705 Specifically, this means that raw bytes in the range 160-255 decimal
28706 are displayed by converting them to the equivalent multibyte characters
28707 according to the current language environment. As a result, they are
28708 displayed according to the current fontset.
28710 Note that this variable affects only how these bytes are displayed,
28711 but does not change the fact they are interpreted as raw bytes. */);
28712 unibyte_display_via_language_environment = 0;
28714 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
28715 doc: /* *Maximum height for resizing mini-windows (the minibuffer and the echo area).
28716 If a float, it specifies a fraction of the mini-window frame's height.
28717 If an integer, it specifies a number of lines. */);
28718 Vmax_mini_window_height = make_float (0.25);
28720 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
28721 doc: /* How to resize mini-windows (the minibuffer and the echo area).
28722 A value of nil means don't automatically resize mini-windows.
28723 A value of t means resize them to fit the text displayed in them.
28724 A value of `grow-only', the default, means let mini-windows grow only;
28725 they return to their normal size when the minibuffer is closed, or the
28726 echo area becomes empty. */);
28727 Vresize_mini_windows = Qgrow_only;
28729 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
28730 doc: /* Alist specifying how to blink the cursor off.
28731 Each element has the form (ON-STATE . OFF-STATE). Whenever the
28732 `cursor-type' frame-parameter or variable equals ON-STATE,
28733 comparing using `equal', Emacs uses OFF-STATE to specify
28734 how to blink it off. ON-STATE and OFF-STATE are values for
28735 the `cursor-type' frame parameter.
28737 If a frame's ON-STATE has no entry in this list,
28738 the frame's other specifications determine how to blink the cursor off. */);
28739 Vblink_cursor_alist = Qnil;
28741 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
28742 doc: /* Allow or disallow automatic horizontal scrolling of windows.
28743 If non-nil, windows are automatically scrolled horizontally to make
28744 point visible. */);
28745 automatic_hscrolling_p = 1;
28746 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
28748 DEFVAR_INT ("hscroll-margin", hscroll_margin,
28749 doc: /* *How many columns away from the window edge point is allowed to get
28750 before automatic hscrolling will horizontally scroll the window. */);
28751 hscroll_margin = 5;
28753 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
28754 doc: /* *How many columns to scroll the window when point gets too close to the edge.
28755 When point is less than `hscroll-margin' columns from the window
28756 edge, automatic hscrolling will scroll the window by the amount of columns
28757 determined by this variable. If its value is a positive integer, scroll that
28758 many columns. If it's a positive floating-point number, it specifies the
28759 fraction of the window's width to scroll. If it's nil or zero, point will be
28760 centered horizontally after the scroll. Any other value, including negative
28761 numbers, are treated as if the value were zero.
28763 Automatic hscrolling always moves point outside the scroll margin, so if
28764 point was more than scroll step columns inside the margin, the window will
28765 scroll more than the value given by the scroll step.
28767 Note that the lower bound for automatic hscrolling specified by `scroll-left'
28768 and `scroll-right' overrides this variable's effect. */);
28769 Vhscroll_step = make_number (0);
28771 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
28772 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
28773 Bind this around calls to `message' to let it take effect. */);
28774 message_truncate_lines = 0;
28776 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
28777 doc: /* Normal hook run to update the menu bar definitions.
28778 Redisplay runs this hook before it redisplays the menu bar.
28779 This is used to update submenus such as Buffers,
28780 whose contents depend on various data. */);
28781 Vmenu_bar_update_hook = Qnil;
28783 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
28784 doc: /* Frame for which we are updating a menu.
28785 The enable predicate for a menu binding should check this variable. */);
28786 Vmenu_updating_frame = Qnil;
28788 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
28789 doc: /* Non-nil means don't update menu bars. Internal use only. */);
28790 inhibit_menubar_update = 0;
28792 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
28793 doc: /* Prefix prepended to all continuation lines at display time.
28794 The value may be a string, an image, or a stretch-glyph; it is
28795 interpreted in the same way as the value of a `display' text property.
28797 This variable is overridden by any `wrap-prefix' text or overlay
28798 property.
28800 To add a prefix to non-continuation lines, use `line-prefix'. */);
28801 Vwrap_prefix = Qnil;
28802 DEFSYM (Qwrap_prefix, "wrap-prefix");
28803 Fmake_variable_buffer_local (Qwrap_prefix);
28805 DEFVAR_LISP ("line-prefix", Vline_prefix,
28806 doc: /* Prefix prepended to all non-continuation lines at display time.
28807 The value may be a string, an image, or a stretch-glyph; it is
28808 interpreted in the same way as the value of a `display' text property.
28810 This variable is overridden by any `line-prefix' text or overlay
28811 property.
28813 To add a prefix to continuation lines, use `wrap-prefix'. */);
28814 Vline_prefix = Qnil;
28815 DEFSYM (Qline_prefix, "line-prefix");
28816 Fmake_variable_buffer_local (Qline_prefix);
28818 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
28819 doc: /* Non-nil means don't eval Lisp during redisplay. */);
28820 inhibit_eval_during_redisplay = 0;
28822 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
28823 doc: /* Non-nil means don't free realized faces. Internal use only. */);
28824 inhibit_free_realized_faces = 0;
28826 #if GLYPH_DEBUG
28827 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
28828 doc: /* Inhibit try_window_id display optimization. */);
28829 inhibit_try_window_id = 0;
28831 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
28832 doc: /* Inhibit try_window_reusing display optimization. */);
28833 inhibit_try_window_reusing = 0;
28835 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
28836 doc: /* Inhibit try_cursor_movement display optimization. */);
28837 inhibit_try_cursor_movement = 0;
28838 #endif /* GLYPH_DEBUG */
28840 DEFVAR_INT ("overline-margin", overline_margin,
28841 doc: /* *Space between overline and text, in pixels.
28842 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
28843 margin to the character height. */);
28844 overline_margin = 2;
28846 DEFVAR_INT ("underline-minimum-offset",
28847 underline_minimum_offset,
28848 doc: /* Minimum distance between baseline and underline.
28849 This can improve legibility of underlined text at small font sizes,
28850 particularly when using variable `x-use-underline-position-properties'
28851 with fonts that specify an UNDERLINE_POSITION relatively close to the
28852 baseline. The default value is 1. */);
28853 underline_minimum_offset = 1;
28855 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
28856 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
28857 This feature only works when on a window system that can change
28858 cursor shapes. */);
28859 display_hourglass_p = 1;
28861 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
28862 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
28863 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
28865 hourglass_atimer = NULL;
28866 hourglass_shown_p = 0;
28868 DEFSYM (Qglyphless_char, "glyphless-char");
28869 DEFSYM (Qhex_code, "hex-code");
28870 DEFSYM (Qempty_box, "empty-box");
28871 DEFSYM (Qthin_space, "thin-space");
28872 DEFSYM (Qzero_width, "zero-width");
28874 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
28875 /* Intern this now in case it isn't already done.
28876 Setting this variable twice is harmless.
28877 But don't staticpro it here--that is done in alloc.c. */
28878 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
28879 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
28881 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
28882 doc: /* Char-table defining glyphless characters.
28883 Each element, if non-nil, should be one of the following:
28884 an ASCII acronym string: display this string in a box
28885 `hex-code': display the hexadecimal code of a character in a box
28886 `empty-box': display as an empty box
28887 `thin-space': display as 1-pixel width space
28888 `zero-width': don't display
28889 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
28890 display method for graphical terminals and text terminals respectively.
28891 GRAPHICAL and TEXT should each have one of the values listed above.
28893 The char-table has one extra slot to control the display of a character for
28894 which no font is found. This slot only takes effect on graphical terminals.
28895 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
28896 `thin-space'. The default is `empty-box'. */);
28897 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
28898 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
28899 Qempty_box);
28903 /* Initialize this module when Emacs starts. */
28905 void
28906 init_xdisp (void)
28908 current_header_line_height = current_mode_line_height = -1;
28910 CHARPOS (this_line_start_pos) = 0;
28912 if (!noninteractive)
28914 struct window *m = XWINDOW (minibuf_window);
28915 Lisp_Object frame = m->frame;
28916 struct frame *f = XFRAME (frame);
28917 Lisp_Object root = FRAME_ROOT_WINDOW (f);
28918 struct window *r = XWINDOW (root);
28919 int i;
28921 echo_area_window = minibuf_window;
28923 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
28924 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
28925 XSETFASTINT (r->total_cols, FRAME_COLS (f));
28926 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
28927 XSETFASTINT (m->total_lines, 1);
28928 XSETFASTINT (m->total_cols, FRAME_COLS (f));
28930 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
28931 scratch_glyph_row.glyphs[TEXT_AREA + 1]
28932 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
28934 /* The default ellipsis glyphs `...'. */
28935 for (i = 0; i < 3; ++i)
28936 default_invis_vector[i] = make_number ('.');
28940 /* Allocate the buffer for frame titles.
28941 Also used for `format-mode-line'. */
28942 int size = 100;
28943 mode_line_noprop_buf = (char *) xmalloc (size);
28944 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
28945 mode_line_noprop_ptr = mode_line_noprop_buf;
28946 mode_line_target = MODE_LINE_DISPLAY;
28949 help_echo_showing_p = 0;
28952 /* Since w32 does not support atimers, it defines its own implementation of
28953 the following three functions in w32fns.c. */
28954 #ifndef WINDOWSNT
28956 /* Platform-independent portion of hourglass implementation. */
28958 /* Return non-zero if hourglass timer has been started or hourglass is
28959 shown. */
28961 hourglass_started (void)
28963 return hourglass_shown_p || hourglass_atimer != NULL;
28966 /* Cancel a currently active hourglass timer, and start a new one. */
28967 void
28968 start_hourglass (void)
28970 #if defined (HAVE_WINDOW_SYSTEM)
28971 EMACS_TIME delay;
28972 int secs, usecs = 0;
28974 cancel_hourglass ();
28976 if (INTEGERP (Vhourglass_delay)
28977 && XINT (Vhourglass_delay) > 0)
28978 secs = XFASTINT (Vhourglass_delay);
28979 else if (FLOATP (Vhourglass_delay)
28980 && XFLOAT_DATA (Vhourglass_delay) > 0)
28982 Lisp_Object tem;
28983 tem = Ftruncate (Vhourglass_delay, Qnil);
28984 secs = XFASTINT (tem);
28985 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
28987 else
28988 secs = DEFAULT_HOURGLASS_DELAY;
28990 EMACS_SET_SECS_USECS (delay, secs, usecs);
28991 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
28992 show_hourglass, NULL);
28993 #endif
28997 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
28998 shown. */
28999 void
29000 cancel_hourglass (void)
29002 #if defined (HAVE_WINDOW_SYSTEM)
29003 if (hourglass_atimer)
29005 cancel_atimer (hourglass_atimer);
29006 hourglass_atimer = NULL;
29009 if (hourglass_shown_p)
29010 hide_hourglass ();
29011 #endif
29013 #endif /* ! WINDOWSNT */