Merge from emacs-24; up to 2012-04-16T19:06:02Z!rgm@gnu.org
[emacs.git] / src / xdisp.c
blobda44281a55e821abf72f7706259b56dfdc8853cd
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, or the underlying buffer
387 or string character, is a space or a TAB character. This is used
388 to determine where word wrapping can occur. */
390 #define IT_DISPLAYING_WHITESPACE(it) \
391 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
392 || ((STRINGP (it->string) \
393 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
394 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
395 || (it->s \
396 && (it->s[IT_BYTEPOS (*it)] == ' ' \
397 || it->s[IT_BYTEPOS (*it)] == '\t')) \
398 || (IT_BYTEPOS (*it) < ZV_BYTE \
399 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
400 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
402 /* Name of the face used to highlight trailing whitespace. */
404 static Lisp_Object Qtrailing_whitespace;
406 /* Name and number of the face used to highlight escape glyphs. */
408 static Lisp_Object Qescape_glyph;
410 /* Name and number of the face used to highlight non-breaking spaces. */
412 static Lisp_Object Qnobreak_space;
414 /* The symbol `image' which is the car of the lists used to represent
415 images in Lisp. Also a tool bar style. */
417 Lisp_Object Qimage;
419 /* The image map types. */
420 Lisp_Object QCmap;
421 static Lisp_Object QCpointer;
422 static Lisp_Object Qrect, Qcircle, Qpoly;
424 /* Tool bar styles */
425 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
427 /* Non-zero means print newline to stdout before next mini-buffer
428 message. */
430 int noninteractive_need_newline;
432 /* Non-zero means print newline to message log before next message. */
434 static int message_log_need_newline;
436 /* Three markers that message_dolog uses.
437 It could allocate them itself, but that causes trouble
438 in handling memory-full errors. */
439 static Lisp_Object message_dolog_marker1;
440 static Lisp_Object message_dolog_marker2;
441 static Lisp_Object message_dolog_marker3;
443 /* The buffer position of the first character appearing entirely or
444 partially on the line of the selected window which contains the
445 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
446 redisplay optimization in redisplay_internal. */
448 static struct text_pos this_line_start_pos;
450 /* Number of characters past the end of the line above, including the
451 terminating newline. */
453 static struct text_pos this_line_end_pos;
455 /* The vertical positions and the height of this line. */
457 static int this_line_vpos;
458 static int this_line_y;
459 static int this_line_pixel_height;
461 /* X position at which this display line starts. Usually zero;
462 negative if first character is partially visible. */
464 static int this_line_start_x;
466 /* The smallest character position seen by move_it_* functions as they
467 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
468 hscrolled lines, see display_line. */
470 static struct text_pos this_line_min_pos;
472 /* Buffer that this_line_.* variables are referring to. */
474 static struct buffer *this_line_buffer;
477 /* Values of those variables at last redisplay are stored as
478 properties on `overlay-arrow-position' symbol. However, if
479 Voverlay_arrow_position is a marker, last-arrow-position is its
480 numerical position. */
482 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
484 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
485 properties on a symbol in overlay-arrow-variable-list. */
487 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
489 Lisp_Object Qmenu_bar_update_hook;
491 /* Nonzero if an overlay arrow has been displayed in this window. */
493 static int overlay_arrow_seen;
495 /* Number of windows showing the buffer of the selected window (or
496 another buffer with the same base buffer). keyboard.c refers to
497 this. */
499 int buffer_shared;
501 /* Vector containing glyphs for an ellipsis `...'. */
503 static Lisp_Object default_invis_vector[3];
505 /* This is the window where the echo area message was displayed. It
506 is always a mini-buffer window, but it may not be the same window
507 currently active as a mini-buffer. */
509 Lisp_Object echo_area_window;
511 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
512 pushes the current message and the value of
513 message_enable_multibyte on the stack, the function restore_message
514 pops the stack and displays MESSAGE again. */
516 static Lisp_Object Vmessage_stack;
518 /* Nonzero means multibyte characters were enabled when the echo area
519 message was specified. */
521 static int message_enable_multibyte;
523 /* Nonzero if we should redraw the mode lines on the next redisplay. */
525 int update_mode_lines;
527 /* Nonzero if window sizes or contents have changed since last
528 redisplay that finished. */
530 int windows_or_buffers_changed;
532 /* Nonzero means a frame's cursor type has been changed. */
534 int cursor_type_changed;
536 /* Nonzero after display_mode_line if %l was used and it displayed a
537 line number. */
539 static int line_number_displayed;
541 /* The name of the *Messages* buffer, a string. */
543 static Lisp_Object Vmessages_buffer_name;
545 /* Current, index 0, and last displayed echo area message. Either
546 buffers from echo_buffers, or nil to indicate no message. */
548 Lisp_Object echo_area_buffer[2];
550 /* The buffers referenced from echo_area_buffer. */
552 static Lisp_Object echo_buffer[2];
554 /* A vector saved used in with_area_buffer to reduce consing. */
556 static Lisp_Object Vwith_echo_area_save_vector;
558 /* Non-zero means display_echo_area should display the last echo area
559 message again. Set by redisplay_preserve_echo_area. */
561 static int display_last_displayed_message_p;
563 /* Nonzero if echo area is being used by print; zero if being used by
564 message. */
566 static int message_buf_print;
568 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
570 static Lisp_Object Qinhibit_menubar_update;
571 static Lisp_Object Qmessage_truncate_lines;
573 /* Set to 1 in clear_message to make redisplay_internal aware
574 of an emptied echo area. */
576 static int message_cleared_p;
578 /* A scratch glyph row with contents used for generating truncation
579 glyphs. Also used in direct_output_for_insert. */
581 #define MAX_SCRATCH_GLYPHS 100
582 static struct glyph_row scratch_glyph_row;
583 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
585 /* Ascent and height of the last line processed by move_it_to. */
587 static int last_max_ascent, last_height;
589 /* Non-zero if there's a help-echo in the echo area. */
591 int help_echo_showing_p;
593 /* If >= 0, computed, exact values of mode-line and header-line height
594 to use in the macros CURRENT_MODE_LINE_HEIGHT and
595 CURRENT_HEADER_LINE_HEIGHT. */
597 int current_mode_line_height, current_header_line_height;
599 /* The maximum distance to look ahead for text properties. Values
600 that are too small let us call compute_char_face and similar
601 functions too often which is expensive. Values that are too large
602 let us call compute_char_face and alike too often because we
603 might not be interested in text properties that far away. */
605 #define TEXT_PROP_DISTANCE_LIMIT 100
607 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
608 iterator state and later restore it. This is needed because the
609 bidi iterator on bidi.c keeps a stacked cache of its states, which
610 is really a singleton. When we use scratch iterator objects to
611 move around the buffer, we can cause the bidi cache to be pushed or
612 popped, and therefore we need to restore the cache state when we
613 return to the original iterator. */
614 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
615 do { \
616 if (CACHE) \
617 bidi_unshelve_cache (CACHE, 1); \
618 ITCOPY = ITORIG; \
619 CACHE = bidi_shelve_cache (); \
620 } while (0)
622 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
623 do { \
624 if (pITORIG != pITCOPY) \
625 *(pITORIG) = *(pITCOPY); \
626 bidi_unshelve_cache (CACHE, 0); \
627 CACHE = NULL; \
628 } while (0)
630 #if GLYPH_DEBUG
632 /* Non-zero means print traces of redisplay if compiled with
633 GLYPH_DEBUG != 0. */
635 int trace_redisplay_p;
637 #endif /* GLYPH_DEBUG */
639 #ifdef DEBUG_TRACE_MOVE
640 /* Non-zero means trace with TRACE_MOVE to stderr. */
641 int trace_move;
643 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
644 #else
645 #define TRACE_MOVE(x) (void) 0
646 #endif
648 static Lisp_Object Qauto_hscroll_mode;
650 /* Buffer being redisplayed -- for redisplay_window_error. */
652 static struct buffer *displayed_buffer;
654 /* Value returned from text property handlers (see below). */
656 enum prop_handled
658 HANDLED_NORMALLY,
659 HANDLED_RECOMPUTE_PROPS,
660 HANDLED_OVERLAY_STRING_CONSUMED,
661 HANDLED_RETURN
664 /* A description of text properties that redisplay is interested
665 in. */
667 struct props
669 /* The name of the property. */
670 Lisp_Object *name;
672 /* A unique index for the property. */
673 enum prop_idx idx;
675 /* A handler function called to set up iterator IT from the property
676 at IT's current position. Value is used to steer handle_stop. */
677 enum prop_handled (*handler) (struct it *it);
680 static enum prop_handled handle_face_prop (struct it *);
681 static enum prop_handled handle_invisible_prop (struct it *);
682 static enum prop_handled handle_display_prop (struct it *);
683 static enum prop_handled handle_composition_prop (struct it *);
684 static enum prop_handled handle_overlay_change (struct it *);
685 static enum prop_handled handle_fontified_prop (struct it *);
687 /* Properties handled by iterators. */
689 static struct props it_props[] =
691 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
692 /* Handle `face' before `display' because some sub-properties of
693 `display' need to know the face. */
694 {&Qface, FACE_PROP_IDX, handle_face_prop},
695 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
696 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
697 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
698 {NULL, 0, NULL}
701 /* Value is the position described by X. If X is a marker, value is
702 the marker_position of X. Otherwise, value is X. */
704 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
706 /* Enumeration returned by some move_it_.* functions internally. */
708 enum move_it_result
710 /* Not used. Undefined value. */
711 MOVE_UNDEFINED,
713 /* Move ended at the requested buffer position or ZV. */
714 MOVE_POS_MATCH_OR_ZV,
716 /* Move ended at the requested X pixel position. */
717 MOVE_X_REACHED,
719 /* Move within a line ended at the end of a line that must be
720 continued. */
721 MOVE_LINE_CONTINUED,
723 /* Move within a line ended at the end of a line that would
724 be displayed truncated. */
725 MOVE_LINE_TRUNCATED,
727 /* Move within a line ended at a line end. */
728 MOVE_NEWLINE_OR_CR
731 /* This counter is used to clear the face cache every once in a while
732 in redisplay_internal. It is incremented for each redisplay.
733 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
734 cleared. */
736 #define CLEAR_FACE_CACHE_COUNT 500
737 static int clear_face_cache_count;
739 /* Similarly for the image cache. */
741 #ifdef HAVE_WINDOW_SYSTEM
742 #define CLEAR_IMAGE_CACHE_COUNT 101
743 static int clear_image_cache_count;
745 /* Null glyph slice */
746 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
747 #endif
749 /* Non-zero while redisplay_internal is in progress. */
751 int redisplaying_p;
753 static Lisp_Object Qinhibit_free_realized_faces;
755 /* If a string, XTread_socket generates an event to display that string.
756 (The display is done in read_char.) */
758 Lisp_Object help_echo_string;
759 Lisp_Object help_echo_window;
760 Lisp_Object help_echo_object;
761 EMACS_INT help_echo_pos;
763 /* Temporary variable for XTread_socket. */
765 Lisp_Object previous_help_echo_string;
767 /* Platform-independent portion of hourglass implementation. */
769 /* Non-zero means an hourglass cursor is currently shown. */
770 int hourglass_shown_p;
772 /* If non-null, an asynchronous timer that, when it expires, displays
773 an hourglass cursor on all frames. */
774 struct atimer *hourglass_atimer;
776 /* Name of the face used to display glyphless characters. */
777 Lisp_Object Qglyphless_char;
779 /* Symbol for the purpose of Vglyphless_char_display. */
780 static Lisp_Object Qglyphless_char_display;
782 /* Method symbols for Vglyphless_char_display. */
783 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
785 /* Default pixel width of `thin-space' display method. */
786 #define THIN_SPACE_WIDTH 1
788 /* Default number of seconds to wait before displaying an hourglass
789 cursor. */
790 #define DEFAULT_HOURGLASS_DELAY 1
793 /* Function prototypes. */
795 static void setup_for_ellipsis (struct it *, int);
796 static void set_iterator_to_next (struct it *, int);
797 static void mark_window_display_accurate_1 (struct window *, int);
798 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
799 static int display_prop_string_p (Lisp_Object, Lisp_Object);
800 static int cursor_row_p (struct glyph_row *);
801 static int redisplay_mode_lines (Lisp_Object, int);
802 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
804 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
806 static void handle_line_prefix (struct it *);
808 static void pint2str (char *, int, EMACS_INT);
809 static void pint2hrstr (char *, int, EMACS_INT);
810 static struct text_pos run_window_scroll_functions (Lisp_Object,
811 struct text_pos);
812 static void reconsider_clip_changes (struct window *, struct buffer *);
813 static int text_outside_line_unchanged_p (struct window *,
814 EMACS_INT, EMACS_INT);
815 static void store_mode_line_noprop_char (char);
816 static int store_mode_line_noprop (const char *, int, int);
817 static void handle_stop (struct it *);
818 static void handle_stop_backwards (struct it *, EMACS_INT);
819 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
820 static void ensure_echo_area_buffers (void);
821 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
822 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
823 static int with_echo_area_buffer (struct window *, int,
824 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
825 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
826 static void clear_garbaged_frames (void);
827 static int current_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
828 static void pop_message (void);
829 static int truncate_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
830 static void set_message (const char *, Lisp_Object, EMACS_INT, int);
831 static int set_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
832 static int display_echo_area (struct window *);
833 static int display_echo_area_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
834 static int resize_mini_window_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
835 static Lisp_Object unwind_redisplay (Lisp_Object);
836 static int string_char_and_length (const unsigned char *, int *);
837 static struct text_pos display_prop_end (struct it *, Lisp_Object,
838 struct text_pos);
839 static int compute_window_start_on_continuation_line (struct window *);
840 static Lisp_Object safe_eval_handler (Lisp_Object);
841 static void insert_left_trunc_glyphs (struct it *);
842 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
843 Lisp_Object);
844 static void extend_face_to_end_of_line (struct it *);
845 static int append_space_for_newline (struct it *, int);
846 static int cursor_row_fully_visible_p (struct window *, int, int);
847 static int try_scrolling (Lisp_Object, int, EMACS_INT, EMACS_INT, int, int);
848 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
849 static int trailing_whitespace_p (EMACS_INT);
850 static intmax_t message_log_check_duplicate (EMACS_INT, EMACS_INT);
851 static void push_it (struct it *, struct text_pos *);
852 static void pop_it (struct it *);
853 static void sync_frame_with_window_matrix_rows (struct window *);
854 static void select_frame_for_redisplay (Lisp_Object);
855 static void redisplay_internal (void);
856 static int echo_area_display (int);
857 static void redisplay_windows (Lisp_Object);
858 static void redisplay_window (Lisp_Object, int);
859 static Lisp_Object redisplay_window_error (Lisp_Object);
860 static Lisp_Object redisplay_window_0 (Lisp_Object);
861 static Lisp_Object redisplay_window_1 (Lisp_Object);
862 static int set_cursor_from_row (struct window *, struct glyph_row *,
863 struct glyph_matrix *, EMACS_INT, EMACS_INT,
864 int, int);
865 static int update_menu_bar (struct frame *, int, int);
866 static int try_window_reusing_current_matrix (struct window *);
867 static int try_window_id (struct window *);
868 static int display_line (struct it *);
869 static int display_mode_lines (struct window *);
870 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
871 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
872 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
873 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
874 static void display_menu_bar (struct window *);
875 static EMACS_INT display_count_lines (EMACS_INT, EMACS_INT, EMACS_INT,
876 EMACS_INT *);
877 static int display_string (const char *, Lisp_Object, Lisp_Object,
878 EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
879 static void compute_line_metrics (struct it *);
880 static void run_redisplay_end_trigger_hook (struct it *);
881 static int get_overlay_strings (struct it *, EMACS_INT);
882 static int get_overlay_strings_1 (struct it *, EMACS_INT, int);
883 static void next_overlay_string (struct it *);
884 static void reseat (struct it *, struct text_pos, int);
885 static void reseat_1 (struct it *, struct text_pos, int);
886 static void back_to_previous_visible_line_start (struct it *);
887 void reseat_at_previous_visible_line_start (struct it *);
888 static void reseat_at_next_visible_line_start (struct it *, int);
889 static int next_element_from_ellipsis (struct it *);
890 static int next_element_from_display_vector (struct it *);
891 static int next_element_from_string (struct it *);
892 static int next_element_from_c_string (struct it *);
893 static int next_element_from_buffer (struct it *);
894 static int next_element_from_composition (struct it *);
895 static int next_element_from_image (struct it *);
896 static int next_element_from_stretch (struct it *);
897 static void load_overlay_strings (struct it *, EMACS_INT);
898 static int init_from_display_pos (struct it *, struct window *,
899 struct display_pos *);
900 static void reseat_to_string (struct it *, const char *,
901 Lisp_Object, EMACS_INT, EMACS_INT, int, int);
902 static int get_next_display_element (struct it *);
903 static enum move_it_result
904 move_it_in_display_line_to (struct it *, EMACS_INT, int,
905 enum move_operation_enum);
906 void move_it_vertically_backward (struct it *, int);
907 static void init_to_row_start (struct it *, struct window *,
908 struct glyph_row *);
909 static int init_to_row_end (struct it *, struct window *,
910 struct glyph_row *);
911 static void back_to_previous_line_start (struct it *);
912 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
913 static struct text_pos string_pos_nchars_ahead (struct text_pos,
914 Lisp_Object, EMACS_INT);
915 static struct text_pos string_pos (EMACS_INT, Lisp_Object);
916 static struct text_pos c_string_pos (EMACS_INT, const char *, int);
917 static EMACS_INT number_of_chars (const char *, int);
918 static void compute_stop_pos (struct it *);
919 static void compute_string_pos (struct text_pos *, struct text_pos,
920 Lisp_Object);
921 static int face_before_or_after_it_pos (struct it *, int);
922 static EMACS_INT next_overlay_change (EMACS_INT);
923 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
924 Lisp_Object, struct text_pos *, EMACS_INT, int);
925 static int handle_single_display_spec (struct it *, Lisp_Object,
926 Lisp_Object, Lisp_Object,
927 struct text_pos *, EMACS_INT, int, int);
928 static int underlying_face_id (struct it *);
929 static int in_ellipses_for_invisible_text_p (struct display_pos *,
930 struct window *);
932 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
933 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
935 #ifdef HAVE_WINDOW_SYSTEM
937 static void x_consider_frame_title (Lisp_Object);
938 static int tool_bar_lines_needed (struct frame *, int *);
939 static void update_tool_bar (struct frame *, int);
940 static void build_desired_tool_bar_string (struct frame *f);
941 static int redisplay_tool_bar (struct frame *);
942 static void display_tool_bar_line (struct it *, int);
943 static void notice_overwritten_cursor (struct window *,
944 enum glyph_row_area,
945 int, int, int, int);
946 static void append_stretch_glyph (struct it *, Lisp_Object,
947 int, int, int);
950 #endif /* HAVE_WINDOW_SYSTEM */
952 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
953 static int coords_in_mouse_face_p (struct window *, int, int);
957 /***********************************************************************
958 Window display dimensions
959 ***********************************************************************/
961 /* Return the bottom boundary y-position for text lines in window W.
962 This is the first y position at which a line cannot start.
963 It is relative to the top of the window.
965 This is the height of W minus the height of a mode line, if any. */
968 window_text_bottom_y (struct window *w)
970 int height = WINDOW_TOTAL_HEIGHT (w);
972 if (WINDOW_WANTS_MODELINE_P (w))
973 height -= CURRENT_MODE_LINE_HEIGHT (w);
974 return height;
977 /* Return the pixel width of display area AREA of window W. AREA < 0
978 means return the total width of W, not including fringes to
979 the left and right of the window. */
982 window_box_width (struct window *w, int area)
984 int cols = XFASTINT (w->total_cols);
985 int pixels = 0;
987 if (!w->pseudo_window_p)
989 cols -= WINDOW_SCROLL_BAR_COLS (w);
991 if (area == TEXT_AREA)
993 if (INTEGERP (w->left_margin_cols))
994 cols -= XFASTINT (w->left_margin_cols);
995 if (INTEGERP (w->right_margin_cols))
996 cols -= XFASTINT (w->right_margin_cols);
997 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
999 else if (area == LEFT_MARGIN_AREA)
1001 cols = (INTEGERP (w->left_margin_cols)
1002 ? XFASTINT (w->left_margin_cols) : 0);
1003 pixels = 0;
1005 else if (area == RIGHT_MARGIN_AREA)
1007 cols = (INTEGERP (w->right_margin_cols)
1008 ? XFASTINT (w->right_margin_cols) : 0);
1009 pixels = 0;
1013 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1017 /* Return the pixel height of the display area of window W, not
1018 including mode lines of W, if any. */
1021 window_box_height (struct window *w)
1023 struct frame *f = XFRAME (w->frame);
1024 int height = WINDOW_TOTAL_HEIGHT (w);
1026 xassert (height >= 0);
1028 /* Note: the code below that determines the mode-line/header-line
1029 height is essentially the same as that contained in the macro
1030 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1031 the appropriate glyph row has its `mode_line_p' flag set,
1032 and if it doesn't, uses estimate_mode_line_height instead. */
1034 if (WINDOW_WANTS_MODELINE_P (w))
1036 struct glyph_row *ml_row
1037 = (w->current_matrix && w->current_matrix->rows
1038 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1039 : 0);
1040 if (ml_row && ml_row->mode_line_p)
1041 height -= ml_row->height;
1042 else
1043 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1046 if (WINDOW_WANTS_HEADER_LINE_P (w))
1048 struct glyph_row *hl_row
1049 = (w->current_matrix && w->current_matrix->rows
1050 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1051 : 0);
1052 if (hl_row && hl_row->mode_line_p)
1053 height -= hl_row->height;
1054 else
1055 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1058 /* With a very small font and a mode-line that's taller than
1059 default, we might end up with a negative height. */
1060 return max (0, height);
1063 /* Return the window-relative coordinate of the left edge of display
1064 area AREA of window W. AREA < 0 means return the left edge of the
1065 whole window, to the right of the left fringe of W. */
1068 window_box_left_offset (struct window *w, int area)
1070 int x;
1072 if (w->pseudo_window_p)
1073 return 0;
1075 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1077 if (area == TEXT_AREA)
1078 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1079 + window_box_width (w, LEFT_MARGIN_AREA));
1080 else if (area == RIGHT_MARGIN_AREA)
1081 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1082 + window_box_width (w, LEFT_MARGIN_AREA)
1083 + window_box_width (w, TEXT_AREA)
1084 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1086 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1087 else if (area == LEFT_MARGIN_AREA
1088 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1089 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1091 return x;
1095 /* Return the window-relative coordinate of the right edge of display
1096 area AREA of window W. AREA < 0 means return the right edge of the
1097 whole window, to the left of the right fringe of W. */
1100 window_box_right_offset (struct window *w, int area)
1102 return window_box_left_offset (w, area) + window_box_width (w, area);
1105 /* Return the frame-relative coordinate of the left edge of display
1106 area AREA of window W. AREA < 0 means return the left edge of the
1107 whole window, to the right of the left fringe of W. */
1110 window_box_left (struct window *w, int area)
1112 struct frame *f = XFRAME (w->frame);
1113 int x;
1115 if (w->pseudo_window_p)
1116 return FRAME_INTERNAL_BORDER_WIDTH (f);
1118 x = (WINDOW_LEFT_EDGE_X (w)
1119 + window_box_left_offset (w, area));
1121 return x;
1125 /* Return the frame-relative coordinate of the right edge of display
1126 area AREA of window W. AREA < 0 means return the right edge of the
1127 whole window, to the left of the right fringe of W. */
1130 window_box_right (struct window *w, int area)
1132 return window_box_left (w, area) + window_box_width (w, area);
1135 /* Get the bounding box of the display area AREA of window W, without
1136 mode lines, in frame-relative coordinates. AREA < 0 means the
1137 whole window, not including the left and right fringes of
1138 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1139 coordinates of the upper-left corner of the box. Return in
1140 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1142 void
1143 window_box (struct window *w, int area, int *box_x, int *box_y,
1144 int *box_width, int *box_height)
1146 if (box_width)
1147 *box_width = window_box_width (w, area);
1148 if (box_height)
1149 *box_height = window_box_height (w);
1150 if (box_x)
1151 *box_x = window_box_left (w, area);
1152 if (box_y)
1154 *box_y = WINDOW_TOP_EDGE_Y (w);
1155 if (WINDOW_WANTS_HEADER_LINE_P (w))
1156 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1161 /* Get the bounding box of the display area AREA of window W, without
1162 mode lines. AREA < 0 means the whole window, not including the
1163 left and right fringe of the window. Return in *TOP_LEFT_X
1164 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1165 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1166 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1167 box. */
1169 static inline void
1170 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1171 int *bottom_right_x, int *bottom_right_y)
1173 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1174 bottom_right_y);
1175 *bottom_right_x += *top_left_x;
1176 *bottom_right_y += *top_left_y;
1181 /***********************************************************************
1182 Utilities
1183 ***********************************************************************/
1185 /* Return the bottom y-position of the line the iterator IT is in.
1186 This can modify IT's settings. */
1189 line_bottom_y (struct it *it)
1191 int line_height = it->max_ascent + it->max_descent;
1192 int line_top_y = it->current_y;
1194 if (line_height == 0)
1196 if (last_height)
1197 line_height = last_height;
1198 else if (IT_CHARPOS (*it) < ZV)
1200 move_it_by_lines (it, 1);
1201 line_height = (it->max_ascent || it->max_descent
1202 ? it->max_ascent + it->max_descent
1203 : last_height);
1205 else
1207 struct glyph_row *row = it->glyph_row;
1209 /* Use the default character height. */
1210 it->glyph_row = NULL;
1211 it->what = IT_CHARACTER;
1212 it->c = ' ';
1213 it->len = 1;
1214 PRODUCE_GLYPHS (it);
1215 line_height = it->ascent + it->descent;
1216 it->glyph_row = row;
1220 return line_top_y + line_height;
1223 /* Subroutine of pos_visible_p below. Extracts a display string, if
1224 any, from the display spec given as its argument. */
1225 static Lisp_Object
1226 string_from_display_spec (Lisp_Object spec)
1228 if (CONSP (spec))
1230 while (CONSP (spec))
1232 if (STRINGP (XCAR (spec)))
1233 return XCAR (spec);
1234 spec = XCDR (spec);
1237 else if (VECTORP (spec))
1239 ptrdiff_t i;
1241 for (i = 0; i < ASIZE (spec); i++)
1243 if (STRINGP (AREF (spec, i)))
1244 return AREF (spec, i);
1246 return Qnil;
1249 return spec;
1252 /* Return 1 if position CHARPOS is visible in window W.
1253 CHARPOS < 0 means return info about WINDOW_END position.
1254 If visible, set *X and *Y to pixel coordinates of top left corner.
1255 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1256 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1259 pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1260 int *rtop, int *rbot, int *rowh, int *vpos)
1262 struct it it;
1263 void *itdata = bidi_shelve_cache ();
1264 struct text_pos top;
1265 int visible_p = 0;
1266 struct buffer *old_buffer = NULL;
1268 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1269 return visible_p;
1271 if (XBUFFER (w->buffer) != current_buffer)
1273 old_buffer = current_buffer;
1274 set_buffer_internal_1 (XBUFFER (w->buffer));
1277 SET_TEXT_POS_FROM_MARKER (top, w->start);
1278 /* Scrolling a minibuffer window via scroll bar when the echo area
1279 shows long text sometimes resets the minibuffer contents behind
1280 our backs. */
1281 if (CHARPOS (top) > ZV)
1282 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1284 /* Compute exact mode line heights. */
1285 if (WINDOW_WANTS_MODELINE_P (w))
1286 current_mode_line_height
1287 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1288 BVAR (current_buffer, mode_line_format));
1290 if (WINDOW_WANTS_HEADER_LINE_P (w))
1291 current_header_line_height
1292 = display_mode_line (w, HEADER_LINE_FACE_ID,
1293 BVAR (current_buffer, header_line_format));
1295 start_display (&it, w, top);
1296 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1297 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1299 if (charpos >= 0
1300 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1301 && IT_CHARPOS (it) >= charpos)
1302 /* When scanning backwards under bidi iteration, move_it_to
1303 stops at or _before_ CHARPOS, because it stops at or to
1304 the _right_ of the character at CHARPOS. */
1305 || (it.bidi_p && it.bidi_it.scan_dir == -1
1306 && IT_CHARPOS (it) <= charpos)))
1308 /* We have reached CHARPOS, or passed it. How the call to
1309 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1310 or covered by a display property, move_it_to stops at the end
1311 of the invisible text, to the right of CHARPOS. (ii) If
1312 CHARPOS is in a display vector, move_it_to stops on its last
1313 glyph. */
1314 int top_x = it.current_x;
1315 int top_y = it.current_y;
1316 enum it_method it_method = it.method;
1317 /* Calling line_bottom_y may change it.method, it.position, etc. */
1318 int bottom_y = (last_height = 0, line_bottom_y (&it));
1319 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1321 if (top_y < window_top_y)
1322 visible_p = bottom_y > window_top_y;
1323 else if (top_y < it.last_visible_y)
1324 visible_p = 1;
1325 if (visible_p)
1327 if (it_method == GET_FROM_DISPLAY_VECTOR)
1329 /* We stopped on the last glyph of a display vector.
1330 Try and recompute. Hack alert! */
1331 if (charpos < 2 || top.charpos >= charpos)
1332 top_x = it.glyph_row->x;
1333 else
1335 struct it it2;
1336 start_display (&it2, w, top);
1337 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1338 get_next_display_element (&it2);
1339 PRODUCE_GLYPHS (&it2);
1340 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1341 || it2.current_x > it2.last_visible_x)
1342 top_x = it.glyph_row->x;
1343 else
1345 top_x = it2.current_x;
1346 top_y = it2.current_y;
1350 else if (IT_CHARPOS (it) != charpos)
1352 Lisp_Object cpos = make_number (charpos);
1353 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1354 Lisp_Object string = string_from_display_spec (spec);
1355 int newline_in_string = 0;
1357 if (STRINGP (string))
1359 const char *s = SSDATA (string);
1360 const char *e = s + SBYTES (string);
1361 while (s < e)
1363 if (*s++ == '\n')
1365 newline_in_string = 1;
1366 break;
1370 /* The tricky code below is needed because there's a
1371 discrepancy between move_it_to and how we set cursor
1372 when the display line ends in a newline from a
1373 display string. move_it_to will stop _after_ such
1374 display strings, whereas set_cursor_from_row
1375 conspires with cursor_row_p to place the cursor on
1376 the first glyph produced from the display string. */
1378 /* We have overshoot PT because it is covered by a
1379 display property whose value is a string. If the
1380 string includes embedded newlines, we are also in the
1381 wrong display line. Backtrack to the correct line,
1382 where the display string begins. */
1383 if (newline_in_string)
1385 Lisp_Object startpos, endpos;
1386 EMACS_INT start, end;
1387 struct it it3;
1388 int it3_moved;
1390 /* Find the first and the last buffer positions
1391 covered by the display string. */
1392 endpos =
1393 Fnext_single_char_property_change (cpos, Qdisplay,
1394 Qnil, Qnil);
1395 startpos =
1396 Fprevious_single_char_property_change (endpos, Qdisplay,
1397 Qnil, Qnil);
1398 start = XFASTINT (startpos);
1399 end = XFASTINT (endpos);
1400 /* Move to the last buffer position before the
1401 display property. */
1402 start_display (&it3, w, top);
1403 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1404 /* Move forward one more line if the position before
1405 the display string is a newline or if it is the
1406 rightmost character on a line that is
1407 continued or word-wrapped. */
1408 if (it3.method == GET_FROM_BUFFER
1409 && it3.c == '\n')
1410 move_it_by_lines (&it3, 1);
1411 else if (move_it_in_display_line_to (&it3, -1,
1412 it3.current_x
1413 + it3.pixel_width,
1414 MOVE_TO_X)
1415 == MOVE_LINE_CONTINUED)
1417 move_it_by_lines (&it3, 1);
1418 /* When we are under word-wrap, the #$@%!
1419 move_it_by_lines moves 2 lines, so we need to
1420 fix that up. */
1421 if (it3.line_wrap == WORD_WRAP)
1422 move_it_by_lines (&it3, -1);
1425 /* Record the vertical coordinate of the display
1426 line where we wound up. */
1427 top_y = it3.current_y;
1428 if (it3.bidi_p)
1430 /* When characters are reordered for display,
1431 the character displayed to the left of the
1432 display string could be _after_ the display
1433 property in the logical order. Use the
1434 smallest vertical position of these two. */
1435 start_display (&it3, w, top);
1436 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1437 if (it3.current_y < top_y)
1438 top_y = it3.current_y;
1440 /* Move from the top of the window to the beginning
1441 of the display line where the display string
1442 begins. */
1443 start_display (&it3, w, top);
1444 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1445 /* If it3_moved stays zero after the 'while' loop
1446 below, that means we already were at a newline
1447 before the loop (e.g., the display string begins
1448 with a newline), so we don't need to (and cannot)
1449 inspect the glyphs of it3.glyph_row, because
1450 PRODUCE_GLYPHS will not produce anything for a
1451 newline, and thus it3.glyph_row stays at its
1452 stale content it got at top of the window. */
1453 it3_moved = 0;
1454 /* Finally, advance the iterator until we hit the
1455 first display element whose character position is
1456 CHARPOS, or until the first newline from the
1457 display string, which signals the end of the
1458 display line. */
1459 while (get_next_display_element (&it3))
1461 PRODUCE_GLYPHS (&it3);
1462 if (IT_CHARPOS (it3) == charpos
1463 || ITERATOR_AT_END_OF_LINE_P (&it3))
1464 break;
1465 it3_moved = 1;
1466 set_iterator_to_next (&it3, 0);
1468 top_x = it3.current_x - it3.pixel_width;
1469 /* Normally, we would exit the above loop because we
1470 found the display element whose character
1471 position is CHARPOS. For the contingency that we
1472 didn't, and stopped at the first newline from the
1473 display string, move back over the glyphs
1474 produced from the string, until we find the
1475 rightmost glyph not from the string. */
1476 if (it3_moved
1477 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1479 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1480 + it3.glyph_row->used[TEXT_AREA];
1482 while (EQ ((g - 1)->object, string))
1484 --g;
1485 top_x -= g->pixel_width;
1487 xassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1488 + it3.glyph_row->used[TEXT_AREA]);
1493 *x = top_x;
1494 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1495 *rtop = max (0, window_top_y - top_y);
1496 *rbot = max (0, bottom_y - it.last_visible_y);
1497 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1498 - max (top_y, window_top_y)));
1499 *vpos = it.vpos;
1502 else
1504 /* We were asked to provide info about WINDOW_END. */
1505 struct it it2;
1506 void *it2data = NULL;
1508 SAVE_IT (it2, it, it2data);
1509 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1510 move_it_by_lines (&it, 1);
1511 if (charpos < IT_CHARPOS (it)
1512 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1514 visible_p = 1;
1515 RESTORE_IT (&it2, &it2, it2data);
1516 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1517 *x = it2.current_x;
1518 *y = it2.current_y + it2.max_ascent - it2.ascent;
1519 *rtop = max (0, -it2.current_y);
1520 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1521 - it.last_visible_y));
1522 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1523 it.last_visible_y)
1524 - max (it2.current_y,
1525 WINDOW_HEADER_LINE_HEIGHT (w))));
1526 *vpos = it2.vpos;
1528 else
1529 bidi_unshelve_cache (it2data, 1);
1531 bidi_unshelve_cache (itdata, 0);
1533 if (old_buffer)
1534 set_buffer_internal_1 (old_buffer);
1536 current_header_line_height = current_mode_line_height = -1;
1538 if (visible_p && XFASTINT (w->hscroll) > 0)
1539 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1541 #if 0
1542 /* Debugging code. */
1543 if (visible_p)
1544 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1545 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1546 else
1547 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1548 #endif
1550 return visible_p;
1554 /* Return the next character from STR. Return in *LEN the length of
1555 the character. This is like STRING_CHAR_AND_LENGTH but never
1556 returns an invalid character. If we find one, we return a `?', but
1557 with the length of the invalid character. */
1559 static inline int
1560 string_char_and_length (const unsigned char *str, int *len)
1562 int c;
1564 c = STRING_CHAR_AND_LENGTH (str, *len);
1565 if (!CHAR_VALID_P (c))
1566 /* We may not change the length here because other places in Emacs
1567 don't use this function, i.e. they silently accept invalid
1568 characters. */
1569 c = '?';
1571 return c;
1576 /* Given a position POS containing a valid character and byte position
1577 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1579 static struct text_pos
1580 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1582 xassert (STRINGP (string) && nchars >= 0);
1584 if (STRING_MULTIBYTE (string))
1586 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1587 int len;
1589 while (nchars--)
1591 string_char_and_length (p, &len);
1592 p += len;
1593 CHARPOS (pos) += 1;
1594 BYTEPOS (pos) += len;
1597 else
1598 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1600 return pos;
1604 /* Value is the text position, i.e. character and byte position,
1605 for character position CHARPOS in STRING. */
1607 static inline struct text_pos
1608 string_pos (EMACS_INT charpos, Lisp_Object string)
1610 struct text_pos pos;
1611 xassert (STRINGP (string));
1612 xassert (charpos >= 0);
1613 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1614 return pos;
1618 /* Value is a text position, i.e. character and byte position, for
1619 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1620 means recognize multibyte characters. */
1622 static struct text_pos
1623 c_string_pos (EMACS_INT charpos, const char *s, int multibyte_p)
1625 struct text_pos pos;
1627 xassert (s != NULL);
1628 xassert (charpos >= 0);
1630 if (multibyte_p)
1632 int len;
1634 SET_TEXT_POS (pos, 0, 0);
1635 while (charpos--)
1637 string_char_and_length ((const unsigned char *) s, &len);
1638 s += len;
1639 CHARPOS (pos) += 1;
1640 BYTEPOS (pos) += len;
1643 else
1644 SET_TEXT_POS (pos, charpos, charpos);
1646 return pos;
1650 /* Value is the number of characters in C string S. MULTIBYTE_P
1651 non-zero means recognize multibyte characters. */
1653 static EMACS_INT
1654 number_of_chars (const char *s, int multibyte_p)
1656 EMACS_INT nchars;
1658 if (multibyte_p)
1660 EMACS_INT rest = strlen (s);
1661 int len;
1662 const unsigned char *p = (const unsigned char *) s;
1664 for (nchars = 0; rest > 0; ++nchars)
1666 string_char_and_length (p, &len);
1667 rest -= len, p += len;
1670 else
1671 nchars = strlen (s);
1673 return nchars;
1677 /* Compute byte position NEWPOS->bytepos corresponding to
1678 NEWPOS->charpos. POS is a known position in string STRING.
1679 NEWPOS->charpos must be >= POS.charpos. */
1681 static void
1682 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1684 xassert (STRINGP (string));
1685 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1687 if (STRING_MULTIBYTE (string))
1688 *newpos = string_pos_nchars_ahead (pos, string,
1689 CHARPOS (*newpos) - CHARPOS (pos));
1690 else
1691 BYTEPOS (*newpos) = CHARPOS (*newpos);
1694 /* EXPORT:
1695 Return an estimation of the pixel height of mode or header lines on
1696 frame F. FACE_ID specifies what line's height to estimate. */
1699 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1701 #ifdef HAVE_WINDOW_SYSTEM
1702 if (FRAME_WINDOW_P (f))
1704 int height = FONT_HEIGHT (FRAME_FONT (f));
1706 /* This function is called so early when Emacs starts that the face
1707 cache and mode line face are not yet initialized. */
1708 if (FRAME_FACE_CACHE (f))
1710 struct face *face = FACE_FROM_ID (f, face_id);
1711 if (face)
1713 if (face->font)
1714 height = FONT_HEIGHT (face->font);
1715 if (face->box_line_width > 0)
1716 height += 2 * face->box_line_width;
1720 return height;
1722 #endif
1724 return 1;
1727 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1728 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1729 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1730 not force the value into range. */
1732 void
1733 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1734 int *x, int *y, NativeRectangle *bounds, int noclip)
1737 #ifdef HAVE_WINDOW_SYSTEM
1738 if (FRAME_WINDOW_P (f))
1740 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1741 even for negative values. */
1742 if (pix_x < 0)
1743 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1744 if (pix_y < 0)
1745 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1747 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1748 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1750 if (bounds)
1751 STORE_NATIVE_RECT (*bounds,
1752 FRAME_COL_TO_PIXEL_X (f, pix_x),
1753 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1754 FRAME_COLUMN_WIDTH (f) - 1,
1755 FRAME_LINE_HEIGHT (f) - 1);
1757 if (!noclip)
1759 if (pix_x < 0)
1760 pix_x = 0;
1761 else if (pix_x > FRAME_TOTAL_COLS (f))
1762 pix_x = FRAME_TOTAL_COLS (f);
1764 if (pix_y < 0)
1765 pix_y = 0;
1766 else if (pix_y > FRAME_LINES (f))
1767 pix_y = FRAME_LINES (f);
1770 #endif
1772 *x = pix_x;
1773 *y = pix_y;
1777 /* Find the glyph under window-relative coordinates X/Y in window W.
1778 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1779 strings. Return in *HPOS and *VPOS the row and column number of
1780 the glyph found. Return in *AREA the glyph area containing X.
1781 Value is a pointer to the glyph found or null if X/Y is not on
1782 text, or we can't tell because W's current matrix is not up to
1783 date. */
1785 static
1786 struct glyph *
1787 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1788 int *dx, int *dy, int *area)
1790 struct glyph *glyph, *end;
1791 struct glyph_row *row = NULL;
1792 int x0, i;
1794 /* Find row containing Y. Give up if some row is not enabled. */
1795 for (i = 0; i < w->current_matrix->nrows; ++i)
1797 row = MATRIX_ROW (w->current_matrix, i);
1798 if (!row->enabled_p)
1799 return NULL;
1800 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1801 break;
1804 *vpos = i;
1805 *hpos = 0;
1807 /* Give up if Y is not in the window. */
1808 if (i == w->current_matrix->nrows)
1809 return NULL;
1811 /* Get the glyph area containing X. */
1812 if (w->pseudo_window_p)
1814 *area = TEXT_AREA;
1815 x0 = 0;
1817 else
1819 if (x < window_box_left_offset (w, TEXT_AREA))
1821 *area = LEFT_MARGIN_AREA;
1822 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1824 else if (x < window_box_right_offset (w, TEXT_AREA))
1826 *area = TEXT_AREA;
1827 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1829 else
1831 *area = RIGHT_MARGIN_AREA;
1832 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1836 /* Find glyph containing X. */
1837 glyph = row->glyphs[*area];
1838 end = glyph + row->used[*area];
1839 x -= x0;
1840 while (glyph < end && x >= glyph->pixel_width)
1842 x -= glyph->pixel_width;
1843 ++glyph;
1846 if (glyph == end)
1847 return NULL;
1849 if (dx)
1851 *dx = x;
1852 *dy = y - (row->y + row->ascent - glyph->ascent);
1855 *hpos = glyph - row->glyphs[*area];
1856 return glyph;
1859 /* Convert frame-relative x/y to coordinates relative to window W.
1860 Takes pseudo-windows into account. */
1862 static void
1863 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1865 if (w->pseudo_window_p)
1867 /* A pseudo-window is always full-width, and starts at the
1868 left edge of the frame, plus a frame border. */
1869 struct frame *f = XFRAME (w->frame);
1870 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1871 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1873 else
1875 *x -= WINDOW_LEFT_EDGE_X (w);
1876 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1880 #ifdef HAVE_WINDOW_SYSTEM
1882 /* EXPORT:
1883 Return in RECTS[] at most N clipping rectangles for glyph string S.
1884 Return the number of stored rectangles. */
1887 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1889 XRectangle r;
1891 if (n <= 0)
1892 return 0;
1894 if (s->row->full_width_p)
1896 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1897 r.x = WINDOW_LEFT_EDGE_X (s->w);
1898 r.width = WINDOW_TOTAL_WIDTH (s->w);
1900 /* Unless displaying a mode or menu bar line, which are always
1901 fully visible, clip to the visible part of the row. */
1902 if (s->w->pseudo_window_p)
1903 r.height = s->row->visible_height;
1904 else
1905 r.height = s->height;
1907 else
1909 /* This is a text line that may be partially visible. */
1910 r.x = window_box_left (s->w, s->area);
1911 r.width = window_box_width (s->w, s->area);
1912 r.height = s->row->visible_height;
1915 if (s->clip_head)
1916 if (r.x < s->clip_head->x)
1918 if (r.width >= s->clip_head->x - r.x)
1919 r.width -= s->clip_head->x - r.x;
1920 else
1921 r.width = 0;
1922 r.x = s->clip_head->x;
1924 if (s->clip_tail)
1925 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1927 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1928 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1929 else
1930 r.width = 0;
1933 /* If S draws overlapping rows, it's sufficient to use the top and
1934 bottom of the window for clipping because this glyph string
1935 intentionally draws over other lines. */
1936 if (s->for_overlaps)
1938 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1939 r.height = window_text_bottom_y (s->w) - r.y;
1941 /* Alas, the above simple strategy does not work for the
1942 environments with anti-aliased text: if the same text is
1943 drawn onto the same place multiple times, it gets thicker.
1944 If the overlap we are processing is for the erased cursor, we
1945 take the intersection with the rectangle of the cursor. */
1946 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1948 XRectangle rc, r_save = r;
1950 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1951 rc.y = s->w->phys_cursor.y;
1952 rc.width = s->w->phys_cursor_width;
1953 rc.height = s->w->phys_cursor_height;
1955 x_intersect_rectangles (&r_save, &rc, &r);
1958 else
1960 /* Don't use S->y for clipping because it doesn't take partially
1961 visible lines into account. For example, it can be negative for
1962 partially visible lines at the top of a window. */
1963 if (!s->row->full_width_p
1964 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1965 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1966 else
1967 r.y = max (0, s->row->y);
1970 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1972 /* If drawing the cursor, don't let glyph draw outside its
1973 advertised boundaries. Cleartype does this under some circumstances. */
1974 if (s->hl == DRAW_CURSOR)
1976 struct glyph *glyph = s->first_glyph;
1977 int height, max_y;
1979 if (s->x > r.x)
1981 r.width -= s->x - r.x;
1982 r.x = s->x;
1984 r.width = min (r.width, glyph->pixel_width);
1986 /* If r.y is below window bottom, ensure that we still see a cursor. */
1987 height = min (glyph->ascent + glyph->descent,
1988 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1989 max_y = window_text_bottom_y (s->w) - height;
1990 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1991 if (s->ybase - glyph->ascent > max_y)
1993 r.y = max_y;
1994 r.height = height;
1996 else
1998 /* Don't draw cursor glyph taller than our actual glyph. */
1999 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2000 if (height < r.height)
2002 max_y = r.y + r.height;
2003 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2004 r.height = min (max_y - r.y, height);
2009 if (s->row->clip)
2011 XRectangle r_save = r;
2013 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2014 r.width = 0;
2017 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2018 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2020 #ifdef CONVERT_FROM_XRECT
2021 CONVERT_FROM_XRECT (r, *rects);
2022 #else
2023 *rects = r;
2024 #endif
2025 return 1;
2027 else
2029 /* If we are processing overlapping and allowed to return
2030 multiple clipping rectangles, we exclude the row of the glyph
2031 string from the clipping rectangle. This is to avoid drawing
2032 the same text on the environment with anti-aliasing. */
2033 #ifdef CONVERT_FROM_XRECT
2034 XRectangle rs[2];
2035 #else
2036 XRectangle *rs = rects;
2037 #endif
2038 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2040 if (s->for_overlaps & OVERLAPS_PRED)
2042 rs[i] = r;
2043 if (r.y + r.height > row_y)
2045 if (r.y < row_y)
2046 rs[i].height = row_y - r.y;
2047 else
2048 rs[i].height = 0;
2050 i++;
2052 if (s->for_overlaps & OVERLAPS_SUCC)
2054 rs[i] = r;
2055 if (r.y < row_y + s->row->visible_height)
2057 if (r.y + r.height > row_y + s->row->visible_height)
2059 rs[i].y = row_y + s->row->visible_height;
2060 rs[i].height = r.y + r.height - rs[i].y;
2062 else
2063 rs[i].height = 0;
2065 i++;
2068 n = i;
2069 #ifdef CONVERT_FROM_XRECT
2070 for (i = 0; i < n; i++)
2071 CONVERT_FROM_XRECT (rs[i], rects[i]);
2072 #endif
2073 return n;
2077 /* EXPORT:
2078 Return in *NR the clipping rectangle for glyph string S. */
2080 void
2081 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2083 get_glyph_string_clip_rects (s, nr, 1);
2087 /* EXPORT:
2088 Return the position and height of the phys cursor in window W.
2089 Set w->phys_cursor_width to width of phys cursor.
2092 void
2093 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2094 struct glyph *glyph, int *xp, int *yp, int *heightp)
2096 struct frame *f = XFRAME (WINDOW_FRAME (w));
2097 int x, y, wd, h, h0, y0;
2099 /* Compute the width of the rectangle to draw. If on a stretch
2100 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2101 rectangle as wide as the glyph, but use a canonical character
2102 width instead. */
2103 wd = glyph->pixel_width - 1;
2104 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2105 wd++; /* Why? */
2106 #endif
2108 x = w->phys_cursor.x;
2109 if (x < 0)
2111 wd += x;
2112 x = 0;
2115 if (glyph->type == STRETCH_GLYPH
2116 && !x_stretch_cursor_p)
2117 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2118 w->phys_cursor_width = wd;
2120 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2122 /* If y is below window bottom, ensure that we still see a cursor. */
2123 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2125 h = max (h0, glyph->ascent + glyph->descent);
2126 h0 = min (h0, glyph->ascent + glyph->descent);
2128 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2129 if (y < y0)
2131 h = max (h - (y0 - y) + 1, h0);
2132 y = y0 - 1;
2134 else
2136 y0 = window_text_bottom_y (w) - h0;
2137 if (y > y0)
2139 h += y - y0;
2140 y = y0;
2144 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2145 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2146 *heightp = h;
2150 * Remember which glyph the mouse is over.
2153 void
2154 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2156 Lisp_Object window;
2157 struct window *w;
2158 struct glyph_row *r, *gr, *end_row;
2159 enum window_part part;
2160 enum glyph_row_area area;
2161 int x, y, width, height;
2163 /* Try to determine frame pixel position and size of the glyph under
2164 frame pixel coordinates X/Y on frame F. */
2166 if (!f->glyphs_initialized_p
2167 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2168 NILP (window)))
2170 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2171 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2172 goto virtual_glyph;
2175 w = XWINDOW (window);
2176 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2177 height = WINDOW_FRAME_LINE_HEIGHT (w);
2179 x = window_relative_x_coord (w, part, gx);
2180 y = gy - WINDOW_TOP_EDGE_Y (w);
2182 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2183 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2185 if (w->pseudo_window_p)
2187 area = TEXT_AREA;
2188 part = ON_MODE_LINE; /* Don't adjust margin. */
2189 goto text_glyph;
2192 switch (part)
2194 case ON_LEFT_MARGIN:
2195 area = LEFT_MARGIN_AREA;
2196 goto text_glyph;
2198 case ON_RIGHT_MARGIN:
2199 area = RIGHT_MARGIN_AREA;
2200 goto text_glyph;
2202 case ON_HEADER_LINE:
2203 case ON_MODE_LINE:
2204 gr = (part == ON_HEADER_LINE
2205 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2206 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2207 gy = gr->y;
2208 area = TEXT_AREA;
2209 goto text_glyph_row_found;
2211 case ON_TEXT:
2212 area = TEXT_AREA;
2214 text_glyph:
2215 gr = 0; gy = 0;
2216 for (; r <= end_row && r->enabled_p; ++r)
2217 if (r->y + r->height > y)
2219 gr = r; gy = r->y;
2220 break;
2223 text_glyph_row_found:
2224 if (gr && gy <= y)
2226 struct glyph *g = gr->glyphs[area];
2227 struct glyph *end = g + gr->used[area];
2229 height = gr->height;
2230 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2231 if (gx + g->pixel_width > x)
2232 break;
2234 if (g < end)
2236 if (g->type == IMAGE_GLYPH)
2238 /* Don't remember when mouse is over image, as
2239 image may have hot-spots. */
2240 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2241 return;
2243 width = g->pixel_width;
2245 else
2247 /* Use nominal char spacing at end of line. */
2248 x -= gx;
2249 gx += (x / width) * width;
2252 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2253 gx += window_box_left_offset (w, area);
2255 else
2257 /* Use nominal line height at end of window. */
2258 gx = (x / width) * width;
2259 y -= gy;
2260 gy += (y / height) * height;
2262 break;
2264 case ON_LEFT_FRINGE:
2265 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2266 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2267 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2268 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2269 goto row_glyph;
2271 case ON_RIGHT_FRINGE:
2272 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2273 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2274 : window_box_right_offset (w, TEXT_AREA));
2275 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2276 goto row_glyph;
2278 case ON_SCROLL_BAR:
2279 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2281 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2282 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2283 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2284 : 0)));
2285 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2287 row_glyph:
2288 gr = 0, gy = 0;
2289 for (; r <= end_row && r->enabled_p; ++r)
2290 if (r->y + r->height > y)
2292 gr = r; gy = r->y;
2293 break;
2296 if (gr && gy <= y)
2297 height = gr->height;
2298 else
2300 /* Use nominal line height at end of window. */
2301 y -= gy;
2302 gy += (y / height) * height;
2304 break;
2306 default:
2308 virtual_glyph:
2309 /* If there is no glyph under the mouse, then we divide the screen
2310 into a grid of the smallest glyph in the frame, and use that
2311 as our "glyph". */
2313 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2314 round down even for negative values. */
2315 if (gx < 0)
2316 gx -= width - 1;
2317 if (gy < 0)
2318 gy -= height - 1;
2320 gx = (gx / width) * width;
2321 gy = (gy / height) * height;
2323 goto store_rect;
2326 gx += WINDOW_LEFT_EDGE_X (w);
2327 gy += WINDOW_TOP_EDGE_Y (w);
2329 store_rect:
2330 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2332 /* Visible feedback for debugging. */
2333 #if 0
2334 #if HAVE_X_WINDOWS
2335 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2336 f->output_data.x->normal_gc,
2337 gx, gy, width, height);
2338 #endif
2339 #endif
2343 #endif /* HAVE_WINDOW_SYSTEM */
2346 /***********************************************************************
2347 Lisp form evaluation
2348 ***********************************************************************/
2350 /* Error handler for safe_eval and safe_call. */
2352 static Lisp_Object
2353 safe_eval_handler (Lisp_Object arg)
2355 add_to_log ("Error during redisplay: %S", arg, Qnil);
2356 return Qnil;
2360 /* Evaluate SEXPR and return the result, or nil if something went
2361 wrong. Prevent redisplay during the evaluation. */
2363 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2364 Return the result, or nil if something went wrong. Prevent
2365 redisplay during the evaluation. */
2367 Lisp_Object
2368 safe_call (ptrdiff_t nargs, Lisp_Object *args)
2370 Lisp_Object val;
2372 if (inhibit_eval_during_redisplay)
2373 val = Qnil;
2374 else
2376 int count = SPECPDL_INDEX ();
2377 struct gcpro gcpro1;
2379 GCPRO1 (args[0]);
2380 gcpro1.nvars = nargs;
2381 specbind (Qinhibit_redisplay, Qt);
2382 /* Use Qt to ensure debugger does not run,
2383 so there is no possibility of wanting to redisplay. */
2384 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2385 safe_eval_handler);
2386 UNGCPRO;
2387 val = unbind_to (count, val);
2390 return val;
2394 /* Call function FN with one argument ARG.
2395 Return the result, or nil if something went wrong. */
2397 Lisp_Object
2398 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2400 Lisp_Object args[2];
2401 args[0] = fn;
2402 args[1] = arg;
2403 return safe_call (2, args);
2406 static Lisp_Object Qeval;
2408 Lisp_Object
2409 safe_eval (Lisp_Object sexpr)
2411 return safe_call1 (Qeval, sexpr);
2414 /* Call function FN with one argument ARG.
2415 Return the result, or nil if something went wrong. */
2417 Lisp_Object
2418 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2420 Lisp_Object args[3];
2421 args[0] = fn;
2422 args[1] = arg1;
2423 args[2] = arg2;
2424 return safe_call (3, args);
2429 /***********************************************************************
2430 Debugging
2431 ***********************************************************************/
2433 #if 0
2435 /* Define CHECK_IT to perform sanity checks on iterators.
2436 This is for debugging. It is too slow to do unconditionally. */
2438 static void
2439 check_it (struct it *it)
2441 if (it->method == GET_FROM_STRING)
2443 xassert (STRINGP (it->string));
2444 xassert (IT_STRING_CHARPOS (*it) >= 0);
2446 else
2448 xassert (IT_STRING_CHARPOS (*it) < 0);
2449 if (it->method == GET_FROM_BUFFER)
2451 /* Check that character and byte positions agree. */
2452 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2456 if (it->dpvec)
2457 xassert (it->current.dpvec_index >= 0);
2458 else
2459 xassert (it->current.dpvec_index < 0);
2462 #define CHECK_IT(IT) check_it ((IT))
2464 #else /* not 0 */
2466 #define CHECK_IT(IT) (void) 0
2468 #endif /* not 0 */
2471 #if GLYPH_DEBUG && XASSERTS
2473 /* Check that the window end of window W is what we expect it
2474 to be---the last row in the current matrix displaying text. */
2476 static void
2477 check_window_end (struct window *w)
2479 if (!MINI_WINDOW_P (w)
2480 && !NILP (w->window_end_valid))
2482 struct glyph_row *row;
2483 xassert ((row = MATRIX_ROW (w->current_matrix,
2484 XFASTINT (w->window_end_vpos)),
2485 !row->enabled_p
2486 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2487 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2491 #define CHECK_WINDOW_END(W) check_window_end ((W))
2493 #else
2495 #define CHECK_WINDOW_END(W) (void) 0
2497 #endif
2501 /***********************************************************************
2502 Iterator initialization
2503 ***********************************************************************/
2505 /* Initialize IT for displaying current_buffer in window W, starting
2506 at character position CHARPOS. CHARPOS < 0 means that no buffer
2507 position is specified which is useful when the iterator is assigned
2508 a position later. BYTEPOS is the byte position corresponding to
2509 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2511 If ROW is not null, calls to produce_glyphs with IT as parameter
2512 will produce glyphs in that row.
2514 BASE_FACE_ID is the id of a base face to use. It must be one of
2515 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2516 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2517 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2519 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2520 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2521 will be initialized to use the corresponding mode line glyph row of
2522 the desired matrix of W. */
2524 void
2525 init_iterator (struct it *it, struct window *w,
2526 EMACS_INT charpos, EMACS_INT bytepos,
2527 struct glyph_row *row, enum face_id base_face_id)
2529 int highlight_region_p;
2530 enum face_id remapped_base_face_id = base_face_id;
2532 /* Some precondition checks. */
2533 xassert (w != NULL && it != NULL);
2534 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2535 && charpos <= ZV));
2537 /* If face attributes have been changed since the last redisplay,
2538 free realized faces now because they depend on face definitions
2539 that might have changed. Don't free faces while there might be
2540 desired matrices pending which reference these faces. */
2541 if (face_change_count && !inhibit_free_realized_faces)
2543 face_change_count = 0;
2544 free_all_realized_faces (Qnil);
2547 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2548 if (! NILP (Vface_remapping_alist))
2549 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2551 /* Use one of the mode line rows of W's desired matrix if
2552 appropriate. */
2553 if (row == NULL)
2555 if (base_face_id == MODE_LINE_FACE_ID
2556 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2557 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2558 else if (base_face_id == HEADER_LINE_FACE_ID)
2559 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2562 /* Clear IT. */
2563 memset (it, 0, sizeof *it);
2564 it->current.overlay_string_index = -1;
2565 it->current.dpvec_index = -1;
2566 it->base_face_id = remapped_base_face_id;
2567 it->string = Qnil;
2568 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2569 it->paragraph_embedding = L2R;
2570 it->bidi_it.string.lstring = Qnil;
2571 it->bidi_it.string.s = NULL;
2572 it->bidi_it.string.bufpos = 0;
2574 /* The window in which we iterate over current_buffer: */
2575 XSETWINDOW (it->window, w);
2576 it->w = w;
2577 it->f = XFRAME (w->frame);
2579 it->cmp_it.id = -1;
2581 /* Extra space between lines (on window systems only). */
2582 if (base_face_id == DEFAULT_FACE_ID
2583 && FRAME_WINDOW_P (it->f))
2585 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2586 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2587 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2588 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2589 * FRAME_LINE_HEIGHT (it->f));
2590 else if (it->f->extra_line_spacing > 0)
2591 it->extra_line_spacing = it->f->extra_line_spacing;
2592 it->max_extra_line_spacing = 0;
2595 /* If realized faces have been removed, e.g. because of face
2596 attribute changes of named faces, recompute them. When running
2597 in batch mode, the face cache of the initial frame is null. If
2598 we happen to get called, make a dummy face cache. */
2599 if (FRAME_FACE_CACHE (it->f) == NULL)
2600 init_frame_faces (it->f);
2601 if (FRAME_FACE_CACHE (it->f)->used == 0)
2602 recompute_basic_faces (it->f);
2604 /* Current value of the `slice', `space-width', and 'height' properties. */
2605 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2606 it->space_width = Qnil;
2607 it->font_height = Qnil;
2608 it->override_ascent = -1;
2610 /* Are control characters displayed as `^C'? */
2611 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2613 /* -1 means everything between a CR and the following line end
2614 is invisible. >0 means lines indented more than this value are
2615 invisible. */
2616 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2617 ? XINT (BVAR (current_buffer, selective_display))
2618 : (!NILP (BVAR (current_buffer, selective_display))
2619 ? -1 : 0));
2620 it->selective_display_ellipsis_p
2621 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2623 /* Display table to use. */
2624 it->dp = window_display_table (w);
2626 /* Are multibyte characters enabled in current_buffer? */
2627 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2629 /* Non-zero if we should highlight the region. */
2630 highlight_region_p
2631 = (!NILP (Vtransient_mark_mode)
2632 && !NILP (BVAR (current_buffer, mark_active))
2633 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2635 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2636 start and end of a visible region in window IT->w. Set both to
2637 -1 to indicate no region. */
2638 if (highlight_region_p
2639 /* Maybe highlight only in selected window. */
2640 && (/* Either show region everywhere. */
2641 highlight_nonselected_windows
2642 /* Or show region in the selected window. */
2643 || w == XWINDOW (selected_window)
2644 /* Or show the region if we are in the mini-buffer and W is
2645 the window the mini-buffer refers to. */
2646 || (MINI_WINDOW_P (XWINDOW (selected_window))
2647 && WINDOWP (minibuf_selected_window)
2648 && w == XWINDOW (minibuf_selected_window))))
2650 EMACS_INT markpos = marker_position (BVAR (current_buffer, mark));
2651 it->region_beg_charpos = min (PT, markpos);
2652 it->region_end_charpos = max (PT, markpos);
2654 else
2655 it->region_beg_charpos = it->region_end_charpos = -1;
2657 /* Get the position at which the redisplay_end_trigger hook should
2658 be run, if it is to be run at all. */
2659 if (MARKERP (w->redisplay_end_trigger)
2660 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2661 it->redisplay_end_trigger_charpos
2662 = marker_position (w->redisplay_end_trigger);
2663 else if (INTEGERP (w->redisplay_end_trigger))
2664 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2666 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2668 /* Are lines in the display truncated? */
2669 if (base_face_id != DEFAULT_FACE_ID
2670 || XINT (it->w->hscroll)
2671 || (! WINDOW_FULL_WIDTH_P (it->w)
2672 && ((!NILP (Vtruncate_partial_width_windows)
2673 && !INTEGERP (Vtruncate_partial_width_windows))
2674 || (INTEGERP (Vtruncate_partial_width_windows)
2675 && (WINDOW_TOTAL_COLS (it->w)
2676 < XINT (Vtruncate_partial_width_windows))))))
2677 it->line_wrap = TRUNCATE;
2678 else if (NILP (BVAR (current_buffer, truncate_lines)))
2679 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2680 ? WINDOW_WRAP : WORD_WRAP;
2681 else
2682 it->line_wrap = TRUNCATE;
2684 /* Get dimensions of truncation and continuation glyphs. These are
2685 displayed as fringe bitmaps under X, so we don't need them for such
2686 frames. */
2687 if (!FRAME_WINDOW_P (it->f))
2689 if (it->line_wrap == TRUNCATE)
2691 /* We will need the truncation glyph. */
2692 xassert (it->glyph_row == NULL);
2693 produce_special_glyphs (it, IT_TRUNCATION);
2694 it->truncation_pixel_width = it->pixel_width;
2696 else
2698 /* We will need the continuation glyph. */
2699 xassert (it->glyph_row == NULL);
2700 produce_special_glyphs (it, IT_CONTINUATION);
2701 it->continuation_pixel_width = it->pixel_width;
2704 /* Reset these values to zero because the produce_special_glyphs
2705 above has changed them. */
2706 it->pixel_width = it->ascent = it->descent = 0;
2707 it->phys_ascent = it->phys_descent = 0;
2710 /* Set this after getting the dimensions of truncation and
2711 continuation glyphs, so that we don't produce glyphs when calling
2712 produce_special_glyphs, above. */
2713 it->glyph_row = row;
2714 it->area = TEXT_AREA;
2716 /* Forget any previous info about this row being reversed. */
2717 if (it->glyph_row)
2718 it->glyph_row->reversed_p = 0;
2720 /* Get the dimensions of the display area. The display area
2721 consists of the visible window area plus a horizontally scrolled
2722 part to the left of the window. All x-values are relative to the
2723 start of this total display area. */
2724 if (base_face_id != DEFAULT_FACE_ID)
2726 /* Mode lines, menu bar in terminal frames. */
2727 it->first_visible_x = 0;
2728 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2730 else
2732 it->first_visible_x
2733 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2734 it->last_visible_x = (it->first_visible_x
2735 + window_box_width (w, TEXT_AREA));
2737 /* If we truncate lines, leave room for the truncator glyph(s) at
2738 the right margin. Otherwise, leave room for the continuation
2739 glyph(s). Truncation and continuation glyphs are not inserted
2740 for window-based redisplay. */
2741 if (!FRAME_WINDOW_P (it->f))
2743 if (it->line_wrap == TRUNCATE)
2744 it->last_visible_x -= it->truncation_pixel_width;
2745 else
2746 it->last_visible_x -= it->continuation_pixel_width;
2749 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2750 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2753 /* Leave room for a border glyph. */
2754 if (!FRAME_WINDOW_P (it->f)
2755 && !WINDOW_RIGHTMOST_P (it->w))
2756 it->last_visible_x -= 1;
2758 it->last_visible_y = window_text_bottom_y (w);
2760 /* For mode lines and alike, arrange for the first glyph having a
2761 left box line if the face specifies a box. */
2762 if (base_face_id != DEFAULT_FACE_ID)
2764 struct face *face;
2766 it->face_id = remapped_base_face_id;
2768 /* If we have a boxed mode line, make the first character appear
2769 with a left box line. */
2770 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2771 if (face->box != FACE_NO_BOX)
2772 it->start_of_box_run_p = 1;
2775 /* If a buffer position was specified, set the iterator there,
2776 getting overlays and face properties from that position. */
2777 if (charpos >= BUF_BEG (current_buffer))
2779 it->end_charpos = ZV;
2780 IT_CHARPOS (*it) = charpos;
2782 /* We will rely on `reseat' to set this up properly, via
2783 handle_face_prop. */
2784 it->face_id = it->base_face_id;
2786 /* Compute byte position if not specified. */
2787 if (bytepos < charpos)
2788 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2789 else
2790 IT_BYTEPOS (*it) = bytepos;
2792 it->start = it->current;
2793 /* Do we need to reorder bidirectional text? Not if this is a
2794 unibyte buffer: by definition, none of the single-byte
2795 characters are strong R2L, so no reordering is needed. And
2796 bidi.c doesn't support unibyte buffers anyway. Also, don't
2797 reorder while we are loading loadup.el, since the tables of
2798 character properties needed for reordering are not yet
2799 available. */
2800 it->bidi_p =
2801 NILP (Vpurify_flag)
2802 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2803 && it->multibyte_p;
2805 /* If we are to reorder bidirectional text, init the bidi
2806 iterator. */
2807 if (it->bidi_p)
2809 /* Note the paragraph direction that this buffer wants to
2810 use. */
2811 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2812 Qleft_to_right))
2813 it->paragraph_embedding = L2R;
2814 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2815 Qright_to_left))
2816 it->paragraph_embedding = R2L;
2817 else
2818 it->paragraph_embedding = NEUTRAL_DIR;
2819 bidi_unshelve_cache (NULL, 0);
2820 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2821 &it->bidi_it);
2824 /* Compute faces etc. */
2825 reseat (it, it->current.pos, 1);
2828 CHECK_IT (it);
2832 /* Initialize IT for the display of window W with window start POS. */
2834 void
2835 start_display (struct it *it, struct window *w, struct text_pos pos)
2837 struct glyph_row *row;
2838 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2840 row = w->desired_matrix->rows + first_vpos;
2841 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2842 it->first_vpos = first_vpos;
2844 /* Don't reseat to previous visible line start if current start
2845 position is in a string or image. */
2846 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2848 int start_at_line_beg_p;
2849 int first_y = it->current_y;
2851 /* If window start is not at a line start, skip forward to POS to
2852 get the correct continuation lines width. */
2853 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2854 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2855 if (!start_at_line_beg_p)
2857 int new_x;
2859 reseat_at_previous_visible_line_start (it);
2860 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2862 new_x = it->current_x + it->pixel_width;
2864 /* If lines are continued, this line may end in the middle
2865 of a multi-glyph character (e.g. a control character
2866 displayed as \003, or in the middle of an overlay
2867 string). In this case move_it_to above will not have
2868 taken us to the start of the continuation line but to the
2869 end of the continued line. */
2870 if (it->current_x > 0
2871 && it->line_wrap != TRUNCATE /* Lines are continued. */
2872 && (/* And glyph doesn't fit on the line. */
2873 new_x > it->last_visible_x
2874 /* Or it fits exactly and we're on a window
2875 system frame. */
2876 || (new_x == it->last_visible_x
2877 && FRAME_WINDOW_P (it->f))))
2879 if ((it->current.dpvec_index >= 0
2880 || it->current.overlay_string_index >= 0)
2881 /* If we are on a newline from a display vector or
2882 overlay string, then we are already at the end of
2883 a screen line; no need to go to the next line in
2884 that case, as this line is not really continued.
2885 (If we do go to the next line, C-e will not DTRT.) */
2886 && it->c != '\n')
2888 set_iterator_to_next (it, 1);
2889 move_it_in_display_line_to (it, -1, -1, 0);
2892 it->continuation_lines_width += it->current_x;
2894 /* If the character at POS is displayed via a display
2895 vector, move_it_to above stops at the final glyph of
2896 IT->dpvec. To make the caller redisplay that character
2897 again (a.k.a. start at POS), we need to reset the
2898 dpvec_index to the beginning of IT->dpvec. */
2899 else if (it->current.dpvec_index >= 0)
2900 it->current.dpvec_index = 0;
2902 /* We're starting a new display line, not affected by the
2903 height of the continued line, so clear the appropriate
2904 fields in the iterator structure. */
2905 it->max_ascent = it->max_descent = 0;
2906 it->max_phys_ascent = it->max_phys_descent = 0;
2908 it->current_y = first_y;
2909 it->vpos = 0;
2910 it->current_x = it->hpos = 0;
2916 /* Return 1 if POS is a position in ellipses displayed for invisible
2917 text. W is the window we display, for text property lookup. */
2919 static int
2920 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2922 Lisp_Object prop, window;
2923 int ellipses_p = 0;
2924 EMACS_INT charpos = CHARPOS (pos->pos);
2926 /* If POS specifies a position in a display vector, this might
2927 be for an ellipsis displayed for invisible text. We won't
2928 get the iterator set up for delivering that ellipsis unless
2929 we make sure that it gets aware of the invisible text. */
2930 if (pos->dpvec_index >= 0
2931 && pos->overlay_string_index < 0
2932 && CHARPOS (pos->string_pos) < 0
2933 && charpos > BEGV
2934 && (XSETWINDOW (window, w),
2935 prop = Fget_char_property (make_number (charpos),
2936 Qinvisible, window),
2937 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2939 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2940 window);
2941 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2944 return ellipses_p;
2948 /* Initialize IT for stepping through current_buffer in window W,
2949 starting at position POS that includes overlay string and display
2950 vector/ control character translation position information. Value
2951 is zero if there are overlay strings with newlines at POS. */
2953 static int
2954 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2956 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2957 int i, overlay_strings_with_newlines = 0;
2959 /* If POS specifies a position in a display vector, this might
2960 be for an ellipsis displayed for invisible text. We won't
2961 get the iterator set up for delivering that ellipsis unless
2962 we make sure that it gets aware of the invisible text. */
2963 if (in_ellipses_for_invisible_text_p (pos, w))
2965 --charpos;
2966 bytepos = 0;
2969 /* Keep in mind: the call to reseat in init_iterator skips invisible
2970 text, so we might end up at a position different from POS. This
2971 is only a problem when POS is a row start after a newline and an
2972 overlay starts there with an after-string, and the overlay has an
2973 invisible property. Since we don't skip invisible text in
2974 display_line and elsewhere immediately after consuming the
2975 newline before the row start, such a POS will not be in a string,
2976 but the call to init_iterator below will move us to the
2977 after-string. */
2978 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2980 /* This only scans the current chunk -- it should scan all chunks.
2981 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2982 to 16 in 22.1 to make this a lesser problem. */
2983 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2985 const char *s = SSDATA (it->overlay_strings[i]);
2986 const char *e = s + SBYTES (it->overlay_strings[i]);
2988 while (s < e && *s != '\n')
2989 ++s;
2991 if (s < e)
2993 overlay_strings_with_newlines = 1;
2994 break;
2998 /* If position is within an overlay string, set up IT to the right
2999 overlay string. */
3000 if (pos->overlay_string_index >= 0)
3002 int relative_index;
3004 /* If the first overlay string happens to have a `display'
3005 property for an image, the iterator will be set up for that
3006 image, and we have to undo that setup first before we can
3007 correct the overlay string index. */
3008 if (it->method == GET_FROM_IMAGE)
3009 pop_it (it);
3011 /* We already have the first chunk of overlay strings in
3012 IT->overlay_strings. Load more until the one for
3013 pos->overlay_string_index is in IT->overlay_strings. */
3014 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3016 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3017 it->current.overlay_string_index = 0;
3018 while (n--)
3020 load_overlay_strings (it, 0);
3021 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3025 it->current.overlay_string_index = pos->overlay_string_index;
3026 relative_index = (it->current.overlay_string_index
3027 % OVERLAY_STRING_CHUNK_SIZE);
3028 it->string = it->overlay_strings[relative_index];
3029 xassert (STRINGP (it->string));
3030 it->current.string_pos = pos->string_pos;
3031 it->method = GET_FROM_STRING;
3034 if (CHARPOS (pos->string_pos) >= 0)
3036 /* Recorded position is not in an overlay string, but in another
3037 string. This can only be a string from a `display' property.
3038 IT should already be filled with that string. */
3039 it->current.string_pos = pos->string_pos;
3040 xassert (STRINGP (it->string));
3043 /* Restore position in display vector translations, control
3044 character translations or ellipses. */
3045 if (pos->dpvec_index >= 0)
3047 if (it->dpvec == NULL)
3048 get_next_display_element (it);
3049 xassert (it->dpvec && it->current.dpvec_index == 0);
3050 it->current.dpvec_index = pos->dpvec_index;
3053 CHECK_IT (it);
3054 return !overlay_strings_with_newlines;
3058 /* Initialize IT for stepping through current_buffer in window W
3059 starting at ROW->start. */
3061 static void
3062 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3064 init_from_display_pos (it, w, &row->start);
3065 it->start = row->start;
3066 it->continuation_lines_width = row->continuation_lines_width;
3067 CHECK_IT (it);
3071 /* Initialize IT for stepping through current_buffer in window W
3072 starting in the line following ROW, i.e. starting at ROW->end.
3073 Value is zero if there are overlay strings with newlines at ROW's
3074 end position. */
3076 static int
3077 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3079 int success = 0;
3081 if (init_from_display_pos (it, w, &row->end))
3083 if (row->continued_p)
3084 it->continuation_lines_width
3085 = row->continuation_lines_width + row->pixel_width;
3086 CHECK_IT (it);
3087 success = 1;
3090 return success;
3096 /***********************************************************************
3097 Text properties
3098 ***********************************************************************/
3100 /* Called when IT reaches IT->stop_charpos. Handle text property and
3101 overlay changes. Set IT->stop_charpos to the next position where
3102 to stop. */
3104 static void
3105 handle_stop (struct it *it)
3107 enum prop_handled handled;
3108 int handle_overlay_change_p;
3109 struct props *p;
3111 it->dpvec = NULL;
3112 it->current.dpvec_index = -1;
3113 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3114 it->ignore_overlay_strings_at_pos_p = 0;
3115 it->ellipsis_p = 0;
3117 /* Use face of preceding text for ellipsis (if invisible) */
3118 if (it->selective_display_ellipsis_p)
3119 it->saved_face_id = it->face_id;
3123 handled = HANDLED_NORMALLY;
3125 /* Call text property handlers. */
3126 for (p = it_props; p->handler; ++p)
3128 handled = p->handler (it);
3130 if (handled == HANDLED_RECOMPUTE_PROPS)
3131 break;
3132 else if (handled == HANDLED_RETURN)
3134 /* We still want to show before and after strings from
3135 overlays even if the actual buffer text is replaced. */
3136 if (!handle_overlay_change_p
3137 || it->sp > 1
3138 || !get_overlay_strings_1 (it, 0, 0))
3140 if (it->ellipsis_p)
3141 setup_for_ellipsis (it, 0);
3142 /* When handling a display spec, we might load an
3143 empty string. In that case, discard it here. We
3144 used to discard it in handle_single_display_spec,
3145 but that causes get_overlay_strings_1, above, to
3146 ignore overlay strings that we must check. */
3147 if (STRINGP (it->string) && !SCHARS (it->string))
3148 pop_it (it);
3149 return;
3151 else if (STRINGP (it->string) && !SCHARS (it->string))
3152 pop_it (it);
3153 else
3155 it->ignore_overlay_strings_at_pos_p = 1;
3156 it->string_from_display_prop_p = 0;
3157 it->from_disp_prop_p = 0;
3158 handle_overlay_change_p = 0;
3160 handled = HANDLED_RECOMPUTE_PROPS;
3161 break;
3163 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3164 handle_overlay_change_p = 0;
3167 if (handled != HANDLED_RECOMPUTE_PROPS)
3169 /* Don't check for overlay strings below when set to deliver
3170 characters from a display vector. */
3171 if (it->method == GET_FROM_DISPLAY_VECTOR)
3172 handle_overlay_change_p = 0;
3174 /* Handle overlay changes.
3175 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3176 if it finds overlays. */
3177 if (handle_overlay_change_p)
3178 handled = handle_overlay_change (it);
3181 if (it->ellipsis_p)
3183 setup_for_ellipsis (it, 0);
3184 break;
3187 while (handled == HANDLED_RECOMPUTE_PROPS);
3189 /* Determine where to stop next. */
3190 if (handled == HANDLED_NORMALLY)
3191 compute_stop_pos (it);
3195 /* Compute IT->stop_charpos from text property and overlay change
3196 information for IT's current position. */
3198 static void
3199 compute_stop_pos (struct it *it)
3201 register INTERVAL iv, next_iv;
3202 Lisp_Object object, limit, position;
3203 EMACS_INT charpos, bytepos;
3205 if (STRINGP (it->string))
3207 /* Strings are usually short, so don't limit the search for
3208 properties. */
3209 it->stop_charpos = it->end_charpos;
3210 object = it->string;
3211 limit = Qnil;
3212 charpos = IT_STRING_CHARPOS (*it);
3213 bytepos = IT_STRING_BYTEPOS (*it);
3215 else
3217 EMACS_INT pos;
3219 /* If end_charpos is out of range for some reason, such as a
3220 misbehaving display function, rationalize it (Bug#5984). */
3221 if (it->end_charpos > ZV)
3222 it->end_charpos = ZV;
3223 it->stop_charpos = it->end_charpos;
3225 /* If next overlay change is in front of the current stop pos
3226 (which is IT->end_charpos), stop there. Note: value of
3227 next_overlay_change is point-max if no overlay change
3228 follows. */
3229 charpos = IT_CHARPOS (*it);
3230 bytepos = IT_BYTEPOS (*it);
3231 pos = next_overlay_change (charpos);
3232 if (pos < it->stop_charpos)
3233 it->stop_charpos = pos;
3235 /* If showing the region, we have to stop at the region
3236 start or end because the face might change there. */
3237 if (it->region_beg_charpos > 0)
3239 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3240 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3241 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3242 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3245 /* Set up variables for computing the stop position from text
3246 property changes. */
3247 XSETBUFFER (object, current_buffer);
3248 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3251 /* Get the interval containing IT's position. Value is a null
3252 interval if there isn't such an interval. */
3253 position = make_number (charpos);
3254 iv = validate_interval_range (object, &position, &position, 0);
3255 if (!NULL_INTERVAL_P (iv))
3257 Lisp_Object values_here[LAST_PROP_IDX];
3258 struct props *p;
3260 /* Get properties here. */
3261 for (p = it_props; p->handler; ++p)
3262 values_here[p->idx] = textget (iv->plist, *p->name);
3264 /* Look for an interval following iv that has different
3265 properties. */
3266 for (next_iv = next_interval (iv);
3267 (!NULL_INTERVAL_P (next_iv)
3268 && (NILP (limit)
3269 || XFASTINT (limit) > next_iv->position));
3270 next_iv = next_interval (next_iv))
3272 for (p = it_props; p->handler; ++p)
3274 Lisp_Object new_value;
3276 new_value = textget (next_iv->plist, *p->name);
3277 if (!EQ (values_here[p->idx], new_value))
3278 break;
3281 if (p->handler)
3282 break;
3285 if (!NULL_INTERVAL_P (next_iv))
3287 if (INTEGERP (limit)
3288 && next_iv->position >= XFASTINT (limit))
3289 /* No text property change up to limit. */
3290 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3291 else
3292 /* Text properties change in next_iv. */
3293 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3297 if (it->cmp_it.id < 0)
3299 EMACS_INT stoppos = it->end_charpos;
3301 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3302 stoppos = -1;
3303 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3304 stoppos, it->string);
3307 xassert (STRINGP (it->string)
3308 || (it->stop_charpos >= BEGV
3309 && it->stop_charpos >= IT_CHARPOS (*it)));
3313 /* Return the position of the next overlay change after POS in
3314 current_buffer. Value is point-max if no overlay change
3315 follows. This is like `next-overlay-change' but doesn't use
3316 xmalloc. */
3318 static EMACS_INT
3319 next_overlay_change (EMACS_INT pos)
3321 ptrdiff_t i, noverlays;
3322 EMACS_INT endpos;
3323 Lisp_Object *overlays;
3325 /* Get all overlays at the given position. */
3326 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3328 /* If any of these overlays ends before endpos,
3329 use its ending point instead. */
3330 for (i = 0; i < noverlays; ++i)
3332 Lisp_Object oend;
3333 EMACS_INT oendpos;
3335 oend = OVERLAY_END (overlays[i]);
3336 oendpos = OVERLAY_POSITION (oend);
3337 endpos = min (endpos, oendpos);
3340 return endpos;
3343 /* How many characters forward to search for a display property or
3344 display string. Searching too far forward makes the bidi display
3345 sluggish, especially in small windows. */
3346 #define MAX_DISP_SCAN 250
3348 /* Return the character position of a display string at or after
3349 position specified by POSITION. If no display string exists at or
3350 after POSITION, return ZV. A display string is either an overlay
3351 with `display' property whose value is a string, or a `display'
3352 text property whose value is a string. STRING is data about the
3353 string to iterate; if STRING->lstring is nil, we are iterating a
3354 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3355 on a GUI frame. DISP_PROP is set to zero if we searched
3356 MAX_DISP_SCAN characters forward without finding any display
3357 strings, non-zero otherwise. It is set to 2 if the display string
3358 uses any kind of `(space ...)' spec that will produce a stretch of
3359 white space in the text area. */
3360 EMACS_INT
3361 compute_display_string_pos (struct text_pos *position,
3362 struct bidi_string_data *string,
3363 int frame_window_p, int *disp_prop)
3365 /* OBJECT = nil means current buffer. */
3366 Lisp_Object object =
3367 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3368 Lisp_Object pos, spec, limpos;
3369 int string_p = (string && (STRINGP (string->lstring) || string->s));
3370 EMACS_INT eob = string_p ? string->schars : ZV;
3371 EMACS_INT begb = string_p ? 0 : BEGV;
3372 EMACS_INT bufpos, charpos = CHARPOS (*position);
3373 EMACS_INT lim =
3374 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3375 struct text_pos tpos;
3376 int rv = 0;
3378 *disp_prop = 1;
3380 if (charpos >= eob
3381 /* We don't support display properties whose values are strings
3382 that have display string properties. */
3383 || string->from_disp_str
3384 /* C strings cannot have display properties. */
3385 || (string->s && !STRINGP (object)))
3387 *disp_prop = 0;
3388 return eob;
3391 /* If the character at CHARPOS is where the display string begins,
3392 return CHARPOS. */
3393 pos = make_number (charpos);
3394 if (STRINGP (object))
3395 bufpos = string->bufpos;
3396 else
3397 bufpos = charpos;
3398 tpos = *position;
3399 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3400 && (charpos <= begb
3401 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3402 object),
3403 spec))
3404 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3405 frame_window_p)))
3407 if (rv == 2)
3408 *disp_prop = 2;
3409 return charpos;
3412 /* Look forward for the first character with a `display' property
3413 that will replace the underlying text when displayed. */
3414 limpos = make_number (lim);
3415 do {
3416 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3417 CHARPOS (tpos) = XFASTINT (pos);
3418 if (CHARPOS (tpos) >= lim)
3420 *disp_prop = 0;
3421 break;
3423 if (STRINGP (object))
3424 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3425 else
3426 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3427 spec = Fget_char_property (pos, Qdisplay, object);
3428 if (!STRINGP (object))
3429 bufpos = CHARPOS (tpos);
3430 } while (NILP (spec)
3431 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3432 bufpos, frame_window_p)));
3433 if (rv == 2)
3434 *disp_prop = 2;
3436 return CHARPOS (tpos);
3439 /* Return the character position of the end of the display string that
3440 started at CHARPOS. If there's no display string at CHARPOS,
3441 return -1. A display string is either an overlay with `display'
3442 property whose value is a string or a `display' text property whose
3443 value is a string. */
3444 EMACS_INT
3445 compute_display_string_end (EMACS_INT charpos, struct bidi_string_data *string)
3447 /* OBJECT = nil means current buffer. */
3448 Lisp_Object object =
3449 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3450 Lisp_Object pos = make_number (charpos);
3451 EMACS_INT eob =
3452 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3454 if (charpos >= eob || (string->s && !STRINGP (object)))
3455 return eob;
3457 /* It could happen that the display property or overlay was removed
3458 since we found it in compute_display_string_pos above. One way
3459 this can happen is if JIT font-lock was called (through
3460 handle_fontified_prop), and jit-lock-functions remove text
3461 properties or overlays from the portion of buffer that includes
3462 CHARPOS. Muse mode is known to do that, for example. In this
3463 case, we return -1 to the caller, to signal that no display
3464 string is actually present at CHARPOS. See bidi_fetch_char for
3465 how this is handled.
3467 An alternative would be to never look for display properties past
3468 it->stop_charpos. But neither compute_display_string_pos nor
3469 bidi_fetch_char that calls it know or care where the next
3470 stop_charpos is. */
3471 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3472 return -1;
3474 /* Look forward for the first character where the `display' property
3475 changes. */
3476 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3478 return XFASTINT (pos);
3483 /***********************************************************************
3484 Fontification
3485 ***********************************************************************/
3487 /* Handle changes in the `fontified' property of the current buffer by
3488 calling hook functions from Qfontification_functions to fontify
3489 regions of text. */
3491 static enum prop_handled
3492 handle_fontified_prop (struct it *it)
3494 Lisp_Object prop, pos;
3495 enum prop_handled handled = HANDLED_NORMALLY;
3497 if (!NILP (Vmemory_full))
3498 return handled;
3500 /* Get the value of the `fontified' property at IT's current buffer
3501 position. (The `fontified' property doesn't have a special
3502 meaning in strings.) If the value is nil, call functions from
3503 Qfontification_functions. */
3504 if (!STRINGP (it->string)
3505 && it->s == NULL
3506 && !NILP (Vfontification_functions)
3507 && !NILP (Vrun_hooks)
3508 && (pos = make_number (IT_CHARPOS (*it)),
3509 prop = Fget_char_property (pos, Qfontified, Qnil),
3510 /* Ignore the special cased nil value always present at EOB since
3511 no amount of fontifying will be able to change it. */
3512 NILP (prop) && IT_CHARPOS (*it) < Z))
3514 int count = SPECPDL_INDEX ();
3515 Lisp_Object val;
3516 struct buffer *obuf = current_buffer;
3517 int begv = BEGV, zv = ZV;
3518 int old_clip_changed = current_buffer->clip_changed;
3520 val = Vfontification_functions;
3521 specbind (Qfontification_functions, Qnil);
3523 xassert (it->end_charpos == ZV);
3525 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3526 safe_call1 (val, pos);
3527 else
3529 Lisp_Object fns, fn;
3530 struct gcpro gcpro1, gcpro2;
3532 fns = Qnil;
3533 GCPRO2 (val, fns);
3535 for (; CONSP (val); val = XCDR (val))
3537 fn = XCAR (val);
3539 if (EQ (fn, Qt))
3541 /* A value of t indicates this hook has a local
3542 binding; it means to run the global binding too.
3543 In a global value, t should not occur. If it
3544 does, we must ignore it to avoid an endless
3545 loop. */
3546 for (fns = Fdefault_value (Qfontification_functions);
3547 CONSP (fns);
3548 fns = XCDR (fns))
3550 fn = XCAR (fns);
3551 if (!EQ (fn, Qt))
3552 safe_call1 (fn, pos);
3555 else
3556 safe_call1 (fn, pos);
3559 UNGCPRO;
3562 unbind_to (count, Qnil);
3564 /* Fontification functions routinely call `save-restriction'.
3565 Normally, this tags clip_changed, which can confuse redisplay
3566 (see discussion in Bug#6671). Since we don't perform any
3567 special handling of fontification changes in the case where
3568 `save-restriction' isn't called, there's no point doing so in
3569 this case either. So, if the buffer's restrictions are
3570 actually left unchanged, reset clip_changed. */
3571 if (obuf == current_buffer)
3573 if (begv == BEGV && zv == ZV)
3574 current_buffer->clip_changed = old_clip_changed;
3576 /* There isn't much we can reasonably do to protect against
3577 misbehaving fontification, but here's a fig leaf. */
3578 else if (!NILP (BVAR (obuf, name)))
3579 set_buffer_internal_1 (obuf);
3581 /* The fontification code may have added/removed text.
3582 It could do even a lot worse, but let's at least protect against
3583 the most obvious case where only the text past `pos' gets changed',
3584 as is/was done in grep.el where some escapes sequences are turned
3585 into face properties (bug#7876). */
3586 it->end_charpos = ZV;
3588 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3589 something. This avoids an endless loop if they failed to
3590 fontify the text for which reason ever. */
3591 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3592 handled = HANDLED_RECOMPUTE_PROPS;
3595 return handled;
3600 /***********************************************************************
3601 Faces
3602 ***********************************************************************/
3604 /* Set up iterator IT from face properties at its current position.
3605 Called from handle_stop. */
3607 static enum prop_handled
3608 handle_face_prop (struct it *it)
3610 int new_face_id;
3611 EMACS_INT next_stop;
3613 if (!STRINGP (it->string))
3615 new_face_id
3616 = face_at_buffer_position (it->w,
3617 IT_CHARPOS (*it),
3618 it->region_beg_charpos,
3619 it->region_end_charpos,
3620 &next_stop,
3621 (IT_CHARPOS (*it)
3622 + TEXT_PROP_DISTANCE_LIMIT),
3623 0, it->base_face_id);
3625 /* Is this a start of a run of characters with box face?
3626 Caveat: this can be called for a freshly initialized
3627 iterator; face_id is -1 in this case. We know that the new
3628 face will not change until limit, i.e. if the new face has a
3629 box, all characters up to limit will have one. But, as
3630 usual, we don't know whether limit is really the end. */
3631 if (new_face_id != it->face_id)
3633 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3635 /* If new face has a box but old face has not, this is
3636 the start of a run of characters with box, i.e. it has
3637 a shadow on the left side. The value of face_id of the
3638 iterator will be -1 if this is the initial call that gets
3639 the face. In this case, we have to look in front of IT's
3640 position and see whether there is a face != new_face_id. */
3641 it->start_of_box_run_p
3642 = (new_face->box != FACE_NO_BOX
3643 && (it->face_id >= 0
3644 || IT_CHARPOS (*it) == BEG
3645 || new_face_id != face_before_it_pos (it)));
3646 it->face_box_p = new_face->box != FACE_NO_BOX;
3649 else
3651 int base_face_id;
3652 EMACS_INT bufpos;
3653 int i;
3654 Lisp_Object from_overlay
3655 = (it->current.overlay_string_index >= 0
3656 ? it->string_overlays[it->current.overlay_string_index]
3657 : Qnil);
3659 /* See if we got to this string directly or indirectly from
3660 an overlay property. That includes the before-string or
3661 after-string of an overlay, strings in display properties
3662 provided by an overlay, their text properties, etc.
3664 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3665 if (! NILP (from_overlay))
3666 for (i = it->sp - 1; i >= 0; i--)
3668 if (it->stack[i].current.overlay_string_index >= 0)
3669 from_overlay
3670 = it->string_overlays[it->stack[i].current.overlay_string_index];
3671 else if (! NILP (it->stack[i].from_overlay))
3672 from_overlay = it->stack[i].from_overlay;
3674 if (!NILP (from_overlay))
3675 break;
3678 if (! NILP (from_overlay))
3680 bufpos = IT_CHARPOS (*it);
3681 /* For a string from an overlay, the base face depends
3682 only on text properties and ignores overlays. */
3683 base_face_id
3684 = face_for_overlay_string (it->w,
3685 IT_CHARPOS (*it),
3686 it->region_beg_charpos,
3687 it->region_end_charpos,
3688 &next_stop,
3689 (IT_CHARPOS (*it)
3690 + TEXT_PROP_DISTANCE_LIMIT),
3692 from_overlay);
3694 else
3696 bufpos = 0;
3698 /* For strings from a `display' property, use the face at
3699 IT's current buffer position as the base face to merge
3700 with, so that overlay strings appear in the same face as
3701 surrounding text, unless they specify their own
3702 faces. */
3703 base_face_id = it->string_from_prefix_prop_p
3704 ? DEFAULT_FACE_ID
3705 : underlying_face_id (it);
3708 new_face_id = face_at_string_position (it->w,
3709 it->string,
3710 IT_STRING_CHARPOS (*it),
3711 bufpos,
3712 it->region_beg_charpos,
3713 it->region_end_charpos,
3714 &next_stop,
3715 base_face_id, 0);
3717 /* Is this a start of a run of characters with box? Caveat:
3718 this can be called for a freshly allocated iterator; face_id
3719 is -1 is this case. We know that the new face will not
3720 change until the next check pos, i.e. if the new face has a
3721 box, all characters up to that position will have a
3722 box. But, as usual, we don't know whether that position
3723 is really the end. */
3724 if (new_face_id != it->face_id)
3726 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3727 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3729 /* If new face has a box but old face hasn't, this is the
3730 start of a run of characters with box, i.e. it has a
3731 shadow on the left side. */
3732 it->start_of_box_run_p
3733 = new_face->box && (old_face == NULL || !old_face->box);
3734 it->face_box_p = new_face->box != FACE_NO_BOX;
3738 it->face_id = new_face_id;
3739 return HANDLED_NORMALLY;
3743 /* Return the ID of the face ``underlying'' IT's current position,
3744 which is in a string. If the iterator is associated with a
3745 buffer, return the face at IT's current buffer position.
3746 Otherwise, use the iterator's base_face_id. */
3748 static int
3749 underlying_face_id (struct it *it)
3751 int face_id = it->base_face_id, i;
3753 xassert (STRINGP (it->string));
3755 for (i = it->sp - 1; i >= 0; --i)
3756 if (NILP (it->stack[i].string))
3757 face_id = it->stack[i].face_id;
3759 return face_id;
3763 /* Compute the face one character before or after the current position
3764 of IT, in the visual order. BEFORE_P non-zero means get the face
3765 in front (to the left in L2R paragraphs, to the right in R2L
3766 paragraphs) of IT's screen position. Value is the ID of the face. */
3768 static int
3769 face_before_or_after_it_pos (struct it *it, int before_p)
3771 int face_id, limit;
3772 EMACS_INT next_check_charpos;
3773 struct it it_copy;
3774 void *it_copy_data = NULL;
3776 xassert (it->s == NULL);
3778 if (STRINGP (it->string))
3780 EMACS_INT bufpos, charpos;
3781 int base_face_id;
3783 /* No face change past the end of the string (for the case
3784 we are padding with spaces). No face change before the
3785 string start. */
3786 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3787 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3788 return it->face_id;
3790 if (!it->bidi_p)
3792 /* Set charpos to the position before or after IT's current
3793 position, in the logical order, which in the non-bidi
3794 case is the same as the visual order. */
3795 if (before_p)
3796 charpos = IT_STRING_CHARPOS (*it) - 1;
3797 else if (it->what == IT_COMPOSITION)
3798 /* For composition, we must check the character after the
3799 composition. */
3800 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3801 else
3802 charpos = IT_STRING_CHARPOS (*it) + 1;
3804 else
3806 if (before_p)
3808 /* With bidi iteration, the character before the current
3809 in the visual order cannot be found by simple
3810 iteration, because "reverse" reordering is not
3811 supported. Instead, we need to use the move_it_*
3812 family of functions. */
3813 /* Ignore face changes before the first visible
3814 character on this display line. */
3815 if (it->current_x <= it->first_visible_x)
3816 return it->face_id;
3817 SAVE_IT (it_copy, *it, it_copy_data);
3818 /* Implementation note: Since move_it_in_display_line
3819 works in the iterator geometry, and thinks the first
3820 character is always the leftmost, even in R2L lines,
3821 we don't need to distinguish between the R2L and L2R
3822 cases here. */
3823 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3824 it_copy.current_x - 1, MOVE_TO_X);
3825 charpos = IT_STRING_CHARPOS (it_copy);
3826 RESTORE_IT (it, it, it_copy_data);
3828 else
3830 /* Set charpos to the string position of the character
3831 that comes after IT's current position in the visual
3832 order. */
3833 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3835 it_copy = *it;
3836 while (n--)
3837 bidi_move_to_visually_next (&it_copy.bidi_it);
3839 charpos = it_copy.bidi_it.charpos;
3842 xassert (0 <= charpos && charpos <= SCHARS (it->string));
3844 if (it->current.overlay_string_index >= 0)
3845 bufpos = IT_CHARPOS (*it);
3846 else
3847 bufpos = 0;
3849 base_face_id = underlying_face_id (it);
3851 /* Get the face for ASCII, or unibyte. */
3852 face_id = face_at_string_position (it->w,
3853 it->string,
3854 charpos,
3855 bufpos,
3856 it->region_beg_charpos,
3857 it->region_end_charpos,
3858 &next_check_charpos,
3859 base_face_id, 0);
3861 /* Correct the face for charsets different from ASCII. Do it
3862 for the multibyte case only. The face returned above is
3863 suitable for unibyte text if IT->string is unibyte. */
3864 if (STRING_MULTIBYTE (it->string))
3866 struct text_pos pos1 = string_pos (charpos, it->string);
3867 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3868 int c, len;
3869 struct face *face = FACE_FROM_ID (it->f, face_id);
3871 c = string_char_and_length (p, &len);
3872 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3875 else
3877 struct text_pos pos;
3879 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3880 || (IT_CHARPOS (*it) <= BEGV && before_p))
3881 return it->face_id;
3883 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3884 pos = it->current.pos;
3886 if (!it->bidi_p)
3888 if (before_p)
3889 DEC_TEXT_POS (pos, it->multibyte_p);
3890 else
3892 if (it->what == IT_COMPOSITION)
3894 /* For composition, we must check the position after
3895 the composition. */
3896 pos.charpos += it->cmp_it.nchars;
3897 pos.bytepos += it->len;
3899 else
3900 INC_TEXT_POS (pos, it->multibyte_p);
3903 else
3905 if (before_p)
3907 /* With bidi iteration, the character before the current
3908 in the visual order cannot be found by simple
3909 iteration, because "reverse" reordering is not
3910 supported. Instead, we need to use the move_it_*
3911 family of functions. */
3912 /* Ignore face changes before the first visible
3913 character on this display line. */
3914 if (it->current_x <= it->first_visible_x)
3915 return it->face_id;
3916 SAVE_IT (it_copy, *it, it_copy_data);
3917 /* Implementation note: Since move_it_in_display_line
3918 works in the iterator geometry, and thinks the first
3919 character is always the leftmost, even in R2L lines,
3920 we don't need to distinguish between the R2L and L2R
3921 cases here. */
3922 move_it_in_display_line (&it_copy, ZV,
3923 it_copy.current_x - 1, MOVE_TO_X);
3924 pos = it_copy.current.pos;
3925 RESTORE_IT (it, it, it_copy_data);
3927 else
3929 /* Set charpos to the buffer position of the character
3930 that comes after IT's current position in the visual
3931 order. */
3932 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3934 it_copy = *it;
3935 while (n--)
3936 bidi_move_to_visually_next (&it_copy.bidi_it);
3938 SET_TEXT_POS (pos,
3939 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3942 xassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
3944 /* Determine face for CHARSET_ASCII, or unibyte. */
3945 face_id = face_at_buffer_position (it->w,
3946 CHARPOS (pos),
3947 it->region_beg_charpos,
3948 it->region_end_charpos,
3949 &next_check_charpos,
3950 limit, 0, -1);
3952 /* Correct the face for charsets different from ASCII. Do it
3953 for the multibyte case only. The face returned above is
3954 suitable for unibyte text if current_buffer is unibyte. */
3955 if (it->multibyte_p)
3957 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3958 struct face *face = FACE_FROM_ID (it->f, face_id);
3959 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3963 return face_id;
3968 /***********************************************************************
3969 Invisible text
3970 ***********************************************************************/
3972 /* Set up iterator IT from invisible properties at its current
3973 position. Called from handle_stop. */
3975 static enum prop_handled
3976 handle_invisible_prop (struct it *it)
3978 enum prop_handled handled = HANDLED_NORMALLY;
3980 if (STRINGP (it->string))
3982 Lisp_Object prop, end_charpos, limit, charpos;
3984 /* Get the value of the invisible text property at the
3985 current position. Value will be nil if there is no such
3986 property. */
3987 charpos = make_number (IT_STRING_CHARPOS (*it));
3988 prop = Fget_text_property (charpos, Qinvisible, it->string);
3990 if (!NILP (prop)
3991 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3993 EMACS_INT endpos;
3995 handled = HANDLED_RECOMPUTE_PROPS;
3997 /* Get the position at which the next change of the
3998 invisible text property can be found in IT->string.
3999 Value will be nil if the property value is the same for
4000 all the rest of IT->string. */
4001 XSETINT (limit, SCHARS (it->string));
4002 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4003 it->string, limit);
4005 /* Text at current position is invisible. The next
4006 change in the property is at position end_charpos.
4007 Move IT's current position to that position. */
4008 if (INTEGERP (end_charpos)
4009 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
4011 struct text_pos old;
4012 EMACS_INT oldpos;
4014 old = it->current.string_pos;
4015 oldpos = CHARPOS (old);
4016 if (it->bidi_p)
4018 if (it->bidi_it.first_elt
4019 && it->bidi_it.charpos < SCHARS (it->string))
4020 bidi_paragraph_init (it->paragraph_embedding,
4021 &it->bidi_it, 1);
4022 /* Bidi-iterate out of the invisible text. */
4025 bidi_move_to_visually_next (&it->bidi_it);
4027 while (oldpos <= it->bidi_it.charpos
4028 && it->bidi_it.charpos < endpos);
4030 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4031 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4032 if (IT_CHARPOS (*it) >= endpos)
4033 it->prev_stop = endpos;
4035 else
4037 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4038 compute_string_pos (&it->current.string_pos, old, it->string);
4041 else
4043 /* The rest of the string is invisible. If this is an
4044 overlay string, proceed with the next overlay string
4045 or whatever comes and return a character from there. */
4046 if (it->current.overlay_string_index >= 0)
4048 next_overlay_string (it);
4049 /* Don't check for overlay strings when we just
4050 finished processing them. */
4051 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4053 else
4055 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4056 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4061 else
4063 int invis_p;
4064 EMACS_INT newpos, next_stop, start_charpos, tem;
4065 Lisp_Object pos, prop, overlay;
4067 /* First of all, is there invisible text at this position? */
4068 tem = start_charpos = IT_CHARPOS (*it);
4069 pos = make_number (tem);
4070 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4071 &overlay);
4072 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4074 /* If we are on invisible text, skip over it. */
4075 if (invis_p && start_charpos < it->end_charpos)
4077 /* Record whether we have to display an ellipsis for the
4078 invisible text. */
4079 int display_ellipsis_p = invis_p == 2;
4081 handled = HANDLED_RECOMPUTE_PROPS;
4083 /* Loop skipping over invisible text. The loop is left at
4084 ZV or with IT on the first char being visible again. */
4087 /* Try to skip some invisible text. Return value is the
4088 position reached which can be equal to where we start
4089 if there is nothing invisible there. This skips both
4090 over invisible text properties and overlays with
4091 invisible property. */
4092 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4094 /* If we skipped nothing at all we weren't at invisible
4095 text in the first place. If everything to the end of
4096 the buffer was skipped, end the loop. */
4097 if (newpos == tem || newpos >= ZV)
4098 invis_p = 0;
4099 else
4101 /* We skipped some characters but not necessarily
4102 all there are. Check if we ended up on visible
4103 text. Fget_char_property returns the property of
4104 the char before the given position, i.e. if we
4105 get invis_p = 0, this means that the char at
4106 newpos is visible. */
4107 pos = make_number (newpos);
4108 prop = Fget_char_property (pos, Qinvisible, it->window);
4109 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4112 /* If we ended up on invisible text, proceed to
4113 skip starting with next_stop. */
4114 if (invis_p)
4115 tem = next_stop;
4117 /* If there are adjacent invisible texts, don't lose the
4118 second one's ellipsis. */
4119 if (invis_p == 2)
4120 display_ellipsis_p = 1;
4122 while (invis_p);
4124 /* The position newpos is now either ZV or on visible text. */
4125 if (it->bidi_p)
4127 EMACS_INT bpos = CHAR_TO_BYTE (newpos);
4128 int on_newline =
4129 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4130 int after_newline =
4131 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4133 /* If the invisible text ends on a newline or on a
4134 character after a newline, we can avoid the costly,
4135 character by character, bidi iteration to NEWPOS, and
4136 instead simply reseat the iterator there. That's
4137 because all bidi reordering information is tossed at
4138 the newline. This is a big win for modes that hide
4139 complete lines, like Outline, Org, etc. */
4140 if (on_newline || after_newline)
4142 struct text_pos tpos;
4143 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4145 SET_TEXT_POS (tpos, newpos, bpos);
4146 reseat_1 (it, tpos, 0);
4147 /* If we reseat on a newline/ZV, we need to prep the
4148 bidi iterator for advancing to the next character
4149 after the newline/EOB, keeping the current paragraph
4150 direction (so that PRODUCE_GLYPHS does TRT wrt
4151 prepending/appending glyphs to a glyph row). */
4152 if (on_newline)
4154 it->bidi_it.first_elt = 0;
4155 it->bidi_it.paragraph_dir = pdir;
4156 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4157 it->bidi_it.nchars = 1;
4158 it->bidi_it.ch_len = 1;
4161 else /* Must use the slow method. */
4163 /* With bidi iteration, the region of invisible text
4164 could start and/or end in the middle of a
4165 non-base embedding level. Therefore, we need to
4166 skip invisible text using the bidi iterator,
4167 starting at IT's current position, until we find
4168 ourselves outside of the invisible text.
4169 Skipping invisible text _after_ bidi iteration
4170 avoids affecting the visual order of the
4171 displayed text when invisible properties are
4172 added or removed. */
4173 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4175 /* If we were `reseat'ed to a new paragraph,
4176 determine the paragraph base direction. We
4177 need to do it now because
4178 next_element_from_buffer may not have a
4179 chance to do it, if we are going to skip any
4180 text at the beginning, which resets the
4181 FIRST_ELT flag. */
4182 bidi_paragraph_init (it->paragraph_embedding,
4183 &it->bidi_it, 1);
4187 bidi_move_to_visually_next (&it->bidi_it);
4189 while (it->stop_charpos <= it->bidi_it.charpos
4190 && it->bidi_it.charpos < newpos);
4191 IT_CHARPOS (*it) = it->bidi_it.charpos;
4192 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4193 /* If we overstepped NEWPOS, record its position in
4194 the iterator, so that we skip invisible text if
4195 later the bidi iteration lands us in the
4196 invisible region again. */
4197 if (IT_CHARPOS (*it) >= newpos)
4198 it->prev_stop = newpos;
4201 else
4203 IT_CHARPOS (*it) = newpos;
4204 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4207 /* If there are before-strings at the start of invisible
4208 text, and the text is invisible because of a text
4209 property, arrange to show before-strings because 20.x did
4210 it that way. (If the text is invisible because of an
4211 overlay property instead of a text property, this is
4212 already handled in the overlay code.) */
4213 if (NILP (overlay)
4214 && get_overlay_strings (it, it->stop_charpos))
4216 handled = HANDLED_RECOMPUTE_PROPS;
4217 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4219 else if (display_ellipsis_p)
4221 /* Make sure that the glyphs of the ellipsis will get
4222 correct `charpos' values. If we would not update
4223 it->position here, the glyphs would belong to the
4224 last visible character _before_ the invisible
4225 text, which confuses `set_cursor_from_row'.
4227 We use the last invisible position instead of the
4228 first because this way the cursor is always drawn on
4229 the first "." of the ellipsis, whenever PT is inside
4230 the invisible text. Otherwise the cursor would be
4231 placed _after_ the ellipsis when the point is after the
4232 first invisible character. */
4233 if (!STRINGP (it->object))
4235 it->position.charpos = newpos - 1;
4236 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4238 it->ellipsis_p = 1;
4239 /* Let the ellipsis display before
4240 considering any properties of the following char.
4241 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4242 handled = HANDLED_RETURN;
4247 return handled;
4251 /* Make iterator IT return `...' next.
4252 Replaces LEN characters from buffer. */
4254 static void
4255 setup_for_ellipsis (struct it *it, int len)
4257 /* Use the display table definition for `...'. Invalid glyphs
4258 will be handled by the method returning elements from dpvec. */
4259 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4261 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4262 it->dpvec = v->contents;
4263 it->dpend = v->contents + v->header.size;
4265 else
4267 /* Default `...'. */
4268 it->dpvec = default_invis_vector;
4269 it->dpend = default_invis_vector + 3;
4272 it->dpvec_char_len = len;
4273 it->current.dpvec_index = 0;
4274 it->dpvec_face_id = -1;
4276 /* Remember the current face id in case glyphs specify faces.
4277 IT's face is restored in set_iterator_to_next.
4278 saved_face_id was set to preceding char's face in handle_stop. */
4279 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4280 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4282 it->method = GET_FROM_DISPLAY_VECTOR;
4283 it->ellipsis_p = 1;
4288 /***********************************************************************
4289 'display' property
4290 ***********************************************************************/
4292 /* Set up iterator IT from `display' property at its current position.
4293 Called from handle_stop.
4294 We return HANDLED_RETURN if some part of the display property
4295 overrides the display of the buffer text itself.
4296 Otherwise we return HANDLED_NORMALLY. */
4298 static enum prop_handled
4299 handle_display_prop (struct it *it)
4301 Lisp_Object propval, object, overlay;
4302 struct text_pos *position;
4303 EMACS_INT bufpos;
4304 /* Nonzero if some property replaces the display of the text itself. */
4305 int display_replaced_p = 0;
4307 if (STRINGP (it->string))
4309 object = it->string;
4310 position = &it->current.string_pos;
4311 bufpos = CHARPOS (it->current.pos);
4313 else
4315 XSETWINDOW (object, it->w);
4316 position = &it->current.pos;
4317 bufpos = CHARPOS (*position);
4320 /* Reset those iterator values set from display property values. */
4321 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4322 it->space_width = Qnil;
4323 it->font_height = Qnil;
4324 it->voffset = 0;
4326 /* We don't support recursive `display' properties, i.e. string
4327 values that have a string `display' property, that have a string
4328 `display' property etc. */
4329 if (!it->string_from_display_prop_p)
4330 it->area = TEXT_AREA;
4332 propval = get_char_property_and_overlay (make_number (position->charpos),
4333 Qdisplay, object, &overlay);
4334 if (NILP (propval))
4335 return HANDLED_NORMALLY;
4336 /* Now OVERLAY is the overlay that gave us this property, or nil
4337 if it was a text property. */
4339 if (!STRINGP (it->string))
4340 object = it->w->buffer;
4342 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4343 position, bufpos,
4344 FRAME_WINDOW_P (it->f));
4346 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4349 /* Subroutine of handle_display_prop. Returns non-zero if the display
4350 specification in SPEC is a replacing specification, i.e. it would
4351 replace the text covered by `display' property with something else,
4352 such as an image or a display string. If SPEC includes any kind or
4353 `(space ...) specification, the value is 2; this is used by
4354 compute_display_string_pos, which see.
4356 See handle_single_display_spec for documentation of arguments.
4357 frame_window_p is non-zero if the window being redisplayed is on a
4358 GUI frame; this argument is used only if IT is NULL, see below.
4360 IT can be NULL, if this is called by the bidi reordering code
4361 through compute_display_string_pos, which see. In that case, this
4362 function only examines SPEC, but does not otherwise "handle" it, in
4363 the sense that it doesn't set up members of IT from the display
4364 spec. */
4365 static int
4366 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4367 Lisp_Object overlay, struct text_pos *position,
4368 EMACS_INT bufpos, int frame_window_p)
4370 int replacing_p = 0;
4371 int rv;
4373 if (CONSP (spec)
4374 /* Simple specifications. */
4375 && !EQ (XCAR (spec), Qimage)
4376 && !EQ (XCAR (spec), Qspace)
4377 && !EQ (XCAR (spec), Qwhen)
4378 && !EQ (XCAR (spec), Qslice)
4379 && !EQ (XCAR (spec), Qspace_width)
4380 && !EQ (XCAR (spec), Qheight)
4381 && !EQ (XCAR (spec), Qraise)
4382 /* Marginal area specifications. */
4383 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4384 && !EQ (XCAR (spec), Qleft_fringe)
4385 && !EQ (XCAR (spec), Qright_fringe)
4386 && !NILP (XCAR (spec)))
4388 for (; CONSP (spec); spec = XCDR (spec))
4390 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4391 overlay, position, bufpos,
4392 replacing_p, frame_window_p)))
4394 replacing_p = rv;
4395 /* If some text in a string is replaced, `position' no
4396 longer points to the position of `object'. */
4397 if (!it || STRINGP (object))
4398 break;
4402 else if (VECTORP (spec))
4404 int i;
4405 for (i = 0; i < ASIZE (spec); ++i)
4406 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4407 overlay, position, bufpos,
4408 replacing_p, frame_window_p)))
4410 replacing_p = rv;
4411 /* If some text in a string is replaced, `position' no
4412 longer points to the position of `object'. */
4413 if (!it || STRINGP (object))
4414 break;
4417 else
4419 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4420 position, bufpos, 0,
4421 frame_window_p)))
4422 replacing_p = rv;
4425 return replacing_p;
4428 /* Value is the position of the end of the `display' property starting
4429 at START_POS in OBJECT. */
4431 static struct text_pos
4432 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4434 Lisp_Object end;
4435 struct text_pos end_pos;
4437 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4438 Qdisplay, object, Qnil);
4439 CHARPOS (end_pos) = XFASTINT (end);
4440 if (STRINGP (object))
4441 compute_string_pos (&end_pos, start_pos, it->string);
4442 else
4443 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4445 return end_pos;
4449 /* Set up IT from a single `display' property specification SPEC. OBJECT
4450 is the object in which the `display' property was found. *POSITION
4451 is the position in OBJECT at which the `display' property was found.
4452 BUFPOS is the buffer position of OBJECT (different from POSITION if
4453 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4454 previously saw a display specification which already replaced text
4455 display with something else, for example an image; we ignore such
4456 properties after the first one has been processed.
4458 OVERLAY is the overlay this `display' property came from,
4459 or nil if it was a text property.
4461 If SPEC is a `space' or `image' specification, and in some other
4462 cases too, set *POSITION to the position where the `display'
4463 property ends.
4465 If IT is NULL, only examine the property specification in SPEC, but
4466 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4467 is intended to be displayed in a window on a GUI frame.
4469 Value is non-zero if something was found which replaces the display
4470 of buffer or string text. */
4472 static int
4473 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4474 Lisp_Object overlay, struct text_pos *position,
4475 EMACS_INT bufpos, int display_replaced_p,
4476 int frame_window_p)
4478 Lisp_Object form;
4479 Lisp_Object location, value;
4480 struct text_pos start_pos = *position;
4481 int valid_p;
4483 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4484 If the result is non-nil, use VALUE instead of SPEC. */
4485 form = Qt;
4486 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4488 spec = XCDR (spec);
4489 if (!CONSP (spec))
4490 return 0;
4491 form = XCAR (spec);
4492 spec = XCDR (spec);
4495 if (!NILP (form) && !EQ (form, Qt))
4497 int count = SPECPDL_INDEX ();
4498 struct gcpro gcpro1;
4500 /* Bind `object' to the object having the `display' property, a
4501 buffer or string. Bind `position' to the position in the
4502 object where the property was found, and `buffer-position'
4503 to the current position in the buffer. */
4505 if (NILP (object))
4506 XSETBUFFER (object, current_buffer);
4507 specbind (Qobject, object);
4508 specbind (Qposition, make_number (CHARPOS (*position)));
4509 specbind (Qbuffer_position, make_number (bufpos));
4510 GCPRO1 (form);
4511 form = safe_eval (form);
4512 UNGCPRO;
4513 unbind_to (count, Qnil);
4516 if (NILP (form))
4517 return 0;
4519 /* Handle `(height HEIGHT)' specifications. */
4520 if (CONSP (spec)
4521 && EQ (XCAR (spec), Qheight)
4522 && CONSP (XCDR (spec)))
4524 if (it)
4526 if (!FRAME_WINDOW_P (it->f))
4527 return 0;
4529 it->font_height = XCAR (XCDR (spec));
4530 if (!NILP (it->font_height))
4532 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4533 int new_height = -1;
4535 if (CONSP (it->font_height)
4536 && (EQ (XCAR (it->font_height), Qplus)
4537 || EQ (XCAR (it->font_height), Qminus))
4538 && CONSP (XCDR (it->font_height))
4539 && INTEGERP (XCAR (XCDR (it->font_height))))
4541 /* `(+ N)' or `(- N)' where N is an integer. */
4542 int steps = XINT (XCAR (XCDR (it->font_height)));
4543 if (EQ (XCAR (it->font_height), Qplus))
4544 steps = - steps;
4545 it->face_id = smaller_face (it->f, it->face_id, steps);
4547 else if (FUNCTIONP (it->font_height))
4549 /* Call function with current height as argument.
4550 Value is the new height. */
4551 Lisp_Object height;
4552 height = safe_call1 (it->font_height,
4553 face->lface[LFACE_HEIGHT_INDEX]);
4554 if (NUMBERP (height))
4555 new_height = XFLOATINT (height);
4557 else if (NUMBERP (it->font_height))
4559 /* Value is a multiple of the canonical char height. */
4560 struct face *f;
4562 f = FACE_FROM_ID (it->f,
4563 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4564 new_height = (XFLOATINT (it->font_height)
4565 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4567 else
4569 /* Evaluate IT->font_height with `height' bound to the
4570 current specified height to get the new height. */
4571 int count = SPECPDL_INDEX ();
4573 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4574 value = safe_eval (it->font_height);
4575 unbind_to (count, Qnil);
4577 if (NUMBERP (value))
4578 new_height = XFLOATINT (value);
4581 if (new_height > 0)
4582 it->face_id = face_with_height (it->f, it->face_id, new_height);
4586 return 0;
4589 /* Handle `(space-width WIDTH)'. */
4590 if (CONSP (spec)
4591 && EQ (XCAR (spec), Qspace_width)
4592 && CONSP (XCDR (spec)))
4594 if (it)
4596 if (!FRAME_WINDOW_P (it->f))
4597 return 0;
4599 value = XCAR (XCDR (spec));
4600 if (NUMBERP (value) && XFLOATINT (value) > 0)
4601 it->space_width = value;
4604 return 0;
4607 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4608 if (CONSP (spec)
4609 && EQ (XCAR (spec), Qslice))
4611 Lisp_Object tem;
4613 if (it)
4615 if (!FRAME_WINDOW_P (it->f))
4616 return 0;
4618 if (tem = XCDR (spec), CONSP (tem))
4620 it->slice.x = XCAR (tem);
4621 if (tem = XCDR (tem), CONSP (tem))
4623 it->slice.y = XCAR (tem);
4624 if (tem = XCDR (tem), CONSP (tem))
4626 it->slice.width = XCAR (tem);
4627 if (tem = XCDR (tem), CONSP (tem))
4628 it->slice.height = XCAR (tem);
4634 return 0;
4637 /* Handle `(raise FACTOR)'. */
4638 if (CONSP (spec)
4639 && EQ (XCAR (spec), Qraise)
4640 && CONSP (XCDR (spec)))
4642 if (it)
4644 if (!FRAME_WINDOW_P (it->f))
4645 return 0;
4647 #ifdef HAVE_WINDOW_SYSTEM
4648 value = XCAR (XCDR (spec));
4649 if (NUMBERP (value))
4651 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4652 it->voffset = - (XFLOATINT (value)
4653 * (FONT_HEIGHT (face->font)));
4655 #endif /* HAVE_WINDOW_SYSTEM */
4658 return 0;
4661 /* Don't handle the other kinds of display specifications
4662 inside a string that we got from a `display' property. */
4663 if (it && it->string_from_display_prop_p)
4664 return 0;
4666 /* Characters having this form of property are not displayed, so
4667 we have to find the end of the property. */
4668 if (it)
4670 start_pos = *position;
4671 *position = display_prop_end (it, object, start_pos);
4673 value = Qnil;
4675 /* Stop the scan at that end position--we assume that all
4676 text properties change there. */
4677 if (it)
4678 it->stop_charpos = position->charpos;
4680 /* Handle `(left-fringe BITMAP [FACE])'
4681 and `(right-fringe BITMAP [FACE])'. */
4682 if (CONSP (spec)
4683 && (EQ (XCAR (spec), Qleft_fringe)
4684 || EQ (XCAR (spec), Qright_fringe))
4685 && CONSP (XCDR (spec)))
4687 int fringe_bitmap;
4689 if (it)
4691 if (!FRAME_WINDOW_P (it->f))
4692 /* If we return here, POSITION has been advanced
4693 across the text with this property. */
4694 return 0;
4696 else if (!frame_window_p)
4697 return 0;
4699 #ifdef HAVE_WINDOW_SYSTEM
4700 value = XCAR (XCDR (spec));
4701 if (!SYMBOLP (value)
4702 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4703 /* If we return here, POSITION has been advanced
4704 across the text with this property. */
4705 return 0;
4707 if (it)
4709 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4711 if (CONSP (XCDR (XCDR (spec))))
4713 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4714 int face_id2 = lookup_derived_face (it->f, face_name,
4715 FRINGE_FACE_ID, 0);
4716 if (face_id2 >= 0)
4717 face_id = face_id2;
4720 /* Save current settings of IT so that we can restore them
4721 when we are finished with the glyph property value. */
4722 push_it (it, position);
4724 it->area = TEXT_AREA;
4725 it->what = IT_IMAGE;
4726 it->image_id = -1; /* no image */
4727 it->position = start_pos;
4728 it->object = NILP (object) ? it->w->buffer : object;
4729 it->method = GET_FROM_IMAGE;
4730 it->from_overlay = Qnil;
4731 it->face_id = face_id;
4732 it->from_disp_prop_p = 1;
4734 /* Say that we haven't consumed the characters with
4735 `display' property yet. The call to pop_it in
4736 set_iterator_to_next will clean this up. */
4737 *position = start_pos;
4739 if (EQ (XCAR (spec), Qleft_fringe))
4741 it->left_user_fringe_bitmap = fringe_bitmap;
4742 it->left_user_fringe_face_id = face_id;
4744 else
4746 it->right_user_fringe_bitmap = fringe_bitmap;
4747 it->right_user_fringe_face_id = face_id;
4750 #endif /* HAVE_WINDOW_SYSTEM */
4751 return 1;
4754 /* Prepare to handle `((margin left-margin) ...)',
4755 `((margin right-margin) ...)' and `((margin nil) ...)'
4756 prefixes for display specifications. */
4757 location = Qunbound;
4758 if (CONSP (spec) && CONSP (XCAR (spec)))
4760 Lisp_Object tem;
4762 value = XCDR (spec);
4763 if (CONSP (value))
4764 value = XCAR (value);
4766 tem = XCAR (spec);
4767 if (EQ (XCAR (tem), Qmargin)
4768 && (tem = XCDR (tem),
4769 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4770 (NILP (tem)
4771 || EQ (tem, Qleft_margin)
4772 || EQ (tem, Qright_margin))))
4773 location = tem;
4776 if (EQ (location, Qunbound))
4778 location = Qnil;
4779 value = spec;
4782 /* After this point, VALUE is the property after any
4783 margin prefix has been stripped. It must be a string,
4784 an image specification, or `(space ...)'.
4786 LOCATION specifies where to display: `left-margin',
4787 `right-margin' or nil. */
4789 valid_p = (STRINGP (value)
4790 #ifdef HAVE_WINDOW_SYSTEM
4791 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4792 && valid_image_p (value))
4793 #endif /* not HAVE_WINDOW_SYSTEM */
4794 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4796 if (valid_p && !display_replaced_p)
4798 int retval = 1;
4800 if (!it)
4802 /* Callers need to know whether the display spec is any kind
4803 of `(space ...)' spec that is about to affect text-area
4804 display. */
4805 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4806 retval = 2;
4807 return retval;
4810 /* Save current settings of IT so that we can restore them
4811 when we are finished with the glyph property value. */
4812 push_it (it, position);
4813 it->from_overlay = overlay;
4814 it->from_disp_prop_p = 1;
4816 if (NILP (location))
4817 it->area = TEXT_AREA;
4818 else if (EQ (location, Qleft_margin))
4819 it->area = LEFT_MARGIN_AREA;
4820 else
4821 it->area = RIGHT_MARGIN_AREA;
4823 if (STRINGP (value))
4825 it->string = value;
4826 it->multibyte_p = STRING_MULTIBYTE (it->string);
4827 it->current.overlay_string_index = -1;
4828 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4829 it->end_charpos = it->string_nchars = SCHARS (it->string);
4830 it->method = GET_FROM_STRING;
4831 it->stop_charpos = 0;
4832 it->prev_stop = 0;
4833 it->base_level_stop = 0;
4834 it->string_from_display_prop_p = 1;
4835 /* Say that we haven't consumed the characters with
4836 `display' property yet. The call to pop_it in
4837 set_iterator_to_next will clean this up. */
4838 if (BUFFERP (object))
4839 *position = start_pos;
4841 /* Force paragraph direction to be that of the parent
4842 object. If the parent object's paragraph direction is
4843 not yet determined, default to L2R. */
4844 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4845 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4846 else
4847 it->paragraph_embedding = L2R;
4849 /* Set up the bidi iterator for this display string. */
4850 if (it->bidi_p)
4852 it->bidi_it.string.lstring = it->string;
4853 it->bidi_it.string.s = NULL;
4854 it->bidi_it.string.schars = it->end_charpos;
4855 it->bidi_it.string.bufpos = bufpos;
4856 it->bidi_it.string.from_disp_str = 1;
4857 it->bidi_it.string.unibyte = !it->multibyte_p;
4858 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4861 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4863 it->method = GET_FROM_STRETCH;
4864 it->object = value;
4865 *position = it->position = start_pos;
4866 retval = 1 + (it->area == TEXT_AREA);
4868 #ifdef HAVE_WINDOW_SYSTEM
4869 else
4871 it->what = IT_IMAGE;
4872 it->image_id = lookup_image (it->f, value);
4873 it->position = start_pos;
4874 it->object = NILP (object) ? it->w->buffer : object;
4875 it->method = GET_FROM_IMAGE;
4877 /* Say that we haven't consumed the characters with
4878 `display' property yet. The call to pop_it in
4879 set_iterator_to_next will clean this up. */
4880 *position = start_pos;
4882 #endif /* HAVE_WINDOW_SYSTEM */
4884 return retval;
4887 /* Invalid property or property not supported. Restore
4888 POSITION to what it was before. */
4889 *position = start_pos;
4890 return 0;
4893 /* Check if PROP is a display property value whose text should be
4894 treated as intangible. OVERLAY is the overlay from which PROP
4895 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4896 specify the buffer position covered by PROP. */
4899 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4900 EMACS_INT charpos, EMACS_INT bytepos)
4902 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4903 struct text_pos position;
4905 SET_TEXT_POS (position, charpos, bytepos);
4906 return handle_display_spec (NULL, prop, Qnil, overlay,
4907 &position, charpos, frame_window_p);
4911 /* Return 1 if PROP is a display sub-property value containing STRING.
4913 Implementation note: this and the following function are really
4914 special cases of handle_display_spec and
4915 handle_single_display_spec, and should ideally use the same code.
4916 Until they do, these two pairs must be consistent and must be
4917 modified in sync. */
4919 static int
4920 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4922 if (EQ (string, prop))
4923 return 1;
4925 /* Skip over `when FORM'. */
4926 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4928 prop = XCDR (prop);
4929 if (!CONSP (prop))
4930 return 0;
4931 /* Actually, the condition following `when' should be eval'ed,
4932 like handle_single_display_spec does, and we should return
4933 zero if it evaluates to nil. However, this function is
4934 called only when the buffer was already displayed and some
4935 glyph in the glyph matrix was found to come from a display
4936 string. Therefore, the condition was already evaluated, and
4937 the result was non-nil, otherwise the display string wouldn't
4938 have been displayed and we would have never been called for
4939 this property. Thus, we can skip the evaluation and assume
4940 its result is non-nil. */
4941 prop = XCDR (prop);
4944 if (CONSP (prop))
4945 /* Skip over `margin LOCATION'. */
4946 if (EQ (XCAR (prop), Qmargin))
4948 prop = XCDR (prop);
4949 if (!CONSP (prop))
4950 return 0;
4952 prop = XCDR (prop);
4953 if (!CONSP (prop))
4954 return 0;
4957 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
4961 /* Return 1 if STRING appears in the `display' property PROP. */
4963 static int
4964 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4966 if (CONSP (prop)
4967 && !EQ (XCAR (prop), Qwhen)
4968 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
4970 /* A list of sub-properties. */
4971 while (CONSP (prop))
4973 if (single_display_spec_string_p (XCAR (prop), string))
4974 return 1;
4975 prop = XCDR (prop);
4978 else if (VECTORP (prop))
4980 /* A vector of sub-properties. */
4981 int i;
4982 for (i = 0; i < ASIZE (prop); ++i)
4983 if (single_display_spec_string_p (AREF (prop, i), string))
4984 return 1;
4986 else
4987 return single_display_spec_string_p (prop, string);
4989 return 0;
4992 /* Look for STRING in overlays and text properties in the current
4993 buffer, between character positions FROM and TO (excluding TO).
4994 BACK_P non-zero means look back (in this case, TO is supposed to be
4995 less than FROM).
4996 Value is the first character position where STRING was found, or
4997 zero if it wasn't found before hitting TO.
4999 This function may only use code that doesn't eval because it is
5000 called asynchronously from note_mouse_highlight. */
5002 static EMACS_INT
5003 string_buffer_position_lim (Lisp_Object string,
5004 EMACS_INT from, EMACS_INT to, int back_p)
5006 Lisp_Object limit, prop, pos;
5007 int found = 0;
5009 pos = make_number (max (from, BEGV));
5011 if (!back_p) /* looking forward */
5013 limit = make_number (min (to, ZV));
5014 while (!found && !EQ (pos, limit))
5016 prop = Fget_char_property (pos, Qdisplay, Qnil);
5017 if (!NILP (prop) && display_prop_string_p (prop, string))
5018 found = 1;
5019 else
5020 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5021 limit);
5024 else /* looking back */
5026 limit = make_number (max (to, BEGV));
5027 while (!found && !EQ (pos, limit))
5029 prop = Fget_char_property (pos, Qdisplay, Qnil);
5030 if (!NILP (prop) && display_prop_string_p (prop, string))
5031 found = 1;
5032 else
5033 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5034 limit);
5038 return found ? XINT (pos) : 0;
5041 /* Determine which buffer position in current buffer STRING comes from.
5042 AROUND_CHARPOS is an approximate position where it could come from.
5043 Value is the buffer position or 0 if it couldn't be determined.
5045 This function is necessary because we don't record buffer positions
5046 in glyphs generated from strings (to keep struct glyph small).
5047 This function may only use code that doesn't eval because it is
5048 called asynchronously from note_mouse_highlight. */
5050 static EMACS_INT
5051 string_buffer_position (Lisp_Object string, EMACS_INT around_charpos)
5053 const int MAX_DISTANCE = 1000;
5054 EMACS_INT found = string_buffer_position_lim (string, around_charpos,
5055 around_charpos + MAX_DISTANCE,
5058 if (!found)
5059 found = string_buffer_position_lim (string, around_charpos,
5060 around_charpos - MAX_DISTANCE, 1);
5061 return found;
5066 /***********************************************************************
5067 `composition' property
5068 ***********************************************************************/
5070 /* Set up iterator IT from `composition' property at its current
5071 position. Called from handle_stop. */
5073 static enum prop_handled
5074 handle_composition_prop (struct it *it)
5076 Lisp_Object prop, string;
5077 EMACS_INT pos, pos_byte, start, end;
5079 if (STRINGP (it->string))
5081 unsigned char *s;
5083 pos = IT_STRING_CHARPOS (*it);
5084 pos_byte = IT_STRING_BYTEPOS (*it);
5085 string = it->string;
5086 s = SDATA (string) + pos_byte;
5087 it->c = STRING_CHAR (s);
5089 else
5091 pos = IT_CHARPOS (*it);
5092 pos_byte = IT_BYTEPOS (*it);
5093 string = Qnil;
5094 it->c = FETCH_CHAR (pos_byte);
5097 /* If there's a valid composition and point is not inside of the
5098 composition (in the case that the composition is from the current
5099 buffer), draw a glyph composed from the composition components. */
5100 if (find_composition (pos, -1, &start, &end, &prop, string)
5101 && COMPOSITION_VALID_P (start, end, prop)
5102 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5104 if (start < pos)
5105 /* As we can't handle this situation (perhaps font-lock added
5106 a new composition), we just return here hoping that next
5107 redisplay will detect this composition much earlier. */
5108 return HANDLED_NORMALLY;
5109 if (start != pos)
5111 if (STRINGP (it->string))
5112 pos_byte = string_char_to_byte (it->string, start);
5113 else
5114 pos_byte = CHAR_TO_BYTE (start);
5116 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5117 prop, string);
5119 if (it->cmp_it.id >= 0)
5121 it->cmp_it.ch = -1;
5122 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5123 it->cmp_it.nglyphs = -1;
5127 return HANDLED_NORMALLY;
5132 /***********************************************************************
5133 Overlay strings
5134 ***********************************************************************/
5136 /* The following structure is used to record overlay strings for
5137 later sorting in load_overlay_strings. */
5139 struct overlay_entry
5141 Lisp_Object overlay;
5142 Lisp_Object string;
5143 int priority;
5144 int after_string_p;
5148 /* Set up iterator IT from overlay strings at its current position.
5149 Called from handle_stop. */
5151 static enum prop_handled
5152 handle_overlay_change (struct it *it)
5154 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5155 return HANDLED_RECOMPUTE_PROPS;
5156 else
5157 return HANDLED_NORMALLY;
5161 /* Set up the next overlay string for delivery by IT, if there is an
5162 overlay string to deliver. Called by set_iterator_to_next when the
5163 end of the current overlay string is reached. If there are more
5164 overlay strings to display, IT->string and
5165 IT->current.overlay_string_index are set appropriately here.
5166 Otherwise IT->string is set to nil. */
5168 static void
5169 next_overlay_string (struct it *it)
5171 ++it->current.overlay_string_index;
5172 if (it->current.overlay_string_index == it->n_overlay_strings)
5174 /* No more overlay strings. Restore IT's settings to what
5175 they were before overlay strings were processed, and
5176 continue to deliver from current_buffer. */
5178 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5179 pop_it (it);
5180 xassert (it->sp > 0
5181 || (NILP (it->string)
5182 && it->method == GET_FROM_BUFFER
5183 && it->stop_charpos >= BEGV
5184 && it->stop_charpos <= it->end_charpos));
5185 it->current.overlay_string_index = -1;
5186 it->n_overlay_strings = 0;
5187 it->overlay_strings_charpos = -1;
5188 /* If there's an empty display string on the stack, pop the
5189 stack, to resync the bidi iterator with IT's position. Such
5190 empty strings are pushed onto the stack in
5191 get_overlay_strings_1. */
5192 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5193 pop_it (it);
5195 /* If we're at the end of the buffer, record that we have
5196 processed the overlay strings there already, so that
5197 next_element_from_buffer doesn't try it again. */
5198 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5199 it->overlay_strings_at_end_processed_p = 1;
5201 else
5203 /* There are more overlay strings to process. If
5204 IT->current.overlay_string_index has advanced to a position
5205 where we must load IT->overlay_strings with more strings, do
5206 it. We must load at the IT->overlay_strings_charpos where
5207 IT->n_overlay_strings was originally computed; when invisible
5208 text is present, this might not be IT_CHARPOS (Bug#7016). */
5209 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5211 if (it->current.overlay_string_index && i == 0)
5212 load_overlay_strings (it, it->overlay_strings_charpos);
5214 /* Initialize IT to deliver display elements from the overlay
5215 string. */
5216 it->string = it->overlay_strings[i];
5217 it->multibyte_p = STRING_MULTIBYTE (it->string);
5218 SET_TEXT_POS (it->current.string_pos, 0, 0);
5219 it->method = GET_FROM_STRING;
5220 it->stop_charpos = 0;
5221 if (it->cmp_it.stop_pos >= 0)
5222 it->cmp_it.stop_pos = 0;
5223 it->prev_stop = 0;
5224 it->base_level_stop = 0;
5226 /* Set up the bidi iterator for this overlay string. */
5227 if (it->bidi_p)
5229 it->bidi_it.string.lstring = it->string;
5230 it->bidi_it.string.s = NULL;
5231 it->bidi_it.string.schars = SCHARS (it->string);
5232 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5233 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5234 it->bidi_it.string.unibyte = !it->multibyte_p;
5235 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5239 CHECK_IT (it);
5243 /* Compare two overlay_entry structures E1 and E2. Used as a
5244 comparison function for qsort in load_overlay_strings. Overlay
5245 strings for the same position are sorted so that
5247 1. All after-strings come in front of before-strings, except
5248 when they come from the same overlay.
5250 2. Within after-strings, strings are sorted so that overlay strings
5251 from overlays with higher priorities come first.
5253 2. Within before-strings, strings are sorted so that overlay
5254 strings from overlays with higher priorities come last.
5256 Value is analogous to strcmp. */
5259 static int
5260 compare_overlay_entries (const void *e1, const void *e2)
5262 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5263 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5264 int result;
5266 if (entry1->after_string_p != entry2->after_string_p)
5268 /* Let after-strings appear in front of before-strings if
5269 they come from different overlays. */
5270 if (EQ (entry1->overlay, entry2->overlay))
5271 result = entry1->after_string_p ? 1 : -1;
5272 else
5273 result = entry1->after_string_p ? -1 : 1;
5275 else if (entry1->after_string_p)
5276 /* After-strings sorted in order of decreasing priority. */
5277 result = entry2->priority - entry1->priority;
5278 else
5279 /* Before-strings sorted in order of increasing priority. */
5280 result = entry1->priority - entry2->priority;
5282 return result;
5286 /* Load the vector IT->overlay_strings with overlay strings from IT's
5287 current buffer position, or from CHARPOS if that is > 0. Set
5288 IT->n_overlays to the total number of overlay strings found.
5290 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5291 a time. On entry into load_overlay_strings,
5292 IT->current.overlay_string_index gives the number of overlay
5293 strings that have already been loaded by previous calls to this
5294 function.
5296 IT->add_overlay_start contains an additional overlay start
5297 position to consider for taking overlay strings from, if non-zero.
5298 This position comes into play when the overlay has an `invisible'
5299 property, and both before and after-strings. When we've skipped to
5300 the end of the overlay, because of its `invisible' property, we
5301 nevertheless want its before-string to appear.
5302 IT->add_overlay_start will contain the overlay start position
5303 in this case.
5305 Overlay strings are sorted so that after-string strings come in
5306 front of before-string strings. Within before and after-strings,
5307 strings are sorted by overlay priority. See also function
5308 compare_overlay_entries. */
5310 static void
5311 load_overlay_strings (struct it *it, EMACS_INT charpos)
5313 Lisp_Object overlay, window, str, invisible;
5314 struct Lisp_Overlay *ov;
5315 EMACS_INT start, end;
5316 int size = 20;
5317 int n = 0, i, j, invis_p;
5318 struct overlay_entry *entries
5319 = (struct overlay_entry *) alloca (size * sizeof *entries);
5321 if (charpos <= 0)
5322 charpos = IT_CHARPOS (*it);
5324 /* Append the overlay string STRING of overlay OVERLAY to vector
5325 `entries' which has size `size' and currently contains `n'
5326 elements. AFTER_P non-zero means STRING is an after-string of
5327 OVERLAY. */
5328 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5329 do \
5331 Lisp_Object priority; \
5333 if (n == size) \
5335 int new_size = 2 * size; \
5336 struct overlay_entry *old = entries; \
5337 entries = \
5338 (struct overlay_entry *) alloca (new_size \
5339 * sizeof *entries); \
5340 memcpy (entries, old, size * sizeof *entries); \
5341 size = new_size; \
5344 entries[n].string = (STRING); \
5345 entries[n].overlay = (OVERLAY); \
5346 priority = Foverlay_get ((OVERLAY), Qpriority); \
5347 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5348 entries[n].after_string_p = (AFTER_P); \
5349 ++n; \
5351 while (0)
5353 /* Process overlay before the overlay center. */
5354 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5356 XSETMISC (overlay, ov);
5357 xassert (OVERLAYP (overlay));
5358 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5359 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5361 if (end < charpos)
5362 break;
5364 /* Skip this overlay if it doesn't start or end at IT's current
5365 position. */
5366 if (end != charpos && start != charpos)
5367 continue;
5369 /* Skip this overlay if it doesn't apply to IT->w. */
5370 window = Foverlay_get (overlay, Qwindow);
5371 if (WINDOWP (window) && XWINDOW (window) != it->w)
5372 continue;
5374 /* If the text ``under'' the overlay is invisible, both before-
5375 and after-strings from this overlay are visible; start and
5376 end position are indistinguishable. */
5377 invisible = Foverlay_get (overlay, Qinvisible);
5378 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5380 /* If overlay has a non-empty before-string, record it. */
5381 if ((start == charpos || (end == charpos && invis_p))
5382 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5383 && SCHARS (str))
5384 RECORD_OVERLAY_STRING (overlay, str, 0);
5386 /* If overlay has a non-empty after-string, record it. */
5387 if ((end == charpos || (start == charpos && invis_p))
5388 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5389 && SCHARS (str))
5390 RECORD_OVERLAY_STRING (overlay, str, 1);
5393 /* Process overlays after the overlay center. */
5394 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5396 XSETMISC (overlay, ov);
5397 xassert (OVERLAYP (overlay));
5398 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5399 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5401 if (start > charpos)
5402 break;
5404 /* Skip this overlay if it doesn't start or end at IT's current
5405 position. */
5406 if (end != charpos && start != charpos)
5407 continue;
5409 /* Skip this overlay if it doesn't apply to IT->w. */
5410 window = Foverlay_get (overlay, Qwindow);
5411 if (WINDOWP (window) && XWINDOW (window) != it->w)
5412 continue;
5414 /* If the text ``under'' the overlay is invisible, it has a zero
5415 dimension, and both before- and after-strings apply. */
5416 invisible = Foverlay_get (overlay, Qinvisible);
5417 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5419 /* If overlay has a non-empty before-string, record it. */
5420 if ((start == charpos || (end == charpos && invis_p))
5421 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5422 && SCHARS (str))
5423 RECORD_OVERLAY_STRING (overlay, str, 0);
5425 /* If overlay has a non-empty after-string, record it. */
5426 if ((end == charpos || (start == charpos && invis_p))
5427 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5428 && SCHARS (str))
5429 RECORD_OVERLAY_STRING (overlay, str, 1);
5432 #undef RECORD_OVERLAY_STRING
5434 /* Sort entries. */
5435 if (n > 1)
5436 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5438 /* Record number of overlay strings, and where we computed it. */
5439 it->n_overlay_strings = n;
5440 it->overlay_strings_charpos = charpos;
5442 /* IT->current.overlay_string_index is the number of overlay strings
5443 that have already been consumed by IT. Copy some of the
5444 remaining overlay strings to IT->overlay_strings. */
5445 i = 0;
5446 j = it->current.overlay_string_index;
5447 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5449 it->overlay_strings[i] = entries[j].string;
5450 it->string_overlays[i++] = entries[j++].overlay;
5453 CHECK_IT (it);
5457 /* Get the first chunk of overlay strings at IT's current buffer
5458 position, or at CHARPOS if that is > 0. Value is non-zero if at
5459 least one overlay string was found. */
5461 static int
5462 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
5464 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5465 process. This fills IT->overlay_strings with strings, and sets
5466 IT->n_overlay_strings to the total number of strings to process.
5467 IT->pos.overlay_string_index has to be set temporarily to zero
5468 because load_overlay_strings needs this; it must be set to -1
5469 when no overlay strings are found because a zero value would
5470 indicate a position in the first overlay string. */
5471 it->current.overlay_string_index = 0;
5472 load_overlay_strings (it, charpos);
5474 /* If we found overlay strings, set up IT to deliver display
5475 elements from the first one. Otherwise set up IT to deliver
5476 from current_buffer. */
5477 if (it->n_overlay_strings)
5479 /* Make sure we know settings in current_buffer, so that we can
5480 restore meaningful values when we're done with the overlay
5481 strings. */
5482 if (compute_stop_p)
5483 compute_stop_pos (it);
5484 xassert (it->face_id >= 0);
5486 /* Save IT's settings. They are restored after all overlay
5487 strings have been processed. */
5488 xassert (!compute_stop_p || it->sp == 0);
5490 /* When called from handle_stop, there might be an empty display
5491 string loaded. In that case, don't bother saving it. But
5492 don't use this optimization with the bidi iterator, since we
5493 need the corresponding pop_it call to resync the bidi
5494 iterator's position with IT's position, after we are done
5495 with the overlay strings. (The corresponding call to pop_it
5496 in case of an empty display string is in
5497 next_overlay_string.) */
5498 if (!(!it->bidi_p
5499 && STRINGP (it->string) && !SCHARS (it->string)))
5500 push_it (it, NULL);
5502 /* Set up IT to deliver display elements from the first overlay
5503 string. */
5504 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5505 it->string = it->overlay_strings[0];
5506 it->from_overlay = Qnil;
5507 it->stop_charpos = 0;
5508 xassert (STRINGP (it->string));
5509 it->end_charpos = SCHARS (it->string);
5510 it->prev_stop = 0;
5511 it->base_level_stop = 0;
5512 it->multibyte_p = STRING_MULTIBYTE (it->string);
5513 it->method = GET_FROM_STRING;
5514 it->from_disp_prop_p = 0;
5516 /* Force paragraph direction to be that of the parent
5517 buffer. */
5518 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5519 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5520 else
5521 it->paragraph_embedding = L2R;
5523 /* Set up the bidi iterator for this overlay string. */
5524 if (it->bidi_p)
5526 EMACS_INT pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5528 it->bidi_it.string.lstring = it->string;
5529 it->bidi_it.string.s = NULL;
5530 it->bidi_it.string.schars = SCHARS (it->string);
5531 it->bidi_it.string.bufpos = pos;
5532 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5533 it->bidi_it.string.unibyte = !it->multibyte_p;
5534 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5536 return 1;
5539 it->current.overlay_string_index = -1;
5540 return 0;
5543 static int
5544 get_overlay_strings (struct it *it, EMACS_INT charpos)
5546 it->string = Qnil;
5547 it->method = GET_FROM_BUFFER;
5549 (void) get_overlay_strings_1 (it, charpos, 1);
5551 CHECK_IT (it);
5553 /* Value is non-zero if we found at least one overlay string. */
5554 return STRINGP (it->string);
5559 /***********************************************************************
5560 Saving and restoring state
5561 ***********************************************************************/
5563 /* Save current settings of IT on IT->stack. Called, for example,
5564 before setting up IT for an overlay string, to be able to restore
5565 IT's settings to what they were after the overlay string has been
5566 processed. If POSITION is non-NULL, it is the position to save on
5567 the stack instead of IT->position. */
5569 static void
5570 push_it (struct it *it, struct text_pos *position)
5572 struct iterator_stack_entry *p;
5574 xassert (it->sp < IT_STACK_SIZE);
5575 p = it->stack + it->sp;
5577 p->stop_charpos = it->stop_charpos;
5578 p->prev_stop = it->prev_stop;
5579 p->base_level_stop = it->base_level_stop;
5580 p->cmp_it = it->cmp_it;
5581 xassert (it->face_id >= 0);
5582 p->face_id = it->face_id;
5583 p->string = it->string;
5584 p->method = it->method;
5585 p->from_overlay = it->from_overlay;
5586 switch (p->method)
5588 case GET_FROM_IMAGE:
5589 p->u.image.object = it->object;
5590 p->u.image.image_id = it->image_id;
5591 p->u.image.slice = it->slice;
5592 break;
5593 case GET_FROM_STRETCH:
5594 p->u.stretch.object = it->object;
5595 break;
5597 p->position = position ? *position : it->position;
5598 p->current = it->current;
5599 p->end_charpos = it->end_charpos;
5600 p->string_nchars = it->string_nchars;
5601 p->area = it->area;
5602 p->multibyte_p = it->multibyte_p;
5603 p->avoid_cursor_p = it->avoid_cursor_p;
5604 p->space_width = it->space_width;
5605 p->font_height = it->font_height;
5606 p->voffset = it->voffset;
5607 p->string_from_display_prop_p = it->string_from_display_prop_p;
5608 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5609 p->display_ellipsis_p = 0;
5610 p->line_wrap = it->line_wrap;
5611 p->bidi_p = it->bidi_p;
5612 p->paragraph_embedding = it->paragraph_embedding;
5613 p->from_disp_prop_p = it->from_disp_prop_p;
5614 ++it->sp;
5616 /* Save the state of the bidi iterator as well. */
5617 if (it->bidi_p)
5618 bidi_push_it (&it->bidi_it);
5621 static void
5622 iterate_out_of_display_property (struct it *it)
5624 int buffer_p = BUFFERP (it->object);
5625 EMACS_INT eob = (buffer_p ? ZV : it->end_charpos);
5626 EMACS_INT bob = (buffer_p ? BEGV : 0);
5628 xassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5630 /* Maybe initialize paragraph direction. If we are at the beginning
5631 of a new paragraph, next_element_from_buffer may not have a
5632 chance to do that. */
5633 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5634 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5635 /* prev_stop can be zero, so check against BEGV as well. */
5636 while (it->bidi_it.charpos >= bob
5637 && it->prev_stop <= it->bidi_it.charpos
5638 && it->bidi_it.charpos < CHARPOS (it->position)
5639 && it->bidi_it.charpos < eob)
5640 bidi_move_to_visually_next (&it->bidi_it);
5641 /* Record the stop_pos we just crossed, for when we cross it
5642 back, maybe. */
5643 if (it->bidi_it.charpos > CHARPOS (it->position))
5644 it->prev_stop = CHARPOS (it->position);
5645 /* If we ended up not where pop_it put us, resync IT's
5646 positional members with the bidi iterator. */
5647 if (it->bidi_it.charpos != CHARPOS (it->position))
5648 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5649 if (buffer_p)
5650 it->current.pos = it->position;
5651 else
5652 it->current.string_pos = it->position;
5655 /* Restore IT's settings from IT->stack. Called, for example, when no
5656 more overlay strings must be processed, and we return to delivering
5657 display elements from a buffer, or when the end of a string from a
5658 `display' property is reached and we return to delivering display
5659 elements from an overlay string, or from a buffer. */
5661 static void
5662 pop_it (struct it *it)
5664 struct iterator_stack_entry *p;
5665 int from_display_prop = it->from_disp_prop_p;
5667 xassert (it->sp > 0);
5668 --it->sp;
5669 p = it->stack + it->sp;
5670 it->stop_charpos = p->stop_charpos;
5671 it->prev_stop = p->prev_stop;
5672 it->base_level_stop = p->base_level_stop;
5673 it->cmp_it = p->cmp_it;
5674 it->face_id = p->face_id;
5675 it->current = p->current;
5676 it->position = p->position;
5677 it->string = p->string;
5678 it->from_overlay = p->from_overlay;
5679 if (NILP (it->string))
5680 SET_TEXT_POS (it->current.string_pos, -1, -1);
5681 it->method = p->method;
5682 switch (it->method)
5684 case GET_FROM_IMAGE:
5685 it->image_id = p->u.image.image_id;
5686 it->object = p->u.image.object;
5687 it->slice = p->u.image.slice;
5688 break;
5689 case GET_FROM_STRETCH:
5690 it->object = p->u.stretch.object;
5691 break;
5692 case GET_FROM_BUFFER:
5693 it->object = it->w->buffer;
5694 break;
5695 case GET_FROM_STRING:
5696 it->object = it->string;
5697 break;
5698 case GET_FROM_DISPLAY_VECTOR:
5699 if (it->s)
5700 it->method = GET_FROM_C_STRING;
5701 else if (STRINGP (it->string))
5702 it->method = GET_FROM_STRING;
5703 else
5705 it->method = GET_FROM_BUFFER;
5706 it->object = it->w->buffer;
5709 it->end_charpos = p->end_charpos;
5710 it->string_nchars = p->string_nchars;
5711 it->area = p->area;
5712 it->multibyte_p = p->multibyte_p;
5713 it->avoid_cursor_p = p->avoid_cursor_p;
5714 it->space_width = p->space_width;
5715 it->font_height = p->font_height;
5716 it->voffset = p->voffset;
5717 it->string_from_display_prop_p = p->string_from_display_prop_p;
5718 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5719 it->line_wrap = p->line_wrap;
5720 it->bidi_p = p->bidi_p;
5721 it->paragraph_embedding = p->paragraph_embedding;
5722 it->from_disp_prop_p = p->from_disp_prop_p;
5723 if (it->bidi_p)
5725 bidi_pop_it (&it->bidi_it);
5726 /* Bidi-iterate until we get out of the portion of text, if any,
5727 covered by a `display' text property or by an overlay with
5728 `display' property. (We cannot just jump there, because the
5729 internal coherency of the bidi iterator state can not be
5730 preserved across such jumps.) We also must determine the
5731 paragraph base direction if the overlay we just processed is
5732 at the beginning of a new paragraph. */
5733 if (from_display_prop
5734 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5735 iterate_out_of_display_property (it);
5737 xassert ((BUFFERP (it->object)
5738 && IT_CHARPOS (*it) == it->bidi_it.charpos
5739 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5740 || (STRINGP (it->object)
5741 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5742 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5743 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5749 /***********************************************************************
5750 Moving over lines
5751 ***********************************************************************/
5753 /* Set IT's current position to the previous line start. */
5755 static void
5756 back_to_previous_line_start (struct it *it)
5758 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5759 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5763 /* Move IT to the next line start.
5765 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5766 we skipped over part of the text (as opposed to moving the iterator
5767 continuously over the text). Otherwise, don't change the value
5768 of *SKIPPED_P.
5770 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5771 iterator on the newline, if it was found.
5773 Newlines may come from buffer text, overlay strings, or strings
5774 displayed via the `display' property. That's the reason we can't
5775 simply use find_next_newline_no_quit.
5777 Note that this function may not skip over invisible text that is so
5778 because of text properties and immediately follows a newline. If
5779 it would, function reseat_at_next_visible_line_start, when called
5780 from set_iterator_to_next, would effectively make invisible
5781 characters following a newline part of the wrong glyph row, which
5782 leads to wrong cursor motion. */
5784 static int
5785 forward_to_next_line_start (struct it *it, int *skipped_p,
5786 struct bidi_it *bidi_it_prev)
5788 EMACS_INT old_selective;
5789 int newline_found_p, n;
5790 const int MAX_NEWLINE_DISTANCE = 500;
5792 /* If already on a newline, just consume it to avoid unintended
5793 skipping over invisible text below. */
5794 if (it->what == IT_CHARACTER
5795 && it->c == '\n'
5796 && CHARPOS (it->position) == IT_CHARPOS (*it))
5798 if (it->bidi_p && bidi_it_prev)
5799 *bidi_it_prev = it->bidi_it;
5800 set_iterator_to_next (it, 0);
5801 it->c = 0;
5802 return 1;
5805 /* Don't handle selective display in the following. It's (a)
5806 unnecessary because it's done by the caller, and (b) leads to an
5807 infinite recursion because next_element_from_ellipsis indirectly
5808 calls this function. */
5809 old_selective = it->selective;
5810 it->selective = 0;
5812 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5813 from buffer text. */
5814 for (n = newline_found_p = 0;
5815 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5816 n += STRINGP (it->string) ? 0 : 1)
5818 if (!get_next_display_element (it))
5819 return 0;
5820 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5821 if (newline_found_p && it->bidi_p && bidi_it_prev)
5822 *bidi_it_prev = it->bidi_it;
5823 set_iterator_to_next (it, 0);
5826 /* If we didn't find a newline near enough, see if we can use a
5827 short-cut. */
5828 if (!newline_found_p)
5830 EMACS_INT start = IT_CHARPOS (*it);
5831 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5832 Lisp_Object pos;
5834 xassert (!STRINGP (it->string));
5836 /* If there isn't any `display' property in sight, and no
5837 overlays, we can just use the position of the newline in
5838 buffer text. */
5839 if (it->stop_charpos >= limit
5840 || ((pos = Fnext_single_property_change (make_number (start),
5841 Qdisplay, Qnil,
5842 make_number (limit)),
5843 NILP (pos))
5844 && next_overlay_change (start) == ZV))
5846 if (!it->bidi_p)
5848 IT_CHARPOS (*it) = limit;
5849 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5851 else
5853 struct bidi_it bprev;
5855 /* Help bidi.c avoid expensive searches for display
5856 properties and overlays, by telling it that there are
5857 none up to `limit'. */
5858 if (it->bidi_it.disp_pos < limit)
5860 it->bidi_it.disp_pos = limit;
5861 it->bidi_it.disp_prop = 0;
5863 do {
5864 bprev = it->bidi_it;
5865 bidi_move_to_visually_next (&it->bidi_it);
5866 } while (it->bidi_it.charpos != limit);
5867 IT_CHARPOS (*it) = limit;
5868 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5869 if (bidi_it_prev)
5870 *bidi_it_prev = bprev;
5872 *skipped_p = newline_found_p = 1;
5874 else
5876 while (get_next_display_element (it)
5877 && !newline_found_p)
5879 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5880 if (newline_found_p && it->bidi_p && bidi_it_prev)
5881 *bidi_it_prev = it->bidi_it;
5882 set_iterator_to_next (it, 0);
5887 it->selective = old_selective;
5888 return newline_found_p;
5892 /* Set IT's current position to the previous visible line start. Skip
5893 invisible text that is so either due to text properties or due to
5894 selective display. Caution: this does not change IT->current_x and
5895 IT->hpos. */
5897 static void
5898 back_to_previous_visible_line_start (struct it *it)
5900 while (IT_CHARPOS (*it) > BEGV)
5902 back_to_previous_line_start (it);
5904 if (IT_CHARPOS (*it) <= BEGV)
5905 break;
5907 /* If selective > 0, then lines indented more than its value are
5908 invisible. */
5909 if (it->selective > 0
5910 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5911 it->selective))
5912 continue;
5914 /* Check the newline before point for invisibility. */
5916 Lisp_Object prop;
5917 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5918 Qinvisible, it->window);
5919 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5920 continue;
5923 if (IT_CHARPOS (*it) <= BEGV)
5924 break;
5927 struct it it2;
5928 void *it2data = NULL;
5929 EMACS_INT pos;
5930 EMACS_INT beg, end;
5931 Lisp_Object val, overlay;
5933 SAVE_IT (it2, *it, it2data);
5935 /* If newline is part of a composition, continue from start of composition */
5936 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5937 && beg < IT_CHARPOS (*it))
5938 goto replaced;
5940 /* If newline is replaced by a display property, find start of overlay
5941 or interval and continue search from that point. */
5942 pos = --IT_CHARPOS (it2);
5943 --IT_BYTEPOS (it2);
5944 it2.sp = 0;
5945 bidi_unshelve_cache (NULL, 0);
5946 it2.string_from_display_prop_p = 0;
5947 it2.from_disp_prop_p = 0;
5948 if (handle_display_prop (&it2) == HANDLED_RETURN
5949 && !NILP (val = get_char_property_and_overlay
5950 (make_number (pos), Qdisplay, Qnil, &overlay))
5951 && (OVERLAYP (overlay)
5952 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5953 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5955 RESTORE_IT (it, it, it2data);
5956 goto replaced;
5959 /* Newline is not replaced by anything -- so we are done. */
5960 RESTORE_IT (it, it, it2data);
5961 break;
5963 replaced:
5964 if (beg < BEGV)
5965 beg = BEGV;
5966 IT_CHARPOS (*it) = beg;
5967 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5971 it->continuation_lines_width = 0;
5973 xassert (IT_CHARPOS (*it) >= BEGV);
5974 xassert (IT_CHARPOS (*it) == BEGV
5975 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5976 CHECK_IT (it);
5980 /* Reseat iterator IT at the previous visible line start. Skip
5981 invisible text that is so either due to text properties or due to
5982 selective display. At the end, update IT's overlay information,
5983 face information etc. */
5985 void
5986 reseat_at_previous_visible_line_start (struct it *it)
5988 back_to_previous_visible_line_start (it);
5989 reseat (it, it->current.pos, 1);
5990 CHECK_IT (it);
5994 /* Reseat iterator IT on the next visible line start in the current
5995 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5996 preceding the line start. Skip over invisible text that is so
5997 because of selective display. Compute faces, overlays etc at the
5998 new position. Note that this function does not skip over text that
5999 is invisible because of text properties. */
6001 static void
6002 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6004 int newline_found_p, skipped_p = 0;
6005 struct bidi_it bidi_it_prev;
6007 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6009 /* Skip over lines that are invisible because they are indented
6010 more than the value of IT->selective. */
6011 if (it->selective > 0)
6012 while (IT_CHARPOS (*it) < ZV
6013 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6014 it->selective))
6016 xassert (IT_BYTEPOS (*it) == BEGV
6017 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6018 newline_found_p =
6019 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6022 /* Position on the newline if that's what's requested. */
6023 if (on_newline_p && newline_found_p)
6025 if (STRINGP (it->string))
6027 if (IT_STRING_CHARPOS (*it) > 0)
6029 if (!it->bidi_p)
6031 --IT_STRING_CHARPOS (*it);
6032 --IT_STRING_BYTEPOS (*it);
6034 else
6036 /* We need to restore the bidi iterator to the state
6037 it had on the newline, and resync the IT's
6038 position with that. */
6039 it->bidi_it = bidi_it_prev;
6040 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6041 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6045 else if (IT_CHARPOS (*it) > BEGV)
6047 if (!it->bidi_p)
6049 --IT_CHARPOS (*it);
6050 --IT_BYTEPOS (*it);
6052 else
6054 /* We need to restore the bidi iterator to the state it
6055 had on the newline and resync IT with that. */
6056 it->bidi_it = bidi_it_prev;
6057 IT_CHARPOS (*it) = it->bidi_it.charpos;
6058 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6060 reseat (it, it->current.pos, 0);
6063 else if (skipped_p)
6064 reseat (it, it->current.pos, 0);
6066 CHECK_IT (it);
6071 /***********************************************************************
6072 Changing an iterator's position
6073 ***********************************************************************/
6075 /* Change IT's current position to POS in current_buffer. If FORCE_P
6076 is non-zero, always check for text properties at the new position.
6077 Otherwise, text properties are only looked up if POS >=
6078 IT->check_charpos of a property. */
6080 static void
6081 reseat (struct it *it, struct text_pos pos, int force_p)
6083 EMACS_INT original_pos = IT_CHARPOS (*it);
6085 reseat_1 (it, pos, 0);
6087 /* Determine where to check text properties. Avoid doing it
6088 where possible because text property lookup is very expensive. */
6089 if (force_p
6090 || CHARPOS (pos) > it->stop_charpos
6091 || CHARPOS (pos) < original_pos)
6093 if (it->bidi_p)
6095 /* For bidi iteration, we need to prime prev_stop and
6096 base_level_stop with our best estimations. */
6097 /* Implementation note: Of course, POS is not necessarily a
6098 stop position, so assigning prev_pos to it is a lie; we
6099 should have called compute_stop_backwards. However, if
6100 the current buffer does not include any R2L characters,
6101 that call would be a waste of cycles, because the
6102 iterator will never move back, and thus never cross this
6103 "fake" stop position. So we delay that backward search
6104 until the time we really need it, in next_element_from_buffer. */
6105 if (CHARPOS (pos) != it->prev_stop)
6106 it->prev_stop = CHARPOS (pos);
6107 if (CHARPOS (pos) < it->base_level_stop)
6108 it->base_level_stop = 0; /* meaning it's unknown */
6109 handle_stop (it);
6111 else
6113 handle_stop (it);
6114 it->prev_stop = it->base_level_stop = 0;
6119 CHECK_IT (it);
6123 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6124 IT->stop_pos to POS, also. */
6126 static void
6127 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6129 /* Don't call this function when scanning a C string. */
6130 xassert (it->s == NULL);
6132 /* POS must be a reasonable value. */
6133 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6135 it->current.pos = it->position = pos;
6136 it->end_charpos = ZV;
6137 it->dpvec = NULL;
6138 it->current.dpvec_index = -1;
6139 it->current.overlay_string_index = -1;
6140 IT_STRING_CHARPOS (*it) = -1;
6141 IT_STRING_BYTEPOS (*it) = -1;
6142 it->string = Qnil;
6143 it->method = GET_FROM_BUFFER;
6144 it->object = it->w->buffer;
6145 it->area = TEXT_AREA;
6146 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6147 it->sp = 0;
6148 it->string_from_display_prop_p = 0;
6149 it->string_from_prefix_prop_p = 0;
6151 it->from_disp_prop_p = 0;
6152 it->face_before_selective_p = 0;
6153 if (it->bidi_p)
6155 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6156 &it->bidi_it);
6157 bidi_unshelve_cache (NULL, 0);
6158 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6159 it->bidi_it.string.s = NULL;
6160 it->bidi_it.string.lstring = Qnil;
6161 it->bidi_it.string.bufpos = 0;
6162 it->bidi_it.string.unibyte = 0;
6165 if (set_stop_p)
6167 it->stop_charpos = CHARPOS (pos);
6168 it->base_level_stop = CHARPOS (pos);
6173 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6174 If S is non-null, it is a C string to iterate over. Otherwise,
6175 STRING gives a Lisp string to iterate over.
6177 If PRECISION > 0, don't return more then PRECISION number of
6178 characters from the string.
6180 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6181 characters have been returned. FIELD_WIDTH < 0 means an infinite
6182 field width.
6184 MULTIBYTE = 0 means disable processing of multibyte characters,
6185 MULTIBYTE > 0 means enable it,
6186 MULTIBYTE < 0 means use IT->multibyte_p.
6188 IT must be initialized via a prior call to init_iterator before
6189 calling this function. */
6191 static void
6192 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6193 EMACS_INT charpos, EMACS_INT precision, int field_width,
6194 int multibyte)
6196 /* No region in strings. */
6197 it->region_beg_charpos = it->region_end_charpos = -1;
6199 /* No text property checks performed by default, but see below. */
6200 it->stop_charpos = -1;
6202 /* Set iterator position and end position. */
6203 memset (&it->current, 0, sizeof it->current);
6204 it->current.overlay_string_index = -1;
6205 it->current.dpvec_index = -1;
6206 xassert (charpos >= 0);
6208 /* If STRING is specified, use its multibyteness, otherwise use the
6209 setting of MULTIBYTE, if specified. */
6210 if (multibyte >= 0)
6211 it->multibyte_p = multibyte > 0;
6213 /* Bidirectional reordering of strings is controlled by the default
6214 value of bidi-display-reordering. Don't try to reorder while
6215 loading loadup.el, as the necessary character property tables are
6216 not yet available. */
6217 it->bidi_p =
6218 NILP (Vpurify_flag)
6219 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6221 if (s == NULL)
6223 xassert (STRINGP (string));
6224 it->string = string;
6225 it->s = NULL;
6226 it->end_charpos = it->string_nchars = SCHARS (string);
6227 it->method = GET_FROM_STRING;
6228 it->current.string_pos = string_pos (charpos, string);
6230 if (it->bidi_p)
6232 it->bidi_it.string.lstring = string;
6233 it->bidi_it.string.s = NULL;
6234 it->bidi_it.string.schars = it->end_charpos;
6235 it->bidi_it.string.bufpos = 0;
6236 it->bidi_it.string.from_disp_str = 0;
6237 it->bidi_it.string.unibyte = !it->multibyte_p;
6238 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6239 FRAME_WINDOW_P (it->f), &it->bidi_it);
6242 else
6244 it->s = (const unsigned char *) s;
6245 it->string = Qnil;
6247 /* Note that we use IT->current.pos, not it->current.string_pos,
6248 for displaying C strings. */
6249 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6250 if (it->multibyte_p)
6252 it->current.pos = c_string_pos (charpos, s, 1);
6253 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6255 else
6257 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6258 it->end_charpos = it->string_nchars = strlen (s);
6261 if (it->bidi_p)
6263 it->bidi_it.string.lstring = Qnil;
6264 it->bidi_it.string.s = (const unsigned char *) s;
6265 it->bidi_it.string.schars = it->end_charpos;
6266 it->bidi_it.string.bufpos = 0;
6267 it->bidi_it.string.from_disp_str = 0;
6268 it->bidi_it.string.unibyte = !it->multibyte_p;
6269 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6270 &it->bidi_it);
6272 it->method = GET_FROM_C_STRING;
6275 /* PRECISION > 0 means don't return more than PRECISION characters
6276 from the string. */
6277 if (precision > 0 && it->end_charpos - charpos > precision)
6279 it->end_charpos = it->string_nchars = charpos + precision;
6280 if (it->bidi_p)
6281 it->bidi_it.string.schars = it->end_charpos;
6284 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6285 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6286 FIELD_WIDTH < 0 means infinite field width. This is useful for
6287 padding with `-' at the end of a mode line. */
6288 if (field_width < 0)
6289 field_width = INFINITY;
6290 /* Implementation note: We deliberately don't enlarge
6291 it->bidi_it.string.schars here to fit it->end_charpos, because
6292 the bidi iterator cannot produce characters out of thin air. */
6293 if (field_width > it->end_charpos - charpos)
6294 it->end_charpos = charpos + field_width;
6296 /* Use the standard display table for displaying strings. */
6297 if (DISP_TABLE_P (Vstandard_display_table))
6298 it->dp = XCHAR_TABLE (Vstandard_display_table);
6300 it->stop_charpos = charpos;
6301 it->prev_stop = charpos;
6302 it->base_level_stop = 0;
6303 if (it->bidi_p)
6305 it->bidi_it.first_elt = 1;
6306 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6307 it->bidi_it.disp_pos = -1;
6309 if (s == NULL && it->multibyte_p)
6311 EMACS_INT endpos = SCHARS (it->string);
6312 if (endpos > it->end_charpos)
6313 endpos = it->end_charpos;
6314 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6315 it->string);
6317 CHECK_IT (it);
6322 /***********************************************************************
6323 Iteration
6324 ***********************************************************************/
6326 /* Map enum it_method value to corresponding next_element_from_* function. */
6328 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6330 next_element_from_buffer,
6331 next_element_from_display_vector,
6332 next_element_from_string,
6333 next_element_from_c_string,
6334 next_element_from_image,
6335 next_element_from_stretch
6338 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6341 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6342 (possibly with the following characters). */
6344 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6345 ((IT)->cmp_it.id >= 0 \
6346 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6347 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6348 END_CHARPOS, (IT)->w, \
6349 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6350 (IT)->string)))
6353 /* Lookup the char-table Vglyphless_char_display for character C (-1
6354 if we want information for no-font case), and return the display
6355 method symbol. By side-effect, update it->what and
6356 it->glyphless_method. This function is called from
6357 get_next_display_element for each character element, and from
6358 x_produce_glyphs when no suitable font was found. */
6360 Lisp_Object
6361 lookup_glyphless_char_display (int c, struct it *it)
6363 Lisp_Object glyphless_method = Qnil;
6365 if (CHAR_TABLE_P (Vglyphless_char_display)
6366 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6368 if (c >= 0)
6370 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6371 if (CONSP (glyphless_method))
6372 glyphless_method = FRAME_WINDOW_P (it->f)
6373 ? XCAR (glyphless_method)
6374 : XCDR (glyphless_method);
6376 else
6377 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6380 retry:
6381 if (NILP (glyphless_method))
6383 if (c >= 0)
6384 /* The default is to display the character by a proper font. */
6385 return Qnil;
6386 /* The default for the no-font case is to display an empty box. */
6387 glyphless_method = Qempty_box;
6389 if (EQ (glyphless_method, Qzero_width))
6391 if (c >= 0)
6392 return glyphless_method;
6393 /* This method can't be used for the no-font case. */
6394 glyphless_method = Qempty_box;
6396 if (EQ (glyphless_method, Qthin_space))
6397 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6398 else if (EQ (glyphless_method, Qempty_box))
6399 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6400 else if (EQ (glyphless_method, Qhex_code))
6401 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6402 else if (STRINGP (glyphless_method))
6403 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6404 else
6406 /* Invalid value. We use the default method. */
6407 glyphless_method = Qnil;
6408 goto retry;
6410 it->what = IT_GLYPHLESS;
6411 return glyphless_method;
6414 /* Load IT's display element fields with information about the next
6415 display element from the current position of IT. Value is zero if
6416 end of buffer (or C string) is reached. */
6418 static struct frame *last_escape_glyph_frame = NULL;
6419 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6420 static int last_escape_glyph_merged_face_id = 0;
6422 struct frame *last_glyphless_glyph_frame = NULL;
6423 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6424 int last_glyphless_glyph_merged_face_id = 0;
6426 static int
6427 get_next_display_element (struct it *it)
6429 /* Non-zero means that we found a display element. Zero means that
6430 we hit the end of what we iterate over. Performance note: the
6431 function pointer `method' used here turns out to be faster than
6432 using a sequence of if-statements. */
6433 int success_p;
6435 get_next:
6436 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6438 if (it->what == IT_CHARACTER)
6440 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6441 and only if (a) the resolved directionality of that character
6442 is R..." */
6443 /* FIXME: Do we need an exception for characters from display
6444 tables? */
6445 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6446 it->c = bidi_mirror_char (it->c);
6447 /* Map via display table or translate control characters.
6448 IT->c, IT->len etc. have been set to the next character by
6449 the function call above. If we have a display table, and it
6450 contains an entry for IT->c, translate it. Don't do this if
6451 IT->c itself comes from a display table, otherwise we could
6452 end up in an infinite recursion. (An alternative could be to
6453 count the recursion depth of this function and signal an
6454 error when a certain maximum depth is reached.) Is it worth
6455 it? */
6456 if (success_p && it->dpvec == NULL)
6458 Lisp_Object dv;
6459 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6460 int nonascii_space_p = 0;
6461 int nonascii_hyphen_p = 0;
6462 int c = it->c; /* This is the character to display. */
6464 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6466 xassert (SINGLE_BYTE_CHAR_P (c));
6467 if (unibyte_display_via_language_environment)
6469 c = DECODE_CHAR (unibyte, c);
6470 if (c < 0)
6471 c = BYTE8_TO_CHAR (it->c);
6473 else
6474 c = BYTE8_TO_CHAR (it->c);
6477 if (it->dp
6478 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6479 VECTORP (dv)))
6481 struct Lisp_Vector *v = XVECTOR (dv);
6483 /* Return the first character from the display table
6484 entry, if not empty. If empty, don't display the
6485 current character. */
6486 if (v->header.size)
6488 it->dpvec_char_len = it->len;
6489 it->dpvec = v->contents;
6490 it->dpend = v->contents + v->header.size;
6491 it->current.dpvec_index = 0;
6492 it->dpvec_face_id = -1;
6493 it->saved_face_id = it->face_id;
6494 it->method = GET_FROM_DISPLAY_VECTOR;
6495 it->ellipsis_p = 0;
6497 else
6499 set_iterator_to_next (it, 0);
6501 goto get_next;
6504 if (! NILP (lookup_glyphless_char_display (c, it)))
6506 if (it->what == IT_GLYPHLESS)
6507 goto done;
6508 /* Don't display this character. */
6509 set_iterator_to_next (it, 0);
6510 goto get_next;
6513 /* If `nobreak-char-display' is non-nil, we display
6514 non-ASCII spaces and hyphens specially. */
6515 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6517 if (c == 0xA0)
6518 nonascii_space_p = 1;
6519 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6520 nonascii_hyphen_p = 1;
6523 /* Translate control characters into `\003' or `^C' form.
6524 Control characters coming from a display table entry are
6525 currently not translated because we use IT->dpvec to hold
6526 the translation. This could easily be changed but I
6527 don't believe that it is worth doing.
6529 The characters handled by `nobreak-char-display' must be
6530 translated too.
6532 Non-printable characters and raw-byte characters are also
6533 translated to octal form. */
6534 if (((c < ' ' || c == 127) /* ASCII control chars */
6535 ? (it->area != TEXT_AREA
6536 /* In mode line, treat \n, \t like other crl chars. */
6537 || (c != '\t'
6538 && it->glyph_row
6539 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6540 || (c != '\n' && c != '\t'))
6541 : (nonascii_space_p
6542 || nonascii_hyphen_p
6543 || CHAR_BYTE8_P (c)
6544 || ! CHAR_PRINTABLE_P (c))))
6546 /* C is a control character, non-ASCII space/hyphen,
6547 raw-byte, or a non-printable character which must be
6548 displayed either as '\003' or as `^C' where the '\\'
6549 and '^' can be defined in the display table. Fill
6550 IT->ctl_chars with glyphs for what we have to
6551 display. Then, set IT->dpvec to these glyphs. */
6552 Lisp_Object gc;
6553 int ctl_len;
6554 int face_id;
6555 EMACS_INT lface_id = 0;
6556 int escape_glyph;
6558 /* Handle control characters with ^. */
6560 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6562 int g;
6564 g = '^'; /* default glyph for Control */
6565 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6566 if (it->dp
6567 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
6568 && GLYPH_CODE_CHAR_VALID_P (gc))
6570 g = GLYPH_CODE_CHAR (gc);
6571 lface_id = GLYPH_CODE_FACE (gc);
6573 if (lface_id)
6575 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6577 else if (it->f == last_escape_glyph_frame
6578 && it->face_id == last_escape_glyph_face_id)
6580 face_id = last_escape_glyph_merged_face_id;
6582 else
6584 /* Merge the escape-glyph face into the current face. */
6585 face_id = merge_faces (it->f, Qescape_glyph, 0,
6586 it->face_id);
6587 last_escape_glyph_frame = it->f;
6588 last_escape_glyph_face_id = it->face_id;
6589 last_escape_glyph_merged_face_id = face_id;
6592 XSETINT (it->ctl_chars[0], g);
6593 XSETINT (it->ctl_chars[1], c ^ 0100);
6594 ctl_len = 2;
6595 goto display_control;
6598 /* Handle non-ascii space in the mode where it only gets
6599 highlighting. */
6601 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6603 /* Merge `nobreak-space' into the current face. */
6604 face_id = merge_faces (it->f, Qnobreak_space, 0,
6605 it->face_id);
6606 XSETINT (it->ctl_chars[0], ' ');
6607 ctl_len = 1;
6608 goto display_control;
6611 /* Handle sequences that start with the "escape glyph". */
6613 /* the default escape glyph is \. */
6614 escape_glyph = '\\';
6616 if (it->dp
6617 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
6618 && GLYPH_CODE_CHAR_VALID_P (gc))
6620 escape_glyph = GLYPH_CODE_CHAR (gc);
6621 lface_id = GLYPH_CODE_FACE (gc);
6623 if (lface_id)
6625 /* The display table specified a face.
6626 Merge it into face_id and also into escape_glyph. */
6627 face_id = merge_faces (it->f, Qt, lface_id,
6628 it->face_id);
6630 else if (it->f == last_escape_glyph_frame
6631 && it->face_id == last_escape_glyph_face_id)
6633 face_id = last_escape_glyph_merged_face_id;
6635 else
6637 /* Merge the escape-glyph face into the current face. */
6638 face_id = merge_faces (it->f, Qescape_glyph, 0,
6639 it->face_id);
6640 last_escape_glyph_frame = it->f;
6641 last_escape_glyph_face_id = it->face_id;
6642 last_escape_glyph_merged_face_id = face_id;
6645 /* Draw non-ASCII hyphen with just highlighting: */
6647 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6649 XSETINT (it->ctl_chars[0], '-');
6650 ctl_len = 1;
6651 goto display_control;
6654 /* Draw non-ASCII space/hyphen with escape glyph: */
6656 if (nonascii_space_p || nonascii_hyphen_p)
6658 XSETINT (it->ctl_chars[0], escape_glyph);
6659 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6660 ctl_len = 2;
6661 goto display_control;
6665 char str[10];
6666 int len, i;
6668 if (CHAR_BYTE8_P (c))
6669 /* Display \200 instead of \17777600. */
6670 c = CHAR_TO_BYTE8 (c);
6671 len = sprintf (str, "%03o", c);
6673 XSETINT (it->ctl_chars[0], escape_glyph);
6674 for (i = 0; i < len; i++)
6675 XSETINT (it->ctl_chars[i + 1], str[i]);
6676 ctl_len = len + 1;
6679 display_control:
6680 /* Set up IT->dpvec and return first character from it. */
6681 it->dpvec_char_len = it->len;
6682 it->dpvec = it->ctl_chars;
6683 it->dpend = it->dpvec + ctl_len;
6684 it->current.dpvec_index = 0;
6685 it->dpvec_face_id = face_id;
6686 it->saved_face_id = it->face_id;
6687 it->method = GET_FROM_DISPLAY_VECTOR;
6688 it->ellipsis_p = 0;
6689 goto get_next;
6691 it->char_to_display = c;
6693 else if (success_p)
6695 it->char_to_display = it->c;
6699 /* Adjust face id for a multibyte character. There are no multibyte
6700 character in unibyte text. */
6701 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6702 && it->multibyte_p
6703 && success_p
6704 && FRAME_WINDOW_P (it->f))
6706 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6708 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6710 /* Automatic composition with glyph-string. */
6711 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6713 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6715 else
6717 EMACS_INT pos = (it->s ? -1
6718 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6719 : IT_CHARPOS (*it));
6720 int c;
6722 if (it->what == IT_CHARACTER)
6723 c = it->char_to_display;
6724 else
6726 struct composition *cmp = composition_table[it->cmp_it.id];
6727 int i;
6729 c = ' ';
6730 for (i = 0; i < cmp->glyph_len; i++)
6731 /* TAB in a composition means display glyphs with
6732 padding space on the left or right. */
6733 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6734 break;
6736 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6740 done:
6741 /* Is this character the last one of a run of characters with
6742 box? If yes, set IT->end_of_box_run_p to 1. */
6743 if (it->face_box_p
6744 && it->s == NULL)
6746 if (it->method == GET_FROM_STRING && it->sp)
6748 int face_id = underlying_face_id (it);
6749 struct face *face = FACE_FROM_ID (it->f, face_id);
6751 if (face)
6753 if (face->box == FACE_NO_BOX)
6755 /* If the box comes from face properties in a
6756 display string, check faces in that string. */
6757 int string_face_id = face_after_it_pos (it);
6758 it->end_of_box_run_p
6759 = (FACE_FROM_ID (it->f, string_face_id)->box
6760 == FACE_NO_BOX);
6762 /* Otherwise, the box comes from the underlying face.
6763 If this is the last string character displayed, check
6764 the next buffer location. */
6765 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6766 && (it->current.overlay_string_index
6767 == it->n_overlay_strings - 1))
6769 EMACS_INT ignore;
6770 int next_face_id;
6771 struct text_pos pos = it->current.pos;
6772 INC_TEXT_POS (pos, it->multibyte_p);
6774 next_face_id = face_at_buffer_position
6775 (it->w, CHARPOS (pos), it->region_beg_charpos,
6776 it->region_end_charpos, &ignore,
6777 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6778 -1);
6779 it->end_of_box_run_p
6780 = (FACE_FROM_ID (it->f, next_face_id)->box
6781 == FACE_NO_BOX);
6785 else
6787 int face_id = face_after_it_pos (it);
6788 it->end_of_box_run_p
6789 = (face_id != it->face_id
6790 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6794 /* Value is 0 if end of buffer or string reached. */
6795 return success_p;
6799 /* Move IT to the next display element.
6801 RESEAT_P non-zero means if called on a newline in buffer text,
6802 skip to the next visible line start.
6804 Functions get_next_display_element and set_iterator_to_next are
6805 separate because I find this arrangement easier to handle than a
6806 get_next_display_element function that also increments IT's
6807 position. The way it is we can first look at an iterator's current
6808 display element, decide whether it fits on a line, and if it does,
6809 increment the iterator position. The other way around we probably
6810 would either need a flag indicating whether the iterator has to be
6811 incremented the next time, or we would have to implement a
6812 decrement position function which would not be easy to write. */
6814 void
6815 set_iterator_to_next (struct it *it, int reseat_p)
6817 /* Reset flags indicating start and end of a sequence of characters
6818 with box. Reset them at the start of this function because
6819 moving the iterator to a new position might set them. */
6820 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6822 switch (it->method)
6824 case GET_FROM_BUFFER:
6825 /* The current display element of IT is a character from
6826 current_buffer. Advance in the buffer, and maybe skip over
6827 invisible lines that are so because of selective display. */
6828 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6829 reseat_at_next_visible_line_start (it, 0);
6830 else if (it->cmp_it.id >= 0)
6832 /* We are currently getting glyphs from a composition. */
6833 int i;
6835 if (! it->bidi_p)
6837 IT_CHARPOS (*it) += it->cmp_it.nchars;
6838 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6839 if (it->cmp_it.to < it->cmp_it.nglyphs)
6841 it->cmp_it.from = it->cmp_it.to;
6843 else
6845 it->cmp_it.id = -1;
6846 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6847 IT_BYTEPOS (*it),
6848 it->end_charpos, Qnil);
6851 else if (! it->cmp_it.reversed_p)
6853 /* Composition created while scanning forward. */
6854 /* Update IT's char/byte positions to point to the first
6855 character of the next grapheme cluster, or to the
6856 character visually after the current composition. */
6857 for (i = 0; i < it->cmp_it.nchars; i++)
6858 bidi_move_to_visually_next (&it->bidi_it);
6859 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6860 IT_CHARPOS (*it) = it->bidi_it.charpos;
6862 if (it->cmp_it.to < it->cmp_it.nglyphs)
6864 /* Proceed to the next grapheme cluster. */
6865 it->cmp_it.from = it->cmp_it.to;
6867 else
6869 /* No more grapheme clusters in this composition.
6870 Find the next stop position. */
6871 EMACS_INT stop = it->end_charpos;
6872 if (it->bidi_it.scan_dir < 0)
6873 /* Now we are scanning backward and don't know
6874 where to stop. */
6875 stop = -1;
6876 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6877 IT_BYTEPOS (*it), stop, Qnil);
6880 else
6882 /* Composition created while scanning backward. */
6883 /* Update IT's char/byte positions to point to the last
6884 character of the previous grapheme cluster, or the
6885 character visually after the current composition. */
6886 for (i = 0; i < it->cmp_it.nchars; i++)
6887 bidi_move_to_visually_next (&it->bidi_it);
6888 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6889 IT_CHARPOS (*it) = it->bidi_it.charpos;
6890 if (it->cmp_it.from > 0)
6892 /* Proceed to the previous grapheme cluster. */
6893 it->cmp_it.to = it->cmp_it.from;
6895 else
6897 /* No more grapheme clusters in this composition.
6898 Find the next stop position. */
6899 EMACS_INT stop = it->end_charpos;
6900 if (it->bidi_it.scan_dir < 0)
6901 /* Now we are scanning backward and don't know
6902 where to stop. */
6903 stop = -1;
6904 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6905 IT_BYTEPOS (*it), stop, Qnil);
6909 else
6911 xassert (it->len != 0);
6913 if (!it->bidi_p)
6915 IT_BYTEPOS (*it) += it->len;
6916 IT_CHARPOS (*it) += 1;
6918 else
6920 int prev_scan_dir = it->bidi_it.scan_dir;
6921 /* If this is a new paragraph, determine its base
6922 direction (a.k.a. its base embedding level). */
6923 if (it->bidi_it.new_paragraph)
6924 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6925 bidi_move_to_visually_next (&it->bidi_it);
6926 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6927 IT_CHARPOS (*it) = it->bidi_it.charpos;
6928 if (prev_scan_dir != it->bidi_it.scan_dir)
6930 /* As the scan direction was changed, we must
6931 re-compute the stop position for composition. */
6932 EMACS_INT stop = it->end_charpos;
6933 if (it->bidi_it.scan_dir < 0)
6934 stop = -1;
6935 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6936 IT_BYTEPOS (*it), stop, Qnil);
6939 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6941 break;
6943 case GET_FROM_C_STRING:
6944 /* Current display element of IT is from a C string. */
6945 if (!it->bidi_p
6946 /* If the string position is beyond string's end, it means
6947 next_element_from_c_string is padding the string with
6948 blanks, in which case we bypass the bidi iterator,
6949 because it cannot deal with such virtual characters. */
6950 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
6952 IT_BYTEPOS (*it) += it->len;
6953 IT_CHARPOS (*it) += 1;
6955 else
6957 bidi_move_to_visually_next (&it->bidi_it);
6958 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6959 IT_CHARPOS (*it) = it->bidi_it.charpos;
6961 break;
6963 case GET_FROM_DISPLAY_VECTOR:
6964 /* Current display element of IT is from a display table entry.
6965 Advance in the display table definition. Reset it to null if
6966 end reached, and continue with characters from buffers/
6967 strings. */
6968 ++it->current.dpvec_index;
6970 /* Restore face of the iterator to what they were before the
6971 display vector entry (these entries may contain faces). */
6972 it->face_id = it->saved_face_id;
6974 if (it->dpvec + it->current.dpvec_index == it->dpend)
6976 int recheck_faces = it->ellipsis_p;
6978 if (it->s)
6979 it->method = GET_FROM_C_STRING;
6980 else if (STRINGP (it->string))
6981 it->method = GET_FROM_STRING;
6982 else
6984 it->method = GET_FROM_BUFFER;
6985 it->object = it->w->buffer;
6988 it->dpvec = NULL;
6989 it->current.dpvec_index = -1;
6991 /* Skip over characters which were displayed via IT->dpvec. */
6992 if (it->dpvec_char_len < 0)
6993 reseat_at_next_visible_line_start (it, 1);
6994 else if (it->dpvec_char_len > 0)
6996 if (it->method == GET_FROM_STRING
6997 && it->n_overlay_strings > 0)
6998 it->ignore_overlay_strings_at_pos_p = 1;
6999 it->len = it->dpvec_char_len;
7000 set_iterator_to_next (it, reseat_p);
7003 /* Maybe recheck faces after display vector */
7004 if (recheck_faces)
7005 it->stop_charpos = IT_CHARPOS (*it);
7007 break;
7009 case GET_FROM_STRING:
7010 /* Current display element is a character from a Lisp string. */
7011 xassert (it->s == NULL && STRINGP (it->string));
7012 if (it->cmp_it.id >= 0)
7014 int i;
7016 if (! it->bidi_p)
7018 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7019 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7020 if (it->cmp_it.to < it->cmp_it.nglyphs)
7021 it->cmp_it.from = it->cmp_it.to;
7022 else
7024 it->cmp_it.id = -1;
7025 composition_compute_stop_pos (&it->cmp_it,
7026 IT_STRING_CHARPOS (*it),
7027 IT_STRING_BYTEPOS (*it),
7028 it->end_charpos, it->string);
7031 else if (! it->cmp_it.reversed_p)
7033 for (i = 0; i < it->cmp_it.nchars; i++)
7034 bidi_move_to_visually_next (&it->bidi_it);
7035 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7036 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7038 if (it->cmp_it.to < it->cmp_it.nglyphs)
7039 it->cmp_it.from = it->cmp_it.to;
7040 else
7042 EMACS_INT stop = it->end_charpos;
7043 if (it->bidi_it.scan_dir < 0)
7044 stop = -1;
7045 composition_compute_stop_pos (&it->cmp_it,
7046 IT_STRING_CHARPOS (*it),
7047 IT_STRING_BYTEPOS (*it), stop,
7048 it->string);
7051 else
7053 for (i = 0; i < it->cmp_it.nchars; i++)
7054 bidi_move_to_visually_next (&it->bidi_it);
7055 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7056 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7057 if (it->cmp_it.from > 0)
7058 it->cmp_it.to = it->cmp_it.from;
7059 else
7061 EMACS_INT stop = it->end_charpos;
7062 if (it->bidi_it.scan_dir < 0)
7063 stop = -1;
7064 composition_compute_stop_pos (&it->cmp_it,
7065 IT_STRING_CHARPOS (*it),
7066 IT_STRING_BYTEPOS (*it), stop,
7067 it->string);
7071 else
7073 if (!it->bidi_p
7074 /* If the string position is beyond string's end, it
7075 means next_element_from_string is padding the string
7076 with blanks, in which case we bypass the bidi
7077 iterator, because it cannot deal with such virtual
7078 characters. */
7079 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7081 IT_STRING_BYTEPOS (*it) += it->len;
7082 IT_STRING_CHARPOS (*it) += 1;
7084 else
7086 int prev_scan_dir = it->bidi_it.scan_dir;
7088 bidi_move_to_visually_next (&it->bidi_it);
7089 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7090 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7091 if (prev_scan_dir != it->bidi_it.scan_dir)
7093 EMACS_INT stop = it->end_charpos;
7095 if (it->bidi_it.scan_dir < 0)
7096 stop = -1;
7097 composition_compute_stop_pos (&it->cmp_it,
7098 IT_STRING_CHARPOS (*it),
7099 IT_STRING_BYTEPOS (*it), stop,
7100 it->string);
7105 consider_string_end:
7107 if (it->current.overlay_string_index >= 0)
7109 /* IT->string is an overlay string. Advance to the
7110 next, if there is one. */
7111 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7113 it->ellipsis_p = 0;
7114 next_overlay_string (it);
7115 if (it->ellipsis_p)
7116 setup_for_ellipsis (it, 0);
7119 else
7121 /* IT->string is not an overlay string. If we reached
7122 its end, and there is something on IT->stack, proceed
7123 with what is on the stack. This can be either another
7124 string, this time an overlay string, or a buffer. */
7125 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7126 && it->sp > 0)
7128 pop_it (it);
7129 if (it->method == GET_FROM_STRING)
7130 goto consider_string_end;
7133 break;
7135 case GET_FROM_IMAGE:
7136 case GET_FROM_STRETCH:
7137 /* The position etc with which we have to proceed are on
7138 the stack. The position may be at the end of a string,
7139 if the `display' property takes up the whole string. */
7140 xassert (it->sp > 0);
7141 pop_it (it);
7142 if (it->method == GET_FROM_STRING)
7143 goto consider_string_end;
7144 break;
7146 default:
7147 /* There are no other methods defined, so this should be a bug. */
7148 abort ();
7151 xassert (it->method != GET_FROM_STRING
7152 || (STRINGP (it->string)
7153 && IT_STRING_CHARPOS (*it) >= 0));
7156 /* Load IT's display element fields with information about the next
7157 display element which comes from a display table entry or from the
7158 result of translating a control character to one of the forms `^C'
7159 or `\003'.
7161 IT->dpvec holds the glyphs to return as characters.
7162 IT->saved_face_id holds the face id before the display vector--it
7163 is restored into IT->face_id in set_iterator_to_next. */
7165 static int
7166 next_element_from_display_vector (struct it *it)
7168 Lisp_Object gc;
7170 /* Precondition. */
7171 xassert (it->dpvec && it->current.dpvec_index >= 0);
7173 it->face_id = it->saved_face_id;
7175 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7176 That seemed totally bogus - so I changed it... */
7177 gc = it->dpvec[it->current.dpvec_index];
7179 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
7181 it->c = GLYPH_CODE_CHAR (gc);
7182 it->len = CHAR_BYTES (it->c);
7184 /* The entry may contain a face id to use. Such a face id is
7185 the id of a Lisp face, not a realized face. A face id of
7186 zero means no face is specified. */
7187 if (it->dpvec_face_id >= 0)
7188 it->face_id = it->dpvec_face_id;
7189 else
7191 EMACS_INT lface_id = GLYPH_CODE_FACE (gc);
7192 if (lface_id > 0)
7193 it->face_id = merge_faces (it->f, Qt, lface_id,
7194 it->saved_face_id);
7197 else
7198 /* Display table entry is invalid. Return a space. */
7199 it->c = ' ', it->len = 1;
7201 /* Don't change position and object of the iterator here. They are
7202 still the values of the character that had this display table
7203 entry or was translated, and that's what we want. */
7204 it->what = IT_CHARACTER;
7205 return 1;
7208 /* Get the first element of string/buffer in the visual order, after
7209 being reseated to a new position in a string or a buffer. */
7210 static void
7211 get_visually_first_element (struct it *it)
7213 int string_p = STRINGP (it->string) || it->s;
7214 EMACS_INT eob = (string_p ? it->bidi_it.string.schars : ZV);
7215 EMACS_INT bob = (string_p ? 0 : BEGV);
7217 if (STRINGP (it->string))
7219 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7220 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7222 else
7224 it->bidi_it.charpos = IT_CHARPOS (*it);
7225 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7228 if (it->bidi_it.charpos == eob)
7230 /* Nothing to do, but reset the FIRST_ELT flag, like
7231 bidi_paragraph_init does, because we are not going to
7232 call it. */
7233 it->bidi_it.first_elt = 0;
7235 else if (it->bidi_it.charpos == bob
7236 || (!string_p
7237 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7238 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7240 /* If we are at the beginning of a line/string, we can produce
7241 the next element right away. */
7242 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7243 bidi_move_to_visually_next (&it->bidi_it);
7245 else
7247 EMACS_INT orig_bytepos = it->bidi_it.bytepos;
7249 /* We need to prime the bidi iterator starting at the line's or
7250 string's beginning, before we will be able to produce the
7251 next element. */
7252 if (string_p)
7253 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7254 else
7256 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7257 -1);
7258 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7260 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7263 /* Now return to buffer/string position where we were asked
7264 to get the next display element, and produce that. */
7265 bidi_move_to_visually_next (&it->bidi_it);
7267 while (it->bidi_it.bytepos != orig_bytepos
7268 && it->bidi_it.charpos < eob);
7271 /* Adjust IT's position information to where we ended up. */
7272 if (STRINGP (it->string))
7274 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7275 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7277 else
7279 IT_CHARPOS (*it) = it->bidi_it.charpos;
7280 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7283 if (STRINGP (it->string) || !it->s)
7285 EMACS_INT stop, charpos, bytepos;
7287 if (STRINGP (it->string))
7289 xassert (!it->s);
7290 stop = SCHARS (it->string);
7291 if (stop > it->end_charpos)
7292 stop = it->end_charpos;
7293 charpos = IT_STRING_CHARPOS (*it);
7294 bytepos = IT_STRING_BYTEPOS (*it);
7296 else
7298 stop = it->end_charpos;
7299 charpos = IT_CHARPOS (*it);
7300 bytepos = IT_BYTEPOS (*it);
7302 if (it->bidi_it.scan_dir < 0)
7303 stop = -1;
7304 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7305 it->string);
7309 /* Load IT with the next display element from Lisp string IT->string.
7310 IT->current.string_pos is the current position within the string.
7311 If IT->current.overlay_string_index >= 0, the Lisp string is an
7312 overlay string. */
7314 static int
7315 next_element_from_string (struct it *it)
7317 struct text_pos position;
7319 xassert (STRINGP (it->string));
7320 xassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7321 xassert (IT_STRING_CHARPOS (*it) >= 0);
7322 position = it->current.string_pos;
7324 /* With bidi reordering, the character to display might not be the
7325 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7326 that we were reseat()ed to a new string, whose paragraph
7327 direction is not known. */
7328 if (it->bidi_p && it->bidi_it.first_elt)
7330 get_visually_first_element (it);
7331 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7334 /* Time to check for invisible text? */
7335 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7337 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7339 if (!(!it->bidi_p
7340 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7341 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7343 /* With bidi non-linear iteration, we could find
7344 ourselves far beyond the last computed stop_charpos,
7345 with several other stop positions in between that we
7346 missed. Scan them all now, in buffer's logical
7347 order, until we find and handle the last stop_charpos
7348 that precedes our current position. */
7349 handle_stop_backwards (it, it->stop_charpos);
7350 return GET_NEXT_DISPLAY_ELEMENT (it);
7352 else
7354 if (it->bidi_p)
7356 /* Take note of the stop position we just moved
7357 across, for when we will move back across it. */
7358 it->prev_stop = it->stop_charpos;
7359 /* If we are at base paragraph embedding level, take
7360 note of the last stop position seen at this
7361 level. */
7362 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7363 it->base_level_stop = it->stop_charpos;
7365 handle_stop (it);
7367 /* Since a handler may have changed IT->method, we must
7368 recurse here. */
7369 return GET_NEXT_DISPLAY_ELEMENT (it);
7372 else if (it->bidi_p
7373 /* If we are before prev_stop, we may have overstepped
7374 on our way backwards a stop_pos, and if so, we need
7375 to handle that stop_pos. */
7376 && IT_STRING_CHARPOS (*it) < it->prev_stop
7377 /* We can sometimes back up for reasons that have nothing
7378 to do with bidi reordering. E.g., compositions. The
7379 code below is only needed when we are above the base
7380 embedding level, so test for that explicitly. */
7381 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7383 /* If we lost track of base_level_stop, we have no better
7384 place for handle_stop_backwards to start from than string
7385 beginning. This happens, e.g., when we were reseated to
7386 the previous screenful of text by vertical-motion. */
7387 if (it->base_level_stop <= 0
7388 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7389 it->base_level_stop = 0;
7390 handle_stop_backwards (it, it->base_level_stop);
7391 return GET_NEXT_DISPLAY_ELEMENT (it);
7395 if (it->current.overlay_string_index >= 0)
7397 /* Get the next character from an overlay string. In overlay
7398 strings, there is no field width or padding with spaces to
7399 do. */
7400 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7402 it->what = IT_EOB;
7403 return 0;
7405 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7406 IT_STRING_BYTEPOS (*it),
7407 it->bidi_it.scan_dir < 0
7408 ? -1
7409 : SCHARS (it->string))
7410 && next_element_from_composition (it))
7412 return 1;
7414 else if (STRING_MULTIBYTE (it->string))
7416 const unsigned char *s = (SDATA (it->string)
7417 + IT_STRING_BYTEPOS (*it));
7418 it->c = string_char_and_length (s, &it->len);
7420 else
7422 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7423 it->len = 1;
7426 else
7428 /* Get the next character from a Lisp string that is not an
7429 overlay string. Such strings come from the mode line, for
7430 example. We may have to pad with spaces, or truncate the
7431 string. See also next_element_from_c_string. */
7432 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7434 it->what = IT_EOB;
7435 return 0;
7437 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7439 /* Pad with spaces. */
7440 it->c = ' ', it->len = 1;
7441 CHARPOS (position) = BYTEPOS (position) = -1;
7443 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7444 IT_STRING_BYTEPOS (*it),
7445 it->bidi_it.scan_dir < 0
7446 ? -1
7447 : it->string_nchars)
7448 && next_element_from_composition (it))
7450 return 1;
7452 else if (STRING_MULTIBYTE (it->string))
7454 const unsigned char *s = (SDATA (it->string)
7455 + IT_STRING_BYTEPOS (*it));
7456 it->c = string_char_and_length (s, &it->len);
7458 else
7460 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7461 it->len = 1;
7465 /* Record what we have and where it came from. */
7466 it->what = IT_CHARACTER;
7467 it->object = it->string;
7468 it->position = position;
7469 return 1;
7473 /* Load IT with next display element from C string IT->s.
7474 IT->string_nchars is the maximum number of characters to return
7475 from the string. IT->end_charpos may be greater than
7476 IT->string_nchars when this function is called, in which case we
7477 may have to return padding spaces. Value is zero if end of string
7478 reached, including padding spaces. */
7480 static int
7481 next_element_from_c_string (struct it *it)
7483 int success_p = 1;
7485 xassert (it->s);
7486 xassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7487 it->what = IT_CHARACTER;
7488 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7489 it->object = Qnil;
7491 /* With bidi reordering, the character to display might not be the
7492 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7493 we were reseated to a new string, whose paragraph direction is
7494 not known. */
7495 if (it->bidi_p && it->bidi_it.first_elt)
7496 get_visually_first_element (it);
7498 /* IT's position can be greater than IT->string_nchars in case a
7499 field width or precision has been specified when the iterator was
7500 initialized. */
7501 if (IT_CHARPOS (*it) >= it->end_charpos)
7503 /* End of the game. */
7504 it->what = IT_EOB;
7505 success_p = 0;
7507 else if (IT_CHARPOS (*it) >= it->string_nchars)
7509 /* Pad with spaces. */
7510 it->c = ' ', it->len = 1;
7511 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7513 else if (it->multibyte_p)
7514 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7515 else
7516 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7518 return success_p;
7522 /* Set up IT to return characters from an ellipsis, if appropriate.
7523 The definition of the ellipsis glyphs may come from a display table
7524 entry. This function fills IT with the first glyph from the
7525 ellipsis if an ellipsis is to be displayed. */
7527 static int
7528 next_element_from_ellipsis (struct it *it)
7530 if (it->selective_display_ellipsis_p)
7531 setup_for_ellipsis (it, it->len);
7532 else
7534 /* The face at the current position may be different from the
7535 face we find after the invisible text. Remember what it
7536 was in IT->saved_face_id, and signal that it's there by
7537 setting face_before_selective_p. */
7538 it->saved_face_id = it->face_id;
7539 it->method = GET_FROM_BUFFER;
7540 it->object = it->w->buffer;
7541 reseat_at_next_visible_line_start (it, 1);
7542 it->face_before_selective_p = 1;
7545 return GET_NEXT_DISPLAY_ELEMENT (it);
7549 /* Deliver an image display element. The iterator IT is already
7550 filled with image information (done in handle_display_prop). Value
7551 is always 1. */
7554 static int
7555 next_element_from_image (struct it *it)
7557 it->what = IT_IMAGE;
7558 it->ignore_overlay_strings_at_pos_p = 0;
7559 return 1;
7563 /* Fill iterator IT with next display element from a stretch glyph
7564 property. IT->object is the value of the text property. Value is
7565 always 1. */
7567 static int
7568 next_element_from_stretch (struct it *it)
7570 it->what = IT_STRETCH;
7571 return 1;
7574 /* Scan backwards from IT's current position until we find a stop
7575 position, or until BEGV. This is called when we find ourself
7576 before both the last known prev_stop and base_level_stop while
7577 reordering bidirectional text. */
7579 static void
7580 compute_stop_pos_backwards (struct it *it)
7582 const int SCAN_BACK_LIMIT = 1000;
7583 struct text_pos pos;
7584 struct display_pos save_current = it->current;
7585 struct text_pos save_position = it->position;
7586 EMACS_INT charpos = IT_CHARPOS (*it);
7587 EMACS_INT where_we_are = charpos;
7588 EMACS_INT save_stop_pos = it->stop_charpos;
7589 EMACS_INT save_end_pos = it->end_charpos;
7591 xassert (NILP (it->string) && !it->s);
7592 xassert (it->bidi_p);
7593 it->bidi_p = 0;
7596 it->end_charpos = min (charpos + 1, ZV);
7597 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7598 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7599 reseat_1 (it, pos, 0);
7600 compute_stop_pos (it);
7601 /* We must advance forward, right? */
7602 if (it->stop_charpos <= charpos)
7603 abort ();
7605 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7607 if (it->stop_charpos <= where_we_are)
7608 it->prev_stop = it->stop_charpos;
7609 else
7610 it->prev_stop = BEGV;
7611 it->bidi_p = 1;
7612 it->current = save_current;
7613 it->position = save_position;
7614 it->stop_charpos = save_stop_pos;
7615 it->end_charpos = save_end_pos;
7618 /* Scan forward from CHARPOS in the current buffer/string, until we
7619 find a stop position > current IT's position. Then handle the stop
7620 position before that. This is called when we bump into a stop
7621 position while reordering bidirectional text. CHARPOS should be
7622 the last previously processed stop_pos (or BEGV/0, if none were
7623 processed yet) whose position is less that IT's current
7624 position. */
7626 static void
7627 handle_stop_backwards (struct it *it, EMACS_INT charpos)
7629 int bufp = !STRINGP (it->string);
7630 EMACS_INT where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7631 struct display_pos save_current = it->current;
7632 struct text_pos save_position = it->position;
7633 struct text_pos pos1;
7634 EMACS_INT next_stop;
7636 /* Scan in strict logical order. */
7637 xassert (it->bidi_p);
7638 it->bidi_p = 0;
7641 it->prev_stop = charpos;
7642 if (bufp)
7644 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7645 reseat_1 (it, pos1, 0);
7647 else
7648 it->current.string_pos = string_pos (charpos, it->string);
7649 compute_stop_pos (it);
7650 /* We must advance forward, right? */
7651 if (it->stop_charpos <= it->prev_stop)
7652 abort ();
7653 charpos = it->stop_charpos;
7655 while (charpos <= where_we_are);
7657 it->bidi_p = 1;
7658 it->current = save_current;
7659 it->position = save_position;
7660 next_stop = it->stop_charpos;
7661 it->stop_charpos = it->prev_stop;
7662 handle_stop (it);
7663 it->stop_charpos = next_stop;
7666 /* Load IT with the next display element from current_buffer. Value
7667 is zero if end of buffer reached. IT->stop_charpos is the next
7668 position at which to stop and check for text properties or buffer
7669 end. */
7671 static int
7672 next_element_from_buffer (struct it *it)
7674 int success_p = 1;
7676 xassert (IT_CHARPOS (*it) >= BEGV);
7677 xassert (NILP (it->string) && !it->s);
7678 xassert (!it->bidi_p
7679 || (EQ (it->bidi_it.string.lstring, Qnil)
7680 && it->bidi_it.string.s == NULL));
7682 /* With bidi reordering, the character to display might not be the
7683 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7684 we were reseat()ed to a new buffer position, which is potentially
7685 a different paragraph. */
7686 if (it->bidi_p && it->bidi_it.first_elt)
7688 get_visually_first_element (it);
7689 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7692 if (IT_CHARPOS (*it) >= it->stop_charpos)
7694 if (IT_CHARPOS (*it) >= it->end_charpos)
7696 int overlay_strings_follow_p;
7698 /* End of the game, except when overlay strings follow that
7699 haven't been returned yet. */
7700 if (it->overlay_strings_at_end_processed_p)
7701 overlay_strings_follow_p = 0;
7702 else
7704 it->overlay_strings_at_end_processed_p = 1;
7705 overlay_strings_follow_p = get_overlay_strings (it, 0);
7708 if (overlay_strings_follow_p)
7709 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7710 else
7712 it->what = IT_EOB;
7713 it->position = it->current.pos;
7714 success_p = 0;
7717 else if (!(!it->bidi_p
7718 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7719 || IT_CHARPOS (*it) == it->stop_charpos))
7721 /* With bidi non-linear iteration, we could find ourselves
7722 far beyond the last computed stop_charpos, with several
7723 other stop positions in between that we missed. Scan
7724 them all now, in buffer's logical order, until we find
7725 and handle the last stop_charpos that precedes our
7726 current position. */
7727 handle_stop_backwards (it, it->stop_charpos);
7728 return GET_NEXT_DISPLAY_ELEMENT (it);
7730 else
7732 if (it->bidi_p)
7734 /* Take note of the stop position we just moved across,
7735 for when we will move back across it. */
7736 it->prev_stop = it->stop_charpos;
7737 /* If we are at base paragraph embedding level, take
7738 note of the last stop position seen at this
7739 level. */
7740 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7741 it->base_level_stop = it->stop_charpos;
7743 handle_stop (it);
7744 return GET_NEXT_DISPLAY_ELEMENT (it);
7747 else if (it->bidi_p
7748 /* If we are before prev_stop, we may have overstepped on
7749 our way backwards a stop_pos, and if so, we need to
7750 handle that stop_pos. */
7751 && IT_CHARPOS (*it) < it->prev_stop
7752 /* We can sometimes back up for reasons that have nothing
7753 to do with bidi reordering. E.g., compositions. The
7754 code below is only needed when we are above the base
7755 embedding level, so test for that explicitly. */
7756 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7758 if (it->base_level_stop <= 0
7759 || IT_CHARPOS (*it) < it->base_level_stop)
7761 /* If we lost track of base_level_stop, we need to find
7762 prev_stop by looking backwards. This happens, e.g., when
7763 we were reseated to the previous screenful of text by
7764 vertical-motion. */
7765 it->base_level_stop = BEGV;
7766 compute_stop_pos_backwards (it);
7767 handle_stop_backwards (it, it->prev_stop);
7769 else
7770 handle_stop_backwards (it, it->base_level_stop);
7771 return GET_NEXT_DISPLAY_ELEMENT (it);
7773 else
7775 /* No face changes, overlays etc. in sight, so just return a
7776 character from current_buffer. */
7777 unsigned char *p;
7778 EMACS_INT stop;
7780 /* Maybe run the redisplay end trigger hook. Performance note:
7781 This doesn't seem to cost measurable time. */
7782 if (it->redisplay_end_trigger_charpos
7783 && it->glyph_row
7784 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7785 run_redisplay_end_trigger_hook (it);
7787 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7788 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7789 stop)
7790 && next_element_from_composition (it))
7792 return 1;
7795 /* Get the next character, maybe multibyte. */
7796 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7797 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7798 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7799 else
7800 it->c = *p, it->len = 1;
7802 /* Record what we have and where it came from. */
7803 it->what = IT_CHARACTER;
7804 it->object = it->w->buffer;
7805 it->position = it->current.pos;
7807 /* Normally we return the character found above, except when we
7808 really want to return an ellipsis for selective display. */
7809 if (it->selective)
7811 if (it->c == '\n')
7813 /* A value of selective > 0 means hide lines indented more
7814 than that number of columns. */
7815 if (it->selective > 0
7816 && IT_CHARPOS (*it) + 1 < ZV
7817 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7818 IT_BYTEPOS (*it) + 1,
7819 it->selective))
7821 success_p = next_element_from_ellipsis (it);
7822 it->dpvec_char_len = -1;
7825 else if (it->c == '\r' && it->selective == -1)
7827 /* A value of selective == -1 means that everything from the
7828 CR to the end of the line is invisible, with maybe an
7829 ellipsis displayed for it. */
7830 success_p = next_element_from_ellipsis (it);
7831 it->dpvec_char_len = -1;
7836 /* Value is zero if end of buffer reached. */
7837 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7838 return success_p;
7842 /* Run the redisplay end trigger hook for IT. */
7844 static void
7845 run_redisplay_end_trigger_hook (struct it *it)
7847 Lisp_Object args[3];
7849 /* IT->glyph_row should be non-null, i.e. we should be actually
7850 displaying something, or otherwise we should not run the hook. */
7851 xassert (it->glyph_row);
7853 /* Set up hook arguments. */
7854 args[0] = Qredisplay_end_trigger_functions;
7855 args[1] = it->window;
7856 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7857 it->redisplay_end_trigger_charpos = 0;
7859 /* Since we are *trying* to run these functions, don't try to run
7860 them again, even if they get an error. */
7861 it->w->redisplay_end_trigger = Qnil;
7862 Frun_hook_with_args (3, args);
7864 /* Notice if it changed the face of the character we are on. */
7865 handle_face_prop (it);
7869 /* Deliver a composition display element. Unlike the other
7870 next_element_from_XXX, this function is not registered in the array
7871 get_next_element[]. It is called from next_element_from_buffer and
7872 next_element_from_string when necessary. */
7874 static int
7875 next_element_from_composition (struct it *it)
7877 it->what = IT_COMPOSITION;
7878 it->len = it->cmp_it.nbytes;
7879 if (STRINGP (it->string))
7881 if (it->c < 0)
7883 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7884 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7885 return 0;
7887 it->position = it->current.string_pos;
7888 it->object = it->string;
7889 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7890 IT_STRING_BYTEPOS (*it), it->string);
7892 else
7894 if (it->c < 0)
7896 IT_CHARPOS (*it) += it->cmp_it.nchars;
7897 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7898 if (it->bidi_p)
7900 if (it->bidi_it.new_paragraph)
7901 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7902 /* Resync the bidi iterator with IT's new position.
7903 FIXME: this doesn't support bidirectional text. */
7904 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7905 bidi_move_to_visually_next (&it->bidi_it);
7907 return 0;
7909 it->position = it->current.pos;
7910 it->object = it->w->buffer;
7911 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7912 IT_BYTEPOS (*it), Qnil);
7914 return 1;
7919 /***********************************************************************
7920 Moving an iterator without producing glyphs
7921 ***********************************************************************/
7923 /* Check if iterator is at a position corresponding to a valid buffer
7924 position after some move_it_ call. */
7926 #define IT_POS_VALID_AFTER_MOVE_P(it) \
7927 ((it)->method == GET_FROM_STRING \
7928 ? IT_STRING_CHARPOS (*it) == 0 \
7929 : 1)
7932 /* Move iterator IT to a specified buffer or X position within one
7933 line on the display without producing glyphs.
7935 OP should be a bit mask including some or all of these bits:
7936 MOVE_TO_X: Stop upon reaching x-position TO_X.
7937 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
7938 Regardless of OP's value, stop upon reaching the end of the display line.
7940 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
7941 This means, in particular, that TO_X includes window's horizontal
7942 scroll amount.
7944 The return value has several possible values that
7945 say what condition caused the scan to stop:
7947 MOVE_POS_MATCH_OR_ZV
7948 - when TO_POS or ZV was reached.
7950 MOVE_X_REACHED
7951 -when TO_X was reached before TO_POS or ZV were reached.
7953 MOVE_LINE_CONTINUED
7954 - when we reached the end of the display area and the line must
7955 be continued.
7957 MOVE_LINE_TRUNCATED
7958 - when we reached the end of the display area and the line is
7959 truncated.
7961 MOVE_NEWLINE_OR_CR
7962 - when we stopped at a line end, i.e. a newline or a CR and selective
7963 display is on. */
7965 static enum move_it_result
7966 move_it_in_display_line_to (struct it *it,
7967 EMACS_INT to_charpos, int to_x,
7968 enum move_operation_enum op)
7970 enum move_it_result result = MOVE_UNDEFINED;
7971 struct glyph_row *saved_glyph_row;
7972 struct it wrap_it, atpos_it, atx_it, ppos_it;
7973 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
7974 void *ppos_data = NULL;
7975 int may_wrap = 0;
7976 enum it_method prev_method = it->method;
7977 EMACS_INT prev_pos = IT_CHARPOS (*it);
7978 int saw_smaller_pos = prev_pos < to_charpos;
7980 /* Don't produce glyphs in produce_glyphs. */
7981 saved_glyph_row = it->glyph_row;
7982 it->glyph_row = NULL;
7984 /* Use wrap_it to save a copy of IT wherever a word wrap could
7985 occur. Use atpos_it to save a copy of IT at the desired buffer
7986 position, if found, so that we can scan ahead and check if the
7987 word later overshoots the window edge. Use atx_it similarly, for
7988 pixel positions. */
7989 wrap_it.sp = -1;
7990 atpos_it.sp = -1;
7991 atx_it.sp = -1;
7993 /* Use ppos_it under bidi reordering to save a copy of IT for the
7994 position > CHARPOS that is the closest to CHARPOS. We restore
7995 that position in IT when we have scanned the entire display line
7996 without finding a match for CHARPOS and all the character
7997 positions are greater than CHARPOS. */
7998 if (it->bidi_p)
8000 SAVE_IT (ppos_it, *it, ppos_data);
8001 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8002 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8003 SAVE_IT (ppos_it, *it, ppos_data);
8006 #define BUFFER_POS_REACHED_P() \
8007 ((op & MOVE_TO_POS) != 0 \
8008 && BUFFERP (it->object) \
8009 && (IT_CHARPOS (*it) == to_charpos \
8010 || ((!it->bidi_p \
8011 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8012 && IT_CHARPOS (*it) > to_charpos) \
8013 || (it->what == IT_COMPOSITION \
8014 && ((IT_CHARPOS (*it) > to_charpos \
8015 && to_charpos >= it->cmp_it.charpos) \
8016 || (IT_CHARPOS (*it) < to_charpos \
8017 && to_charpos <= it->cmp_it.charpos)))) \
8018 && (it->method == GET_FROM_BUFFER \
8019 || (it->method == GET_FROM_DISPLAY_VECTOR \
8020 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8022 /* If there's a line-/wrap-prefix, handle it. */
8023 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8024 && it->current_y < it->last_visible_y)
8025 handle_line_prefix (it);
8027 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8028 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8030 while (1)
8032 int x, i, ascent = 0, descent = 0;
8034 /* Utility macro to reset an iterator with x, ascent, and descent. */
8035 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8036 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8037 (IT)->max_descent = descent)
8039 /* Stop if we move beyond TO_CHARPOS (after an image or a
8040 display string or stretch glyph). */
8041 if ((op & MOVE_TO_POS) != 0
8042 && BUFFERP (it->object)
8043 && it->method == GET_FROM_BUFFER
8044 && (((!it->bidi_p
8045 /* When the iterator is at base embedding level, we
8046 are guaranteed that characters are delivered for
8047 display in strictly increasing order of their
8048 buffer positions. */
8049 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8050 && IT_CHARPOS (*it) > to_charpos)
8051 || (it->bidi_p
8052 && (prev_method == GET_FROM_IMAGE
8053 || prev_method == GET_FROM_STRETCH
8054 || prev_method == GET_FROM_STRING)
8055 /* Passed TO_CHARPOS from left to right. */
8056 && ((prev_pos < to_charpos
8057 && IT_CHARPOS (*it) > to_charpos)
8058 /* Passed TO_CHARPOS from right to left. */
8059 || (prev_pos > to_charpos
8060 && IT_CHARPOS (*it) < to_charpos)))))
8062 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8064 result = MOVE_POS_MATCH_OR_ZV;
8065 break;
8067 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8068 /* If wrap_it is valid, the current position might be in a
8069 word that is wrapped. So, save the iterator in
8070 atpos_it and continue to see if wrapping happens. */
8071 SAVE_IT (atpos_it, *it, atpos_data);
8074 /* Stop when ZV reached.
8075 We used to stop here when TO_CHARPOS reached as well, but that is
8076 too soon if this glyph does not fit on this line. So we handle it
8077 explicitly below. */
8078 if (!get_next_display_element (it))
8080 result = MOVE_POS_MATCH_OR_ZV;
8081 break;
8084 if (it->line_wrap == TRUNCATE)
8086 if (BUFFER_POS_REACHED_P ())
8088 result = MOVE_POS_MATCH_OR_ZV;
8089 break;
8092 else
8094 if (it->line_wrap == WORD_WRAP)
8096 if (IT_DISPLAYING_WHITESPACE (it))
8097 may_wrap = 1;
8098 else if (may_wrap)
8100 /* We have reached a glyph that follows one or more
8101 whitespace characters. If the position is
8102 already found, we are done. */
8103 if (atpos_it.sp >= 0)
8105 RESTORE_IT (it, &atpos_it, atpos_data);
8106 result = MOVE_POS_MATCH_OR_ZV;
8107 goto done;
8109 if (atx_it.sp >= 0)
8111 RESTORE_IT (it, &atx_it, atx_data);
8112 result = MOVE_X_REACHED;
8113 goto done;
8115 /* Otherwise, we can wrap here. */
8116 SAVE_IT (wrap_it, *it, wrap_data);
8117 may_wrap = 0;
8122 /* Remember the line height for the current line, in case
8123 the next element doesn't fit on the line. */
8124 ascent = it->max_ascent;
8125 descent = it->max_descent;
8127 /* The call to produce_glyphs will get the metrics of the
8128 display element IT is loaded with. Record the x-position
8129 before this display element, in case it doesn't fit on the
8130 line. */
8131 x = it->current_x;
8133 PRODUCE_GLYPHS (it);
8135 if (it->area != TEXT_AREA)
8137 prev_method = it->method;
8138 if (it->method == GET_FROM_BUFFER)
8139 prev_pos = IT_CHARPOS (*it);
8140 set_iterator_to_next (it, 1);
8141 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8142 SET_TEXT_POS (this_line_min_pos,
8143 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8144 if (it->bidi_p
8145 && (op & MOVE_TO_POS)
8146 && IT_CHARPOS (*it) > to_charpos
8147 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8148 SAVE_IT (ppos_it, *it, ppos_data);
8149 continue;
8152 /* The number of glyphs we get back in IT->nglyphs will normally
8153 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8154 character on a terminal frame, or (iii) a line end. For the
8155 second case, IT->nglyphs - 1 padding glyphs will be present.
8156 (On X frames, there is only one glyph produced for a
8157 composite character.)
8159 The behavior implemented below means, for continuation lines,
8160 that as many spaces of a TAB as fit on the current line are
8161 displayed there. For terminal frames, as many glyphs of a
8162 multi-glyph character are displayed in the current line, too.
8163 This is what the old redisplay code did, and we keep it that
8164 way. Under X, the whole shape of a complex character must
8165 fit on the line or it will be completely displayed in the
8166 next line.
8168 Note that both for tabs and padding glyphs, all glyphs have
8169 the same width. */
8170 if (it->nglyphs)
8172 /* More than one glyph or glyph doesn't fit on line. All
8173 glyphs have the same width. */
8174 int single_glyph_width = it->pixel_width / it->nglyphs;
8175 int new_x;
8176 int x_before_this_char = x;
8177 int hpos_before_this_char = it->hpos;
8179 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8181 new_x = x + single_glyph_width;
8183 /* We want to leave anything reaching TO_X to the caller. */
8184 if ((op & MOVE_TO_X) && new_x > to_x)
8186 if (BUFFER_POS_REACHED_P ())
8188 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8189 goto buffer_pos_reached;
8190 if (atpos_it.sp < 0)
8192 SAVE_IT (atpos_it, *it, atpos_data);
8193 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8196 else
8198 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8200 it->current_x = x;
8201 result = MOVE_X_REACHED;
8202 break;
8204 if (atx_it.sp < 0)
8206 SAVE_IT (atx_it, *it, atx_data);
8207 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8212 if (/* Lines are continued. */
8213 it->line_wrap != TRUNCATE
8214 && (/* And glyph doesn't fit on the line. */
8215 new_x > it->last_visible_x
8216 /* Or it fits exactly and we're on a window
8217 system frame. */
8218 || (new_x == it->last_visible_x
8219 && FRAME_WINDOW_P (it->f))))
8221 if (/* IT->hpos == 0 means the very first glyph
8222 doesn't fit on the line, e.g. a wide image. */
8223 it->hpos == 0
8224 || (new_x == it->last_visible_x
8225 && FRAME_WINDOW_P (it->f)))
8227 ++it->hpos;
8228 it->current_x = new_x;
8230 /* The character's last glyph just barely fits
8231 in this row. */
8232 if (i == it->nglyphs - 1)
8234 /* If this is the destination position,
8235 return a position *before* it in this row,
8236 now that we know it fits in this row. */
8237 if (BUFFER_POS_REACHED_P ())
8239 if (it->line_wrap != WORD_WRAP
8240 || wrap_it.sp < 0)
8242 it->hpos = hpos_before_this_char;
8243 it->current_x = x_before_this_char;
8244 result = MOVE_POS_MATCH_OR_ZV;
8245 break;
8247 if (it->line_wrap == WORD_WRAP
8248 && atpos_it.sp < 0)
8250 SAVE_IT (atpos_it, *it, atpos_data);
8251 atpos_it.current_x = x_before_this_char;
8252 atpos_it.hpos = hpos_before_this_char;
8256 prev_method = it->method;
8257 if (it->method == GET_FROM_BUFFER)
8258 prev_pos = IT_CHARPOS (*it);
8259 set_iterator_to_next (it, 1);
8260 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8261 SET_TEXT_POS (this_line_min_pos,
8262 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8263 /* On graphical terminals, newlines may
8264 "overflow" into the fringe if
8265 overflow-newline-into-fringe is non-nil.
8266 On text-only terminals, newlines may
8267 overflow into the last glyph on the
8268 display line.*/
8269 if (!FRAME_WINDOW_P (it->f)
8270 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8272 if (!get_next_display_element (it))
8274 result = MOVE_POS_MATCH_OR_ZV;
8275 break;
8277 if (BUFFER_POS_REACHED_P ())
8279 if (ITERATOR_AT_END_OF_LINE_P (it))
8280 result = MOVE_POS_MATCH_OR_ZV;
8281 else
8282 result = MOVE_LINE_CONTINUED;
8283 break;
8285 if (ITERATOR_AT_END_OF_LINE_P (it))
8287 result = MOVE_NEWLINE_OR_CR;
8288 break;
8293 else
8294 IT_RESET_X_ASCENT_DESCENT (it);
8296 if (wrap_it.sp >= 0)
8298 RESTORE_IT (it, &wrap_it, wrap_data);
8299 atpos_it.sp = -1;
8300 atx_it.sp = -1;
8303 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8304 IT_CHARPOS (*it)));
8305 result = MOVE_LINE_CONTINUED;
8306 break;
8309 if (BUFFER_POS_REACHED_P ())
8311 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8312 goto buffer_pos_reached;
8313 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8315 SAVE_IT (atpos_it, *it, atpos_data);
8316 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8320 if (new_x > it->first_visible_x)
8322 /* Glyph is visible. Increment number of glyphs that
8323 would be displayed. */
8324 ++it->hpos;
8328 if (result != MOVE_UNDEFINED)
8329 break;
8331 else if (BUFFER_POS_REACHED_P ())
8333 buffer_pos_reached:
8334 IT_RESET_X_ASCENT_DESCENT (it);
8335 result = MOVE_POS_MATCH_OR_ZV;
8336 break;
8338 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8340 /* Stop when TO_X specified and reached. This check is
8341 necessary here because of lines consisting of a line end,
8342 only. The line end will not produce any glyphs and we
8343 would never get MOVE_X_REACHED. */
8344 xassert (it->nglyphs == 0);
8345 result = MOVE_X_REACHED;
8346 break;
8349 /* Is this a line end? If yes, we're done. */
8350 if (ITERATOR_AT_END_OF_LINE_P (it))
8352 /* If we are past TO_CHARPOS, but never saw any character
8353 positions smaller than TO_CHARPOS, return
8354 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8355 did. */
8356 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8358 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8360 if (IT_CHARPOS (ppos_it) < ZV)
8362 RESTORE_IT (it, &ppos_it, ppos_data);
8363 result = MOVE_POS_MATCH_OR_ZV;
8365 else
8366 goto buffer_pos_reached;
8368 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8369 && IT_CHARPOS (*it) > to_charpos)
8370 goto buffer_pos_reached;
8371 else
8372 result = MOVE_NEWLINE_OR_CR;
8374 else
8375 result = MOVE_NEWLINE_OR_CR;
8376 break;
8379 prev_method = it->method;
8380 if (it->method == GET_FROM_BUFFER)
8381 prev_pos = IT_CHARPOS (*it);
8382 /* The current display element has been consumed. Advance
8383 to the next. */
8384 set_iterator_to_next (it, 1);
8385 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8386 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8387 if (IT_CHARPOS (*it) < to_charpos)
8388 saw_smaller_pos = 1;
8389 if (it->bidi_p
8390 && (op & MOVE_TO_POS)
8391 && IT_CHARPOS (*it) >= to_charpos
8392 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8393 SAVE_IT (ppos_it, *it, ppos_data);
8395 /* Stop if lines are truncated and IT's current x-position is
8396 past the right edge of the window now. */
8397 if (it->line_wrap == TRUNCATE
8398 && it->current_x >= it->last_visible_x)
8400 if (!FRAME_WINDOW_P (it->f)
8401 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8403 int at_eob_p = 0;
8405 if ((at_eob_p = !get_next_display_element (it))
8406 || BUFFER_POS_REACHED_P ()
8407 /* If we are past TO_CHARPOS, but never saw any
8408 character positions smaller than TO_CHARPOS,
8409 return MOVE_POS_MATCH_OR_ZV, like the
8410 unidirectional display did. */
8411 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8412 && !saw_smaller_pos
8413 && IT_CHARPOS (*it) > to_charpos))
8415 if (it->bidi_p
8416 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8417 RESTORE_IT (it, &ppos_it, ppos_data);
8418 result = MOVE_POS_MATCH_OR_ZV;
8419 break;
8421 if (ITERATOR_AT_END_OF_LINE_P (it))
8423 result = MOVE_NEWLINE_OR_CR;
8424 break;
8427 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8428 && !saw_smaller_pos
8429 && IT_CHARPOS (*it) > to_charpos)
8431 if (IT_CHARPOS (ppos_it) < ZV)
8432 RESTORE_IT (it, &ppos_it, ppos_data);
8433 result = MOVE_POS_MATCH_OR_ZV;
8434 break;
8436 result = MOVE_LINE_TRUNCATED;
8437 break;
8439 #undef IT_RESET_X_ASCENT_DESCENT
8442 #undef BUFFER_POS_REACHED_P
8444 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8445 restore the saved iterator. */
8446 if (atpos_it.sp >= 0)
8447 RESTORE_IT (it, &atpos_it, atpos_data);
8448 else if (atx_it.sp >= 0)
8449 RESTORE_IT (it, &atx_it, atx_data);
8451 done:
8453 if (atpos_data)
8454 bidi_unshelve_cache (atpos_data, 1);
8455 if (atx_data)
8456 bidi_unshelve_cache (atx_data, 1);
8457 if (wrap_data)
8458 bidi_unshelve_cache (wrap_data, 1);
8459 if (ppos_data)
8460 bidi_unshelve_cache (ppos_data, 1);
8462 /* Restore the iterator settings altered at the beginning of this
8463 function. */
8464 it->glyph_row = saved_glyph_row;
8465 return result;
8468 /* For external use. */
8469 void
8470 move_it_in_display_line (struct it *it,
8471 EMACS_INT to_charpos, int to_x,
8472 enum move_operation_enum op)
8474 if (it->line_wrap == WORD_WRAP
8475 && (op & MOVE_TO_X))
8477 struct it save_it;
8478 void *save_data = NULL;
8479 int skip;
8481 SAVE_IT (save_it, *it, save_data);
8482 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8483 /* When word-wrap is on, TO_X may lie past the end
8484 of a wrapped line. Then it->current is the
8485 character on the next line, so backtrack to the
8486 space before the wrap point. */
8487 if (skip == MOVE_LINE_CONTINUED)
8489 int prev_x = max (it->current_x - 1, 0);
8490 RESTORE_IT (it, &save_it, save_data);
8491 move_it_in_display_line_to
8492 (it, -1, prev_x, MOVE_TO_X);
8494 else
8495 bidi_unshelve_cache (save_data, 1);
8497 else
8498 move_it_in_display_line_to (it, to_charpos, to_x, op);
8502 /* Move IT forward until it satisfies one or more of the criteria in
8503 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8505 OP is a bit-mask that specifies where to stop, and in particular,
8506 which of those four position arguments makes a difference. See the
8507 description of enum move_operation_enum.
8509 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8510 screen line, this function will set IT to the next position that is
8511 displayed to the right of TO_CHARPOS on the screen. */
8513 void
8514 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
8516 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8517 int line_height, line_start_x = 0, reached = 0;
8518 void *backup_data = NULL;
8520 for (;;)
8522 if (op & MOVE_TO_VPOS)
8524 /* If no TO_CHARPOS and no TO_X specified, stop at the
8525 start of the line TO_VPOS. */
8526 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8528 if (it->vpos == to_vpos)
8530 reached = 1;
8531 break;
8533 else
8534 skip = move_it_in_display_line_to (it, -1, -1, 0);
8536 else
8538 /* TO_VPOS >= 0 means stop at TO_X in the line at
8539 TO_VPOS, or at TO_POS, whichever comes first. */
8540 if (it->vpos == to_vpos)
8542 reached = 2;
8543 break;
8546 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8548 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8550 reached = 3;
8551 break;
8553 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8555 /* We have reached TO_X but not in the line we want. */
8556 skip = move_it_in_display_line_to (it, to_charpos,
8557 -1, MOVE_TO_POS);
8558 if (skip == MOVE_POS_MATCH_OR_ZV)
8560 reached = 4;
8561 break;
8566 else if (op & MOVE_TO_Y)
8568 struct it it_backup;
8570 if (it->line_wrap == WORD_WRAP)
8571 SAVE_IT (it_backup, *it, backup_data);
8573 /* TO_Y specified means stop at TO_X in the line containing
8574 TO_Y---or at TO_CHARPOS if this is reached first. The
8575 problem is that we can't really tell whether the line
8576 contains TO_Y before we have completely scanned it, and
8577 this may skip past TO_X. What we do is to first scan to
8578 TO_X.
8580 If TO_X is not specified, use a TO_X of zero. The reason
8581 is to make the outcome of this function more predictable.
8582 If we didn't use TO_X == 0, we would stop at the end of
8583 the line which is probably not what a caller would expect
8584 to happen. */
8585 skip = move_it_in_display_line_to
8586 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8587 (MOVE_TO_X | (op & MOVE_TO_POS)));
8589 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8590 if (skip == MOVE_POS_MATCH_OR_ZV)
8591 reached = 5;
8592 else if (skip == MOVE_X_REACHED)
8594 /* If TO_X was reached, we want to know whether TO_Y is
8595 in the line. We know this is the case if the already
8596 scanned glyphs make the line tall enough. Otherwise,
8597 we must check by scanning the rest of the line. */
8598 line_height = it->max_ascent + it->max_descent;
8599 if (to_y >= it->current_y
8600 && to_y < it->current_y + line_height)
8602 reached = 6;
8603 break;
8605 SAVE_IT (it_backup, *it, backup_data);
8606 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8607 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8608 op & MOVE_TO_POS);
8609 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8610 line_height = it->max_ascent + it->max_descent;
8611 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8613 if (to_y >= it->current_y
8614 && to_y < it->current_y + line_height)
8616 /* If TO_Y is in this line and TO_X was reached
8617 above, we scanned too far. We have to restore
8618 IT's settings to the ones before skipping. */
8619 RESTORE_IT (it, &it_backup, backup_data);
8620 reached = 6;
8622 else
8624 skip = skip2;
8625 if (skip == MOVE_POS_MATCH_OR_ZV)
8626 reached = 7;
8629 else
8631 /* Check whether TO_Y is in this line. */
8632 line_height = it->max_ascent + it->max_descent;
8633 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8635 if (to_y >= it->current_y
8636 && to_y < it->current_y + line_height)
8638 /* When word-wrap is on, TO_X may lie past the end
8639 of a wrapped line. Then it->current is the
8640 character on the next line, so backtrack to the
8641 space before the wrap point. */
8642 if (skip == MOVE_LINE_CONTINUED
8643 && it->line_wrap == WORD_WRAP)
8645 int prev_x = max (it->current_x - 1, 0);
8646 RESTORE_IT (it, &it_backup, backup_data);
8647 skip = move_it_in_display_line_to
8648 (it, -1, prev_x, MOVE_TO_X);
8650 reached = 6;
8654 if (reached)
8655 break;
8657 else if (BUFFERP (it->object)
8658 && (it->method == GET_FROM_BUFFER
8659 || it->method == GET_FROM_STRETCH)
8660 && IT_CHARPOS (*it) >= to_charpos
8661 /* Under bidi iteration, a call to set_iterator_to_next
8662 can scan far beyond to_charpos if the initial
8663 portion of the next line needs to be reordered. In
8664 that case, give move_it_in_display_line_to another
8665 chance below. */
8666 && !(it->bidi_p
8667 && it->bidi_it.scan_dir == -1))
8668 skip = MOVE_POS_MATCH_OR_ZV;
8669 else
8670 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8672 switch (skip)
8674 case MOVE_POS_MATCH_OR_ZV:
8675 reached = 8;
8676 goto out;
8678 case MOVE_NEWLINE_OR_CR:
8679 set_iterator_to_next (it, 1);
8680 it->continuation_lines_width = 0;
8681 break;
8683 case MOVE_LINE_TRUNCATED:
8684 it->continuation_lines_width = 0;
8685 reseat_at_next_visible_line_start (it, 0);
8686 if ((op & MOVE_TO_POS) != 0
8687 && IT_CHARPOS (*it) > to_charpos)
8689 reached = 9;
8690 goto out;
8692 break;
8694 case MOVE_LINE_CONTINUED:
8695 /* For continued lines ending in a tab, some of the glyphs
8696 associated with the tab are displayed on the current
8697 line. Since it->current_x does not include these glyphs,
8698 we use it->last_visible_x instead. */
8699 if (it->c == '\t')
8701 it->continuation_lines_width += it->last_visible_x;
8702 /* When moving by vpos, ensure that the iterator really
8703 advances to the next line (bug#847, bug#969). Fixme:
8704 do we need to do this in other circumstances? */
8705 if (it->current_x != it->last_visible_x
8706 && (op & MOVE_TO_VPOS)
8707 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8709 line_start_x = it->current_x + it->pixel_width
8710 - it->last_visible_x;
8711 set_iterator_to_next (it, 0);
8714 else
8715 it->continuation_lines_width += it->current_x;
8716 break;
8718 default:
8719 abort ();
8722 /* Reset/increment for the next run. */
8723 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8724 it->current_x = line_start_x;
8725 line_start_x = 0;
8726 it->hpos = 0;
8727 it->current_y += it->max_ascent + it->max_descent;
8728 ++it->vpos;
8729 last_height = it->max_ascent + it->max_descent;
8730 last_max_ascent = it->max_ascent;
8731 it->max_ascent = it->max_descent = 0;
8734 out:
8736 /* On text terminals, we may stop at the end of a line in the middle
8737 of a multi-character glyph. If the glyph itself is continued,
8738 i.e. it is actually displayed on the next line, don't treat this
8739 stopping point as valid; move to the next line instead (unless
8740 that brings us offscreen). */
8741 if (!FRAME_WINDOW_P (it->f)
8742 && op & MOVE_TO_POS
8743 && IT_CHARPOS (*it) == to_charpos
8744 && it->what == IT_CHARACTER
8745 && it->nglyphs > 1
8746 && it->line_wrap == WINDOW_WRAP
8747 && it->current_x == it->last_visible_x - 1
8748 && it->c != '\n'
8749 && it->c != '\t'
8750 && it->vpos < XFASTINT (it->w->window_end_vpos))
8752 it->continuation_lines_width += it->current_x;
8753 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8754 it->current_y += it->max_ascent + it->max_descent;
8755 ++it->vpos;
8756 last_height = it->max_ascent + it->max_descent;
8757 last_max_ascent = it->max_ascent;
8760 if (backup_data)
8761 bidi_unshelve_cache (backup_data, 1);
8763 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8767 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8769 If DY > 0, move IT backward at least that many pixels. DY = 0
8770 means move IT backward to the preceding line start or BEGV. This
8771 function may move over more than DY pixels if IT->current_y - DY
8772 ends up in the middle of a line; in this case IT->current_y will be
8773 set to the top of the line moved to. */
8775 void
8776 move_it_vertically_backward (struct it *it, int dy)
8778 int nlines, h;
8779 struct it it2, it3;
8780 void *it2data = NULL, *it3data = NULL;
8781 EMACS_INT start_pos;
8783 move_further_back:
8784 xassert (dy >= 0);
8786 start_pos = IT_CHARPOS (*it);
8788 /* Estimate how many newlines we must move back. */
8789 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8791 /* Set the iterator's position that many lines back. */
8792 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8793 back_to_previous_visible_line_start (it);
8795 /* Reseat the iterator here. When moving backward, we don't want
8796 reseat to skip forward over invisible text, set up the iterator
8797 to deliver from overlay strings at the new position etc. So,
8798 use reseat_1 here. */
8799 reseat_1 (it, it->current.pos, 1);
8801 /* We are now surely at a line start. */
8802 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8803 reordering is in effect. */
8804 it->continuation_lines_width = 0;
8806 /* Move forward and see what y-distance we moved. First move to the
8807 start of the next line so that we get its height. We need this
8808 height to be able to tell whether we reached the specified
8809 y-distance. */
8810 SAVE_IT (it2, *it, it2data);
8811 it2.max_ascent = it2.max_descent = 0;
8814 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8815 MOVE_TO_POS | MOVE_TO_VPOS);
8817 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8818 /* If we are in a display string which starts at START_POS,
8819 and that display string includes a newline, and we are
8820 right after that newline (i.e. at the beginning of a
8821 display line), exit the loop, because otherwise we will
8822 infloop, since move_it_to will see that it is already at
8823 START_POS and will not move. */
8824 || (it2.method == GET_FROM_STRING
8825 && IT_CHARPOS (it2) == start_pos
8826 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
8827 xassert (IT_CHARPOS (*it) >= BEGV);
8828 SAVE_IT (it3, it2, it3data);
8830 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8831 xassert (IT_CHARPOS (*it) >= BEGV);
8832 /* H is the actual vertical distance from the position in *IT
8833 and the starting position. */
8834 h = it2.current_y - it->current_y;
8835 /* NLINES is the distance in number of lines. */
8836 nlines = it2.vpos - it->vpos;
8838 /* Correct IT's y and vpos position
8839 so that they are relative to the starting point. */
8840 it->vpos -= nlines;
8841 it->current_y -= h;
8843 if (dy == 0)
8845 /* DY == 0 means move to the start of the screen line. The
8846 value of nlines is > 0 if continuation lines were involved,
8847 or if the original IT position was at start of a line. */
8848 RESTORE_IT (it, it, it2data);
8849 if (nlines > 0)
8850 move_it_by_lines (it, nlines);
8851 /* The above code moves us to some position NLINES down,
8852 usually to its first glyph (leftmost in an L2R line), but
8853 that's not necessarily the start of the line, under bidi
8854 reordering. We want to get to the character position
8855 that is immediately after the newline of the previous
8856 line. */
8857 if (it->bidi_p
8858 && !it->continuation_lines_width
8859 && !STRINGP (it->string)
8860 && IT_CHARPOS (*it) > BEGV
8861 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8863 EMACS_INT nl_pos =
8864 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
8866 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
8868 bidi_unshelve_cache (it3data, 1);
8870 else
8872 /* The y-position we try to reach, relative to *IT.
8873 Note that H has been subtracted in front of the if-statement. */
8874 int target_y = it->current_y + h - dy;
8875 int y0 = it3.current_y;
8876 int y1;
8877 int line_height;
8879 RESTORE_IT (&it3, &it3, it3data);
8880 y1 = line_bottom_y (&it3);
8881 line_height = y1 - y0;
8882 RESTORE_IT (it, it, it2data);
8883 /* If we did not reach target_y, try to move further backward if
8884 we can. If we moved too far backward, try to move forward. */
8885 if (target_y < it->current_y
8886 /* This is heuristic. In a window that's 3 lines high, with
8887 a line height of 13 pixels each, recentering with point
8888 on the bottom line will try to move -39/2 = 19 pixels
8889 backward. Try to avoid moving into the first line. */
8890 && (it->current_y - target_y
8891 > min (window_box_height (it->w), line_height * 2 / 3))
8892 && IT_CHARPOS (*it) > BEGV)
8894 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8895 target_y - it->current_y));
8896 dy = it->current_y - target_y;
8897 goto move_further_back;
8899 else if (target_y >= it->current_y + line_height
8900 && IT_CHARPOS (*it) < ZV)
8902 /* Should move forward by at least one line, maybe more.
8904 Note: Calling move_it_by_lines can be expensive on
8905 terminal frames, where compute_motion is used (via
8906 vmotion) to do the job, when there are very long lines
8907 and truncate-lines is nil. That's the reason for
8908 treating terminal frames specially here. */
8910 if (!FRAME_WINDOW_P (it->f))
8911 move_it_vertically (it, target_y - (it->current_y + line_height));
8912 else
8916 move_it_by_lines (it, 1);
8918 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
8925 /* Move IT by a specified amount of pixel lines DY. DY negative means
8926 move backwards. DY = 0 means move to start of screen line. At the
8927 end, IT will be on the start of a screen line. */
8929 void
8930 move_it_vertically (struct it *it, int dy)
8932 if (dy <= 0)
8933 move_it_vertically_backward (it, -dy);
8934 else
8936 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
8937 move_it_to (it, ZV, -1, it->current_y + dy, -1,
8938 MOVE_TO_POS | MOVE_TO_Y);
8939 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
8941 /* If buffer ends in ZV without a newline, move to the start of
8942 the line to satisfy the post-condition. */
8943 if (IT_CHARPOS (*it) == ZV
8944 && ZV > BEGV
8945 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8946 move_it_by_lines (it, 0);
8951 /* Move iterator IT past the end of the text line it is in. */
8953 void
8954 move_it_past_eol (struct it *it)
8956 enum move_it_result rc;
8958 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
8959 if (rc == MOVE_NEWLINE_OR_CR)
8960 set_iterator_to_next (it, 0);
8964 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
8965 negative means move up. DVPOS == 0 means move to the start of the
8966 screen line.
8968 Optimization idea: If we would know that IT->f doesn't use
8969 a face with proportional font, we could be faster for
8970 truncate-lines nil. */
8972 void
8973 move_it_by_lines (struct it *it, int dvpos)
8976 /* The commented-out optimization uses vmotion on terminals. This
8977 gives bad results, because elements like it->what, on which
8978 callers such as pos_visible_p rely, aren't updated. */
8979 /* struct position pos;
8980 if (!FRAME_WINDOW_P (it->f))
8982 struct text_pos textpos;
8984 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
8985 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
8986 reseat (it, textpos, 1);
8987 it->vpos += pos.vpos;
8988 it->current_y += pos.vpos;
8990 else */
8992 if (dvpos == 0)
8994 /* DVPOS == 0 means move to the start of the screen line. */
8995 move_it_vertically_backward (it, 0);
8996 /* Let next call to line_bottom_y calculate real line height */
8997 last_height = 0;
8999 else if (dvpos > 0)
9001 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9002 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9004 /* Only move to the next buffer position if we ended up in a
9005 string from display property, not in an overlay string
9006 (before-string or after-string). That is because the
9007 latter don't conceal the underlying buffer position, so
9008 we can ask to move the iterator to the exact position we
9009 are interested in. Note that, even if we are already at
9010 IT_CHARPOS (*it), the call below is not a no-op, as it
9011 will detect that we are at the end of the string, pop the
9012 iterator, and compute it->current_x and it->hpos
9013 correctly. */
9014 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9015 -1, -1, -1, MOVE_TO_POS);
9018 else
9020 struct it it2;
9021 void *it2data = NULL;
9022 EMACS_INT start_charpos, i;
9024 /* Start at the beginning of the screen line containing IT's
9025 position. This may actually move vertically backwards,
9026 in case of overlays, so adjust dvpos accordingly. */
9027 dvpos += it->vpos;
9028 move_it_vertically_backward (it, 0);
9029 dvpos -= it->vpos;
9031 /* Go back -DVPOS visible lines and reseat the iterator there. */
9032 start_charpos = IT_CHARPOS (*it);
9033 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9034 back_to_previous_visible_line_start (it);
9035 reseat (it, it->current.pos, 1);
9037 /* Move further back if we end up in a string or an image. */
9038 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9040 /* First try to move to start of display line. */
9041 dvpos += it->vpos;
9042 move_it_vertically_backward (it, 0);
9043 dvpos -= it->vpos;
9044 if (IT_POS_VALID_AFTER_MOVE_P (it))
9045 break;
9046 /* If start of line is still in string or image,
9047 move further back. */
9048 back_to_previous_visible_line_start (it);
9049 reseat (it, it->current.pos, 1);
9050 dvpos--;
9053 it->current_x = it->hpos = 0;
9055 /* Above call may have moved too far if continuation lines
9056 are involved. Scan forward and see if it did. */
9057 SAVE_IT (it2, *it, it2data);
9058 it2.vpos = it2.current_y = 0;
9059 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9060 it->vpos -= it2.vpos;
9061 it->current_y -= it2.current_y;
9062 it->current_x = it->hpos = 0;
9064 /* If we moved too far back, move IT some lines forward. */
9065 if (it2.vpos > -dvpos)
9067 int delta = it2.vpos + dvpos;
9069 RESTORE_IT (&it2, &it2, it2data);
9070 SAVE_IT (it2, *it, it2data);
9071 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9072 /* Move back again if we got too far ahead. */
9073 if (IT_CHARPOS (*it) >= start_charpos)
9074 RESTORE_IT (it, &it2, it2data);
9075 else
9076 bidi_unshelve_cache (it2data, 1);
9078 else
9079 RESTORE_IT (it, it, it2data);
9083 /* Return 1 if IT points into the middle of a display vector. */
9086 in_display_vector_p (struct it *it)
9088 return (it->method == GET_FROM_DISPLAY_VECTOR
9089 && it->current.dpvec_index > 0
9090 && it->dpvec + it->current.dpvec_index != it->dpend);
9094 /***********************************************************************
9095 Messages
9096 ***********************************************************************/
9099 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9100 to *Messages*. */
9102 void
9103 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9105 Lisp_Object args[3];
9106 Lisp_Object msg, fmt;
9107 char *buffer;
9108 EMACS_INT len;
9109 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9110 USE_SAFE_ALLOCA;
9112 /* Do nothing if called asynchronously. Inserting text into
9113 a buffer may call after-change-functions and alike and
9114 that would means running Lisp asynchronously. */
9115 if (handling_signal)
9116 return;
9118 fmt = msg = Qnil;
9119 GCPRO4 (fmt, msg, arg1, arg2);
9121 args[0] = fmt = build_string (format);
9122 args[1] = arg1;
9123 args[2] = arg2;
9124 msg = Fformat (3, args);
9126 len = SBYTES (msg) + 1;
9127 SAFE_ALLOCA (buffer, char *, len);
9128 memcpy (buffer, SDATA (msg), len);
9130 message_dolog (buffer, len - 1, 1, 0);
9131 SAFE_FREE ();
9133 UNGCPRO;
9137 /* Output a newline in the *Messages* buffer if "needs" one. */
9139 void
9140 message_log_maybe_newline (void)
9142 if (message_log_need_newline)
9143 message_dolog ("", 0, 1, 0);
9147 /* Add a string M of length NBYTES to the message log, optionally
9148 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9149 nonzero, means interpret the contents of M as multibyte. This
9150 function calls low-level routines in order to bypass text property
9151 hooks, etc. which might not be safe to run.
9153 This may GC (insert may run before/after change hooks),
9154 so the buffer M must NOT point to a Lisp string. */
9156 void
9157 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
9159 const unsigned char *msg = (const unsigned char *) m;
9161 if (!NILP (Vmemory_full))
9162 return;
9164 if (!NILP (Vmessage_log_max))
9166 struct buffer *oldbuf;
9167 Lisp_Object oldpoint, oldbegv, oldzv;
9168 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9169 EMACS_INT point_at_end = 0;
9170 EMACS_INT zv_at_end = 0;
9171 Lisp_Object old_deactivate_mark, tem;
9172 struct gcpro gcpro1;
9174 old_deactivate_mark = Vdeactivate_mark;
9175 oldbuf = current_buffer;
9176 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9177 BVAR (current_buffer, undo_list) = Qt;
9179 oldpoint = message_dolog_marker1;
9180 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9181 oldbegv = message_dolog_marker2;
9182 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9183 oldzv = message_dolog_marker3;
9184 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9185 GCPRO1 (old_deactivate_mark);
9187 if (PT == Z)
9188 point_at_end = 1;
9189 if (ZV == Z)
9190 zv_at_end = 1;
9192 BEGV = BEG;
9193 BEGV_BYTE = BEG_BYTE;
9194 ZV = Z;
9195 ZV_BYTE = Z_BYTE;
9196 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9198 /* Insert the string--maybe converting multibyte to single byte
9199 or vice versa, so that all the text fits the buffer. */
9200 if (multibyte
9201 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9203 EMACS_INT i;
9204 int c, char_bytes;
9205 char work[1];
9207 /* Convert a multibyte string to single-byte
9208 for the *Message* buffer. */
9209 for (i = 0; i < nbytes; i += char_bytes)
9211 c = string_char_and_length (msg + i, &char_bytes);
9212 work[0] = (ASCII_CHAR_P (c)
9214 : multibyte_char_to_unibyte (c));
9215 insert_1_both (work, 1, 1, 1, 0, 0);
9218 else if (! multibyte
9219 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9221 EMACS_INT i;
9222 int c, char_bytes;
9223 unsigned char str[MAX_MULTIBYTE_LENGTH];
9224 /* Convert a single-byte string to multibyte
9225 for the *Message* buffer. */
9226 for (i = 0; i < nbytes; i++)
9228 c = msg[i];
9229 MAKE_CHAR_MULTIBYTE (c);
9230 char_bytes = CHAR_STRING (c, str);
9231 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9234 else if (nbytes)
9235 insert_1 (m, nbytes, 1, 0, 0);
9237 if (nlflag)
9239 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9240 printmax_t dups;
9241 insert_1 ("\n", 1, 1, 0, 0);
9243 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9244 this_bol = PT;
9245 this_bol_byte = PT_BYTE;
9247 /* See if this line duplicates the previous one.
9248 If so, combine duplicates. */
9249 if (this_bol > BEG)
9251 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9252 prev_bol = PT;
9253 prev_bol_byte = PT_BYTE;
9255 dups = message_log_check_duplicate (prev_bol_byte,
9256 this_bol_byte);
9257 if (dups)
9259 del_range_both (prev_bol, prev_bol_byte,
9260 this_bol, this_bol_byte, 0);
9261 if (dups > 1)
9263 char dupstr[sizeof " [ times]"
9264 + INT_STRLEN_BOUND (printmax_t)];
9265 int duplen;
9267 /* If you change this format, don't forget to also
9268 change message_log_check_duplicate. */
9269 sprintf (dupstr, " [%"pMd" times]", dups);
9270 duplen = strlen (dupstr);
9271 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9272 insert_1 (dupstr, duplen, 1, 0, 1);
9277 /* If we have more than the desired maximum number of lines
9278 in the *Messages* buffer now, delete the oldest ones.
9279 This is safe because we don't have undo in this buffer. */
9281 if (NATNUMP (Vmessage_log_max))
9283 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9284 -XFASTINT (Vmessage_log_max) - 1, 0);
9285 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9288 BEGV = XMARKER (oldbegv)->charpos;
9289 BEGV_BYTE = marker_byte_position (oldbegv);
9291 if (zv_at_end)
9293 ZV = Z;
9294 ZV_BYTE = Z_BYTE;
9296 else
9298 ZV = XMARKER (oldzv)->charpos;
9299 ZV_BYTE = marker_byte_position (oldzv);
9302 if (point_at_end)
9303 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9304 else
9305 /* We can't do Fgoto_char (oldpoint) because it will run some
9306 Lisp code. */
9307 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9308 XMARKER (oldpoint)->bytepos);
9310 UNGCPRO;
9311 unchain_marker (XMARKER (oldpoint));
9312 unchain_marker (XMARKER (oldbegv));
9313 unchain_marker (XMARKER (oldzv));
9315 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9316 set_buffer_internal (oldbuf);
9317 if (NILP (tem))
9318 windows_or_buffers_changed = old_windows_or_buffers_changed;
9319 message_log_need_newline = !nlflag;
9320 Vdeactivate_mark = old_deactivate_mark;
9325 /* We are at the end of the buffer after just having inserted a newline.
9326 (Note: We depend on the fact we won't be crossing the gap.)
9327 Check to see if the most recent message looks a lot like the previous one.
9328 Return 0 if different, 1 if the new one should just replace it, or a
9329 value N > 1 if we should also append " [N times]". */
9331 static intmax_t
9332 message_log_check_duplicate (EMACS_INT prev_bol_byte, EMACS_INT this_bol_byte)
9334 EMACS_INT i;
9335 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
9336 int seen_dots = 0;
9337 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9338 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9340 for (i = 0; i < len; i++)
9342 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9343 seen_dots = 1;
9344 if (p1[i] != p2[i])
9345 return seen_dots;
9347 p1 += len;
9348 if (*p1 == '\n')
9349 return 2;
9350 if (*p1++ == ' ' && *p1++ == '[')
9352 char *pend;
9353 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9354 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9355 return n+1;
9357 return 0;
9361 /* Display an echo area message M with a specified length of NBYTES
9362 bytes. The string may include null characters. If M is 0, clear
9363 out any existing message, and let the mini-buffer text show
9364 through.
9366 This may GC, so the buffer M must NOT point to a Lisp string. */
9368 void
9369 message2 (const char *m, EMACS_INT nbytes, int multibyte)
9371 /* First flush out any partial line written with print. */
9372 message_log_maybe_newline ();
9373 if (m)
9374 message_dolog (m, nbytes, 1, multibyte);
9375 message2_nolog (m, nbytes, multibyte);
9379 /* The non-logging counterpart of message2. */
9381 void
9382 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
9384 struct frame *sf = SELECTED_FRAME ();
9385 message_enable_multibyte = multibyte;
9387 if (FRAME_INITIAL_P (sf))
9389 if (noninteractive_need_newline)
9390 putc ('\n', stderr);
9391 noninteractive_need_newline = 0;
9392 if (m)
9393 fwrite (m, nbytes, 1, stderr);
9394 if (cursor_in_echo_area == 0)
9395 fprintf (stderr, "\n");
9396 fflush (stderr);
9398 /* A null message buffer means that the frame hasn't really been
9399 initialized yet. Error messages get reported properly by
9400 cmd_error, so this must be just an informative message; toss it. */
9401 else if (INTERACTIVE
9402 && sf->glyphs_initialized_p
9403 && FRAME_MESSAGE_BUF (sf))
9405 Lisp_Object mini_window;
9406 struct frame *f;
9408 /* Get the frame containing the mini-buffer
9409 that the selected frame is using. */
9410 mini_window = FRAME_MINIBUF_WINDOW (sf);
9411 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9413 FRAME_SAMPLE_VISIBILITY (f);
9414 if (FRAME_VISIBLE_P (sf)
9415 && ! FRAME_VISIBLE_P (f))
9416 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9418 if (m)
9420 set_message (m, Qnil, nbytes, multibyte);
9421 if (minibuffer_auto_raise)
9422 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9424 else
9425 clear_message (1, 1);
9427 do_pending_window_change (0);
9428 echo_area_display (1);
9429 do_pending_window_change (0);
9430 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9431 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9436 /* Display an echo area message M with a specified length of NBYTES
9437 bytes. The string may include null characters. If M is not a
9438 string, clear out any existing message, and let the mini-buffer
9439 text show through.
9441 This function cancels echoing. */
9443 void
9444 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9446 struct gcpro gcpro1;
9448 GCPRO1 (m);
9449 clear_message (1,1);
9450 cancel_echoing ();
9452 /* First flush out any partial line written with print. */
9453 message_log_maybe_newline ();
9454 if (STRINGP (m))
9456 char *buffer;
9457 USE_SAFE_ALLOCA;
9459 SAFE_ALLOCA (buffer, char *, nbytes);
9460 memcpy (buffer, SDATA (m), nbytes);
9461 message_dolog (buffer, nbytes, 1, multibyte);
9462 SAFE_FREE ();
9464 message3_nolog (m, nbytes, multibyte);
9466 UNGCPRO;
9470 /* The non-logging version of message3.
9471 This does not cancel echoing, because it is used for echoing.
9472 Perhaps we need to make a separate function for echoing
9473 and make this cancel echoing. */
9475 void
9476 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9478 struct frame *sf = SELECTED_FRAME ();
9479 message_enable_multibyte = multibyte;
9481 if (FRAME_INITIAL_P (sf))
9483 if (noninteractive_need_newline)
9484 putc ('\n', stderr);
9485 noninteractive_need_newline = 0;
9486 if (STRINGP (m))
9487 fwrite (SDATA (m), nbytes, 1, stderr);
9488 if (cursor_in_echo_area == 0)
9489 fprintf (stderr, "\n");
9490 fflush (stderr);
9492 /* A null message buffer means that the frame hasn't really been
9493 initialized yet. Error messages get reported properly by
9494 cmd_error, so this must be just an informative message; toss it. */
9495 else if (INTERACTIVE
9496 && sf->glyphs_initialized_p
9497 && FRAME_MESSAGE_BUF (sf))
9499 Lisp_Object mini_window;
9500 Lisp_Object frame;
9501 struct frame *f;
9503 /* Get the frame containing the mini-buffer
9504 that the selected frame is using. */
9505 mini_window = FRAME_MINIBUF_WINDOW (sf);
9506 frame = XWINDOW (mini_window)->frame;
9507 f = XFRAME (frame);
9509 FRAME_SAMPLE_VISIBILITY (f);
9510 if (FRAME_VISIBLE_P (sf)
9511 && !FRAME_VISIBLE_P (f))
9512 Fmake_frame_visible (frame);
9514 if (STRINGP (m) && SCHARS (m) > 0)
9516 set_message (NULL, m, nbytes, multibyte);
9517 if (minibuffer_auto_raise)
9518 Fraise_frame (frame);
9519 /* Assume we are not echoing.
9520 (If we are, echo_now will override this.) */
9521 echo_message_buffer = Qnil;
9523 else
9524 clear_message (1, 1);
9526 do_pending_window_change (0);
9527 echo_area_display (1);
9528 do_pending_window_change (0);
9529 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9530 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9535 /* Display a null-terminated echo area message M. If M is 0, clear
9536 out any existing message, and let the mini-buffer text show through.
9538 The buffer M must continue to exist until after the echo area gets
9539 cleared or some other message gets displayed there. Do not pass
9540 text that is stored in a Lisp string. Do not pass text in a buffer
9541 that was alloca'd. */
9543 void
9544 message1 (const char *m)
9546 message2 (m, (m ? strlen (m) : 0), 0);
9550 /* The non-logging counterpart of message1. */
9552 void
9553 message1_nolog (const char *m)
9555 message2_nolog (m, (m ? strlen (m) : 0), 0);
9558 /* Display a message M which contains a single %s
9559 which gets replaced with STRING. */
9561 void
9562 message_with_string (const char *m, Lisp_Object string, int log)
9564 CHECK_STRING (string);
9566 if (noninteractive)
9568 if (m)
9570 if (noninteractive_need_newline)
9571 putc ('\n', stderr);
9572 noninteractive_need_newline = 0;
9573 fprintf (stderr, m, SDATA (string));
9574 if (!cursor_in_echo_area)
9575 fprintf (stderr, "\n");
9576 fflush (stderr);
9579 else if (INTERACTIVE)
9581 /* The frame whose minibuffer we're going to display the message on.
9582 It may be larger than the selected frame, so we need
9583 to use its buffer, not the selected frame's buffer. */
9584 Lisp_Object mini_window;
9585 struct frame *f, *sf = SELECTED_FRAME ();
9587 /* Get the frame containing the minibuffer
9588 that the selected frame is using. */
9589 mini_window = FRAME_MINIBUF_WINDOW (sf);
9590 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9592 /* A null message buffer means that the frame hasn't really been
9593 initialized yet. Error messages get reported properly by
9594 cmd_error, so this must be just an informative message; toss it. */
9595 if (FRAME_MESSAGE_BUF (f))
9597 Lisp_Object args[2], msg;
9598 struct gcpro gcpro1, gcpro2;
9600 args[0] = build_string (m);
9601 args[1] = msg = string;
9602 GCPRO2 (args[0], msg);
9603 gcpro1.nvars = 2;
9605 msg = Fformat (2, args);
9607 if (log)
9608 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9609 else
9610 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9612 UNGCPRO;
9614 /* Print should start at the beginning of the message
9615 buffer next time. */
9616 message_buf_print = 0;
9622 /* Dump an informative message to the minibuf. If M is 0, clear out
9623 any existing message, and let the mini-buffer text show through. */
9625 static void
9626 vmessage (const char *m, va_list ap)
9628 if (noninteractive)
9630 if (m)
9632 if (noninteractive_need_newline)
9633 putc ('\n', stderr);
9634 noninteractive_need_newline = 0;
9635 vfprintf (stderr, m, ap);
9636 if (cursor_in_echo_area == 0)
9637 fprintf (stderr, "\n");
9638 fflush (stderr);
9641 else if (INTERACTIVE)
9643 /* The frame whose mini-buffer we're going to display the message
9644 on. It may be larger than the selected frame, so we need to
9645 use its buffer, not the selected frame's buffer. */
9646 Lisp_Object mini_window;
9647 struct frame *f, *sf = SELECTED_FRAME ();
9649 /* Get the frame containing the mini-buffer
9650 that the selected frame is using. */
9651 mini_window = FRAME_MINIBUF_WINDOW (sf);
9652 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9654 /* A null message buffer means that the frame hasn't really been
9655 initialized yet. Error messages get reported properly by
9656 cmd_error, so this must be just an informative message; toss
9657 it. */
9658 if (FRAME_MESSAGE_BUF (f))
9660 if (m)
9662 ptrdiff_t len;
9664 len = doprnt (FRAME_MESSAGE_BUF (f),
9665 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9667 message2 (FRAME_MESSAGE_BUF (f), len, 0);
9669 else
9670 message1 (0);
9672 /* Print should start at the beginning of the message
9673 buffer next time. */
9674 message_buf_print = 0;
9679 void
9680 message (const char *m, ...)
9682 va_list ap;
9683 va_start (ap, m);
9684 vmessage (m, ap);
9685 va_end (ap);
9689 #if 0
9690 /* The non-logging version of message. */
9692 void
9693 message_nolog (const char *m, ...)
9695 Lisp_Object old_log_max;
9696 va_list ap;
9697 va_start (ap, m);
9698 old_log_max = Vmessage_log_max;
9699 Vmessage_log_max = Qnil;
9700 vmessage (m, ap);
9701 Vmessage_log_max = old_log_max;
9702 va_end (ap);
9704 #endif
9707 /* Display the current message in the current mini-buffer. This is
9708 only called from error handlers in process.c, and is not time
9709 critical. */
9711 void
9712 update_echo_area (void)
9714 if (!NILP (echo_area_buffer[0]))
9716 Lisp_Object string;
9717 string = Fcurrent_message ();
9718 message3 (string, SBYTES (string),
9719 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9724 /* Make sure echo area buffers in `echo_buffers' are live.
9725 If they aren't, make new ones. */
9727 static void
9728 ensure_echo_area_buffers (void)
9730 int i;
9732 for (i = 0; i < 2; ++i)
9733 if (!BUFFERP (echo_buffer[i])
9734 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9736 char name[30];
9737 Lisp_Object old_buffer;
9738 int j;
9740 old_buffer = echo_buffer[i];
9741 sprintf (name, " *Echo Area %d*", i);
9742 echo_buffer[i] = Fget_buffer_create (build_string (name));
9743 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9744 /* to force word wrap in echo area -
9745 it was decided to postpone this*/
9746 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9748 for (j = 0; j < 2; ++j)
9749 if (EQ (old_buffer, echo_area_buffer[j]))
9750 echo_area_buffer[j] = echo_buffer[i];
9755 /* Call FN with args A1..A4 with either the current or last displayed
9756 echo_area_buffer as current buffer.
9758 WHICH zero means use the current message buffer
9759 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9760 from echo_buffer[] and clear it.
9762 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9763 suitable buffer from echo_buffer[] and clear it.
9765 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9766 that the current message becomes the last displayed one, make
9767 choose a suitable buffer for echo_area_buffer[0], and clear it.
9769 Value is what FN returns. */
9771 static int
9772 with_echo_area_buffer (struct window *w, int which,
9773 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
9774 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9776 Lisp_Object buffer;
9777 int this_one, the_other, clear_buffer_p, rc;
9778 int count = SPECPDL_INDEX ();
9780 /* If buffers aren't live, make new ones. */
9781 ensure_echo_area_buffers ();
9783 clear_buffer_p = 0;
9785 if (which == 0)
9786 this_one = 0, the_other = 1;
9787 else if (which > 0)
9788 this_one = 1, the_other = 0;
9789 else
9791 this_one = 0, the_other = 1;
9792 clear_buffer_p = 1;
9794 /* We need a fresh one in case the current echo buffer equals
9795 the one containing the last displayed echo area message. */
9796 if (!NILP (echo_area_buffer[this_one])
9797 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9798 echo_area_buffer[this_one] = Qnil;
9801 /* Choose a suitable buffer from echo_buffer[] is we don't
9802 have one. */
9803 if (NILP (echo_area_buffer[this_one]))
9805 echo_area_buffer[this_one]
9806 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9807 ? echo_buffer[the_other]
9808 : echo_buffer[this_one]);
9809 clear_buffer_p = 1;
9812 buffer = echo_area_buffer[this_one];
9814 /* Don't get confused by reusing the buffer used for echoing
9815 for a different purpose. */
9816 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9817 cancel_echoing ();
9819 record_unwind_protect (unwind_with_echo_area_buffer,
9820 with_echo_area_buffer_unwind_data (w));
9822 /* Make the echo area buffer current. Note that for display
9823 purposes, it is not necessary that the displayed window's buffer
9824 == current_buffer, except for text property lookup. So, let's
9825 only set that buffer temporarily here without doing a full
9826 Fset_window_buffer. We must also change w->pointm, though,
9827 because otherwise an assertions in unshow_buffer fails, and Emacs
9828 aborts. */
9829 set_buffer_internal_1 (XBUFFER (buffer));
9830 if (w)
9832 w->buffer = buffer;
9833 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9836 BVAR (current_buffer, undo_list) = Qt;
9837 BVAR (current_buffer, read_only) = Qnil;
9838 specbind (Qinhibit_read_only, Qt);
9839 specbind (Qinhibit_modification_hooks, Qt);
9841 if (clear_buffer_p && Z > BEG)
9842 del_range (BEG, Z);
9844 xassert (BEGV >= BEG);
9845 xassert (ZV <= Z && ZV >= BEGV);
9847 rc = fn (a1, a2, a3, a4);
9849 xassert (BEGV >= BEG);
9850 xassert (ZV <= Z && ZV >= BEGV);
9852 unbind_to (count, Qnil);
9853 return rc;
9857 /* Save state that should be preserved around the call to the function
9858 FN called in with_echo_area_buffer. */
9860 static Lisp_Object
9861 with_echo_area_buffer_unwind_data (struct window *w)
9863 int i = 0;
9864 Lisp_Object vector, tmp;
9866 /* Reduce consing by keeping one vector in
9867 Vwith_echo_area_save_vector. */
9868 vector = Vwith_echo_area_save_vector;
9869 Vwith_echo_area_save_vector = Qnil;
9871 if (NILP (vector))
9872 vector = Fmake_vector (make_number (7), Qnil);
9874 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9875 ASET (vector, i, Vdeactivate_mark); ++i;
9876 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9878 if (w)
9880 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9881 ASET (vector, i, w->buffer); ++i;
9882 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9883 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9885 else
9887 int end = i + 4;
9888 for (; i < end; ++i)
9889 ASET (vector, i, Qnil);
9892 xassert (i == ASIZE (vector));
9893 return vector;
9897 /* Restore global state from VECTOR which was created by
9898 with_echo_area_buffer_unwind_data. */
9900 static Lisp_Object
9901 unwind_with_echo_area_buffer (Lisp_Object vector)
9903 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
9904 Vdeactivate_mark = AREF (vector, 1);
9905 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
9907 if (WINDOWP (AREF (vector, 3)))
9909 struct window *w;
9910 Lisp_Object buffer, charpos, bytepos;
9912 w = XWINDOW (AREF (vector, 3));
9913 buffer = AREF (vector, 4);
9914 charpos = AREF (vector, 5);
9915 bytepos = AREF (vector, 6);
9917 w->buffer = buffer;
9918 set_marker_both (w->pointm, buffer,
9919 XFASTINT (charpos), XFASTINT (bytepos));
9922 Vwith_echo_area_save_vector = vector;
9923 return Qnil;
9927 /* Set up the echo area for use by print functions. MULTIBYTE_P
9928 non-zero means we will print multibyte. */
9930 void
9931 setup_echo_area_for_printing (int multibyte_p)
9933 /* If we can't find an echo area any more, exit. */
9934 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
9935 Fkill_emacs (Qnil);
9937 ensure_echo_area_buffers ();
9939 if (!message_buf_print)
9941 /* A message has been output since the last time we printed.
9942 Choose a fresh echo area buffer. */
9943 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9944 echo_area_buffer[0] = echo_buffer[1];
9945 else
9946 echo_area_buffer[0] = echo_buffer[0];
9948 /* Switch to that buffer and clear it. */
9949 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9950 BVAR (current_buffer, truncate_lines) = Qnil;
9952 if (Z > BEG)
9954 int count = SPECPDL_INDEX ();
9955 specbind (Qinhibit_read_only, Qt);
9956 /* Note that undo recording is always disabled. */
9957 del_range (BEG, Z);
9958 unbind_to (count, Qnil);
9960 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9962 /* Set up the buffer for the multibyteness we need. */
9963 if (multibyte_p
9964 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9965 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
9967 /* Raise the frame containing the echo area. */
9968 if (minibuffer_auto_raise)
9970 struct frame *sf = SELECTED_FRAME ();
9971 Lisp_Object mini_window;
9972 mini_window = FRAME_MINIBUF_WINDOW (sf);
9973 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9976 message_log_maybe_newline ();
9977 message_buf_print = 1;
9979 else
9981 if (NILP (echo_area_buffer[0]))
9983 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9984 echo_area_buffer[0] = echo_buffer[1];
9985 else
9986 echo_area_buffer[0] = echo_buffer[0];
9989 if (current_buffer != XBUFFER (echo_area_buffer[0]))
9991 /* Someone switched buffers between print requests. */
9992 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9993 BVAR (current_buffer, truncate_lines) = Qnil;
9999 /* Display an echo area message in window W. Value is non-zero if W's
10000 height is changed. If display_last_displayed_message_p is
10001 non-zero, display the message that was last displayed, otherwise
10002 display the current message. */
10004 static int
10005 display_echo_area (struct window *w)
10007 int i, no_message_p, window_height_changed_p, count;
10009 /* Temporarily disable garbage collections while displaying the echo
10010 area. This is done because a GC can print a message itself.
10011 That message would modify the echo area buffer's contents while a
10012 redisplay of the buffer is going on, and seriously confuse
10013 redisplay. */
10014 count = inhibit_garbage_collection ();
10016 /* If there is no message, we must call display_echo_area_1
10017 nevertheless because it resizes the window. But we will have to
10018 reset the echo_area_buffer in question to nil at the end because
10019 with_echo_area_buffer will sets it to an empty buffer. */
10020 i = display_last_displayed_message_p ? 1 : 0;
10021 no_message_p = NILP (echo_area_buffer[i]);
10023 window_height_changed_p
10024 = with_echo_area_buffer (w, display_last_displayed_message_p,
10025 display_echo_area_1,
10026 (intptr_t) w, Qnil, 0, 0);
10028 if (no_message_p)
10029 echo_area_buffer[i] = Qnil;
10031 unbind_to (count, Qnil);
10032 return window_height_changed_p;
10036 /* Helper for display_echo_area. Display the current buffer which
10037 contains the current echo area message in window W, a mini-window,
10038 a pointer to which is passed in A1. A2..A4 are currently not used.
10039 Change the height of W so that all of the message is displayed.
10040 Value is non-zero if height of W was changed. */
10042 static int
10043 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10045 intptr_t i1 = a1;
10046 struct window *w = (struct window *) i1;
10047 Lisp_Object window;
10048 struct text_pos start;
10049 int window_height_changed_p = 0;
10051 /* Do this before displaying, so that we have a large enough glyph
10052 matrix for the display. If we can't get enough space for the
10053 whole text, display the last N lines. That works by setting w->start. */
10054 window_height_changed_p = resize_mini_window (w, 0);
10056 /* Use the starting position chosen by resize_mini_window. */
10057 SET_TEXT_POS_FROM_MARKER (start, w->start);
10059 /* Display. */
10060 clear_glyph_matrix (w->desired_matrix);
10061 XSETWINDOW (window, w);
10062 try_window (window, start, 0);
10064 return window_height_changed_p;
10068 /* Resize the echo area window to exactly the size needed for the
10069 currently displayed message, if there is one. If a mini-buffer
10070 is active, don't shrink it. */
10072 void
10073 resize_echo_area_exactly (void)
10075 if (BUFFERP (echo_area_buffer[0])
10076 && WINDOWP (echo_area_window))
10078 struct window *w = XWINDOW (echo_area_window);
10079 int resized_p;
10080 Lisp_Object resize_exactly;
10082 if (minibuf_level == 0)
10083 resize_exactly = Qt;
10084 else
10085 resize_exactly = Qnil;
10087 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10088 (intptr_t) w, resize_exactly,
10089 0, 0);
10090 if (resized_p)
10092 ++windows_or_buffers_changed;
10093 ++update_mode_lines;
10094 redisplay_internal ();
10100 /* Callback function for with_echo_area_buffer, when used from
10101 resize_echo_area_exactly. A1 contains a pointer to the window to
10102 resize, EXACTLY non-nil means resize the mini-window exactly to the
10103 size of the text displayed. A3 and A4 are not used. Value is what
10104 resize_mini_window returns. */
10106 static int
10107 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
10109 intptr_t i1 = a1;
10110 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10114 /* Resize mini-window W to fit the size of its contents. EXACT_P
10115 means size the window exactly to the size needed. Otherwise, it's
10116 only enlarged until W's buffer is empty.
10118 Set W->start to the right place to begin display. If the whole
10119 contents fit, start at the beginning. Otherwise, start so as
10120 to make the end of the contents appear. This is particularly
10121 important for y-or-n-p, but seems desirable generally.
10123 Value is non-zero if the window height has been changed. */
10126 resize_mini_window (struct window *w, int exact_p)
10128 struct frame *f = XFRAME (w->frame);
10129 int window_height_changed_p = 0;
10131 xassert (MINI_WINDOW_P (w));
10133 /* By default, start display at the beginning. */
10134 set_marker_both (w->start, w->buffer,
10135 BUF_BEGV (XBUFFER (w->buffer)),
10136 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10138 /* Don't resize windows while redisplaying a window; it would
10139 confuse redisplay functions when the size of the window they are
10140 displaying changes from under them. Such a resizing can happen,
10141 for instance, when which-func prints a long message while
10142 we are running fontification-functions. We're running these
10143 functions with safe_call which binds inhibit-redisplay to t. */
10144 if (!NILP (Vinhibit_redisplay))
10145 return 0;
10147 /* Nil means don't try to resize. */
10148 if (NILP (Vresize_mini_windows)
10149 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10150 return 0;
10152 if (!FRAME_MINIBUF_ONLY_P (f))
10154 struct it it;
10155 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10156 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10157 int height, max_height;
10158 int unit = FRAME_LINE_HEIGHT (f);
10159 struct text_pos start;
10160 struct buffer *old_current_buffer = NULL;
10162 if (current_buffer != XBUFFER (w->buffer))
10164 old_current_buffer = current_buffer;
10165 set_buffer_internal (XBUFFER (w->buffer));
10168 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10170 /* Compute the max. number of lines specified by the user. */
10171 if (FLOATP (Vmax_mini_window_height))
10172 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10173 else if (INTEGERP (Vmax_mini_window_height))
10174 max_height = XINT (Vmax_mini_window_height);
10175 else
10176 max_height = total_height / 4;
10178 /* Correct that max. height if it's bogus. */
10179 max_height = max (1, max_height);
10180 max_height = min (total_height, max_height);
10182 /* Find out the height of the text in the window. */
10183 if (it.line_wrap == TRUNCATE)
10184 height = 1;
10185 else
10187 last_height = 0;
10188 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10189 if (it.max_ascent == 0 && it.max_descent == 0)
10190 height = it.current_y + last_height;
10191 else
10192 height = it.current_y + it.max_ascent + it.max_descent;
10193 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10194 height = (height + unit - 1) / unit;
10197 /* Compute a suitable window start. */
10198 if (height > max_height)
10200 height = max_height;
10201 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10202 move_it_vertically_backward (&it, (height - 1) * unit);
10203 start = it.current.pos;
10205 else
10206 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10207 SET_MARKER_FROM_TEXT_POS (w->start, start);
10209 if (EQ (Vresize_mini_windows, Qgrow_only))
10211 /* Let it grow only, until we display an empty message, in which
10212 case the window shrinks again. */
10213 if (height > WINDOW_TOTAL_LINES (w))
10215 int old_height = WINDOW_TOTAL_LINES (w);
10216 freeze_window_starts (f, 1);
10217 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10218 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10220 else if (height < WINDOW_TOTAL_LINES (w)
10221 && (exact_p || BEGV == ZV))
10223 int old_height = WINDOW_TOTAL_LINES (w);
10224 freeze_window_starts (f, 0);
10225 shrink_mini_window (w);
10226 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10229 else
10231 /* Always resize to exact size needed. */
10232 if (height > WINDOW_TOTAL_LINES (w))
10234 int old_height = WINDOW_TOTAL_LINES (w);
10235 freeze_window_starts (f, 1);
10236 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10237 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10239 else if (height < WINDOW_TOTAL_LINES (w))
10241 int old_height = WINDOW_TOTAL_LINES (w);
10242 freeze_window_starts (f, 0);
10243 shrink_mini_window (w);
10245 if (height)
10247 freeze_window_starts (f, 1);
10248 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10251 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10255 if (old_current_buffer)
10256 set_buffer_internal (old_current_buffer);
10259 return window_height_changed_p;
10263 /* Value is the current message, a string, or nil if there is no
10264 current message. */
10266 Lisp_Object
10267 current_message (void)
10269 Lisp_Object msg;
10271 if (!BUFFERP (echo_area_buffer[0]))
10272 msg = Qnil;
10273 else
10275 with_echo_area_buffer (0, 0, current_message_1,
10276 (intptr_t) &msg, Qnil, 0, 0);
10277 if (NILP (msg))
10278 echo_area_buffer[0] = Qnil;
10281 return msg;
10285 static int
10286 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10288 intptr_t i1 = a1;
10289 Lisp_Object *msg = (Lisp_Object *) i1;
10291 if (Z > BEG)
10292 *msg = make_buffer_string (BEG, Z, 1);
10293 else
10294 *msg = Qnil;
10295 return 0;
10299 /* Push the current message on Vmessage_stack for later restoration
10300 by restore_message. Value is non-zero if the current message isn't
10301 empty. This is a relatively infrequent operation, so it's not
10302 worth optimizing. */
10305 push_message (void)
10307 Lisp_Object msg;
10308 msg = current_message ();
10309 Vmessage_stack = Fcons (msg, Vmessage_stack);
10310 return STRINGP (msg);
10314 /* Restore message display from the top of Vmessage_stack. */
10316 void
10317 restore_message (void)
10319 Lisp_Object msg;
10321 xassert (CONSP (Vmessage_stack));
10322 msg = XCAR (Vmessage_stack);
10323 if (STRINGP (msg))
10324 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10325 else
10326 message3_nolog (msg, 0, 0);
10330 /* Handler for record_unwind_protect calling pop_message. */
10332 Lisp_Object
10333 pop_message_unwind (Lisp_Object dummy)
10335 pop_message ();
10336 return Qnil;
10339 /* Pop the top-most entry off Vmessage_stack. */
10341 static void
10342 pop_message (void)
10344 xassert (CONSP (Vmessage_stack));
10345 Vmessage_stack = XCDR (Vmessage_stack);
10349 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10350 exits. If the stack is not empty, we have a missing pop_message
10351 somewhere. */
10353 void
10354 check_message_stack (void)
10356 if (!NILP (Vmessage_stack))
10357 abort ();
10361 /* Truncate to NCHARS what will be displayed in the echo area the next
10362 time we display it---but don't redisplay it now. */
10364 void
10365 truncate_echo_area (EMACS_INT nchars)
10367 if (nchars == 0)
10368 echo_area_buffer[0] = Qnil;
10369 /* A null message buffer means that the frame hasn't really been
10370 initialized yet. Error messages get reported properly by
10371 cmd_error, so this must be just an informative message; toss it. */
10372 else if (!noninteractive
10373 && INTERACTIVE
10374 && !NILP (echo_area_buffer[0]))
10376 struct frame *sf = SELECTED_FRAME ();
10377 if (FRAME_MESSAGE_BUF (sf))
10378 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10383 /* Helper function for truncate_echo_area. Truncate the current
10384 message to at most NCHARS characters. */
10386 static int
10387 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10389 if (BEG + nchars < Z)
10390 del_range (BEG + nchars, Z);
10391 if (Z == BEG)
10392 echo_area_buffer[0] = Qnil;
10393 return 0;
10397 /* Set the current message to a substring of S or STRING.
10399 If STRING is a Lisp string, set the message to the first NBYTES
10400 bytes from STRING. NBYTES zero means use the whole string. If
10401 STRING is multibyte, the message will be displayed multibyte.
10403 If S is not null, set the message to the first LEN bytes of S. LEN
10404 zero means use the whole string. MULTIBYTE_P non-zero means S is
10405 multibyte. Display the message multibyte in that case.
10407 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10408 to t before calling set_message_1 (which calls insert).
10411 static void
10412 set_message (const char *s, Lisp_Object string,
10413 EMACS_INT nbytes, int multibyte_p)
10415 message_enable_multibyte
10416 = ((s && multibyte_p)
10417 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10419 with_echo_area_buffer (0, -1, set_message_1,
10420 (intptr_t) s, string, nbytes, multibyte_p);
10421 message_buf_print = 0;
10422 help_echo_showing_p = 0;
10426 /* Helper function for set_message. Arguments have the same meaning
10427 as there, with A1 corresponding to S and A2 corresponding to STRING
10428 This function is called with the echo area buffer being
10429 current. */
10431 static int
10432 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
10434 intptr_t i1 = a1;
10435 const char *s = (const char *) i1;
10436 const unsigned char *msg = (const unsigned char *) s;
10437 Lisp_Object string = a2;
10439 /* Change multibyteness of the echo buffer appropriately. */
10440 if (message_enable_multibyte
10441 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10442 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10444 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10445 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10446 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10448 /* Insert new message at BEG. */
10449 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10451 if (STRINGP (string))
10453 EMACS_INT nchars;
10455 if (nbytes == 0)
10456 nbytes = SBYTES (string);
10457 nchars = string_byte_to_char (string, nbytes);
10459 /* This function takes care of single/multibyte conversion. We
10460 just have to ensure that the echo area buffer has the right
10461 setting of enable_multibyte_characters. */
10462 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10464 else if (s)
10466 if (nbytes == 0)
10467 nbytes = strlen (s);
10469 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10471 /* Convert from multi-byte to single-byte. */
10472 EMACS_INT i;
10473 int c, n;
10474 char work[1];
10476 /* Convert a multibyte string to single-byte. */
10477 for (i = 0; i < nbytes; i += n)
10479 c = string_char_and_length (msg + i, &n);
10480 work[0] = (ASCII_CHAR_P (c)
10482 : multibyte_char_to_unibyte (c));
10483 insert_1_both (work, 1, 1, 1, 0, 0);
10486 else if (!multibyte_p
10487 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10489 /* Convert from single-byte to multi-byte. */
10490 EMACS_INT i;
10491 int c, n;
10492 unsigned char str[MAX_MULTIBYTE_LENGTH];
10494 /* Convert a single-byte string to multibyte. */
10495 for (i = 0; i < nbytes; i++)
10497 c = msg[i];
10498 MAKE_CHAR_MULTIBYTE (c);
10499 n = CHAR_STRING (c, str);
10500 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10503 else
10504 insert_1 (s, nbytes, 1, 0, 0);
10507 return 0;
10511 /* Clear messages. CURRENT_P non-zero means clear the current
10512 message. LAST_DISPLAYED_P non-zero means clear the message
10513 last displayed. */
10515 void
10516 clear_message (int current_p, int last_displayed_p)
10518 if (current_p)
10520 echo_area_buffer[0] = Qnil;
10521 message_cleared_p = 1;
10524 if (last_displayed_p)
10525 echo_area_buffer[1] = Qnil;
10527 message_buf_print = 0;
10530 /* Clear garbaged frames.
10532 This function is used where the old redisplay called
10533 redraw_garbaged_frames which in turn called redraw_frame which in
10534 turn called clear_frame. The call to clear_frame was a source of
10535 flickering. I believe a clear_frame is not necessary. It should
10536 suffice in the new redisplay to invalidate all current matrices,
10537 and ensure a complete redisplay of all windows. */
10539 static void
10540 clear_garbaged_frames (void)
10542 if (frame_garbaged)
10544 Lisp_Object tail, frame;
10545 int changed_count = 0;
10547 FOR_EACH_FRAME (tail, frame)
10549 struct frame *f = XFRAME (frame);
10551 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10553 if (f->resized_p)
10555 Fredraw_frame (frame);
10556 f->force_flush_display_p = 1;
10558 clear_current_matrices (f);
10559 changed_count++;
10560 f->garbaged = 0;
10561 f->resized_p = 0;
10565 frame_garbaged = 0;
10566 if (changed_count)
10567 ++windows_or_buffers_changed;
10572 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10573 is non-zero update selected_frame. Value is non-zero if the
10574 mini-windows height has been changed. */
10576 static int
10577 echo_area_display (int update_frame_p)
10579 Lisp_Object mini_window;
10580 struct window *w;
10581 struct frame *f;
10582 int window_height_changed_p = 0;
10583 struct frame *sf = SELECTED_FRAME ();
10585 mini_window = FRAME_MINIBUF_WINDOW (sf);
10586 w = XWINDOW (mini_window);
10587 f = XFRAME (WINDOW_FRAME (w));
10589 /* Don't display if frame is invisible or not yet initialized. */
10590 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10591 return 0;
10593 #ifdef HAVE_WINDOW_SYSTEM
10594 /* When Emacs starts, selected_frame may be the initial terminal
10595 frame. If we let this through, a message would be displayed on
10596 the terminal. */
10597 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10598 return 0;
10599 #endif /* HAVE_WINDOW_SYSTEM */
10601 /* Redraw garbaged frames. */
10602 if (frame_garbaged)
10603 clear_garbaged_frames ();
10605 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10607 echo_area_window = mini_window;
10608 window_height_changed_p = display_echo_area (w);
10609 w->must_be_updated_p = 1;
10611 /* Update the display, unless called from redisplay_internal.
10612 Also don't update the screen during redisplay itself. The
10613 update will happen at the end of redisplay, and an update
10614 here could cause confusion. */
10615 if (update_frame_p && !redisplaying_p)
10617 int n = 0;
10619 /* If the display update has been interrupted by pending
10620 input, update mode lines in the frame. Due to the
10621 pending input, it might have been that redisplay hasn't
10622 been called, so that mode lines above the echo area are
10623 garbaged. This looks odd, so we prevent it here. */
10624 if (!display_completed)
10625 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10627 if (window_height_changed_p
10628 /* Don't do this if Emacs is shutting down. Redisplay
10629 needs to run hooks. */
10630 && !NILP (Vrun_hooks))
10632 /* Must update other windows. Likewise as in other
10633 cases, don't let this update be interrupted by
10634 pending input. */
10635 int count = SPECPDL_INDEX ();
10636 specbind (Qredisplay_dont_pause, Qt);
10637 windows_or_buffers_changed = 1;
10638 redisplay_internal ();
10639 unbind_to (count, Qnil);
10641 else if (FRAME_WINDOW_P (f) && n == 0)
10643 /* Window configuration is the same as before.
10644 Can do with a display update of the echo area,
10645 unless we displayed some mode lines. */
10646 update_single_window (w, 1);
10647 FRAME_RIF (f)->flush_display (f);
10649 else
10650 update_frame (f, 1, 1);
10652 /* If cursor is in the echo area, make sure that the next
10653 redisplay displays the minibuffer, so that the cursor will
10654 be replaced with what the minibuffer wants. */
10655 if (cursor_in_echo_area)
10656 ++windows_or_buffers_changed;
10659 else if (!EQ (mini_window, selected_window))
10660 windows_or_buffers_changed++;
10662 /* Last displayed message is now the current message. */
10663 echo_area_buffer[1] = echo_area_buffer[0];
10664 /* Inform read_char that we're not echoing. */
10665 echo_message_buffer = Qnil;
10667 /* Prevent redisplay optimization in redisplay_internal by resetting
10668 this_line_start_pos. This is done because the mini-buffer now
10669 displays the message instead of its buffer text. */
10670 if (EQ (mini_window, selected_window))
10671 CHARPOS (this_line_start_pos) = 0;
10673 return window_height_changed_p;
10678 /***********************************************************************
10679 Mode Lines and Frame Titles
10680 ***********************************************************************/
10682 /* A buffer for constructing non-propertized mode-line strings and
10683 frame titles in it; allocated from the heap in init_xdisp and
10684 resized as needed in store_mode_line_noprop_char. */
10686 static char *mode_line_noprop_buf;
10688 /* The buffer's end, and a current output position in it. */
10690 static char *mode_line_noprop_buf_end;
10691 static char *mode_line_noprop_ptr;
10693 #define MODE_LINE_NOPROP_LEN(start) \
10694 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10696 static enum {
10697 MODE_LINE_DISPLAY = 0,
10698 MODE_LINE_TITLE,
10699 MODE_LINE_NOPROP,
10700 MODE_LINE_STRING
10701 } mode_line_target;
10703 /* Alist that caches the results of :propertize.
10704 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10705 static Lisp_Object mode_line_proptrans_alist;
10707 /* List of strings making up the mode-line. */
10708 static Lisp_Object mode_line_string_list;
10710 /* Base face property when building propertized mode line string. */
10711 static Lisp_Object mode_line_string_face;
10712 static Lisp_Object mode_line_string_face_prop;
10715 /* Unwind data for mode line strings */
10717 static Lisp_Object Vmode_line_unwind_vector;
10719 static Lisp_Object
10720 format_mode_line_unwind_data (struct buffer *obuf,
10721 Lisp_Object owin,
10722 int save_proptrans)
10724 Lisp_Object vector, tmp;
10726 /* Reduce consing by keeping one vector in
10727 Vwith_echo_area_save_vector. */
10728 vector = Vmode_line_unwind_vector;
10729 Vmode_line_unwind_vector = Qnil;
10731 if (NILP (vector))
10732 vector = Fmake_vector (make_number (8), Qnil);
10734 ASET (vector, 0, make_number (mode_line_target));
10735 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10736 ASET (vector, 2, mode_line_string_list);
10737 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10738 ASET (vector, 4, mode_line_string_face);
10739 ASET (vector, 5, mode_line_string_face_prop);
10741 if (obuf)
10742 XSETBUFFER (tmp, obuf);
10743 else
10744 tmp = Qnil;
10745 ASET (vector, 6, tmp);
10746 ASET (vector, 7, owin);
10748 return vector;
10751 static Lisp_Object
10752 unwind_format_mode_line (Lisp_Object vector)
10754 mode_line_target = XINT (AREF (vector, 0));
10755 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10756 mode_line_string_list = AREF (vector, 2);
10757 if (! EQ (AREF (vector, 3), Qt))
10758 mode_line_proptrans_alist = AREF (vector, 3);
10759 mode_line_string_face = AREF (vector, 4);
10760 mode_line_string_face_prop = AREF (vector, 5);
10762 if (!NILP (AREF (vector, 7)))
10763 /* Select window before buffer, since it may change the buffer. */
10764 Fselect_window (AREF (vector, 7), Qt);
10766 if (!NILP (AREF (vector, 6)))
10768 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10769 ASET (vector, 6, Qnil);
10772 Vmode_line_unwind_vector = vector;
10773 return Qnil;
10777 /* Store a single character C for the frame title in mode_line_noprop_buf.
10778 Re-allocate mode_line_noprop_buf if necessary. */
10780 static void
10781 store_mode_line_noprop_char (char c)
10783 /* If output position has reached the end of the allocated buffer,
10784 increase the buffer's size. */
10785 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10787 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10788 ptrdiff_t size = len;
10789 mode_line_noprop_buf =
10790 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10791 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10792 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10795 *mode_line_noprop_ptr++ = c;
10799 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10800 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10801 characters that yield more columns than PRECISION; PRECISION <= 0
10802 means copy the whole string. Pad with spaces until FIELD_WIDTH
10803 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10804 pad. Called from display_mode_element when it is used to build a
10805 frame title. */
10807 static int
10808 store_mode_line_noprop (const char *string, int field_width, int precision)
10810 const unsigned char *str = (const unsigned char *) string;
10811 int n = 0;
10812 EMACS_INT dummy, nbytes;
10814 /* Copy at most PRECISION chars from STR. */
10815 nbytes = strlen (string);
10816 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10817 while (nbytes--)
10818 store_mode_line_noprop_char (*str++);
10820 /* Fill up with spaces until FIELD_WIDTH reached. */
10821 while (field_width > 0
10822 && n < field_width)
10824 store_mode_line_noprop_char (' ');
10825 ++n;
10828 return n;
10831 /***********************************************************************
10832 Frame Titles
10833 ***********************************************************************/
10835 #ifdef HAVE_WINDOW_SYSTEM
10837 /* Set the title of FRAME, if it has changed. The title format is
10838 Vicon_title_format if FRAME is iconified, otherwise it is
10839 frame_title_format. */
10841 static void
10842 x_consider_frame_title (Lisp_Object frame)
10844 struct frame *f = XFRAME (frame);
10846 if (FRAME_WINDOW_P (f)
10847 || FRAME_MINIBUF_ONLY_P (f)
10848 || f->explicit_name)
10850 /* Do we have more than one visible frame on this X display? */
10851 Lisp_Object tail;
10852 Lisp_Object fmt;
10853 ptrdiff_t title_start;
10854 char *title;
10855 ptrdiff_t len;
10856 struct it it;
10857 int count = SPECPDL_INDEX ();
10859 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
10861 Lisp_Object other_frame = XCAR (tail);
10862 struct frame *tf = XFRAME (other_frame);
10864 if (tf != f
10865 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10866 && !FRAME_MINIBUF_ONLY_P (tf)
10867 && !EQ (other_frame, tip_frame)
10868 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10869 break;
10872 /* Set global variable indicating that multiple frames exist. */
10873 multiple_frames = CONSP (tail);
10875 /* Switch to the buffer of selected window of the frame. Set up
10876 mode_line_target so that display_mode_element will output into
10877 mode_line_noprop_buf; then display the title. */
10878 record_unwind_protect (unwind_format_mode_line,
10879 format_mode_line_unwind_data
10880 (current_buffer, selected_window, 0));
10882 Fselect_window (f->selected_window, Qt);
10883 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
10884 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
10886 mode_line_target = MODE_LINE_TITLE;
10887 title_start = MODE_LINE_NOPROP_LEN (0);
10888 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
10889 NULL, DEFAULT_FACE_ID);
10890 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
10891 len = MODE_LINE_NOPROP_LEN (title_start);
10892 title = mode_line_noprop_buf + title_start;
10893 unbind_to (count, Qnil);
10895 /* Set the title only if it's changed. This avoids consing in
10896 the common case where it hasn't. (If it turns out that we've
10897 already wasted too much time by walking through the list with
10898 display_mode_element, then we might need to optimize at a
10899 higher level than this.) */
10900 if (! STRINGP (f->name)
10901 || SBYTES (f->name) != len
10902 || memcmp (title, SDATA (f->name), len) != 0)
10903 x_implicitly_set_name (f, make_string (title, len), Qnil);
10907 #endif /* not HAVE_WINDOW_SYSTEM */
10912 /***********************************************************************
10913 Menu Bars
10914 ***********************************************************************/
10917 /* Prepare for redisplay by updating menu-bar item lists when
10918 appropriate. This can call eval. */
10920 void
10921 prepare_menu_bars (void)
10923 int all_windows;
10924 struct gcpro gcpro1, gcpro2;
10925 struct frame *f;
10926 Lisp_Object tooltip_frame;
10928 #ifdef HAVE_WINDOW_SYSTEM
10929 tooltip_frame = tip_frame;
10930 #else
10931 tooltip_frame = Qnil;
10932 #endif
10934 /* Update all frame titles based on their buffer names, etc. We do
10935 this before the menu bars so that the buffer-menu will show the
10936 up-to-date frame titles. */
10937 #ifdef HAVE_WINDOW_SYSTEM
10938 if (windows_or_buffers_changed || update_mode_lines)
10940 Lisp_Object tail, frame;
10942 FOR_EACH_FRAME (tail, frame)
10944 f = XFRAME (frame);
10945 if (!EQ (frame, tooltip_frame)
10946 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
10947 x_consider_frame_title (frame);
10950 #endif /* HAVE_WINDOW_SYSTEM */
10952 /* Update the menu bar item lists, if appropriate. This has to be
10953 done before any actual redisplay or generation of display lines. */
10954 all_windows = (update_mode_lines
10955 || buffer_shared > 1
10956 || windows_or_buffers_changed);
10957 if (all_windows)
10959 Lisp_Object tail, frame;
10960 int count = SPECPDL_INDEX ();
10961 /* 1 means that update_menu_bar has run its hooks
10962 so any further calls to update_menu_bar shouldn't do so again. */
10963 int menu_bar_hooks_run = 0;
10965 record_unwind_save_match_data ();
10967 FOR_EACH_FRAME (tail, frame)
10969 f = XFRAME (frame);
10971 /* Ignore tooltip frame. */
10972 if (EQ (frame, tooltip_frame))
10973 continue;
10975 /* If a window on this frame changed size, report that to
10976 the user and clear the size-change flag. */
10977 if (FRAME_WINDOW_SIZES_CHANGED (f))
10979 Lisp_Object functions;
10981 /* Clear flag first in case we get an error below. */
10982 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
10983 functions = Vwindow_size_change_functions;
10984 GCPRO2 (tail, functions);
10986 while (CONSP (functions))
10988 if (!EQ (XCAR (functions), Qt))
10989 call1 (XCAR (functions), frame);
10990 functions = XCDR (functions);
10992 UNGCPRO;
10995 GCPRO1 (tail);
10996 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
10997 #ifdef HAVE_WINDOW_SYSTEM
10998 update_tool_bar (f, 0);
10999 #endif
11000 #ifdef HAVE_NS
11001 if (windows_or_buffers_changed
11002 && FRAME_NS_P (f))
11003 ns_set_doc_edited (f, Fbuffer_modified_p
11004 (XWINDOW (f->selected_window)->buffer));
11005 #endif
11006 UNGCPRO;
11009 unbind_to (count, Qnil);
11011 else
11013 struct frame *sf = SELECTED_FRAME ();
11014 update_menu_bar (sf, 1, 0);
11015 #ifdef HAVE_WINDOW_SYSTEM
11016 update_tool_bar (sf, 1);
11017 #endif
11022 /* Update the menu bar item list for frame F. This has to be done
11023 before we start to fill in any display lines, because it can call
11024 eval.
11026 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11028 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11029 already ran the menu bar hooks for this redisplay, so there
11030 is no need to run them again. The return value is the
11031 updated value of this flag, to pass to the next call. */
11033 static int
11034 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11036 Lisp_Object window;
11037 register struct window *w;
11039 /* If called recursively during a menu update, do nothing. This can
11040 happen when, for instance, an activate-menubar-hook causes a
11041 redisplay. */
11042 if (inhibit_menubar_update)
11043 return hooks_run;
11045 window = FRAME_SELECTED_WINDOW (f);
11046 w = XWINDOW (window);
11048 if (FRAME_WINDOW_P (f)
11050 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11051 || defined (HAVE_NS) || defined (USE_GTK)
11052 FRAME_EXTERNAL_MENU_BAR (f)
11053 #else
11054 FRAME_MENU_BAR_LINES (f) > 0
11055 #endif
11056 : FRAME_MENU_BAR_LINES (f) > 0)
11058 /* If the user has switched buffers or windows, we need to
11059 recompute to reflect the new bindings. But we'll
11060 recompute when update_mode_lines is set too; that means
11061 that people can use force-mode-line-update to request
11062 that the menu bar be recomputed. The adverse effect on
11063 the rest of the redisplay algorithm is about the same as
11064 windows_or_buffers_changed anyway. */
11065 if (windows_or_buffers_changed
11066 /* This used to test w->update_mode_line, but we believe
11067 there is no need to recompute the menu in that case. */
11068 || update_mode_lines
11069 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11070 < BUF_MODIFF (XBUFFER (w->buffer)))
11071 != !NILP (w->last_had_star))
11072 || ((!NILP (Vtransient_mark_mode)
11073 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11074 != !NILP (w->region_showing)))
11076 struct buffer *prev = current_buffer;
11077 int count = SPECPDL_INDEX ();
11079 specbind (Qinhibit_menubar_update, Qt);
11081 set_buffer_internal_1 (XBUFFER (w->buffer));
11082 if (save_match_data)
11083 record_unwind_save_match_data ();
11084 if (NILP (Voverriding_local_map_menu_flag))
11086 specbind (Qoverriding_terminal_local_map, Qnil);
11087 specbind (Qoverriding_local_map, Qnil);
11090 if (!hooks_run)
11092 /* Run the Lucid hook. */
11093 safe_run_hooks (Qactivate_menubar_hook);
11095 /* If it has changed current-menubar from previous value,
11096 really recompute the menu-bar from the value. */
11097 if (! NILP (Vlucid_menu_bar_dirty_flag))
11098 call0 (Qrecompute_lucid_menubar);
11100 safe_run_hooks (Qmenu_bar_update_hook);
11102 hooks_run = 1;
11105 XSETFRAME (Vmenu_updating_frame, f);
11106 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
11108 /* Redisplay the menu bar in case we changed it. */
11109 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11110 || defined (HAVE_NS) || defined (USE_GTK)
11111 if (FRAME_WINDOW_P (f))
11113 #if defined (HAVE_NS)
11114 /* All frames on Mac OS share the same menubar. So only
11115 the selected frame should be allowed to set it. */
11116 if (f == SELECTED_FRAME ())
11117 #endif
11118 set_frame_menubar (f, 0, 0);
11120 else
11121 /* On a terminal screen, the menu bar is an ordinary screen
11122 line, and this makes it get updated. */
11123 w->update_mode_line = Qt;
11124 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11125 /* In the non-toolkit version, the menu bar is an ordinary screen
11126 line, and this makes it get updated. */
11127 w->update_mode_line = Qt;
11128 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11130 unbind_to (count, Qnil);
11131 set_buffer_internal_1 (prev);
11135 return hooks_run;
11140 /***********************************************************************
11141 Output Cursor
11142 ***********************************************************************/
11144 #ifdef HAVE_WINDOW_SYSTEM
11146 /* EXPORT:
11147 Nominal cursor position -- where to draw output.
11148 HPOS and VPOS are window relative glyph matrix coordinates.
11149 X and Y are window relative pixel coordinates. */
11151 struct cursor_pos output_cursor;
11154 /* EXPORT:
11155 Set the global variable output_cursor to CURSOR. All cursor
11156 positions are relative to updated_window. */
11158 void
11159 set_output_cursor (struct cursor_pos *cursor)
11161 output_cursor.hpos = cursor->hpos;
11162 output_cursor.vpos = cursor->vpos;
11163 output_cursor.x = cursor->x;
11164 output_cursor.y = cursor->y;
11168 /* EXPORT for RIF:
11169 Set a nominal cursor position.
11171 HPOS and VPOS are column/row positions in a window glyph matrix. X
11172 and Y are window text area relative pixel positions.
11174 If this is done during an update, updated_window will contain the
11175 window that is being updated and the position is the future output
11176 cursor position for that window. If updated_window is null, use
11177 selected_window and display the cursor at the given position. */
11179 void
11180 x_cursor_to (int vpos, int hpos, int y, int x)
11182 struct window *w;
11184 /* If updated_window is not set, work on selected_window. */
11185 if (updated_window)
11186 w = updated_window;
11187 else
11188 w = XWINDOW (selected_window);
11190 /* Set the output cursor. */
11191 output_cursor.hpos = hpos;
11192 output_cursor.vpos = vpos;
11193 output_cursor.x = x;
11194 output_cursor.y = y;
11196 /* If not called as part of an update, really display the cursor.
11197 This will also set the cursor position of W. */
11198 if (updated_window == NULL)
11200 BLOCK_INPUT;
11201 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11202 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11203 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11204 UNBLOCK_INPUT;
11208 #endif /* HAVE_WINDOW_SYSTEM */
11211 /***********************************************************************
11212 Tool-bars
11213 ***********************************************************************/
11215 #ifdef HAVE_WINDOW_SYSTEM
11217 /* Where the mouse was last time we reported a mouse event. */
11219 FRAME_PTR last_mouse_frame;
11221 /* Tool-bar item index of the item on which a mouse button was pressed
11222 or -1. */
11224 int last_tool_bar_item;
11227 static Lisp_Object
11228 update_tool_bar_unwind (Lisp_Object frame)
11230 selected_frame = frame;
11231 return Qnil;
11234 /* Update the tool-bar item list for frame F. This has to be done
11235 before we start to fill in any display lines. Called from
11236 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11237 and restore it here. */
11239 static void
11240 update_tool_bar (struct frame *f, int save_match_data)
11242 #if defined (USE_GTK) || defined (HAVE_NS)
11243 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11244 #else
11245 int do_update = WINDOWP (f->tool_bar_window)
11246 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11247 #endif
11249 if (do_update)
11251 Lisp_Object window;
11252 struct window *w;
11254 window = FRAME_SELECTED_WINDOW (f);
11255 w = XWINDOW (window);
11257 /* If the user has switched buffers or windows, we need to
11258 recompute to reflect the new bindings. But we'll
11259 recompute when update_mode_lines is set too; that means
11260 that people can use force-mode-line-update to request
11261 that the menu bar be recomputed. The adverse effect on
11262 the rest of the redisplay algorithm is about the same as
11263 windows_or_buffers_changed anyway. */
11264 if (windows_or_buffers_changed
11265 || !NILP (w->update_mode_line)
11266 || update_mode_lines
11267 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11268 < BUF_MODIFF (XBUFFER (w->buffer)))
11269 != !NILP (w->last_had_star))
11270 || ((!NILP (Vtransient_mark_mode)
11271 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11272 != !NILP (w->region_showing)))
11274 struct buffer *prev = current_buffer;
11275 int count = SPECPDL_INDEX ();
11276 Lisp_Object frame, new_tool_bar;
11277 int new_n_tool_bar;
11278 struct gcpro gcpro1;
11280 /* Set current_buffer to the buffer of the selected
11281 window of the frame, so that we get the right local
11282 keymaps. */
11283 set_buffer_internal_1 (XBUFFER (w->buffer));
11285 /* Save match data, if we must. */
11286 if (save_match_data)
11287 record_unwind_save_match_data ();
11289 /* Make sure that we don't accidentally use bogus keymaps. */
11290 if (NILP (Voverriding_local_map_menu_flag))
11292 specbind (Qoverriding_terminal_local_map, Qnil);
11293 specbind (Qoverriding_local_map, Qnil);
11296 GCPRO1 (new_tool_bar);
11298 /* We must temporarily set the selected frame to this frame
11299 before calling tool_bar_items, because the calculation of
11300 the tool-bar keymap uses the selected frame (see
11301 `tool-bar-make-keymap' in tool-bar.el). */
11302 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11303 XSETFRAME (frame, f);
11304 selected_frame = frame;
11306 /* Build desired tool-bar items from keymaps. */
11307 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11308 &new_n_tool_bar);
11310 /* Redisplay the tool-bar if we changed it. */
11311 if (new_n_tool_bar != f->n_tool_bar_items
11312 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11314 /* Redisplay that happens asynchronously due to an expose event
11315 may access f->tool_bar_items. Make sure we update both
11316 variables within BLOCK_INPUT so no such event interrupts. */
11317 BLOCK_INPUT;
11318 f->tool_bar_items = new_tool_bar;
11319 f->n_tool_bar_items = new_n_tool_bar;
11320 w->update_mode_line = Qt;
11321 UNBLOCK_INPUT;
11324 UNGCPRO;
11326 unbind_to (count, Qnil);
11327 set_buffer_internal_1 (prev);
11333 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11334 F's desired tool-bar contents. F->tool_bar_items must have
11335 been set up previously by calling prepare_menu_bars. */
11337 static void
11338 build_desired_tool_bar_string (struct frame *f)
11340 int i, size, size_needed;
11341 struct gcpro gcpro1, gcpro2, gcpro3;
11342 Lisp_Object image, plist, props;
11344 image = plist = props = Qnil;
11345 GCPRO3 (image, plist, props);
11347 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11348 Otherwise, make a new string. */
11350 /* The size of the string we might be able to reuse. */
11351 size = (STRINGP (f->desired_tool_bar_string)
11352 ? SCHARS (f->desired_tool_bar_string)
11353 : 0);
11355 /* We need one space in the string for each image. */
11356 size_needed = f->n_tool_bar_items;
11358 /* Reuse f->desired_tool_bar_string, if possible. */
11359 if (size < size_needed || NILP (f->desired_tool_bar_string))
11360 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
11361 make_number (' '));
11362 else
11364 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11365 Fremove_text_properties (make_number (0), make_number (size),
11366 props, f->desired_tool_bar_string);
11369 /* Put a `display' property on the string for the images to display,
11370 put a `menu_item' property on tool-bar items with a value that
11371 is the index of the item in F's tool-bar item vector. */
11372 for (i = 0; i < f->n_tool_bar_items; ++i)
11374 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11376 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11377 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11378 int hmargin, vmargin, relief, idx, end;
11380 /* If image is a vector, choose the image according to the
11381 button state. */
11382 image = PROP (TOOL_BAR_ITEM_IMAGES);
11383 if (VECTORP (image))
11385 if (enabled_p)
11386 idx = (selected_p
11387 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11388 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11389 else
11390 idx = (selected_p
11391 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11392 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11394 xassert (ASIZE (image) >= idx);
11395 image = AREF (image, idx);
11397 else
11398 idx = -1;
11400 /* Ignore invalid image specifications. */
11401 if (!valid_image_p (image))
11402 continue;
11404 /* Display the tool-bar button pressed, or depressed. */
11405 plist = Fcopy_sequence (XCDR (image));
11407 /* Compute margin and relief to draw. */
11408 relief = (tool_bar_button_relief >= 0
11409 ? tool_bar_button_relief
11410 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11411 hmargin = vmargin = relief;
11413 if (INTEGERP (Vtool_bar_button_margin)
11414 && XINT (Vtool_bar_button_margin) > 0)
11416 hmargin += XFASTINT (Vtool_bar_button_margin);
11417 vmargin += XFASTINT (Vtool_bar_button_margin);
11419 else if (CONSP (Vtool_bar_button_margin))
11421 if (INTEGERP (XCAR (Vtool_bar_button_margin))
11422 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
11423 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11425 if (INTEGERP (XCDR (Vtool_bar_button_margin))
11426 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
11427 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11430 if (auto_raise_tool_bar_buttons_p)
11432 /* Add a `:relief' property to the image spec if the item is
11433 selected. */
11434 if (selected_p)
11436 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11437 hmargin -= relief;
11438 vmargin -= relief;
11441 else
11443 /* If image is selected, display it pressed, i.e. with a
11444 negative relief. If it's not selected, display it with a
11445 raised relief. */
11446 plist = Fplist_put (plist, QCrelief,
11447 (selected_p
11448 ? make_number (-relief)
11449 : make_number (relief)));
11450 hmargin -= relief;
11451 vmargin -= relief;
11454 /* Put a margin around the image. */
11455 if (hmargin || vmargin)
11457 if (hmargin == vmargin)
11458 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11459 else
11460 plist = Fplist_put (plist, QCmargin,
11461 Fcons (make_number (hmargin),
11462 make_number (vmargin)));
11465 /* If button is not enabled, and we don't have special images
11466 for the disabled state, make the image appear disabled by
11467 applying an appropriate algorithm to it. */
11468 if (!enabled_p && idx < 0)
11469 plist = Fplist_put (plist, QCconversion, Qdisabled);
11471 /* Put a `display' text property on the string for the image to
11472 display. Put a `menu-item' property on the string that gives
11473 the start of this item's properties in the tool-bar items
11474 vector. */
11475 image = Fcons (Qimage, plist);
11476 props = list4 (Qdisplay, image,
11477 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11479 /* Let the last image hide all remaining spaces in the tool bar
11480 string. The string can be longer than needed when we reuse a
11481 previous string. */
11482 if (i + 1 == f->n_tool_bar_items)
11483 end = SCHARS (f->desired_tool_bar_string);
11484 else
11485 end = i + 1;
11486 Fadd_text_properties (make_number (i), make_number (end),
11487 props, f->desired_tool_bar_string);
11488 #undef PROP
11491 UNGCPRO;
11495 /* Display one line of the tool-bar of frame IT->f.
11497 HEIGHT specifies the desired height of the tool-bar line.
11498 If the actual height of the glyph row is less than HEIGHT, the
11499 row's height is increased to HEIGHT, and the icons are centered
11500 vertically in the new height.
11502 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11503 count a final empty row in case the tool-bar width exactly matches
11504 the window width.
11507 static void
11508 display_tool_bar_line (struct it *it, int height)
11510 struct glyph_row *row = it->glyph_row;
11511 int max_x = it->last_visible_x;
11512 struct glyph *last;
11514 prepare_desired_row (row);
11515 row->y = it->current_y;
11517 /* Note that this isn't made use of if the face hasn't a box,
11518 so there's no need to check the face here. */
11519 it->start_of_box_run_p = 1;
11521 while (it->current_x < max_x)
11523 int x, n_glyphs_before, i, nglyphs;
11524 struct it it_before;
11526 /* Get the next display element. */
11527 if (!get_next_display_element (it))
11529 /* Don't count empty row if we are counting needed tool-bar lines. */
11530 if (height < 0 && !it->hpos)
11531 return;
11532 break;
11535 /* Produce glyphs. */
11536 n_glyphs_before = row->used[TEXT_AREA];
11537 it_before = *it;
11539 PRODUCE_GLYPHS (it);
11541 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11542 i = 0;
11543 x = it_before.current_x;
11544 while (i < nglyphs)
11546 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11548 if (x + glyph->pixel_width > max_x)
11550 /* Glyph doesn't fit on line. Backtrack. */
11551 row->used[TEXT_AREA] = n_glyphs_before;
11552 *it = it_before;
11553 /* If this is the only glyph on this line, it will never fit on the
11554 tool-bar, so skip it. But ensure there is at least one glyph,
11555 so we don't accidentally disable the tool-bar. */
11556 if (n_glyphs_before == 0
11557 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11558 break;
11559 goto out;
11562 ++it->hpos;
11563 x += glyph->pixel_width;
11564 ++i;
11567 /* Stop at line end. */
11568 if (ITERATOR_AT_END_OF_LINE_P (it))
11569 break;
11571 set_iterator_to_next (it, 1);
11574 out:;
11576 row->displays_text_p = row->used[TEXT_AREA] != 0;
11578 /* Use default face for the border below the tool bar.
11580 FIXME: When auto-resize-tool-bars is grow-only, there is
11581 no additional border below the possibly empty tool-bar lines.
11582 So to make the extra empty lines look "normal", we have to
11583 use the tool-bar face for the border too. */
11584 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11585 it->face_id = DEFAULT_FACE_ID;
11587 extend_face_to_end_of_line (it);
11588 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11589 last->right_box_line_p = 1;
11590 if (last == row->glyphs[TEXT_AREA])
11591 last->left_box_line_p = 1;
11593 /* Make line the desired height and center it vertically. */
11594 if ((height -= it->max_ascent + it->max_descent) > 0)
11596 /* Don't add more than one line height. */
11597 height %= FRAME_LINE_HEIGHT (it->f);
11598 it->max_ascent += height / 2;
11599 it->max_descent += (height + 1) / 2;
11602 compute_line_metrics (it);
11604 /* If line is empty, make it occupy the rest of the tool-bar. */
11605 if (!row->displays_text_p)
11607 row->height = row->phys_height = it->last_visible_y - row->y;
11608 row->visible_height = row->height;
11609 row->ascent = row->phys_ascent = 0;
11610 row->extra_line_spacing = 0;
11613 row->full_width_p = 1;
11614 row->continued_p = 0;
11615 row->truncated_on_left_p = 0;
11616 row->truncated_on_right_p = 0;
11618 it->current_x = it->hpos = 0;
11619 it->current_y += row->height;
11620 ++it->vpos;
11621 ++it->glyph_row;
11625 /* Max tool-bar height. */
11627 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11628 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11630 /* Value is the number of screen lines needed to make all tool-bar
11631 items of frame F visible. The number of actual rows needed is
11632 returned in *N_ROWS if non-NULL. */
11634 static int
11635 tool_bar_lines_needed (struct frame *f, int *n_rows)
11637 struct window *w = XWINDOW (f->tool_bar_window);
11638 struct it it;
11639 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11640 the desired matrix, so use (unused) mode-line row as temporary row to
11641 avoid destroying the first tool-bar row. */
11642 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11644 /* Initialize an iterator for iteration over
11645 F->desired_tool_bar_string in the tool-bar window of frame F. */
11646 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11647 it.first_visible_x = 0;
11648 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11649 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11650 it.paragraph_embedding = L2R;
11652 while (!ITERATOR_AT_END_P (&it))
11654 clear_glyph_row (temp_row);
11655 it.glyph_row = temp_row;
11656 display_tool_bar_line (&it, -1);
11658 clear_glyph_row (temp_row);
11660 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11661 if (n_rows)
11662 *n_rows = it.vpos > 0 ? it.vpos : -1;
11664 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11668 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11669 0, 1, 0,
11670 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11671 (Lisp_Object frame)
11673 struct frame *f;
11674 struct window *w;
11675 int nlines = 0;
11677 if (NILP (frame))
11678 frame = selected_frame;
11679 else
11680 CHECK_FRAME (frame);
11681 f = XFRAME (frame);
11683 if (WINDOWP (f->tool_bar_window)
11684 && (w = XWINDOW (f->tool_bar_window),
11685 WINDOW_TOTAL_LINES (w) > 0))
11687 update_tool_bar (f, 1);
11688 if (f->n_tool_bar_items)
11690 build_desired_tool_bar_string (f);
11691 nlines = tool_bar_lines_needed (f, NULL);
11695 return make_number (nlines);
11699 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11700 height should be changed. */
11702 static int
11703 redisplay_tool_bar (struct frame *f)
11705 struct window *w;
11706 struct it it;
11707 struct glyph_row *row;
11709 #if defined (USE_GTK) || defined (HAVE_NS)
11710 if (FRAME_EXTERNAL_TOOL_BAR (f))
11711 update_frame_tool_bar (f);
11712 return 0;
11713 #endif
11715 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11716 do anything. This means you must start with tool-bar-lines
11717 non-zero to get the auto-sizing effect. Or in other words, you
11718 can turn off tool-bars by specifying tool-bar-lines zero. */
11719 if (!WINDOWP (f->tool_bar_window)
11720 || (w = XWINDOW (f->tool_bar_window),
11721 WINDOW_TOTAL_LINES (w) == 0))
11722 return 0;
11724 /* Set up an iterator for the tool-bar window. */
11725 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11726 it.first_visible_x = 0;
11727 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11728 row = it.glyph_row;
11730 /* Build a string that represents the contents of the tool-bar. */
11731 build_desired_tool_bar_string (f);
11732 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11733 /* FIXME: This should be controlled by a user option. But it
11734 doesn't make sense to have an R2L tool bar if the menu bar cannot
11735 be drawn also R2L, and making the menu bar R2L is tricky due
11736 toolkit-specific code that implements it. If an R2L tool bar is
11737 ever supported, display_tool_bar_line should also be augmented to
11738 call unproduce_glyphs like display_line and display_string
11739 do. */
11740 it.paragraph_embedding = L2R;
11742 if (f->n_tool_bar_rows == 0)
11744 int nlines;
11746 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11747 nlines != WINDOW_TOTAL_LINES (w)))
11749 Lisp_Object frame;
11750 int old_height = WINDOW_TOTAL_LINES (w);
11752 XSETFRAME (frame, f);
11753 Fmodify_frame_parameters (frame,
11754 Fcons (Fcons (Qtool_bar_lines,
11755 make_number (nlines)),
11756 Qnil));
11757 if (WINDOW_TOTAL_LINES (w) != old_height)
11759 clear_glyph_matrix (w->desired_matrix);
11760 fonts_changed_p = 1;
11761 return 1;
11766 /* Display as many lines as needed to display all tool-bar items. */
11768 if (f->n_tool_bar_rows > 0)
11770 int border, rows, height, extra;
11772 if (INTEGERP (Vtool_bar_border))
11773 border = XINT (Vtool_bar_border);
11774 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11775 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11776 else if (EQ (Vtool_bar_border, Qborder_width))
11777 border = f->border_width;
11778 else
11779 border = 0;
11780 if (border < 0)
11781 border = 0;
11783 rows = f->n_tool_bar_rows;
11784 height = max (1, (it.last_visible_y - border) / rows);
11785 extra = it.last_visible_y - border - height * rows;
11787 while (it.current_y < it.last_visible_y)
11789 int h = 0;
11790 if (extra > 0 && rows-- > 0)
11792 h = (extra + rows - 1) / rows;
11793 extra -= h;
11795 display_tool_bar_line (&it, height + h);
11798 else
11800 while (it.current_y < it.last_visible_y)
11801 display_tool_bar_line (&it, 0);
11804 /* It doesn't make much sense to try scrolling in the tool-bar
11805 window, so don't do it. */
11806 w->desired_matrix->no_scrolling_p = 1;
11807 w->must_be_updated_p = 1;
11809 if (!NILP (Vauto_resize_tool_bars))
11811 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11812 int change_height_p = 0;
11814 /* If we couldn't display everything, change the tool-bar's
11815 height if there is room for more. */
11816 if (IT_STRING_CHARPOS (it) < it.end_charpos
11817 && it.current_y < max_tool_bar_height)
11818 change_height_p = 1;
11820 row = it.glyph_row - 1;
11822 /* If there are blank lines at the end, except for a partially
11823 visible blank line at the end that is smaller than
11824 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11825 if (!row->displays_text_p
11826 && row->height >= FRAME_LINE_HEIGHT (f))
11827 change_height_p = 1;
11829 /* If row displays tool-bar items, but is partially visible,
11830 change the tool-bar's height. */
11831 if (row->displays_text_p
11832 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11833 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11834 change_height_p = 1;
11836 /* Resize windows as needed by changing the `tool-bar-lines'
11837 frame parameter. */
11838 if (change_height_p)
11840 Lisp_Object frame;
11841 int old_height = WINDOW_TOTAL_LINES (w);
11842 int nrows;
11843 int nlines = tool_bar_lines_needed (f, &nrows);
11845 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11846 && !f->minimize_tool_bar_window_p)
11847 ? (nlines > old_height)
11848 : (nlines != old_height));
11849 f->minimize_tool_bar_window_p = 0;
11851 if (change_height_p)
11853 XSETFRAME (frame, f);
11854 Fmodify_frame_parameters (frame,
11855 Fcons (Fcons (Qtool_bar_lines,
11856 make_number (nlines)),
11857 Qnil));
11858 if (WINDOW_TOTAL_LINES (w) != old_height)
11860 clear_glyph_matrix (w->desired_matrix);
11861 f->n_tool_bar_rows = nrows;
11862 fonts_changed_p = 1;
11863 return 1;
11869 f->minimize_tool_bar_window_p = 0;
11870 return 0;
11874 /* Get information about the tool-bar item which is displayed in GLYPH
11875 on frame F. Return in *PROP_IDX the index where tool-bar item
11876 properties start in F->tool_bar_items. Value is zero if
11877 GLYPH doesn't display a tool-bar item. */
11879 static int
11880 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11882 Lisp_Object prop;
11883 int success_p;
11884 int charpos;
11886 /* This function can be called asynchronously, which means we must
11887 exclude any possibility that Fget_text_property signals an
11888 error. */
11889 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
11890 charpos = max (0, charpos);
11892 /* Get the text property `menu-item' at pos. The value of that
11893 property is the start index of this item's properties in
11894 F->tool_bar_items. */
11895 prop = Fget_text_property (make_number (charpos),
11896 Qmenu_item, f->current_tool_bar_string);
11897 if (INTEGERP (prop))
11899 *prop_idx = XINT (prop);
11900 success_p = 1;
11902 else
11903 success_p = 0;
11905 return success_p;
11909 /* Get information about the tool-bar item at position X/Y on frame F.
11910 Return in *GLYPH a pointer to the glyph of the tool-bar item in
11911 the current matrix of the tool-bar window of F, or NULL if not
11912 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
11913 item in F->tool_bar_items. Value is
11915 -1 if X/Y is not on a tool-bar item
11916 0 if X/Y is on the same item that was highlighted before.
11917 1 otherwise. */
11919 static int
11920 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
11921 int *hpos, int *vpos, int *prop_idx)
11923 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11924 struct window *w = XWINDOW (f->tool_bar_window);
11925 int area;
11927 /* Find the glyph under X/Y. */
11928 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
11929 if (*glyph == NULL)
11930 return -1;
11932 /* Get the start of this tool-bar item's properties in
11933 f->tool_bar_items. */
11934 if (!tool_bar_item_info (f, *glyph, prop_idx))
11935 return -1;
11937 /* Is mouse on the highlighted item? */
11938 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
11939 && *vpos >= hlinfo->mouse_face_beg_row
11940 && *vpos <= hlinfo->mouse_face_end_row
11941 && (*vpos > hlinfo->mouse_face_beg_row
11942 || *hpos >= hlinfo->mouse_face_beg_col)
11943 && (*vpos < hlinfo->mouse_face_end_row
11944 || *hpos < hlinfo->mouse_face_end_col
11945 || hlinfo->mouse_face_past_end))
11946 return 0;
11948 return 1;
11952 /* EXPORT:
11953 Handle mouse button event on the tool-bar of frame F, at
11954 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
11955 0 for button release. MODIFIERS is event modifiers for button
11956 release. */
11958 void
11959 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
11960 unsigned int modifiers)
11962 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11963 struct window *w = XWINDOW (f->tool_bar_window);
11964 int hpos, vpos, prop_idx;
11965 struct glyph *glyph;
11966 Lisp_Object enabled_p;
11968 /* If not on the highlighted tool-bar item, return. */
11969 frame_to_window_pixel_xy (w, &x, &y);
11970 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
11971 return;
11973 /* If item is disabled, do nothing. */
11974 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11975 if (NILP (enabled_p))
11976 return;
11978 if (down_p)
11980 /* Show item in pressed state. */
11981 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
11982 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
11983 last_tool_bar_item = prop_idx;
11985 else
11987 Lisp_Object key, frame;
11988 struct input_event event;
11989 EVENT_INIT (event);
11991 /* Show item in released state. */
11992 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
11993 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
11995 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
11997 XSETFRAME (frame, f);
11998 event.kind = TOOL_BAR_EVENT;
11999 event.frame_or_window = frame;
12000 event.arg = frame;
12001 kbd_buffer_store_event (&event);
12003 event.kind = TOOL_BAR_EVENT;
12004 event.frame_or_window = frame;
12005 event.arg = key;
12006 event.modifiers = modifiers;
12007 kbd_buffer_store_event (&event);
12008 last_tool_bar_item = -1;
12013 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12014 tool-bar window-relative coordinates X/Y. Called from
12015 note_mouse_highlight. */
12017 static void
12018 note_tool_bar_highlight (struct frame *f, int x, int y)
12020 Lisp_Object window = f->tool_bar_window;
12021 struct window *w = XWINDOW (window);
12022 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12023 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12024 int hpos, vpos;
12025 struct glyph *glyph;
12026 struct glyph_row *row;
12027 int i;
12028 Lisp_Object enabled_p;
12029 int prop_idx;
12030 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12031 int mouse_down_p, rc;
12033 /* Function note_mouse_highlight is called with negative X/Y
12034 values when mouse moves outside of the frame. */
12035 if (x <= 0 || y <= 0)
12037 clear_mouse_face (hlinfo);
12038 return;
12041 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12042 if (rc < 0)
12044 /* Not on tool-bar item. */
12045 clear_mouse_face (hlinfo);
12046 return;
12048 else if (rc == 0)
12049 /* On same tool-bar item as before. */
12050 goto set_help_echo;
12052 clear_mouse_face (hlinfo);
12054 /* Mouse is down, but on different tool-bar item? */
12055 mouse_down_p = (dpyinfo->grabbed
12056 && f == last_mouse_frame
12057 && FRAME_LIVE_P (f));
12058 if (mouse_down_p
12059 && last_tool_bar_item != prop_idx)
12060 return;
12062 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12063 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12065 /* If tool-bar item is not enabled, don't highlight it. */
12066 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12067 if (!NILP (enabled_p))
12069 /* Compute the x-position of the glyph. In front and past the
12070 image is a space. We include this in the highlighted area. */
12071 row = MATRIX_ROW (w->current_matrix, vpos);
12072 for (i = x = 0; i < hpos; ++i)
12073 x += row->glyphs[TEXT_AREA][i].pixel_width;
12075 /* Record this as the current active region. */
12076 hlinfo->mouse_face_beg_col = hpos;
12077 hlinfo->mouse_face_beg_row = vpos;
12078 hlinfo->mouse_face_beg_x = x;
12079 hlinfo->mouse_face_beg_y = row->y;
12080 hlinfo->mouse_face_past_end = 0;
12082 hlinfo->mouse_face_end_col = hpos + 1;
12083 hlinfo->mouse_face_end_row = vpos;
12084 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12085 hlinfo->mouse_face_end_y = row->y;
12086 hlinfo->mouse_face_window = window;
12087 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12089 /* Display it as active. */
12090 show_mouse_face (hlinfo, draw);
12091 hlinfo->mouse_face_image_state = draw;
12094 set_help_echo:
12096 /* Set help_echo_string to a help string to display for this tool-bar item.
12097 XTread_socket does the rest. */
12098 help_echo_object = help_echo_window = Qnil;
12099 help_echo_pos = -1;
12100 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12101 if (NILP (help_echo_string))
12102 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12105 #endif /* HAVE_WINDOW_SYSTEM */
12109 /************************************************************************
12110 Horizontal scrolling
12111 ************************************************************************/
12113 static int hscroll_window_tree (Lisp_Object);
12114 static int hscroll_windows (Lisp_Object);
12116 /* For all leaf windows in the window tree rooted at WINDOW, set their
12117 hscroll value so that PT is (i) visible in the window, and (ii) so
12118 that it is not within a certain margin at the window's left and
12119 right border. Value is non-zero if any window's hscroll has been
12120 changed. */
12122 static int
12123 hscroll_window_tree (Lisp_Object window)
12125 int hscrolled_p = 0;
12126 int hscroll_relative_p = FLOATP (Vhscroll_step);
12127 int hscroll_step_abs = 0;
12128 double hscroll_step_rel = 0;
12130 if (hscroll_relative_p)
12132 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12133 if (hscroll_step_rel < 0)
12135 hscroll_relative_p = 0;
12136 hscroll_step_abs = 0;
12139 else if (INTEGERP (Vhscroll_step))
12141 hscroll_step_abs = XINT (Vhscroll_step);
12142 if (hscroll_step_abs < 0)
12143 hscroll_step_abs = 0;
12145 else
12146 hscroll_step_abs = 0;
12148 while (WINDOWP (window))
12150 struct window *w = XWINDOW (window);
12152 if (WINDOWP (w->hchild))
12153 hscrolled_p |= hscroll_window_tree (w->hchild);
12154 else if (WINDOWP (w->vchild))
12155 hscrolled_p |= hscroll_window_tree (w->vchild);
12156 else if (w->cursor.vpos >= 0)
12158 int h_margin;
12159 int text_area_width;
12160 struct glyph_row *current_cursor_row
12161 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12162 struct glyph_row *desired_cursor_row
12163 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12164 struct glyph_row *cursor_row
12165 = (desired_cursor_row->enabled_p
12166 ? desired_cursor_row
12167 : current_cursor_row);
12168 int row_r2l_p = cursor_row->reversed_p;
12170 text_area_width = window_box_width (w, TEXT_AREA);
12172 /* Scroll when cursor is inside this scroll margin. */
12173 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12175 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12176 /* For left-to-right rows, hscroll when cursor is either
12177 (i) inside the right hscroll margin, or (ii) if it is
12178 inside the left margin and the window is already
12179 hscrolled. */
12180 && ((!row_r2l_p
12181 && ((XFASTINT (w->hscroll)
12182 && w->cursor.x <= h_margin)
12183 || (cursor_row->enabled_p
12184 && cursor_row->truncated_on_right_p
12185 && (w->cursor.x >= text_area_width - h_margin))))
12186 /* For right-to-left rows, the logic is similar,
12187 except that rules for scrolling to left and right
12188 are reversed. E.g., if cursor.x <= h_margin, we
12189 need to hscroll "to the right" unconditionally,
12190 and that will scroll the screen to the left so as
12191 to reveal the next portion of the row. */
12192 || (row_r2l_p
12193 && ((cursor_row->enabled_p
12194 /* FIXME: It is confusing to set the
12195 truncated_on_right_p flag when R2L rows
12196 are actually truncated on the left. */
12197 && cursor_row->truncated_on_right_p
12198 && w->cursor.x <= h_margin)
12199 || (XFASTINT (w->hscroll)
12200 && (w->cursor.x >= text_area_width - h_margin))))))
12202 struct it it;
12203 int hscroll;
12204 struct buffer *saved_current_buffer;
12205 EMACS_INT pt;
12206 int wanted_x;
12208 /* Find point in a display of infinite width. */
12209 saved_current_buffer = current_buffer;
12210 current_buffer = XBUFFER (w->buffer);
12212 if (w == XWINDOW (selected_window))
12213 pt = PT;
12214 else
12216 pt = marker_position (w->pointm);
12217 pt = max (BEGV, pt);
12218 pt = min (ZV, pt);
12221 /* Move iterator to pt starting at cursor_row->start in
12222 a line with infinite width. */
12223 init_to_row_start (&it, w, cursor_row);
12224 it.last_visible_x = INFINITY;
12225 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12226 current_buffer = saved_current_buffer;
12228 /* Position cursor in window. */
12229 if (!hscroll_relative_p && hscroll_step_abs == 0)
12230 hscroll = max (0, (it.current_x
12231 - (ITERATOR_AT_END_OF_LINE_P (&it)
12232 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12233 : (text_area_width / 2))))
12234 / FRAME_COLUMN_WIDTH (it.f);
12235 else if ((!row_r2l_p
12236 && w->cursor.x >= text_area_width - h_margin)
12237 || (row_r2l_p && w->cursor.x <= h_margin))
12239 if (hscroll_relative_p)
12240 wanted_x = text_area_width * (1 - hscroll_step_rel)
12241 - h_margin;
12242 else
12243 wanted_x = text_area_width
12244 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12245 - h_margin;
12246 hscroll
12247 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12249 else
12251 if (hscroll_relative_p)
12252 wanted_x = text_area_width * hscroll_step_rel
12253 + h_margin;
12254 else
12255 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12256 + h_margin;
12257 hscroll
12258 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12260 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
12262 /* Don't prevent redisplay optimizations if hscroll
12263 hasn't changed, as it will unnecessarily slow down
12264 redisplay. */
12265 if (XFASTINT (w->hscroll) != hscroll)
12267 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12268 w->hscroll = make_number (hscroll);
12269 hscrolled_p = 1;
12274 window = w->next;
12277 /* Value is non-zero if hscroll of any leaf window has been changed. */
12278 return hscrolled_p;
12282 /* Set hscroll so that cursor is visible and not inside horizontal
12283 scroll margins for all windows in the tree rooted at WINDOW. See
12284 also hscroll_window_tree above. Value is non-zero if any window's
12285 hscroll has been changed. If it has, desired matrices on the frame
12286 of WINDOW are cleared. */
12288 static int
12289 hscroll_windows (Lisp_Object window)
12291 int hscrolled_p = hscroll_window_tree (window);
12292 if (hscrolled_p)
12293 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12294 return hscrolled_p;
12299 /************************************************************************
12300 Redisplay
12301 ************************************************************************/
12303 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12304 to a non-zero value. This is sometimes handy to have in a debugger
12305 session. */
12307 #if GLYPH_DEBUG
12309 /* First and last unchanged row for try_window_id. */
12311 static int debug_first_unchanged_at_end_vpos;
12312 static int debug_last_unchanged_at_beg_vpos;
12314 /* Delta vpos and y. */
12316 static int debug_dvpos, debug_dy;
12318 /* Delta in characters and bytes for try_window_id. */
12320 static EMACS_INT debug_delta, debug_delta_bytes;
12322 /* Values of window_end_pos and window_end_vpos at the end of
12323 try_window_id. */
12325 static EMACS_INT debug_end_vpos;
12327 /* Append a string to W->desired_matrix->method. FMT is a printf
12328 format string. If trace_redisplay_p is non-zero also printf the
12329 resulting string to stderr. */
12331 static void debug_method_add (struct window *, char const *, ...)
12332 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12334 static void
12335 debug_method_add (struct window *w, char const *fmt, ...)
12337 char buffer[512];
12338 char *method = w->desired_matrix->method;
12339 int len = strlen (method);
12340 int size = sizeof w->desired_matrix->method;
12341 int remaining = size - len - 1;
12342 va_list ap;
12344 va_start (ap, fmt);
12345 vsprintf (buffer, fmt, ap);
12346 va_end (ap);
12347 if (len && remaining)
12349 method[len] = '|';
12350 --remaining, ++len;
12353 strncpy (method + len, buffer, remaining);
12355 if (trace_redisplay_p)
12356 fprintf (stderr, "%p (%s): %s\n",
12358 ((BUFFERP (w->buffer)
12359 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12360 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12361 : "no buffer"),
12362 buffer);
12365 #endif /* GLYPH_DEBUG */
12368 /* Value is non-zero if all changes in window W, which displays
12369 current_buffer, are in the text between START and END. START is a
12370 buffer position, END is given as a distance from Z. Used in
12371 redisplay_internal for display optimization. */
12373 static inline int
12374 text_outside_line_unchanged_p (struct window *w,
12375 EMACS_INT start, EMACS_INT end)
12377 int unchanged_p = 1;
12379 /* If text or overlays have changed, see where. */
12380 if (XFASTINT (w->last_modified) < MODIFF
12381 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12383 /* Gap in the line? */
12384 if (GPT < start || Z - GPT < end)
12385 unchanged_p = 0;
12387 /* Changes start in front of the line, or end after it? */
12388 if (unchanged_p
12389 && (BEG_UNCHANGED < start - 1
12390 || END_UNCHANGED < end))
12391 unchanged_p = 0;
12393 /* If selective display, can't optimize if changes start at the
12394 beginning of the line. */
12395 if (unchanged_p
12396 && INTEGERP (BVAR (current_buffer, selective_display))
12397 && XINT (BVAR (current_buffer, selective_display)) > 0
12398 && (BEG_UNCHANGED < start || GPT <= start))
12399 unchanged_p = 0;
12401 /* If there are overlays at the start or end of the line, these
12402 may have overlay strings with newlines in them. A change at
12403 START, for instance, may actually concern the display of such
12404 overlay strings as well, and they are displayed on different
12405 lines. So, quickly rule out this case. (For the future, it
12406 might be desirable to implement something more telling than
12407 just BEG/END_UNCHANGED.) */
12408 if (unchanged_p)
12410 if (BEG + BEG_UNCHANGED == start
12411 && overlay_touches_p (start))
12412 unchanged_p = 0;
12413 if (END_UNCHANGED == end
12414 && overlay_touches_p (Z - end))
12415 unchanged_p = 0;
12418 /* Under bidi reordering, adding or deleting a character in the
12419 beginning of a paragraph, before the first strong directional
12420 character, can change the base direction of the paragraph (unless
12421 the buffer specifies a fixed paragraph direction), which will
12422 require to redisplay the whole paragraph. It might be worthwhile
12423 to find the paragraph limits and widen the range of redisplayed
12424 lines to that, but for now just give up this optimization. */
12425 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12426 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12427 unchanged_p = 0;
12430 return unchanged_p;
12434 /* Do a frame update, taking possible shortcuts into account. This is
12435 the main external entry point for redisplay.
12437 If the last redisplay displayed an echo area message and that message
12438 is no longer requested, we clear the echo area or bring back the
12439 mini-buffer if that is in use. */
12441 void
12442 redisplay (void)
12444 redisplay_internal ();
12448 static Lisp_Object
12449 overlay_arrow_string_or_property (Lisp_Object var)
12451 Lisp_Object val;
12453 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12454 return val;
12456 return Voverlay_arrow_string;
12459 /* Return 1 if there are any overlay-arrows in current_buffer. */
12460 static int
12461 overlay_arrow_in_current_buffer_p (void)
12463 Lisp_Object vlist;
12465 for (vlist = Voverlay_arrow_variable_list;
12466 CONSP (vlist);
12467 vlist = XCDR (vlist))
12469 Lisp_Object var = XCAR (vlist);
12470 Lisp_Object val;
12472 if (!SYMBOLP (var))
12473 continue;
12474 val = find_symbol_value (var);
12475 if (MARKERP (val)
12476 && current_buffer == XMARKER (val)->buffer)
12477 return 1;
12479 return 0;
12483 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12484 has changed. */
12486 static int
12487 overlay_arrows_changed_p (void)
12489 Lisp_Object vlist;
12491 for (vlist = Voverlay_arrow_variable_list;
12492 CONSP (vlist);
12493 vlist = XCDR (vlist))
12495 Lisp_Object var = XCAR (vlist);
12496 Lisp_Object val, pstr;
12498 if (!SYMBOLP (var))
12499 continue;
12500 val = find_symbol_value (var);
12501 if (!MARKERP (val))
12502 continue;
12503 if (! EQ (COERCE_MARKER (val),
12504 Fget (var, Qlast_arrow_position))
12505 || ! (pstr = overlay_arrow_string_or_property (var),
12506 EQ (pstr, Fget (var, Qlast_arrow_string))))
12507 return 1;
12509 return 0;
12512 /* Mark overlay arrows to be updated on next redisplay. */
12514 static void
12515 update_overlay_arrows (int up_to_date)
12517 Lisp_Object vlist;
12519 for (vlist = Voverlay_arrow_variable_list;
12520 CONSP (vlist);
12521 vlist = XCDR (vlist))
12523 Lisp_Object var = XCAR (vlist);
12525 if (!SYMBOLP (var))
12526 continue;
12528 if (up_to_date > 0)
12530 Lisp_Object val = find_symbol_value (var);
12531 Fput (var, Qlast_arrow_position,
12532 COERCE_MARKER (val));
12533 Fput (var, Qlast_arrow_string,
12534 overlay_arrow_string_or_property (var));
12536 else if (up_to_date < 0
12537 || !NILP (Fget (var, Qlast_arrow_position)))
12539 Fput (var, Qlast_arrow_position, Qt);
12540 Fput (var, Qlast_arrow_string, Qt);
12546 /* Return overlay arrow string to display at row.
12547 Return integer (bitmap number) for arrow bitmap in left fringe.
12548 Return nil if no overlay arrow. */
12550 static Lisp_Object
12551 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12553 Lisp_Object vlist;
12555 for (vlist = Voverlay_arrow_variable_list;
12556 CONSP (vlist);
12557 vlist = XCDR (vlist))
12559 Lisp_Object var = XCAR (vlist);
12560 Lisp_Object val;
12562 if (!SYMBOLP (var))
12563 continue;
12565 val = find_symbol_value (var);
12567 if (MARKERP (val)
12568 && current_buffer == XMARKER (val)->buffer
12569 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12571 if (FRAME_WINDOW_P (it->f)
12572 /* FIXME: if ROW->reversed_p is set, this should test
12573 the right fringe, not the left one. */
12574 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12576 #ifdef HAVE_WINDOW_SYSTEM
12577 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12579 int fringe_bitmap;
12580 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12581 return make_number (fringe_bitmap);
12583 #endif
12584 return make_number (-1); /* Use default arrow bitmap */
12586 return overlay_arrow_string_or_property (var);
12590 return Qnil;
12593 /* Return 1 if point moved out of or into a composition. Otherwise
12594 return 0. PREV_BUF and PREV_PT are the last point buffer and
12595 position. BUF and PT are the current point buffer and position. */
12597 static int
12598 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
12599 struct buffer *buf, EMACS_INT pt)
12601 EMACS_INT start, end;
12602 Lisp_Object prop;
12603 Lisp_Object buffer;
12605 XSETBUFFER (buffer, buf);
12606 /* Check a composition at the last point if point moved within the
12607 same buffer. */
12608 if (prev_buf == buf)
12610 if (prev_pt == pt)
12611 /* Point didn't move. */
12612 return 0;
12614 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12615 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12616 && COMPOSITION_VALID_P (start, end, prop)
12617 && start < prev_pt && end > prev_pt)
12618 /* The last point was within the composition. Return 1 iff
12619 point moved out of the composition. */
12620 return (pt <= start || pt >= end);
12623 /* Check a composition at the current point. */
12624 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12625 && find_composition (pt, -1, &start, &end, &prop, buffer)
12626 && COMPOSITION_VALID_P (start, end, prop)
12627 && start < pt && end > pt);
12631 /* Reconsider the setting of B->clip_changed which is displayed
12632 in window W. */
12634 static inline void
12635 reconsider_clip_changes (struct window *w, struct buffer *b)
12637 if (b->clip_changed
12638 && !NILP (w->window_end_valid)
12639 && w->current_matrix->buffer == b
12640 && w->current_matrix->zv == BUF_ZV (b)
12641 && w->current_matrix->begv == BUF_BEGV (b))
12642 b->clip_changed = 0;
12644 /* If display wasn't paused, and W is not a tool bar window, see if
12645 point has been moved into or out of a composition. In that case,
12646 we set b->clip_changed to 1 to force updating the screen. If
12647 b->clip_changed has already been set to 1, we can skip this
12648 check. */
12649 if (!b->clip_changed
12650 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12652 EMACS_INT pt;
12654 if (w == XWINDOW (selected_window))
12655 pt = PT;
12656 else
12657 pt = marker_position (w->pointm);
12659 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12660 || pt != XINT (w->last_point))
12661 && check_point_in_composition (w->current_matrix->buffer,
12662 XINT (w->last_point),
12663 XBUFFER (w->buffer), pt))
12664 b->clip_changed = 1;
12669 /* Select FRAME to forward the values of frame-local variables into C
12670 variables so that the redisplay routines can access those values
12671 directly. */
12673 static void
12674 select_frame_for_redisplay (Lisp_Object frame)
12676 Lisp_Object tail, tem;
12677 Lisp_Object old = selected_frame;
12678 struct Lisp_Symbol *sym;
12680 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12682 selected_frame = frame;
12684 do {
12685 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12686 if (CONSP (XCAR (tail))
12687 && (tem = XCAR (XCAR (tail)),
12688 SYMBOLP (tem))
12689 && (sym = indirect_variable (XSYMBOL (tem)),
12690 sym->redirect == SYMBOL_LOCALIZED)
12691 && sym->val.blv->frame_local)
12692 /* Use find_symbol_value rather than Fsymbol_value
12693 to avoid an error if it is void. */
12694 find_symbol_value (tem);
12695 } while (!EQ (frame, old) && (frame = old, 1));
12699 #define STOP_POLLING \
12700 do { if (! polling_stopped_here) stop_polling (); \
12701 polling_stopped_here = 1; } while (0)
12703 #define RESUME_POLLING \
12704 do { if (polling_stopped_here) start_polling (); \
12705 polling_stopped_here = 0; } while (0)
12708 /* Perhaps in the future avoid recentering windows if it
12709 is not necessary; currently that causes some problems. */
12711 static void
12712 redisplay_internal (void)
12714 struct window *w = XWINDOW (selected_window);
12715 struct window *sw;
12716 struct frame *fr;
12717 int pending;
12718 int must_finish = 0;
12719 struct text_pos tlbufpos, tlendpos;
12720 int number_of_visible_frames;
12721 int count, count1;
12722 struct frame *sf;
12723 int polling_stopped_here = 0;
12724 Lisp_Object old_frame = selected_frame;
12726 /* Non-zero means redisplay has to consider all windows on all
12727 frames. Zero means, only selected_window is considered. */
12728 int consider_all_windows_p;
12730 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12732 /* No redisplay if running in batch mode or frame is not yet fully
12733 initialized, or redisplay is explicitly turned off by setting
12734 Vinhibit_redisplay. */
12735 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12736 || !NILP (Vinhibit_redisplay))
12737 return;
12739 /* Don't examine these until after testing Vinhibit_redisplay.
12740 When Emacs is shutting down, perhaps because its connection to
12741 X has dropped, we should not look at them at all. */
12742 fr = XFRAME (w->frame);
12743 sf = SELECTED_FRAME ();
12745 if (!fr->glyphs_initialized_p)
12746 return;
12748 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12749 if (popup_activated ())
12750 return;
12751 #endif
12753 /* I don't think this happens but let's be paranoid. */
12754 if (redisplaying_p)
12755 return;
12757 /* Record a function that resets redisplaying_p to its old value
12758 when we leave this function. */
12759 count = SPECPDL_INDEX ();
12760 record_unwind_protect (unwind_redisplay,
12761 Fcons (make_number (redisplaying_p), selected_frame));
12762 ++redisplaying_p;
12763 specbind (Qinhibit_free_realized_faces, Qnil);
12766 Lisp_Object tail, frame;
12768 FOR_EACH_FRAME (tail, frame)
12770 struct frame *f = XFRAME (frame);
12771 f->already_hscrolled_p = 0;
12775 retry:
12776 /* Remember the currently selected window. */
12777 sw = w;
12779 if (!EQ (old_frame, selected_frame)
12780 && FRAME_LIVE_P (XFRAME (old_frame)))
12781 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12782 selected_frame and selected_window to be temporarily out-of-sync so
12783 when we come back here via `goto retry', we need to resync because we
12784 may need to run Elisp code (via prepare_menu_bars). */
12785 select_frame_for_redisplay (old_frame);
12787 pending = 0;
12788 reconsider_clip_changes (w, current_buffer);
12789 last_escape_glyph_frame = NULL;
12790 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12791 last_glyphless_glyph_frame = NULL;
12792 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12794 /* If new fonts have been loaded that make a glyph matrix adjustment
12795 necessary, do it. */
12796 if (fonts_changed_p)
12798 adjust_glyphs (NULL);
12799 ++windows_or_buffers_changed;
12800 fonts_changed_p = 0;
12803 /* If face_change_count is non-zero, init_iterator will free all
12804 realized faces, which includes the faces referenced from current
12805 matrices. So, we can't reuse current matrices in this case. */
12806 if (face_change_count)
12807 ++windows_or_buffers_changed;
12809 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12810 && FRAME_TTY (sf)->previous_frame != sf)
12812 /* Since frames on a single ASCII terminal share the same
12813 display area, displaying a different frame means redisplay
12814 the whole thing. */
12815 windows_or_buffers_changed++;
12816 SET_FRAME_GARBAGED (sf);
12817 #ifndef DOS_NT
12818 set_tty_color_mode (FRAME_TTY (sf), sf);
12819 #endif
12820 FRAME_TTY (sf)->previous_frame = sf;
12823 /* Set the visible flags for all frames. Do this before checking
12824 for resized or garbaged frames; they want to know if their frames
12825 are visible. See the comment in frame.h for
12826 FRAME_SAMPLE_VISIBILITY. */
12828 Lisp_Object tail, frame;
12830 number_of_visible_frames = 0;
12832 FOR_EACH_FRAME (tail, frame)
12834 struct frame *f = XFRAME (frame);
12836 FRAME_SAMPLE_VISIBILITY (f);
12837 if (FRAME_VISIBLE_P (f))
12838 ++number_of_visible_frames;
12839 clear_desired_matrices (f);
12843 /* Notice any pending interrupt request to change frame size. */
12844 do_pending_window_change (1);
12846 /* do_pending_window_change could change the selected_window due to
12847 frame resizing which makes the selected window too small. */
12848 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12850 sw = w;
12851 reconsider_clip_changes (w, current_buffer);
12854 /* Clear frames marked as garbaged. */
12855 if (frame_garbaged)
12856 clear_garbaged_frames ();
12858 /* Build menubar and tool-bar items. */
12859 if (NILP (Vmemory_full))
12860 prepare_menu_bars ();
12862 if (windows_or_buffers_changed)
12863 update_mode_lines++;
12865 /* Detect case that we need to write or remove a star in the mode line. */
12866 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
12868 w->update_mode_line = Qt;
12869 if (buffer_shared > 1)
12870 update_mode_lines++;
12873 /* Avoid invocation of point motion hooks by `current_column' below. */
12874 count1 = SPECPDL_INDEX ();
12875 specbind (Qinhibit_point_motion_hooks, Qt);
12877 /* If %c is in the mode line, update it if needed. */
12878 if (!NILP (w->column_number_displayed)
12879 /* This alternative quickly identifies a common case
12880 where no change is needed. */
12881 && !(PT == XFASTINT (w->last_point)
12882 && XFASTINT (w->last_modified) >= MODIFF
12883 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12884 && (XFASTINT (w->column_number_displayed) != current_column ()))
12885 w->update_mode_line = Qt;
12887 unbind_to (count1, Qnil);
12889 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12891 /* The variable buffer_shared is set in redisplay_window and
12892 indicates that we redisplay a buffer in different windows. See
12893 there. */
12894 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
12895 || cursor_type_changed);
12897 /* If specs for an arrow have changed, do thorough redisplay
12898 to ensure we remove any arrow that should no longer exist. */
12899 if (overlay_arrows_changed_p ())
12900 consider_all_windows_p = windows_or_buffers_changed = 1;
12902 /* Normally the message* functions will have already displayed and
12903 updated the echo area, but the frame may have been trashed, or
12904 the update may have been preempted, so display the echo area
12905 again here. Checking message_cleared_p captures the case that
12906 the echo area should be cleared. */
12907 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12908 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12909 || (message_cleared_p
12910 && minibuf_level == 0
12911 /* If the mini-window is currently selected, this means the
12912 echo-area doesn't show through. */
12913 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12915 int window_height_changed_p = echo_area_display (0);
12916 must_finish = 1;
12918 /* If we don't display the current message, don't clear the
12919 message_cleared_p flag, because, if we did, we wouldn't clear
12920 the echo area in the next redisplay which doesn't preserve
12921 the echo area. */
12922 if (!display_last_displayed_message_p)
12923 message_cleared_p = 0;
12925 if (fonts_changed_p)
12926 goto retry;
12927 else if (window_height_changed_p)
12929 consider_all_windows_p = 1;
12930 ++update_mode_lines;
12931 ++windows_or_buffers_changed;
12933 /* If window configuration was changed, frames may have been
12934 marked garbaged. Clear them or we will experience
12935 surprises wrt scrolling. */
12936 if (frame_garbaged)
12937 clear_garbaged_frames ();
12940 else if (EQ (selected_window, minibuf_window)
12941 && (current_buffer->clip_changed
12942 || XFASTINT (w->last_modified) < MODIFF
12943 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12944 && resize_mini_window (w, 0))
12946 /* Resized active mini-window to fit the size of what it is
12947 showing if its contents might have changed. */
12948 must_finish = 1;
12949 /* FIXME: this causes all frames to be updated, which seems unnecessary
12950 since only the current frame needs to be considered. This function needs
12951 to be rewritten with two variables, consider_all_windows and
12952 consider_all_frames. */
12953 consider_all_windows_p = 1;
12954 ++windows_or_buffers_changed;
12955 ++update_mode_lines;
12957 /* If window configuration was changed, frames may have been
12958 marked garbaged. Clear them or we will experience
12959 surprises wrt scrolling. */
12960 if (frame_garbaged)
12961 clear_garbaged_frames ();
12965 /* If showing the region, and mark has changed, we must redisplay
12966 the whole window. The assignment to this_line_start_pos prevents
12967 the optimization directly below this if-statement. */
12968 if (((!NILP (Vtransient_mark_mode)
12969 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
12970 != !NILP (w->region_showing))
12971 || (!NILP (w->region_showing)
12972 && !EQ (w->region_showing,
12973 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
12974 CHARPOS (this_line_start_pos) = 0;
12976 /* Optimize the case that only the line containing the cursor in the
12977 selected window has changed. Variables starting with this_ are
12978 set in display_line and record information about the line
12979 containing the cursor. */
12980 tlbufpos = this_line_start_pos;
12981 tlendpos = this_line_end_pos;
12982 if (!consider_all_windows_p
12983 && CHARPOS (tlbufpos) > 0
12984 && NILP (w->update_mode_line)
12985 && !current_buffer->clip_changed
12986 && !current_buffer->prevent_redisplay_optimizations_p
12987 && FRAME_VISIBLE_P (XFRAME (w->frame))
12988 && !FRAME_OBSCURED_P (XFRAME (w->frame))
12989 /* Make sure recorded data applies to current buffer, etc. */
12990 && this_line_buffer == current_buffer
12991 && current_buffer == XBUFFER (w->buffer)
12992 && NILP (w->force_start)
12993 && NILP (w->optional_new_start)
12994 /* Point must be on the line that we have info recorded about. */
12995 && PT >= CHARPOS (tlbufpos)
12996 && PT <= Z - CHARPOS (tlendpos)
12997 /* All text outside that line, including its final newline,
12998 must be unchanged. */
12999 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13000 CHARPOS (tlendpos)))
13002 if (CHARPOS (tlbufpos) > BEGV
13003 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13004 && (CHARPOS (tlbufpos) == ZV
13005 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13006 /* Former continuation line has disappeared by becoming empty. */
13007 goto cancel;
13008 else if (XFASTINT (w->last_modified) < MODIFF
13009 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
13010 || MINI_WINDOW_P (w))
13012 /* We have to handle the case of continuation around a
13013 wide-column character (see the comment in indent.c around
13014 line 1340).
13016 For instance, in the following case:
13018 -------- Insert --------
13019 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13020 J_I_ ==> J_I_ `^^' are cursors.
13021 ^^ ^^
13022 -------- --------
13024 As we have to redraw the line above, we cannot use this
13025 optimization. */
13027 struct it it;
13028 int line_height_before = this_line_pixel_height;
13030 /* Note that start_display will handle the case that the
13031 line starting at tlbufpos is a continuation line. */
13032 start_display (&it, w, tlbufpos);
13034 /* Implementation note: It this still necessary? */
13035 if (it.current_x != this_line_start_x)
13036 goto cancel;
13038 TRACE ((stderr, "trying display optimization 1\n"));
13039 w->cursor.vpos = -1;
13040 overlay_arrow_seen = 0;
13041 it.vpos = this_line_vpos;
13042 it.current_y = this_line_y;
13043 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13044 display_line (&it);
13046 /* If line contains point, is not continued,
13047 and ends at same distance from eob as before, we win. */
13048 if (w->cursor.vpos >= 0
13049 /* Line is not continued, otherwise this_line_start_pos
13050 would have been set to 0 in display_line. */
13051 && CHARPOS (this_line_start_pos)
13052 /* Line ends as before. */
13053 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13054 /* Line has same height as before. Otherwise other lines
13055 would have to be shifted up or down. */
13056 && this_line_pixel_height == line_height_before)
13058 /* If this is not the window's last line, we must adjust
13059 the charstarts of the lines below. */
13060 if (it.current_y < it.last_visible_y)
13062 struct glyph_row *row
13063 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13064 EMACS_INT delta, delta_bytes;
13066 /* We used to distinguish between two cases here,
13067 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13068 when the line ends in a newline or the end of the
13069 buffer's accessible portion. But both cases did
13070 the same, so they were collapsed. */
13071 delta = (Z
13072 - CHARPOS (tlendpos)
13073 - MATRIX_ROW_START_CHARPOS (row));
13074 delta_bytes = (Z_BYTE
13075 - BYTEPOS (tlendpos)
13076 - MATRIX_ROW_START_BYTEPOS (row));
13078 increment_matrix_positions (w->current_matrix,
13079 this_line_vpos + 1,
13080 w->current_matrix->nrows,
13081 delta, delta_bytes);
13084 /* If this row displays text now but previously didn't,
13085 or vice versa, w->window_end_vpos may have to be
13086 adjusted. */
13087 if ((it.glyph_row - 1)->displays_text_p)
13089 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13090 XSETINT (w->window_end_vpos, this_line_vpos);
13092 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13093 && this_line_vpos > 0)
13094 XSETINT (w->window_end_vpos, this_line_vpos - 1);
13095 w->window_end_valid = Qnil;
13097 /* Update hint: No need to try to scroll in update_window. */
13098 w->desired_matrix->no_scrolling_p = 1;
13100 #if GLYPH_DEBUG
13101 *w->desired_matrix->method = 0;
13102 debug_method_add (w, "optimization 1");
13103 #endif
13104 #ifdef HAVE_WINDOW_SYSTEM
13105 update_window_fringes (w, 0);
13106 #endif
13107 goto update;
13109 else
13110 goto cancel;
13112 else if (/* Cursor position hasn't changed. */
13113 PT == XFASTINT (w->last_point)
13114 /* Make sure the cursor was last displayed
13115 in this window. Otherwise we have to reposition it. */
13116 && 0 <= w->cursor.vpos
13117 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13119 if (!must_finish)
13121 do_pending_window_change (1);
13122 /* If selected_window changed, redisplay again. */
13123 if (WINDOWP (selected_window)
13124 && (w = XWINDOW (selected_window)) != sw)
13125 goto retry;
13127 /* We used to always goto end_of_redisplay here, but this
13128 isn't enough if we have a blinking cursor. */
13129 if (w->cursor_off_p == w->last_cursor_off_p)
13130 goto end_of_redisplay;
13132 goto update;
13134 /* If highlighting the region, or if the cursor is in the echo area,
13135 then we can't just move the cursor. */
13136 else if (! (!NILP (Vtransient_mark_mode)
13137 && !NILP (BVAR (current_buffer, mark_active)))
13138 && (EQ (selected_window, BVAR (current_buffer, last_selected_window))
13139 || highlight_nonselected_windows)
13140 && NILP (w->region_showing)
13141 && NILP (Vshow_trailing_whitespace)
13142 && !cursor_in_echo_area)
13144 struct it it;
13145 struct glyph_row *row;
13147 /* Skip from tlbufpos to PT and see where it is. Note that
13148 PT may be in invisible text. If so, we will end at the
13149 next visible position. */
13150 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13151 NULL, DEFAULT_FACE_ID);
13152 it.current_x = this_line_start_x;
13153 it.current_y = this_line_y;
13154 it.vpos = this_line_vpos;
13156 /* The call to move_it_to stops in front of PT, but
13157 moves over before-strings. */
13158 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13160 if (it.vpos == this_line_vpos
13161 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13162 row->enabled_p))
13164 xassert (this_line_vpos == it.vpos);
13165 xassert (this_line_y == it.current_y);
13166 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13167 #if GLYPH_DEBUG
13168 *w->desired_matrix->method = 0;
13169 debug_method_add (w, "optimization 3");
13170 #endif
13171 goto update;
13173 else
13174 goto cancel;
13177 cancel:
13178 /* Text changed drastically or point moved off of line. */
13179 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13182 CHARPOS (this_line_start_pos) = 0;
13183 consider_all_windows_p |= buffer_shared > 1;
13184 ++clear_face_cache_count;
13185 #ifdef HAVE_WINDOW_SYSTEM
13186 ++clear_image_cache_count;
13187 #endif
13189 /* Build desired matrices, and update the display. If
13190 consider_all_windows_p is non-zero, do it for all windows on all
13191 frames. Otherwise do it for selected_window, only. */
13193 if (consider_all_windows_p)
13195 Lisp_Object tail, frame;
13197 FOR_EACH_FRAME (tail, frame)
13198 XFRAME (frame)->updated_p = 0;
13200 /* Recompute # windows showing selected buffer. This will be
13201 incremented each time such a window is displayed. */
13202 buffer_shared = 0;
13204 FOR_EACH_FRAME (tail, frame)
13206 struct frame *f = XFRAME (frame);
13208 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13210 if (! EQ (frame, selected_frame))
13211 /* Select the frame, for the sake of frame-local
13212 variables. */
13213 select_frame_for_redisplay (frame);
13215 /* Mark all the scroll bars to be removed; we'll redeem
13216 the ones we want when we redisplay their windows. */
13217 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13218 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13220 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13221 redisplay_windows (FRAME_ROOT_WINDOW (f));
13223 /* The X error handler may have deleted that frame. */
13224 if (!FRAME_LIVE_P (f))
13225 continue;
13227 /* Any scroll bars which redisplay_windows should have
13228 nuked should now go away. */
13229 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13230 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13232 /* If fonts changed, display again. */
13233 /* ??? rms: I suspect it is a mistake to jump all the way
13234 back to retry here. It should just retry this frame. */
13235 if (fonts_changed_p)
13236 goto retry;
13238 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13240 /* See if we have to hscroll. */
13241 if (!f->already_hscrolled_p)
13243 f->already_hscrolled_p = 1;
13244 if (hscroll_windows (f->root_window))
13245 goto retry;
13248 /* Prevent various kinds of signals during display
13249 update. stdio is not robust about handling
13250 signals, which can cause an apparent I/O
13251 error. */
13252 if (interrupt_input)
13253 unrequest_sigio ();
13254 STOP_POLLING;
13256 /* Update the display. */
13257 set_window_update_flags (XWINDOW (f->root_window), 1);
13258 pending |= update_frame (f, 0, 0);
13259 f->updated_p = 1;
13264 if (!EQ (old_frame, selected_frame)
13265 && FRAME_LIVE_P (XFRAME (old_frame)))
13266 /* We played a bit fast-and-loose above and allowed selected_frame
13267 and selected_window to be temporarily out-of-sync but let's make
13268 sure this stays contained. */
13269 select_frame_for_redisplay (old_frame);
13270 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13272 if (!pending)
13274 /* Do the mark_window_display_accurate after all windows have
13275 been redisplayed because this call resets flags in buffers
13276 which are needed for proper redisplay. */
13277 FOR_EACH_FRAME (tail, frame)
13279 struct frame *f = XFRAME (frame);
13280 if (f->updated_p)
13282 mark_window_display_accurate (f->root_window, 1);
13283 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13284 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13289 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13291 Lisp_Object mini_window;
13292 struct frame *mini_frame;
13294 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13295 /* Use list_of_error, not Qerror, so that
13296 we catch only errors and don't run the debugger. */
13297 internal_condition_case_1 (redisplay_window_1, selected_window,
13298 list_of_error,
13299 redisplay_window_error);
13301 /* Compare desired and current matrices, perform output. */
13303 update:
13304 /* If fonts changed, display again. */
13305 if (fonts_changed_p)
13306 goto retry;
13308 /* Prevent various kinds of signals during display update.
13309 stdio is not robust about handling signals,
13310 which can cause an apparent I/O error. */
13311 if (interrupt_input)
13312 unrequest_sigio ();
13313 STOP_POLLING;
13315 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13317 if (hscroll_windows (selected_window))
13318 goto retry;
13320 XWINDOW (selected_window)->must_be_updated_p = 1;
13321 pending = update_frame (sf, 0, 0);
13324 /* We may have called echo_area_display at the top of this
13325 function. If the echo area is on another frame, that may
13326 have put text on a frame other than the selected one, so the
13327 above call to update_frame would not have caught it. Catch
13328 it here. */
13329 mini_window = FRAME_MINIBUF_WINDOW (sf);
13330 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13332 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13334 XWINDOW (mini_window)->must_be_updated_p = 1;
13335 pending |= update_frame (mini_frame, 0, 0);
13336 if (!pending && hscroll_windows (mini_window))
13337 goto retry;
13341 /* If display was paused because of pending input, make sure we do a
13342 thorough update the next time. */
13343 if (pending)
13345 /* Prevent the optimization at the beginning of
13346 redisplay_internal that tries a single-line update of the
13347 line containing the cursor in the selected window. */
13348 CHARPOS (this_line_start_pos) = 0;
13350 /* Let the overlay arrow be updated the next time. */
13351 update_overlay_arrows (0);
13353 /* If we pause after scrolling, some rows in the current
13354 matrices of some windows are not valid. */
13355 if (!WINDOW_FULL_WIDTH_P (w)
13356 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13357 update_mode_lines = 1;
13359 else
13361 if (!consider_all_windows_p)
13363 /* This has already been done above if
13364 consider_all_windows_p is set. */
13365 mark_window_display_accurate_1 (w, 1);
13367 /* Say overlay arrows are up to date. */
13368 update_overlay_arrows (1);
13370 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13371 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13374 update_mode_lines = 0;
13375 windows_or_buffers_changed = 0;
13376 cursor_type_changed = 0;
13379 /* Start SIGIO interrupts coming again. Having them off during the
13380 code above makes it less likely one will discard output, but not
13381 impossible, since there might be stuff in the system buffer here.
13382 But it is much hairier to try to do anything about that. */
13383 if (interrupt_input)
13384 request_sigio ();
13385 RESUME_POLLING;
13387 /* If a frame has become visible which was not before, redisplay
13388 again, so that we display it. Expose events for such a frame
13389 (which it gets when becoming visible) don't call the parts of
13390 redisplay constructing glyphs, so simply exposing a frame won't
13391 display anything in this case. So, we have to display these
13392 frames here explicitly. */
13393 if (!pending)
13395 Lisp_Object tail, frame;
13396 int new_count = 0;
13398 FOR_EACH_FRAME (tail, frame)
13400 int this_is_visible = 0;
13402 if (XFRAME (frame)->visible)
13403 this_is_visible = 1;
13404 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13405 if (XFRAME (frame)->visible)
13406 this_is_visible = 1;
13408 if (this_is_visible)
13409 new_count++;
13412 if (new_count != number_of_visible_frames)
13413 windows_or_buffers_changed++;
13416 /* Change frame size now if a change is pending. */
13417 do_pending_window_change (1);
13419 /* If we just did a pending size change, or have additional
13420 visible frames, or selected_window changed, redisplay again. */
13421 if ((windows_or_buffers_changed && !pending)
13422 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13423 goto retry;
13425 /* Clear the face and image caches.
13427 We used to do this only if consider_all_windows_p. But the cache
13428 needs to be cleared if a timer creates images in the current
13429 buffer (e.g. the test case in Bug#6230). */
13431 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13433 clear_face_cache (0);
13434 clear_face_cache_count = 0;
13437 #ifdef HAVE_WINDOW_SYSTEM
13438 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13440 clear_image_caches (Qnil);
13441 clear_image_cache_count = 0;
13443 #endif /* HAVE_WINDOW_SYSTEM */
13445 end_of_redisplay:
13446 unbind_to (count, Qnil);
13447 RESUME_POLLING;
13451 /* Redisplay, but leave alone any recent echo area message unless
13452 another message has been requested in its place.
13454 This is useful in situations where you need to redisplay but no
13455 user action has occurred, making it inappropriate for the message
13456 area to be cleared. See tracking_off and
13457 wait_reading_process_output for examples of these situations.
13459 FROM_WHERE is an integer saying from where this function was
13460 called. This is useful for debugging. */
13462 void
13463 redisplay_preserve_echo_area (int from_where)
13465 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13467 if (!NILP (echo_area_buffer[1]))
13469 /* We have a previously displayed message, but no current
13470 message. Redisplay the previous message. */
13471 display_last_displayed_message_p = 1;
13472 redisplay_internal ();
13473 display_last_displayed_message_p = 0;
13475 else
13476 redisplay_internal ();
13478 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13479 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13480 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13484 /* Function registered with record_unwind_protect in
13485 redisplay_internal. Reset redisplaying_p to the value it had
13486 before redisplay_internal was called, and clear
13487 prevent_freeing_realized_faces_p. It also selects the previously
13488 selected frame, unless it has been deleted (by an X connection
13489 failure during redisplay, for example). */
13491 static Lisp_Object
13492 unwind_redisplay (Lisp_Object val)
13494 Lisp_Object old_redisplaying_p, old_frame;
13496 old_redisplaying_p = XCAR (val);
13497 redisplaying_p = XFASTINT (old_redisplaying_p);
13498 old_frame = XCDR (val);
13499 if (! EQ (old_frame, selected_frame)
13500 && FRAME_LIVE_P (XFRAME (old_frame)))
13501 select_frame_for_redisplay (old_frame);
13502 return Qnil;
13506 /* Mark the display of window W as accurate or inaccurate. If
13507 ACCURATE_P is non-zero mark display of W as accurate. If
13508 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13509 redisplay_internal is called. */
13511 static void
13512 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13514 if (BUFFERP (w->buffer))
13516 struct buffer *b = XBUFFER (w->buffer);
13518 w->last_modified
13519 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
13520 w->last_overlay_modified
13521 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
13522 w->last_had_star
13523 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
13525 if (accurate_p)
13527 b->clip_changed = 0;
13528 b->prevent_redisplay_optimizations_p = 0;
13530 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13531 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13532 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13533 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13535 w->current_matrix->buffer = b;
13536 w->current_matrix->begv = BUF_BEGV (b);
13537 w->current_matrix->zv = BUF_ZV (b);
13539 w->last_cursor = w->cursor;
13540 w->last_cursor_off_p = w->cursor_off_p;
13542 if (w == XWINDOW (selected_window))
13543 w->last_point = make_number (BUF_PT (b));
13544 else
13545 w->last_point = make_number (XMARKER (w->pointm)->charpos);
13549 if (accurate_p)
13551 w->window_end_valid = w->buffer;
13552 w->update_mode_line = Qnil;
13557 /* Mark the display of windows in the window tree rooted at WINDOW as
13558 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13559 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13560 be redisplayed the next time redisplay_internal is called. */
13562 void
13563 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13565 struct window *w;
13567 for (; !NILP (window); window = w->next)
13569 w = XWINDOW (window);
13570 mark_window_display_accurate_1 (w, accurate_p);
13572 if (!NILP (w->vchild))
13573 mark_window_display_accurate (w->vchild, accurate_p);
13574 if (!NILP (w->hchild))
13575 mark_window_display_accurate (w->hchild, accurate_p);
13578 if (accurate_p)
13580 update_overlay_arrows (1);
13582 else
13584 /* Force a thorough redisplay the next time by setting
13585 last_arrow_position and last_arrow_string to t, which is
13586 unequal to any useful value of Voverlay_arrow_... */
13587 update_overlay_arrows (-1);
13592 /* Return value in display table DP (Lisp_Char_Table *) for character
13593 C. Since a display table doesn't have any parent, we don't have to
13594 follow parent. Do not call this function directly but use the
13595 macro DISP_CHAR_VECTOR. */
13597 Lisp_Object
13598 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13600 Lisp_Object val;
13602 if (ASCII_CHAR_P (c))
13604 val = dp->ascii;
13605 if (SUB_CHAR_TABLE_P (val))
13606 val = XSUB_CHAR_TABLE (val)->contents[c];
13608 else
13610 Lisp_Object table;
13612 XSETCHAR_TABLE (table, dp);
13613 val = char_table_ref (table, c);
13615 if (NILP (val))
13616 val = dp->defalt;
13617 return val;
13622 /***********************************************************************
13623 Window Redisplay
13624 ***********************************************************************/
13626 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13628 static void
13629 redisplay_windows (Lisp_Object window)
13631 while (!NILP (window))
13633 struct window *w = XWINDOW (window);
13635 if (!NILP (w->hchild))
13636 redisplay_windows (w->hchild);
13637 else if (!NILP (w->vchild))
13638 redisplay_windows (w->vchild);
13639 else if (!NILP (w->buffer))
13641 displayed_buffer = XBUFFER (w->buffer);
13642 /* Use list_of_error, not Qerror, so that
13643 we catch only errors and don't run the debugger. */
13644 internal_condition_case_1 (redisplay_window_0, window,
13645 list_of_error,
13646 redisplay_window_error);
13649 window = w->next;
13653 static Lisp_Object
13654 redisplay_window_error (Lisp_Object ignore)
13656 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13657 return Qnil;
13660 static Lisp_Object
13661 redisplay_window_0 (Lisp_Object window)
13663 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13664 redisplay_window (window, 0);
13665 return Qnil;
13668 static Lisp_Object
13669 redisplay_window_1 (Lisp_Object window)
13671 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13672 redisplay_window (window, 1);
13673 return Qnil;
13677 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13678 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13679 which positions recorded in ROW differ from current buffer
13680 positions.
13682 Return 0 if cursor is not on this row, 1 otherwise. */
13684 static int
13685 set_cursor_from_row (struct window *w, struct glyph_row *row,
13686 struct glyph_matrix *matrix,
13687 EMACS_INT delta, EMACS_INT delta_bytes,
13688 int dy, int dvpos)
13690 struct glyph *glyph = row->glyphs[TEXT_AREA];
13691 struct glyph *end = glyph + row->used[TEXT_AREA];
13692 struct glyph *cursor = NULL;
13693 /* The last known character position in row. */
13694 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13695 int x = row->x;
13696 EMACS_INT pt_old = PT - delta;
13697 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13698 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13699 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13700 /* A glyph beyond the edge of TEXT_AREA which we should never
13701 touch. */
13702 struct glyph *glyphs_end = end;
13703 /* Non-zero means we've found a match for cursor position, but that
13704 glyph has the avoid_cursor_p flag set. */
13705 int match_with_avoid_cursor = 0;
13706 /* Non-zero means we've seen at least one glyph that came from a
13707 display string. */
13708 int string_seen = 0;
13709 /* Largest and smallest buffer positions seen so far during scan of
13710 glyph row. */
13711 EMACS_INT bpos_max = pos_before;
13712 EMACS_INT bpos_min = pos_after;
13713 /* Last buffer position covered by an overlay string with an integer
13714 `cursor' property. */
13715 EMACS_INT bpos_covered = 0;
13716 /* Non-zero means the display string on which to display the cursor
13717 comes from a text property, not from an overlay. */
13718 int string_from_text_prop = 0;
13720 /* Don't even try doing anything if called for a mode-line or
13721 header-line row, since the rest of the code isn't prepared to
13722 deal with such calamities. */
13723 xassert (!row->mode_line_p);
13724 if (row->mode_line_p)
13725 return 0;
13727 /* Skip over glyphs not having an object at the start and the end of
13728 the row. These are special glyphs like truncation marks on
13729 terminal frames. */
13730 if (row->displays_text_p)
13732 if (!row->reversed_p)
13734 while (glyph < end
13735 && INTEGERP (glyph->object)
13736 && glyph->charpos < 0)
13738 x += glyph->pixel_width;
13739 ++glyph;
13741 while (end > glyph
13742 && INTEGERP ((end - 1)->object)
13743 /* CHARPOS is zero for blanks and stretch glyphs
13744 inserted by extend_face_to_end_of_line. */
13745 && (end - 1)->charpos <= 0)
13746 --end;
13747 glyph_before = glyph - 1;
13748 glyph_after = end;
13750 else
13752 struct glyph *g;
13754 /* If the glyph row is reversed, we need to process it from back
13755 to front, so swap the edge pointers. */
13756 glyphs_end = end = glyph - 1;
13757 glyph += row->used[TEXT_AREA] - 1;
13759 while (glyph > end + 1
13760 && INTEGERP (glyph->object)
13761 && glyph->charpos < 0)
13763 --glyph;
13764 x -= glyph->pixel_width;
13766 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13767 --glyph;
13768 /* By default, in reversed rows we put the cursor on the
13769 rightmost (first in the reading order) glyph. */
13770 for (g = end + 1; g < glyph; g++)
13771 x += g->pixel_width;
13772 while (end < glyph
13773 && INTEGERP ((end + 1)->object)
13774 && (end + 1)->charpos <= 0)
13775 ++end;
13776 glyph_before = glyph + 1;
13777 glyph_after = end;
13780 else if (row->reversed_p)
13782 /* In R2L rows that don't display text, put the cursor on the
13783 rightmost glyph. Case in point: an empty last line that is
13784 part of an R2L paragraph. */
13785 cursor = end - 1;
13786 /* Avoid placing the cursor on the last glyph of the row, where
13787 on terminal frames we hold the vertical border between
13788 adjacent windows. */
13789 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13790 && !WINDOW_RIGHTMOST_P (w)
13791 && cursor == row->glyphs[LAST_AREA] - 1)
13792 cursor--;
13793 x = -1; /* will be computed below, at label compute_x */
13796 /* Step 1: Try to find the glyph whose character position
13797 corresponds to point. If that's not possible, find 2 glyphs
13798 whose character positions are the closest to point, one before
13799 point, the other after it. */
13800 if (!row->reversed_p)
13801 while (/* not marched to end of glyph row */
13802 glyph < end
13803 /* glyph was not inserted by redisplay for internal purposes */
13804 && !INTEGERP (glyph->object))
13806 if (BUFFERP (glyph->object))
13808 EMACS_INT dpos = glyph->charpos - pt_old;
13810 if (glyph->charpos > bpos_max)
13811 bpos_max = glyph->charpos;
13812 if (glyph->charpos < bpos_min)
13813 bpos_min = glyph->charpos;
13814 if (!glyph->avoid_cursor_p)
13816 /* If we hit point, we've found the glyph on which to
13817 display the cursor. */
13818 if (dpos == 0)
13820 match_with_avoid_cursor = 0;
13821 break;
13823 /* See if we've found a better approximation to
13824 POS_BEFORE or to POS_AFTER. Note that we want the
13825 first (leftmost) glyph of all those that are the
13826 closest from below, and the last (rightmost) of all
13827 those from above. */
13828 if (0 > dpos && dpos > pos_before - pt_old)
13830 pos_before = glyph->charpos;
13831 glyph_before = glyph;
13833 else if (0 < dpos && dpos <= pos_after - pt_old)
13835 pos_after = glyph->charpos;
13836 glyph_after = glyph;
13839 else if (dpos == 0)
13840 match_with_avoid_cursor = 1;
13842 else if (STRINGP (glyph->object))
13844 Lisp_Object chprop;
13845 EMACS_INT glyph_pos = glyph->charpos;
13847 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13848 glyph->object);
13849 if (!NILP (chprop))
13851 /* If the string came from a `display' text property,
13852 look up the buffer position of that property and
13853 use that position to update bpos_max, as if we
13854 actually saw such a position in one of the row's
13855 glyphs. This helps with supporting integer values
13856 of `cursor' property on the display string in
13857 situations where most or all of the row's buffer
13858 text is completely covered by display properties,
13859 so that no glyph with valid buffer positions is
13860 ever seen in the row. */
13861 EMACS_INT prop_pos =
13862 string_buffer_position_lim (glyph->object, pos_before,
13863 pos_after, 0);
13865 if (prop_pos >= pos_before)
13866 bpos_max = prop_pos - 1;
13868 if (INTEGERP (chprop))
13870 bpos_covered = bpos_max + XINT (chprop);
13871 /* If the `cursor' property covers buffer positions up
13872 to and including point, we should display cursor on
13873 this glyph. Note that, if a `cursor' property on one
13874 of the string's characters has an integer value, we
13875 will break out of the loop below _before_ we get to
13876 the position match above. IOW, integer values of
13877 the `cursor' property override the "exact match for
13878 point" strategy of positioning the cursor. */
13879 /* Implementation note: bpos_max == pt_old when, e.g.,
13880 we are in an empty line, where bpos_max is set to
13881 MATRIX_ROW_START_CHARPOS, see above. */
13882 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13884 cursor = glyph;
13885 break;
13889 string_seen = 1;
13891 x += glyph->pixel_width;
13892 ++glyph;
13894 else if (glyph > end) /* row is reversed */
13895 while (!INTEGERP (glyph->object))
13897 if (BUFFERP (glyph->object))
13899 EMACS_INT dpos = glyph->charpos - pt_old;
13901 if (glyph->charpos > bpos_max)
13902 bpos_max = glyph->charpos;
13903 if (glyph->charpos < bpos_min)
13904 bpos_min = glyph->charpos;
13905 if (!glyph->avoid_cursor_p)
13907 if (dpos == 0)
13909 match_with_avoid_cursor = 0;
13910 break;
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 EMACS_INT prop_pos =
13936 string_buffer_position_lim (glyph->object, pos_before,
13937 pos_after, 0);
13939 if (prop_pos >= pos_before)
13940 bpos_max = prop_pos - 1;
13942 if (INTEGERP (chprop))
13944 bpos_covered = bpos_max + XINT (chprop);
13945 /* If the `cursor' property covers buffer positions up
13946 to and including point, we should display cursor on
13947 this glyph. */
13948 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13950 cursor = glyph;
13951 break;
13954 string_seen = 1;
13956 --glyph;
13957 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
13959 x--; /* can't use any pixel_width */
13960 break;
13962 x -= glyph->pixel_width;
13965 /* Step 2: If we didn't find an exact match for point, we need to
13966 look for a proper place to put the cursor among glyphs between
13967 GLYPH_BEFORE and GLYPH_AFTER. */
13968 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13969 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
13970 && bpos_covered < pt_old)
13972 /* An empty line has a single glyph whose OBJECT is zero and
13973 whose CHARPOS is the position of a newline on that line.
13974 Note that on a TTY, there are more glyphs after that, which
13975 were produced by extend_face_to_end_of_line, but their
13976 CHARPOS is zero or negative. */
13977 int empty_line_p =
13978 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13979 && INTEGERP (glyph->object) && glyph->charpos > 0;
13981 if (row->ends_in_ellipsis_p && pos_after == last_pos)
13983 EMACS_INT ellipsis_pos;
13985 /* Scan back over the ellipsis glyphs. */
13986 if (!row->reversed_p)
13988 ellipsis_pos = (glyph - 1)->charpos;
13989 while (glyph > row->glyphs[TEXT_AREA]
13990 && (glyph - 1)->charpos == ellipsis_pos)
13991 glyph--, x -= glyph->pixel_width;
13992 /* That loop always goes one position too far, including
13993 the glyph before the ellipsis. So scan forward over
13994 that one. */
13995 x += glyph->pixel_width;
13996 glyph++;
13998 else /* row is reversed */
14000 ellipsis_pos = (glyph + 1)->charpos;
14001 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14002 && (glyph + 1)->charpos == ellipsis_pos)
14003 glyph++, x += glyph->pixel_width;
14004 x -= glyph->pixel_width;
14005 glyph--;
14008 else if (match_with_avoid_cursor)
14010 cursor = glyph_after;
14011 x = -1;
14013 else if (string_seen)
14015 int incr = row->reversed_p ? -1 : +1;
14017 /* Need to find the glyph that came out of a string which is
14018 present at point. That glyph is somewhere between
14019 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14020 positioned between POS_BEFORE and POS_AFTER in the
14021 buffer. */
14022 struct glyph *start, *stop;
14023 EMACS_INT pos = pos_before;
14025 x = -1;
14027 /* If the row ends in a newline from a display string,
14028 reordering could have moved the glyphs belonging to the
14029 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14030 in this case we extend the search to the last glyph in
14031 the row that was not inserted by redisplay. */
14032 if (row->ends_in_newline_from_string_p)
14034 glyph_after = end;
14035 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14038 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14039 correspond to POS_BEFORE and POS_AFTER, respectively. We
14040 need START and STOP in the order that corresponds to the
14041 row's direction as given by its reversed_p flag. If the
14042 directionality of characters between POS_BEFORE and
14043 POS_AFTER is the opposite of the row's base direction,
14044 these characters will have been reordered for display,
14045 and we need to reverse START and STOP. */
14046 if (!row->reversed_p)
14048 start = min (glyph_before, glyph_after);
14049 stop = max (glyph_before, glyph_after);
14051 else
14053 start = max (glyph_before, glyph_after);
14054 stop = min (glyph_before, glyph_after);
14056 for (glyph = start + incr;
14057 row->reversed_p ? glyph > stop : glyph < stop; )
14060 /* Any glyphs that come from the buffer are here because
14061 of bidi reordering. Skip them, and only pay
14062 attention to glyphs that came from some string. */
14063 if (STRINGP (glyph->object))
14065 Lisp_Object str;
14066 EMACS_INT tem;
14067 /* If the display property covers the newline, we
14068 need to search for it one position farther. */
14069 EMACS_INT lim = pos_after
14070 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14072 string_from_text_prop = 0;
14073 str = glyph->object;
14074 tem = string_buffer_position_lim (str, pos, lim, 0);
14075 if (tem == 0 /* from overlay */
14076 || pos <= tem)
14078 /* If the string from which this glyph came is
14079 found in the buffer at point, or at position
14080 that is closer to point than pos_after, then
14081 we've found the glyph we've been looking for.
14082 If it comes from an overlay (tem == 0), and
14083 it has the `cursor' property on one of its
14084 glyphs, record that glyph as a candidate for
14085 displaying the cursor. (As in the
14086 unidirectional version, we will display the
14087 cursor on the last candidate we find.) */
14088 if (tem == 0
14089 || tem == pt_old
14090 || (tem - pt_old > 0 && tem < pos_after))
14092 /* The glyphs from this string could have
14093 been reordered. Find the one with the
14094 smallest string position. Or there could
14095 be a character in the string with the
14096 `cursor' property, which means display
14097 cursor on that character's glyph. */
14098 EMACS_INT strpos = glyph->charpos;
14100 if (tem)
14102 cursor = glyph;
14103 string_from_text_prop = 1;
14105 for ( ;
14106 (row->reversed_p ? glyph > stop : glyph < stop)
14107 && EQ (glyph->object, str);
14108 glyph += incr)
14110 Lisp_Object cprop;
14111 EMACS_INT gpos = glyph->charpos;
14113 cprop = Fget_char_property (make_number (gpos),
14114 Qcursor,
14115 glyph->object);
14116 if (!NILP (cprop))
14118 cursor = glyph;
14119 break;
14121 if (tem && glyph->charpos < strpos)
14123 strpos = glyph->charpos;
14124 cursor = glyph;
14128 if (tem == pt_old
14129 || (tem - pt_old > 0 && tem < pos_after))
14130 goto compute_x;
14132 if (tem)
14133 pos = tem + 1; /* don't find previous instances */
14135 /* This string is not what we want; skip all of the
14136 glyphs that came from it. */
14137 while ((row->reversed_p ? glyph > stop : glyph < stop)
14138 && EQ (glyph->object, str))
14139 glyph += incr;
14141 else
14142 glyph += incr;
14145 /* If we reached the end of the line, and END was from a string,
14146 the cursor is not on this line. */
14147 if (cursor == NULL
14148 && (row->reversed_p ? glyph <= end : glyph >= end)
14149 && STRINGP (end->object)
14150 && row->continued_p)
14151 return 0;
14153 /* A truncated row may not include PT among its character positions.
14154 Setting the cursor inside the scroll margin will trigger
14155 recalculation of hscroll in hscroll_window_tree. But if a
14156 display string covers point, defer to the string-handling
14157 code below to figure this out. */
14158 else if (row->truncated_on_left_p && pt_old < bpos_min)
14160 cursor = glyph_before;
14161 x = -1;
14163 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14164 /* Zero-width characters produce no glyphs. */
14165 || (!empty_line_p
14166 && (row->reversed_p
14167 ? glyph_after > glyphs_end
14168 : glyph_after < glyphs_end)))
14170 cursor = glyph_after;
14171 x = -1;
14175 compute_x:
14176 if (cursor != NULL)
14177 glyph = cursor;
14178 if (x < 0)
14180 struct glyph *g;
14182 /* Need to compute x that corresponds to GLYPH. */
14183 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14185 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14186 abort ();
14187 x += g->pixel_width;
14191 /* ROW could be part of a continued line, which, under bidi
14192 reordering, might have other rows whose start and end charpos
14193 occlude point. Only set w->cursor if we found a better
14194 approximation to the cursor position than we have from previously
14195 examined candidate rows belonging to the same continued line. */
14196 if (/* we already have a candidate row */
14197 w->cursor.vpos >= 0
14198 /* that candidate is not the row we are processing */
14199 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14200 /* Make sure cursor.vpos specifies a row whose start and end
14201 charpos occlude point, and it is valid candidate for being a
14202 cursor-row. This is because some callers of this function
14203 leave cursor.vpos at the row where the cursor was displayed
14204 during the last redisplay cycle. */
14205 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14206 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14207 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14209 struct glyph *g1 =
14210 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14212 /* Don't consider glyphs that are outside TEXT_AREA. */
14213 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14214 return 0;
14215 /* Keep the candidate whose buffer position is the closest to
14216 point or has the `cursor' property. */
14217 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14218 w->cursor.hpos >= 0
14219 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14220 && ((BUFFERP (g1->object)
14221 && (g1->charpos == pt_old /* an exact match always wins */
14222 || (BUFFERP (glyph->object)
14223 && eabs (g1->charpos - pt_old)
14224 < eabs (glyph->charpos - pt_old))))
14225 /* previous candidate is a glyph from a string that has
14226 a non-nil `cursor' property */
14227 || (STRINGP (g1->object)
14228 && (!NILP (Fget_char_property (make_number (g1->charpos),
14229 Qcursor, g1->object))
14230 /* previous candidate is from the same display
14231 string as this one, and the display string
14232 came from a text property */
14233 || (EQ (g1->object, glyph->object)
14234 && string_from_text_prop)
14235 /* this candidate is from newline and its
14236 position is not an exact match */
14237 || (INTEGERP (glyph->object)
14238 && glyph->charpos != pt_old)))))
14239 return 0;
14240 /* If this candidate gives an exact match, use that. */
14241 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14242 /* If this candidate is a glyph created for the
14243 terminating newline of a line, and point is on that
14244 newline, it wins because it's an exact match. */
14245 || (!row->continued_p
14246 && INTEGERP (glyph->object)
14247 && glyph->charpos == 0
14248 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14249 /* Otherwise, keep the candidate that comes from a row
14250 spanning less buffer positions. This may win when one or
14251 both candidate positions are on glyphs that came from
14252 display strings, for which we cannot compare buffer
14253 positions. */
14254 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14255 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14256 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14257 return 0;
14259 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14260 w->cursor.x = x;
14261 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14262 w->cursor.y = row->y + dy;
14264 if (w == XWINDOW (selected_window))
14266 if (!row->continued_p
14267 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14268 && row->x == 0)
14270 this_line_buffer = XBUFFER (w->buffer);
14272 CHARPOS (this_line_start_pos)
14273 = MATRIX_ROW_START_CHARPOS (row) + delta;
14274 BYTEPOS (this_line_start_pos)
14275 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14277 CHARPOS (this_line_end_pos)
14278 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14279 BYTEPOS (this_line_end_pos)
14280 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14282 this_line_y = w->cursor.y;
14283 this_line_pixel_height = row->height;
14284 this_line_vpos = w->cursor.vpos;
14285 this_line_start_x = row->x;
14287 else
14288 CHARPOS (this_line_start_pos) = 0;
14291 return 1;
14295 /* Run window scroll functions, if any, for WINDOW with new window
14296 start STARTP. Sets the window start of WINDOW to that position.
14298 We assume that the window's buffer is really current. */
14300 static inline struct text_pos
14301 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14303 struct window *w = XWINDOW (window);
14304 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14306 if (current_buffer != XBUFFER (w->buffer))
14307 abort ();
14309 if (!NILP (Vwindow_scroll_functions))
14311 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14312 make_number (CHARPOS (startp)));
14313 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14314 /* In case the hook functions switch buffers. */
14315 if (current_buffer != XBUFFER (w->buffer))
14316 set_buffer_internal_1 (XBUFFER (w->buffer));
14319 return startp;
14323 /* Make sure the line containing the cursor is fully visible.
14324 A value of 1 means there is nothing to be done.
14325 (Either the line is fully visible, or it cannot be made so,
14326 or we cannot tell.)
14328 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14329 is higher than window.
14331 A value of 0 means the caller should do scrolling
14332 as if point had gone off the screen. */
14334 static int
14335 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14337 struct glyph_matrix *matrix;
14338 struct glyph_row *row;
14339 int window_height;
14341 if (!make_cursor_line_fully_visible_p)
14342 return 1;
14344 /* It's not always possible to find the cursor, e.g, when a window
14345 is full of overlay strings. Don't do anything in that case. */
14346 if (w->cursor.vpos < 0)
14347 return 1;
14349 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14350 row = MATRIX_ROW (matrix, w->cursor.vpos);
14352 /* If the cursor row is not partially visible, there's nothing to do. */
14353 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14354 return 1;
14356 /* If the row the cursor is in is taller than the window's height,
14357 it's not clear what to do, so do nothing. */
14358 window_height = window_box_height (w);
14359 if (row->height >= window_height)
14361 if (!force_p || MINI_WINDOW_P (w)
14362 || w->vscroll || w->cursor.vpos == 0)
14363 return 1;
14365 return 0;
14369 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14370 non-zero means only WINDOW is redisplayed in redisplay_internal.
14371 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14372 in redisplay_window to bring a partially visible line into view in
14373 the case that only the cursor has moved.
14375 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14376 last screen line's vertical height extends past the end of the screen.
14378 Value is
14380 1 if scrolling succeeded
14382 0 if scrolling didn't find point.
14384 -1 if new fonts have been loaded so that we must interrupt
14385 redisplay, adjust glyph matrices, and try again. */
14387 enum
14389 SCROLLING_SUCCESS,
14390 SCROLLING_FAILED,
14391 SCROLLING_NEED_LARGER_MATRICES
14394 /* If scroll-conservatively is more than this, never recenter.
14396 If you change this, don't forget to update the doc string of
14397 `scroll-conservatively' and the Emacs manual. */
14398 #define SCROLL_LIMIT 100
14400 static int
14401 try_scrolling (Lisp_Object window, int just_this_one_p,
14402 EMACS_INT arg_scroll_conservatively, EMACS_INT scroll_step,
14403 int temp_scroll_step, int last_line_misfit)
14405 struct window *w = XWINDOW (window);
14406 struct frame *f = XFRAME (w->frame);
14407 struct text_pos pos, startp;
14408 struct it it;
14409 int this_scroll_margin, scroll_max, rc, height;
14410 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14411 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14412 Lisp_Object aggressive;
14413 /* We will never try scrolling more than this number of lines. */
14414 int scroll_limit = SCROLL_LIMIT;
14416 #if GLYPH_DEBUG
14417 debug_method_add (w, "try_scrolling");
14418 #endif
14420 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14422 /* Compute scroll margin height in pixels. We scroll when point is
14423 within this distance from the top or bottom of the window. */
14424 if (scroll_margin > 0)
14425 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14426 * FRAME_LINE_HEIGHT (f);
14427 else
14428 this_scroll_margin = 0;
14430 /* Force arg_scroll_conservatively to have a reasonable value, to
14431 avoid scrolling too far away with slow move_it_* functions. Note
14432 that the user can supply scroll-conservatively equal to
14433 `most-positive-fixnum', which can be larger than INT_MAX. */
14434 if (arg_scroll_conservatively > scroll_limit)
14436 arg_scroll_conservatively = scroll_limit + 1;
14437 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14439 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14440 /* Compute how much we should try to scroll maximally to bring
14441 point into view. */
14442 scroll_max = (max (scroll_step,
14443 max (arg_scroll_conservatively, temp_scroll_step))
14444 * FRAME_LINE_HEIGHT (f));
14445 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14446 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14447 /* We're trying to scroll because of aggressive scrolling but no
14448 scroll_step is set. Choose an arbitrary one. */
14449 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14450 else
14451 scroll_max = 0;
14453 too_near_end:
14455 /* Decide whether to scroll down. */
14456 if (PT > CHARPOS (startp))
14458 int scroll_margin_y;
14460 /* Compute the pixel ypos of the scroll margin, then move IT to
14461 either that ypos or PT, whichever comes first. */
14462 start_display (&it, w, startp);
14463 scroll_margin_y = it.last_visible_y - this_scroll_margin
14464 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14465 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14466 (MOVE_TO_POS | MOVE_TO_Y));
14468 if (PT > CHARPOS (it.current.pos))
14470 int y0 = line_bottom_y (&it);
14471 /* Compute how many pixels below window bottom to stop searching
14472 for PT. This avoids costly search for PT that is far away if
14473 the user limited scrolling by a small number of lines, but
14474 always finds PT if scroll_conservatively is set to a large
14475 number, such as most-positive-fixnum. */
14476 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14477 int y_to_move = it.last_visible_y + slack;
14479 /* Compute the distance from the scroll margin to PT or to
14480 the scroll limit, whichever comes first. This should
14481 include the height of the cursor line, to make that line
14482 fully visible. */
14483 move_it_to (&it, PT, -1, y_to_move,
14484 -1, MOVE_TO_POS | MOVE_TO_Y);
14485 dy = line_bottom_y (&it) - y0;
14487 if (dy > scroll_max)
14488 return SCROLLING_FAILED;
14490 if (dy > 0)
14491 scroll_down_p = 1;
14495 if (scroll_down_p)
14497 /* Point is in or below the bottom scroll margin, so move the
14498 window start down. If scrolling conservatively, move it just
14499 enough down to make point visible. If scroll_step is set,
14500 move it down by scroll_step. */
14501 if (arg_scroll_conservatively)
14502 amount_to_scroll
14503 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14504 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14505 else if (scroll_step || temp_scroll_step)
14506 amount_to_scroll = scroll_max;
14507 else
14509 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14510 height = WINDOW_BOX_TEXT_HEIGHT (w);
14511 if (NUMBERP (aggressive))
14513 double float_amount = XFLOATINT (aggressive) * height;
14514 amount_to_scroll = float_amount;
14515 if (amount_to_scroll == 0 && float_amount > 0)
14516 amount_to_scroll = 1;
14517 /* Don't let point enter the scroll margin near top of
14518 the window. */
14519 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14520 amount_to_scroll = height - 2*this_scroll_margin + dy;
14524 if (amount_to_scroll <= 0)
14525 return SCROLLING_FAILED;
14527 start_display (&it, w, startp);
14528 if (arg_scroll_conservatively <= scroll_limit)
14529 move_it_vertically (&it, amount_to_scroll);
14530 else
14532 /* Extra precision for users who set scroll-conservatively
14533 to a large number: make sure the amount we scroll
14534 the window start is never less than amount_to_scroll,
14535 which was computed as distance from window bottom to
14536 point. This matters when lines at window top and lines
14537 below window bottom have different height. */
14538 struct it it1;
14539 void *it1data = NULL;
14540 /* We use a temporary it1 because line_bottom_y can modify
14541 its argument, if it moves one line down; see there. */
14542 int start_y;
14544 SAVE_IT (it1, it, it1data);
14545 start_y = line_bottom_y (&it1);
14546 do {
14547 RESTORE_IT (&it, &it, it1data);
14548 move_it_by_lines (&it, 1);
14549 SAVE_IT (it1, it, it1data);
14550 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14553 /* If STARTP is unchanged, move it down another screen line. */
14554 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14555 move_it_by_lines (&it, 1);
14556 startp = it.current.pos;
14558 else
14560 struct text_pos scroll_margin_pos = startp;
14562 /* See if point is inside the scroll margin at the top of the
14563 window. */
14564 if (this_scroll_margin)
14566 start_display (&it, w, startp);
14567 move_it_vertically (&it, this_scroll_margin);
14568 scroll_margin_pos = it.current.pos;
14571 if (PT < CHARPOS (scroll_margin_pos))
14573 /* Point is in the scroll margin at the top of the window or
14574 above what is displayed in the window. */
14575 int y0, y_to_move;
14577 /* Compute the vertical distance from PT to the scroll
14578 margin position. Move as far as scroll_max allows, or
14579 one screenful, or 10 screen lines, whichever is largest.
14580 Give up if distance is greater than scroll_max. */
14581 SET_TEXT_POS (pos, PT, PT_BYTE);
14582 start_display (&it, w, pos);
14583 y0 = it.current_y;
14584 y_to_move = max (it.last_visible_y,
14585 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14586 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14587 y_to_move, -1,
14588 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14589 dy = it.current_y - y0;
14590 if (dy > scroll_max)
14591 return SCROLLING_FAILED;
14593 /* Compute new window start. */
14594 start_display (&it, w, startp);
14596 if (arg_scroll_conservatively)
14597 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14598 max (scroll_step, temp_scroll_step));
14599 else if (scroll_step || temp_scroll_step)
14600 amount_to_scroll = scroll_max;
14601 else
14603 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14604 height = WINDOW_BOX_TEXT_HEIGHT (w);
14605 if (NUMBERP (aggressive))
14607 double float_amount = XFLOATINT (aggressive) * height;
14608 amount_to_scroll = float_amount;
14609 if (amount_to_scroll == 0 && float_amount > 0)
14610 amount_to_scroll = 1;
14611 amount_to_scroll -=
14612 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14613 /* Don't let point enter the scroll margin near
14614 bottom of the window. */
14615 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14616 amount_to_scroll = height - 2*this_scroll_margin + dy;
14620 if (amount_to_scroll <= 0)
14621 return SCROLLING_FAILED;
14623 move_it_vertically_backward (&it, amount_to_scroll);
14624 startp = it.current.pos;
14628 /* Run window scroll functions. */
14629 startp = run_window_scroll_functions (window, startp);
14631 /* Display the window. Give up if new fonts are loaded, or if point
14632 doesn't appear. */
14633 if (!try_window (window, startp, 0))
14634 rc = SCROLLING_NEED_LARGER_MATRICES;
14635 else if (w->cursor.vpos < 0)
14637 clear_glyph_matrix (w->desired_matrix);
14638 rc = SCROLLING_FAILED;
14640 else
14642 /* Maybe forget recorded base line for line number display. */
14643 if (!just_this_one_p
14644 || current_buffer->clip_changed
14645 || BEG_UNCHANGED < CHARPOS (startp))
14646 w->base_line_number = Qnil;
14648 /* If cursor ends up on a partially visible line,
14649 treat that as being off the bottom of the screen. */
14650 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14651 /* It's possible that the cursor is on the first line of the
14652 buffer, which is partially obscured due to a vscroll
14653 (Bug#7537). In that case, avoid looping forever . */
14654 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14656 clear_glyph_matrix (w->desired_matrix);
14657 ++extra_scroll_margin_lines;
14658 goto too_near_end;
14660 rc = SCROLLING_SUCCESS;
14663 return rc;
14667 /* Compute a suitable window start for window W if display of W starts
14668 on a continuation line. Value is non-zero if a new window start
14669 was computed.
14671 The new window start will be computed, based on W's width, starting
14672 from the start of the continued line. It is the start of the
14673 screen line with the minimum distance from the old start W->start. */
14675 static int
14676 compute_window_start_on_continuation_line (struct window *w)
14678 struct text_pos pos, start_pos;
14679 int window_start_changed_p = 0;
14681 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14683 /* If window start is on a continuation line... Window start may be
14684 < BEGV in case there's invisible text at the start of the
14685 buffer (M-x rmail, for example). */
14686 if (CHARPOS (start_pos) > BEGV
14687 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14689 struct it it;
14690 struct glyph_row *row;
14692 /* Handle the case that the window start is out of range. */
14693 if (CHARPOS (start_pos) < BEGV)
14694 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14695 else if (CHARPOS (start_pos) > ZV)
14696 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14698 /* Find the start of the continued line. This should be fast
14699 because scan_buffer is fast (newline cache). */
14700 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14701 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14702 row, DEFAULT_FACE_ID);
14703 reseat_at_previous_visible_line_start (&it);
14705 /* If the line start is "too far" away from the window start,
14706 say it takes too much time to compute a new window start. */
14707 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14708 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14710 int min_distance, distance;
14712 /* Move forward by display lines to find the new window
14713 start. If window width was enlarged, the new start can
14714 be expected to be > the old start. If window width was
14715 decreased, the new window start will be < the old start.
14716 So, we're looking for the display line start with the
14717 minimum distance from the old window start. */
14718 pos = it.current.pos;
14719 min_distance = INFINITY;
14720 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14721 distance < min_distance)
14723 min_distance = distance;
14724 pos = it.current.pos;
14725 move_it_by_lines (&it, 1);
14728 /* Set the window start there. */
14729 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14730 window_start_changed_p = 1;
14734 return window_start_changed_p;
14738 /* Try cursor movement in case text has not changed in window WINDOW,
14739 with window start STARTP. Value is
14741 CURSOR_MOVEMENT_SUCCESS if successful
14743 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14745 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14746 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14747 we want to scroll as if scroll-step were set to 1. See the code.
14749 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14750 which case we have to abort this redisplay, and adjust matrices
14751 first. */
14753 enum
14755 CURSOR_MOVEMENT_SUCCESS,
14756 CURSOR_MOVEMENT_CANNOT_BE_USED,
14757 CURSOR_MOVEMENT_MUST_SCROLL,
14758 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14761 static int
14762 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14764 struct window *w = XWINDOW (window);
14765 struct frame *f = XFRAME (w->frame);
14766 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14768 #if GLYPH_DEBUG
14769 if (inhibit_try_cursor_movement)
14770 return rc;
14771 #endif
14773 /* Handle case where text has not changed, only point, and it has
14774 not moved off the frame. */
14775 if (/* Point may be in this window. */
14776 PT >= CHARPOS (startp)
14777 /* Selective display hasn't changed. */
14778 && !current_buffer->clip_changed
14779 /* Function force-mode-line-update is used to force a thorough
14780 redisplay. It sets either windows_or_buffers_changed or
14781 update_mode_lines. So don't take a shortcut here for these
14782 cases. */
14783 && !update_mode_lines
14784 && !windows_or_buffers_changed
14785 && !cursor_type_changed
14786 /* Can't use this case if highlighting a region. When a
14787 region exists, cursor movement has to do more than just
14788 set the cursor. */
14789 && !(!NILP (Vtransient_mark_mode)
14790 && !NILP (BVAR (current_buffer, mark_active)))
14791 && NILP (w->region_showing)
14792 && NILP (Vshow_trailing_whitespace)
14793 /* Right after splitting windows, last_point may be nil. */
14794 && INTEGERP (w->last_point)
14795 /* This code is not used for mini-buffer for the sake of the case
14796 of redisplaying to replace an echo area message; since in
14797 that case the mini-buffer contents per se are usually
14798 unchanged. This code is of no real use in the mini-buffer
14799 since the handling of this_line_start_pos, etc., in redisplay
14800 handles the same cases. */
14801 && !EQ (window, minibuf_window)
14802 /* When splitting windows or for new windows, it happens that
14803 redisplay is called with a nil window_end_vpos or one being
14804 larger than the window. This should really be fixed in
14805 window.c. I don't have this on my list, now, so we do
14806 approximately the same as the old redisplay code. --gerd. */
14807 && INTEGERP (w->window_end_vpos)
14808 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14809 && (FRAME_WINDOW_P (f)
14810 || !overlay_arrow_in_current_buffer_p ()))
14812 int this_scroll_margin, top_scroll_margin;
14813 struct glyph_row *row = NULL;
14815 #if GLYPH_DEBUG
14816 debug_method_add (w, "cursor movement");
14817 #endif
14819 /* Scroll if point within this distance from the top or bottom
14820 of the window. This is a pixel value. */
14821 if (scroll_margin > 0)
14823 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14824 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14826 else
14827 this_scroll_margin = 0;
14829 top_scroll_margin = this_scroll_margin;
14830 if (WINDOW_WANTS_HEADER_LINE_P (w))
14831 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14833 /* Start with the row the cursor was displayed during the last
14834 not paused redisplay. Give up if that row is not valid. */
14835 if (w->last_cursor.vpos < 0
14836 || w->last_cursor.vpos >= w->current_matrix->nrows)
14837 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14838 else
14840 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14841 if (row->mode_line_p)
14842 ++row;
14843 if (!row->enabled_p)
14844 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14847 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14849 int scroll_p = 0, must_scroll = 0;
14850 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14852 if (PT > XFASTINT (w->last_point))
14854 /* Point has moved forward. */
14855 while (MATRIX_ROW_END_CHARPOS (row) < PT
14856 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14858 xassert (row->enabled_p);
14859 ++row;
14862 /* If the end position of a row equals the start
14863 position of the next row, and PT is at that position,
14864 we would rather display cursor in the next line. */
14865 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14866 && MATRIX_ROW_END_CHARPOS (row) == PT
14867 && row < w->current_matrix->rows
14868 + w->current_matrix->nrows - 1
14869 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14870 && !cursor_row_p (row))
14871 ++row;
14873 /* If within the scroll margin, scroll. Note that
14874 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14875 the next line would be drawn, and that
14876 this_scroll_margin can be zero. */
14877 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14878 || PT > MATRIX_ROW_END_CHARPOS (row)
14879 /* Line is completely visible last line in window
14880 and PT is to be set in the next line. */
14881 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14882 && PT == MATRIX_ROW_END_CHARPOS (row)
14883 && !row->ends_at_zv_p
14884 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14885 scroll_p = 1;
14887 else if (PT < XFASTINT (w->last_point))
14889 /* Cursor has to be moved backward. Note that PT >=
14890 CHARPOS (startp) because of the outer if-statement. */
14891 while (!row->mode_line_p
14892 && (MATRIX_ROW_START_CHARPOS (row) > PT
14893 || (MATRIX_ROW_START_CHARPOS (row) == PT
14894 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14895 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14896 row > w->current_matrix->rows
14897 && (row-1)->ends_in_newline_from_string_p))))
14898 && (row->y > top_scroll_margin
14899 || CHARPOS (startp) == BEGV))
14901 xassert (row->enabled_p);
14902 --row;
14905 /* Consider the following case: Window starts at BEGV,
14906 there is invisible, intangible text at BEGV, so that
14907 display starts at some point START > BEGV. It can
14908 happen that we are called with PT somewhere between
14909 BEGV and START. Try to handle that case. */
14910 if (row < w->current_matrix->rows
14911 || row->mode_line_p)
14913 row = w->current_matrix->rows;
14914 if (row->mode_line_p)
14915 ++row;
14918 /* Due to newlines in overlay strings, we may have to
14919 skip forward over overlay strings. */
14920 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14921 && MATRIX_ROW_END_CHARPOS (row) == PT
14922 && !cursor_row_p (row))
14923 ++row;
14925 /* If within the scroll margin, scroll. */
14926 if (row->y < top_scroll_margin
14927 && CHARPOS (startp) != BEGV)
14928 scroll_p = 1;
14930 else
14932 /* Cursor did not move. So don't scroll even if cursor line
14933 is partially visible, as it was so before. */
14934 rc = CURSOR_MOVEMENT_SUCCESS;
14937 if (PT < MATRIX_ROW_START_CHARPOS (row)
14938 || PT > MATRIX_ROW_END_CHARPOS (row))
14940 /* if PT is not in the glyph row, give up. */
14941 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14942 must_scroll = 1;
14944 else if (rc != CURSOR_MOVEMENT_SUCCESS
14945 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14947 struct glyph_row *row1;
14949 /* If rows are bidi-reordered and point moved, back up
14950 until we find a row that does not belong to a
14951 continuation line. This is because we must consider
14952 all rows of a continued line as candidates for the
14953 new cursor positioning, since row start and end
14954 positions change non-linearly with vertical position
14955 in such rows. */
14956 /* FIXME: Revisit this when glyph ``spilling'' in
14957 continuation lines' rows is implemented for
14958 bidi-reordered rows. */
14959 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14960 MATRIX_ROW_CONTINUATION_LINE_P (row);
14961 --row)
14963 /* If we hit the beginning of the displayed portion
14964 without finding the first row of a continued
14965 line, give up. */
14966 if (row <= row1)
14968 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14969 break;
14971 xassert (row->enabled_p);
14974 if (must_scroll)
14976 else if (rc != CURSOR_MOVEMENT_SUCCESS
14977 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
14978 /* Make sure this isn't a header line by any chance, since
14979 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
14980 && !row->mode_line_p
14981 && make_cursor_line_fully_visible_p)
14983 if (PT == MATRIX_ROW_END_CHARPOS (row)
14984 && !row->ends_at_zv_p
14985 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14986 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14987 else if (row->height > window_box_height (w))
14989 /* If we end up in a partially visible line, let's
14990 make it fully visible, except when it's taller
14991 than the window, in which case we can't do much
14992 about it. */
14993 *scroll_step = 1;
14994 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14996 else
14998 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14999 if (!cursor_row_fully_visible_p (w, 0, 1))
15000 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15001 else
15002 rc = CURSOR_MOVEMENT_SUCCESS;
15005 else if (scroll_p)
15006 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15007 else if (rc != CURSOR_MOVEMENT_SUCCESS
15008 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15010 /* With bidi-reordered rows, there could be more than
15011 one candidate row whose start and end positions
15012 occlude point. We need to let set_cursor_from_row
15013 find the best candidate. */
15014 /* FIXME: Revisit this when glyph ``spilling'' in
15015 continuation lines' rows is implemented for
15016 bidi-reordered rows. */
15017 int rv = 0;
15021 int at_zv_p = 0, exact_match_p = 0;
15023 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15024 && PT <= MATRIX_ROW_END_CHARPOS (row)
15025 && cursor_row_p (row))
15026 rv |= set_cursor_from_row (w, row, w->current_matrix,
15027 0, 0, 0, 0);
15028 /* As soon as we've found the exact match for point,
15029 or the first suitable row whose ends_at_zv_p flag
15030 is set, we are done. */
15031 at_zv_p =
15032 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15033 if (rv && !at_zv_p
15034 && w->cursor.hpos >= 0
15035 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15036 w->cursor.vpos))
15038 struct glyph_row *candidate =
15039 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15040 struct glyph *g =
15041 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15042 EMACS_INT endpos = MATRIX_ROW_END_CHARPOS (candidate);
15044 exact_match_p =
15045 (BUFFERP (g->object) && g->charpos == PT)
15046 || (INTEGERP (g->object)
15047 && (g->charpos == PT
15048 || (g->charpos == 0 && endpos - 1 == PT)));
15050 if (rv && (at_zv_p || exact_match_p))
15052 rc = CURSOR_MOVEMENT_SUCCESS;
15053 break;
15055 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15056 break;
15057 ++row;
15059 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15060 || row->continued_p)
15061 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15062 || (MATRIX_ROW_START_CHARPOS (row) == PT
15063 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15064 /* If we didn't find any candidate rows, or exited the
15065 loop before all the candidates were examined, signal
15066 to the caller that this method failed. */
15067 if (rc != CURSOR_MOVEMENT_SUCCESS
15068 && !(rv
15069 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15070 && !row->continued_p))
15071 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15072 else if (rv)
15073 rc = CURSOR_MOVEMENT_SUCCESS;
15075 else
15079 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15081 rc = CURSOR_MOVEMENT_SUCCESS;
15082 break;
15084 ++row;
15086 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15087 && MATRIX_ROW_START_CHARPOS (row) == PT
15088 && cursor_row_p (row));
15093 return rc;
15096 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15097 static
15098 #endif
15099 void
15100 set_vertical_scroll_bar (struct window *w)
15102 EMACS_INT start, end, whole;
15104 /* Calculate the start and end positions for the current window.
15105 At some point, it would be nice to choose between scrollbars
15106 which reflect the whole buffer size, with special markers
15107 indicating narrowing, and scrollbars which reflect only the
15108 visible region.
15110 Note that mini-buffers sometimes aren't displaying any text. */
15111 if (!MINI_WINDOW_P (w)
15112 || (w == XWINDOW (minibuf_window)
15113 && NILP (echo_area_buffer[0])))
15115 struct buffer *buf = XBUFFER (w->buffer);
15116 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15117 start = marker_position (w->start) - BUF_BEGV (buf);
15118 /* I don't think this is guaranteed to be right. For the
15119 moment, we'll pretend it is. */
15120 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15122 if (end < start)
15123 end = start;
15124 if (whole < (end - start))
15125 whole = end - start;
15127 else
15128 start = end = whole = 0;
15130 /* Indicate what this scroll bar ought to be displaying now. */
15131 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15132 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15133 (w, end - start, whole, start);
15137 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15138 selected_window is redisplayed.
15140 We can return without actually redisplaying the window if
15141 fonts_changed_p is nonzero. In that case, redisplay_internal will
15142 retry. */
15144 static void
15145 redisplay_window (Lisp_Object window, int just_this_one_p)
15147 struct window *w = XWINDOW (window);
15148 struct frame *f = XFRAME (w->frame);
15149 struct buffer *buffer = XBUFFER (w->buffer);
15150 struct buffer *old = current_buffer;
15151 struct text_pos lpoint, opoint, startp;
15152 int update_mode_line;
15153 int tem;
15154 struct it it;
15155 /* Record it now because it's overwritten. */
15156 int current_matrix_up_to_date_p = 0;
15157 int used_current_matrix_p = 0;
15158 /* This is less strict than current_matrix_up_to_date_p.
15159 It indicates that the buffer contents and narrowing are unchanged. */
15160 int buffer_unchanged_p = 0;
15161 int temp_scroll_step = 0;
15162 int count = SPECPDL_INDEX ();
15163 int rc;
15164 int centering_position = -1;
15165 int last_line_misfit = 0;
15166 EMACS_INT beg_unchanged, end_unchanged;
15168 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15169 opoint = lpoint;
15171 /* W must be a leaf window here. */
15172 xassert (!NILP (w->buffer));
15173 #if GLYPH_DEBUG
15174 *w->desired_matrix->method = 0;
15175 #endif
15177 restart:
15178 reconsider_clip_changes (w, buffer);
15180 /* Has the mode line to be updated? */
15181 update_mode_line = (!NILP (w->update_mode_line)
15182 || update_mode_lines
15183 || buffer->clip_changed
15184 || buffer->prevent_redisplay_optimizations_p);
15186 if (MINI_WINDOW_P (w))
15188 if (w == XWINDOW (echo_area_window)
15189 && !NILP (echo_area_buffer[0]))
15191 if (update_mode_line)
15192 /* We may have to update a tty frame's menu bar or a
15193 tool-bar. Example `M-x C-h C-h C-g'. */
15194 goto finish_menu_bars;
15195 else
15196 /* We've already displayed the echo area glyphs in this window. */
15197 goto finish_scroll_bars;
15199 else if ((w != XWINDOW (minibuf_window)
15200 || minibuf_level == 0)
15201 /* When buffer is nonempty, redisplay window normally. */
15202 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15203 /* Quail displays non-mini buffers in minibuffer window.
15204 In that case, redisplay the window normally. */
15205 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15207 /* W is a mini-buffer window, but it's not active, so clear
15208 it. */
15209 int yb = window_text_bottom_y (w);
15210 struct glyph_row *row;
15211 int y;
15213 for (y = 0, row = w->desired_matrix->rows;
15214 y < yb;
15215 y += row->height, ++row)
15216 blank_row (w, row, y);
15217 goto finish_scroll_bars;
15220 clear_glyph_matrix (w->desired_matrix);
15223 /* Otherwise set up data on this window; select its buffer and point
15224 value. */
15225 /* Really select the buffer, for the sake of buffer-local
15226 variables. */
15227 set_buffer_internal_1 (XBUFFER (w->buffer));
15229 current_matrix_up_to_date_p
15230 = (!NILP (w->window_end_valid)
15231 && !current_buffer->clip_changed
15232 && !current_buffer->prevent_redisplay_optimizations_p
15233 && XFASTINT (w->last_modified) >= MODIFF
15234 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15236 /* Run the window-bottom-change-functions
15237 if it is possible that the text on the screen has changed
15238 (either due to modification of the text, or any other reason). */
15239 if (!current_matrix_up_to_date_p
15240 && !NILP (Vwindow_text_change_functions))
15242 safe_run_hooks (Qwindow_text_change_functions);
15243 goto restart;
15246 beg_unchanged = BEG_UNCHANGED;
15247 end_unchanged = END_UNCHANGED;
15249 SET_TEXT_POS (opoint, PT, PT_BYTE);
15251 specbind (Qinhibit_point_motion_hooks, Qt);
15253 buffer_unchanged_p
15254 = (!NILP (w->window_end_valid)
15255 && !current_buffer->clip_changed
15256 && XFASTINT (w->last_modified) >= MODIFF
15257 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15259 /* When windows_or_buffers_changed is non-zero, we can't rely on
15260 the window end being valid, so set it to nil there. */
15261 if (windows_or_buffers_changed)
15263 /* If window starts on a continuation line, maybe adjust the
15264 window start in case the window's width changed. */
15265 if (XMARKER (w->start)->buffer == current_buffer)
15266 compute_window_start_on_continuation_line (w);
15268 w->window_end_valid = Qnil;
15271 /* Some sanity checks. */
15272 CHECK_WINDOW_END (w);
15273 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15274 abort ();
15275 if (BYTEPOS (opoint) < CHARPOS (opoint))
15276 abort ();
15278 /* If %c is in mode line, update it if needed. */
15279 if (!NILP (w->column_number_displayed)
15280 /* This alternative quickly identifies a common case
15281 where no change is needed. */
15282 && !(PT == XFASTINT (w->last_point)
15283 && XFASTINT (w->last_modified) >= MODIFF
15284 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
15285 && (XFASTINT (w->column_number_displayed) != current_column ()))
15286 update_mode_line = 1;
15288 /* Count number of windows showing the selected buffer. An indirect
15289 buffer counts as its base buffer. */
15290 if (!just_this_one_p)
15292 struct buffer *current_base, *window_base;
15293 current_base = current_buffer;
15294 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15295 if (current_base->base_buffer)
15296 current_base = current_base->base_buffer;
15297 if (window_base->base_buffer)
15298 window_base = window_base->base_buffer;
15299 if (current_base == window_base)
15300 buffer_shared++;
15303 /* Point refers normally to the selected window. For any other
15304 window, set up appropriate value. */
15305 if (!EQ (window, selected_window))
15307 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
15308 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
15309 if (new_pt < BEGV)
15311 new_pt = BEGV;
15312 new_pt_byte = BEGV_BYTE;
15313 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15315 else if (new_pt > (ZV - 1))
15317 new_pt = ZV;
15318 new_pt_byte = ZV_BYTE;
15319 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15322 /* We don't use SET_PT so that the point-motion hooks don't run. */
15323 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15326 /* If any of the character widths specified in the display table
15327 have changed, invalidate the width run cache. It's true that
15328 this may be a bit late to catch such changes, but the rest of
15329 redisplay goes (non-fatally) haywire when the display table is
15330 changed, so why should we worry about doing any better? */
15331 if (current_buffer->width_run_cache)
15333 struct Lisp_Char_Table *disptab = buffer_display_table ();
15335 if (! disptab_matches_widthtab (disptab,
15336 XVECTOR (BVAR (current_buffer, width_table))))
15338 invalidate_region_cache (current_buffer,
15339 current_buffer->width_run_cache,
15340 BEG, Z);
15341 recompute_width_table (current_buffer, disptab);
15345 /* If window-start is screwed up, choose a new one. */
15346 if (XMARKER (w->start)->buffer != current_buffer)
15347 goto recenter;
15349 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15351 /* If someone specified a new starting point but did not insist,
15352 check whether it can be used. */
15353 if (!NILP (w->optional_new_start)
15354 && CHARPOS (startp) >= BEGV
15355 && CHARPOS (startp) <= ZV)
15357 w->optional_new_start = Qnil;
15358 start_display (&it, w, startp);
15359 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15360 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15361 if (IT_CHARPOS (it) == PT)
15362 w->force_start = Qt;
15363 /* IT may overshoot PT if text at PT is invisible. */
15364 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15365 w->force_start = Qt;
15368 force_start:
15370 /* Handle case where place to start displaying has been specified,
15371 unless the specified location is outside the accessible range. */
15372 if (!NILP (w->force_start)
15373 || w->frozen_window_start_p)
15375 /* We set this later on if we have to adjust point. */
15376 int new_vpos = -1;
15378 w->force_start = Qnil;
15379 w->vscroll = 0;
15380 w->window_end_valid = Qnil;
15382 /* Forget any recorded base line for line number display. */
15383 if (!buffer_unchanged_p)
15384 w->base_line_number = Qnil;
15386 /* Redisplay the mode line. Select the buffer properly for that.
15387 Also, run the hook window-scroll-functions
15388 because we have scrolled. */
15389 /* Note, we do this after clearing force_start because
15390 if there's an error, it is better to forget about force_start
15391 than to get into an infinite loop calling the hook functions
15392 and having them get more errors. */
15393 if (!update_mode_line
15394 || ! NILP (Vwindow_scroll_functions))
15396 update_mode_line = 1;
15397 w->update_mode_line = Qt;
15398 startp = run_window_scroll_functions (window, startp);
15401 w->last_modified = make_number (0);
15402 w->last_overlay_modified = make_number (0);
15403 if (CHARPOS (startp) < BEGV)
15404 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15405 else if (CHARPOS (startp) > ZV)
15406 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15408 /* Redisplay, then check if cursor has been set during the
15409 redisplay. Give up if new fonts were loaded. */
15410 /* We used to issue a CHECK_MARGINS argument to try_window here,
15411 but this causes scrolling to fail when point begins inside
15412 the scroll margin (bug#148) -- cyd */
15413 if (!try_window (window, startp, 0))
15415 w->force_start = Qt;
15416 clear_glyph_matrix (w->desired_matrix);
15417 goto need_larger_matrices;
15420 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15422 /* If point does not appear, try to move point so it does
15423 appear. The desired matrix has been built above, so we
15424 can use it here. */
15425 new_vpos = window_box_height (w) / 2;
15428 if (!cursor_row_fully_visible_p (w, 0, 0))
15430 /* Point does appear, but on a line partly visible at end of window.
15431 Move it back to a fully-visible line. */
15432 new_vpos = window_box_height (w);
15435 /* If we need to move point for either of the above reasons,
15436 now actually do it. */
15437 if (new_vpos >= 0)
15439 struct glyph_row *row;
15441 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15442 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15443 ++row;
15445 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15446 MATRIX_ROW_START_BYTEPOS (row));
15448 if (w != XWINDOW (selected_window))
15449 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15450 else if (current_buffer == old)
15451 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15453 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15455 /* If we are highlighting the region, then we just changed
15456 the region, so redisplay to show it. */
15457 if (!NILP (Vtransient_mark_mode)
15458 && !NILP (BVAR (current_buffer, mark_active)))
15460 clear_glyph_matrix (w->desired_matrix);
15461 if (!try_window (window, startp, 0))
15462 goto need_larger_matrices;
15466 #if GLYPH_DEBUG
15467 debug_method_add (w, "forced window start");
15468 #endif
15469 goto done;
15472 /* Handle case where text has not changed, only point, and it has
15473 not moved off the frame, and we are not retrying after hscroll.
15474 (current_matrix_up_to_date_p is nonzero when retrying.) */
15475 if (current_matrix_up_to_date_p
15476 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15477 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15479 switch (rc)
15481 case CURSOR_MOVEMENT_SUCCESS:
15482 used_current_matrix_p = 1;
15483 goto done;
15485 case CURSOR_MOVEMENT_MUST_SCROLL:
15486 goto try_to_scroll;
15488 default:
15489 abort ();
15492 /* If current starting point was originally the beginning of a line
15493 but no longer is, find a new starting point. */
15494 else if (!NILP (w->start_at_line_beg)
15495 && !(CHARPOS (startp) <= BEGV
15496 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15498 #if GLYPH_DEBUG
15499 debug_method_add (w, "recenter 1");
15500 #endif
15501 goto recenter;
15504 /* Try scrolling with try_window_id. Value is > 0 if update has
15505 been done, it is -1 if we know that the same window start will
15506 not work. It is 0 if unsuccessful for some other reason. */
15507 else if ((tem = try_window_id (w)) != 0)
15509 #if GLYPH_DEBUG
15510 debug_method_add (w, "try_window_id %d", tem);
15511 #endif
15513 if (fonts_changed_p)
15514 goto need_larger_matrices;
15515 if (tem > 0)
15516 goto done;
15518 /* Otherwise try_window_id has returned -1 which means that we
15519 don't want the alternative below this comment to execute. */
15521 else if (CHARPOS (startp) >= BEGV
15522 && CHARPOS (startp) <= ZV
15523 && PT >= CHARPOS (startp)
15524 && (CHARPOS (startp) < ZV
15525 /* Avoid starting at end of buffer. */
15526 || CHARPOS (startp) == BEGV
15527 || (XFASTINT (w->last_modified) >= MODIFF
15528 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
15530 int d1, d2, d3, d4, d5, d6;
15532 /* If first window line is a continuation line, and window start
15533 is inside the modified region, but the first change is before
15534 current window start, we must select a new window start.
15536 However, if this is the result of a down-mouse event (e.g. by
15537 extending the mouse-drag-overlay), we don't want to select a
15538 new window start, since that would change the position under
15539 the mouse, resulting in an unwanted mouse-movement rather
15540 than a simple mouse-click. */
15541 if (NILP (w->start_at_line_beg)
15542 && NILP (do_mouse_tracking)
15543 && CHARPOS (startp) > BEGV
15544 && CHARPOS (startp) > BEG + beg_unchanged
15545 && CHARPOS (startp) <= Z - end_unchanged
15546 /* Even if w->start_at_line_beg is nil, a new window may
15547 start at a line_beg, since that's how set_buffer_window
15548 sets it. So, we need to check the return value of
15549 compute_window_start_on_continuation_line. (See also
15550 bug#197). */
15551 && XMARKER (w->start)->buffer == current_buffer
15552 && compute_window_start_on_continuation_line (w)
15553 /* It doesn't make sense to force the window start like we
15554 do at label force_start if it is already known that point
15555 will not be visible in the resulting window, because
15556 doing so will move point from its correct position
15557 instead of scrolling the window to bring point into view.
15558 See bug#9324. */
15559 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15561 w->force_start = Qt;
15562 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15563 goto force_start;
15566 #if GLYPH_DEBUG
15567 debug_method_add (w, "same window start");
15568 #endif
15570 /* Try to redisplay starting at same place as before.
15571 If point has not moved off frame, accept the results. */
15572 if (!current_matrix_up_to_date_p
15573 /* Don't use try_window_reusing_current_matrix in this case
15574 because a window scroll function can have changed the
15575 buffer. */
15576 || !NILP (Vwindow_scroll_functions)
15577 || MINI_WINDOW_P (w)
15578 || !(used_current_matrix_p
15579 = try_window_reusing_current_matrix (w)))
15581 IF_DEBUG (debug_method_add (w, "1"));
15582 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15583 /* -1 means we need to scroll.
15584 0 means we need new matrices, but fonts_changed_p
15585 is set in that case, so we will detect it below. */
15586 goto try_to_scroll;
15589 if (fonts_changed_p)
15590 goto need_larger_matrices;
15592 if (w->cursor.vpos >= 0)
15594 if (!just_this_one_p
15595 || current_buffer->clip_changed
15596 || BEG_UNCHANGED < CHARPOS (startp))
15597 /* Forget any recorded base line for line number display. */
15598 w->base_line_number = Qnil;
15600 if (!cursor_row_fully_visible_p (w, 1, 0))
15602 clear_glyph_matrix (w->desired_matrix);
15603 last_line_misfit = 1;
15605 /* Drop through and scroll. */
15606 else
15607 goto done;
15609 else
15610 clear_glyph_matrix (w->desired_matrix);
15613 try_to_scroll:
15615 w->last_modified = make_number (0);
15616 w->last_overlay_modified = make_number (0);
15618 /* Redisplay the mode line. Select the buffer properly for that. */
15619 if (!update_mode_line)
15621 update_mode_line = 1;
15622 w->update_mode_line = Qt;
15625 /* Try to scroll by specified few lines. */
15626 if ((scroll_conservatively
15627 || emacs_scroll_step
15628 || temp_scroll_step
15629 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15630 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15631 && CHARPOS (startp) >= BEGV
15632 && CHARPOS (startp) <= ZV)
15634 /* The function returns -1 if new fonts were loaded, 1 if
15635 successful, 0 if not successful. */
15636 int ss = try_scrolling (window, just_this_one_p,
15637 scroll_conservatively,
15638 emacs_scroll_step,
15639 temp_scroll_step, last_line_misfit);
15640 switch (ss)
15642 case SCROLLING_SUCCESS:
15643 goto done;
15645 case SCROLLING_NEED_LARGER_MATRICES:
15646 goto need_larger_matrices;
15648 case SCROLLING_FAILED:
15649 break;
15651 default:
15652 abort ();
15656 /* Finally, just choose a place to start which positions point
15657 according to user preferences. */
15659 recenter:
15661 #if GLYPH_DEBUG
15662 debug_method_add (w, "recenter");
15663 #endif
15665 /* w->vscroll = 0; */
15667 /* Forget any previously recorded base line for line number display. */
15668 if (!buffer_unchanged_p)
15669 w->base_line_number = Qnil;
15671 /* Determine the window start relative to point. */
15672 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15673 it.current_y = it.last_visible_y;
15674 if (centering_position < 0)
15676 int margin =
15677 scroll_margin > 0
15678 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15679 : 0;
15680 EMACS_INT margin_pos = CHARPOS (startp);
15681 Lisp_Object aggressive;
15682 int scrolling_up;
15684 /* If there is a scroll margin at the top of the window, find
15685 its character position. */
15686 if (margin
15687 /* Cannot call start_display if startp is not in the
15688 accessible region of the buffer. This can happen when we
15689 have just switched to a different buffer and/or changed
15690 its restriction. In that case, startp is initialized to
15691 the character position 1 (BEGV) because we did not yet
15692 have chance to display the buffer even once. */
15693 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15695 struct it it1;
15696 void *it1data = NULL;
15698 SAVE_IT (it1, it, it1data);
15699 start_display (&it1, w, startp);
15700 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15701 margin_pos = IT_CHARPOS (it1);
15702 RESTORE_IT (&it, &it, it1data);
15704 scrolling_up = PT > margin_pos;
15705 aggressive =
15706 scrolling_up
15707 ? BVAR (current_buffer, scroll_up_aggressively)
15708 : BVAR (current_buffer, scroll_down_aggressively);
15710 if (!MINI_WINDOW_P (w)
15711 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15713 int pt_offset = 0;
15715 /* Setting scroll-conservatively overrides
15716 scroll-*-aggressively. */
15717 if (!scroll_conservatively && NUMBERP (aggressive))
15719 double float_amount = XFLOATINT (aggressive);
15721 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15722 if (pt_offset == 0 && float_amount > 0)
15723 pt_offset = 1;
15724 if (pt_offset && margin > 0)
15725 margin -= 1;
15727 /* Compute how much to move the window start backward from
15728 point so that point will be displayed where the user
15729 wants it. */
15730 if (scrolling_up)
15732 centering_position = it.last_visible_y;
15733 if (pt_offset)
15734 centering_position -= pt_offset;
15735 centering_position -=
15736 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15737 + WINDOW_HEADER_LINE_HEIGHT (w);
15738 /* Don't let point enter the scroll margin near top of
15739 the window. */
15740 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15741 centering_position = margin * FRAME_LINE_HEIGHT (f);
15743 else
15744 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15746 else
15747 /* Set the window start half the height of the window backward
15748 from point. */
15749 centering_position = window_box_height (w) / 2;
15751 move_it_vertically_backward (&it, centering_position);
15753 xassert (IT_CHARPOS (it) >= BEGV);
15755 /* The function move_it_vertically_backward may move over more
15756 than the specified y-distance. If it->w is small, e.g. a
15757 mini-buffer window, we may end up in front of the window's
15758 display area. Start displaying at the start of the line
15759 containing PT in this case. */
15760 if (it.current_y <= 0)
15762 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15763 move_it_vertically_backward (&it, 0);
15764 it.current_y = 0;
15767 it.current_x = it.hpos = 0;
15769 /* Set the window start position here explicitly, to avoid an
15770 infinite loop in case the functions in window-scroll-functions
15771 get errors. */
15772 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15774 /* Run scroll hooks. */
15775 startp = run_window_scroll_functions (window, it.current.pos);
15777 /* Redisplay the window. */
15778 if (!current_matrix_up_to_date_p
15779 || windows_or_buffers_changed
15780 || cursor_type_changed
15781 /* Don't use try_window_reusing_current_matrix in this case
15782 because it can have changed the buffer. */
15783 || !NILP (Vwindow_scroll_functions)
15784 || !just_this_one_p
15785 || MINI_WINDOW_P (w)
15786 || !(used_current_matrix_p
15787 = try_window_reusing_current_matrix (w)))
15788 try_window (window, startp, 0);
15790 /* If new fonts have been loaded (due to fontsets), give up. We
15791 have to start a new redisplay since we need to re-adjust glyph
15792 matrices. */
15793 if (fonts_changed_p)
15794 goto need_larger_matrices;
15796 /* If cursor did not appear assume that the middle of the window is
15797 in the first line of the window. Do it again with the next line.
15798 (Imagine a window of height 100, displaying two lines of height
15799 60. Moving back 50 from it->last_visible_y will end in the first
15800 line.) */
15801 if (w->cursor.vpos < 0)
15803 if (!NILP (w->window_end_valid)
15804 && PT >= Z - XFASTINT (w->window_end_pos))
15806 clear_glyph_matrix (w->desired_matrix);
15807 move_it_by_lines (&it, 1);
15808 try_window (window, it.current.pos, 0);
15810 else if (PT < IT_CHARPOS (it))
15812 clear_glyph_matrix (w->desired_matrix);
15813 move_it_by_lines (&it, -1);
15814 try_window (window, it.current.pos, 0);
15816 else
15818 /* Not much we can do about it. */
15822 /* Consider the following case: Window starts at BEGV, there is
15823 invisible, intangible text at BEGV, so that display starts at
15824 some point START > BEGV. It can happen that we are called with
15825 PT somewhere between BEGV and START. Try to handle that case. */
15826 if (w->cursor.vpos < 0)
15828 struct glyph_row *row = w->current_matrix->rows;
15829 if (row->mode_line_p)
15830 ++row;
15831 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15834 if (!cursor_row_fully_visible_p (w, 0, 0))
15836 /* If vscroll is enabled, disable it and try again. */
15837 if (w->vscroll)
15839 w->vscroll = 0;
15840 clear_glyph_matrix (w->desired_matrix);
15841 goto recenter;
15844 /* Users who set scroll-conservatively to a large number want
15845 point just above/below the scroll margin. If we ended up
15846 with point's row partially visible, move the window start to
15847 make that row fully visible and out of the margin. */
15848 if (scroll_conservatively > SCROLL_LIMIT)
15850 int margin =
15851 scroll_margin > 0
15852 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15853 : 0;
15854 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
15856 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
15857 clear_glyph_matrix (w->desired_matrix);
15858 if (1 == try_window (window, it.current.pos,
15859 TRY_WINDOW_CHECK_MARGINS))
15860 goto done;
15863 /* If centering point failed to make the whole line visible,
15864 put point at the top instead. That has to make the whole line
15865 visible, if it can be done. */
15866 if (centering_position == 0)
15867 goto done;
15869 clear_glyph_matrix (w->desired_matrix);
15870 centering_position = 0;
15871 goto recenter;
15874 done:
15876 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15877 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
15878 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
15879 ? Qt : Qnil);
15881 /* Display the mode line, if we must. */
15882 if ((update_mode_line
15883 /* If window not full width, must redo its mode line
15884 if (a) the window to its side is being redone and
15885 (b) we do a frame-based redisplay. This is a consequence
15886 of how inverted lines are drawn in frame-based redisplay. */
15887 || (!just_this_one_p
15888 && !FRAME_WINDOW_P (f)
15889 && !WINDOW_FULL_WIDTH_P (w))
15890 /* Line number to display. */
15891 || INTEGERP (w->base_line_pos)
15892 /* Column number is displayed and different from the one displayed. */
15893 || (!NILP (w->column_number_displayed)
15894 && (XFASTINT (w->column_number_displayed) != current_column ())))
15895 /* This means that the window has a mode line. */
15896 && (WINDOW_WANTS_MODELINE_P (w)
15897 || WINDOW_WANTS_HEADER_LINE_P (w)))
15899 display_mode_lines (w);
15901 /* If mode line height has changed, arrange for a thorough
15902 immediate redisplay using the correct mode line height. */
15903 if (WINDOW_WANTS_MODELINE_P (w)
15904 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
15906 fonts_changed_p = 1;
15907 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
15908 = DESIRED_MODE_LINE_HEIGHT (w);
15911 /* If header line height has changed, arrange for a thorough
15912 immediate redisplay using the correct header line height. */
15913 if (WINDOW_WANTS_HEADER_LINE_P (w)
15914 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
15916 fonts_changed_p = 1;
15917 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
15918 = DESIRED_HEADER_LINE_HEIGHT (w);
15921 if (fonts_changed_p)
15922 goto need_larger_matrices;
15925 if (!line_number_displayed
15926 && !BUFFERP (w->base_line_pos))
15928 w->base_line_pos = Qnil;
15929 w->base_line_number = Qnil;
15932 finish_menu_bars:
15934 /* When we reach a frame's selected window, redo the frame's menu bar. */
15935 if (update_mode_line
15936 && EQ (FRAME_SELECTED_WINDOW (f), window))
15938 int redisplay_menu_p = 0;
15940 if (FRAME_WINDOW_P (f))
15942 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
15943 || defined (HAVE_NS) || defined (USE_GTK)
15944 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
15945 #else
15946 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15947 #endif
15949 else
15950 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15952 if (redisplay_menu_p)
15953 display_menu_bar (w);
15955 #ifdef HAVE_WINDOW_SYSTEM
15956 if (FRAME_WINDOW_P (f))
15958 #if defined (USE_GTK) || defined (HAVE_NS)
15959 if (FRAME_EXTERNAL_TOOL_BAR (f))
15960 redisplay_tool_bar (f);
15961 #else
15962 if (WINDOWP (f->tool_bar_window)
15963 && (FRAME_TOOL_BAR_LINES (f) > 0
15964 || !NILP (Vauto_resize_tool_bars))
15965 && redisplay_tool_bar (f))
15966 ignore_mouse_drag_p = 1;
15967 #endif
15969 #endif
15972 #ifdef HAVE_WINDOW_SYSTEM
15973 if (FRAME_WINDOW_P (f)
15974 && update_window_fringes (w, (just_this_one_p
15975 || (!used_current_matrix_p && !overlay_arrow_seen)
15976 || w->pseudo_window_p)))
15978 update_begin (f);
15979 BLOCK_INPUT;
15980 if (draw_window_fringes (w, 1))
15981 x_draw_vertical_border (w);
15982 UNBLOCK_INPUT;
15983 update_end (f);
15985 #endif /* HAVE_WINDOW_SYSTEM */
15987 /* We go to this label, with fonts_changed_p nonzero,
15988 if it is necessary to try again using larger glyph matrices.
15989 We have to redeem the scroll bar even in this case,
15990 because the loop in redisplay_internal expects that. */
15991 need_larger_matrices:
15993 finish_scroll_bars:
15995 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
15997 /* Set the thumb's position and size. */
15998 set_vertical_scroll_bar (w);
16000 /* Note that we actually used the scroll bar attached to this
16001 window, so it shouldn't be deleted at the end of redisplay. */
16002 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16003 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16006 /* Restore current_buffer and value of point in it. The window
16007 update may have changed the buffer, so first make sure `opoint'
16008 is still valid (Bug#6177). */
16009 if (CHARPOS (opoint) < BEGV)
16010 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16011 else if (CHARPOS (opoint) > ZV)
16012 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16013 else
16014 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16016 set_buffer_internal_1 (old);
16017 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16018 shorter. This can be caused by log truncation in *Messages*. */
16019 if (CHARPOS (lpoint) <= ZV)
16020 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16022 unbind_to (count, Qnil);
16026 /* Build the complete desired matrix of WINDOW with a window start
16027 buffer position POS.
16029 Value is 1 if successful. It is zero if fonts were loaded during
16030 redisplay which makes re-adjusting glyph matrices necessary, and -1
16031 if point would appear in the scroll margins.
16032 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16033 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16034 set in FLAGS.) */
16037 try_window (Lisp_Object window, struct text_pos pos, int flags)
16039 struct window *w = XWINDOW (window);
16040 struct it it;
16041 struct glyph_row *last_text_row = NULL;
16042 struct frame *f = XFRAME (w->frame);
16044 /* Make POS the new window start. */
16045 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16047 /* Mark cursor position as unknown. No overlay arrow seen. */
16048 w->cursor.vpos = -1;
16049 overlay_arrow_seen = 0;
16051 /* Initialize iterator and info to start at POS. */
16052 start_display (&it, w, pos);
16054 /* Display all lines of W. */
16055 while (it.current_y < it.last_visible_y)
16057 if (display_line (&it))
16058 last_text_row = it.glyph_row - 1;
16059 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16060 return 0;
16063 /* Don't let the cursor end in the scroll margins. */
16064 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16065 && !MINI_WINDOW_P (w))
16067 int this_scroll_margin;
16069 if (scroll_margin > 0)
16071 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16072 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16074 else
16075 this_scroll_margin = 0;
16077 if ((w->cursor.y >= 0 /* not vscrolled */
16078 && w->cursor.y < this_scroll_margin
16079 && CHARPOS (pos) > BEGV
16080 && IT_CHARPOS (it) < ZV)
16081 /* rms: considering make_cursor_line_fully_visible_p here
16082 seems to give wrong results. We don't want to recenter
16083 when the last line is partly visible, we want to allow
16084 that case to be handled in the usual way. */
16085 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16087 w->cursor.vpos = -1;
16088 clear_glyph_matrix (w->desired_matrix);
16089 return -1;
16093 /* If bottom moved off end of frame, change mode line percentage. */
16094 if (XFASTINT (w->window_end_pos) <= 0
16095 && Z != IT_CHARPOS (it))
16096 w->update_mode_line = Qt;
16098 /* Set window_end_pos to the offset of the last character displayed
16099 on the window from the end of current_buffer. Set
16100 window_end_vpos to its row number. */
16101 if (last_text_row)
16103 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16104 w->window_end_bytepos
16105 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16106 w->window_end_pos
16107 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16108 w->window_end_vpos
16109 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16110 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
16111 ->displays_text_p);
16113 else
16115 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16116 w->window_end_pos = make_number (Z - ZV);
16117 w->window_end_vpos = make_number (0);
16120 /* But that is not valid info until redisplay finishes. */
16121 w->window_end_valid = Qnil;
16122 return 1;
16127 /************************************************************************
16128 Window redisplay reusing current matrix when buffer has not changed
16129 ************************************************************************/
16131 /* Try redisplay of window W showing an unchanged buffer with a
16132 different window start than the last time it was displayed by
16133 reusing its current matrix. Value is non-zero if successful.
16134 W->start is the new window start. */
16136 static int
16137 try_window_reusing_current_matrix (struct window *w)
16139 struct frame *f = XFRAME (w->frame);
16140 struct glyph_row *bottom_row;
16141 struct it it;
16142 struct run run;
16143 struct text_pos start, new_start;
16144 int nrows_scrolled, i;
16145 struct glyph_row *last_text_row;
16146 struct glyph_row *last_reused_text_row;
16147 struct glyph_row *start_row;
16148 int start_vpos, min_y, max_y;
16150 #if GLYPH_DEBUG
16151 if (inhibit_try_window_reusing)
16152 return 0;
16153 #endif
16155 if (/* This function doesn't handle terminal frames. */
16156 !FRAME_WINDOW_P (f)
16157 /* Don't try to reuse the display if windows have been split
16158 or such. */
16159 || windows_or_buffers_changed
16160 || cursor_type_changed)
16161 return 0;
16163 /* Can't do this if region may have changed. */
16164 if ((!NILP (Vtransient_mark_mode)
16165 && !NILP (BVAR (current_buffer, mark_active)))
16166 || !NILP (w->region_showing)
16167 || !NILP (Vshow_trailing_whitespace))
16168 return 0;
16170 /* If top-line visibility has changed, give up. */
16171 if (WINDOW_WANTS_HEADER_LINE_P (w)
16172 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16173 return 0;
16175 /* Give up if old or new display is scrolled vertically. We could
16176 make this function handle this, but right now it doesn't. */
16177 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16178 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16179 return 0;
16181 /* The variable new_start now holds the new window start. The old
16182 start `start' can be determined from the current matrix. */
16183 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16184 start = start_row->minpos;
16185 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16187 /* Clear the desired matrix for the display below. */
16188 clear_glyph_matrix (w->desired_matrix);
16190 if (CHARPOS (new_start) <= CHARPOS (start))
16192 /* Don't use this method if the display starts with an ellipsis
16193 displayed for invisible text. It's not easy to handle that case
16194 below, and it's certainly not worth the effort since this is
16195 not a frequent case. */
16196 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16197 return 0;
16199 IF_DEBUG (debug_method_add (w, "twu1"));
16201 /* Display up to a row that can be reused. The variable
16202 last_text_row is set to the last row displayed that displays
16203 text. Note that it.vpos == 0 if or if not there is a
16204 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16205 start_display (&it, w, new_start);
16206 w->cursor.vpos = -1;
16207 last_text_row = last_reused_text_row = NULL;
16209 while (it.current_y < it.last_visible_y
16210 && !fonts_changed_p)
16212 /* If we have reached into the characters in the START row,
16213 that means the line boundaries have changed. So we
16214 can't start copying with the row START. Maybe it will
16215 work to start copying with the following row. */
16216 while (IT_CHARPOS (it) > CHARPOS (start))
16218 /* Advance to the next row as the "start". */
16219 start_row++;
16220 start = start_row->minpos;
16221 /* If there are no more rows to try, or just one, give up. */
16222 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16223 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16224 || CHARPOS (start) == ZV)
16226 clear_glyph_matrix (w->desired_matrix);
16227 return 0;
16230 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16232 /* If we have reached alignment, we can copy the rest of the
16233 rows. */
16234 if (IT_CHARPOS (it) == CHARPOS (start)
16235 /* Don't accept "alignment" inside a display vector,
16236 since start_row could have started in the middle of
16237 that same display vector (thus their character
16238 positions match), and we have no way of telling if
16239 that is the case. */
16240 && it.current.dpvec_index < 0)
16241 break;
16243 if (display_line (&it))
16244 last_text_row = it.glyph_row - 1;
16248 /* A value of current_y < last_visible_y means that we stopped
16249 at the previous window start, which in turn means that we
16250 have at least one reusable row. */
16251 if (it.current_y < it.last_visible_y)
16253 struct glyph_row *row;
16255 /* IT.vpos always starts from 0; it counts text lines. */
16256 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16258 /* Find PT if not already found in the lines displayed. */
16259 if (w->cursor.vpos < 0)
16261 int dy = it.current_y - start_row->y;
16263 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16264 row = row_containing_pos (w, PT, row, NULL, dy);
16265 if (row)
16266 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16267 dy, nrows_scrolled);
16268 else
16270 clear_glyph_matrix (w->desired_matrix);
16271 return 0;
16275 /* Scroll the display. Do it before the current matrix is
16276 changed. The problem here is that update has not yet
16277 run, i.e. part of the current matrix is not up to date.
16278 scroll_run_hook will clear the cursor, and use the
16279 current matrix to get the height of the row the cursor is
16280 in. */
16281 run.current_y = start_row->y;
16282 run.desired_y = it.current_y;
16283 run.height = it.last_visible_y - it.current_y;
16285 if (run.height > 0 && run.current_y != run.desired_y)
16287 update_begin (f);
16288 FRAME_RIF (f)->update_window_begin_hook (w);
16289 FRAME_RIF (f)->clear_window_mouse_face (w);
16290 FRAME_RIF (f)->scroll_run_hook (w, &run);
16291 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16292 update_end (f);
16295 /* Shift current matrix down by nrows_scrolled lines. */
16296 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16297 rotate_matrix (w->current_matrix,
16298 start_vpos,
16299 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16300 nrows_scrolled);
16302 /* Disable lines that must be updated. */
16303 for (i = 0; i < nrows_scrolled; ++i)
16304 (start_row + i)->enabled_p = 0;
16306 /* Re-compute Y positions. */
16307 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16308 max_y = it.last_visible_y;
16309 for (row = start_row + nrows_scrolled;
16310 row < bottom_row;
16311 ++row)
16313 row->y = it.current_y;
16314 row->visible_height = row->height;
16316 if (row->y < min_y)
16317 row->visible_height -= min_y - row->y;
16318 if (row->y + row->height > max_y)
16319 row->visible_height -= row->y + row->height - max_y;
16320 if (row->fringe_bitmap_periodic_p)
16321 row->redraw_fringe_bitmaps_p = 1;
16323 it.current_y += row->height;
16325 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16326 last_reused_text_row = row;
16327 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16328 break;
16331 /* Disable lines in the current matrix which are now
16332 below the window. */
16333 for (++row; row < bottom_row; ++row)
16334 row->enabled_p = row->mode_line_p = 0;
16337 /* Update window_end_pos etc.; last_reused_text_row is the last
16338 reused row from the current matrix containing text, if any.
16339 The value of last_text_row is the last displayed line
16340 containing text. */
16341 if (last_reused_text_row)
16343 w->window_end_bytepos
16344 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16345 w->window_end_pos
16346 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
16347 w->window_end_vpos
16348 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16349 w->current_matrix));
16351 else if (last_text_row)
16353 w->window_end_bytepos
16354 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16355 w->window_end_pos
16356 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16357 w->window_end_vpos
16358 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16360 else
16362 /* This window must be completely empty. */
16363 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16364 w->window_end_pos = make_number (Z - ZV);
16365 w->window_end_vpos = make_number (0);
16367 w->window_end_valid = Qnil;
16369 /* Update hint: don't try scrolling again in update_window. */
16370 w->desired_matrix->no_scrolling_p = 1;
16372 #if GLYPH_DEBUG
16373 debug_method_add (w, "try_window_reusing_current_matrix 1");
16374 #endif
16375 return 1;
16377 else if (CHARPOS (new_start) > CHARPOS (start))
16379 struct glyph_row *pt_row, *row;
16380 struct glyph_row *first_reusable_row;
16381 struct glyph_row *first_row_to_display;
16382 int dy;
16383 int yb = window_text_bottom_y (w);
16385 /* Find the row starting at new_start, if there is one. Don't
16386 reuse a partially visible line at the end. */
16387 first_reusable_row = start_row;
16388 while (first_reusable_row->enabled_p
16389 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16390 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16391 < CHARPOS (new_start)))
16392 ++first_reusable_row;
16394 /* Give up if there is no row to reuse. */
16395 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16396 || !first_reusable_row->enabled_p
16397 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16398 != CHARPOS (new_start)))
16399 return 0;
16401 /* We can reuse fully visible rows beginning with
16402 first_reusable_row to the end of the window. Set
16403 first_row_to_display to the first row that cannot be reused.
16404 Set pt_row to the row containing point, if there is any. */
16405 pt_row = NULL;
16406 for (first_row_to_display = first_reusable_row;
16407 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16408 ++first_row_to_display)
16410 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16411 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16412 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16413 && first_row_to_display->ends_at_zv_p
16414 && pt_row == NULL)))
16415 pt_row = first_row_to_display;
16418 /* Start displaying at the start of first_row_to_display. */
16419 xassert (first_row_to_display->y < yb);
16420 init_to_row_start (&it, w, first_row_to_display);
16422 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16423 - start_vpos);
16424 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16425 - nrows_scrolled);
16426 it.current_y = (first_row_to_display->y - first_reusable_row->y
16427 + WINDOW_HEADER_LINE_HEIGHT (w));
16429 /* Display lines beginning with first_row_to_display in the
16430 desired matrix. Set last_text_row to the last row displayed
16431 that displays text. */
16432 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16433 if (pt_row == NULL)
16434 w->cursor.vpos = -1;
16435 last_text_row = NULL;
16436 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16437 if (display_line (&it))
16438 last_text_row = it.glyph_row - 1;
16440 /* If point is in a reused row, adjust y and vpos of the cursor
16441 position. */
16442 if (pt_row)
16444 w->cursor.vpos -= nrows_scrolled;
16445 w->cursor.y -= first_reusable_row->y - start_row->y;
16448 /* Give up if point isn't in a row displayed or reused. (This
16449 also handles the case where w->cursor.vpos < nrows_scrolled
16450 after the calls to display_line, which can happen with scroll
16451 margins. See bug#1295.) */
16452 if (w->cursor.vpos < 0)
16454 clear_glyph_matrix (w->desired_matrix);
16455 return 0;
16458 /* Scroll the display. */
16459 run.current_y = first_reusable_row->y;
16460 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16461 run.height = it.last_visible_y - run.current_y;
16462 dy = run.current_y - run.desired_y;
16464 if (run.height)
16466 update_begin (f);
16467 FRAME_RIF (f)->update_window_begin_hook (w);
16468 FRAME_RIF (f)->clear_window_mouse_face (w);
16469 FRAME_RIF (f)->scroll_run_hook (w, &run);
16470 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16471 update_end (f);
16474 /* Adjust Y positions of reused rows. */
16475 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16476 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16477 max_y = it.last_visible_y;
16478 for (row = first_reusable_row; row < first_row_to_display; ++row)
16480 row->y -= dy;
16481 row->visible_height = row->height;
16482 if (row->y < min_y)
16483 row->visible_height -= min_y - row->y;
16484 if (row->y + row->height > max_y)
16485 row->visible_height -= row->y + row->height - max_y;
16486 if (row->fringe_bitmap_periodic_p)
16487 row->redraw_fringe_bitmaps_p = 1;
16490 /* Scroll the current matrix. */
16491 xassert (nrows_scrolled > 0);
16492 rotate_matrix (w->current_matrix,
16493 start_vpos,
16494 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16495 -nrows_scrolled);
16497 /* Disable rows not reused. */
16498 for (row -= nrows_scrolled; row < bottom_row; ++row)
16499 row->enabled_p = 0;
16501 /* Point may have moved to a different line, so we cannot assume that
16502 the previous cursor position is valid; locate the correct row. */
16503 if (pt_row)
16505 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16506 row < bottom_row
16507 && PT >= MATRIX_ROW_END_CHARPOS (row)
16508 && !row->ends_at_zv_p;
16509 row++)
16511 w->cursor.vpos++;
16512 w->cursor.y = row->y;
16514 if (row < bottom_row)
16516 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16517 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16519 /* Can't use this optimization with bidi-reordered glyph
16520 rows, unless cursor is already at point. */
16521 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16523 if (!(w->cursor.hpos >= 0
16524 && w->cursor.hpos < row->used[TEXT_AREA]
16525 && BUFFERP (glyph->object)
16526 && glyph->charpos == PT))
16527 return 0;
16529 else
16530 for (; glyph < end
16531 && (!BUFFERP (glyph->object)
16532 || glyph->charpos < PT);
16533 glyph++)
16535 w->cursor.hpos++;
16536 w->cursor.x += glyph->pixel_width;
16541 /* Adjust window end. A null value of last_text_row means that
16542 the window end is in reused rows which in turn means that
16543 only its vpos can have changed. */
16544 if (last_text_row)
16546 w->window_end_bytepos
16547 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16548 w->window_end_pos
16549 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16550 w->window_end_vpos
16551 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16553 else
16555 w->window_end_vpos
16556 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16559 w->window_end_valid = Qnil;
16560 w->desired_matrix->no_scrolling_p = 1;
16562 #if GLYPH_DEBUG
16563 debug_method_add (w, "try_window_reusing_current_matrix 2");
16564 #endif
16565 return 1;
16568 return 0;
16573 /************************************************************************
16574 Window redisplay reusing current matrix when buffer has changed
16575 ************************************************************************/
16577 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16578 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16579 EMACS_INT *, EMACS_INT *);
16580 static struct glyph_row *
16581 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16582 struct glyph_row *);
16585 /* Return the last row in MATRIX displaying text. If row START is
16586 non-null, start searching with that row. IT gives the dimensions
16587 of the display. Value is null if matrix is empty; otherwise it is
16588 a pointer to the row found. */
16590 static struct glyph_row *
16591 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16592 struct glyph_row *start)
16594 struct glyph_row *row, *row_found;
16596 /* Set row_found to the last row in IT->w's current matrix
16597 displaying text. The loop looks funny but think of partially
16598 visible lines. */
16599 row_found = NULL;
16600 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16601 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16603 xassert (row->enabled_p);
16604 row_found = row;
16605 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16606 break;
16607 ++row;
16610 return row_found;
16614 /* Return the last row in the current matrix of W that is not affected
16615 by changes at the start of current_buffer that occurred since W's
16616 current matrix was built. Value is null if no such row exists.
16618 BEG_UNCHANGED us the number of characters unchanged at the start of
16619 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16620 first changed character in current_buffer. Characters at positions <
16621 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16622 when the current matrix was built. */
16624 static struct glyph_row *
16625 find_last_unchanged_at_beg_row (struct window *w)
16627 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
16628 struct glyph_row *row;
16629 struct glyph_row *row_found = NULL;
16630 int yb = window_text_bottom_y (w);
16632 /* Find the last row displaying unchanged text. */
16633 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16634 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16635 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16636 ++row)
16638 if (/* If row ends before first_changed_pos, it is unchanged,
16639 except in some case. */
16640 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16641 /* When row ends in ZV and we write at ZV it is not
16642 unchanged. */
16643 && !row->ends_at_zv_p
16644 /* When first_changed_pos is the end of a continued line,
16645 row is not unchanged because it may be no longer
16646 continued. */
16647 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16648 && (row->continued_p
16649 || row->exact_window_width_line_p))
16650 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16651 needs to be recomputed, so don't consider this row as
16652 unchanged. This happens when the last line was
16653 bidi-reordered and was killed immediately before this
16654 redisplay cycle. In that case, ROW->end stores the
16655 buffer position of the first visual-order character of
16656 the killed text, which is now beyond ZV. */
16657 && CHARPOS (row->end.pos) <= ZV)
16658 row_found = row;
16660 /* Stop if last visible row. */
16661 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16662 break;
16665 return row_found;
16669 /* Find the first glyph row in the current matrix of W that is not
16670 affected by changes at the end of current_buffer since the
16671 time W's current matrix was built.
16673 Return in *DELTA the number of chars by which buffer positions in
16674 unchanged text at the end of current_buffer must be adjusted.
16676 Return in *DELTA_BYTES the corresponding number of bytes.
16678 Value is null if no such row exists, i.e. all rows are affected by
16679 changes. */
16681 static struct glyph_row *
16682 find_first_unchanged_at_end_row (struct window *w,
16683 EMACS_INT *delta, EMACS_INT *delta_bytes)
16685 struct glyph_row *row;
16686 struct glyph_row *row_found = NULL;
16688 *delta = *delta_bytes = 0;
16690 /* Display must not have been paused, otherwise the current matrix
16691 is not up to date. */
16692 eassert (!NILP (w->window_end_valid));
16694 /* A value of window_end_pos >= END_UNCHANGED means that the window
16695 end is in the range of changed text. If so, there is no
16696 unchanged row at the end of W's current matrix. */
16697 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16698 return NULL;
16700 /* Set row to the last row in W's current matrix displaying text. */
16701 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16703 /* If matrix is entirely empty, no unchanged row exists. */
16704 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16706 /* The value of row is the last glyph row in the matrix having a
16707 meaningful buffer position in it. The end position of row
16708 corresponds to window_end_pos. This allows us to translate
16709 buffer positions in the current matrix to current buffer
16710 positions for characters not in changed text. */
16711 EMACS_INT Z_old =
16712 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16713 EMACS_INT Z_BYTE_old =
16714 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16715 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
16716 struct glyph_row *first_text_row
16717 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16719 *delta = Z - Z_old;
16720 *delta_bytes = Z_BYTE - Z_BYTE_old;
16722 /* Set last_unchanged_pos to the buffer position of the last
16723 character in the buffer that has not been changed. Z is the
16724 index + 1 of the last character in current_buffer, i.e. by
16725 subtracting END_UNCHANGED we get the index of the last
16726 unchanged character, and we have to add BEG to get its buffer
16727 position. */
16728 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16729 last_unchanged_pos_old = last_unchanged_pos - *delta;
16731 /* Search backward from ROW for a row displaying a line that
16732 starts at a minimum position >= last_unchanged_pos_old. */
16733 for (; row > first_text_row; --row)
16735 /* This used to abort, but it can happen.
16736 It is ok to just stop the search instead here. KFS. */
16737 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16738 break;
16740 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16741 row_found = row;
16745 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16747 return row_found;
16751 /* Make sure that glyph rows in the current matrix of window W
16752 reference the same glyph memory as corresponding rows in the
16753 frame's frame matrix. This function is called after scrolling W's
16754 current matrix on a terminal frame in try_window_id and
16755 try_window_reusing_current_matrix. */
16757 static void
16758 sync_frame_with_window_matrix_rows (struct window *w)
16760 struct frame *f = XFRAME (w->frame);
16761 struct glyph_row *window_row, *window_row_end, *frame_row;
16763 /* Preconditions: W must be a leaf window and full-width. Its frame
16764 must have a frame matrix. */
16765 xassert (NILP (w->hchild) && NILP (w->vchild));
16766 xassert (WINDOW_FULL_WIDTH_P (w));
16767 xassert (!FRAME_WINDOW_P (f));
16769 /* If W is a full-width window, glyph pointers in W's current matrix
16770 have, by definition, to be the same as glyph pointers in the
16771 corresponding frame matrix. Note that frame matrices have no
16772 marginal areas (see build_frame_matrix). */
16773 window_row = w->current_matrix->rows;
16774 window_row_end = window_row + w->current_matrix->nrows;
16775 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16776 while (window_row < window_row_end)
16778 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16779 struct glyph *end = window_row->glyphs[LAST_AREA];
16781 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16782 frame_row->glyphs[TEXT_AREA] = start;
16783 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16784 frame_row->glyphs[LAST_AREA] = end;
16786 /* Disable frame rows whose corresponding window rows have
16787 been disabled in try_window_id. */
16788 if (!window_row->enabled_p)
16789 frame_row->enabled_p = 0;
16791 ++window_row, ++frame_row;
16796 /* Find the glyph row in window W containing CHARPOS. Consider all
16797 rows between START and END (not inclusive). END null means search
16798 all rows to the end of the display area of W. Value is the row
16799 containing CHARPOS or null. */
16801 struct glyph_row *
16802 row_containing_pos (struct window *w, EMACS_INT charpos,
16803 struct glyph_row *start, struct glyph_row *end, int dy)
16805 struct glyph_row *row = start;
16806 struct glyph_row *best_row = NULL;
16807 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16808 int last_y;
16810 /* If we happen to start on a header-line, skip that. */
16811 if (row->mode_line_p)
16812 ++row;
16814 if ((end && row >= end) || !row->enabled_p)
16815 return NULL;
16817 last_y = window_text_bottom_y (w) - dy;
16819 while (1)
16821 /* Give up if we have gone too far. */
16822 if (end && row >= end)
16823 return NULL;
16824 /* This formerly returned if they were equal.
16825 I think that both quantities are of a "last plus one" type;
16826 if so, when they are equal, the row is within the screen. -- rms. */
16827 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16828 return NULL;
16830 /* If it is in this row, return this row. */
16831 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16832 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16833 /* The end position of a row equals the start
16834 position of the next row. If CHARPOS is there, we
16835 would rather display it in the next line, except
16836 when this line ends in ZV. */
16837 && !row->ends_at_zv_p
16838 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16839 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16841 struct glyph *g;
16843 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16844 || (!best_row && !row->continued_p))
16845 return row;
16846 /* In bidi-reordered rows, there could be several rows
16847 occluding point, all of them belonging to the same
16848 continued line. We need to find the row which fits
16849 CHARPOS the best. */
16850 for (g = row->glyphs[TEXT_AREA];
16851 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16852 g++)
16854 if (!STRINGP (g->object))
16856 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16858 mindif = eabs (g->charpos - charpos);
16859 best_row = row;
16860 /* Exact match always wins. */
16861 if (mindif == 0)
16862 return best_row;
16867 else if (best_row && !row->continued_p)
16868 return best_row;
16869 ++row;
16874 /* Try to redisplay window W by reusing its existing display. W's
16875 current matrix must be up to date when this function is called,
16876 i.e. window_end_valid must not be nil.
16878 Value is
16880 1 if display has been updated
16881 0 if otherwise unsuccessful
16882 -1 if redisplay with same window start is known not to succeed
16884 The following steps are performed:
16886 1. Find the last row in the current matrix of W that is not
16887 affected by changes at the start of current_buffer. If no such row
16888 is found, give up.
16890 2. Find the first row in W's current matrix that is not affected by
16891 changes at the end of current_buffer. Maybe there is no such row.
16893 3. Display lines beginning with the row + 1 found in step 1 to the
16894 row found in step 2 or, if step 2 didn't find a row, to the end of
16895 the window.
16897 4. If cursor is not known to appear on the window, give up.
16899 5. If display stopped at the row found in step 2, scroll the
16900 display and current matrix as needed.
16902 6. Maybe display some lines at the end of W, if we must. This can
16903 happen under various circumstances, like a partially visible line
16904 becoming fully visible, or because newly displayed lines are displayed
16905 in smaller font sizes.
16907 7. Update W's window end information. */
16909 static int
16910 try_window_id (struct window *w)
16912 struct frame *f = XFRAME (w->frame);
16913 struct glyph_matrix *current_matrix = w->current_matrix;
16914 struct glyph_matrix *desired_matrix = w->desired_matrix;
16915 struct glyph_row *last_unchanged_at_beg_row;
16916 struct glyph_row *first_unchanged_at_end_row;
16917 struct glyph_row *row;
16918 struct glyph_row *bottom_row;
16919 int bottom_vpos;
16920 struct it it;
16921 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
16922 int dvpos, dy;
16923 struct text_pos start_pos;
16924 struct run run;
16925 int first_unchanged_at_end_vpos = 0;
16926 struct glyph_row *last_text_row, *last_text_row_at_end;
16927 struct text_pos start;
16928 EMACS_INT first_changed_charpos, last_changed_charpos;
16930 #if GLYPH_DEBUG
16931 if (inhibit_try_window_id)
16932 return 0;
16933 #endif
16935 /* This is handy for debugging. */
16936 #if 0
16937 #define GIVE_UP(X) \
16938 do { \
16939 fprintf (stderr, "try_window_id give up %d\n", (X)); \
16940 return 0; \
16941 } while (0)
16942 #else
16943 #define GIVE_UP(X) return 0
16944 #endif
16946 SET_TEXT_POS_FROM_MARKER (start, w->start);
16948 /* Don't use this for mini-windows because these can show
16949 messages and mini-buffers, and we don't handle that here. */
16950 if (MINI_WINDOW_P (w))
16951 GIVE_UP (1);
16953 /* This flag is used to prevent redisplay optimizations. */
16954 if (windows_or_buffers_changed || cursor_type_changed)
16955 GIVE_UP (2);
16957 /* Verify that narrowing has not changed.
16958 Also verify that we were not told to prevent redisplay optimizations.
16959 It would be nice to further
16960 reduce the number of cases where this prevents try_window_id. */
16961 if (current_buffer->clip_changed
16962 || current_buffer->prevent_redisplay_optimizations_p)
16963 GIVE_UP (3);
16965 /* Window must either use window-based redisplay or be full width. */
16966 if (!FRAME_WINDOW_P (f)
16967 && (!FRAME_LINE_INS_DEL_OK (f)
16968 || !WINDOW_FULL_WIDTH_P (w)))
16969 GIVE_UP (4);
16971 /* Give up if point is known NOT to appear in W. */
16972 if (PT < CHARPOS (start))
16973 GIVE_UP (5);
16975 /* Another way to prevent redisplay optimizations. */
16976 if (XFASTINT (w->last_modified) == 0)
16977 GIVE_UP (6);
16979 /* Verify that window is not hscrolled. */
16980 if (XFASTINT (w->hscroll) != 0)
16981 GIVE_UP (7);
16983 /* Verify that display wasn't paused. */
16984 if (NILP (w->window_end_valid))
16985 GIVE_UP (8);
16987 /* Can't use this if highlighting a region because a cursor movement
16988 will do more than just set the cursor. */
16989 if (!NILP (Vtransient_mark_mode)
16990 && !NILP (BVAR (current_buffer, mark_active)))
16991 GIVE_UP (9);
16993 /* Likewise if highlighting trailing whitespace. */
16994 if (!NILP (Vshow_trailing_whitespace))
16995 GIVE_UP (11);
16997 /* Likewise if showing a region. */
16998 if (!NILP (w->region_showing))
16999 GIVE_UP (10);
17001 /* Can't use this if overlay arrow position and/or string have
17002 changed. */
17003 if (overlay_arrows_changed_p ())
17004 GIVE_UP (12);
17006 /* When word-wrap is on, adding a space to the first word of a
17007 wrapped line can change the wrap position, altering the line
17008 above it. It might be worthwhile to handle this more
17009 intelligently, but for now just redisplay from scratch. */
17010 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17011 GIVE_UP (21);
17013 /* Under bidi reordering, adding or deleting a character in the
17014 beginning of a paragraph, before the first strong directional
17015 character, can change the base direction of the paragraph (unless
17016 the buffer specifies a fixed paragraph direction), which will
17017 require to redisplay the whole paragraph. It might be worthwhile
17018 to find the paragraph limits and widen the range of redisplayed
17019 lines to that, but for now just give up this optimization and
17020 redisplay from scratch. */
17021 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17022 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17023 GIVE_UP (22);
17025 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17026 only if buffer has really changed. The reason is that the gap is
17027 initially at Z for freshly visited files. The code below would
17028 set end_unchanged to 0 in that case. */
17029 if (MODIFF > SAVE_MODIFF
17030 /* This seems to happen sometimes after saving a buffer. */
17031 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17033 if (GPT - BEG < BEG_UNCHANGED)
17034 BEG_UNCHANGED = GPT - BEG;
17035 if (Z - GPT < END_UNCHANGED)
17036 END_UNCHANGED = Z - GPT;
17039 /* The position of the first and last character that has been changed. */
17040 first_changed_charpos = BEG + BEG_UNCHANGED;
17041 last_changed_charpos = Z - END_UNCHANGED;
17043 /* If window starts after a line end, and the last change is in
17044 front of that newline, then changes don't affect the display.
17045 This case happens with stealth-fontification. Note that although
17046 the display is unchanged, glyph positions in the matrix have to
17047 be adjusted, of course. */
17048 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17049 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17050 && ((last_changed_charpos < CHARPOS (start)
17051 && CHARPOS (start) == BEGV)
17052 || (last_changed_charpos < CHARPOS (start) - 1
17053 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17055 EMACS_INT Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17056 struct glyph_row *r0;
17058 /* Compute how many chars/bytes have been added to or removed
17059 from the buffer. */
17060 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17061 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17062 Z_delta = Z - Z_old;
17063 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17065 /* Give up if PT is not in the window. Note that it already has
17066 been checked at the start of try_window_id that PT is not in
17067 front of the window start. */
17068 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17069 GIVE_UP (13);
17071 /* If window start is unchanged, we can reuse the whole matrix
17072 as is, after adjusting glyph positions. No need to compute
17073 the window end again, since its offset from Z hasn't changed. */
17074 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17075 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17076 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17077 /* PT must not be in a partially visible line. */
17078 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17079 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17081 /* Adjust positions in the glyph matrix. */
17082 if (Z_delta || Z_delta_bytes)
17084 struct glyph_row *r1
17085 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17086 increment_matrix_positions (w->current_matrix,
17087 MATRIX_ROW_VPOS (r0, current_matrix),
17088 MATRIX_ROW_VPOS (r1, current_matrix),
17089 Z_delta, Z_delta_bytes);
17092 /* Set the cursor. */
17093 row = row_containing_pos (w, PT, r0, NULL, 0);
17094 if (row)
17095 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17096 else
17097 abort ();
17098 return 1;
17102 /* Handle the case that changes are all below what is displayed in
17103 the window, and that PT is in the window. This shortcut cannot
17104 be taken if ZV is visible in the window, and text has been added
17105 there that is visible in the window. */
17106 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17107 /* ZV is not visible in the window, or there are no
17108 changes at ZV, actually. */
17109 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17110 || first_changed_charpos == last_changed_charpos))
17112 struct glyph_row *r0;
17114 /* Give up if PT is not in the window. Note that it already has
17115 been checked at the start of try_window_id that PT is not in
17116 front of the window start. */
17117 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17118 GIVE_UP (14);
17120 /* If window start is unchanged, we can reuse the whole matrix
17121 as is, without changing glyph positions since no text has
17122 been added/removed in front of the window end. */
17123 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17124 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17125 /* PT must not be in a partially visible line. */
17126 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17127 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17129 /* We have to compute the window end anew since text
17130 could have been added/removed after it. */
17131 w->window_end_pos
17132 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17133 w->window_end_bytepos
17134 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17136 /* Set the cursor. */
17137 row = row_containing_pos (w, PT, r0, NULL, 0);
17138 if (row)
17139 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17140 else
17141 abort ();
17142 return 2;
17146 /* Give up if window start is in the changed area.
17148 The condition used to read
17150 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17152 but why that was tested escapes me at the moment. */
17153 if (CHARPOS (start) >= first_changed_charpos
17154 && CHARPOS (start) <= last_changed_charpos)
17155 GIVE_UP (15);
17157 /* Check that window start agrees with the start of the first glyph
17158 row in its current matrix. Check this after we know the window
17159 start is not in changed text, otherwise positions would not be
17160 comparable. */
17161 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17162 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17163 GIVE_UP (16);
17165 /* Give up if the window ends in strings. Overlay strings
17166 at the end are difficult to handle, so don't try. */
17167 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17168 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17169 GIVE_UP (20);
17171 /* Compute the position at which we have to start displaying new
17172 lines. Some of the lines at the top of the window might be
17173 reusable because they are not displaying changed text. Find the
17174 last row in W's current matrix not affected by changes at the
17175 start of current_buffer. Value is null if changes start in the
17176 first line of window. */
17177 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17178 if (last_unchanged_at_beg_row)
17180 /* Avoid starting to display in the middle of a character, a TAB
17181 for instance. This is easier than to set up the iterator
17182 exactly, and it's not a frequent case, so the additional
17183 effort wouldn't really pay off. */
17184 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17185 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17186 && last_unchanged_at_beg_row > w->current_matrix->rows)
17187 --last_unchanged_at_beg_row;
17189 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17190 GIVE_UP (17);
17192 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17193 GIVE_UP (18);
17194 start_pos = it.current.pos;
17196 /* Start displaying new lines in the desired matrix at the same
17197 vpos we would use in the current matrix, i.e. below
17198 last_unchanged_at_beg_row. */
17199 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17200 current_matrix);
17201 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17202 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17204 xassert (it.hpos == 0 && it.current_x == 0);
17206 else
17208 /* There are no reusable lines at the start of the window.
17209 Start displaying in the first text line. */
17210 start_display (&it, w, start);
17211 it.vpos = it.first_vpos;
17212 start_pos = it.current.pos;
17215 /* Find the first row that is not affected by changes at the end of
17216 the buffer. Value will be null if there is no unchanged row, in
17217 which case we must redisplay to the end of the window. delta
17218 will be set to the value by which buffer positions beginning with
17219 first_unchanged_at_end_row have to be adjusted due to text
17220 changes. */
17221 first_unchanged_at_end_row
17222 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17223 IF_DEBUG (debug_delta = delta);
17224 IF_DEBUG (debug_delta_bytes = delta_bytes);
17226 /* Set stop_pos to the buffer position up to which we will have to
17227 display new lines. If first_unchanged_at_end_row != NULL, this
17228 is the buffer position of the start of the line displayed in that
17229 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17230 that we don't stop at a buffer position. */
17231 stop_pos = 0;
17232 if (first_unchanged_at_end_row)
17234 xassert (last_unchanged_at_beg_row == NULL
17235 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17237 /* If this is a continuation line, move forward to the next one
17238 that isn't. Changes in lines above affect this line.
17239 Caution: this may move first_unchanged_at_end_row to a row
17240 not displaying text. */
17241 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17242 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17243 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17244 < it.last_visible_y))
17245 ++first_unchanged_at_end_row;
17247 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17248 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17249 >= it.last_visible_y))
17250 first_unchanged_at_end_row = NULL;
17251 else
17253 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17254 + delta);
17255 first_unchanged_at_end_vpos
17256 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17257 xassert (stop_pos >= Z - END_UNCHANGED);
17260 else if (last_unchanged_at_beg_row == NULL)
17261 GIVE_UP (19);
17264 #if GLYPH_DEBUG
17266 /* Either there is no unchanged row at the end, or the one we have
17267 now displays text. This is a necessary condition for the window
17268 end pos calculation at the end of this function. */
17269 xassert (first_unchanged_at_end_row == NULL
17270 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17272 debug_last_unchanged_at_beg_vpos
17273 = (last_unchanged_at_beg_row
17274 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17275 : -1);
17276 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17278 #endif /* GLYPH_DEBUG != 0 */
17281 /* Display new lines. Set last_text_row to the last new line
17282 displayed which has text on it, i.e. might end up as being the
17283 line where the window_end_vpos is. */
17284 w->cursor.vpos = -1;
17285 last_text_row = NULL;
17286 overlay_arrow_seen = 0;
17287 while (it.current_y < it.last_visible_y
17288 && !fonts_changed_p
17289 && (first_unchanged_at_end_row == NULL
17290 || IT_CHARPOS (it) < stop_pos))
17292 if (display_line (&it))
17293 last_text_row = it.glyph_row - 1;
17296 if (fonts_changed_p)
17297 return -1;
17300 /* Compute differences in buffer positions, y-positions etc. for
17301 lines reused at the bottom of the window. Compute what we can
17302 scroll. */
17303 if (first_unchanged_at_end_row
17304 /* No lines reused because we displayed everything up to the
17305 bottom of the window. */
17306 && it.current_y < it.last_visible_y)
17308 dvpos = (it.vpos
17309 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17310 current_matrix));
17311 dy = it.current_y - first_unchanged_at_end_row->y;
17312 run.current_y = first_unchanged_at_end_row->y;
17313 run.desired_y = run.current_y + dy;
17314 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17316 else
17318 delta = delta_bytes = dvpos = dy
17319 = run.current_y = run.desired_y = run.height = 0;
17320 first_unchanged_at_end_row = NULL;
17322 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17325 /* Find the cursor if not already found. We have to decide whether
17326 PT will appear on this window (it sometimes doesn't, but this is
17327 not a very frequent case.) This decision has to be made before
17328 the current matrix is altered. A value of cursor.vpos < 0 means
17329 that PT is either in one of the lines beginning at
17330 first_unchanged_at_end_row or below the window. Don't care for
17331 lines that might be displayed later at the window end; as
17332 mentioned, this is not a frequent case. */
17333 if (w->cursor.vpos < 0)
17335 /* Cursor in unchanged rows at the top? */
17336 if (PT < CHARPOS (start_pos)
17337 && last_unchanged_at_beg_row)
17339 row = row_containing_pos (w, PT,
17340 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17341 last_unchanged_at_beg_row + 1, 0);
17342 if (row)
17343 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17346 /* Start from first_unchanged_at_end_row looking for PT. */
17347 else if (first_unchanged_at_end_row)
17349 row = row_containing_pos (w, PT - delta,
17350 first_unchanged_at_end_row, NULL, 0);
17351 if (row)
17352 set_cursor_from_row (w, row, w->current_matrix, delta,
17353 delta_bytes, dy, dvpos);
17356 /* Give up if cursor was not found. */
17357 if (w->cursor.vpos < 0)
17359 clear_glyph_matrix (w->desired_matrix);
17360 return -1;
17364 /* Don't let the cursor end in the scroll margins. */
17366 int this_scroll_margin, cursor_height;
17368 this_scroll_margin =
17369 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17370 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17371 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17373 if ((w->cursor.y < this_scroll_margin
17374 && CHARPOS (start) > BEGV)
17375 /* Old redisplay didn't take scroll margin into account at the bottom,
17376 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17377 || (w->cursor.y + (make_cursor_line_fully_visible_p
17378 ? cursor_height + this_scroll_margin
17379 : 1)) > it.last_visible_y)
17381 w->cursor.vpos = -1;
17382 clear_glyph_matrix (w->desired_matrix);
17383 return -1;
17387 /* Scroll the display. Do it before changing the current matrix so
17388 that xterm.c doesn't get confused about where the cursor glyph is
17389 found. */
17390 if (dy && run.height)
17392 update_begin (f);
17394 if (FRAME_WINDOW_P (f))
17396 FRAME_RIF (f)->update_window_begin_hook (w);
17397 FRAME_RIF (f)->clear_window_mouse_face (w);
17398 FRAME_RIF (f)->scroll_run_hook (w, &run);
17399 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17401 else
17403 /* Terminal frame. In this case, dvpos gives the number of
17404 lines to scroll by; dvpos < 0 means scroll up. */
17405 int from_vpos
17406 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17407 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17408 int end = (WINDOW_TOP_EDGE_LINE (w)
17409 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17410 + window_internal_height (w));
17412 #if defined (HAVE_GPM) || defined (MSDOS)
17413 x_clear_window_mouse_face (w);
17414 #endif
17415 /* Perform the operation on the screen. */
17416 if (dvpos > 0)
17418 /* Scroll last_unchanged_at_beg_row to the end of the
17419 window down dvpos lines. */
17420 set_terminal_window (f, end);
17422 /* On dumb terminals delete dvpos lines at the end
17423 before inserting dvpos empty lines. */
17424 if (!FRAME_SCROLL_REGION_OK (f))
17425 ins_del_lines (f, end - dvpos, -dvpos);
17427 /* Insert dvpos empty lines in front of
17428 last_unchanged_at_beg_row. */
17429 ins_del_lines (f, from, dvpos);
17431 else if (dvpos < 0)
17433 /* Scroll up last_unchanged_at_beg_vpos to the end of
17434 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17435 set_terminal_window (f, end);
17437 /* Delete dvpos lines in front of
17438 last_unchanged_at_beg_vpos. ins_del_lines will set
17439 the cursor to the given vpos and emit |dvpos| delete
17440 line sequences. */
17441 ins_del_lines (f, from + dvpos, dvpos);
17443 /* On a dumb terminal insert dvpos empty lines at the
17444 end. */
17445 if (!FRAME_SCROLL_REGION_OK (f))
17446 ins_del_lines (f, end + dvpos, -dvpos);
17449 set_terminal_window (f, 0);
17452 update_end (f);
17455 /* Shift reused rows of the current matrix to the right position.
17456 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17457 text. */
17458 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17459 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17460 if (dvpos < 0)
17462 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17463 bottom_vpos, dvpos);
17464 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17465 bottom_vpos, 0);
17467 else if (dvpos > 0)
17469 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17470 bottom_vpos, dvpos);
17471 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17472 first_unchanged_at_end_vpos + dvpos, 0);
17475 /* For frame-based redisplay, make sure that current frame and window
17476 matrix are in sync with respect to glyph memory. */
17477 if (!FRAME_WINDOW_P (f))
17478 sync_frame_with_window_matrix_rows (w);
17480 /* Adjust buffer positions in reused rows. */
17481 if (delta || delta_bytes)
17482 increment_matrix_positions (current_matrix,
17483 first_unchanged_at_end_vpos + dvpos,
17484 bottom_vpos, delta, delta_bytes);
17486 /* Adjust Y positions. */
17487 if (dy)
17488 shift_glyph_matrix (w, current_matrix,
17489 first_unchanged_at_end_vpos + dvpos,
17490 bottom_vpos, dy);
17492 if (first_unchanged_at_end_row)
17494 first_unchanged_at_end_row += dvpos;
17495 if (first_unchanged_at_end_row->y >= it.last_visible_y
17496 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17497 first_unchanged_at_end_row = NULL;
17500 /* If scrolling up, there may be some lines to display at the end of
17501 the window. */
17502 last_text_row_at_end = NULL;
17503 if (dy < 0)
17505 /* Scrolling up can leave for example a partially visible line
17506 at the end of the window to be redisplayed. */
17507 /* Set last_row to the glyph row in the current matrix where the
17508 window end line is found. It has been moved up or down in
17509 the matrix by dvpos. */
17510 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17511 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17513 /* If last_row is the window end line, it should display text. */
17514 xassert (last_row->displays_text_p);
17516 /* If window end line was partially visible before, begin
17517 displaying at that line. Otherwise begin displaying with the
17518 line following it. */
17519 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17521 init_to_row_start (&it, w, last_row);
17522 it.vpos = last_vpos;
17523 it.current_y = last_row->y;
17525 else
17527 init_to_row_end (&it, w, last_row);
17528 it.vpos = 1 + last_vpos;
17529 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17530 ++last_row;
17533 /* We may start in a continuation line. If so, we have to
17534 get the right continuation_lines_width and current_x. */
17535 it.continuation_lines_width = last_row->continuation_lines_width;
17536 it.hpos = it.current_x = 0;
17538 /* Display the rest of the lines at the window end. */
17539 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17540 while (it.current_y < it.last_visible_y
17541 && !fonts_changed_p)
17543 /* Is it always sure that the display agrees with lines in
17544 the current matrix? I don't think so, so we mark rows
17545 displayed invalid in the current matrix by setting their
17546 enabled_p flag to zero. */
17547 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17548 if (display_line (&it))
17549 last_text_row_at_end = it.glyph_row - 1;
17553 /* Update window_end_pos and window_end_vpos. */
17554 if (first_unchanged_at_end_row
17555 && !last_text_row_at_end)
17557 /* Window end line if one of the preserved rows from the current
17558 matrix. Set row to the last row displaying text in current
17559 matrix starting at first_unchanged_at_end_row, after
17560 scrolling. */
17561 xassert (first_unchanged_at_end_row->displays_text_p);
17562 row = find_last_row_displaying_text (w->current_matrix, &it,
17563 first_unchanged_at_end_row);
17564 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17566 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17567 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17568 w->window_end_vpos
17569 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17570 xassert (w->window_end_bytepos >= 0);
17571 IF_DEBUG (debug_method_add (w, "A"));
17573 else if (last_text_row_at_end)
17575 w->window_end_pos
17576 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17577 w->window_end_bytepos
17578 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17579 w->window_end_vpos
17580 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17581 xassert (w->window_end_bytepos >= 0);
17582 IF_DEBUG (debug_method_add (w, "B"));
17584 else if (last_text_row)
17586 /* We have displayed either to the end of the window or at the
17587 end of the window, i.e. the last row with text is to be found
17588 in the desired matrix. */
17589 w->window_end_pos
17590 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17591 w->window_end_bytepos
17592 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17593 w->window_end_vpos
17594 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17595 xassert (w->window_end_bytepos >= 0);
17597 else if (first_unchanged_at_end_row == NULL
17598 && last_text_row == NULL
17599 && last_text_row_at_end == NULL)
17601 /* Displayed to end of window, but no line containing text was
17602 displayed. Lines were deleted at the end of the window. */
17603 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17604 int vpos = XFASTINT (w->window_end_vpos);
17605 struct glyph_row *current_row = current_matrix->rows + vpos;
17606 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17608 for (row = NULL;
17609 row == NULL && vpos >= first_vpos;
17610 --vpos, --current_row, --desired_row)
17612 if (desired_row->enabled_p)
17614 if (desired_row->displays_text_p)
17615 row = desired_row;
17617 else if (current_row->displays_text_p)
17618 row = current_row;
17621 xassert (row != NULL);
17622 w->window_end_vpos = make_number (vpos + 1);
17623 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17624 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17625 xassert (w->window_end_bytepos >= 0);
17626 IF_DEBUG (debug_method_add (w, "C"));
17628 else
17629 abort ();
17631 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17632 debug_end_vpos = XFASTINT (w->window_end_vpos));
17634 /* Record that display has not been completed. */
17635 w->window_end_valid = Qnil;
17636 w->desired_matrix->no_scrolling_p = 1;
17637 return 3;
17639 #undef GIVE_UP
17644 /***********************************************************************
17645 More debugging support
17646 ***********************************************************************/
17648 #if GLYPH_DEBUG
17650 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17651 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17652 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17655 /* Dump the contents of glyph matrix MATRIX on stderr.
17657 GLYPHS 0 means don't show glyph contents.
17658 GLYPHS 1 means show glyphs in short form
17659 GLYPHS > 1 means show glyphs in long form. */
17661 void
17662 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17664 int i;
17665 for (i = 0; i < matrix->nrows; ++i)
17666 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17670 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17671 the glyph row and area where the glyph comes from. */
17673 void
17674 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17676 if (glyph->type == CHAR_GLYPH)
17678 fprintf (stderr,
17679 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17680 glyph - row->glyphs[TEXT_AREA],
17681 'C',
17682 glyph->charpos,
17683 (BUFFERP (glyph->object)
17684 ? 'B'
17685 : (STRINGP (glyph->object)
17686 ? 'S'
17687 : '-')),
17688 glyph->pixel_width,
17689 glyph->u.ch,
17690 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17691 ? glyph->u.ch
17692 : '.'),
17693 glyph->face_id,
17694 glyph->left_box_line_p,
17695 glyph->right_box_line_p);
17697 else if (glyph->type == STRETCH_GLYPH)
17699 fprintf (stderr,
17700 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17701 glyph - row->glyphs[TEXT_AREA],
17702 'S',
17703 glyph->charpos,
17704 (BUFFERP (glyph->object)
17705 ? 'B'
17706 : (STRINGP (glyph->object)
17707 ? 'S'
17708 : '-')),
17709 glyph->pixel_width,
17711 '.',
17712 glyph->face_id,
17713 glyph->left_box_line_p,
17714 glyph->right_box_line_p);
17716 else if (glyph->type == IMAGE_GLYPH)
17718 fprintf (stderr,
17719 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17720 glyph - row->glyphs[TEXT_AREA],
17721 'I',
17722 glyph->charpos,
17723 (BUFFERP (glyph->object)
17724 ? 'B'
17725 : (STRINGP (glyph->object)
17726 ? 'S'
17727 : '-')),
17728 glyph->pixel_width,
17729 glyph->u.img_id,
17730 '.',
17731 glyph->face_id,
17732 glyph->left_box_line_p,
17733 glyph->right_box_line_p);
17735 else if (glyph->type == COMPOSITE_GLYPH)
17737 fprintf (stderr,
17738 " %5td %4c %6"pI"d %c %3d 0x%05x",
17739 glyph - row->glyphs[TEXT_AREA],
17740 '+',
17741 glyph->charpos,
17742 (BUFFERP (glyph->object)
17743 ? 'B'
17744 : (STRINGP (glyph->object)
17745 ? 'S'
17746 : '-')),
17747 glyph->pixel_width,
17748 glyph->u.cmp.id);
17749 if (glyph->u.cmp.automatic)
17750 fprintf (stderr,
17751 "[%d-%d]",
17752 glyph->slice.cmp.from, glyph->slice.cmp.to);
17753 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17754 glyph->face_id,
17755 glyph->left_box_line_p,
17756 glyph->right_box_line_p);
17761 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17762 GLYPHS 0 means don't show glyph contents.
17763 GLYPHS 1 means show glyphs in short form
17764 GLYPHS > 1 means show glyphs in long form. */
17766 void
17767 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17769 if (glyphs != 1)
17771 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17772 fprintf (stderr, "======================================================================\n");
17774 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17775 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17776 vpos,
17777 MATRIX_ROW_START_CHARPOS (row),
17778 MATRIX_ROW_END_CHARPOS (row),
17779 row->used[TEXT_AREA],
17780 row->contains_overlapping_glyphs_p,
17781 row->enabled_p,
17782 row->truncated_on_left_p,
17783 row->truncated_on_right_p,
17784 row->continued_p,
17785 MATRIX_ROW_CONTINUATION_LINE_P (row),
17786 row->displays_text_p,
17787 row->ends_at_zv_p,
17788 row->fill_line_p,
17789 row->ends_in_middle_of_char_p,
17790 row->starts_in_middle_of_char_p,
17791 row->mouse_face_p,
17792 row->x,
17793 row->y,
17794 row->pixel_width,
17795 row->height,
17796 row->visible_height,
17797 row->ascent,
17798 row->phys_ascent);
17799 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
17800 row->end.overlay_string_index,
17801 row->continuation_lines_width);
17802 fprintf (stderr, "%9"pI"d %5"pI"d\n",
17803 CHARPOS (row->start.string_pos),
17804 CHARPOS (row->end.string_pos));
17805 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17806 row->end.dpvec_index);
17809 if (glyphs > 1)
17811 int area;
17813 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17815 struct glyph *glyph = row->glyphs[area];
17816 struct glyph *glyph_end = glyph + row->used[area];
17818 /* Glyph for a line end in text. */
17819 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17820 ++glyph_end;
17822 if (glyph < glyph_end)
17823 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
17825 for (; glyph < glyph_end; ++glyph)
17826 dump_glyph (row, glyph, area);
17829 else if (glyphs == 1)
17831 int area;
17833 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17835 char *s = (char *) alloca (row->used[area] + 1);
17836 int i;
17838 for (i = 0; i < row->used[area]; ++i)
17840 struct glyph *glyph = row->glyphs[area] + i;
17841 if (glyph->type == CHAR_GLYPH
17842 && glyph->u.ch < 0x80
17843 && glyph->u.ch >= ' ')
17844 s[i] = glyph->u.ch;
17845 else
17846 s[i] = '.';
17849 s[i] = '\0';
17850 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17856 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17857 Sdump_glyph_matrix, 0, 1, "p",
17858 doc: /* Dump the current matrix of the selected window to stderr.
17859 Shows contents of glyph row structures. With non-nil
17860 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17861 glyphs in short form, otherwise show glyphs in long form. */)
17862 (Lisp_Object glyphs)
17864 struct window *w = XWINDOW (selected_window);
17865 struct buffer *buffer = XBUFFER (w->buffer);
17867 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17868 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17869 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17870 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17871 fprintf (stderr, "=============================================\n");
17872 dump_glyph_matrix (w->current_matrix,
17873 NILP (glyphs) ? 0 : XINT (glyphs));
17874 return Qnil;
17878 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17879 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17880 (void)
17882 struct frame *f = XFRAME (selected_frame);
17883 dump_glyph_matrix (f->current_matrix, 1);
17884 return Qnil;
17888 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17889 doc: /* Dump glyph row ROW to stderr.
17890 GLYPH 0 means don't dump glyphs.
17891 GLYPH 1 means dump glyphs in short form.
17892 GLYPH > 1 or omitted means dump glyphs in long form. */)
17893 (Lisp_Object row, Lisp_Object glyphs)
17895 struct glyph_matrix *matrix;
17896 int vpos;
17898 CHECK_NUMBER (row);
17899 matrix = XWINDOW (selected_window)->current_matrix;
17900 vpos = XINT (row);
17901 if (vpos >= 0 && vpos < matrix->nrows)
17902 dump_glyph_row (MATRIX_ROW (matrix, vpos),
17903 vpos,
17904 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17905 return Qnil;
17909 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
17910 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
17911 GLYPH 0 means don't dump glyphs.
17912 GLYPH 1 means dump glyphs in short form.
17913 GLYPH > 1 or omitted means dump glyphs in long form. */)
17914 (Lisp_Object row, Lisp_Object glyphs)
17916 struct frame *sf = SELECTED_FRAME ();
17917 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
17918 int vpos;
17920 CHECK_NUMBER (row);
17921 vpos = XINT (row);
17922 if (vpos >= 0 && vpos < m->nrows)
17923 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
17924 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17925 return Qnil;
17929 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
17930 doc: /* Toggle tracing of redisplay.
17931 With ARG, turn tracing on if and only if ARG is positive. */)
17932 (Lisp_Object arg)
17934 if (NILP (arg))
17935 trace_redisplay_p = !trace_redisplay_p;
17936 else
17938 arg = Fprefix_numeric_value (arg);
17939 trace_redisplay_p = XINT (arg) > 0;
17942 return Qnil;
17946 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
17947 doc: /* Like `format', but print result to stderr.
17948 usage: (trace-to-stderr STRING &rest OBJECTS) */)
17949 (ptrdiff_t nargs, Lisp_Object *args)
17951 Lisp_Object s = Fformat (nargs, args);
17952 fprintf (stderr, "%s", SDATA (s));
17953 return Qnil;
17956 #endif /* GLYPH_DEBUG */
17960 /***********************************************************************
17961 Building Desired Matrix Rows
17962 ***********************************************************************/
17964 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
17965 Used for non-window-redisplay windows, and for windows w/o left fringe. */
17967 static struct glyph_row *
17968 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
17970 struct frame *f = XFRAME (WINDOW_FRAME (w));
17971 struct buffer *buffer = XBUFFER (w->buffer);
17972 struct buffer *old = current_buffer;
17973 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
17974 int arrow_len = SCHARS (overlay_arrow_string);
17975 const unsigned char *arrow_end = arrow_string + arrow_len;
17976 const unsigned char *p;
17977 struct it it;
17978 int multibyte_p;
17979 int n_glyphs_before;
17981 set_buffer_temp (buffer);
17982 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
17983 it.glyph_row->used[TEXT_AREA] = 0;
17984 SET_TEXT_POS (it.position, 0, 0);
17986 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
17987 p = arrow_string;
17988 while (p < arrow_end)
17990 Lisp_Object face, ilisp;
17992 /* Get the next character. */
17993 if (multibyte_p)
17994 it.c = it.char_to_display = string_char_and_length (p, &it.len);
17995 else
17997 it.c = it.char_to_display = *p, it.len = 1;
17998 if (! ASCII_CHAR_P (it.c))
17999 it.char_to_display = BYTE8_TO_CHAR (it.c);
18001 p += it.len;
18003 /* Get its face. */
18004 ilisp = make_number (p - arrow_string);
18005 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18006 it.face_id = compute_char_face (f, it.char_to_display, face);
18008 /* Compute its width, get its glyphs. */
18009 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18010 SET_TEXT_POS (it.position, -1, -1);
18011 PRODUCE_GLYPHS (&it);
18013 /* If this character doesn't fit any more in the line, we have
18014 to remove some glyphs. */
18015 if (it.current_x > it.last_visible_x)
18017 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18018 break;
18022 set_buffer_temp (old);
18023 return it.glyph_row;
18027 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
18028 glyphs are only inserted for terminal frames since we can't really
18029 win with truncation glyphs when partially visible glyphs are
18030 involved. Which glyphs to insert is determined by
18031 produce_special_glyphs. */
18033 static void
18034 insert_left_trunc_glyphs (struct it *it)
18036 struct it truncate_it;
18037 struct glyph *from, *end, *to, *toend;
18039 xassert (!FRAME_WINDOW_P (it->f));
18041 /* Get the truncation glyphs. */
18042 truncate_it = *it;
18043 truncate_it.current_x = 0;
18044 truncate_it.face_id = DEFAULT_FACE_ID;
18045 truncate_it.glyph_row = &scratch_glyph_row;
18046 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18047 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18048 truncate_it.object = make_number (0);
18049 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18051 /* Overwrite glyphs from IT with truncation glyphs. */
18052 if (!it->glyph_row->reversed_p)
18054 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18055 end = from + truncate_it.glyph_row->used[TEXT_AREA];
18056 to = it->glyph_row->glyphs[TEXT_AREA];
18057 toend = to + it->glyph_row->used[TEXT_AREA];
18059 while (from < end)
18060 *to++ = *from++;
18062 /* There may be padding glyphs left over. Overwrite them too. */
18063 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18065 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18066 while (from < end)
18067 *to++ = *from++;
18070 if (to > toend)
18071 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18073 else
18075 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18076 that back to front. */
18077 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18078 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18079 toend = it->glyph_row->glyphs[TEXT_AREA];
18080 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18082 while (from >= end && to >= toend)
18083 *to-- = *from--;
18084 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18086 from =
18087 truncate_it.glyph_row->glyphs[TEXT_AREA]
18088 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18089 while (from >= end && to >= toend)
18090 *to-- = *from--;
18092 if (from >= end)
18094 /* Need to free some room before prepending additional
18095 glyphs. */
18096 int move_by = from - end + 1;
18097 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18098 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18100 for ( ; g >= g0; g--)
18101 g[move_by] = *g;
18102 while (from >= end)
18103 *to-- = *from--;
18104 it->glyph_row->used[TEXT_AREA] += move_by;
18109 /* Compute the hash code for ROW. */
18110 unsigned
18111 row_hash (struct glyph_row *row)
18113 int area, k;
18114 unsigned hashval = 0;
18116 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18117 for (k = 0; k < row->used[area]; ++k)
18118 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18119 + row->glyphs[area][k].u.val
18120 + row->glyphs[area][k].face_id
18121 + row->glyphs[area][k].padding_p
18122 + (row->glyphs[area][k].type << 2));
18124 return hashval;
18127 /* Compute the pixel height and width of IT->glyph_row.
18129 Most of the time, ascent and height of a display line will be equal
18130 to the max_ascent and max_height values of the display iterator
18131 structure. This is not the case if
18133 1. We hit ZV without displaying anything. In this case, max_ascent
18134 and max_height will be zero.
18136 2. We have some glyphs that don't contribute to the line height.
18137 (The glyph row flag contributes_to_line_height_p is for future
18138 pixmap extensions).
18140 The first case is easily covered by using default values because in
18141 these cases, the line height does not really matter, except that it
18142 must not be zero. */
18144 static void
18145 compute_line_metrics (struct it *it)
18147 struct glyph_row *row = it->glyph_row;
18149 if (FRAME_WINDOW_P (it->f))
18151 int i, min_y, max_y;
18153 /* The line may consist of one space only, that was added to
18154 place the cursor on it. If so, the row's height hasn't been
18155 computed yet. */
18156 if (row->height == 0)
18158 if (it->max_ascent + it->max_descent == 0)
18159 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18160 row->ascent = it->max_ascent;
18161 row->height = it->max_ascent + it->max_descent;
18162 row->phys_ascent = it->max_phys_ascent;
18163 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18164 row->extra_line_spacing = it->max_extra_line_spacing;
18167 /* Compute the width of this line. */
18168 row->pixel_width = row->x;
18169 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18170 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18172 xassert (row->pixel_width >= 0);
18173 xassert (row->ascent >= 0 && row->height > 0);
18175 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18176 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18178 /* If first line's physical ascent is larger than its logical
18179 ascent, use the physical ascent, and make the row taller.
18180 This makes accented characters fully visible. */
18181 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18182 && row->phys_ascent > row->ascent)
18184 row->height += row->phys_ascent - row->ascent;
18185 row->ascent = row->phys_ascent;
18188 /* Compute how much of the line is visible. */
18189 row->visible_height = row->height;
18191 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18192 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18194 if (row->y < min_y)
18195 row->visible_height -= min_y - row->y;
18196 if (row->y + row->height > max_y)
18197 row->visible_height -= row->y + row->height - max_y;
18199 else
18201 row->pixel_width = row->used[TEXT_AREA];
18202 if (row->continued_p)
18203 row->pixel_width -= it->continuation_pixel_width;
18204 else if (row->truncated_on_right_p)
18205 row->pixel_width -= it->truncation_pixel_width;
18206 row->ascent = row->phys_ascent = 0;
18207 row->height = row->phys_height = row->visible_height = 1;
18208 row->extra_line_spacing = 0;
18211 /* Compute a hash code for this row. */
18212 row->hash = row_hash (row);
18214 it->max_ascent = it->max_descent = 0;
18215 it->max_phys_ascent = it->max_phys_descent = 0;
18219 /* Append one space to the glyph row of iterator IT if doing a
18220 window-based redisplay. The space has the same face as
18221 IT->face_id. Value is non-zero if a space was added.
18223 This function is called to make sure that there is always one glyph
18224 at the end of a glyph row that the cursor can be set on under
18225 window-systems. (If there weren't such a glyph we would not know
18226 how wide and tall a box cursor should be displayed).
18228 At the same time this space let's a nicely handle clearing to the
18229 end of the line if the row ends in italic text. */
18231 static int
18232 append_space_for_newline (struct it *it, int default_face_p)
18234 if (FRAME_WINDOW_P (it->f))
18236 int n = it->glyph_row->used[TEXT_AREA];
18238 if (it->glyph_row->glyphs[TEXT_AREA] + n
18239 < it->glyph_row->glyphs[1 + TEXT_AREA])
18241 /* Save some values that must not be changed.
18242 Must save IT->c and IT->len because otherwise
18243 ITERATOR_AT_END_P wouldn't work anymore after
18244 append_space_for_newline has been called. */
18245 enum display_element_type saved_what = it->what;
18246 int saved_c = it->c, saved_len = it->len;
18247 int saved_char_to_display = it->char_to_display;
18248 int saved_x = it->current_x;
18249 int saved_face_id = it->face_id;
18250 struct text_pos saved_pos;
18251 Lisp_Object saved_object;
18252 struct face *face;
18254 saved_object = it->object;
18255 saved_pos = it->position;
18257 it->what = IT_CHARACTER;
18258 memset (&it->position, 0, sizeof it->position);
18259 it->object = make_number (0);
18260 it->c = it->char_to_display = ' ';
18261 it->len = 1;
18263 /* If the default face was remapped, be sure to use the
18264 remapped face for the appended newline. */
18265 if (default_face_p)
18266 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18267 else if (it->face_before_selective_p)
18268 it->face_id = it->saved_face_id;
18269 face = FACE_FROM_ID (it->f, it->face_id);
18270 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18272 PRODUCE_GLYPHS (it);
18274 it->override_ascent = -1;
18275 it->constrain_row_ascent_descent_p = 0;
18276 it->current_x = saved_x;
18277 it->object = saved_object;
18278 it->position = saved_pos;
18279 it->what = saved_what;
18280 it->face_id = saved_face_id;
18281 it->len = saved_len;
18282 it->c = saved_c;
18283 it->char_to_display = saved_char_to_display;
18284 return 1;
18288 return 0;
18292 /* Extend the face of the last glyph in the text area of IT->glyph_row
18293 to the end of the display line. Called from display_line. If the
18294 glyph row is empty, add a space glyph to it so that we know the
18295 face to draw. Set the glyph row flag fill_line_p. If the glyph
18296 row is R2L, prepend a stretch glyph to cover the empty space to the
18297 left of the leftmost glyph. */
18299 static void
18300 extend_face_to_end_of_line (struct it *it)
18302 struct face *face, *default_face;
18303 struct frame *f = it->f;
18305 /* If line is already filled, do nothing. Non window-system frames
18306 get a grace of one more ``pixel'' because their characters are
18307 1-``pixel'' wide, so they hit the equality too early. This grace
18308 is needed only for R2L rows that are not continued, to produce
18309 one extra blank where we could display the cursor. */
18310 if (it->current_x >= it->last_visible_x
18311 + (!FRAME_WINDOW_P (f)
18312 && it->glyph_row->reversed_p
18313 && !it->glyph_row->continued_p))
18314 return;
18316 /* The default face, possibly remapped. */
18317 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18319 /* Face extension extends the background and box of IT->face_id
18320 to the end of the line. If the background equals the background
18321 of the frame, we don't have to do anything. */
18322 if (it->face_before_selective_p)
18323 face = FACE_FROM_ID (f, it->saved_face_id);
18324 else
18325 face = FACE_FROM_ID (f, it->face_id);
18327 if (FRAME_WINDOW_P (f)
18328 && it->glyph_row->displays_text_p
18329 && face->box == FACE_NO_BOX
18330 && face->background == FRAME_BACKGROUND_PIXEL (f)
18331 && !face->stipple
18332 && !it->glyph_row->reversed_p)
18333 return;
18335 /* Set the glyph row flag indicating that the face of the last glyph
18336 in the text area has to be drawn to the end of the text area. */
18337 it->glyph_row->fill_line_p = 1;
18339 /* If current character of IT is not ASCII, make sure we have the
18340 ASCII face. This will be automatically undone the next time
18341 get_next_display_element returns a multibyte character. Note
18342 that the character will always be single byte in unibyte
18343 text. */
18344 if (!ASCII_CHAR_P (it->c))
18346 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18349 if (FRAME_WINDOW_P (f))
18351 /* If the row is empty, add a space with the current face of IT,
18352 so that we know which face to draw. */
18353 if (it->glyph_row->used[TEXT_AREA] == 0)
18355 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18356 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18357 it->glyph_row->used[TEXT_AREA] = 1;
18359 #ifdef HAVE_WINDOW_SYSTEM
18360 if (it->glyph_row->reversed_p)
18362 /* Prepend a stretch glyph to the row, such that the
18363 rightmost glyph will be drawn flushed all the way to the
18364 right margin of the window. The stretch glyph that will
18365 occupy the empty space, if any, to the left of the
18366 glyphs. */
18367 struct font *font = face->font ? face->font : FRAME_FONT (f);
18368 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18369 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18370 struct glyph *g;
18371 int row_width, stretch_ascent, stretch_width;
18372 struct text_pos saved_pos;
18373 int saved_face_id, saved_avoid_cursor;
18375 for (row_width = 0, g = row_start; g < row_end; g++)
18376 row_width += g->pixel_width;
18377 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18378 if (stretch_width > 0)
18380 stretch_ascent =
18381 (((it->ascent + it->descent)
18382 * FONT_BASE (font)) / FONT_HEIGHT (font));
18383 saved_pos = it->position;
18384 memset (&it->position, 0, sizeof it->position);
18385 saved_avoid_cursor = it->avoid_cursor_p;
18386 it->avoid_cursor_p = 1;
18387 saved_face_id = it->face_id;
18388 /* The last row's stretch glyph should get the default
18389 face, to avoid painting the rest of the window with
18390 the region face, if the region ends at ZV. */
18391 if (it->glyph_row->ends_at_zv_p)
18392 it->face_id = default_face->id;
18393 else
18394 it->face_id = face->id;
18395 append_stretch_glyph (it, make_number (0), stretch_width,
18396 it->ascent + it->descent, stretch_ascent);
18397 it->position = saved_pos;
18398 it->avoid_cursor_p = saved_avoid_cursor;
18399 it->face_id = saved_face_id;
18402 #endif /* HAVE_WINDOW_SYSTEM */
18404 else
18406 /* Save some values that must not be changed. */
18407 int saved_x = it->current_x;
18408 struct text_pos saved_pos;
18409 Lisp_Object saved_object;
18410 enum display_element_type saved_what = it->what;
18411 int saved_face_id = it->face_id;
18413 saved_object = it->object;
18414 saved_pos = it->position;
18416 it->what = IT_CHARACTER;
18417 memset (&it->position, 0, sizeof it->position);
18418 it->object = make_number (0);
18419 it->c = it->char_to_display = ' ';
18420 it->len = 1;
18421 /* The last row's blank glyphs should get the default face, to
18422 avoid painting the rest of the window with the region face,
18423 if the region ends at ZV. */
18424 if (it->glyph_row->ends_at_zv_p)
18425 it->face_id = default_face->id;
18426 else
18427 it->face_id = face->id;
18429 PRODUCE_GLYPHS (it);
18431 while (it->current_x <= it->last_visible_x)
18432 PRODUCE_GLYPHS (it);
18434 /* Don't count these blanks really. It would let us insert a left
18435 truncation glyph below and make us set the cursor on them, maybe. */
18436 it->current_x = saved_x;
18437 it->object = saved_object;
18438 it->position = saved_pos;
18439 it->what = saved_what;
18440 it->face_id = saved_face_id;
18445 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18446 trailing whitespace. */
18448 static int
18449 trailing_whitespace_p (EMACS_INT charpos)
18451 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
18452 int c = 0;
18454 while (bytepos < ZV_BYTE
18455 && (c = FETCH_CHAR (bytepos),
18456 c == ' ' || c == '\t'))
18457 ++bytepos;
18459 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18461 if (bytepos != PT_BYTE)
18462 return 1;
18464 return 0;
18468 /* Highlight trailing whitespace, if any, in ROW. */
18470 static void
18471 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18473 int used = row->used[TEXT_AREA];
18475 if (used)
18477 struct glyph *start = row->glyphs[TEXT_AREA];
18478 struct glyph *glyph = start + used - 1;
18480 if (row->reversed_p)
18482 /* Right-to-left rows need to be processed in the opposite
18483 direction, so swap the edge pointers. */
18484 glyph = start;
18485 start = row->glyphs[TEXT_AREA] + used - 1;
18488 /* Skip over glyphs inserted to display the cursor at the
18489 end of a line, for extending the face of the last glyph
18490 to the end of the line on terminals, and for truncation
18491 and continuation glyphs. */
18492 if (!row->reversed_p)
18494 while (glyph >= start
18495 && glyph->type == CHAR_GLYPH
18496 && INTEGERP (glyph->object))
18497 --glyph;
18499 else
18501 while (glyph <= start
18502 && glyph->type == CHAR_GLYPH
18503 && INTEGERP (glyph->object))
18504 ++glyph;
18507 /* If last glyph is a space or stretch, and it's trailing
18508 whitespace, set the face of all trailing whitespace glyphs in
18509 IT->glyph_row to `trailing-whitespace'. */
18510 if ((row->reversed_p ? glyph <= start : glyph >= start)
18511 && BUFFERP (glyph->object)
18512 && (glyph->type == STRETCH_GLYPH
18513 || (glyph->type == CHAR_GLYPH
18514 && glyph->u.ch == ' '))
18515 && trailing_whitespace_p (glyph->charpos))
18517 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18518 if (face_id < 0)
18519 return;
18521 if (!row->reversed_p)
18523 while (glyph >= start
18524 && BUFFERP (glyph->object)
18525 && (glyph->type == STRETCH_GLYPH
18526 || (glyph->type == CHAR_GLYPH
18527 && glyph->u.ch == ' ')))
18528 (glyph--)->face_id = face_id;
18530 else
18532 while (glyph <= start
18533 && BUFFERP (glyph->object)
18534 && (glyph->type == STRETCH_GLYPH
18535 || (glyph->type == CHAR_GLYPH
18536 && glyph->u.ch == ' ')))
18537 (glyph++)->face_id = face_id;
18544 /* Value is non-zero if glyph row ROW should be
18545 used to hold the cursor. */
18547 static int
18548 cursor_row_p (struct glyph_row *row)
18550 int result = 1;
18552 if (PT == CHARPOS (row->end.pos)
18553 || PT == MATRIX_ROW_END_CHARPOS (row))
18555 /* Suppose the row ends on a string.
18556 Unless the row is continued, that means it ends on a newline
18557 in the string. If it's anything other than a display string
18558 (e.g., a before-string from an overlay), we don't want the
18559 cursor there. (This heuristic seems to give the optimal
18560 behavior for the various types of multi-line strings.)
18561 One exception: if the string has `cursor' property on one of
18562 its characters, we _do_ want the cursor there. */
18563 if (CHARPOS (row->end.string_pos) >= 0)
18565 if (row->continued_p)
18566 result = 1;
18567 else
18569 /* Check for `display' property. */
18570 struct glyph *beg = row->glyphs[TEXT_AREA];
18571 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18572 struct glyph *glyph;
18574 result = 0;
18575 for (glyph = end; glyph >= beg; --glyph)
18576 if (STRINGP (glyph->object))
18578 Lisp_Object prop
18579 = Fget_char_property (make_number (PT),
18580 Qdisplay, Qnil);
18581 result =
18582 (!NILP (prop)
18583 && display_prop_string_p (prop, glyph->object));
18584 /* If there's a `cursor' property on one of the
18585 string's characters, this row is a cursor row,
18586 even though this is not a display string. */
18587 if (!result)
18589 Lisp_Object s = glyph->object;
18591 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18593 EMACS_INT gpos = glyph->charpos;
18595 if (!NILP (Fget_char_property (make_number (gpos),
18596 Qcursor, s)))
18598 result = 1;
18599 break;
18603 break;
18607 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18609 /* If the row ends in middle of a real character,
18610 and the line is continued, we want the cursor here.
18611 That's because CHARPOS (ROW->end.pos) would equal
18612 PT if PT is before the character. */
18613 if (!row->ends_in_ellipsis_p)
18614 result = row->continued_p;
18615 else
18616 /* If the row ends in an ellipsis, then
18617 CHARPOS (ROW->end.pos) will equal point after the
18618 invisible text. We want that position to be displayed
18619 after the ellipsis. */
18620 result = 0;
18622 /* If the row ends at ZV, display the cursor at the end of that
18623 row instead of at the start of the row below. */
18624 else if (row->ends_at_zv_p)
18625 result = 1;
18626 else
18627 result = 0;
18630 return result;
18635 /* Push the property PROP so that it will be rendered at the current
18636 position in IT. Return 1 if PROP was successfully pushed, 0
18637 otherwise. Called from handle_line_prefix to handle the
18638 `line-prefix' and `wrap-prefix' properties. */
18640 static int
18641 push_prefix_prop (struct it *it, Lisp_Object prop)
18643 struct text_pos pos =
18644 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18646 xassert (it->method == GET_FROM_BUFFER
18647 || it->method == GET_FROM_DISPLAY_VECTOR
18648 || it->method == GET_FROM_STRING);
18650 /* We need to save the current buffer/string position, so it will be
18651 restored by pop_it, because iterate_out_of_display_property
18652 depends on that being set correctly, but some situations leave
18653 it->position not yet set when this function is called. */
18654 push_it (it, &pos);
18656 if (STRINGP (prop))
18658 if (SCHARS (prop) == 0)
18660 pop_it (it);
18661 return 0;
18664 it->string = prop;
18665 it->string_from_prefix_prop_p = 1;
18666 it->multibyte_p = STRING_MULTIBYTE (it->string);
18667 it->current.overlay_string_index = -1;
18668 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18669 it->end_charpos = it->string_nchars = SCHARS (it->string);
18670 it->method = GET_FROM_STRING;
18671 it->stop_charpos = 0;
18672 it->prev_stop = 0;
18673 it->base_level_stop = 0;
18675 /* Force paragraph direction to be that of the parent
18676 buffer/string. */
18677 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18678 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18679 else
18680 it->paragraph_embedding = L2R;
18682 /* Set up the bidi iterator for this display string. */
18683 if (it->bidi_p)
18685 it->bidi_it.string.lstring = it->string;
18686 it->bidi_it.string.s = NULL;
18687 it->bidi_it.string.schars = it->end_charpos;
18688 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18689 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18690 it->bidi_it.string.unibyte = !it->multibyte_p;
18691 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18694 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18696 it->method = GET_FROM_STRETCH;
18697 it->object = prop;
18699 #ifdef HAVE_WINDOW_SYSTEM
18700 else if (IMAGEP (prop))
18702 it->what = IT_IMAGE;
18703 it->image_id = lookup_image (it->f, prop);
18704 it->method = GET_FROM_IMAGE;
18706 #endif /* HAVE_WINDOW_SYSTEM */
18707 else
18709 pop_it (it); /* bogus display property, give up */
18710 return 0;
18713 return 1;
18716 /* Return the character-property PROP at the current position in IT. */
18718 static Lisp_Object
18719 get_it_property (struct it *it, Lisp_Object prop)
18721 Lisp_Object position;
18723 if (STRINGP (it->object))
18724 position = make_number (IT_STRING_CHARPOS (*it));
18725 else if (BUFFERP (it->object))
18726 position = make_number (IT_CHARPOS (*it));
18727 else
18728 return Qnil;
18730 return Fget_char_property (position, prop, it->object);
18733 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18735 static void
18736 handle_line_prefix (struct it *it)
18738 Lisp_Object prefix;
18740 if (it->continuation_lines_width > 0)
18742 prefix = get_it_property (it, Qwrap_prefix);
18743 if (NILP (prefix))
18744 prefix = Vwrap_prefix;
18746 else
18748 prefix = get_it_property (it, Qline_prefix);
18749 if (NILP (prefix))
18750 prefix = Vline_prefix;
18752 if (! NILP (prefix) && push_prefix_prop (it, prefix))
18754 /* If the prefix is wider than the window, and we try to wrap
18755 it, it would acquire its own wrap prefix, and so on till the
18756 iterator stack overflows. So, don't wrap the prefix. */
18757 it->line_wrap = TRUNCATE;
18758 it->avoid_cursor_p = 1;
18764 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18765 only for R2L lines from display_line and display_string, when they
18766 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18767 the line/string needs to be continued on the next glyph row. */
18768 static void
18769 unproduce_glyphs (struct it *it, int n)
18771 struct glyph *glyph, *end;
18773 xassert (it->glyph_row);
18774 xassert (it->glyph_row->reversed_p);
18775 xassert (it->area == TEXT_AREA);
18776 xassert (n <= it->glyph_row->used[TEXT_AREA]);
18778 if (n > it->glyph_row->used[TEXT_AREA])
18779 n = it->glyph_row->used[TEXT_AREA];
18780 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18781 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18782 for ( ; glyph < end; glyph++)
18783 glyph[-n] = *glyph;
18786 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18787 and ROW->maxpos. */
18788 static void
18789 find_row_edges (struct it *it, struct glyph_row *row,
18790 EMACS_INT min_pos, EMACS_INT min_bpos,
18791 EMACS_INT max_pos, EMACS_INT max_bpos)
18793 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18794 lines' rows is implemented for bidi-reordered rows. */
18796 /* ROW->minpos is the value of min_pos, the minimal buffer position
18797 we have in ROW, or ROW->start.pos if that is smaller. */
18798 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18799 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18800 else
18801 /* We didn't find buffer positions smaller than ROW->start, or
18802 didn't find _any_ valid buffer positions in any of the glyphs,
18803 so we must trust the iterator's computed positions. */
18804 row->minpos = row->start.pos;
18805 if (max_pos <= 0)
18807 max_pos = CHARPOS (it->current.pos);
18808 max_bpos = BYTEPOS (it->current.pos);
18811 /* Here are the various use-cases for ending the row, and the
18812 corresponding values for ROW->maxpos:
18814 Line ends in a newline from buffer eol_pos + 1
18815 Line is continued from buffer max_pos + 1
18816 Line is truncated on right it->current.pos
18817 Line ends in a newline from string max_pos + 1(*)
18818 (*) + 1 only when line ends in a forward scan
18819 Line is continued from string max_pos
18820 Line is continued from display vector max_pos
18821 Line is entirely from a string min_pos == max_pos
18822 Line is entirely from a display vector min_pos == max_pos
18823 Line that ends at ZV ZV
18825 If you discover other use-cases, please add them here as
18826 appropriate. */
18827 if (row->ends_at_zv_p)
18828 row->maxpos = it->current.pos;
18829 else if (row->used[TEXT_AREA])
18831 int seen_this_string = 0;
18832 struct glyph_row *r1 = row - 1;
18834 /* Did we see the same display string on the previous row? */
18835 if (STRINGP (it->object)
18836 /* this is not the first row */
18837 && row > it->w->desired_matrix->rows
18838 /* previous row is not the header line */
18839 && !r1->mode_line_p
18840 /* previous row also ends in a newline from a string */
18841 && r1->ends_in_newline_from_string_p)
18843 struct glyph *start, *end;
18845 /* Search for the last glyph of the previous row that came
18846 from buffer or string. Depending on whether the row is
18847 L2R or R2L, we need to process it front to back or the
18848 other way round. */
18849 if (!r1->reversed_p)
18851 start = r1->glyphs[TEXT_AREA];
18852 end = start + r1->used[TEXT_AREA];
18853 /* Glyphs inserted by redisplay have an integer (zero)
18854 as their object. */
18855 while (end > start
18856 && INTEGERP ((end - 1)->object)
18857 && (end - 1)->charpos <= 0)
18858 --end;
18859 if (end > start)
18861 if (EQ ((end - 1)->object, it->object))
18862 seen_this_string = 1;
18864 else
18865 /* If all the glyphs of the previous row were inserted
18866 by redisplay, it means the previous row was
18867 produced from a single newline, which is only
18868 possible if that newline came from the same string
18869 as the one which produced this ROW. */
18870 seen_this_string = 1;
18872 else
18874 end = r1->glyphs[TEXT_AREA] - 1;
18875 start = end + r1->used[TEXT_AREA];
18876 while (end < start
18877 && INTEGERP ((end + 1)->object)
18878 && (end + 1)->charpos <= 0)
18879 ++end;
18880 if (end < start)
18882 if (EQ ((end + 1)->object, it->object))
18883 seen_this_string = 1;
18885 else
18886 seen_this_string = 1;
18889 /* Take note of each display string that covers a newline only
18890 once, the first time we see it. This is for when a display
18891 string includes more than one newline in it. */
18892 if (row->ends_in_newline_from_string_p && !seen_this_string)
18894 /* If we were scanning the buffer forward when we displayed
18895 the string, we want to account for at least one buffer
18896 position that belongs to this row (position covered by
18897 the display string), so that cursor positioning will
18898 consider this row as a candidate when point is at the end
18899 of the visual line represented by this row. This is not
18900 required when scanning back, because max_pos will already
18901 have a much larger value. */
18902 if (CHARPOS (row->end.pos) > max_pos)
18903 INC_BOTH (max_pos, max_bpos);
18904 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18906 else if (CHARPOS (it->eol_pos) > 0)
18907 SET_TEXT_POS (row->maxpos,
18908 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
18909 else if (row->continued_p)
18911 /* If max_pos is different from IT's current position, it
18912 means IT->method does not belong to the display element
18913 at max_pos. However, it also means that the display
18914 element at max_pos was displayed in its entirety on this
18915 line, which is equivalent to saying that the next line
18916 starts at the next buffer position. */
18917 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
18918 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18919 else
18921 INC_BOTH (max_pos, max_bpos);
18922 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18925 else if (row->truncated_on_right_p)
18926 /* display_line already called reseat_at_next_visible_line_start,
18927 which puts the iterator at the beginning of the next line, in
18928 the logical order. */
18929 row->maxpos = it->current.pos;
18930 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
18931 /* A line that is entirely from a string/image/stretch... */
18932 row->maxpos = row->minpos;
18933 else
18934 abort ();
18936 else
18937 row->maxpos = it->current.pos;
18940 /* Construct the glyph row IT->glyph_row in the desired matrix of
18941 IT->w from text at the current position of IT. See dispextern.h
18942 for an overview of struct it. Value is non-zero if
18943 IT->glyph_row displays text, as opposed to a line displaying ZV
18944 only. */
18946 static int
18947 display_line (struct it *it)
18949 struct glyph_row *row = it->glyph_row;
18950 Lisp_Object overlay_arrow_string;
18951 struct it wrap_it;
18952 void *wrap_data = NULL;
18953 int may_wrap = 0, wrap_x IF_LINT (= 0);
18954 int wrap_row_used = -1;
18955 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
18956 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
18957 int wrap_row_extra_line_spacing IF_LINT (= 0);
18958 EMACS_INT wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
18959 EMACS_INT wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
18960 int cvpos;
18961 EMACS_INT min_pos = ZV + 1, max_pos = 0;
18962 EMACS_INT min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
18964 /* We always start displaying at hpos zero even if hscrolled. */
18965 xassert (it->hpos == 0 && it->current_x == 0);
18967 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
18968 >= it->w->desired_matrix->nrows)
18970 it->w->nrows_scale_factor++;
18971 fonts_changed_p = 1;
18972 return 0;
18975 /* Is IT->w showing the region? */
18976 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
18978 /* Clear the result glyph row and enable it. */
18979 prepare_desired_row (row);
18981 row->y = it->current_y;
18982 row->start = it->start;
18983 row->continuation_lines_width = it->continuation_lines_width;
18984 row->displays_text_p = 1;
18985 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
18986 it->starts_in_middle_of_char_p = 0;
18988 /* Arrange the overlays nicely for our purposes. Usually, we call
18989 display_line on only one line at a time, in which case this
18990 can't really hurt too much, or we call it on lines which appear
18991 one after another in the buffer, in which case all calls to
18992 recenter_overlay_lists but the first will be pretty cheap. */
18993 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
18995 /* Move over display elements that are not visible because we are
18996 hscrolled. This may stop at an x-position < IT->first_visible_x
18997 if the first glyph is partially visible or if we hit a line end. */
18998 if (it->current_x < it->first_visible_x)
19000 this_line_min_pos = row->start.pos;
19001 move_it_in_display_line_to (it, ZV, it->first_visible_x,
19002 MOVE_TO_POS | MOVE_TO_X);
19003 /* Record the smallest positions seen while we moved over
19004 display elements that are not visible. This is needed by
19005 redisplay_internal for optimizing the case where the cursor
19006 stays inside the same line. The rest of this function only
19007 considers positions that are actually displayed, so
19008 RECORD_MAX_MIN_POS will not otherwise record positions that
19009 are hscrolled to the left of the left edge of the window. */
19010 min_pos = CHARPOS (this_line_min_pos);
19011 min_bpos = BYTEPOS (this_line_min_pos);
19013 else
19015 /* We only do this when not calling `move_it_in_display_line_to'
19016 above, because move_it_in_display_line_to calls
19017 handle_line_prefix itself. */
19018 handle_line_prefix (it);
19021 /* Get the initial row height. This is either the height of the
19022 text hscrolled, if there is any, or zero. */
19023 row->ascent = it->max_ascent;
19024 row->height = it->max_ascent + it->max_descent;
19025 row->phys_ascent = it->max_phys_ascent;
19026 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19027 row->extra_line_spacing = it->max_extra_line_spacing;
19029 /* Utility macro to record max and min buffer positions seen until now. */
19030 #define RECORD_MAX_MIN_POS(IT) \
19031 do \
19033 int composition_p = !STRINGP ((IT)->string) \
19034 && ((IT)->what == IT_COMPOSITION); \
19035 EMACS_INT current_pos = \
19036 composition_p ? (IT)->cmp_it.charpos \
19037 : IT_CHARPOS (*(IT)); \
19038 EMACS_INT current_bpos = \
19039 composition_p ? CHAR_TO_BYTE (current_pos) \
19040 : IT_BYTEPOS (*(IT)); \
19041 if (current_pos < min_pos) \
19043 min_pos = current_pos; \
19044 min_bpos = current_bpos; \
19046 if (IT_CHARPOS (*it) > max_pos) \
19048 max_pos = IT_CHARPOS (*it); \
19049 max_bpos = IT_BYTEPOS (*it); \
19052 while (0)
19054 /* Loop generating characters. The loop is left with IT on the next
19055 character to display. */
19056 while (1)
19058 int n_glyphs_before, hpos_before, x_before;
19059 int x, nglyphs;
19060 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19062 /* Retrieve the next thing to display. Value is zero if end of
19063 buffer reached. */
19064 if (!get_next_display_element (it))
19066 /* Maybe add a space at the end of this line that is used to
19067 display the cursor there under X. Set the charpos of the
19068 first glyph of blank lines not corresponding to any text
19069 to -1. */
19070 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19071 row->exact_window_width_line_p = 1;
19072 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19073 || row->used[TEXT_AREA] == 0)
19075 row->glyphs[TEXT_AREA]->charpos = -1;
19076 row->displays_text_p = 0;
19078 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19079 && (!MINI_WINDOW_P (it->w)
19080 || (minibuf_level && EQ (it->window, minibuf_window))))
19081 row->indicate_empty_line_p = 1;
19084 it->continuation_lines_width = 0;
19085 row->ends_at_zv_p = 1;
19086 /* A row that displays right-to-left text must always have
19087 its last face extended all the way to the end of line,
19088 even if this row ends in ZV, because we still write to
19089 the screen left to right. We also need to extend the
19090 last face if the default face is remapped to some
19091 different face, otherwise the functions that clear
19092 portions of the screen will clear with the default face's
19093 background color. */
19094 if (row->reversed_p
19095 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19096 extend_face_to_end_of_line (it);
19097 break;
19100 /* Now, get the metrics of what we want to display. This also
19101 generates glyphs in `row' (which is IT->glyph_row). */
19102 n_glyphs_before = row->used[TEXT_AREA];
19103 x = it->current_x;
19105 /* Remember the line height so far in case the next element doesn't
19106 fit on the line. */
19107 if (it->line_wrap != TRUNCATE)
19109 ascent = it->max_ascent;
19110 descent = it->max_descent;
19111 phys_ascent = it->max_phys_ascent;
19112 phys_descent = it->max_phys_descent;
19114 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19116 if (IT_DISPLAYING_WHITESPACE (it))
19117 may_wrap = 1;
19118 else if (may_wrap)
19120 SAVE_IT (wrap_it, *it, wrap_data);
19121 wrap_x = x;
19122 wrap_row_used = row->used[TEXT_AREA];
19123 wrap_row_ascent = row->ascent;
19124 wrap_row_height = row->height;
19125 wrap_row_phys_ascent = row->phys_ascent;
19126 wrap_row_phys_height = row->phys_height;
19127 wrap_row_extra_line_spacing = row->extra_line_spacing;
19128 wrap_row_min_pos = min_pos;
19129 wrap_row_min_bpos = min_bpos;
19130 wrap_row_max_pos = max_pos;
19131 wrap_row_max_bpos = max_bpos;
19132 may_wrap = 0;
19137 PRODUCE_GLYPHS (it);
19139 /* If this display element was in marginal areas, continue with
19140 the next one. */
19141 if (it->area != TEXT_AREA)
19143 row->ascent = max (row->ascent, it->max_ascent);
19144 row->height = max (row->height, it->max_ascent + it->max_descent);
19145 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19146 row->phys_height = max (row->phys_height,
19147 it->max_phys_ascent + it->max_phys_descent);
19148 row->extra_line_spacing = max (row->extra_line_spacing,
19149 it->max_extra_line_spacing);
19150 set_iterator_to_next (it, 1);
19151 continue;
19154 /* Does the display element fit on the line? If we truncate
19155 lines, we should draw past the right edge of the window. If
19156 we don't truncate, we want to stop so that we can display the
19157 continuation glyph before the right margin. If lines are
19158 continued, there are two possible strategies for characters
19159 resulting in more than 1 glyph (e.g. tabs): Display as many
19160 glyphs as possible in this line and leave the rest for the
19161 continuation line, or display the whole element in the next
19162 line. Original redisplay did the former, so we do it also. */
19163 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19164 hpos_before = it->hpos;
19165 x_before = x;
19167 if (/* Not a newline. */
19168 nglyphs > 0
19169 /* Glyphs produced fit entirely in the line. */
19170 && it->current_x < it->last_visible_x)
19172 it->hpos += nglyphs;
19173 row->ascent = max (row->ascent, it->max_ascent);
19174 row->height = max (row->height, it->max_ascent + it->max_descent);
19175 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19176 row->phys_height = max (row->phys_height,
19177 it->max_phys_ascent + it->max_phys_descent);
19178 row->extra_line_spacing = max (row->extra_line_spacing,
19179 it->max_extra_line_spacing);
19180 if (it->current_x - it->pixel_width < it->first_visible_x)
19181 row->x = x - it->first_visible_x;
19182 /* Record the maximum and minimum buffer positions seen so
19183 far in glyphs that will be displayed by this row. */
19184 if (it->bidi_p)
19185 RECORD_MAX_MIN_POS (it);
19187 else
19189 int i, new_x;
19190 struct glyph *glyph;
19192 for (i = 0; i < nglyphs; ++i, x = new_x)
19194 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19195 new_x = x + glyph->pixel_width;
19197 if (/* Lines are continued. */
19198 it->line_wrap != TRUNCATE
19199 && (/* Glyph doesn't fit on the line. */
19200 new_x > it->last_visible_x
19201 /* Or it fits exactly on a window system frame. */
19202 || (new_x == it->last_visible_x
19203 && FRAME_WINDOW_P (it->f))))
19205 /* End of a continued line. */
19207 if (it->hpos == 0
19208 || (new_x == it->last_visible_x
19209 && FRAME_WINDOW_P (it->f)))
19211 /* Current glyph is the only one on the line or
19212 fits exactly on the line. We must continue
19213 the line because we can't draw the cursor
19214 after the glyph. */
19215 row->continued_p = 1;
19216 it->current_x = new_x;
19217 it->continuation_lines_width += new_x;
19218 ++it->hpos;
19219 if (i == nglyphs - 1)
19221 /* If line-wrap is on, check if a previous
19222 wrap point was found. */
19223 if (wrap_row_used > 0
19224 /* Even if there is a previous wrap
19225 point, continue the line here as
19226 usual, if (i) the previous character
19227 was a space or tab AND (ii) the
19228 current character is not. */
19229 && (!may_wrap
19230 || IT_DISPLAYING_WHITESPACE (it)))
19231 goto back_to_wrap;
19233 /* Record the maximum and minimum buffer
19234 positions seen so far in glyphs that will be
19235 displayed by this row. */
19236 if (it->bidi_p)
19237 RECORD_MAX_MIN_POS (it);
19238 set_iterator_to_next (it, 1);
19239 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19241 if (!get_next_display_element (it))
19243 row->exact_window_width_line_p = 1;
19244 it->continuation_lines_width = 0;
19245 row->continued_p = 0;
19246 row->ends_at_zv_p = 1;
19248 else if (ITERATOR_AT_END_OF_LINE_P (it))
19250 row->continued_p = 0;
19251 row->exact_window_width_line_p = 1;
19255 else if (it->bidi_p)
19256 RECORD_MAX_MIN_POS (it);
19258 else if (CHAR_GLYPH_PADDING_P (*glyph)
19259 && !FRAME_WINDOW_P (it->f))
19261 /* A padding glyph that doesn't fit on this line.
19262 This means the whole character doesn't fit
19263 on the line. */
19264 if (row->reversed_p)
19265 unproduce_glyphs (it, row->used[TEXT_AREA]
19266 - n_glyphs_before);
19267 row->used[TEXT_AREA] = n_glyphs_before;
19269 /* Fill the rest of the row with continuation
19270 glyphs like in 20.x. */
19271 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19272 < row->glyphs[1 + TEXT_AREA])
19273 produce_special_glyphs (it, IT_CONTINUATION);
19275 row->continued_p = 1;
19276 it->current_x = x_before;
19277 it->continuation_lines_width += x_before;
19279 /* Restore the height to what it was before the
19280 element not fitting on the line. */
19281 it->max_ascent = ascent;
19282 it->max_descent = descent;
19283 it->max_phys_ascent = phys_ascent;
19284 it->max_phys_descent = phys_descent;
19286 else if (wrap_row_used > 0)
19288 back_to_wrap:
19289 if (row->reversed_p)
19290 unproduce_glyphs (it,
19291 row->used[TEXT_AREA] - wrap_row_used);
19292 RESTORE_IT (it, &wrap_it, wrap_data);
19293 it->continuation_lines_width += wrap_x;
19294 row->used[TEXT_AREA] = wrap_row_used;
19295 row->ascent = wrap_row_ascent;
19296 row->height = wrap_row_height;
19297 row->phys_ascent = wrap_row_phys_ascent;
19298 row->phys_height = wrap_row_phys_height;
19299 row->extra_line_spacing = wrap_row_extra_line_spacing;
19300 min_pos = wrap_row_min_pos;
19301 min_bpos = wrap_row_min_bpos;
19302 max_pos = wrap_row_max_pos;
19303 max_bpos = wrap_row_max_bpos;
19304 row->continued_p = 1;
19305 row->ends_at_zv_p = 0;
19306 row->exact_window_width_line_p = 0;
19307 it->continuation_lines_width += x;
19309 /* Make sure that a non-default face is extended
19310 up to the right margin of the window. */
19311 extend_face_to_end_of_line (it);
19313 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19315 /* A TAB that extends past the right edge of the
19316 window. This produces a single glyph on
19317 window system frames. We leave the glyph in
19318 this row and let it fill the row, but don't
19319 consume the TAB. */
19320 it->continuation_lines_width += it->last_visible_x;
19321 row->ends_in_middle_of_char_p = 1;
19322 row->continued_p = 1;
19323 glyph->pixel_width = it->last_visible_x - x;
19324 it->starts_in_middle_of_char_p = 1;
19326 else
19328 /* Something other than a TAB that draws past
19329 the right edge of the window. Restore
19330 positions to values before the element. */
19331 if (row->reversed_p)
19332 unproduce_glyphs (it, row->used[TEXT_AREA]
19333 - (n_glyphs_before + i));
19334 row->used[TEXT_AREA] = n_glyphs_before + i;
19336 /* Display continuation glyphs. */
19337 if (!FRAME_WINDOW_P (it->f))
19338 produce_special_glyphs (it, IT_CONTINUATION);
19339 row->continued_p = 1;
19341 it->current_x = x_before;
19342 it->continuation_lines_width += x;
19343 extend_face_to_end_of_line (it);
19345 if (nglyphs > 1 && i > 0)
19347 row->ends_in_middle_of_char_p = 1;
19348 it->starts_in_middle_of_char_p = 1;
19351 /* Restore the height to what it was before the
19352 element not fitting on the line. */
19353 it->max_ascent = ascent;
19354 it->max_descent = descent;
19355 it->max_phys_ascent = phys_ascent;
19356 it->max_phys_descent = phys_descent;
19359 break;
19361 else if (new_x > it->first_visible_x)
19363 /* Increment number of glyphs actually displayed. */
19364 ++it->hpos;
19366 /* Record the maximum and minimum buffer positions
19367 seen so far in glyphs that will be displayed by
19368 this row. */
19369 if (it->bidi_p)
19370 RECORD_MAX_MIN_POS (it);
19372 if (x < it->first_visible_x)
19373 /* Glyph is partially visible, i.e. row starts at
19374 negative X position. */
19375 row->x = x - it->first_visible_x;
19377 else
19379 /* Glyph is completely off the left margin of the
19380 window. This should not happen because of the
19381 move_it_in_display_line at the start of this
19382 function, unless the text display area of the
19383 window is empty. */
19384 xassert (it->first_visible_x <= it->last_visible_x);
19387 /* Even if this display element produced no glyphs at all,
19388 we want to record its position. */
19389 if (it->bidi_p && nglyphs == 0)
19390 RECORD_MAX_MIN_POS (it);
19392 row->ascent = max (row->ascent, it->max_ascent);
19393 row->height = max (row->height, it->max_ascent + it->max_descent);
19394 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19395 row->phys_height = max (row->phys_height,
19396 it->max_phys_ascent + it->max_phys_descent);
19397 row->extra_line_spacing = max (row->extra_line_spacing,
19398 it->max_extra_line_spacing);
19400 /* End of this display line if row is continued. */
19401 if (row->continued_p || row->ends_at_zv_p)
19402 break;
19405 at_end_of_line:
19406 /* Is this a line end? If yes, we're also done, after making
19407 sure that a non-default face is extended up to the right
19408 margin of the window. */
19409 if (ITERATOR_AT_END_OF_LINE_P (it))
19411 int used_before = row->used[TEXT_AREA];
19413 row->ends_in_newline_from_string_p = STRINGP (it->object);
19415 /* Add a space at the end of the line that is used to
19416 display the cursor there. */
19417 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19418 append_space_for_newline (it, 0);
19420 /* Extend the face to the end of the line. */
19421 extend_face_to_end_of_line (it);
19423 /* Make sure we have the position. */
19424 if (used_before == 0)
19425 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19427 /* Record the position of the newline, for use in
19428 find_row_edges. */
19429 it->eol_pos = it->current.pos;
19431 /* Consume the line end. This skips over invisible lines. */
19432 set_iterator_to_next (it, 1);
19433 it->continuation_lines_width = 0;
19434 break;
19437 /* Proceed with next display element. Note that this skips
19438 over lines invisible because of selective display. */
19439 set_iterator_to_next (it, 1);
19441 /* If we truncate lines, we are done when the last displayed
19442 glyphs reach past the right margin of the window. */
19443 if (it->line_wrap == TRUNCATE
19444 && (FRAME_WINDOW_P (it->f)
19445 ? (it->current_x >= it->last_visible_x)
19446 : (it->current_x > it->last_visible_x)))
19448 /* Maybe add truncation glyphs. */
19449 if (!FRAME_WINDOW_P (it->f))
19451 int i, n;
19453 if (!row->reversed_p)
19455 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19456 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19457 break;
19459 else
19461 for (i = 0; i < row->used[TEXT_AREA]; i++)
19462 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19463 break;
19464 /* Remove any padding glyphs at the front of ROW, to
19465 make room for the truncation glyphs we will be
19466 adding below. The loop below always inserts at
19467 least one truncation glyph, so also remove the
19468 last glyph added to ROW. */
19469 unproduce_glyphs (it, i + 1);
19470 /* Adjust i for the loop below. */
19471 i = row->used[TEXT_AREA] - (i + 1);
19474 for (n = row->used[TEXT_AREA]; i < n; ++i)
19476 row->used[TEXT_AREA] = i;
19477 produce_special_glyphs (it, IT_TRUNCATION);
19480 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19482 /* Don't truncate if we can overflow newline into fringe. */
19483 if (!get_next_display_element (it))
19485 it->continuation_lines_width = 0;
19486 row->ends_at_zv_p = 1;
19487 row->exact_window_width_line_p = 1;
19488 break;
19490 if (ITERATOR_AT_END_OF_LINE_P (it))
19492 row->exact_window_width_line_p = 1;
19493 goto at_end_of_line;
19497 row->truncated_on_right_p = 1;
19498 it->continuation_lines_width = 0;
19499 reseat_at_next_visible_line_start (it, 0);
19500 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19501 it->hpos = hpos_before;
19502 it->current_x = x_before;
19503 break;
19507 if (wrap_data)
19508 bidi_unshelve_cache (wrap_data, 1);
19510 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19511 at the left window margin. */
19512 if (it->first_visible_x
19513 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19515 if (!FRAME_WINDOW_P (it->f))
19516 insert_left_trunc_glyphs (it);
19517 row->truncated_on_left_p = 1;
19520 /* Remember the position at which this line ends.
19522 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19523 cannot be before the call to find_row_edges below, since that is
19524 where these positions are determined. */
19525 row->end = it->current;
19526 if (!it->bidi_p)
19528 row->minpos = row->start.pos;
19529 row->maxpos = row->end.pos;
19531 else
19533 /* ROW->minpos and ROW->maxpos must be the smallest and
19534 `1 + the largest' buffer positions in ROW. But if ROW was
19535 bidi-reordered, these two positions can be anywhere in the
19536 row, so we must determine them now. */
19537 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19540 /* If the start of this line is the overlay arrow-position, then
19541 mark this glyph row as the one containing the overlay arrow.
19542 This is clearly a mess with variable size fonts. It would be
19543 better to let it be displayed like cursors under X. */
19544 if ((row->displays_text_p || !overlay_arrow_seen)
19545 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19546 !NILP (overlay_arrow_string)))
19548 /* Overlay arrow in window redisplay is a fringe bitmap. */
19549 if (STRINGP (overlay_arrow_string))
19551 struct glyph_row *arrow_row
19552 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19553 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19554 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19555 struct glyph *p = row->glyphs[TEXT_AREA];
19556 struct glyph *p2, *end;
19558 /* Copy the arrow glyphs. */
19559 while (glyph < arrow_end)
19560 *p++ = *glyph++;
19562 /* Throw away padding glyphs. */
19563 p2 = p;
19564 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19565 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19566 ++p2;
19567 if (p2 > p)
19569 while (p2 < end)
19570 *p++ = *p2++;
19571 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19574 else
19576 xassert (INTEGERP (overlay_arrow_string));
19577 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19579 overlay_arrow_seen = 1;
19582 /* Highlight trailing whitespace. */
19583 if (!NILP (Vshow_trailing_whitespace))
19584 highlight_trailing_whitespace (it->f, it->glyph_row);
19586 /* Compute pixel dimensions of this line. */
19587 compute_line_metrics (it);
19589 /* Implementation note: No changes in the glyphs of ROW or in their
19590 faces can be done past this point, because compute_line_metrics
19591 computes ROW's hash value and stores it within the glyph_row
19592 structure. */
19594 /* Record whether this row ends inside an ellipsis. */
19595 row->ends_in_ellipsis_p
19596 = (it->method == GET_FROM_DISPLAY_VECTOR
19597 && it->ellipsis_p);
19599 /* Save fringe bitmaps in this row. */
19600 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19601 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19602 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19603 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19605 it->left_user_fringe_bitmap = 0;
19606 it->left_user_fringe_face_id = 0;
19607 it->right_user_fringe_bitmap = 0;
19608 it->right_user_fringe_face_id = 0;
19610 /* Maybe set the cursor. */
19611 cvpos = it->w->cursor.vpos;
19612 if ((cvpos < 0
19613 /* In bidi-reordered rows, keep checking for proper cursor
19614 position even if one has been found already, because buffer
19615 positions in such rows change non-linearly with ROW->VPOS,
19616 when a line is continued. One exception: when we are at ZV,
19617 display cursor on the first suitable glyph row, since all
19618 the empty rows after that also have their position set to ZV. */
19619 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19620 lines' rows is implemented for bidi-reordered rows. */
19621 || (it->bidi_p
19622 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19623 && PT >= MATRIX_ROW_START_CHARPOS (row)
19624 && PT <= MATRIX_ROW_END_CHARPOS (row)
19625 && cursor_row_p (row))
19626 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19628 /* Prepare for the next line. This line starts horizontally at (X
19629 HPOS) = (0 0). Vertical positions are incremented. As a
19630 convenience for the caller, IT->glyph_row is set to the next
19631 row to be used. */
19632 it->current_x = it->hpos = 0;
19633 it->current_y += row->height;
19634 SET_TEXT_POS (it->eol_pos, 0, 0);
19635 ++it->vpos;
19636 ++it->glyph_row;
19637 /* The next row should by default use the same value of the
19638 reversed_p flag as this one. set_iterator_to_next decides when
19639 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19640 the flag accordingly. */
19641 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19642 it->glyph_row->reversed_p = row->reversed_p;
19643 it->start = row->end;
19644 return row->displays_text_p;
19646 #undef RECORD_MAX_MIN_POS
19649 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19650 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19651 doc: /* Return paragraph direction at point in BUFFER.
19652 Value is either `left-to-right' or `right-to-left'.
19653 If BUFFER is omitted or nil, it defaults to the current buffer.
19655 Paragraph direction determines how the text in the paragraph is displayed.
19656 In left-to-right paragraphs, text begins at the left margin of the window
19657 and the reading direction is generally left to right. In right-to-left
19658 paragraphs, text begins at the right margin and is read from right to left.
19660 See also `bidi-paragraph-direction'. */)
19661 (Lisp_Object buffer)
19663 struct buffer *buf = current_buffer;
19664 struct buffer *old = buf;
19666 if (! NILP (buffer))
19668 CHECK_BUFFER (buffer);
19669 buf = XBUFFER (buffer);
19672 if (NILP (BVAR (buf, bidi_display_reordering))
19673 || NILP (BVAR (buf, enable_multibyte_characters))
19674 /* When we are loading loadup.el, the character property tables
19675 needed for bidi iteration are not yet available. */
19676 || !NILP (Vpurify_flag))
19677 return Qleft_to_right;
19678 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19679 return BVAR (buf, bidi_paragraph_direction);
19680 else
19682 /* Determine the direction from buffer text. We could try to
19683 use current_matrix if it is up to date, but this seems fast
19684 enough as it is. */
19685 struct bidi_it itb;
19686 EMACS_INT pos = BUF_PT (buf);
19687 EMACS_INT bytepos = BUF_PT_BYTE (buf);
19688 int c;
19689 void *itb_data = bidi_shelve_cache ();
19691 set_buffer_temp (buf);
19692 /* bidi_paragraph_init finds the base direction of the paragraph
19693 by searching forward from paragraph start. We need the base
19694 direction of the current or _previous_ paragraph, so we need
19695 to make sure we are within that paragraph. To that end, find
19696 the previous non-empty line. */
19697 if (pos >= ZV && pos > BEGV)
19699 pos--;
19700 bytepos = CHAR_TO_BYTE (pos);
19702 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19703 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19705 while ((c = FETCH_BYTE (bytepos)) == '\n'
19706 || c == ' ' || c == '\t' || c == '\f')
19708 if (bytepos <= BEGV_BYTE)
19709 break;
19710 bytepos--;
19711 pos--;
19713 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19714 bytepos--;
19716 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19717 itb.paragraph_dir = NEUTRAL_DIR;
19718 itb.string.s = NULL;
19719 itb.string.lstring = Qnil;
19720 itb.string.bufpos = 0;
19721 itb.string.unibyte = 0;
19722 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19723 bidi_unshelve_cache (itb_data, 0);
19724 set_buffer_temp (old);
19725 switch (itb.paragraph_dir)
19727 case L2R:
19728 return Qleft_to_right;
19729 break;
19730 case R2L:
19731 return Qright_to_left;
19732 break;
19733 default:
19734 abort ();
19741 /***********************************************************************
19742 Menu Bar
19743 ***********************************************************************/
19745 /* Redisplay the menu bar in the frame for window W.
19747 The menu bar of X frames that don't have X toolkit support is
19748 displayed in a special window W->frame->menu_bar_window.
19750 The menu bar of terminal frames is treated specially as far as
19751 glyph matrices are concerned. Menu bar lines are not part of
19752 windows, so the update is done directly on the frame matrix rows
19753 for the menu bar. */
19755 static void
19756 display_menu_bar (struct window *w)
19758 struct frame *f = XFRAME (WINDOW_FRAME (w));
19759 struct it it;
19760 Lisp_Object items;
19761 int i;
19763 /* Don't do all this for graphical frames. */
19764 #ifdef HAVE_NTGUI
19765 if (FRAME_W32_P (f))
19766 return;
19767 #endif
19768 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
19769 if (FRAME_X_P (f))
19770 return;
19771 #endif
19773 #ifdef HAVE_NS
19774 if (FRAME_NS_P (f))
19775 return;
19776 #endif /* HAVE_NS */
19778 #ifdef USE_X_TOOLKIT
19779 xassert (!FRAME_WINDOW_P (f));
19780 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
19781 it.first_visible_x = 0;
19782 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19783 #else /* not USE_X_TOOLKIT */
19784 if (FRAME_WINDOW_P (f))
19786 /* Menu bar lines are displayed in the desired matrix of the
19787 dummy window menu_bar_window. */
19788 struct window *menu_w;
19789 xassert (WINDOWP (f->menu_bar_window));
19790 menu_w = XWINDOW (f->menu_bar_window);
19791 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
19792 MENU_FACE_ID);
19793 it.first_visible_x = 0;
19794 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19796 else
19798 /* This is a TTY frame, i.e. character hpos/vpos are used as
19799 pixel x/y. */
19800 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
19801 MENU_FACE_ID);
19802 it.first_visible_x = 0;
19803 it.last_visible_x = FRAME_COLS (f);
19805 #endif /* not USE_X_TOOLKIT */
19807 /* FIXME: This should be controlled by a user option. See the
19808 comments in redisplay_tool_bar and display_mode_line about
19809 this. */
19810 it.paragraph_embedding = L2R;
19812 if (! mode_line_inverse_video)
19813 /* Force the menu-bar to be displayed in the default face. */
19814 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19816 /* Clear all rows of the menu bar. */
19817 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
19819 struct glyph_row *row = it.glyph_row + i;
19820 clear_glyph_row (row);
19821 row->enabled_p = 1;
19822 row->full_width_p = 1;
19825 /* Display all items of the menu bar. */
19826 items = FRAME_MENU_BAR_ITEMS (it.f);
19827 for (i = 0; i < ASIZE (items); i += 4)
19829 Lisp_Object string;
19831 /* Stop at nil string. */
19832 string = AREF (items, i + 1);
19833 if (NILP (string))
19834 break;
19836 /* Remember where item was displayed. */
19837 ASET (items, i + 3, make_number (it.hpos));
19839 /* Display the item, pad with one space. */
19840 if (it.current_x < it.last_visible_x)
19841 display_string (NULL, string, Qnil, 0, 0, &it,
19842 SCHARS (string) + 1, 0, 0, -1);
19845 /* Fill out the line with spaces. */
19846 if (it.current_x < it.last_visible_x)
19847 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
19849 /* Compute the total height of the lines. */
19850 compute_line_metrics (&it);
19855 /***********************************************************************
19856 Mode Line
19857 ***********************************************************************/
19859 /* Redisplay mode lines in the window tree whose root is WINDOW. If
19860 FORCE is non-zero, redisplay mode lines unconditionally.
19861 Otherwise, redisplay only mode lines that are garbaged. Value is
19862 the number of windows whose mode lines were redisplayed. */
19864 static int
19865 redisplay_mode_lines (Lisp_Object window, int force)
19867 int nwindows = 0;
19869 while (!NILP (window))
19871 struct window *w = XWINDOW (window);
19873 if (WINDOWP (w->hchild))
19874 nwindows += redisplay_mode_lines (w->hchild, force);
19875 else if (WINDOWP (w->vchild))
19876 nwindows += redisplay_mode_lines (w->vchild, force);
19877 else if (force
19878 || FRAME_GARBAGED_P (XFRAME (w->frame))
19879 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
19881 struct text_pos lpoint;
19882 struct buffer *old = current_buffer;
19884 /* Set the window's buffer for the mode line display. */
19885 SET_TEXT_POS (lpoint, PT, PT_BYTE);
19886 set_buffer_internal_1 (XBUFFER (w->buffer));
19888 /* Point refers normally to the selected window. For any
19889 other window, set up appropriate value. */
19890 if (!EQ (window, selected_window))
19892 struct text_pos pt;
19894 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
19895 if (CHARPOS (pt) < BEGV)
19896 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
19897 else if (CHARPOS (pt) > (ZV - 1))
19898 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
19899 else
19900 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
19903 /* Display mode lines. */
19904 clear_glyph_matrix (w->desired_matrix);
19905 if (display_mode_lines (w))
19907 ++nwindows;
19908 w->must_be_updated_p = 1;
19911 /* Restore old settings. */
19912 set_buffer_internal_1 (old);
19913 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
19916 window = w->next;
19919 return nwindows;
19923 /* Display the mode and/or header line of window W. Value is the
19924 sum number of mode lines and header lines displayed. */
19926 static int
19927 display_mode_lines (struct window *w)
19929 Lisp_Object old_selected_window, old_selected_frame;
19930 int n = 0;
19932 old_selected_frame = selected_frame;
19933 selected_frame = w->frame;
19934 old_selected_window = selected_window;
19935 XSETWINDOW (selected_window, w);
19937 /* These will be set while the mode line specs are processed. */
19938 line_number_displayed = 0;
19939 w->column_number_displayed = Qnil;
19941 if (WINDOW_WANTS_MODELINE_P (w))
19943 struct window *sel_w = XWINDOW (old_selected_window);
19945 /* Select mode line face based on the real selected window. */
19946 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
19947 BVAR (current_buffer, mode_line_format));
19948 ++n;
19951 if (WINDOW_WANTS_HEADER_LINE_P (w))
19953 display_mode_line (w, HEADER_LINE_FACE_ID,
19954 BVAR (current_buffer, header_line_format));
19955 ++n;
19958 selected_frame = old_selected_frame;
19959 selected_window = old_selected_window;
19960 return n;
19964 /* Display mode or header line of window W. FACE_ID specifies which
19965 line to display; it is either MODE_LINE_FACE_ID or
19966 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
19967 display. Value is the pixel height of the mode/header line
19968 displayed. */
19970 static int
19971 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
19973 struct it it;
19974 struct face *face;
19975 int count = SPECPDL_INDEX ();
19977 init_iterator (&it, w, -1, -1, NULL, face_id);
19978 /* Don't extend on a previously drawn mode-line.
19979 This may happen if called from pos_visible_p. */
19980 it.glyph_row->enabled_p = 0;
19981 prepare_desired_row (it.glyph_row);
19983 it.glyph_row->mode_line_p = 1;
19985 if (! mode_line_inverse_video)
19986 /* Force the mode-line to be displayed in the default face. */
19987 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19989 /* FIXME: This should be controlled by a user option. But
19990 supporting such an option is not trivial, since the mode line is
19991 made up of many separate strings. */
19992 it.paragraph_embedding = L2R;
19994 record_unwind_protect (unwind_format_mode_line,
19995 format_mode_line_unwind_data (NULL, Qnil, 0));
19997 mode_line_target = MODE_LINE_DISPLAY;
19999 /* Temporarily make frame's keyboard the current kboard so that
20000 kboard-local variables in the mode_line_format will get the right
20001 values. */
20002 push_kboard (FRAME_KBOARD (it.f));
20003 record_unwind_save_match_data ();
20004 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20005 pop_kboard ();
20007 unbind_to (count, Qnil);
20009 /* Fill up with spaces. */
20010 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20012 compute_line_metrics (&it);
20013 it.glyph_row->full_width_p = 1;
20014 it.glyph_row->continued_p = 0;
20015 it.glyph_row->truncated_on_left_p = 0;
20016 it.glyph_row->truncated_on_right_p = 0;
20018 /* Make a 3D mode-line have a shadow at its right end. */
20019 face = FACE_FROM_ID (it.f, face_id);
20020 extend_face_to_end_of_line (&it);
20021 if (face->box != FACE_NO_BOX)
20023 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20024 + it.glyph_row->used[TEXT_AREA] - 1);
20025 last->right_box_line_p = 1;
20028 return it.glyph_row->height;
20031 /* Move element ELT in LIST to the front of LIST.
20032 Return the updated list. */
20034 static Lisp_Object
20035 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20037 register Lisp_Object tail, prev;
20038 register Lisp_Object tem;
20040 tail = list;
20041 prev = Qnil;
20042 while (CONSP (tail))
20044 tem = XCAR (tail);
20046 if (EQ (elt, tem))
20048 /* Splice out the link TAIL. */
20049 if (NILP (prev))
20050 list = XCDR (tail);
20051 else
20052 Fsetcdr (prev, XCDR (tail));
20054 /* Now make it the first. */
20055 Fsetcdr (tail, list);
20056 return tail;
20058 else
20059 prev = tail;
20060 tail = XCDR (tail);
20061 QUIT;
20064 /* Not found--return unchanged LIST. */
20065 return list;
20068 /* Contribute ELT to the mode line for window IT->w. How it
20069 translates into text depends on its data type.
20071 IT describes the display environment in which we display, as usual.
20073 DEPTH is the depth in recursion. It is used to prevent
20074 infinite recursion here.
20076 FIELD_WIDTH is the number of characters the display of ELT should
20077 occupy in the mode line, and PRECISION is the maximum number of
20078 characters to display from ELT's representation. See
20079 display_string for details.
20081 Returns the hpos of the end of the text generated by ELT.
20083 PROPS is a property list to add to any string we encounter.
20085 If RISKY is nonzero, remove (disregard) any properties in any string
20086 we encounter, and ignore :eval and :propertize.
20088 The global variable `mode_line_target' determines whether the
20089 output is passed to `store_mode_line_noprop',
20090 `store_mode_line_string', or `display_string'. */
20092 static int
20093 display_mode_element (struct it *it, int depth, int field_width, int precision,
20094 Lisp_Object elt, Lisp_Object props, int risky)
20096 int n = 0, field, prec;
20097 int literal = 0;
20099 tail_recurse:
20100 if (depth > 100)
20101 elt = build_string ("*too-deep*");
20103 depth++;
20105 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
20107 case Lisp_String:
20109 /* A string: output it and check for %-constructs within it. */
20110 unsigned char c;
20111 EMACS_INT offset = 0;
20113 if (SCHARS (elt) > 0
20114 && (!NILP (props) || risky))
20116 Lisp_Object oprops, aelt;
20117 oprops = Ftext_properties_at (make_number (0), elt);
20119 /* If the starting string's properties are not what
20120 we want, translate the string. Also, if the string
20121 is risky, do that anyway. */
20123 if (NILP (Fequal (props, oprops)) || risky)
20125 /* If the starting string has properties,
20126 merge the specified ones onto the existing ones. */
20127 if (! NILP (oprops) && !risky)
20129 Lisp_Object tem;
20131 oprops = Fcopy_sequence (oprops);
20132 tem = props;
20133 while (CONSP (tem))
20135 oprops = Fplist_put (oprops, XCAR (tem),
20136 XCAR (XCDR (tem)));
20137 tem = XCDR (XCDR (tem));
20139 props = oprops;
20142 aelt = Fassoc (elt, mode_line_proptrans_alist);
20143 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20145 /* AELT is what we want. Move it to the front
20146 without consing. */
20147 elt = XCAR (aelt);
20148 mode_line_proptrans_alist
20149 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20151 else
20153 Lisp_Object tem;
20155 /* If AELT has the wrong props, it is useless.
20156 so get rid of it. */
20157 if (! NILP (aelt))
20158 mode_line_proptrans_alist
20159 = Fdelq (aelt, mode_line_proptrans_alist);
20161 elt = Fcopy_sequence (elt);
20162 Fset_text_properties (make_number (0), Flength (elt),
20163 props, elt);
20164 /* Add this item to mode_line_proptrans_alist. */
20165 mode_line_proptrans_alist
20166 = Fcons (Fcons (elt, props),
20167 mode_line_proptrans_alist);
20168 /* Truncate mode_line_proptrans_alist
20169 to at most 50 elements. */
20170 tem = Fnthcdr (make_number (50),
20171 mode_line_proptrans_alist);
20172 if (! NILP (tem))
20173 XSETCDR (tem, Qnil);
20178 offset = 0;
20180 if (literal)
20182 prec = precision - n;
20183 switch (mode_line_target)
20185 case MODE_LINE_NOPROP:
20186 case MODE_LINE_TITLE:
20187 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20188 break;
20189 case MODE_LINE_STRING:
20190 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20191 break;
20192 case MODE_LINE_DISPLAY:
20193 n += display_string (NULL, elt, Qnil, 0, 0, it,
20194 0, prec, 0, STRING_MULTIBYTE (elt));
20195 break;
20198 break;
20201 /* Handle the non-literal case. */
20203 while ((precision <= 0 || n < precision)
20204 && SREF (elt, offset) != 0
20205 && (mode_line_target != MODE_LINE_DISPLAY
20206 || it->current_x < it->last_visible_x))
20208 EMACS_INT last_offset = offset;
20210 /* Advance to end of string or next format specifier. */
20211 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20214 if (offset - 1 != last_offset)
20216 EMACS_INT nchars, nbytes;
20218 /* Output to end of string or up to '%'. Field width
20219 is length of string. Don't output more than
20220 PRECISION allows us. */
20221 offset--;
20223 prec = c_string_width (SDATA (elt) + last_offset,
20224 offset - last_offset, precision - n,
20225 &nchars, &nbytes);
20227 switch (mode_line_target)
20229 case MODE_LINE_NOPROP:
20230 case MODE_LINE_TITLE:
20231 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20232 break;
20233 case MODE_LINE_STRING:
20235 EMACS_INT bytepos = last_offset;
20236 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
20237 EMACS_INT endpos = (precision <= 0
20238 ? string_byte_to_char (elt, offset)
20239 : charpos + nchars);
20241 n += store_mode_line_string (NULL,
20242 Fsubstring (elt, make_number (charpos),
20243 make_number (endpos)),
20244 0, 0, 0, Qnil);
20246 break;
20247 case MODE_LINE_DISPLAY:
20249 EMACS_INT bytepos = last_offset;
20250 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
20252 if (precision <= 0)
20253 nchars = string_byte_to_char (elt, offset) - charpos;
20254 n += display_string (NULL, elt, Qnil, 0, charpos,
20255 it, 0, nchars, 0,
20256 STRING_MULTIBYTE (elt));
20258 break;
20261 else /* c == '%' */
20263 EMACS_INT percent_position = offset;
20265 /* Get the specified minimum width. Zero means
20266 don't pad. */
20267 field = 0;
20268 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20269 field = field * 10 + c - '0';
20271 /* Don't pad beyond the total padding allowed. */
20272 if (field_width - n > 0 && field > field_width - n)
20273 field = field_width - n;
20275 /* Note that either PRECISION <= 0 or N < PRECISION. */
20276 prec = precision - n;
20278 if (c == 'M')
20279 n += display_mode_element (it, depth, field, prec,
20280 Vglobal_mode_string, props,
20281 risky);
20282 else if (c != 0)
20284 int multibyte;
20285 EMACS_INT bytepos, charpos;
20286 const char *spec;
20287 Lisp_Object string;
20289 bytepos = percent_position;
20290 charpos = (STRING_MULTIBYTE (elt)
20291 ? string_byte_to_char (elt, bytepos)
20292 : bytepos);
20293 spec = decode_mode_spec (it->w, c, field, &string);
20294 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20296 switch (mode_line_target)
20298 case MODE_LINE_NOPROP:
20299 case MODE_LINE_TITLE:
20300 n += store_mode_line_noprop (spec, field, prec);
20301 break;
20302 case MODE_LINE_STRING:
20304 Lisp_Object tem = build_string (spec);
20305 props = Ftext_properties_at (make_number (charpos), elt);
20306 /* Should only keep face property in props */
20307 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20309 break;
20310 case MODE_LINE_DISPLAY:
20312 int nglyphs_before, nwritten;
20314 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20315 nwritten = display_string (spec, string, elt,
20316 charpos, 0, it,
20317 field, prec, 0,
20318 multibyte);
20320 /* Assign to the glyphs written above the
20321 string where the `%x' came from, position
20322 of the `%'. */
20323 if (nwritten > 0)
20325 struct glyph *glyph
20326 = (it->glyph_row->glyphs[TEXT_AREA]
20327 + nglyphs_before);
20328 int i;
20330 for (i = 0; i < nwritten; ++i)
20332 glyph[i].object = elt;
20333 glyph[i].charpos = charpos;
20336 n += nwritten;
20339 break;
20342 else /* c == 0 */
20343 break;
20347 break;
20349 case Lisp_Symbol:
20350 /* A symbol: process the value of the symbol recursively
20351 as if it appeared here directly. Avoid error if symbol void.
20352 Special case: if value of symbol is a string, output the string
20353 literally. */
20355 register Lisp_Object tem;
20357 /* If the variable is not marked as risky to set
20358 then its contents are risky to use. */
20359 if (NILP (Fget (elt, Qrisky_local_variable)))
20360 risky = 1;
20362 tem = Fboundp (elt);
20363 if (!NILP (tem))
20365 tem = Fsymbol_value (elt);
20366 /* If value is a string, output that string literally:
20367 don't check for % within it. */
20368 if (STRINGP (tem))
20369 literal = 1;
20371 if (!EQ (tem, elt))
20373 /* Give up right away for nil or t. */
20374 elt = tem;
20375 goto tail_recurse;
20379 break;
20381 case Lisp_Cons:
20383 register Lisp_Object car, tem;
20385 /* A cons cell: five distinct cases.
20386 If first element is :eval or :propertize, do something special.
20387 If first element is a string or a cons, process all the elements
20388 and effectively concatenate them.
20389 If first element is a negative number, truncate displaying cdr to
20390 at most that many characters. If positive, pad (with spaces)
20391 to at least that many characters.
20392 If first element is a symbol, process the cadr or caddr recursively
20393 according to whether the symbol's value is non-nil or nil. */
20394 car = XCAR (elt);
20395 if (EQ (car, QCeval))
20397 /* An element of the form (:eval FORM) means evaluate FORM
20398 and use the result as mode line elements. */
20400 if (risky)
20401 break;
20403 if (CONSP (XCDR (elt)))
20405 Lisp_Object spec;
20406 spec = safe_eval (XCAR (XCDR (elt)));
20407 n += display_mode_element (it, depth, field_width - n,
20408 precision - n, spec, props,
20409 risky);
20412 else if (EQ (car, QCpropertize))
20414 /* An element of the form (:propertize ELT PROPS...)
20415 means display ELT but applying properties PROPS. */
20417 if (risky)
20418 break;
20420 if (CONSP (XCDR (elt)))
20421 n += display_mode_element (it, depth, field_width - n,
20422 precision - n, XCAR (XCDR (elt)),
20423 XCDR (XCDR (elt)), risky);
20425 else if (SYMBOLP (car))
20427 tem = Fboundp (car);
20428 elt = XCDR (elt);
20429 if (!CONSP (elt))
20430 goto invalid;
20431 /* elt is now the cdr, and we know it is a cons cell.
20432 Use its car if CAR has a non-nil value. */
20433 if (!NILP (tem))
20435 tem = Fsymbol_value (car);
20436 if (!NILP (tem))
20438 elt = XCAR (elt);
20439 goto tail_recurse;
20442 /* Symbol's value is nil (or symbol is unbound)
20443 Get the cddr of the original list
20444 and if possible find the caddr and use that. */
20445 elt = XCDR (elt);
20446 if (NILP (elt))
20447 break;
20448 else if (!CONSP (elt))
20449 goto invalid;
20450 elt = XCAR (elt);
20451 goto tail_recurse;
20453 else if (INTEGERP (car))
20455 register int lim = XINT (car);
20456 elt = XCDR (elt);
20457 if (lim < 0)
20459 /* Negative int means reduce maximum width. */
20460 if (precision <= 0)
20461 precision = -lim;
20462 else
20463 precision = min (precision, -lim);
20465 else if (lim > 0)
20467 /* Padding specified. Don't let it be more than
20468 current maximum. */
20469 if (precision > 0)
20470 lim = min (precision, lim);
20472 /* If that's more padding than already wanted, queue it.
20473 But don't reduce padding already specified even if
20474 that is beyond the current truncation point. */
20475 field_width = max (lim, field_width);
20477 goto tail_recurse;
20479 else if (STRINGP (car) || CONSP (car))
20481 Lisp_Object halftail = elt;
20482 int len = 0;
20484 while (CONSP (elt)
20485 && (precision <= 0 || n < precision))
20487 n += display_mode_element (it, depth,
20488 /* Do padding only after the last
20489 element in the list. */
20490 (! CONSP (XCDR (elt))
20491 ? field_width - n
20492 : 0),
20493 precision - n, XCAR (elt),
20494 props, risky);
20495 elt = XCDR (elt);
20496 len++;
20497 if ((len & 1) == 0)
20498 halftail = XCDR (halftail);
20499 /* Check for cycle. */
20500 if (EQ (halftail, elt))
20501 break;
20505 break;
20507 default:
20508 invalid:
20509 elt = build_string ("*invalid*");
20510 goto tail_recurse;
20513 /* Pad to FIELD_WIDTH. */
20514 if (field_width > 0 && n < field_width)
20516 switch (mode_line_target)
20518 case MODE_LINE_NOPROP:
20519 case MODE_LINE_TITLE:
20520 n += store_mode_line_noprop ("", field_width - n, 0);
20521 break;
20522 case MODE_LINE_STRING:
20523 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20524 break;
20525 case MODE_LINE_DISPLAY:
20526 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20527 0, 0, 0);
20528 break;
20532 return n;
20535 /* Store a mode-line string element in mode_line_string_list.
20537 If STRING is non-null, display that C string. Otherwise, the Lisp
20538 string LISP_STRING is displayed.
20540 FIELD_WIDTH is the minimum number of output glyphs to produce.
20541 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20542 with spaces. FIELD_WIDTH <= 0 means don't pad.
20544 PRECISION is the maximum number of characters to output from
20545 STRING. PRECISION <= 0 means don't truncate the string.
20547 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20548 properties to the string.
20550 PROPS are the properties to add to the string.
20551 The mode_line_string_face face property is always added to the string.
20554 static int
20555 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20556 int field_width, int precision, Lisp_Object props)
20558 EMACS_INT len;
20559 int n = 0;
20561 if (string != NULL)
20563 len = strlen (string);
20564 if (precision > 0 && len > precision)
20565 len = precision;
20566 lisp_string = make_string (string, len);
20567 if (NILP (props))
20568 props = mode_line_string_face_prop;
20569 else if (!NILP (mode_line_string_face))
20571 Lisp_Object face = Fplist_get (props, Qface);
20572 props = Fcopy_sequence (props);
20573 if (NILP (face))
20574 face = mode_line_string_face;
20575 else
20576 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20577 props = Fplist_put (props, Qface, face);
20579 Fadd_text_properties (make_number (0), make_number (len),
20580 props, lisp_string);
20582 else
20584 len = XFASTINT (Flength (lisp_string));
20585 if (precision > 0 && len > precision)
20587 len = precision;
20588 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20589 precision = -1;
20591 if (!NILP (mode_line_string_face))
20593 Lisp_Object face;
20594 if (NILP (props))
20595 props = Ftext_properties_at (make_number (0), lisp_string);
20596 face = Fplist_get (props, Qface);
20597 if (NILP (face))
20598 face = mode_line_string_face;
20599 else
20600 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20601 props = Fcons (Qface, Fcons (face, Qnil));
20602 if (copy_string)
20603 lisp_string = Fcopy_sequence (lisp_string);
20605 if (!NILP (props))
20606 Fadd_text_properties (make_number (0), make_number (len),
20607 props, lisp_string);
20610 if (len > 0)
20612 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20613 n += len;
20616 if (field_width > len)
20618 field_width -= len;
20619 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20620 if (!NILP (props))
20621 Fadd_text_properties (make_number (0), make_number (field_width),
20622 props, lisp_string);
20623 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20624 n += field_width;
20627 return n;
20631 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20632 1, 4, 0,
20633 doc: /* Format a string out of a mode line format specification.
20634 First arg FORMAT specifies the mode line format (see `mode-line-format'
20635 for details) to use.
20637 By default, the format is evaluated for the currently selected window.
20639 Optional second arg FACE specifies the face property to put on all
20640 characters for which no face is specified. The value nil means the
20641 default face. The value t means whatever face the window's mode line
20642 currently uses (either `mode-line' or `mode-line-inactive',
20643 depending on whether the window is the selected window or not).
20644 An integer value means the value string has no text
20645 properties.
20647 Optional third and fourth args WINDOW and BUFFER specify the window
20648 and buffer to use as the context for the formatting (defaults
20649 are the selected window and the WINDOW's buffer). */)
20650 (Lisp_Object format, Lisp_Object face,
20651 Lisp_Object window, Lisp_Object buffer)
20653 struct it it;
20654 int len;
20655 struct window *w;
20656 struct buffer *old_buffer = NULL;
20657 int face_id;
20658 int no_props = INTEGERP (face);
20659 int count = SPECPDL_INDEX ();
20660 Lisp_Object str;
20661 int string_start = 0;
20663 if (NILP (window))
20664 window = selected_window;
20665 CHECK_WINDOW (window);
20666 w = XWINDOW (window);
20668 if (NILP (buffer))
20669 buffer = w->buffer;
20670 CHECK_BUFFER (buffer);
20672 /* Make formatting the modeline a non-op when noninteractive, otherwise
20673 there will be problems later caused by a partially initialized frame. */
20674 if (NILP (format) || noninteractive)
20675 return empty_unibyte_string;
20677 if (no_props)
20678 face = Qnil;
20680 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20681 : EQ (face, Qt) ? (EQ (window, selected_window)
20682 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20683 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20684 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20685 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20686 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20687 : DEFAULT_FACE_ID;
20689 if (XBUFFER (buffer) != current_buffer)
20690 old_buffer = current_buffer;
20692 /* Save things including mode_line_proptrans_alist,
20693 and set that to nil so that we don't alter the outer value. */
20694 record_unwind_protect (unwind_format_mode_line,
20695 format_mode_line_unwind_data
20696 (old_buffer, selected_window, 1));
20697 mode_line_proptrans_alist = Qnil;
20699 Fselect_window (window, Qt);
20700 if (old_buffer)
20701 set_buffer_internal_1 (XBUFFER (buffer));
20703 init_iterator (&it, w, -1, -1, NULL, face_id);
20705 if (no_props)
20707 mode_line_target = MODE_LINE_NOPROP;
20708 mode_line_string_face_prop = Qnil;
20709 mode_line_string_list = Qnil;
20710 string_start = MODE_LINE_NOPROP_LEN (0);
20712 else
20714 mode_line_target = MODE_LINE_STRING;
20715 mode_line_string_list = Qnil;
20716 mode_line_string_face = face;
20717 mode_line_string_face_prop
20718 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20721 push_kboard (FRAME_KBOARD (it.f));
20722 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20723 pop_kboard ();
20725 if (no_props)
20727 len = MODE_LINE_NOPROP_LEN (string_start);
20728 str = make_string (mode_line_noprop_buf + string_start, len);
20730 else
20732 mode_line_string_list = Fnreverse (mode_line_string_list);
20733 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20734 empty_unibyte_string);
20737 unbind_to (count, Qnil);
20738 return str;
20741 /* Write a null-terminated, right justified decimal representation of
20742 the positive integer D to BUF using a minimal field width WIDTH. */
20744 static void
20745 pint2str (register char *buf, register int width, register EMACS_INT d)
20747 register char *p = buf;
20749 if (d <= 0)
20750 *p++ = '0';
20751 else
20753 while (d > 0)
20755 *p++ = d % 10 + '0';
20756 d /= 10;
20760 for (width -= (int) (p - buf); width > 0; --width)
20761 *p++ = ' ';
20762 *p-- = '\0';
20763 while (p > buf)
20765 d = *buf;
20766 *buf++ = *p;
20767 *p-- = d;
20771 /* Write a null-terminated, right justified decimal and "human
20772 readable" representation of the nonnegative integer D to BUF using
20773 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20775 static const char power_letter[] =
20777 0, /* no letter */
20778 'k', /* kilo */
20779 'M', /* mega */
20780 'G', /* giga */
20781 'T', /* tera */
20782 'P', /* peta */
20783 'E', /* exa */
20784 'Z', /* zetta */
20785 'Y' /* yotta */
20788 static void
20789 pint2hrstr (char *buf, int width, EMACS_INT d)
20791 /* We aim to represent the nonnegative integer D as
20792 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
20793 EMACS_INT quotient = d;
20794 int remainder = 0;
20795 /* -1 means: do not use TENTHS. */
20796 int tenths = -1;
20797 int exponent = 0;
20799 /* Length of QUOTIENT.TENTHS as a string. */
20800 int length;
20802 char * psuffix;
20803 char * p;
20805 if (1000 <= quotient)
20807 /* Scale to the appropriate EXPONENT. */
20810 remainder = quotient % 1000;
20811 quotient /= 1000;
20812 exponent++;
20814 while (1000 <= quotient);
20816 /* Round to nearest and decide whether to use TENTHS or not. */
20817 if (quotient <= 9)
20819 tenths = remainder / 100;
20820 if (50 <= remainder % 100)
20822 if (tenths < 9)
20823 tenths++;
20824 else
20826 quotient++;
20827 if (quotient == 10)
20828 tenths = -1;
20829 else
20830 tenths = 0;
20834 else
20835 if (500 <= remainder)
20837 if (quotient < 999)
20838 quotient++;
20839 else
20841 quotient = 1;
20842 exponent++;
20843 tenths = 0;
20848 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
20849 if (tenths == -1 && quotient <= 99)
20850 if (quotient <= 9)
20851 length = 1;
20852 else
20853 length = 2;
20854 else
20855 length = 3;
20856 p = psuffix = buf + max (width, length);
20858 /* Print EXPONENT. */
20859 *psuffix++ = power_letter[exponent];
20860 *psuffix = '\0';
20862 /* Print TENTHS. */
20863 if (tenths >= 0)
20865 *--p = '0' + tenths;
20866 *--p = '.';
20869 /* Print QUOTIENT. */
20872 int digit = quotient % 10;
20873 *--p = '0' + digit;
20875 while ((quotient /= 10) != 0);
20877 /* Print leading spaces. */
20878 while (buf < p)
20879 *--p = ' ';
20882 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
20883 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
20884 type of CODING_SYSTEM. Return updated pointer into BUF. */
20886 static unsigned char invalid_eol_type[] = "(*invalid*)";
20888 static char *
20889 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
20891 Lisp_Object val;
20892 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
20893 const unsigned char *eol_str;
20894 int eol_str_len;
20895 /* The EOL conversion we are using. */
20896 Lisp_Object eoltype;
20898 val = CODING_SYSTEM_SPEC (coding_system);
20899 eoltype = Qnil;
20901 if (!VECTORP (val)) /* Not yet decided. */
20903 if (multibyte)
20904 *buf++ = '-';
20905 if (eol_flag)
20906 eoltype = eol_mnemonic_undecided;
20907 /* Don't mention EOL conversion if it isn't decided. */
20909 else
20911 Lisp_Object attrs;
20912 Lisp_Object eolvalue;
20914 attrs = AREF (val, 0);
20915 eolvalue = AREF (val, 2);
20917 if (multibyte)
20918 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
20920 if (eol_flag)
20922 /* The EOL conversion that is normal on this system. */
20924 if (NILP (eolvalue)) /* Not yet decided. */
20925 eoltype = eol_mnemonic_undecided;
20926 else if (VECTORP (eolvalue)) /* Not yet decided. */
20927 eoltype = eol_mnemonic_undecided;
20928 else /* eolvalue is Qunix, Qdos, or Qmac. */
20929 eoltype = (EQ (eolvalue, Qunix)
20930 ? eol_mnemonic_unix
20931 : (EQ (eolvalue, Qdos) == 1
20932 ? eol_mnemonic_dos : eol_mnemonic_mac));
20936 if (eol_flag)
20938 /* Mention the EOL conversion if it is not the usual one. */
20939 if (STRINGP (eoltype))
20941 eol_str = SDATA (eoltype);
20942 eol_str_len = SBYTES (eoltype);
20944 else if (CHARACTERP (eoltype))
20946 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
20947 int c = XFASTINT (eoltype);
20948 eol_str_len = CHAR_STRING (c, tmp);
20949 eol_str = tmp;
20951 else
20953 eol_str = invalid_eol_type;
20954 eol_str_len = sizeof (invalid_eol_type) - 1;
20956 memcpy (buf, eol_str, eol_str_len);
20957 buf += eol_str_len;
20960 return buf;
20963 /* Return a string for the output of a mode line %-spec for window W,
20964 generated by character C. FIELD_WIDTH > 0 means pad the string
20965 returned with spaces to that value. Return a Lisp string in
20966 *STRING if the resulting string is taken from that Lisp string.
20968 Note we operate on the current buffer for most purposes,
20969 the exception being w->base_line_pos. */
20971 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
20973 static const char *
20974 decode_mode_spec (struct window *w, register int c, int field_width,
20975 Lisp_Object *string)
20977 Lisp_Object obj;
20978 struct frame *f = XFRAME (WINDOW_FRAME (w));
20979 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
20980 struct buffer *b = current_buffer;
20982 obj = Qnil;
20983 *string = Qnil;
20985 switch (c)
20987 case '*':
20988 if (!NILP (BVAR (b, read_only)))
20989 return "%";
20990 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20991 return "*";
20992 return "-";
20994 case '+':
20995 /* This differs from %* only for a modified read-only buffer. */
20996 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20997 return "*";
20998 if (!NILP (BVAR (b, read_only)))
20999 return "%";
21000 return "-";
21002 case '&':
21003 /* This differs from %* in ignoring read-only-ness. */
21004 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21005 return "*";
21006 return "-";
21008 case '%':
21009 return "%";
21011 case '[':
21013 int i;
21014 char *p;
21016 if (command_loop_level > 5)
21017 return "[[[... ";
21018 p = decode_mode_spec_buf;
21019 for (i = 0; i < command_loop_level; i++)
21020 *p++ = '[';
21021 *p = 0;
21022 return decode_mode_spec_buf;
21025 case ']':
21027 int i;
21028 char *p;
21030 if (command_loop_level > 5)
21031 return " ...]]]";
21032 p = decode_mode_spec_buf;
21033 for (i = 0; i < command_loop_level; i++)
21034 *p++ = ']';
21035 *p = 0;
21036 return decode_mode_spec_buf;
21039 case '-':
21041 register int i;
21043 /* Let lots_of_dashes be a string of infinite length. */
21044 if (mode_line_target == MODE_LINE_NOPROP ||
21045 mode_line_target == MODE_LINE_STRING)
21046 return "--";
21047 if (field_width <= 0
21048 || field_width > sizeof (lots_of_dashes))
21050 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21051 decode_mode_spec_buf[i] = '-';
21052 decode_mode_spec_buf[i] = '\0';
21053 return decode_mode_spec_buf;
21055 else
21056 return lots_of_dashes;
21059 case 'b':
21060 obj = BVAR (b, name);
21061 break;
21063 case 'c':
21064 /* %c and %l are ignored in `frame-title-format'.
21065 (In redisplay_internal, the frame title is drawn _before_ the
21066 windows are updated, so the stuff which depends on actual
21067 window contents (such as %l) may fail to render properly, or
21068 even crash emacs.) */
21069 if (mode_line_target == MODE_LINE_TITLE)
21070 return "";
21071 else
21073 EMACS_INT col = current_column ();
21074 w->column_number_displayed = make_number (col);
21075 pint2str (decode_mode_spec_buf, field_width, col);
21076 return decode_mode_spec_buf;
21079 case 'e':
21080 #ifndef SYSTEM_MALLOC
21082 if (NILP (Vmemory_full))
21083 return "";
21084 else
21085 return "!MEM FULL! ";
21087 #else
21088 return "";
21089 #endif
21091 case 'F':
21092 /* %F displays the frame name. */
21093 if (!NILP (f->title))
21094 return SSDATA (f->title);
21095 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21096 return SSDATA (f->name);
21097 return "Emacs";
21099 case 'f':
21100 obj = BVAR (b, filename);
21101 break;
21103 case 'i':
21105 EMACS_INT size = ZV - BEGV;
21106 pint2str (decode_mode_spec_buf, field_width, size);
21107 return decode_mode_spec_buf;
21110 case 'I':
21112 EMACS_INT size = ZV - BEGV;
21113 pint2hrstr (decode_mode_spec_buf, field_width, size);
21114 return decode_mode_spec_buf;
21117 case 'l':
21119 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
21120 EMACS_INT topline, nlines, height;
21121 EMACS_INT junk;
21123 /* %c and %l are ignored in `frame-title-format'. */
21124 if (mode_line_target == MODE_LINE_TITLE)
21125 return "";
21127 startpos = XMARKER (w->start)->charpos;
21128 startpos_byte = marker_byte_position (w->start);
21129 height = WINDOW_TOTAL_LINES (w);
21131 /* If we decided that this buffer isn't suitable for line numbers,
21132 don't forget that too fast. */
21133 if (EQ (w->base_line_pos, w->buffer))
21134 goto no_value;
21135 /* But do forget it, if the window shows a different buffer now. */
21136 else if (BUFFERP (w->base_line_pos))
21137 w->base_line_pos = Qnil;
21139 /* If the buffer is very big, don't waste time. */
21140 if (INTEGERP (Vline_number_display_limit)
21141 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21143 w->base_line_pos = Qnil;
21144 w->base_line_number = Qnil;
21145 goto no_value;
21148 if (INTEGERP (w->base_line_number)
21149 && INTEGERP (w->base_line_pos)
21150 && XFASTINT (w->base_line_pos) <= startpos)
21152 line = XFASTINT (w->base_line_number);
21153 linepos = XFASTINT (w->base_line_pos);
21154 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21156 else
21158 line = 1;
21159 linepos = BUF_BEGV (b);
21160 linepos_byte = BUF_BEGV_BYTE (b);
21163 /* Count lines from base line to window start position. */
21164 nlines = display_count_lines (linepos_byte,
21165 startpos_byte,
21166 startpos, &junk);
21168 topline = nlines + line;
21170 /* Determine a new base line, if the old one is too close
21171 or too far away, or if we did not have one.
21172 "Too close" means it's plausible a scroll-down would
21173 go back past it. */
21174 if (startpos == BUF_BEGV (b))
21176 w->base_line_number = make_number (topline);
21177 w->base_line_pos = make_number (BUF_BEGV (b));
21179 else if (nlines < height + 25 || nlines > height * 3 + 50
21180 || linepos == BUF_BEGV (b))
21182 EMACS_INT limit = BUF_BEGV (b);
21183 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
21184 EMACS_INT position;
21185 EMACS_INT distance =
21186 (height * 2 + 30) * line_number_display_limit_width;
21188 if (startpos - distance > limit)
21190 limit = startpos - distance;
21191 limit_byte = CHAR_TO_BYTE (limit);
21194 nlines = display_count_lines (startpos_byte,
21195 limit_byte,
21196 - (height * 2 + 30),
21197 &position);
21198 /* If we couldn't find the lines we wanted within
21199 line_number_display_limit_width chars per line,
21200 give up on line numbers for this window. */
21201 if (position == limit_byte && limit == startpos - distance)
21203 w->base_line_pos = w->buffer;
21204 w->base_line_number = Qnil;
21205 goto no_value;
21208 w->base_line_number = make_number (topline - nlines);
21209 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
21212 /* Now count lines from the start pos to point. */
21213 nlines = display_count_lines (startpos_byte,
21214 PT_BYTE, PT, &junk);
21216 /* Record that we did display the line number. */
21217 line_number_displayed = 1;
21219 /* Make the string to show. */
21220 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21221 return decode_mode_spec_buf;
21222 no_value:
21224 char* p = decode_mode_spec_buf;
21225 int pad = field_width - 2;
21226 while (pad-- > 0)
21227 *p++ = ' ';
21228 *p++ = '?';
21229 *p++ = '?';
21230 *p = '\0';
21231 return decode_mode_spec_buf;
21234 break;
21236 case 'm':
21237 obj = BVAR (b, mode_name);
21238 break;
21240 case 'n':
21241 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21242 return " Narrow";
21243 break;
21245 case 'p':
21247 EMACS_INT pos = marker_position (w->start);
21248 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
21250 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21252 if (pos <= BUF_BEGV (b))
21253 return "All";
21254 else
21255 return "Bottom";
21257 else if (pos <= BUF_BEGV (b))
21258 return "Top";
21259 else
21261 if (total > 1000000)
21262 /* Do it differently for a large value, to avoid overflow. */
21263 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21264 else
21265 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21266 /* We can't normally display a 3-digit number,
21267 so get us a 2-digit number that is close. */
21268 if (total == 100)
21269 total = 99;
21270 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
21271 return decode_mode_spec_buf;
21275 /* Display percentage of size above the bottom of the screen. */
21276 case 'P':
21278 EMACS_INT toppos = marker_position (w->start);
21279 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21280 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
21282 if (botpos >= BUF_ZV (b))
21284 if (toppos <= BUF_BEGV (b))
21285 return "All";
21286 else
21287 return "Bottom";
21289 else
21291 if (total > 1000000)
21292 /* Do it differently for a large value, to avoid overflow. */
21293 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21294 else
21295 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21296 /* We can't normally display a 3-digit number,
21297 so get us a 2-digit number that is close. */
21298 if (total == 100)
21299 total = 99;
21300 if (toppos <= BUF_BEGV (b))
21301 sprintf (decode_mode_spec_buf, "Top%2"pI"d%%", total);
21302 else
21303 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
21304 return decode_mode_spec_buf;
21308 case 's':
21309 /* status of process */
21310 obj = Fget_buffer_process (Fcurrent_buffer ());
21311 if (NILP (obj))
21312 return "no process";
21313 #ifndef MSDOS
21314 obj = Fsymbol_name (Fprocess_status (obj));
21315 #endif
21316 break;
21318 case '@':
21320 int count = inhibit_garbage_collection ();
21321 Lisp_Object val = call1 (intern ("file-remote-p"),
21322 BVAR (current_buffer, directory));
21323 unbind_to (count, Qnil);
21325 if (NILP (val))
21326 return "-";
21327 else
21328 return "@";
21331 case 't': /* indicate TEXT or BINARY */
21332 return "T";
21334 case 'z':
21335 /* coding-system (not including end-of-line format) */
21336 case 'Z':
21337 /* coding-system (including end-of-line type) */
21339 int eol_flag = (c == 'Z');
21340 char *p = decode_mode_spec_buf;
21342 if (! FRAME_WINDOW_P (f))
21344 /* No need to mention EOL here--the terminal never needs
21345 to do EOL conversion. */
21346 p = decode_mode_spec_coding (CODING_ID_NAME
21347 (FRAME_KEYBOARD_CODING (f)->id),
21348 p, 0);
21349 p = decode_mode_spec_coding (CODING_ID_NAME
21350 (FRAME_TERMINAL_CODING (f)->id),
21351 p, 0);
21353 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21354 p, eol_flag);
21356 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21357 #ifdef subprocesses
21358 obj = Fget_buffer_process (Fcurrent_buffer ());
21359 if (PROCESSP (obj))
21361 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
21362 p, eol_flag);
21363 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
21364 p, eol_flag);
21366 #endif /* subprocesses */
21367 #endif /* 0 */
21368 *p = 0;
21369 return decode_mode_spec_buf;
21373 if (STRINGP (obj))
21375 *string = obj;
21376 return SSDATA (obj);
21378 else
21379 return "";
21383 /* Count up to COUNT lines starting from START_BYTE.
21384 But don't go beyond LIMIT_BYTE.
21385 Return the number of lines thus found (always nonnegative).
21387 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21389 static EMACS_INT
21390 display_count_lines (EMACS_INT start_byte,
21391 EMACS_INT limit_byte, EMACS_INT count,
21392 EMACS_INT *byte_pos_ptr)
21394 register unsigned char *cursor;
21395 unsigned char *base;
21397 register EMACS_INT ceiling;
21398 register unsigned char *ceiling_addr;
21399 EMACS_INT orig_count = count;
21401 /* If we are not in selective display mode,
21402 check only for newlines. */
21403 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21404 && !INTEGERP (BVAR (current_buffer, selective_display)));
21406 if (count > 0)
21408 while (start_byte < limit_byte)
21410 ceiling = BUFFER_CEILING_OF (start_byte);
21411 ceiling = min (limit_byte - 1, ceiling);
21412 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21413 base = (cursor = BYTE_POS_ADDR (start_byte));
21414 while (1)
21416 if (selective_display)
21417 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21419 else
21420 while (*cursor != '\n' && ++cursor != ceiling_addr)
21423 if (cursor != ceiling_addr)
21425 if (--count == 0)
21427 start_byte += cursor - base + 1;
21428 *byte_pos_ptr = start_byte;
21429 return orig_count;
21431 else
21432 if (++cursor == ceiling_addr)
21433 break;
21435 else
21436 break;
21438 start_byte += cursor - base;
21441 else
21443 while (start_byte > limit_byte)
21445 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21446 ceiling = max (limit_byte, ceiling);
21447 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21448 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21449 while (1)
21451 if (selective_display)
21452 while (--cursor != ceiling_addr
21453 && *cursor != '\n' && *cursor != 015)
21455 else
21456 while (--cursor != ceiling_addr && *cursor != '\n')
21459 if (cursor != ceiling_addr)
21461 if (++count == 0)
21463 start_byte += cursor - base + 1;
21464 *byte_pos_ptr = start_byte;
21465 /* When scanning backwards, we should
21466 not count the newline posterior to which we stop. */
21467 return - orig_count - 1;
21470 else
21471 break;
21473 /* Here we add 1 to compensate for the last decrement
21474 of CURSOR, which took it past the valid range. */
21475 start_byte += cursor - base + 1;
21479 *byte_pos_ptr = limit_byte;
21481 if (count < 0)
21482 return - orig_count + count;
21483 return orig_count - count;
21489 /***********************************************************************
21490 Displaying strings
21491 ***********************************************************************/
21493 /* Display a NUL-terminated string, starting with index START.
21495 If STRING is non-null, display that C string. Otherwise, the Lisp
21496 string LISP_STRING is displayed. There's a case that STRING is
21497 non-null and LISP_STRING is not nil. It means STRING is a string
21498 data of LISP_STRING. In that case, we display LISP_STRING while
21499 ignoring its text properties.
21501 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21502 FACE_STRING. Display STRING or LISP_STRING with the face at
21503 FACE_STRING_POS in FACE_STRING:
21505 Display the string in the environment given by IT, but use the
21506 standard display table, temporarily.
21508 FIELD_WIDTH is the minimum number of output glyphs to produce.
21509 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21510 with spaces. If STRING has more characters, more than FIELD_WIDTH
21511 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21513 PRECISION is the maximum number of characters to output from
21514 STRING. PRECISION < 0 means don't truncate the string.
21516 This is roughly equivalent to printf format specifiers:
21518 FIELD_WIDTH PRECISION PRINTF
21519 ----------------------------------------
21520 -1 -1 %s
21521 -1 10 %.10s
21522 10 -1 %10s
21523 20 10 %20.10s
21525 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21526 display them, and < 0 means obey the current buffer's value of
21527 enable_multibyte_characters.
21529 Value is the number of columns displayed. */
21531 static int
21532 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21533 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
21534 int field_width, int precision, int max_x, int multibyte)
21536 int hpos_at_start = it->hpos;
21537 int saved_face_id = it->face_id;
21538 struct glyph_row *row = it->glyph_row;
21539 EMACS_INT it_charpos;
21541 /* Initialize the iterator IT for iteration over STRING beginning
21542 with index START. */
21543 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21544 precision, field_width, multibyte);
21545 if (string && STRINGP (lisp_string))
21546 /* LISP_STRING is the one returned by decode_mode_spec. We should
21547 ignore its text properties. */
21548 it->stop_charpos = it->end_charpos;
21550 /* If displaying STRING, set up the face of the iterator from
21551 FACE_STRING, if that's given. */
21552 if (STRINGP (face_string))
21554 EMACS_INT endptr;
21555 struct face *face;
21557 it->face_id
21558 = face_at_string_position (it->w, face_string, face_string_pos,
21559 0, it->region_beg_charpos,
21560 it->region_end_charpos,
21561 &endptr, it->base_face_id, 0);
21562 face = FACE_FROM_ID (it->f, it->face_id);
21563 it->face_box_p = face->box != FACE_NO_BOX;
21566 /* Set max_x to the maximum allowed X position. Don't let it go
21567 beyond the right edge of the window. */
21568 if (max_x <= 0)
21569 max_x = it->last_visible_x;
21570 else
21571 max_x = min (max_x, it->last_visible_x);
21573 /* Skip over display elements that are not visible. because IT->w is
21574 hscrolled. */
21575 if (it->current_x < it->first_visible_x)
21576 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21577 MOVE_TO_POS | MOVE_TO_X);
21579 row->ascent = it->max_ascent;
21580 row->height = it->max_ascent + it->max_descent;
21581 row->phys_ascent = it->max_phys_ascent;
21582 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21583 row->extra_line_spacing = it->max_extra_line_spacing;
21585 if (STRINGP (it->string))
21586 it_charpos = IT_STRING_CHARPOS (*it);
21587 else
21588 it_charpos = IT_CHARPOS (*it);
21590 /* This condition is for the case that we are called with current_x
21591 past last_visible_x. */
21592 while (it->current_x < max_x)
21594 int x_before, x, n_glyphs_before, i, nglyphs;
21596 /* Get the next display element. */
21597 if (!get_next_display_element (it))
21598 break;
21600 /* Produce glyphs. */
21601 x_before = it->current_x;
21602 n_glyphs_before = row->used[TEXT_AREA];
21603 PRODUCE_GLYPHS (it);
21605 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21606 i = 0;
21607 x = x_before;
21608 while (i < nglyphs)
21610 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21612 if (it->line_wrap != TRUNCATE
21613 && x + glyph->pixel_width > max_x)
21615 /* End of continued line or max_x reached. */
21616 if (CHAR_GLYPH_PADDING_P (*glyph))
21618 /* A wide character is unbreakable. */
21619 if (row->reversed_p)
21620 unproduce_glyphs (it, row->used[TEXT_AREA]
21621 - n_glyphs_before);
21622 row->used[TEXT_AREA] = n_glyphs_before;
21623 it->current_x = x_before;
21625 else
21627 if (row->reversed_p)
21628 unproduce_glyphs (it, row->used[TEXT_AREA]
21629 - (n_glyphs_before + i));
21630 row->used[TEXT_AREA] = n_glyphs_before + i;
21631 it->current_x = x;
21633 break;
21635 else if (x + glyph->pixel_width >= it->first_visible_x)
21637 /* Glyph is at least partially visible. */
21638 ++it->hpos;
21639 if (x < it->first_visible_x)
21640 row->x = x - it->first_visible_x;
21642 else
21644 /* Glyph is off the left margin of the display area.
21645 Should not happen. */
21646 abort ();
21649 row->ascent = max (row->ascent, it->max_ascent);
21650 row->height = max (row->height, it->max_ascent + it->max_descent);
21651 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21652 row->phys_height = max (row->phys_height,
21653 it->max_phys_ascent + it->max_phys_descent);
21654 row->extra_line_spacing = max (row->extra_line_spacing,
21655 it->max_extra_line_spacing);
21656 x += glyph->pixel_width;
21657 ++i;
21660 /* Stop if max_x reached. */
21661 if (i < nglyphs)
21662 break;
21664 /* Stop at line ends. */
21665 if (ITERATOR_AT_END_OF_LINE_P (it))
21667 it->continuation_lines_width = 0;
21668 break;
21671 set_iterator_to_next (it, 1);
21672 if (STRINGP (it->string))
21673 it_charpos = IT_STRING_CHARPOS (*it);
21674 else
21675 it_charpos = IT_CHARPOS (*it);
21677 /* Stop if truncating at the right edge. */
21678 if (it->line_wrap == TRUNCATE
21679 && it->current_x >= it->last_visible_x)
21681 /* Add truncation mark, but don't do it if the line is
21682 truncated at a padding space. */
21683 if (it_charpos < it->string_nchars)
21685 if (!FRAME_WINDOW_P (it->f))
21687 int ii, n;
21689 if (it->current_x > it->last_visible_x)
21691 if (!row->reversed_p)
21693 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21694 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21695 break;
21697 else
21699 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21700 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21701 break;
21702 unproduce_glyphs (it, ii + 1);
21703 ii = row->used[TEXT_AREA] - (ii + 1);
21705 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21707 row->used[TEXT_AREA] = ii;
21708 produce_special_glyphs (it, IT_TRUNCATION);
21711 produce_special_glyphs (it, IT_TRUNCATION);
21713 row->truncated_on_right_p = 1;
21715 break;
21719 /* Maybe insert a truncation at the left. */
21720 if (it->first_visible_x
21721 && it_charpos > 0)
21723 if (!FRAME_WINDOW_P (it->f))
21724 insert_left_trunc_glyphs (it);
21725 row->truncated_on_left_p = 1;
21728 it->face_id = saved_face_id;
21730 /* Value is number of columns displayed. */
21731 return it->hpos - hpos_at_start;
21736 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21737 appears as an element of LIST or as the car of an element of LIST.
21738 If PROPVAL is a list, compare each element against LIST in that
21739 way, and return 1/2 if any element of PROPVAL is found in LIST.
21740 Otherwise return 0. This function cannot quit.
21741 The return value is 2 if the text is invisible but with an ellipsis
21742 and 1 if it's invisible and without an ellipsis. */
21745 invisible_p (register Lisp_Object propval, Lisp_Object list)
21747 register Lisp_Object tail, proptail;
21749 for (tail = list; CONSP (tail); tail = XCDR (tail))
21751 register Lisp_Object tem;
21752 tem = XCAR (tail);
21753 if (EQ (propval, tem))
21754 return 1;
21755 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21756 return NILP (XCDR (tem)) ? 1 : 2;
21759 if (CONSP (propval))
21761 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
21763 Lisp_Object propelt;
21764 propelt = XCAR (proptail);
21765 for (tail = list; CONSP (tail); tail = XCDR (tail))
21767 register Lisp_Object tem;
21768 tem = XCAR (tail);
21769 if (EQ (propelt, tem))
21770 return 1;
21771 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
21772 return NILP (XCDR (tem)) ? 1 : 2;
21777 return 0;
21780 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
21781 doc: /* Non-nil if the property makes the text invisible.
21782 POS-OR-PROP can be a marker or number, in which case it is taken to be
21783 a position in the current buffer and the value of the `invisible' property
21784 is checked; or it can be some other value, which is then presumed to be the
21785 value of the `invisible' property of the text of interest.
21786 The non-nil value returned can be t for truly invisible text or something
21787 else if the text is replaced by an ellipsis. */)
21788 (Lisp_Object pos_or_prop)
21790 Lisp_Object prop
21791 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
21792 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
21793 : pos_or_prop);
21794 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
21795 return (invis == 0 ? Qnil
21796 : invis == 1 ? Qt
21797 : make_number (invis));
21800 /* Calculate a width or height in pixels from a specification using
21801 the following elements:
21803 SPEC ::=
21804 NUM - a (fractional) multiple of the default font width/height
21805 (NUM) - specifies exactly NUM pixels
21806 UNIT - a fixed number of pixels, see below.
21807 ELEMENT - size of a display element in pixels, see below.
21808 (NUM . SPEC) - equals NUM * SPEC
21809 (+ SPEC SPEC ...) - add pixel values
21810 (- SPEC SPEC ...) - subtract pixel values
21811 (- SPEC) - negate pixel value
21813 NUM ::=
21814 INT or FLOAT - a number constant
21815 SYMBOL - use symbol's (buffer local) variable binding.
21817 UNIT ::=
21818 in - pixels per inch *)
21819 mm - pixels per 1/1000 meter *)
21820 cm - pixels per 1/100 meter *)
21821 width - width of current font in pixels.
21822 height - height of current font in pixels.
21824 *) using the ratio(s) defined in display-pixels-per-inch.
21826 ELEMENT ::=
21828 left-fringe - left fringe width in pixels
21829 right-fringe - right fringe width in pixels
21831 left-margin - left margin width in pixels
21832 right-margin - right margin width in pixels
21834 scroll-bar - scroll-bar area width in pixels
21836 Examples:
21838 Pixels corresponding to 5 inches:
21839 (5 . in)
21841 Total width of non-text areas on left side of window (if scroll-bar is on left):
21842 '(space :width (+ left-fringe left-margin scroll-bar))
21844 Align to first text column (in header line):
21845 '(space :align-to 0)
21847 Align to middle of text area minus half the width of variable `my-image'
21848 containing a loaded image:
21849 '(space :align-to (0.5 . (- text my-image)))
21851 Width of left margin minus width of 1 character in the default font:
21852 '(space :width (- left-margin 1))
21854 Width of left margin minus width of 2 characters in the current font:
21855 '(space :width (- left-margin (2 . width)))
21857 Center 1 character over left-margin (in header line):
21858 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
21860 Different ways to express width of left fringe plus left margin minus one pixel:
21861 '(space :width (- (+ left-fringe left-margin) (1)))
21862 '(space :width (+ left-fringe left-margin (- (1))))
21863 '(space :width (+ left-fringe left-margin (-1)))
21867 #define NUMVAL(X) \
21868 ((INTEGERP (X) || FLOATP (X)) \
21869 ? XFLOATINT (X) \
21870 : - 1)
21872 static int
21873 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
21874 struct font *font, int width_p, int *align_to)
21876 double pixels;
21878 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
21879 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
21881 if (NILP (prop))
21882 return OK_PIXELS (0);
21884 xassert (FRAME_LIVE_P (it->f));
21886 if (SYMBOLP (prop))
21888 if (SCHARS (SYMBOL_NAME (prop)) == 2)
21890 char *unit = SSDATA (SYMBOL_NAME (prop));
21892 if (unit[0] == 'i' && unit[1] == 'n')
21893 pixels = 1.0;
21894 else if (unit[0] == 'm' && unit[1] == 'm')
21895 pixels = 25.4;
21896 else if (unit[0] == 'c' && unit[1] == 'm')
21897 pixels = 2.54;
21898 else
21899 pixels = 0;
21900 if (pixels > 0)
21902 double ppi;
21903 #ifdef HAVE_WINDOW_SYSTEM
21904 if (FRAME_WINDOW_P (it->f)
21905 && (ppi = (width_p
21906 ? FRAME_X_DISPLAY_INFO (it->f)->resx
21907 : FRAME_X_DISPLAY_INFO (it->f)->resy),
21908 ppi > 0))
21909 return OK_PIXELS (ppi / pixels);
21910 #endif
21912 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
21913 || (CONSP (Vdisplay_pixels_per_inch)
21914 && (ppi = (width_p
21915 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
21916 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
21917 ppi > 0)))
21918 return OK_PIXELS (ppi / pixels);
21920 return 0;
21924 #ifdef HAVE_WINDOW_SYSTEM
21925 if (EQ (prop, Qheight))
21926 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
21927 if (EQ (prop, Qwidth))
21928 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
21929 #else
21930 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
21931 return OK_PIXELS (1);
21932 #endif
21934 if (EQ (prop, Qtext))
21935 return OK_PIXELS (width_p
21936 ? window_box_width (it->w, TEXT_AREA)
21937 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
21939 if (align_to && *align_to < 0)
21941 *res = 0;
21942 if (EQ (prop, Qleft))
21943 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
21944 if (EQ (prop, Qright))
21945 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
21946 if (EQ (prop, Qcenter))
21947 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
21948 + window_box_width (it->w, TEXT_AREA) / 2);
21949 if (EQ (prop, Qleft_fringe))
21950 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21951 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
21952 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
21953 if (EQ (prop, Qright_fringe))
21954 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21955 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21956 : window_box_right_offset (it->w, TEXT_AREA));
21957 if (EQ (prop, Qleft_margin))
21958 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
21959 if (EQ (prop, Qright_margin))
21960 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
21961 if (EQ (prop, Qscroll_bar))
21962 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
21964 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21965 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21966 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21967 : 0)));
21969 else
21971 if (EQ (prop, Qleft_fringe))
21972 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
21973 if (EQ (prop, Qright_fringe))
21974 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
21975 if (EQ (prop, Qleft_margin))
21976 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
21977 if (EQ (prop, Qright_margin))
21978 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
21979 if (EQ (prop, Qscroll_bar))
21980 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
21983 prop = Fbuffer_local_value (prop, it->w->buffer);
21986 if (INTEGERP (prop) || FLOATP (prop))
21988 int base_unit = (width_p
21989 ? FRAME_COLUMN_WIDTH (it->f)
21990 : FRAME_LINE_HEIGHT (it->f));
21991 return OK_PIXELS (XFLOATINT (prop) * base_unit);
21994 if (CONSP (prop))
21996 Lisp_Object car = XCAR (prop);
21997 Lisp_Object cdr = XCDR (prop);
21999 if (SYMBOLP (car))
22001 #ifdef HAVE_WINDOW_SYSTEM
22002 if (FRAME_WINDOW_P (it->f)
22003 && valid_image_p (prop))
22005 ptrdiff_t id = lookup_image (it->f, prop);
22006 struct image *img = IMAGE_FROM_ID (it->f, id);
22008 return OK_PIXELS (width_p ? img->width : img->height);
22010 #endif
22011 if (EQ (car, Qplus) || EQ (car, Qminus))
22013 int first = 1;
22014 double px;
22016 pixels = 0;
22017 while (CONSP (cdr))
22019 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22020 font, width_p, align_to))
22021 return 0;
22022 if (first)
22023 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22024 else
22025 pixels += px;
22026 cdr = XCDR (cdr);
22028 if (EQ (car, Qminus))
22029 pixels = -pixels;
22030 return OK_PIXELS (pixels);
22033 car = Fbuffer_local_value (car, it->w->buffer);
22036 if (INTEGERP (car) || FLOATP (car))
22038 double fact;
22039 pixels = XFLOATINT (car);
22040 if (NILP (cdr))
22041 return OK_PIXELS (pixels);
22042 if (calc_pixel_width_or_height (&fact, it, cdr,
22043 font, width_p, align_to))
22044 return OK_PIXELS (pixels * fact);
22045 return 0;
22048 return 0;
22051 return 0;
22055 /***********************************************************************
22056 Glyph Display
22057 ***********************************************************************/
22059 #ifdef HAVE_WINDOW_SYSTEM
22061 #if GLYPH_DEBUG
22063 void
22064 dump_glyph_string (struct glyph_string *s)
22066 fprintf (stderr, "glyph string\n");
22067 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22068 s->x, s->y, s->width, s->height);
22069 fprintf (stderr, " ybase = %d\n", s->ybase);
22070 fprintf (stderr, " hl = %d\n", s->hl);
22071 fprintf (stderr, " left overhang = %d, right = %d\n",
22072 s->left_overhang, s->right_overhang);
22073 fprintf (stderr, " nchars = %d\n", s->nchars);
22074 fprintf (stderr, " extends to end of line = %d\n",
22075 s->extends_to_end_of_line_p);
22076 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22077 fprintf (stderr, " bg width = %d\n", s->background_width);
22080 #endif /* GLYPH_DEBUG */
22082 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22083 of XChar2b structures for S; it can't be allocated in
22084 init_glyph_string because it must be allocated via `alloca'. W
22085 is the window on which S is drawn. ROW and AREA are the glyph row
22086 and area within the row from which S is constructed. START is the
22087 index of the first glyph structure covered by S. HL is a
22088 face-override for drawing S. */
22090 #ifdef HAVE_NTGUI
22091 #define OPTIONAL_HDC(hdc) HDC hdc,
22092 #define DECLARE_HDC(hdc) HDC hdc;
22093 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22094 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22095 #endif
22097 #ifndef OPTIONAL_HDC
22098 #define OPTIONAL_HDC(hdc)
22099 #define DECLARE_HDC(hdc)
22100 #define ALLOCATE_HDC(hdc, f)
22101 #define RELEASE_HDC(hdc, f)
22102 #endif
22104 static void
22105 init_glyph_string (struct glyph_string *s,
22106 OPTIONAL_HDC (hdc)
22107 XChar2b *char2b, struct window *w, struct glyph_row *row,
22108 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22110 memset (s, 0, sizeof *s);
22111 s->w = w;
22112 s->f = XFRAME (w->frame);
22113 #ifdef HAVE_NTGUI
22114 s->hdc = hdc;
22115 #endif
22116 s->display = FRAME_X_DISPLAY (s->f);
22117 s->window = FRAME_X_WINDOW (s->f);
22118 s->char2b = char2b;
22119 s->hl = hl;
22120 s->row = row;
22121 s->area = area;
22122 s->first_glyph = row->glyphs[area] + start;
22123 s->height = row->height;
22124 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22125 s->ybase = s->y + row->ascent;
22129 /* Append the list of glyph strings with head H and tail T to the list
22130 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22132 static inline void
22133 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22134 struct glyph_string *h, struct glyph_string *t)
22136 if (h)
22138 if (*head)
22139 (*tail)->next = h;
22140 else
22141 *head = h;
22142 h->prev = *tail;
22143 *tail = t;
22148 /* Prepend the list of glyph strings with head H and tail T to the
22149 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22150 result. */
22152 static inline void
22153 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22154 struct glyph_string *h, struct glyph_string *t)
22156 if (h)
22158 if (*head)
22159 (*head)->prev = t;
22160 else
22161 *tail = t;
22162 t->next = *head;
22163 *head = h;
22168 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22169 Set *HEAD and *TAIL to the resulting list. */
22171 static inline void
22172 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22173 struct glyph_string *s)
22175 s->next = s->prev = NULL;
22176 append_glyph_string_lists (head, tail, s, s);
22180 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22181 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22182 make sure that X resources for the face returned are allocated.
22183 Value is a pointer to a realized face that is ready for display if
22184 DISPLAY_P is non-zero. */
22186 static inline struct face *
22187 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22188 XChar2b *char2b, int display_p)
22190 struct face *face = FACE_FROM_ID (f, face_id);
22192 if (face->font)
22194 unsigned code = face->font->driver->encode_char (face->font, c);
22196 if (code != FONT_INVALID_CODE)
22197 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22198 else
22199 STORE_XCHAR2B (char2b, 0, 0);
22202 /* Make sure X resources of the face are allocated. */
22203 #ifdef HAVE_X_WINDOWS
22204 if (display_p)
22205 #endif
22207 xassert (face != NULL);
22208 PREPARE_FACE_FOR_DISPLAY (f, face);
22211 return face;
22215 /* Get face and two-byte form of character glyph GLYPH on frame F.
22216 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22217 a pointer to a realized face that is ready for display. */
22219 static inline struct face *
22220 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22221 XChar2b *char2b, int *two_byte_p)
22223 struct face *face;
22225 xassert (glyph->type == CHAR_GLYPH);
22226 face = FACE_FROM_ID (f, glyph->face_id);
22228 if (two_byte_p)
22229 *two_byte_p = 0;
22231 if (face->font)
22233 unsigned code;
22235 if (CHAR_BYTE8_P (glyph->u.ch))
22236 code = CHAR_TO_BYTE8 (glyph->u.ch);
22237 else
22238 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22240 if (code != FONT_INVALID_CODE)
22241 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22242 else
22243 STORE_XCHAR2B (char2b, 0, 0);
22246 /* Make sure X resources of the face are allocated. */
22247 xassert (face != NULL);
22248 PREPARE_FACE_FOR_DISPLAY (f, face);
22249 return face;
22253 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22254 Return 1 if FONT has a glyph for C, otherwise return 0. */
22256 static inline int
22257 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22259 unsigned code;
22261 if (CHAR_BYTE8_P (c))
22262 code = CHAR_TO_BYTE8 (c);
22263 else
22264 code = font->driver->encode_char (font, c);
22266 if (code == FONT_INVALID_CODE)
22267 return 0;
22268 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22269 return 1;
22273 /* Fill glyph string S with composition components specified by S->cmp.
22275 BASE_FACE is the base face of the composition.
22276 S->cmp_from is the index of the first component for S.
22278 OVERLAPS non-zero means S should draw the foreground only, and use
22279 its physical height for clipping. See also draw_glyphs.
22281 Value is the index of a component not in S. */
22283 static int
22284 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22285 int overlaps)
22287 int i;
22288 /* For all glyphs of this composition, starting at the offset
22289 S->cmp_from, until we reach the end of the definition or encounter a
22290 glyph that requires the different face, add it to S. */
22291 struct face *face;
22293 xassert (s);
22295 s->for_overlaps = overlaps;
22296 s->face = NULL;
22297 s->font = NULL;
22298 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22300 int c = COMPOSITION_GLYPH (s->cmp, i);
22302 /* TAB in a composition means display glyphs with padding space
22303 on the left or right. */
22304 if (c != '\t')
22306 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22307 -1, Qnil);
22309 face = get_char_face_and_encoding (s->f, c, face_id,
22310 s->char2b + i, 1);
22311 if (face)
22313 if (! s->face)
22315 s->face = face;
22316 s->font = s->face->font;
22318 else if (s->face != face)
22319 break;
22322 ++s->nchars;
22324 s->cmp_to = i;
22326 if (s->face == NULL)
22328 s->face = base_face->ascii_face;
22329 s->font = s->face->font;
22332 /* All glyph strings for the same composition has the same width,
22333 i.e. the width set for the first component of the composition. */
22334 s->width = s->first_glyph->pixel_width;
22336 /* If the specified font could not be loaded, use the frame's
22337 default font, but record the fact that we couldn't load it in
22338 the glyph string so that we can draw rectangles for the
22339 characters of the glyph string. */
22340 if (s->font == NULL)
22342 s->font_not_found_p = 1;
22343 s->font = FRAME_FONT (s->f);
22346 /* Adjust base line for subscript/superscript text. */
22347 s->ybase += s->first_glyph->voffset;
22349 /* This glyph string must always be drawn with 16-bit functions. */
22350 s->two_byte_p = 1;
22352 return s->cmp_to;
22355 static int
22356 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22357 int start, int end, int overlaps)
22359 struct glyph *glyph, *last;
22360 Lisp_Object lgstring;
22361 int i;
22363 s->for_overlaps = overlaps;
22364 glyph = s->row->glyphs[s->area] + start;
22365 last = s->row->glyphs[s->area] + end;
22366 s->cmp_id = glyph->u.cmp.id;
22367 s->cmp_from = glyph->slice.cmp.from;
22368 s->cmp_to = glyph->slice.cmp.to + 1;
22369 s->face = FACE_FROM_ID (s->f, face_id);
22370 lgstring = composition_gstring_from_id (s->cmp_id);
22371 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22372 glyph++;
22373 while (glyph < last
22374 && glyph->u.cmp.automatic
22375 && glyph->u.cmp.id == s->cmp_id
22376 && s->cmp_to == glyph->slice.cmp.from)
22377 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22379 for (i = s->cmp_from; i < s->cmp_to; i++)
22381 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22382 unsigned code = LGLYPH_CODE (lglyph);
22384 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22386 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22387 return glyph - s->row->glyphs[s->area];
22391 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22392 See the comment of fill_glyph_string for arguments.
22393 Value is the index of the first glyph not in S. */
22396 static int
22397 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22398 int start, int end, int overlaps)
22400 struct glyph *glyph, *last;
22401 int voffset;
22403 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22404 s->for_overlaps = overlaps;
22405 glyph = s->row->glyphs[s->area] + start;
22406 last = s->row->glyphs[s->area] + end;
22407 voffset = glyph->voffset;
22408 s->face = FACE_FROM_ID (s->f, face_id);
22409 s->font = s->face->font;
22410 s->nchars = 1;
22411 s->width = glyph->pixel_width;
22412 glyph++;
22413 while (glyph < last
22414 && glyph->type == GLYPHLESS_GLYPH
22415 && glyph->voffset == voffset
22416 && glyph->face_id == face_id)
22418 s->nchars++;
22419 s->width += glyph->pixel_width;
22420 glyph++;
22422 s->ybase += voffset;
22423 return glyph - s->row->glyphs[s->area];
22427 /* Fill glyph string S from a sequence of character glyphs.
22429 FACE_ID is the face id of the string. START is the index of the
22430 first glyph to consider, END is the index of the last + 1.
22431 OVERLAPS non-zero means S should draw the foreground only, and use
22432 its physical height for clipping. See also draw_glyphs.
22434 Value is the index of the first glyph not in S. */
22436 static int
22437 fill_glyph_string (struct glyph_string *s, int face_id,
22438 int start, int end, int overlaps)
22440 struct glyph *glyph, *last;
22441 int voffset;
22442 int glyph_not_available_p;
22444 xassert (s->f == XFRAME (s->w->frame));
22445 xassert (s->nchars == 0);
22446 xassert (start >= 0 && end > start);
22448 s->for_overlaps = overlaps;
22449 glyph = s->row->glyphs[s->area] + start;
22450 last = s->row->glyphs[s->area] + end;
22451 voffset = glyph->voffset;
22452 s->padding_p = glyph->padding_p;
22453 glyph_not_available_p = glyph->glyph_not_available_p;
22455 while (glyph < last
22456 && glyph->type == CHAR_GLYPH
22457 && glyph->voffset == voffset
22458 /* Same face id implies same font, nowadays. */
22459 && glyph->face_id == face_id
22460 && glyph->glyph_not_available_p == glyph_not_available_p)
22462 int two_byte_p;
22464 s->face = get_glyph_face_and_encoding (s->f, glyph,
22465 s->char2b + s->nchars,
22466 &two_byte_p);
22467 s->two_byte_p = two_byte_p;
22468 ++s->nchars;
22469 xassert (s->nchars <= end - start);
22470 s->width += glyph->pixel_width;
22471 if (glyph++->padding_p != s->padding_p)
22472 break;
22475 s->font = s->face->font;
22477 /* If the specified font could not be loaded, use the frame's font,
22478 but record the fact that we couldn't load it in
22479 S->font_not_found_p so that we can draw rectangles for the
22480 characters of the glyph string. */
22481 if (s->font == NULL || glyph_not_available_p)
22483 s->font_not_found_p = 1;
22484 s->font = FRAME_FONT (s->f);
22487 /* Adjust base line for subscript/superscript text. */
22488 s->ybase += voffset;
22490 xassert (s->face && s->face->gc);
22491 return glyph - s->row->glyphs[s->area];
22495 /* Fill glyph string S from image glyph S->first_glyph. */
22497 static void
22498 fill_image_glyph_string (struct glyph_string *s)
22500 xassert (s->first_glyph->type == IMAGE_GLYPH);
22501 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22502 xassert (s->img);
22503 s->slice = s->first_glyph->slice.img;
22504 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22505 s->font = s->face->font;
22506 s->width = s->first_glyph->pixel_width;
22508 /* Adjust base line for subscript/superscript text. */
22509 s->ybase += s->first_glyph->voffset;
22513 /* Fill glyph string S from a sequence of stretch glyphs.
22515 START is the index of the first glyph to consider,
22516 END is the index of the last + 1.
22518 Value is the index of the first glyph not in S. */
22520 static int
22521 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22523 struct glyph *glyph, *last;
22524 int voffset, face_id;
22526 xassert (s->first_glyph->type == STRETCH_GLYPH);
22528 glyph = s->row->glyphs[s->area] + start;
22529 last = s->row->glyphs[s->area] + end;
22530 face_id = glyph->face_id;
22531 s->face = FACE_FROM_ID (s->f, face_id);
22532 s->font = s->face->font;
22533 s->width = glyph->pixel_width;
22534 s->nchars = 1;
22535 voffset = glyph->voffset;
22537 for (++glyph;
22538 (glyph < last
22539 && glyph->type == STRETCH_GLYPH
22540 && glyph->voffset == voffset
22541 && glyph->face_id == face_id);
22542 ++glyph)
22543 s->width += glyph->pixel_width;
22545 /* Adjust base line for subscript/superscript text. */
22546 s->ybase += voffset;
22548 /* The case that face->gc == 0 is handled when drawing the glyph
22549 string by calling PREPARE_FACE_FOR_DISPLAY. */
22550 xassert (s->face);
22551 return glyph - s->row->glyphs[s->area];
22554 static struct font_metrics *
22555 get_per_char_metric (struct font *font, XChar2b *char2b)
22557 static struct font_metrics metrics;
22558 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22560 if (! font || code == FONT_INVALID_CODE)
22561 return NULL;
22562 font->driver->text_extents (font, &code, 1, &metrics);
22563 return &metrics;
22566 /* EXPORT for RIF:
22567 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22568 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22569 assumed to be zero. */
22571 void
22572 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22574 *left = *right = 0;
22576 if (glyph->type == CHAR_GLYPH)
22578 struct face *face;
22579 XChar2b char2b;
22580 struct font_metrics *pcm;
22582 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22583 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22585 if (pcm->rbearing > pcm->width)
22586 *right = pcm->rbearing - pcm->width;
22587 if (pcm->lbearing < 0)
22588 *left = -pcm->lbearing;
22591 else if (glyph->type == COMPOSITE_GLYPH)
22593 if (! glyph->u.cmp.automatic)
22595 struct composition *cmp = composition_table[glyph->u.cmp.id];
22597 if (cmp->rbearing > cmp->pixel_width)
22598 *right = cmp->rbearing - cmp->pixel_width;
22599 if (cmp->lbearing < 0)
22600 *left = - cmp->lbearing;
22602 else
22604 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22605 struct font_metrics metrics;
22607 composition_gstring_width (gstring, glyph->slice.cmp.from,
22608 glyph->slice.cmp.to + 1, &metrics);
22609 if (metrics.rbearing > metrics.width)
22610 *right = metrics.rbearing - metrics.width;
22611 if (metrics.lbearing < 0)
22612 *left = - metrics.lbearing;
22618 /* Return the index of the first glyph preceding glyph string S that
22619 is overwritten by S because of S's left overhang. Value is -1
22620 if no glyphs are overwritten. */
22622 static int
22623 left_overwritten (struct glyph_string *s)
22625 int k;
22627 if (s->left_overhang)
22629 int x = 0, i;
22630 struct glyph *glyphs = s->row->glyphs[s->area];
22631 int first = s->first_glyph - glyphs;
22633 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22634 x -= glyphs[i].pixel_width;
22636 k = i + 1;
22638 else
22639 k = -1;
22641 return k;
22645 /* Return the index of the first glyph preceding glyph string S that
22646 is overwriting S because of its right overhang. Value is -1 if no
22647 glyph in front of S overwrites S. */
22649 static int
22650 left_overwriting (struct glyph_string *s)
22652 int i, k, x;
22653 struct glyph *glyphs = s->row->glyphs[s->area];
22654 int first = s->first_glyph - glyphs;
22656 k = -1;
22657 x = 0;
22658 for (i = first - 1; i >= 0; --i)
22660 int left, right;
22661 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22662 if (x + right > 0)
22663 k = i;
22664 x -= glyphs[i].pixel_width;
22667 return k;
22671 /* Return the index of the last glyph following glyph string S that is
22672 overwritten by S because of S's right overhang. Value is -1 if
22673 no such glyph is found. */
22675 static int
22676 right_overwritten (struct glyph_string *s)
22678 int k = -1;
22680 if (s->right_overhang)
22682 int x = 0, i;
22683 struct glyph *glyphs = s->row->glyphs[s->area];
22684 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22685 int end = s->row->used[s->area];
22687 for (i = first; i < end && s->right_overhang > x; ++i)
22688 x += glyphs[i].pixel_width;
22690 k = i;
22693 return k;
22697 /* Return the index of the last glyph following glyph string S that
22698 overwrites S because of its left overhang. Value is negative
22699 if no such glyph is found. */
22701 static int
22702 right_overwriting (struct glyph_string *s)
22704 int i, k, x;
22705 int end = s->row->used[s->area];
22706 struct glyph *glyphs = s->row->glyphs[s->area];
22707 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22709 k = -1;
22710 x = 0;
22711 for (i = first; i < end; ++i)
22713 int left, right;
22714 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22715 if (x - left < 0)
22716 k = i;
22717 x += glyphs[i].pixel_width;
22720 return k;
22724 /* Set background width of glyph string S. START is the index of the
22725 first glyph following S. LAST_X is the right-most x-position + 1
22726 in the drawing area. */
22728 static inline void
22729 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22731 /* If the face of this glyph string has to be drawn to the end of
22732 the drawing area, set S->extends_to_end_of_line_p. */
22734 if (start == s->row->used[s->area]
22735 && s->area == TEXT_AREA
22736 && ((s->row->fill_line_p
22737 && (s->hl == DRAW_NORMAL_TEXT
22738 || s->hl == DRAW_IMAGE_RAISED
22739 || s->hl == DRAW_IMAGE_SUNKEN))
22740 || s->hl == DRAW_MOUSE_FACE))
22741 s->extends_to_end_of_line_p = 1;
22743 /* If S extends its face to the end of the line, set its
22744 background_width to the distance to the right edge of the drawing
22745 area. */
22746 if (s->extends_to_end_of_line_p)
22747 s->background_width = last_x - s->x + 1;
22748 else
22749 s->background_width = s->width;
22753 /* Compute overhangs and x-positions for glyph string S and its
22754 predecessors, or successors. X is the starting x-position for S.
22755 BACKWARD_P non-zero means process predecessors. */
22757 static void
22758 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22760 if (backward_p)
22762 while (s)
22764 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22765 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22766 x -= s->width;
22767 s->x = x;
22768 s = s->prev;
22771 else
22773 while (s)
22775 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22776 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22777 s->x = x;
22778 x += s->width;
22779 s = s->next;
22786 /* The following macros are only called from draw_glyphs below.
22787 They reference the following parameters of that function directly:
22788 `w', `row', `area', and `overlap_p'
22789 as well as the following local variables:
22790 `s', `f', and `hdc' (in W32) */
22792 #ifdef HAVE_NTGUI
22793 /* On W32, silently add local `hdc' variable to argument list of
22794 init_glyph_string. */
22795 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22796 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
22797 #else
22798 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22799 init_glyph_string (s, char2b, w, row, area, start, hl)
22800 #endif
22802 /* Add a glyph string for a stretch glyph to the list of strings
22803 between HEAD and TAIL. START is the index of the stretch glyph in
22804 row area AREA of glyph row ROW. END is the index of the last glyph
22805 in that glyph row area. X is the current output position assigned
22806 to the new glyph string constructed. HL overrides that face of the
22807 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22808 is the right-most x-position of the drawing area. */
22810 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
22811 and below -- keep them on one line. */
22812 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22813 do \
22815 s = (struct glyph_string *) alloca (sizeof *s); \
22816 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22817 START = fill_stretch_glyph_string (s, START, END); \
22818 append_glyph_string (&HEAD, &TAIL, s); \
22819 s->x = (X); \
22821 while (0)
22824 /* Add a glyph string for an image glyph to the list of strings
22825 between HEAD and TAIL. START is the index of the image glyph in
22826 row area AREA of glyph row ROW. END is the index of the last glyph
22827 in that glyph row area. X is the current output position assigned
22828 to the new glyph string constructed. HL overrides that face of the
22829 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22830 is the right-most x-position of the drawing area. */
22832 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22833 do \
22835 s = (struct glyph_string *) alloca (sizeof *s); \
22836 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22837 fill_image_glyph_string (s); \
22838 append_glyph_string (&HEAD, &TAIL, s); \
22839 ++START; \
22840 s->x = (X); \
22842 while (0)
22845 /* Add a glyph string for a sequence of character glyphs to the list
22846 of strings between HEAD and TAIL. START is the index of the first
22847 glyph in row area AREA of glyph row ROW that is part of the new
22848 glyph string. END is the index of the last glyph in that glyph row
22849 area. X is the current output position assigned to the new glyph
22850 string constructed. HL overrides that face of the glyph; e.g. it
22851 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
22852 right-most x-position of the drawing area. */
22854 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22855 do \
22857 int face_id; \
22858 XChar2b *char2b; \
22860 face_id = (row)->glyphs[area][START].face_id; \
22862 s = (struct glyph_string *) alloca (sizeof *s); \
22863 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
22864 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22865 append_glyph_string (&HEAD, &TAIL, s); \
22866 s->x = (X); \
22867 START = fill_glyph_string (s, face_id, START, END, overlaps); \
22869 while (0)
22872 /* Add a glyph string for a composite sequence to the list of strings
22873 between HEAD and TAIL. START is the index of the first glyph in
22874 row area AREA of glyph row ROW that is part of the new glyph
22875 string. END is the index of the last glyph in that glyph row area.
22876 X is the current output position assigned to the new glyph string
22877 constructed. HL overrides that face of the glyph; e.g. it is
22878 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
22879 x-position of the drawing area. */
22881 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22882 do { \
22883 int face_id = (row)->glyphs[area][START].face_id; \
22884 struct face *base_face = FACE_FROM_ID (f, face_id); \
22885 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
22886 struct composition *cmp = composition_table[cmp_id]; \
22887 XChar2b *char2b; \
22888 struct glyph_string *first_s = NULL; \
22889 int n; \
22891 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
22893 /* Make glyph_strings for each glyph sequence that is drawable by \
22894 the same face, and append them to HEAD/TAIL. */ \
22895 for (n = 0; n < cmp->glyph_len;) \
22897 s = (struct glyph_string *) alloca (sizeof *s); \
22898 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22899 append_glyph_string (&(HEAD), &(TAIL), s); \
22900 s->cmp = cmp; \
22901 s->cmp_from = n; \
22902 s->x = (X); \
22903 if (n == 0) \
22904 first_s = s; \
22905 n = fill_composite_glyph_string (s, base_face, overlaps); \
22908 ++START; \
22909 s = first_s; \
22910 } while (0)
22913 /* Add a glyph string for a glyph-string sequence to the list of strings
22914 between HEAD and TAIL. */
22916 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22917 do { \
22918 int face_id; \
22919 XChar2b *char2b; \
22920 Lisp_Object gstring; \
22922 face_id = (row)->glyphs[area][START].face_id; \
22923 gstring = (composition_gstring_from_id \
22924 ((row)->glyphs[area][START].u.cmp.id)); \
22925 s = (struct glyph_string *) alloca (sizeof *s); \
22926 char2b = (XChar2b *) alloca ((sizeof *char2b) \
22927 * LGSTRING_GLYPH_LEN (gstring)); \
22928 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22929 append_glyph_string (&(HEAD), &(TAIL), s); \
22930 s->x = (X); \
22931 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
22932 } while (0)
22935 /* Add a glyph string for a sequence of glyphless character's glyphs
22936 to the list of strings between HEAD and TAIL. The meanings of
22937 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
22939 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22940 do \
22942 int face_id; \
22944 face_id = (row)->glyphs[area][START].face_id; \
22946 s = (struct glyph_string *) alloca (sizeof *s); \
22947 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22948 append_glyph_string (&HEAD, &TAIL, s); \
22949 s->x = (X); \
22950 START = fill_glyphless_glyph_string (s, face_id, START, END, \
22951 overlaps); \
22953 while (0)
22956 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
22957 of AREA of glyph row ROW on window W between indices START and END.
22958 HL overrides the face for drawing glyph strings, e.g. it is
22959 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
22960 x-positions of the drawing area.
22962 This is an ugly monster macro construct because we must use alloca
22963 to allocate glyph strings (because draw_glyphs can be called
22964 asynchronously). */
22966 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22967 do \
22969 HEAD = TAIL = NULL; \
22970 while (START < END) \
22972 struct glyph *first_glyph = (row)->glyphs[area] + START; \
22973 switch (first_glyph->type) \
22975 case CHAR_GLYPH: \
22976 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
22977 HL, X, LAST_X); \
22978 break; \
22980 case COMPOSITE_GLYPH: \
22981 if (first_glyph->u.cmp.automatic) \
22982 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
22983 HL, X, LAST_X); \
22984 else \
22985 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
22986 HL, X, LAST_X); \
22987 break; \
22989 case STRETCH_GLYPH: \
22990 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
22991 HL, X, LAST_X); \
22992 break; \
22994 case IMAGE_GLYPH: \
22995 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
22996 HL, X, LAST_X); \
22997 break; \
22999 case GLYPHLESS_GLYPH: \
23000 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23001 HL, X, LAST_X); \
23002 break; \
23004 default: \
23005 abort (); \
23008 if (s) \
23010 set_glyph_string_background_width (s, START, LAST_X); \
23011 (X) += s->width; \
23014 } while (0)
23017 /* Draw glyphs between START and END in AREA of ROW on window W,
23018 starting at x-position X. X is relative to AREA in W. HL is a
23019 face-override with the following meaning:
23021 DRAW_NORMAL_TEXT draw normally
23022 DRAW_CURSOR draw in cursor face
23023 DRAW_MOUSE_FACE draw in mouse face.
23024 DRAW_INVERSE_VIDEO draw in mode line face
23025 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23026 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23028 If OVERLAPS is non-zero, draw only the foreground of characters and
23029 clip to the physical height of ROW. Non-zero value also defines
23030 the overlapping part to be drawn:
23032 OVERLAPS_PRED overlap with preceding rows
23033 OVERLAPS_SUCC overlap with succeeding rows
23034 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23035 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23037 Value is the x-position reached, relative to AREA of W. */
23039 static int
23040 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23041 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
23042 enum draw_glyphs_face hl, int overlaps)
23044 struct glyph_string *head, *tail;
23045 struct glyph_string *s;
23046 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23047 int i, j, x_reached, last_x, area_left = 0;
23048 struct frame *f = XFRAME (WINDOW_FRAME (w));
23049 DECLARE_HDC (hdc);
23051 ALLOCATE_HDC (hdc, f);
23053 /* Let's rather be paranoid than getting a SEGV. */
23054 end = min (end, row->used[area]);
23055 start = max (0, start);
23056 start = min (end, start);
23058 /* Translate X to frame coordinates. Set last_x to the right
23059 end of the drawing area. */
23060 if (row->full_width_p)
23062 /* X is relative to the left edge of W, without scroll bars
23063 or fringes. */
23064 area_left = WINDOW_LEFT_EDGE_X (w);
23065 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23067 else
23069 area_left = window_box_left (w, area);
23070 last_x = area_left + window_box_width (w, area);
23072 x += area_left;
23074 /* Build a doubly-linked list of glyph_string structures between
23075 head and tail from what we have to draw. Note that the macro
23076 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23077 the reason we use a separate variable `i'. */
23078 i = start;
23079 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23080 if (tail)
23081 x_reached = tail->x + tail->background_width;
23082 else
23083 x_reached = x;
23085 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23086 the row, redraw some glyphs in front or following the glyph
23087 strings built above. */
23088 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23090 struct glyph_string *h, *t;
23091 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23092 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23093 int check_mouse_face = 0;
23094 int dummy_x = 0;
23096 /* If mouse highlighting is on, we may need to draw adjacent
23097 glyphs using mouse-face highlighting. */
23098 if (area == TEXT_AREA && row->mouse_face_p)
23100 struct glyph_row *mouse_beg_row, *mouse_end_row;
23102 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23103 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23105 if (row >= mouse_beg_row && row <= mouse_end_row)
23107 check_mouse_face = 1;
23108 mouse_beg_col = (row == mouse_beg_row)
23109 ? hlinfo->mouse_face_beg_col : 0;
23110 mouse_end_col = (row == mouse_end_row)
23111 ? hlinfo->mouse_face_end_col
23112 : row->used[TEXT_AREA];
23116 /* Compute overhangs for all glyph strings. */
23117 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23118 for (s = head; s; s = s->next)
23119 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23121 /* Prepend glyph strings for glyphs in front of the first glyph
23122 string that are overwritten because of the first glyph
23123 string's left overhang. The background of all strings
23124 prepended must be drawn because the first glyph string
23125 draws over it. */
23126 i = left_overwritten (head);
23127 if (i >= 0)
23129 enum draw_glyphs_face overlap_hl;
23131 /* If this row contains mouse highlighting, attempt to draw
23132 the overlapped glyphs with the correct highlight. This
23133 code fails if the overlap encompasses more than one glyph
23134 and mouse-highlight spans only some of these glyphs.
23135 However, making it work perfectly involves a lot more
23136 code, and I don't know if the pathological case occurs in
23137 practice, so we'll stick to this for now. --- cyd */
23138 if (check_mouse_face
23139 && mouse_beg_col < start && mouse_end_col > i)
23140 overlap_hl = DRAW_MOUSE_FACE;
23141 else
23142 overlap_hl = DRAW_NORMAL_TEXT;
23144 j = i;
23145 BUILD_GLYPH_STRINGS (j, start, h, t,
23146 overlap_hl, dummy_x, last_x);
23147 start = i;
23148 compute_overhangs_and_x (t, head->x, 1);
23149 prepend_glyph_string_lists (&head, &tail, h, t);
23150 clip_head = head;
23153 /* Prepend glyph strings for glyphs in front of the first glyph
23154 string that overwrite that glyph string because of their
23155 right overhang. For these strings, only the foreground must
23156 be drawn, because it draws over the glyph string at `head'.
23157 The background must not be drawn because this would overwrite
23158 right overhangs of preceding glyphs for which no glyph
23159 strings exist. */
23160 i = left_overwriting (head);
23161 if (i >= 0)
23163 enum draw_glyphs_face overlap_hl;
23165 if (check_mouse_face
23166 && mouse_beg_col < start && mouse_end_col > i)
23167 overlap_hl = DRAW_MOUSE_FACE;
23168 else
23169 overlap_hl = DRAW_NORMAL_TEXT;
23171 clip_head = head;
23172 BUILD_GLYPH_STRINGS (i, start, h, t,
23173 overlap_hl, dummy_x, last_x);
23174 for (s = h; s; s = s->next)
23175 s->background_filled_p = 1;
23176 compute_overhangs_and_x (t, head->x, 1);
23177 prepend_glyph_string_lists (&head, &tail, h, t);
23180 /* Append glyphs strings for glyphs following the last glyph
23181 string tail that are overwritten by tail. The background of
23182 these strings has to be drawn because tail's foreground draws
23183 over it. */
23184 i = right_overwritten (tail);
23185 if (i >= 0)
23187 enum draw_glyphs_face overlap_hl;
23189 if (check_mouse_face
23190 && mouse_beg_col < i && mouse_end_col > end)
23191 overlap_hl = DRAW_MOUSE_FACE;
23192 else
23193 overlap_hl = DRAW_NORMAL_TEXT;
23195 BUILD_GLYPH_STRINGS (end, i, h, t,
23196 overlap_hl, x, last_x);
23197 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23198 we don't have `end = i;' here. */
23199 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23200 append_glyph_string_lists (&head, &tail, h, t);
23201 clip_tail = tail;
23204 /* Append glyph strings for glyphs following the last glyph
23205 string tail that overwrite tail. The foreground of such
23206 glyphs has to be drawn because it writes into the background
23207 of tail. The background must not be drawn because it could
23208 paint over the foreground of following glyphs. */
23209 i = right_overwriting (tail);
23210 if (i >= 0)
23212 enum draw_glyphs_face overlap_hl;
23213 if (check_mouse_face
23214 && mouse_beg_col < i && mouse_end_col > end)
23215 overlap_hl = DRAW_MOUSE_FACE;
23216 else
23217 overlap_hl = DRAW_NORMAL_TEXT;
23219 clip_tail = tail;
23220 i++; /* We must include the Ith glyph. */
23221 BUILD_GLYPH_STRINGS (end, i, h, t,
23222 overlap_hl, x, last_x);
23223 for (s = h; s; s = s->next)
23224 s->background_filled_p = 1;
23225 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23226 append_glyph_string_lists (&head, &tail, h, t);
23228 if (clip_head || clip_tail)
23229 for (s = head; s; s = s->next)
23231 s->clip_head = clip_head;
23232 s->clip_tail = clip_tail;
23236 /* Draw all strings. */
23237 for (s = head; s; s = s->next)
23238 FRAME_RIF (f)->draw_glyph_string (s);
23240 #ifndef HAVE_NS
23241 /* When focus a sole frame and move horizontally, this sets on_p to 0
23242 causing a failure to erase prev cursor position. */
23243 if (area == TEXT_AREA
23244 && !row->full_width_p
23245 /* When drawing overlapping rows, only the glyph strings'
23246 foreground is drawn, which doesn't erase a cursor
23247 completely. */
23248 && !overlaps)
23250 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23251 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23252 : (tail ? tail->x + tail->background_width : x));
23253 x0 -= area_left;
23254 x1 -= area_left;
23256 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23257 row->y, MATRIX_ROW_BOTTOM_Y (row));
23259 #endif
23261 /* Value is the x-position up to which drawn, relative to AREA of W.
23262 This doesn't include parts drawn because of overhangs. */
23263 if (row->full_width_p)
23264 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23265 else
23266 x_reached -= area_left;
23268 RELEASE_HDC (hdc, f);
23270 return x_reached;
23273 /* Expand row matrix if too narrow. Don't expand if area
23274 is not present. */
23276 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23278 if (!fonts_changed_p \
23279 && (it->glyph_row->glyphs[area] \
23280 < it->glyph_row->glyphs[area + 1])) \
23282 it->w->ncols_scale_factor++; \
23283 fonts_changed_p = 1; \
23287 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23288 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23290 static inline void
23291 append_glyph (struct it *it)
23293 struct glyph *glyph;
23294 enum glyph_row_area area = it->area;
23296 xassert (it->glyph_row);
23297 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23299 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23300 if (glyph < it->glyph_row->glyphs[area + 1])
23302 /* If the glyph row is reversed, we need to prepend the glyph
23303 rather than append it. */
23304 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23306 struct glyph *g;
23308 /* Make room for the additional glyph. */
23309 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23310 g[1] = *g;
23311 glyph = it->glyph_row->glyphs[area];
23313 glyph->charpos = CHARPOS (it->position);
23314 glyph->object = it->object;
23315 if (it->pixel_width > 0)
23317 glyph->pixel_width = it->pixel_width;
23318 glyph->padding_p = 0;
23320 else
23322 /* Assure at least 1-pixel width. Otherwise, cursor can't
23323 be displayed correctly. */
23324 glyph->pixel_width = 1;
23325 glyph->padding_p = 1;
23327 glyph->ascent = it->ascent;
23328 glyph->descent = it->descent;
23329 glyph->voffset = it->voffset;
23330 glyph->type = CHAR_GLYPH;
23331 glyph->avoid_cursor_p = it->avoid_cursor_p;
23332 glyph->multibyte_p = it->multibyte_p;
23333 glyph->left_box_line_p = it->start_of_box_run_p;
23334 glyph->right_box_line_p = it->end_of_box_run_p;
23335 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23336 || it->phys_descent > it->descent);
23337 glyph->glyph_not_available_p = it->glyph_not_available_p;
23338 glyph->face_id = it->face_id;
23339 glyph->u.ch = it->char_to_display;
23340 glyph->slice.img = null_glyph_slice;
23341 glyph->font_type = FONT_TYPE_UNKNOWN;
23342 if (it->bidi_p)
23344 glyph->resolved_level = it->bidi_it.resolved_level;
23345 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23346 abort ();
23347 glyph->bidi_type = it->bidi_it.type;
23349 else
23351 glyph->resolved_level = 0;
23352 glyph->bidi_type = UNKNOWN_BT;
23354 ++it->glyph_row->used[area];
23356 else
23357 IT_EXPAND_MATRIX_WIDTH (it, area);
23360 /* Store one glyph for the composition IT->cmp_it.id in
23361 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23362 non-null. */
23364 static inline void
23365 append_composite_glyph (struct it *it)
23367 struct glyph *glyph;
23368 enum glyph_row_area area = it->area;
23370 xassert (it->glyph_row);
23372 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23373 if (glyph < it->glyph_row->glyphs[area + 1])
23375 /* If the glyph row is reversed, we need to prepend the glyph
23376 rather than append it. */
23377 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23379 struct glyph *g;
23381 /* Make room for the new glyph. */
23382 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23383 g[1] = *g;
23384 glyph = it->glyph_row->glyphs[it->area];
23386 glyph->charpos = it->cmp_it.charpos;
23387 glyph->object = it->object;
23388 glyph->pixel_width = it->pixel_width;
23389 glyph->ascent = it->ascent;
23390 glyph->descent = it->descent;
23391 glyph->voffset = it->voffset;
23392 glyph->type = COMPOSITE_GLYPH;
23393 if (it->cmp_it.ch < 0)
23395 glyph->u.cmp.automatic = 0;
23396 glyph->u.cmp.id = it->cmp_it.id;
23397 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23399 else
23401 glyph->u.cmp.automatic = 1;
23402 glyph->u.cmp.id = it->cmp_it.id;
23403 glyph->slice.cmp.from = it->cmp_it.from;
23404 glyph->slice.cmp.to = it->cmp_it.to - 1;
23406 glyph->avoid_cursor_p = it->avoid_cursor_p;
23407 glyph->multibyte_p = it->multibyte_p;
23408 glyph->left_box_line_p = it->start_of_box_run_p;
23409 glyph->right_box_line_p = it->end_of_box_run_p;
23410 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23411 || it->phys_descent > it->descent);
23412 glyph->padding_p = 0;
23413 glyph->glyph_not_available_p = 0;
23414 glyph->face_id = it->face_id;
23415 glyph->font_type = FONT_TYPE_UNKNOWN;
23416 if (it->bidi_p)
23418 glyph->resolved_level = it->bidi_it.resolved_level;
23419 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23420 abort ();
23421 glyph->bidi_type = it->bidi_it.type;
23423 ++it->glyph_row->used[area];
23425 else
23426 IT_EXPAND_MATRIX_WIDTH (it, area);
23430 /* Change IT->ascent and IT->height according to the setting of
23431 IT->voffset. */
23433 static inline void
23434 take_vertical_position_into_account (struct it *it)
23436 if (it->voffset)
23438 if (it->voffset < 0)
23439 /* Increase the ascent so that we can display the text higher
23440 in the line. */
23441 it->ascent -= it->voffset;
23442 else
23443 /* Increase the descent so that we can display the text lower
23444 in the line. */
23445 it->descent += it->voffset;
23450 /* Produce glyphs/get display metrics for the image IT is loaded with.
23451 See the description of struct display_iterator in dispextern.h for
23452 an overview of struct display_iterator. */
23454 static void
23455 produce_image_glyph (struct it *it)
23457 struct image *img;
23458 struct face *face;
23459 int glyph_ascent, crop;
23460 struct glyph_slice slice;
23462 xassert (it->what == IT_IMAGE);
23464 face = FACE_FROM_ID (it->f, it->face_id);
23465 xassert (face);
23466 /* Make sure X resources of the face is loaded. */
23467 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23469 if (it->image_id < 0)
23471 /* Fringe bitmap. */
23472 it->ascent = it->phys_ascent = 0;
23473 it->descent = it->phys_descent = 0;
23474 it->pixel_width = 0;
23475 it->nglyphs = 0;
23476 return;
23479 img = IMAGE_FROM_ID (it->f, it->image_id);
23480 xassert (img);
23481 /* Make sure X resources of the image is loaded. */
23482 prepare_image_for_display (it->f, img);
23484 slice.x = slice.y = 0;
23485 slice.width = img->width;
23486 slice.height = img->height;
23488 if (INTEGERP (it->slice.x))
23489 slice.x = XINT (it->slice.x);
23490 else if (FLOATP (it->slice.x))
23491 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23493 if (INTEGERP (it->slice.y))
23494 slice.y = XINT (it->slice.y);
23495 else if (FLOATP (it->slice.y))
23496 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23498 if (INTEGERP (it->slice.width))
23499 slice.width = XINT (it->slice.width);
23500 else if (FLOATP (it->slice.width))
23501 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23503 if (INTEGERP (it->slice.height))
23504 slice.height = XINT (it->slice.height);
23505 else if (FLOATP (it->slice.height))
23506 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23508 if (slice.x >= img->width)
23509 slice.x = img->width;
23510 if (slice.y >= img->height)
23511 slice.y = img->height;
23512 if (slice.x + slice.width >= img->width)
23513 slice.width = img->width - slice.x;
23514 if (slice.y + slice.height > img->height)
23515 slice.height = img->height - slice.y;
23517 if (slice.width == 0 || slice.height == 0)
23518 return;
23520 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23522 it->descent = slice.height - glyph_ascent;
23523 if (slice.y == 0)
23524 it->descent += img->vmargin;
23525 if (slice.y + slice.height == img->height)
23526 it->descent += img->vmargin;
23527 it->phys_descent = it->descent;
23529 it->pixel_width = slice.width;
23530 if (slice.x == 0)
23531 it->pixel_width += img->hmargin;
23532 if (slice.x + slice.width == img->width)
23533 it->pixel_width += img->hmargin;
23535 /* It's quite possible for images to have an ascent greater than
23536 their height, so don't get confused in that case. */
23537 if (it->descent < 0)
23538 it->descent = 0;
23540 it->nglyphs = 1;
23542 if (face->box != FACE_NO_BOX)
23544 if (face->box_line_width > 0)
23546 if (slice.y == 0)
23547 it->ascent += face->box_line_width;
23548 if (slice.y + slice.height == img->height)
23549 it->descent += face->box_line_width;
23552 if (it->start_of_box_run_p && slice.x == 0)
23553 it->pixel_width += eabs (face->box_line_width);
23554 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23555 it->pixel_width += eabs (face->box_line_width);
23558 take_vertical_position_into_account (it);
23560 /* Automatically crop wide image glyphs at right edge so we can
23561 draw the cursor on same display row. */
23562 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23563 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23565 it->pixel_width -= crop;
23566 slice.width -= crop;
23569 if (it->glyph_row)
23571 struct glyph *glyph;
23572 enum glyph_row_area area = it->area;
23574 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23575 if (glyph < it->glyph_row->glyphs[area + 1])
23577 glyph->charpos = CHARPOS (it->position);
23578 glyph->object = it->object;
23579 glyph->pixel_width = it->pixel_width;
23580 glyph->ascent = glyph_ascent;
23581 glyph->descent = it->descent;
23582 glyph->voffset = it->voffset;
23583 glyph->type = IMAGE_GLYPH;
23584 glyph->avoid_cursor_p = it->avoid_cursor_p;
23585 glyph->multibyte_p = it->multibyte_p;
23586 glyph->left_box_line_p = it->start_of_box_run_p;
23587 glyph->right_box_line_p = it->end_of_box_run_p;
23588 glyph->overlaps_vertically_p = 0;
23589 glyph->padding_p = 0;
23590 glyph->glyph_not_available_p = 0;
23591 glyph->face_id = it->face_id;
23592 glyph->u.img_id = img->id;
23593 glyph->slice.img = slice;
23594 glyph->font_type = FONT_TYPE_UNKNOWN;
23595 if (it->bidi_p)
23597 glyph->resolved_level = it->bidi_it.resolved_level;
23598 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23599 abort ();
23600 glyph->bidi_type = it->bidi_it.type;
23602 ++it->glyph_row->used[area];
23604 else
23605 IT_EXPAND_MATRIX_WIDTH (it, area);
23610 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23611 of the glyph, WIDTH and HEIGHT are the width and height of the
23612 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23614 static void
23615 append_stretch_glyph (struct it *it, Lisp_Object object,
23616 int width, int height, int ascent)
23618 struct glyph *glyph;
23619 enum glyph_row_area area = it->area;
23621 xassert (ascent >= 0 && ascent <= height);
23623 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23624 if (glyph < it->glyph_row->glyphs[area + 1])
23626 /* If the glyph row is reversed, we need to prepend the glyph
23627 rather than append it. */
23628 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23630 struct glyph *g;
23632 /* Make room for the additional glyph. */
23633 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23634 g[1] = *g;
23635 glyph = it->glyph_row->glyphs[area];
23637 glyph->charpos = CHARPOS (it->position);
23638 glyph->object = object;
23639 glyph->pixel_width = width;
23640 glyph->ascent = ascent;
23641 glyph->descent = height - ascent;
23642 glyph->voffset = it->voffset;
23643 glyph->type = STRETCH_GLYPH;
23644 glyph->avoid_cursor_p = it->avoid_cursor_p;
23645 glyph->multibyte_p = it->multibyte_p;
23646 glyph->left_box_line_p = it->start_of_box_run_p;
23647 glyph->right_box_line_p = it->end_of_box_run_p;
23648 glyph->overlaps_vertically_p = 0;
23649 glyph->padding_p = 0;
23650 glyph->glyph_not_available_p = 0;
23651 glyph->face_id = it->face_id;
23652 glyph->u.stretch.ascent = ascent;
23653 glyph->u.stretch.height = height;
23654 glyph->slice.img = null_glyph_slice;
23655 glyph->font_type = FONT_TYPE_UNKNOWN;
23656 if (it->bidi_p)
23658 glyph->resolved_level = it->bidi_it.resolved_level;
23659 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23660 abort ();
23661 glyph->bidi_type = it->bidi_it.type;
23663 else
23665 glyph->resolved_level = 0;
23666 glyph->bidi_type = UNKNOWN_BT;
23668 ++it->glyph_row->used[area];
23670 else
23671 IT_EXPAND_MATRIX_WIDTH (it, area);
23674 #endif /* HAVE_WINDOW_SYSTEM */
23676 /* Produce a stretch glyph for iterator IT. IT->object is the value
23677 of the glyph property displayed. The value must be a list
23678 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23679 being recognized:
23681 1. `:width WIDTH' specifies that the space should be WIDTH *
23682 canonical char width wide. WIDTH may be an integer or floating
23683 point number.
23685 2. `:relative-width FACTOR' specifies that the width of the stretch
23686 should be computed from the width of the first character having the
23687 `glyph' property, and should be FACTOR times that width.
23689 3. `:align-to HPOS' specifies that the space should be wide enough
23690 to reach HPOS, a value in canonical character units.
23692 Exactly one of the above pairs must be present.
23694 4. `:height HEIGHT' specifies that the height of the stretch produced
23695 should be HEIGHT, measured in canonical character units.
23697 5. `:relative-height FACTOR' specifies that the height of the
23698 stretch should be FACTOR times the height of the characters having
23699 the glyph property.
23701 Either none or exactly one of 4 or 5 must be present.
23703 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23704 of the stretch should be used for the ascent of the stretch.
23705 ASCENT must be in the range 0 <= ASCENT <= 100. */
23707 void
23708 produce_stretch_glyph (struct it *it)
23710 /* (space :width WIDTH :height HEIGHT ...) */
23711 Lisp_Object prop, plist;
23712 int width = 0, height = 0, align_to = -1;
23713 int zero_width_ok_p = 0;
23714 int ascent = 0;
23715 double tem;
23716 struct face *face = NULL;
23717 struct font *font = NULL;
23719 #ifdef HAVE_WINDOW_SYSTEM
23720 int zero_height_ok_p = 0;
23722 if (FRAME_WINDOW_P (it->f))
23724 face = FACE_FROM_ID (it->f, it->face_id);
23725 font = face->font ? face->font : FRAME_FONT (it->f);
23726 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23728 #endif
23730 /* List should start with `space'. */
23731 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
23732 plist = XCDR (it->object);
23734 /* Compute the width of the stretch. */
23735 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
23736 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
23738 /* Absolute width `:width WIDTH' specified and valid. */
23739 zero_width_ok_p = 1;
23740 width = (int)tem;
23742 #ifdef HAVE_WINDOW_SYSTEM
23743 else if (FRAME_WINDOW_P (it->f)
23744 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
23746 /* Relative width `:relative-width FACTOR' specified and valid.
23747 Compute the width of the characters having the `glyph'
23748 property. */
23749 struct it it2;
23750 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
23752 it2 = *it;
23753 if (it->multibyte_p)
23754 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
23755 else
23757 it2.c = it2.char_to_display = *p, it2.len = 1;
23758 if (! ASCII_CHAR_P (it2.c))
23759 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
23762 it2.glyph_row = NULL;
23763 it2.what = IT_CHARACTER;
23764 x_produce_glyphs (&it2);
23765 width = NUMVAL (prop) * it2.pixel_width;
23767 #endif /* HAVE_WINDOW_SYSTEM */
23768 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
23769 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
23771 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
23772 align_to = (align_to < 0
23774 : align_to - window_box_left_offset (it->w, TEXT_AREA));
23775 else if (align_to < 0)
23776 align_to = window_box_left_offset (it->w, TEXT_AREA);
23777 width = max (0, (int)tem + align_to - it->current_x);
23778 zero_width_ok_p = 1;
23780 else
23781 /* Nothing specified -> width defaults to canonical char width. */
23782 width = FRAME_COLUMN_WIDTH (it->f);
23784 if (width <= 0 && (width < 0 || !zero_width_ok_p))
23785 width = 1;
23787 #ifdef HAVE_WINDOW_SYSTEM
23788 /* Compute height. */
23789 if (FRAME_WINDOW_P (it->f))
23791 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
23792 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23794 height = (int)tem;
23795 zero_height_ok_p = 1;
23797 else if (prop = Fplist_get (plist, QCrelative_height),
23798 NUMVAL (prop) > 0)
23799 height = FONT_HEIGHT (font) * NUMVAL (prop);
23800 else
23801 height = FONT_HEIGHT (font);
23803 if (height <= 0 && (height < 0 || !zero_height_ok_p))
23804 height = 1;
23806 /* Compute percentage of height used for ascent. If
23807 `:ascent ASCENT' is present and valid, use that. Otherwise,
23808 derive the ascent from the font in use. */
23809 if (prop = Fplist_get (plist, QCascent),
23810 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
23811 ascent = height * NUMVAL (prop) / 100.0;
23812 else if (!NILP (prop)
23813 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23814 ascent = min (max (0, (int)tem), height);
23815 else
23816 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
23818 else
23819 #endif /* HAVE_WINDOW_SYSTEM */
23820 height = 1;
23822 if (width > 0 && it->line_wrap != TRUNCATE
23823 && it->current_x + width > it->last_visible_x)
23825 width = it->last_visible_x - it->current_x;
23826 #ifdef HAVE_WINDOW_SYSTEM
23827 /* Subtract one more pixel from the stretch width, but only on
23828 GUI frames, since on a TTY each glyph is one "pixel" wide. */
23829 width -= FRAME_WINDOW_P (it->f);
23830 #endif
23833 if (width > 0 && height > 0 && it->glyph_row)
23835 Lisp_Object o_object = it->object;
23836 Lisp_Object object = it->stack[it->sp - 1].string;
23837 int n = width;
23839 if (!STRINGP (object))
23840 object = it->w->buffer;
23841 #ifdef HAVE_WINDOW_SYSTEM
23842 if (FRAME_WINDOW_P (it->f))
23843 append_stretch_glyph (it, object, width, height, ascent);
23844 else
23845 #endif
23847 it->object = object;
23848 it->char_to_display = ' ';
23849 it->pixel_width = it->len = 1;
23850 while (n--)
23851 tty_append_glyph (it);
23852 it->object = o_object;
23856 it->pixel_width = width;
23857 #ifdef HAVE_WINDOW_SYSTEM
23858 if (FRAME_WINDOW_P (it->f))
23860 it->ascent = it->phys_ascent = ascent;
23861 it->descent = it->phys_descent = height - it->ascent;
23862 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
23863 take_vertical_position_into_account (it);
23865 else
23866 #endif
23867 it->nglyphs = width;
23870 #ifdef HAVE_WINDOW_SYSTEM
23872 /* Calculate line-height and line-spacing properties.
23873 An integer value specifies explicit pixel value.
23874 A float value specifies relative value to current face height.
23875 A cons (float . face-name) specifies relative value to
23876 height of specified face font.
23878 Returns height in pixels, or nil. */
23881 static Lisp_Object
23882 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
23883 int boff, int override)
23885 Lisp_Object face_name = Qnil;
23886 int ascent, descent, height;
23888 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
23889 return val;
23891 if (CONSP (val))
23893 face_name = XCAR (val);
23894 val = XCDR (val);
23895 if (!NUMBERP (val))
23896 val = make_number (1);
23897 if (NILP (face_name))
23899 height = it->ascent + it->descent;
23900 goto scale;
23904 if (NILP (face_name))
23906 font = FRAME_FONT (it->f);
23907 boff = FRAME_BASELINE_OFFSET (it->f);
23909 else if (EQ (face_name, Qt))
23911 override = 0;
23913 else
23915 int face_id;
23916 struct face *face;
23918 face_id = lookup_named_face (it->f, face_name, 0);
23919 if (face_id < 0)
23920 return make_number (-1);
23922 face = FACE_FROM_ID (it->f, face_id);
23923 font = face->font;
23924 if (font == NULL)
23925 return make_number (-1);
23926 boff = font->baseline_offset;
23927 if (font->vertical_centering)
23928 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23931 ascent = FONT_BASE (font) + boff;
23932 descent = FONT_DESCENT (font) - boff;
23934 if (override)
23936 it->override_ascent = ascent;
23937 it->override_descent = descent;
23938 it->override_boff = boff;
23941 height = ascent + descent;
23943 scale:
23944 if (FLOATP (val))
23945 height = (int)(XFLOAT_DATA (val) * height);
23946 else if (INTEGERP (val))
23947 height *= XINT (val);
23949 return make_number (height);
23953 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
23954 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
23955 and only if this is for a character for which no font was found.
23957 If the display method (it->glyphless_method) is
23958 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
23959 length of the acronym or the hexadecimal string, UPPER_XOFF and
23960 UPPER_YOFF are pixel offsets for the upper part of the string,
23961 LOWER_XOFF and LOWER_YOFF are for the lower part.
23963 For the other display methods, LEN through LOWER_YOFF are zero. */
23965 static void
23966 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
23967 short upper_xoff, short upper_yoff,
23968 short lower_xoff, short lower_yoff)
23970 struct glyph *glyph;
23971 enum glyph_row_area area = it->area;
23973 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23974 if (glyph < it->glyph_row->glyphs[area + 1])
23976 /* If the glyph row is reversed, we need to prepend the glyph
23977 rather than append it. */
23978 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23980 struct glyph *g;
23982 /* Make room for the additional glyph. */
23983 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23984 g[1] = *g;
23985 glyph = it->glyph_row->glyphs[area];
23987 glyph->charpos = CHARPOS (it->position);
23988 glyph->object = it->object;
23989 glyph->pixel_width = it->pixel_width;
23990 glyph->ascent = it->ascent;
23991 glyph->descent = it->descent;
23992 glyph->voffset = it->voffset;
23993 glyph->type = GLYPHLESS_GLYPH;
23994 glyph->u.glyphless.method = it->glyphless_method;
23995 glyph->u.glyphless.for_no_font = for_no_font;
23996 glyph->u.glyphless.len = len;
23997 glyph->u.glyphless.ch = it->c;
23998 glyph->slice.glyphless.upper_xoff = upper_xoff;
23999 glyph->slice.glyphless.upper_yoff = upper_yoff;
24000 glyph->slice.glyphless.lower_xoff = lower_xoff;
24001 glyph->slice.glyphless.lower_yoff = lower_yoff;
24002 glyph->avoid_cursor_p = it->avoid_cursor_p;
24003 glyph->multibyte_p = it->multibyte_p;
24004 glyph->left_box_line_p = it->start_of_box_run_p;
24005 glyph->right_box_line_p = it->end_of_box_run_p;
24006 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24007 || it->phys_descent > it->descent);
24008 glyph->padding_p = 0;
24009 glyph->glyph_not_available_p = 0;
24010 glyph->face_id = face_id;
24011 glyph->font_type = FONT_TYPE_UNKNOWN;
24012 if (it->bidi_p)
24014 glyph->resolved_level = it->bidi_it.resolved_level;
24015 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24016 abort ();
24017 glyph->bidi_type = it->bidi_it.type;
24019 ++it->glyph_row->used[area];
24021 else
24022 IT_EXPAND_MATRIX_WIDTH (it, area);
24026 /* Produce a glyph for a glyphless character for iterator IT.
24027 IT->glyphless_method specifies which method to use for displaying
24028 the character. See the description of enum
24029 glyphless_display_method in dispextern.h for the detail.
24031 FOR_NO_FONT is nonzero if and only if this is for a character for
24032 which no font was found. ACRONYM, if non-nil, is an acronym string
24033 for the character. */
24035 static void
24036 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24038 int face_id;
24039 struct face *face;
24040 struct font *font;
24041 int base_width, base_height, width, height;
24042 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24043 int len;
24045 /* Get the metrics of the base font. We always refer to the current
24046 ASCII face. */
24047 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24048 font = face->font ? face->font : FRAME_FONT (it->f);
24049 it->ascent = FONT_BASE (font) + font->baseline_offset;
24050 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24051 base_height = it->ascent + it->descent;
24052 base_width = font->average_width;
24054 /* Get a face ID for the glyph by utilizing a cache (the same way as
24055 done for `escape-glyph' in get_next_display_element). */
24056 if (it->f == last_glyphless_glyph_frame
24057 && it->face_id == last_glyphless_glyph_face_id)
24059 face_id = last_glyphless_glyph_merged_face_id;
24061 else
24063 /* Merge the `glyphless-char' face into the current face. */
24064 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24065 last_glyphless_glyph_frame = it->f;
24066 last_glyphless_glyph_face_id = it->face_id;
24067 last_glyphless_glyph_merged_face_id = face_id;
24070 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24072 it->pixel_width = THIN_SPACE_WIDTH;
24073 len = 0;
24074 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24076 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24078 width = CHAR_WIDTH (it->c);
24079 if (width == 0)
24080 width = 1;
24081 else if (width > 4)
24082 width = 4;
24083 it->pixel_width = base_width * width;
24084 len = 0;
24085 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24087 else
24089 char buf[7];
24090 const char *str;
24091 unsigned int code[6];
24092 int upper_len;
24093 int ascent, descent;
24094 struct font_metrics metrics_upper, metrics_lower;
24096 face = FACE_FROM_ID (it->f, face_id);
24097 font = face->font ? face->font : FRAME_FONT (it->f);
24098 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24100 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24102 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24103 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24104 if (CONSP (acronym))
24105 acronym = XCAR (acronym);
24106 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24108 else
24110 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24111 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24112 str = buf;
24114 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24115 code[len] = font->driver->encode_char (font, str[len]);
24116 upper_len = (len + 1) / 2;
24117 font->driver->text_extents (font, code, upper_len,
24118 &metrics_upper);
24119 font->driver->text_extents (font, code + upper_len, len - upper_len,
24120 &metrics_lower);
24124 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24125 width = max (metrics_upper.width, metrics_lower.width) + 4;
24126 upper_xoff = upper_yoff = 2; /* the typical case */
24127 if (base_width >= width)
24129 /* Align the upper to the left, the lower to the right. */
24130 it->pixel_width = base_width;
24131 lower_xoff = base_width - 2 - metrics_lower.width;
24133 else
24135 /* Center the shorter one. */
24136 it->pixel_width = width;
24137 if (metrics_upper.width >= metrics_lower.width)
24138 lower_xoff = (width - metrics_lower.width) / 2;
24139 else
24141 /* FIXME: This code doesn't look right. It formerly was
24142 missing the "lower_xoff = 0;", which couldn't have
24143 been right since it left lower_xoff uninitialized. */
24144 lower_xoff = 0;
24145 upper_xoff = (width - metrics_upper.width) / 2;
24149 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24150 top, bottom, and between upper and lower strings. */
24151 height = (metrics_upper.ascent + metrics_upper.descent
24152 + metrics_lower.ascent + metrics_lower.descent) + 5;
24153 /* Center vertically.
24154 H:base_height, D:base_descent
24155 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24157 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24158 descent = D - H/2 + h/2;
24159 lower_yoff = descent - 2 - ld;
24160 upper_yoff = lower_yoff - la - 1 - ud; */
24161 ascent = - (it->descent - (base_height + height + 1) / 2);
24162 descent = it->descent - (base_height - height) / 2;
24163 lower_yoff = descent - 2 - metrics_lower.descent;
24164 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24165 - metrics_upper.descent);
24166 /* Don't make the height shorter than the base height. */
24167 if (height > base_height)
24169 it->ascent = ascent;
24170 it->descent = descent;
24174 it->phys_ascent = it->ascent;
24175 it->phys_descent = it->descent;
24176 if (it->glyph_row)
24177 append_glyphless_glyph (it, face_id, for_no_font, len,
24178 upper_xoff, upper_yoff,
24179 lower_xoff, lower_yoff);
24180 it->nglyphs = 1;
24181 take_vertical_position_into_account (it);
24185 /* RIF:
24186 Produce glyphs/get display metrics for the display element IT is
24187 loaded with. See the description of struct it in dispextern.h
24188 for an overview of struct it. */
24190 void
24191 x_produce_glyphs (struct it *it)
24193 int extra_line_spacing = it->extra_line_spacing;
24195 it->glyph_not_available_p = 0;
24197 if (it->what == IT_CHARACTER)
24199 XChar2b char2b;
24200 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24201 struct font *font = face->font;
24202 struct font_metrics *pcm = NULL;
24203 int boff; /* baseline offset */
24205 if (font == NULL)
24207 /* When no suitable font is found, display this character by
24208 the method specified in the first extra slot of
24209 Vglyphless_char_display. */
24210 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24212 xassert (it->what == IT_GLYPHLESS);
24213 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24214 goto done;
24217 boff = font->baseline_offset;
24218 if (font->vertical_centering)
24219 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24221 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24223 int stretched_p;
24225 it->nglyphs = 1;
24227 if (it->override_ascent >= 0)
24229 it->ascent = it->override_ascent;
24230 it->descent = it->override_descent;
24231 boff = it->override_boff;
24233 else
24235 it->ascent = FONT_BASE (font) + boff;
24236 it->descent = FONT_DESCENT (font) - boff;
24239 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24241 pcm = get_per_char_metric (font, &char2b);
24242 if (pcm->width == 0
24243 && pcm->rbearing == 0 && pcm->lbearing == 0)
24244 pcm = NULL;
24247 if (pcm)
24249 it->phys_ascent = pcm->ascent + boff;
24250 it->phys_descent = pcm->descent - boff;
24251 it->pixel_width = pcm->width;
24253 else
24255 it->glyph_not_available_p = 1;
24256 it->phys_ascent = it->ascent;
24257 it->phys_descent = it->descent;
24258 it->pixel_width = font->space_width;
24261 if (it->constrain_row_ascent_descent_p)
24263 if (it->descent > it->max_descent)
24265 it->ascent += it->descent - it->max_descent;
24266 it->descent = it->max_descent;
24268 if (it->ascent > it->max_ascent)
24270 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24271 it->ascent = it->max_ascent;
24273 it->phys_ascent = min (it->phys_ascent, it->ascent);
24274 it->phys_descent = min (it->phys_descent, it->descent);
24275 extra_line_spacing = 0;
24278 /* If this is a space inside a region of text with
24279 `space-width' property, change its width. */
24280 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24281 if (stretched_p)
24282 it->pixel_width *= XFLOATINT (it->space_width);
24284 /* If face has a box, add the box thickness to the character
24285 height. If character has a box line to the left and/or
24286 right, add the box line width to the character's width. */
24287 if (face->box != FACE_NO_BOX)
24289 int thick = face->box_line_width;
24291 if (thick > 0)
24293 it->ascent += thick;
24294 it->descent += thick;
24296 else
24297 thick = -thick;
24299 if (it->start_of_box_run_p)
24300 it->pixel_width += thick;
24301 if (it->end_of_box_run_p)
24302 it->pixel_width += thick;
24305 /* If face has an overline, add the height of the overline
24306 (1 pixel) and a 1 pixel margin to the character height. */
24307 if (face->overline_p)
24308 it->ascent += overline_margin;
24310 if (it->constrain_row_ascent_descent_p)
24312 if (it->ascent > it->max_ascent)
24313 it->ascent = it->max_ascent;
24314 if (it->descent > it->max_descent)
24315 it->descent = it->max_descent;
24318 take_vertical_position_into_account (it);
24320 /* If we have to actually produce glyphs, do it. */
24321 if (it->glyph_row)
24323 if (stretched_p)
24325 /* Translate a space with a `space-width' property
24326 into a stretch glyph. */
24327 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24328 / FONT_HEIGHT (font));
24329 append_stretch_glyph (it, it->object, it->pixel_width,
24330 it->ascent + it->descent, ascent);
24332 else
24333 append_glyph (it);
24335 /* If characters with lbearing or rbearing are displayed
24336 in this line, record that fact in a flag of the
24337 glyph row. This is used to optimize X output code. */
24338 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24339 it->glyph_row->contains_overlapping_glyphs_p = 1;
24341 if (! stretched_p && it->pixel_width == 0)
24342 /* We assure that all visible glyphs have at least 1-pixel
24343 width. */
24344 it->pixel_width = 1;
24346 else if (it->char_to_display == '\n')
24348 /* A newline has no width, but we need the height of the
24349 line. But if previous part of the line sets a height,
24350 don't increase that height */
24352 Lisp_Object height;
24353 Lisp_Object total_height = Qnil;
24355 it->override_ascent = -1;
24356 it->pixel_width = 0;
24357 it->nglyphs = 0;
24359 height = get_it_property (it, Qline_height);
24360 /* Split (line-height total-height) list */
24361 if (CONSP (height)
24362 && CONSP (XCDR (height))
24363 && NILP (XCDR (XCDR (height))))
24365 total_height = XCAR (XCDR (height));
24366 height = XCAR (height);
24368 height = calc_line_height_property (it, height, font, boff, 1);
24370 if (it->override_ascent >= 0)
24372 it->ascent = it->override_ascent;
24373 it->descent = it->override_descent;
24374 boff = it->override_boff;
24376 else
24378 it->ascent = FONT_BASE (font) + boff;
24379 it->descent = FONT_DESCENT (font) - boff;
24382 if (EQ (height, Qt))
24384 if (it->descent > it->max_descent)
24386 it->ascent += it->descent - it->max_descent;
24387 it->descent = it->max_descent;
24389 if (it->ascent > it->max_ascent)
24391 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24392 it->ascent = it->max_ascent;
24394 it->phys_ascent = min (it->phys_ascent, it->ascent);
24395 it->phys_descent = min (it->phys_descent, it->descent);
24396 it->constrain_row_ascent_descent_p = 1;
24397 extra_line_spacing = 0;
24399 else
24401 Lisp_Object spacing;
24403 it->phys_ascent = it->ascent;
24404 it->phys_descent = it->descent;
24406 if ((it->max_ascent > 0 || it->max_descent > 0)
24407 && face->box != FACE_NO_BOX
24408 && face->box_line_width > 0)
24410 it->ascent += face->box_line_width;
24411 it->descent += face->box_line_width;
24413 if (!NILP (height)
24414 && XINT (height) > it->ascent + it->descent)
24415 it->ascent = XINT (height) - it->descent;
24417 if (!NILP (total_height))
24418 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24419 else
24421 spacing = get_it_property (it, Qline_spacing);
24422 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24424 if (INTEGERP (spacing))
24426 extra_line_spacing = XINT (spacing);
24427 if (!NILP (total_height))
24428 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24432 else /* i.e. (it->char_to_display == '\t') */
24434 if (font->space_width > 0)
24436 int tab_width = it->tab_width * font->space_width;
24437 int x = it->current_x + it->continuation_lines_width;
24438 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24440 /* If the distance from the current position to the next tab
24441 stop is less than a space character width, use the
24442 tab stop after that. */
24443 if (next_tab_x - x < font->space_width)
24444 next_tab_x += tab_width;
24446 it->pixel_width = next_tab_x - x;
24447 it->nglyphs = 1;
24448 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24449 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24451 if (it->glyph_row)
24453 append_stretch_glyph (it, it->object, it->pixel_width,
24454 it->ascent + it->descent, it->ascent);
24457 else
24459 it->pixel_width = 0;
24460 it->nglyphs = 1;
24464 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24466 /* A static composition.
24468 Note: A composition is represented as one glyph in the
24469 glyph matrix. There are no padding glyphs.
24471 Important note: pixel_width, ascent, and descent are the
24472 values of what is drawn by draw_glyphs (i.e. the values of
24473 the overall glyphs composed). */
24474 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24475 int boff; /* baseline offset */
24476 struct composition *cmp = composition_table[it->cmp_it.id];
24477 int glyph_len = cmp->glyph_len;
24478 struct font *font = face->font;
24480 it->nglyphs = 1;
24482 /* If we have not yet calculated pixel size data of glyphs of
24483 the composition for the current face font, calculate them
24484 now. Theoretically, we have to check all fonts for the
24485 glyphs, but that requires much time and memory space. So,
24486 here we check only the font of the first glyph. This may
24487 lead to incorrect display, but it's very rare, and C-l
24488 (recenter-top-bottom) can correct the display anyway. */
24489 if (! cmp->font || cmp->font != font)
24491 /* Ascent and descent of the font of the first character
24492 of this composition (adjusted by baseline offset).
24493 Ascent and descent of overall glyphs should not be less
24494 than these, respectively. */
24495 int font_ascent, font_descent, font_height;
24496 /* Bounding box of the overall glyphs. */
24497 int leftmost, rightmost, lowest, highest;
24498 int lbearing, rbearing;
24499 int i, width, ascent, descent;
24500 int left_padded = 0, right_padded = 0;
24501 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24502 XChar2b char2b;
24503 struct font_metrics *pcm;
24504 int font_not_found_p;
24505 EMACS_INT pos;
24507 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24508 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24509 break;
24510 if (glyph_len < cmp->glyph_len)
24511 right_padded = 1;
24512 for (i = 0; i < glyph_len; i++)
24514 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24515 break;
24516 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24518 if (i > 0)
24519 left_padded = 1;
24521 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24522 : IT_CHARPOS (*it));
24523 /* If no suitable font is found, use the default font. */
24524 font_not_found_p = font == NULL;
24525 if (font_not_found_p)
24527 face = face->ascii_face;
24528 font = face->font;
24530 boff = font->baseline_offset;
24531 if (font->vertical_centering)
24532 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24533 font_ascent = FONT_BASE (font) + boff;
24534 font_descent = FONT_DESCENT (font) - boff;
24535 font_height = FONT_HEIGHT (font);
24537 cmp->font = (void *) font;
24539 pcm = NULL;
24540 if (! font_not_found_p)
24542 get_char_face_and_encoding (it->f, c, it->face_id,
24543 &char2b, 0);
24544 pcm = get_per_char_metric (font, &char2b);
24547 /* Initialize the bounding box. */
24548 if (pcm)
24550 width = cmp->glyph_len > 0 ? pcm->width : 0;
24551 ascent = pcm->ascent;
24552 descent = pcm->descent;
24553 lbearing = pcm->lbearing;
24554 rbearing = pcm->rbearing;
24556 else
24558 width = cmp->glyph_len > 0 ? font->space_width : 0;
24559 ascent = FONT_BASE (font);
24560 descent = FONT_DESCENT (font);
24561 lbearing = 0;
24562 rbearing = width;
24565 rightmost = width;
24566 leftmost = 0;
24567 lowest = - descent + boff;
24568 highest = ascent + boff;
24570 if (! font_not_found_p
24571 && font->default_ascent
24572 && CHAR_TABLE_P (Vuse_default_ascent)
24573 && !NILP (Faref (Vuse_default_ascent,
24574 make_number (it->char_to_display))))
24575 highest = font->default_ascent + boff;
24577 /* Draw the first glyph at the normal position. It may be
24578 shifted to right later if some other glyphs are drawn
24579 at the left. */
24580 cmp->offsets[i * 2] = 0;
24581 cmp->offsets[i * 2 + 1] = boff;
24582 cmp->lbearing = lbearing;
24583 cmp->rbearing = rbearing;
24585 /* Set cmp->offsets for the remaining glyphs. */
24586 for (i++; i < glyph_len; i++)
24588 int left, right, btm, top;
24589 int ch = COMPOSITION_GLYPH (cmp, i);
24590 int face_id;
24591 struct face *this_face;
24593 if (ch == '\t')
24594 ch = ' ';
24595 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24596 this_face = FACE_FROM_ID (it->f, face_id);
24597 font = this_face->font;
24599 if (font == NULL)
24600 pcm = NULL;
24601 else
24603 get_char_face_and_encoding (it->f, ch, face_id,
24604 &char2b, 0);
24605 pcm = get_per_char_metric (font, &char2b);
24607 if (! pcm)
24608 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24609 else
24611 width = pcm->width;
24612 ascent = pcm->ascent;
24613 descent = pcm->descent;
24614 lbearing = pcm->lbearing;
24615 rbearing = pcm->rbearing;
24616 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
24618 /* Relative composition with or without
24619 alternate chars. */
24620 left = (leftmost + rightmost - width) / 2;
24621 btm = - descent + boff;
24622 if (font->relative_compose
24623 && (! CHAR_TABLE_P (Vignore_relative_composition)
24624 || NILP (Faref (Vignore_relative_composition,
24625 make_number (ch)))))
24628 if (- descent >= font->relative_compose)
24629 /* One extra pixel between two glyphs. */
24630 btm = highest + 1;
24631 else if (ascent <= 0)
24632 /* One extra pixel between two glyphs. */
24633 btm = lowest - 1 - ascent - descent;
24636 else
24638 /* A composition rule is specified by an integer
24639 value that encodes global and new reference
24640 points (GREF and NREF). GREF and NREF are
24641 specified by numbers as below:
24643 0---1---2 -- ascent
24647 9--10--11 -- center
24649 ---3---4---5--- baseline
24651 6---7---8 -- descent
24653 int rule = COMPOSITION_RULE (cmp, i);
24654 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
24656 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
24657 grefx = gref % 3, nrefx = nref % 3;
24658 grefy = gref / 3, nrefy = nref / 3;
24659 if (xoff)
24660 xoff = font_height * (xoff - 128) / 256;
24661 if (yoff)
24662 yoff = font_height * (yoff - 128) / 256;
24664 left = (leftmost
24665 + grefx * (rightmost - leftmost) / 2
24666 - nrefx * width / 2
24667 + xoff);
24669 btm = ((grefy == 0 ? highest
24670 : grefy == 1 ? 0
24671 : grefy == 2 ? lowest
24672 : (highest + lowest) / 2)
24673 - (nrefy == 0 ? ascent + descent
24674 : nrefy == 1 ? descent - boff
24675 : nrefy == 2 ? 0
24676 : (ascent + descent) / 2)
24677 + yoff);
24680 cmp->offsets[i * 2] = left;
24681 cmp->offsets[i * 2 + 1] = btm + descent;
24683 /* Update the bounding box of the overall glyphs. */
24684 if (width > 0)
24686 right = left + width;
24687 if (left < leftmost)
24688 leftmost = left;
24689 if (right > rightmost)
24690 rightmost = right;
24692 top = btm + descent + ascent;
24693 if (top > highest)
24694 highest = top;
24695 if (btm < lowest)
24696 lowest = btm;
24698 if (cmp->lbearing > left + lbearing)
24699 cmp->lbearing = left + lbearing;
24700 if (cmp->rbearing < left + rbearing)
24701 cmp->rbearing = left + rbearing;
24705 /* If there are glyphs whose x-offsets are negative,
24706 shift all glyphs to the right and make all x-offsets
24707 non-negative. */
24708 if (leftmost < 0)
24710 for (i = 0; i < cmp->glyph_len; i++)
24711 cmp->offsets[i * 2] -= leftmost;
24712 rightmost -= leftmost;
24713 cmp->lbearing -= leftmost;
24714 cmp->rbearing -= leftmost;
24717 if (left_padded && cmp->lbearing < 0)
24719 for (i = 0; i < cmp->glyph_len; i++)
24720 cmp->offsets[i * 2] -= cmp->lbearing;
24721 rightmost -= cmp->lbearing;
24722 cmp->rbearing -= cmp->lbearing;
24723 cmp->lbearing = 0;
24725 if (right_padded && rightmost < cmp->rbearing)
24727 rightmost = cmp->rbearing;
24730 cmp->pixel_width = rightmost;
24731 cmp->ascent = highest;
24732 cmp->descent = - lowest;
24733 if (cmp->ascent < font_ascent)
24734 cmp->ascent = font_ascent;
24735 if (cmp->descent < font_descent)
24736 cmp->descent = font_descent;
24739 if (it->glyph_row
24740 && (cmp->lbearing < 0
24741 || cmp->rbearing > cmp->pixel_width))
24742 it->glyph_row->contains_overlapping_glyphs_p = 1;
24744 it->pixel_width = cmp->pixel_width;
24745 it->ascent = it->phys_ascent = cmp->ascent;
24746 it->descent = it->phys_descent = cmp->descent;
24747 if (face->box != FACE_NO_BOX)
24749 int thick = face->box_line_width;
24751 if (thick > 0)
24753 it->ascent += thick;
24754 it->descent += thick;
24756 else
24757 thick = - thick;
24759 if (it->start_of_box_run_p)
24760 it->pixel_width += thick;
24761 if (it->end_of_box_run_p)
24762 it->pixel_width += thick;
24765 /* If face has an overline, add the height of the overline
24766 (1 pixel) and a 1 pixel margin to the character height. */
24767 if (face->overline_p)
24768 it->ascent += overline_margin;
24770 take_vertical_position_into_account (it);
24771 if (it->ascent < 0)
24772 it->ascent = 0;
24773 if (it->descent < 0)
24774 it->descent = 0;
24776 if (it->glyph_row && cmp->glyph_len > 0)
24777 append_composite_glyph (it);
24779 else if (it->what == IT_COMPOSITION)
24781 /* A dynamic (automatic) composition. */
24782 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24783 Lisp_Object gstring;
24784 struct font_metrics metrics;
24786 it->nglyphs = 1;
24788 gstring = composition_gstring_from_id (it->cmp_it.id);
24789 it->pixel_width
24790 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
24791 &metrics);
24792 if (it->glyph_row
24793 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
24794 it->glyph_row->contains_overlapping_glyphs_p = 1;
24795 it->ascent = it->phys_ascent = metrics.ascent;
24796 it->descent = it->phys_descent = metrics.descent;
24797 if (face->box != FACE_NO_BOX)
24799 int thick = face->box_line_width;
24801 if (thick > 0)
24803 it->ascent += thick;
24804 it->descent += thick;
24806 else
24807 thick = - thick;
24809 if (it->start_of_box_run_p)
24810 it->pixel_width += thick;
24811 if (it->end_of_box_run_p)
24812 it->pixel_width += thick;
24814 /* If face has an overline, add the height of the overline
24815 (1 pixel) and a 1 pixel margin to the character height. */
24816 if (face->overline_p)
24817 it->ascent += overline_margin;
24818 take_vertical_position_into_account (it);
24819 if (it->ascent < 0)
24820 it->ascent = 0;
24821 if (it->descent < 0)
24822 it->descent = 0;
24824 if (it->glyph_row)
24825 append_composite_glyph (it);
24827 else if (it->what == IT_GLYPHLESS)
24828 produce_glyphless_glyph (it, 0, Qnil);
24829 else if (it->what == IT_IMAGE)
24830 produce_image_glyph (it);
24831 else if (it->what == IT_STRETCH)
24832 produce_stretch_glyph (it);
24834 done:
24835 /* Accumulate dimensions. Note: can't assume that it->descent > 0
24836 because this isn't true for images with `:ascent 100'. */
24837 xassert (it->ascent >= 0 && it->descent >= 0);
24838 if (it->area == TEXT_AREA)
24839 it->current_x += it->pixel_width;
24841 if (extra_line_spacing > 0)
24843 it->descent += extra_line_spacing;
24844 if (extra_line_spacing > it->max_extra_line_spacing)
24845 it->max_extra_line_spacing = extra_line_spacing;
24848 it->max_ascent = max (it->max_ascent, it->ascent);
24849 it->max_descent = max (it->max_descent, it->descent);
24850 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
24851 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
24854 /* EXPORT for RIF:
24855 Output LEN glyphs starting at START at the nominal cursor position.
24856 Advance the nominal cursor over the text. The global variable
24857 updated_window contains the window being updated, updated_row is
24858 the glyph row being updated, and updated_area is the area of that
24859 row being updated. */
24861 void
24862 x_write_glyphs (struct glyph *start, int len)
24864 int x, hpos, chpos = updated_window->phys_cursor.hpos;
24866 xassert (updated_window && updated_row);
24867 /* When the window is hscrolled, cursor hpos can legitimately be out
24868 of bounds, but we draw the cursor at the corresponding window
24869 margin in that case. */
24870 if (!updated_row->reversed_p && chpos < 0)
24871 chpos = 0;
24872 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
24873 chpos = updated_row->used[TEXT_AREA] - 1;
24875 BLOCK_INPUT;
24877 /* Write glyphs. */
24879 hpos = start - updated_row->glyphs[updated_area];
24880 x = draw_glyphs (updated_window, output_cursor.x,
24881 updated_row, updated_area,
24882 hpos, hpos + len,
24883 DRAW_NORMAL_TEXT, 0);
24885 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
24886 if (updated_area == TEXT_AREA
24887 && updated_window->phys_cursor_on_p
24888 && updated_window->phys_cursor.vpos == output_cursor.vpos
24889 && chpos >= hpos
24890 && chpos < hpos + len)
24891 updated_window->phys_cursor_on_p = 0;
24893 UNBLOCK_INPUT;
24895 /* Advance the output cursor. */
24896 output_cursor.hpos += len;
24897 output_cursor.x = x;
24901 /* EXPORT for RIF:
24902 Insert LEN glyphs from START at the nominal cursor position. */
24904 void
24905 x_insert_glyphs (struct glyph *start, int len)
24907 struct frame *f;
24908 struct window *w;
24909 int line_height, shift_by_width, shifted_region_width;
24910 struct glyph_row *row;
24911 struct glyph *glyph;
24912 int frame_x, frame_y;
24913 EMACS_INT hpos;
24915 xassert (updated_window && updated_row);
24916 BLOCK_INPUT;
24917 w = updated_window;
24918 f = XFRAME (WINDOW_FRAME (w));
24920 /* Get the height of the line we are in. */
24921 row = updated_row;
24922 line_height = row->height;
24924 /* Get the width of the glyphs to insert. */
24925 shift_by_width = 0;
24926 for (glyph = start; glyph < start + len; ++glyph)
24927 shift_by_width += glyph->pixel_width;
24929 /* Get the width of the region to shift right. */
24930 shifted_region_width = (window_box_width (w, updated_area)
24931 - output_cursor.x
24932 - shift_by_width);
24934 /* Shift right. */
24935 frame_x = window_box_left (w, updated_area) + output_cursor.x;
24936 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
24938 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
24939 line_height, shift_by_width);
24941 /* Write the glyphs. */
24942 hpos = start - row->glyphs[updated_area];
24943 draw_glyphs (w, output_cursor.x, row, updated_area,
24944 hpos, hpos + len,
24945 DRAW_NORMAL_TEXT, 0);
24947 /* Advance the output cursor. */
24948 output_cursor.hpos += len;
24949 output_cursor.x += shift_by_width;
24950 UNBLOCK_INPUT;
24954 /* EXPORT for RIF:
24955 Erase the current text line from the nominal cursor position
24956 (inclusive) to pixel column TO_X (exclusive). The idea is that
24957 everything from TO_X onward is already erased.
24959 TO_X is a pixel position relative to updated_area of
24960 updated_window. TO_X == -1 means clear to the end of this area. */
24962 void
24963 x_clear_end_of_line (int to_x)
24965 struct frame *f;
24966 struct window *w = updated_window;
24967 int max_x, min_y, max_y;
24968 int from_x, from_y, to_y;
24970 xassert (updated_window && updated_row);
24971 f = XFRAME (w->frame);
24973 if (updated_row->full_width_p)
24974 max_x = WINDOW_TOTAL_WIDTH (w);
24975 else
24976 max_x = window_box_width (w, updated_area);
24977 max_y = window_text_bottom_y (w);
24979 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
24980 of window. For TO_X > 0, truncate to end of drawing area. */
24981 if (to_x == 0)
24982 return;
24983 else if (to_x < 0)
24984 to_x = max_x;
24985 else
24986 to_x = min (to_x, max_x);
24988 to_y = min (max_y, output_cursor.y + updated_row->height);
24990 /* Notice if the cursor will be cleared by this operation. */
24991 if (!updated_row->full_width_p)
24992 notice_overwritten_cursor (w, updated_area,
24993 output_cursor.x, -1,
24994 updated_row->y,
24995 MATRIX_ROW_BOTTOM_Y (updated_row));
24997 from_x = output_cursor.x;
24999 /* Translate to frame coordinates. */
25000 if (updated_row->full_width_p)
25002 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25003 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25005 else
25007 int area_left = window_box_left (w, updated_area);
25008 from_x += area_left;
25009 to_x += area_left;
25012 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25013 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25014 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25016 /* Prevent inadvertently clearing to end of the X window. */
25017 if (to_x > from_x && to_y > from_y)
25019 BLOCK_INPUT;
25020 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25021 to_x - from_x, to_y - from_y);
25022 UNBLOCK_INPUT;
25026 #endif /* HAVE_WINDOW_SYSTEM */
25030 /***********************************************************************
25031 Cursor types
25032 ***********************************************************************/
25034 /* Value is the internal representation of the specified cursor type
25035 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25036 of the bar cursor. */
25038 static enum text_cursor_kinds
25039 get_specified_cursor_type (Lisp_Object arg, int *width)
25041 enum text_cursor_kinds type;
25043 if (NILP (arg))
25044 return NO_CURSOR;
25046 if (EQ (arg, Qbox))
25047 return FILLED_BOX_CURSOR;
25049 if (EQ (arg, Qhollow))
25050 return HOLLOW_BOX_CURSOR;
25052 if (EQ (arg, Qbar))
25054 *width = 2;
25055 return BAR_CURSOR;
25058 if (CONSP (arg)
25059 && EQ (XCAR (arg), Qbar)
25060 && INTEGERP (XCDR (arg))
25061 && XINT (XCDR (arg)) >= 0)
25063 *width = XINT (XCDR (arg));
25064 return BAR_CURSOR;
25067 if (EQ (arg, Qhbar))
25069 *width = 2;
25070 return HBAR_CURSOR;
25073 if (CONSP (arg)
25074 && EQ (XCAR (arg), Qhbar)
25075 && INTEGERP (XCDR (arg))
25076 && XINT (XCDR (arg)) >= 0)
25078 *width = XINT (XCDR (arg));
25079 return HBAR_CURSOR;
25082 /* Treat anything unknown as "hollow box cursor".
25083 It was bad to signal an error; people have trouble fixing
25084 .Xdefaults with Emacs, when it has something bad in it. */
25085 type = HOLLOW_BOX_CURSOR;
25087 return type;
25090 /* Set the default cursor types for specified frame. */
25091 void
25092 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25094 int width = 1;
25095 Lisp_Object tem;
25097 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25098 FRAME_CURSOR_WIDTH (f) = width;
25100 /* By default, set up the blink-off state depending on the on-state. */
25102 tem = Fassoc (arg, Vblink_cursor_alist);
25103 if (!NILP (tem))
25105 FRAME_BLINK_OFF_CURSOR (f)
25106 = get_specified_cursor_type (XCDR (tem), &width);
25107 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25109 else
25110 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25114 #ifdef HAVE_WINDOW_SYSTEM
25116 /* Return the cursor we want to be displayed in window W. Return
25117 width of bar/hbar cursor through WIDTH arg. Return with
25118 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25119 (i.e. if the `system caret' should track this cursor).
25121 In a mini-buffer window, we want the cursor only to appear if we
25122 are reading input from this window. For the selected window, we
25123 want the cursor type given by the frame parameter or buffer local
25124 setting of cursor-type. If explicitly marked off, draw no cursor.
25125 In all other cases, we want a hollow box cursor. */
25127 static enum text_cursor_kinds
25128 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25129 int *active_cursor)
25131 struct frame *f = XFRAME (w->frame);
25132 struct buffer *b = XBUFFER (w->buffer);
25133 int cursor_type = DEFAULT_CURSOR;
25134 Lisp_Object alt_cursor;
25135 int non_selected = 0;
25137 *active_cursor = 1;
25139 /* Echo area */
25140 if (cursor_in_echo_area
25141 && FRAME_HAS_MINIBUF_P (f)
25142 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25144 if (w == XWINDOW (echo_area_window))
25146 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25148 *width = FRAME_CURSOR_WIDTH (f);
25149 return FRAME_DESIRED_CURSOR (f);
25151 else
25152 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25155 *active_cursor = 0;
25156 non_selected = 1;
25159 /* Detect a nonselected window or nonselected frame. */
25160 else if (w != XWINDOW (f->selected_window)
25161 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25163 *active_cursor = 0;
25165 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25166 return NO_CURSOR;
25168 non_selected = 1;
25171 /* Never display a cursor in a window in which cursor-type is nil. */
25172 if (NILP (BVAR (b, cursor_type)))
25173 return NO_CURSOR;
25175 /* Get the normal cursor type for this window. */
25176 if (EQ (BVAR (b, cursor_type), Qt))
25178 cursor_type = FRAME_DESIRED_CURSOR (f);
25179 *width = FRAME_CURSOR_WIDTH (f);
25181 else
25182 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25184 /* Use cursor-in-non-selected-windows instead
25185 for non-selected window or frame. */
25186 if (non_selected)
25188 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25189 if (!EQ (Qt, alt_cursor))
25190 return get_specified_cursor_type (alt_cursor, width);
25191 /* t means modify the normal cursor type. */
25192 if (cursor_type == FILLED_BOX_CURSOR)
25193 cursor_type = HOLLOW_BOX_CURSOR;
25194 else if (cursor_type == BAR_CURSOR && *width > 1)
25195 --*width;
25196 return cursor_type;
25199 /* Use normal cursor if not blinked off. */
25200 if (!w->cursor_off_p)
25202 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25204 if (cursor_type == FILLED_BOX_CURSOR)
25206 /* Using a block cursor on large images can be very annoying.
25207 So use a hollow cursor for "large" images.
25208 If image is not transparent (no mask), also use hollow cursor. */
25209 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25210 if (img != NULL && IMAGEP (img->spec))
25212 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25213 where N = size of default frame font size.
25214 This should cover most of the "tiny" icons people may use. */
25215 if (!img->mask
25216 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25217 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25218 cursor_type = HOLLOW_BOX_CURSOR;
25221 else if (cursor_type != NO_CURSOR)
25223 /* Display current only supports BOX and HOLLOW cursors for images.
25224 So for now, unconditionally use a HOLLOW cursor when cursor is
25225 not a solid box cursor. */
25226 cursor_type = HOLLOW_BOX_CURSOR;
25229 return cursor_type;
25232 /* Cursor is blinked off, so determine how to "toggle" it. */
25234 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25235 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25236 return get_specified_cursor_type (XCDR (alt_cursor), width);
25238 /* Then see if frame has specified a specific blink off cursor type. */
25239 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25241 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25242 return FRAME_BLINK_OFF_CURSOR (f);
25245 #if 0
25246 /* Some people liked having a permanently visible blinking cursor,
25247 while others had very strong opinions against it. So it was
25248 decided to remove it. KFS 2003-09-03 */
25250 /* Finally perform built-in cursor blinking:
25251 filled box <-> hollow box
25252 wide [h]bar <-> narrow [h]bar
25253 narrow [h]bar <-> no cursor
25254 other type <-> no cursor */
25256 if (cursor_type == FILLED_BOX_CURSOR)
25257 return HOLLOW_BOX_CURSOR;
25259 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25261 *width = 1;
25262 return cursor_type;
25264 #endif
25266 return NO_CURSOR;
25270 /* Notice when the text cursor of window W has been completely
25271 overwritten by a drawing operation that outputs glyphs in AREA
25272 starting at X0 and ending at X1 in the line starting at Y0 and
25273 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25274 the rest of the line after X0 has been written. Y coordinates
25275 are window-relative. */
25277 static void
25278 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25279 int x0, int x1, int y0, int y1)
25281 int cx0, cx1, cy0, cy1;
25282 struct glyph_row *row;
25284 if (!w->phys_cursor_on_p)
25285 return;
25286 if (area != TEXT_AREA)
25287 return;
25289 if (w->phys_cursor.vpos < 0
25290 || w->phys_cursor.vpos >= w->current_matrix->nrows
25291 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25292 !(row->enabled_p && row->displays_text_p)))
25293 return;
25295 if (row->cursor_in_fringe_p)
25297 row->cursor_in_fringe_p = 0;
25298 draw_fringe_bitmap (w, row, row->reversed_p);
25299 w->phys_cursor_on_p = 0;
25300 return;
25303 cx0 = w->phys_cursor.x;
25304 cx1 = cx0 + w->phys_cursor_width;
25305 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25306 return;
25308 /* The cursor image will be completely removed from the
25309 screen if the output area intersects the cursor area in
25310 y-direction. When we draw in [y0 y1[, and some part of
25311 the cursor is at y < y0, that part must have been drawn
25312 before. When scrolling, the cursor is erased before
25313 actually scrolling, so we don't come here. When not
25314 scrolling, the rows above the old cursor row must have
25315 changed, and in this case these rows must have written
25316 over the cursor image.
25318 Likewise if part of the cursor is below y1, with the
25319 exception of the cursor being in the first blank row at
25320 the buffer and window end because update_text_area
25321 doesn't draw that row. (Except when it does, but
25322 that's handled in update_text_area.) */
25324 cy0 = w->phys_cursor.y;
25325 cy1 = cy0 + w->phys_cursor_height;
25326 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25327 return;
25329 w->phys_cursor_on_p = 0;
25332 #endif /* HAVE_WINDOW_SYSTEM */
25335 /************************************************************************
25336 Mouse Face
25337 ************************************************************************/
25339 #ifdef HAVE_WINDOW_SYSTEM
25341 /* EXPORT for RIF:
25342 Fix the display of area AREA of overlapping row ROW in window W
25343 with respect to the overlapping part OVERLAPS. */
25345 void
25346 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25347 enum glyph_row_area area, int overlaps)
25349 int i, x;
25351 BLOCK_INPUT;
25353 x = 0;
25354 for (i = 0; i < row->used[area];)
25356 if (row->glyphs[area][i].overlaps_vertically_p)
25358 int start = i, start_x = x;
25362 x += row->glyphs[area][i].pixel_width;
25363 ++i;
25365 while (i < row->used[area]
25366 && row->glyphs[area][i].overlaps_vertically_p);
25368 draw_glyphs (w, start_x, row, area,
25369 start, i,
25370 DRAW_NORMAL_TEXT, overlaps);
25372 else
25374 x += row->glyphs[area][i].pixel_width;
25375 ++i;
25379 UNBLOCK_INPUT;
25383 /* EXPORT:
25384 Draw the cursor glyph of window W in glyph row ROW. See the
25385 comment of draw_glyphs for the meaning of HL. */
25387 void
25388 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25389 enum draw_glyphs_face hl)
25391 /* If cursor hpos is out of bounds, don't draw garbage. This can
25392 happen in mini-buffer windows when switching between echo area
25393 glyphs and mini-buffer. */
25394 if ((row->reversed_p
25395 ? (w->phys_cursor.hpos >= 0)
25396 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25398 int on_p = w->phys_cursor_on_p;
25399 int x1;
25400 int hpos = w->phys_cursor.hpos;
25402 /* When the window is hscrolled, cursor hpos can legitimately be
25403 out of bounds, but we draw the cursor at the corresponding
25404 window margin in that case. */
25405 if (!row->reversed_p && hpos < 0)
25406 hpos = 0;
25407 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25408 hpos = row->used[TEXT_AREA] - 1;
25410 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25411 hl, 0);
25412 w->phys_cursor_on_p = on_p;
25414 if (hl == DRAW_CURSOR)
25415 w->phys_cursor_width = x1 - w->phys_cursor.x;
25416 /* When we erase the cursor, and ROW is overlapped by other
25417 rows, make sure that these overlapping parts of other rows
25418 are redrawn. */
25419 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25421 w->phys_cursor_width = x1 - w->phys_cursor.x;
25423 if (row > w->current_matrix->rows
25424 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25425 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25426 OVERLAPS_ERASED_CURSOR);
25428 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25429 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25430 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25431 OVERLAPS_ERASED_CURSOR);
25437 /* EXPORT:
25438 Erase the image of a cursor of window W from the screen. */
25440 void
25441 erase_phys_cursor (struct window *w)
25443 struct frame *f = XFRAME (w->frame);
25444 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25445 int hpos = w->phys_cursor.hpos;
25446 int vpos = w->phys_cursor.vpos;
25447 int mouse_face_here_p = 0;
25448 struct glyph_matrix *active_glyphs = w->current_matrix;
25449 struct glyph_row *cursor_row;
25450 struct glyph *cursor_glyph;
25451 enum draw_glyphs_face hl;
25453 /* No cursor displayed or row invalidated => nothing to do on the
25454 screen. */
25455 if (w->phys_cursor_type == NO_CURSOR)
25456 goto mark_cursor_off;
25458 /* VPOS >= active_glyphs->nrows means that window has been resized.
25459 Don't bother to erase the cursor. */
25460 if (vpos >= active_glyphs->nrows)
25461 goto mark_cursor_off;
25463 /* If row containing cursor is marked invalid, there is nothing we
25464 can do. */
25465 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25466 if (!cursor_row->enabled_p)
25467 goto mark_cursor_off;
25469 /* If line spacing is > 0, old cursor may only be partially visible in
25470 window after split-window. So adjust visible height. */
25471 cursor_row->visible_height = min (cursor_row->visible_height,
25472 window_text_bottom_y (w) - cursor_row->y);
25474 /* If row is completely invisible, don't attempt to delete a cursor which
25475 isn't there. This can happen if cursor is at top of a window, and
25476 we switch to a buffer with a header line in that window. */
25477 if (cursor_row->visible_height <= 0)
25478 goto mark_cursor_off;
25480 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25481 if (cursor_row->cursor_in_fringe_p)
25483 cursor_row->cursor_in_fringe_p = 0;
25484 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25485 goto mark_cursor_off;
25488 /* This can happen when the new row is shorter than the old one.
25489 In this case, either draw_glyphs or clear_end_of_line
25490 should have cleared the cursor. Note that we wouldn't be
25491 able to erase the cursor in this case because we don't have a
25492 cursor glyph at hand. */
25493 if ((cursor_row->reversed_p
25494 ? (w->phys_cursor.hpos < 0)
25495 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25496 goto mark_cursor_off;
25498 /* When the window is hscrolled, cursor hpos can legitimately be out
25499 of bounds, but we draw the cursor at the corresponding window
25500 margin in that case. */
25501 if (!cursor_row->reversed_p && hpos < 0)
25502 hpos = 0;
25503 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25504 hpos = cursor_row->used[TEXT_AREA] - 1;
25506 /* If the cursor is in the mouse face area, redisplay that when
25507 we clear the cursor. */
25508 if (! NILP (hlinfo->mouse_face_window)
25509 && coords_in_mouse_face_p (w, hpos, vpos)
25510 /* Don't redraw the cursor's spot in mouse face if it is at the
25511 end of a line (on a newline). The cursor appears there, but
25512 mouse highlighting does not. */
25513 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25514 mouse_face_here_p = 1;
25516 /* Maybe clear the display under the cursor. */
25517 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25519 int x, y, left_x;
25520 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25521 int width;
25523 cursor_glyph = get_phys_cursor_glyph (w);
25524 if (cursor_glyph == NULL)
25525 goto mark_cursor_off;
25527 width = cursor_glyph->pixel_width;
25528 left_x = window_box_left_offset (w, TEXT_AREA);
25529 x = w->phys_cursor.x;
25530 if (x < left_x)
25531 width -= left_x - x;
25532 width = min (width, window_box_width (w, TEXT_AREA) - x);
25533 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25534 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25536 if (width > 0)
25537 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25540 /* Erase the cursor by redrawing the character underneath it. */
25541 if (mouse_face_here_p)
25542 hl = DRAW_MOUSE_FACE;
25543 else
25544 hl = DRAW_NORMAL_TEXT;
25545 draw_phys_cursor_glyph (w, cursor_row, hl);
25547 mark_cursor_off:
25548 w->phys_cursor_on_p = 0;
25549 w->phys_cursor_type = NO_CURSOR;
25553 /* EXPORT:
25554 Display or clear cursor of window W. If ON is zero, clear the
25555 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25556 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25558 void
25559 display_and_set_cursor (struct window *w, int on,
25560 int hpos, int vpos, int x, int y)
25562 struct frame *f = XFRAME (w->frame);
25563 int new_cursor_type;
25564 int new_cursor_width;
25565 int active_cursor;
25566 struct glyph_row *glyph_row;
25567 struct glyph *glyph;
25569 /* This is pointless on invisible frames, and dangerous on garbaged
25570 windows and frames; in the latter case, the frame or window may
25571 be in the midst of changing its size, and x and y may be off the
25572 window. */
25573 if (! FRAME_VISIBLE_P (f)
25574 || FRAME_GARBAGED_P (f)
25575 || vpos >= w->current_matrix->nrows
25576 || hpos >= w->current_matrix->matrix_w)
25577 return;
25579 /* If cursor is off and we want it off, return quickly. */
25580 if (!on && !w->phys_cursor_on_p)
25581 return;
25583 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25584 /* If cursor row is not enabled, we don't really know where to
25585 display the cursor. */
25586 if (!glyph_row->enabled_p)
25588 w->phys_cursor_on_p = 0;
25589 return;
25592 glyph = NULL;
25593 if (!glyph_row->exact_window_width_line_p
25594 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25595 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25597 xassert (interrupt_input_blocked);
25599 /* Set new_cursor_type to the cursor we want to be displayed. */
25600 new_cursor_type = get_window_cursor_type (w, glyph,
25601 &new_cursor_width, &active_cursor);
25603 /* If cursor is currently being shown and we don't want it to be or
25604 it is in the wrong place, or the cursor type is not what we want,
25605 erase it. */
25606 if (w->phys_cursor_on_p
25607 && (!on
25608 || w->phys_cursor.x != x
25609 || w->phys_cursor.y != y
25610 || new_cursor_type != w->phys_cursor_type
25611 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
25612 && new_cursor_width != w->phys_cursor_width)))
25613 erase_phys_cursor (w);
25615 /* Don't check phys_cursor_on_p here because that flag is only set
25616 to zero in some cases where we know that the cursor has been
25617 completely erased, to avoid the extra work of erasing the cursor
25618 twice. In other words, phys_cursor_on_p can be 1 and the cursor
25619 still not be visible, or it has only been partly erased. */
25620 if (on)
25622 w->phys_cursor_ascent = glyph_row->ascent;
25623 w->phys_cursor_height = glyph_row->height;
25625 /* Set phys_cursor_.* before x_draw_.* is called because some
25626 of them may need the information. */
25627 w->phys_cursor.x = x;
25628 w->phys_cursor.y = glyph_row->y;
25629 w->phys_cursor.hpos = hpos;
25630 w->phys_cursor.vpos = vpos;
25633 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
25634 new_cursor_type, new_cursor_width,
25635 on, active_cursor);
25639 /* Switch the display of W's cursor on or off, according to the value
25640 of ON. */
25642 static void
25643 update_window_cursor (struct window *w, int on)
25645 /* Don't update cursor in windows whose frame is in the process
25646 of being deleted. */
25647 if (w->current_matrix)
25649 int hpos = w->phys_cursor.hpos;
25650 int vpos = w->phys_cursor.vpos;
25651 struct glyph_row *row;
25653 if (vpos >= w->current_matrix->nrows
25654 || hpos >= w->current_matrix->matrix_w)
25655 return;
25657 row = MATRIX_ROW (w->current_matrix, vpos);
25659 /* When the window is hscrolled, cursor hpos can legitimately be
25660 out of bounds, but we draw the cursor at the corresponding
25661 window margin in that case. */
25662 if (!row->reversed_p && hpos < 0)
25663 hpos = 0;
25664 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25665 hpos = row->used[TEXT_AREA] - 1;
25667 BLOCK_INPUT;
25668 display_and_set_cursor (w, on, hpos, vpos,
25669 w->phys_cursor.x, w->phys_cursor.y);
25670 UNBLOCK_INPUT;
25675 /* Call update_window_cursor with parameter ON_P on all leaf windows
25676 in the window tree rooted at W. */
25678 static void
25679 update_cursor_in_window_tree (struct window *w, int on_p)
25681 while (w)
25683 if (!NILP (w->hchild))
25684 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
25685 else if (!NILP (w->vchild))
25686 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
25687 else
25688 update_window_cursor (w, on_p);
25690 w = NILP (w->next) ? 0 : XWINDOW (w->next);
25695 /* EXPORT:
25696 Display the cursor on window W, or clear it, according to ON_P.
25697 Don't change the cursor's position. */
25699 void
25700 x_update_cursor (struct frame *f, int on_p)
25702 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
25706 /* EXPORT:
25707 Clear the cursor of window W to background color, and mark the
25708 cursor as not shown. This is used when the text where the cursor
25709 is about to be rewritten. */
25711 void
25712 x_clear_cursor (struct window *w)
25714 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
25715 update_window_cursor (w, 0);
25718 #endif /* HAVE_WINDOW_SYSTEM */
25720 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
25721 and MSDOS. */
25722 static void
25723 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
25724 int start_hpos, int end_hpos,
25725 enum draw_glyphs_face draw)
25727 #ifdef HAVE_WINDOW_SYSTEM
25728 if (FRAME_WINDOW_P (XFRAME (w->frame)))
25730 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
25731 return;
25733 #endif
25734 #if defined (HAVE_GPM) || defined (MSDOS)
25735 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
25736 #endif
25739 /* Display the active region described by mouse_face_* according to DRAW. */
25741 static void
25742 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
25744 struct window *w = XWINDOW (hlinfo->mouse_face_window);
25745 struct frame *f = XFRAME (WINDOW_FRAME (w));
25747 if (/* If window is in the process of being destroyed, don't bother
25748 to do anything. */
25749 w->current_matrix != NULL
25750 /* Don't update mouse highlight if hidden */
25751 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
25752 /* Recognize when we are called to operate on rows that don't exist
25753 anymore. This can happen when a window is split. */
25754 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
25756 int phys_cursor_on_p = w->phys_cursor_on_p;
25757 struct glyph_row *row, *first, *last;
25759 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
25760 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
25762 for (row = first; row <= last && row->enabled_p; ++row)
25764 int start_hpos, end_hpos, start_x;
25766 /* For all but the first row, the highlight starts at column 0. */
25767 if (row == first)
25769 /* R2L rows have BEG and END in reversed order, but the
25770 screen drawing geometry is always left to right. So
25771 we need to mirror the beginning and end of the
25772 highlighted area in R2L rows. */
25773 if (!row->reversed_p)
25775 start_hpos = hlinfo->mouse_face_beg_col;
25776 start_x = hlinfo->mouse_face_beg_x;
25778 else if (row == last)
25780 start_hpos = hlinfo->mouse_face_end_col;
25781 start_x = hlinfo->mouse_face_end_x;
25783 else
25785 start_hpos = 0;
25786 start_x = 0;
25789 else if (row->reversed_p && row == last)
25791 start_hpos = hlinfo->mouse_face_end_col;
25792 start_x = hlinfo->mouse_face_end_x;
25794 else
25796 start_hpos = 0;
25797 start_x = 0;
25800 if (row == last)
25802 if (!row->reversed_p)
25803 end_hpos = hlinfo->mouse_face_end_col;
25804 else if (row == first)
25805 end_hpos = hlinfo->mouse_face_beg_col;
25806 else
25808 end_hpos = row->used[TEXT_AREA];
25809 if (draw == DRAW_NORMAL_TEXT)
25810 row->fill_line_p = 1; /* Clear to end of line */
25813 else if (row->reversed_p && row == first)
25814 end_hpos = hlinfo->mouse_face_beg_col;
25815 else
25817 end_hpos = row->used[TEXT_AREA];
25818 if (draw == DRAW_NORMAL_TEXT)
25819 row->fill_line_p = 1; /* Clear to end of line */
25822 if (end_hpos > start_hpos)
25824 draw_row_with_mouse_face (w, start_x, row,
25825 start_hpos, end_hpos, draw);
25827 row->mouse_face_p
25828 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
25832 #ifdef HAVE_WINDOW_SYSTEM
25833 /* When we've written over the cursor, arrange for it to
25834 be displayed again. */
25835 if (FRAME_WINDOW_P (f)
25836 && phys_cursor_on_p && !w->phys_cursor_on_p)
25838 int hpos = w->phys_cursor.hpos;
25840 /* When the window is hscrolled, cursor hpos can legitimately be
25841 out of bounds, but we draw the cursor at the corresponding
25842 window margin in that case. */
25843 if (!row->reversed_p && hpos < 0)
25844 hpos = 0;
25845 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25846 hpos = row->used[TEXT_AREA] - 1;
25848 BLOCK_INPUT;
25849 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
25850 w->phys_cursor.x, w->phys_cursor.y);
25851 UNBLOCK_INPUT;
25853 #endif /* HAVE_WINDOW_SYSTEM */
25856 #ifdef HAVE_WINDOW_SYSTEM
25857 /* Change the mouse cursor. */
25858 if (FRAME_WINDOW_P (f))
25860 if (draw == DRAW_NORMAL_TEXT
25861 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
25862 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
25863 else if (draw == DRAW_MOUSE_FACE)
25864 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
25865 else
25866 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
25868 #endif /* HAVE_WINDOW_SYSTEM */
25871 /* EXPORT:
25872 Clear out the mouse-highlighted active region.
25873 Redraw it un-highlighted first. Value is non-zero if mouse
25874 face was actually drawn unhighlighted. */
25877 clear_mouse_face (Mouse_HLInfo *hlinfo)
25879 int cleared = 0;
25881 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
25883 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
25884 cleared = 1;
25887 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25888 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25889 hlinfo->mouse_face_window = Qnil;
25890 hlinfo->mouse_face_overlay = Qnil;
25891 return cleared;
25894 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
25895 within the mouse face on that window. */
25896 static int
25897 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
25899 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25901 /* Quickly resolve the easy cases. */
25902 if (!(WINDOWP (hlinfo->mouse_face_window)
25903 && XWINDOW (hlinfo->mouse_face_window) == w))
25904 return 0;
25905 if (vpos < hlinfo->mouse_face_beg_row
25906 || vpos > hlinfo->mouse_face_end_row)
25907 return 0;
25908 if (vpos > hlinfo->mouse_face_beg_row
25909 && vpos < hlinfo->mouse_face_end_row)
25910 return 1;
25912 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
25914 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25916 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
25917 return 1;
25919 else if ((vpos == hlinfo->mouse_face_beg_row
25920 && hpos >= hlinfo->mouse_face_beg_col)
25921 || (vpos == hlinfo->mouse_face_end_row
25922 && hpos < hlinfo->mouse_face_end_col))
25923 return 1;
25925 else
25927 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25929 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
25930 return 1;
25932 else if ((vpos == hlinfo->mouse_face_beg_row
25933 && hpos <= hlinfo->mouse_face_beg_col)
25934 || (vpos == hlinfo->mouse_face_end_row
25935 && hpos > hlinfo->mouse_face_end_col))
25936 return 1;
25938 return 0;
25942 /* EXPORT:
25943 Non-zero if physical cursor of window W is within mouse face. */
25946 cursor_in_mouse_face_p (struct window *w)
25948 int hpos = w->phys_cursor.hpos;
25949 int vpos = w->phys_cursor.vpos;
25950 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
25952 /* When the window is hscrolled, cursor hpos can legitimately be out
25953 of bounds, but we draw the cursor at the corresponding window
25954 margin in that case. */
25955 if (!row->reversed_p && hpos < 0)
25956 hpos = 0;
25957 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25958 hpos = row->used[TEXT_AREA] - 1;
25960 return coords_in_mouse_face_p (w, hpos, vpos);
25965 /* Find the glyph rows START_ROW and END_ROW of window W that display
25966 characters between buffer positions START_CHARPOS and END_CHARPOS
25967 (excluding END_CHARPOS). DISP_STRING is a display string that
25968 covers these buffer positions. This is similar to
25969 row_containing_pos, but is more accurate when bidi reordering makes
25970 buffer positions change non-linearly with glyph rows. */
25971 static void
25972 rows_from_pos_range (struct window *w,
25973 EMACS_INT start_charpos, EMACS_INT end_charpos,
25974 Lisp_Object disp_string,
25975 struct glyph_row **start, struct glyph_row **end)
25977 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25978 int last_y = window_text_bottom_y (w);
25979 struct glyph_row *row;
25981 *start = NULL;
25982 *end = NULL;
25984 while (!first->enabled_p
25985 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
25986 first++;
25988 /* Find the START row. */
25989 for (row = first;
25990 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
25991 row++)
25993 /* A row can potentially be the START row if the range of the
25994 characters it displays intersects the range
25995 [START_CHARPOS..END_CHARPOS). */
25996 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
25997 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
25998 /* See the commentary in row_containing_pos, for the
25999 explanation of the complicated way to check whether
26000 some position is beyond the end of the characters
26001 displayed by a row. */
26002 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26003 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26004 && !row->ends_at_zv_p
26005 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26006 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26007 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26008 && !row->ends_at_zv_p
26009 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26011 /* Found a candidate row. Now make sure at least one of the
26012 glyphs it displays has a charpos from the range
26013 [START_CHARPOS..END_CHARPOS).
26015 This is not obvious because bidi reordering could make
26016 buffer positions of a row be 1,2,3,102,101,100, and if we
26017 want to highlight characters in [50..60), we don't want
26018 this row, even though [50..60) does intersect [1..103),
26019 the range of character positions given by the row's start
26020 and end positions. */
26021 struct glyph *g = row->glyphs[TEXT_AREA];
26022 struct glyph *e = g + row->used[TEXT_AREA];
26024 while (g < e)
26026 if (((BUFFERP (g->object) || INTEGERP (g->object))
26027 && start_charpos <= g->charpos && g->charpos < end_charpos)
26028 /* A glyph that comes from DISP_STRING is by
26029 definition to be highlighted. */
26030 || EQ (g->object, disp_string))
26031 *start = row;
26032 g++;
26034 if (*start)
26035 break;
26039 /* Find the END row. */
26040 if (!*start
26041 /* If the last row is partially visible, start looking for END
26042 from that row, instead of starting from FIRST. */
26043 && !(row->enabled_p
26044 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26045 row = first;
26046 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26048 struct glyph_row *next = row + 1;
26049 EMACS_INT next_start = MATRIX_ROW_START_CHARPOS (next);
26051 if (!next->enabled_p
26052 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26053 /* The first row >= START whose range of displayed characters
26054 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26055 is the row END + 1. */
26056 || (start_charpos < next_start
26057 && end_charpos < next_start)
26058 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26059 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26060 && !next->ends_at_zv_p
26061 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26062 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26063 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26064 && !next->ends_at_zv_p
26065 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26067 *end = row;
26068 break;
26070 else
26072 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26073 but none of the characters it displays are in the range, it is
26074 also END + 1. */
26075 struct glyph *g = next->glyphs[TEXT_AREA];
26076 struct glyph *s = g;
26077 struct glyph *e = g + next->used[TEXT_AREA];
26079 while (g < e)
26081 if (((BUFFERP (g->object) || INTEGERP (g->object))
26082 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26083 /* If the buffer position of the first glyph in
26084 the row is equal to END_CHARPOS, it means
26085 the last character to be highlighted is the
26086 newline of ROW, and we must consider NEXT as
26087 END, not END+1. */
26088 || (((!next->reversed_p && g == s)
26089 || (next->reversed_p && g == e - 1))
26090 && (g->charpos == end_charpos
26091 /* Special case for when NEXT is an
26092 empty line at ZV. */
26093 || (g->charpos == -1
26094 && !row->ends_at_zv_p
26095 && next_start == end_charpos)))))
26096 /* A glyph that comes from DISP_STRING is by
26097 definition to be highlighted. */
26098 || EQ (g->object, disp_string))
26099 break;
26100 g++;
26102 if (g == e)
26104 *end = row;
26105 break;
26107 /* The first row that ends at ZV must be the last to be
26108 highlighted. */
26109 else if (next->ends_at_zv_p)
26111 *end = next;
26112 break;
26118 /* This function sets the mouse_face_* elements of HLINFO, assuming
26119 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26120 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26121 for the overlay or run of text properties specifying the mouse
26122 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26123 before-string and after-string that must also be highlighted.
26124 DISP_STRING, if non-nil, is a display string that may cover some
26125 or all of the highlighted text. */
26127 static void
26128 mouse_face_from_buffer_pos (Lisp_Object window,
26129 Mouse_HLInfo *hlinfo,
26130 EMACS_INT mouse_charpos,
26131 EMACS_INT start_charpos,
26132 EMACS_INT end_charpos,
26133 Lisp_Object before_string,
26134 Lisp_Object after_string,
26135 Lisp_Object disp_string)
26137 struct window *w = XWINDOW (window);
26138 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26139 struct glyph_row *r1, *r2;
26140 struct glyph *glyph, *end;
26141 EMACS_INT ignore, pos;
26142 int x;
26144 xassert (NILP (disp_string) || STRINGP (disp_string));
26145 xassert (NILP (before_string) || STRINGP (before_string));
26146 xassert (NILP (after_string) || STRINGP (after_string));
26148 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26149 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26150 if (r1 == NULL)
26151 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26152 /* If the before-string or display-string contains newlines,
26153 rows_from_pos_range skips to its last row. Move back. */
26154 if (!NILP (before_string) || !NILP (disp_string))
26156 struct glyph_row *prev;
26157 while ((prev = r1 - 1, prev >= first)
26158 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26159 && prev->used[TEXT_AREA] > 0)
26161 struct glyph *beg = prev->glyphs[TEXT_AREA];
26162 glyph = beg + prev->used[TEXT_AREA];
26163 while (--glyph >= beg && INTEGERP (glyph->object));
26164 if (glyph < beg
26165 || !(EQ (glyph->object, before_string)
26166 || EQ (glyph->object, disp_string)))
26167 break;
26168 r1 = prev;
26171 if (r2 == NULL)
26173 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26174 hlinfo->mouse_face_past_end = 1;
26176 else if (!NILP (after_string))
26178 /* If the after-string has newlines, advance to its last row. */
26179 struct glyph_row *next;
26180 struct glyph_row *last
26181 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26183 for (next = r2 + 1;
26184 next <= last
26185 && next->used[TEXT_AREA] > 0
26186 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26187 ++next)
26188 r2 = next;
26190 /* The rest of the display engine assumes that mouse_face_beg_row is
26191 either above mouse_face_end_row or identical to it. But with
26192 bidi-reordered continued lines, the row for START_CHARPOS could
26193 be below the row for END_CHARPOS. If so, swap the rows and store
26194 them in correct order. */
26195 if (r1->y > r2->y)
26197 struct glyph_row *tem = r2;
26199 r2 = r1;
26200 r1 = tem;
26203 hlinfo->mouse_face_beg_y = r1->y;
26204 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26205 hlinfo->mouse_face_end_y = r2->y;
26206 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26208 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26209 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26210 could be anywhere in the row and in any order. The strategy
26211 below is to find the leftmost and the rightmost glyph that
26212 belongs to either of these 3 strings, or whose position is
26213 between START_CHARPOS and END_CHARPOS, and highlight all the
26214 glyphs between those two. This may cover more than just the text
26215 between START_CHARPOS and END_CHARPOS if the range of characters
26216 strides the bidi level boundary, e.g. if the beginning is in R2L
26217 text while the end is in L2R text or vice versa. */
26218 if (!r1->reversed_p)
26220 /* This row is in a left to right paragraph. Scan it left to
26221 right. */
26222 glyph = r1->glyphs[TEXT_AREA];
26223 end = glyph + r1->used[TEXT_AREA];
26224 x = r1->x;
26226 /* Skip truncation glyphs at the start of the glyph row. */
26227 if (r1->displays_text_p)
26228 for (; glyph < end
26229 && INTEGERP (glyph->object)
26230 && glyph->charpos < 0;
26231 ++glyph)
26232 x += glyph->pixel_width;
26234 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26235 or DISP_STRING, and the first glyph from buffer whose
26236 position is between START_CHARPOS and END_CHARPOS. */
26237 for (; glyph < end
26238 && !INTEGERP (glyph->object)
26239 && !EQ (glyph->object, disp_string)
26240 && !(BUFFERP (glyph->object)
26241 && (glyph->charpos >= start_charpos
26242 && glyph->charpos < end_charpos));
26243 ++glyph)
26245 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26246 are present at buffer positions between START_CHARPOS and
26247 END_CHARPOS, or if they come from an overlay. */
26248 if (EQ (glyph->object, before_string))
26250 pos = string_buffer_position (before_string,
26251 start_charpos);
26252 /* If pos == 0, it means before_string came from an
26253 overlay, not from a buffer position. */
26254 if (!pos || (pos >= start_charpos && pos < end_charpos))
26255 break;
26257 else if (EQ (glyph->object, after_string))
26259 pos = string_buffer_position (after_string, end_charpos);
26260 if (!pos || (pos >= start_charpos && pos < end_charpos))
26261 break;
26263 x += glyph->pixel_width;
26265 hlinfo->mouse_face_beg_x = x;
26266 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26268 else
26270 /* This row is in a right to left paragraph. Scan it right to
26271 left. */
26272 struct glyph *g;
26274 end = r1->glyphs[TEXT_AREA] - 1;
26275 glyph = end + r1->used[TEXT_AREA];
26277 /* Skip truncation glyphs at the start of the glyph row. */
26278 if (r1->displays_text_p)
26279 for (; glyph > end
26280 && INTEGERP (glyph->object)
26281 && glyph->charpos < 0;
26282 --glyph)
26285 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26286 or DISP_STRING, and the first glyph from buffer whose
26287 position is between START_CHARPOS and END_CHARPOS. */
26288 for (; glyph > end
26289 && !INTEGERP (glyph->object)
26290 && !EQ (glyph->object, disp_string)
26291 && !(BUFFERP (glyph->object)
26292 && (glyph->charpos >= start_charpos
26293 && glyph->charpos < end_charpos));
26294 --glyph)
26296 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26297 are present at buffer positions between START_CHARPOS and
26298 END_CHARPOS, or if they come from an overlay. */
26299 if (EQ (glyph->object, before_string))
26301 pos = string_buffer_position (before_string, start_charpos);
26302 /* If pos == 0, it means before_string came from an
26303 overlay, not from a buffer position. */
26304 if (!pos || (pos >= start_charpos && pos < end_charpos))
26305 break;
26307 else if (EQ (glyph->object, after_string))
26309 pos = string_buffer_position (after_string, end_charpos);
26310 if (!pos || (pos >= start_charpos && pos < end_charpos))
26311 break;
26315 glyph++; /* first glyph to the right of the highlighted area */
26316 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26317 x += g->pixel_width;
26318 hlinfo->mouse_face_beg_x = x;
26319 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26322 /* If the highlight ends in a different row, compute GLYPH and END
26323 for the end row. Otherwise, reuse the values computed above for
26324 the row where the highlight begins. */
26325 if (r2 != r1)
26327 if (!r2->reversed_p)
26329 glyph = r2->glyphs[TEXT_AREA];
26330 end = glyph + r2->used[TEXT_AREA];
26331 x = r2->x;
26333 else
26335 end = r2->glyphs[TEXT_AREA] - 1;
26336 glyph = end + r2->used[TEXT_AREA];
26340 if (!r2->reversed_p)
26342 /* Skip truncation and continuation glyphs near the end of the
26343 row, and also blanks and stretch glyphs inserted by
26344 extend_face_to_end_of_line. */
26345 while (end > glyph
26346 && INTEGERP ((end - 1)->object))
26347 --end;
26348 /* Scan the rest of the glyph row from the end, looking for the
26349 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26350 DISP_STRING, or whose position is between START_CHARPOS
26351 and END_CHARPOS */
26352 for (--end;
26353 end > glyph
26354 && !INTEGERP (end->object)
26355 && !EQ (end->object, disp_string)
26356 && !(BUFFERP (end->object)
26357 && (end->charpos >= start_charpos
26358 && end->charpos < end_charpos));
26359 --end)
26361 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26362 are present at buffer positions between START_CHARPOS and
26363 END_CHARPOS, or if they come from an overlay. */
26364 if (EQ (end->object, before_string))
26366 pos = string_buffer_position (before_string, start_charpos);
26367 if (!pos || (pos >= start_charpos && pos < end_charpos))
26368 break;
26370 else if (EQ (end->object, after_string))
26372 pos = string_buffer_position (after_string, end_charpos);
26373 if (!pos || (pos >= start_charpos && pos < end_charpos))
26374 break;
26377 /* Find the X coordinate of the last glyph to be highlighted. */
26378 for (; glyph <= end; ++glyph)
26379 x += glyph->pixel_width;
26381 hlinfo->mouse_face_end_x = x;
26382 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26384 else
26386 /* Skip truncation and continuation glyphs near the end of the
26387 row, and also blanks and stretch glyphs inserted by
26388 extend_face_to_end_of_line. */
26389 x = r2->x;
26390 end++;
26391 while (end < glyph
26392 && INTEGERP (end->object))
26394 x += end->pixel_width;
26395 ++end;
26397 /* Scan the rest of the glyph row from the end, looking for the
26398 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26399 DISP_STRING, or whose position is between START_CHARPOS
26400 and END_CHARPOS */
26401 for ( ;
26402 end < glyph
26403 && !INTEGERP (end->object)
26404 && !EQ (end->object, disp_string)
26405 && !(BUFFERP (end->object)
26406 && (end->charpos >= start_charpos
26407 && end->charpos < end_charpos));
26408 ++end)
26410 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26411 are present at buffer positions between START_CHARPOS and
26412 END_CHARPOS, or if they come from an overlay. */
26413 if (EQ (end->object, before_string))
26415 pos = string_buffer_position (before_string, start_charpos);
26416 if (!pos || (pos >= start_charpos && pos < end_charpos))
26417 break;
26419 else if (EQ (end->object, after_string))
26421 pos = string_buffer_position (after_string, end_charpos);
26422 if (!pos || (pos >= start_charpos && pos < end_charpos))
26423 break;
26425 x += end->pixel_width;
26427 /* If we exited the above loop because we arrived at the last
26428 glyph of the row, and its buffer position is still not in
26429 range, it means the last character in range is the preceding
26430 newline. Bump the end column and x values to get past the
26431 last glyph. */
26432 if (end == glyph
26433 && BUFFERP (end->object)
26434 && (end->charpos < start_charpos
26435 || end->charpos >= end_charpos))
26437 x += end->pixel_width;
26438 ++end;
26440 hlinfo->mouse_face_end_x = x;
26441 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26444 hlinfo->mouse_face_window = window;
26445 hlinfo->mouse_face_face_id
26446 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26447 mouse_charpos + 1,
26448 !hlinfo->mouse_face_hidden, -1);
26449 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26452 /* The following function is not used anymore (replaced with
26453 mouse_face_from_string_pos), but I leave it here for the time
26454 being, in case someone would. */
26456 #if 0 /* not used */
26458 /* Find the position of the glyph for position POS in OBJECT in
26459 window W's current matrix, and return in *X, *Y the pixel
26460 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26462 RIGHT_P non-zero means return the position of the right edge of the
26463 glyph, RIGHT_P zero means return the left edge position.
26465 If no glyph for POS exists in the matrix, return the position of
26466 the glyph with the next smaller position that is in the matrix, if
26467 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26468 exists in the matrix, return the position of the glyph with the
26469 next larger position in OBJECT.
26471 Value is non-zero if a glyph was found. */
26473 static int
26474 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
26475 int *hpos, int *vpos, int *x, int *y, int right_p)
26477 int yb = window_text_bottom_y (w);
26478 struct glyph_row *r;
26479 struct glyph *best_glyph = NULL;
26480 struct glyph_row *best_row = NULL;
26481 int best_x = 0;
26483 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26484 r->enabled_p && r->y < yb;
26485 ++r)
26487 struct glyph *g = r->glyphs[TEXT_AREA];
26488 struct glyph *e = g + r->used[TEXT_AREA];
26489 int gx;
26491 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26492 if (EQ (g->object, object))
26494 if (g->charpos == pos)
26496 best_glyph = g;
26497 best_x = gx;
26498 best_row = r;
26499 goto found;
26501 else if (best_glyph == NULL
26502 || ((eabs (g->charpos - pos)
26503 < eabs (best_glyph->charpos - pos))
26504 && (right_p
26505 ? g->charpos < pos
26506 : g->charpos > pos)))
26508 best_glyph = g;
26509 best_x = gx;
26510 best_row = r;
26515 found:
26517 if (best_glyph)
26519 *x = best_x;
26520 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26522 if (right_p)
26524 *x += best_glyph->pixel_width;
26525 ++*hpos;
26528 *y = best_row->y;
26529 *vpos = best_row - w->current_matrix->rows;
26532 return best_glyph != NULL;
26534 #endif /* not used */
26536 /* Find the positions of the first and the last glyphs in window W's
26537 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26538 (assumed to be a string), and return in HLINFO's mouse_face_*
26539 members the pixel and column/row coordinates of those glyphs. */
26541 static void
26542 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26543 Lisp_Object object,
26544 EMACS_INT startpos, EMACS_INT endpos)
26546 int yb = window_text_bottom_y (w);
26547 struct glyph_row *r;
26548 struct glyph *g, *e;
26549 int gx;
26550 int found = 0;
26552 /* Find the glyph row with at least one position in the range
26553 [STARTPOS..ENDPOS], and the first glyph in that row whose
26554 position belongs to that range. */
26555 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26556 r->enabled_p && r->y < yb;
26557 ++r)
26559 if (!r->reversed_p)
26561 g = r->glyphs[TEXT_AREA];
26562 e = g + r->used[TEXT_AREA];
26563 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26564 if (EQ (g->object, object)
26565 && startpos <= g->charpos && g->charpos <= endpos)
26567 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26568 hlinfo->mouse_face_beg_y = r->y;
26569 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26570 hlinfo->mouse_face_beg_x = gx;
26571 found = 1;
26572 break;
26575 else
26577 struct glyph *g1;
26579 e = r->glyphs[TEXT_AREA];
26580 g = e + r->used[TEXT_AREA];
26581 for ( ; g > e; --g)
26582 if (EQ ((g-1)->object, object)
26583 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26585 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26586 hlinfo->mouse_face_beg_y = r->y;
26587 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26588 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26589 gx += g1->pixel_width;
26590 hlinfo->mouse_face_beg_x = gx;
26591 found = 1;
26592 break;
26595 if (found)
26596 break;
26599 if (!found)
26600 return;
26602 /* Starting with the next row, look for the first row which does NOT
26603 include any glyphs whose positions are in the range. */
26604 for (++r; r->enabled_p && r->y < yb; ++r)
26606 g = r->glyphs[TEXT_AREA];
26607 e = g + r->used[TEXT_AREA];
26608 found = 0;
26609 for ( ; g < e; ++g)
26610 if (EQ (g->object, object)
26611 && startpos <= g->charpos && g->charpos <= endpos)
26613 found = 1;
26614 break;
26616 if (!found)
26617 break;
26620 /* The highlighted region ends on the previous row. */
26621 r--;
26623 /* Set the end row and its vertical pixel coordinate. */
26624 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
26625 hlinfo->mouse_face_end_y = r->y;
26627 /* Compute and set the end column and the end column's horizontal
26628 pixel coordinate. */
26629 if (!r->reversed_p)
26631 g = r->glyphs[TEXT_AREA];
26632 e = g + r->used[TEXT_AREA];
26633 for ( ; e > g; --e)
26634 if (EQ ((e-1)->object, object)
26635 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
26636 break;
26637 hlinfo->mouse_face_end_col = e - g;
26639 for (gx = r->x; g < e; ++g)
26640 gx += g->pixel_width;
26641 hlinfo->mouse_face_end_x = gx;
26643 else
26645 e = r->glyphs[TEXT_AREA];
26646 g = e + r->used[TEXT_AREA];
26647 for (gx = r->x ; e < g; ++e)
26649 if (EQ (e->object, object)
26650 && startpos <= e->charpos && e->charpos <= endpos)
26651 break;
26652 gx += e->pixel_width;
26654 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
26655 hlinfo->mouse_face_end_x = gx;
26659 #ifdef HAVE_WINDOW_SYSTEM
26661 /* See if position X, Y is within a hot-spot of an image. */
26663 static int
26664 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
26666 if (!CONSP (hot_spot))
26667 return 0;
26669 if (EQ (XCAR (hot_spot), Qrect))
26671 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
26672 Lisp_Object rect = XCDR (hot_spot);
26673 Lisp_Object tem;
26674 if (!CONSP (rect))
26675 return 0;
26676 if (!CONSP (XCAR (rect)))
26677 return 0;
26678 if (!CONSP (XCDR (rect)))
26679 return 0;
26680 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
26681 return 0;
26682 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
26683 return 0;
26684 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
26685 return 0;
26686 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
26687 return 0;
26688 return 1;
26690 else if (EQ (XCAR (hot_spot), Qcircle))
26692 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
26693 Lisp_Object circ = XCDR (hot_spot);
26694 Lisp_Object lr, lx0, ly0;
26695 if (CONSP (circ)
26696 && CONSP (XCAR (circ))
26697 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
26698 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
26699 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
26701 double r = XFLOATINT (lr);
26702 double dx = XINT (lx0) - x;
26703 double dy = XINT (ly0) - y;
26704 return (dx * dx + dy * dy <= r * r);
26707 else if (EQ (XCAR (hot_spot), Qpoly))
26709 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
26710 if (VECTORP (XCDR (hot_spot)))
26712 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
26713 Lisp_Object *poly = v->contents;
26714 int n = v->header.size;
26715 int i;
26716 int inside = 0;
26717 Lisp_Object lx, ly;
26718 int x0, y0;
26720 /* Need an even number of coordinates, and at least 3 edges. */
26721 if (n < 6 || n & 1)
26722 return 0;
26724 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
26725 If count is odd, we are inside polygon. Pixels on edges
26726 may or may not be included depending on actual geometry of the
26727 polygon. */
26728 if ((lx = poly[n-2], !INTEGERP (lx))
26729 || (ly = poly[n-1], !INTEGERP (lx)))
26730 return 0;
26731 x0 = XINT (lx), y0 = XINT (ly);
26732 for (i = 0; i < n; i += 2)
26734 int x1 = x0, y1 = y0;
26735 if ((lx = poly[i], !INTEGERP (lx))
26736 || (ly = poly[i+1], !INTEGERP (ly)))
26737 return 0;
26738 x0 = XINT (lx), y0 = XINT (ly);
26740 /* Does this segment cross the X line? */
26741 if (x0 >= x)
26743 if (x1 >= x)
26744 continue;
26746 else if (x1 < x)
26747 continue;
26748 if (y > y0 && y > y1)
26749 continue;
26750 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
26751 inside = !inside;
26753 return inside;
26756 return 0;
26759 Lisp_Object
26760 find_hot_spot (Lisp_Object map, int x, int y)
26762 while (CONSP (map))
26764 if (CONSP (XCAR (map))
26765 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
26766 return XCAR (map);
26767 map = XCDR (map);
26770 return Qnil;
26773 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
26774 3, 3, 0,
26775 doc: /* Lookup in image map MAP coordinates X and Y.
26776 An image map is an alist where each element has the format (AREA ID PLIST).
26777 An AREA is specified as either a rectangle, a circle, or a polygon:
26778 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
26779 pixel coordinates of the upper left and bottom right corners.
26780 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
26781 and the radius of the circle; r may be a float or integer.
26782 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
26783 vector describes one corner in the polygon.
26784 Returns the alist element for the first matching AREA in MAP. */)
26785 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
26787 if (NILP (map))
26788 return Qnil;
26790 CHECK_NUMBER (x);
26791 CHECK_NUMBER (y);
26793 return find_hot_spot (map, XINT (x), XINT (y));
26797 /* Display frame CURSOR, optionally using shape defined by POINTER. */
26798 static void
26799 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
26801 /* Do not change cursor shape while dragging mouse. */
26802 if (!NILP (do_mouse_tracking))
26803 return;
26805 if (!NILP (pointer))
26807 if (EQ (pointer, Qarrow))
26808 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26809 else if (EQ (pointer, Qhand))
26810 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
26811 else if (EQ (pointer, Qtext))
26812 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26813 else if (EQ (pointer, intern ("hdrag")))
26814 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26815 #ifdef HAVE_X_WINDOWS
26816 else if (EQ (pointer, intern ("vdrag")))
26817 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
26818 #endif
26819 else if (EQ (pointer, intern ("hourglass")))
26820 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
26821 else if (EQ (pointer, Qmodeline))
26822 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
26823 else
26824 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26827 if (cursor != No_Cursor)
26828 FRAME_RIF (f)->define_frame_cursor (f, cursor);
26831 #endif /* HAVE_WINDOW_SYSTEM */
26833 /* Take proper action when mouse has moved to the mode or header line
26834 or marginal area AREA of window W, x-position X and y-position Y.
26835 X is relative to the start of the text display area of W, so the
26836 width of bitmap areas and scroll bars must be subtracted to get a
26837 position relative to the start of the mode line. */
26839 static void
26840 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
26841 enum window_part area)
26843 struct window *w = XWINDOW (window);
26844 struct frame *f = XFRAME (w->frame);
26845 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26846 #ifdef HAVE_WINDOW_SYSTEM
26847 Display_Info *dpyinfo;
26848 #endif
26849 Cursor cursor = No_Cursor;
26850 Lisp_Object pointer = Qnil;
26851 int dx, dy, width, height;
26852 EMACS_INT charpos;
26853 Lisp_Object string, object = Qnil;
26854 Lisp_Object pos, help;
26856 Lisp_Object mouse_face;
26857 int original_x_pixel = x;
26858 struct glyph * glyph = NULL, * row_start_glyph = NULL;
26859 struct glyph_row *row;
26861 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
26863 int x0;
26864 struct glyph *end;
26866 /* Kludge alert: mode_line_string takes X/Y in pixels, but
26867 returns them in row/column units! */
26868 string = mode_line_string (w, area, &x, &y, &charpos,
26869 &object, &dx, &dy, &width, &height);
26871 row = (area == ON_MODE_LINE
26872 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
26873 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
26875 /* Find the glyph under the mouse pointer. */
26876 if (row->mode_line_p && row->enabled_p)
26878 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
26879 end = glyph + row->used[TEXT_AREA];
26881 for (x0 = original_x_pixel;
26882 glyph < end && x0 >= glyph->pixel_width;
26883 ++glyph)
26884 x0 -= glyph->pixel_width;
26886 if (glyph >= end)
26887 glyph = NULL;
26890 else
26892 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
26893 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
26894 returns them in row/column units! */
26895 string = marginal_area_string (w, area, &x, &y, &charpos,
26896 &object, &dx, &dy, &width, &height);
26899 help = Qnil;
26901 #ifdef HAVE_WINDOW_SYSTEM
26902 if (IMAGEP (object))
26904 Lisp_Object image_map, hotspot;
26905 if ((image_map = Fplist_get (XCDR (object), QCmap),
26906 !NILP (image_map))
26907 && (hotspot = find_hot_spot (image_map, dx, dy),
26908 CONSP (hotspot))
26909 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
26911 Lisp_Object plist;
26913 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
26914 If so, we could look for mouse-enter, mouse-leave
26915 properties in PLIST (and do something...). */
26916 hotspot = XCDR (hotspot);
26917 if (CONSP (hotspot)
26918 && (plist = XCAR (hotspot), CONSP (plist)))
26920 pointer = Fplist_get (plist, Qpointer);
26921 if (NILP (pointer))
26922 pointer = Qhand;
26923 help = Fplist_get (plist, Qhelp_echo);
26924 if (!NILP (help))
26926 help_echo_string = help;
26927 /* Is this correct? ++kfs */
26928 XSETWINDOW (help_echo_window, w);
26929 help_echo_object = w->buffer;
26930 help_echo_pos = charpos;
26934 if (NILP (pointer))
26935 pointer = Fplist_get (XCDR (object), QCpointer);
26937 #endif /* HAVE_WINDOW_SYSTEM */
26939 if (STRINGP (string))
26941 pos = make_number (charpos);
26942 /* If we're on a string with `help-echo' text property, arrange
26943 for the help to be displayed. This is done by setting the
26944 global variable help_echo_string to the help string. */
26945 if (NILP (help))
26947 help = Fget_text_property (pos, Qhelp_echo, string);
26948 if (!NILP (help))
26950 help_echo_string = help;
26951 XSETWINDOW (help_echo_window, w);
26952 help_echo_object = string;
26953 help_echo_pos = charpos;
26957 #ifdef HAVE_WINDOW_SYSTEM
26958 if (FRAME_WINDOW_P (f))
26960 dpyinfo = FRAME_X_DISPLAY_INFO (f);
26961 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26962 if (NILP (pointer))
26963 pointer = Fget_text_property (pos, Qpointer, string);
26965 /* Change the mouse pointer according to what is under X/Y. */
26966 if (NILP (pointer)
26967 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
26969 Lisp_Object map;
26970 map = Fget_text_property (pos, Qlocal_map, string);
26971 if (!KEYMAPP (map))
26972 map = Fget_text_property (pos, Qkeymap, string);
26973 if (!KEYMAPP (map))
26974 cursor = dpyinfo->vertical_scroll_bar_cursor;
26977 #endif
26979 /* Change the mouse face according to what is under X/Y. */
26980 mouse_face = Fget_text_property (pos, Qmouse_face, string);
26981 if (!NILP (mouse_face)
26982 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
26983 && glyph)
26985 Lisp_Object b, e;
26987 struct glyph * tmp_glyph;
26989 int gpos;
26990 int gseq_length;
26991 int total_pixel_width;
26992 EMACS_INT begpos, endpos, ignore;
26994 int vpos, hpos;
26996 b = Fprevious_single_property_change (make_number (charpos + 1),
26997 Qmouse_face, string, Qnil);
26998 if (NILP (b))
26999 begpos = 0;
27000 else
27001 begpos = XINT (b);
27003 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27004 if (NILP (e))
27005 endpos = SCHARS (string);
27006 else
27007 endpos = XINT (e);
27009 /* Calculate the glyph position GPOS of GLYPH in the
27010 displayed string, relative to the beginning of the
27011 highlighted part of the string.
27013 Note: GPOS is different from CHARPOS. CHARPOS is the
27014 position of GLYPH in the internal string object. A mode
27015 line string format has structures which are converted to
27016 a flattened string by the Emacs Lisp interpreter. The
27017 internal string is an element of those structures. The
27018 displayed string is the flattened string. */
27019 tmp_glyph = row_start_glyph;
27020 while (tmp_glyph < glyph
27021 && (!(EQ (tmp_glyph->object, glyph->object)
27022 && begpos <= tmp_glyph->charpos
27023 && tmp_glyph->charpos < endpos)))
27024 tmp_glyph++;
27025 gpos = glyph - tmp_glyph;
27027 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27028 the highlighted part of the displayed string to which
27029 GLYPH belongs. Note: GSEQ_LENGTH is different from
27030 SCHARS (STRING), because the latter returns the length of
27031 the internal string. */
27032 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27033 tmp_glyph > glyph
27034 && (!(EQ (tmp_glyph->object, glyph->object)
27035 && begpos <= tmp_glyph->charpos
27036 && tmp_glyph->charpos < endpos));
27037 tmp_glyph--)
27039 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27041 /* Calculate the total pixel width of all the glyphs between
27042 the beginning of the highlighted area and GLYPH. */
27043 total_pixel_width = 0;
27044 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27045 total_pixel_width += tmp_glyph->pixel_width;
27047 /* Pre calculation of re-rendering position. Note: X is in
27048 column units here, after the call to mode_line_string or
27049 marginal_area_string. */
27050 hpos = x - gpos;
27051 vpos = (area == ON_MODE_LINE
27052 ? (w->current_matrix)->nrows - 1
27053 : 0);
27055 /* If GLYPH's position is included in the region that is
27056 already drawn in mouse face, we have nothing to do. */
27057 if ( EQ (window, hlinfo->mouse_face_window)
27058 && (!row->reversed_p
27059 ? (hlinfo->mouse_face_beg_col <= hpos
27060 && hpos < hlinfo->mouse_face_end_col)
27061 /* In R2L rows we swap BEG and END, see below. */
27062 : (hlinfo->mouse_face_end_col <= hpos
27063 && hpos < hlinfo->mouse_face_beg_col))
27064 && hlinfo->mouse_face_beg_row == vpos )
27065 return;
27067 if (clear_mouse_face (hlinfo))
27068 cursor = No_Cursor;
27070 if (!row->reversed_p)
27072 hlinfo->mouse_face_beg_col = hpos;
27073 hlinfo->mouse_face_beg_x = original_x_pixel
27074 - (total_pixel_width + dx);
27075 hlinfo->mouse_face_end_col = hpos + gseq_length;
27076 hlinfo->mouse_face_end_x = 0;
27078 else
27080 /* In R2L rows, show_mouse_face expects BEG and END
27081 coordinates to be swapped. */
27082 hlinfo->mouse_face_end_col = hpos;
27083 hlinfo->mouse_face_end_x = original_x_pixel
27084 - (total_pixel_width + dx);
27085 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27086 hlinfo->mouse_face_beg_x = 0;
27089 hlinfo->mouse_face_beg_row = vpos;
27090 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27091 hlinfo->mouse_face_beg_y = 0;
27092 hlinfo->mouse_face_end_y = 0;
27093 hlinfo->mouse_face_past_end = 0;
27094 hlinfo->mouse_face_window = window;
27096 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27097 charpos,
27098 0, 0, 0,
27099 &ignore,
27100 glyph->face_id,
27102 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27104 if (NILP (pointer))
27105 pointer = Qhand;
27107 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27108 clear_mouse_face (hlinfo);
27110 #ifdef HAVE_WINDOW_SYSTEM
27111 if (FRAME_WINDOW_P (f))
27112 define_frame_cursor1 (f, cursor, pointer);
27113 #endif
27117 /* EXPORT:
27118 Take proper action when the mouse has moved to position X, Y on
27119 frame F as regards highlighting characters that have mouse-face
27120 properties. Also de-highlighting chars where the mouse was before.
27121 X and Y can be negative or out of range. */
27123 void
27124 note_mouse_highlight (struct frame *f, int x, int y)
27126 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27127 enum window_part part = ON_NOTHING;
27128 Lisp_Object window;
27129 struct window *w;
27130 Cursor cursor = No_Cursor;
27131 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27132 struct buffer *b;
27134 /* When a menu is active, don't highlight because this looks odd. */
27135 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27136 if (popup_activated ())
27137 return;
27138 #endif
27140 if (NILP (Vmouse_highlight)
27141 || !f->glyphs_initialized_p
27142 || f->pointer_invisible)
27143 return;
27145 hlinfo->mouse_face_mouse_x = x;
27146 hlinfo->mouse_face_mouse_y = y;
27147 hlinfo->mouse_face_mouse_frame = f;
27149 if (hlinfo->mouse_face_defer)
27150 return;
27152 if (gc_in_progress)
27154 hlinfo->mouse_face_deferred_gc = 1;
27155 return;
27158 /* Which window is that in? */
27159 window = window_from_coordinates (f, x, y, &part, 1);
27161 /* If displaying active text in another window, clear that. */
27162 if (! EQ (window, hlinfo->mouse_face_window)
27163 /* Also clear if we move out of text area in same window. */
27164 || (!NILP (hlinfo->mouse_face_window)
27165 && !NILP (window)
27166 && part != ON_TEXT
27167 && part != ON_MODE_LINE
27168 && part != ON_HEADER_LINE))
27169 clear_mouse_face (hlinfo);
27171 /* Not on a window -> return. */
27172 if (!WINDOWP (window))
27173 return;
27175 /* Reset help_echo_string. It will get recomputed below. */
27176 help_echo_string = Qnil;
27178 /* Convert to window-relative pixel coordinates. */
27179 w = XWINDOW (window);
27180 frame_to_window_pixel_xy (w, &x, &y);
27182 #ifdef HAVE_WINDOW_SYSTEM
27183 /* Handle tool-bar window differently since it doesn't display a
27184 buffer. */
27185 if (EQ (window, f->tool_bar_window))
27187 note_tool_bar_highlight (f, x, y);
27188 return;
27190 #endif
27192 /* Mouse is on the mode, header line or margin? */
27193 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27194 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27196 note_mode_line_or_margin_highlight (window, x, y, part);
27197 return;
27200 #ifdef HAVE_WINDOW_SYSTEM
27201 if (part == ON_VERTICAL_BORDER)
27203 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27204 help_echo_string = build_string ("drag-mouse-1: resize");
27206 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27207 || part == ON_SCROLL_BAR)
27208 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27209 else
27210 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27211 #endif
27213 /* Are we in a window whose display is up to date?
27214 And verify the buffer's text has not changed. */
27215 b = XBUFFER (w->buffer);
27216 if (part == ON_TEXT
27217 && EQ (w->window_end_valid, w->buffer)
27218 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
27219 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
27221 int hpos, vpos, dx, dy, area = LAST_AREA;
27222 EMACS_INT pos;
27223 struct glyph *glyph;
27224 Lisp_Object object;
27225 Lisp_Object mouse_face = Qnil, position;
27226 Lisp_Object *overlay_vec = NULL;
27227 ptrdiff_t i, noverlays;
27228 struct buffer *obuf;
27229 EMACS_INT obegv, ozv;
27230 int same_region;
27232 /* Find the glyph under X/Y. */
27233 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27235 #ifdef HAVE_WINDOW_SYSTEM
27236 /* Look for :pointer property on image. */
27237 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27239 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27240 if (img != NULL && IMAGEP (img->spec))
27242 Lisp_Object image_map, hotspot;
27243 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27244 !NILP (image_map))
27245 && (hotspot = find_hot_spot (image_map,
27246 glyph->slice.img.x + dx,
27247 glyph->slice.img.y + dy),
27248 CONSP (hotspot))
27249 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27251 Lisp_Object plist;
27253 /* Could check XCAR (hotspot) to see if we enter/leave
27254 this hot-spot.
27255 If so, we could look for mouse-enter, mouse-leave
27256 properties in PLIST (and do something...). */
27257 hotspot = XCDR (hotspot);
27258 if (CONSP (hotspot)
27259 && (plist = XCAR (hotspot), CONSP (plist)))
27261 pointer = Fplist_get (plist, Qpointer);
27262 if (NILP (pointer))
27263 pointer = Qhand;
27264 help_echo_string = Fplist_get (plist, Qhelp_echo);
27265 if (!NILP (help_echo_string))
27267 help_echo_window = window;
27268 help_echo_object = glyph->object;
27269 help_echo_pos = glyph->charpos;
27273 if (NILP (pointer))
27274 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27277 #endif /* HAVE_WINDOW_SYSTEM */
27279 /* Clear mouse face if X/Y not over text. */
27280 if (glyph == NULL
27281 || area != TEXT_AREA
27282 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27283 /* Glyph's OBJECT is an integer for glyphs inserted by the
27284 display engine for its internal purposes, like truncation
27285 and continuation glyphs and blanks beyond the end of
27286 line's text on text terminals. If we are over such a
27287 glyph, we are not over any text. */
27288 || INTEGERP (glyph->object)
27289 /* R2L rows have a stretch glyph at their front, which
27290 stands for no text, whereas L2R rows have no glyphs at
27291 all beyond the end of text. Treat such stretch glyphs
27292 like we do with NULL glyphs in L2R rows. */
27293 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27294 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27295 && glyph->type == STRETCH_GLYPH
27296 && glyph->avoid_cursor_p))
27298 if (clear_mouse_face (hlinfo))
27299 cursor = No_Cursor;
27300 #ifdef HAVE_WINDOW_SYSTEM
27301 if (FRAME_WINDOW_P (f) && NILP (pointer))
27303 if (area != TEXT_AREA)
27304 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27305 else
27306 pointer = Vvoid_text_area_pointer;
27308 #endif
27309 goto set_cursor;
27312 pos = glyph->charpos;
27313 object = glyph->object;
27314 if (!STRINGP (object) && !BUFFERP (object))
27315 goto set_cursor;
27317 /* If we get an out-of-range value, return now; avoid an error. */
27318 if (BUFFERP (object) && pos > BUF_Z (b))
27319 goto set_cursor;
27321 /* Make the window's buffer temporarily current for
27322 overlays_at and compute_char_face. */
27323 obuf = current_buffer;
27324 current_buffer = b;
27325 obegv = BEGV;
27326 ozv = ZV;
27327 BEGV = BEG;
27328 ZV = Z;
27330 /* Is this char mouse-active or does it have help-echo? */
27331 position = make_number (pos);
27333 if (BUFFERP (object))
27335 /* Put all the overlays we want in a vector in overlay_vec. */
27336 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27337 /* Sort overlays into increasing priority order. */
27338 noverlays = sort_overlays (overlay_vec, noverlays, w);
27340 else
27341 noverlays = 0;
27343 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27345 if (same_region)
27346 cursor = No_Cursor;
27348 /* Check mouse-face highlighting. */
27349 if (! same_region
27350 /* If there exists an overlay with mouse-face overlapping
27351 the one we are currently highlighting, we have to
27352 check if we enter the overlapping overlay, and then
27353 highlight only that. */
27354 || (OVERLAYP (hlinfo->mouse_face_overlay)
27355 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27357 /* Find the highest priority overlay with a mouse-face. */
27358 Lisp_Object overlay = Qnil;
27359 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27361 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27362 if (!NILP (mouse_face))
27363 overlay = overlay_vec[i];
27366 /* If we're highlighting the same overlay as before, there's
27367 no need to do that again. */
27368 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27369 goto check_help_echo;
27370 hlinfo->mouse_face_overlay = overlay;
27372 /* Clear the display of the old active region, if any. */
27373 if (clear_mouse_face (hlinfo))
27374 cursor = No_Cursor;
27376 /* If no overlay applies, get a text property. */
27377 if (NILP (overlay))
27378 mouse_face = Fget_text_property (position, Qmouse_face, object);
27380 /* Next, compute the bounds of the mouse highlighting and
27381 display it. */
27382 if (!NILP (mouse_face) && STRINGP (object))
27384 /* The mouse-highlighting comes from a display string
27385 with a mouse-face. */
27386 Lisp_Object s, e;
27387 EMACS_INT ignore;
27389 s = Fprevious_single_property_change
27390 (make_number (pos + 1), Qmouse_face, object, Qnil);
27391 e = Fnext_single_property_change
27392 (position, Qmouse_face, object, Qnil);
27393 if (NILP (s))
27394 s = make_number (0);
27395 if (NILP (e))
27396 e = make_number (SCHARS (object) - 1);
27397 mouse_face_from_string_pos (w, hlinfo, object,
27398 XINT (s), XINT (e));
27399 hlinfo->mouse_face_past_end = 0;
27400 hlinfo->mouse_face_window = window;
27401 hlinfo->mouse_face_face_id
27402 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27403 glyph->face_id, 1);
27404 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27405 cursor = No_Cursor;
27407 else
27409 /* The mouse-highlighting, if any, comes from an overlay
27410 or text property in the buffer. */
27411 Lisp_Object buffer IF_LINT (= Qnil);
27412 Lisp_Object disp_string IF_LINT (= Qnil);
27414 if (STRINGP (object))
27416 /* If we are on a display string with no mouse-face,
27417 check if the text under it has one. */
27418 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27419 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27420 pos = string_buffer_position (object, start);
27421 if (pos > 0)
27423 mouse_face = get_char_property_and_overlay
27424 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27425 buffer = w->buffer;
27426 disp_string = object;
27429 else
27431 buffer = object;
27432 disp_string = Qnil;
27435 if (!NILP (mouse_face))
27437 Lisp_Object before, after;
27438 Lisp_Object before_string, after_string;
27439 /* To correctly find the limits of mouse highlight
27440 in a bidi-reordered buffer, we must not use the
27441 optimization of limiting the search in
27442 previous-single-property-change and
27443 next-single-property-change, because
27444 rows_from_pos_range needs the real start and end
27445 positions to DTRT in this case. That's because
27446 the first row visible in a window does not
27447 necessarily display the character whose position
27448 is the smallest. */
27449 Lisp_Object lim1 =
27450 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27451 ? Fmarker_position (w->start)
27452 : Qnil;
27453 Lisp_Object lim2 =
27454 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27455 ? make_number (BUF_Z (XBUFFER (buffer))
27456 - XFASTINT (w->window_end_pos))
27457 : Qnil;
27459 if (NILP (overlay))
27461 /* Handle the text property case. */
27462 before = Fprevious_single_property_change
27463 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27464 after = Fnext_single_property_change
27465 (make_number (pos), Qmouse_face, buffer, lim2);
27466 before_string = after_string = Qnil;
27468 else
27470 /* Handle the overlay case. */
27471 before = Foverlay_start (overlay);
27472 after = Foverlay_end (overlay);
27473 before_string = Foverlay_get (overlay, Qbefore_string);
27474 after_string = Foverlay_get (overlay, Qafter_string);
27476 if (!STRINGP (before_string)) before_string = Qnil;
27477 if (!STRINGP (after_string)) after_string = Qnil;
27480 mouse_face_from_buffer_pos (window, hlinfo, pos,
27481 NILP (before)
27483 : XFASTINT (before),
27484 NILP (after)
27485 ? BUF_Z (XBUFFER (buffer))
27486 : XFASTINT (after),
27487 before_string, after_string,
27488 disp_string);
27489 cursor = No_Cursor;
27494 check_help_echo:
27496 /* Look for a `help-echo' property. */
27497 if (NILP (help_echo_string)) {
27498 Lisp_Object help, overlay;
27500 /* Check overlays first. */
27501 help = overlay = Qnil;
27502 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27504 overlay = overlay_vec[i];
27505 help = Foverlay_get (overlay, Qhelp_echo);
27508 if (!NILP (help))
27510 help_echo_string = help;
27511 help_echo_window = window;
27512 help_echo_object = overlay;
27513 help_echo_pos = pos;
27515 else
27517 Lisp_Object obj = glyph->object;
27518 EMACS_INT charpos = glyph->charpos;
27520 /* Try text properties. */
27521 if (STRINGP (obj)
27522 && charpos >= 0
27523 && charpos < SCHARS (obj))
27525 help = Fget_text_property (make_number (charpos),
27526 Qhelp_echo, obj);
27527 if (NILP (help))
27529 /* If the string itself doesn't specify a help-echo,
27530 see if the buffer text ``under'' it does. */
27531 struct glyph_row *r
27532 = MATRIX_ROW (w->current_matrix, vpos);
27533 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27534 EMACS_INT p = string_buffer_position (obj, start);
27535 if (p > 0)
27537 help = Fget_char_property (make_number (p),
27538 Qhelp_echo, w->buffer);
27539 if (!NILP (help))
27541 charpos = p;
27542 obj = w->buffer;
27547 else if (BUFFERP (obj)
27548 && charpos >= BEGV
27549 && charpos < ZV)
27550 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27551 obj);
27553 if (!NILP (help))
27555 help_echo_string = help;
27556 help_echo_window = window;
27557 help_echo_object = obj;
27558 help_echo_pos = charpos;
27563 #ifdef HAVE_WINDOW_SYSTEM
27564 /* Look for a `pointer' property. */
27565 if (FRAME_WINDOW_P (f) && NILP (pointer))
27567 /* Check overlays first. */
27568 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27569 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27571 if (NILP (pointer))
27573 Lisp_Object obj = glyph->object;
27574 EMACS_INT charpos = glyph->charpos;
27576 /* Try text properties. */
27577 if (STRINGP (obj)
27578 && charpos >= 0
27579 && charpos < SCHARS (obj))
27581 pointer = Fget_text_property (make_number (charpos),
27582 Qpointer, obj);
27583 if (NILP (pointer))
27585 /* If the string itself doesn't specify a pointer,
27586 see if the buffer text ``under'' it does. */
27587 struct glyph_row *r
27588 = MATRIX_ROW (w->current_matrix, vpos);
27589 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27590 EMACS_INT p = string_buffer_position (obj, start);
27591 if (p > 0)
27592 pointer = Fget_char_property (make_number (p),
27593 Qpointer, w->buffer);
27596 else if (BUFFERP (obj)
27597 && charpos >= BEGV
27598 && charpos < ZV)
27599 pointer = Fget_text_property (make_number (charpos),
27600 Qpointer, obj);
27603 #endif /* HAVE_WINDOW_SYSTEM */
27605 BEGV = obegv;
27606 ZV = ozv;
27607 current_buffer = obuf;
27610 set_cursor:
27612 #ifdef HAVE_WINDOW_SYSTEM
27613 if (FRAME_WINDOW_P (f))
27614 define_frame_cursor1 (f, cursor, pointer);
27615 #else
27616 /* This is here to prevent a compiler error, about "label at end of
27617 compound statement". */
27618 return;
27619 #endif
27623 /* EXPORT for RIF:
27624 Clear any mouse-face on window W. This function is part of the
27625 redisplay interface, and is called from try_window_id and similar
27626 functions to ensure the mouse-highlight is off. */
27628 void
27629 x_clear_window_mouse_face (struct window *w)
27631 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27632 Lisp_Object window;
27634 BLOCK_INPUT;
27635 XSETWINDOW (window, w);
27636 if (EQ (window, hlinfo->mouse_face_window))
27637 clear_mouse_face (hlinfo);
27638 UNBLOCK_INPUT;
27642 /* EXPORT:
27643 Just discard the mouse face information for frame F, if any.
27644 This is used when the size of F is changed. */
27646 void
27647 cancel_mouse_face (struct frame *f)
27649 Lisp_Object window;
27650 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27652 window = hlinfo->mouse_face_window;
27653 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
27655 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27656 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27657 hlinfo->mouse_face_window = Qnil;
27663 /***********************************************************************
27664 Exposure Events
27665 ***********************************************************************/
27667 #ifdef HAVE_WINDOW_SYSTEM
27669 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
27670 which intersects rectangle R. R is in window-relative coordinates. */
27672 static void
27673 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
27674 enum glyph_row_area area)
27676 struct glyph *first = row->glyphs[area];
27677 struct glyph *end = row->glyphs[area] + row->used[area];
27678 struct glyph *last;
27679 int first_x, start_x, x;
27681 if (area == TEXT_AREA && row->fill_line_p)
27682 /* If row extends face to end of line write the whole line. */
27683 draw_glyphs (w, 0, row, area,
27684 0, row->used[area],
27685 DRAW_NORMAL_TEXT, 0);
27686 else
27688 /* Set START_X to the window-relative start position for drawing glyphs of
27689 AREA. The first glyph of the text area can be partially visible.
27690 The first glyphs of other areas cannot. */
27691 start_x = window_box_left_offset (w, area);
27692 x = start_x;
27693 if (area == TEXT_AREA)
27694 x += row->x;
27696 /* Find the first glyph that must be redrawn. */
27697 while (first < end
27698 && x + first->pixel_width < r->x)
27700 x += first->pixel_width;
27701 ++first;
27704 /* Find the last one. */
27705 last = first;
27706 first_x = x;
27707 while (last < end
27708 && x < r->x + r->width)
27710 x += last->pixel_width;
27711 ++last;
27714 /* Repaint. */
27715 if (last > first)
27716 draw_glyphs (w, first_x - start_x, row, area,
27717 first - row->glyphs[area], last - row->glyphs[area],
27718 DRAW_NORMAL_TEXT, 0);
27723 /* Redraw the parts of the glyph row ROW on window W intersecting
27724 rectangle R. R is in window-relative coordinates. Value is
27725 non-zero if mouse-face was overwritten. */
27727 static int
27728 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
27730 xassert (row->enabled_p);
27732 if (row->mode_line_p || w->pseudo_window_p)
27733 draw_glyphs (w, 0, row, TEXT_AREA,
27734 0, row->used[TEXT_AREA],
27735 DRAW_NORMAL_TEXT, 0);
27736 else
27738 if (row->used[LEFT_MARGIN_AREA])
27739 expose_area (w, row, r, LEFT_MARGIN_AREA);
27740 if (row->used[TEXT_AREA])
27741 expose_area (w, row, r, TEXT_AREA);
27742 if (row->used[RIGHT_MARGIN_AREA])
27743 expose_area (w, row, r, RIGHT_MARGIN_AREA);
27744 draw_row_fringe_bitmaps (w, row);
27747 return row->mouse_face_p;
27751 /* Redraw those parts of glyphs rows during expose event handling that
27752 overlap other rows. Redrawing of an exposed line writes over parts
27753 of lines overlapping that exposed line; this function fixes that.
27755 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
27756 row in W's current matrix that is exposed and overlaps other rows.
27757 LAST_OVERLAPPING_ROW is the last such row. */
27759 static void
27760 expose_overlaps (struct window *w,
27761 struct glyph_row *first_overlapping_row,
27762 struct glyph_row *last_overlapping_row,
27763 XRectangle *r)
27765 struct glyph_row *row;
27767 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
27768 if (row->overlapping_p)
27770 xassert (row->enabled_p && !row->mode_line_p);
27772 row->clip = r;
27773 if (row->used[LEFT_MARGIN_AREA])
27774 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
27776 if (row->used[TEXT_AREA])
27777 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
27779 if (row->used[RIGHT_MARGIN_AREA])
27780 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
27781 row->clip = NULL;
27786 /* Return non-zero if W's cursor intersects rectangle R. */
27788 static int
27789 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
27791 XRectangle cr, result;
27792 struct glyph *cursor_glyph;
27793 struct glyph_row *row;
27795 if (w->phys_cursor.vpos >= 0
27796 && w->phys_cursor.vpos < w->current_matrix->nrows
27797 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
27798 row->enabled_p)
27799 && row->cursor_in_fringe_p)
27801 /* Cursor is in the fringe. */
27802 cr.x = window_box_right_offset (w,
27803 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
27804 ? RIGHT_MARGIN_AREA
27805 : TEXT_AREA));
27806 cr.y = row->y;
27807 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
27808 cr.height = row->height;
27809 return x_intersect_rectangles (&cr, r, &result);
27812 cursor_glyph = get_phys_cursor_glyph (w);
27813 if (cursor_glyph)
27815 /* r is relative to W's box, but w->phys_cursor.x is relative
27816 to left edge of W's TEXT area. Adjust it. */
27817 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
27818 cr.y = w->phys_cursor.y;
27819 cr.width = cursor_glyph->pixel_width;
27820 cr.height = w->phys_cursor_height;
27821 /* ++KFS: W32 version used W32-specific IntersectRect here, but
27822 I assume the effect is the same -- and this is portable. */
27823 return x_intersect_rectangles (&cr, r, &result);
27825 /* If we don't understand the format, pretend we're not in the hot-spot. */
27826 return 0;
27830 /* EXPORT:
27831 Draw a vertical window border to the right of window W if W doesn't
27832 have vertical scroll bars. */
27834 void
27835 x_draw_vertical_border (struct window *w)
27837 struct frame *f = XFRAME (WINDOW_FRAME (w));
27839 /* We could do better, if we knew what type of scroll-bar the adjacent
27840 windows (on either side) have... But we don't :-(
27841 However, I think this works ok. ++KFS 2003-04-25 */
27843 /* Redraw borders between horizontally adjacent windows. Don't
27844 do it for frames with vertical scroll bars because either the
27845 right scroll bar of a window, or the left scroll bar of its
27846 neighbor will suffice as a border. */
27847 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
27848 return;
27850 if (!WINDOW_RIGHTMOST_P (w)
27851 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
27853 int x0, x1, y0, y1;
27855 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27856 y1 -= 1;
27858 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27859 x1 -= 1;
27861 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
27863 else if (!WINDOW_LEFTMOST_P (w)
27864 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
27866 int x0, x1, y0, y1;
27868 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27869 y1 -= 1;
27871 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27872 x0 -= 1;
27874 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
27879 /* Redraw the part of window W intersection rectangle FR. Pixel
27880 coordinates in FR are frame-relative. Call this function with
27881 input blocked. Value is non-zero if the exposure overwrites
27882 mouse-face. */
27884 static int
27885 expose_window (struct window *w, XRectangle *fr)
27887 struct frame *f = XFRAME (w->frame);
27888 XRectangle wr, r;
27889 int mouse_face_overwritten_p = 0;
27891 /* If window is not yet fully initialized, do nothing. This can
27892 happen when toolkit scroll bars are used and a window is split.
27893 Reconfiguring the scroll bar will generate an expose for a newly
27894 created window. */
27895 if (w->current_matrix == NULL)
27896 return 0;
27898 /* When we're currently updating the window, display and current
27899 matrix usually don't agree. Arrange for a thorough display
27900 later. */
27901 if (w == updated_window)
27903 SET_FRAME_GARBAGED (f);
27904 return 0;
27907 /* Frame-relative pixel rectangle of W. */
27908 wr.x = WINDOW_LEFT_EDGE_X (w);
27909 wr.y = WINDOW_TOP_EDGE_Y (w);
27910 wr.width = WINDOW_TOTAL_WIDTH (w);
27911 wr.height = WINDOW_TOTAL_HEIGHT (w);
27913 if (x_intersect_rectangles (fr, &wr, &r))
27915 int yb = window_text_bottom_y (w);
27916 struct glyph_row *row;
27917 int cursor_cleared_p, phys_cursor_on_p;
27918 struct glyph_row *first_overlapping_row, *last_overlapping_row;
27920 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
27921 r.x, r.y, r.width, r.height));
27923 /* Convert to window coordinates. */
27924 r.x -= WINDOW_LEFT_EDGE_X (w);
27925 r.y -= WINDOW_TOP_EDGE_Y (w);
27927 /* Turn off the cursor. */
27928 if (!w->pseudo_window_p
27929 && phys_cursor_in_rect_p (w, &r))
27931 x_clear_cursor (w);
27932 cursor_cleared_p = 1;
27934 else
27935 cursor_cleared_p = 0;
27937 /* If the row containing the cursor extends face to end of line,
27938 then expose_area might overwrite the cursor outside the
27939 rectangle and thus notice_overwritten_cursor might clear
27940 w->phys_cursor_on_p. We remember the original value and
27941 check later if it is changed. */
27942 phys_cursor_on_p = w->phys_cursor_on_p;
27944 /* Update lines intersecting rectangle R. */
27945 first_overlapping_row = last_overlapping_row = NULL;
27946 for (row = w->current_matrix->rows;
27947 row->enabled_p;
27948 ++row)
27950 int y0 = row->y;
27951 int y1 = MATRIX_ROW_BOTTOM_Y (row);
27953 if ((y0 >= r.y && y0 < r.y + r.height)
27954 || (y1 > r.y && y1 < r.y + r.height)
27955 || (r.y >= y0 && r.y < y1)
27956 || (r.y + r.height > y0 && r.y + r.height < y1))
27958 /* A header line may be overlapping, but there is no need
27959 to fix overlapping areas for them. KFS 2005-02-12 */
27960 if (row->overlapping_p && !row->mode_line_p)
27962 if (first_overlapping_row == NULL)
27963 first_overlapping_row = row;
27964 last_overlapping_row = row;
27967 row->clip = fr;
27968 if (expose_line (w, row, &r))
27969 mouse_face_overwritten_p = 1;
27970 row->clip = NULL;
27972 else if (row->overlapping_p)
27974 /* We must redraw a row overlapping the exposed area. */
27975 if (y0 < r.y
27976 ? y0 + row->phys_height > r.y
27977 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
27979 if (first_overlapping_row == NULL)
27980 first_overlapping_row = row;
27981 last_overlapping_row = row;
27985 if (y1 >= yb)
27986 break;
27989 /* Display the mode line if there is one. */
27990 if (WINDOW_WANTS_MODELINE_P (w)
27991 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
27992 row->enabled_p)
27993 && row->y < r.y + r.height)
27995 if (expose_line (w, row, &r))
27996 mouse_face_overwritten_p = 1;
27999 if (!w->pseudo_window_p)
28001 /* Fix the display of overlapping rows. */
28002 if (first_overlapping_row)
28003 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28004 fr);
28006 /* Draw border between windows. */
28007 x_draw_vertical_border (w);
28009 /* Turn the cursor on again. */
28010 if (cursor_cleared_p
28011 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28012 update_window_cursor (w, 1);
28016 return mouse_face_overwritten_p;
28021 /* Redraw (parts) of all windows in the window tree rooted at W that
28022 intersect R. R contains frame pixel coordinates. Value is
28023 non-zero if the exposure overwrites mouse-face. */
28025 static int
28026 expose_window_tree (struct window *w, XRectangle *r)
28028 struct frame *f = XFRAME (w->frame);
28029 int mouse_face_overwritten_p = 0;
28031 while (w && !FRAME_GARBAGED_P (f))
28033 if (!NILP (w->hchild))
28034 mouse_face_overwritten_p
28035 |= expose_window_tree (XWINDOW (w->hchild), r);
28036 else if (!NILP (w->vchild))
28037 mouse_face_overwritten_p
28038 |= expose_window_tree (XWINDOW (w->vchild), r);
28039 else
28040 mouse_face_overwritten_p |= expose_window (w, r);
28042 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28045 return mouse_face_overwritten_p;
28049 /* EXPORT:
28050 Redisplay an exposed area of frame F. X and Y are the upper-left
28051 corner of the exposed rectangle. W and H are width and height of
28052 the exposed area. All are pixel values. W or H zero means redraw
28053 the entire frame. */
28055 void
28056 expose_frame (struct frame *f, int x, int y, int w, int h)
28058 XRectangle r;
28059 int mouse_face_overwritten_p = 0;
28061 TRACE ((stderr, "expose_frame "));
28063 /* No need to redraw if frame will be redrawn soon. */
28064 if (FRAME_GARBAGED_P (f))
28066 TRACE ((stderr, " garbaged\n"));
28067 return;
28070 /* If basic faces haven't been realized yet, there is no point in
28071 trying to redraw anything. This can happen when we get an expose
28072 event while Emacs is starting, e.g. by moving another window. */
28073 if (FRAME_FACE_CACHE (f) == NULL
28074 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28076 TRACE ((stderr, " no faces\n"));
28077 return;
28080 if (w == 0 || h == 0)
28082 r.x = r.y = 0;
28083 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28084 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28086 else
28088 r.x = x;
28089 r.y = y;
28090 r.width = w;
28091 r.height = h;
28094 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28095 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28097 if (WINDOWP (f->tool_bar_window))
28098 mouse_face_overwritten_p
28099 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28101 #ifdef HAVE_X_WINDOWS
28102 #ifndef MSDOS
28103 #ifndef USE_X_TOOLKIT
28104 if (WINDOWP (f->menu_bar_window))
28105 mouse_face_overwritten_p
28106 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28107 #endif /* not USE_X_TOOLKIT */
28108 #endif
28109 #endif
28111 /* Some window managers support a focus-follows-mouse style with
28112 delayed raising of frames. Imagine a partially obscured frame,
28113 and moving the mouse into partially obscured mouse-face on that
28114 frame. The visible part of the mouse-face will be highlighted,
28115 then the WM raises the obscured frame. With at least one WM, KDE
28116 2.1, Emacs is not getting any event for the raising of the frame
28117 (even tried with SubstructureRedirectMask), only Expose events.
28118 These expose events will draw text normally, i.e. not
28119 highlighted. Which means we must redo the highlight here.
28120 Subsume it under ``we love X''. --gerd 2001-08-15 */
28121 /* Included in Windows version because Windows most likely does not
28122 do the right thing if any third party tool offers
28123 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28124 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28126 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28127 if (f == hlinfo->mouse_face_mouse_frame)
28129 int mouse_x = hlinfo->mouse_face_mouse_x;
28130 int mouse_y = hlinfo->mouse_face_mouse_y;
28131 clear_mouse_face (hlinfo);
28132 note_mouse_highlight (f, mouse_x, mouse_y);
28138 /* EXPORT:
28139 Determine the intersection of two rectangles R1 and R2. Return
28140 the intersection in *RESULT. Value is non-zero if RESULT is not
28141 empty. */
28144 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28146 XRectangle *left, *right;
28147 XRectangle *upper, *lower;
28148 int intersection_p = 0;
28150 /* Rearrange so that R1 is the left-most rectangle. */
28151 if (r1->x < r2->x)
28152 left = r1, right = r2;
28153 else
28154 left = r2, right = r1;
28156 /* X0 of the intersection is right.x0, if this is inside R1,
28157 otherwise there is no intersection. */
28158 if (right->x <= left->x + left->width)
28160 result->x = right->x;
28162 /* The right end of the intersection is the minimum of
28163 the right ends of left and right. */
28164 result->width = (min (left->x + left->width, right->x + right->width)
28165 - result->x);
28167 /* Same game for Y. */
28168 if (r1->y < r2->y)
28169 upper = r1, lower = r2;
28170 else
28171 upper = r2, lower = r1;
28173 /* The upper end of the intersection is lower.y0, if this is inside
28174 of upper. Otherwise, there is no intersection. */
28175 if (lower->y <= upper->y + upper->height)
28177 result->y = lower->y;
28179 /* The lower end of the intersection is the minimum of the lower
28180 ends of upper and lower. */
28181 result->height = (min (lower->y + lower->height,
28182 upper->y + upper->height)
28183 - result->y);
28184 intersection_p = 1;
28188 return intersection_p;
28191 #endif /* HAVE_WINDOW_SYSTEM */
28194 /***********************************************************************
28195 Initialization
28196 ***********************************************************************/
28198 void
28199 syms_of_xdisp (void)
28201 Vwith_echo_area_save_vector = Qnil;
28202 staticpro (&Vwith_echo_area_save_vector);
28204 Vmessage_stack = Qnil;
28205 staticpro (&Vmessage_stack);
28207 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28209 message_dolog_marker1 = Fmake_marker ();
28210 staticpro (&message_dolog_marker1);
28211 message_dolog_marker2 = Fmake_marker ();
28212 staticpro (&message_dolog_marker2);
28213 message_dolog_marker3 = Fmake_marker ();
28214 staticpro (&message_dolog_marker3);
28216 #if GLYPH_DEBUG
28217 defsubr (&Sdump_frame_glyph_matrix);
28218 defsubr (&Sdump_glyph_matrix);
28219 defsubr (&Sdump_glyph_row);
28220 defsubr (&Sdump_tool_bar_row);
28221 defsubr (&Strace_redisplay);
28222 defsubr (&Strace_to_stderr);
28223 #endif
28224 #ifdef HAVE_WINDOW_SYSTEM
28225 defsubr (&Stool_bar_lines_needed);
28226 defsubr (&Slookup_image_map);
28227 #endif
28228 defsubr (&Sformat_mode_line);
28229 defsubr (&Sinvisible_p);
28230 defsubr (&Scurrent_bidi_paragraph_direction);
28232 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28233 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28234 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28235 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28236 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28237 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28238 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28239 DEFSYM (Qeval, "eval");
28240 DEFSYM (QCdata, ":data");
28241 DEFSYM (Qdisplay, "display");
28242 DEFSYM (Qspace_width, "space-width");
28243 DEFSYM (Qraise, "raise");
28244 DEFSYM (Qslice, "slice");
28245 DEFSYM (Qspace, "space");
28246 DEFSYM (Qmargin, "margin");
28247 DEFSYM (Qpointer, "pointer");
28248 DEFSYM (Qleft_margin, "left-margin");
28249 DEFSYM (Qright_margin, "right-margin");
28250 DEFSYM (Qcenter, "center");
28251 DEFSYM (Qline_height, "line-height");
28252 DEFSYM (QCalign_to, ":align-to");
28253 DEFSYM (QCrelative_width, ":relative-width");
28254 DEFSYM (QCrelative_height, ":relative-height");
28255 DEFSYM (QCeval, ":eval");
28256 DEFSYM (QCpropertize, ":propertize");
28257 DEFSYM (QCfile, ":file");
28258 DEFSYM (Qfontified, "fontified");
28259 DEFSYM (Qfontification_functions, "fontification-functions");
28260 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28261 DEFSYM (Qescape_glyph, "escape-glyph");
28262 DEFSYM (Qnobreak_space, "nobreak-space");
28263 DEFSYM (Qimage, "image");
28264 DEFSYM (Qtext, "text");
28265 DEFSYM (Qboth, "both");
28266 DEFSYM (Qboth_horiz, "both-horiz");
28267 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28268 DEFSYM (QCmap, ":map");
28269 DEFSYM (QCpointer, ":pointer");
28270 DEFSYM (Qrect, "rect");
28271 DEFSYM (Qcircle, "circle");
28272 DEFSYM (Qpoly, "poly");
28273 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28274 DEFSYM (Qgrow_only, "grow-only");
28275 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28276 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28277 DEFSYM (Qposition, "position");
28278 DEFSYM (Qbuffer_position, "buffer-position");
28279 DEFSYM (Qobject, "object");
28280 DEFSYM (Qbar, "bar");
28281 DEFSYM (Qhbar, "hbar");
28282 DEFSYM (Qbox, "box");
28283 DEFSYM (Qhollow, "hollow");
28284 DEFSYM (Qhand, "hand");
28285 DEFSYM (Qarrow, "arrow");
28286 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28288 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28289 Fcons (intern_c_string ("void-variable"), Qnil)),
28290 Qnil);
28291 staticpro (&list_of_error);
28293 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28294 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28295 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28296 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28298 echo_buffer[0] = echo_buffer[1] = Qnil;
28299 staticpro (&echo_buffer[0]);
28300 staticpro (&echo_buffer[1]);
28302 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28303 staticpro (&echo_area_buffer[0]);
28304 staticpro (&echo_area_buffer[1]);
28306 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
28307 staticpro (&Vmessages_buffer_name);
28309 mode_line_proptrans_alist = Qnil;
28310 staticpro (&mode_line_proptrans_alist);
28311 mode_line_string_list = Qnil;
28312 staticpro (&mode_line_string_list);
28313 mode_line_string_face = Qnil;
28314 staticpro (&mode_line_string_face);
28315 mode_line_string_face_prop = Qnil;
28316 staticpro (&mode_line_string_face_prop);
28317 Vmode_line_unwind_vector = Qnil;
28318 staticpro (&Vmode_line_unwind_vector);
28320 help_echo_string = Qnil;
28321 staticpro (&help_echo_string);
28322 help_echo_object = Qnil;
28323 staticpro (&help_echo_object);
28324 help_echo_window = Qnil;
28325 staticpro (&help_echo_window);
28326 previous_help_echo_string = Qnil;
28327 staticpro (&previous_help_echo_string);
28328 help_echo_pos = -1;
28330 DEFSYM (Qright_to_left, "right-to-left");
28331 DEFSYM (Qleft_to_right, "left-to-right");
28333 #ifdef HAVE_WINDOW_SYSTEM
28334 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28335 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28336 For example, if a block cursor is over a tab, it will be drawn as
28337 wide as that tab on the display. */);
28338 x_stretch_cursor_p = 0;
28339 #endif
28341 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28342 doc: /* Non-nil means highlight trailing whitespace.
28343 The face used for trailing whitespace is `trailing-whitespace'. */);
28344 Vshow_trailing_whitespace = Qnil;
28346 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28347 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28348 If the value is t, Emacs highlights non-ASCII chars which have the
28349 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28350 or `escape-glyph' face respectively.
28352 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28353 U+2011 (non-breaking hyphen) are affected.
28355 Any other non-nil value means to display these characters as a escape
28356 glyph followed by an ordinary space or hyphen.
28358 A value of nil means no special handling of these characters. */);
28359 Vnobreak_char_display = Qt;
28361 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28362 doc: /* The pointer shape to show in void text areas.
28363 A value of nil means to show the text pointer. Other options are `arrow',
28364 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28365 Vvoid_text_area_pointer = Qarrow;
28367 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28368 doc: /* Non-nil means don't actually do any redisplay.
28369 This is used for internal purposes. */);
28370 Vinhibit_redisplay = Qnil;
28372 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28373 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28374 Vglobal_mode_string = Qnil;
28376 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28377 doc: /* Marker for where to display an arrow on top of the buffer text.
28378 This must be the beginning of a line in order to work.
28379 See also `overlay-arrow-string'. */);
28380 Voverlay_arrow_position = Qnil;
28382 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28383 doc: /* String to display as an arrow in non-window frames.
28384 See also `overlay-arrow-position'. */);
28385 Voverlay_arrow_string = make_pure_c_string ("=>");
28387 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28388 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28389 The symbols on this list are examined during redisplay to determine
28390 where to display overlay arrows. */);
28391 Voverlay_arrow_variable_list
28392 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28394 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28395 doc: /* The number of lines to try scrolling a window by when point moves out.
28396 If that fails to bring point back on frame, point is centered instead.
28397 If this is zero, point is always centered after it moves off frame.
28398 If you want scrolling to always be a line at a time, you should set
28399 `scroll-conservatively' to a large value rather than set this to 1. */);
28401 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28402 doc: /* Scroll up to this many lines, to bring point back on screen.
28403 If point moves off-screen, redisplay will scroll by up to
28404 `scroll-conservatively' lines in order to bring point just barely
28405 onto the screen again. If that cannot be done, then redisplay
28406 recenters point as usual.
28408 If the value is greater than 100, redisplay will never recenter point,
28409 but will always scroll just enough text to bring point into view, even
28410 if you move far away.
28412 A value of zero means always recenter point if it moves off screen. */);
28413 scroll_conservatively = 0;
28415 DEFVAR_INT ("scroll-margin", scroll_margin,
28416 doc: /* Number of lines of margin at the top and bottom of a window.
28417 Recenter the window whenever point gets within this many lines
28418 of the top or bottom of the window. */);
28419 scroll_margin = 0;
28421 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28422 doc: /* Pixels per inch value for non-window system displays.
28423 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28424 Vdisplay_pixels_per_inch = make_float (72.0);
28426 #if GLYPH_DEBUG
28427 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28428 #endif
28430 DEFVAR_LISP ("truncate-partial-width-windows",
28431 Vtruncate_partial_width_windows,
28432 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28433 For an integer value, truncate lines in each window narrower than the
28434 full frame width, provided the window width is less than that integer;
28435 otherwise, respect the value of `truncate-lines'.
28437 For any other non-nil value, truncate lines in all windows that do
28438 not span the full frame width.
28440 A value of nil means to respect the value of `truncate-lines'.
28442 If `word-wrap' is enabled, you might want to reduce this. */);
28443 Vtruncate_partial_width_windows = make_number (50);
28445 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28446 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28447 Any other value means to use the appropriate face, `mode-line',
28448 `header-line', or `menu' respectively. */);
28449 mode_line_inverse_video = 1;
28451 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28452 doc: /* Maximum buffer size for which line number should be displayed.
28453 If the buffer is bigger than this, the line number does not appear
28454 in the mode line. A value of nil means no limit. */);
28455 Vline_number_display_limit = Qnil;
28457 DEFVAR_INT ("line-number-display-limit-width",
28458 line_number_display_limit_width,
28459 doc: /* Maximum line width (in characters) for line number display.
28460 If the average length of the lines near point is bigger than this, then the
28461 line number may be omitted from the mode line. */);
28462 line_number_display_limit_width = 200;
28464 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28465 doc: /* Non-nil means highlight region even in nonselected windows. */);
28466 highlight_nonselected_windows = 0;
28468 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28469 doc: /* Non-nil if more than one frame is visible on this display.
28470 Minibuffer-only frames don't count, but iconified frames do.
28471 This variable is not guaranteed to be accurate except while processing
28472 `frame-title-format' and `icon-title-format'. */);
28474 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28475 doc: /* Template for displaying the title bar of visible frames.
28476 \(Assuming the window manager supports this feature.)
28478 This variable has the same structure as `mode-line-format', except that
28479 the %c and %l constructs are ignored. It is used only on frames for
28480 which no explicit name has been set \(see `modify-frame-parameters'). */);
28482 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28483 doc: /* Template for displaying the title bar of an iconified frame.
28484 \(Assuming the window manager supports this feature.)
28485 This variable has the same structure as `mode-line-format' (which see),
28486 and is used only on frames for which no explicit name has been set
28487 \(see `modify-frame-parameters'). */);
28488 Vicon_title_format
28489 = Vframe_title_format
28490 = pure_cons (intern_c_string ("multiple-frames"),
28491 pure_cons (make_pure_c_string ("%b"),
28492 pure_cons (pure_cons (empty_unibyte_string,
28493 pure_cons (intern_c_string ("invocation-name"),
28494 pure_cons (make_pure_c_string ("@"),
28495 pure_cons (intern_c_string ("system-name"),
28496 Qnil)))),
28497 Qnil)));
28499 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28500 doc: /* Maximum number of lines to keep in the message log buffer.
28501 If nil, disable message logging. If t, log messages but don't truncate
28502 the buffer when it becomes large. */);
28503 Vmessage_log_max = make_number (100);
28505 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28506 doc: /* Functions called before redisplay, if window sizes have changed.
28507 The value should be a list of functions that take one argument.
28508 Just before redisplay, for each frame, if any of its windows have changed
28509 size since the last redisplay, or have been split or deleted,
28510 all the functions in the list are called, with the frame as argument. */);
28511 Vwindow_size_change_functions = Qnil;
28513 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28514 doc: /* List of functions to call before redisplaying a window with scrolling.
28515 Each function is called with two arguments, the window and its new
28516 display-start position. Note that these functions are also called by
28517 `set-window-buffer'. Also note that the value of `window-end' is not
28518 valid when these functions are called.
28520 Warning: Do not use this feature to alter the way the window
28521 is scrolled. It is not designed for that, and such use probably won't
28522 work. */);
28523 Vwindow_scroll_functions = Qnil;
28525 DEFVAR_LISP ("window-text-change-functions",
28526 Vwindow_text_change_functions,
28527 doc: /* Functions to call in redisplay when text in the window might change. */);
28528 Vwindow_text_change_functions = Qnil;
28530 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28531 doc: /* Functions called when redisplay of a window reaches the end trigger.
28532 Each function is called with two arguments, the window and the end trigger value.
28533 See `set-window-redisplay-end-trigger'. */);
28534 Vredisplay_end_trigger_functions = Qnil;
28536 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28537 doc: /* Non-nil means autoselect window with mouse pointer.
28538 If nil, do not autoselect windows.
28539 A positive number means delay autoselection by that many seconds: a
28540 window is autoselected only after the mouse has remained in that
28541 window for the duration of the delay.
28542 A negative number has a similar effect, but causes windows to be
28543 autoselected only after the mouse has stopped moving. \(Because of
28544 the way Emacs compares mouse events, you will occasionally wait twice
28545 that time before the window gets selected.\)
28546 Any other value means to autoselect window instantaneously when the
28547 mouse pointer enters it.
28549 Autoselection selects the minibuffer only if it is active, and never
28550 unselects the minibuffer if it is active.
28552 When customizing this variable make sure that the actual value of
28553 `focus-follows-mouse' matches the behavior of your window manager. */);
28554 Vmouse_autoselect_window = Qnil;
28556 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28557 doc: /* Non-nil means automatically resize tool-bars.
28558 This dynamically changes the tool-bar's height to the minimum height
28559 that is needed to make all tool-bar items visible.
28560 If value is `grow-only', the tool-bar's height is only increased
28561 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28562 Vauto_resize_tool_bars = Qt;
28564 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28565 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28566 auto_raise_tool_bar_buttons_p = 1;
28568 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28569 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28570 make_cursor_line_fully_visible_p = 1;
28572 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
28573 doc: /* Border below tool-bar in pixels.
28574 If an integer, use it as the height of the border.
28575 If it is one of `internal-border-width' or `border-width', use the
28576 value of the corresponding frame parameter.
28577 Otherwise, no border is added below the tool-bar. */);
28578 Vtool_bar_border = Qinternal_border_width;
28580 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
28581 doc: /* Margin around tool-bar buttons in pixels.
28582 If an integer, use that for both horizontal and vertical margins.
28583 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
28584 HORZ specifying the horizontal margin, and VERT specifying the
28585 vertical margin. */);
28586 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
28588 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
28589 doc: /* Relief thickness of tool-bar buttons. */);
28590 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
28592 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
28593 doc: /* Tool bar style to use.
28594 It can be one of
28595 image - show images only
28596 text - show text only
28597 both - show both, text below image
28598 both-horiz - show text to the right of the image
28599 text-image-horiz - show text to the left of the image
28600 any other - use system default or image if no system default. */);
28601 Vtool_bar_style = Qnil;
28603 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
28604 doc: /* Maximum number of characters a label can have to be shown.
28605 The tool bar style must also show labels for this to have any effect, see
28606 `tool-bar-style'. */);
28607 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
28609 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
28610 doc: /* List of functions to call to fontify regions of text.
28611 Each function is called with one argument POS. Functions must
28612 fontify a region starting at POS in the current buffer, and give
28613 fontified regions the property `fontified'. */);
28614 Vfontification_functions = Qnil;
28615 Fmake_variable_buffer_local (Qfontification_functions);
28617 DEFVAR_BOOL ("unibyte-display-via-language-environment",
28618 unibyte_display_via_language_environment,
28619 doc: /* Non-nil means display unibyte text according to language environment.
28620 Specifically, this means that raw bytes in the range 160-255 decimal
28621 are displayed by converting them to the equivalent multibyte characters
28622 according to the current language environment. As a result, they are
28623 displayed according to the current fontset.
28625 Note that this variable affects only how these bytes are displayed,
28626 but does not change the fact they are interpreted as raw bytes. */);
28627 unibyte_display_via_language_environment = 0;
28629 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
28630 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
28631 If a float, it specifies a fraction of the mini-window frame's height.
28632 If an integer, it specifies a number of lines. */);
28633 Vmax_mini_window_height = make_float (0.25);
28635 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
28636 doc: /* How to resize mini-windows (the minibuffer and the echo area).
28637 A value of nil means don't automatically resize mini-windows.
28638 A value of t means resize them to fit the text displayed in them.
28639 A value of `grow-only', the default, means let mini-windows grow only;
28640 they return to their normal size when the minibuffer is closed, or the
28641 echo area becomes empty. */);
28642 Vresize_mini_windows = Qgrow_only;
28644 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
28645 doc: /* Alist specifying how to blink the cursor off.
28646 Each element has the form (ON-STATE . OFF-STATE). Whenever the
28647 `cursor-type' frame-parameter or variable equals ON-STATE,
28648 comparing using `equal', Emacs uses OFF-STATE to specify
28649 how to blink it off. ON-STATE and OFF-STATE are values for
28650 the `cursor-type' frame parameter.
28652 If a frame's ON-STATE has no entry in this list,
28653 the frame's other specifications determine how to blink the cursor off. */);
28654 Vblink_cursor_alist = Qnil;
28656 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
28657 doc: /* Allow or disallow automatic horizontal scrolling of windows.
28658 If non-nil, windows are automatically scrolled horizontally to make
28659 point visible. */);
28660 automatic_hscrolling_p = 1;
28661 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
28663 DEFVAR_INT ("hscroll-margin", hscroll_margin,
28664 doc: /* How many columns away from the window edge point is allowed to get
28665 before automatic hscrolling will horizontally scroll the window. */);
28666 hscroll_margin = 5;
28668 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
28669 doc: /* How many columns to scroll the window when point gets too close to the edge.
28670 When point is less than `hscroll-margin' columns from the window
28671 edge, automatic hscrolling will scroll the window by the amount of columns
28672 determined by this variable. If its value is a positive integer, scroll that
28673 many columns. If it's a positive floating-point number, it specifies the
28674 fraction of the window's width to scroll. If it's nil or zero, point will be
28675 centered horizontally after the scroll. Any other value, including negative
28676 numbers, are treated as if the value were zero.
28678 Automatic hscrolling always moves point outside the scroll margin, so if
28679 point was more than scroll step columns inside the margin, the window will
28680 scroll more than the value given by the scroll step.
28682 Note that the lower bound for automatic hscrolling specified by `scroll-left'
28683 and `scroll-right' overrides this variable's effect. */);
28684 Vhscroll_step = make_number (0);
28686 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
28687 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
28688 Bind this around calls to `message' to let it take effect. */);
28689 message_truncate_lines = 0;
28691 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
28692 doc: /* Normal hook run to update the menu bar definitions.
28693 Redisplay runs this hook before it redisplays the menu bar.
28694 This is used to update submenus such as Buffers,
28695 whose contents depend on various data. */);
28696 Vmenu_bar_update_hook = Qnil;
28698 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
28699 doc: /* Frame for which we are updating a menu.
28700 The enable predicate for a menu binding should check this variable. */);
28701 Vmenu_updating_frame = Qnil;
28703 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
28704 doc: /* Non-nil means don't update menu bars. Internal use only. */);
28705 inhibit_menubar_update = 0;
28707 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
28708 doc: /* Prefix prepended to all continuation lines at display time.
28709 The value may be a string, an image, or a stretch-glyph; it is
28710 interpreted in the same way as the value of a `display' text property.
28712 This variable is overridden by any `wrap-prefix' text or overlay
28713 property.
28715 To add a prefix to non-continuation lines, use `line-prefix'. */);
28716 Vwrap_prefix = Qnil;
28717 DEFSYM (Qwrap_prefix, "wrap-prefix");
28718 Fmake_variable_buffer_local (Qwrap_prefix);
28720 DEFVAR_LISP ("line-prefix", Vline_prefix,
28721 doc: /* Prefix prepended to all non-continuation lines at display time.
28722 The value may be a string, an image, or a stretch-glyph; it is
28723 interpreted in the same way as the value of a `display' text property.
28725 This variable is overridden by any `line-prefix' text or overlay
28726 property.
28728 To add a prefix to continuation lines, use `wrap-prefix'. */);
28729 Vline_prefix = Qnil;
28730 DEFSYM (Qline_prefix, "line-prefix");
28731 Fmake_variable_buffer_local (Qline_prefix);
28733 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
28734 doc: /* Non-nil means don't eval Lisp during redisplay. */);
28735 inhibit_eval_during_redisplay = 0;
28737 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
28738 doc: /* Non-nil means don't free realized faces. Internal use only. */);
28739 inhibit_free_realized_faces = 0;
28741 #if GLYPH_DEBUG
28742 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
28743 doc: /* Inhibit try_window_id display optimization. */);
28744 inhibit_try_window_id = 0;
28746 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
28747 doc: /* Inhibit try_window_reusing display optimization. */);
28748 inhibit_try_window_reusing = 0;
28750 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
28751 doc: /* Inhibit try_cursor_movement display optimization. */);
28752 inhibit_try_cursor_movement = 0;
28753 #endif /* GLYPH_DEBUG */
28755 DEFVAR_INT ("overline-margin", overline_margin,
28756 doc: /* Space between overline and text, in pixels.
28757 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
28758 margin to the character height. */);
28759 overline_margin = 2;
28761 DEFVAR_INT ("underline-minimum-offset",
28762 underline_minimum_offset,
28763 doc: /* Minimum distance between baseline and underline.
28764 This can improve legibility of underlined text at small font sizes,
28765 particularly when using variable `x-use-underline-position-properties'
28766 with fonts that specify an UNDERLINE_POSITION relatively close to the
28767 baseline. The default value is 1. */);
28768 underline_minimum_offset = 1;
28770 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
28771 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
28772 This feature only works when on a window system that can change
28773 cursor shapes. */);
28774 display_hourglass_p = 1;
28776 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
28777 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
28778 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
28780 hourglass_atimer = NULL;
28781 hourglass_shown_p = 0;
28783 DEFSYM (Qglyphless_char, "glyphless-char");
28784 DEFSYM (Qhex_code, "hex-code");
28785 DEFSYM (Qempty_box, "empty-box");
28786 DEFSYM (Qthin_space, "thin-space");
28787 DEFSYM (Qzero_width, "zero-width");
28789 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
28790 /* Intern this now in case it isn't already done.
28791 Setting this variable twice is harmless.
28792 But don't staticpro it here--that is done in alloc.c. */
28793 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
28794 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
28796 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
28797 doc: /* Char-table defining glyphless characters.
28798 Each element, if non-nil, should be one of the following:
28799 an ASCII acronym string: display this string in a box
28800 `hex-code': display the hexadecimal code of a character in a box
28801 `empty-box': display as an empty box
28802 `thin-space': display as 1-pixel width space
28803 `zero-width': don't display
28804 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
28805 display method for graphical terminals and text terminals respectively.
28806 GRAPHICAL and TEXT should each have one of the values listed above.
28808 The char-table has one extra slot to control the display of a character for
28809 which no font is found. This slot only takes effect on graphical terminals.
28810 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
28811 `thin-space'. The default is `empty-box'. */);
28812 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
28813 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
28814 Qempty_box);
28818 /* Initialize this module when Emacs starts. */
28820 void
28821 init_xdisp (void)
28823 current_header_line_height = current_mode_line_height = -1;
28825 CHARPOS (this_line_start_pos) = 0;
28827 if (!noninteractive)
28829 struct window *m = XWINDOW (minibuf_window);
28830 Lisp_Object frame = m->frame;
28831 struct frame *f = XFRAME (frame);
28832 Lisp_Object root = FRAME_ROOT_WINDOW (f);
28833 struct window *r = XWINDOW (root);
28834 int i;
28836 echo_area_window = minibuf_window;
28838 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
28839 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
28840 XSETFASTINT (r->total_cols, FRAME_COLS (f));
28841 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
28842 XSETFASTINT (m->total_lines, 1);
28843 XSETFASTINT (m->total_cols, FRAME_COLS (f));
28845 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
28846 scratch_glyph_row.glyphs[TEXT_AREA + 1]
28847 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
28849 /* The default ellipsis glyphs `...'. */
28850 for (i = 0; i < 3; ++i)
28851 default_invis_vector[i] = make_number ('.');
28855 /* Allocate the buffer for frame titles.
28856 Also used for `format-mode-line'. */
28857 int size = 100;
28858 mode_line_noprop_buf = (char *) xmalloc (size);
28859 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
28860 mode_line_noprop_ptr = mode_line_noprop_buf;
28861 mode_line_target = MODE_LINE_DISPLAY;
28864 help_echo_showing_p = 0;
28867 /* Since w32 does not support atimers, it defines its own implementation of
28868 the following three functions in w32fns.c. */
28869 #ifndef WINDOWSNT
28871 /* Platform-independent portion of hourglass implementation. */
28873 /* Return non-zero if hourglass timer has been started or hourglass is
28874 shown. */
28876 hourglass_started (void)
28878 return hourglass_shown_p || hourglass_atimer != NULL;
28881 /* Cancel a currently active hourglass timer, and start a new one. */
28882 void
28883 start_hourglass (void)
28885 #if defined (HAVE_WINDOW_SYSTEM)
28886 EMACS_TIME delay;
28887 int secs, usecs = 0;
28889 cancel_hourglass ();
28891 if (INTEGERP (Vhourglass_delay)
28892 && XINT (Vhourglass_delay) > 0)
28893 secs = XFASTINT (Vhourglass_delay);
28894 else if (FLOATP (Vhourglass_delay)
28895 && XFLOAT_DATA (Vhourglass_delay) > 0)
28897 Lisp_Object tem;
28898 tem = Ftruncate (Vhourglass_delay, Qnil);
28899 secs = XFASTINT (tem);
28900 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
28902 else
28903 secs = DEFAULT_HOURGLASS_DELAY;
28905 EMACS_SET_SECS_USECS (delay, secs, usecs);
28906 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
28907 show_hourglass, NULL);
28908 #endif
28912 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
28913 shown. */
28914 void
28915 cancel_hourglass (void)
28917 #if defined (HAVE_WINDOW_SYSTEM)
28918 if (hourglass_atimer)
28920 cancel_atimer (hourglass_atimer);
28921 hourglass_atimer = NULL;
28924 if (hourglass_shown_p)
28925 hide_hourglass ();
28926 #endif
28928 #endif /* ! WINDOWSNT */