Rename configure.in to configure.ac.
[emacs.git] / src / xdisp.c
blob9018dce37fa8e7702e50df07e196140ad5948519
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 "character.h"
285 #include "buffer.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 #ifdef GLYPH_DEBUG
632 /* Non-zero means print traces of redisplay if compiled with
633 GLYPH_DEBUG defined. */
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;
754 static Lisp_Object Qmode_line_default_help_echo;
756 /* If a string, XTread_socket generates an event to display that string.
757 (The display is done in read_char.) */
759 Lisp_Object help_echo_string;
760 Lisp_Object help_echo_window;
761 Lisp_Object help_echo_object;
762 ptrdiff_t help_echo_pos;
764 /* Temporary variable for XTread_socket. */
766 Lisp_Object previous_help_echo_string;
768 /* Platform-independent portion of hourglass implementation. */
770 /* Non-zero means an hourglass cursor is currently shown. */
771 int hourglass_shown_p;
773 /* If non-null, an asynchronous timer that, when it expires, displays
774 an hourglass cursor on all frames. */
775 struct atimer *hourglass_atimer;
777 /* Name of the face used to display glyphless characters. */
778 Lisp_Object Qglyphless_char;
780 /* Symbol for the purpose of Vglyphless_char_display. */
781 static Lisp_Object Qglyphless_char_display;
783 /* Method symbols for Vglyphless_char_display. */
784 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
786 /* Default pixel width of `thin-space' display method. */
787 #define THIN_SPACE_WIDTH 1
789 /* Default number of seconds to wait before displaying an hourglass
790 cursor. */
791 #define DEFAULT_HOURGLASS_DELAY 1
794 /* Function prototypes. */
796 static void setup_for_ellipsis (struct it *, int);
797 static void set_iterator_to_next (struct it *, int);
798 static void mark_window_display_accurate_1 (struct window *, int);
799 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
800 static int display_prop_string_p (Lisp_Object, Lisp_Object);
801 static int cursor_row_p (struct glyph_row *);
802 static int redisplay_mode_lines (Lisp_Object, int);
803 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
805 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
807 static void handle_line_prefix (struct it *);
809 static void pint2str (char *, int, ptrdiff_t);
810 static void pint2hrstr (char *, int, ptrdiff_t);
811 static struct text_pos run_window_scroll_functions (Lisp_Object,
812 struct text_pos);
813 static void reconsider_clip_changes (struct window *, struct buffer *);
814 static int text_outside_line_unchanged_p (struct window *,
815 ptrdiff_t, ptrdiff_t);
816 static void store_mode_line_noprop_char (char);
817 static int store_mode_line_noprop (const char *, int, int);
818 static void handle_stop (struct it *);
819 static void handle_stop_backwards (struct it *, ptrdiff_t);
820 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
821 static void ensure_echo_area_buffers (void);
822 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
823 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
824 static int with_echo_area_buffer (struct window *, int,
825 int (*) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
826 ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
827 static void clear_garbaged_frames (void);
828 static int current_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
829 static void pop_message (void);
830 static int truncate_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
831 static void set_message (const char *, Lisp_Object, ptrdiff_t, int);
832 static int set_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
833 static int display_echo_area (struct window *);
834 static int display_echo_area_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
835 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
836 static Lisp_Object unwind_redisplay (Lisp_Object);
837 static int string_char_and_length (const unsigned char *, int *);
838 static struct text_pos display_prop_end (struct it *, Lisp_Object,
839 struct text_pos);
840 static int compute_window_start_on_continuation_line (struct window *);
841 static Lisp_Object safe_eval_handler (Lisp_Object);
842 static void insert_left_trunc_glyphs (struct it *);
843 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
844 Lisp_Object);
845 static void extend_face_to_end_of_line (struct it *);
846 static int append_space_for_newline (struct it *, int);
847 static int cursor_row_fully_visible_p (struct window *, int, int);
848 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
849 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
850 static int trailing_whitespace_p (ptrdiff_t);
851 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
852 static void push_it (struct it *, struct text_pos *);
853 static void iterate_out_of_display_property (struct it *);
854 static void pop_it (struct it *);
855 static void sync_frame_with_window_matrix_rows (struct window *);
856 static void select_frame_for_redisplay (Lisp_Object);
857 static void redisplay_internal (void);
858 static int echo_area_display (int);
859 static void redisplay_windows (Lisp_Object);
860 static void redisplay_window (Lisp_Object, int);
861 static Lisp_Object redisplay_window_error (Lisp_Object);
862 static Lisp_Object redisplay_window_0 (Lisp_Object);
863 static Lisp_Object redisplay_window_1 (Lisp_Object);
864 static int set_cursor_from_row (struct window *, struct glyph_row *,
865 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
866 int, int);
867 static int update_menu_bar (struct frame *, int, int);
868 static int try_window_reusing_current_matrix (struct window *);
869 static int try_window_id (struct window *);
870 static int display_line (struct it *);
871 static int display_mode_lines (struct window *);
872 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
873 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
874 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
875 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
876 static void display_menu_bar (struct window *);
877 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
878 ptrdiff_t *);
879 static int display_string (const char *, Lisp_Object, Lisp_Object,
880 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
881 static void compute_line_metrics (struct it *);
882 static void run_redisplay_end_trigger_hook (struct it *);
883 static int get_overlay_strings (struct it *, ptrdiff_t);
884 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
885 static void next_overlay_string (struct it *);
886 static void reseat (struct it *, struct text_pos, int);
887 static void reseat_1 (struct it *, struct text_pos, int);
888 static void back_to_previous_visible_line_start (struct it *);
889 void reseat_at_previous_visible_line_start (struct it *);
890 static void reseat_at_next_visible_line_start (struct it *, int);
891 static int next_element_from_ellipsis (struct it *);
892 static int next_element_from_display_vector (struct it *);
893 static int next_element_from_string (struct it *);
894 static int next_element_from_c_string (struct it *);
895 static int next_element_from_buffer (struct it *);
896 static int next_element_from_composition (struct it *);
897 static int next_element_from_image (struct it *);
898 static int next_element_from_stretch (struct it *);
899 static void load_overlay_strings (struct it *, ptrdiff_t);
900 static int init_from_display_pos (struct it *, struct window *,
901 struct display_pos *);
902 static void reseat_to_string (struct it *, const char *,
903 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
904 static int get_next_display_element (struct it *);
905 static enum move_it_result
906 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
907 enum move_operation_enum);
908 void move_it_vertically_backward (struct it *, int);
909 static void init_to_row_start (struct it *, struct window *,
910 struct glyph_row *);
911 static int init_to_row_end (struct it *, struct window *,
912 struct glyph_row *);
913 static void back_to_previous_line_start (struct it *);
914 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
915 static struct text_pos string_pos_nchars_ahead (struct text_pos,
916 Lisp_Object, ptrdiff_t);
917 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
918 static struct text_pos c_string_pos (ptrdiff_t, const char *, int);
919 static ptrdiff_t number_of_chars (const char *, int);
920 static void compute_stop_pos (struct it *);
921 static void compute_string_pos (struct text_pos *, struct text_pos,
922 Lisp_Object);
923 static int face_before_or_after_it_pos (struct it *, int);
924 static ptrdiff_t next_overlay_change (ptrdiff_t);
925 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
926 Lisp_Object, struct text_pos *, ptrdiff_t, int);
927 static int handle_single_display_spec (struct it *, Lisp_Object,
928 Lisp_Object, Lisp_Object,
929 struct text_pos *, ptrdiff_t, int, int);
930 static int underlying_face_id (struct it *);
931 static int in_ellipses_for_invisible_text_p (struct display_pos *,
932 struct window *);
934 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
935 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
937 #ifdef HAVE_WINDOW_SYSTEM
939 static void x_consider_frame_title (Lisp_Object);
940 static int tool_bar_lines_needed (struct frame *, int *);
941 static void update_tool_bar (struct frame *, int);
942 static void build_desired_tool_bar_string (struct frame *f);
943 static int redisplay_tool_bar (struct frame *);
944 static void display_tool_bar_line (struct it *, int);
945 static void notice_overwritten_cursor (struct window *,
946 enum glyph_row_area,
947 int, int, int, int);
948 static void append_stretch_glyph (struct it *, Lisp_Object,
949 int, int, int);
952 #endif /* HAVE_WINDOW_SYSTEM */
954 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
955 static int coords_in_mouse_face_p (struct window *, int, int);
959 /***********************************************************************
960 Window display dimensions
961 ***********************************************************************/
963 /* Return the bottom boundary y-position for text lines in window W.
964 This is the first y position at which a line cannot start.
965 It is relative to the top of the window.
967 This is the height of W minus the height of a mode line, if any. */
970 window_text_bottom_y (struct window *w)
972 int height = WINDOW_TOTAL_HEIGHT (w);
974 if (WINDOW_WANTS_MODELINE_P (w))
975 height -= CURRENT_MODE_LINE_HEIGHT (w);
976 return height;
979 /* Return the pixel width of display area AREA of window W. AREA < 0
980 means return the total width of W, not including fringes to
981 the left and right of the window. */
984 window_box_width (struct window *w, int area)
986 int cols = XFASTINT (w->total_cols);
987 int pixels = 0;
989 if (!w->pseudo_window_p)
991 cols -= WINDOW_SCROLL_BAR_COLS (w);
993 if (area == TEXT_AREA)
995 if (INTEGERP (w->left_margin_cols))
996 cols -= XFASTINT (w->left_margin_cols);
997 if (INTEGERP (w->right_margin_cols))
998 cols -= XFASTINT (w->right_margin_cols);
999 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1001 else if (area == LEFT_MARGIN_AREA)
1003 cols = (INTEGERP (w->left_margin_cols)
1004 ? XFASTINT (w->left_margin_cols) : 0);
1005 pixels = 0;
1007 else if (area == RIGHT_MARGIN_AREA)
1009 cols = (INTEGERP (w->right_margin_cols)
1010 ? XFASTINT (w->right_margin_cols) : 0);
1011 pixels = 0;
1015 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1019 /* Return the pixel height of the display area of window W, not
1020 including mode lines of W, if any. */
1023 window_box_height (struct window *w)
1025 struct frame *f = XFRAME (w->frame);
1026 int height = WINDOW_TOTAL_HEIGHT (w);
1028 eassert (height >= 0);
1030 /* Note: the code below that determines the mode-line/header-line
1031 height is essentially the same as that contained in the macro
1032 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1033 the appropriate glyph row has its `mode_line_p' flag set,
1034 and if it doesn't, uses estimate_mode_line_height instead. */
1036 if (WINDOW_WANTS_MODELINE_P (w))
1038 struct glyph_row *ml_row
1039 = (w->current_matrix && w->current_matrix->rows
1040 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1041 : 0);
1042 if (ml_row && ml_row->mode_line_p)
1043 height -= ml_row->height;
1044 else
1045 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1048 if (WINDOW_WANTS_HEADER_LINE_P (w))
1050 struct glyph_row *hl_row
1051 = (w->current_matrix && w->current_matrix->rows
1052 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1053 : 0);
1054 if (hl_row && hl_row->mode_line_p)
1055 height -= hl_row->height;
1056 else
1057 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1060 /* With a very small font and a mode-line that's taller than
1061 default, we might end up with a negative height. */
1062 return max (0, height);
1065 /* Return the window-relative coordinate of the left edge of display
1066 area AREA of window W. AREA < 0 means return the left edge of the
1067 whole window, to the right of the left fringe of W. */
1070 window_box_left_offset (struct window *w, int area)
1072 int x;
1074 if (w->pseudo_window_p)
1075 return 0;
1077 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1079 if (area == TEXT_AREA)
1080 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1081 + window_box_width (w, LEFT_MARGIN_AREA));
1082 else if (area == RIGHT_MARGIN_AREA)
1083 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1084 + window_box_width (w, LEFT_MARGIN_AREA)
1085 + window_box_width (w, TEXT_AREA)
1086 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1088 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1089 else if (area == LEFT_MARGIN_AREA
1090 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1091 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1093 return x;
1097 /* Return the window-relative coordinate of the right edge of display
1098 area AREA of window W. AREA < 0 means return the right edge of the
1099 whole window, to the left of the right fringe of W. */
1102 window_box_right_offset (struct window *w, int area)
1104 return window_box_left_offset (w, area) + window_box_width (w, area);
1107 /* Return the frame-relative coordinate of the left edge of display
1108 area AREA of window W. AREA < 0 means return the left edge of the
1109 whole window, to the right of the left fringe of W. */
1112 window_box_left (struct window *w, int area)
1114 struct frame *f = XFRAME (w->frame);
1115 int x;
1117 if (w->pseudo_window_p)
1118 return FRAME_INTERNAL_BORDER_WIDTH (f);
1120 x = (WINDOW_LEFT_EDGE_X (w)
1121 + window_box_left_offset (w, area));
1123 return x;
1127 /* Return the frame-relative coordinate of the right edge of display
1128 area AREA of window W. AREA < 0 means return the right edge of the
1129 whole window, to the left of the right fringe of W. */
1132 window_box_right (struct window *w, int area)
1134 return window_box_left (w, area) + window_box_width (w, area);
1137 /* Get the bounding box of the display area AREA of window W, without
1138 mode lines, in frame-relative coordinates. AREA < 0 means the
1139 whole window, not including the left and right fringes of
1140 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1141 coordinates of the upper-left corner of the box. Return in
1142 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1144 void
1145 window_box (struct window *w, int area, int *box_x, int *box_y,
1146 int *box_width, int *box_height)
1148 if (box_width)
1149 *box_width = window_box_width (w, area);
1150 if (box_height)
1151 *box_height = window_box_height (w);
1152 if (box_x)
1153 *box_x = window_box_left (w, area);
1154 if (box_y)
1156 *box_y = WINDOW_TOP_EDGE_Y (w);
1157 if (WINDOW_WANTS_HEADER_LINE_P (w))
1158 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1163 /* Get the bounding box of the display area AREA of window W, without
1164 mode lines. AREA < 0 means the whole window, not including the
1165 left and right fringe of the window. Return in *TOP_LEFT_X
1166 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1167 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1168 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1169 box. */
1171 static inline void
1172 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1173 int *bottom_right_x, int *bottom_right_y)
1175 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1176 bottom_right_y);
1177 *bottom_right_x += *top_left_x;
1178 *bottom_right_y += *top_left_y;
1183 /***********************************************************************
1184 Utilities
1185 ***********************************************************************/
1187 /* Return the bottom y-position of the line the iterator IT is in.
1188 This can modify IT's settings. */
1191 line_bottom_y (struct it *it)
1193 int line_height = it->max_ascent + it->max_descent;
1194 int line_top_y = it->current_y;
1196 if (line_height == 0)
1198 if (last_height)
1199 line_height = last_height;
1200 else if (IT_CHARPOS (*it) < ZV)
1202 move_it_by_lines (it, 1);
1203 line_height = (it->max_ascent || it->max_descent
1204 ? it->max_ascent + it->max_descent
1205 : last_height);
1207 else
1209 struct glyph_row *row = it->glyph_row;
1211 /* Use the default character height. */
1212 it->glyph_row = NULL;
1213 it->what = IT_CHARACTER;
1214 it->c = ' ';
1215 it->len = 1;
1216 PRODUCE_GLYPHS (it);
1217 line_height = it->ascent + it->descent;
1218 it->glyph_row = row;
1222 return line_top_y + line_height;
1225 /* Subroutine of pos_visible_p below. Extracts a display string, if
1226 any, from the display spec given as its argument. */
1227 static Lisp_Object
1228 string_from_display_spec (Lisp_Object spec)
1230 if (CONSP (spec))
1232 while (CONSP (spec))
1234 if (STRINGP (XCAR (spec)))
1235 return XCAR (spec);
1236 spec = XCDR (spec);
1239 else if (VECTORP (spec))
1241 ptrdiff_t i;
1243 for (i = 0; i < ASIZE (spec); i++)
1245 if (STRINGP (AREF (spec, i)))
1246 return AREF (spec, i);
1248 return Qnil;
1251 return spec;
1255 /* Limit insanely large values of W->hscroll on frame F to the largest
1256 value that will still prevent first_visible_x and last_visible_x of
1257 'struct it' from overflowing an int. */
1258 static inline int
1259 window_hscroll_limited (struct window *w, struct frame *f)
1261 ptrdiff_t window_hscroll = w->hscroll;
1262 int window_text_width = window_box_width (w, TEXT_AREA);
1263 int colwidth = FRAME_COLUMN_WIDTH (f);
1265 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1266 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1268 return window_hscroll;
1271 /* Return 1 if position CHARPOS is visible in window W.
1272 CHARPOS < 0 means return info about WINDOW_END position.
1273 If visible, set *X and *Y to pixel coordinates of top left corner.
1274 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1275 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1278 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1279 int *rtop, int *rbot, int *rowh, int *vpos)
1281 struct it it;
1282 void *itdata = bidi_shelve_cache ();
1283 struct text_pos top;
1284 int visible_p = 0;
1285 struct buffer *old_buffer = NULL;
1287 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1288 return visible_p;
1290 if (XBUFFER (w->buffer) != current_buffer)
1292 old_buffer = current_buffer;
1293 set_buffer_internal_1 (XBUFFER (w->buffer));
1296 SET_TEXT_POS_FROM_MARKER (top, w->start);
1297 /* Scrolling a minibuffer window via scroll bar when the echo area
1298 shows long text sometimes resets the minibuffer contents behind
1299 our backs. */
1300 if (CHARPOS (top) > ZV)
1301 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1303 /* Compute exact mode line heights. */
1304 if (WINDOW_WANTS_MODELINE_P (w))
1305 current_mode_line_height
1306 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1307 BVAR (current_buffer, mode_line_format));
1309 if (WINDOW_WANTS_HEADER_LINE_P (w))
1310 current_header_line_height
1311 = display_mode_line (w, HEADER_LINE_FACE_ID,
1312 BVAR (current_buffer, header_line_format));
1314 start_display (&it, w, top);
1315 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1316 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1318 if (charpos >= 0
1319 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1320 && IT_CHARPOS (it) >= charpos)
1321 /* When scanning backwards under bidi iteration, move_it_to
1322 stops at or _before_ CHARPOS, because it stops at or to
1323 the _right_ of the character at CHARPOS. */
1324 || (it.bidi_p && it.bidi_it.scan_dir == -1
1325 && IT_CHARPOS (it) <= charpos)))
1327 /* We have reached CHARPOS, or passed it. How the call to
1328 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1329 or covered by a display property, move_it_to stops at the end
1330 of the invisible text, to the right of CHARPOS. (ii) If
1331 CHARPOS is in a display vector, move_it_to stops on its last
1332 glyph. */
1333 int top_x = it.current_x;
1334 int top_y = it.current_y;
1335 /* Calling line_bottom_y may change it.method, it.position, etc. */
1336 enum it_method it_method = it.method;
1337 int bottom_y = (last_height = 0, line_bottom_y (&it));
1338 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1340 if (top_y < window_top_y)
1341 visible_p = bottom_y > window_top_y;
1342 else if (top_y < it.last_visible_y)
1343 visible_p = 1;
1344 if (bottom_y >= it.last_visible_y
1345 && it.bidi_p && it.bidi_it.scan_dir == -1
1346 && IT_CHARPOS (it) < charpos)
1348 /* When the last line of the window is scanned backwards
1349 under bidi iteration, we could be duped into thinking
1350 that we have passed CHARPOS, when in fact move_it_to
1351 simply stopped short of CHARPOS because it reached
1352 last_visible_y. To see if that's what happened, we call
1353 move_it_to again with a slightly larger vertical limit,
1354 and see if it actually moved vertically; if it did, we
1355 didn't really reach CHARPOS, which is beyond window end. */
1356 struct it save_it = it;
1357 /* Why 10? because we don't know how many canonical lines
1358 will the height of the next line(s) be. So we guess. */
1359 int ten_more_lines =
1360 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1362 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1363 MOVE_TO_POS | MOVE_TO_Y);
1364 if (it.current_y > top_y)
1365 visible_p = 0;
1367 it = save_it;
1369 if (visible_p)
1371 if (it_method == GET_FROM_DISPLAY_VECTOR)
1373 /* We stopped on the last glyph of a display vector.
1374 Try and recompute. Hack alert! */
1375 if (charpos < 2 || top.charpos >= charpos)
1376 top_x = it.glyph_row->x;
1377 else
1379 struct it it2;
1380 start_display (&it2, w, top);
1381 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1382 get_next_display_element (&it2);
1383 PRODUCE_GLYPHS (&it2);
1384 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1385 || it2.current_x > it2.last_visible_x)
1386 top_x = it.glyph_row->x;
1387 else
1389 top_x = it2.current_x;
1390 top_y = it2.current_y;
1394 else if (IT_CHARPOS (it) != charpos)
1396 Lisp_Object cpos = make_number (charpos);
1397 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1398 Lisp_Object string = string_from_display_spec (spec);
1399 int newline_in_string = 0;
1401 if (STRINGP (string))
1403 const char *s = SSDATA (string);
1404 const char *e = s + SBYTES (string);
1405 while (s < e)
1407 if (*s++ == '\n')
1409 newline_in_string = 1;
1410 break;
1414 /* The tricky code below is needed because there's a
1415 discrepancy between move_it_to and how we set cursor
1416 when the display line ends in a newline from a
1417 display string. move_it_to will stop _after_ such
1418 display strings, whereas set_cursor_from_row
1419 conspires with cursor_row_p to place the cursor on
1420 the first glyph produced from the display string. */
1422 /* We have overshoot PT because it is covered by a
1423 display property whose value is a string. If the
1424 string includes embedded newlines, we are also in the
1425 wrong display line. Backtrack to the correct line,
1426 where the display string begins. */
1427 if (newline_in_string)
1429 Lisp_Object startpos, endpos;
1430 EMACS_INT start, end;
1431 struct it it3;
1432 int it3_moved;
1434 /* Find the first and the last buffer positions
1435 covered by the display string. */
1436 endpos =
1437 Fnext_single_char_property_change (cpos, Qdisplay,
1438 Qnil, Qnil);
1439 startpos =
1440 Fprevious_single_char_property_change (endpos, Qdisplay,
1441 Qnil, Qnil);
1442 start = XFASTINT (startpos);
1443 end = XFASTINT (endpos);
1444 /* Move to the last buffer position before the
1445 display property. */
1446 start_display (&it3, w, top);
1447 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1448 /* Move forward one more line if the position before
1449 the display string is a newline or if it is the
1450 rightmost character on a line that is
1451 continued or word-wrapped. */
1452 if (it3.method == GET_FROM_BUFFER
1453 && it3.c == '\n')
1454 move_it_by_lines (&it3, 1);
1455 else if (move_it_in_display_line_to (&it3, -1,
1456 it3.current_x
1457 + it3.pixel_width,
1458 MOVE_TO_X)
1459 == MOVE_LINE_CONTINUED)
1461 move_it_by_lines (&it3, 1);
1462 /* When we are under word-wrap, the #$@%!
1463 move_it_by_lines moves 2 lines, so we need to
1464 fix that up. */
1465 if (it3.line_wrap == WORD_WRAP)
1466 move_it_by_lines (&it3, -1);
1469 /* Record the vertical coordinate of the display
1470 line where we wound up. */
1471 top_y = it3.current_y;
1472 if (it3.bidi_p)
1474 /* When characters are reordered for display,
1475 the character displayed to the left of the
1476 display string could be _after_ the display
1477 property in the logical order. Use the
1478 smallest vertical position of these two. */
1479 start_display (&it3, w, top);
1480 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1481 if (it3.current_y < top_y)
1482 top_y = it3.current_y;
1484 /* Move from the top of the window to the beginning
1485 of the display line where the display string
1486 begins. */
1487 start_display (&it3, w, top);
1488 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1489 /* If it3_moved stays zero after the 'while' loop
1490 below, that means we already were at a newline
1491 before the loop (e.g., the display string begins
1492 with a newline), so we don't need to (and cannot)
1493 inspect the glyphs of it3.glyph_row, because
1494 PRODUCE_GLYPHS will not produce anything for a
1495 newline, and thus it3.glyph_row stays at its
1496 stale content it got at top of the window. */
1497 it3_moved = 0;
1498 /* Finally, advance the iterator until we hit the
1499 first display element whose character position is
1500 CHARPOS, or until the first newline from the
1501 display string, which signals the end of the
1502 display line. */
1503 while (get_next_display_element (&it3))
1505 PRODUCE_GLYPHS (&it3);
1506 if (IT_CHARPOS (it3) == charpos
1507 || ITERATOR_AT_END_OF_LINE_P (&it3))
1508 break;
1509 it3_moved = 1;
1510 set_iterator_to_next (&it3, 0);
1512 top_x = it3.current_x - it3.pixel_width;
1513 /* Normally, we would exit the above loop because we
1514 found the display element whose character
1515 position is CHARPOS. For the contingency that we
1516 didn't, and stopped at the first newline from the
1517 display string, move back over the glyphs
1518 produced from the string, until we find the
1519 rightmost glyph not from the string. */
1520 if (it3_moved
1521 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1523 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1524 + it3.glyph_row->used[TEXT_AREA];
1526 while (EQ ((g - 1)->object, string))
1528 --g;
1529 top_x -= g->pixel_width;
1531 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1532 + it3.glyph_row->used[TEXT_AREA]);
1537 *x = top_x;
1538 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1539 *rtop = max (0, window_top_y - top_y);
1540 *rbot = max (0, bottom_y - it.last_visible_y);
1541 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1542 - max (top_y, window_top_y)));
1543 *vpos = it.vpos;
1546 else
1548 /* We were asked to provide info about WINDOW_END. */
1549 struct it it2;
1550 void *it2data = NULL;
1552 SAVE_IT (it2, it, it2data);
1553 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1554 move_it_by_lines (&it, 1);
1555 if (charpos < IT_CHARPOS (it)
1556 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1558 visible_p = 1;
1559 RESTORE_IT (&it2, &it2, it2data);
1560 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1561 *x = it2.current_x;
1562 *y = it2.current_y + it2.max_ascent - it2.ascent;
1563 *rtop = max (0, -it2.current_y);
1564 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1565 - it.last_visible_y));
1566 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1567 it.last_visible_y)
1568 - max (it2.current_y,
1569 WINDOW_HEADER_LINE_HEIGHT (w))));
1570 *vpos = it2.vpos;
1572 else
1573 bidi_unshelve_cache (it2data, 1);
1575 bidi_unshelve_cache (itdata, 0);
1577 if (old_buffer)
1578 set_buffer_internal_1 (old_buffer);
1580 current_header_line_height = current_mode_line_height = -1;
1582 if (visible_p && w->hscroll > 0)
1583 *x -=
1584 window_hscroll_limited (w, WINDOW_XFRAME (w))
1585 * WINDOW_FRAME_COLUMN_WIDTH (w);
1587 #if 0
1588 /* Debugging code. */
1589 if (visible_p)
1590 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1591 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1592 else
1593 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1594 #endif
1596 return visible_p;
1600 /* Return the next character from STR. Return in *LEN the length of
1601 the character. This is like STRING_CHAR_AND_LENGTH but never
1602 returns an invalid character. If we find one, we return a `?', but
1603 with the length of the invalid character. */
1605 static inline int
1606 string_char_and_length (const unsigned char *str, int *len)
1608 int c;
1610 c = STRING_CHAR_AND_LENGTH (str, *len);
1611 if (!CHAR_VALID_P (c))
1612 /* We may not change the length here because other places in Emacs
1613 don't use this function, i.e. they silently accept invalid
1614 characters. */
1615 c = '?';
1617 return c;
1622 /* Given a position POS containing a valid character and byte position
1623 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1625 static struct text_pos
1626 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1628 eassert (STRINGP (string) && nchars >= 0);
1630 if (STRING_MULTIBYTE (string))
1632 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1633 int len;
1635 while (nchars--)
1637 string_char_and_length (p, &len);
1638 p += len;
1639 CHARPOS (pos) += 1;
1640 BYTEPOS (pos) += len;
1643 else
1644 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1646 return pos;
1650 /* Value is the text position, i.e. character and byte position,
1651 for character position CHARPOS in STRING. */
1653 static inline struct text_pos
1654 string_pos (ptrdiff_t charpos, Lisp_Object string)
1656 struct text_pos pos;
1657 eassert (STRINGP (string));
1658 eassert (charpos >= 0);
1659 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1660 return pos;
1664 /* Value is a text position, i.e. character and byte position, for
1665 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1666 means recognize multibyte characters. */
1668 static struct text_pos
1669 c_string_pos (ptrdiff_t charpos, const char *s, int multibyte_p)
1671 struct text_pos pos;
1673 eassert (s != NULL);
1674 eassert (charpos >= 0);
1676 if (multibyte_p)
1678 int len;
1680 SET_TEXT_POS (pos, 0, 0);
1681 while (charpos--)
1683 string_char_and_length ((const unsigned char *) s, &len);
1684 s += len;
1685 CHARPOS (pos) += 1;
1686 BYTEPOS (pos) += len;
1689 else
1690 SET_TEXT_POS (pos, charpos, charpos);
1692 return pos;
1696 /* Value is the number of characters in C string S. MULTIBYTE_P
1697 non-zero means recognize multibyte characters. */
1699 static ptrdiff_t
1700 number_of_chars (const char *s, int multibyte_p)
1702 ptrdiff_t nchars;
1704 if (multibyte_p)
1706 ptrdiff_t rest = strlen (s);
1707 int len;
1708 const unsigned char *p = (const unsigned char *) s;
1710 for (nchars = 0; rest > 0; ++nchars)
1712 string_char_and_length (p, &len);
1713 rest -= len, p += len;
1716 else
1717 nchars = strlen (s);
1719 return nchars;
1723 /* Compute byte position NEWPOS->bytepos corresponding to
1724 NEWPOS->charpos. POS is a known position in string STRING.
1725 NEWPOS->charpos must be >= POS.charpos. */
1727 static void
1728 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1730 eassert (STRINGP (string));
1731 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1733 if (STRING_MULTIBYTE (string))
1734 *newpos = string_pos_nchars_ahead (pos, string,
1735 CHARPOS (*newpos) - CHARPOS (pos));
1736 else
1737 BYTEPOS (*newpos) = CHARPOS (*newpos);
1740 /* EXPORT:
1741 Return an estimation of the pixel height of mode or header lines on
1742 frame F. FACE_ID specifies what line's height to estimate. */
1745 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1747 #ifdef HAVE_WINDOW_SYSTEM
1748 if (FRAME_WINDOW_P (f))
1750 int height = FONT_HEIGHT (FRAME_FONT (f));
1752 /* This function is called so early when Emacs starts that the face
1753 cache and mode line face are not yet initialized. */
1754 if (FRAME_FACE_CACHE (f))
1756 struct face *face = FACE_FROM_ID (f, face_id);
1757 if (face)
1759 if (face->font)
1760 height = FONT_HEIGHT (face->font);
1761 if (face->box_line_width > 0)
1762 height += 2 * face->box_line_width;
1766 return height;
1768 #endif
1770 return 1;
1773 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1774 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1775 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1776 not force the value into range. */
1778 void
1779 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1780 int *x, int *y, NativeRectangle *bounds, int noclip)
1783 #ifdef HAVE_WINDOW_SYSTEM
1784 if (FRAME_WINDOW_P (f))
1786 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1787 even for negative values. */
1788 if (pix_x < 0)
1789 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1790 if (pix_y < 0)
1791 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1793 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1794 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1796 if (bounds)
1797 STORE_NATIVE_RECT (*bounds,
1798 FRAME_COL_TO_PIXEL_X (f, pix_x),
1799 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1800 FRAME_COLUMN_WIDTH (f) - 1,
1801 FRAME_LINE_HEIGHT (f) - 1);
1803 if (!noclip)
1805 if (pix_x < 0)
1806 pix_x = 0;
1807 else if (pix_x > FRAME_TOTAL_COLS (f))
1808 pix_x = FRAME_TOTAL_COLS (f);
1810 if (pix_y < 0)
1811 pix_y = 0;
1812 else if (pix_y > FRAME_LINES (f))
1813 pix_y = FRAME_LINES (f);
1816 #endif
1818 *x = pix_x;
1819 *y = pix_y;
1823 /* Find the glyph under window-relative coordinates X/Y in window W.
1824 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1825 strings. Return in *HPOS and *VPOS the row and column number of
1826 the glyph found. Return in *AREA the glyph area containing X.
1827 Value is a pointer to the glyph found or null if X/Y is not on
1828 text, or we can't tell because W's current matrix is not up to
1829 date. */
1831 static
1832 struct glyph *
1833 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1834 int *dx, int *dy, int *area)
1836 struct glyph *glyph, *end;
1837 struct glyph_row *row = NULL;
1838 int x0, i;
1840 /* Find row containing Y. Give up if some row is not enabled. */
1841 for (i = 0; i < w->current_matrix->nrows; ++i)
1843 row = MATRIX_ROW (w->current_matrix, i);
1844 if (!row->enabled_p)
1845 return NULL;
1846 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1847 break;
1850 *vpos = i;
1851 *hpos = 0;
1853 /* Give up if Y is not in the window. */
1854 if (i == w->current_matrix->nrows)
1855 return NULL;
1857 /* Get the glyph area containing X. */
1858 if (w->pseudo_window_p)
1860 *area = TEXT_AREA;
1861 x0 = 0;
1863 else
1865 if (x < window_box_left_offset (w, TEXT_AREA))
1867 *area = LEFT_MARGIN_AREA;
1868 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1870 else if (x < window_box_right_offset (w, TEXT_AREA))
1872 *area = TEXT_AREA;
1873 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1875 else
1877 *area = RIGHT_MARGIN_AREA;
1878 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1882 /* Find glyph containing X. */
1883 glyph = row->glyphs[*area];
1884 end = glyph + row->used[*area];
1885 x -= x0;
1886 while (glyph < end && x >= glyph->pixel_width)
1888 x -= glyph->pixel_width;
1889 ++glyph;
1892 if (glyph == end)
1893 return NULL;
1895 if (dx)
1897 *dx = x;
1898 *dy = y - (row->y + row->ascent - glyph->ascent);
1901 *hpos = glyph - row->glyphs[*area];
1902 return glyph;
1905 /* Convert frame-relative x/y to coordinates relative to window W.
1906 Takes pseudo-windows into account. */
1908 static void
1909 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1911 if (w->pseudo_window_p)
1913 /* A pseudo-window is always full-width, and starts at the
1914 left edge of the frame, plus a frame border. */
1915 struct frame *f = XFRAME (w->frame);
1916 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1917 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1919 else
1921 *x -= WINDOW_LEFT_EDGE_X (w);
1922 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1926 #ifdef HAVE_WINDOW_SYSTEM
1928 /* EXPORT:
1929 Return in RECTS[] at most N clipping rectangles for glyph string S.
1930 Return the number of stored rectangles. */
1933 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1935 XRectangle r;
1937 if (n <= 0)
1938 return 0;
1940 if (s->row->full_width_p)
1942 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1943 r.x = WINDOW_LEFT_EDGE_X (s->w);
1944 r.width = WINDOW_TOTAL_WIDTH (s->w);
1946 /* Unless displaying a mode or menu bar line, which are always
1947 fully visible, clip to the visible part of the row. */
1948 if (s->w->pseudo_window_p)
1949 r.height = s->row->visible_height;
1950 else
1951 r.height = s->height;
1953 else
1955 /* This is a text line that may be partially visible. */
1956 r.x = window_box_left (s->w, s->area);
1957 r.width = window_box_width (s->w, s->area);
1958 r.height = s->row->visible_height;
1961 if (s->clip_head)
1962 if (r.x < s->clip_head->x)
1964 if (r.width >= s->clip_head->x - r.x)
1965 r.width -= s->clip_head->x - r.x;
1966 else
1967 r.width = 0;
1968 r.x = s->clip_head->x;
1970 if (s->clip_tail)
1971 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1973 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1974 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1975 else
1976 r.width = 0;
1979 /* If S draws overlapping rows, it's sufficient to use the top and
1980 bottom of the window for clipping because this glyph string
1981 intentionally draws over other lines. */
1982 if (s->for_overlaps)
1984 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1985 r.height = window_text_bottom_y (s->w) - r.y;
1987 /* Alas, the above simple strategy does not work for the
1988 environments with anti-aliased text: if the same text is
1989 drawn onto the same place multiple times, it gets thicker.
1990 If the overlap we are processing is for the erased cursor, we
1991 take the intersection with the rectangle of the cursor. */
1992 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1994 XRectangle rc, r_save = r;
1996 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1997 rc.y = s->w->phys_cursor.y;
1998 rc.width = s->w->phys_cursor_width;
1999 rc.height = s->w->phys_cursor_height;
2001 x_intersect_rectangles (&r_save, &rc, &r);
2004 else
2006 /* Don't use S->y for clipping because it doesn't take partially
2007 visible lines into account. For example, it can be negative for
2008 partially visible lines at the top of a window. */
2009 if (!s->row->full_width_p
2010 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2011 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2012 else
2013 r.y = max (0, s->row->y);
2016 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2018 /* If drawing the cursor, don't let glyph draw outside its
2019 advertised boundaries. Cleartype does this under some circumstances. */
2020 if (s->hl == DRAW_CURSOR)
2022 struct glyph *glyph = s->first_glyph;
2023 int height, max_y;
2025 if (s->x > r.x)
2027 r.width -= s->x - r.x;
2028 r.x = s->x;
2030 r.width = min (r.width, glyph->pixel_width);
2032 /* If r.y is below window bottom, ensure that we still see a cursor. */
2033 height = min (glyph->ascent + glyph->descent,
2034 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2035 max_y = window_text_bottom_y (s->w) - height;
2036 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2037 if (s->ybase - glyph->ascent > max_y)
2039 r.y = max_y;
2040 r.height = height;
2042 else
2044 /* Don't draw cursor glyph taller than our actual glyph. */
2045 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2046 if (height < r.height)
2048 max_y = r.y + r.height;
2049 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2050 r.height = min (max_y - r.y, height);
2055 if (s->row->clip)
2057 XRectangle r_save = r;
2059 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2060 r.width = 0;
2063 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2064 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2066 #ifdef CONVERT_FROM_XRECT
2067 CONVERT_FROM_XRECT (r, *rects);
2068 #else
2069 *rects = r;
2070 #endif
2071 return 1;
2073 else
2075 /* If we are processing overlapping and allowed to return
2076 multiple clipping rectangles, we exclude the row of the glyph
2077 string from the clipping rectangle. This is to avoid drawing
2078 the same text on the environment with anti-aliasing. */
2079 #ifdef CONVERT_FROM_XRECT
2080 XRectangle rs[2];
2081 #else
2082 XRectangle *rs = rects;
2083 #endif
2084 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2086 if (s->for_overlaps & OVERLAPS_PRED)
2088 rs[i] = r;
2089 if (r.y + r.height > row_y)
2091 if (r.y < row_y)
2092 rs[i].height = row_y - r.y;
2093 else
2094 rs[i].height = 0;
2096 i++;
2098 if (s->for_overlaps & OVERLAPS_SUCC)
2100 rs[i] = r;
2101 if (r.y < row_y + s->row->visible_height)
2103 if (r.y + r.height > row_y + s->row->visible_height)
2105 rs[i].y = row_y + s->row->visible_height;
2106 rs[i].height = r.y + r.height - rs[i].y;
2108 else
2109 rs[i].height = 0;
2111 i++;
2114 n = i;
2115 #ifdef CONVERT_FROM_XRECT
2116 for (i = 0; i < n; i++)
2117 CONVERT_FROM_XRECT (rs[i], rects[i]);
2118 #endif
2119 return n;
2123 /* EXPORT:
2124 Return in *NR the clipping rectangle for glyph string S. */
2126 void
2127 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2129 get_glyph_string_clip_rects (s, nr, 1);
2133 /* EXPORT:
2134 Return the position and height of the phys cursor in window W.
2135 Set w->phys_cursor_width to width of phys cursor.
2138 void
2139 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2140 struct glyph *glyph, int *xp, int *yp, int *heightp)
2142 struct frame *f = XFRAME (WINDOW_FRAME (w));
2143 int x, y, wd, h, h0, y0;
2145 /* Compute the width of the rectangle to draw. If on a stretch
2146 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2147 rectangle as wide as the glyph, but use a canonical character
2148 width instead. */
2149 wd = glyph->pixel_width - 1;
2150 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2151 wd++; /* Why? */
2152 #endif
2154 x = w->phys_cursor.x;
2155 if (x < 0)
2157 wd += x;
2158 x = 0;
2161 if (glyph->type == STRETCH_GLYPH
2162 && !x_stretch_cursor_p)
2163 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2164 w->phys_cursor_width = wd;
2166 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2168 /* If y is below window bottom, ensure that we still see a cursor. */
2169 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2171 h = max (h0, glyph->ascent + glyph->descent);
2172 h0 = min (h0, glyph->ascent + glyph->descent);
2174 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2175 if (y < y0)
2177 h = max (h - (y0 - y) + 1, h0);
2178 y = y0 - 1;
2180 else
2182 y0 = window_text_bottom_y (w) - h0;
2183 if (y > y0)
2185 h += y - y0;
2186 y = y0;
2190 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2191 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2192 *heightp = h;
2196 * Remember which glyph the mouse is over.
2199 void
2200 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2202 Lisp_Object window;
2203 struct window *w;
2204 struct glyph_row *r, *gr, *end_row;
2205 enum window_part part;
2206 enum glyph_row_area area;
2207 int x, y, width, height;
2209 /* Try to determine frame pixel position and size of the glyph under
2210 frame pixel coordinates X/Y on frame F. */
2212 if (!f->glyphs_initialized_p
2213 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2214 NILP (window)))
2216 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2217 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2218 goto virtual_glyph;
2221 w = XWINDOW (window);
2222 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2223 height = WINDOW_FRAME_LINE_HEIGHT (w);
2225 x = window_relative_x_coord (w, part, gx);
2226 y = gy - WINDOW_TOP_EDGE_Y (w);
2228 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2229 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2231 if (w->pseudo_window_p)
2233 area = TEXT_AREA;
2234 part = ON_MODE_LINE; /* Don't adjust margin. */
2235 goto text_glyph;
2238 switch (part)
2240 case ON_LEFT_MARGIN:
2241 area = LEFT_MARGIN_AREA;
2242 goto text_glyph;
2244 case ON_RIGHT_MARGIN:
2245 area = RIGHT_MARGIN_AREA;
2246 goto text_glyph;
2248 case ON_HEADER_LINE:
2249 case ON_MODE_LINE:
2250 gr = (part == ON_HEADER_LINE
2251 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2252 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2253 gy = gr->y;
2254 area = TEXT_AREA;
2255 goto text_glyph_row_found;
2257 case ON_TEXT:
2258 area = TEXT_AREA;
2260 text_glyph:
2261 gr = 0; gy = 0;
2262 for (; r <= end_row && r->enabled_p; ++r)
2263 if (r->y + r->height > y)
2265 gr = r; gy = r->y;
2266 break;
2269 text_glyph_row_found:
2270 if (gr && gy <= y)
2272 struct glyph *g = gr->glyphs[area];
2273 struct glyph *end = g + gr->used[area];
2275 height = gr->height;
2276 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2277 if (gx + g->pixel_width > x)
2278 break;
2280 if (g < end)
2282 if (g->type == IMAGE_GLYPH)
2284 /* Don't remember when mouse is over image, as
2285 image may have hot-spots. */
2286 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2287 return;
2289 width = g->pixel_width;
2291 else
2293 /* Use nominal char spacing at end of line. */
2294 x -= gx;
2295 gx += (x / width) * width;
2298 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2299 gx += window_box_left_offset (w, area);
2301 else
2303 /* Use nominal line height at end of window. */
2304 gx = (x / width) * width;
2305 y -= gy;
2306 gy += (y / height) * height;
2308 break;
2310 case ON_LEFT_FRINGE:
2311 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2312 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2313 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2314 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2315 goto row_glyph;
2317 case ON_RIGHT_FRINGE:
2318 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2319 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2320 : window_box_right_offset (w, TEXT_AREA));
2321 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2322 goto row_glyph;
2324 case ON_SCROLL_BAR:
2325 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2327 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2328 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2329 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2330 : 0)));
2331 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2333 row_glyph:
2334 gr = 0, gy = 0;
2335 for (; r <= end_row && r->enabled_p; ++r)
2336 if (r->y + r->height > y)
2338 gr = r; gy = r->y;
2339 break;
2342 if (gr && gy <= y)
2343 height = gr->height;
2344 else
2346 /* Use nominal line height at end of window. */
2347 y -= gy;
2348 gy += (y / height) * height;
2350 break;
2352 default:
2354 virtual_glyph:
2355 /* If there is no glyph under the mouse, then we divide the screen
2356 into a grid of the smallest glyph in the frame, and use that
2357 as our "glyph". */
2359 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2360 round down even for negative values. */
2361 if (gx < 0)
2362 gx -= width - 1;
2363 if (gy < 0)
2364 gy -= height - 1;
2366 gx = (gx / width) * width;
2367 gy = (gy / height) * height;
2369 goto store_rect;
2372 gx += WINDOW_LEFT_EDGE_X (w);
2373 gy += WINDOW_TOP_EDGE_Y (w);
2375 store_rect:
2376 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2378 /* Visible feedback for debugging. */
2379 #if 0
2380 #if HAVE_X_WINDOWS
2381 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2382 f->output_data.x->normal_gc,
2383 gx, gy, width, height);
2384 #endif
2385 #endif
2389 #endif /* HAVE_WINDOW_SYSTEM */
2392 /***********************************************************************
2393 Lisp form evaluation
2394 ***********************************************************************/
2396 /* Error handler for safe_eval and safe_call. */
2398 static Lisp_Object
2399 safe_eval_handler (Lisp_Object arg)
2401 add_to_log ("Error during redisplay: %S", arg, Qnil);
2402 return Qnil;
2406 /* Evaluate SEXPR and return the result, or nil if something went
2407 wrong. Prevent redisplay during the evaluation. */
2409 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2410 Return the result, or nil if something went wrong. Prevent
2411 redisplay during the evaluation. */
2413 Lisp_Object
2414 safe_call (ptrdiff_t nargs, Lisp_Object *args)
2416 Lisp_Object val;
2418 if (inhibit_eval_during_redisplay)
2419 val = Qnil;
2420 else
2422 ptrdiff_t count = SPECPDL_INDEX ();
2423 struct gcpro gcpro1;
2425 GCPRO1 (args[0]);
2426 gcpro1.nvars = nargs;
2427 specbind (Qinhibit_redisplay, Qt);
2428 /* Use Qt to ensure debugger does not run,
2429 so there is no possibility of wanting to redisplay. */
2430 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2431 safe_eval_handler);
2432 UNGCPRO;
2433 val = unbind_to (count, val);
2436 return val;
2440 /* Call function FN with one argument ARG.
2441 Return the result, or nil if something went wrong. */
2443 Lisp_Object
2444 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2446 Lisp_Object args[2];
2447 args[0] = fn;
2448 args[1] = arg;
2449 return safe_call (2, args);
2452 static Lisp_Object Qeval;
2454 Lisp_Object
2455 safe_eval (Lisp_Object sexpr)
2457 return safe_call1 (Qeval, sexpr);
2460 /* Call function FN with one argument ARG.
2461 Return the result, or nil if something went wrong. */
2463 Lisp_Object
2464 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2466 Lisp_Object args[3];
2467 args[0] = fn;
2468 args[1] = arg1;
2469 args[2] = arg2;
2470 return safe_call (3, args);
2475 /***********************************************************************
2476 Debugging
2477 ***********************************************************************/
2479 #if 0
2481 /* Define CHECK_IT to perform sanity checks on iterators.
2482 This is for debugging. It is too slow to do unconditionally. */
2484 static void
2485 check_it (struct it *it)
2487 if (it->method == GET_FROM_STRING)
2489 eassert (STRINGP (it->string));
2490 eassert (IT_STRING_CHARPOS (*it) >= 0);
2492 else
2494 eassert (IT_STRING_CHARPOS (*it) < 0);
2495 if (it->method == GET_FROM_BUFFER)
2497 /* Check that character and byte positions agree. */
2498 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2502 if (it->dpvec)
2503 eassert (it->current.dpvec_index >= 0);
2504 else
2505 eassert (it->current.dpvec_index < 0);
2508 #define CHECK_IT(IT) check_it ((IT))
2510 #else /* not 0 */
2512 #define CHECK_IT(IT) (void) 0
2514 #endif /* not 0 */
2517 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2519 /* Check that the window end of window W is what we expect it
2520 to be---the last row in the current matrix displaying text. */
2522 static void
2523 check_window_end (struct window *w)
2525 if (!MINI_WINDOW_P (w)
2526 && !NILP (w->window_end_valid))
2528 struct glyph_row *row;
2529 eassert ((row = MATRIX_ROW (w->current_matrix,
2530 XFASTINT (w->window_end_vpos)),
2531 !row->enabled_p
2532 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2533 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2537 #define CHECK_WINDOW_END(W) check_window_end ((W))
2539 #else
2541 #define CHECK_WINDOW_END(W) (void) 0
2543 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2547 /***********************************************************************
2548 Iterator initialization
2549 ***********************************************************************/
2551 /* Initialize IT for displaying current_buffer in window W, starting
2552 at character position CHARPOS. CHARPOS < 0 means that no buffer
2553 position is specified which is useful when the iterator is assigned
2554 a position later. BYTEPOS is the byte position corresponding to
2555 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2557 If ROW is not null, calls to produce_glyphs with IT as parameter
2558 will produce glyphs in that row.
2560 BASE_FACE_ID is the id of a base face to use. It must be one of
2561 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2562 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2563 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2565 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2566 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2567 will be initialized to use the corresponding mode line glyph row of
2568 the desired matrix of W. */
2570 void
2571 init_iterator (struct it *it, struct window *w,
2572 ptrdiff_t charpos, ptrdiff_t bytepos,
2573 struct glyph_row *row, enum face_id base_face_id)
2575 int highlight_region_p;
2576 enum face_id remapped_base_face_id = base_face_id;
2578 /* Some precondition checks. */
2579 eassert (w != NULL && it != NULL);
2580 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2581 && charpos <= ZV));
2583 /* If face attributes have been changed since the last redisplay,
2584 free realized faces now because they depend on face definitions
2585 that might have changed. Don't free faces while there might be
2586 desired matrices pending which reference these faces. */
2587 if (face_change_count && !inhibit_free_realized_faces)
2589 face_change_count = 0;
2590 free_all_realized_faces (Qnil);
2593 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2594 if (! NILP (Vface_remapping_alist))
2595 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2597 /* Use one of the mode line rows of W's desired matrix if
2598 appropriate. */
2599 if (row == NULL)
2601 if (base_face_id == MODE_LINE_FACE_ID
2602 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2603 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2604 else if (base_face_id == HEADER_LINE_FACE_ID)
2605 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2608 /* Clear IT. */
2609 memset (it, 0, sizeof *it);
2610 it->current.overlay_string_index = -1;
2611 it->current.dpvec_index = -1;
2612 it->base_face_id = remapped_base_face_id;
2613 it->string = Qnil;
2614 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2615 it->paragraph_embedding = L2R;
2616 it->bidi_it.string.lstring = Qnil;
2617 it->bidi_it.string.s = NULL;
2618 it->bidi_it.string.bufpos = 0;
2620 /* The window in which we iterate over current_buffer: */
2621 XSETWINDOW (it->window, w);
2622 it->w = w;
2623 it->f = XFRAME (w->frame);
2625 it->cmp_it.id = -1;
2627 /* Extra space between lines (on window systems only). */
2628 if (base_face_id == DEFAULT_FACE_ID
2629 && FRAME_WINDOW_P (it->f))
2631 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2632 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2633 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2634 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2635 * FRAME_LINE_HEIGHT (it->f));
2636 else if (it->f->extra_line_spacing > 0)
2637 it->extra_line_spacing = it->f->extra_line_spacing;
2638 it->max_extra_line_spacing = 0;
2641 /* If realized faces have been removed, e.g. because of face
2642 attribute changes of named faces, recompute them. When running
2643 in batch mode, the face cache of the initial frame is null. If
2644 we happen to get called, make a dummy face cache. */
2645 if (FRAME_FACE_CACHE (it->f) == NULL)
2646 init_frame_faces (it->f);
2647 if (FRAME_FACE_CACHE (it->f)->used == 0)
2648 recompute_basic_faces (it->f);
2650 /* Current value of the `slice', `space-width', and 'height' properties. */
2651 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2652 it->space_width = Qnil;
2653 it->font_height = Qnil;
2654 it->override_ascent = -1;
2656 /* Are control characters displayed as `^C'? */
2657 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2659 /* -1 means everything between a CR and the following line end
2660 is invisible. >0 means lines indented more than this value are
2661 invisible. */
2662 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2663 ? clip_to_bounds (-1, XINT (BVAR (current_buffer,
2664 selective_display)),
2665 PTRDIFF_MAX)
2666 : (!NILP (BVAR (current_buffer, selective_display))
2667 ? -1 : 0));
2668 it->selective_display_ellipsis_p
2669 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2671 /* Display table to use. */
2672 it->dp = window_display_table (w);
2674 /* Are multibyte characters enabled in current_buffer? */
2675 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2677 /* Non-zero if we should highlight the region. */
2678 highlight_region_p
2679 = (!NILP (Vtransient_mark_mode)
2680 && !NILP (BVAR (current_buffer, mark_active))
2681 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2683 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2684 start and end of a visible region in window IT->w. Set both to
2685 -1 to indicate no region. */
2686 if (highlight_region_p
2687 /* Maybe highlight only in selected window. */
2688 && (/* Either show region everywhere. */
2689 highlight_nonselected_windows
2690 /* Or show region in the selected window. */
2691 || w == XWINDOW (selected_window)
2692 /* Or show the region if we are in the mini-buffer and W is
2693 the window the mini-buffer refers to. */
2694 || (MINI_WINDOW_P (XWINDOW (selected_window))
2695 && WINDOWP (minibuf_selected_window)
2696 && w == XWINDOW (minibuf_selected_window))))
2698 ptrdiff_t markpos = marker_position (BVAR (current_buffer, mark));
2699 it->region_beg_charpos = min (PT, markpos);
2700 it->region_end_charpos = max (PT, markpos);
2702 else
2703 it->region_beg_charpos = it->region_end_charpos = -1;
2705 /* Get the position at which the redisplay_end_trigger hook should
2706 be run, if it is to be run at all. */
2707 if (MARKERP (w->redisplay_end_trigger)
2708 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2709 it->redisplay_end_trigger_charpos
2710 = marker_position (w->redisplay_end_trigger);
2711 else if (INTEGERP (w->redisplay_end_trigger))
2712 it->redisplay_end_trigger_charpos =
2713 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2715 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2717 /* Are lines in the display truncated? */
2718 if (base_face_id != DEFAULT_FACE_ID
2719 || it->w->hscroll
2720 || (! WINDOW_FULL_WIDTH_P (it->w)
2721 && ((!NILP (Vtruncate_partial_width_windows)
2722 && !INTEGERP (Vtruncate_partial_width_windows))
2723 || (INTEGERP (Vtruncate_partial_width_windows)
2724 && (WINDOW_TOTAL_COLS (it->w)
2725 < XINT (Vtruncate_partial_width_windows))))))
2726 it->line_wrap = TRUNCATE;
2727 else if (NILP (BVAR (current_buffer, truncate_lines)))
2728 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2729 ? WINDOW_WRAP : WORD_WRAP;
2730 else
2731 it->line_wrap = TRUNCATE;
2733 /* Get dimensions of truncation and continuation glyphs. These are
2734 displayed as fringe bitmaps under X, but we need them for such
2735 frames when the fringes are turned off. */
2736 if (it->line_wrap == TRUNCATE)
2738 /* We will need the truncation glyph. */
2739 eassert (it->glyph_row == NULL);
2740 produce_special_glyphs (it, IT_TRUNCATION);
2741 it->truncation_pixel_width = it->pixel_width;
2743 else
2745 /* We will need the continuation glyph. */
2746 eassert (it->glyph_row == NULL);
2747 produce_special_glyphs (it, IT_CONTINUATION);
2748 it->continuation_pixel_width = it->pixel_width;
2751 /* Reset these values to zero because the produce_special_glyphs
2752 above has changed them. */
2753 it->pixel_width = it->ascent = it->descent = 0;
2754 it->phys_ascent = it->phys_descent = 0;
2756 /* Set this after getting the dimensions of truncation and
2757 continuation glyphs, so that we don't produce glyphs when calling
2758 produce_special_glyphs, above. */
2759 it->glyph_row = row;
2760 it->area = TEXT_AREA;
2762 /* Forget any previous info about this row being reversed. */
2763 if (it->glyph_row)
2764 it->glyph_row->reversed_p = 0;
2766 /* Get the dimensions of the display area. The display area
2767 consists of the visible window area plus a horizontally scrolled
2768 part to the left of the window. All x-values are relative to the
2769 start of this total display area. */
2770 if (base_face_id != DEFAULT_FACE_ID)
2772 /* Mode lines, menu bar in terminal frames. */
2773 it->first_visible_x = 0;
2774 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2776 else
2778 it->first_visible_x =
2779 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2780 it->last_visible_x = (it->first_visible_x
2781 + window_box_width (w, TEXT_AREA));
2783 /* If we truncate lines, leave room for the truncation glyph(s) at
2784 the right margin. Otherwise, leave room for the continuation
2785 glyph(s). Done only if the window has no fringes. Since we
2786 don't know at this point whether there will be any R2L lines in
2787 the window, we reserve space for truncation/continuation glyphs
2788 even if only one of the fringes is absent. */
2789 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2790 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2792 if (it->line_wrap == TRUNCATE)
2793 it->last_visible_x -= it->truncation_pixel_width;
2794 else
2795 it->last_visible_x -= it->continuation_pixel_width;
2798 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2799 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2802 /* Leave room for a border glyph. */
2803 if (!FRAME_WINDOW_P (it->f)
2804 && !WINDOW_RIGHTMOST_P (it->w))
2805 it->last_visible_x -= 1;
2807 it->last_visible_y = window_text_bottom_y (w);
2809 /* For mode lines and alike, arrange for the first glyph having a
2810 left box line if the face specifies a box. */
2811 if (base_face_id != DEFAULT_FACE_ID)
2813 struct face *face;
2815 it->face_id = remapped_base_face_id;
2817 /* If we have a boxed mode line, make the first character appear
2818 with a left box line. */
2819 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2820 if (face->box != FACE_NO_BOX)
2821 it->start_of_box_run_p = 1;
2824 /* If a buffer position was specified, set the iterator there,
2825 getting overlays and face properties from that position. */
2826 if (charpos >= BUF_BEG (current_buffer))
2828 it->end_charpos = ZV;
2829 IT_CHARPOS (*it) = charpos;
2831 /* We will rely on `reseat' to set this up properly, via
2832 handle_face_prop. */
2833 it->face_id = it->base_face_id;
2835 /* Compute byte position if not specified. */
2836 if (bytepos < charpos)
2837 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2838 else
2839 IT_BYTEPOS (*it) = bytepos;
2841 it->start = it->current;
2842 /* Do we need to reorder bidirectional text? Not if this is a
2843 unibyte buffer: by definition, none of the single-byte
2844 characters are strong R2L, so no reordering is needed. And
2845 bidi.c doesn't support unibyte buffers anyway. Also, don't
2846 reorder while we are loading loadup.el, since the tables of
2847 character properties needed for reordering are not yet
2848 available. */
2849 it->bidi_p =
2850 NILP (Vpurify_flag)
2851 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2852 && it->multibyte_p;
2854 /* If we are to reorder bidirectional text, init the bidi
2855 iterator. */
2856 if (it->bidi_p)
2858 /* Note the paragraph direction that this buffer wants to
2859 use. */
2860 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2861 Qleft_to_right))
2862 it->paragraph_embedding = L2R;
2863 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2864 Qright_to_left))
2865 it->paragraph_embedding = R2L;
2866 else
2867 it->paragraph_embedding = NEUTRAL_DIR;
2868 bidi_unshelve_cache (NULL, 0);
2869 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2870 &it->bidi_it);
2873 /* Compute faces etc. */
2874 reseat (it, it->current.pos, 1);
2877 CHECK_IT (it);
2881 /* Initialize IT for the display of window W with window start POS. */
2883 void
2884 start_display (struct it *it, struct window *w, struct text_pos pos)
2886 struct glyph_row *row;
2887 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2889 row = w->desired_matrix->rows + first_vpos;
2890 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2891 it->first_vpos = first_vpos;
2893 /* Don't reseat to previous visible line start if current start
2894 position is in a string or image. */
2895 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2897 int start_at_line_beg_p;
2898 int first_y = it->current_y;
2900 /* If window start is not at a line start, skip forward to POS to
2901 get the correct continuation lines width. */
2902 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2903 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2904 if (!start_at_line_beg_p)
2906 int new_x;
2908 reseat_at_previous_visible_line_start (it);
2909 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2911 new_x = it->current_x + it->pixel_width;
2913 /* If lines are continued, this line may end in the middle
2914 of a multi-glyph character (e.g. a control character
2915 displayed as \003, or in the middle of an overlay
2916 string). In this case move_it_to above will not have
2917 taken us to the start of the continuation line but to the
2918 end of the continued line. */
2919 if (it->current_x > 0
2920 && it->line_wrap != TRUNCATE /* Lines are continued. */
2921 && (/* And glyph doesn't fit on the line. */
2922 new_x > it->last_visible_x
2923 /* Or it fits exactly and we're on a window
2924 system frame. */
2925 || (new_x == it->last_visible_x
2926 && FRAME_WINDOW_P (it->f)
2927 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2928 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2929 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2931 if ((it->current.dpvec_index >= 0
2932 || it->current.overlay_string_index >= 0)
2933 /* If we are on a newline from a display vector or
2934 overlay string, then we are already at the end of
2935 a screen line; no need to go to the next line in
2936 that case, as this line is not really continued.
2937 (If we do go to the next line, C-e will not DTRT.) */
2938 && it->c != '\n')
2940 set_iterator_to_next (it, 1);
2941 move_it_in_display_line_to (it, -1, -1, 0);
2944 it->continuation_lines_width += it->current_x;
2946 /* If the character at POS is displayed via a display
2947 vector, move_it_to above stops at the final glyph of
2948 IT->dpvec. To make the caller redisplay that character
2949 again (a.k.a. start at POS), we need to reset the
2950 dpvec_index to the beginning of IT->dpvec. */
2951 else if (it->current.dpvec_index >= 0)
2952 it->current.dpvec_index = 0;
2954 /* We're starting a new display line, not affected by the
2955 height of the continued line, so clear the appropriate
2956 fields in the iterator structure. */
2957 it->max_ascent = it->max_descent = 0;
2958 it->max_phys_ascent = it->max_phys_descent = 0;
2960 it->current_y = first_y;
2961 it->vpos = 0;
2962 it->current_x = it->hpos = 0;
2968 /* Return 1 if POS is a position in ellipses displayed for invisible
2969 text. W is the window we display, for text property lookup. */
2971 static int
2972 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2974 Lisp_Object prop, window;
2975 int ellipses_p = 0;
2976 ptrdiff_t charpos = CHARPOS (pos->pos);
2978 /* If POS specifies a position in a display vector, this might
2979 be for an ellipsis displayed for invisible text. We won't
2980 get the iterator set up for delivering that ellipsis unless
2981 we make sure that it gets aware of the invisible text. */
2982 if (pos->dpvec_index >= 0
2983 && pos->overlay_string_index < 0
2984 && CHARPOS (pos->string_pos) < 0
2985 && charpos > BEGV
2986 && (XSETWINDOW (window, w),
2987 prop = Fget_char_property (make_number (charpos),
2988 Qinvisible, window),
2989 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2991 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2992 window);
2993 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2996 return ellipses_p;
3000 /* Initialize IT for stepping through current_buffer in window W,
3001 starting at position POS that includes overlay string and display
3002 vector/ control character translation position information. Value
3003 is zero if there are overlay strings with newlines at POS. */
3005 static int
3006 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3008 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3009 int i, overlay_strings_with_newlines = 0;
3011 /* If POS specifies a position in a display vector, this might
3012 be for an ellipsis displayed for invisible text. We won't
3013 get the iterator set up for delivering that ellipsis unless
3014 we make sure that it gets aware of the invisible text. */
3015 if (in_ellipses_for_invisible_text_p (pos, w))
3017 --charpos;
3018 bytepos = 0;
3021 /* Keep in mind: the call to reseat in init_iterator skips invisible
3022 text, so we might end up at a position different from POS. This
3023 is only a problem when POS is a row start after a newline and an
3024 overlay starts there with an after-string, and the overlay has an
3025 invisible property. Since we don't skip invisible text in
3026 display_line and elsewhere immediately after consuming the
3027 newline before the row start, such a POS will not be in a string,
3028 but the call to init_iterator below will move us to the
3029 after-string. */
3030 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3032 /* This only scans the current chunk -- it should scan all chunks.
3033 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3034 to 16 in 22.1 to make this a lesser problem. */
3035 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3037 const char *s = SSDATA (it->overlay_strings[i]);
3038 const char *e = s + SBYTES (it->overlay_strings[i]);
3040 while (s < e && *s != '\n')
3041 ++s;
3043 if (s < e)
3045 overlay_strings_with_newlines = 1;
3046 break;
3050 /* If position is within an overlay string, set up IT to the right
3051 overlay string. */
3052 if (pos->overlay_string_index >= 0)
3054 int relative_index;
3056 /* If the first overlay string happens to have a `display'
3057 property for an image, the iterator will be set up for that
3058 image, and we have to undo that setup first before we can
3059 correct the overlay string index. */
3060 if (it->method == GET_FROM_IMAGE)
3061 pop_it (it);
3063 /* We already have the first chunk of overlay strings in
3064 IT->overlay_strings. Load more until the one for
3065 pos->overlay_string_index is in IT->overlay_strings. */
3066 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3068 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3069 it->current.overlay_string_index = 0;
3070 while (n--)
3072 load_overlay_strings (it, 0);
3073 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3077 it->current.overlay_string_index = pos->overlay_string_index;
3078 relative_index = (it->current.overlay_string_index
3079 % OVERLAY_STRING_CHUNK_SIZE);
3080 it->string = it->overlay_strings[relative_index];
3081 eassert (STRINGP (it->string));
3082 it->current.string_pos = pos->string_pos;
3083 it->method = GET_FROM_STRING;
3086 if (CHARPOS (pos->string_pos) >= 0)
3088 /* Recorded position is not in an overlay string, but in another
3089 string. This can only be a string from a `display' property.
3090 IT should already be filled with that string. */
3091 it->current.string_pos = pos->string_pos;
3092 eassert (STRINGP (it->string));
3095 /* Restore position in display vector translations, control
3096 character translations or ellipses. */
3097 if (pos->dpvec_index >= 0)
3099 if (it->dpvec == NULL)
3100 get_next_display_element (it);
3101 eassert (it->dpvec && it->current.dpvec_index == 0);
3102 it->current.dpvec_index = pos->dpvec_index;
3105 CHECK_IT (it);
3106 return !overlay_strings_with_newlines;
3110 /* Initialize IT for stepping through current_buffer in window W
3111 starting at ROW->start. */
3113 static void
3114 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3116 init_from_display_pos (it, w, &row->start);
3117 it->start = row->start;
3118 it->continuation_lines_width = row->continuation_lines_width;
3119 CHECK_IT (it);
3123 /* Initialize IT for stepping through current_buffer in window W
3124 starting in the line following ROW, i.e. starting at ROW->end.
3125 Value is zero if there are overlay strings with newlines at ROW's
3126 end position. */
3128 static int
3129 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3131 int success = 0;
3133 if (init_from_display_pos (it, w, &row->end))
3135 if (row->continued_p)
3136 it->continuation_lines_width
3137 = row->continuation_lines_width + row->pixel_width;
3138 CHECK_IT (it);
3139 success = 1;
3142 return success;
3148 /***********************************************************************
3149 Text properties
3150 ***********************************************************************/
3152 /* Called when IT reaches IT->stop_charpos. Handle text property and
3153 overlay changes. Set IT->stop_charpos to the next position where
3154 to stop. */
3156 static void
3157 handle_stop (struct it *it)
3159 enum prop_handled handled;
3160 int handle_overlay_change_p;
3161 struct props *p;
3163 it->dpvec = NULL;
3164 it->current.dpvec_index = -1;
3165 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3166 it->ignore_overlay_strings_at_pos_p = 0;
3167 it->ellipsis_p = 0;
3169 /* Use face of preceding text for ellipsis (if invisible) */
3170 if (it->selective_display_ellipsis_p)
3171 it->saved_face_id = it->face_id;
3175 handled = HANDLED_NORMALLY;
3177 /* Call text property handlers. */
3178 for (p = it_props; p->handler; ++p)
3180 handled = p->handler (it);
3182 if (handled == HANDLED_RECOMPUTE_PROPS)
3183 break;
3184 else if (handled == HANDLED_RETURN)
3186 /* We still want to show before and after strings from
3187 overlays even if the actual buffer text is replaced. */
3188 if (!handle_overlay_change_p
3189 || it->sp > 1
3190 /* Don't call get_overlay_strings_1 if we already
3191 have overlay strings loaded, because doing so
3192 will load them again and push the iterator state
3193 onto the stack one more time, which is not
3194 expected by the rest of the code that processes
3195 overlay strings. */
3196 || (it->current.overlay_string_index < 0
3197 ? !get_overlay_strings_1 (it, 0, 0)
3198 : 0))
3200 if (it->ellipsis_p)
3201 setup_for_ellipsis (it, 0);
3202 /* When handling a display spec, we might load an
3203 empty string. In that case, discard it here. We
3204 used to discard it in handle_single_display_spec,
3205 but that causes get_overlay_strings_1, above, to
3206 ignore overlay strings that we must check. */
3207 if (STRINGP (it->string) && !SCHARS (it->string))
3208 pop_it (it);
3209 return;
3211 else if (STRINGP (it->string) && !SCHARS (it->string))
3212 pop_it (it);
3213 else
3215 it->ignore_overlay_strings_at_pos_p = 1;
3216 it->string_from_display_prop_p = 0;
3217 it->from_disp_prop_p = 0;
3218 handle_overlay_change_p = 0;
3220 handled = HANDLED_RECOMPUTE_PROPS;
3221 break;
3223 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3224 handle_overlay_change_p = 0;
3227 if (handled != HANDLED_RECOMPUTE_PROPS)
3229 /* Don't check for overlay strings below when set to deliver
3230 characters from a display vector. */
3231 if (it->method == GET_FROM_DISPLAY_VECTOR)
3232 handle_overlay_change_p = 0;
3234 /* Handle overlay changes.
3235 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3236 if it finds overlays. */
3237 if (handle_overlay_change_p)
3238 handled = handle_overlay_change (it);
3241 if (it->ellipsis_p)
3243 setup_for_ellipsis (it, 0);
3244 break;
3247 while (handled == HANDLED_RECOMPUTE_PROPS);
3249 /* Determine where to stop next. */
3250 if (handled == HANDLED_NORMALLY)
3251 compute_stop_pos (it);
3255 /* Compute IT->stop_charpos from text property and overlay change
3256 information for IT's current position. */
3258 static void
3259 compute_stop_pos (struct it *it)
3261 register INTERVAL iv, next_iv;
3262 Lisp_Object object, limit, position;
3263 ptrdiff_t charpos, bytepos;
3265 if (STRINGP (it->string))
3267 /* Strings are usually short, so don't limit the search for
3268 properties. */
3269 it->stop_charpos = it->end_charpos;
3270 object = it->string;
3271 limit = Qnil;
3272 charpos = IT_STRING_CHARPOS (*it);
3273 bytepos = IT_STRING_BYTEPOS (*it);
3275 else
3277 ptrdiff_t pos;
3279 /* If end_charpos is out of range for some reason, such as a
3280 misbehaving display function, rationalize it (Bug#5984). */
3281 if (it->end_charpos > ZV)
3282 it->end_charpos = ZV;
3283 it->stop_charpos = it->end_charpos;
3285 /* If next overlay change is in front of the current stop pos
3286 (which is IT->end_charpos), stop there. Note: value of
3287 next_overlay_change is point-max if no overlay change
3288 follows. */
3289 charpos = IT_CHARPOS (*it);
3290 bytepos = IT_BYTEPOS (*it);
3291 pos = next_overlay_change (charpos);
3292 if (pos < it->stop_charpos)
3293 it->stop_charpos = pos;
3295 /* If showing the region, we have to stop at the region
3296 start or end because the face might change there. */
3297 if (it->region_beg_charpos > 0)
3299 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3300 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3301 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3302 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3305 /* Set up variables for computing the stop position from text
3306 property changes. */
3307 XSETBUFFER (object, current_buffer);
3308 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3311 /* Get the interval containing IT's position. Value is a null
3312 interval if there isn't such an interval. */
3313 position = make_number (charpos);
3314 iv = validate_interval_range (object, &position, &position, 0);
3315 if (!NULL_INTERVAL_P (iv))
3317 Lisp_Object values_here[LAST_PROP_IDX];
3318 struct props *p;
3320 /* Get properties here. */
3321 for (p = it_props; p->handler; ++p)
3322 values_here[p->idx] = textget (iv->plist, *p->name);
3324 /* Look for an interval following iv that has different
3325 properties. */
3326 for (next_iv = next_interval (iv);
3327 (!NULL_INTERVAL_P (next_iv)
3328 && (NILP (limit)
3329 || XFASTINT (limit) > next_iv->position));
3330 next_iv = next_interval (next_iv))
3332 for (p = it_props; p->handler; ++p)
3334 Lisp_Object new_value;
3336 new_value = textget (next_iv->plist, *p->name);
3337 if (!EQ (values_here[p->idx], new_value))
3338 break;
3341 if (p->handler)
3342 break;
3345 if (!NULL_INTERVAL_P (next_iv))
3347 if (INTEGERP (limit)
3348 && next_iv->position >= XFASTINT (limit))
3349 /* No text property change up to limit. */
3350 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3351 else
3352 /* Text properties change in next_iv. */
3353 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3357 if (it->cmp_it.id < 0)
3359 ptrdiff_t stoppos = it->end_charpos;
3361 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3362 stoppos = -1;
3363 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3364 stoppos, it->string);
3367 eassert (STRINGP (it->string)
3368 || (it->stop_charpos >= BEGV
3369 && it->stop_charpos >= IT_CHARPOS (*it)));
3373 /* Return the position of the next overlay change after POS in
3374 current_buffer. Value is point-max if no overlay change
3375 follows. This is like `next-overlay-change' but doesn't use
3376 xmalloc. */
3378 static ptrdiff_t
3379 next_overlay_change (ptrdiff_t pos)
3381 ptrdiff_t i, noverlays;
3382 ptrdiff_t endpos;
3383 Lisp_Object *overlays;
3385 /* Get all overlays at the given position. */
3386 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3388 /* If any of these overlays ends before endpos,
3389 use its ending point instead. */
3390 for (i = 0; i < noverlays; ++i)
3392 Lisp_Object oend;
3393 ptrdiff_t oendpos;
3395 oend = OVERLAY_END (overlays[i]);
3396 oendpos = OVERLAY_POSITION (oend);
3397 endpos = min (endpos, oendpos);
3400 return endpos;
3403 /* How many characters forward to search for a display property or
3404 display string. Searching too far forward makes the bidi display
3405 sluggish, especially in small windows. */
3406 #define MAX_DISP_SCAN 250
3408 /* Return the character position of a display string at or after
3409 position specified by POSITION. If no display string exists at or
3410 after POSITION, return ZV. A display string is either an overlay
3411 with `display' property whose value is a string, or a `display'
3412 text property whose value is a string. STRING is data about the
3413 string to iterate; if STRING->lstring is nil, we are iterating a
3414 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3415 on a GUI frame. DISP_PROP is set to zero if we searched
3416 MAX_DISP_SCAN characters forward without finding any display
3417 strings, non-zero otherwise. It is set to 2 if the display string
3418 uses any kind of `(space ...)' spec that will produce a stretch of
3419 white space in the text area. */
3420 ptrdiff_t
3421 compute_display_string_pos (struct text_pos *position,
3422 struct bidi_string_data *string,
3423 int frame_window_p, int *disp_prop)
3425 /* OBJECT = nil means current buffer. */
3426 Lisp_Object object =
3427 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3428 Lisp_Object pos, spec, limpos;
3429 int string_p = (string && (STRINGP (string->lstring) || string->s));
3430 ptrdiff_t eob = string_p ? string->schars : ZV;
3431 ptrdiff_t begb = string_p ? 0 : BEGV;
3432 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3433 ptrdiff_t lim =
3434 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3435 struct text_pos tpos;
3436 int rv = 0;
3438 *disp_prop = 1;
3440 if (charpos >= eob
3441 /* We don't support display properties whose values are strings
3442 that have display string properties. */
3443 || string->from_disp_str
3444 /* C strings cannot have display properties. */
3445 || (string->s && !STRINGP (object)))
3447 *disp_prop = 0;
3448 return eob;
3451 /* If the character at CHARPOS is where the display string begins,
3452 return CHARPOS. */
3453 pos = make_number (charpos);
3454 if (STRINGP (object))
3455 bufpos = string->bufpos;
3456 else
3457 bufpos = charpos;
3458 tpos = *position;
3459 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3460 && (charpos <= begb
3461 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3462 object),
3463 spec))
3464 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3465 frame_window_p)))
3467 if (rv == 2)
3468 *disp_prop = 2;
3469 return charpos;
3472 /* Look forward for the first character with a `display' property
3473 that will replace the underlying text when displayed. */
3474 limpos = make_number (lim);
3475 do {
3476 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3477 CHARPOS (tpos) = XFASTINT (pos);
3478 if (CHARPOS (tpos) >= lim)
3480 *disp_prop = 0;
3481 break;
3483 if (STRINGP (object))
3484 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3485 else
3486 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3487 spec = Fget_char_property (pos, Qdisplay, object);
3488 if (!STRINGP (object))
3489 bufpos = CHARPOS (tpos);
3490 } while (NILP (spec)
3491 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3492 bufpos, frame_window_p)));
3493 if (rv == 2)
3494 *disp_prop = 2;
3496 return CHARPOS (tpos);
3499 /* Return the character position of the end of the display string that
3500 started at CHARPOS. If there's no display string at CHARPOS,
3501 return -1. A display string is either an overlay with `display'
3502 property whose value is a string or a `display' text property whose
3503 value is a string. */
3504 ptrdiff_t
3505 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3507 /* OBJECT = nil means current buffer. */
3508 Lisp_Object object =
3509 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3510 Lisp_Object pos = make_number (charpos);
3511 ptrdiff_t eob =
3512 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3514 if (charpos >= eob || (string->s && !STRINGP (object)))
3515 return eob;
3517 /* It could happen that the display property or overlay was removed
3518 since we found it in compute_display_string_pos above. One way
3519 this can happen is if JIT font-lock was called (through
3520 handle_fontified_prop), and jit-lock-functions remove text
3521 properties or overlays from the portion of buffer that includes
3522 CHARPOS. Muse mode is known to do that, for example. In this
3523 case, we return -1 to the caller, to signal that no display
3524 string is actually present at CHARPOS. See bidi_fetch_char for
3525 how this is handled.
3527 An alternative would be to never look for display properties past
3528 it->stop_charpos. But neither compute_display_string_pos nor
3529 bidi_fetch_char that calls it know or care where the next
3530 stop_charpos is. */
3531 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3532 return -1;
3534 /* Look forward for the first character where the `display' property
3535 changes. */
3536 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3538 return XFASTINT (pos);
3543 /***********************************************************************
3544 Fontification
3545 ***********************************************************************/
3547 /* Handle changes in the `fontified' property of the current buffer by
3548 calling hook functions from Qfontification_functions to fontify
3549 regions of text. */
3551 static enum prop_handled
3552 handle_fontified_prop (struct it *it)
3554 Lisp_Object prop, pos;
3555 enum prop_handled handled = HANDLED_NORMALLY;
3557 if (!NILP (Vmemory_full))
3558 return handled;
3560 /* Get the value of the `fontified' property at IT's current buffer
3561 position. (The `fontified' property doesn't have a special
3562 meaning in strings.) If the value is nil, call functions from
3563 Qfontification_functions. */
3564 if (!STRINGP (it->string)
3565 && it->s == NULL
3566 && !NILP (Vfontification_functions)
3567 && !NILP (Vrun_hooks)
3568 && (pos = make_number (IT_CHARPOS (*it)),
3569 prop = Fget_char_property (pos, Qfontified, Qnil),
3570 /* Ignore the special cased nil value always present at EOB since
3571 no amount of fontifying will be able to change it. */
3572 NILP (prop) && IT_CHARPOS (*it) < Z))
3574 ptrdiff_t count = SPECPDL_INDEX ();
3575 Lisp_Object val;
3576 struct buffer *obuf = current_buffer;
3577 int begv = BEGV, zv = ZV;
3578 int old_clip_changed = current_buffer->clip_changed;
3580 val = Vfontification_functions;
3581 specbind (Qfontification_functions, Qnil);
3583 eassert (it->end_charpos == ZV);
3585 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3586 safe_call1 (val, pos);
3587 else
3589 Lisp_Object fns, fn;
3590 struct gcpro gcpro1, gcpro2;
3592 fns = Qnil;
3593 GCPRO2 (val, fns);
3595 for (; CONSP (val); val = XCDR (val))
3597 fn = XCAR (val);
3599 if (EQ (fn, Qt))
3601 /* A value of t indicates this hook has a local
3602 binding; it means to run the global binding too.
3603 In a global value, t should not occur. If it
3604 does, we must ignore it to avoid an endless
3605 loop. */
3606 for (fns = Fdefault_value (Qfontification_functions);
3607 CONSP (fns);
3608 fns = XCDR (fns))
3610 fn = XCAR (fns);
3611 if (!EQ (fn, Qt))
3612 safe_call1 (fn, pos);
3615 else
3616 safe_call1 (fn, pos);
3619 UNGCPRO;
3622 unbind_to (count, Qnil);
3624 /* Fontification functions routinely call `save-restriction'.
3625 Normally, this tags clip_changed, which can confuse redisplay
3626 (see discussion in Bug#6671). Since we don't perform any
3627 special handling of fontification changes in the case where
3628 `save-restriction' isn't called, there's no point doing so in
3629 this case either. So, if the buffer's restrictions are
3630 actually left unchanged, reset clip_changed. */
3631 if (obuf == current_buffer)
3633 if (begv == BEGV && zv == ZV)
3634 current_buffer->clip_changed = old_clip_changed;
3636 /* There isn't much we can reasonably do to protect against
3637 misbehaving fontification, but here's a fig leaf. */
3638 else if (!NILP (BVAR (obuf, name)))
3639 set_buffer_internal_1 (obuf);
3641 /* The fontification code may have added/removed text.
3642 It could do even a lot worse, but let's at least protect against
3643 the most obvious case where only the text past `pos' gets changed',
3644 as is/was done in grep.el where some escapes sequences are turned
3645 into face properties (bug#7876). */
3646 it->end_charpos = ZV;
3648 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3649 something. This avoids an endless loop if they failed to
3650 fontify the text for which reason ever. */
3651 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3652 handled = HANDLED_RECOMPUTE_PROPS;
3655 return handled;
3660 /***********************************************************************
3661 Faces
3662 ***********************************************************************/
3664 /* Set up iterator IT from face properties at its current position.
3665 Called from handle_stop. */
3667 static enum prop_handled
3668 handle_face_prop (struct it *it)
3670 int new_face_id;
3671 ptrdiff_t next_stop;
3673 if (!STRINGP (it->string))
3675 new_face_id
3676 = face_at_buffer_position (it->w,
3677 IT_CHARPOS (*it),
3678 it->region_beg_charpos,
3679 it->region_end_charpos,
3680 &next_stop,
3681 (IT_CHARPOS (*it)
3682 + TEXT_PROP_DISTANCE_LIMIT),
3683 0, it->base_face_id);
3685 /* Is this a start of a run of characters with box face?
3686 Caveat: this can be called for a freshly initialized
3687 iterator; face_id is -1 in this case. We know that the new
3688 face will not change until limit, i.e. if the new face has a
3689 box, all characters up to limit will have one. But, as
3690 usual, we don't know whether limit is really the end. */
3691 if (new_face_id != it->face_id)
3693 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3695 /* If new face has a box but old face has not, this is
3696 the start of a run of characters with box, i.e. it has
3697 a shadow on the left side. The value of face_id of the
3698 iterator will be -1 if this is the initial call that gets
3699 the face. In this case, we have to look in front of IT's
3700 position and see whether there is a face != new_face_id. */
3701 it->start_of_box_run_p
3702 = (new_face->box != FACE_NO_BOX
3703 && (it->face_id >= 0
3704 || IT_CHARPOS (*it) == BEG
3705 || new_face_id != face_before_it_pos (it)));
3706 it->face_box_p = new_face->box != FACE_NO_BOX;
3709 else
3711 int base_face_id;
3712 ptrdiff_t bufpos;
3713 int i;
3714 Lisp_Object from_overlay
3715 = (it->current.overlay_string_index >= 0
3716 ? it->string_overlays[it->current.overlay_string_index
3717 % OVERLAY_STRING_CHUNK_SIZE]
3718 : Qnil);
3720 /* See if we got to this string directly or indirectly from
3721 an overlay property. That includes the before-string or
3722 after-string of an overlay, strings in display properties
3723 provided by an overlay, their text properties, etc.
3725 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3726 if (! NILP (from_overlay))
3727 for (i = it->sp - 1; i >= 0; i--)
3729 if (it->stack[i].current.overlay_string_index >= 0)
3730 from_overlay
3731 = it->string_overlays[it->stack[i].current.overlay_string_index
3732 % OVERLAY_STRING_CHUNK_SIZE];
3733 else if (! NILP (it->stack[i].from_overlay))
3734 from_overlay = it->stack[i].from_overlay;
3736 if (!NILP (from_overlay))
3737 break;
3740 if (! NILP (from_overlay))
3742 bufpos = IT_CHARPOS (*it);
3743 /* For a string from an overlay, the base face depends
3744 only on text properties and ignores overlays. */
3745 base_face_id
3746 = face_for_overlay_string (it->w,
3747 IT_CHARPOS (*it),
3748 it->region_beg_charpos,
3749 it->region_end_charpos,
3750 &next_stop,
3751 (IT_CHARPOS (*it)
3752 + TEXT_PROP_DISTANCE_LIMIT),
3754 from_overlay);
3756 else
3758 bufpos = 0;
3760 /* For strings from a `display' property, use the face at
3761 IT's current buffer position as the base face to merge
3762 with, so that overlay strings appear in the same face as
3763 surrounding text, unless they specify their own
3764 faces. */
3765 base_face_id = it->string_from_prefix_prop_p
3766 ? DEFAULT_FACE_ID
3767 : underlying_face_id (it);
3770 new_face_id = face_at_string_position (it->w,
3771 it->string,
3772 IT_STRING_CHARPOS (*it),
3773 bufpos,
3774 it->region_beg_charpos,
3775 it->region_end_charpos,
3776 &next_stop,
3777 base_face_id, 0);
3779 /* Is this a start of a run of characters with box? Caveat:
3780 this can be called for a freshly allocated iterator; face_id
3781 is -1 is this case. We know that the new face will not
3782 change until the next check pos, i.e. if the new face has a
3783 box, all characters up to that position will have a
3784 box. But, as usual, we don't know whether that position
3785 is really the end. */
3786 if (new_face_id != it->face_id)
3788 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3789 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3791 /* If new face has a box but old face hasn't, this is the
3792 start of a run of characters with box, i.e. it has a
3793 shadow on the left side. */
3794 it->start_of_box_run_p
3795 = new_face->box && (old_face == NULL || !old_face->box);
3796 it->face_box_p = new_face->box != FACE_NO_BOX;
3800 it->face_id = new_face_id;
3801 return HANDLED_NORMALLY;
3805 /* Return the ID of the face ``underlying'' IT's current position,
3806 which is in a string. If the iterator is associated with a
3807 buffer, return the face at IT's current buffer position.
3808 Otherwise, use the iterator's base_face_id. */
3810 static int
3811 underlying_face_id (struct it *it)
3813 int face_id = it->base_face_id, i;
3815 eassert (STRINGP (it->string));
3817 for (i = it->sp - 1; i >= 0; --i)
3818 if (NILP (it->stack[i].string))
3819 face_id = it->stack[i].face_id;
3821 return face_id;
3825 /* Compute the face one character before or after the current position
3826 of IT, in the visual order. BEFORE_P non-zero means get the face
3827 in front (to the left in L2R paragraphs, to the right in R2L
3828 paragraphs) of IT's screen position. Value is the ID of the face. */
3830 static int
3831 face_before_or_after_it_pos (struct it *it, int before_p)
3833 int face_id, limit;
3834 ptrdiff_t next_check_charpos;
3835 struct it it_copy;
3836 void *it_copy_data = NULL;
3838 eassert (it->s == NULL);
3840 if (STRINGP (it->string))
3842 ptrdiff_t bufpos, charpos;
3843 int base_face_id;
3845 /* No face change past the end of the string (for the case
3846 we are padding with spaces). No face change before the
3847 string start. */
3848 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3849 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3850 return it->face_id;
3852 if (!it->bidi_p)
3854 /* Set charpos to the position before or after IT's current
3855 position, in the logical order, which in the non-bidi
3856 case is the same as the visual order. */
3857 if (before_p)
3858 charpos = IT_STRING_CHARPOS (*it) - 1;
3859 else if (it->what == IT_COMPOSITION)
3860 /* For composition, we must check the character after the
3861 composition. */
3862 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3863 else
3864 charpos = IT_STRING_CHARPOS (*it) + 1;
3866 else
3868 if (before_p)
3870 /* With bidi iteration, the character before the current
3871 in the visual order cannot be found by simple
3872 iteration, because "reverse" reordering is not
3873 supported. Instead, we need to use the move_it_*
3874 family of functions. */
3875 /* Ignore face changes before the first visible
3876 character on this display line. */
3877 if (it->current_x <= it->first_visible_x)
3878 return it->face_id;
3879 SAVE_IT (it_copy, *it, it_copy_data);
3880 /* Implementation note: Since move_it_in_display_line
3881 works in the iterator geometry, and thinks the first
3882 character is always the leftmost, even in R2L lines,
3883 we don't need to distinguish between the R2L and L2R
3884 cases here. */
3885 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3886 it_copy.current_x - 1, MOVE_TO_X);
3887 charpos = IT_STRING_CHARPOS (it_copy);
3888 RESTORE_IT (it, it, it_copy_data);
3890 else
3892 /* Set charpos to the string position of the character
3893 that comes after IT's current position in the visual
3894 order. */
3895 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3897 it_copy = *it;
3898 while (n--)
3899 bidi_move_to_visually_next (&it_copy.bidi_it);
3901 charpos = it_copy.bidi_it.charpos;
3904 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3906 if (it->current.overlay_string_index >= 0)
3907 bufpos = IT_CHARPOS (*it);
3908 else
3909 bufpos = 0;
3911 base_face_id = underlying_face_id (it);
3913 /* Get the face for ASCII, or unibyte. */
3914 face_id = face_at_string_position (it->w,
3915 it->string,
3916 charpos,
3917 bufpos,
3918 it->region_beg_charpos,
3919 it->region_end_charpos,
3920 &next_check_charpos,
3921 base_face_id, 0);
3923 /* Correct the face for charsets different from ASCII. Do it
3924 for the multibyte case only. The face returned above is
3925 suitable for unibyte text if IT->string is unibyte. */
3926 if (STRING_MULTIBYTE (it->string))
3928 struct text_pos pos1 = string_pos (charpos, it->string);
3929 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3930 int c, len;
3931 struct face *face = FACE_FROM_ID (it->f, face_id);
3933 c = string_char_and_length (p, &len);
3934 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3937 else
3939 struct text_pos pos;
3941 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3942 || (IT_CHARPOS (*it) <= BEGV && before_p))
3943 return it->face_id;
3945 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3946 pos = it->current.pos;
3948 if (!it->bidi_p)
3950 if (before_p)
3951 DEC_TEXT_POS (pos, it->multibyte_p);
3952 else
3954 if (it->what == IT_COMPOSITION)
3956 /* For composition, we must check the position after
3957 the composition. */
3958 pos.charpos += it->cmp_it.nchars;
3959 pos.bytepos += it->len;
3961 else
3962 INC_TEXT_POS (pos, it->multibyte_p);
3965 else
3967 if (before_p)
3969 /* With bidi iteration, the character before the current
3970 in the visual order cannot be found by simple
3971 iteration, because "reverse" reordering is not
3972 supported. Instead, we need to use the move_it_*
3973 family of functions. */
3974 /* Ignore face changes before the first visible
3975 character on this display line. */
3976 if (it->current_x <= it->first_visible_x)
3977 return it->face_id;
3978 SAVE_IT (it_copy, *it, it_copy_data);
3979 /* Implementation note: Since move_it_in_display_line
3980 works in the iterator geometry, and thinks the first
3981 character is always the leftmost, even in R2L lines,
3982 we don't need to distinguish between the R2L and L2R
3983 cases here. */
3984 move_it_in_display_line (&it_copy, ZV,
3985 it_copy.current_x - 1, MOVE_TO_X);
3986 pos = it_copy.current.pos;
3987 RESTORE_IT (it, it, it_copy_data);
3989 else
3991 /* Set charpos to the buffer position of the character
3992 that comes after IT's current position in the visual
3993 order. */
3994 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3996 it_copy = *it;
3997 while (n--)
3998 bidi_move_to_visually_next (&it_copy.bidi_it);
4000 SET_TEXT_POS (pos,
4001 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4004 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4006 /* Determine face for CHARSET_ASCII, or unibyte. */
4007 face_id = face_at_buffer_position (it->w,
4008 CHARPOS (pos),
4009 it->region_beg_charpos,
4010 it->region_end_charpos,
4011 &next_check_charpos,
4012 limit, 0, -1);
4014 /* Correct the face for charsets different from ASCII. Do it
4015 for the multibyte case only. The face returned above is
4016 suitable for unibyte text if current_buffer is unibyte. */
4017 if (it->multibyte_p)
4019 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4020 struct face *face = FACE_FROM_ID (it->f, face_id);
4021 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4025 return face_id;
4030 /***********************************************************************
4031 Invisible text
4032 ***********************************************************************/
4034 /* Set up iterator IT from invisible properties at its current
4035 position. Called from handle_stop. */
4037 static enum prop_handled
4038 handle_invisible_prop (struct it *it)
4040 enum prop_handled handled = HANDLED_NORMALLY;
4042 if (STRINGP (it->string))
4044 Lisp_Object prop, end_charpos, limit, charpos;
4046 /* Get the value of the invisible text property at the
4047 current position. Value will be nil if there is no such
4048 property. */
4049 charpos = make_number (IT_STRING_CHARPOS (*it));
4050 prop = Fget_text_property (charpos, Qinvisible, it->string);
4052 if (!NILP (prop)
4053 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4055 ptrdiff_t endpos;
4057 handled = HANDLED_RECOMPUTE_PROPS;
4059 /* Get the position at which the next change of the
4060 invisible text property can be found in IT->string.
4061 Value will be nil if the property value is the same for
4062 all the rest of IT->string. */
4063 XSETINT (limit, SCHARS (it->string));
4064 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4065 it->string, limit);
4067 /* Text at current position is invisible. The next
4068 change in the property is at position end_charpos.
4069 Move IT's current position to that position. */
4070 if (INTEGERP (end_charpos)
4071 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
4073 struct text_pos old;
4074 ptrdiff_t oldpos;
4076 old = it->current.string_pos;
4077 oldpos = CHARPOS (old);
4078 if (it->bidi_p)
4080 if (it->bidi_it.first_elt
4081 && it->bidi_it.charpos < SCHARS (it->string))
4082 bidi_paragraph_init (it->paragraph_embedding,
4083 &it->bidi_it, 1);
4084 /* Bidi-iterate out of the invisible text. */
4087 bidi_move_to_visually_next (&it->bidi_it);
4089 while (oldpos <= it->bidi_it.charpos
4090 && it->bidi_it.charpos < endpos);
4092 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4093 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4094 if (IT_CHARPOS (*it) >= endpos)
4095 it->prev_stop = endpos;
4097 else
4099 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4100 compute_string_pos (&it->current.string_pos, old, it->string);
4103 else
4105 /* The rest of the string is invisible. If this is an
4106 overlay string, proceed with the next overlay string
4107 or whatever comes and return a character from there. */
4108 if (it->current.overlay_string_index >= 0)
4110 next_overlay_string (it);
4111 /* Don't check for overlay strings when we just
4112 finished processing them. */
4113 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4115 else
4117 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4118 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4123 else
4125 int invis_p;
4126 ptrdiff_t newpos, next_stop, start_charpos, tem;
4127 Lisp_Object pos, prop, overlay;
4129 /* First of all, is there invisible text at this position? */
4130 tem = start_charpos = IT_CHARPOS (*it);
4131 pos = make_number (tem);
4132 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4133 &overlay);
4134 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4136 /* If we are on invisible text, skip over it. */
4137 if (invis_p && start_charpos < it->end_charpos)
4139 /* Record whether we have to display an ellipsis for the
4140 invisible text. */
4141 int display_ellipsis_p = invis_p == 2;
4143 handled = HANDLED_RECOMPUTE_PROPS;
4145 /* Loop skipping over invisible text. The loop is left at
4146 ZV or with IT on the first char being visible again. */
4149 /* Try to skip some invisible text. Return value is the
4150 position reached which can be equal to where we start
4151 if there is nothing invisible there. This skips both
4152 over invisible text properties and overlays with
4153 invisible property. */
4154 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4156 /* If we skipped nothing at all we weren't at invisible
4157 text in the first place. If everything to the end of
4158 the buffer was skipped, end the loop. */
4159 if (newpos == tem || newpos >= ZV)
4160 invis_p = 0;
4161 else
4163 /* We skipped some characters but not necessarily
4164 all there are. Check if we ended up on visible
4165 text. Fget_char_property returns the property of
4166 the char before the given position, i.e. if we
4167 get invis_p = 0, this means that the char at
4168 newpos is visible. */
4169 pos = make_number (newpos);
4170 prop = Fget_char_property (pos, Qinvisible, it->window);
4171 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4174 /* If we ended up on invisible text, proceed to
4175 skip starting with next_stop. */
4176 if (invis_p)
4177 tem = next_stop;
4179 /* If there are adjacent invisible texts, don't lose the
4180 second one's ellipsis. */
4181 if (invis_p == 2)
4182 display_ellipsis_p = 1;
4184 while (invis_p);
4186 /* The position newpos is now either ZV or on visible text. */
4187 if (it->bidi_p)
4189 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4190 int on_newline =
4191 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4192 int after_newline =
4193 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4195 /* If the invisible text ends on a newline or on a
4196 character after a newline, we can avoid the costly,
4197 character by character, bidi iteration to NEWPOS, and
4198 instead simply reseat the iterator there. That's
4199 because all bidi reordering information is tossed at
4200 the newline. This is a big win for modes that hide
4201 complete lines, like Outline, Org, etc. */
4202 if (on_newline || after_newline)
4204 struct text_pos tpos;
4205 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4207 SET_TEXT_POS (tpos, newpos, bpos);
4208 reseat_1 (it, tpos, 0);
4209 /* If we reseat on a newline/ZV, we need to prep the
4210 bidi iterator for advancing to the next character
4211 after the newline/EOB, keeping the current paragraph
4212 direction (so that PRODUCE_GLYPHS does TRT wrt
4213 prepending/appending glyphs to a glyph row). */
4214 if (on_newline)
4216 it->bidi_it.first_elt = 0;
4217 it->bidi_it.paragraph_dir = pdir;
4218 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4219 it->bidi_it.nchars = 1;
4220 it->bidi_it.ch_len = 1;
4223 else /* Must use the slow method. */
4225 /* With bidi iteration, the region of invisible text
4226 could start and/or end in the middle of a
4227 non-base embedding level. Therefore, we need to
4228 skip invisible text using the bidi iterator,
4229 starting at IT's current position, until we find
4230 ourselves outside of the invisible text.
4231 Skipping invisible text _after_ bidi iteration
4232 avoids affecting the visual order of the
4233 displayed text when invisible properties are
4234 added or removed. */
4235 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4237 /* If we were `reseat'ed to a new paragraph,
4238 determine the paragraph base direction. We
4239 need to do it now because
4240 next_element_from_buffer may not have a
4241 chance to do it, if we are going to skip any
4242 text at the beginning, which resets the
4243 FIRST_ELT flag. */
4244 bidi_paragraph_init (it->paragraph_embedding,
4245 &it->bidi_it, 1);
4249 bidi_move_to_visually_next (&it->bidi_it);
4251 while (it->stop_charpos <= it->bidi_it.charpos
4252 && it->bidi_it.charpos < newpos);
4253 IT_CHARPOS (*it) = it->bidi_it.charpos;
4254 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4255 /* If we overstepped NEWPOS, record its position in
4256 the iterator, so that we skip invisible text if
4257 later the bidi iteration lands us in the
4258 invisible region again. */
4259 if (IT_CHARPOS (*it) >= newpos)
4260 it->prev_stop = newpos;
4263 else
4265 IT_CHARPOS (*it) = newpos;
4266 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4269 /* If there are before-strings at the start of invisible
4270 text, and the text is invisible because of a text
4271 property, arrange to show before-strings because 20.x did
4272 it that way. (If the text is invisible because of an
4273 overlay property instead of a text property, this is
4274 already handled in the overlay code.) */
4275 if (NILP (overlay)
4276 && get_overlay_strings (it, it->stop_charpos))
4278 handled = HANDLED_RECOMPUTE_PROPS;
4279 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4281 else if (display_ellipsis_p)
4283 /* Make sure that the glyphs of the ellipsis will get
4284 correct `charpos' values. If we would not update
4285 it->position here, the glyphs would belong to the
4286 last visible character _before_ the invisible
4287 text, which confuses `set_cursor_from_row'.
4289 We use the last invisible position instead of the
4290 first because this way the cursor is always drawn on
4291 the first "." of the ellipsis, whenever PT is inside
4292 the invisible text. Otherwise the cursor would be
4293 placed _after_ the ellipsis when the point is after the
4294 first invisible character. */
4295 if (!STRINGP (it->object))
4297 it->position.charpos = newpos - 1;
4298 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4300 it->ellipsis_p = 1;
4301 /* Let the ellipsis display before
4302 considering any properties of the following char.
4303 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4304 handled = HANDLED_RETURN;
4309 return handled;
4313 /* Make iterator IT return `...' next.
4314 Replaces LEN characters from buffer. */
4316 static void
4317 setup_for_ellipsis (struct it *it, int len)
4319 /* Use the display table definition for `...'. Invalid glyphs
4320 will be handled by the method returning elements from dpvec. */
4321 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4323 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4324 it->dpvec = v->contents;
4325 it->dpend = v->contents + v->header.size;
4327 else
4329 /* Default `...'. */
4330 it->dpvec = default_invis_vector;
4331 it->dpend = default_invis_vector + 3;
4334 it->dpvec_char_len = len;
4335 it->current.dpvec_index = 0;
4336 it->dpvec_face_id = -1;
4338 /* Remember the current face id in case glyphs specify faces.
4339 IT's face is restored in set_iterator_to_next.
4340 saved_face_id was set to preceding char's face in handle_stop. */
4341 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4342 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4344 it->method = GET_FROM_DISPLAY_VECTOR;
4345 it->ellipsis_p = 1;
4350 /***********************************************************************
4351 'display' property
4352 ***********************************************************************/
4354 /* Set up iterator IT from `display' property at its current position.
4355 Called from handle_stop.
4356 We return HANDLED_RETURN if some part of the display property
4357 overrides the display of the buffer text itself.
4358 Otherwise we return HANDLED_NORMALLY. */
4360 static enum prop_handled
4361 handle_display_prop (struct it *it)
4363 Lisp_Object propval, object, overlay;
4364 struct text_pos *position;
4365 ptrdiff_t bufpos;
4366 /* Nonzero if some property replaces the display of the text itself. */
4367 int display_replaced_p = 0;
4369 if (STRINGP (it->string))
4371 object = it->string;
4372 position = &it->current.string_pos;
4373 bufpos = CHARPOS (it->current.pos);
4375 else
4377 XSETWINDOW (object, it->w);
4378 position = &it->current.pos;
4379 bufpos = CHARPOS (*position);
4382 /* Reset those iterator values set from display property values. */
4383 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4384 it->space_width = Qnil;
4385 it->font_height = Qnil;
4386 it->voffset = 0;
4388 /* We don't support recursive `display' properties, i.e. string
4389 values that have a string `display' property, that have a string
4390 `display' property etc. */
4391 if (!it->string_from_display_prop_p)
4392 it->area = TEXT_AREA;
4394 propval = get_char_property_and_overlay (make_number (position->charpos),
4395 Qdisplay, object, &overlay);
4396 if (NILP (propval))
4397 return HANDLED_NORMALLY;
4398 /* Now OVERLAY is the overlay that gave us this property, or nil
4399 if it was a text property. */
4401 if (!STRINGP (it->string))
4402 object = it->w->buffer;
4404 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4405 position, bufpos,
4406 FRAME_WINDOW_P (it->f));
4408 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4411 /* Subroutine of handle_display_prop. Returns non-zero if the display
4412 specification in SPEC is a replacing specification, i.e. it would
4413 replace the text covered by `display' property with something else,
4414 such as an image or a display string. If SPEC includes any kind or
4415 `(space ...) specification, the value is 2; this is used by
4416 compute_display_string_pos, which see.
4418 See handle_single_display_spec for documentation of arguments.
4419 frame_window_p is non-zero if the window being redisplayed is on a
4420 GUI frame; this argument is used only if IT is NULL, see below.
4422 IT can be NULL, if this is called by the bidi reordering code
4423 through compute_display_string_pos, which see. In that case, this
4424 function only examines SPEC, but does not otherwise "handle" it, in
4425 the sense that it doesn't set up members of IT from the display
4426 spec. */
4427 static int
4428 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4429 Lisp_Object overlay, struct text_pos *position,
4430 ptrdiff_t bufpos, int frame_window_p)
4432 int replacing_p = 0;
4433 int rv;
4435 if (CONSP (spec)
4436 /* Simple specifications. */
4437 && !EQ (XCAR (spec), Qimage)
4438 && !EQ (XCAR (spec), Qspace)
4439 && !EQ (XCAR (spec), Qwhen)
4440 && !EQ (XCAR (spec), Qslice)
4441 && !EQ (XCAR (spec), Qspace_width)
4442 && !EQ (XCAR (spec), Qheight)
4443 && !EQ (XCAR (spec), Qraise)
4444 /* Marginal area specifications. */
4445 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4446 && !EQ (XCAR (spec), Qleft_fringe)
4447 && !EQ (XCAR (spec), Qright_fringe)
4448 && !NILP (XCAR (spec)))
4450 for (; CONSP (spec); spec = XCDR (spec))
4452 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4453 overlay, position, bufpos,
4454 replacing_p, frame_window_p)))
4456 replacing_p = rv;
4457 /* If some text in a string is replaced, `position' no
4458 longer points to the position of `object'. */
4459 if (!it || STRINGP (object))
4460 break;
4464 else if (VECTORP (spec))
4466 ptrdiff_t i;
4467 for (i = 0; i < ASIZE (spec); ++i)
4468 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4469 overlay, position, bufpos,
4470 replacing_p, frame_window_p)))
4472 replacing_p = rv;
4473 /* If some text in a string is replaced, `position' no
4474 longer points to the position of `object'. */
4475 if (!it || STRINGP (object))
4476 break;
4479 else
4481 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4482 position, bufpos, 0,
4483 frame_window_p)))
4484 replacing_p = rv;
4487 return replacing_p;
4490 /* Value is the position of the end of the `display' property starting
4491 at START_POS in OBJECT. */
4493 static struct text_pos
4494 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4496 Lisp_Object end;
4497 struct text_pos end_pos;
4499 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4500 Qdisplay, object, Qnil);
4501 CHARPOS (end_pos) = XFASTINT (end);
4502 if (STRINGP (object))
4503 compute_string_pos (&end_pos, start_pos, it->string);
4504 else
4505 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4507 return end_pos;
4511 /* Set up IT from a single `display' property specification SPEC. OBJECT
4512 is the object in which the `display' property was found. *POSITION
4513 is the position in OBJECT at which the `display' property was found.
4514 BUFPOS is the buffer position of OBJECT (different from POSITION if
4515 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4516 previously saw a display specification which already replaced text
4517 display with something else, for example an image; we ignore such
4518 properties after the first one has been processed.
4520 OVERLAY is the overlay this `display' property came from,
4521 or nil if it was a text property.
4523 If SPEC is a `space' or `image' specification, and in some other
4524 cases too, set *POSITION to the position where the `display'
4525 property ends.
4527 If IT is NULL, only examine the property specification in SPEC, but
4528 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4529 is intended to be displayed in a window on a GUI frame.
4531 Value is non-zero if something was found which replaces the display
4532 of buffer or string text. */
4534 static int
4535 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4536 Lisp_Object overlay, struct text_pos *position,
4537 ptrdiff_t bufpos, int display_replaced_p,
4538 int frame_window_p)
4540 Lisp_Object form;
4541 Lisp_Object location, value;
4542 struct text_pos start_pos = *position;
4543 int valid_p;
4545 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4546 If the result is non-nil, use VALUE instead of SPEC. */
4547 form = Qt;
4548 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4550 spec = XCDR (spec);
4551 if (!CONSP (spec))
4552 return 0;
4553 form = XCAR (spec);
4554 spec = XCDR (spec);
4557 if (!NILP (form) && !EQ (form, Qt))
4559 ptrdiff_t count = SPECPDL_INDEX ();
4560 struct gcpro gcpro1;
4562 /* Bind `object' to the object having the `display' property, a
4563 buffer or string. Bind `position' to the position in the
4564 object where the property was found, and `buffer-position'
4565 to the current position in the buffer. */
4567 if (NILP (object))
4568 XSETBUFFER (object, current_buffer);
4569 specbind (Qobject, object);
4570 specbind (Qposition, make_number (CHARPOS (*position)));
4571 specbind (Qbuffer_position, make_number (bufpos));
4572 GCPRO1 (form);
4573 form = safe_eval (form);
4574 UNGCPRO;
4575 unbind_to (count, Qnil);
4578 if (NILP (form))
4579 return 0;
4581 /* Handle `(height HEIGHT)' specifications. */
4582 if (CONSP (spec)
4583 && EQ (XCAR (spec), Qheight)
4584 && CONSP (XCDR (spec)))
4586 if (it)
4588 if (!FRAME_WINDOW_P (it->f))
4589 return 0;
4591 it->font_height = XCAR (XCDR (spec));
4592 if (!NILP (it->font_height))
4594 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4595 int new_height = -1;
4597 if (CONSP (it->font_height)
4598 && (EQ (XCAR (it->font_height), Qplus)
4599 || EQ (XCAR (it->font_height), Qminus))
4600 && CONSP (XCDR (it->font_height))
4601 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4603 /* `(+ N)' or `(- N)' where N is an integer. */
4604 int steps = XINT (XCAR (XCDR (it->font_height)));
4605 if (EQ (XCAR (it->font_height), Qplus))
4606 steps = - steps;
4607 it->face_id = smaller_face (it->f, it->face_id, steps);
4609 else if (FUNCTIONP (it->font_height))
4611 /* Call function with current height as argument.
4612 Value is the new height. */
4613 Lisp_Object height;
4614 height = safe_call1 (it->font_height,
4615 face->lface[LFACE_HEIGHT_INDEX]);
4616 if (NUMBERP (height))
4617 new_height = XFLOATINT (height);
4619 else if (NUMBERP (it->font_height))
4621 /* Value is a multiple of the canonical char height. */
4622 struct face *f;
4624 f = FACE_FROM_ID (it->f,
4625 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4626 new_height = (XFLOATINT (it->font_height)
4627 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4629 else
4631 /* Evaluate IT->font_height with `height' bound to the
4632 current specified height to get the new height. */
4633 ptrdiff_t count = SPECPDL_INDEX ();
4635 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4636 value = safe_eval (it->font_height);
4637 unbind_to (count, Qnil);
4639 if (NUMBERP (value))
4640 new_height = XFLOATINT (value);
4643 if (new_height > 0)
4644 it->face_id = face_with_height (it->f, it->face_id, new_height);
4648 return 0;
4651 /* Handle `(space-width WIDTH)'. */
4652 if (CONSP (spec)
4653 && EQ (XCAR (spec), Qspace_width)
4654 && CONSP (XCDR (spec)))
4656 if (it)
4658 if (!FRAME_WINDOW_P (it->f))
4659 return 0;
4661 value = XCAR (XCDR (spec));
4662 if (NUMBERP (value) && XFLOATINT (value) > 0)
4663 it->space_width = value;
4666 return 0;
4669 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4670 if (CONSP (spec)
4671 && EQ (XCAR (spec), Qslice))
4673 Lisp_Object tem;
4675 if (it)
4677 if (!FRAME_WINDOW_P (it->f))
4678 return 0;
4680 if (tem = XCDR (spec), CONSP (tem))
4682 it->slice.x = XCAR (tem);
4683 if (tem = XCDR (tem), CONSP (tem))
4685 it->slice.y = XCAR (tem);
4686 if (tem = XCDR (tem), CONSP (tem))
4688 it->slice.width = XCAR (tem);
4689 if (tem = XCDR (tem), CONSP (tem))
4690 it->slice.height = XCAR (tem);
4696 return 0;
4699 /* Handle `(raise FACTOR)'. */
4700 if (CONSP (spec)
4701 && EQ (XCAR (spec), Qraise)
4702 && CONSP (XCDR (spec)))
4704 if (it)
4706 if (!FRAME_WINDOW_P (it->f))
4707 return 0;
4709 #ifdef HAVE_WINDOW_SYSTEM
4710 value = XCAR (XCDR (spec));
4711 if (NUMBERP (value))
4713 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4714 it->voffset = - (XFLOATINT (value)
4715 * (FONT_HEIGHT (face->font)));
4717 #endif /* HAVE_WINDOW_SYSTEM */
4720 return 0;
4723 /* Don't handle the other kinds of display specifications
4724 inside a string that we got from a `display' property. */
4725 if (it && it->string_from_display_prop_p)
4726 return 0;
4728 /* Characters having this form of property are not displayed, so
4729 we have to find the end of the property. */
4730 if (it)
4732 start_pos = *position;
4733 *position = display_prop_end (it, object, start_pos);
4735 value = Qnil;
4737 /* Stop the scan at that end position--we assume that all
4738 text properties change there. */
4739 if (it)
4740 it->stop_charpos = position->charpos;
4742 /* Handle `(left-fringe BITMAP [FACE])'
4743 and `(right-fringe BITMAP [FACE])'. */
4744 if (CONSP (spec)
4745 && (EQ (XCAR (spec), Qleft_fringe)
4746 || EQ (XCAR (spec), Qright_fringe))
4747 && CONSP (XCDR (spec)))
4749 int fringe_bitmap;
4751 if (it)
4753 if (!FRAME_WINDOW_P (it->f))
4754 /* If we return here, POSITION has been advanced
4755 across the text with this property. */
4757 /* Synchronize the bidi iterator with POSITION. This is
4758 needed because we are not going to push the iterator
4759 on behalf of this display property, so there will be
4760 no pop_it call to do this synchronization for us. */
4761 if (it->bidi_p)
4763 it->position = *position;
4764 iterate_out_of_display_property (it);
4765 *position = it->position;
4767 return 1;
4770 else if (!frame_window_p)
4771 return 1;
4773 #ifdef HAVE_WINDOW_SYSTEM
4774 value = XCAR (XCDR (spec));
4775 if (!SYMBOLP (value)
4776 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4777 /* If we return here, POSITION has been advanced
4778 across the text with this property. */
4780 if (it && it->bidi_p)
4782 it->position = *position;
4783 iterate_out_of_display_property (it);
4784 *position = it->position;
4786 return 1;
4789 if (it)
4791 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4793 if (CONSP (XCDR (XCDR (spec))))
4795 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4796 int face_id2 = lookup_derived_face (it->f, face_name,
4797 FRINGE_FACE_ID, 0);
4798 if (face_id2 >= 0)
4799 face_id = face_id2;
4802 /* Save current settings of IT so that we can restore them
4803 when we are finished with the glyph property value. */
4804 push_it (it, position);
4806 it->area = TEXT_AREA;
4807 it->what = IT_IMAGE;
4808 it->image_id = -1; /* no image */
4809 it->position = start_pos;
4810 it->object = NILP (object) ? it->w->buffer : object;
4811 it->method = GET_FROM_IMAGE;
4812 it->from_overlay = Qnil;
4813 it->face_id = face_id;
4814 it->from_disp_prop_p = 1;
4816 /* Say that we haven't consumed the characters with
4817 `display' property yet. The call to pop_it in
4818 set_iterator_to_next will clean this up. */
4819 *position = start_pos;
4821 if (EQ (XCAR (spec), Qleft_fringe))
4823 it->left_user_fringe_bitmap = fringe_bitmap;
4824 it->left_user_fringe_face_id = face_id;
4826 else
4828 it->right_user_fringe_bitmap = fringe_bitmap;
4829 it->right_user_fringe_face_id = face_id;
4832 #endif /* HAVE_WINDOW_SYSTEM */
4833 return 1;
4836 /* Prepare to handle `((margin left-margin) ...)',
4837 `((margin right-margin) ...)' and `((margin nil) ...)'
4838 prefixes for display specifications. */
4839 location = Qunbound;
4840 if (CONSP (spec) && CONSP (XCAR (spec)))
4842 Lisp_Object tem;
4844 value = XCDR (spec);
4845 if (CONSP (value))
4846 value = XCAR (value);
4848 tem = XCAR (spec);
4849 if (EQ (XCAR (tem), Qmargin)
4850 && (tem = XCDR (tem),
4851 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4852 (NILP (tem)
4853 || EQ (tem, Qleft_margin)
4854 || EQ (tem, Qright_margin))))
4855 location = tem;
4858 if (EQ (location, Qunbound))
4860 location = Qnil;
4861 value = spec;
4864 /* After this point, VALUE is the property after any
4865 margin prefix has been stripped. It must be a string,
4866 an image specification, or `(space ...)'.
4868 LOCATION specifies where to display: `left-margin',
4869 `right-margin' or nil. */
4871 valid_p = (STRINGP (value)
4872 #ifdef HAVE_WINDOW_SYSTEM
4873 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4874 && valid_image_p (value))
4875 #endif /* not HAVE_WINDOW_SYSTEM */
4876 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4878 if (valid_p && !display_replaced_p)
4880 int retval = 1;
4882 if (!it)
4884 /* Callers need to know whether the display spec is any kind
4885 of `(space ...)' spec that is about to affect text-area
4886 display. */
4887 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4888 retval = 2;
4889 return retval;
4892 /* Save current settings of IT so that we can restore them
4893 when we are finished with the glyph property value. */
4894 push_it (it, position);
4895 it->from_overlay = overlay;
4896 it->from_disp_prop_p = 1;
4898 if (NILP (location))
4899 it->area = TEXT_AREA;
4900 else if (EQ (location, Qleft_margin))
4901 it->area = LEFT_MARGIN_AREA;
4902 else
4903 it->area = RIGHT_MARGIN_AREA;
4905 if (STRINGP (value))
4907 it->string = value;
4908 it->multibyte_p = STRING_MULTIBYTE (it->string);
4909 it->current.overlay_string_index = -1;
4910 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4911 it->end_charpos = it->string_nchars = SCHARS (it->string);
4912 it->method = GET_FROM_STRING;
4913 it->stop_charpos = 0;
4914 it->prev_stop = 0;
4915 it->base_level_stop = 0;
4916 it->string_from_display_prop_p = 1;
4917 /* Say that we haven't consumed the characters with
4918 `display' property yet. The call to pop_it in
4919 set_iterator_to_next will clean this up. */
4920 if (BUFFERP (object))
4921 *position = start_pos;
4923 /* Force paragraph direction to be that of the parent
4924 object. If the parent object's paragraph direction is
4925 not yet determined, default to L2R. */
4926 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4927 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4928 else
4929 it->paragraph_embedding = L2R;
4931 /* Set up the bidi iterator for this display string. */
4932 if (it->bidi_p)
4934 it->bidi_it.string.lstring = it->string;
4935 it->bidi_it.string.s = NULL;
4936 it->bidi_it.string.schars = it->end_charpos;
4937 it->bidi_it.string.bufpos = bufpos;
4938 it->bidi_it.string.from_disp_str = 1;
4939 it->bidi_it.string.unibyte = !it->multibyte_p;
4940 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4943 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4945 it->method = GET_FROM_STRETCH;
4946 it->object = value;
4947 *position = it->position = start_pos;
4948 retval = 1 + (it->area == TEXT_AREA);
4950 #ifdef HAVE_WINDOW_SYSTEM
4951 else
4953 it->what = IT_IMAGE;
4954 it->image_id = lookup_image (it->f, value);
4955 it->position = start_pos;
4956 it->object = NILP (object) ? it->w->buffer : object;
4957 it->method = GET_FROM_IMAGE;
4959 /* Say that we haven't consumed the characters with
4960 `display' property yet. The call to pop_it in
4961 set_iterator_to_next will clean this up. */
4962 *position = start_pos;
4964 #endif /* HAVE_WINDOW_SYSTEM */
4966 return retval;
4969 /* Invalid property or property not supported. Restore
4970 POSITION to what it was before. */
4971 *position = start_pos;
4972 return 0;
4975 /* Check if PROP is a display property value whose text should be
4976 treated as intangible. OVERLAY is the overlay from which PROP
4977 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4978 specify the buffer position covered by PROP. */
4981 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4982 ptrdiff_t charpos, ptrdiff_t bytepos)
4984 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4985 struct text_pos position;
4987 SET_TEXT_POS (position, charpos, bytepos);
4988 return handle_display_spec (NULL, prop, Qnil, overlay,
4989 &position, charpos, frame_window_p);
4993 /* Return 1 if PROP is a display sub-property value containing STRING.
4995 Implementation note: this and the following function are really
4996 special cases of handle_display_spec and
4997 handle_single_display_spec, and should ideally use the same code.
4998 Until they do, these two pairs must be consistent and must be
4999 modified in sync. */
5001 static int
5002 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5004 if (EQ (string, prop))
5005 return 1;
5007 /* Skip over `when FORM'. */
5008 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5010 prop = XCDR (prop);
5011 if (!CONSP (prop))
5012 return 0;
5013 /* Actually, the condition following `when' should be eval'ed,
5014 like handle_single_display_spec does, and we should return
5015 zero if it evaluates to nil. However, this function is
5016 called only when the buffer was already displayed and some
5017 glyph in the glyph matrix was found to come from a display
5018 string. Therefore, the condition was already evaluated, and
5019 the result was non-nil, otherwise the display string wouldn't
5020 have been displayed and we would have never been called for
5021 this property. Thus, we can skip the evaluation and assume
5022 its result is non-nil. */
5023 prop = XCDR (prop);
5026 if (CONSP (prop))
5027 /* Skip over `margin LOCATION'. */
5028 if (EQ (XCAR (prop), Qmargin))
5030 prop = XCDR (prop);
5031 if (!CONSP (prop))
5032 return 0;
5034 prop = XCDR (prop);
5035 if (!CONSP (prop))
5036 return 0;
5039 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5043 /* Return 1 if STRING appears in the `display' property PROP. */
5045 static int
5046 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5048 if (CONSP (prop)
5049 && !EQ (XCAR (prop), Qwhen)
5050 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5052 /* A list of sub-properties. */
5053 while (CONSP (prop))
5055 if (single_display_spec_string_p (XCAR (prop), string))
5056 return 1;
5057 prop = XCDR (prop);
5060 else if (VECTORP (prop))
5062 /* A vector of sub-properties. */
5063 ptrdiff_t i;
5064 for (i = 0; i < ASIZE (prop); ++i)
5065 if (single_display_spec_string_p (AREF (prop, i), string))
5066 return 1;
5068 else
5069 return single_display_spec_string_p (prop, string);
5071 return 0;
5074 /* Look for STRING in overlays and text properties in the current
5075 buffer, between character positions FROM and TO (excluding TO).
5076 BACK_P non-zero means look back (in this case, TO is supposed to be
5077 less than FROM).
5078 Value is the first character position where STRING was found, or
5079 zero if it wasn't found before hitting TO.
5081 This function may only use code that doesn't eval because it is
5082 called asynchronously from note_mouse_highlight. */
5084 static ptrdiff_t
5085 string_buffer_position_lim (Lisp_Object string,
5086 ptrdiff_t from, ptrdiff_t to, int back_p)
5088 Lisp_Object limit, prop, pos;
5089 int found = 0;
5091 pos = make_number (max (from, BEGV));
5093 if (!back_p) /* looking forward */
5095 limit = make_number (min (to, ZV));
5096 while (!found && !EQ (pos, limit))
5098 prop = Fget_char_property (pos, Qdisplay, Qnil);
5099 if (!NILP (prop) && display_prop_string_p (prop, string))
5100 found = 1;
5101 else
5102 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5103 limit);
5106 else /* looking back */
5108 limit = make_number (max (to, BEGV));
5109 while (!found && !EQ (pos, limit))
5111 prop = Fget_char_property (pos, Qdisplay, Qnil);
5112 if (!NILP (prop) && display_prop_string_p (prop, string))
5113 found = 1;
5114 else
5115 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5116 limit);
5120 return found ? XINT (pos) : 0;
5123 /* Determine which buffer position in current buffer STRING comes from.
5124 AROUND_CHARPOS is an approximate position where it could come from.
5125 Value is the buffer position or 0 if it couldn't be determined.
5127 This function is necessary because we don't record buffer positions
5128 in glyphs generated from strings (to keep struct glyph small).
5129 This function may only use code that doesn't eval because it is
5130 called asynchronously from note_mouse_highlight. */
5132 static ptrdiff_t
5133 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5135 const int MAX_DISTANCE = 1000;
5136 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5137 around_charpos + MAX_DISTANCE,
5140 if (!found)
5141 found = string_buffer_position_lim (string, around_charpos,
5142 around_charpos - MAX_DISTANCE, 1);
5143 return found;
5148 /***********************************************************************
5149 `composition' property
5150 ***********************************************************************/
5152 /* Set up iterator IT from `composition' property at its current
5153 position. Called from handle_stop. */
5155 static enum prop_handled
5156 handle_composition_prop (struct it *it)
5158 Lisp_Object prop, string;
5159 ptrdiff_t pos, pos_byte, start, end;
5161 if (STRINGP (it->string))
5163 unsigned char *s;
5165 pos = IT_STRING_CHARPOS (*it);
5166 pos_byte = IT_STRING_BYTEPOS (*it);
5167 string = it->string;
5168 s = SDATA (string) + pos_byte;
5169 it->c = STRING_CHAR (s);
5171 else
5173 pos = IT_CHARPOS (*it);
5174 pos_byte = IT_BYTEPOS (*it);
5175 string = Qnil;
5176 it->c = FETCH_CHAR (pos_byte);
5179 /* If there's a valid composition and point is not inside of the
5180 composition (in the case that the composition is from the current
5181 buffer), draw a glyph composed from the composition components. */
5182 if (find_composition (pos, -1, &start, &end, &prop, string)
5183 && COMPOSITION_VALID_P (start, end, prop)
5184 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5186 if (start < pos)
5187 /* As we can't handle this situation (perhaps font-lock added
5188 a new composition), we just return here hoping that next
5189 redisplay will detect this composition much earlier. */
5190 return HANDLED_NORMALLY;
5191 if (start != pos)
5193 if (STRINGP (it->string))
5194 pos_byte = string_char_to_byte (it->string, start);
5195 else
5196 pos_byte = CHAR_TO_BYTE (start);
5198 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5199 prop, string);
5201 if (it->cmp_it.id >= 0)
5203 it->cmp_it.ch = -1;
5204 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5205 it->cmp_it.nglyphs = -1;
5209 return HANDLED_NORMALLY;
5214 /***********************************************************************
5215 Overlay strings
5216 ***********************************************************************/
5218 /* The following structure is used to record overlay strings for
5219 later sorting in load_overlay_strings. */
5221 struct overlay_entry
5223 Lisp_Object overlay;
5224 Lisp_Object string;
5225 EMACS_INT priority;
5226 int after_string_p;
5230 /* Set up iterator IT from overlay strings at its current position.
5231 Called from handle_stop. */
5233 static enum prop_handled
5234 handle_overlay_change (struct it *it)
5236 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5237 return HANDLED_RECOMPUTE_PROPS;
5238 else
5239 return HANDLED_NORMALLY;
5243 /* Set up the next overlay string for delivery by IT, if there is an
5244 overlay string to deliver. Called by set_iterator_to_next when the
5245 end of the current overlay string is reached. If there are more
5246 overlay strings to display, IT->string and
5247 IT->current.overlay_string_index are set appropriately here.
5248 Otherwise IT->string is set to nil. */
5250 static void
5251 next_overlay_string (struct it *it)
5253 ++it->current.overlay_string_index;
5254 if (it->current.overlay_string_index == it->n_overlay_strings)
5256 /* No more overlay strings. Restore IT's settings to what
5257 they were before overlay strings were processed, and
5258 continue to deliver from current_buffer. */
5260 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5261 pop_it (it);
5262 eassert (it->sp > 0
5263 || (NILP (it->string)
5264 && it->method == GET_FROM_BUFFER
5265 && it->stop_charpos >= BEGV
5266 && it->stop_charpos <= it->end_charpos));
5267 it->current.overlay_string_index = -1;
5268 it->n_overlay_strings = 0;
5269 it->overlay_strings_charpos = -1;
5270 /* If there's an empty display string on the stack, pop the
5271 stack, to resync the bidi iterator with IT's position. Such
5272 empty strings are pushed onto the stack in
5273 get_overlay_strings_1. */
5274 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5275 pop_it (it);
5277 /* If we're at the end of the buffer, record that we have
5278 processed the overlay strings there already, so that
5279 next_element_from_buffer doesn't try it again. */
5280 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5281 it->overlay_strings_at_end_processed_p = 1;
5283 else
5285 /* There are more overlay strings to process. If
5286 IT->current.overlay_string_index has advanced to a position
5287 where we must load IT->overlay_strings with more strings, do
5288 it. We must load at the IT->overlay_strings_charpos where
5289 IT->n_overlay_strings was originally computed; when invisible
5290 text is present, this might not be IT_CHARPOS (Bug#7016). */
5291 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5293 if (it->current.overlay_string_index && i == 0)
5294 load_overlay_strings (it, it->overlay_strings_charpos);
5296 /* Initialize IT to deliver display elements from the overlay
5297 string. */
5298 it->string = it->overlay_strings[i];
5299 it->multibyte_p = STRING_MULTIBYTE (it->string);
5300 SET_TEXT_POS (it->current.string_pos, 0, 0);
5301 it->method = GET_FROM_STRING;
5302 it->stop_charpos = 0;
5303 if (it->cmp_it.stop_pos >= 0)
5304 it->cmp_it.stop_pos = 0;
5305 it->prev_stop = 0;
5306 it->base_level_stop = 0;
5308 /* Set up the bidi iterator for this overlay string. */
5309 if (it->bidi_p)
5311 it->bidi_it.string.lstring = it->string;
5312 it->bidi_it.string.s = NULL;
5313 it->bidi_it.string.schars = SCHARS (it->string);
5314 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5315 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5316 it->bidi_it.string.unibyte = !it->multibyte_p;
5317 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5321 CHECK_IT (it);
5325 /* Compare two overlay_entry structures E1 and E2. Used as a
5326 comparison function for qsort in load_overlay_strings. Overlay
5327 strings for the same position are sorted so that
5329 1. All after-strings come in front of before-strings, except
5330 when they come from the same overlay.
5332 2. Within after-strings, strings are sorted so that overlay strings
5333 from overlays with higher priorities come first.
5335 2. Within before-strings, strings are sorted so that overlay
5336 strings from overlays with higher priorities come last.
5338 Value is analogous to strcmp. */
5341 static int
5342 compare_overlay_entries (const void *e1, const void *e2)
5344 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5345 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5346 int result;
5348 if (entry1->after_string_p != entry2->after_string_p)
5350 /* Let after-strings appear in front of before-strings if
5351 they come from different overlays. */
5352 if (EQ (entry1->overlay, entry2->overlay))
5353 result = entry1->after_string_p ? 1 : -1;
5354 else
5355 result = entry1->after_string_p ? -1 : 1;
5357 else if (entry1->priority != entry2->priority)
5359 if (entry1->after_string_p)
5360 /* After-strings sorted in order of decreasing priority. */
5361 result = entry2->priority < entry1->priority ? -1 : 1;
5362 else
5363 /* Before-strings sorted in order of increasing priority. */
5364 result = entry1->priority < entry2->priority ? -1 : 1;
5366 else
5367 result = 0;
5369 return result;
5373 /* Load the vector IT->overlay_strings with overlay strings from IT's
5374 current buffer position, or from CHARPOS if that is > 0. Set
5375 IT->n_overlays to the total number of overlay strings found.
5377 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5378 a time. On entry into load_overlay_strings,
5379 IT->current.overlay_string_index gives the number of overlay
5380 strings that have already been loaded by previous calls to this
5381 function.
5383 IT->add_overlay_start contains an additional overlay start
5384 position to consider for taking overlay strings from, if non-zero.
5385 This position comes into play when the overlay has an `invisible'
5386 property, and both before and after-strings. When we've skipped to
5387 the end of the overlay, because of its `invisible' property, we
5388 nevertheless want its before-string to appear.
5389 IT->add_overlay_start will contain the overlay start position
5390 in this case.
5392 Overlay strings are sorted so that after-string strings come in
5393 front of before-string strings. Within before and after-strings,
5394 strings are sorted by overlay priority. See also function
5395 compare_overlay_entries. */
5397 static void
5398 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5400 Lisp_Object overlay, window, str, invisible;
5401 struct Lisp_Overlay *ov;
5402 ptrdiff_t start, end;
5403 ptrdiff_t size = 20;
5404 ptrdiff_t n = 0, i, j;
5405 int invis_p;
5406 struct overlay_entry *entries = alloca (size * sizeof *entries);
5407 USE_SAFE_ALLOCA;
5409 if (charpos <= 0)
5410 charpos = IT_CHARPOS (*it);
5412 /* Append the overlay string STRING of overlay OVERLAY to vector
5413 `entries' which has size `size' and currently contains `n'
5414 elements. AFTER_P non-zero means STRING is an after-string of
5415 OVERLAY. */
5416 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5417 do \
5419 Lisp_Object priority; \
5421 if (n == size) \
5423 struct overlay_entry *old = entries; \
5424 SAFE_NALLOCA (entries, 2, size); \
5425 memcpy (entries, old, size * sizeof *entries); \
5426 size *= 2; \
5429 entries[n].string = (STRING); \
5430 entries[n].overlay = (OVERLAY); \
5431 priority = Foverlay_get ((OVERLAY), Qpriority); \
5432 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5433 entries[n].after_string_p = (AFTER_P); \
5434 ++n; \
5436 while (0)
5438 /* Process overlay before the overlay center. */
5439 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5441 XSETMISC (overlay, ov);
5442 eassert (OVERLAYP (overlay));
5443 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5444 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5446 if (end < charpos)
5447 break;
5449 /* Skip this overlay if it doesn't start or end at IT's current
5450 position. */
5451 if (end != charpos && start != charpos)
5452 continue;
5454 /* Skip this overlay if it doesn't apply to IT->w. */
5455 window = Foverlay_get (overlay, Qwindow);
5456 if (WINDOWP (window) && XWINDOW (window) != it->w)
5457 continue;
5459 /* If the text ``under'' the overlay is invisible, both before-
5460 and after-strings from this overlay are visible; start and
5461 end position are indistinguishable. */
5462 invisible = Foverlay_get (overlay, Qinvisible);
5463 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5465 /* If overlay has a non-empty before-string, record it. */
5466 if ((start == charpos || (end == charpos && invis_p))
5467 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5468 && SCHARS (str))
5469 RECORD_OVERLAY_STRING (overlay, str, 0);
5471 /* If overlay has a non-empty after-string, record it. */
5472 if ((end == charpos || (start == charpos && invis_p))
5473 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5474 && SCHARS (str))
5475 RECORD_OVERLAY_STRING (overlay, str, 1);
5478 /* Process overlays after the overlay center. */
5479 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5481 XSETMISC (overlay, ov);
5482 eassert (OVERLAYP (overlay));
5483 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5484 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5486 if (start > charpos)
5487 break;
5489 /* Skip this overlay if it doesn't start or end at IT's current
5490 position. */
5491 if (end != charpos && start != charpos)
5492 continue;
5494 /* Skip this overlay if it doesn't apply to IT->w. */
5495 window = Foverlay_get (overlay, Qwindow);
5496 if (WINDOWP (window) && XWINDOW (window) != it->w)
5497 continue;
5499 /* If the text ``under'' the overlay is invisible, it has a zero
5500 dimension, and both before- and after-strings apply. */
5501 invisible = Foverlay_get (overlay, Qinvisible);
5502 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5504 /* If overlay has a non-empty before-string, record it. */
5505 if ((start == charpos || (end == charpos && invis_p))
5506 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5507 && SCHARS (str))
5508 RECORD_OVERLAY_STRING (overlay, str, 0);
5510 /* If overlay has a non-empty after-string, record it. */
5511 if ((end == charpos || (start == charpos && invis_p))
5512 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5513 && SCHARS (str))
5514 RECORD_OVERLAY_STRING (overlay, str, 1);
5517 #undef RECORD_OVERLAY_STRING
5519 /* Sort entries. */
5520 if (n > 1)
5521 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5523 /* Record number of overlay strings, and where we computed it. */
5524 it->n_overlay_strings = n;
5525 it->overlay_strings_charpos = charpos;
5527 /* IT->current.overlay_string_index is the number of overlay strings
5528 that have already been consumed by IT. Copy some of the
5529 remaining overlay strings to IT->overlay_strings. */
5530 i = 0;
5531 j = it->current.overlay_string_index;
5532 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5534 it->overlay_strings[i] = entries[j].string;
5535 it->string_overlays[i++] = entries[j++].overlay;
5538 CHECK_IT (it);
5539 SAFE_FREE ();
5543 /* Get the first chunk of overlay strings at IT's current buffer
5544 position, or at CHARPOS if that is > 0. Value is non-zero if at
5545 least one overlay string was found. */
5547 static int
5548 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5550 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5551 process. This fills IT->overlay_strings with strings, and sets
5552 IT->n_overlay_strings to the total number of strings to process.
5553 IT->pos.overlay_string_index has to be set temporarily to zero
5554 because load_overlay_strings needs this; it must be set to -1
5555 when no overlay strings are found because a zero value would
5556 indicate a position in the first overlay string. */
5557 it->current.overlay_string_index = 0;
5558 load_overlay_strings (it, charpos);
5560 /* If we found overlay strings, set up IT to deliver display
5561 elements from the first one. Otherwise set up IT to deliver
5562 from current_buffer. */
5563 if (it->n_overlay_strings)
5565 /* Make sure we know settings in current_buffer, so that we can
5566 restore meaningful values when we're done with the overlay
5567 strings. */
5568 if (compute_stop_p)
5569 compute_stop_pos (it);
5570 eassert (it->face_id >= 0);
5572 /* Save IT's settings. They are restored after all overlay
5573 strings have been processed. */
5574 eassert (!compute_stop_p || it->sp == 0);
5576 /* When called from handle_stop, there might be an empty display
5577 string loaded. In that case, don't bother saving it. But
5578 don't use this optimization with the bidi iterator, since we
5579 need the corresponding pop_it call to resync the bidi
5580 iterator's position with IT's position, after we are done
5581 with the overlay strings. (The corresponding call to pop_it
5582 in case of an empty display string is in
5583 next_overlay_string.) */
5584 if (!(!it->bidi_p
5585 && STRINGP (it->string) && !SCHARS (it->string)))
5586 push_it (it, NULL);
5588 /* Set up IT to deliver display elements from the first overlay
5589 string. */
5590 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5591 it->string = it->overlay_strings[0];
5592 it->from_overlay = Qnil;
5593 it->stop_charpos = 0;
5594 eassert (STRINGP (it->string));
5595 it->end_charpos = SCHARS (it->string);
5596 it->prev_stop = 0;
5597 it->base_level_stop = 0;
5598 it->multibyte_p = STRING_MULTIBYTE (it->string);
5599 it->method = GET_FROM_STRING;
5600 it->from_disp_prop_p = 0;
5602 /* Force paragraph direction to be that of the parent
5603 buffer. */
5604 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5605 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5606 else
5607 it->paragraph_embedding = L2R;
5609 /* Set up the bidi iterator for this overlay string. */
5610 if (it->bidi_p)
5612 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5614 it->bidi_it.string.lstring = it->string;
5615 it->bidi_it.string.s = NULL;
5616 it->bidi_it.string.schars = SCHARS (it->string);
5617 it->bidi_it.string.bufpos = pos;
5618 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5619 it->bidi_it.string.unibyte = !it->multibyte_p;
5620 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5622 return 1;
5625 it->current.overlay_string_index = -1;
5626 return 0;
5629 static int
5630 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5632 it->string = Qnil;
5633 it->method = GET_FROM_BUFFER;
5635 (void) get_overlay_strings_1 (it, charpos, 1);
5637 CHECK_IT (it);
5639 /* Value is non-zero if we found at least one overlay string. */
5640 return STRINGP (it->string);
5645 /***********************************************************************
5646 Saving and restoring state
5647 ***********************************************************************/
5649 /* Save current settings of IT on IT->stack. Called, for example,
5650 before setting up IT for an overlay string, to be able to restore
5651 IT's settings to what they were after the overlay string has been
5652 processed. If POSITION is non-NULL, it is the position to save on
5653 the stack instead of IT->position. */
5655 static void
5656 push_it (struct it *it, struct text_pos *position)
5658 struct iterator_stack_entry *p;
5660 eassert (it->sp < IT_STACK_SIZE);
5661 p = it->stack + it->sp;
5663 p->stop_charpos = it->stop_charpos;
5664 p->prev_stop = it->prev_stop;
5665 p->base_level_stop = it->base_level_stop;
5666 p->cmp_it = it->cmp_it;
5667 eassert (it->face_id >= 0);
5668 p->face_id = it->face_id;
5669 p->string = it->string;
5670 p->method = it->method;
5671 p->from_overlay = it->from_overlay;
5672 switch (p->method)
5674 case GET_FROM_IMAGE:
5675 p->u.image.object = it->object;
5676 p->u.image.image_id = it->image_id;
5677 p->u.image.slice = it->slice;
5678 break;
5679 case GET_FROM_STRETCH:
5680 p->u.stretch.object = it->object;
5681 break;
5683 p->position = position ? *position : it->position;
5684 p->current = it->current;
5685 p->end_charpos = it->end_charpos;
5686 p->string_nchars = it->string_nchars;
5687 p->area = it->area;
5688 p->multibyte_p = it->multibyte_p;
5689 p->avoid_cursor_p = it->avoid_cursor_p;
5690 p->space_width = it->space_width;
5691 p->font_height = it->font_height;
5692 p->voffset = it->voffset;
5693 p->string_from_display_prop_p = it->string_from_display_prop_p;
5694 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5695 p->display_ellipsis_p = 0;
5696 p->line_wrap = it->line_wrap;
5697 p->bidi_p = it->bidi_p;
5698 p->paragraph_embedding = it->paragraph_embedding;
5699 p->from_disp_prop_p = it->from_disp_prop_p;
5700 ++it->sp;
5702 /* Save the state of the bidi iterator as well. */
5703 if (it->bidi_p)
5704 bidi_push_it (&it->bidi_it);
5707 static void
5708 iterate_out_of_display_property (struct it *it)
5710 int buffer_p = !STRINGP (it->string);
5711 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5712 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5714 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5716 /* Maybe initialize paragraph direction. If we are at the beginning
5717 of a new paragraph, next_element_from_buffer may not have a
5718 chance to do that. */
5719 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5720 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5721 /* prev_stop can be zero, so check against BEGV as well. */
5722 while (it->bidi_it.charpos >= bob
5723 && it->prev_stop <= it->bidi_it.charpos
5724 && it->bidi_it.charpos < CHARPOS (it->position)
5725 && it->bidi_it.charpos < eob)
5726 bidi_move_to_visually_next (&it->bidi_it);
5727 /* Record the stop_pos we just crossed, for when we cross it
5728 back, maybe. */
5729 if (it->bidi_it.charpos > CHARPOS (it->position))
5730 it->prev_stop = CHARPOS (it->position);
5731 /* If we ended up not where pop_it put us, resync IT's
5732 positional members with the bidi iterator. */
5733 if (it->bidi_it.charpos != CHARPOS (it->position))
5734 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5735 if (buffer_p)
5736 it->current.pos = it->position;
5737 else
5738 it->current.string_pos = it->position;
5741 /* Restore IT's settings from IT->stack. Called, for example, when no
5742 more overlay strings must be processed, and we return to delivering
5743 display elements from a buffer, or when the end of a string from a
5744 `display' property is reached and we return to delivering display
5745 elements from an overlay string, or from a buffer. */
5747 static void
5748 pop_it (struct it *it)
5750 struct iterator_stack_entry *p;
5751 int from_display_prop = it->from_disp_prop_p;
5753 eassert (it->sp > 0);
5754 --it->sp;
5755 p = it->stack + it->sp;
5756 it->stop_charpos = p->stop_charpos;
5757 it->prev_stop = p->prev_stop;
5758 it->base_level_stop = p->base_level_stop;
5759 it->cmp_it = p->cmp_it;
5760 it->face_id = p->face_id;
5761 it->current = p->current;
5762 it->position = p->position;
5763 it->string = p->string;
5764 it->from_overlay = p->from_overlay;
5765 if (NILP (it->string))
5766 SET_TEXT_POS (it->current.string_pos, -1, -1);
5767 it->method = p->method;
5768 switch (it->method)
5770 case GET_FROM_IMAGE:
5771 it->image_id = p->u.image.image_id;
5772 it->object = p->u.image.object;
5773 it->slice = p->u.image.slice;
5774 break;
5775 case GET_FROM_STRETCH:
5776 it->object = p->u.stretch.object;
5777 break;
5778 case GET_FROM_BUFFER:
5779 it->object = it->w->buffer;
5780 break;
5781 case GET_FROM_STRING:
5782 it->object = it->string;
5783 break;
5784 case GET_FROM_DISPLAY_VECTOR:
5785 if (it->s)
5786 it->method = GET_FROM_C_STRING;
5787 else if (STRINGP (it->string))
5788 it->method = GET_FROM_STRING;
5789 else
5791 it->method = GET_FROM_BUFFER;
5792 it->object = it->w->buffer;
5795 it->end_charpos = p->end_charpos;
5796 it->string_nchars = p->string_nchars;
5797 it->area = p->area;
5798 it->multibyte_p = p->multibyte_p;
5799 it->avoid_cursor_p = p->avoid_cursor_p;
5800 it->space_width = p->space_width;
5801 it->font_height = p->font_height;
5802 it->voffset = p->voffset;
5803 it->string_from_display_prop_p = p->string_from_display_prop_p;
5804 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5805 it->line_wrap = p->line_wrap;
5806 it->bidi_p = p->bidi_p;
5807 it->paragraph_embedding = p->paragraph_embedding;
5808 it->from_disp_prop_p = p->from_disp_prop_p;
5809 if (it->bidi_p)
5811 bidi_pop_it (&it->bidi_it);
5812 /* Bidi-iterate until we get out of the portion of text, if any,
5813 covered by a `display' text property or by an overlay with
5814 `display' property. (We cannot just jump there, because the
5815 internal coherency of the bidi iterator state can not be
5816 preserved across such jumps.) We also must determine the
5817 paragraph base direction if the overlay we just processed is
5818 at the beginning of a new paragraph. */
5819 if (from_display_prop
5820 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5821 iterate_out_of_display_property (it);
5823 eassert ((BUFFERP (it->object)
5824 && IT_CHARPOS (*it) == it->bidi_it.charpos
5825 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5826 || (STRINGP (it->object)
5827 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5828 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5829 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5835 /***********************************************************************
5836 Moving over lines
5837 ***********************************************************************/
5839 /* Set IT's current position to the previous line start. */
5841 static void
5842 back_to_previous_line_start (struct it *it)
5844 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5845 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5849 /* Move IT to the next line start.
5851 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5852 we skipped over part of the text (as opposed to moving the iterator
5853 continuously over the text). Otherwise, don't change the value
5854 of *SKIPPED_P.
5856 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5857 iterator on the newline, if it was found.
5859 Newlines may come from buffer text, overlay strings, or strings
5860 displayed via the `display' property. That's the reason we can't
5861 simply use find_next_newline_no_quit.
5863 Note that this function may not skip over invisible text that is so
5864 because of text properties and immediately follows a newline. If
5865 it would, function reseat_at_next_visible_line_start, when called
5866 from set_iterator_to_next, would effectively make invisible
5867 characters following a newline part of the wrong glyph row, which
5868 leads to wrong cursor motion. */
5870 static int
5871 forward_to_next_line_start (struct it *it, int *skipped_p,
5872 struct bidi_it *bidi_it_prev)
5874 ptrdiff_t old_selective;
5875 int newline_found_p, n;
5876 const int MAX_NEWLINE_DISTANCE = 500;
5878 /* If already on a newline, just consume it to avoid unintended
5879 skipping over invisible text below. */
5880 if (it->what == IT_CHARACTER
5881 && it->c == '\n'
5882 && CHARPOS (it->position) == IT_CHARPOS (*it))
5884 if (it->bidi_p && bidi_it_prev)
5885 *bidi_it_prev = it->bidi_it;
5886 set_iterator_to_next (it, 0);
5887 it->c = 0;
5888 return 1;
5891 /* Don't handle selective display in the following. It's (a)
5892 unnecessary because it's done by the caller, and (b) leads to an
5893 infinite recursion because next_element_from_ellipsis indirectly
5894 calls this function. */
5895 old_selective = it->selective;
5896 it->selective = 0;
5898 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5899 from buffer text. */
5900 for (n = newline_found_p = 0;
5901 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5902 n += STRINGP (it->string) ? 0 : 1)
5904 if (!get_next_display_element (it))
5905 return 0;
5906 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5907 if (newline_found_p && it->bidi_p && bidi_it_prev)
5908 *bidi_it_prev = it->bidi_it;
5909 set_iterator_to_next (it, 0);
5912 /* If we didn't find a newline near enough, see if we can use a
5913 short-cut. */
5914 if (!newline_found_p)
5916 ptrdiff_t start = IT_CHARPOS (*it);
5917 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
5918 Lisp_Object pos;
5920 eassert (!STRINGP (it->string));
5922 /* If there isn't any `display' property in sight, and no
5923 overlays, we can just use the position of the newline in
5924 buffer text. */
5925 if (it->stop_charpos >= limit
5926 || ((pos = Fnext_single_property_change (make_number (start),
5927 Qdisplay, Qnil,
5928 make_number (limit)),
5929 NILP (pos))
5930 && next_overlay_change (start) == ZV))
5932 if (!it->bidi_p)
5934 IT_CHARPOS (*it) = limit;
5935 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5937 else
5939 struct bidi_it bprev;
5941 /* Help bidi.c avoid expensive searches for display
5942 properties and overlays, by telling it that there are
5943 none up to `limit'. */
5944 if (it->bidi_it.disp_pos < limit)
5946 it->bidi_it.disp_pos = limit;
5947 it->bidi_it.disp_prop = 0;
5949 do {
5950 bprev = it->bidi_it;
5951 bidi_move_to_visually_next (&it->bidi_it);
5952 } while (it->bidi_it.charpos != limit);
5953 IT_CHARPOS (*it) = limit;
5954 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5955 if (bidi_it_prev)
5956 *bidi_it_prev = bprev;
5958 *skipped_p = newline_found_p = 1;
5960 else
5962 while (get_next_display_element (it)
5963 && !newline_found_p)
5965 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5966 if (newline_found_p && it->bidi_p && bidi_it_prev)
5967 *bidi_it_prev = it->bidi_it;
5968 set_iterator_to_next (it, 0);
5973 it->selective = old_selective;
5974 return newline_found_p;
5978 /* Set IT's current position to the previous visible line start. Skip
5979 invisible text that is so either due to text properties or due to
5980 selective display. Caution: this does not change IT->current_x and
5981 IT->hpos. */
5983 static void
5984 back_to_previous_visible_line_start (struct it *it)
5986 while (IT_CHARPOS (*it) > BEGV)
5988 back_to_previous_line_start (it);
5990 if (IT_CHARPOS (*it) <= BEGV)
5991 break;
5993 /* If selective > 0, then lines indented more than its value are
5994 invisible. */
5995 if (it->selective > 0
5996 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5997 it->selective))
5998 continue;
6000 /* Check the newline before point for invisibility. */
6002 Lisp_Object prop;
6003 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6004 Qinvisible, it->window);
6005 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6006 continue;
6009 if (IT_CHARPOS (*it) <= BEGV)
6010 break;
6013 struct it it2;
6014 void *it2data = NULL;
6015 ptrdiff_t pos;
6016 ptrdiff_t beg, end;
6017 Lisp_Object val, overlay;
6019 SAVE_IT (it2, *it, it2data);
6021 /* If newline is part of a composition, continue from start of composition */
6022 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6023 && beg < IT_CHARPOS (*it))
6024 goto replaced;
6026 /* If newline is replaced by a display property, find start of overlay
6027 or interval and continue search from that point. */
6028 pos = --IT_CHARPOS (it2);
6029 --IT_BYTEPOS (it2);
6030 it2.sp = 0;
6031 bidi_unshelve_cache (NULL, 0);
6032 it2.string_from_display_prop_p = 0;
6033 it2.from_disp_prop_p = 0;
6034 if (handle_display_prop (&it2) == HANDLED_RETURN
6035 && !NILP (val = get_char_property_and_overlay
6036 (make_number (pos), Qdisplay, Qnil, &overlay))
6037 && (OVERLAYP (overlay)
6038 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6039 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6041 RESTORE_IT (it, it, it2data);
6042 goto replaced;
6045 /* Newline is not replaced by anything -- so we are done. */
6046 RESTORE_IT (it, it, it2data);
6047 break;
6049 replaced:
6050 if (beg < BEGV)
6051 beg = BEGV;
6052 IT_CHARPOS (*it) = beg;
6053 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6057 it->continuation_lines_width = 0;
6059 eassert (IT_CHARPOS (*it) >= BEGV);
6060 eassert (IT_CHARPOS (*it) == BEGV
6061 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6062 CHECK_IT (it);
6066 /* Reseat iterator IT at the previous visible line start. Skip
6067 invisible text that is so either due to text properties or due to
6068 selective display. At the end, update IT's overlay information,
6069 face information etc. */
6071 void
6072 reseat_at_previous_visible_line_start (struct it *it)
6074 back_to_previous_visible_line_start (it);
6075 reseat (it, it->current.pos, 1);
6076 CHECK_IT (it);
6080 /* Reseat iterator IT on the next visible line start in the current
6081 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6082 preceding the line start. Skip over invisible text that is so
6083 because of selective display. Compute faces, overlays etc at the
6084 new position. Note that this function does not skip over text that
6085 is invisible because of text properties. */
6087 static void
6088 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6090 int newline_found_p, skipped_p = 0;
6091 struct bidi_it bidi_it_prev;
6093 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6095 /* Skip over lines that are invisible because they are indented
6096 more than the value of IT->selective. */
6097 if (it->selective > 0)
6098 while (IT_CHARPOS (*it) < ZV
6099 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6100 it->selective))
6102 eassert (IT_BYTEPOS (*it) == BEGV
6103 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6104 newline_found_p =
6105 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6108 /* Position on the newline if that's what's requested. */
6109 if (on_newline_p && newline_found_p)
6111 if (STRINGP (it->string))
6113 if (IT_STRING_CHARPOS (*it) > 0)
6115 if (!it->bidi_p)
6117 --IT_STRING_CHARPOS (*it);
6118 --IT_STRING_BYTEPOS (*it);
6120 else
6122 /* We need to restore the bidi iterator to the state
6123 it had on the newline, and resync the IT's
6124 position with that. */
6125 it->bidi_it = bidi_it_prev;
6126 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6127 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6131 else if (IT_CHARPOS (*it) > BEGV)
6133 if (!it->bidi_p)
6135 --IT_CHARPOS (*it);
6136 --IT_BYTEPOS (*it);
6138 else
6140 /* We need to restore the bidi iterator to the state it
6141 had on the newline and resync IT with that. */
6142 it->bidi_it = bidi_it_prev;
6143 IT_CHARPOS (*it) = it->bidi_it.charpos;
6144 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6146 reseat (it, it->current.pos, 0);
6149 else if (skipped_p)
6150 reseat (it, it->current.pos, 0);
6152 CHECK_IT (it);
6157 /***********************************************************************
6158 Changing an iterator's position
6159 ***********************************************************************/
6161 /* Change IT's current position to POS in current_buffer. If FORCE_P
6162 is non-zero, always check for text properties at the new position.
6163 Otherwise, text properties are only looked up if POS >=
6164 IT->check_charpos of a property. */
6166 static void
6167 reseat (struct it *it, struct text_pos pos, int force_p)
6169 ptrdiff_t original_pos = IT_CHARPOS (*it);
6171 reseat_1 (it, pos, 0);
6173 /* Determine where to check text properties. Avoid doing it
6174 where possible because text property lookup is very expensive. */
6175 if (force_p
6176 || CHARPOS (pos) > it->stop_charpos
6177 || CHARPOS (pos) < original_pos)
6179 if (it->bidi_p)
6181 /* For bidi iteration, we need to prime prev_stop and
6182 base_level_stop with our best estimations. */
6183 /* Implementation note: Of course, POS is not necessarily a
6184 stop position, so assigning prev_pos to it is a lie; we
6185 should have called compute_stop_backwards. However, if
6186 the current buffer does not include any R2L characters,
6187 that call would be a waste of cycles, because the
6188 iterator will never move back, and thus never cross this
6189 "fake" stop position. So we delay that backward search
6190 until the time we really need it, in next_element_from_buffer. */
6191 if (CHARPOS (pos) != it->prev_stop)
6192 it->prev_stop = CHARPOS (pos);
6193 if (CHARPOS (pos) < it->base_level_stop)
6194 it->base_level_stop = 0; /* meaning it's unknown */
6195 handle_stop (it);
6197 else
6199 handle_stop (it);
6200 it->prev_stop = it->base_level_stop = 0;
6205 CHECK_IT (it);
6209 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6210 IT->stop_pos to POS, also. */
6212 static void
6213 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6215 /* Don't call this function when scanning a C string. */
6216 eassert (it->s == NULL);
6218 /* POS must be a reasonable value. */
6219 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6221 it->current.pos = it->position = pos;
6222 it->end_charpos = ZV;
6223 it->dpvec = NULL;
6224 it->current.dpvec_index = -1;
6225 it->current.overlay_string_index = -1;
6226 IT_STRING_CHARPOS (*it) = -1;
6227 IT_STRING_BYTEPOS (*it) = -1;
6228 it->string = Qnil;
6229 it->method = GET_FROM_BUFFER;
6230 it->object = it->w->buffer;
6231 it->area = TEXT_AREA;
6232 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6233 it->sp = 0;
6234 it->string_from_display_prop_p = 0;
6235 it->string_from_prefix_prop_p = 0;
6237 it->from_disp_prop_p = 0;
6238 it->face_before_selective_p = 0;
6239 if (it->bidi_p)
6241 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6242 &it->bidi_it);
6243 bidi_unshelve_cache (NULL, 0);
6244 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6245 it->bidi_it.string.s = NULL;
6246 it->bidi_it.string.lstring = Qnil;
6247 it->bidi_it.string.bufpos = 0;
6248 it->bidi_it.string.unibyte = 0;
6251 if (set_stop_p)
6253 it->stop_charpos = CHARPOS (pos);
6254 it->base_level_stop = CHARPOS (pos);
6259 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6260 If S is non-null, it is a C string to iterate over. Otherwise,
6261 STRING gives a Lisp string to iterate over.
6263 If PRECISION > 0, don't return more then PRECISION number of
6264 characters from the string.
6266 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6267 characters have been returned. FIELD_WIDTH < 0 means an infinite
6268 field width.
6270 MULTIBYTE = 0 means disable processing of multibyte characters,
6271 MULTIBYTE > 0 means enable it,
6272 MULTIBYTE < 0 means use IT->multibyte_p.
6274 IT must be initialized via a prior call to init_iterator before
6275 calling this function. */
6277 static void
6278 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6279 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6280 int multibyte)
6282 /* No region in strings. */
6283 it->region_beg_charpos = it->region_end_charpos = -1;
6285 /* No text property checks performed by default, but see below. */
6286 it->stop_charpos = -1;
6288 /* Set iterator position and end position. */
6289 memset (&it->current, 0, sizeof it->current);
6290 it->current.overlay_string_index = -1;
6291 it->current.dpvec_index = -1;
6292 eassert (charpos >= 0);
6294 /* If STRING is specified, use its multibyteness, otherwise use the
6295 setting of MULTIBYTE, if specified. */
6296 if (multibyte >= 0)
6297 it->multibyte_p = multibyte > 0;
6299 /* Bidirectional reordering of strings is controlled by the default
6300 value of bidi-display-reordering. Don't try to reorder while
6301 loading loadup.el, as the necessary character property tables are
6302 not yet available. */
6303 it->bidi_p =
6304 NILP (Vpurify_flag)
6305 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6307 if (s == NULL)
6309 eassert (STRINGP (string));
6310 it->string = string;
6311 it->s = NULL;
6312 it->end_charpos = it->string_nchars = SCHARS (string);
6313 it->method = GET_FROM_STRING;
6314 it->current.string_pos = string_pos (charpos, string);
6316 if (it->bidi_p)
6318 it->bidi_it.string.lstring = string;
6319 it->bidi_it.string.s = NULL;
6320 it->bidi_it.string.schars = it->end_charpos;
6321 it->bidi_it.string.bufpos = 0;
6322 it->bidi_it.string.from_disp_str = 0;
6323 it->bidi_it.string.unibyte = !it->multibyte_p;
6324 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6325 FRAME_WINDOW_P (it->f), &it->bidi_it);
6328 else
6330 it->s = (const unsigned char *) s;
6331 it->string = Qnil;
6333 /* Note that we use IT->current.pos, not it->current.string_pos,
6334 for displaying C strings. */
6335 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6336 if (it->multibyte_p)
6338 it->current.pos = c_string_pos (charpos, s, 1);
6339 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6341 else
6343 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6344 it->end_charpos = it->string_nchars = strlen (s);
6347 if (it->bidi_p)
6349 it->bidi_it.string.lstring = Qnil;
6350 it->bidi_it.string.s = (const unsigned char *) s;
6351 it->bidi_it.string.schars = it->end_charpos;
6352 it->bidi_it.string.bufpos = 0;
6353 it->bidi_it.string.from_disp_str = 0;
6354 it->bidi_it.string.unibyte = !it->multibyte_p;
6355 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6356 &it->bidi_it);
6358 it->method = GET_FROM_C_STRING;
6361 /* PRECISION > 0 means don't return more than PRECISION characters
6362 from the string. */
6363 if (precision > 0 && it->end_charpos - charpos > precision)
6365 it->end_charpos = it->string_nchars = charpos + precision;
6366 if (it->bidi_p)
6367 it->bidi_it.string.schars = it->end_charpos;
6370 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6371 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6372 FIELD_WIDTH < 0 means infinite field width. This is useful for
6373 padding with `-' at the end of a mode line. */
6374 if (field_width < 0)
6375 field_width = INFINITY;
6376 /* Implementation note: We deliberately don't enlarge
6377 it->bidi_it.string.schars here to fit it->end_charpos, because
6378 the bidi iterator cannot produce characters out of thin air. */
6379 if (field_width > it->end_charpos - charpos)
6380 it->end_charpos = charpos + field_width;
6382 /* Use the standard display table for displaying strings. */
6383 if (DISP_TABLE_P (Vstandard_display_table))
6384 it->dp = XCHAR_TABLE (Vstandard_display_table);
6386 it->stop_charpos = charpos;
6387 it->prev_stop = charpos;
6388 it->base_level_stop = 0;
6389 if (it->bidi_p)
6391 it->bidi_it.first_elt = 1;
6392 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6393 it->bidi_it.disp_pos = -1;
6395 if (s == NULL && it->multibyte_p)
6397 ptrdiff_t endpos = SCHARS (it->string);
6398 if (endpos > it->end_charpos)
6399 endpos = it->end_charpos;
6400 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6401 it->string);
6403 CHECK_IT (it);
6408 /***********************************************************************
6409 Iteration
6410 ***********************************************************************/
6412 /* Map enum it_method value to corresponding next_element_from_* function. */
6414 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6416 next_element_from_buffer,
6417 next_element_from_display_vector,
6418 next_element_from_string,
6419 next_element_from_c_string,
6420 next_element_from_image,
6421 next_element_from_stretch
6424 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6427 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6428 (possibly with the following characters). */
6430 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6431 ((IT)->cmp_it.id >= 0 \
6432 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6433 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6434 END_CHARPOS, (IT)->w, \
6435 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6436 (IT)->string)))
6439 /* Lookup the char-table Vglyphless_char_display for character C (-1
6440 if we want information for no-font case), and return the display
6441 method symbol. By side-effect, update it->what and
6442 it->glyphless_method. This function is called from
6443 get_next_display_element for each character element, and from
6444 x_produce_glyphs when no suitable font was found. */
6446 Lisp_Object
6447 lookup_glyphless_char_display (int c, struct it *it)
6449 Lisp_Object glyphless_method = Qnil;
6451 if (CHAR_TABLE_P (Vglyphless_char_display)
6452 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6454 if (c >= 0)
6456 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6457 if (CONSP (glyphless_method))
6458 glyphless_method = FRAME_WINDOW_P (it->f)
6459 ? XCAR (glyphless_method)
6460 : XCDR (glyphless_method);
6462 else
6463 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6466 retry:
6467 if (NILP (glyphless_method))
6469 if (c >= 0)
6470 /* The default is to display the character by a proper font. */
6471 return Qnil;
6472 /* The default for the no-font case is to display an empty box. */
6473 glyphless_method = Qempty_box;
6475 if (EQ (glyphless_method, Qzero_width))
6477 if (c >= 0)
6478 return glyphless_method;
6479 /* This method can't be used for the no-font case. */
6480 glyphless_method = Qempty_box;
6482 if (EQ (glyphless_method, Qthin_space))
6483 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6484 else if (EQ (glyphless_method, Qempty_box))
6485 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6486 else if (EQ (glyphless_method, Qhex_code))
6487 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6488 else if (STRINGP (glyphless_method))
6489 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6490 else
6492 /* Invalid value. We use the default method. */
6493 glyphless_method = Qnil;
6494 goto retry;
6496 it->what = IT_GLYPHLESS;
6497 return glyphless_method;
6500 /* Load IT's display element fields with information about the next
6501 display element from the current position of IT. Value is zero if
6502 end of buffer (or C string) is reached. */
6504 static struct frame *last_escape_glyph_frame = NULL;
6505 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6506 static int last_escape_glyph_merged_face_id = 0;
6508 struct frame *last_glyphless_glyph_frame = NULL;
6509 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6510 int last_glyphless_glyph_merged_face_id = 0;
6512 static int
6513 get_next_display_element (struct it *it)
6515 /* Non-zero means that we found a display element. Zero means that
6516 we hit the end of what we iterate over. Performance note: the
6517 function pointer `method' used here turns out to be faster than
6518 using a sequence of if-statements. */
6519 int success_p;
6521 get_next:
6522 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6524 if (it->what == IT_CHARACTER)
6526 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6527 and only if (a) the resolved directionality of that character
6528 is R..." */
6529 /* FIXME: Do we need an exception for characters from display
6530 tables? */
6531 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6532 it->c = bidi_mirror_char (it->c);
6533 /* Map via display table or translate control characters.
6534 IT->c, IT->len etc. have been set to the next character by
6535 the function call above. If we have a display table, and it
6536 contains an entry for IT->c, translate it. Don't do this if
6537 IT->c itself comes from a display table, otherwise we could
6538 end up in an infinite recursion. (An alternative could be to
6539 count the recursion depth of this function and signal an
6540 error when a certain maximum depth is reached.) Is it worth
6541 it? */
6542 if (success_p && it->dpvec == NULL)
6544 Lisp_Object dv;
6545 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6546 int nonascii_space_p = 0;
6547 int nonascii_hyphen_p = 0;
6548 int c = it->c; /* This is the character to display. */
6550 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6552 eassert (SINGLE_BYTE_CHAR_P (c));
6553 if (unibyte_display_via_language_environment)
6555 c = DECODE_CHAR (unibyte, c);
6556 if (c < 0)
6557 c = BYTE8_TO_CHAR (it->c);
6559 else
6560 c = BYTE8_TO_CHAR (it->c);
6563 if (it->dp
6564 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6565 VECTORP (dv)))
6567 struct Lisp_Vector *v = XVECTOR (dv);
6569 /* Return the first character from the display table
6570 entry, if not empty. If empty, don't display the
6571 current character. */
6572 if (v->header.size)
6574 it->dpvec_char_len = it->len;
6575 it->dpvec = v->contents;
6576 it->dpend = v->contents + v->header.size;
6577 it->current.dpvec_index = 0;
6578 it->dpvec_face_id = -1;
6579 it->saved_face_id = it->face_id;
6580 it->method = GET_FROM_DISPLAY_VECTOR;
6581 it->ellipsis_p = 0;
6583 else
6585 set_iterator_to_next (it, 0);
6587 goto get_next;
6590 if (! NILP (lookup_glyphless_char_display (c, it)))
6592 if (it->what == IT_GLYPHLESS)
6593 goto done;
6594 /* Don't display this character. */
6595 set_iterator_to_next (it, 0);
6596 goto get_next;
6599 /* If `nobreak-char-display' is non-nil, we display
6600 non-ASCII spaces and hyphens specially. */
6601 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6603 if (c == 0xA0)
6604 nonascii_space_p = 1;
6605 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6606 nonascii_hyphen_p = 1;
6609 /* Translate control characters into `\003' or `^C' form.
6610 Control characters coming from a display table entry are
6611 currently not translated because we use IT->dpvec to hold
6612 the translation. This could easily be changed but I
6613 don't believe that it is worth doing.
6615 The characters handled by `nobreak-char-display' must be
6616 translated too.
6618 Non-printable characters and raw-byte characters are also
6619 translated to octal form. */
6620 if (((c < ' ' || c == 127) /* ASCII control chars */
6621 ? (it->area != TEXT_AREA
6622 /* In mode line, treat \n, \t like other crl chars. */
6623 || (c != '\t'
6624 && it->glyph_row
6625 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6626 || (c != '\n' && c != '\t'))
6627 : (nonascii_space_p
6628 || nonascii_hyphen_p
6629 || CHAR_BYTE8_P (c)
6630 || ! CHAR_PRINTABLE_P (c))))
6632 /* C is a control character, non-ASCII space/hyphen,
6633 raw-byte, or a non-printable character which must be
6634 displayed either as '\003' or as `^C' where the '\\'
6635 and '^' can be defined in the display table. Fill
6636 IT->ctl_chars with glyphs for what we have to
6637 display. Then, set IT->dpvec to these glyphs. */
6638 Lisp_Object gc;
6639 int ctl_len;
6640 int face_id;
6641 int lface_id = 0;
6642 int escape_glyph;
6644 /* Handle control characters with ^. */
6646 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6648 int g;
6650 g = '^'; /* default glyph for Control */
6651 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6652 if (it->dp
6653 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6655 g = GLYPH_CODE_CHAR (gc);
6656 lface_id = GLYPH_CODE_FACE (gc);
6658 if (lface_id)
6660 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6662 else if (it->f == last_escape_glyph_frame
6663 && it->face_id == last_escape_glyph_face_id)
6665 face_id = last_escape_glyph_merged_face_id;
6667 else
6669 /* Merge the escape-glyph face into the current face. */
6670 face_id = merge_faces (it->f, Qescape_glyph, 0,
6671 it->face_id);
6672 last_escape_glyph_frame = it->f;
6673 last_escape_glyph_face_id = it->face_id;
6674 last_escape_glyph_merged_face_id = face_id;
6677 XSETINT (it->ctl_chars[0], g);
6678 XSETINT (it->ctl_chars[1], c ^ 0100);
6679 ctl_len = 2;
6680 goto display_control;
6683 /* Handle non-ascii space in the mode where it only gets
6684 highlighting. */
6686 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6688 /* Merge `nobreak-space' into the current face. */
6689 face_id = merge_faces (it->f, Qnobreak_space, 0,
6690 it->face_id);
6691 XSETINT (it->ctl_chars[0], ' ');
6692 ctl_len = 1;
6693 goto display_control;
6696 /* Handle sequences that start with the "escape glyph". */
6698 /* the default escape glyph is \. */
6699 escape_glyph = '\\';
6701 if (it->dp
6702 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6704 escape_glyph = GLYPH_CODE_CHAR (gc);
6705 lface_id = GLYPH_CODE_FACE (gc);
6707 if (lface_id)
6709 /* The display table specified a face.
6710 Merge it into face_id and also into escape_glyph. */
6711 face_id = merge_faces (it->f, Qt, lface_id,
6712 it->face_id);
6714 else if (it->f == last_escape_glyph_frame
6715 && it->face_id == last_escape_glyph_face_id)
6717 face_id = last_escape_glyph_merged_face_id;
6719 else
6721 /* Merge the escape-glyph face into the current face. */
6722 face_id = merge_faces (it->f, Qescape_glyph, 0,
6723 it->face_id);
6724 last_escape_glyph_frame = it->f;
6725 last_escape_glyph_face_id = it->face_id;
6726 last_escape_glyph_merged_face_id = face_id;
6729 /* Draw non-ASCII hyphen with just highlighting: */
6731 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6733 XSETINT (it->ctl_chars[0], '-');
6734 ctl_len = 1;
6735 goto display_control;
6738 /* Draw non-ASCII space/hyphen with escape glyph: */
6740 if (nonascii_space_p || nonascii_hyphen_p)
6742 XSETINT (it->ctl_chars[0], escape_glyph);
6743 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6744 ctl_len = 2;
6745 goto display_control;
6749 char str[10];
6750 int len, i;
6752 if (CHAR_BYTE8_P (c))
6753 /* Display \200 instead of \17777600. */
6754 c = CHAR_TO_BYTE8 (c);
6755 len = sprintf (str, "%03o", c);
6757 XSETINT (it->ctl_chars[0], escape_glyph);
6758 for (i = 0; i < len; i++)
6759 XSETINT (it->ctl_chars[i + 1], str[i]);
6760 ctl_len = len + 1;
6763 display_control:
6764 /* Set up IT->dpvec and return first character from it. */
6765 it->dpvec_char_len = it->len;
6766 it->dpvec = it->ctl_chars;
6767 it->dpend = it->dpvec + ctl_len;
6768 it->current.dpvec_index = 0;
6769 it->dpvec_face_id = face_id;
6770 it->saved_face_id = it->face_id;
6771 it->method = GET_FROM_DISPLAY_VECTOR;
6772 it->ellipsis_p = 0;
6773 goto get_next;
6775 it->char_to_display = c;
6777 else if (success_p)
6779 it->char_to_display = it->c;
6783 /* Adjust face id for a multibyte character. There are no multibyte
6784 character in unibyte text. */
6785 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6786 && it->multibyte_p
6787 && success_p
6788 && FRAME_WINDOW_P (it->f))
6790 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6792 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6794 /* Automatic composition with glyph-string. */
6795 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6797 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6799 else
6801 ptrdiff_t pos = (it->s ? -1
6802 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6803 : IT_CHARPOS (*it));
6804 int c;
6806 if (it->what == IT_CHARACTER)
6807 c = it->char_to_display;
6808 else
6810 struct composition *cmp = composition_table[it->cmp_it.id];
6811 int i;
6813 c = ' ';
6814 for (i = 0; i < cmp->glyph_len; i++)
6815 /* TAB in a composition means display glyphs with
6816 padding space on the left or right. */
6817 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6818 break;
6820 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6824 done:
6825 /* Is this character the last one of a run of characters with
6826 box? If yes, set IT->end_of_box_run_p to 1. */
6827 if (it->face_box_p
6828 && it->s == NULL)
6830 if (it->method == GET_FROM_STRING && it->sp)
6832 int face_id = underlying_face_id (it);
6833 struct face *face = FACE_FROM_ID (it->f, face_id);
6835 if (face)
6837 if (face->box == FACE_NO_BOX)
6839 /* If the box comes from face properties in a
6840 display string, check faces in that string. */
6841 int string_face_id = face_after_it_pos (it);
6842 it->end_of_box_run_p
6843 = (FACE_FROM_ID (it->f, string_face_id)->box
6844 == FACE_NO_BOX);
6846 /* Otherwise, the box comes from the underlying face.
6847 If this is the last string character displayed, check
6848 the next buffer location. */
6849 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6850 && (it->current.overlay_string_index
6851 == it->n_overlay_strings - 1))
6853 ptrdiff_t ignore;
6854 int next_face_id;
6855 struct text_pos pos = it->current.pos;
6856 INC_TEXT_POS (pos, it->multibyte_p);
6858 next_face_id = face_at_buffer_position
6859 (it->w, CHARPOS (pos), it->region_beg_charpos,
6860 it->region_end_charpos, &ignore,
6861 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6862 -1);
6863 it->end_of_box_run_p
6864 = (FACE_FROM_ID (it->f, next_face_id)->box
6865 == FACE_NO_BOX);
6869 else
6871 int face_id = face_after_it_pos (it);
6872 it->end_of_box_run_p
6873 = (face_id != it->face_id
6874 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6877 /* If we reached the end of the object we've been iterating (e.g., a
6878 display string or an overlay string), and there's something on
6879 IT->stack, proceed with what's on the stack. It doesn't make
6880 sense to return zero if there's unprocessed stuff on the stack,
6881 because otherwise that stuff will never be displayed. */
6882 if (!success_p && it->sp > 0)
6884 set_iterator_to_next (it, 0);
6885 success_p = get_next_display_element (it);
6888 /* Value is 0 if end of buffer or string reached. */
6889 return success_p;
6893 /* Move IT to the next display element.
6895 RESEAT_P non-zero means if called on a newline in buffer text,
6896 skip to the next visible line start.
6898 Functions get_next_display_element and set_iterator_to_next are
6899 separate because I find this arrangement easier to handle than a
6900 get_next_display_element function that also increments IT's
6901 position. The way it is we can first look at an iterator's current
6902 display element, decide whether it fits on a line, and if it does,
6903 increment the iterator position. The other way around we probably
6904 would either need a flag indicating whether the iterator has to be
6905 incremented the next time, or we would have to implement a
6906 decrement position function which would not be easy to write. */
6908 void
6909 set_iterator_to_next (struct it *it, int reseat_p)
6911 /* Reset flags indicating start and end of a sequence of characters
6912 with box. Reset them at the start of this function because
6913 moving the iterator to a new position might set them. */
6914 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6916 switch (it->method)
6918 case GET_FROM_BUFFER:
6919 /* The current display element of IT is a character from
6920 current_buffer. Advance in the buffer, and maybe skip over
6921 invisible lines that are so because of selective display. */
6922 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6923 reseat_at_next_visible_line_start (it, 0);
6924 else if (it->cmp_it.id >= 0)
6926 /* We are currently getting glyphs from a composition. */
6927 int i;
6929 if (! it->bidi_p)
6931 IT_CHARPOS (*it) += it->cmp_it.nchars;
6932 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6933 if (it->cmp_it.to < it->cmp_it.nglyphs)
6935 it->cmp_it.from = it->cmp_it.to;
6937 else
6939 it->cmp_it.id = -1;
6940 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6941 IT_BYTEPOS (*it),
6942 it->end_charpos, Qnil);
6945 else if (! it->cmp_it.reversed_p)
6947 /* Composition created while scanning forward. */
6948 /* Update IT's char/byte positions to point to the first
6949 character of the next grapheme cluster, or to the
6950 character visually after the current composition. */
6951 for (i = 0; i < it->cmp_it.nchars; i++)
6952 bidi_move_to_visually_next (&it->bidi_it);
6953 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6954 IT_CHARPOS (*it) = it->bidi_it.charpos;
6956 if (it->cmp_it.to < it->cmp_it.nglyphs)
6958 /* Proceed to the next grapheme cluster. */
6959 it->cmp_it.from = it->cmp_it.to;
6961 else
6963 /* No more grapheme clusters in this composition.
6964 Find the next stop position. */
6965 ptrdiff_t stop = it->end_charpos;
6966 if (it->bidi_it.scan_dir < 0)
6967 /* Now we are scanning backward and don't know
6968 where to stop. */
6969 stop = -1;
6970 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6971 IT_BYTEPOS (*it), stop, Qnil);
6974 else
6976 /* Composition created while scanning backward. */
6977 /* Update IT's char/byte positions to point to the last
6978 character of the previous grapheme cluster, or the
6979 character visually after the current composition. */
6980 for (i = 0; i < it->cmp_it.nchars; i++)
6981 bidi_move_to_visually_next (&it->bidi_it);
6982 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6983 IT_CHARPOS (*it) = it->bidi_it.charpos;
6984 if (it->cmp_it.from > 0)
6986 /* Proceed to the previous grapheme cluster. */
6987 it->cmp_it.to = it->cmp_it.from;
6989 else
6991 /* No more grapheme clusters in this composition.
6992 Find the next stop position. */
6993 ptrdiff_t stop = it->end_charpos;
6994 if (it->bidi_it.scan_dir < 0)
6995 /* Now we are scanning backward and don't know
6996 where to stop. */
6997 stop = -1;
6998 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6999 IT_BYTEPOS (*it), stop, Qnil);
7003 else
7005 eassert (it->len != 0);
7007 if (!it->bidi_p)
7009 IT_BYTEPOS (*it) += it->len;
7010 IT_CHARPOS (*it) += 1;
7012 else
7014 int prev_scan_dir = it->bidi_it.scan_dir;
7015 /* If this is a new paragraph, determine its base
7016 direction (a.k.a. its base embedding level). */
7017 if (it->bidi_it.new_paragraph)
7018 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7019 bidi_move_to_visually_next (&it->bidi_it);
7020 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7021 IT_CHARPOS (*it) = it->bidi_it.charpos;
7022 if (prev_scan_dir != it->bidi_it.scan_dir)
7024 /* As the scan direction was changed, we must
7025 re-compute the stop position for composition. */
7026 ptrdiff_t stop = it->end_charpos;
7027 if (it->bidi_it.scan_dir < 0)
7028 stop = -1;
7029 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7030 IT_BYTEPOS (*it), stop, Qnil);
7033 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7035 break;
7037 case GET_FROM_C_STRING:
7038 /* Current display element of IT is from a C string. */
7039 if (!it->bidi_p
7040 /* If the string position is beyond string's end, it means
7041 next_element_from_c_string is padding the string with
7042 blanks, in which case we bypass the bidi iterator,
7043 because it cannot deal with such virtual characters. */
7044 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7046 IT_BYTEPOS (*it) += it->len;
7047 IT_CHARPOS (*it) += 1;
7049 else
7051 bidi_move_to_visually_next (&it->bidi_it);
7052 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7053 IT_CHARPOS (*it) = it->bidi_it.charpos;
7055 break;
7057 case GET_FROM_DISPLAY_VECTOR:
7058 /* Current display element of IT is from a display table entry.
7059 Advance in the display table definition. Reset it to null if
7060 end reached, and continue with characters from buffers/
7061 strings. */
7062 ++it->current.dpvec_index;
7064 /* Restore face of the iterator to what they were before the
7065 display vector entry (these entries may contain faces). */
7066 it->face_id = it->saved_face_id;
7068 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7070 int recheck_faces = it->ellipsis_p;
7072 if (it->s)
7073 it->method = GET_FROM_C_STRING;
7074 else if (STRINGP (it->string))
7075 it->method = GET_FROM_STRING;
7076 else
7078 it->method = GET_FROM_BUFFER;
7079 it->object = it->w->buffer;
7082 it->dpvec = NULL;
7083 it->current.dpvec_index = -1;
7085 /* Skip over characters which were displayed via IT->dpvec. */
7086 if (it->dpvec_char_len < 0)
7087 reseat_at_next_visible_line_start (it, 1);
7088 else if (it->dpvec_char_len > 0)
7090 if (it->method == GET_FROM_STRING
7091 && it->n_overlay_strings > 0)
7092 it->ignore_overlay_strings_at_pos_p = 1;
7093 it->len = it->dpvec_char_len;
7094 set_iterator_to_next (it, reseat_p);
7097 /* Maybe recheck faces after display vector */
7098 if (recheck_faces)
7099 it->stop_charpos = IT_CHARPOS (*it);
7101 break;
7103 case GET_FROM_STRING:
7104 /* Current display element is a character from a Lisp string. */
7105 eassert (it->s == NULL && STRINGP (it->string));
7106 /* Don't advance past string end. These conditions are true
7107 when set_iterator_to_next is called at the end of
7108 get_next_display_element, in which case the Lisp string is
7109 already exhausted, and all we want is pop the iterator
7110 stack. */
7111 if (it->current.overlay_string_index >= 0)
7113 /* This is an overlay string, so there's no padding with
7114 spaces, and the number of characters in the string is
7115 where the string ends. */
7116 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7117 goto consider_string_end;
7119 else
7121 /* Not an overlay string. There could be padding, so test
7122 against it->end_charpos . */
7123 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7124 goto consider_string_end;
7126 if (it->cmp_it.id >= 0)
7128 int i;
7130 if (! it->bidi_p)
7132 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7133 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7134 if (it->cmp_it.to < it->cmp_it.nglyphs)
7135 it->cmp_it.from = it->cmp_it.to;
7136 else
7138 it->cmp_it.id = -1;
7139 composition_compute_stop_pos (&it->cmp_it,
7140 IT_STRING_CHARPOS (*it),
7141 IT_STRING_BYTEPOS (*it),
7142 it->end_charpos, it->string);
7145 else if (! it->cmp_it.reversed_p)
7147 for (i = 0; i < it->cmp_it.nchars; i++)
7148 bidi_move_to_visually_next (&it->bidi_it);
7149 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7150 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7152 if (it->cmp_it.to < it->cmp_it.nglyphs)
7153 it->cmp_it.from = it->cmp_it.to;
7154 else
7156 ptrdiff_t stop = it->end_charpos;
7157 if (it->bidi_it.scan_dir < 0)
7158 stop = -1;
7159 composition_compute_stop_pos (&it->cmp_it,
7160 IT_STRING_CHARPOS (*it),
7161 IT_STRING_BYTEPOS (*it), stop,
7162 it->string);
7165 else
7167 for (i = 0; i < it->cmp_it.nchars; i++)
7168 bidi_move_to_visually_next (&it->bidi_it);
7169 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7170 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7171 if (it->cmp_it.from > 0)
7172 it->cmp_it.to = it->cmp_it.from;
7173 else
7175 ptrdiff_t stop = it->end_charpos;
7176 if (it->bidi_it.scan_dir < 0)
7177 stop = -1;
7178 composition_compute_stop_pos (&it->cmp_it,
7179 IT_STRING_CHARPOS (*it),
7180 IT_STRING_BYTEPOS (*it), stop,
7181 it->string);
7185 else
7187 if (!it->bidi_p
7188 /* If the string position is beyond string's end, it
7189 means next_element_from_string is padding the string
7190 with blanks, in which case we bypass the bidi
7191 iterator, because it cannot deal with such virtual
7192 characters. */
7193 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7195 IT_STRING_BYTEPOS (*it) += it->len;
7196 IT_STRING_CHARPOS (*it) += 1;
7198 else
7200 int prev_scan_dir = it->bidi_it.scan_dir;
7202 bidi_move_to_visually_next (&it->bidi_it);
7203 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7204 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7205 if (prev_scan_dir != it->bidi_it.scan_dir)
7207 ptrdiff_t stop = it->end_charpos;
7209 if (it->bidi_it.scan_dir < 0)
7210 stop = -1;
7211 composition_compute_stop_pos (&it->cmp_it,
7212 IT_STRING_CHARPOS (*it),
7213 IT_STRING_BYTEPOS (*it), stop,
7214 it->string);
7219 consider_string_end:
7221 if (it->current.overlay_string_index >= 0)
7223 /* IT->string is an overlay string. Advance to the
7224 next, if there is one. */
7225 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7227 it->ellipsis_p = 0;
7228 next_overlay_string (it);
7229 if (it->ellipsis_p)
7230 setup_for_ellipsis (it, 0);
7233 else
7235 /* IT->string is not an overlay string. If we reached
7236 its end, and there is something on IT->stack, proceed
7237 with what is on the stack. This can be either another
7238 string, this time an overlay string, or a buffer. */
7239 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7240 && it->sp > 0)
7242 pop_it (it);
7243 if (it->method == GET_FROM_STRING)
7244 goto consider_string_end;
7247 break;
7249 case GET_FROM_IMAGE:
7250 case GET_FROM_STRETCH:
7251 /* The position etc with which we have to proceed are on
7252 the stack. The position may be at the end of a string,
7253 if the `display' property takes up the whole string. */
7254 eassert (it->sp > 0);
7255 pop_it (it);
7256 if (it->method == GET_FROM_STRING)
7257 goto consider_string_end;
7258 break;
7260 default:
7261 /* There are no other methods defined, so this should be a bug. */
7262 abort ();
7265 eassert (it->method != GET_FROM_STRING
7266 || (STRINGP (it->string)
7267 && IT_STRING_CHARPOS (*it) >= 0));
7270 /* Load IT's display element fields with information about the next
7271 display element which comes from a display table entry or from the
7272 result of translating a control character to one of the forms `^C'
7273 or `\003'.
7275 IT->dpvec holds the glyphs to return as characters.
7276 IT->saved_face_id holds the face id before the display vector--it
7277 is restored into IT->face_id in set_iterator_to_next. */
7279 static int
7280 next_element_from_display_vector (struct it *it)
7282 Lisp_Object gc;
7284 /* Precondition. */
7285 eassert (it->dpvec && it->current.dpvec_index >= 0);
7287 it->face_id = it->saved_face_id;
7289 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7290 That seemed totally bogus - so I changed it... */
7291 gc = it->dpvec[it->current.dpvec_index];
7293 if (GLYPH_CODE_P (gc))
7295 it->c = GLYPH_CODE_CHAR (gc);
7296 it->len = CHAR_BYTES (it->c);
7298 /* The entry may contain a face id to use. Such a face id is
7299 the id of a Lisp face, not a realized face. A face id of
7300 zero means no face is specified. */
7301 if (it->dpvec_face_id >= 0)
7302 it->face_id = it->dpvec_face_id;
7303 else
7305 int lface_id = GLYPH_CODE_FACE (gc);
7306 if (lface_id > 0)
7307 it->face_id = merge_faces (it->f, Qt, lface_id,
7308 it->saved_face_id);
7311 else
7312 /* Display table entry is invalid. Return a space. */
7313 it->c = ' ', it->len = 1;
7315 /* Don't change position and object of the iterator here. They are
7316 still the values of the character that had this display table
7317 entry or was translated, and that's what we want. */
7318 it->what = IT_CHARACTER;
7319 return 1;
7322 /* Get the first element of string/buffer in the visual order, after
7323 being reseated to a new position in a string or a buffer. */
7324 static void
7325 get_visually_first_element (struct it *it)
7327 int string_p = STRINGP (it->string) || it->s;
7328 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7329 ptrdiff_t bob = (string_p ? 0 : BEGV);
7331 if (STRINGP (it->string))
7333 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7334 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7336 else
7338 it->bidi_it.charpos = IT_CHARPOS (*it);
7339 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7342 if (it->bidi_it.charpos == eob)
7344 /* Nothing to do, but reset the FIRST_ELT flag, like
7345 bidi_paragraph_init does, because we are not going to
7346 call it. */
7347 it->bidi_it.first_elt = 0;
7349 else if (it->bidi_it.charpos == bob
7350 || (!string_p
7351 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7352 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7354 /* If we are at the beginning of a line/string, we can produce
7355 the next element right away. */
7356 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7357 bidi_move_to_visually_next (&it->bidi_it);
7359 else
7361 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7363 /* We need to prime the bidi iterator starting at the line's or
7364 string's beginning, before we will be able to produce the
7365 next element. */
7366 if (string_p)
7367 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7368 else
7370 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7371 -1);
7372 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7374 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7377 /* Now return to buffer/string position where we were asked
7378 to get the next display element, and produce that. */
7379 bidi_move_to_visually_next (&it->bidi_it);
7381 while (it->bidi_it.bytepos != orig_bytepos
7382 && it->bidi_it.charpos < eob);
7385 /* Adjust IT's position information to where we ended up. */
7386 if (STRINGP (it->string))
7388 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7389 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7391 else
7393 IT_CHARPOS (*it) = it->bidi_it.charpos;
7394 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7397 if (STRINGP (it->string) || !it->s)
7399 ptrdiff_t stop, charpos, bytepos;
7401 if (STRINGP (it->string))
7403 eassert (!it->s);
7404 stop = SCHARS (it->string);
7405 if (stop > it->end_charpos)
7406 stop = it->end_charpos;
7407 charpos = IT_STRING_CHARPOS (*it);
7408 bytepos = IT_STRING_BYTEPOS (*it);
7410 else
7412 stop = it->end_charpos;
7413 charpos = IT_CHARPOS (*it);
7414 bytepos = IT_BYTEPOS (*it);
7416 if (it->bidi_it.scan_dir < 0)
7417 stop = -1;
7418 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7419 it->string);
7423 /* Load IT with the next display element from Lisp string IT->string.
7424 IT->current.string_pos is the current position within the string.
7425 If IT->current.overlay_string_index >= 0, the Lisp string is an
7426 overlay string. */
7428 static int
7429 next_element_from_string (struct it *it)
7431 struct text_pos position;
7433 eassert (STRINGP (it->string));
7434 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7435 eassert (IT_STRING_CHARPOS (*it) >= 0);
7436 position = it->current.string_pos;
7438 /* With bidi reordering, the character to display might not be the
7439 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7440 that we were reseat()ed to a new string, whose paragraph
7441 direction is not known. */
7442 if (it->bidi_p && it->bidi_it.first_elt)
7444 get_visually_first_element (it);
7445 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7448 /* Time to check for invisible text? */
7449 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7451 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7453 if (!(!it->bidi_p
7454 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7455 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7457 /* With bidi non-linear iteration, we could find
7458 ourselves far beyond the last computed stop_charpos,
7459 with several other stop positions in between that we
7460 missed. Scan them all now, in buffer's logical
7461 order, until we find and handle the last stop_charpos
7462 that precedes our current position. */
7463 handle_stop_backwards (it, it->stop_charpos);
7464 return GET_NEXT_DISPLAY_ELEMENT (it);
7466 else
7468 if (it->bidi_p)
7470 /* Take note of the stop position we just moved
7471 across, for when we will move back across it. */
7472 it->prev_stop = it->stop_charpos;
7473 /* If we are at base paragraph embedding level, take
7474 note of the last stop position seen at this
7475 level. */
7476 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7477 it->base_level_stop = it->stop_charpos;
7479 handle_stop (it);
7481 /* Since a handler may have changed IT->method, we must
7482 recurse here. */
7483 return GET_NEXT_DISPLAY_ELEMENT (it);
7486 else if (it->bidi_p
7487 /* If we are before prev_stop, we may have overstepped
7488 on our way backwards a stop_pos, and if so, we need
7489 to handle that stop_pos. */
7490 && IT_STRING_CHARPOS (*it) < it->prev_stop
7491 /* We can sometimes back up for reasons that have nothing
7492 to do with bidi reordering. E.g., compositions. The
7493 code below is only needed when we are above the base
7494 embedding level, so test for that explicitly. */
7495 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7497 /* If we lost track of base_level_stop, we have no better
7498 place for handle_stop_backwards to start from than string
7499 beginning. This happens, e.g., when we were reseated to
7500 the previous screenful of text by vertical-motion. */
7501 if (it->base_level_stop <= 0
7502 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7503 it->base_level_stop = 0;
7504 handle_stop_backwards (it, it->base_level_stop);
7505 return GET_NEXT_DISPLAY_ELEMENT (it);
7509 if (it->current.overlay_string_index >= 0)
7511 /* Get the next character from an overlay string. In overlay
7512 strings, there is no field width or padding with spaces to
7513 do. */
7514 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7516 it->what = IT_EOB;
7517 return 0;
7519 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7520 IT_STRING_BYTEPOS (*it),
7521 it->bidi_it.scan_dir < 0
7522 ? -1
7523 : SCHARS (it->string))
7524 && next_element_from_composition (it))
7526 return 1;
7528 else if (STRING_MULTIBYTE (it->string))
7530 const unsigned char *s = (SDATA (it->string)
7531 + IT_STRING_BYTEPOS (*it));
7532 it->c = string_char_and_length (s, &it->len);
7534 else
7536 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7537 it->len = 1;
7540 else
7542 /* Get the next character from a Lisp string that is not an
7543 overlay string. Such strings come from the mode line, for
7544 example. We may have to pad with spaces, or truncate the
7545 string. See also next_element_from_c_string. */
7546 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7548 it->what = IT_EOB;
7549 return 0;
7551 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7553 /* Pad with spaces. */
7554 it->c = ' ', it->len = 1;
7555 CHARPOS (position) = BYTEPOS (position) = -1;
7557 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7558 IT_STRING_BYTEPOS (*it),
7559 it->bidi_it.scan_dir < 0
7560 ? -1
7561 : it->string_nchars)
7562 && next_element_from_composition (it))
7564 return 1;
7566 else if (STRING_MULTIBYTE (it->string))
7568 const unsigned char *s = (SDATA (it->string)
7569 + IT_STRING_BYTEPOS (*it));
7570 it->c = string_char_and_length (s, &it->len);
7572 else
7574 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7575 it->len = 1;
7579 /* Record what we have and where it came from. */
7580 it->what = IT_CHARACTER;
7581 it->object = it->string;
7582 it->position = position;
7583 return 1;
7587 /* Load IT with next display element from C string IT->s.
7588 IT->string_nchars is the maximum number of characters to return
7589 from the string. IT->end_charpos may be greater than
7590 IT->string_nchars when this function is called, in which case we
7591 may have to return padding spaces. Value is zero if end of string
7592 reached, including padding spaces. */
7594 static int
7595 next_element_from_c_string (struct it *it)
7597 int success_p = 1;
7599 eassert (it->s);
7600 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7601 it->what = IT_CHARACTER;
7602 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7603 it->object = Qnil;
7605 /* With bidi reordering, the character to display might not be the
7606 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7607 we were reseated to a new string, whose paragraph direction is
7608 not known. */
7609 if (it->bidi_p && it->bidi_it.first_elt)
7610 get_visually_first_element (it);
7612 /* IT's position can be greater than IT->string_nchars in case a
7613 field width or precision has been specified when the iterator was
7614 initialized. */
7615 if (IT_CHARPOS (*it) >= it->end_charpos)
7617 /* End of the game. */
7618 it->what = IT_EOB;
7619 success_p = 0;
7621 else if (IT_CHARPOS (*it) >= it->string_nchars)
7623 /* Pad with spaces. */
7624 it->c = ' ', it->len = 1;
7625 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7627 else if (it->multibyte_p)
7628 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7629 else
7630 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7632 return success_p;
7636 /* Set up IT to return characters from an ellipsis, if appropriate.
7637 The definition of the ellipsis glyphs may come from a display table
7638 entry. This function fills IT with the first glyph from the
7639 ellipsis if an ellipsis is to be displayed. */
7641 static int
7642 next_element_from_ellipsis (struct it *it)
7644 if (it->selective_display_ellipsis_p)
7645 setup_for_ellipsis (it, it->len);
7646 else
7648 /* The face at the current position may be different from the
7649 face we find after the invisible text. Remember what it
7650 was in IT->saved_face_id, and signal that it's there by
7651 setting face_before_selective_p. */
7652 it->saved_face_id = it->face_id;
7653 it->method = GET_FROM_BUFFER;
7654 it->object = it->w->buffer;
7655 reseat_at_next_visible_line_start (it, 1);
7656 it->face_before_selective_p = 1;
7659 return GET_NEXT_DISPLAY_ELEMENT (it);
7663 /* Deliver an image display element. The iterator IT is already
7664 filled with image information (done in handle_display_prop). Value
7665 is always 1. */
7668 static int
7669 next_element_from_image (struct it *it)
7671 it->what = IT_IMAGE;
7672 it->ignore_overlay_strings_at_pos_p = 0;
7673 return 1;
7677 /* Fill iterator IT with next display element from a stretch glyph
7678 property. IT->object is the value of the text property. Value is
7679 always 1. */
7681 static int
7682 next_element_from_stretch (struct it *it)
7684 it->what = IT_STRETCH;
7685 return 1;
7688 /* Scan backwards from IT's current position until we find a stop
7689 position, or until BEGV. This is called when we find ourself
7690 before both the last known prev_stop and base_level_stop while
7691 reordering bidirectional text. */
7693 static void
7694 compute_stop_pos_backwards (struct it *it)
7696 const int SCAN_BACK_LIMIT = 1000;
7697 struct text_pos pos;
7698 struct display_pos save_current = it->current;
7699 struct text_pos save_position = it->position;
7700 ptrdiff_t charpos = IT_CHARPOS (*it);
7701 ptrdiff_t where_we_are = charpos;
7702 ptrdiff_t save_stop_pos = it->stop_charpos;
7703 ptrdiff_t save_end_pos = it->end_charpos;
7705 eassert (NILP (it->string) && !it->s);
7706 eassert (it->bidi_p);
7707 it->bidi_p = 0;
7710 it->end_charpos = min (charpos + 1, ZV);
7711 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7712 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7713 reseat_1 (it, pos, 0);
7714 compute_stop_pos (it);
7715 /* We must advance forward, right? */
7716 if (it->stop_charpos <= charpos)
7717 abort ();
7719 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7721 if (it->stop_charpos <= where_we_are)
7722 it->prev_stop = it->stop_charpos;
7723 else
7724 it->prev_stop = BEGV;
7725 it->bidi_p = 1;
7726 it->current = save_current;
7727 it->position = save_position;
7728 it->stop_charpos = save_stop_pos;
7729 it->end_charpos = save_end_pos;
7732 /* Scan forward from CHARPOS in the current buffer/string, until we
7733 find a stop position > current IT's position. Then handle the stop
7734 position before that. This is called when we bump into a stop
7735 position while reordering bidirectional text. CHARPOS should be
7736 the last previously processed stop_pos (or BEGV/0, if none were
7737 processed yet) whose position is less that IT's current
7738 position. */
7740 static void
7741 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7743 int bufp = !STRINGP (it->string);
7744 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7745 struct display_pos save_current = it->current;
7746 struct text_pos save_position = it->position;
7747 struct text_pos pos1;
7748 ptrdiff_t next_stop;
7750 /* Scan in strict logical order. */
7751 eassert (it->bidi_p);
7752 it->bidi_p = 0;
7755 it->prev_stop = charpos;
7756 if (bufp)
7758 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7759 reseat_1 (it, pos1, 0);
7761 else
7762 it->current.string_pos = string_pos (charpos, it->string);
7763 compute_stop_pos (it);
7764 /* We must advance forward, right? */
7765 if (it->stop_charpos <= it->prev_stop)
7766 abort ();
7767 charpos = it->stop_charpos;
7769 while (charpos <= where_we_are);
7771 it->bidi_p = 1;
7772 it->current = save_current;
7773 it->position = save_position;
7774 next_stop = it->stop_charpos;
7775 it->stop_charpos = it->prev_stop;
7776 handle_stop (it);
7777 it->stop_charpos = next_stop;
7780 /* Load IT with the next display element from current_buffer. Value
7781 is zero if end of buffer reached. IT->stop_charpos is the next
7782 position at which to stop and check for text properties or buffer
7783 end. */
7785 static int
7786 next_element_from_buffer (struct it *it)
7788 int success_p = 1;
7790 eassert (IT_CHARPOS (*it) >= BEGV);
7791 eassert (NILP (it->string) && !it->s);
7792 eassert (!it->bidi_p
7793 || (EQ (it->bidi_it.string.lstring, Qnil)
7794 && it->bidi_it.string.s == NULL));
7796 /* With bidi reordering, the character to display might not be the
7797 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7798 we were reseat()ed to a new buffer position, which is potentially
7799 a different paragraph. */
7800 if (it->bidi_p && it->bidi_it.first_elt)
7802 get_visually_first_element (it);
7803 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7806 if (IT_CHARPOS (*it) >= it->stop_charpos)
7808 if (IT_CHARPOS (*it) >= it->end_charpos)
7810 int overlay_strings_follow_p;
7812 /* End of the game, except when overlay strings follow that
7813 haven't been returned yet. */
7814 if (it->overlay_strings_at_end_processed_p)
7815 overlay_strings_follow_p = 0;
7816 else
7818 it->overlay_strings_at_end_processed_p = 1;
7819 overlay_strings_follow_p = get_overlay_strings (it, 0);
7822 if (overlay_strings_follow_p)
7823 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7824 else
7826 it->what = IT_EOB;
7827 it->position = it->current.pos;
7828 success_p = 0;
7831 else if (!(!it->bidi_p
7832 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7833 || IT_CHARPOS (*it) == it->stop_charpos))
7835 /* With bidi non-linear iteration, we could find ourselves
7836 far beyond the last computed stop_charpos, with several
7837 other stop positions in between that we missed. Scan
7838 them all now, in buffer's logical order, until we find
7839 and handle the last stop_charpos that precedes our
7840 current position. */
7841 handle_stop_backwards (it, it->stop_charpos);
7842 return GET_NEXT_DISPLAY_ELEMENT (it);
7844 else
7846 if (it->bidi_p)
7848 /* Take note of the stop position we just moved across,
7849 for when we will move back across it. */
7850 it->prev_stop = it->stop_charpos;
7851 /* If we are at base paragraph embedding level, take
7852 note of the last stop position seen at this
7853 level. */
7854 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7855 it->base_level_stop = it->stop_charpos;
7857 handle_stop (it);
7858 return GET_NEXT_DISPLAY_ELEMENT (it);
7861 else if (it->bidi_p
7862 /* If we are before prev_stop, we may have overstepped on
7863 our way backwards a stop_pos, and if so, we need to
7864 handle that stop_pos. */
7865 && IT_CHARPOS (*it) < it->prev_stop
7866 /* We can sometimes back up for reasons that have nothing
7867 to do with bidi reordering. E.g., compositions. The
7868 code below is only needed when we are above the base
7869 embedding level, so test for that explicitly. */
7870 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7872 if (it->base_level_stop <= 0
7873 || IT_CHARPOS (*it) < it->base_level_stop)
7875 /* If we lost track of base_level_stop, we need to find
7876 prev_stop by looking backwards. This happens, e.g., when
7877 we were reseated to the previous screenful of text by
7878 vertical-motion. */
7879 it->base_level_stop = BEGV;
7880 compute_stop_pos_backwards (it);
7881 handle_stop_backwards (it, it->prev_stop);
7883 else
7884 handle_stop_backwards (it, it->base_level_stop);
7885 return GET_NEXT_DISPLAY_ELEMENT (it);
7887 else
7889 /* No face changes, overlays etc. in sight, so just return a
7890 character from current_buffer. */
7891 unsigned char *p;
7892 ptrdiff_t stop;
7894 /* Maybe run the redisplay end trigger hook. Performance note:
7895 This doesn't seem to cost measurable time. */
7896 if (it->redisplay_end_trigger_charpos
7897 && it->glyph_row
7898 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7899 run_redisplay_end_trigger_hook (it);
7901 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7902 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7903 stop)
7904 && next_element_from_composition (it))
7906 return 1;
7909 /* Get the next character, maybe multibyte. */
7910 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7911 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7912 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7913 else
7914 it->c = *p, it->len = 1;
7916 /* Record what we have and where it came from. */
7917 it->what = IT_CHARACTER;
7918 it->object = it->w->buffer;
7919 it->position = it->current.pos;
7921 /* Normally we return the character found above, except when we
7922 really want to return an ellipsis for selective display. */
7923 if (it->selective)
7925 if (it->c == '\n')
7927 /* A value of selective > 0 means hide lines indented more
7928 than that number of columns. */
7929 if (it->selective > 0
7930 && IT_CHARPOS (*it) + 1 < ZV
7931 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7932 IT_BYTEPOS (*it) + 1,
7933 it->selective))
7935 success_p = next_element_from_ellipsis (it);
7936 it->dpvec_char_len = -1;
7939 else if (it->c == '\r' && it->selective == -1)
7941 /* A value of selective == -1 means that everything from the
7942 CR to the end of the line is invisible, with maybe an
7943 ellipsis displayed for it. */
7944 success_p = next_element_from_ellipsis (it);
7945 it->dpvec_char_len = -1;
7950 /* Value is zero if end of buffer reached. */
7951 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7952 return success_p;
7956 /* Run the redisplay end trigger hook for IT. */
7958 static void
7959 run_redisplay_end_trigger_hook (struct it *it)
7961 Lisp_Object args[3];
7963 /* IT->glyph_row should be non-null, i.e. we should be actually
7964 displaying something, or otherwise we should not run the hook. */
7965 eassert (it->glyph_row);
7967 /* Set up hook arguments. */
7968 args[0] = Qredisplay_end_trigger_functions;
7969 args[1] = it->window;
7970 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7971 it->redisplay_end_trigger_charpos = 0;
7973 /* Since we are *trying* to run these functions, don't try to run
7974 them again, even if they get an error. */
7975 it->w->redisplay_end_trigger = Qnil;
7976 Frun_hook_with_args (3, args);
7978 /* Notice if it changed the face of the character we are on. */
7979 handle_face_prop (it);
7983 /* Deliver a composition display element. Unlike the other
7984 next_element_from_XXX, this function is not registered in the array
7985 get_next_element[]. It is called from next_element_from_buffer and
7986 next_element_from_string when necessary. */
7988 static int
7989 next_element_from_composition (struct it *it)
7991 it->what = IT_COMPOSITION;
7992 it->len = it->cmp_it.nbytes;
7993 if (STRINGP (it->string))
7995 if (it->c < 0)
7997 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7998 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7999 return 0;
8001 it->position = it->current.string_pos;
8002 it->object = it->string;
8003 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8004 IT_STRING_BYTEPOS (*it), it->string);
8006 else
8008 if (it->c < 0)
8010 IT_CHARPOS (*it) += it->cmp_it.nchars;
8011 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8012 if (it->bidi_p)
8014 if (it->bidi_it.new_paragraph)
8015 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8016 /* Resync the bidi iterator with IT's new position.
8017 FIXME: this doesn't support bidirectional text. */
8018 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8019 bidi_move_to_visually_next (&it->bidi_it);
8021 return 0;
8023 it->position = it->current.pos;
8024 it->object = it->w->buffer;
8025 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8026 IT_BYTEPOS (*it), Qnil);
8028 return 1;
8033 /***********************************************************************
8034 Moving an iterator without producing glyphs
8035 ***********************************************************************/
8037 /* Check if iterator is at a position corresponding to a valid buffer
8038 position after some move_it_ call. */
8040 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8041 ((it)->method == GET_FROM_STRING \
8042 ? IT_STRING_CHARPOS (*it) == 0 \
8043 : 1)
8046 /* Move iterator IT to a specified buffer or X position within one
8047 line on the display without producing glyphs.
8049 OP should be a bit mask including some or all of these bits:
8050 MOVE_TO_X: Stop upon reaching x-position TO_X.
8051 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8052 Regardless of OP's value, stop upon reaching the end of the display line.
8054 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8055 This means, in particular, that TO_X includes window's horizontal
8056 scroll amount.
8058 The return value has several possible values that
8059 say what condition caused the scan to stop:
8061 MOVE_POS_MATCH_OR_ZV
8062 - when TO_POS or ZV was reached.
8064 MOVE_X_REACHED
8065 -when TO_X was reached before TO_POS or ZV were reached.
8067 MOVE_LINE_CONTINUED
8068 - when we reached the end of the display area and the line must
8069 be continued.
8071 MOVE_LINE_TRUNCATED
8072 - when we reached the end of the display area and the line is
8073 truncated.
8075 MOVE_NEWLINE_OR_CR
8076 - when we stopped at a line end, i.e. a newline or a CR and selective
8077 display is on. */
8079 static enum move_it_result
8080 move_it_in_display_line_to (struct it *it,
8081 ptrdiff_t to_charpos, int to_x,
8082 enum move_operation_enum op)
8084 enum move_it_result result = MOVE_UNDEFINED;
8085 struct glyph_row *saved_glyph_row;
8086 struct it wrap_it, atpos_it, atx_it, ppos_it;
8087 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8088 void *ppos_data = NULL;
8089 int may_wrap = 0;
8090 enum it_method prev_method = it->method;
8091 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8092 int saw_smaller_pos = prev_pos < to_charpos;
8094 /* Don't produce glyphs in produce_glyphs. */
8095 saved_glyph_row = it->glyph_row;
8096 it->glyph_row = NULL;
8098 /* Use wrap_it to save a copy of IT wherever a word wrap could
8099 occur. Use atpos_it to save a copy of IT at the desired buffer
8100 position, if found, so that we can scan ahead and check if the
8101 word later overshoots the window edge. Use atx_it similarly, for
8102 pixel positions. */
8103 wrap_it.sp = -1;
8104 atpos_it.sp = -1;
8105 atx_it.sp = -1;
8107 /* Use ppos_it under bidi reordering to save a copy of IT for the
8108 position > CHARPOS that is the closest to CHARPOS. We restore
8109 that position in IT when we have scanned the entire display line
8110 without finding a match for CHARPOS and all the character
8111 positions are greater than CHARPOS. */
8112 if (it->bidi_p)
8114 SAVE_IT (ppos_it, *it, ppos_data);
8115 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8116 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8117 SAVE_IT (ppos_it, *it, ppos_data);
8120 #define BUFFER_POS_REACHED_P() \
8121 ((op & MOVE_TO_POS) != 0 \
8122 && BUFFERP (it->object) \
8123 && (IT_CHARPOS (*it) == to_charpos \
8124 || ((!it->bidi_p \
8125 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8126 && IT_CHARPOS (*it) > to_charpos) \
8127 || (it->what == IT_COMPOSITION \
8128 && ((IT_CHARPOS (*it) > to_charpos \
8129 && to_charpos >= it->cmp_it.charpos) \
8130 || (IT_CHARPOS (*it) < to_charpos \
8131 && to_charpos <= it->cmp_it.charpos)))) \
8132 && (it->method == GET_FROM_BUFFER \
8133 || (it->method == GET_FROM_DISPLAY_VECTOR \
8134 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8136 /* If there's a line-/wrap-prefix, handle it. */
8137 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8138 && it->current_y < it->last_visible_y)
8139 handle_line_prefix (it);
8141 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8142 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8144 while (1)
8146 int x, i, ascent = 0, descent = 0;
8148 /* Utility macro to reset an iterator with x, ascent, and descent. */
8149 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8150 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8151 (IT)->max_descent = descent)
8153 /* Stop if we move beyond TO_CHARPOS (after an image or a
8154 display string or stretch glyph). */
8155 if ((op & MOVE_TO_POS) != 0
8156 && BUFFERP (it->object)
8157 && it->method == GET_FROM_BUFFER
8158 && (((!it->bidi_p
8159 /* When the iterator is at base embedding level, we
8160 are guaranteed that characters are delivered for
8161 display in strictly increasing order of their
8162 buffer positions. */
8163 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8164 && IT_CHARPOS (*it) > to_charpos)
8165 || (it->bidi_p
8166 && (prev_method == GET_FROM_IMAGE
8167 || prev_method == GET_FROM_STRETCH
8168 || prev_method == GET_FROM_STRING)
8169 /* Passed TO_CHARPOS from left to right. */
8170 && ((prev_pos < to_charpos
8171 && IT_CHARPOS (*it) > to_charpos)
8172 /* Passed TO_CHARPOS from right to left. */
8173 || (prev_pos > to_charpos
8174 && IT_CHARPOS (*it) < to_charpos)))))
8176 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8178 result = MOVE_POS_MATCH_OR_ZV;
8179 break;
8181 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8182 /* If wrap_it is valid, the current position might be in a
8183 word that is wrapped. So, save the iterator in
8184 atpos_it and continue to see if wrapping happens. */
8185 SAVE_IT (atpos_it, *it, atpos_data);
8188 /* Stop when ZV reached.
8189 We used to stop here when TO_CHARPOS reached as well, but that is
8190 too soon if this glyph does not fit on this line. So we handle it
8191 explicitly below. */
8192 if (!get_next_display_element (it))
8194 result = MOVE_POS_MATCH_OR_ZV;
8195 break;
8198 if (it->line_wrap == TRUNCATE)
8200 if (BUFFER_POS_REACHED_P ())
8202 result = MOVE_POS_MATCH_OR_ZV;
8203 break;
8206 else
8208 if (it->line_wrap == WORD_WRAP)
8210 if (IT_DISPLAYING_WHITESPACE (it))
8211 may_wrap = 1;
8212 else if (may_wrap)
8214 /* We have reached a glyph that follows one or more
8215 whitespace characters. If the position is
8216 already found, we are done. */
8217 if (atpos_it.sp >= 0)
8219 RESTORE_IT (it, &atpos_it, atpos_data);
8220 result = MOVE_POS_MATCH_OR_ZV;
8221 goto done;
8223 if (atx_it.sp >= 0)
8225 RESTORE_IT (it, &atx_it, atx_data);
8226 result = MOVE_X_REACHED;
8227 goto done;
8229 /* Otherwise, we can wrap here. */
8230 SAVE_IT (wrap_it, *it, wrap_data);
8231 may_wrap = 0;
8236 /* Remember the line height for the current line, in case
8237 the next element doesn't fit on the line. */
8238 ascent = it->max_ascent;
8239 descent = it->max_descent;
8241 /* The call to produce_glyphs will get the metrics of the
8242 display element IT is loaded with. Record the x-position
8243 before this display element, in case it doesn't fit on the
8244 line. */
8245 x = it->current_x;
8247 PRODUCE_GLYPHS (it);
8249 if (it->area != TEXT_AREA)
8251 prev_method = it->method;
8252 if (it->method == GET_FROM_BUFFER)
8253 prev_pos = IT_CHARPOS (*it);
8254 set_iterator_to_next (it, 1);
8255 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8256 SET_TEXT_POS (this_line_min_pos,
8257 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8258 if (it->bidi_p
8259 && (op & MOVE_TO_POS)
8260 && IT_CHARPOS (*it) > to_charpos
8261 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8262 SAVE_IT (ppos_it, *it, ppos_data);
8263 continue;
8266 /* The number of glyphs we get back in IT->nglyphs will normally
8267 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8268 character on a terminal frame, or (iii) a line end. For the
8269 second case, IT->nglyphs - 1 padding glyphs will be present.
8270 (On X frames, there is only one glyph produced for a
8271 composite character.)
8273 The behavior implemented below means, for continuation lines,
8274 that as many spaces of a TAB as fit on the current line are
8275 displayed there. For terminal frames, as many glyphs of a
8276 multi-glyph character are displayed in the current line, too.
8277 This is what the old redisplay code did, and we keep it that
8278 way. Under X, the whole shape of a complex character must
8279 fit on the line or it will be completely displayed in the
8280 next line.
8282 Note that both for tabs and padding glyphs, all glyphs have
8283 the same width. */
8284 if (it->nglyphs)
8286 /* More than one glyph or glyph doesn't fit on line. All
8287 glyphs have the same width. */
8288 int single_glyph_width = it->pixel_width / it->nglyphs;
8289 int new_x;
8290 int x_before_this_char = x;
8291 int hpos_before_this_char = it->hpos;
8293 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8295 new_x = x + single_glyph_width;
8297 /* We want to leave anything reaching TO_X to the caller. */
8298 if ((op & MOVE_TO_X) && new_x > to_x)
8300 if (BUFFER_POS_REACHED_P ())
8302 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8303 goto buffer_pos_reached;
8304 if (atpos_it.sp < 0)
8306 SAVE_IT (atpos_it, *it, atpos_data);
8307 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8310 else
8312 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8314 it->current_x = x;
8315 result = MOVE_X_REACHED;
8316 break;
8318 if (atx_it.sp < 0)
8320 SAVE_IT (atx_it, *it, atx_data);
8321 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8326 if (/* Lines are continued. */
8327 it->line_wrap != TRUNCATE
8328 && (/* And glyph doesn't fit on the line. */
8329 new_x > it->last_visible_x
8330 /* Or it fits exactly and we're on a window
8331 system frame. */
8332 || (new_x == it->last_visible_x
8333 && FRAME_WINDOW_P (it->f)
8334 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8335 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8336 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8338 if (/* IT->hpos == 0 means the very first glyph
8339 doesn't fit on the line, e.g. a wide image. */
8340 it->hpos == 0
8341 || (new_x == it->last_visible_x
8342 && FRAME_WINDOW_P (it->f)))
8344 ++it->hpos;
8345 it->current_x = new_x;
8347 /* The character's last glyph just barely fits
8348 in this row. */
8349 if (i == it->nglyphs - 1)
8351 /* If this is the destination position,
8352 return a position *before* it in this row,
8353 now that we know it fits in this row. */
8354 if (BUFFER_POS_REACHED_P ())
8356 if (it->line_wrap != WORD_WRAP
8357 || wrap_it.sp < 0)
8359 it->hpos = hpos_before_this_char;
8360 it->current_x = x_before_this_char;
8361 result = MOVE_POS_MATCH_OR_ZV;
8362 break;
8364 if (it->line_wrap == WORD_WRAP
8365 && atpos_it.sp < 0)
8367 SAVE_IT (atpos_it, *it, atpos_data);
8368 atpos_it.current_x = x_before_this_char;
8369 atpos_it.hpos = hpos_before_this_char;
8373 prev_method = it->method;
8374 if (it->method == GET_FROM_BUFFER)
8375 prev_pos = IT_CHARPOS (*it);
8376 set_iterator_to_next (it, 1);
8377 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8378 SET_TEXT_POS (this_line_min_pos,
8379 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8380 /* On graphical terminals, newlines may
8381 "overflow" into the fringe if
8382 overflow-newline-into-fringe is non-nil.
8383 On text terminals, newlines may overflow
8384 into the last glyph on the display
8385 line.*/
8386 if (!FRAME_WINDOW_P (it->f)
8387 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8389 if (!get_next_display_element (it))
8391 result = MOVE_POS_MATCH_OR_ZV;
8392 break;
8394 if (BUFFER_POS_REACHED_P ())
8396 if (ITERATOR_AT_END_OF_LINE_P (it))
8397 result = MOVE_POS_MATCH_OR_ZV;
8398 else
8399 result = MOVE_LINE_CONTINUED;
8400 break;
8402 if (ITERATOR_AT_END_OF_LINE_P (it))
8404 result = MOVE_NEWLINE_OR_CR;
8405 break;
8410 else
8411 IT_RESET_X_ASCENT_DESCENT (it);
8413 if (wrap_it.sp >= 0)
8415 RESTORE_IT (it, &wrap_it, wrap_data);
8416 atpos_it.sp = -1;
8417 atx_it.sp = -1;
8420 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8421 IT_CHARPOS (*it)));
8422 result = MOVE_LINE_CONTINUED;
8423 break;
8426 if (BUFFER_POS_REACHED_P ())
8428 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8429 goto buffer_pos_reached;
8430 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8432 SAVE_IT (atpos_it, *it, atpos_data);
8433 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8437 if (new_x > it->first_visible_x)
8439 /* Glyph is visible. Increment number of glyphs that
8440 would be displayed. */
8441 ++it->hpos;
8445 if (result != MOVE_UNDEFINED)
8446 break;
8448 else if (BUFFER_POS_REACHED_P ())
8450 buffer_pos_reached:
8451 IT_RESET_X_ASCENT_DESCENT (it);
8452 result = MOVE_POS_MATCH_OR_ZV;
8453 break;
8455 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8457 /* Stop when TO_X specified and reached. This check is
8458 necessary here because of lines consisting of a line end,
8459 only. The line end will not produce any glyphs and we
8460 would never get MOVE_X_REACHED. */
8461 eassert (it->nglyphs == 0);
8462 result = MOVE_X_REACHED;
8463 break;
8466 /* Is this a line end? If yes, we're done. */
8467 if (ITERATOR_AT_END_OF_LINE_P (it))
8469 /* If we are past TO_CHARPOS, but never saw any character
8470 positions smaller than TO_CHARPOS, return
8471 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8472 did. */
8473 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8475 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8477 if (IT_CHARPOS (ppos_it) < ZV)
8479 RESTORE_IT (it, &ppos_it, ppos_data);
8480 result = MOVE_POS_MATCH_OR_ZV;
8482 else
8483 goto buffer_pos_reached;
8485 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8486 && IT_CHARPOS (*it) > to_charpos)
8487 goto buffer_pos_reached;
8488 else
8489 result = MOVE_NEWLINE_OR_CR;
8491 else
8492 result = MOVE_NEWLINE_OR_CR;
8493 break;
8496 prev_method = it->method;
8497 if (it->method == GET_FROM_BUFFER)
8498 prev_pos = IT_CHARPOS (*it);
8499 /* The current display element has been consumed. Advance
8500 to the next. */
8501 set_iterator_to_next (it, 1);
8502 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8503 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8504 if (IT_CHARPOS (*it) < to_charpos)
8505 saw_smaller_pos = 1;
8506 if (it->bidi_p
8507 && (op & MOVE_TO_POS)
8508 && IT_CHARPOS (*it) >= to_charpos
8509 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8510 SAVE_IT (ppos_it, *it, ppos_data);
8512 /* Stop if lines are truncated and IT's current x-position is
8513 past the right edge of the window now. */
8514 if (it->line_wrap == TRUNCATE
8515 && it->current_x >= it->last_visible_x)
8517 if (!FRAME_WINDOW_P (it->f)
8518 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8519 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8520 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
8521 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8523 int at_eob_p = 0;
8525 if ((at_eob_p = !get_next_display_element (it))
8526 || BUFFER_POS_REACHED_P ()
8527 /* If we are past TO_CHARPOS, but never saw any
8528 character positions smaller than TO_CHARPOS,
8529 return MOVE_POS_MATCH_OR_ZV, like the
8530 unidirectional display did. */
8531 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8532 && !saw_smaller_pos
8533 && IT_CHARPOS (*it) > to_charpos))
8535 if (it->bidi_p
8536 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8537 RESTORE_IT (it, &ppos_it, ppos_data);
8538 result = MOVE_POS_MATCH_OR_ZV;
8539 break;
8541 if (ITERATOR_AT_END_OF_LINE_P (it))
8543 result = MOVE_NEWLINE_OR_CR;
8544 break;
8547 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8548 && !saw_smaller_pos
8549 && IT_CHARPOS (*it) > to_charpos)
8551 if (IT_CHARPOS (ppos_it) < ZV)
8552 RESTORE_IT (it, &ppos_it, ppos_data);
8553 result = MOVE_POS_MATCH_OR_ZV;
8554 break;
8556 result = MOVE_LINE_TRUNCATED;
8557 break;
8559 #undef IT_RESET_X_ASCENT_DESCENT
8562 #undef BUFFER_POS_REACHED_P
8564 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8565 restore the saved iterator. */
8566 if (atpos_it.sp >= 0)
8567 RESTORE_IT (it, &atpos_it, atpos_data);
8568 else if (atx_it.sp >= 0)
8569 RESTORE_IT (it, &atx_it, atx_data);
8571 done:
8573 if (atpos_data)
8574 bidi_unshelve_cache (atpos_data, 1);
8575 if (atx_data)
8576 bidi_unshelve_cache (atx_data, 1);
8577 if (wrap_data)
8578 bidi_unshelve_cache (wrap_data, 1);
8579 if (ppos_data)
8580 bidi_unshelve_cache (ppos_data, 1);
8582 /* Restore the iterator settings altered at the beginning of this
8583 function. */
8584 it->glyph_row = saved_glyph_row;
8585 return result;
8588 /* For external use. */
8589 void
8590 move_it_in_display_line (struct it *it,
8591 ptrdiff_t to_charpos, int to_x,
8592 enum move_operation_enum op)
8594 if (it->line_wrap == WORD_WRAP
8595 && (op & MOVE_TO_X))
8597 struct it save_it;
8598 void *save_data = NULL;
8599 int skip;
8601 SAVE_IT (save_it, *it, save_data);
8602 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8603 /* When word-wrap is on, TO_X may lie past the end
8604 of a wrapped line. Then it->current is the
8605 character on the next line, so backtrack to the
8606 space before the wrap point. */
8607 if (skip == MOVE_LINE_CONTINUED)
8609 int prev_x = max (it->current_x - 1, 0);
8610 RESTORE_IT (it, &save_it, save_data);
8611 move_it_in_display_line_to
8612 (it, -1, prev_x, MOVE_TO_X);
8614 else
8615 bidi_unshelve_cache (save_data, 1);
8617 else
8618 move_it_in_display_line_to (it, to_charpos, to_x, op);
8622 /* Move IT forward until it satisfies one or more of the criteria in
8623 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8625 OP is a bit-mask that specifies where to stop, and in particular,
8626 which of those four position arguments makes a difference. See the
8627 description of enum move_operation_enum.
8629 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8630 screen line, this function will set IT to the next position that is
8631 displayed to the right of TO_CHARPOS on the screen. */
8633 void
8634 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8636 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8637 int line_height, line_start_x = 0, reached = 0;
8638 void *backup_data = NULL;
8640 for (;;)
8642 if (op & MOVE_TO_VPOS)
8644 /* If no TO_CHARPOS and no TO_X specified, stop at the
8645 start of the line TO_VPOS. */
8646 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8648 if (it->vpos == to_vpos)
8650 reached = 1;
8651 break;
8653 else
8654 skip = move_it_in_display_line_to (it, -1, -1, 0);
8656 else
8658 /* TO_VPOS >= 0 means stop at TO_X in the line at
8659 TO_VPOS, or at TO_POS, whichever comes first. */
8660 if (it->vpos == to_vpos)
8662 reached = 2;
8663 break;
8666 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8668 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8670 reached = 3;
8671 break;
8673 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8675 /* We have reached TO_X but not in the line we want. */
8676 skip = move_it_in_display_line_to (it, to_charpos,
8677 -1, MOVE_TO_POS);
8678 if (skip == MOVE_POS_MATCH_OR_ZV)
8680 reached = 4;
8681 break;
8686 else if (op & MOVE_TO_Y)
8688 struct it it_backup;
8690 if (it->line_wrap == WORD_WRAP)
8691 SAVE_IT (it_backup, *it, backup_data);
8693 /* TO_Y specified means stop at TO_X in the line containing
8694 TO_Y---or at TO_CHARPOS if this is reached first. The
8695 problem is that we can't really tell whether the line
8696 contains TO_Y before we have completely scanned it, and
8697 this may skip past TO_X. What we do is to first scan to
8698 TO_X.
8700 If TO_X is not specified, use a TO_X of zero. The reason
8701 is to make the outcome of this function more predictable.
8702 If we didn't use TO_X == 0, we would stop at the end of
8703 the line which is probably not what a caller would expect
8704 to happen. */
8705 skip = move_it_in_display_line_to
8706 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8707 (MOVE_TO_X | (op & MOVE_TO_POS)));
8709 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8710 if (skip == MOVE_POS_MATCH_OR_ZV)
8711 reached = 5;
8712 else if (skip == MOVE_X_REACHED)
8714 /* If TO_X was reached, we want to know whether TO_Y is
8715 in the line. We know this is the case if the already
8716 scanned glyphs make the line tall enough. Otherwise,
8717 we must check by scanning the rest of the line. */
8718 line_height = it->max_ascent + it->max_descent;
8719 if (to_y >= it->current_y
8720 && to_y < it->current_y + line_height)
8722 reached = 6;
8723 break;
8725 SAVE_IT (it_backup, *it, backup_data);
8726 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8727 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8728 op & MOVE_TO_POS);
8729 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8730 line_height = it->max_ascent + it->max_descent;
8731 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8733 if (to_y >= it->current_y
8734 && to_y < it->current_y + line_height)
8736 /* If TO_Y is in this line and TO_X was reached
8737 above, we scanned too far. We have to restore
8738 IT's settings to the ones before skipping. But
8739 keep the more accurate values of max_ascent and
8740 max_descent we've found while skipping the rest
8741 of the line, for the sake of callers, such as
8742 pos_visible_p, that need to know the line
8743 height. */
8744 int max_ascent = it->max_ascent;
8745 int max_descent = it->max_descent;
8747 RESTORE_IT (it, &it_backup, backup_data);
8748 it->max_ascent = max_ascent;
8749 it->max_descent = max_descent;
8750 reached = 6;
8752 else
8754 skip = skip2;
8755 if (skip == MOVE_POS_MATCH_OR_ZV)
8756 reached = 7;
8759 else
8761 /* Check whether TO_Y is in this line. */
8762 line_height = it->max_ascent + it->max_descent;
8763 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8765 if (to_y >= it->current_y
8766 && to_y < it->current_y + line_height)
8768 /* When word-wrap is on, TO_X may lie past the end
8769 of a wrapped line. Then it->current is the
8770 character on the next line, so backtrack to the
8771 space before the wrap point. */
8772 if (skip == MOVE_LINE_CONTINUED
8773 && it->line_wrap == WORD_WRAP)
8775 int prev_x = max (it->current_x - 1, 0);
8776 RESTORE_IT (it, &it_backup, backup_data);
8777 skip = move_it_in_display_line_to
8778 (it, -1, prev_x, MOVE_TO_X);
8780 reached = 6;
8784 if (reached)
8785 break;
8787 else if (BUFFERP (it->object)
8788 && (it->method == GET_FROM_BUFFER
8789 || it->method == GET_FROM_STRETCH)
8790 && IT_CHARPOS (*it) >= to_charpos
8791 /* Under bidi iteration, a call to set_iterator_to_next
8792 can scan far beyond to_charpos if the initial
8793 portion of the next line needs to be reordered. In
8794 that case, give move_it_in_display_line_to another
8795 chance below. */
8796 && !(it->bidi_p
8797 && it->bidi_it.scan_dir == -1))
8798 skip = MOVE_POS_MATCH_OR_ZV;
8799 else
8800 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8802 switch (skip)
8804 case MOVE_POS_MATCH_OR_ZV:
8805 reached = 8;
8806 goto out;
8808 case MOVE_NEWLINE_OR_CR:
8809 set_iterator_to_next (it, 1);
8810 it->continuation_lines_width = 0;
8811 break;
8813 case MOVE_LINE_TRUNCATED:
8814 it->continuation_lines_width = 0;
8815 reseat_at_next_visible_line_start (it, 0);
8816 if ((op & MOVE_TO_POS) != 0
8817 && IT_CHARPOS (*it) > to_charpos)
8819 reached = 9;
8820 goto out;
8822 break;
8824 case MOVE_LINE_CONTINUED:
8825 /* For continued lines ending in a tab, some of the glyphs
8826 associated with the tab are displayed on the current
8827 line. Since it->current_x does not include these glyphs,
8828 we use it->last_visible_x instead. */
8829 if (it->c == '\t')
8831 it->continuation_lines_width += it->last_visible_x;
8832 /* When moving by vpos, ensure that the iterator really
8833 advances to the next line (bug#847, bug#969). Fixme:
8834 do we need to do this in other circumstances? */
8835 if (it->current_x != it->last_visible_x
8836 && (op & MOVE_TO_VPOS)
8837 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8839 line_start_x = it->current_x + it->pixel_width
8840 - it->last_visible_x;
8841 set_iterator_to_next (it, 0);
8844 else
8845 it->continuation_lines_width += it->current_x;
8846 break;
8848 default:
8849 abort ();
8852 /* Reset/increment for the next run. */
8853 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8854 it->current_x = line_start_x;
8855 line_start_x = 0;
8856 it->hpos = 0;
8857 it->current_y += it->max_ascent + it->max_descent;
8858 ++it->vpos;
8859 last_height = it->max_ascent + it->max_descent;
8860 last_max_ascent = it->max_ascent;
8861 it->max_ascent = it->max_descent = 0;
8864 out:
8866 /* On text terminals, we may stop at the end of a line in the middle
8867 of a multi-character glyph. If the glyph itself is continued,
8868 i.e. it is actually displayed on the next line, don't treat this
8869 stopping point as valid; move to the next line instead (unless
8870 that brings us offscreen). */
8871 if (!FRAME_WINDOW_P (it->f)
8872 && op & MOVE_TO_POS
8873 && IT_CHARPOS (*it) == to_charpos
8874 && it->what == IT_CHARACTER
8875 && it->nglyphs > 1
8876 && it->line_wrap == WINDOW_WRAP
8877 && it->current_x == it->last_visible_x - 1
8878 && it->c != '\n'
8879 && it->c != '\t'
8880 && it->vpos < XFASTINT (it->w->window_end_vpos))
8882 it->continuation_lines_width += it->current_x;
8883 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8884 it->current_y += it->max_ascent + it->max_descent;
8885 ++it->vpos;
8886 last_height = it->max_ascent + it->max_descent;
8887 last_max_ascent = it->max_ascent;
8890 if (backup_data)
8891 bidi_unshelve_cache (backup_data, 1);
8893 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8897 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8899 If DY > 0, move IT backward at least that many pixels. DY = 0
8900 means move IT backward to the preceding line start or BEGV. This
8901 function may move over more than DY pixels if IT->current_y - DY
8902 ends up in the middle of a line; in this case IT->current_y will be
8903 set to the top of the line moved to. */
8905 void
8906 move_it_vertically_backward (struct it *it, int dy)
8908 int nlines, h;
8909 struct it it2, it3;
8910 void *it2data = NULL, *it3data = NULL;
8911 ptrdiff_t start_pos;
8913 move_further_back:
8914 eassert (dy >= 0);
8916 start_pos = IT_CHARPOS (*it);
8918 /* Estimate how many newlines we must move back. */
8919 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8921 /* Set the iterator's position that many lines back. */
8922 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8923 back_to_previous_visible_line_start (it);
8925 /* Reseat the iterator here. When moving backward, we don't want
8926 reseat to skip forward over invisible text, set up the iterator
8927 to deliver from overlay strings at the new position etc. So,
8928 use reseat_1 here. */
8929 reseat_1 (it, it->current.pos, 1);
8931 /* We are now surely at a line start. */
8932 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8933 reordering is in effect. */
8934 it->continuation_lines_width = 0;
8936 /* Move forward and see what y-distance we moved. First move to the
8937 start of the next line so that we get its height. We need this
8938 height to be able to tell whether we reached the specified
8939 y-distance. */
8940 SAVE_IT (it2, *it, it2data);
8941 it2.max_ascent = it2.max_descent = 0;
8944 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8945 MOVE_TO_POS | MOVE_TO_VPOS);
8947 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8948 /* If we are in a display string which starts at START_POS,
8949 and that display string includes a newline, and we are
8950 right after that newline (i.e. at the beginning of a
8951 display line), exit the loop, because otherwise we will
8952 infloop, since move_it_to will see that it is already at
8953 START_POS and will not move. */
8954 || (it2.method == GET_FROM_STRING
8955 && IT_CHARPOS (it2) == start_pos
8956 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
8957 eassert (IT_CHARPOS (*it) >= BEGV);
8958 SAVE_IT (it3, it2, it3data);
8960 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8961 eassert (IT_CHARPOS (*it) >= BEGV);
8962 /* H is the actual vertical distance from the position in *IT
8963 and the starting position. */
8964 h = it2.current_y - it->current_y;
8965 /* NLINES is the distance in number of lines. */
8966 nlines = it2.vpos - it->vpos;
8968 /* Correct IT's y and vpos position
8969 so that they are relative to the starting point. */
8970 it->vpos -= nlines;
8971 it->current_y -= h;
8973 if (dy == 0)
8975 /* DY == 0 means move to the start of the screen line. The
8976 value of nlines is > 0 if continuation lines were involved,
8977 or if the original IT position was at start of a line. */
8978 RESTORE_IT (it, it, it2data);
8979 if (nlines > 0)
8980 move_it_by_lines (it, nlines);
8981 /* The above code moves us to some position NLINES down,
8982 usually to its first glyph (leftmost in an L2R line), but
8983 that's not necessarily the start of the line, under bidi
8984 reordering. We want to get to the character position
8985 that is immediately after the newline of the previous
8986 line. */
8987 if (it->bidi_p
8988 && !it->continuation_lines_width
8989 && !STRINGP (it->string)
8990 && IT_CHARPOS (*it) > BEGV
8991 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8993 ptrdiff_t nl_pos =
8994 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
8996 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
8998 bidi_unshelve_cache (it3data, 1);
9000 else
9002 /* The y-position we try to reach, relative to *IT.
9003 Note that H has been subtracted in front of the if-statement. */
9004 int target_y = it->current_y + h - dy;
9005 int y0 = it3.current_y;
9006 int y1;
9007 int line_height;
9009 RESTORE_IT (&it3, &it3, it3data);
9010 y1 = line_bottom_y (&it3);
9011 line_height = y1 - y0;
9012 RESTORE_IT (it, it, it2data);
9013 /* If we did not reach target_y, try to move further backward if
9014 we can. If we moved too far backward, try to move forward. */
9015 if (target_y < it->current_y
9016 /* This is heuristic. In a window that's 3 lines high, with
9017 a line height of 13 pixels each, recentering with point
9018 on the bottom line will try to move -39/2 = 19 pixels
9019 backward. Try to avoid moving into the first line. */
9020 && (it->current_y - target_y
9021 > min (window_box_height (it->w), line_height * 2 / 3))
9022 && IT_CHARPOS (*it) > BEGV)
9024 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9025 target_y - it->current_y));
9026 dy = it->current_y - target_y;
9027 goto move_further_back;
9029 else if (target_y >= it->current_y + line_height
9030 && IT_CHARPOS (*it) < ZV)
9032 /* Should move forward by at least one line, maybe more.
9034 Note: Calling move_it_by_lines can be expensive on
9035 terminal frames, where compute_motion is used (via
9036 vmotion) to do the job, when there are very long lines
9037 and truncate-lines is nil. That's the reason for
9038 treating terminal frames specially here. */
9040 if (!FRAME_WINDOW_P (it->f))
9041 move_it_vertically (it, target_y - (it->current_y + line_height));
9042 else
9046 move_it_by_lines (it, 1);
9048 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9055 /* Move IT by a specified amount of pixel lines DY. DY negative means
9056 move backwards. DY = 0 means move to start of screen line. At the
9057 end, IT will be on the start of a screen line. */
9059 void
9060 move_it_vertically (struct it *it, int dy)
9062 if (dy <= 0)
9063 move_it_vertically_backward (it, -dy);
9064 else
9066 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9067 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9068 MOVE_TO_POS | MOVE_TO_Y);
9069 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9071 /* If buffer ends in ZV without a newline, move to the start of
9072 the line to satisfy the post-condition. */
9073 if (IT_CHARPOS (*it) == ZV
9074 && ZV > BEGV
9075 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9076 move_it_by_lines (it, 0);
9081 /* Move iterator IT past the end of the text line it is in. */
9083 void
9084 move_it_past_eol (struct it *it)
9086 enum move_it_result rc;
9088 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9089 if (rc == MOVE_NEWLINE_OR_CR)
9090 set_iterator_to_next (it, 0);
9094 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9095 negative means move up. DVPOS == 0 means move to the start of the
9096 screen line.
9098 Optimization idea: If we would know that IT->f doesn't use
9099 a face with proportional font, we could be faster for
9100 truncate-lines nil. */
9102 void
9103 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9106 /* The commented-out optimization uses vmotion on terminals. This
9107 gives bad results, because elements like it->what, on which
9108 callers such as pos_visible_p rely, aren't updated. */
9109 /* struct position pos;
9110 if (!FRAME_WINDOW_P (it->f))
9112 struct text_pos textpos;
9114 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9115 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9116 reseat (it, textpos, 1);
9117 it->vpos += pos.vpos;
9118 it->current_y += pos.vpos;
9120 else */
9122 if (dvpos == 0)
9124 /* DVPOS == 0 means move to the start of the screen line. */
9125 move_it_vertically_backward (it, 0);
9126 /* Let next call to line_bottom_y calculate real line height */
9127 last_height = 0;
9129 else if (dvpos > 0)
9131 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9132 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9134 /* Only move to the next buffer position if we ended up in a
9135 string from display property, not in an overlay string
9136 (before-string or after-string). That is because the
9137 latter don't conceal the underlying buffer position, so
9138 we can ask to move the iterator to the exact position we
9139 are interested in. Note that, even if we are already at
9140 IT_CHARPOS (*it), the call below is not a no-op, as it
9141 will detect that we are at the end of the string, pop the
9142 iterator, and compute it->current_x and it->hpos
9143 correctly. */
9144 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9145 -1, -1, -1, MOVE_TO_POS);
9148 else
9150 struct it it2;
9151 void *it2data = NULL;
9152 ptrdiff_t start_charpos, i;
9154 /* Start at the beginning of the screen line containing IT's
9155 position. This may actually move vertically backwards,
9156 in case of overlays, so adjust dvpos accordingly. */
9157 dvpos += it->vpos;
9158 move_it_vertically_backward (it, 0);
9159 dvpos -= it->vpos;
9161 /* Go back -DVPOS visible lines and reseat the iterator there. */
9162 start_charpos = IT_CHARPOS (*it);
9163 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9164 back_to_previous_visible_line_start (it);
9165 reseat (it, it->current.pos, 1);
9167 /* Move further back if we end up in a string or an image. */
9168 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9170 /* First try to move to start of display line. */
9171 dvpos += it->vpos;
9172 move_it_vertically_backward (it, 0);
9173 dvpos -= it->vpos;
9174 if (IT_POS_VALID_AFTER_MOVE_P (it))
9175 break;
9176 /* If start of line is still in string or image,
9177 move further back. */
9178 back_to_previous_visible_line_start (it);
9179 reseat (it, it->current.pos, 1);
9180 dvpos--;
9183 it->current_x = it->hpos = 0;
9185 /* Above call may have moved too far if continuation lines
9186 are involved. Scan forward and see if it did. */
9187 SAVE_IT (it2, *it, it2data);
9188 it2.vpos = it2.current_y = 0;
9189 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9190 it->vpos -= it2.vpos;
9191 it->current_y -= it2.current_y;
9192 it->current_x = it->hpos = 0;
9194 /* If we moved too far back, move IT some lines forward. */
9195 if (it2.vpos > -dvpos)
9197 int delta = it2.vpos + dvpos;
9199 RESTORE_IT (&it2, &it2, it2data);
9200 SAVE_IT (it2, *it, it2data);
9201 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9202 /* Move back again if we got too far ahead. */
9203 if (IT_CHARPOS (*it) >= start_charpos)
9204 RESTORE_IT (it, &it2, it2data);
9205 else
9206 bidi_unshelve_cache (it2data, 1);
9208 else
9209 RESTORE_IT (it, it, it2data);
9213 /* Return 1 if IT points into the middle of a display vector. */
9216 in_display_vector_p (struct it *it)
9218 return (it->method == GET_FROM_DISPLAY_VECTOR
9219 && it->current.dpvec_index > 0
9220 && it->dpvec + it->current.dpvec_index != it->dpend);
9224 /***********************************************************************
9225 Messages
9226 ***********************************************************************/
9229 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9230 to *Messages*. */
9232 void
9233 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9235 Lisp_Object args[3];
9236 Lisp_Object msg, fmt;
9237 char *buffer;
9238 ptrdiff_t len;
9239 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9240 USE_SAFE_ALLOCA;
9242 /* Do nothing if called asynchronously. Inserting text into
9243 a buffer may call after-change-functions and alike and
9244 that would means running Lisp asynchronously. */
9245 if (handling_signal)
9246 return;
9248 fmt = msg = Qnil;
9249 GCPRO4 (fmt, msg, arg1, arg2);
9251 args[0] = fmt = build_string (format);
9252 args[1] = arg1;
9253 args[2] = arg2;
9254 msg = Fformat (3, args);
9256 len = SBYTES (msg) + 1;
9257 SAFE_ALLOCA (buffer, char *, len);
9258 memcpy (buffer, SDATA (msg), len);
9260 message_dolog (buffer, len - 1, 1, 0);
9261 SAFE_FREE ();
9263 UNGCPRO;
9267 /* Output a newline in the *Messages* buffer if "needs" one. */
9269 void
9270 message_log_maybe_newline (void)
9272 if (message_log_need_newline)
9273 message_dolog ("", 0, 1, 0);
9277 /* Add a string M of length NBYTES to the message log, optionally
9278 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9279 nonzero, means interpret the contents of M as multibyte. This
9280 function calls low-level routines in order to bypass text property
9281 hooks, etc. which might not be safe to run.
9283 This may GC (insert may run before/after change hooks),
9284 so the buffer M must NOT point to a Lisp string. */
9286 void
9287 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9289 const unsigned char *msg = (const unsigned char *) m;
9291 if (!NILP (Vmemory_full))
9292 return;
9294 if (!NILP (Vmessage_log_max))
9296 struct buffer *oldbuf;
9297 Lisp_Object oldpoint, oldbegv, oldzv;
9298 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9299 ptrdiff_t point_at_end = 0;
9300 ptrdiff_t zv_at_end = 0;
9301 Lisp_Object old_deactivate_mark, tem;
9302 struct gcpro gcpro1;
9304 old_deactivate_mark = Vdeactivate_mark;
9305 oldbuf = current_buffer;
9306 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9307 BVAR (current_buffer, undo_list) = Qt;
9309 oldpoint = message_dolog_marker1;
9310 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9311 oldbegv = message_dolog_marker2;
9312 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9313 oldzv = message_dolog_marker3;
9314 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9315 GCPRO1 (old_deactivate_mark);
9317 if (PT == Z)
9318 point_at_end = 1;
9319 if (ZV == Z)
9320 zv_at_end = 1;
9322 BEGV = BEG;
9323 BEGV_BYTE = BEG_BYTE;
9324 ZV = Z;
9325 ZV_BYTE = Z_BYTE;
9326 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9328 /* Insert the string--maybe converting multibyte to single byte
9329 or vice versa, so that all the text fits the buffer. */
9330 if (multibyte
9331 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9333 ptrdiff_t i;
9334 int c, char_bytes;
9335 char work[1];
9337 /* Convert a multibyte string to single-byte
9338 for the *Message* buffer. */
9339 for (i = 0; i < nbytes; i += char_bytes)
9341 c = string_char_and_length (msg + i, &char_bytes);
9342 work[0] = (ASCII_CHAR_P (c)
9344 : multibyte_char_to_unibyte (c));
9345 insert_1_both (work, 1, 1, 1, 0, 0);
9348 else if (! multibyte
9349 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9351 ptrdiff_t i;
9352 int c, char_bytes;
9353 unsigned char str[MAX_MULTIBYTE_LENGTH];
9354 /* Convert a single-byte string to multibyte
9355 for the *Message* buffer. */
9356 for (i = 0; i < nbytes; i++)
9358 c = msg[i];
9359 MAKE_CHAR_MULTIBYTE (c);
9360 char_bytes = CHAR_STRING (c, str);
9361 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9364 else if (nbytes)
9365 insert_1 (m, nbytes, 1, 0, 0);
9367 if (nlflag)
9369 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9370 printmax_t dups;
9371 insert_1 ("\n", 1, 1, 0, 0);
9373 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9374 this_bol = PT;
9375 this_bol_byte = PT_BYTE;
9377 /* See if this line duplicates the previous one.
9378 If so, combine duplicates. */
9379 if (this_bol > BEG)
9381 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9382 prev_bol = PT;
9383 prev_bol_byte = PT_BYTE;
9385 dups = message_log_check_duplicate (prev_bol_byte,
9386 this_bol_byte);
9387 if (dups)
9389 del_range_both (prev_bol, prev_bol_byte,
9390 this_bol, this_bol_byte, 0);
9391 if (dups > 1)
9393 char dupstr[sizeof " [ times]"
9394 + INT_STRLEN_BOUND (printmax_t)];
9396 /* If you change this format, don't forget to also
9397 change message_log_check_duplicate. */
9398 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9399 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9400 insert_1 (dupstr, duplen, 1, 0, 1);
9405 /* If we have more than the desired maximum number of lines
9406 in the *Messages* buffer now, delete the oldest ones.
9407 This is safe because we don't have undo in this buffer. */
9409 if (NATNUMP (Vmessage_log_max))
9411 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9412 -XFASTINT (Vmessage_log_max) - 1, 0);
9413 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9416 BEGV = XMARKER (oldbegv)->charpos;
9417 BEGV_BYTE = marker_byte_position (oldbegv);
9419 if (zv_at_end)
9421 ZV = Z;
9422 ZV_BYTE = Z_BYTE;
9424 else
9426 ZV = XMARKER (oldzv)->charpos;
9427 ZV_BYTE = marker_byte_position (oldzv);
9430 if (point_at_end)
9431 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9432 else
9433 /* We can't do Fgoto_char (oldpoint) because it will run some
9434 Lisp code. */
9435 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9436 XMARKER (oldpoint)->bytepos);
9438 UNGCPRO;
9439 unchain_marker (XMARKER (oldpoint));
9440 unchain_marker (XMARKER (oldbegv));
9441 unchain_marker (XMARKER (oldzv));
9443 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9444 set_buffer_internal (oldbuf);
9445 if (NILP (tem))
9446 windows_or_buffers_changed = old_windows_or_buffers_changed;
9447 message_log_need_newline = !nlflag;
9448 Vdeactivate_mark = old_deactivate_mark;
9453 /* We are at the end of the buffer after just having inserted a newline.
9454 (Note: We depend on the fact we won't be crossing the gap.)
9455 Check to see if the most recent message looks a lot like the previous one.
9456 Return 0 if different, 1 if the new one should just replace it, or a
9457 value N > 1 if we should also append " [N times]". */
9459 static intmax_t
9460 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9462 ptrdiff_t i;
9463 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9464 int seen_dots = 0;
9465 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9466 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9468 for (i = 0; i < len; i++)
9470 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9471 seen_dots = 1;
9472 if (p1[i] != p2[i])
9473 return seen_dots;
9475 p1 += len;
9476 if (*p1 == '\n')
9477 return 2;
9478 if (*p1++ == ' ' && *p1++ == '[')
9480 char *pend;
9481 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9482 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9483 return n+1;
9485 return 0;
9489 /* Display an echo area message M with a specified length of NBYTES
9490 bytes. The string may include null characters. If M is 0, clear
9491 out any existing message, and let the mini-buffer text show
9492 through.
9494 This may GC, so the buffer M must NOT point to a Lisp string. */
9496 void
9497 message2 (const char *m, ptrdiff_t nbytes, int multibyte)
9499 /* First flush out any partial line written with print. */
9500 message_log_maybe_newline ();
9501 if (m)
9502 message_dolog (m, nbytes, 1, multibyte);
9503 message2_nolog (m, nbytes, multibyte);
9507 /* The non-logging counterpart of message2. */
9509 void
9510 message2_nolog (const char *m, ptrdiff_t nbytes, int multibyte)
9512 struct frame *sf = SELECTED_FRAME ();
9513 message_enable_multibyte = multibyte;
9515 if (FRAME_INITIAL_P (sf))
9517 if (noninteractive_need_newline)
9518 putc ('\n', stderr);
9519 noninteractive_need_newline = 0;
9520 if (m)
9521 fwrite (m, nbytes, 1, stderr);
9522 if (cursor_in_echo_area == 0)
9523 fprintf (stderr, "\n");
9524 fflush (stderr);
9526 /* A null message buffer means that the frame hasn't really been
9527 initialized yet. Error messages get reported properly by
9528 cmd_error, so this must be just an informative message; toss it. */
9529 else if (INTERACTIVE
9530 && sf->glyphs_initialized_p
9531 && FRAME_MESSAGE_BUF (sf))
9533 Lisp_Object mini_window;
9534 struct frame *f;
9536 /* Get the frame containing the mini-buffer
9537 that the selected frame is using. */
9538 mini_window = FRAME_MINIBUF_WINDOW (sf);
9539 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9541 FRAME_SAMPLE_VISIBILITY (f);
9542 if (FRAME_VISIBLE_P (sf)
9543 && ! FRAME_VISIBLE_P (f))
9544 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9546 if (m)
9548 set_message (m, Qnil, nbytes, multibyte);
9549 if (minibuffer_auto_raise)
9550 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9552 else
9553 clear_message (1, 1);
9555 do_pending_window_change (0);
9556 echo_area_display (1);
9557 do_pending_window_change (0);
9558 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9559 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9564 /* Display an echo area message M with a specified length of NBYTES
9565 bytes. The string may include null characters. If M is not a
9566 string, clear out any existing message, and let the mini-buffer
9567 text show through.
9569 This function cancels echoing. */
9571 void
9572 message3 (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9574 struct gcpro gcpro1;
9576 GCPRO1 (m);
9577 clear_message (1,1);
9578 cancel_echoing ();
9580 /* First flush out any partial line written with print. */
9581 message_log_maybe_newline ();
9582 if (STRINGP (m))
9584 char *buffer;
9585 USE_SAFE_ALLOCA;
9587 SAFE_ALLOCA (buffer, char *, nbytes);
9588 memcpy (buffer, SDATA (m), nbytes);
9589 message_dolog (buffer, nbytes, 1, multibyte);
9590 SAFE_FREE ();
9592 message3_nolog (m, nbytes, multibyte);
9594 UNGCPRO;
9598 /* The non-logging version of message3.
9599 This does not cancel echoing, because it is used for echoing.
9600 Perhaps we need to make a separate function for echoing
9601 and make this cancel echoing. */
9603 void
9604 message3_nolog (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9606 struct frame *sf = SELECTED_FRAME ();
9607 message_enable_multibyte = multibyte;
9609 if (FRAME_INITIAL_P (sf))
9611 if (noninteractive_need_newline)
9612 putc ('\n', stderr);
9613 noninteractive_need_newline = 0;
9614 if (STRINGP (m))
9615 fwrite (SDATA (m), nbytes, 1, stderr);
9616 if (cursor_in_echo_area == 0)
9617 fprintf (stderr, "\n");
9618 fflush (stderr);
9620 /* A null message buffer means that the frame hasn't really been
9621 initialized yet. Error messages get reported properly by
9622 cmd_error, so this must be just an informative message; toss it. */
9623 else if (INTERACTIVE
9624 && sf->glyphs_initialized_p
9625 && FRAME_MESSAGE_BUF (sf))
9627 Lisp_Object mini_window;
9628 Lisp_Object frame;
9629 struct frame *f;
9631 /* Get the frame containing the mini-buffer
9632 that the selected frame is using. */
9633 mini_window = FRAME_MINIBUF_WINDOW (sf);
9634 frame = XWINDOW (mini_window)->frame;
9635 f = XFRAME (frame);
9637 FRAME_SAMPLE_VISIBILITY (f);
9638 if (FRAME_VISIBLE_P (sf)
9639 && !FRAME_VISIBLE_P (f))
9640 Fmake_frame_visible (frame);
9642 if (STRINGP (m) && SCHARS (m) > 0)
9644 set_message (NULL, m, nbytes, multibyte);
9645 if (minibuffer_auto_raise)
9646 Fraise_frame (frame);
9647 /* Assume we are not echoing.
9648 (If we are, echo_now will override this.) */
9649 echo_message_buffer = Qnil;
9651 else
9652 clear_message (1, 1);
9654 do_pending_window_change (0);
9655 echo_area_display (1);
9656 do_pending_window_change (0);
9657 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9658 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9663 /* Display a null-terminated echo area message M. If M is 0, clear
9664 out any existing message, and let the mini-buffer text show through.
9666 The buffer M must continue to exist until after the echo area gets
9667 cleared or some other message gets displayed there. Do not pass
9668 text that is stored in a Lisp string. Do not pass text in a buffer
9669 that was alloca'd. */
9671 void
9672 message1 (const char *m)
9674 message2 (m, (m ? strlen (m) : 0), 0);
9678 /* The non-logging counterpart of message1. */
9680 void
9681 message1_nolog (const char *m)
9683 message2_nolog (m, (m ? strlen (m) : 0), 0);
9686 /* Display a message M which contains a single %s
9687 which gets replaced with STRING. */
9689 void
9690 message_with_string (const char *m, Lisp_Object string, int log)
9692 CHECK_STRING (string);
9694 if (noninteractive)
9696 if (m)
9698 if (noninteractive_need_newline)
9699 putc ('\n', stderr);
9700 noninteractive_need_newline = 0;
9701 fprintf (stderr, m, SDATA (string));
9702 if (!cursor_in_echo_area)
9703 fprintf (stderr, "\n");
9704 fflush (stderr);
9707 else if (INTERACTIVE)
9709 /* The frame whose minibuffer we're going to display the message on.
9710 It may be larger than the selected frame, so we need
9711 to use its buffer, not the selected frame's buffer. */
9712 Lisp_Object mini_window;
9713 struct frame *f, *sf = SELECTED_FRAME ();
9715 /* Get the frame containing the minibuffer
9716 that the selected frame is using. */
9717 mini_window = FRAME_MINIBUF_WINDOW (sf);
9718 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9720 /* A null message buffer means that the frame hasn't really been
9721 initialized yet. Error messages get reported properly by
9722 cmd_error, so this must be just an informative message; toss it. */
9723 if (FRAME_MESSAGE_BUF (f))
9725 Lisp_Object args[2], msg;
9726 struct gcpro gcpro1, gcpro2;
9728 args[0] = build_string (m);
9729 args[1] = msg = string;
9730 GCPRO2 (args[0], msg);
9731 gcpro1.nvars = 2;
9733 msg = Fformat (2, args);
9735 if (log)
9736 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9737 else
9738 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9740 UNGCPRO;
9742 /* Print should start at the beginning of the message
9743 buffer next time. */
9744 message_buf_print = 0;
9750 /* Dump an informative message to the minibuf. If M is 0, clear out
9751 any existing message, and let the mini-buffer text show through. */
9753 static void
9754 vmessage (const char *m, va_list ap)
9756 if (noninteractive)
9758 if (m)
9760 if (noninteractive_need_newline)
9761 putc ('\n', stderr);
9762 noninteractive_need_newline = 0;
9763 vfprintf (stderr, m, ap);
9764 if (cursor_in_echo_area == 0)
9765 fprintf (stderr, "\n");
9766 fflush (stderr);
9769 else if (INTERACTIVE)
9771 /* The frame whose mini-buffer we're going to display the message
9772 on. It may be larger than the selected frame, so we need to
9773 use its buffer, not the selected frame's buffer. */
9774 Lisp_Object mini_window;
9775 struct frame *f, *sf = SELECTED_FRAME ();
9777 /* Get the frame containing the mini-buffer
9778 that the selected frame is using. */
9779 mini_window = FRAME_MINIBUF_WINDOW (sf);
9780 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9782 /* A null message buffer means that the frame hasn't really been
9783 initialized yet. Error messages get reported properly by
9784 cmd_error, so this must be just an informative message; toss
9785 it. */
9786 if (FRAME_MESSAGE_BUF (f))
9788 if (m)
9790 ptrdiff_t len;
9792 len = doprnt (FRAME_MESSAGE_BUF (f),
9793 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9795 message2 (FRAME_MESSAGE_BUF (f), len, 1);
9797 else
9798 message1 (0);
9800 /* Print should start at the beginning of the message
9801 buffer next time. */
9802 message_buf_print = 0;
9807 void
9808 message (const char *m, ...)
9810 va_list ap;
9811 va_start (ap, m);
9812 vmessage (m, ap);
9813 va_end (ap);
9817 #if 0
9818 /* The non-logging version of message. */
9820 void
9821 message_nolog (const char *m, ...)
9823 Lisp_Object old_log_max;
9824 va_list ap;
9825 va_start (ap, m);
9826 old_log_max = Vmessage_log_max;
9827 Vmessage_log_max = Qnil;
9828 vmessage (m, ap);
9829 Vmessage_log_max = old_log_max;
9830 va_end (ap);
9832 #endif
9835 /* Display the current message in the current mini-buffer. This is
9836 only called from error handlers in process.c, and is not time
9837 critical. */
9839 void
9840 update_echo_area (void)
9842 if (!NILP (echo_area_buffer[0]))
9844 Lisp_Object string;
9845 string = Fcurrent_message ();
9846 message3 (string, SBYTES (string),
9847 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9852 /* Make sure echo area buffers in `echo_buffers' are live.
9853 If they aren't, make new ones. */
9855 static void
9856 ensure_echo_area_buffers (void)
9858 int i;
9860 for (i = 0; i < 2; ++i)
9861 if (!BUFFERP (echo_buffer[i])
9862 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9864 char name[30];
9865 Lisp_Object old_buffer;
9866 int j;
9868 old_buffer = echo_buffer[i];
9869 sprintf (name, " *Echo Area %d*", i);
9870 echo_buffer[i] = Fget_buffer_create (build_string (name));
9871 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9872 /* to force word wrap in echo area -
9873 it was decided to postpone this*/
9874 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9876 for (j = 0; j < 2; ++j)
9877 if (EQ (old_buffer, echo_area_buffer[j]))
9878 echo_area_buffer[j] = echo_buffer[i];
9883 /* Call FN with args A1..A4 with either the current or last displayed
9884 echo_area_buffer as current buffer.
9886 WHICH zero means use the current message buffer
9887 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9888 from echo_buffer[] and clear it.
9890 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9891 suitable buffer from echo_buffer[] and clear it.
9893 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9894 that the current message becomes the last displayed one, make
9895 choose a suitable buffer for echo_area_buffer[0], and clear it.
9897 Value is what FN returns. */
9899 static int
9900 with_echo_area_buffer (struct window *w, int which,
9901 int (*fn) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
9902 ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
9904 Lisp_Object buffer;
9905 int this_one, the_other, clear_buffer_p, rc;
9906 ptrdiff_t count = SPECPDL_INDEX ();
9908 /* If buffers aren't live, make new ones. */
9909 ensure_echo_area_buffers ();
9911 clear_buffer_p = 0;
9913 if (which == 0)
9914 this_one = 0, the_other = 1;
9915 else if (which > 0)
9916 this_one = 1, the_other = 0;
9917 else
9919 this_one = 0, the_other = 1;
9920 clear_buffer_p = 1;
9922 /* We need a fresh one in case the current echo buffer equals
9923 the one containing the last displayed echo area message. */
9924 if (!NILP (echo_area_buffer[this_one])
9925 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9926 echo_area_buffer[this_one] = Qnil;
9929 /* Choose a suitable buffer from echo_buffer[] is we don't
9930 have one. */
9931 if (NILP (echo_area_buffer[this_one]))
9933 echo_area_buffer[this_one]
9934 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9935 ? echo_buffer[the_other]
9936 : echo_buffer[this_one]);
9937 clear_buffer_p = 1;
9940 buffer = echo_area_buffer[this_one];
9942 /* Don't get confused by reusing the buffer used for echoing
9943 for a different purpose. */
9944 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9945 cancel_echoing ();
9947 record_unwind_protect (unwind_with_echo_area_buffer,
9948 with_echo_area_buffer_unwind_data (w));
9950 /* Make the echo area buffer current. Note that for display
9951 purposes, it is not necessary that the displayed window's buffer
9952 == current_buffer, except for text property lookup. So, let's
9953 only set that buffer temporarily here without doing a full
9954 Fset_window_buffer. We must also change w->pointm, though,
9955 because otherwise an assertions in unshow_buffer fails, and Emacs
9956 aborts. */
9957 set_buffer_internal_1 (XBUFFER (buffer));
9958 if (w)
9960 w->buffer = buffer;
9961 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9964 BVAR (current_buffer, undo_list) = Qt;
9965 BVAR (current_buffer, read_only) = Qnil;
9966 specbind (Qinhibit_read_only, Qt);
9967 specbind (Qinhibit_modification_hooks, Qt);
9969 if (clear_buffer_p && Z > BEG)
9970 del_range (BEG, Z);
9972 eassert (BEGV >= BEG);
9973 eassert (ZV <= Z && ZV >= BEGV);
9975 rc = fn (a1, a2, a3, a4);
9977 eassert (BEGV >= BEG);
9978 eassert (ZV <= Z && ZV >= BEGV);
9980 unbind_to (count, Qnil);
9981 return rc;
9985 /* Save state that should be preserved around the call to the function
9986 FN called in with_echo_area_buffer. */
9988 static Lisp_Object
9989 with_echo_area_buffer_unwind_data (struct window *w)
9991 int i = 0;
9992 Lisp_Object vector, tmp;
9994 /* Reduce consing by keeping one vector in
9995 Vwith_echo_area_save_vector. */
9996 vector = Vwith_echo_area_save_vector;
9997 Vwith_echo_area_save_vector = Qnil;
9999 if (NILP (vector))
10000 vector = Fmake_vector (make_number (7), Qnil);
10002 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10003 ASET (vector, i, Vdeactivate_mark); ++i;
10004 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10006 if (w)
10008 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10009 ASET (vector, i, w->buffer); ++i;
10010 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
10011 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
10013 else
10015 int end = i + 4;
10016 for (; i < end; ++i)
10017 ASET (vector, i, Qnil);
10020 eassert (i == ASIZE (vector));
10021 return vector;
10025 /* Restore global state from VECTOR which was created by
10026 with_echo_area_buffer_unwind_data. */
10028 static Lisp_Object
10029 unwind_with_echo_area_buffer (Lisp_Object vector)
10031 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10032 Vdeactivate_mark = AREF (vector, 1);
10033 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10035 if (WINDOWP (AREF (vector, 3)))
10037 struct window *w;
10038 Lisp_Object buffer, charpos, bytepos;
10040 w = XWINDOW (AREF (vector, 3));
10041 buffer = AREF (vector, 4);
10042 charpos = AREF (vector, 5);
10043 bytepos = AREF (vector, 6);
10045 w->buffer = buffer;
10046 set_marker_both (w->pointm, buffer,
10047 XFASTINT (charpos), XFASTINT (bytepos));
10050 Vwith_echo_area_save_vector = vector;
10051 return Qnil;
10055 /* Set up the echo area for use by print functions. MULTIBYTE_P
10056 non-zero means we will print multibyte. */
10058 void
10059 setup_echo_area_for_printing (int multibyte_p)
10061 /* If we can't find an echo area any more, exit. */
10062 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10063 Fkill_emacs (Qnil);
10065 ensure_echo_area_buffers ();
10067 if (!message_buf_print)
10069 /* A message has been output since the last time we printed.
10070 Choose a fresh echo area buffer. */
10071 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10072 echo_area_buffer[0] = echo_buffer[1];
10073 else
10074 echo_area_buffer[0] = echo_buffer[0];
10076 /* Switch to that buffer and clear it. */
10077 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10078 BVAR (current_buffer, truncate_lines) = Qnil;
10080 if (Z > BEG)
10082 ptrdiff_t count = SPECPDL_INDEX ();
10083 specbind (Qinhibit_read_only, Qt);
10084 /* Note that undo recording is always disabled. */
10085 del_range (BEG, Z);
10086 unbind_to (count, Qnil);
10088 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10090 /* Set up the buffer for the multibyteness we need. */
10091 if (multibyte_p
10092 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10093 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10095 /* Raise the frame containing the echo area. */
10096 if (minibuffer_auto_raise)
10098 struct frame *sf = SELECTED_FRAME ();
10099 Lisp_Object mini_window;
10100 mini_window = FRAME_MINIBUF_WINDOW (sf);
10101 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10104 message_log_maybe_newline ();
10105 message_buf_print = 1;
10107 else
10109 if (NILP (echo_area_buffer[0]))
10111 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10112 echo_area_buffer[0] = echo_buffer[1];
10113 else
10114 echo_area_buffer[0] = echo_buffer[0];
10117 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10119 /* Someone switched buffers between print requests. */
10120 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10121 BVAR (current_buffer, truncate_lines) = Qnil;
10127 /* Display an echo area message in window W. Value is non-zero if W's
10128 height is changed. If display_last_displayed_message_p is
10129 non-zero, display the message that was last displayed, otherwise
10130 display the current message. */
10132 static int
10133 display_echo_area (struct window *w)
10135 int i, no_message_p, window_height_changed_p;
10137 /* Temporarily disable garbage collections while displaying the echo
10138 area. This is done because a GC can print a message itself.
10139 That message would modify the echo area buffer's contents while a
10140 redisplay of the buffer is going on, and seriously confuse
10141 redisplay. */
10142 ptrdiff_t count = inhibit_garbage_collection ();
10144 /* If there is no message, we must call display_echo_area_1
10145 nevertheless because it resizes the window. But we will have to
10146 reset the echo_area_buffer in question to nil at the end because
10147 with_echo_area_buffer will sets it to an empty buffer. */
10148 i = display_last_displayed_message_p ? 1 : 0;
10149 no_message_p = NILP (echo_area_buffer[i]);
10151 window_height_changed_p
10152 = with_echo_area_buffer (w, display_last_displayed_message_p,
10153 display_echo_area_1,
10154 (intptr_t) w, Qnil, 0, 0);
10156 if (no_message_p)
10157 echo_area_buffer[i] = Qnil;
10159 unbind_to (count, Qnil);
10160 return window_height_changed_p;
10164 /* Helper for display_echo_area. Display the current buffer which
10165 contains the current echo area message in window W, a mini-window,
10166 a pointer to which is passed in A1. A2..A4 are currently not used.
10167 Change the height of W so that all of the message is displayed.
10168 Value is non-zero if height of W was changed. */
10170 static int
10171 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10173 intptr_t i1 = a1;
10174 struct window *w = (struct window *) i1;
10175 Lisp_Object window;
10176 struct text_pos start;
10177 int window_height_changed_p = 0;
10179 /* Do this before displaying, so that we have a large enough glyph
10180 matrix for the display. If we can't get enough space for the
10181 whole text, display the last N lines. That works by setting w->start. */
10182 window_height_changed_p = resize_mini_window (w, 0);
10184 /* Use the starting position chosen by resize_mini_window. */
10185 SET_TEXT_POS_FROM_MARKER (start, w->start);
10187 /* Display. */
10188 clear_glyph_matrix (w->desired_matrix);
10189 XSETWINDOW (window, w);
10190 try_window (window, start, 0);
10192 return window_height_changed_p;
10196 /* Resize the echo area window to exactly the size needed for the
10197 currently displayed message, if there is one. If a mini-buffer
10198 is active, don't shrink it. */
10200 void
10201 resize_echo_area_exactly (void)
10203 if (BUFFERP (echo_area_buffer[0])
10204 && WINDOWP (echo_area_window))
10206 struct window *w = XWINDOW (echo_area_window);
10207 int resized_p;
10208 Lisp_Object resize_exactly;
10210 if (minibuf_level == 0)
10211 resize_exactly = Qt;
10212 else
10213 resize_exactly = Qnil;
10215 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10216 (intptr_t) w, resize_exactly,
10217 0, 0);
10218 if (resized_p)
10220 ++windows_or_buffers_changed;
10221 ++update_mode_lines;
10222 redisplay_internal ();
10228 /* Callback function for with_echo_area_buffer, when used from
10229 resize_echo_area_exactly. A1 contains a pointer to the window to
10230 resize, EXACTLY non-nil means resize the mini-window exactly to the
10231 size of the text displayed. A3 and A4 are not used. Value is what
10232 resize_mini_window returns. */
10234 static int
10235 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly, ptrdiff_t a3, ptrdiff_t a4)
10237 intptr_t i1 = a1;
10238 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10242 /* Resize mini-window W to fit the size of its contents. EXACT_P
10243 means size the window exactly to the size needed. Otherwise, it's
10244 only enlarged until W's buffer is empty.
10246 Set W->start to the right place to begin display. If the whole
10247 contents fit, start at the beginning. Otherwise, start so as
10248 to make the end of the contents appear. This is particularly
10249 important for y-or-n-p, but seems desirable generally.
10251 Value is non-zero if the window height has been changed. */
10254 resize_mini_window (struct window *w, int exact_p)
10256 struct frame *f = XFRAME (w->frame);
10257 int window_height_changed_p = 0;
10259 eassert (MINI_WINDOW_P (w));
10261 /* By default, start display at the beginning. */
10262 set_marker_both (w->start, w->buffer,
10263 BUF_BEGV (XBUFFER (w->buffer)),
10264 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10266 /* Don't resize windows while redisplaying a window; it would
10267 confuse redisplay functions when the size of the window they are
10268 displaying changes from under them. Such a resizing can happen,
10269 for instance, when which-func prints a long message while
10270 we are running fontification-functions. We're running these
10271 functions with safe_call which binds inhibit-redisplay to t. */
10272 if (!NILP (Vinhibit_redisplay))
10273 return 0;
10275 /* Nil means don't try to resize. */
10276 if (NILP (Vresize_mini_windows)
10277 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10278 return 0;
10280 if (!FRAME_MINIBUF_ONLY_P (f))
10282 struct it it;
10283 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10284 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10285 int height;
10286 EMACS_INT max_height;
10287 int unit = FRAME_LINE_HEIGHT (f);
10288 struct text_pos start;
10289 struct buffer *old_current_buffer = NULL;
10291 if (current_buffer != XBUFFER (w->buffer))
10293 old_current_buffer = current_buffer;
10294 set_buffer_internal (XBUFFER (w->buffer));
10297 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10299 /* Compute the max. number of lines specified by the user. */
10300 if (FLOATP (Vmax_mini_window_height))
10301 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10302 else if (INTEGERP (Vmax_mini_window_height))
10303 max_height = XINT (Vmax_mini_window_height);
10304 else
10305 max_height = total_height / 4;
10307 /* Correct that max. height if it's bogus. */
10308 max_height = max (1, max_height);
10309 max_height = min (total_height, max_height);
10311 /* Find out the height of the text in the window. */
10312 if (it.line_wrap == TRUNCATE)
10313 height = 1;
10314 else
10316 last_height = 0;
10317 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10318 if (it.max_ascent == 0 && it.max_descent == 0)
10319 height = it.current_y + last_height;
10320 else
10321 height = it.current_y + it.max_ascent + it.max_descent;
10322 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10323 height = (height + unit - 1) / unit;
10326 /* Compute a suitable window start. */
10327 if (height > max_height)
10329 height = max_height;
10330 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10331 move_it_vertically_backward (&it, (height - 1) * unit);
10332 start = it.current.pos;
10334 else
10335 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10336 SET_MARKER_FROM_TEXT_POS (w->start, start);
10338 if (EQ (Vresize_mini_windows, Qgrow_only))
10340 /* Let it grow only, until we display an empty message, in which
10341 case the window shrinks again. */
10342 if (height > WINDOW_TOTAL_LINES (w))
10344 int old_height = WINDOW_TOTAL_LINES (w);
10345 freeze_window_starts (f, 1);
10346 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10347 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10349 else if (height < WINDOW_TOTAL_LINES (w)
10350 && (exact_p || BEGV == ZV))
10352 int old_height = WINDOW_TOTAL_LINES (w);
10353 freeze_window_starts (f, 0);
10354 shrink_mini_window (w);
10355 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10358 else
10360 /* Always resize to exact size needed. */
10361 if (height > WINDOW_TOTAL_LINES (w))
10363 int old_height = WINDOW_TOTAL_LINES (w);
10364 freeze_window_starts (f, 1);
10365 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10366 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10368 else if (height < WINDOW_TOTAL_LINES (w))
10370 int old_height = WINDOW_TOTAL_LINES (w);
10371 freeze_window_starts (f, 0);
10372 shrink_mini_window (w);
10374 if (height)
10376 freeze_window_starts (f, 1);
10377 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10380 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10384 if (old_current_buffer)
10385 set_buffer_internal (old_current_buffer);
10388 return window_height_changed_p;
10392 /* Value is the current message, a string, or nil if there is no
10393 current message. */
10395 Lisp_Object
10396 current_message (void)
10398 Lisp_Object msg;
10400 if (!BUFFERP (echo_area_buffer[0]))
10401 msg = Qnil;
10402 else
10404 with_echo_area_buffer (0, 0, current_message_1,
10405 (intptr_t) &msg, Qnil, 0, 0);
10406 if (NILP (msg))
10407 echo_area_buffer[0] = Qnil;
10410 return msg;
10414 static int
10415 current_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10417 intptr_t i1 = a1;
10418 Lisp_Object *msg = (Lisp_Object *) i1;
10420 if (Z > BEG)
10421 *msg = make_buffer_string (BEG, Z, 1);
10422 else
10423 *msg = Qnil;
10424 return 0;
10428 /* Push the current message on Vmessage_stack for later restoration
10429 by restore_message. Value is non-zero if the current message isn't
10430 empty. This is a relatively infrequent operation, so it's not
10431 worth optimizing. */
10434 push_message (void)
10436 Lisp_Object msg;
10437 msg = current_message ();
10438 Vmessage_stack = Fcons (msg, Vmessage_stack);
10439 return STRINGP (msg);
10443 /* Restore message display from the top of Vmessage_stack. */
10445 void
10446 restore_message (void)
10448 Lisp_Object msg;
10450 eassert (CONSP (Vmessage_stack));
10451 msg = XCAR (Vmessage_stack);
10452 if (STRINGP (msg))
10453 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10454 else
10455 message3_nolog (msg, 0, 0);
10459 /* Handler for record_unwind_protect calling pop_message. */
10461 Lisp_Object
10462 pop_message_unwind (Lisp_Object dummy)
10464 pop_message ();
10465 return Qnil;
10468 /* Pop the top-most entry off Vmessage_stack. */
10470 static void
10471 pop_message (void)
10473 eassert (CONSP (Vmessage_stack));
10474 Vmessage_stack = XCDR (Vmessage_stack);
10478 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10479 exits. If the stack is not empty, we have a missing pop_message
10480 somewhere. */
10482 void
10483 check_message_stack (void)
10485 if (!NILP (Vmessage_stack))
10486 abort ();
10490 /* Truncate to NCHARS what will be displayed in the echo area the next
10491 time we display it---but don't redisplay it now. */
10493 void
10494 truncate_echo_area (ptrdiff_t nchars)
10496 if (nchars == 0)
10497 echo_area_buffer[0] = Qnil;
10498 /* A null message buffer means that the frame hasn't really been
10499 initialized yet. Error messages get reported properly by
10500 cmd_error, so this must be just an informative message; toss it. */
10501 else if (!noninteractive
10502 && INTERACTIVE
10503 && !NILP (echo_area_buffer[0]))
10505 struct frame *sf = SELECTED_FRAME ();
10506 if (FRAME_MESSAGE_BUF (sf))
10507 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10512 /* Helper function for truncate_echo_area. Truncate the current
10513 message to at most NCHARS characters. */
10515 static int
10516 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10518 if (BEG + nchars < Z)
10519 del_range (BEG + nchars, Z);
10520 if (Z == BEG)
10521 echo_area_buffer[0] = Qnil;
10522 return 0;
10526 /* Set the current message to a substring of S or STRING.
10528 If STRING is a Lisp string, set the message to the first NBYTES
10529 bytes from STRING. NBYTES zero means use the whole string. If
10530 STRING is multibyte, the message will be displayed multibyte.
10532 If S is not null, set the message to the first LEN bytes of S. LEN
10533 zero means use the whole string. MULTIBYTE_P non-zero means S is
10534 multibyte. Display the message multibyte in that case.
10536 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10537 to t before calling set_message_1 (which calls insert).
10540 static void
10541 set_message (const char *s, Lisp_Object string,
10542 ptrdiff_t nbytes, int multibyte_p)
10544 message_enable_multibyte
10545 = ((s && multibyte_p)
10546 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10548 with_echo_area_buffer (0, -1, set_message_1,
10549 (intptr_t) s, string, nbytes, multibyte_p);
10550 message_buf_print = 0;
10551 help_echo_showing_p = 0;
10555 /* Helper function for set_message. Arguments have the same meaning
10556 as there, with A1 corresponding to S and A2 corresponding to STRING
10557 This function is called with the echo area buffer being
10558 current. */
10560 static int
10561 set_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t nbytes, ptrdiff_t multibyte_p)
10563 intptr_t i1 = a1;
10564 const char *s = (const char *) i1;
10565 const unsigned char *msg = (const unsigned char *) s;
10566 Lisp_Object string = a2;
10568 /* Change multibyteness of the echo buffer appropriately. */
10569 if (message_enable_multibyte
10570 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10571 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10573 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10574 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10575 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10577 /* Insert new message at BEG. */
10578 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10580 if (STRINGP (string))
10582 ptrdiff_t nchars;
10584 if (nbytes == 0)
10585 nbytes = SBYTES (string);
10586 nchars = string_byte_to_char (string, nbytes);
10588 /* This function takes care of single/multibyte conversion. We
10589 just have to ensure that the echo area buffer has the right
10590 setting of enable_multibyte_characters. */
10591 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10593 else if (s)
10595 if (nbytes == 0)
10596 nbytes = strlen (s);
10598 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10600 /* Convert from multi-byte to single-byte. */
10601 ptrdiff_t i;
10602 int c, n;
10603 char work[1];
10605 /* Convert a multibyte string to single-byte. */
10606 for (i = 0; i < nbytes; i += n)
10608 c = string_char_and_length (msg + i, &n);
10609 work[0] = (ASCII_CHAR_P (c)
10611 : multibyte_char_to_unibyte (c));
10612 insert_1_both (work, 1, 1, 1, 0, 0);
10615 else if (!multibyte_p
10616 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10618 /* Convert from single-byte to multi-byte. */
10619 ptrdiff_t i;
10620 int c, n;
10621 unsigned char str[MAX_MULTIBYTE_LENGTH];
10623 /* Convert a single-byte string to multibyte. */
10624 for (i = 0; i < nbytes; i++)
10626 c = msg[i];
10627 MAKE_CHAR_MULTIBYTE (c);
10628 n = CHAR_STRING (c, str);
10629 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10632 else
10633 insert_1 (s, nbytes, 1, 0, 0);
10636 return 0;
10640 /* Clear messages. CURRENT_P non-zero means clear the current
10641 message. LAST_DISPLAYED_P non-zero means clear the message
10642 last displayed. */
10644 void
10645 clear_message (int current_p, int last_displayed_p)
10647 if (current_p)
10649 echo_area_buffer[0] = Qnil;
10650 message_cleared_p = 1;
10653 if (last_displayed_p)
10654 echo_area_buffer[1] = Qnil;
10656 message_buf_print = 0;
10659 /* Clear garbaged frames.
10661 This function is used where the old redisplay called
10662 redraw_garbaged_frames which in turn called redraw_frame which in
10663 turn called clear_frame. The call to clear_frame was a source of
10664 flickering. I believe a clear_frame is not necessary. It should
10665 suffice in the new redisplay to invalidate all current matrices,
10666 and ensure a complete redisplay of all windows. */
10668 static void
10669 clear_garbaged_frames (void)
10671 if (frame_garbaged)
10673 Lisp_Object tail, frame;
10674 int changed_count = 0;
10676 FOR_EACH_FRAME (tail, frame)
10678 struct frame *f = XFRAME (frame);
10680 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10682 if (f->resized_p)
10684 Fredraw_frame (frame);
10685 f->force_flush_display_p = 1;
10687 clear_current_matrices (f);
10688 changed_count++;
10689 f->garbaged = 0;
10690 f->resized_p = 0;
10694 frame_garbaged = 0;
10695 if (changed_count)
10696 ++windows_or_buffers_changed;
10701 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10702 is non-zero update selected_frame. Value is non-zero if the
10703 mini-windows height has been changed. */
10705 static int
10706 echo_area_display (int update_frame_p)
10708 Lisp_Object mini_window;
10709 struct window *w;
10710 struct frame *f;
10711 int window_height_changed_p = 0;
10712 struct frame *sf = SELECTED_FRAME ();
10714 mini_window = FRAME_MINIBUF_WINDOW (sf);
10715 w = XWINDOW (mini_window);
10716 f = XFRAME (WINDOW_FRAME (w));
10718 /* Don't display if frame is invisible or not yet initialized. */
10719 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10720 return 0;
10722 #ifdef HAVE_WINDOW_SYSTEM
10723 /* When Emacs starts, selected_frame may be the initial terminal
10724 frame. If we let this through, a message would be displayed on
10725 the terminal. */
10726 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10727 return 0;
10728 #endif /* HAVE_WINDOW_SYSTEM */
10730 /* Redraw garbaged frames. */
10731 if (frame_garbaged)
10732 clear_garbaged_frames ();
10734 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10736 echo_area_window = mini_window;
10737 window_height_changed_p = display_echo_area (w);
10738 w->must_be_updated_p = 1;
10740 /* Update the display, unless called from redisplay_internal.
10741 Also don't update the screen during redisplay itself. The
10742 update will happen at the end of redisplay, and an update
10743 here could cause confusion. */
10744 if (update_frame_p && !redisplaying_p)
10746 int n = 0;
10748 /* If the display update has been interrupted by pending
10749 input, update mode lines in the frame. Due to the
10750 pending input, it might have been that redisplay hasn't
10751 been called, so that mode lines above the echo area are
10752 garbaged. This looks odd, so we prevent it here. */
10753 if (!display_completed)
10754 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10756 if (window_height_changed_p
10757 /* Don't do this if Emacs is shutting down. Redisplay
10758 needs to run hooks. */
10759 && !NILP (Vrun_hooks))
10761 /* Must update other windows. Likewise as in other
10762 cases, don't let this update be interrupted by
10763 pending input. */
10764 ptrdiff_t count = SPECPDL_INDEX ();
10765 specbind (Qredisplay_dont_pause, Qt);
10766 windows_or_buffers_changed = 1;
10767 redisplay_internal ();
10768 unbind_to (count, Qnil);
10770 else if (FRAME_WINDOW_P (f) && n == 0)
10772 /* Window configuration is the same as before.
10773 Can do with a display update of the echo area,
10774 unless we displayed some mode lines. */
10775 update_single_window (w, 1);
10776 FRAME_RIF (f)->flush_display (f);
10778 else
10779 update_frame (f, 1, 1);
10781 /* If cursor is in the echo area, make sure that the next
10782 redisplay displays the minibuffer, so that the cursor will
10783 be replaced with what the minibuffer wants. */
10784 if (cursor_in_echo_area)
10785 ++windows_or_buffers_changed;
10788 else if (!EQ (mini_window, selected_window))
10789 windows_or_buffers_changed++;
10791 /* Last displayed message is now the current message. */
10792 echo_area_buffer[1] = echo_area_buffer[0];
10793 /* Inform read_char that we're not echoing. */
10794 echo_message_buffer = Qnil;
10796 /* Prevent redisplay optimization in redisplay_internal by resetting
10797 this_line_start_pos. This is done because the mini-buffer now
10798 displays the message instead of its buffer text. */
10799 if (EQ (mini_window, selected_window))
10800 CHARPOS (this_line_start_pos) = 0;
10802 return window_height_changed_p;
10807 /***********************************************************************
10808 Mode Lines and Frame Titles
10809 ***********************************************************************/
10811 /* A buffer for constructing non-propertized mode-line strings and
10812 frame titles in it; allocated from the heap in init_xdisp and
10813 resized as needed in store_mode_line_noprop_char. */
10815 static char *mode_line_noprop_buf;
10817 /* The buffer's end, and a current output position in it. */
10819 static char *mode_line_noprop_buf_end;
10820 static char *mode_line_noprop_ptr;
10822 #define MODE_LINE_NOPROP_LEN(start) \
10823 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10825 static enum {
10826 MODE_LINE_DISPLAY = 0,
10827 MODE_LINE_TITLE,
10828 MODE_LINE_NOPROP,
10829 MODE_LINE_STRING
10830 } mode_line_target;
10832 /* Alist that caches the results of :propertize.
10833 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10834 static Lisp_Object mode_line_proptrans_alist;
10836 /* List of strings making up the mode-line. */
10837 static Lisp_Object mode_line_string_list;
10839 /* Base face property when building propertized mode line string. */
10840 static Lisp_Object mode_line_string_face;
10841 static Lisp_Object mode_line_string_face_prop;
10844 /* Unwind data for mode line strings */
10846 static Lisp_Object Vmode_line_unwind_vector;
10848 static Lisp_Object
10849 format_mode_line_unwind_data (struct frame *target_frame,
10850 struct buffer *obuf,
10851 Lisp_Object owin,
10852 int save_proptrans)
10854 Lisp_Object vector, tmp;
10856 /* Reduce consing by keeping one vector in
10857 Vwith_echo_area_save_vector. */
10858 vector = Vmode_line_unwind_vector;
10859 Vmode_line_unwind_vector = Qnil;
10861 if (NILP (vector))
10862 vector = Fmake_vector (make_number (10), Qnil);
10864 ASET (vector, 0, make_number (mode_line_target));
10865 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10866 ASET (vector, 2, mode_line_string_list);
10867 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10868 ASET (vector, 4, mode_line_string_face);
10869 ASET (vector, 5, mode_line_string_face_prop);
10871 if (obuf)
10872 XSETBUFFER (tmp, obuf);
10873 else
10874 tmp = Qnil;
10875 ASET (vector, 6, tmp);
10876 ASET (vector, 7, owin);
10877 if (target_frame)
10879 /* Similarly to `with-selected-window', if the operation selects
10880 a window on another frame, we must restore that frame's
10881 selected window, and (for a tty) the top-frame. */
10882 ASET (vector, 8, target_frame->selected_window);
10883 if (FRAME_TERMCAP_P (target_frame))
10884 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10887 return vector;
10890 static Lisp_Object
10891 unwind_format_mode_line (Lisp_Object vector)
10893 Lisp_Object old_window = AREF (vector, 7);
10894 Lisp_Object target_frame_window = AREF (vector, 8);
10895 Lisp_Object old_top_frame = AREF (vector, 9);
10897 mode_line_target = XINT (AREF (vector, 0));
10898 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10899 mode_line_string_list = AREF (vector, 2);
10900 if (! EQ (AREF (vector, 3), Qt))
10901 mode_line_proptrans_alist = AREF (vector, 3);
10902 mode_line_string_face = AREF (vector, 4);
10903 mode_line_string_face_prop = AREF (vector, 5);
10905 /* Select window before buffer, since it may change the buffer. */
10906 if (!NILP (old_window))
10908 /* If the operation that we are unwinding had selected a window
10909 on a different frame, reset its frame-selected-window. For a
10910 text terminal, reset its top-frame if necessary. */
10911 if (!NILP (target_frame_window))
10913 Lisp_Object frame
10914 = WINDOW_FRAME (XWINDOW (target_frame_window));
10916 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
10917 Fselect_window (target_frame_window, Qt);
10919 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
10920 Fselect_frame (old_top_frame, Qt);
10923 Fselect_window (old_window, Qt);
10926 if (!NILP (AREF (vector, 6)))
10928 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10929 ASET (vector, 6, Qnil);
10932 Vmode_line_unwind_vector = vector;
10933 return Qnil;
10937 /* Store a single character C for the frame title in mode_line_noprop_buf.
10938 Re-allocate mode_line_noprop_buf if necessary. */
10940 static void
10941 store_mode_line_noprop_char (char c)
10943 /* If output position has reached the end of the allocated buffer,
10944 increase the buffer's size. */
10945 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10947 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10948 ptrdiff_t size = len;
10949 mode_line_noprop_buf =
10950 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10951 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10952 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10955 *mode_line_noprop_ptr++ = c;
10959 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10960 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10961 characters that yield more columns than PRECISION; PRECISION <= 0
10962 means copy the whole string. Pad with spaces until FIELD_WIDTH
10963 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10964 pad. Called from display_mode_element when it is used to build a
10965 frame title. */
10967 static int
10968 store_mode_line_noprop (const char *string, int field_width, int precision)
10970 const unsigned char *str = (const unsigned char *) string;
10971 int n = 0;
10972 ptrdiff_t dummy, nbytes;
10974 /* Copy at most PRECISION chars from STR. */
10975 nbytes = strlen (string);
10976 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10977 while (nbytes--)
10978 store_mode_line_noprop_char (*str++);
10980 /* Fill up with spaces until FIELD_WIDTH reached. */
10981 while (field_width > 0
10982 && n < field_width)
10984 store_mode_line_noprop_char (' ');
10985 ++n;
10988 return n;
10991 /***********************************************************************
10992 Frame Titles
10993 ***********************************************************************/
10995 #ifdef HAVE_WINDOW_SYSTEM
10997 /* Set the title of FRAME, if it has changed. The title format is
10998 Vicon_title_format if FRAME is iconified, otherwise it is
10999 frame_title_format. */
11001 static void
11002 x_consider_frame_title (Lisp_Object frame)
11004 struct frame *f = XFRAME (frame);
11006 if (FRAME_WINDOW_P (f)
11007 || FRAME_MINIBUF_ONLY_P (f)
11008 || f->explicit_name)
11010 /* Do we have more than one visible frame on this X display? */
11011 Lisp_Object tail;
11012 Lisp_Object fmt;
11013 ptrdiff_t title_start;
11014 char *title;
11015 ptrdiff_t len;
11016 struct it it;
11017 ptrdiff_t count = SPECPDL_INDEX ();
11019 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
11021 Lisp_Object other_frame = XCAR (tail);
11022 struct frame *tf = XFRAME (other_frame);
11024 if (tf != f
11025 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11026 && !FRAME_MINIBUF_ONLY_P (tf)
11027 && !EQ (other_frame, tip_frame)
11028 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11029 break;
11032 /* Set global variable indicating that multiple frames exist. */
11033 multiple_frames = CONSP (tail);
11035 /* Switch to the buffer of selected window of the frame. Set up
11036 mode_line_target so that display_mode_element will output into
11037 mode_line_noprop_buf; then display the title. */
11038 record_unwind_protect (unwind_format_mode_line,
11039 format_mode_line_unwind_data
11040 (f, current_buffer, selected_window, 0));
11042 Fselect_window (f->selected_window, Qt);
11043 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
11044 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11046 mode_line_target = MODE_LINE_TITLE;
11047 title_start = MODE_LINE_NOPROP_LEN (0);
11048 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11049 NULL, DEFAULT_FACE_ID);
11050 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11051 len = MODE_LINE_NOPROP_LEN (title_start);
11052 title = mode_line_noprop_buf + title_start;
11053 unbind_to (count, Qnil);
11055 /* Set the title only if it's changed. This avoids consing in
11056 the common case where it hasn't. (If it turns out that we've
11057 already wasted too much time by walking through the list with
11058 display_mode_element, then we might need to optimize at a
11059 higher level than this.) */
11060 if (! STRINGP (f->name)
11061 || SBYTES (f->name) != len
11062 || memcmp (title, SDATA (f->name), len) != 0)
11063 x_implicitly_set_name (f, make_string (title, len), Qnil);
11067 #endif /* not HAVE_WINDOW_SYSTEM */
11070 /***********************************************************************
11071 Menu Bars
11072 ***********************************************************************/
11075 /* Prepare for redisplay by updating menu-bar item lists when
11076 appropriate. This can call eval. */
11078 void
11079 prepare_menu_bars (void)
11081 int all_windows;
11082 struct gcpro gcpro1, gcpro2;
11083 struct frame *f;
11084 Lisp_Object tooltip_frame;
11086 #ifdef HAVE_WINDOW_SYSTEM
11087 tooltip_frame = tip_frame;
11088 #else
11089 tooltip_frame = Qnil;
11090 #endif
11092 /* Update all frame titles based on their buffer names, etc. We do
11093 this before the menu bars so that the buffer-menu will show the
11094 up-to-date frame titles. */
11095 #ifdef HAVE_WINDOW_SYSTEM
11096 if (windows_or_buffers_changed || update_mode_lines)
11098 Lisp_Object tail, frame;
11100 FOR_EACH_FRAME (tail, frame)
11102 f = XFRAME (frame);
11103 if (!EQ (frame, tooltip_frame)
11104 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11105 x_consider_frame_title (frame);
11108 #endif /* HAVE_WINDOW_SYSTEM */
11110 /* Update the menu bar item lists, if appropriate. This has to be
11111 done before any actual redisplay or generation of display lines. */
11112 all_windows = (update_mode_lines
11113 || buffer_shared > 1
11114 || windows_or_buffers_changed);
11115 if (all_windows)
11117 Lisp_Object tail, frame;
11118 ptrdiff_t count = SPECPDL_INDEX ();
11119 /* 1 means that update_menu_bar has run its hooks
11120 so any further calls to update_menu_bar shouldn't do so again. */
11121 int menu_bar_hooks_run = 0;
11123 record_unwind_save_match_data ();
11125 FOR_EACH_FRAME (tail, frame)
11127 f = XFRAME (frame);
11129 /* Ignore tooltip frame. */
11130 if (EQ (frame, tooltip_frame))
11131 continue;
11133 /* If a window on this frame changed size, report that to
11134 the user and clear the size-change flag. */
11135 if (FRAME_WINDOW_SIZES_CHANGED (f))
11137 Lisp_Object functions;
11139 /* Clear flag first in case we get an error below. */
11140 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11141 functions = Vwindow_size_change_functions;
11142 GCPRO2 (tail, functions);
11144 while (CONSP (functions))
11146 if (!EQ (XCAR (functions), Qt))
11147 call1 (XCAR (functions), frame);
11148 functions = XCDR (functions);
11150 UNGCPRO;
11153 GCPRO1 (tail);
11154 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11155 #ifdef HAVE_WINDOW_SYSTEM
11156 update_tool_bar (f, 0);
11157 #endif
11158 #ifdef HAVE_NS
11159 if (windows_or_buffers_changed
11160 && FRAME_NS_P (f))
11161 ns_set_doc_edited (f, Fbuffer_modified_p
11162 (XWINDOW (f->selected_window)->buffer));
11163 #endif
11164 UNGCPRO;
11167 unbind_to (count, Qnil);
11169 else
11171 struct frame *sf = SELECTED_FRAME ();
11172 update_menu_bar (sf, 1, 0);
11173 #ifdef HAVE_WINDOW_SYSTEM
11174 update_tool_bar (sf, 1);
11175 #endif
11180 /* Update the menu bar item list for frame F. This has to be done
11181 before we start to fill in any display lines, because it can call
11182 eval.
11184 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11186 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11187 already ran the menu bar hooks for this redisplay, so there
11188 is no need to run them again. The return value is the
11189 updated value of this flag, to pass to the next call. */
11191 static int
11192 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11194 Lisp_Object window;
11195 register struct window *w;
11197 /* If called recursively during a menu update, do nothing. This can
11198 happen when, for instance, an activate-menubar-hook causes a
11199 redisplay. */
11200 if (inhibit_menubar_update)
11201 return hooks_run;
11203 window = FRAME_SELECTED_WINDOW (f);
11204 w = XWINDOW (window);
11206 if (FRAME_WINDOW_P (f)
11208 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11209 || defined (HAVE_NS) || defined (USE_GTK)
11210 FRAME_EXTERNAL_MENU_BAR (f)
11211 #else
11212 FRAME_MENU_BAR_LINES (f) > 0
11213 #endif
11214 : FRAME_MENU_BAR_LINES (f) > 0)
11216 /* If the user has switched buffers or windows, we need to
11217 recompute to reflect the new bindings. But we'll
11218 recompute when update_mode_lines is set too; that means
11219 that people can use force-mode-line-update to request
11220 that the menu bar be recomputed. The adverse effect on
11221 the rest of the redisplay algorithm is about the same as
11222 windows_or_buffers_changed anyway. */
11223 if (windows_or_buffers_changed
11224 /* This used to test w->update_mode_line, but we believe
11225 there is no need to recompute the menu in that case. */
11226 || update_mode_lines
11227 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11228 < BUF_MODIFF (XBUFFER (w->buffer)))
11229 != w->last_had_star)
11230 || ((!NILP (Vtransient_mark_mode)
11231 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11232 != !NILP (w->region_showing)))
11234 struct buffer *prev = current_buffer;
11235 ptrdiff_t count = SPECPDL_INDEX ();
11237 specbind (Qinhibit_menubar_update, Qt);
11239 set_buffer_internal_1 (XBUFFER (w->buffer));
11240 if (save_match_data)
11241 record_unwind_save_match_data ();
11242 if (NILP (Voverriding_local_map_menu_flag))
11244 specbind (Qoverriding_terminal_local_map, Qnil);
11245 specbind (Qoverriding_local_map, Qnil);
11248 if (!hooks_run)
11250 /* Run the Lucid hook. */
11251 safe_run_hooks (Qactivate_menubar_hook);
11253 /* If it has changed current-menubar from previous value,
11254 really recompute the menu-bar from the value. */
11255 if (! NILP (Vlucid_menu_bar_dirty_flag))
11256 call0 (Qrecompute_lucid_menubar);
11258 safe_run_hooks (Qmenu_bar_update_hook);
11260 hooks_run = 1;
11263 XSETFRAME (Vmenu_updating_frame, f);
11264 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
11266 /* Redisplay the menu bar in case we changed it. */
11267 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11268 || defined (HAVE_NS) || defined (USE_GTK)
11269 if (FRAME_WINDOW_P (f))
11271 #if defined (HAVE_NS)
11272 /* All frames on Mac OS share the same menubar. So only
11273 the selected frame should be allowed to set it. */
11274 if (f == SELECTED_FRAME ())
11275 #endif
11276 set_frame_menubar (f, 0, 0);
11278 else
11279 /* On a terminal screen, the menu bar is an ordinary screen
11280 line, and this makes it get updated. */
11281 w->update_mode_line = 1;
11282 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11283 /* In the non-toolkit version, the menu bar is an ordinary screen
11284 line, and this makes it get updated. */
11285 w->update_mode_line = 1;
11286 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11288 unbind_to (count, Qnil);
11289 set_buffer_internal_1 (prev);
11293 return hooks_run;
11298 /***********************************************************************
11299 Output Cursor
11300 ***********************************************************************/
11302 #ifdef HAVE_WINDOW_SYSTEM
11304 /* EXPORT:
11305 Nominal cursor position -- where to draw output.
11306 HPOS and VPOS are window relative glyph matrix coordinates.
11307 X and Y are window relative pixel coordinates. */
11309 struct cursor_pos output_cursor;
11312 /* EXPORT:
11313 Set the global variable output_cursor to CURSOR. All cursor
11314 positions are relative to updated_window. */
11316 void
11317 set_output_cursor (struct cursor_pos *cursor)
11319 output_cursor.hpos = cursor->hpos;
11320 output_cursor.vpos = cursor->vpos;
11321 output_cursor.x = cursor->x;
11322 output_cursor.y = cursor->y;
11326 /* EXPORT for RIF:
11327 Set a nominal cursor position.
11329 HPOS and VPOS are column/row positions in a window glyph matrix. X
11330 and Y are window text area relative pixel positions.
11332 If this is done during an update, updated_window will contain the
11333 window that is being updated and the position is the future output
11334 cursor position for that window. If updated_window is null, use
11335 selected_window and display the cursor at the given position. */
11337 void
11338 x_cursor_to (int vpos, int hpos, int y, int x)
11340 struct window *w;
11342 /* If updated_window is not set, work on selected_window. */
11343 if (updated_window)
11344 w = updated_window;
11345 else
11346 w = XWINDOW (selected_window);
11348 /* Set the output cursor. */
11349 output_cursor.hpos = hpos;
11350 output_cursor.vpos = vpos;
11351 output_cursor.x = x;
11352 output_cursor.y = y;
11354 /* If not called as part of an update, really display the cursor.
11355 This will also set the cursor position of W. */
11356 if (updated_window == NULL)
11358 BLOCK_INPUT;
11359 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11360 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11361 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11362 UNBLOCK_INPUT;
11366 #endif /* HAVE_WINDOW_SYSTEM */
11369 /***********************************************************************
11370 Tool-bars
11371 ***********************************************************************/
11373 #ifdef HAVE_WINDOW_SYSTEM
11375 /* Where the mouse was last time we reported a mouse event. */
11377 FRAME_PTR last_mouse_frame;
11379 /* Tool-bar item index of the item on which a mouse button was pressed
11380 or -1. */
11382 int last_tool_bar_item;
11385 static Lisp_Object
11386 update_tool_bar_unwind (Lisp_Object frame)
11388 selected_frame = frame;
11389 return Qnil;
11392 /* Update the tool-bar item list for frame F. This has to be done
11393 before we start to fill in any display lines. Called from
11394 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11395 and restore it here. */
11397 static void
11398 update_tool_bar (struct frame *f, int save_match_data)
11400 #if defined (USE_GTK) || defined (HAVE_NS)
11401 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11402 #else
11403 int do_update = WINDOWP (f->tool_bar_window)
11404 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11405 #endif
11407 if (do_update)
11409 Lisp_Object window;
11410 struct window *w;
11412 window = FRAME_SELECTED_WINDOW (f);
11413 w = XWINDOW (window);
11415 /* If the user has switched buffers or windows, we need to
11416 recompute to reflect the new bindings. But we'll
11417 recompute when update_mode_lines is set too; that means
11418 that people can use force-mode-line-update to request
11419 that the menu bar be recomputed. The adverse effect on
11420 the rest of the redisplay algorithm is about the same as
11421 windows_or_buffers_changed anyway. */
11422 if (windows_or_buffers_changed
11423 || w->update_mode_line
11424 || update_mode_lines
11425 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11426 < BUF_MODIFF (XBUFFER (w->buffer)))
11427 != w->last_had_star)
11428 || ((!NILP (Vtransient_mark_mode)
11429 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11430 != !NILP (w->region_showing)))
11432 struct buffer *prev = current_buffer;
11433 ptrdiff_t count = SPECPDL_INDEX ();
11434 Lisp_Object frame, new_tool_bar;
11435 int new_n_tool_bar;
11436 struct gcpro gcpro1;
11438 /* Set current_buffer to the buffer of the selected
11439 window of the frame, so that we get the right local
11440 keymaps. */
11441 set_buffer_internal_1 (XBUFFER (w->buffer));
11443 /* Save match data, if we must. */
11444 if (save_match_data)
11445 record_unwind_save_match_data ();
11447 /* Make sure that we don't accidentally use bogus keymaps. */
11448 if (NILP (Voverriding_local_map_menu_flag))
11450 specbind (Qoverriding_terminal_local_map, Qnil);
11451 specbind (Qoverriding_local_map, Qnil);
11454 GCPRO1 (new_tool_bar);
11456 /* We must temporarily set the selected frame to this frame
11457 before calling tool_bar_items, because the calculation of
11458 the tool-bar keymap uses the selected frame (see
11459 `tool-bar-make-keymap' in tool-bar.el). */
11460 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11461 XSETFRAME (frame, f);
11462 selected_frame = frame;
11464 /* Build desired tool-bar items from keymaps. */
11465 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11466 &new_n_tool_bar);
11468 /* Redisplay the tool-bar if we changed it. */
11469 if (new_n_tool_bar != f->n_tool_bar_items
11470 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11472 /* Redisplay that happens asynchronously due to an expose event
11473 may access f->tool_bar_items. Make sure we update both
11474 variables within BLOCK_INPUT so no such event interrupts. */
11475 BLOCK_INPUT;
11476 f->tool_bar_items = new_tool_bar;
11477 f->n_tool_bar_items = new_n_tool_bar;
11478 w->update_mode_line = 1;
11479 UNBLOCK_INPUT;
11482 UNGCPRO;
11484 unbind_to (count, Qnil);
11485 set_buffer_internal_1 (prev);
11491 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11492 F's desired tool-bar contents. F->tool_bar_items must have
11493 been set up previously by calling prepare_menu_bars. */
11495 static void
11496 build_desired_tool_bar_string (struct frame *f)
11498 int i, size, size_needed;
11499 struct gcpro gcpro1, gcpro2, gcpro3;
11500 Lisp_Object image, plist, props;
11502 image = plist = props = Qnil;
11503 GCPRO3 (image, plist, props);
11505 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11506 Otherwise, make a new string. */
11508 /* The size of the string we might be able to reuse. */
11509 size = (STRINGP (f->desired_tool_bar_string)
11510 ? SCHARS (f->desired_tool_bar_string)
11511 : 0);
11513 /* We need one space in the string for each image. */
11514 size_needed = f->n_tool_bar_items;
11516 /* Reuse f->desired_tool_bar_string, if possible. */
11517 if (size < size_needed || NILP (f->desired_tool_bar_string))
11518 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
11519 make_number (' '));
11520 else
11522 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11523 Fremove_text_properties (make_number (0), make_number (size),
11524 props, f->desired_tool_bar_string);
11527 /* Put a `display' property on the string for the images to display,
11528 put a `menu_item' property on tool-bar items with a value that
11529 is the index of the item in F's tool-bar item vector. */
11530 for (i = 0; i < f->n_tool_bar_items; ++i)
11532 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11534 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11535 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11536 int hmargin, vmargin, relief, idx, end;
11538 /* If image is a vector, choose the image according to the
11539 button state. */
11540 image = PROP (TOOL_BAR_ITEM_IMAGES);
11541 if (VECTORP (image))
11543 if (enabled_p)
11544 idx = (selected_p
11545 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11546 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11547 else
11548 idx = (selected_p
11549 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11550 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11552 eassert (ASIZE (image) >= idx);
11553 image = AREF (image, idx);
11555 else
11556 idx = -1;
11558 /* Ignore invalid image specifications. */
11559 if (!valid_image_p (image))
11560 continue;
11562 /* Display the tool-bar button pressed, or depressed. */
11563 plist = Fcopy_sequence (XCDR (image));
11565 /* Compute margin and relief to draw. */
11566 relief = (tool_bar_button_relief >= 0
11567 ? tool_bar_button_relief
11568 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11569 hmargin = vmargin = relief;
11571 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11572 INT_MAX - max (hmargin, vmargin)))
11574 hmargin += XFASTINT (Vtool_bar_button_margin);
11575 vmargin += XFASTINT (Vtool_bar_button_margin);
11577 else if (CONSP (Vtool_bar_button_margin))
11579 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11580 INT_MAX - hmargin))
11581 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11583 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11584 INT_MAX - vmargin))
11585 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11588 if (auto_raise_tool_bar_buttons_p)
11590 /* Add a `:relief' property to the image spec if the item is
11591 selected. */
11592 if (selected_p)
11594 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11595 hmargin -= relief;
11596 vmargin -= relief;
11599 else
11601 /* If image is selected, display it pressed, i.e. with a
11602 negative relief. If it's not selected, display it with a
11603 raised relief. */
11604 plist = Fplist_put (plist, QCrelief,
11605 (selected_p
11606 ? make_number (-relief)
11607 : make_number (relief)));
11608 hmargin -= relief;
11609 vmargin -= relief;
11612 /* Put a margin around the image. */
11613 if (hmargin || vmargin)
11615 if (hmargin == vmargin)
11616 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11617 else
11618 plist = Fplist_put (plist, QCmargin,
11619 Fcons (make_number (hmargin),
11620 make_number (vmargin)));
11623 /* If button is not enabled, and we don't have special images
11624 for the disabled state, make the image appear disabled by
11625 applying an appropriate algorithm to it. */
11626 if (!enabled_p && idx < 0)
11627 plist = Fplist_put (plist, QCconversion, Qdisabled);
11629 /* Put a `display' text property on the string for the image to
11630 display. Put a `menu-item' property on the string that gives
11631 the start of this item's properties in the tool-bar items
11632 vector. */
11633 image = Fcons (Qimage, plist);
11634 props = list4 (Qdisplay, image,
11635 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11637 /* Let the last image hide all remaining spaces in the tool bar
11638 string. The string can be longer than needed when we reuse a
11639 previous string. */
11640 if (i + 1 == f->n_tool_bar_items)
11641 end = SCHARS (f->desired_tool_bar_string);
11642 else
11643 end = i + 1;
11644 Fadd_text_properties (make_number (i), make_number (end),
11645 props, f->desired_tool_bar_string);
11646 #undef PROP
11649 UNGCPRO;
11653 /* Display one line of the tool-bar of frame IT->f.
11655 HEIGHT specifies the desired height of the tool-bar line.
11656 If the actual height of the glyph row is less than HEIGHT, the
11657 row's height is increased to HEIGHT, and the icons are centered
11658 vertically in the new height.
11660 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11661 count a final empty row in case the tool-bar width exactly matches
11662 the window width.
11665 static void
11666 display_tool_bar_line (struct it *it, int height)
11668 struct glyph_row *row = it->glyph_row;
11669 int max_x = it->last_visible_x;
11670 struct glyph *last;
11672 prepare_desired_row (row);
11673 row->y = it->current_y;
11675 /* Note that this isn't made use of if the face hasn't a box,
11676 so there's no need to check the face here. */
11677 it->start_of_box_run_p = 1;
11679 while (it->current_x < max_x)
11681 int x, n_glyphs_before, i, nglyphs;
11682 struct it it_before;
11684 /* Get the next display element. */
11685 if (!get_next_display_element (it))
11687 /* Don't count empty row if we are counting needed tool-bar lines. */
11688 if (height < 0 && !it->hpos)
11689 return;
11690 break;
11693 /* Produce glyphs. */
11694 n_glyphs_before = row->used[TEXT_AREA];
11695 it_before = *it;
11697 PRODUCE_GLYPHS (it);
11699 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11700 i = 0;
11701 x = it_before.current_x;
11702 while (i < nglyphs)
11704 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11706 if (x + glyph->pixel_width > max_x)
11708 /* Glyph doesn't fit on line. Backtrack. */
11709 row->used[TEXT_AREA] = n_glyphs_before;
11710 *it = it_before;
11711 /* If this is the only glyph on this line, it will never fit on the
11712 tool-bar, so skip it. But ensure there is at least one glyph,
11713 so we don't accidentally disable the tool-bar. */
11714 if (n_glyphs_before == 0
11715 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11716 break;
11717 goto out;
11720 ++it->hpos;
11721 x += glyph->pixel_width;
11722 ++i;
11725 /* Stop at line end. */
11726 if (ITERATOR_AT_END_OF_LINE_P (it))
11727 break;
11729 set_iterator_to_next (it, 1);
11732 out:;
11734 row->displays_text_p = row->used[TEXT_AREA] != 0;
11736 /* Use default face for the border below the tool bar.
11738 FIXME: When auto-resize-tool-bars is grow-only, there is
11739 no additional border below the possibly empty tool-bar lines.
11740 So to make the extra empty lines look "normal", we have to
11741 use the tool-bar face for the border too. */
11742 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11743 it->face_id = DEFAULT_FACE_ID;
11745 extend_face_to_end_of_line (it);
11746 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11747 last->right_box_line_p = 1;
11748 if (last == row->glyphs[TEXT_AREA])
11749 last->left_box_line_p = 1;
11751 /* Make line the desired height and center it vertically. */
11752 if ((height -= it->max_ascent + it->max_descent) > 0)
11754 /* Don't add more than one line height. */
11755 height %= FRAME_LINE_HEIGHT (it->f);
11756 it->max_ascent += height / 2;
11757 it->max_descent += (height + 1) / 2;
11760 compute_line_metrics (it);
11762 /* If line is empty, make it occupy the rest of the tool-bar. */
11763 if (!row->displays_text_p)
11765 row->height = row->phys_height = it->last_visible_y - row->y;
11766 row->visible_height = row->height;
11767 row->ascent = row->phys_ascent = 0;
11768 row->extra_line_spacing = 0;
11771 row->full_width_p = 1;
11772 row->continued_p = 0;
11773 row->truncated_on_left_p = 0;
11774 row->truncated_on_right_p = 0;
11776 it->current_x = it->hpos = 0;
11777 it->current_y += row->height;
11778 ++it->vpos;
11779 ++it->glyph_row;
11783 /* Max tool-bar height. */
11785 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11786 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11788 /* Value is the number of screen lines needed to make all tool-bar
11789 items of frame F visible. The number of actual rows needed is
11790 returned in *N_ROWS if non-NULL. */
11792 static int
11793 tool_bar_lines_needed (struct frame *f, int *n_rows)
11795 struct window *w = XWINDOW (f->tool_bar_window);
11796 struct it it;
11797 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11798 the desired matrix, so use (unused) mode-line row as temporary row to
11799 avoid destroying the first tool-bar row. */
11800 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11802 /* Initialize an iterator for iteration over
11803 F->desired_tool_bar_string in the tool-bar window of frame F. */
11804 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11805 it.first_visible_x = 0;
11806 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11807 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11808 it.paragraph_embedding = L2R;
11810 while (!ITERATOR_AT_END_P (&it))
11812 clear_glyph_row (temp_row);
11813 it.glyph_row = temp_row;
11814 display_tool_bar_line (&it, -1);
11816 clear_glyph_row (temp_row);
11818 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11819 if (n_rows)
11820 *n_rows = it.vpos > 0 ? it.vpos : -1;
11822 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11826 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11827 0, 1, 0,
11828 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11829 (Lisp_Object frame)
11831 struct frame *f;
11832 struct window *w;
11833 int nlines = 0;
11835 if (NILP (frame))
11836 frame = selected_frame;
11837 else
11838 CHECK_FRAME (frame);
11839 f = XFRAME (frame);
11841 if (WINDOWP (f->tool_bar_window)
11842 && (w = XWINDOW (f->tool_bar_window),
11843 WINDOW_TOTAL_LINES (w) > 0))
11845 update_tool_bar (f, 1);
11846 if (f->n_tool_bar_items)
11848 build_desired_tool_bar_string (f);
11849 nlines = tool_bar_lines_needed (f, NULL);
11853 return make_number (nlines);
11857 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11858 height should be changed. */
11860 static int
11861 redisplay_tool_bar (struct frame *f)
11863 struct window *w;
11864 struct it it;
11865 struct glyph_row *row;
11867 #if defined (USE_GTK) || defined (HAVE_NS)
11868 if (FRAME_EXTERNAL_TOOL_BAR (f))
11869 update_frame_tool_bar (f);
11870 return 0;
11871 #endif
11873 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11874 do anything. This means you must start with tool-bar-lines
11875 non-zero to get the auto-sizing effect. Or in other words, you
11876 can turn off tool-bars by specifying tool-bar-lines zero. */
11877 if (!WINDOWP (f->tool_bar_window)
11878 || (w = XWINDOW (f->tool_bar_window),
11879 WINDOW_TOTAL_LINES (w) == 0))
11880 return 0;
11882 /* Set up an iterator for the tool-bar window. */
11883 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11884 it.first_visible_x = 0;
11885 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11886 row = it.glyph_row;
11888 /* Build a string that represents the contents of the tool-bar. */
11889 build_desired_tool_bar_string (f);
11890 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11891 /* FIXME: This should be controlled by a user option. But it
11892 doesn't make sense to have an R2L tool bar if the menu bar cannot
11893 be drawn also R2L, and making the menu bar R2L is tricky due
11894 toolkit-specific code that implements it. If an R2L tool bar is
11895 ever supported, display_tool_bar_line should also be augmented to
11896 call unproduce_glyphs like display_line and display_string
11897 do. */
11898 it.paragraph_embedding = L2R;
11900 if (f->n_tool_bar_rows == 0)
11902 int nlines;
11904 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11905 nlines != WINDOW_TOTAL_LINES (w)))
11907 Lisp_Object frame;
11908 int old_height = WINDOW_TOTAL_LINES (w);
11910 XSETFRAME (frame, f);
11911 Fmodify_frame_parameters (frame,
11912 Fcons (Fcons (Qtool_bar_lines,
11913 make_number (nlines)),
11914 Qnil));
11915 if (WINDOW_TOTAL_LINES (w) != old_height)
11917 clear_glyph_matrix (w->desired_matrix);
11918 fonts_changed_p = 1;
11919 return 1;
11924 /* Display as many lines as needed to display all tool-bar items. */
11926 if (f->n_tool_bar_rows > 0)
11928 int border, rows, height, extra;
11930 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11931 border = XINT (Vtool_bar_border);
11932 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11933 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11934 else if (EQ (Vtool_bar_border, Qborder_width))
11935 border = f->border_width;
11936 else
11937 border = 0;
11938 if (border < 0)
11939 border = 0;
11941 rows = f->n_tool_bar_rows;
11942 height = max (1, (it.last_visible_y - border) / rows);
11943 extra = it.last_visible_y - border - height * rows;
11945 while (it.current_y < it.last_visible_y)
11947 int h = 0;
11948 if (extra > 0 && rows-- > 0)
11950 h = (extra + rows - 1) / rows;
11951 extra -= h;
11953 display_tool_bar_line (&it, height + h);
11956 else
11958 while (it.current_y < it.last_visible_y)
11959 display_tool_bar_line (&it, 0);
11962 /* It doesn't make much sense to try scrolling in the tool-bar
11963 window, so don't do it. */
11964 w->desired_matrix->no_scrolling_p = 1;
11965 w->must_be_updated_p = 1;
11967 if (!NILP (Vauto_resize_tool_bars))
11969 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11970 int change_height_p = 0;
11972 /* If we couldn't display everything, change the tool-bar's
11973 height if there is room for more. */
11974 if (IT_STRING_CHARPOS (it) < it.end_charpos
11975 && it.current_y < max_tool_bar_height)
11976 change_height_p = 1;
11978 row = it.glyph_row - 1;
11980 /* If there are blank lines at the end, except for a partially
11981 visible blank line at the end that is smaller than
11982 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11983 if (!row->displays_text_p
11984 && row->height >= FRAME_LINE_HEIGHT (f))
11985 change_height_p = 1;
11987 /* If row displays tool-bar items, but is partially visible,
11988 change the tool-bar's height. */
11989 if (row->displays_text_p
11990 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11991 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11992 change_height_p = 1;
11994 /* Resize windows as needed by changing the `tool-bar-lines'
11995 frame parameter. */
11996 if (change_height_p)
11998 Lisp_Object frame;
11999 int old_height = WINDOW_TOTAL_LINES (w);
12000 int nrows;
12001 int nlines = tool_bar_lines_needed (f, &nrows);
12003 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12004 && !f->minimize_tool_bar_window_p)
12005 ? (nlines > old_height)
12006 : (nlines != old_height));
12007 f->minimize_tool_bar_window_p = 0;
12009 if (change_height_p)
12011 XSETFRAME (frame, f);
12012 Fmodify_frame_parameters (frame,
12013 Fcons (Fcons (Qtool_bar_lines,
12014 make_number (nlines)),
12015 Qnil));
12016 if (WINDOW_TOTAL_LINES (w) != old_height)
12018 clear_glyph_matrix (w->desired_matrix);
12019 f->n_tool_bar_rows = nrows;
12020 fonts_changed_p = 1;
12021 return 1;
12027 f->minimize_tool_bar_window_p = 0;
12028 return 0;
12032 /* Get information about the tool-bar item which is displayed in GLYPH
12033 on frame F. Return in *PROP_IDX the index where tool-bar item
12034 properties start in F->tool_bar_items. Value is zero if
12035 GLYPH doesn't display a tool-bar item. */
12037 static int
12038 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12040 Lisp_Object prop;
12041 int success_p;
12042 int charpos;
12044 /* This function can be called asynchronously, which means we must
12045 exclude any possibility that Fget_text_property signals an
12046 error. */
12047 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12048 charpos = max (0, charpos);
12050 /* Get the text property `menu-item' at pos. The value of that
12051 property is the start index of this item's properties in
12052 F->tool_bar_items. */
12053 prop = Fget_text_property (make_number (charpos),
12054 Qmenu_item, f->current_tool_bar_string);
12055 if (INTEGERP (prop))
12057 *prop_idx = XINT (prop);
12058 success_p = 1;
12060 else
12061 success_p = 0;
12063 return success_p;
12067 /* Get information about the tool-bar item at position X/Y on frame F.
12068 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12069 the current matrix of the tool-bar window of F, or NULL if not
12070 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12071 item in F->tool_bar_items. Value is
12073 -1 if X/Y is not on a tool-bar item
12074 0 if X/Y is on the same item that was highlighted before.
12075 1 otherwise. */
12077 static int
12078 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12079 int *hpos, int *vpos, int *prop_idx)
12081 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12082 struct window *w = XWINDOW (f->tool_bar_window);
12083 int area;
12085 /* Find the glyph under X/Y. */
12086 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12087 if (*glyph == NULL)
12088 return -1;
12090 /* Get the start of this tool-bar item's properties in
12091 f->tool_bar_items. */
12092 if (!tool_bar_item_info (f, *glyph, prop_idx))
12093 return -1;
12095 /* Is mouse on the highlighted item? */
12096 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12097 && *vpos >= hlinfo->mouse_face_beg_row
12098 && *vpos <= hlinfo->mouse_face_end_row
12099 && (*vpos > hlinfo->mouse_face_beg_row
12100 || *hpos >= hlinfo->mouse_face_beg_col)
12101 && (*vpos < hlinfo->mouse_face_end_row
12102 || *hpos < hlinfo->mouse_face_end_col
12103 || hlinfo->mouse_face_past_end))
12104 return 0;
12106 return 1;
12110 /* EXPORT:
12111 Handle mouse button event on the tool-bar of frame F, at
12112 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12113 0 for button release. MODIFIERS is event modifiers for button
12114 release. */
12116 void
12117 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12118 int modifiers)
12120 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12121 struct window *w = XWINDOW (f->tool_bar_window);
12122 int hpos, vpos, prop_idx;
12123 struct glyph *glyph;
12124 Lisp_Object enabled_p;
12126 /* If not on the highlighted tool-bar item, return. */
12127 frame_to_window_pixel_xy (w, &x, &y);
12128 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12129 return;
12131 /* If item is disabled, do nothing. */
12132 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12133 if (NILP (enabled_p))
12134 return;
12136 if (down_p)
12138 /* Show item in pressed state. */
12139 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12140 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
12141 last_tool_bar_item = prop_idx;
12143 else
12145 Lisp_Object key, frame;
12146 struct input_event event;
12147 EVENT_INIT (event);
12149 /* Show item in released state. */
12150 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12151 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
12153 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12155 XSETFRAME (frame, f);
12156 event.kind = TOOL_BAR_EVENT;
12157 event.frame_or_window = frame;
12158 event.arg = frame;
12159 kbd_buffer_store_event (&event);
12161 event.kind = TOOL_BAR_EVENT;
12162 event.frame_or_window = frame;
12163 event.arg = key;
12164 event.modifiers = modifiers;
12165 kbd_buffer_store_event (&event);
12166 last_tool_bar_item = -1;
12171 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12172 tool-bar window-relative coordinates X/Y. Called from
12173 note_mouse_highlight. */
12175 static void
12176 note_tool_bar_highlight (struct frame *f, int x, int y)
12178 Lisp_Object window = f->tool_bar_window;
12179 struct window *w = XWINDOW (window);
12180 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12181 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12182 int hpos, vpos;
12183 struct glyph *glyph;
12184 struct glyph_row *row;
12185 int i;
12186 Lisp_Object enabled_p;
12187 int prop_idx;
12188 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12189 int mouse_down_p, rc;
12191 /* Function note_mouse_highlight is called with negative X/Y
12192 values when mouse moves outside of the frame. */
12193 if (x <= 0 || y <= 0)
12195 clear_mouse_face (hlinfo);
12196 return;
12199 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12200 if (rc < 0)
12202 /* Not on tool-bar item. */
12203 clear_mouse_face (hlinfo);
12204 return;
12206 else if (rc == 0)
12207 /* On same tool-bar item as before. */
12208 goto set_help_echo;
12210 clear_mouse_face (hlinfo);
12212 /* Mouse is down, but on different tool-bar item? */
12213 mouse_down_p = (dpyinfo->grabbed
12214 && f == last_mouse_frame
12215 && FRAME_LIVE_P (f));
12216 if (mouse_down_p
12217 && last_tool_bar_item != prop_idx)
12218 return;
12220 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12221 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12223 /* If tool-bar item is not enabled, don't highlight it. */
12224 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12225 if (!NILP (enabled_p))
12227 /* Compute the x-position of the glyph. In front and past the
12228 image is a space. We include this in the highlighted area. */
12229 row = MATRIX_ROW (w->current_matrix, vpos);
12230 for (i = x = 0; i < hpos; ++i)
12231 x += row->glyphs[TEXT_AREA][i].pixel_width;
12233 /* Record this as the current active region. */
12234 hlinfo->mouse_face_beg_col = hpos;
12235 hlinfo->mouse_face_beg_row = vpos;
12236 hlinfo->mouse_face_beg_x = x;
12237 hlinfo->mouse_face_beg_y = row->y;
12238 hlinfo->mouse_face_past_end = 0;
12240 hlinfo->mouse_face_end_col = hpos + 1;
12241 hlinfo->mouse_face_end_row = vpos;
12242 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12243 hlinfo->mouse_face_end_y = row->y;
12244 hlinfo->mouse_face_window = window;
12245 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12247 /* Display it as active. */
12248 show_mouse_face (hlinfo, draw);
12249 hlinfo->mouse_face_image_state = draw;
12252 set_help_echo:
12254 /* Set help_echo_string to a help string to display for this tool-bar item.
12255 XTread_socket does the rest. */
12256 help_echo_object = help_echo_window = Qnil;
12257 help_echo_pos = -1;
12258 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12259 if (NILP (help_echo_string))
12260 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12263 #endif /* HAVE_WINDOW_SYSTEM */
12267 /************************************************************************
12268 Horizontal scrolling
12269 ************************************************************************/
12271 static int hscroll_window_tree (Lisp_Object);
12272 static int hscroll_windows (Lisp_Object);
12274 /* For all leaf windows in the window tree rooted at WINDOW, set their
12275 hscroll value so that PT is (i) visible in the window, and (ii) so
12276 that it is not within a certain margin at the window's left and
12277 right border. Value is non-zero if any window's hscroll has been
12278 changed. */
12280 static int
12281 hscroll_window_tree (Lisp_Object window)
12283 int hscrolled_p = 0;
12284 int hscroll_relative_p = FLOATP (Vhscroll_step);
12285 int hscroll_step_abs = 0;
12286 double hscroll_step_rel = 0;
12288 if (hscroll_relative_p)
12290 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12291 if (hscroll_step_rel < 0)
12293 hscroll_relative_p = 0;
12294 hscroll_step_abs = 0;
12297 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12299 hscroll_step_abs = XINT (Vhscroll_step);
12300 if (hscroll_step_abs < 0)
12301 hscroll_step_abs = 0;
12303 else
12304 hscroll_step_abs = 0;
12306 while (WINDOWP (window))
12308 struct window *w = XWINDOW (window);
12310 if (WINDOWP (w->hchild))
12311 hscrolled_p |= hscroll_window_tree (w->hchild);
12312 else if (WINDOWP (w->vchild))
12313 hscrolled_p |= hscroll_window_tree (w->vchild);
12314 else if (w->cursor.vpos >= 0)
12316 int h_margin;
12317 int text_area_width;
12318 struct glyph_row *current_cursor_row
12319 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12320 struct glyph_row *desired_cursor_row
12321 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12322 struct glyph_row *cursor_row
12323 = (desired_cursor_row->enabled_p
12324 ? desired_cursor_row
12325 : current_cursor_row);
12326 int row_r2l_p = cursor_row->reversed_p;
12328 text_area_width = window_box_width (w, TEXT_AREA);
12330 /* Scroll when cursor is inside this scroll margin. */
12331 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12333 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12334 /* For left-to-right rows, hscroll when cursor is either
12335 (i) inside the right hscroll margin, or (ii) if it is
12336 inside the left margin and the window is already
12337 hscrolled. */
12338 && ((!row_r2l_p
12339 && ((w->hscroll
12340 && w->cursor.x <= h_margin)
12341 || (cursor_row->enabled_p
12342 && cursor_row->truncated_on_right_p
12343 && (w->cursor.x >= text_area_width - h_margin))))
12344 /* For right-to-left rows, the logic is similar,
12345 except that rules for scrolling to left and right
12346 are reversed. E.g., if cursor.x <= h_margin, we
12347 need to hscroll "to the right" unconditionally,
12348 and that will scroll the screen to the left so as
12349 to reveal the next portion of the row. */
12350 || (row_r2l_p
12351 && ((cursor_row->enabled_p
12352 /* FIXME: It is confusing to set the
12353 truncated_on_right_p flag when R2L rows
12354 are actually truncated on the left. */
12355 && cursor_row->truncated_on_right_p
12356 && w->cursor.x <= h_margin)
12357 || (w->hscroll
12358 && (w->cursor.x >= text_area_width - h_margin))))))
12360 struct it it;
12361 ptrdiff_t hscroll;
12362 struct buffer *saved_current_buffer;
12363 ptrdiff_t pt;
12364 int wanted_x;
12366 /* Find point in a display of infinite width. */
12367 saved_current_buffer = current_buffer;
12368 current_buffer = XBUFFER (w->buffer);
12370 if (w == XWINDOW (selected_window))
12371 pt = PT;
12372 else
12374 pt = marker_position (w->pointm);
12375 pt = max (BEGV, pt);
12376 pt = min (ZV, pt);
12379 /* Move iterator to pt starting at cursor_row->start in
12380 a line with infinite width. */
12381 init_to_row_start (&it, w, cursor_row);
12382 it.last_visible_x = INFINITY;
12383 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12384 current_buffer = saved_current_buffer;
12386 /* Position cursor in window. */
12387 if (!hscroll_relative_p && hscroll_step_abs == 0)
12388 hscroll = max (0, (it.current_x
12389 - (ITERATOR_AT_END_OF_LINE_P (&it)
12390 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12391 : (text_area_width / 2))))
12392 / FRAME_COLUMN_WIDTH (it.f);
12393 else if ((!row_r2l_p
12394 && w->cursor.x >= text_area_width - h_margin)
12395 || (row_r2l_p && w->cursor.x <= h_margin))
12397 if (hscroll_relative_p)
12398 wanted_x = text_area_width * (1 - hscroll_step_rel)
12399 - h_margin;
12400 else
12401 wanted_x = text_area_width
12402 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12403 - h_margin;
12404 hscroll
12405 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12407 else
12409 if (hscroll_relative_p)
12410 wanted_x = text_area_width * hscroll_step_rel
12411 + h_margin;
12412 else
12413 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12414 + h_margin;
12415 hscroll
12416 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12418 hscroll = max (hscroll, w->min_hscroll);
12420 /* Don't prevent redisplay optimizations if hscroll
12421 hasn't changed, as it will unnecessarily slow down
12422 redisplay. */
12423 if (w->hscroll != hscroll)
12425 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12426 w->hscroll = hscroll;
12427 hscrolled_p = 1;
12432 window = w->next;
12435 /* Value is non-zero if hscroll of any leaf window has been changed. */
12436 return hscrolled_p;
12440 /* Set hscroll so that cursor is visible and not inside horizontal
12441 scroll margins for all windows in the tree rooted at WINDOW. See
12442 also hscroll_window_tree above. Value is non-zero if any window's
12443 hscroll has been changed. If it has, desired matrices on the frame
12444 of WINDOW are cleared. */
12446 static int
12447 hscroll_windows (Lisp_Object window)
12449 int hscrolled_p = hscroll_window_tree (window);
12450 if (hscrolled_p)
12451 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12452 return hscrolled_p;
12457 /************************************************************************
12458 Redisplay
12459 ************************************************************************/
12461 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12462 to a non-zero value. This is sometimes handy to have in a debugger
12463 session. */
12465 #ifdef GLYPH_DEBUG
12467 /* First and last unchanged row for try_window_id. */
12469 static int debug_first_unchanged_at_end_vpos;
12470 static int debug_last_unchanged_at_beg_vpos;
12472 /* Delta vpos and y. */
12474 static int debug_dvpos, debug_dy;
12476 /* Delta in characters and bytes for try_window_id. */
12478 static ptrdiff_t debug_delta, debug_delta_bytes;
12480 /* Values of window_end_pos and window_end_vpos at the end of
12481 try_window_id. */
12483 static ptrdiff_t debug_end_vpos;
12485 /* Append a string to W->desired_matrix->method. FMT is a printf
12486 format string. If trace_redisplay_p is non-zero also printf the
12487 resulting string to stderr. */
12489 static void debug_method_add (struct window *, char const *, ...)
12490 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12492 static void
12493 debug_method_add (struct window *w, char const *fmt, ...)
12495 char buffer[512];
12496 char *method = w->desired_matrix->method;
12497 int len = strlen (method);
12498 int size = sizeof w->desired_matrix->method;
12499 int remaining = size - len - 1;
12500 va_list ap;
12502 va_start (ap, fmt);
12503 vsprintf (buffer, fmt, ap);
12504 va_end (ap);
12505 if (len && remaining)
12507 method[len] = '|';
12508 --remaining, ++len;
12511 strncpy (method + len, buffer, remaining);
12513 if (trace_redisplay_p)
12514 fprintf (stderr, "%p (%s): %s\n",
12516 ((BUFFERP (w->buffer)
12517 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12518 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12519 : "no buffer"),
12520 buffer);
12523 #endif /* GLYPH_DEBUG */
12526 /* Value is non-zero if all changes in window W, which displays
12527 current_buffer, are in the text between START and END. START is a
12528 buffer position, END is given as a distance from Z. Used in
12529 redisplay_internal for display optimization. */
12531 static inline int
12532 text_outside_line_unchanged_p (struct window *w,
12533 ptrdiff_t start, ptrdiff_t end)
12535 int unchanged_p = 1;
12537 /* If text or overlays have changed, see where. */
12538 if (w->last_modified < MODIFF
12539 || w->last_overlay_modified < OVERLAY_MODIFF)
12541 /* Gap in the line? */
12542 if (GPT < start || Z - GPT < end)
12543 unchanged_p = 0;
12545 /* Changes start in front of the line, or end after it? */
12546 if (unchanged_p
12547 && (BEG_UNCHANGED < start - 1
12548 || END_UNCHANGED < end))
12549 unchanged_p = 0;
12551 /* If selective display, can't optimize if changes start at the
12552 beginning of the line. */
12553 if (unchanged_p
12554 && INTEGERP (BVAR (current_buffer, selective_display))
12555 && XINT (BVAR (current_buffer, selective_display)) > 0
12556 && (BEG_UNCHANGED < start || GPT <= start))
12557 unchanged_p = 0;
12559 /* If there are overlays at the start or end of the line, these
12560 may have overlay strings with newlines in them. A change at
12561 START, for instance, may actually concern the display of such
12562 overlay strings as well, and they are displayed on different
12563 lines. So, quickly rule out this case. (For the future, it
12564 might be desirable to implement something more telling than
12565 just BEG/END_UNCHANGED.) */
12566 if (unchanged_p)
12568 if (BEG + BEG_UNCHANGED == start
12569 && overlay_touches_p (start))
12570 unchanged_p = 0;
12571 if (END_UNCHANGED == end
12572 && overlay_touches_p (Z - end))
12573 unchanged_p = 0;
12576 /* Under bidi reordering, adding or deleting a character in the
12577 beginning of a paragraph, before the first strong directional
12578 character, can change the base direction of the paragraph (unless
12579 the buffer specifies a fixed paragraph direction), which will
12580 require to redisplay the whole paragraph. It might be worthwhile
12581 to find the paragraph limits and widen the range of redisplayed
12582 lines to that, but for now just give up this optimization. */
12583 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12584 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12585 unchanged_p = 0;
12588 return unchanged_p;
12592 /* Do a frame update, taking possible shortcuts into account. This is
12593 the main external entry point for redisplay.
12595 If the last redisplay displayed an echo area message and that message
12596 is no longer requested, we clear the echo area or bring back the
12597 mini-buffer if that is in use. */
12599 void
12600 redisplay (void)
12602 redisplay_internal ();
12606 static Lisp_Object
12607 overlay_arrow_string_or_property (Lisp_Object var)
12609 Lisp_Object val;
12611 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12612 return val;
12614 return Voverlay_arrow_string;
12617 /* Return 1 if there are any overlay-arrows in current_buffer. */
12618 static int
12619 overlay_arrow_in_current_buffer_p (void)
12621 Lisp_Object vlist;
12623 for (vlist = Voverlay_arrow_variable_list;
12624 CONSP (vlist);
12625 vlist = XCDR (vlist))
12627 Lisp_Object var = XCAR (vlist);
12628 Lisp_Object val;
12630 if (!SYMBOLP (var))
12631 continue;
12632 val = find_symbol_value (var);
12633 if (MARKERP (val)
12634 && current_buffer == XMARKER (val)->buffer)
12635 return 1;
12637 return 0;
12641 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12642 has changed. */
12644 static int
12645 overlay_arrows_changed_p (void)
12647 Lisp_Object vlist;
12649 for (vlist = Voverlay_arrow_variable_list;
12650 CONSP (vlist);
12651 vlist = XCDR (vlist))
12653 Lisp_Object var = XCAR (vlist);
12654 Lisp_Object val, pstr;
12656 if (!SYMBOLP (var))
12657 continue;
12658 val = find_symbol_value (var);
12659 if (!MARKERP (val))
12660 continue;
12661 if (! EQ (COERCE_MARKER (val),
12662 Fget (var, Qlast_arrow_position))
12663 || ! (pstr = overlay_arrow_string_or_property (var),
12664 EQ (pstr, Fget (var, Qlast_arrow_string))))
12665 return 1;
12667 return 0;
12670 /* Mark overlay arrows to be updated on next redisplay. */
12672 static void
12673 update_overlay_arrows (int up_to_date)
12675 Lisp_Object vlist;
12677 for (vlist = Voverlay_arrow_variable_list;
12678 CONSP (vlist);
12679 vlist = XCDR (vlist))
12681 Lisp_Object var = XCAR (vlist);
12683 if (!SYMBOLP (var))
12684 continue;
12686 if (up_to_date > 0)
12688 Lisp_Object val = find_symbol_value (var);
12689 Fput (var, Qlast_arrow_position,
12690 COERCE_MARKER (val));
12691 Fput (var, Qlast_arrow_string,
12692 overlay_arrow_string_or_property (var));
12694 else if (up_to_date < 0
12695 || !NILP (Fget (var, Qlast_arrow_position)))
12697 Fput (var, Qlast_arrow_position, Qt);
12698 Fput (var, Qlast_arrow_string, Qt);
12704 /* Return overlay arrow string to display at row.
12705 Return integer (bitmap number) for arrow bitmap in left fringe.
12706 Return nil if no overlay arrow. */
12708 static Lisp_Object
12709 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12711 Lisp_Object vlist;
12713 for (vlist = Voverlay_arrow_variable_list;
12714 CONSP (vlist);
12715 vlist = XCDR (vlist))
12717 Lisp_Object var = XCAR (vlist);
12718 Lisp_Object val;
12720 if (!SYMBOLP (var))
12721 continue;
12723 val = find_symbol_value (var);
12725 if (MARKERP (val)
12726 && current_buffer == XMARKER (val)->buffer
12727 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12729 if (FRAME_WINDOW_P (it->f)
12730 /* FIXME: if ROW->reversed_p is set, this should test
12731 the right fringe, not the left one. */
12732 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12734 #ifdef HAVE_WINDOW_SYSTEM
12735 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12737 int fringe_bitmap;
12738 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12739 return make_number (fringe_bitmap);
12741 #endif
12742 return make_number (-1); /* Use default arrow bitmap */
12744 return overlay_arrow_string_or_property (var);
12748 return Qnil;
12751 /* Return 1 if point moved out of or into a composition. Otherwise
12752 return 0. PREV_BUF and PREV_PT are the last point buffer and
12753 position. BUF and PT are the current point buffer and position. */
12755 static int
12756 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12757 struct buffer *buf, ptrdiff_t pt)
12759 ptrdiff_t start, end;
12760 Lisp_Object prop;
12761 Lisp_Object buffer;
12763 XSETBUFFER (buffer, buf);
12764 /* Check a composition at the last point if point moved within the
12765 same buffer. */
12766 if (prev_buf == buf)
12768 if (prev_pt == pt)
12769 /* Point didn't move. */
12770 return 0;
12772 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12773 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12774 && COMPOSITION_VALID_P (start, end, prop)
12775 && start < prev_pt && end > prev_pt)
12776 /* The last point was within the composition. Return 1 iff
12777 point moved out of the composition. */
12778 return (pt <= start || pt >= end);
12781 /* Check a composition at the current point. */
12782 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12783 && find_composition (pt, -1, &start, &end, &prop, buffer)
12784 && COMPOSITION_VALID_P (start, end, prop)
12785 && start < pt && end > pt);
12789 /* Reconsider the setting of B->clip_changed which is displayed
12790 in window W. */
12792 static inline void
12793 reconsider_clip_changes (struct window *w, struct buffer *b)
12795 if (b->clip_changed
12796 && !NILP (w->window_end_valid)
12797 && w->current_matrix->buffer == b
12798 && w->current_matrix->zv == BUF_ZV (b)
12799 && w->current_matrix->begv == BUF_BEGV (b))
12800 b->clip_changed = 0;
12802 /* If display wasn't paused, and W is not a tool bar window, see if
12803 point has been moved into or out of a composition. In that case,
12804 we set b->clip_changed to 1 to force updating the screen. If
12805 b->clip_changed has already been set to 1, we can skip this
12806 check. */
12807 if (!b->clip_changed
12808 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12810 ptrdiff_t pt;
12812 if (w == XWINDOW (selected_window))
12813 pt = PT;
12814 else
12815 pt = marker_position (w->pointm);
12817 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12818 || pt != w->last_point)
12819 && check_point_in_composition (w->current_matrix->buffer,
12820 w->last_point,
12821 XBUFFER (w->buffer), pt))
12822 b->clip_changed = 1;
12827 /* Select FRAME to forward the values of frame-local variables into C
12828 variables so that the redisplay routines can access those values
12829 directly. */
12831 static void
12832 select_frame_for_redisplay (Lisp_Object frame)
12834 Lisp_Object tail, tem;
12835 Lisp_Object old = selected_frame;
12836 struct Lisp_Symbol *sym;
12838 eassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12840 selected_frame = frame;
12842 do {
12843 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12844 if (CONSP (XCAR (tail))
12845 && (tem = XCAR (XCAR (tail)),
12846 SYMBOLP (tem))
12847 && (sym = indirect_variable (XSYMBOL (tem)),
12848 sym->redirect == SYMBOL_LOCALIZED)
12849 && sym->val.blv->frame_local)
12850 /* Use find_symbol_value rather than Fsymbol_value
12851 to avoid an error if it is void. */
12852 find_symbol_value (tem);
12853 } while (!EQ (frame, old) && (frame = old, 1));
12857 #define STOP_POLLING \
12858 do { if (! polling_stopped_here) stop_polling (); \
12859 polling_stopped_here = 1; } while (0)
12861 #define RESUME_POLLING \
12862 do { if (polling_stopped_here) start_polling (); \
12863 polling_stopped_here = 0; } while (0)
12866 /* Perhaps in the future avoid recentering windows if it
12867 is not necessary; currently that causes some problems. */
12869 static void
12870 redisplay_internal (void)
12872 struct window *w = XWINDOW (selected_window);
12873 struct window *sw;
12874 struct frame *fr;
12875 int pending;
12876 int must_finish = 0;
12877 struct text_pos tlbufpos, tlendpos;
12878 int number_of_visible_frames;
12879 ptrdiff_t count, count1;
12880 struct frame *sf;
12881 int polling_stopped_here = 0;
12882 Lisp_Object old_frame = selected_frame;
12884 /* Non-zero means redisplay has to consider all windows on all
12885 frames. Zero means, only selected_window is considered. */
12886 int consider_all_windows_p;
12888 /* Non-zero means redisplay has to redisplay the miniwindow */
12889 int update_miniwindow_p = 0;
12891 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12893 /* No redisplay if running in batch mode or frame is not yet fully
12894 initialized, or redisplay is explicitly turned off by setting
12895 Vinhibit_redisplay. */
12896 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12897 || !NILP (Vinhibit_redisplay))
12898 return;
12900 /* Don't examine these until after testing Vinhibit_redisplay.
12901 When Emacs is shutting down, perhaps because its connection to
12902 X has dropped, we should not look at them at all. */
12903 fr = XFRAME (w->frame);
12904 sf = SELECTED_FRAME ();
12906 if (!fr->glyphs_initialized_p)
12907 return;
12909 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12910 if (popup_activated ())
12911 return;
12912 #endif
12914 /* I don't think this happens but let's be paranoid. */
12915 if (redisplaying_p)
12916 return;
12918 /* Record a function that resets redisplaying_p to its old value
12919 when we leave this function. */
12920 count = SPECPDL_INDEX ();
12921 record_unwind_protect (unwind_redisplay,
12922 Fcons (make_number (redisplaying_p), selected_frame));
12923 ++redisplaying_p;
12924 specbind (Qinhibit_free_realized_faces, Qnil);
12927 Lisp_Object tail, frame;
12929 FOR_EACH_FRAME (tail, frame)
12931 struct frame *f = XFRAME (frame);
12932 f->already_hscrolled_p = 0;
12936 retry:
12937 /* Remember the currently selected window. */
12938 sw = w;
12940 if (!EQ (old_frame, selected_frame)
12941 && FRAME_LIVE_P (XFRAME (old_frame)))
12942 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12943 selected_frame and selected_window to be temporarily out-of-sync so
12944 when we come back here via `goto retry', we need to resync because we
12945 may need to run Elisp code (via prepare_menu_bars). */
12946 select_frame_for_redisplay (old_frame);
12948 pending = 0;
12949 reconsider_clip_changes (w, current_buffer);
12950 last_escape_glyph_frame = NULL;
12951 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12952 last_glyphless_glyph_frame = NULL;
12953 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12955 /* If new fonts have been loaded that make a glyph matrix adjustment
12956 necessary, do it. */
12957 if (fonts_changed_p)
12959 adjust_glyphs (NULL);
12960 ++windows_or_buffers_changed;
12961 fonts_changed_p = 0;
12964 /* If face_change_count is non-zero, init_iterator will free all
12965 realized faces, which includes the faces referenced from current
12966 matrices. So, we can't reuse current matrices in this case. */
12967 if (face_change_count)
12968 ++windows_or_buffers_changed;
12970 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12971 && FRAME_TTY (sf)->previous_frame != sf)
12973 /* Since frames on a single ASCII terminal share the same
12974 display area, displaying a different frame means redisplay
12975 the whole thing. */
12976 windows_or_buffers_changed++;
12977 SET_FRAME_GARBAGED (sf);
12978 #ifndef DOS_NT
12979 set_tty_color_mode (FRAME_TTY (sf), sf);
12980 #endif
12981 FRAME_TTY (sf)->previous_frame = sf;
12984 /* Set the visible flags for all frames. Do this before checking
12985 for resized or garbaged frames; they want to know if their frames
12986 are visible. See the comment in frame.h for
12987 FRAME_SAMPLE_VISIBILITY. */
12989 Lisp_Object tail, frame;
12991 number_of_visible_frames = 0;
12993 FOR_EACH_FRAME (tail, frame)
12995 struct frame *f = XFRAME (frame);
12997 FRAME_SAMPLE_VISIBILITY (f);
12998 if (FRAME_VISIBLE_P (f))
12999 ++number_of_visible_frames;
13000 clear_desired_matrices (f);
13004 /* Notice any pending interrupt request to change frame size. */
13005 do_pending_window_change (1);
13007 /* do_pending_window_change could change the selected_window due to
13008 frame resizing which makes the selected window too small. */
13009 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13011 sw = w;
13012 reconsider_clip_changes (w, current_buffer);
13015 /* Clear frames marked as garbaged. */
13016 if (frame_garbaged)
13017 clear_garbaged_frames ();
13019 /* Build menubar and tool-bar items. */
13020 if (NILP (Vmemory_full))
13021 prepare_menu_bars ();
13023 if (windows_or_buffers_changed)
13024 update_mode_lines++;
13026 /* Detect case that we need to write or remove a star in the mode line. */
13027 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13029 w->update_mode_line = 1;
13030 if (buffer_shared > 1)
13031 update_mode_lines++;
13034 /* Avoid invocation of point motion hooks by `current_column' below. */
13035 count1 = SPECPDL_INDEX ();
13036 specbind (Qinhibit_point_motion_hooks, Qt);
13038 /* If %c is in the mode line, update it if needed. */
13039 if (!NILP (w->column_number_displayed)
13040 /* This alternative quickly identifies a common case
13041 where no change is needed. */
13042 && !(PT == w->last_point
13043 && w->last_modified >= MODIFF
13044 && w->last_overlay_modified >= OVERLAY_MODIFF)
13045 && (XFASTINT (w->column_number_displayed) != current_column ()))
13046 w->update_mode_line = 1;
13048 unbind_to (count1, Qnil);
13050 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
13052 /* The variable buffer_shared is set in redisplay_window and
13053 indicates that we redisplay a buffer in different windows. See
13054 there. */
13055 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
13056 || cursor_type_changed);
13058 /* If specs for an arrow have changed, do thorough redisplay
13059 to ensure we remove any arrow that should no longer exist. */
13060 if (overlay_arrows_changed_p ())
13061 consider_all_windows_p = windows_or_buffers_changed = 1;
13063 /* Normally the message* functions will have already displayed and
13064 updated the echo area, but the frame may have been trashed, or
13065 the update may have been preempted, so display the echo area
13066 again here. Checking message_cleared_p captures the case that
13067 the echo area should be cleared. */
13068 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13069 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13070 || (message_cleared_p
13071 && minibuf_level == 0
13072 /* If the mini-window is currently selected, this means the
13073 echo-area doesn't show through. */
13074 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13076 int window_height_changed_p = echo_area_display (0);
13078 if (message_cleared_p)
13079 update_miniwindow_p = 1;
13081 must_finish = 1;
13083 /* If we don't display the current message, don't clear the
13084 message_cleared_p flag, because, if we did, we wouldn't clear
13085 the echo area in the next redisplay which doesn't preserve
13086 the echo area. */
13087 if (!display_last_displayed_message_p)
13088 message_cleared_p = 0;
13090 if (fonts_changed_p)
13091 goto retry;
13092 else if (window_height_changed_p)
13094 consider_all_windows_p = 1;
13095 ++update_mode_lines;
13096 ++windows_or_buffers_changed;
13098 /* If window configuration was changed, frames may have been
13099 marked garbaged. Clear them or we will experience
13100 surprises wrt scrolling. */
13101 if (frame_garbaged)
13102 clear_garbaged_frames ();
13105 else if (EQ (selected_window, minibuf_window)
13106 && (current_buffer->clip_changed
13107 || w->last_modified < MODIFF
13108 || w->last_overlay_modified < OVERLAY_MODIFF)
13109 && resize_mini_window (w, 0))
13111 /* Resized active mini-window to fit the size of what it is
13112 showing if its contents might have changed. */
13113 must_finish = 1;
13114 /* FIXME: this causes all frames to be updated, which seems unnecessary
13115 since only the current frame needs to be considered. This function needs
13116 to be rewritten with two variables, consider_all_windows and
13117 consider_all_frames. */
13118 consider_all_windows_p = 1;
13119 ++windows_or_buffers_changed;
13120 ++update_mode_lines;
13122 /* If window configuration was changed, frames may have been
13123 marked garbaged. Clear them or we will experience
13124 surprises wrt scrolling. */
13125 if (frame_garbaged)
13126 clear_garbaged_frames ();
13130 /* If showing the region, and mark has changed, we must redisplay
13131 the whole window. The assignment to this_line_start_pos prevents
13132 the optimization directly below this if-statement. */
13133 if (((!NILP (Vtransient_mark_mode)
13134 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13135 != !NILP (w->region_showing))
13136 || (!NILP (w->region_showing)
13137 && !EQ (w->region_showing,
13138 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13139 CHARPOS (this_line_start_pos) = 0;
13141 /* Optimize the case that only the line containing the cursor in the
13142 selected window has changed. Variables starting with this_ are
13143 set in display_line and record information about the line
13144 containing the cursor. */
13145 tlbufpos = this_line_start_pos;
13146 tlendpos = this_line_end_pos;
13147 if (!consider_all_windows_p
13148 && CHARPOS (tlbufpos) > 0
13149 && !w->update_mode_line
13150 && !current_buffer->clip_changed
13151 && !current_buffer->prevent_redisplay_optimizations_p
13152 && FRAME_VISIBLE_P (XFRAME (w->frame))
13153 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13154 /* Make sure recorded data applies to current buffer, etc. */
13155 && this_line_buffer == current_buffer
13156 && current_buffer == XBUFFER (w->buffer)
13157 && !w->force_start
13158 && !w->optional_new_start
13159 /* Point must be on the line that we have info recorded about. */
13160 && PT >= CHARPOS (tlbufpos)
13161 && PT <= Z - CHARPOS (tlendpos)
13162 /* All text outside that line, including its final newline,
13163 must be unchanged. */
13164 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13165 CHARPOS (tlendpos)))
13167 if (CHARPOS (tlbufpos) > BEGV
13168 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13169 && (CHARPOS (tlbufpos) == ZV
13170 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13171 /* Former continuation line has disappeared by becoming empty. */
13172 goto cancel;
13173 else if (w->last_modified < MODIFF
13174 || w->last_overlay_modified < OVERLAY_MODIFF
13175 || MINI_WINDOW_P (w))
13177 /* We have to handle the case of continuation around a
13178 wide-column character (see the comment in indent.c around
13179 line 1340).
13181 For instance, in the following case:
13183 -------- Insert --------
13184 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13185 J_I_ ==> J_I_ `^^' are cursors.
13186 ^^ ^^
13187 -------- --------
13189 As we have to redraw the line above, we cannot use this
13190 optimization. */
13192 struct it it;
13193 int line_height_before = this_line_pixel_height;
13195 /* Note that start_display will handle the case that the
13196 line starting at tlbufpos is a continuation line. */
13197 start_display (&it, w, tlbufpos);
13199 /* Implementation note: It this still necessary? */
13200 if (it.current_x != this_line_start_x)
13201 goto cancel;
13203 TRACE ((stderr, "trying display optimization 1\n"));
13204 w->cursor.vpos = -1;
13205 overlay_arrow_seen = 0;
13206 it.vpos = this_line_vpos;
13207 it.current_y = this_line_y;
13208 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13209 display_line (&it);
13211 /* If line contains point, is not continued,
13212 and ends at same distance from eob as before, we win. */
13213 if (w->cursor.vpos >= 0
13214 /* Line is not continued, otherwise this_line_start_pos
13215 would have been set to 0 in display_line. */
13216 && CHARPOS (this_line_start_pos)
13217 /* Line ends as before. */
13218 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13219 /* Line has same height as before. Otherwise other lines
13220 would have to be shifted up or down. */
13221 && this_line_pixel_height == line_height_before)
13223 /* If this is not the window's last line, we must adjust
13224 the charstarts of the lines below. */
13225 if (it.current_y < it.last_visible_y)
13227 struct glyph_row *row
13228 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13229 ptrdiff_t delta, delta_bytes;
13231 /* We used to distinguish between two cases here,
13232 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13233 when the line ends in a newline or the end of the
13234 buffer's accessible portion. But both cases did
13235 the same, so they were collapsed. */
13236 delta = (Z
13237 - CHARPOS (tlendpos)
13238 - MATRIX_ROW_START_CHARPOS (row));
13239 delta_bytes = (Z_BYTE
13240 - BYTEPOS (tlendpos)
13241 - MATRIX_ROW_START_BYTEPOS (row));
13243 increment_matrix_positions (w->current_matrix,
13244 this_line_vpos + 1,
13245 w->current_matrix->nrows,
13246 delta, delta_bytes);
13249 /* If this row displays text now but previously didn't,
13250 or vice versa, w->window_end_vpos may have to be
13251 adjusted. */
13252 if ((it.glyph_row - 1)->displays_text_p)
13254 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13255 XSETINT (w->window_end_vpos, this_line_vpos);
13257 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13258 && this_line_vpos > 0)
13259 XSETINT (w->window_end_vpos, this_line_vpos - 1);
13260 w->window_end_valid = Qnil;
13262 /* Update hint: No need to try to scroll in update_window. */
13263 w->desired_matrix->no_scrolling_p = 1;
13265 #ifdef GLYPH_DEBUG
13266 *w->desired_matrix->method = 0;
13267 debug_method_add (w, "optimization 1");
13268 #endif
13269 #ifdef HAVE_WINDOW_SYSTEM
13270 update_window_fringes (w, 0);
13271 #endif
13272 goto update;
13274 else
13275 goto cancel;
13277 else if (/* Cursor position hasn't changed. */
13278 PT == w->last_point
13279 /* Make sure the cursor was last displayed
13280 in this window. Otherwise we have to reposition it. */
13281 && 0 <= w->cursor.vpos
13282 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13284 if (!must_finish)
13286 do_pending_window_change (1);
13287 /* If selected_window changed, redisplay again. */
13288 if (WINDOWP (selected_window)
13289 && (w = XWINDOW (selected_window)) != sw)
13290 goto retry;
13292 /* We used to always goto end_of_redisplay here, but this
13293 isn't enough if we have a blinking cursor. */
13294 if (w->cursor_off_p == w->last_cursor_off_p)
13295 goto end_of_redisplay;
13297 goto update;
13299 /* If highlighting the region, or if the cursor is in the echo area,
13300 then we can't just move the cursor. */
13301 else if (! (!NILP (Vtransient_mark_mode)
13302 && !NILP (BVAR (current_buffer, mark_active)))
13303 && (EQ (selected_window,
13304 BVAR (current_buffer, last_selected_window))
13305 || highlight_nonselected_windows)
13306 && NILP (w->region_showing)
13307 && NILP (Vshow_trailing_whitespace)
13308 && !cursor_in_echo_area)
13310 struct it it;
13311 struct glyph_row *row;
13313 /* Skip from tlbufpos to PT and see where it is. Note that
13314 PT may be in invisible text. If so, we will end at the
13315 next visible position. */
13316 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13317 NULL, DEFAULT_FACE_ID);
13318 it.current_x = this_line_start_x;
13319 it.current_y = this_line_y;
13320 it.vpos = this_line_vpos;
13322 /* The call to move_it_to stops in front of PT, but
13323 moves over before-strings. */
13324 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13326 if (it.vpos == this_line_vpos
13327 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13328 row->enabled_p))
13330 eassert (this_line_vpos == it.vpos);
13331 eassert (this_line_y == it.current_y);
13332 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13333 #ifdef GLYPH_DEBUG
13334 *w->desired_matrix->method = 0;
13335 debug_method_add (w, "optimization 3");
13336 #endif
13337 goto update;
13339 else
13340 goto cancel;
13343 cancel:
13344 /* Text changed drastically or point moved off of line. */
13345 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13348 CHARPOS (this_line_start_pos) = 0;
13349 consider_all_windows_p |= buffer_shared > 1;
13350 ++clear_face_cache_count;
13351 #ifdef HAVE_WINDOW_SYSTEM
13352 ++clear_image_cache_count;
13353 #endif
13355 /* Build desired matrices, and update the display. If
13356 consider_all_windows_p is non-zero, do it for all windows on all
13357 frames. Otherwise do it for selected_window, only. */
13359 if (consider_all_windows_p)
13361 Lisp_Object tail, frame;
13363 FOR_EACH_FRAME (tail, frame)
13364 XFRAME (frame)->updated_p = 0;
13366 /* Recompute # windows showing selected buffer. This will be
13367 incremented each time such a window is displayed. */
13368 buffer_shared = 0;
13370 FOR_EACH_FRAME (tail, frame)
13372 struct frame *f = XFRAME (frame);
13374 /* We don't have to do anything for unselected terminal
13375 frames. */
13376 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13377 && !EQ (FRAME_TTY (f)->top_frame, frame))
13378 continue;
13380 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13382 if (! EQ (frame, selected_frame))
13383 /* Select the frame, for the sake of frame-local
13384 variables. */
13385 select_frame_for_redisplay (frame);
13387 /* Mark all the scroll bars to be removed; we'll redeem
13388 the ones we want when we redisplay their windows. */
13389 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13390 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13392 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13393 redisplay_windows (FRAME_ROOT_WINDOW (f));
13395 /* The X error handler may have deleted that frame. */
13396 if (!FRAME_LIVE_P (f))
13397 continue;
13399 /* Any scroll bars which redisplay_windows should have
13400 nuked should now go away. */
13401 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13402 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13404 /* If fonts changed, display again. */
13405 /* ??? rms: I suspect it is a mistake to jump all the way
13406 back to retry here. It should just retry this frame. */
13407 if (fonts_changed_p)
13408 goto retry;
13410 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13412 /* See if we have to hscroll. */
13413 if (!f->already_hscrolled_p)
13415 f->already_hscrolled_p = 1;
13416 if (hscroll_windows (f->root_window))
13417 goto retry;
13420 /* Prevent various kinds of signals during display
13421 update. stdio is not robust about handling
13422 signals, which can cause an apparent I/O
13423 error. */
13424 if (interrupt_input)
13425 unrequest_sigio ();
13426 STOP_POLLING;
13428 /* Update the display. */
13429 set_window_update_flags (XWINDOW (f->root_window), 1);
13430 pending |= update_frame (f, 0, 0);
13431 f->updated_p = 1;
13436 if (!EQ (old_frame, selected_frame)
13437 && FRAME_LIVE_P (XFRAME (old_frame)))
13438 /* We played a bit fast-and-loose above and allowed selected_frame
13439 and selected_window to be temporarily out-of-sync but let's make
13440 sure this stays contained. */
13441 select_frame_for_redisplay (old_frame);
13442 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13444 if (!pending)
13446 /* Do the mark_window_display_accurate after all windows have
13447 been redisplayed because this call resets flags in buffers
13448 which are needed for proper redisplay. */
13449 FOR_EACH_FRAME (tail, frame)
13451 struct frame *f = XFRAME (frame);
13452 if (f->updated_p)
13454 mark_window_display_accurate (f->root_window, 1);
13455 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13456 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13461 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13463 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13464 struct frame *mini_frame;
13466 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13467 /* Use list_of_error, not Qerror, so that
13468 we catch only errors and don't run the debugger. */
13469 internal_condition_case_1 (redisplay_window_1, selected_window,
13470 list_of_error,
13471 redisplay_window_error);
13472 if (update_miniwindow_p)
13473 internal_condition_case_1 (redisplay_window_1, mini_window,
13474 list_of_error,
13475 redisplay_window_error);
13477 /* Compare desired and current matrices, perform output. */
13479 update:
13480 /* If fonts changed, display again. */
13481 if (fonts_changed_p)
13482 goto retry;
13484 /* Prevent various kinds of signals during display update.
13485 stdio is not robust about handling signals,
13486 which can cause an apparent I/O error. */
13487 if (interrupt_input)
13488 unrequest_sigio ();
13489 STOP_POLLING;
13491 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13493 if (hscroll_windows (selected_window))
13494 goto retry;
13496 XWINDOW (selected_window)->must_be_updated_p = 1;
13497 pending = update_frame (sf, 0, 0);
13500 /* We may have called echo_area_display at the top of this
13501 function. If the echo area is on another frame, that may
13502 have put text on a frame other than the selected one, so the
13503 above call to update_frame would not have caught it. Catch
13504 it here. */
13505 mini_window = FRAME_MINIBUF_WINDOW (sf);
13506 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13508 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13510 XWINDOW (mini_window)->must_be_updated_p = 1;
13511 pending |= update_frame (mini_frame, 0, 0);
13512 if (!pending && hscroll_windows (mini_window))
13513 goto retry;
13517 /* If display was paused because of pending input, make sure we do a
13518 thorough update the next time. */
13519 if (pending)
13521 /* Prevent the optimization at the beginning of
13522 redisplay_internal that tries a single-line update of the
13523 line containing the cursor in the selected window. */
13524 CHARPOS (this_line_start_pos) = 0;
13526 /* Let the overlay arrow be updated the next time. */
13527 update_overlay_arrows (0);
13529 /* If we pause after scrolling, some rows in the current
13530 matrices of some windows are not valid. */
13531 if (!WINDOW_FULL_WIDTH_P (w)
13532 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13533 update_mode_lines = 1;
13535 else
13537 if (!consider_all_windows_p)
13539 /* This has already been done above if
13540 consider_all_windows_p is set. */
13541 mark_window_display_accurate_1 (w, 1);
13543 /* Say overlay arrows are up to date. */
13544 update_overlay_arrows (1);
13546 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13547 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13550 update_mode_lines = 0;
13551 windows_or_buffers_changed = 0;
13552 cursor_type_changed = 0;
13555 /* Start SIGIO interrupts coming again. Having them off during the
13556 code above makes it less likely one will discard output, but not
13557 impossible, since there might be stuff in the system buffer here.
13558 But it is much hairier to try to do anything about that. */
13559 if (interrupt_input)
13560 request_sigio ();
13561 RESUME_POLLING;
13563 /* If a frame has become visible which was not before, redisplay
13564 again, so that we display it. Expose events for such a frame
13565 (which it gets when becoming visible) don't call the parts of
13566 redisplay constructing glyphs, so simply exposing a frame won't
13567 display anything in this case. So, we have to display these
13568 frames here explicitly. */
13569 if (!pending)
13571 Lisp_Object tail, frame;
13572 int new_count = 0;
13574 FOR_EACH_FRAME (tail, frame)
13576 int this_is_visible = 0;
13578 if (XFRAME (frame)->visible)
13579 this_is_visible = 1;
13580 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13581 if (XFRAME (frame)->visible)
13582 this_is_visible = 1;
13584 if (this_is_visible)
13585 new_count++;
13588 if (new_count != number_of_visible_frames)
13589 windows_or_buffers_changed++;
13592 /* Change frame size now if a change is pending. */
13593 do_pending_window_change (1);
13595 /* If we just did a pending size change, or have additional
13596 visible frames, or selected_window changed, redisplay again. */
13597 if ((windows_or_buffers_changed && !pending)
13598 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13599 goto retry;
13601 /* Clear the face and image caches.
13603 We used to do this only if consider_all_windows_p. But the cache
13604 needs to be cleared if a timer creates images in the current
13605 buffer (e.g. the test case in Bug#6230). */
13607 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13609 clear_face_cache (0);
13610 clear_face_cache_count = 0;
13613 #ifdef HAVE_WINDOW_SYSTEM
13614 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13616 clear_image_caches (Qnil);
13617 clear_image_cache_count = 0;
13619 #endif /* HAVE_WINDOW_SYSTEM */
13621 end_of_redisplay:
13622 unbind_to (count, Qnil);
13623 RESUME_POLLING;
13627 /* Redisplay, but leave alone any recent echo area message unless
13628 another message has been requested in its place.
13630 This is useful in situations where you need to redisplay but no
13631 user action has occurred, making it inappropriate for the message
13632 area to be cleared. See tracking_off and
13633 wait_reading_process_output for examples of these situations.
13635 FROM_WHERE is an integer saying from where this function was
13636 called. This is useful for debugging. */
13638 void
13639 redisplay_preserve_echo_area (int from_where)
13641 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13643 if (!NILP (echo_area_buffer[1]))
13645 /* We have a previously displayed message, but no current
13646 message. Redisplay the previous message. */
13647 display_last_displayed_message_p = 1;
13648 redisplay_internal ();
13649 display_last_displayed_message_p = 0;
13651 else
13652 redisplay_internal ();
13654 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13655 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13656 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13660 /* Function registered with record_unwind_protect in
13661 redisplay_internal. Reset redisplaying_p to the value it had
13662 before redisplay_internal was called, and clear
13663 prevent_freeing_realized_faces_p. It also selects the previously
13664 selected frame, unless it has been deleted (by an X connection
13665 failure during redisplay, for example). */
13667 static Lisp_Object
13668 unwind_redisplay (Lisp_Object val)
13670 Lisp_Object old_redisplaying_p, old_frame;
13672 old_redisplaying_p = XCAR (val);
13673 redisplaying_p = XFASTINT (old_redisplaying_p);
13674 old_frame = XCDR (val);
13675 if (! EQ (old_frame, selected_frame)
13676 && FRAME_LIVE_P (XFRAME (old_frame)))
13677 select_frame_for_redisplay (old_frame);
13678 return Qnil;
13682 /* Mark the display of window W as accurate or inaccurate. If
13683 ACCURATE_P is non-zero mark display of W as accurate. If
13684 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13685 redisplay_internal is called. */
13687 static void
13688 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13690 if (BUFFERP (w->buffer))
13692 struct buffer *b = XBUFFER (w->buffer);
13694 w->last_modified = accurate_p ? BUF_MODIFF(b) : 0;
13695 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF(b) : 0;
13696 w->last_had_star
13697 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13699 if (accurate_p)
13701 b->clip_changed = 0;
13702 b->prevent_redisplay_optimizations_p = 0;
13704 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13705 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13706 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13707 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13709 w->current_matrix->buffer = b;
13710 w->current_matrix->begv = BUF_BEGV (b);
13711 w->current_matrix->zv = BUF_ZV (b);
13713 w->last_cursor = w->cursor;
13714 w->last_cursor_off_p = w->cursor_off_p;
13716 if (w == XWINDOW (selected_window))
13717 w->last_point = BUF_PT (b);
13718 else
13719 w->last_point = XMARKER (w->pointm)->charpos;
13723 if (accurate_p)
13725 w->window_end_valid = w->buffer;
13726 w->update_mode_line = 0;
13731 /* Mark the display of windows in the window tree rooted at WINDOW as
13732 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13733 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13734 be redisplayed the next time redisplay_internal is called. */
13736 void
13737 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13739 struct window *w;
13741 for (; !NILP (window); window = w->next)
13743 w = XWINDOW (window);
13744 mark_window_display_accurate_1 (w, accurate_p);
13746 if (!NILP (w->vchild))
13747 mark_window_display_accurate (w->vchild, accurate_p);
13748 if (!NILP (w->hchild))
13749 mark_window_display_accurate (w->hchild, accurate_p);
13752 if (accurate_p)
13754 update_overlay_arrows (1);
13756 else
13758 /* Force a thorough redisplay the next time by setting
13759 last_arrow_position and last_arrow_string to t, which is
13760 unequal to any useful value of Voverlay_arrow_... */
13761 update_overlay_arrows (-1);
13766 /* Return value in display table DP (Lisp_Char_Table *) for character
13767 C. Since a display table doesn't have any parent, we don't have to
13768 follow parent. Do not call this function directly but use the
13769 macro DISP_CHAR_VECTOR. */
13771 Lisp_Object
13772 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13774 Lisp_Object val;
13776 if (ASCII_CHAR_P (c))
13778 val = dp->ascii;
13779 if (SUB_CHAR_TABLE_P (val))
13780 val = XSUB_CHAR_TABLE (val)->contents[c];
13782 else
13784 Lisp_Object table;
13786 XSETCHAR_TABLE (table, dp);
13787 val = char_table_ref (table, c);
13789 if (NILP (val))
13790 val = dp->defalt;
13791 return val;
13796 /***********************************************************************
13797 Window Redisplay
13798 ***********************************************************************/
13800 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13802 static void
13803 redisplay_windows (Lisp_Object window)
13805 while (!NILP (window))
13807 struct window *w = XWINDOW (window);
13809 if (!NILP (w->hchild))
13810 redisplay_windows (w->hchild);
13811 else if (!NILP (w->vchild))
13812 redisplay_windows (w->vchild);
13813 else if (!NILP (w->buffer))
13815 displayed_buffer = XBUFFER (w->buffer);
13816 /* Use list_of_error, not Qerror, so that
13817 we catch only errors and don't run the debugger. */
13818 internal_condition_case_1 (redisplay_window_0, window,
13819 list_of_error,
13820 redisplay_window_error);
13823 window = w->next;
13827 static Lisp_Object
13828 redisplay_window_error (Lisp_Object ignore)
13830 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13831 return Qnil;
13834 static Lisp_Object
13835 redisplay_window_0 (Lisp_Object window)
13837 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13838 redisplay_window (window, 0);
13839 return Qnil;
13842 static Lisp_Object
13843 redisplay_window_1 (Lisp_Object window)
13845 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13846 redisplay_window (window, 1);
13847 return Qnil;
13851 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13852 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13853 which positions recorded in ROW differ from current buffer
13854 positions.
13856 Return 0 if cursor is not on this row, 1 otherwise. */
13858 static int
13859 set_cursor_from_row (struct window *w, struct glyph_row *row,
13860 struct glyph_matrix *matrix,
13861 ptrdiff_t delta, ptrdiff_t delta_bytes,
13862 int dy, int dvpos)
13864 struct glyph *glyph = row->glyphs[TEXT_AREA];
13865 struct glyph *end = glyph + row->used[TEXT_AREA];
13866 struct glyph *cursor = NULL;
13867 /* The last known character position in row. */
13868 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13869 int x = row->x;
13870 ptrdiff_t pt_old = PT - delta;
13871 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13872 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13873 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13874 /* A glyph beyond the edge of TEXT_AREA which we should never
13875 touch. */
13876 struct glyph *glyphs_end = end;
13877 /* Non-zero means we've found a match for cursor position, but that
13878 glyph has the avoid_cursor_p flag set. */
13879 int match_with_avoid_cursor = 0;
13880 /* Non-zero means we've seen at least one glyph that came from a
13881 display string. */
13882 int string_seen = 0;
13883 /* Largest and smallest buffer positions seen so far during scan of
13884 glyph row. */
13885 ptrdiff_t bpos_max = pos_before;
13886 ptrdiff_t bpos_min = pos_after;
13887 /* Last buffer position covered by an overlay string with an integer
13888 `cursor' property. */
13889 ptrdiff_t bpos_covered = 0;
13890 /* Non-zero means the display string on which to display the cursor
13891 comes from a text property, not from an overlay. */
13892 int string_from_text_prop = 0;
13894 /* Don't even try doing anything if called for a mode-line or
13895 header-line row, since the rest of the code isn't prepared to
13896 deal with such calamities. */
13897 eassert (!row->mode_line_p);
13898 if (row->mode_line_p)
13899 return 0;
13901 /* Skip over glyphs not having an object at the start and the end of
13902 the row. These are special glyphs like truncation marks on
13903 terminal frames. */
13904 if (row->displays_text_p)
13906 if (!row->reversed_p)
13908 while (glyph < end
13909 && INTEGERP (glyph->object)
13910 && glyph->charpos < 0)
13912 x += glyph->pixel_width;
13913 ++glyph;
13915 while (end > glyph
13916 && INTEGERP ((end - 1)->object)
13917 /* CHARPOS is zero for blanks and stretch glyphs
13918 inserted by extend_face_to_end_of_line. */
13919 && (end - 1)->charpos <= 0)
13920 --end;
13921 glyph_before = glyph - 1;
13922 glyph_after = end;
13924 else
13926 struct glyph *g;
13928 /* If the glyph row is reversed, we need to process it from back
13929 to front, so swap the edge pointers. */
13930 glyphs_end = end = glyph - 1;
13931 glyph += row->used[TEXT_AREA] - 1;
13933 while (glyph > end + 1
13934 && INTEGERP (glyph->object)
13935 && glyph->charpos < 0)
13937 --glyph;
13938 x -= glyph->pixel_width;
13940 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13941 --glyph;
13942 /* By default, in reversed rows we put the cursor on the
13943 rightmost (first in the reading order) glyph. */
13944 for (g = end + 1; g < glyph; g++)
13945 x += g->pixel_width;
13946 while (end < glyph
13947 && INTEGERP ((end + 1)->object)
13948 && (end + 1)->charpos <= 0)
13949 ++end;
13950 glyph_before = glyph + 1;
13951 glyph_after = end;
13954 else if (row->reversed_p)
13956 /* In R2L rows that don't display text, put the cursor on the
13957 rightmost glyph. Case in point: an empty last line that is
13958 part of an R2L paragraph. */
13959 cursor = end - 1;
13960 /* Avoid placing the cursor on the last glyph of the row, where
13961 on terminal frames we hold the vertical border between
13962 adjacent windows. */
13963 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13964 && !WINDOW_RIGHTMOST_P (w)
13965 && cursor == row->glyphs[LAST_AREA] - 1)
13966 cursor--;
13967 x = -1; /* will be computed below, at label compute_x */
13970 /* Step 1: Try to find the glyph whose character position
13971 corresponds to point. If that's not possible, find 2 glyphs
13972 whose character positions are the closest to point, one before
13973 point, the other after it. */
13974 if (!row->reversed_p)
13975 while (/* not marched to end of glyph row */
13976 glyph < end
13977 /* glyph was not inserted by redisplay for internal purposes */
13978 && !INTEGERP (glyph->object))
13980 if (BUFFERP (glyph->object))
13982 ptrdiff_t dpos = glyph->charpos - pt_old;
13984 if (glyph->charpos > bpos_max)
13985 bpos_max = glyph->charpos;
13986 if (glyph->charpos < bpos_min)
13987 bpos_min = glyph->charpos;
13988 if (!glyph->avoid_cursor_p)
13990 /* If we hit point, we've found the glyph on which to
13991 display the cursor. */
13992 if (dpos == 0)
13994 match_with_avoid_cursor = 0;
13995 break;
13997 /* See if we've found a better approximation to
13998 POS_BEFORE or to POS_AFTER. */
13999 if (0 > dpos && dpos > pos_before - pt_old)
14001 pos_before = glyph->charpos;
14002 glyph_before = glyph;
14004 else if (0 < dpos && dpos < pos_after - pt_old)
14006 pos_after = glyph->charpos;
14007 glyph_after = glyph;
14010 else if (dpos == 0)
14011 match_with_avoid_cursor = 1;
14013 else if (STRINGP (glyph->object))
14015 Lisp_Object chprop;
14016 ptrdiff_t glyph_pos = glyph->charpos;
14018 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14019 glyph->object);
14020 if (!NILP (chprop))
14022 /* If the string came from a `display' text property,
14023 look up the buffer position of that property and
14024 use that position to update bpos_max, as if we
14025 actually saw such a position in one of the row's
14026 glyphs. This helps with supporting integer values
14027 of `cursor' property on the display string in
14028 situations where most or all of the row's buffer
14029 text is completely covered by display properties,
14030 so that no glyph with valid buffer positions is
14031 ever seen in the row. */
14032 ptrdiff_t prop_pos =
14033 string_buffer_position_lim (glyph->object, pos_before,
14034 pos_after, 0);
14036 if (prop_pos >= pos_before)
14037 bpos_max = prop_pos - 1;
14039 if (INTEGERP (chprop))
14041 bpos_covered = bpos_max + XINT (chprop);
14042 /* If the `cursor' property covers buffer positions up
14043 to and including point, we should display cursor on
14044 this glyph. Note that, if a `cursor' property on one
14045 of the string's characters has an integer value, we
14046 will break out of the loop below _before_ we get to
14047 the position match above. IOW, integer values of
14048 the `cursor' property override the "exact match for
14049 point" strategy of positioning the cursor. */
14050 /* Implementation note: bpos_max == pt_old when, e.g.,
14051 we are in an empty line, where bpos_max is set to
14052 MATRIX_ROW_START_CHARPOS, see above. */
14053 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14055 cursor = glyph;
14056 break;
14060 string_seen = 1;
14062 x += glyph->pixel_width;
14063 ++glyph;
14065 else if (glyph > end) /* row is reversed */
14066 while (!INTEGERP (glyph->object))
14068 if (BUFFERP (glyph->object))
14070 ptrdiff_t dpos = glyph->charpos - pt_old;
14072 if (glyph->charpos > bpos_max)
14073 bpos_max = glyph->charpos;
14074 if (glyph->charpos < bpos_min)
14075 bpos_min = glyph->charpos;
14076 if (!glyph->avoid_cursor_p)
14078 if (dpos == 0)
14080 match_with_avoid_cursor = 0;
14081 break;
14083 if (0 > dpos && dpos > pos_before - pt_old)
14085 pos_before = glyph->charpos;
14086 glyph_before = glyph;
14088 else if (0 < dpos && dpos < pos_after - pt_old)
14090 pos_after = glyph->charpos;
14091 glyph_after = glyph;
14094 else if (dpos == 0)
14095 match_with_avoid_cursor = 1;
14097 else if (STRINGP (glyph->object))
14099 Lisp_Object chprop;
14100 ptrdiff_t glyph_pos = glyph->charpos;
14102 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14103 glyph->object);
14104 if (!NILP (chprop))
14106 ptrdiff_t prop_pos =
14107 string_buffer_position_lim (glyph->object, pos_before,
14108 pos_after, 0);
14110 if (prop_pos >= pos_before)
14111 bpos_max = prop_pos - 1;
14113 if (INTEGERP (chprop))
14115 bpos_covered = bpos_max + XINT (chprop);
14116 /* If the `cursor' property covers buffer positions up
14117 to and including point, we should display cursor on
14118 this glyph. */
14119 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14121 cursor = glyph;
14122 break;
14125 string_seen = 1;
14127 --glyph;
14128 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14130 x--; /* can't use any pixel_width */
14131 break;
14133 x -= glyph->pixel_width;
14136 /* Step 2: If we didn't find an exact match for point, we need to
14137 look for a proper place to put the cursor among glyphs between
14138 GLYPH_BEFORE and GLYPH_AFTER. */
14139 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14140 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14141 && bpos_covered < pt_old)
14143 /* An empty line has a single glyph whose OBJECT is zero and
14144 whose CHARPOS is the position of a newline on that line.
14145 Note that on a TTY, there are more glyphs after that, which
14146 were produced by extend_face_to_end_of_line, but their
14147 CHARPOS is zero or negative. */
14148 int empty_line_p =
14149 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14150 && INTEGERP (glyph->object) && glyph->charpos > 0;
14152 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14154 ptrdiff_t ellipsis_pos;
14156 /* Scan back over the ellipsis glyphs. */
14157 if (!row->reversed_p)
14159 ellipsis_pos = (glyph - 1)->charpos;
14160 while (glyph > row->glyphs[TEXT_AREA]
14161 && (glyph - 1)->charpos == ellipsis_pos)
14162 glyph--, x -= glyph->pixel_width;
14163 /* That loop always goes one position too far, including
14164 the glyph before the ellipsis. So scan forward over
14165 that one. */
14166 x += glyph->pixel_width;
14167 glyph++;
14169 else /* row is reversed */
14171 ellipsis_pos = (glyph + 1)->charpos;
14172 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14173 && (glyph + 1)->charpos == ellipsis_pos)
14174 glyph++, x += glyph->pixel_width;
14175 x -= glyph->pixel_width;
14176 glyph--;
14179 else if (match_with_avoid_cursor)
14181 cursor = glyph_after;
14182 x = -1;
14184 else if (string_seen)
14186 int incr = row->reversed_p ? -1 : +1;
14188 /* Need to find the glyph that came out of a string which is
14189 present at point. That glyph is somewhere between
14190 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14191 positioned between POS_BEFORE and POS_AFTER in the
14192 buffer. */
14193 struct glyph *start, *stop;
14194 ptrdiff_t pos = pos_before;
14196 x = -1;
14198 /* If the row ends in a newline from a display string,
14199 reordering could have moved the glyphs belonging to the
14200 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14201 in this case we extend the search to the last glyph in
14202 the row that was not inserted by redisplay. */
14203 if (row->ends_in_newline_from_string_p)
14205 glyph_after = end;
14206 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14209 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14210 correspond to POS_BEFORE and POS_AFTER, respectively. We
14211 need START and STOP in the order that corresponds to the
14212 row's direction as given by its reversed_p flag. If the
14213 directionality of characters between POS_BEFORE and
14214 POS_AFTER is the opposite of the row's base direction,
14215 these characters will have been reordered for display,
14216 and we need to reverse START and STOP. */
14217 if (!row->reversed_p)
14219 start = min (glyph_before, glyph_after);
14220 stop = max (glyph_before, glyph_after);
14222 else
14224 start = max (glyph_before, glyph_after);
14225 stop = min (glyph_before, glyph_after);
14227 for (glyph = start + incr;
14228 row->reversed_p ? glyph > stop : glyph < stop; )
14231 /* Any glyphs that come from the buffer are here because
14232 of bidi reordering. Skip them, and only pay
14233 attention to glyphs that came from some string. */
14234 if (STRINGP (glyph->object))
14236 Lisp_Object str;
14237 ptrdiff_t tem;
14238 /* If the display property covers the newline, we
14239 need to search for it one position farther. */
14240 ptrdiff_t lim = pos_after
14241 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14243 string_from_text_prop = 0;
14244 str = glyph->object;
14245 tem = string_buffer_position_lim (str, pos, lim, 0);
14246 if (tem == 0 /* from overlay */
14247 || pos <= tem)
14249 /* If the string from which this glyph came is
14250 found in the buffer at point, or at position
14251 that is closer to point than pos_after, then
14252 we've found the glyph we've been looking for.
14253 If it comes from an overlay (tem == 0), and
14254 it has the `cursor' property on one of its
14255 glyphs, record that glyph as a candidate for
14256 displaying the cursor. (As in the
14257 unidirectional version, we will display the
14258 cursor on the last candidate we find.) */
14259 if (tem == 0
14260 || tem == pt_old
14261 || (tem - pt_old > 0 && tem < pos_after))
14263 /* The glyphs from this string could have
14264 been reordered. Find the one with the
14265 smallest string position. Or there could
14266 be a character in the string with the
14267 `cursor' property, which means display
14268 cursor on that character's glyph. */
14269 ptrdiff_t strpos = glyph->charpos;
14271 if (tem)
14273 cursor = glyph;
14274 string_from_text_prop = 1;
14276 for ( ;
14277 (row->reversed_p ? glyph > stop : glyph < stop)
14278 && EQ (glyph->object, str);
14279 glyph += incr)
14281 Lisp_Object cprop;
14282 ptrdiff_t gpos = glyph->charpos;
14284 cprop = Fget_char_property (make_number (gpos),
14285 Qcursor,
14286 glyph->object);
14287 if (!NILP (cprop))
14289 cursor = glyph;
14290 break;
14292 if (tem && glyph->charpos < strpos)
14294 strpos = glyph->charpos;
14295 cursor = glyph;
14299 if (tem == pt_old
14300 || (tem - pt_old > 0 && tem < pos_after))
14301 goto compute_x;
14303 if (tem)
14304 pos = tem + 1; /* don't find previous instances */
14306 /* This string is not what we want; skip all of the
14307 glyphs that came from it. */
14308 while ((row->reversed_p ? glyph > stop : glyph < stop)
14309 && EQ (glyph->object, str))
14310 glyph += incr;
14312 else
14313 glyph += incr;
14316 /* If we reached the end of the line, and END was from a string,
14317 the cursor is not on this line. */
14318 if (cursor == NULL
14319 && (row->reversed_p ? glyph <= end : glyph >= end)
14320 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14321 && STRINGP (end->object)
14322 && row->continued_p)
14323 return 0;
14325 /* A truncated row may not include PT among its character positions.
14326 Setting the cursor inside the scroll margin will trigger
14327 recalculation of hscroll in hscroll_window_tree. But if a
14328 display string covers point, defer to the string-handling
14329 code below to figure this out. */
14330 else if (row->truncated_on_left_p && pt_old < bpos_min)
14332 cursor = glyph_before;
14333 x = -1;
14335 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14336 /* Zero-width characters produce no glyphs. */
14337 || (!empty_line_p
14338 && (row->reversed_p
14339 ? glyph_after > glyphs_end
14340 : glyph_after < glyphs_end)))
14342 cursor = glyph_after;
14343 x = -1;
14347 compute_x:
14348 if (cursor != NULL)
14349 glyph = cursor;
14350 else if (glyph == glyphs_end
14351 && pos_before == pos_after
14352 && STRINGP ((row->reversed_p
14353 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14354 : row->glyphs[TEXT_AREA])->object))
14356 /* If all the glyphs of this row came from strings, put the
14357 cursor on the first glyph of the row. This avoids having the
14358 cursor outside of the text area in this very rare and hard
14359 use case. */
14360 glyph =
14361 row->reversed_p
14362 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14363 : row->glyphs[TEXT_AREA];
14365 if (x < 0)
14367 struct glyph *g;
14369 /* Need to compute x that corresponds to GLYPH. */
14370 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14372 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14373 abort ();
14374 x += g->pixel_width;
14378 /* ROW could be part of a continued line, which, under bidi
14379 reordering, might have other rows whose start and end charpos
14380 occlude point. Only set w->cursor if we found a better
14381 approximation to the cursor position than we have from previously
14382 examined candidate rows belonging to the same continued line. */
14383 if (/* we already have a candidate row */
14384 w->cursor.vpos >= 0
14385 /* that candidate is not the row we are processing */
14386 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14387 /* Make sure cursor.vpos specifies a row whose start and end
14388 charpos occlude point, and it is valid candidate for being a
14389 cursor-row. This is because some callers of this function
14390 leave cursor.vpos at the row where the cursor was displayed
14391 during the last redisplay cycle. */
14392 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14393 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14394 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14396 struct glyph *g1 =
14397 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14399 /* Don't consider glyphs that are outside TEXT_AREA. */
14400 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14401 return 0;
14402 /* Keep the candidate whose buffer position is the closest to
14403 point or has the `cursor' property. */
14404 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14405 w->cursor.hpos >= 0
14406 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14407 && ((BUFFERP (g1->object)
14408 && (g1->charpos == pt_old /* an exact match always wins */
14409 || (BUFFERP (glyph->object)
14410 && eabs (g1->charpos - pt_old)
14411 < eabs (glyph->charpos - pt_old))))
14412 /* previous candidate is a glyph from a string that has
14413 a non-nil `cursor' property */
14414 || (STRINGP (g1->object)
14415 && (!NILP (Fget_char_property (make_number (g1->charpos),
14416 Qcursor, g1->object))
14417 /* previous candidate is from the same display
14418 string as this one, and the display string
14419 came from a text property */
14420 || (EQ (g1->object, glyph->object)
14421 && string_from_text_prop)
14422 /* this candidate is from newline and its
14423 position is not an exact match */
14424 || (INTEGERP (glyph->object)
14425 && glyph->charpos != pt_old)))))
14426 return 0;
14427 /* If this candidate gives an exact match, use that. */
14428 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14429 /* If this candidate is a glyph created for the
14430 terminating newline of a line, and point is on that
14431 newline, it wins because it's an exact match. */
14432 || (!row->continued_p
14433 && INTEGERP (glyph->object)
14434 && glyph->charpos == 0
14435 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14436 /* Otherwise, keep the candidate that comes from a row
14437 spanning less buffer positions. This may win when one or
14438 both candidate positions are on glyphs that came from
14439 display strings, for which we cannot compare buffer
14440 positions. */
14441 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14442 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14443 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14444 return 0;
14446 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14447 w->cursor.x = x;
14448 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14449 w->cursor.y = row->y + dy;
14451 if (w == XWINDOW (selected_window))
14453 if (!row->continued_p
14454 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14455 && row->x == 0)
14457 this_line_buffer = XBUFFER (w->buffer);
14459 CHARPOS (this_line_start_pos)
14460 = MATRIX_ROW_START_CHARPOS (row) + delta;
14461 BYTEPOS (this_line_start_pos)
14462 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14464 CHARPOS (this_line_end_pos)
14465 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14466 BYTEPOS (this_line_end_pos)
14467 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14469 this_line_y = w->cursor.y;
14470 this_line_pixel_height = row->height;
14471 this_line_vpos = w->cursor.vpos;
14472 this_line_start_x = row->x;
14474 else
14475 CHARPOS (this_line_start_pos) = 0;
14478 return 1;
14482 /* Run window scroll functions, if any, for WINDOW with new window
14483 start STARTP. Sets the window start of WINDOW to that position.
14485 We assume that the window's buffer is really current. */
14487 static inline struct text_pos
14488 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14490 struct window *w = XWINDOW (window);
14491 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14493 if (current_buffer != XBUFFER (w->buffer))
14494 abort ();
14496 if (!NILP (Vwindow_scroll_functions))
14498 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14499 make_number (CHARPOS (startp)));
14500 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14501 /* In case the hook functions switch buffers. */
14502 if (current_buffer != XBUFFER (w->buffer))
14503 set_buffer_internal_1 (XBUFFER (w->buffer));
14506 return startp;
14510 /* Make sure the line containing the cursor is fully visible.
14511 A value of 1 means there is nothing to be done.
14512 (Either the line is fully visible, or it cannot be made so,
14513 or we cannot tell.)
14515 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14516 is higher than window.
14518 A value of 0 means the caller should do scrolling
14519 as if point had gone off the screen. */
14521 static int
14522 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14524 struct glyph_matrix *matrix;
14525 struct glyph_row *row;
14526 int window_height;
14528 if (!make_cursor_line_fully_visible_p)
14529 return 1;
14531 /* It's not always possible to find the cursor, e.g, when a window
14532 is full of overlay strings. Don't do anything in that case. */
14533 if (w->cursor.vpos < 0)
14534 return 1;
14536 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14537 row = MATRIX_ROW (matrix, w->cursor.vpos);
14539 /* If the cursor row is not partially visible, there's nothing to do. */
14540 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14541 return 1;
14543 /* If the row the cursor is in is taller than the window's height,
14544 it's not clear what to do, so do nothing. */
14545 window_height = window_box_height (w);
14546 if (row->height >= window_height)
14548 if (!force_p || MINI_WINDOW_P (w)
14549 || w->vscroll || w->cursor.vpos == 0)
14550 return 1;
14552 return 0;
14556 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14557 non-zero means only WINDOW is redisplayed in redisplay_internal.
14558 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14559 in redisplay_window to bring a partially visible line into view in
14560 the case that only the cursor has moved.
14562 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14563 last screen line's vertical height extends past the end of the screen.
14565 Value is
14567 1 if scrolling succeeded
14569 0 if scrolling didn't find point.
14571 -1 if new fonts have been loaded so that we must interrupt
14572 redisplay, adjust glyph matrices, and try again. */
14574 enum
14576 SCROLLING_SUCCESS,
14577 SCROLLING_FAILED,
14578 SCROLLING_NEED_LARGER_MATRICES
14581 /* If scroll-conservatively is more than this, never recenter.
14583 If you change this, don't forget to update the doc string of
14584 `scroll-conservatively' and the Emacs manual. */
14585 #define SCROLL_LIMIT 100
14587 static int
14588 try_scrolling (Lisp_Object window, int just_this_one_p,
14589 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14590 int temp_scroll_step, int last_line_misfit)
14592 struct window *w = XWINDOW (window);
14593 struct frame *f = XFRAME (w->frame);
14594 struct text_pos pos, startp;
14595 struct it it;
14596 int this_scroll_margin, scroll_max, rc, height;
14597 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14598 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14599 Lisp_Object aggressive;
14600 /* We will never try scrolling more than this number of lines. */
14601 int scroll_limit = SCROLL_LIMIT;
14603 #ifdef GLYPH_DEBUG
14604 debug_method_add (w, "try_scrolling");
14605 #endif
14607 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14609 /* Compute scroll margin height in pixels. We scroll when point is
14610 within this distance from the top or bottom of the window. */
14611 if (scroll_margin > 0)
14612 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14613 * FRAME_LINE_HEIGHT (f);
14614 else
14615 this_scroll_margin = 0;
14617 /* Force arg_scroll_conservatively to have a reasonable value, to
14618 avoid scrolling too far away with slow move_it_* functions. Note
14619 that the user can supply scroll-conservatively equal to
14620 `most-positive-fixnum', which can be larger than INT_MAX. */
14621 if (arg_scroll_conservatively > scroll_limit)
14623 arg_scroll_conservatively = scroll_limit + 1;
14624 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14626 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14627 /* Compute how much we should try to scroll maximally to bring
14628 point into view. */
14629 scroll_max = (max (scroll_step,
14630 max (arg_scroll_conservatively, temp_scroll_step))
14631 * FRAME_LINE_HEIGHT (f));
14632 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14633 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14634 /* We're trying to scroll because of aggressive scrolling but no
14635 scroll_step is set. Choose an arbitrary one. */
14636 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14637 else
14638 scroll_max = 0;
14640 too_near_end:
14642 /* Decide whether to scroll down. */
14643 if (PT > CHARPOS (startp))
14645 int scroll_margin_y;
14647 /* Compute the pixel ypos of the scroll margin, then move IT to
14648 either that ypos or PT, whichever comes first. */
14649 start_display (&it, w, startp);
14650 scroll_margin_y = it.last_visible_y - this_scroll_margin
14651 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14652 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14653 (MOVE_TO_POS | MOVE_TO_Y));
14655 if (PT > CHARPOS (it.current.pos))
14657 int y0 = line_bottom_y (&it);
14658 /* Compute how many pixels below window bottom to stop searching
14659 for PT. This avoids costly search for PT that is far away if
14660 the user limited scrolling by a small number of lines, but
14661 always finds PT if scroll_conservatively is set to a large
14662 number, such as most-positive-fixnum. */
14663 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14664 int y_to_move = it.last_visible_y + slack;
14666 /* Compute the distance from the scroll margin to PT or to
14667 the scroll limit, whichever comes first. This should
14668 include the height of the cursor line, to make that line
14669 fully visible. */
14670 move_it_to (&it, PT, -1, y_to_move,
14671 -1, MOVE_TO_POS | MOVE_TO_Y);
14672 dy = line_bottom_y (&it) - y0;
14674 if (dy > scroll_max)
14675 return SCROLLING_FAILED;
14677 if (dy > 0)
14678 scroll_down_p = 1;
14682 if (scroll_down_p)
14684 /* Point is in or below the bottom scroll margin, so move the
14685 window start down. If scrolling conservatively, move it just
14686 enough down to make point visible. If scroll_step is set,
14687 move it down by scroll_step. */
14688 if (arg_scroll_conservatively)
14689 amount_to_scroll
14690 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14691 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14692 else if (scroll_step || temp_scroll_step)
14693 amount_to_scroll = scroll_max;
14694 else
14696 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14697 height = WINDOW_BOX_TEXT_HEIGHT (w);
14698 if (NUMBERP (aggressive))
14700 double float_amount = XFLOATINT (aggressive) * height;
14701 amount_to_scroll = float_amount;
14702 if (amount_to_scroll == 0 && float_amount > 0)
14703 amount_to_scroll = 1;
14704 /* Don't let point enter the scroll margin near top of
14705 the window. */
14706 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14707 amount_to_scroll = height - 2*this_scroll_margin + dy;
14711 if (amount_to_scroll <= 0)
14712 return SCROLLING_FAILED;
14714 start_display (&it, w, startp);
14715 if (arg_scroll_conservatively <= scroll_limit)
14716 move_it_vertically (&it, amount_to_scroll);
14717 else
14719 /* Extra precision for users who set scroll-conservatively
14720 to a large number: make sure the amount we scroll
14721 the window start is never less than amount_to_scroll,
14722 which was computed as distance from window bottom to
14723 point. This matters when lines at window top and lines
14724 below window bottom have different height. */
14725 struct it it1;
14726 void *it1data = NULL;
14727 /* We use a temporary it1 because line_bottom_y can modify
14728 its argument, if it moves one line down; see there. */
14729 int start_y;
14731 SAVE_IT (it1, it, it1data);
14732 start_y = line_bottom_y (&it1);
14733 do {
14734 RESTORE_IT (&it, &it, it1data);
14735 move_it_by_lines (&it, 1);
14736 SAVE_IT (it1, it, it1data);
14737 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14740 /* If STARTP is unchanged, move it down another screen line. */
14741 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14742 move_it_by_lines (&it, 1);
14743 startp = it.current.pos;
14745 else
14747 struct text_pos scroll_margin_pos = startp;
14749 /* See if point is inside the scroll margin at the top of the
14750 window. */
14751 if (this_scroll_margin)
14753 start_display (&it, w, startp);
14754 move_it_vertically (&it, this_scroll_margin);
14755 scroll_margin_pos = it.current.pos;
14758 if (PT < CHARPOS (scroll_margin_pos))
14760 /* Point is in the scroll margin at the top of the window or
14761 above what is displayed in the window. */
14762 int y0, y_to_move;
14764 /* Compute the vertical distance from PT to the scroll
14765 margin position. Move as far as scroll_max allows, or
14766 one screenful, or 10 screen lines, whichever is largest.
14767 Give up if distance is greater than scroll_max. */
14768 SET_TEXT_POS (pos, PT, PT_BYTE);
14769 start_display (&it, w, pos);
14770 y0 = it.current_y;
14771 y_to_move = max (it.last_visible_y,
14772 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14773 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14774 y_to_move, -1,
14775 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14776 dy = it.current_y - y0;
14777 if (dy > scroll_max)
14778 return SCROLLING_FAILED;
14780 /* Compute new window start. */
14781 start_display (&it, w, startp);
14783 if (arg_scroll_conservatively)
14784 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14785 max (scroll_step, temp_scroll_step));
14786 else if (scroll_step || temp_scroll_step)
14787 amount_to_scroll = scroll_max;
14788 else
14790 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14791 height = WINDOW_BOX_TEXT_HEIGHT (w);
14792 if (NUMBERP (aggressive))
14794 double float_amount = XFLOATINT (aggressive) * height;
14795 amount_to_scroll = float_amount;
14796 if (amount_to_scroll == 0 && float_amount > 0)
14797 amount_to_scroll = 1;
14798 amount_to_scroll -=
14799 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14800 /* Don't let point enter the scroll margin near
14801 bottom of the window. */
14802 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14803 amount_to_scroll = height - 2*this_scroll_margin + dy;
14807 if (amount_to_scroll <= 0)
14808 return SCROLLING_FAILED;
14810 move_it_vertically_backward (&it, amount_to_scroll);
14811 startp = it.current.pos;
14815 /* Run window scroll functions. */
14816 startp = run_window_scroll_functions (window, startp);
14818 /* Display the window. Give up if new fonts are loaded, or if point
14819 doesn't appear. */
14820 if (!try_window (window, startp, 0))
14821 rc = SCROLLING_NEED_LARGER_MATRICES;
14822 else if (w->cursor.vpos < 0)
14824 clear_glyph_matrix (w->desired_matrix);
14825 rc = SCROLLING_FAILED;
14827 else
14829 /* Maybe forget recorded base line for line number display. */
14830 if (!just_this_one_p
14831 || current_buffer->clip_changed
14832 || BEG_UNCHANGED < CHARPOS (startp))
14833 w->base_line_number = Qnil;
14835 /* If cursor ends up on a partially visible line,
14836 treat that as being off the bottom of the screen. */
14837 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14838 /* It's possible that the cursor is on the first line of the
14839 buffer, which is partially obscured due to a vscroll
14840 (Bug#7537). In that case, avoid looping forever . */
14841 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14843 clear_glyph_matrix (w->desired_matrix);
14844 ++extra_scroll_margin_lines;
14845 goto too_near_end;
14847 rc = SCROLLING_SUCCESS;
14850 return rc;
14854 /* Compute a suitable window start for window W if display of W starts
14855 on a continuation line. Value is non-zero if a new window start
14856 was computed.
14858 The new window start will be computed, based on W's width, starting
14859 from the start of the continued line. It is the start of the
14860 screen line with the minimum distance from the old start W->start. */
14862 static int
14863 compute_window_start_on_continuation_line (struct window *w)
14865 struct text_pos pos, start_pos;
14866 int window_start_changed_p = 0;
14868 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14870 /* If window start is on a continuation line... Window start may be
14871 < BEGV in case there's invisible text at the start of the
14872 buffer (M-x rmail, for example). */
14873 if (CHARPOS (start_pos) > BEGV
14874 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14876 struct it it;
14877 struct glyph_row *row;
14879 /* Handle the case that the window start is out of range. */
14880 if (CHARPOS (start_pos) < BEGV)
14881 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14882 else if (CHARPOS (start_pos) > ZV)
14883 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14885 /* Find the start of the continued line. This should be fast
14886 because scan_buffer is fast (newline cache). */
14887 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14888 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14889 row, DEFAULT_FACE_ID);
14890 reseat_at_previous_visible_line_start (&it);
14892 /* If the line start is "too far" away from the window start,
14893 say it takes too much time to compute a new window start. */
14894 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14895 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14897 int min_distance, distance;
14899 /* Move forward by display lines to find the new window
14900 start. If window width was enlarged, the new start can
14901 be expected to be > the old start. If window width was
14902 decreased, the new window start will be < the old start.
14903 So, we're looking for the display line start with the
14904 minimum distance from the old window start. */
14905 pos = it.current.pos;
14906 min_distance = INFINITY;
14907 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14908 distance < min_distance)
14910 min_distance = distance;
14911 pos = it.current.pos;
14912 move_it_by_lines (&it, 1);
14915 /* Set the window start there. */
14916 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14917 window_start_changed_p = 1;
14921 return window_start_changed_p;
14925 /* Try cursor movement in case text has not changed in window WINDOW,
14926 with window start STARTP. Value is
14928 CURSOR_MOVEMENT_SUCCESS if successful
14930 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14932 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14933 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14934 we want to scroll as if scroll-step were set to 1. See the code.
14936 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14937 which case we have to abort this redisplay, and adjust matrices
14938 first. */
14940 enum
14942 CURSOR_MOVEMENT_SUCCESS,
14943 CURSOR_MOVEMENT_CANNOT_BE_USED,
14944 CURSOR_MOVEMENT_MUST_SCROLL,
14945 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14948 static int
14949 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14951 struct window *w = XWINDOW (window);
14952 struct frame *f = XFRAME (w->frame);
14953 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14955 #ifdef GLYPH_DEBUG
14956 if (inhibit_try_cursor_movement)
14957 return rc;
14958 #endif
14960 /* Previously, there was a check for Lisp integer in the
14961 if-statement below. Now, this field is converted to
14962 ptrdiff_t, thus zero means invalid position in a buffer. */
14963 eassert (w->last_point > 0);
14965 /* Handle case where text has not changed, only point, and it has
14966 not moved off the frame. */
14967 if (/* Point may be in this window. */
14968 PT >= CHARPOS (startp)
14969 /* Selective display hasn't changed. */
14970 && !current_buffer->clip_changed
14971 /* Function force-mode-line-update is used to force a thorough
14972 redisplay. It sets either windows_or_buffers_changed or
14973 update_mode_lines. So don't take a shortcut here for these
14974 cases. */
14975 && !update_mode_lines
14976 && !windows_or_buffers_changed
14977 && !cursor_type_changed
14978 /* Can't use this case if highlighting a region. When a
14979 region exists, cursor movement has to do more than just
14980 set the cursor. */
14981 && !(!NILP (Vtransient_mark_mode)
14982 && !NILP (BVAR (current_buffer, mark_active)))
14983 && NILP (w->region_showing)
14984 && NILP (Vshow_trailing_whitespace)
14985 /* This code is not used for mini-buffer for the sake of the case
14986 of redisplaying to replace an echo area message; since in
14987 that case the mini-buffer contents per se are usually
14988 unchanged. This code is of no real use in the mini-buffer
14989 since the handling of this_line_start_pos, etc., in redisplay
14990 handles the same cases. */
14991 && !EQ (window, minibuf_window)
14992 /* When splitting windows or for new windows, it happens that
14993 redisplay is called with a nil window_end_vpos or one being
14994 larger than the window. This should really be fixed in
14995 window.c. I don't have this on my list, now, so we do
14996 approximately the same as the old redisplay code. --gerd. */
14997 && INTEGERP (w->window_end_vpos)
14998 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14999 && (FRAME_WINDOW_P (f)
15000 || !overlay_arrow_in_current_buffer_p ()))
15002 int this_scroll_margin, top_scroll_margin;
15003 struct glyph_row *row = NULL;
15005 #ifdef GLYPH_DEBUG
15006 debug_method_add (w, "cursor movement");
15007 #endif
15009 /* Scroll if point within this distance from the top or bottom
15010 of the window. This is a pixel value. */
15011 if (scroll_margin > 0)
15013 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15014 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15016 else
15017 this_scroll_margin = 0;
15019 top_scroll_margin = this_scroll_margin;
15020 if (WINDOW_WANTS_HEADER_LINE_P (w))
15021 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15023 /* Start with the row the cursor was displayed during the last
15024 not paused redisplay. Give up if that row is not valid. */
15025 if (w->last_cursor.vpos < 0
15026 || w->last_cursor.vpos >= w->current_matrix->nrows)
15027 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15028 else
15030 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15031 if (row->mode_line_p)
15032 ++row;
15033 if (!row->enabled_p)
15034 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15037 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15039 int scroll_p = 0, must_scroll = 0;
15040 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15042 if (PT > w->last_point)
15044 /* Point has moved forward. */
15045 while (MATRIX_ROW_END_CHARPOS (row) < PT
15046 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15048 eassert (row->enabled_p);
15049 ++row;
15052 /* If the end position of a row equals the start
15053 position of the next row, and PT is at that position,
15054 we would rather display cursor in the next line. */
15055 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15056 && MATRIX_ROW_END_CHARPOS (row) == PT
15057 && row < w->current_matrix->rows
15058 + w->current_matrix->nrows - 1
15059 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15060 && !cursor_row_p (row))
15061 ++row;
15063 /* If within the scroll margin, scroll. Note that
15064 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15065 the next line would be drawn, and that
15066 this_scroll_margin can be zero. */
15067 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15068 || PT > MATRIX_ROW_END_CHARPOS (row)
15069 /* Line is completely visible last line in window
15070 and PT is to be set in the next line. */
15071 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15072 && PT == MATRIX_ROW_END_CHARPOS (row)
15073 && !row->ends_at_zv_p
15074 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15075 scroll_p = 1;
15077 else if (PT < w->last_point)
15079 /* Cursor has to be moved backward. Note that PT >=
15080 CHARPOS (startp) because of the outer if-statement. */
15081 while (!row->mode_line_p
15082 && (MATRIX_ROW_START_CHARPOS (row) > PT
15083 || (MATRIX_ROW_START_CHARPOS (row) == PT
15084 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15085 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15086 row > w->current_matrix->rows
15087 && (row-1)->ends_in_newline_from_string_p))))
15088 && (row->y > top_scroll_margin
15089 || CHARPOS (startp) == BEGV))
15091 eassert (row->enabled_p);
15092 --row;
15095 /* Consider the following case: Window starts at BEGV,
15096 there is invisible, intangible text at BEGV, so that
15097 display starts at some point START > BEGV. It can
15098 happen that we are called with PT somewhere between
15099 BEGV and START. Try to handle that case. */
15100 if (row < w->current_matrix->rows
15101 || row->mode_line_p)
15103 row = w->current_matrix->rows;
15104 if (row->mode_line_p)
15105 ++row;
15108 /* Due to newlines in overlay strings, we may have to
15109 skip forward over overlay strings. */
15110 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15111 && MATRIX_ROW_END_CHARPOS (row) == PT
15112 && !cursor_row_p (row))
15113 ++row;
15115 /* If within the scroll margin, scroll. */
15116 if (row->y < top_scroll_margin
15117 && CHARPOS (startp) != BEGV)
15118 scroll_p = 1;
15120 else
15122 /* Cursor did not move. So don't scroll even if cursor line
15123 is partially visible, as it was so before. */
15124 rc = CURSOR_MOVEMENT_SUCCESS;
15127 if (PT < MATRIX_ROW_START_CHARPOS (row)
15128 || PT > MATRIX_ROW_END_CHARPOS (row))
15130 /* if PT is not in the glyph row, give up. */
15131 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15132 must_scroll = 1;
15134 else if (rc != CURSOR_MOVEMENT_SUCCESS
15135 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15137 struct glyph_row *row1;
15139 /* If rows are bidi-reordered and point moved, back up
15140 until we find a row that does not belong to a
15141 continuation line. This is because we must consider
15142 all rows of a continued line as candidates for the
15143 new cursor positioning, since row start and end
15144 positions change non-linearly with vertical position
15145 in such rows. */
15146 /* FIXME: Revisit this when glyph ``spilling'' in
15147 continuation lines' rows is implemented for
15148 bidi-reordered rows. */
15149 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15150 MATRIX_ROW_CONTINUATION_LINE_P (row);
15151 --row)
15153 /* If we hit the beginning of the displayed portion
15154 without finding the first row of a continued
15155 line, give up. */
15156 if (row <= row1)
15158 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15159 break;
15161 eassert (row->enabled_p);
15164 if (must_scroll)
15166 else if (rc != CURSOR_MOVEMENT_SUCCESS
15167 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15168 /* Make sure this isn't a header line by any chance, since
15169 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15170 && !row->mode_line_p
15171 && make_cursor_line_fully_visible_p)
15173 if (PT == MATRIX_ROW_END_CHARPOS (row)
15174 && !row->ends_at_zv_p
15175 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15176 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15177 else if (row->height > window_box_height (w))
15179 /* If we end up in a partially visible line, let's
15180 make it fully visible, except when it's taller
15181 than the window, in which case we can't do much
15182 about it. */
15183 *scroll_step = 1;
15184 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15186 else
15188 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15189 if (!cursor_row_fully_visible_p (w, 0, 1))
15190 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15191 else
15192 rc = CURSOR_MOVEMENT_SUCCESS;
15195 else if (scroll_p)
15196 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15197 else if (rc != CURSOR_MOVEMENT_SUCCESS
15198 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15200 /* With bidi-reordered rows, there could be more than
15201 one candidate row whose start and end positions
15202 occlude point. We need to let set_cursor_from_row
15203 find the best candidate. */
15204 /* FIXME: Revisit this when glyph ``spilling'' in
15205 continuation lines' rows is implemented for
15206 bidi-reordered rows. */
15207 int rv = 0;
15211 int at_zv_p = 0, exact_match_p = 0;
15213 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15214 && PT <= MATRIX_ROW_END_CHARPOS (row)
15215 && cursor_row_p (row))
15216 rv |= set_cursor_from_row (w, row, w->current_matrix,
15217 0, 0, 0, 0);
15218 /* As soon as we've found the exact match for point,
15219 or the first suitable row whose ends_at_zv_p flag
15220 is set, we are done. */
15221 at_zv_p =
15222 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15223 if (rv && !at_zv_p
15224 && w->cursor.hpos >= 0
15225 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15226 w->cursor.vpos))
15228 struct glyph_row *candidate =
15229 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15230 struct glyph *g =
15231 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15232 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15234 exact_match_p =
15235 (BUFFERP (g->object) && g->charpos == PT)
15236 || (INTEGERP (g->object)
15237 && (g->charpos == PT
15238 || (g->charpos == 0 && endpos - 1 == PT)));
15240 if (rv && (at_zv_p || exact_match_p))
15242 rc = CURSOR_MOVEMENT_SUCCESS;
15243 break;
15245 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15246 break;
15247 ++row;
15249 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15250 || row->continued_p)
15251 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15252 || (MATRIX_ROW_START_CHARPOS (row) == PT
15253 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15254 /* If we didn't find any candidate rows, or exited the
15255 loop before all the candidates were examined, signal
15256 to the caller that this method failed. */
15257 if (rc != CURSOR_MOVEMENT_SUCCESS
15258 && !(rv
15259 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15260 && !row->continued_p))
15261 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15262 else if (rv)
15263 rc = CURSOR_MOVEMENT_SUCCESS;
15265 else
15269 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15271 rc = CURSOR_MOVEMENT_SUCCESS;
15272 break;
15274 ++row;
15276 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15277 && MATRIX_ROW_START_CHARPOS (row) == PT
15278 && cursor_row_p (row));
15283 return rc;
15286 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15287 static
15288 #endif
15289 void
15290 set_vertical_scroll_bar (struct window *w)
15292 ptrdiff_t start, end, whole;
15294 /* Calculate the start and end positions for the current window.
15295 At some point, it would be nice to choose between scrollbars
15296 which reflect the whole buffer size, with special markers
15297 indicating narrowing, and scrollbars which reflect only the
15298 visible region.
15300 Note that mini-buffers sometimes aren't displaying any text. */
15301 if (!MINI_WINDOW_P (w)
15302 || (w == XWINDOW (minibuf_window)
15303 && NILP (echo_area_buffer[0])))
15305 struct buffer *buf = XBUFFER (w->buffer);
15306 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15307 start = marker_position (w->start) - BUF_BEGV (buf);
15308 /* I don't think this is guaranteed to be right. For the
15309 moment, we'll pretend it is. */
15310 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15312 if (end < start)
15313 end = start;
15314 if (whole < (end - start))
15315 whole = end - start;
15317 else
15318 start = end = whole = 0;
15320 /* Indicate what this scroll bar ought to be displaying now. */
15321 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15322 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15323 (w, end - start, whole, start);
15327 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15328 selected_window is redisplayed.
15330 We can return without actually redisplaying the window if
15331 fonts_changed_p is nonzero. In that case, redisplay_internal will
15332 retry. */
15334 static void
15335 redisplay_window (Lisp_Object window, int just_this_one_p)
15337 struct window *w = XWINDOW (window);
15338 struct frame *f = XFRAME (w->frame);
15339 struct buffer *buffer = XBUFFER (w->buffer);
15340 struct buffer *old = current_buffer;
15341 struct text_pos lpoint, opoint, startp;
15342 int update_mode_line;
15343 int tem;
15344 struct it it;
15345 /* Record it now because it's overwritten. */
15346 int current_matrix_up_to_date_p = 0;
15347 int used_current_matrix_p = 0;
15348 /* This is less strict than current_matrix_up_to_date_p.
15349 It indicates that the buffer contents and narrowing are unchanged. */
15350 int buffer_unchanged_p = 0;
15351 int temp_scroll_step = 0;
15352 ptrdiff_t count = SPECPDL_INDEX ();
15353 int rc;
15354 int centering_position = -1;
15355 int last_line_misfit = 0;
15356 ptrdiff_t beg_unchanged, end_unchanged;
15358 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15359 opoint = lpoint;
15361 /* W must be a leaf window here. */
15362 eassert (!NILP (w->buffer));
15363 #ifdef GLYPH_DEBUG
15364 *w->desired_matrix->method = 0;
15365 #endif
15367 restart:
15368 reconsider_clip_changes (w, buffer);
15370 /* Has the mode line to be updated? */
15371 update_mode_line = (w->update_mode_line
15372 || update_mode_lines
15373 || buffer->clip_changed
15374 || buffer->prevent_redisplay_optimizations_p);
15376 if (MINI_WINDOW_P (w))
15378 if (w == XWINDOW (echo_area_window)
15379 && !NILP (echo_area_buffer[0]))
15381 if (update_mode_line)
15382 /* We may have to update a tty frame's menu bar or a
15383 tool-bar. Example `M-x C-h C-h C-g'. */
15384 goto finish_menu_bars;
15385 else
15386 /* We've already displayed the echo area glyphs in this window. */
15387 goto finish_scroll_bars;
15389 else if ((w != XWINDOW (minibuf_window)
15390 || minibuf_level == 0)
15391 /* When buffer is nonempty, redisplay window normally. */
15392 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15393 /* Quail displays non-mini buffers in minibuffer window.
15394 In that case, redisplay the window normally. */
15395 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15397 /* W is a mini-buffer window, but it's not active, so clear
15398 it. */
15399 int yb = window_text_bottom_y (w);
15400 struct glyph_row *row;
15401 int y;
15403 for (y = 0, row = w->desired_matrix->rows;
15404 y < yb;
15405 y += row->height, ++row)
15406 blank_row (w, row, y);
15407 goto finish_scroll_bars;
15410 clear_glyph_matrix (w->desired_matrix);
15413 /* Otherwise set up data on this window; select its buffer and point
15414 value. */
15415 /* Really select the buffer, for the sake of buffer-local
15416 variables. */
15417 set_buffer_internal_1 (XBUFFER (w->buffer));
15419 current_matrix_up_to_date_p
15420 = (!NILP (w->window_end_valid)
15421 && !current_buffer->clip_changed
15422 && !current_buffer->prevent_redisplay_optimizations_p
15423 && w->last_modified >= MODIFF
15424 && w->last_overlay_modified >= OVERLAY_MODIFF);
15426 /* Run the window-bottom-change-functions
15427 if it is possible that the text on the screen has changed
15428 (either due to modification of the text, or any other reason). */
15429 if (!current_matrix_up_to_date_p
15430 && !NILP (Vwindow_text_change_functions))
15432 safe_run_hooks (Qwindow_text_change_functions);
15433 goto restart;
15436 beg_unchanged = BEG_UNCHANGED;
15437 end_unchanged = END_UNCHANGED;
15439 SET_TEXT_POS (opoint, PT, PT_BYTE);
15441 specbind (Qinhibit_point_motion_hooks, Qt);
15443 buffer_unchanged_p
15444 = (!NILP (w->window_end_valid)
15445 && !current_buffer->clip_changed
15446 && w->last_modified >= MODIFF
15447 && w->last_overlay_modified >= OVERLAY_MODIFF);
15449 /* When windows_or_buffers_changed is non-zero, we can't rely on
15450 the window end being valid, so set it to nil there. */
15451 if (windows_or_buffers_changed)
15453 /* If window starts on a continuation line, maybe adjust the
15454 window start in case the window's width changed. */
15455 if (XMARKER (w->start)->buffer == current_buffer)
15456 compute_window_start_on_continuation_line (w);
15458 w->window_end_valid = Qnil;
15461 /* Some sanity checks. */
15462 CHECK_WINDOW_END (w);
15463 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15464 abort ();
15465 if (BYTEPOS (opoint) < CHARPOS (opoint))
15466 abort ();
15468 /* If %c is in mode line, update it if needed. */
15469 if (!NILP (w->column_number_displayed)
15470 /* This alternative quickly identifies a common case
15471 where no change is needed. */
15472 && !(PT == w->last_point
15473 && w->last_modified >= MODIFF
15474 && w->last_overlay_modified >= OVERLAY_MODIFF)
15475 && (XFASTINT (w->column_number_displayed) != current_column ()))
15476 update_mode_line = 1;
15478 /* Count number of windows showing the selected buffer. An indirect
15479 buffer counts as its base buffer. */
15480 if (!just_this_one_p)
15482 struct buffer *current_base, *window_base;
15483 current_base = current_buffer;
15484 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15485 if (current_base->base_buffer)
15486 current_base = current_base->base_buffer;
15487 if (window_base->base_buffer)
15488 window_base = window_base->base_buffer;
15489 if (current_base == window_base)
15490 buffer_shared++;
15493 /* Point refers normally to the selected window. For any other
15494 window, set up appropriate value. */
15495 if (!EQ (window, selected_window))
15497 ptrdiff_t new_pt = XMARKER (w->pointm)->charpos;
15498 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15499 if (new_pt < BEGV)
15501 new_pt = BEGV;
15502 new_pt_byte = BEGV_BYTE;
15503 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15505 else if (new_pt > (ZV - 1))
15507 new_pt = ZV;
15508 new_pt_byte = ZV_BYTE;
15509 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15512 /* We don't use SET_PT so that the point-motion hooks don't run. */
15513 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15516 /* If any of the character widths specified in the display table
15517 have changed, invalidate the width run cache. It's true that
15518 this may be a bit late to catch such changes, but the rest of
15519 redisplay goes (non-fatally) haywire when the display table is
15520 changed, so why should we worry about doing any better? */
15521 if (current_buffer->width_run_cache)
15523 struct Lisp_Char_Table *disptab = buffer_display_table ();
15525 if (! disptab_matches_widthtab (disptab,
15526 XVECTOR (BVAR (current_buffer, width_table))))
15528 invalidate_region_cache (current_buffer,
15529 current_buffer->width_run_cache,
15530 BEG, Z);
15531 recompute_width_table (current_buffer, disptab);
15535 /* If window-start is screwed up, choose a new one. */
15536 if (XMARKER (w->start)->buffer != current_buffer)
15537 goto recenter;
15539 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15541 /* If someone specified a new starting point but did not insist,
15542 check whether it can be used. */
15543 if (w->optional_new_start
15544 && CHARPOS (startp) >= BEGV
15545 && CHARPOS (startp) <= ZV)
15547 w->optional_new_start = 0;
15548 start_display (&it, w, startp);
15549 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15550 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15551 if (IT_CHARPOS (it) == PT)
15552 w->force_start = 1;
15553 /* IT may overshoot PT if text at PT is invisible. */
15554 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15555 w->force_start = 1;
15558 force_start:
15560 /* Handle case where place to start displaying has been specified,
15561 unless the specified location is outside the accessible range. */
15562 if (w->force_start || w->frozen_window_start_p)
15564 /* We set this later on if we have to adjust point. */
15565 int new_vpos = -1;
15567 w->force_start = 0;
15568 w->vscroll = 0;
15569 w->window_end_valid = Qnil;
15571 /* Forget any recorded base line for line number display. */
15572 if (!buffer_unchanged_p)
15573 w->base_line_number = Qnil;
15575 /* Redisplay the mode line. Select the buffer properly for that.
15576 Also, run the hook window-scroll-functions
15577 because we have scrolled. */
15578 /* Note, we do this after clearing force_start because
15579 if there's an error, it is better to forget about force_start
15580 than to get into an infinite loop calling the hook functions
15581 and having them get more errors. */
15582 if (!update_mode_line
15583 || ! NILP (Vwindow_scroll_functions))
15585 update_mode_line = 1;
15586 w->update_mode_line = 1;
15587 startp = run_window_scroll_functions (window, startp);
15590 w->last_modified = 0;
15591 w->last_overlay_modified = 0;
15592 if (CHARPOS (startp) < BEGV)
15593 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15594 else if (CHARPOS (startp) > ZV)
15595 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15597 /* Redisplay, then check if cursor has been set during the
15598 redisplay. Give up if new fonts were loaded. */
15599 /* We used to issue a CHECK_MARGINS argument to try_window here,
15600 but this causes scrolling to fail when point begins inside
15601 the scroll margin (bug#148) -- cyd */
15602 if (!try_window (window, startp, 0))
15604 w->force_start = 1;
15605 clear_glyph_matrix (w->desired_matrix);
15606 goto need_larger_matrices;
15609 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15611 /* If point does not appear, try to move point so it does
15612 appear. The desired matrix has been built above, so we
15613 can use it here. */
15614 new_vpos = window_box_height (w) / 2;
15617 if (!cursor_row_fully_visible_p (w, 0, 0))
15619 /* Point does appear, but on a line partly visible at end of window.
15620 Move it back to a fully-visible line. */
15621 new_vpos = window_box_height (w);
15624 /* If we need to move point for either of the above reasons,
15625 now actually do it. */
15626 if (new_vpos >= 0)
15628 struct glyph_row *row;
15630 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15631 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15632 ++row;
15634 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15635 MATRIX_ROW_START_BYTEPOS (row));
15637 if (w != XWINDOW (selected_window))
15638 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15639 else if (current_buffer == old)
15640 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15642 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15644 /* If we are highlighting the region, then we just changed
15645 the region, so redisplay to show it. */
15646 if (!NILP (Vtransient_mark_mode)
15647 && !NILP (BVAR (current_buffer, mark_active)))
15649 clear_glyph_matrix (w->desired_matrix);
15650 if (!try_window (window, startp, 0))
15651 goto need_larger_matrices;
15655 #ifdef GLYPH_DEBUG
15656 debug_method_add (w, "forced window start");
15657 #endif
15658 goto done;
15661 /* Handle case where text has not changed, only point, and it has
15662 not moved off the frame, and we are not retrying after hscroll.
15663 (current_matrix_up_to_date_p is nonzero when retrying.) */
15664 if (current_matrix_up_to_date_p
15665 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15666 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15668 switch (rc)
15670 case CURSOR_MOVEMENT_SUCCESS:
15671 used_current_matrix_p = 1;
15672 goto done;
15674 case CURSOR_MOVEMENT_MUST_SCROLL:
15675 goto try_to_scroll;
15677 default:
15678 abort ();
15681 /* If current starting point was originally the beginning of a line
15682 but no longer is, find a new starting point. */
15683 else if (w->start_at_line_beg
15684 && !(CHARPOS (startp) <= BEGV
15685 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15687 #ifdef GLYPH_DEBUG
15688 debug_method_add (w, "recenter 1");
15689 #endif
15690 goto recenter;
15693 /* Try scrolling with try_window_id. Value is > 0 if update has
15694 been done, it is -1 if we know that the same window start will
15695 not work. It is 0 if unsuccessful for some other reason. */
15696 else if ((tem = try_window_id (w)) != 0)
15698 #ifdef GLYPH_DEBUG
15699 debug_method_add (w, "try_window_id %d", tem);
15700 #endif
15702 if (fonts_changed_p)
15703 goto need_larger_matrices;
15704 if (tem > 0)
15705 goto done;
15707 /* Otherwise try_window_id has returned -1 which means that we
15708 don't want the alternative below this comment to execute. */
15710 else if (CHARPOS (startp) >= BEGV
15711 && CHARPOS (startp) <= ZV
15712 && PT >= CHARPOS (startp)
15713 && (CHARPOS (startp) < ZV
15714 /* Avoid starting at end of buffer. */
15715 || CHARPOS (startp) == BEGV
15716 || (w->last_modified >= MODIFF
15717 && w->last_overlay_modified >= OVERLAY_MODIFF)))
15719 int d1, d2, d3, d4, d5, d6;
15721 /* If first window line is a continuation line, and window start
15722 is inside the modified region, but the first change is before
15723 current window start, we must select a new window start.
15725 However, if this is the result of a down-mouse event (e.g. by
15726 extending the mouse-drag-overlay), we don't want to select a
15727 new window start, since that would change the position under
15728 the mouse, resulting in an unwanted mouse-movement rather
15729 than a simple mouse-click. */
15730 if (!w->start_at_line_beg
15731 && NILP (do_mouse_tracking)
15732 && CHARPOS (startp) > BEGV
15733 && CHARPOS (startp) > BEG + beg_unchanged
15734 && CHARPOS (startp) <= Z - end_unchanged
15735 /* Even if w->start_at_line_beg is nil, a new window may
15736 start at a line_beg, since that's how set_buffer_window
15737 sets it. So, we need to check the return value of
15738 compute_window_start_on_continuation_line. (See also
15739 bug#197). */
15740 && XMARKER (w->start)->buffer == current_buffer
15741 && compute_window_start_on_continuation_line (w)
15742 /* It doesn't make sense to force the window start like we
15743 do at label force_start if it is already known that point
15744 will not be visible in the resulting window, because
15745 doing so will move point from its correct position
15746 instead of scrolling the window to bring point into view.
15747 See bug#9324. */
15748 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15750 w->force_start = 1;
15751 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15752 goto force_start;
15755 #ifdef GLYPH_DEBUG
15756 debug_method_add (w, "same window start");
15757 #endif
15759 /* Try to redisplay starting at same place as before.
15760 If point has not moved off frame, accept the results. */
15761 if (!current_matrix_up_to_date_p
15762 /* Don't use try_window_reusing_current_matrix in this case
15763 because a window scroll function can have changed the
15764 buffer. */
15765 || !NILP (Vwindow_scroll_functions)
15766 || MINI_WINDOW_P (w)
15767 || !(used_current_matrix_p
15768 = try_window_reusing_current_matrix (w)))
15770 IF_DEBUG (debug_method_add (w, "1"));
15771 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15772 /* -1 means we need to scroll.
15773 0 means we need new matrices, but fonts_changed_p
15774 is set in that case, so we will detect it below. */
15775 goto try_to_scroll;
15778 if (fonts_changed_p)
15779 goto need_larger_matrices;
15781 if (w->cursor.vpos >= 0)
15783 if (!just_this_one_p
15784 || current_buffer->clip_changed
15785 || BEG_UNCHANGED < CHARPOS (startp))
15786 /* Forget any recorded base line for line number display. */
15787 w->base_line_number = Qnil;
15789 if (!cursor_row_fully_visible_p (w, 1, 0))
15791 clear_glyph_matrix (w->desired_matrix);
15792 last_line_misfit = 1;
15794 /* Drop through and scroll. */
15795 else
15796 goto done;
15798 else
15799 clear_glyph_matrix (w->desired_matrix);
15802 try_to_scroll:
15804 w->last_modified = 0;
15805 w->last_overlay_modified = 0;
15807 /* Redisplay the mode line. Select the buffer properly for that. */
15808 if (!update_mode_line)
15810 update_mode_line = 1;
15811 w->update_mode_line = 1;
15814 /* Try to scroll by specified few lines. */
15815 if ((scroll_conservatively
15816 || emacs_scroll_step
15817 || temp_scroll_step
15818 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15819 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15820 && CHARPOS (startp) >= BEGV
15821 && CHARPOS (startp) <= ZV)
15823 /* The function returns -1 if new fonts were loaded, 1 if
15824 successful, 0 if not successful. */
15825 int ss = try_scrolling (window, just_this_one_p,
15826 scroll_conservatively,
15827 emacs_scroll_step,
15828 temp_scroll_step, last_line_misfit);
15829 switch (ss)
15831 case SCROLLING_SUCCESS:
15832 goto done;
15834 case SCROLLING_NEED_LARGER_MATRICES:
15835 goto need_larger_matrices;
15837 case SCROLLING_FAILED:
15838 break;
15840 default:
15841 abort ();
15845 /* Finally, just choose a place to start which positions point
15846 according to user preferences. */
15848 recenter:
15850 #ifdef GLYPH_DEBUG
15851 debug_method_add (w, "recenter");
15852 #endif
15854 /* w->vscroll = 0; */
15856 /* Forget any previously recorded base line for line number display. */
15857 if (!buffer_unchanged_p)
15858 w->base_line_number = Qnil;
15860 /* Determine the window start relative to point. */
15861 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15862 it.current_y = it.last_visible_y;
15863 if (centering_position < 0)
15865 int margin =
15866 scroll_margin > 0
15867 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15868 : 0;
15869 ptrdiff_t margin_pos = CHARPOS (startp);
15870 Lisp_Object aggressive;
15871 int scrolling_up;
15873 /* If there is a scroll margin at the top of the window, find
15874 its character position. */
15875 if (margin
15876 /* Cannot call start_display if startp is not in the
15877 accessible region of the buffer. This can happen when we
15878 have just switched to a different buffer and/or changed
15879 its restriction. In that case, startp is initialized to
15880 the character position 1 (BEGV) because we did not yet
15881 have chance to display the buffer even once. */
15882 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15884 struct it it1;
15885 void *it1data = NULL;
15887 SAVE_IT (it1, it, it1data);
15888 start_display (&it1, w, startp);
15889 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15890 margin_pos = IT_CHARPOS (it1);
15891 RESTORE_IT (&it, &it, it1data);
15893 scrolling_up = PT > margin_pos;
15894 aggressive =
15895 scrolling_up
15896 ? BVAR (current_buffer, scroll_up_aggressively)
15897 : BVAR (current_buffer, scroll_down_aggressively);
15899 if (!MINI_WINDOW_P (w)
15900 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15902 int pt_offset = 0;
15904 /* Setting scroll-conservatively overrides
15905 scroll-*-aggressively. */
15906 if (!scroll_conservatively && NUMBERP (aggressive))
15908 double float_amount = XFLOATINT (aggressive);
15910 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15911 if (pt_offset == 0 && float_amount > 0)
15912 pt_offset = 1;
15913 if (pt_offset && margin > 0)
15914 margin -= 1;
15916 /* Compute how much to move the window start backward from
15917 point so that point will be displayed where the user
15918 wants it. */
15919 if (scrolling_up)
15921 centering_position = it.last_visible_y;
15922 if (pt_offset)
15923 centering_position -= pt_offset;
15924 centering_position -=
15925 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15926 + WINDOW_HEADER_LINE_HEIGHT (w);
15927 /* Don't let point enter the scroll margin near top of
15928 the window. */
15929 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15930 centering_position = margin * FRAME_LINE_HEIGHT (f);
15932 else
15933 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15935 else
15936 /* Set the window start half the height of the window backward
15937 from point. */
15938 centering_position = window_box_height (w) / 2;
15940 move_it_vertically_backward (&it, centering_position);
15942 eassert (IT_CHARPOS (it) >= BEGV);
15944 /* The function move_it_vertically_backward may move over more
15945 than the specified y-distance. If it->w is small, e.g. a
15946 mini-buffer window, we may end up in front of the window's
15947 display area. Start displaying at the start of the line
15948 containing PT in this case. */
15949 if (it.current_y <= 0)
15951 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15952 move_it_vertically_backward (&it, 0);
15953 it.current_y = 0;
15956 it.current_x = it.hpos = 0;
15958 /* Set the window start position here explicitly, to avoid an
15959 infinite loop in case the functions in window-scroll-functions
15960 get errors. */
15961 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15963 /* Run scroll hooks. */
15964 startp = run_window_scroll_functions (window, it.current.pos);
15966 /* Redisplay the window. */
15967 if (!current_matrix_up_to_date_p
15968 || windows_or_buffers_changed
15969 || cursor_type_changed
15970 /* Don't use try_window_reusing_current_matrix in this case
15971 because it can have changed the buffer. */
15972 || !NILP (Vwindow_scroll_functions)
15973 || !just_this_one_p
15974 || MINI_WINDOW_P (w)
15975 || !(used_current_matrix_p
15976 = try_window_reusing_current_matrix (w)))
15977 try_window (window, startp, 0);
15979 /* If new fonts have been loaded (due to fontsets), give up. We
15980 have to start a new redisplay since we need to re-adjust glyph
15981 matrices. */
15982 if (fonts_changed_p)
15983 goto need_larger_matrices;
15985 /* If cursor did not appear assume that the middle of the window is
15986 in the first line of the window. Do it again with the next line.
15987 (Imagine a window of height 100, displaying two lines of height
15988 60. Moving back 50 from it->last_visible_y will end in the first
15989 line.) */
15990 if (w->cursor.vpos < 0)
15992 if (!NILP (w->window_end_valid)
15993 && PT >= Z - XFASTINT (w->window_end_pos))
15995 clear_glyph_matrix (w->desired_matrix);
15996 move_it_by_lines (&it, 1);
15997 try_window (window, it.current.pos, 0);
15999 else if (PT < IT_CHARPOS (it))
16001 clear_glyph_matrix (w->desired_matrix);
16002 move_it_by_lines (&it, -1);
16003 try_window (window, it.current.pos, 0);
16005 else
16007 /* Not much we can do about it. */
16011 /* Consider the following case: Window starts at BEGV, there is
16012 invisible, intangible text at BEGV, so that display starts at
16013 some point START > BEGV. It can happen that we are called with
16014 PT somewhere between BEGV and START. Try to handle that case. */
16015 if (w->cursor.vpos < 0)
16017 struct glyph_row *row = w->current_matrix->rows;
16018 if (row->mode_line_p)
16019 ++row;
16020 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16023 if (!cursor_row_fully_visible_p (w, 0, 0))
16025 /* If vscroll is enabled, disable it and try again. */
16026 if (w->vscroll)
16028 w->vscroll = 0;
16029 clear_glyph_matrix (w->desired_matrix);
16030 goto recenter;
16033 /* Users who set scroll-conservatively to a large number want
16034 point just above/below the scroll margin. If we ended up
16035 with point's row partially visible, move the window start to
16036 make that row fully visible and out of the margin. */
16037 if (scroll_conservatively > SCROLL_LIMIT)
16039 int margin =
16040 scroll_margin > 0
16041 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
16042 : 0;
16043 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
16045 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16046 clear_glyph_matrix (w->desired_matrix);
16047 if (1 == try_window (window, it.current.pos,
16048 TRY_WINDOW_CHECK_MARGINS))
16049 goto done;
16052 /* If centering point failed to make the whole line visible,
16053 put point at the top instead. That has to make the whole line
16054 visible, if it can be done. */
16055 if (centering_position == 0)
16056 goto done;
16058 clear_glyph_matrix (w->desired_matrix);
16059 centering_position = 0;
16060 goto recenter;
16063 done:
16065 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16066 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16067 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16069 /* Display the mode line, if we must. */
16070 if ((update_mode_line
16071 /* If window not full width, must redo its mode line
16072 if (a) the window to its side is being redone and
16073 (b) we do a frame-based redisplay. This is a consequence
16074 of how inverted lines are drawn in frame-based redisplay. */
16075 || (!just_this_one_p
16076 && !FRAME_WINDOW_P (f)
16077 && !WINDOW_FULL_WIDTH_P (w))
16078 /* Line number to display. */
16079 || INTEGERP (w->base_line_pos)
16080 /* Column number is displayed and different from the one displayed. */
16081 || (!NILP (w->column_number_displayed)
16082 && (XFASTINT (w->column_number_displayed) != current_column ())))
16083 /* This means that the window has a mode line. */
16084 && (WINDOW_WANTS_MODELINE_P (w)
16085 || WINDOW_WANTS_HEADER_LINE_P (w)))
16087 display_mode_lines (w);
16089 /* If mode line height has changed, arrange for a thorough
16090 immediate redisplay using the correct mode line height. */
16091 if (WINDOW_WANTS_MODELINE_P (w)
16092 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16094 fonts_changed_p = 1;
16095 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16096 = DESIRED_MODE_LINE_HEIGHT (w);
16099 /* If header line height has changed, arrange for a thorough
16100 immediate redisplay using the correct header line height. */
16101 if (WINDOW_WANTS_HEADER_LINE_P (w)
16102 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16104 fonts_changed_p = 1;
16105 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16106 = DESIRED_HEADER_LINE_HEIGHT (w);
16109 if (fonts_changed_p)
16110 goto need_larger_matrices;
16113 if (!line_number_displayed
16114 && !BUFFERP (w->base_line_pos))
16116 w->base_line_pos = Qnil;
16117 w->base_line_number = Qnil;
16120 finish_menu_bars:
16122 /* When we reach a frame's selected window, redo the frame's menu bar. */
16123 if (update_mode_line
16124 && EQ (FRAME_SELECTED_WINDOW (f), window))
16126 int redisplay_menu_p = 0;
16128 if (FRAME_WINDOW_P (f))
16130 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16131 || defined (HAVE_NS) || defined (USE_GTK)
16132 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16133 #else
16134 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16135 #endif
16137 else
16138 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16140 if (redisplay_menu_p)
16141 display_menu_bar (w);
16143 #ifdef HAVE_WINDOW_SYSTEM
16144 if (FRAME_WINDOW_P (f))
16146 #if defined (USE_GTK) || defined (HAVE_NS)
16147 if (FRAME_EXTERNAL_TOOL_BAR (f))
16148 redisplay_tool_bar (f);
16149 #else
16150 if (WINDOWP (f->tool_bar_window)
16151 && (FRAME_TOOL_BAR_LINES (f) > 0
16152 || !NILP (Vauto_resize_tool_bars))
16153 && redisplay_tool_bar (f))
16154 ignore_mouse_drag_p = 1;
16155 #endif
16157 #endif
16160 #ifdef HAVE_WINDOW_SYSTEM
16161 if (FRAME_WINDOW_P (f)
16162 && update_window_fringes (w, (just_this_one_p
16163 || (!used_current_matrix_p && !overlay_arrow_seen)
16164 || w->pseudo_window_p)))
16166 update_begin (f);
16167 BLOCK_INPUT;
16168 if (draw_window_fringes (w, 1))
16169 x_draw_vertical_border (w);
16170 UNBLOCK_INPUT;
16171 update_end (f);
16173 #endif /* HAVE_WINDOW_SYSTEM */
16175 /* We go to this label, with fonts_changed_p nonzero,
16176 if it is necessary to try again using larger glyph matrices.
16177 We have to redeem the scroll bar even in this case,
16178 because the loop in redisplay_internal expects that. */
16179 need_larger_matrices:
16181 finish_scroll_bars:
16183 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16185 /* Set the thumb's position and size. */
16186 set_vertical_scroll_bar (w);
16188 /* Note that we actually used the scroll bar attached to this
16189 window, so it shouldn't be deleted at the end of redisplay. */
16190 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16191 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16194 /* Restore current_buffer and value of point in it. The window
16195 update may have changed the buffer, so first make sure `opoint'
16196 is still valid (Bug#6177). */
16197 if (CHARPOS (opoint) < BEGV)
16198 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16199 else if (CHARPOS (opoint) > ZV)
16200 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16201 else
16202 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16204 set_buffer_internal_1 (old);
16205 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16206 shorter. This can be caused by log truncation in *Messages*. */
16207 if (CHARPOS (lpoint) <= ZV)
16208 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16210 unbind_to (count, Qnil);
16214 /* Build the complete desired matrix of WINDOW with a window start
16215 buffer position POS.
16217 Value is 1 if successful. It is zero if fonts were loaded during
16218 redisplay which makes re-adjusting glyph matrices necessary, and -1
16219 if point would appear in the scroll margins.
16220 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16221 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16222 set in FLAGS.) */
16225 try_window (Lisp_Object window, struct text_pos pos, int flags)
16227 struct window *w = XWINDOW (window);
16228 struct it it;
16229 struct glyph_row *last_text_row = NULL;
16230 struct frame *f = XFRAME (w->frame);
16232 /* Make POS the new window start. */
16233 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16235 /* Mark cursor position as unknown. No overlay arrow seen. */
16236 w->cursor.vpos = -1;
16237 overlay_arrow_seen = 0;
16239 /* Initialize iterator and info to start at POS. */
16240 start_display (&it, w, pos);
16242 /* Display all lines of W. */
16243 while (it.current_y < it.last_visible_y)
16245 if (display_line (&it))
16246 last_text_row = it.glyph_row - 1;
16247 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16248 return 0;
16251 /* Don't let the cursor end in the scroll margins. */
16252 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16253 && !MINI_WINDOW_P (w))
16255 int this_scroll_margin;
16257 if (scroll_margin > 0)
16259 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16260 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16262 else
16263 this_scroll_margin = 0;
16265 if ((w->cursor.y >= 0 /* not vscrolled */
16266 && w->cursor.y < this_scroll_margin
16267 && CHARPOS (pos) > BEGV
16268 && IT_CHARPOS (it) < ZV)
16269 /* rms: considering make_cursor_line_fully_visible_p here
16270 seems to give wrong results. We don't want to recenter
16271 when the last line is partly visible, we want to allow
16272 that case to be handled in the usual way. */
16273 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16275 w->cursor.vpos = -1;
16276 clear_glyph_matrix (w->desired_matrix);
16277 return -1;
16281 /* If bottom moved off end of frame, change mode line percentage. */
16282 if (XFASTINT (w->window_end_pos) <= 0
16283 && Z != IT_CHARPOS (it))
16284 w->update_mode_line = 1;
16286 /* Set window_end_pos to the offset of the last character displayed
16287 on the window from the end of current_buffer. Set
16288 window_end_vpos to its row number. */
16289 if (last_text_row)
16291 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16292 w->window_end_bytepos
16293 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16294 w->window_end_pos
16295 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16296 w->window_end_vpos
16297 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16298 eassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
16299 ->displays_text_p);
16301 else
16303 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16304 w->window_end_pos = make_number (Z - ZV);
16305 w->window_end_vpos = make_number (0);
16308 /* But that is not valid info until redisplay finishes. */
16309 w->window_end_valid = Qnil;
16310 return 1;
16315 /************************************************************************
16316 Window redisplay reusing current matrix when buffer has not changed
16317 ************************************************************************/
16319 /* Try redisplay of window W showing an unchanged buffer with a
16320 different window start than the last time it was displayed by
16321 reusing its current matrix. Value is non-zero if successful.
16322 W->start is the new window start. */
16324 static int
16325 try_window_reusing_current_matrix (struct window *w)
16327 struct frame *f = XFRAME (w->frame);
16328 struct glyph_row *bottom_row;
16329 struct it it;
16330 struct run run;
16331 struct text_pos start, new_start;
16332 int nrows_scrolled, i;
16333 struct glyph_row *last_text_row;
16334 struct glyph_row *last_reused_text_row;
16335 struct glyph_row *start_row;
16336 int start_vpos, min_y, max_y;
16338 #ifdef GLYPH_DEBUG
16339 if (inhibit_try_window_reusing)
16340 return 0;
16341 #endif
16343 if (/* This function doesn't handle terminal frames. */
16344 !FRAME_WINDOW_P (f)
16345 /* Don't try to reuse the display if windows have been split
16346 or such. */
16347 || windows_or_buffers_changed
16348 || cursor_type_changed)
16349 return 0;
16351 /* Can't do this if region may have changed. */
16352 if ((!NILP (Vtransient_mark_mode)
16353 && !NILP (BVAR (current_buffer, mark_active)))
16354 || !NILP (w->region_showing)
16355 || !NILP (Vshow_trailing_whitespace))
16356 return 0;
16358 /* If top-line visibility has changed, give up. */
16359 if (WINDOW_WANTS_HEADER_LINE_P (w)
16360 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16361 return 0;
16363 /* Give up if old or new display is scrolled vertically. We could
16364 make this function handle this, but right now it doesn't. */
16365 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16366 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16367 return 0;
16369 /* The variable new_start now holds the new window start. The old
16370 start `start' can be determined from the current matrix. */
16371 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16372 start = start_row->minpos;
16373 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16375 /* Clear the desired matrix for the display below. */
16376 clear_glyph_matrix (w->desired_matrix);
16378 if (CHARPOS (new_start) <= CHARPOS (start))
16380 /* Don't use this method if the display starts with an ellipsis
16381 displayed for invisible text. It's not easy to handle that case
16382 below, and it's certainly not worth the effort since this is
16383 not a frequent case. */
16384 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16385 return 0;
16387 IF_DEBUG (debug_method_add (w, "twu1"));
16389 /* Display up to a row that can be reused. The variable
16390 last_text_row is set to the last row displayed that displays
16391 text. Note that it.vpos == 0 if or if not there is a
16392 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16393 start_display (&it, w, new_start);
16394 w->cursor.vpos = -1;
16395 last_text_row = last_reused_text_row = NULL;
16397 while (it.current_y < it.last_visible_y
16398 && !fonts_changed_p)
16400 /* If we have reached into the characters in the START row,
16401 that means the line boundaries have changed. So we
16402 can't start copying with the row START. Maybe it will
16403 work to start copying with the following row. */
16404 while (IT_CHARPOS (it) > CHARPOS (start))
16406 /* Advance to the next row as the "start". */
16407 start_row++;
16408 start = start_row->minpos;
16409 /* If there are no more rows to try, or just one, give up. */
16410 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16411 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16412 || CHARPOS (start) == ZV)
16414 clear_glyph_matrix (w->desired_matrix);
16415 return 0;
16418 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16420 /* If we have reached alignment, we can copy the rest of the
16421 rows. */
16422 if (IT_CHARPOS (it) == CHARPOS (start)
16423 /* Don't accept "alignment" inside a display vector,
16424 since start_row could have started in the middle of
16425 that same display vector (thus their character
16426 positions match), and we have no way of telling if
16427 that is the case. */
16428 && it.current.dpvec_index < 0)
16429 break;
16431 if (display_line (&it))
16432 last_text_row = it.glyph_row - 1;
16436 /* A value of current_y < last_visible_y means that we stopped
16437 at the previous window start, which in turn means that we
16438 have at least one reusable row. */
16439 if (it.current_y < it.last_visible_y)
16441 struct glyph_row *row;
16443 /* IT.vpos always starts from 0; it counts text lines. */
16444 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16446 /* Find PT if not already found in the lines displayed. */
16447 if (w->cursor.vpos < 0)
16449 int dy = it.current_y - start_row->y;
16451 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16452 row = row_containing_pos (w, PT, row, NULL, dy);
16453 if (row)
16454 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16455 dy, nrows_scrolled);
16456 else
16458 clear_glyph_matrix (w->desired_matrix);
16459 return 0;
16463 /* Scroll the display. Do it before the current matrix is
16464 changed. The problem here is that update has not yet
16465 run, i.e. part of the current matrix is not up to date.
16466 scroll_run_hook will clear the cursor, and use the
16467 current matrix to get the height of the row the cursor is
16468 in. */
16469 run.current_y = start_row->y;
16470 run.desired_y = it.current_y;
16471 run.height = it.last_visible_y - it.current_y;
16473 if (run.height > 0 && run.current_y != run.desired_y)
16475 update_begin (f);
16476 FRAME_RIF (f)->update_window_begin_hook (w);
16477 FRAME_RIF (f)->clear_window_mouse_face (w);
16478 FRAME_RIF (f)->scroll_run_hook (w, &run);
16479 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16480 update_end (f);
16483 /* Shift current matrix down by nrows_scrolled lines. */
16484 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16485 rotate_matrix (w->current_matrix,
16486 start_vpos,
16487 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16488 nrows_scrolled);
16490 /* Disable lines that must be updated. */
16491 for (i = 0; i < nrows_scrolled; ++i)
16492 (start_row + i)->enabled_p = 0;
16494 /* Re-compute Y positions. */
16495 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16496 max_y = it.last_visible_y;
16497 for (row = start_row + nrows_scrolled;
16498 row < bottom_row;
16499 ++row)
16501 row->y = it.current_y;
16502 row->visible_height = row->height;
16504 if (row->y < min_y)
16505 row->visible_height -= min_y - row->y;
16506 if (row->y + row->height > max_y)
16507 row->visible_height -= row->y + row->height - max_y;
16508 if (row->fringe_bitmap_periodic_p)
16509 row->redraw_fringe_bitmaps_p = 1;
16511 it.current_y += row->height;
16513 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16514 last_reused_text_row = row;
16515 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16516 break;
16519 /* Disable lines in the current matrix which are now
16520 below the window. */
16521 for (++row; row < bottom_row; ++row)
16522 row->enabled_p = row->mode_line_p = 0;
16525 /* Update window_end_pos etc.; last_reused_text_row is the last
16526 reused row from the current matrix containing text, if any.
16527 The value of last_text_row is the last displayed line
16528 containing text. */
16529 if (last_reused_text_row)
16531 w->window_end_bytepos
16532 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16533 w->window_end_pos
16534 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
16535 w->window_end_vpos
16536 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16537 w->current_matrix));
16539 else if (last_text_row)
16541 w->window_end_bytepos
16542 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16543 w->window_end_pos
16544 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16545 w->window_end_vpos
16546 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16548 else
16550 /* This window must be completely empty. */
16551 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16552 w->window_end_pos = make_number (Z - ZV);
16553 w->window_end_vpos = make_number (0);
16555 w->window_end_valid = Qnil;
16557 /* Update hint: don't try scrolling again in update_window. */
16558 w->desired_matrix->no_scrolling_p = 1;
16560 #ifdef GLYPH_DEBUG
16561 debug_method_add (w, "try_window_reusing_current_matrix 1");
16562 #endif
16563 return 1;
16565 else if (CHARPOS (new_start) > CHARPOS (start))
16567 struct glyph_row *pt_row, *row;
16568 struct glyph_row *first_reusable_row;
16569 struct glyph_row *first_row_to_display;
16570 int dy;
16571 int yb = window_text_bottom_y (w);
16573 /* Find the row starting at new_start, if there is one. Don't
16574 reuse a partially visible line at the end. */
16575 first_reusable_row = start_row;
16576 while (first_reusable_row->enabled_p
16577 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16578 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16579 < CHARPOS (new_start)))
16580 ++first_reusable_row;
16582 /* Give up if there is no row to reuse. */
16583 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16584 || !first_reusable_row->enabled_p
16585 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16586 != CHARPOS (new_start)))
16587 return 0;
16589 /* We can reuse fully visible rows beginning with
16590 first_reusable_row to the end of the window. Set
16591 first_row_to_display to the first row that cannot be reused.
16592 Set pt_row to the row containing point, if there is any. */
16593 pt_row = NULL;
16594 for (first_row_to_display = first_reusable_row;
16595 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16596 ++first_row_to_display)
16598 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16599 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16600 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16601 && first_row_to_display->ends_at_zv_p
16602 && pt_row == NULL)))
16603 pt_row = first_row_to_display;
16606 /* Start displaying at the start of first_row_to_display. */
16607 eassert (first_row_to_display->y < yb);
16608 init_to_row_start (&it, w, first_row_to_display);
16610 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16611 - start_vpos);
16612 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16613 - nrows_scrolled);
16614 it.current_y = (first_row_to_display->y - first_reusable_row->y
16615 + WINDOW_HEADER_LINE_HEIGHT (w));
16617 /* Display lines beginning with first_row_to_display in the
16618 desired matrix. Set last_text_row to the last row displayed
16619 that displays text. */
16620 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16621 if (pt_row == NULL)
16622 w->cursor.vpos = -1;
16623 last_text_row = NULL;
16624 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16625 if (display_line (&it))
16626 last_text_row = it.glyph_row - 1;
16628 /* If point is in a reused row, adjust y and vpos of the cursor
16629 position. */
16630 if (pt_row)
16632 w->cursor.vpos -= nrows_scrolled;
16633 w->cursor.y -= first_reusable_row->y - start_row->y;
16636 /* Give up if point isn't in a row displayed or reused. (This
16637 also handles the case where w->cursor.vpos < nrows_scrolled
16638 after the calls to display_line, which can happen with scroll
16639 margins. See bug#1295.) */
16640 if (w->cursor.vpos < 0)
16642 clear_glyph_matrix (w->desired_matrix);
16643 return 0;
16646 /* Scroll the display. */
16647 run.current_y = first_reusable_row->y;
16648 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16649 run.height = it.last_visible_y - run.current_y;
16650 dy = run.current_y - run.desired_y;
16652 if (run.height)
16654 update_begin (f);
16655 FRAME_RIF (f)->update_window_begin_hook (w);
16656 FRAME_RIF (f)->clear_window_mouse_face (w);
16657 FRAME_RIF (f)->scroll_run_hook (w, &run);
16658 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16659 update_end (f);
16662 /* Adjust Y positions of reused rows. */
16663 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16664 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16665 max_y = it.last_visible_y;
16666 for (row = first_reusable_row; row < first_row_to_display; ++row)
16668 row->y -= dy;
16669 row->visible_height = row->height;
16670 if (row->y < min_y)
16671 row->visible_height -= min_y - row->y;
16672 if (row->y + row->height > max_y)
16673 row->visible_height -= row->y + row->height - max_y;
16674 if (row->fringe_bitmap_periodic_p)
16675 row->redraw_fringe_bitmaps_p = 1;
16678 /* Scroll the current matrix. */
16679 eassert (nrows_scrolled > 0);
16680 rotate_matrix (w->current_matrix,
16681 start_vpos,
16682 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16683 -nrows_scrolled);
16685 /* Disable rows not reused. */
16686 for (row -= nrows_scrolled; row < bottom_row; ++row)
16687 row->enabled_p = 0;
16689 /* Point may have moved to a different line, so we cannot assume that
16690 the previous cursor position is valid; locate the correct row. */
16691 if (pt_row)
16693 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16694 row < bottom_row
16695 && PT >= MATRIX_ROW_END_CHARPOS (row)
16696 && !row->ends_at_zv_p;
16697 row++)
16699 w->cursor.vpos++;
16700 w->cursor.y = row->y;
16702 if (row < bottom_row)
16704 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16705 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16707 /* Can't use this optimization with bidi-reordered glyph
16708 rows, unless cursor is already at point. */
16709 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16711 if (!(w->cursor.hpos >= 0
16712 && w->cursor.hpos < row->used[TEXT_AREA]
16713 && BUFFERP (glyph->object)
16714 && glyph->charpos == PT))
16715 return 0;
16717 else
16718 for (; glyph < end
16719 && (!BUFFERP (glyph->object)
16720 || glyph->charpos < PT);
16721 glyph++)
16723 w->cursor.hpos++;
16724 w->cursor.x += glyph->pixel_width;
16729 /* Adjust window end. A null value of last_text_row means that
16730 the window end is in reused rows which in turn means that
16731 only its vpos can have changed. */
16732 if (last_text_row)
16734 w->window_end_bytepos
16735 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16736 w->window_end_pos
16737 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16738 w->window_end_vpos
16739 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16741 else
16743 w->window_end_vpos
16744 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16747 w->window_end_valid = Qnil;
16748 w->desired_matrix->no_scrolling_p = 1;
16750 #ifdef GLYPH_DEBUG
16751 debug_method_add (w, "try_window_reusing_current_matrix 2");
16752 #endif
16753 return 1;
16756 return 0;
16761 /************************************************************************
16762 Window redisplay reusing current matrix when buffer has changed
16763 ************************************************************************/
16765 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16766 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16767 ptrdiff_t *, ptrdiff_t *);
16768 static struct glyph_row *
16769 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16770 struct glyph_row *);
16773 /* Return the last row in MATRIX displaying text. If row START is
16774 non-null, start searching with that row. IT gives the dimensions
16775 of the display. Value is null if matrix is empty; otherwise it is
16776 a pointer to the row found. */
16778 static struct glyph_row *
16779 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16780 struct glyph_row *start)
16782 struct glyph_row *row, *row_found;
16784 /* Set row_found to the last row in IT->w's current matrix
16785 displaying text. The loop looks funny but think of partially
16786 visible lines. */
16787 row_found = NULL;
16788 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16789 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16791 eassert (row->enabled_p);
16792 row_found = row;
16793 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16794 break;
16795 ++row;
16798 return row_found;
16802 /* Return the last row in the current matrix of W that is not affected
16803 by changes at the start of current_buffer that occurred since W's
16804 current matrix was built. Value is null if no such row exists.
16806 BEG_UNCHANGED us the number of characters unchanged at the start of
16807 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16808 first changed character in current_buffer. Characters at positions <
16809 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16810 when the current matrix was built. */
16812 static struct glyph_row *
16813 find_last_unchanged_at_beg_row (struct window *w)
16815 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16816 struct glyph_row *row;
16817 struct glyph_row *row_found = NULL;
16818 int yb = window_text_bottom_y (w);
16820 /* Find the last row displaying unchanged text. */
16821 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16822 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16823 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16824 ++row)
16826 if (/* If row ends before first_changed_pos, it is unchanged,
16827 except in some case. */
16828 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16829 /* When row ends in ZV and we write at ZV it is not
16830 unchanged. */
16831 && !row->ends_at_zv_p
16832 /* When first_changed_pos is the end of a continued line,
16833 row is not unchanged because it may be no longer
16834 continued. */
16835 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16836 && (row->continued_p
16837 || row->exact_window_width_line_p))
16838 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16839 needs to be recomputed, so don't consider this row as
16840 unchanged. This happens when the last line was
16841 bidi-reordered and was killed immediately before this
16842 redisplay cycle. In that case, ROW->end stores the
16843 buffer position of the first visual-order character of
16844 the killed text, which is now beyond ZV. */
16845 && CHARPOS (row->end.pos) <= ZV)
16846 row_found = row;
16848 /* Stop if last visible row. */
16849 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16850 break;
16853 return row_found;
16857 /* Find the first glyph row in the current matrix of W that is not
16858 affected by changes at the end of current_buffer since the
16859 time W's current matrix was built.
16861 Return in *DELTA the number of chars by which buffer positions in
16862 unchanged text at the end of current_buffer must be adjusted.
16864 Return in *DELTA_BYTES the corresponding number of bytes.
16866 Value is null if no such row exists, i.e. all rows are affected by
16867 changes. */
16869 static struct glyph_row *
16870 find_first_unchanged_at_end_row (struct window *w,
16871 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16873 struct glyph_row *row;
16874 struct glyph_row *row_found = NULL;
16876 *delta = *delta_bytes = 0;
16878 /* Display must not have been paused, otherwise the current matrix
16879 is not up to date. */
16880 eassert (!NILP (w->window_end_valid));
16882 /* A value of window_end_pos >= END_UNCHANGED means that the window
16883 end is in the range of changed text. If so, there is no
16884 unchanged row at the end of W's current matrix. */
16885 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16886 return NULL;
16888 /* Set row to the last row in W's current matrix displaying text. */
16889 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16891 /* If matrix is entirely empty, no unchanged row exists. */
16892 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16894 /* The value of row is the last glyph row in the matrix having a
16895 meaningful buffer position in it. The end position of row
16896 corresponds to window_end_pos. This allows us to translate
16897 buffer positions in the current matrix to current buffer
16898 positions for characters not in changed text. */
16899 ptrdiff_t Z_old =
16900 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16901 ptrdiff_t Z_BYTE_old =
16902 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16903 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16904 struct glyph_row *first_text_row
16905 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16907 *delta = Z - Z_old;
16908 *delta_bytes = Z_BYTE - Z_BYTE_old;
16910 /* Set last_unchanged_pos to the buffer position of the last
16911 character in the buffer that has not been changed. Z is the
16912 index + 1 of the last character in current_buffer, i.e. by
16913 subtracting END_UNCHANGED we get the index of the last
16914 unchanged character, and we have to add BEG to get its buffer
16915 position. */
16916 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16917 last_unchanged_pos_old = last_unchanged_pos - *delta;
16919 /* Search backward from ROW for a row displaying a line that
16920 starts at a minimum position >= last_unchanged_pos_old. */
16921 for (; row > first_text_row; --row)
16923 /* This used to abort, but it can happen.
16924 It is ok to just stop the search instead here. KFS. */
16925 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16926 break;
16928 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16929 row_found = row;
16933 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16935 return row_found;
16939 /* Make sure that glyph rows in the current matrix of window W
16940 reference the same glyph memory as corresponding rows in the
16941 frame's frame matrix. This function is called after scrolling W's
16942 current matrix on a terminal frame in try_window_id and
16943 try_window_reusing_current_matrix. */
16945 static void
16946 sync_frame_with_window_matrix_rows (struct window *w)
16948 struct frame *f = XFRAME (w->frame);
16949 struct glyph_row *window_row, *window_row_end, *frame_row;
16951 /* Preconditions: W must be a leaf window and full-width. Its frame
16952 must have a frame matrix. */
16953 eassert (NILP (w->hchild) && NILP (w->vchild));
16954 eassert (WINDOW_FULL_WIDTH_P (w));
16955 eassert (!FRAME_WINDOW_P (f));
16957 /* If W is a full-width window, glyph pointers in W's current matrix
16958 have, by definition, to be the same as glyph pointers in the
16959 corresponding frame matrix. Note that frame matrices have no
16960 marginal areas (see build_frame_matrix). */
16961 window_row = w->current_matrix->rows;
16962 window_row_end = window_row + w->current_matrix->nrows;
16963 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16964 while (window_row < window_row_end)
16966 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16967 struct glyph *end = window_row->glyphs[LAST_AREA];
16969 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16970 frame_row->glyphs[TEXT_AREA] = start;
16971 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16972 frame_row->glyphs[LAST_AREA] = end;
16974 /* Disable frame rows whose corresponding window rows have
16975 been disabled in try_window_id. */
16976 if (!window_row->enabled_p)
16977 frame_row->enabled_p = 0;
16979 ++window_row, ++frame_row;
16984 /* Find the glyph row in window W containing CHARPOS. Consider all
16985 rows between START and END (not inclusive). END null means search
16986 all rows to the end of the display area of W. Value is the row
16987 containing CHARPOS or null. */
16989 struct glyph_row *
16990 row_containing_pos (struct window *w, ptrdiff_t charpos,
16991 struct glyph_row *start, struct glyph_row *end, int dy)
16993 struct glyph_row *row = start;
16994 struct glyph_row *best_row = NULL;
16995 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16996 int last_y;
16998 /* If we happen to start on a header-line, skip that. */
16999 if (row->mode_line_p)
17000 ++row;
17002 if ((end && row >= end) || !row->enabled_p)
17003 return NULL;
17005 last_y = window_text_bottom_y (w) - dy;
17007 while (1)
17009 /* Give up if we have gone too far. */
17010 if (end && row >= end)
17011 return NULL;
17012 /* This formerly returned if they were equal.
17013 I think that both quantities are of a "last plus one" type;
17014 if so, when they are equal, the row is within the screen. -- rms. */
17015 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17016 return NULL;
17018 /* If it is in this row, return this row. */
17019 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17020 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17021 /* The end position of a row equals the start
17022 position of the next row. If CHARPOS is there, we
17023 would rather display it in the next line, except
17024 when this line ends in ZV. */
17025 && !row->ends_at_zv_p
17026 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
17027 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17029 struct glyph *g;
17031 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17032 || (!best_row && !row->continued_p))
17033 return row;
17034 /* In bidi-reordered rows, there could be several rows
17035 occluding point, all of them belonging to the same
17036 continued line. We need to find the row which fits
17037 CHARPOS the best. */
17038 for (g = row->glyphs[TEXT_AREA];
17039 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17040 g++)
17042 if (!STRINGP (g->object))
17044 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17046 mindif = eabs (g->charpos - charpos);
17047 best_row = row;
17048 /* Exact match always wins. */
17049 if (mindif == 0)
17050 return best_row;
17055 else if (best_row && !row->continued_p)
17056 return best_row;
17057 ++row;
17062 /* Try to redisplay window W by reusing its existing display. W's
17063 current matrix must be up to date when this function is called,
17064 i.e. window_end_valid must not be nil.
17066 Value is
17068 1 if display has been updated
17069 0 if otherwise unsuccessful
17070 -1 if redisplay with same window start is known not to succeed
17072 The following steps are performed:
17074 1. Find the last row in the current matrix of W that is not
17075 affected by changes at the start of current_buffer. If no such row
17076 is found, give up.
17078 2. Find the first row in W's current matrix that is not affected by
17079 changes at the end of current_buffer. Maybe there is no such row.
17081 3. Display lines beginning with the row + 1 found in step 1 to the
17082 row found in step 2 or, if step 2 didn't find a row, to the end of
17083 the window.
17085 4. If cursor is not known to appear on the window, give up.
17087 5. If display stopped at the row found in step 2, scroll the
17088 display and current matrix as needed.
17090 6. Maybe display some lines at the end of W, if we must. This can
17091 happen under various circumstances, like a partially visible line
17092 becoming fully visible, or because newly displayed lines are displayed
17093 in smaller font sizes.
17095 7. Update W's window end information. */
17097 static int
17098 try_window_id (struct window *w)
17100 struct frame *f = XFRAME (w->frame);
17101 struct glyph_matrix *current_matrix = w->current_matrix;
17102 struct glyph_matrix *desired_matrix = w->desired_matrix;
17103 struct glyph_row *last_unchanged_at_beg_row;
17104 struct glyph_row *first_unchanged_at_end_row;
17105 struct glyph_row *row;
17106 struct glyph_row *bottom_row;
17107 int bottom_vpos;
17108 struct it it;
17109 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17110 int dvpos, dy;
17111 struct text_pos start_pos;
17112 struct run run;
17113 int first_unchanged_at_end_vpos = 0;
17114 struct glyph_row *last_text_row, *last_text_row_at_end;
17115 struct text_pos start;
17116 ptrdiff_t first_changed_charpos, last_changed_charpos;
17118 #ifdef GLYPH_DEBUG
17119 if (inhibit_try_window_id)
17120 return 0;
17121 #endif
17123 /* This is handy for debugging. */
17124 #if 0
17125 #define GIVE_UP(X) \
17126 do { \
17127 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17128 return 0; \
17129 } while (0)
17130 #else
17131 #define GIVE_UP(X) return 0
17132 #endif
17134 SET_TEXT_POS_FROM_MARKER (start, w->start);
17136 /* Don't use this for mini-windows because these can show
17137 messages and mini-buffers, and we don't handle that here. */
17138 if (MINI_WINDOW_P (w))
17139 GIVE_UP (1);
17141 /* This flag is used to prevent redisplay optimizations. */
17142 if (windows_or_buffers_changed || cursor_type_changed)
17143 GIVE_UP (2);
17145 /* Verify that narrowing has not changed.
17146 Also verify that we were not told to prevent redisplay optimizations.
17147 It would be nice to further
17148 reduce the number of cases where this prevents try_window_id. */
17149 if (current_buffer->clip_changed
17150 || current_buffer->prevent_redisplay_optimizations_p)
17151 GIVE_UP (3);
17153 /* Window must either use window-based redisplay or be full width. */
17154 if (!FRAME_WINDOW_P (f)
17155 && (!FRAME_LINE_INS_DEL_OK (f)
17156 || !WINDOW_FULL_WIDTH_P (w)))
17157 GIVE_UP (4);
17159 /* Give up if point is known NOT to appear in W. */
17160 if (PT < CHARPOS (start))
17161 GIVE_UP (5);
17163 /* Another way to prevent redisplay optimizations. */
17164 if (w->last_modified == 0)
17165 GIVE_UP (6);
17167 /* Verify that window is not hscrolled. */
17168 if (w->hscroll != 0)
17169 GIVE_UP (7);
17171 /* Verify that display wasn't paused. */
17172 if (NILP (w->window_end_valid))
17173 GIVE_UP (8);
17175 /* Can't use this if highlighting a region because a cursor movement
17176 will do more than just set the cursor. */
17177 if (!NILP (Vtransient_mark_mode)
17178 && !NILP (BVAR (current_buffer, mark_active)))
17179 GIVE_UP (9);
17181 /* Likewise if highlighting trailing whitespace. */
17182 if (!NILP (Vshow_trailing_whitespace))
17183 GIVE_UP (11);
17185 /* Likewise if showing a region. */
17186 if (!NILP (w->region_showing))
17187 GIVE_UP (10);
17189 /* Can't use this if overlay arrow position and/or string have
17190 changed. */
17191 if (overlay_arrows_changed_p ())
17192 GIVE_UP (12);
17194 /* When word-wrap is on, adding a space to the first word of a
17195 wrapped line can change the wrap position, altering the line
17196 above it. It might be worthwhile to handle this more
17197 intelligently, but for now just redisplay from scratch. */
17198 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17199 GIVE_UP (21);
17201 /* Under bidi reordering, adding or deleting a character in the
17202 beginning of a paragraph, before the first strong directional
17203 character, can change the base direction of the paragraph (unless
17204 the buffer specifies a fixed paragraph direction), which will
17205 require to redisplay the whole paragraph. It might be worthwhile
17206 to find the paragraph limits and widen the range of redisplayed
17207 lines to that, but for now just give up this optimization and
17208 redisplay from scratch. */
17209 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17210 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17211 GIVE_UP (22);
17213 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17214 only if buffer has really changed. The reason is that the gap is
17215 initially at Z for freshly visited files. The code below would
17216 set end_unchanged to 0 in that case. */
17217 if (MODIFF > SAVE_MODIFF
17218 /* This seems to happen sometimes after saving a buffer. */
17219 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17221 if (GPT - BEG < BEG_UNCHANGED)
17222 BEG_UNCHANGED = GPT - BEG;
17223 if (Z - GPT < END_UNCHANGED)
17224 END_UNCHANGED = Z - GPT;
17227 /* The position of the first and last character that has been changed. */
17228 first_changed_charpos = BEG + BEG_UNCHANGED;
17229 last_changed_charpos = Z - END_UNCHANGED;
17231 /* If window starts after a line end, and the last change is in
17232 front of that newline, then changes don't affect the display.
17233 This case happens with stealth-fontification. Note that although
17234 the display is unchanged, glyph positions in the matrix have to
17235 be adjusted, of course. */
17236 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17237 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17238 && ((last_changed_charpos < CHARPOS (start)
17239 && CHARPOS (start) == BEGV)
17240 || (last_changed_charpos < CHARPOS (start) - 1
17241 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17243 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17244 struct glyph_row *r0;
17246 /* Compute how many chars/bytes have been added to or removed
17247 from the buffer. */
17248 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17249 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17250 Z_delta = Z - Z_old;
17251 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17253 /* Give up if PT is not in the window. Note that it already has
17254 been checked at the start of try_window_id that PT is not in
17255 front of the window start. */
17256 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17257 GIVE_UP (13);
17259 /* If window start is unchanged, we can reuse the whole matrix
17260 as is, after adjusting glyph positions. No need to compute
17261 the window end again, since its offset from Z hasn't changed. */
17262 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17263 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17264 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17265 /* PT must not be in a partially visible line. */
17266 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17267 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17269 /* Adjust positions in the glyph matrix. */
17270 if (Z_delta || Z_delta_bytes)
17272 struct glyph_row *r1
17273 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17274 increment_matrix_positions (w->current_matrix,
17275 MATRIX_ROW_VPOS (r0, current_matrix),
17276 MATRIX_ROW_VPOS (r1, current_matrix),
17277 Z_delta, Z_delta_bytes);
17280 /* Set the cursor. */
17281 row = row_containing_pos (w, PT, r0, NULL, 0);
17282 if (row)
17283 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17284 else
17285 abort ();
17286 return 1;
17290 /* Handle the case that changes are all below what is displayed in
17291 the window, and that PT is in the window. This shortcut cannot
17292 be taken if ZV is visible in the window, and text has been added
17293 there that is visible in the window. */
17294 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17295 /* ZV is not visible in the window, or there are no
17296 changes at ZV, actually. */
17297 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17298 || first_changed_charpos == last_changed_charpos))
17300 struct glyph_row *r0;
17302 /* Give up if PT is not in the window. Note that it already has
17303 been checked at the start of try_window_id that PT is not in
17304 front of the window start. */
17305 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17306 GIVE_UP (14);
17308 /* If window start is unchanged, we can reuse the whole matrix
17309 as is, without changing glyph positions since no text has
17310 been added/removed in front of the window end. */
17311 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17312 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17313 /* PT must not be in a partially visible line. */
17314 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17315 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17317 /* We have to compute the window end anew since text
17318 could have been added/removed after it. */
17319 w->window_end_pos
17320 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17321 w->window_end_bytepos
17322 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17324 /* Set the cursor. */
17325 row = row_containing_pos (w, PT, r0, NULL, 0);
17326 if (row)
17327 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17328 else
17329 abort ();
17330 return 2;
17334 /* Give up if window start is in the changed area.
17336 The condition used to read
17338 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17340 but why that was tested escapes me at the moment. */
17341 if (CHARPOS (start) >= first_changed_charpos
17342 && CHARPOS (start) <= last_changed_charpos)
17343 GIVE_UP (15);
17345 /* Check that window start agrees with the start of the first glyph
17346 row in its current matrix. Check this after we know the window
17347 start is not in changed text, otherwise positions would not be
17348 comparable. */
17349 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17350 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17351 GIVE_UP (16);
17353 /* Give up if the window ends in strings. Overlay strings
17354 at the end are difficult to handle, so don't try. */
17355 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17356 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17357 GIVE_UP (20);
17359 /* Compute the position at which we have to start displaying new
17360 lines. Some of the lines at the top of the window might be
17361 reusable because they are not displaying changed text. Find the
17362 last row in W's current matrix not affected by changes at the
17363 start of current_buffer. Value is null if changes start in the
17364 first line of window. */
17365 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17366 if (last_unchanged_at_beg_row)
17368 /* Avoid starting to display in the middle of a character, a TAB
17369 for instance. This is easier than to set up the iterator
17370 exactly, and it's not a frequent case, so the additional
17371 effort wouldn't really pay off. */
17372 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17373 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17374 && last_unchanged_at_beg_row > w->current_matrix->rows)
17375 --last_unchanged_at_beg_row;
17377 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17378 GIVE_UP (17);
17380 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17381 GIVE_UP (18);
17382 start_pos = it.current.pos;
17384 /* Start displaying new lines in the desired matrix at the same
17385 vpos we would use in the current matrix, i.e. below
17386 last_unchanged_at_beg_row. */
17387 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17388 current_matrix);
17389 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17390 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17392 eassert (it.hpos == 0 && it.current_x == 0);
17394 else
17396 /* There are no reusable lines at the start of the window.
17397 Start displaying in the first text line. */
17398 start_display (&it, w, start);
17399 it.vpos = it.first_vpos;
17400 start_pos = it.current.pos;
17403 /* Find the first row that is not affected by changes at the end of
17404 the buffer. Value will be null if there is no unchanged row, in
17405 which case we must redisplay to the end of the window. delta
17406 will be set to the value by which buffer positions beginning with
17407 first_unchanged_at_end_row have to be adjusted due to text
17408 changes. */
17409 first_unchanged_at_end_row
17410 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17411 IF_DEBUG (debug_delta = delta);
17412 IF_DEBUG (debug_delta_bytes = delta_bytes);
17414 /* Set stop_pos to the buffer position up to which we will have to
17415 display new lines. If first_unchanged_at_end_row != NULL, this
17416 is the buffer position of the start of the line displayed in that
17417 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17418 that we don't stop at a buffer position. */
17419 stop_pos = 0;
17420 if (first_unchanged_at_end_row)
17422 eassert (last_unchanged_at_beg_row == NULL
17423 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17425 /* If this is a continuation line, move forward to the next one
17426 that isn't. Changes in lines above affect this line.
17427 Caution: this may move first_unchanged_at_end_row to a row
17428 not displaying text. */
17429 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17430 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17431 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17432 < it.last_visible_y))
17433 ++first_unchanged_at_end_row;
17435 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17436 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17437 >= it.last_visible_y))
17438 first_unchanged_at_end_row = NULL;
17439 else
17441 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17442 + delta);
17443 first_unchanged_at_end_vpos
17444 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17445 eassert (stop_pos >= Z - END_UNCHANGED);
17448 else if (last_unchanged_at_beg_row == NULL)
17449 GIVE_UP (19);
17452 #ifdef GLYPH_DEBUG
17454 /* Either there is no unchanged row at the end, or the one we have
17455 now displays text. This is a necessary condition for the window
17456 end pos calculation at the end of this function. */
17457 eassert (first_unchanged_at_end_row == NULL
17458 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17460 debug_last_unchanged_at_beg_vpos
17461 = (last_unchanged_at_beg_row
17462 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17463 : -1);
17464 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17466 #endif /* GLYPH_DEBUG */
17469 /* Display new lines. Set last_text_row to the last new line
17470 displayed which has text on it, i.e. might end up as being the
17471 line where the window_end_vpos is. */
17472 w->cursor.vpos = -1;
17473 last_text_row = NULL;
17474 overlay_arrow_seen = 0;
17475 while (it.current_y < it.last_visible_y
17476 && !fonts_changed_p
17477 && (first_unchanged_at_end_row == NULL
17478 || IT_CHARPOS (it) < stop_pos))
17480 if (display_line (&it))
17481 last_text_row = it.glyph_row - 1;
17484 if (fonts_changed_p)
17485 return -1;
17488 /* Compute differences in buffer positions, y-positions etc. for
17489 lines reused at the bottom of the window. Compute what we can
17490 scroll. */
17491 if (first_unchanged_at_end_row
17492 /* No lines reused because we displayed everything up to the
17493 bottom of the window. */
17494 && it.current_y < it.last_visible_y)
17496 dvpos = (it.vpos
17497 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17498 current_matrix));
17499 dy = it.current_y - first_unchanged_at_end_row->y;
17500 run.current_y = first_unchanged_at_end_row->y;
17501 run.desired_y = run.current_y + dy;
17502 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17504 else
17506 delta = delta_bytes = dvpos = dy
17507 = run.current_y = run.desired_y = run.height = 0;
17508 first_unchanged_at_end_row = NULL;
17510 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17513 /* Find the cursor if not already found. We have to decide whether
17514 PT will appear on this window (it sometimes doesn't, but this is
17515 not a very frequent case.) This decision has to be made before
17516 the current matrix is altered. A value of cursor.vpos < 0 means
17517 that PT is either in one of the lines beginning at
17518 first_unchanged_at_end_row or below the window. Don't care for
17519 lines that might be displayed later at the window end; as
17520 mentioned, this is not a frequent case. */
17521 if (w->cursor.vpos < 0)
17523 /* Cursor in unchanged rows at the top? */
17524 if (PT < CHARPOS (start_pos)
17525 && last_unchanged_at_beg_row)
17527 row = row_containing_pos (w, PT,
17528 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17529 last_unchanged_at_beg_row + 1, 0);
17530 if (row)
17531 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17534 /* Start from first_unchanged_at_end_row looking for PT. */
17535 else if (first_unchanged_at_end_row)
17537 row = row_containing_pos (w, PT - delta,
17538 first_unchanged_at_end_row, NULL, 0);
17539 if (row)
17540 set_cursor_from_row (w, row, w->current_matrix, delta,
17541 delta_bytes, dy, dvpos);
17544 /* Give up if cursor was not found. */
17545 if (w->cursor.vpos < 0)
17547 clear_glyph_matrix (w->desired_matrix);
17548 return -1;
17552 /* Don't let the cursor end in the scroll margins. */
17554 int this_scroll_margin, cursor_height;
17556 this_scroll_margin =
17557 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17558 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17559 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17561 if ((w->cursor.y < this_scroll_margin
17562 && CHARPOS (start) > BEGV)
17563 /* Old redisplay didn't take scroll margin into account at the bottom,
17564 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17565 || (w->cursor.y + (make_cursor_line_fully_visible_p
17566 ? cursor_height + this_scroll_margin
17567 : 1)) > it.last_visible_y)
17569 w->cursor.vpos = -1;
17570 clear_glyph_matrix (w->desired_matrix);
17571 return -1;
17575 /* Scroll the display. Do it before changing the current matrix so
17576 that xterm.c doesn't get confused about where the cursor glyph is
17577 found. */
17578 if (dy && run.height)
17580 update_begin (f);
17582 if (FRAME_WINDOW_P (f))
17584 FRAME_RIF (f)->update_window_begin_hook (w);
17585 FRAME_RIF (f)->clear_window_mouse_face (w);
17586 FRAME_RIF (f)->scroll_run_hook (w, &run);
17587 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17589 else
17591 /* Terminal frame. In this case, dvpos gives the number of
17592 lines to scroll by; dvpos < 0 means scroll up. */
17593 int from_vpos
17594 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17595 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17596 int end = (WINDOW_TOP_EDGE_LINE (w)
17597 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17598 + window_internal_height (w));
17600 #if defined (HAVE_GPM) || defined (MSDOS)
17601 x_clear_window_mouse_face (w);
17602 #endif
17603 /* Perform the operation on the screen. */
17604 if (dvpos > 0)
17606 /* Scroll last_unchanged_at_beg_row to the end of the
17607 window down dvpos lines. */
17608 set_terminal_window (f, end);
17610 /* On dumb terminals delete dvpos lines at the end
17611 before inserting dvpos empty lines. */
17612 if (!FRAME_SCROLL_REGION_OK (f))
17613 ins_del_lines (f, end - dvpos, -dvpos);
17615 /* Insert dvpos empty lines in front of
17616 last_unchanged_at_beg_row. */
17617 ins_del_lines (f, from, dvpos);
17619 else if (dvpos < 0)
17621 /* Scroll up last_unchanged_at_beg_vpos to the end of
17622 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17623 set_terminal_window (f, end);
17625 /* Delete dvpos lines in front of
17626 last_unchanged_at_beg_vpos. ins_del_lines will set
17627 the cursor to the given vpos and emit |dvpos| delete
17628 line sequences. */
17629 ins_del_lines (f, from + dvpos, dvpos);
17631 /* On a dumb terminal insert dvpos empty lines at the
17632 end. */
17633 if (!FRAME_SCROLL_REGION_OK (f))
17634 ins_del_lines (f, end + dvpos, -dvpos);
17637 set_terminal_window (f, 0);
17640 update_end (f);
17643 /* Shift reused rows of the current matrix to the right position.
17644 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17645 text. */
17646 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17647 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17648 if (dvpos < 0)
17650 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17651 bottom_vpos, dvpos);
17652 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17653 bottom_vpos, 0);
17655 else if (dvpos > 0)
17657 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17658 bottom_vpos, dvpos);
17659 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17660 first_unchanged_at_end_vpos + dvpos, 0);
17663 /* For frame-based redisplay, make sure that current frame and window
17664 matrix are in sync with respect to glyph memory. */
17665 if (!FRAME_WINDOW_P (f))
17666 sync_frame_with_window_matrix_rows (w);
17668 /* Adjust buffer positions in reused rows. */
17669 if (delta || delta_bytes)
17670 increment_matrix_positions (current_matrix,
17671 first_unchanged_at_end_vpos + dvpos,
17672 bottom_vpos, delta, delta_bytes);
17674 /* Adjust Y positions. */
17675 if (dy)
17676 shift_glyph_matrix (w, current_matrix,
17677 first_unchanged_at_end_vpos + dvpos,
17678 bottom_vpos, dy);
17680 if (first_unchanged_at_end_row)
17682 first_unchanged_at_end_row += dvpos;
17683 if (first_unchanged_at_end_row->y >= it.last_visible_y
17684 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17685 first_unchanged_at_end_row = NULL;
17688 /* If scrolling up, there may be some lines to display at the end of
17689 the window. */
17690 last_text_row_at_end = NULL;
17691 if (dy < 0)
17693 /* Scrolling up can leave for example a partially visible line
17694 at the end of the window to be redisplayed. */
17695 /* Set last_row to the glyph row in the current matrix where the
17696 window end line is found. It has been moved up or down in
17697 the matrix by dvpos. */
17698 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17699 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17701 /* If last_row is the window end line, it should display text. */
17702 eassert (last_row->displays_text_p);
17704 /* If window end line was partially visible before, begin
17705 displaying at that line. Otherwise begin displaying with the
17706 line following it. */
17707 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17709 init_to_row_start (&it, w, last_row);
17710 it.vpos = last_vpos;
17711 it.current_y = last_row->y;
17713 else
17715 init_to_row_end (&it, w, last_row);
17716 it.vpos = 1 + last_vpos;
17717 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17718 ++last_row;
17721 /* We may start in a continuation line. If so, we have to
17722 get the right continuation_lines_width and current_x. */
17723 it.continuation_lines_width = last_row->continuation_lines_width;
17724 it.hpos = it.current_x = 0;
17726 /* Display the rest of the lines at the window end. */
17727 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17728 while (it.current_y < it.last_visible_y
17729 && !fonts_changed_p)
17731 /* Is it always sure that the display agrees with lines in
17732 the current matrix? I don't think so, so we mark rows
17733 displayed invalid in the current matrix by setting their
17734 enabled_p flag to zero. */
17735 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17736 if (display_line (&it))
17737 last_text_row_at_end = it.glyph_row - 1;
17741 /* Update window_end_pos and window_end_vpos. */
17742 if (first_unchanged_at_end_row
17743 && !last_text_row_at_end)
17745 /* Window end line if one of the preserved rows from the current
17746 matrix. Set row to the last row displaying text in current
17747 matrix starting at first_unchanged_at_end_row, after
17748 scrolling. */
17749 eassert (first_unchanged_at_end_row->displays_text_p);
17750 row = find_last_row_displaying_text (w->current_matrix, &it,
17751 first_unchanged_at_end_row);
17752 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17754 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17755 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17756 w->window_end_vpos
17757 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17758 eassert (w->window_end_bytepos >= 0);
17759 IF_DEBUG (debug_method_add (w, "A"));
17761 else if (last_text_row_at_end)
17763 w->window_end_pos
17764 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17765 w->window_end_bytepos
17766 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17767 w->window_end_vpos
17768 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17769 eassert (w->window_end_bytepos >= 0);
17770 IF_DEBUG (debug_method_add (w, "B"));
17772 else if (last_text_row)
17774 /* We have displayed either to the end of the window or at the
17775 end of the window, i.e. the last row with text is to be found
17776 in the desired matrix. */
17777 w->window_end_pos
17778 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17779 w->window_end_bytepos
17780 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17781 w->window_end_vpos
17782 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17783 eassert (w->window_end_bytepos >= 0);
17785 else if (first_unchanged_at_end_row == NULL
17786 && last_text_row == NULL
17787 && last_text_row_at_end == NULL)
17789 /* Displayed to end of window, but no line containing text was
17790 displayed. Lines were deleted at the end of the window. */
17791 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17792 int vpos = XFASTINT (w->window_end_vpos);
17793 struct glyph_row *current_row = current_matrix->rows + vpos;
17794 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17796 for (row = NULL;
17797 row == NULL && vpos >= first_vpos;
17798 --vpos, --current_row, --desired_row)
17800 if (desired_row->enabled_p)
17802 if (desired_row->displays_text_p)
17803 row = desired_row;
17805 else if (current_row->displays_text_p)
17806 row = current_row;
17809 eassert (row != NULL);
17810 w->window_end_vpos = make_number (vpos + 1);
17811 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17812 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17813 eassert (w->window_end_bytepos >= 0);
17814 IF_DEBUG (debug_method_add (w, "C"));
17816 else
17817 abort ();
17819 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17820 debug_end_vpos = XFASTINT (w->window_end_vpos));
17822 /* Record that display has not been completed. */
17823 w->window_end_valid = Qnil;
17824 w->desired_matrix->no_scrolling_p = 1;
17825 return 3;
17827 #undef GIVE_UP
17832 /***********************************************************************
17833 More debugging support
17834 ***********************************************************************/
17836 #ifdef GLYPH_DEBUG
17838 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17839 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17840 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17843 /* Dump the contents of glyph matrix MATRIX on stderr.
17845 GLYPHS 0 means don't show glyph contents.
17846 GLYPHS 1 means show glyphs in short form
17847 GLYPHS > 1 means show glyphs in long form. */
17849 void
17850 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17852 int i;
17853 for (i = 0; i < matrix->nrows; ++i)
17854 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17858 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17859 the glyph row and area where the glyph comes from. */
17861 void
17862 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17864 if (glyph->type == CHAR_GLYPH)
17866 fprintf (stderr,
17867 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17868 glyph - row->glyphs[TEXT_AREA],
17869 'C',
17870 glyph->charpos,
17871 (BUFFERP (glyph->object)
17872 ? 'B'
17873 : (STRINGP (glyph->object)
17874 ? 'S'
17875 : '-')),
17876 glyph->pixel_width,
17877 glyph->u.ch,
17878 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17879 ? glyph->u.ch
17880 : '.'),
17881 glyph->face_id,
17882 glyph->left_box_line_p,
17883 glyph->right_box_line_p);
17885 else if (glyph->type == STRETCH_GLYPH)
17887 fprintf (stderr,
17888 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17889 glyph - row->glyphs[TEXT_AREA],
17890 'S',
17891 glyph->charpos,
17892 (BUFFERP (glyph->object)
17893 ? 'B'
17894 : (STRINGP (glyph->object)
17895 ? 'S'
17896 : '-')),
17897 glyph->pixel_width,
17899 '.',
17900 glyph->face_id,
17901 glyph->left_box_line_p,
17902 glyph->right_box_line_p);
17904 else if (glyph->type == IMAGE_GLYPH)
17906 fprintf (stderr,
17907 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17908 glyph - row->glyphs[TEXT_AREA],
17909 'I',
17910 glyph->charpos,
17911 (BUFFERP (glyph->object)
17912 ? 'B'
17913 : (STRINGP (glyph->object)
17914 ? 'S'
17915 : '-')),
17916 glyph->pixel_width,
17917 glyph->u.img_id,
17918 '.',
17919 glyph->face_id,
17920 glyph->left_box_line_p,
17921 glyph->right_box_line_p);
17923 else if (glyph->type == COMPOSITE_GLYPH)
17925 fprintf (stderr,
17926 " %5td %4c %6"pI"d %c %3d 0x%05x",
17927 glyph - row->glyphs[TEXT_AREA],
17928 '+',
17929 glyph->charpos,
17930 (BUFFERP (glyph->object)
17931 ? 'B'
17932 : (STRINGP (glyph->object)
17933 ? 'S'
17934 : '-')),
17935 glyph->pixel_width,
17936 glyph->u.cmp.id);
17937 if (glyph->u.cmp.automatic)
17938 fprintf (stderr,
17939 "[%d-%d]",
17940 glyph->slice.cmp.from, glyph->slice.cmp.to);
17941 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17942 glyph->face_id,
17943 glyph->left_box_line_p,
17944 glyph->right_box_line_p);
17949 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17950 GLYPHS 0 means don't show glyph contents.
17951 GLYPHS 1 means show glyphs in short form
17952 GLYPHS > 1 means show glyphs in long form. */
17954 void
17955 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17957 if (glyphs != 1)
17959 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17960 fprintf (stderr, "======================================================================\n");
17962 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17963 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17964 vpos,
17965 MATRIX_ROW_START_CHARPOS (row),
17966 MATRIX_ROW_END_CHARPOS (row),
17967 row->used[TEXT_AREA],
17968 row->contains_overlapping_glyphs_p,
17969 row->enabled_p,
17970 row->truncated_on_left_p,
17971 row->truncated_on_right_p,
17972 row->continued_p,
17973 MATRIX_ROW_CONTINUATION_LINE_P (row),
17974 row->displays_text_p,
17975 row->ends_at_zv_p,
17976 row->fill_line_p,
17977 row->ends_in_middle_of_char_p,
17978 row->starts_in_middle_of_char_p,
17979 row->mouse_face_p,
17980 row->x,
17981 row->y,
17982 row->pixel_width,
17983 row->height,
17984 row->visible_height,
17985 row->ascent,
17986 row->phys_ascent);
17987 fprintf (stderr, "%9"pD"d %5"pD"d\t%5d\n", row->start.overlay_string_index,
17988 row->end.overlay_string_index,
17989 row->continuation_lines_width);
17990 fprintf (stderr, "%9"pI"d %5"pI"d\n",
17991 CHARPOS (row->start.string_pos),
17992 CHARPOS (row->end.string_pos));
17993 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17994 row->end.dpvec_index);
17997 if (glyphs > 1)
17999 int area;
18001 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18003 struct glyph *glyph = row->glyphs[area];
18004 struct glyph *glyph_end = glyph + row->used[area];
18006 /* Glyph for a line end in text. */
18007 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18008 ++glyph_end;
18010 if (glyph < glyph_end)
18011 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
18013 for (; glyph < glyph_end; ++glyph)
18014 dump_glyph (row, glyph, area);
18017 else if (glyphs == 1)
18019 int area;
18021 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18023 char *s = alloca (row->used[area] + 1);
18024 int i;
18026 for (i = 0; i < row->used[area]; ++i)
18028 struct glyph *glyph = row->glyphs[area] + i;
18029 if (glyph->type == CHAR_GLYPH
18030 && glyph->u.ch < 0x80
18031 && glyph->u.ch >= ' ')
18032 s[i] = glyph->u.ch;
18033 else
18034 s[i] = '.';
18037 s[i] = '\0';
18038 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18044 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18045 Sdump_glyph_matrix, 0, 1, "p",
18046 doc: /* Dump the current matrix of the selected window to stderr.
18047 Shows contents of glyph row structures. With non-nil
18048 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18049 glyphs in short form, otherwise show glyphs in long form. */)
18050 (Lisp_Object glyphs)
18052 struct window *w = XWINDOW (selected_window);
18053 struct buffer *buffer = XBUFFER (w->buffer);
18055 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18056 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18057 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18058 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18059 fprintf (stderr, "=============================================\n");
18060 dump_glyph_matrix (w->current_matrix,
18061 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18062 return Qnil;
18066 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18067 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18068 (void)
18070 struct frame *f = XFRAME (selected_frame);
18071 dump_glyph_matrix (f->current_matrix, 1);
18072 return Qnil;
18076 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18077 doc: /* Dump glyph row ROW to stderr.
18078 GLYPH 0 means don't dump glyphs.
18079 GLYPH 1 means dump glyphs in short form.
18080 GLYPH > 1 or omitted means dump glyphs in long form. */)
18081 (Lisp_Object row, Lisp_Object glyphs)
18083 struct glyph_matrix *matrix;
18084 EMACS_INT vpos;
18086 CHECK_NUMBER (row);
18087 matrix = XWINDOW (selected_window)->current_matrix;
18088 vpos = XINT (row);
18089 if (vpos >= 0 && vpos < matrix->nrows)
18090 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18091 vpos,
18092 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18093 return Qnil;
18097 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18098 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18099 GLYPH 0 means don't dump glyphs.
18100 GLYPH 1 means dump glyphs in short form.
18101 GLYPH > 1 or omitted means dump glyphs in long form. */)
18102 (Lisp_Object row, Lisp_Object glyphs)
18104 struct frame *sf = SELECTED_FRAME ();
18105 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18106 EMACS_INT vpos;
18108 CHECK_NUMBER (row);
18109 vpos = XINT (row);
18110 if (vpos >= 0 && vpos < m->nrows)
18111 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18112 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18113 return Qnil;
18117 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18118 doc: /* Toggle tracing of redisplay.
18119 With ARG, turn tracing on if and only if ARG is positive. */)
18120 (Lisp_Object arg)
18122 if (NILP (arg))
18123 trace_redisplay_p = !trace_redisplay_p;
18124 else
18126 arg = Fprefix_numeric_value (arg);
18127 trace_redisplay_p = XINT (arg) > 0;
18130 return Qnil;
18134 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18135 doc: /* Like `format', but print result to stderr.
18136 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18137 (ptrdiff_t nargs, Lisp_Object *args)
18139 Lisp_Object s = Fformat (nargs, args);
18140 fprintf (stderr, "%s", SDATA (s));
18141 return Qnil;
18144 #endif /* GLYPH_DEBUG */
18148 /***********************************************************************
18149 Building Desired Matrix Rows
18150 ***********************************************************************/
18152 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18153 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18155 static struct glyph_row *
18156 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18158 struct frame *f = XFRAME (WINDOW_FRAME (w));
18159 struct buffer *buffer = XBUFFER (w->buffer);
18160 struct buffer *old = current_buffer;
18161 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18162 int arrow_len = SCHARS (overlay_arrow_string);
18163 const unsigned char *arrow_end = arrow_string + arrow_len;
18164 const unsigned char *p;
18165 struct it it;
18166 int multibyte_p;
18167 int n_glyphs_before;
18169 set_buffer_temp (buffer);
18170 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18171 it.glyph_row->used[TEXT_AREA] = 0;
18172 SET_TEXT_POS (it.position, 0, 0);
18174 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18175 p = arrow_string;
18176 while (p < arrow_end)
18178 Lisp_Object face, ilisp;
18180 /* Get the next character. */
18181 if (multibyte_p)
18182 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18183 else
18185 it.c = it.char_to_display = *p, it.len = 1;
18186 if (! ASCII_CHAR_P (it.c))
18187 it.char_to_display = BYTE8_TO_CHAR (it.c);
18189 p += it.len;
18191 /* Get its face. */
18192 ilisp = make_number (p - arrow_string);
18193 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18194 it.face_id = compute_char_face (f, it.char_to_display, face);
18196 /* Compute its width, get its glyphs. */
18197 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18198 SET_TEXT_POS (it.position, -1, -1);
18199 PRODUCE_GLYPHS (&it);
18201 /* If this character doesn't fit any more in the line, we have
18202 to remove some glyphs. */
18203 if (it.current_x > it.last_visible_x)
18205 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18206 break;
18210 set_buffer_temp (old);
18211 return it.glyph_row;
18215 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18216 glyphs to insert is determined by produce_special_glyphs. */
18218 static void
18219 insert_left_trunc_glyphs (struct it *it)
18221 struct it truncate_it;
18222 struct glyph *from, *end, *to, *toend;
18224 eassert (!FRAME_WINDOW_P (it->f)
18225 || (!it->glyph_row->reversed_p
18226 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18227 || (it->glyph_row->reversed_p
18228 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18230 /* Get the truncation glyphs. */
18231 truncate_it = *it;
18232 truncate_it.current_x = 0;
18233 truncate_it.face_id = DEFAULT_FACE_ID;
18234 truncate_it.glyph_row = &scratch_glyph_row;
18235 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18236 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18237 truncate_it.object = make_number (0);
18238 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18240 /* Overwrite glyphs from IT with truncation glyphs. */
18241 if (!it->glyph_row->reversed_p)
18243 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18245 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18246 end = from + tused;
18247 to = it->glyph_row->glyphs[TEXT_AREA];
18248 toend = to + it->glyph_row->used[TEXT_AREA];
18249 if (FRAME_WINDOW_P (it->f))
18251 /* On GUI frames, when variable-size fonts are displayed,
18252 the truncation glyphs may need more pixels than the row's
18253 glyphs they overwrite. We overwrite more glyphs to free
18254 enough screen real estate, and enlarge the stretch glyph
18255 on the right (see display_line), if there is one, to
18256 preserve the screen position of the truncation glyphs on
18257 the right. */
18258 int w = 0;
18259 struct glyph *g = to;
18260 short used;
18262 while (g < toend && it->glyph_row->x + w < 0)
18264 w += g->pixel_width;
18265 ++g;
18267 it->glyph_row->x = 0;
18268 w = 0;
18269 while (g < toend && w < it->truncation_pixel_width)
18271 w += g->pixel_width;
18272 ++g;
18274 if (g - to - tused > 0)
18276 memmove (to + tused, g, toend - g);
18277 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18279 used = it->glyph_row->used[TEXT_AREA];
18280 if (it->glyph_row->truncated_on_right_p
18281 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18282 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18283 == STRETCH_GLYPH)
18285 int extra = w - it->truncation_pixel_width;
18287 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18291 while (from < end)
18292 *to++ = *from++;
18294 /* There may be padding glyphs left over. Overwrite them too. */
18295 if (!FRAME_WINDOW_P (it->f))
18297 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18299 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18300 while (from < end)
18301 *to++ = *from++;
18305 if (to > toend)
18306 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18308 else
18310 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18312 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18313 that back to front. */
18314 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18315 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18316 toend = it->glyph_row->glyphs[TEXT_AREA];
18317 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18318 if (FRAME_WINDOW_P (it->f))
18320 int w = 0;
18321 struct glyph *g = to;
18323 while (g >= toend && w < it->truncation_pixel_width)
18325 w += g->pixel_width;
18326 --g;
18328 if (to - g - tused > 0)
18329 to = g + tused;
18330 if (it->glyph_row->truncated_on_right_p
18331 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18332 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18334 int extra = w - it->truncation_pixel_width;
18336 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18340 while (from >= end && to >= toend)
18341 *to-- = *from--;
18342 if (!FRAME_WINDOW_P (it->f))
18344 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18346 from =
18347 truncate_it.glyph_row->glyphs[TEXT_AREA]
18348 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18349 while (from >= end && to >= toend)
18350 *to-- = *from--;
18353 if (from >= end)
18355 /* Need to free some room before prepending additional
18356 glyphs. */
18357 int move_by = from - end + 1;
18358 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18359 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18361 for ( ; g >= g0; g--)
18362 g[move_by] = *g;
18363 while (from >= end)
18364 *to-- = *from--;
18365 it->glyph_row->used[TEXT_AREA] += move_by;
18370 /* Compute the hash code for ROW. */
18371 unsigned
18372 row_hash (struct glyph_row *row)
18374 int area, k;
18375 unsigned hashval = 0;
18377 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18378 for (k = 0; k < row->used[area]; ++k)
18379 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18380 + row->glyphs[area][k].u.val
18381 + row->glyphs[area][k].face_id
18382 + row->glyphs[area][k].padding_p
18383 + (row->glyphs[area][k].type << 2));
18385 return hashval;
18388 /* Compute the pixel height and width of IT->glyph_row.
18390 Most of the time, ascent and height of a display line will be equal
18391 to the max_ascent and max_height values of the display iterator
18392 structure. This is not the case if
18394 1. We hit ZV without displaying anything. In this case, max_ascent
18395 and max_height will be zero.
18397 2. We have some glyphs that don't contribute to the line height.
18398 (The glyph row flag contributes_to_line_height_p is for future
18399 pixmap extensions).
18401 The first case is easily covered by using default values because in
18402 these cases, the line height does not really matter, except that it
18403 must not be zero. */
18405 static void
18406 compute_line_metrics (struct it *it)
18408 struct glyph_row *row = it->glyph_row;
18410 if (FRAME_WINDOW_P (it->f))
18412 int i, min_y, max_y;
18414 /* The line may consist of one space only, that was added to
18415 place the cursor on it. If so, the row's height hasn't been
18416 computed yet. */
18417 if (row->height == 0)
18419 if (it->max_ascent + it->max_descent == 0)
18420 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18421 row->ascent = it->max_ascent;
18422 row->height = it->max_ascent + it->max_descent;
18423 row->phys_ascent = it->max_phys_ascent;
18424 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18425 row->extra_line_spacing = it->max_extra_line_spacing;
18428 /* Compute the width of this line. */
18429 row->pixel_width = row->x;
18430 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18431 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18433 eassert (row->pixel_width >= 0);
18434 eassert (row->ascent >= 0 && row->height > 0);
18436 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18437 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18439 /* If first line's physical ascent is larger than its logical
18440 ascent, use the physical ascent, and make the row taller.
18441 This makes accented characters fully visible. */
18442 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18443 && row->phys_ascent > row->ascent)
18445 row->height += row->phys_ascent - row->ascent;
18446 row->ascent = row->phys_ascent;
18449 /* Compute how much of the line is visible. */
18450 row->visible_height = row->height;
18452 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18453 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18455 if (row->y < min_y)
18456 row->visible_height -= min_y - row->y;
18457 if (row->y + row->height > max_y)
18458 row->visible_height -= row->y + row->height - max_y;
18460 else
18462 row->pixel_width = row->used[TEXT_AREA];
18463 if (row->continued_p)
18464 row->pixel_width -= it->continuation_pixel_width;
18465 else if (row->truncated_on_right_p)
18466 row->pixel_width -= it->truncation_pixel_width;
18467 row->ascent = row->phys_ascent = 0;
18468 row->height = row->phys_height = row->visible_height = 1;
18469 row->extra_line_spacing = 0;
18472 /* Compute a hash code for this row. */
18473 row->hash = row_hash (row);
18475 it->max_ascent = it->max_descent = 0;
18476 it->max_phys_ascent = it->max_phys_descent = 0;
18480 /* Append one space to the glyph row of iterator IT if doing a
18481 window-based redisplay. The space has the same face as
18482 IT->face_id. Value is non-zero if a space was added.
18484 This function is called to make sure that there is always one glyph
18485 at the end of a glyph row that the cursor can be set on under
18486 window-systems. (If there weren't such a glyph we would not know
18487 how wide and tall a box cursor should be displayed).
18489 At the same time this space let's a nicely handle clearing to the
18490 end of the line if the row ends in italic text. */
18492 static int
18493 append_space_for_newline (struct it *it, int default_face_p)
18495 if (FRAME_WINDOW_P (it->f))
18497 int n = it->glyph_row->used[TEXT_AREA];
18499 if (it->glyph_row->glyphs[TEXT_AREA] + n
18500 < it->glyph_row->glyphs[1 + TEXT_AREA])
18502 /* Save some values that must not be changed.
18503 Must save IT->c and IT->len because otherwise
18504 ITERATOR_AT_END_P wouldn't work anymore after
18505 append_space_for_newline has been called. */
18506 enum display_element_type saved_what = it->what;
18507 int saved_c = it->c, saved_len = it->len;
18508 int saved_char_to_display = it->char_to_display;
18509 int saved_x = it->current_x;
18510 int saved_face_id = it->face_id;
18511 struct text_pos saved_pos;
18512 Lisp_Object saved_object;
18513 struct face *face;
18515 saved_object = it->object;
18516 saved_pos = it->position;
18518 it->what = IT_CHARACTER;
18519 memset (&it->position, 0, sizeof it->position);
18520 it->object = make_number (0);
18521 it->c = it->char_to_display = ' ';
18522 it->len = 1;
18524 /* If the default face was remapped, be sure to use the
18525 remapped face for the appended newline. */
18526 if (default_face_p)
18527 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18528 else if (it->face_before_selective_p)
18529 it->face_id = it->saved_face_id;
18530 face = FACE_FROM_ID (it->f, it->face_id);
18531 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18533 PRODUCE_GLYPHS (it);
18535 it->override_ascent = -1;
18536 it->constrain_row_ascent_descent_p = 0;
18537 it->current_x = saved_x;
18538 it->object = saved_object;
18539 it->position = saved_pos;
18540 it->what = saved_what;
18541 it->face_id = saved_face_id;
18542 it->len = saved_len;
18543 it->c = saved_c;
18544 it->char_to_display = saved_char_to_display;
18545 return 1;
18549 return 0;
18553 /* Extend the face of the last glyph in the text area of IT->glyph_row
18554 to the end of the display line. Called from display_line. If the
18555 glyph row is empty, add a space glyph to it so that we know the
18556 face to draw. Set the glyph row flag fill_line_p. If the glyph
18557 row is R2L, prepend a stretch glyph to cover the empty space to the
18558 left of the leftmost glyph. */
18560 static void
18561 extend_face_to_end_of_line (struct it *it)
18563 struct face *face, *default_face;
18564 struct frame *f = it->f;
18566 /* If line is already filled, do nothing. Non window-system frames
18567 get a grace of one more ``pixel'' because their characters are
18568 1-``pixel'' wide, so they hit the equality too early. This grace
18569 is needed only for R2L rows that are not continued, to produce
18570 one extra blank where we could display the cursor. */
18571 if (it->current_x >= it->last_visible_x
18572 + (!FRAME_WINDOW_P (f)
18573 && it->glyph_row->reversed_p
18574 && !it->glyph_row->continued_p))
18575 return;
18577 /* The default face, possibly remapped. */
18578 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18580 /* Face extension extends the background and box of IT->face_id
18581 to the end of the line. If the background equals the background
18582 of the frame, we don't have to do anything. */
18583 if (it->face_before_selective_p)
18584 face = FACE_FROM_ID (f, it->saved_face_id);
18585 else
18586 face = FACE_FROM_ID (f, it->face_id);
18588 if (FRAME_WINDOW_P (f)
18589 && it->glyph_row->displays_text_p
18590 && face->box == FACE_NO_BOX
18591 && face->background == FRAME_BACKGROUND_PIXEL (f)
18592 && !face->stipple
18593 && !it->glyph_row->reversed_p)
18594 return;
18596 /* Set the glyph row flag indicating that the face of the last glyph
18597 in the text area has to be drawn to the end of the text area. */
18598 it->glyph_row->fill_line_p = 1;
18600 /* If current character of IT is not ASCII, make sure we have the
18601 ASCII face. This will be automatically undone the next time
18602 get_next_display_element returns a multibyte character. Note
18603 that the character will always be single byte in unibyte
18604 text. */
18605 if (!ASCII_CHAR_P (it->c))
18607 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18610 if (FRAME_WINDOW_P (f))
18612 /* If the row is empty, add a space with the current face of IT,
18613 so that we know which face to draw. */
18614 if (it->glyph_row->used[TEXT_AREA] == 0)
18616 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18617 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18618 it->glyph_row->used[TEXT_AREA] = 1;
18620 #ifdef HAVE_WINDOW_SYSTEM
18621 if (it->glyph_row->reversed_p)
18623 /* Prepend a stretch glyph to the row, such that the
18624 rightmost glyph will be drawn flushed all the way to the
18625 right margin of the window. The stretch glyph that will
18626 occupy the empty space, if any, to the left of the
18627 glyphs. */
18628 struct font *font = face->font ? face->font : FRAME_FONT (f);
18629 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18630 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18631 struct glyph *g;
18632 int row_width, stretch_ascent, stretch_width;
18633 struct text_pos saved_pos;
18634 int saved_face_id, saved_avoid_cursor;
18636 for (row_width = 0, g = row_start; g < row_end; g++)
18637 row_width += g->pixel_width;
18638 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18639 if (stretch_width > 0)
18641 stretch_ascent =
18642 (((it->ascent + it->descent)
18643 * FONT_BASE (font)) / FONT_HEIGHT (font));
18644 saved_pos = it->position;
18645 memset (&it->position, 0, sizeof it->position);
18646 saved_avoid_cursor = it->avoid_cursor_p;
18647 it->avoid_cursor_p = 1;
18648 saved_face_id = it->face_id;
18649 /* The last row's stretch glyph should get the default
18650 face, to avoid painting the rest of the window with
18651 the region face, if the region ends at ZV. */
18652 if (it->glyph_row->ends_at_zv_p)
18653 it->face_id = default_face->id;
18654 else
18655 it->face_id = face->id;
18656 append_stretch_glyph (it, make_number (0), stretch_width,
18657 it->ascent + it->descent, stretch_ascent);
18658 it->position = saved_pos;
18659 it->avoid_cursor_p = saved_avoid_cursor;
18660 it->face_id = saved_face_id;
18663 #endif /* HAVE_WINDOW_SYSTEM */
18665 else
18667 /* Save some values that must not be changed. */
18668 int saved_x = it->current_x;
18669 struct text_pos saved_pos;
18670 Lisp_Object saved_object;
18671 enum display_element_type saved_what = it->what;
18672 int saved_face_id = it->face_id;
18674 saved_object = it->object;
18675 saved_pos = it->position;
18677 it->what = IT_CHARACTER;
18678 memset (&it->position, 0, sizeof it->position);
18679 it->object = make_number (0);
18680 it->c = it->char_to_display = ' ';
18681 it->len = 1;
18682 /* The last row's blank glyphs should get the default face, to
18683 avoid painting the rest of the window with the region face,
18684 if the region ends at ZV. */
18685 if (it->glyph_row->ends_at_zv_p)
18686 it->face_id = default_face->id;
18687 else
18688 it->face_id = face->id;
18690 PRODUCE_GLYPHS (it);
18692 while (it->current_x <= it->last_visible_x)
18693 PRODUCE_GLYPHS (it);
18695 /* Don't count these blanks really. It would let us insert a left
18696 truncation glyph below and make us set the cursor on them, maybe. */
18697 it->current_x = saved_x;
18698 it->object = saved_object;
18699 it->position = saved_pos;
18700 it->what = saved_what;
18701 it->face_id = saved_face_id;
18706 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18707 trailing whitespace. */
18709 static int
18710 trailing_whitespace_p (ptrdiff_t charpos)
18712 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18713 int c = 0;
18715 while (bytepos < ZV_BYTE
18716 && (c = FETCH_CHAR (bytepos),
18717 c == ' ' || c == '\t'))
18718 ++bytepos;
18720 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18722 if (bytepos != PT_BYTE)
18723 return 1;
18725 return 0;
18729 /* Highlight trailing whitespace, if any, in ROW. */
18731 static void
18732 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18734 int used = row->used[TEXT_AREA];
18736 if (used)
18738 struct glyph *start = row->glyphs[TEXT_AREA];
18739 struct glyph *glyph = start + used - 1;
18741 if (row->reversed_p)
18743 /* Right-to-left rows need to be processed in the opposite
18744 direction, so swap the edge pointers. */
18745 glyph = start;
18746 start = row->glyphs[TEXT_AREA] + used - 1;
18749 /* Skip over glyphs inserted to display the cursor at the
18750 end of a line, for extending the face of the last glyph
18751 to the end of the line on terminals, and for truncation
18752 and continuation glyphs. */
18753 if (!row->reversed_p)
18755 while (glyph >= start
18756 && glyph->type == CHAR_GLYPH
18757 && INTEGERP (glyph->object))
18758 --glyph;
18760 else
18762 while (glyph <= start
18763 && glyph->type == CHAR_GLYPH
18764 && INTEGERP (glyph->object))
18765 ++glyph;
18768 /* If last glyph is a space or stretch, and it's trailing
18769 whitespace, set the face of all trailing whitespace glyphs in
18770 IT->glyph_row to `trailing-whitespace'. */
18771 if ((row->reversed_p ? glyph <= start : glyph >= start)
18772 && BUFFERP (glyph->object)
18773 && (glyph->type == STRETCH_GLYPH
18774 || (glyph->type == CHAR_GLYPH
18775 && glyph->u.ch == ' '))
18776 && trailing_whitespace_p (glyph->charpos))
18778 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18779 if (face_id < 0)
18780 return;
18782 if (!row->reversed_p)
18784 while (glyph >= start
18785 && BUFFERP (glyph->object)
18786 && (glyph->type == STRETCH_GLYPH
18787 || (glyph->type == CHAR_GLYPH
18788 && glyph->u.ch == ' ')))
18789 (glyph--)->face_id = face_id;
18791 else
18793 while (glyph <= start
18794 && BUFFERP (glyph->object)
18795 && (glyph->type == STRETCH_GLYPH
18796 || (glyph->type == CHAR_GLYPH
18797 && glyph->u.ch == ' ')))
18798 (glyph++)->face_id = face_id;
18805 /* Value is non-zero if glyph row ROW should be
18806 used to hold the cursor. */
18808 static int
18809 cursor_row_p (struct glyph_row *row)
18811 int result = 1;
18813 if (PT == CHARPOS (row->end.pos)
18814 || PT == MATRIX_ROW_END_CHARPOS (row))
18816 /* Suppose the row ends on a string.
18817 Unless the row is continued, that means it ends on a newline
18818 in the string. If it's anything other than a display string
18819 (e.g., a before-string from an overlay), we don't want the
18820 cursor there. (This heuristic seems to give the optimal
18821 behavior for the various types of multi-line strings.)
18822 One exception: if the string has `cursor' property on one of
18823 its characters, we _do_ want the cursor there. */
18824 if (CHARPOS (row->end.string_pos) >= 0)
18826 if (row->continued_p)
18827 result = 1;
18828 else
18830 /* Check for `display' property. */
18831 struct glyph *beg = row->glyphs[TEXT_AREA];
18832 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18833 struct glyph *glyph;
18835 result = 0;
18836 for (glyph = end; glyph >= beg; --glyph)
18837 if (STRINGP (glyph->object))
18839 Lisp_Object prop
18840 = Fget_char_property (make_number (PT),
18841 Qdisplay, Qnil);
18842 result =
18843 (!NILP (prop)
18844 && display_prop_string_p (prop, glyph->object));
18845 /* If there's a `cursor' property on one of the
18846 string's characters, this row is a cursor row,
18847 even though this is not a display string. */
18848 if (!result)
18850 Lisp_Object s = glyph->object;
18852 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18854 ptrdiff_t gpos = glyph->charpos;
18856 if (!NILP (Fget_char_property (make_number (gpos),
18857 Qcursor, s)))
18859 result = 1;
18860 break;
18864 break;
18868 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18870 /* If the row ends in middle of a real character,
18871 and the line is continued, we want the cursor here.
18872 That's because CHARPOS (ROW->end.pos) would equal
18873 PT if PT is before the character. */
18874 if (!row->ends_in_ellipsis_p)
18875 result = row->continued_p;
18876 else
18877 /* If the row ends in an ellipsis, then
18878 CHARPOS (ROW->end.pos) will equal point after the
18879 invisible text. We want that position to be displayed
18880 after the ellipsis. */
18881 result = 0;
18883 /* If the row ends at ZV, display the cursor at the end of that
18884 row instead of at the start of the row below. */
18885 else if (row->ends_at_zv_p)
18886 result = 1;
18887 else
18888 result = 0;
18891 return result;
18896 /* Push the property PROP so that it will be rendered at the current
18897 position in IT. Return 1 if PROP was successfully pushed, 0
18898 otherwise. Called from handle_line_prefix to handle the
18899 `line-prefix' and `wrap-prefix' properties. */
18901 static int
18902 push_prefix_prop (struct it *it, Lisp_Object prop)
18904 struct text_pos pos =
18905 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18907 eassert (it->method == GET_FROM_BUFFER
18908 || it->method == GET_FROM_DISPLAY_VECTOR
18909 || it->method == GET_FROM_STRING);
18911 /* We need to save the current buffer/string position, so it will be
18912 restored by pop_it, because iterate_out_of_display_property
18913 depends on that being set correctly, but some situations leave
18914 it->position not yet set when this function is called. */
18915 push_it (it, &pos);
18917 if (STRINGP (prop))
18919 if (SCHARS (prop) == 0)
18921 pop_it (it);
18922 return 0;
18925 it->string = prop;
18926 it->string_from_prefix_prop_p = 1;
18927 it->multibyte_p = STRING_MULTIBYTE (it->string);
18928 it->current.overlay_string_index = -1;
18929 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18930 it->end_charpos = it->string_nchars = SCHARS (it->string);
18931 it->method = GET_FROM_STRING;
18932 it->stop_charpos = 0;
18933 it->prev_stop = 0;
18934 it->base_level_stop = 0;
18936 /* Force paragraph direction to be that of the parent
18937 buffer/string. */
18938 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18939 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18940 else
18941 it->paragraph_embedding = L2R;
18943 /* Set up the bidi iterator for this display string. */
18944 if (it->bidi_p)
18946 it->bidi_it.string.lstring = it->string;
18947 it->bidi_it.string.s = NULL;
18948 it->bidi_it.string.schars = it->end_charpos;
18949 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18950 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18951 it->bidi_it.string.unibyte = !it->multibyte_p;
18952 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18955 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18957 it->method = GET_FROM_STRETCH;
18958 it->object = prop;
18960 #ifdef HAVE_WINDOW_SYSTEM
18961 else if (IMAGEP (prop))
18963 it->what = IT_IMAGE;
18964 it->image_id = lookup_image (it->f, prop);
18965 it->method = GET_FROM_IMAGE;
18967 #endif /* HAVE_WINDOW_SYSTEM */
18968 else
18970 pop_it (it); /* bogus display property, give up */
18971 return 0;
18974 return 1;
18977 /* Return the character-property PROP at the current position in IT. */
18979 static Lisp_Object
18980 get_it_property (struct it *it, Lisp_Object prop)
18982 Lisp_Object position;
18984 if (STRINGP (it->object))
18985 position = make_number (IT_STRING_CHARPOS (*it));
18986 else if (BUFFERP (it->object))
18987 position = make_number (IT_CHARPOS (*it));
18988 else
18989 return Qnil;
18991 return Fget_char_property (position, prop, it->object);
18994 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18996 static void
18997 handle_line_prefix (struct it *it)
18999 Lisp_Object prefix;
19001 if (it->continuation_lines_width > 0)
19003 prefix = get_it_property (it, Qwrap_prefix);
19004 if (NILP (prefix))
19005 prefix = Vwrap_prefix;
19007 else
19009 prefix = get_it_property (it, Qline_prefix);
19010 if (NILP (prefix))
19011 prefix = Vline_prefix;
19013 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19015 /* If the prefix is wider than the window, and we try to wrap
19016 it, it would acquire its own wrap prefix, and so on till the
19017 iterator stack overflows. So, don't wrap the prefix. */
19018 it->line_wrap = TRUNCATE;
19019 it->avoid_cursor_p = 1;
19025 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19026 only for R2L lines from display_line and display_string, when they
19027 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19028 the line/string needs to be continued on the next glyph row. */
19029 static void
19030 unproduce_glyphs (struct it *it, int n)
19032 struct glyph *glyph, *end;
19034 eassert (it->glyph_row);
19035 eassert (it->glyph_row->reversed_p);
19036 eassert (it->area == TEXT_AREA);
19037 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19039 if (n > it->glyph_row->used[TEXT_AREA])
19040 n = it->glyph_row->used[TEXT_AREA];
19041 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19042 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19043 for ( ; glyph < end; glyph++)
19044 glyph[-n] = *glyph;
19047 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19048 and ROW->maxpos. */
19049 static void
19050 find_row_edges (struct it *it, struct glyph_row *row,
19051 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19052 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19054 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19055 lines' rows is implemented for bidi-reordered rows. */
19057 /* ROW->minpos is the value of min_pos, the minimal buffer position
19058 we have in ROW, or ROW->start.pos if that is smaller. */
19059 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19060 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19061 else
19062 /* We didn't find buffer positions smaller than ROW->start, or
19063 didn't find _any_ valid buffer positions in any of the glyphs,
19064 so we must trust the iterator's computed positions. */
19065 row->minpos = row->start.pos;
19066 if (max_pos <= 0)
19068 max_pos = CHARPOS (it->current.pos);
19069 max_bpos = BYTEPOS (it->current.pos);
19072 /* Here are the various use-cases for ending the row, and the
19073 corresponding values for ROW->maxpos:
19075 Line ends in a newline from buffer eol_pos + 1
19076 Line is continued from buffer max_pos + 1
19077 Line is truncated on right it->current.pos
19078 Line ends in a newline from string max_pos + 1(*)
19079 (*) + 1 only when line ends in a forward scan
19080 Line is continued from string max_pos
19081 Line is continued from display vector max_pos
19082 Line is entirely from a string min_pos == max_pos
19083 Line is entirely from a display vector min_pos == max_pos
19084 Line that ends at ZV ZV
19086 If you discover other use-cases, please add them here as
19087 appropriate. */
19088 if (row->ends_at_zv_p)
19089 row->maxpos = it->current.pos;
19090 else if (row->used[TEXT_AREA])
19092 int seen_this_string = 0;
19093 struct glyph_row *r1 = row - 1;
19095 /* Did we see the same display string on the previous row? */
19096 if (STRINGP (it->object)
19097 /* this is not the first row */
19098 && row > it->w->desired_matrix->rows
19099 /* previous row is not the header line */
19100 && !r1->mode_line_p
19101 /* previous row also ends in a newline from a string */
19102 && r1->ends_in_newline_from_string_p)
19104 struct glyph *start, *end;
19106 /* Search for the last glyph of the previous row that came
19107 from buffer or string. Depending on whether the row is
19108 L2R or R2L, we need to process it front to back or the
19109 other way round. */
19110 if (!r1->reversed_p)
19112 start = r1->glyphs[TEXT_AREA];
19113 end = start + r1->used[TEXT_AREA];
19114 /* Glyphs inserted by redisplay have an integer (zero)
19115 as their object. */
19116 while (end > start
19117 && INTEGERP ((end - 1)->object)
19118 && (end - 1)->charpos <= 0)
19119 --end;
19120 if (end > start)
19122 if (EQ ((end - 1)->object, it->object))
19123 seen_this_string = 1;
19125 else
19126 /* If all the glyphs of the previous row were inserted
19127 by redisplay, it means the previous row was
19128 produced from a single newline, which is only
19129 possible if that newline came from the same string
19130 as the one which produced this ROW. */
19131 seen_this_string = 1;
19133 else
19135 end = r1->glyphs[TEXT_AREA] - 1;
19136 start = end + r1->used[TEXT_AREA];
19137 while (end < start
19138 && INTEGERP ((end + 1)->object)
19139 && (end + 1)->charpos <= 0)
19140 ++end;
19141 if (end < start)
19143 if (EQ ((end + 1)->object, it->object))
19144 seen_this_string = 1;
19146 else
19147 seen_this_string = 1;
19150 /* Take note of each display string that covers a newline only
19151 once, the first time we see it. This is for when a display
19152 string includes more than one newline in it. */
19153 if (row->ends_in_newline_from_string_p && !seen_this_string)
19155 /* If we were scanning the buffer forward when we displayed
19156 the string, we want to account for at least one buffer
19157 position that belongs to this row (position covered by
19158 the display string), so that cursor positioning will
19159 consider this row as a candidate when point is at the end
19160 of the visual line represented by this row. This is not
19161 required when scanning back, because max_pos will already
19162 have a much larger value. */
19163 if (CHARPOS (row->end.pos) > max_pos)
19164 INC_BOTH (max_pos, max_bpos);
19165 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19167 else if (CHARPOS (it->eol_pos) > 0)
19168 SET_TEXT_POS (row->maxpos,
19169 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19170 else if (row->continued_p)
19172 /* If max_pos is different from IT's current position, it
19173 means IT->method does not belong to the display element
19174 at max_pos. However, it also means that the display
19175 element at max_pos was displayed in its entirety on this
19176 line, which is equivalent to saying that the next line
19177 starts at the next buffer position. */
19178 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19179 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19180 else
19182 INC_BOTH (max_pos, max_bpos);
19183 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19186 else if (row->truncated_on_right_p)
19187 /* display_line already called reseat_at_next_visible_line_start,
19188 which puts the iterator at the beginning of the next line, in
19189 the logical order. */
19190 row->maxpos = it->current.pos;
19191 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19192 /* A line that is entirely from a string/image/stretch... */
19193 row->maxpos = row->minpos;
19194 else
19195 abort ();
19197 else
19198 row->maxpos = it->current.pos;
19201 /* Construct the glyph row IT->glyph_row in the desired matrix of
19202 IT->w from text at the current position of IT. See dispextern.h
19203 for an overview of struct it. Value is non-zero if
19204 IT->glyph_row displays text, as opposed to a line displaying ZV
19205 only. */
19207 static int
19208 display_line (struct it *it)
19210 struct glyph_row *row = it->glyph_row;
19211 Lisp_Object overlay_arrow_string;
19212 struct it wrap_it;
19213 void *wrap_data = NULL;
19214 int may_wrap = 0, wrap_x IF_LINT (= 0);
19215 int wrap_row_used = -1;
19216 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19217 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19218 int wrap_row_extra_line_spacing IF_LINT (= 0);
19219 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19220 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19221 int cvpos;
19222 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19223 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19225 /* We always start displaying at hpos zero even if hscrolled. */
19226 eassert (it->hpos == 0 && it->current_x == 0);
19228 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19229 >= it->w->desired_matrix->nrows)
19231 it->w->nrows_scale_factor++;
19232 fonts_changed_p = 1;
19233 return 0;
19236 /* Is IT->w showing the region? */
19237 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
19239 /* Clear the result glyph row and enable it. */
19240 prepare_desired_row (row);
19242 row->y = it->current_y;
19243 row->start = it->start;
19244 row->continuation_lines_width = it->continuation_lines_width;
19245 row->displays_text_p = 1;
19246 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19247 it->starts_in_middle_of_char_p = 0;
19249 /* Arrange the overlays nicely for our purposes. Usually, we call
19250 display_line on only one line at a time, in which case this
19251 can't really hurt too much, or we call it on lines which appear
19252 one after another in the buffer, in which case all calls to
19253 recenter_overlay_lists but the first will be pretty cheap. */
19254 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19256 /* Move over display elements that are not visible because we are
19257 hscrolled. This may stop at an x-position < IT->first_visible_x
19258 if the first glyph is partially visible or if we hit a line end. */
19259 if (it->current_x < it->first_visible_x)
19261 enum move_it_result move_result;
19263 this_line_min_pos = row->start.pos;
19264 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19265 MOVE_TO_POS | MOVE_TO_X);
19266 /* If we are under a large hscroll, move_it_in_display_line_to
19267 could hit the end of the line without reaching
19268 it->first_visible_x. Pretend that we did reach it. This is
19269 especially important on a TTY, where we will call
19270 extend_face_to_end_of_line, which needs to know how many
19271 blank glyphs to produce. */
19272 if (it->current_x < it->first_visible_x
19273 && (move_result == MOVE_NEWLINE_OR_CR
19274 || move_result == MOVE_POS_MATCH_OR_ZV))
19275 it->current_x = it->first_visible_x;
19277 /* Record the smallest positions seen while we moved over
19278 display elements that are not visible. This is needed by
19279 redisplay_internal for optimizing the case where the cursor
19280 stays inside the same line. The rest of this function only
19281 considers positions that are actually displayed, so
19282 RECORD_MAX_MIN_POS will not otherwise record positions that
19283 are hscrolled to the left of the left edge of the window. */
19284 min_pos = CHARPOS (this_line_min_pos);
19285 min_bpos = BYTEPOS (this_line_min_pos);
19287 else
19289 /* We only do this when not calling `move_it_in_display_line_to'
19290 above, because move_it_in_display_line_to calls
19291 handle_line_prefix itself. */
19292 handle_line_prefix (it);
19295 /* Get the initial row height. This is either the height of the
19296 text hscrolled, if there is any, or zero. */
19297 row->ascent = it->max_ascent;
19298 row->height = it->max_ascent + it->max_descent;
19299 row->phys_ascent = it->max_phys_ascent;
19300 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19301 row->extra_line_spacing = it->max_extra_line_spacing;
19303 /* Utility macro to record max and min buffer positions seen until now. */
19304 #define RECORD_MAX_MIN_POS(IT) \
19305 do \
19307 int composition_p = !STRINGP ((IT)->string) \
19308 && ((IT)->what == IT_COMPOSITION); \
19309 ptrdiff_t current_pos = \
19310 composition_p ? (IT)->cmp_it.charpos \
19311 : IT_CHARPOS (*(IT)); \
19312 ptrdiff_t current_bpos = \
19313 composition_p ? CHAR_TO_BYTE (current_pos) \
19314 : IT_BYTEPOS (*(IT)); \
19315 if (current_pos < min_pos) \
19317 min_pos = current_pos; \
19318 min_bpos = current_bpos; \
19320 if (IT_CHARPOS (*it) > max_pos) \
19322 max_pos = IT_CHARPOS (*it); \
19323 max_bpos = IT_BYTEPOS (*it); \
19326 while (0)
19328 /* Loop generating characters. The loop is left with IT on the next
19329 character to display. */
19330 while (1)
19332 int n_glyphs_before, hpos_before, x_before;
19333 int x, nglyphs;
19334 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19336 /* Retrieve the next thing to display. Value is zero if end of
19337 buffer reached. */
19338 if (!get_next_display_element (it))
19340 /* Maybe add a space at the end of this line that is used to
19341 display the cursor there under X. Set the charpos of the
19342 first glyph of blank lines not corresponding to any text
19343 to -1. */
19344 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19345 row->exact_window_width_line_p = 1;
19346 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19347 || row->used[TEXT_AREA] == 0)
19349 row->glyphs[TEXT_AREA]->charpos = -1;
19350 row->displays_text_p = 0;
19352 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19353 && (!MINI_WINDOW_P (it->w)
19354 || (minibuf_level && EQ (it->window, minibuf_window))))
19355 row->indicate_empty_line_p = 1;
19358 it->continuation_lines_width = 0;
19359 row->ends_at_zv_p = 1;
19360 /* A row that displays right-to-left text must always have
19361 its last face extended all the way to the end of line,
19362 even if this row ends in ZV, because we still write to
19363 the screen left to right. We also need to extend the
19364 last face if the default face is remapped to some
19365 different face, otherwise the functions that clear
19366 portions of the screen will clear with the default face's
19367 background color. */
19368 if (row->reversed_p
19369 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19370 extend_face_to_end_of_line (it);
19371 break;
19374 /* Now, get the metrics of what we want to display. This also
19375 generates glyphs in `row' (which is IT->glyph_row). */
19376 n_glyphs_before = row->used[TEXT_AREA];
19377 x = it->current_x;
19379 /* Remember the line height so far in case the next element doesn't
19380 fit on the line. */
19381 if (it->line_wrap != TRUNCATE)
19383 ascent = it->max_ascent;
19384 descent = it->max_descent;
19385 phys_ascent = it->max_phys_ascent;
19386 phys_descent = it->max_phys_descent;
19388 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19390 if (IT_DISPLAYING_WHITESPACE (it))
19391 may_wrap = 1;
19392 else if (may_wrap)
19394 SAVE_IT (wrap_it, *it, wrap_data);
19395 wrap_x = x;
19396 wrap_row_used = row->used[TEXT_AREA];
19397 wrap_row_ascent = row->ascent;
19398 wrap_row_height = row->height;
19399 wrap_row_phys_ascent = row->phys_ascent;
19400 wrap_row_phys_height = row->phys_height;
19401 wrap_row_extra_line_spacing = row->extra_line_spacing;
19402 wrap_row_min_pos = min_pos;
19403 wrap_row_min_bpos = min_bpos;
19404 wrap_row_max_pos = max_pos;
19405 wrap_row_max_bpos = max_bpos;
19406 may_wrap = 0;
19411 PRODUCE_GLYPHS (it);
19413 /* If this display element was in marginal areas, continue with
19414 the next one. */
19415 if (it->area != TEXT_AREA)
19417 row->ascent = max (row->ascent, it->max_ascent);
19418 row->height = max (row->height, it->max_ascent + it->max_descent);
19419 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19420 row->phys_height = max (row->phys_height,
19421 it->max_phys_ascent + it->max_phys_descent);
19422 row->extra_line_spacing = max (row->extra_line_spacing,
19423 it->max_extra_line_spacing);
19424 set_iterator_to_next (it, 1);
19425 continue;
19428 /* Does the display element fit on the line? If we truncate
19429 lines, we should draw past the right edge of the window. If
19430 we don't truncate, we want to stop so that we can display the
19431 continuation glyph before the right margin. If lines are
19432 continued, there are two possible strategies for characters
19433 resulting in more than 1 glyph (e.g. tabs): Display as many
19434 glyphs as possible in this line and leave the rest for the
19435 continuation line, or display the whole element in the next
19436 line. Original redisplay did the former, so we do it also. */
19437 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19438 hpos_before = it->hpos;
19439 x_before = x;
19441 if (/* Not a newline. */
19442 nglyphs > 0
19443 /* Glyphs produced fit entirely in the line. */
19444 && it->current_x < it->last_visible_x)
19446 it->hpos += nglyphs;
19447 row->ascent = max (row->ascent, it->max_ascent);
19448 row->height = max (row->height, it->max_ascent + it->max_descent);
19449 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19450 row->phys_height = max (row->phys_height,
19451 it->max_phys_ascent + it->max_phys_descent);
19452 row->extra_line_spacing = max (row->extra_line_spacing,
19453 it->max_extra_line_spacing);
19454 if (it->current_x - it->pixel_width < it->first_visible_x)
19455 row->x = x - it->first_visible_x;
19456 /* Record the maximum and minimum buffer positions seen so
19457 far in glyphs that will be displayed by this row. */
19458 if (it->bidi_p)
19459 RECORD_MAX_MIN_POS (it);
19461 else
19463 int i, new_x;
19464 struct glyph *glyph;
19466 for (i = 0; i < nglyphs; ++i, x = new_x)
19468 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19469 new_x = x + glyph->pixel_width;
19471 if (/* Lines are continued. */
19472 it->line_wrap != TRUNCATE
19473 && (/* Glyph doesn't fit on the line. */
19474 new_x > it->last_visible_x
19475 /* Or it fits exactly on a window system frame. */
19476 || (new_x == it->last_visible_x
19477 && FRAME_WINDOW_P (it->f)
19478 && (row->reversed_p
19479 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19480 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19482 /* End of a continued line. */
19484 if (it->hpos == 0
19485 || (new_x == it->last_visible_x
19486 && FRAME_WINDOW_P (it->f)
19487 && (row->reversed_p
19488 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19489 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19491 /* Current glyph is the only one on the line or
19492 fits exactly on the line. We must continue
19493 the line because we can't draw the cursor
19494 after the glyph. */
19495 row->continued_p = 1;
19496 it->current_x = new_x;
19497 it->continuation_lines_width += new_x;
19498 ++it->hpos;
19499 if (i == nglyphs - 1)
19501 /* If line-wrap is on, check if a previous
19502 wrap point was found. */
19503 if (wrap_row_used > 0
19504 /* Even if there is a previous wrap
19505 point, continue the line here as
19506 usual, if (i) the previous character
19507 was a space or tab AND (ii) the
19508 current character is not. */
19509 && (!may_wrap
19510 || IT_DISPLAYING_WHITESPACE (it)))
19511 goto back_to_wrap;
19513 /* Record the maximum and minimum buffer
19514 positions seen so far in glyphs that will be
19515 displayed by this row. */
19516 if (it->bidi_p)
19517 RECORD_MAX_MIN_POS (it);
19518 set_iterator_to_next (it, 1);
19519 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19521 if (!get_next_display_element (it))
19523 row->exact_window_width_line_p = 1;
19524 it->continuation_lines_width = 0;
19525 row->continued_p = 0;
19526 row->ends_at_zv_p = 1;
19528 else if (ITERATOR_AT_END_OF_LINE_P (it))
19530 row->continued_p = 0;
19531 row->exact_window_width_line_p = 1;
19535 else if (it->bidi_p)
19536 RECORD_MAX_MIN_POS (it);
19538 else if (CHAR_GLYPH_PADDING_P (*glyph)
19539 && !FRAME_WINDOW_P (it->f))
19541 /* A padding glyph that doesn't fit on this line.
19542 This means the whole character doesn't fit
19543 on the line. */
19544 if (row->reversed_p)
19545 unproduce_glyphs (it, row->used[TEXT_AREA]
19546 - n_glyphs_before);
19547 row->used[TEXT_AREA] = n_glyphs_before;
19549 /* Fill the rest of the row with continuation
19550 glyphs like in 20.x. */
19551 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19552 < row->glyphs[1 + TEXT_AREA])
19553 produce_special_glyphs (it, IT_CONTINUATION);
19555 row->continued_p = 1;
19556 it->current_x = x_before;
19557 it->continuation_lines_width += x_before;
19559 /* Restore the height to what it was before the
19560 element not fitting on the line. */
19561 it->max_ascent = ascent;
19562 it->max_descent = descent;
19563 it->max_phys_ascent = phys_ascent;
19564 it->max_phys_descent = phys_descent;
19566 else if (wrap_row_used > 0)
19568 back_to_wrap:
19569 if (row->reversed_p)
19570 unproduce_glyphs (it,
19571 row->used[TEXT_AREA] - wrap_row_used);
19572 RESTORE_IT (it, &wrap_it, wrap_data);
19573 it->continuation_lines_width += wrap_x;
19574 row->used[TEXT_AREA] = wrap_row_used;
19575 row->ascent = wrap_row_ascent;
19576 row->height = wrap_row_height;
19577 row->phys_ascent = wrap_row_phys_ascent;
19578 row->phys_height = wrap_row_phys_height;
19579 row->extra_line_spacing = wrap_row_extra_line_spacing;
19580 min_pos = wrap_row_min_pos;
19581 min_bpos = wrap_row_min_bpos;
19582 max_pos = wrap_row_max_pos;
19583 max_bpos = wrap_row_max_bpos;
19584 row->continued_p = 1;
19585 row->ends_at_zv_p = 0;
19586 row->exact_window_width_line_p = 0;
19587 it->continuation_lines_width += x;
19589 /* Make sure that a non-default face is extended
19590 up to the right margin of the window. */
19591 extend_face_to_end_of_line (it);
19593 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19595 /* A TAB that extends past the right edge of the
19596 window. This produces a single glyph on
19597 window system frames. We leave the glyph in
19598 this row and let it fill the row, but don't
19599 consume the TAB. */
19600 if ((row->reversed_p
19601 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19602 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19603 produce_special_glyphs (it, IT_CONTINUATION);
19604 it->continuation_lines_width += it->last_visible_x;
19605 row->ends_in_middle_of_char_p = 1;
19606 row->continued_p = 1;
19607 glyph->pixel_width = it->last_visible_x - x;
19608 it->starts_in_middle_of_char_p = 1;
19610 else
19612 /* Something other than a TAB that draws past
19613 the right edge of the window. Restore
19614 positions to values before the element. */
19615 if (row->reversed_p)
19616 unproduce_glyphs (it, row->used[TEXT_AREA]
19617 - (n_glyphs_before + i));
19618 row->used[TEXT_AREA] = n_glyphs_before + i;
19620 /* Display continuation glyphs. */
19621 if (!FRAME_WINDOW_P (it->f)
19622 || (row->reversed_p
19623 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19624 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19625 produce_special_glyphs (it, IT_CONTINUATION);
19626 row->continued_p = 1;
19628 it->current_x = x_before;
19629 it->continuation_lines_width += x;
19630 extend_face_to_end_of_line (it);
19632 if (nglyphs > 1 && i > 0)
19634 row->ends_in_middle_of_char_p = 1;
19635 it->starts_in_middle_of_char_p = 1;
19638 /* Restore the height to what it was before the
19639 element not fitting on the line. */
19640 it->max_ascent = ascent;
19641 it->max_descent = descent;
19642 it->max_phys_ascent = phys_ascent;
19643 it->max_phys_descent = phys_descent;
19646 break;
19648 else if (new_x > it->first_visible_x)
19650 /* Increment number of glyphs actually displayed. */
19651 ++it->hpos;
19653 /* Record the maximum and minimum buffer positions
19654 seen so far in glyphs that will be displayed by
19655 this row. */
19656 if (it->bidi_p)
19657 RECORD_MAX_MIN_POS (it);
19659 if (x < it->first_visible_x)
19660 /* Glyph is partially visible, i.e. row starts at
19661 negative X position. */
19662 row->x = x - it->first_visible_x;
19664 else
19666 /* Glyph is completely off the left margin of the
19667 window. This should not happen because of the
19668 move_it_in_display_line at the start of this
19669 function, unless the text display area of the
19670 window is empty. */
19671 eassert (it->first_visible_x <= it->last_visible_x);
19674 /* Even if this display element produced no glyphs at all,
19675 we want to record its position. */
19676 if (it->bidi_p && nglyphs == 0)
19677 RECORD_MAX_MIN_POS (it);
19679 row->ascent = max (row->ascent, it->max_ascent);
19680 row->height = max (row->height, it->max_ascent + it->max_descent);
19681 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19682 row->phys_height = max (row->phys_height,
19683 it->max_phys_ascent + it->max_phys_descent);
19684 row->extra_line_spacing = max (row->extra_line_spacing,
19685 it->max_extra_line_spacing);
19687 /* End of this display line if row is continued. */
19688 if (row->continued_p || row->ends_at_zv_p)
19689 break;
19692 at_end_of_line:
19693 /* Is this a line end? If yes, we're also done, after making
19694 sure that a non-default face is extended up to the right
19695 margin of the window. */
19696 if (ITERATOR_AT_END_OF_LINE_P (it))
19698 int used_before = row->used[TEXT_AREA];
19700 row->ends_in_newline_from_string_p = STRINGP (it->object);
19702 /* Add a space at the end of the line that is used to
19703 display the cursor there. */
19704 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19705 append_space_for_newline (it, 0);
19707 /* Extend the face to the end of the line. */
19708 extend_face_to_end_of_line (it);
19710 /* Make sure we have the position. */
19711 if (used_before == 0)
19712 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19714 /* Record the position of the newline, for use in
19715 find_row_edges. */
19716 it->eol_pos = it->current.pos;
19718 /* Consume the line end. This skips over invisible lines. */
19719 set_iterator_to_next (it, 1);
19720 it->continuation_lines_width = 0;
19721 break;
19724 /* Proceed with next display element. Note that this skips
19725 over lines invisible because of selective display. */
19726 set_iterator_to_next (it, 1);
19728 /* If we truncate lines, we are done when the last displayed
19729 glyphs reach past the right margin of the window. */
19730 if (it->line_wrap == TRUNCATE
19731 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19732 ? (it->current_x >= it->last_visible_x)
19733 : (it->current_x > it->last_visible_x)))
19735 /* Maybe add truncation glyphs. */
19736 if (!FRAME_WINDOW_P (it->f)
19737 || (row->reversed_p
19738 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19739 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19741 int i, n;
19743 if (!row->reversed_p)
19745 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19746 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19747 break;
19749 else
19751 for (i = 0; i < row->used[TEXT_AREA]; i++)
19752 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19753 break;
19754 /* Remove any padding glyphs at the front of ROW, to
19755 make room for the truncation glyphs we will be
19756 adding below. The loop below always inserts at
19757 least one truncation glyph, so also remove the
19758 last glyph added to ROW. */
19759 unproduce_glyphs (it, i + 1);
19760 /* Adjust i for the loop below. */
19761 i = row->used[TEXT_AREA] - (i + 1);
19764 it->current_x = x_before;
19765 if (!FRAME_WINDOW_P (it->f))
19767 for (n = row->used[TEXT_AREA]; i < n; ++i)
19769 row->used[TEXT_AREA] = i;
19770 produce_special_glyphs (it, IT_TRUNCATION);
19773 #ifdef HAVE_WINDOW_SYSTEM
19774 else
19776 /* On a GUI frame, when the right fringe (left
19777 fringe for R2L rows) is turned off, we produce
19778 truncation glyphs preceded by a stretch glyph
19779 whose width is computed such that the truncation
19780 glyphs are aligned at the window margin, even
19781 when very different fonts are used in different
19782 glyph rows. */
19783 int stretch_width = it->last_visible_x - it->current_x;
19785 row->used[TEXT_AREA] = i;
19786 if (stretch_width > 0)
19788 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19789 struct font *font =
19790 face->font ? face->font : FRAME_FONT (it->f);
19791 int stretch_ascent =
19792 (((it->ascent + it->descent)
19793 * FONT_BASE (font)) / FONT_HEIGHT (font));
19794 struct text_pos saved_pos = it->position;
19796 memset (&it->position, 0, sizeof it->position);
19797 append_stretch_glyph (it, make_number (0), stretch_width,
19798 it->ascent + it->descent,
19799 stretch_ascent);
19800 it->position = saved_pos;
19802 produce_special_glyphs (it, IT_TRUNCATION);
19804 #endif
19806 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19808 /* Don't truncate if we can overflow newline into fringe. */
19809 if (!get_next_display_element (it))
19811 it->continuation_lines_width = 0;
19812 row->ends_at_zv_p = 1;
19813 row->exact_window_width_line_p = 1;
19814 break;
19816 if (ITERATOR_AT_END_OF_LINE_P (it))
19818 row->exact_window_width_line_p = 1;
19819 goto at_end_of_line;
19821 it->current_x = x_before;
19824 row->truncated_on_right_p = 1;
19825 it->continuation_lines_width = 0;
19826 reseat_at_next_visible_line_start (it, 0);
19827 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19828 it->hpos = hpos_before;
19829 break;
19833 if (wrap_data)
19834 bidi_unshelve_cache (wrap_data, 1);
19836 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19837 at the left window margin. */
19838 if (it->first_visible_x
19839 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19841 if (!FRAME_WINDOW_P (it->f)
19842 || (row->reversed_p
19843 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19844 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19845 insert_left_trunc_glyphs (it);
19846 row->truncated_on_left_p = 1;
19849 /* Remember the position at which this line ends.
19851 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19852 cannot be before the call to find_row_edges below, since that is
19853 where these positions are determined. */
19854 row->end = it->current;
19855 if (!it->bidi_p)
19857 row->minpos = row->start.pos;
19858 row->maxpos = row->end.pos;
19860 else
19862 /* ROW->minpos and ROW->maxpos must be the smallest and
19863 `1 + the largest' buffer positions in ROW. But if ROW was
19864 bidi-reordered, these two positions can be anywhere in the
19865 row, so we must determine them now. */
19866 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19869 /* If the start of this line is the overlay arrow-position, then
19870 mark this glyph row as the one containing the overlay arrow.
19871 This is clearly a mess with variable size fonts. It would be
19872 better to let it be displayed like cursors under X. */
19873 if ((row->displays_text_p || !overlay_arrow_seen)
19874 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19875 !NILP (overlay_arrow_string)))
19877 /* Overlay arrow in window redisplay is a fringe bitmap. */
19878 if (STRINGP (overlay_arrow_string))
19880 struct glyph_row *arrow_row
19881 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19882 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19883 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19884 struct glyph *p = row->glyphs[TEXT_AREA];
19885 struct glyph *p2, *end;
19887 /* Copy the arrow glyphs. */
19888 while (glyph < arrow_end)
19889 *p++ = *glyph++;
19891 /* Throw away padding glyphs. */
19892 p2 = p;
19893 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19894 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19895 ++p2;
19896 if (p2 > p)
19898 while (p2 < end)
19899 *p++ = *p2++;
19900 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19903 else
19905 eassert (INTEGERP (overlay_arrow_string));
19906 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19908 overlay_arrow_seen = 1;
19911 /* Highlight trailing whitespace. */
19912 if (!NILP (Vshow_trailing_whitespace))
19913 highlight_trailing_whitespace (it->f, it->glyph_row);
19915 /* Compute pixel dimensions of this line. */
19916 compute_line_metrics (it);
19918 /* Implementation note: No changes in the glyphs of ROW or in their
19919 faces can be done past this point, because compute_line_metrics
19920 computes ROW's hash value and stores it within the glyph_row
19921 structure. */
19923 /* Record whether this row ends inside an ellipsis. */
19924 row->ends_in_ellipsis_p
19925 = (it->method == GET_FROM_DISPLAY_VECTOR
19926 && it->ellipsis_p);
19928 /* Save fringe bitmaps in this row. */
19929 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19930 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19931 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19932 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19934 it->left_user_fringe_bitmap = 0;
19935 it->left_user_fringe_face_id = 0;
19936 it->right_user_fringe_bitmap = 0;
19937 it->right_user_fringe_face_id = 0;
19939 /* Maybe set the cursor. */
19940 cvpos = it->w->cursor.vpos;
19941 if ((cvpos < 0
19942 /* In bidi-reordered rows, keep checking for proper cursor
19943 position even if one has been found already, because buffer
19944 positions in such rows change non-linearly with ROW->VPOS,
19945 when a line is continued. One exception: when we are at ZV,
19946 display cursor on the first suitable glyph row, since all
19947 the empty rows after that also have their position set to ZV. */
19948 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19949 lines' rows is implemented for bidi-reordered rows. */
19950 || (it->bidi_p
19951 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19952 && PT >= MATRIX_ROW_START_CHARPOS (row)
19953 && PT <= MATRIX_ROW_END_CHARPOS (row)
19954 && cursor_row_p (row))
19955 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19957 /* Prepare for the next line. This line starts horizontally at (X
19958 HPOS) = (0 0). Vertical positions are incremented. As a
19959 convenience for the caller, IT->glyph_row is set to the next
19960 row to be used. */
19961 it->current_x = it->hpos = 0;
19962 it->current_y += row->height;
19963 SET_TEXT_POS (it->eol_pos, 0, 0);
19964 ++it->vpos;
19965 ++it->glyph_row;
19966 /* The next row should by default use the same value of the
19967 reversed_p flag as this one. set_iterator_to_next decides when
19968 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19969 the flag accordingly. */
19970 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19971 it->glyph_row->reversed_p = row->reversed_p;
19972 it->start = row->end;
19973 return row->displays_text_p;
19975 #undef RECORD_MAX_MIN_POS
19978 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19979 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19980 doc: /* Return paragraph direction at point in BUFFER.
19981 Value is either `left-to-right' or `right-to-left'.
19982 If BUFFER is omitted or nil, it defaults to the current buffer.
19984 Paragraph direction determines how the text in the paragraph is displayed.
19985 In left-to-right paragraphs, text begins at the left margin of the window
19986 and the reading direction is generally left to right. In right-to-left
19987 paragraphs, text begins at the right margin and is read from right to left.
19989 See also `bidi-paragraph-direction'. */)
19990 (Lisp_Object buffer)
19992 struct buffer *buf = current_buffer;
19993 struct buffer *old = buf;
19995 if (! NILP (buffer))
19997 CHECK_BUFFER (buffer);
19998 buf = XBUFFER (buffer);
20001 if (NILP (BVAR (buf, bidi_display_reordering))
20002 || NILP (BVAR (buf, enable_multibyte_characters))
20003 /* When we are loading loadup.el, the character property tables
20004 needed for bidi iteration are not yet available. */
20005 || !NILP (Vpurify_flag))
20006 return Qleft_to_right;
20007 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20008 return BVAR (buf, bidi_paragraph_direction);
20009 else
20011 /* Determine the direction from buffer text. We could try to
20012 use current_matrix if it is up to date, but this seems fast
20013 enough as it is. */
20014 struct bidi_it itb;
20015 ptrdiff_t pos = BUF_PT (buf);
20016 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20017 int c;
20018 void *itb_data = bidi_shelve_cache ();
20020 set_buffer_temp (buf);
20021 /* bidi_paragraph_init finds the base direction of the paragraph
20022 by searching forward from paragraph start. We need the base
20023 direction of the current or _previous_ paragraph, so we need
20024 to make sure we are within that paragraph. To that end, find
20025 the previous non-empty line. */
20026 if (pos >= ZV && pos > BEGV)
20028 pos--;
20029 bytepos = CHAR_TO_BYTE (pos);
20031 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20032 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20034 while ((c = FETCH_BYTE (bytepos)) == '\n'
20035 || c == ' ' || c == '\t' || c == '\f')
20037 if (bytepos <= BEGV_BYTE)
20038 break;
20039 bytepos--;
20040 pos--;
20042 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20043 bytepos--;
20045 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20046 itb.paragraph_dir = NEUTRAL_DIR;
20047 itb.string.s = NULL;
20048 itb.string.lstring = Qnil;
20049 itb.string.bufpos = 0;
20050 itb.string.unibyte = 0;
20051 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20052 bidi_unshelve_cache (itb_data, 0);
20053 set_buffer_temp (old);
20054 switch (itb.paragraph_dir)
20056 case L2R:
20057 return Qleft_to_right;
20058 break;
20059 case R2L:
20060 return Qright_to_left;
20061 break;
20062 default:
20063 abort ();
20070 /***********************************************************************
20071 Menu Bar
20072 ***********************************************************************/
20074 /* Redisplay the menu bar in the frame for window W.
20076 The menu bar of X frames that don't have X toolkit support is
20077 displayed in a special window W->frame->menu_bar_window.
20079 The menu bar of terminal frames is treated specially as far as
20080 glyph matrices are concerned. Menu bar lines are not part of
20081 windows, so the update is done directly on the frame matrix rows
20082 for the menu bar. */
20084 static void
20085 display_menu_bar (struct window *w)
20087 struct frame *f = XFRAME (WINDOW_FRAME (w));
20088 struct it it;
20089 Lisp_Object items;
20090 int i;
20092 /* Don't do all this for graphical frames. */
20093 #ifdef HAVE_NTGUI
20094 if (FRAME_W32_P (f))
20095 return;
20096 #endif
20097 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20098 if (FRAME_X_P (f))
20099 return;
20100 #endif
20102 #ifdef HAVE_NS
20103 if (FRAME_NS_P (f))
20104 return;
20105 #endif /* HAVE_NS */
20107 #ifdef USE_X_TOOLKIT
20108 eassert (!FRAME_WINDOW_P (f));
20109 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20110 it.first_visible_x = 0;
20111 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20112 #else /* not USE_X_TOOLKIT */
20113 if (FRAME_WINDOW_P (f))
20115 /* Menu bar lines are displayed in the desired matrix of the
20116 dummy window menu_bar_window. */
20117 struct window *menu_w;
20118 eassert (WINDOWP (f->menu_bar_window));
20119 menu_w = XWINDOW (f->menu_bar_window);
20120 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20121 MENU_FACE_ID);
20122 it.first_visible_x = 0;
20123 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20125 else
20127 /* This is a TTY frame, i.e. character hpos/vpos are used as
20128 pixel x/y. */
20129 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20130 MENU_FACE_ID);
20131 it.first_visible_x = 0;
20132 it.last_visible_x = FRAME_COLS (f);
20134 #endif /* not USE_X_TOOLKIT */
20136 /* FIXME: This should be controlled by a user option. See the
20137 comments in redisplay_tool_bar and display_mode_line about
20138 this. */
20139 it.paragraph_embedding = L2R;
20141 if (! mode_line_inverse_video)
20142 /* Force the menu-bar to be displayed in the default face. */
20143 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20145 /* Clear all rows of the menu bar. */
20146 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20148 struct glyph_row *row = it.glyph_row + i;
20149 clear_glyph_row (row);
20150 row->enabled_p = 1;
20151 row->full_width_p = 1;
20154 /* Display all items of the menu bar. */
20155 items = FRAME_MENU_BAR_ITEMS (it.f);
20156 for (i = 0; i < ASIZE (items); i += 4)
20158 Lisp_Object string;
20160 /* Stop at nil string. */
20161 string = AREF (items, i + 1);
20162 if (NILP (string))
20163 break;
20165 /* Remember where item was displayed. */
20166 ASET (items, i + 3, make_number (it.hpos));
20168 /* Display the item, pad with one space. */
20169 if (it.current_x < it.last_visible_x)
20170 display_string (NULL, string, Qnil, 0, 0, &it,
20171 SCHARS (string) + 1, 0, 0, -1);
20174 /* Fill out the line with spaces. */
20175 if (it.current_x < it.last_visible_x)
20176 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20178 /* Compute the total height of the lines. */
20179 compute_line_metrics (&it);
20184 /***********************************************************************
20185 Mode Line
20186 ***********************************************************************/
20188 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20189 FORCE is non-zero, redisplay mode lines unconditionally.
20190 Otherwise, redisplay only mode lines that are garbaged. Value is
20191 the number of windows whose mode lines were redisplayed. */
20193 static int
20194 redisplay_mode_lines (Lisp_Object window, int force)
20196 int nwindows = 0;
20198 while (!NILP (window))
20200 struct window *w = XWINDOW (window);
20202 if (WINDOWP (w->hchild))
20203 nwindows += redisplay_mode_lines (w->hchild, force);
20204 else if (WINDOWP (w->vchild))
20205 nwindows += redisplay_mode_lines (w->vchild, force);
20206 else if (force
20207 || FRAME_GARBAGED_P (XFRAME (w->frame))
20208 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20210 struct text_pos lpoint;
20211 struct buffer *old = current_buffer;
20213 /* Set the window's buffer for the mode line display. */
20214 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20215 set_buffer_internal_1 (XBUFFER (w->buffer));
20217 /* Point refers normally to the selected window. For any
20218 other window, set up appropriate value. */
20219 if (!EQ (window, selected_window))
20221 struct text_pos pt;
20223 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20224 if (CHARPOS (pt) < BEGV)
20225 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20226 else if (CHARPOS (pt) > (ZV - 1))
20227 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20228 else
20229 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20232 /* Display mode lines. */
20233 clear_glyph_matrix (w->desired_matrix);
20234 if (display_mode_lines (w))
20236 ++nwindows;
20237 w->must_be_updated_p = 1;
20240 /* Restore old settings. */
20241 set_buffer_internal_1 (old);
20242 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20245 window = w->next;
20248 return nwindows;
20252 /* Display the mode and/or header line of window W. Value is the
20253 sum number of mode lines and header lines displayed. */
20255 static int
20256 display_mode_lines (struct window *w)
20258 Lisp_Object old_selected_window, old_selected_frame;
20259 int n = 0;
20261 old_selected_frame = selected_frame;
20262 selected_frame = w->frame;
20263 old_selected_window = selected_window;
20264 XSETWINDOW (selected_window, w);
20266 /* These will be set while the mode line specs are processed. */
20267 line_number_displayed = 0;
20268 w->column_number_displayed = Qnil;
20270 if (WINDOW_WANTS_MODELINE_P (w))
20272 struct window *sel_w = XWINDOW (old_selected_window);
20274 /* Select mode line face based on the real selected window. */
20275 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20276 BVAR (current_buffer, mode_line_format));
20277 ++n;
20280 if (WINDOW_WANTS_HEADER_LINE_P (w))
20282 display_mode_line (w, HEADER_LINE_FACE_ID,
20283 BVAR (current_buffer, header_line_format));
20284 ++n;
20287 selected_frame = old_selected_frame;
20288 selected_window = old_selected_window;
20289 return n;
20293 /* Display mode or header line of window W. FACE_ID specifies which
20294 line to display; it is either MODE_LINE_FACE_ID or
20295 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20296 display. Value is the pixel height of the mode/header line
20297 displayed. */
20299 static int
20300 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20302 struct it it;
20303 struct face *face;
20304 ptrdiff_t count = SPECPDL_INDEX ();
20306 init_iterator (&it, w, -1, -1, NULL, face_id);
20307 /* Don't extend on a previously drawn mode-line.
20308 This may happen if called from pos_visible_p. */
20309 it.glyph_row->enabled_p = 0;
20310 prepare_desired_row (it.glyph_row);
20312 it.glyph_row->mode_line_p = 1;
20314 if (! mode_line_inverse_video)
20315 /* Force the mode-line to be displayed in the default face. */
20316 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20318 /* FIXME: This should be controlled by a user option. But
20319 supporting such an option is not trivial, since the mode line is
20320 made up of many separate strings. */
20321 it.paragraph_embedding = L2R;
20323 record_unwind_protect (unwind_format_mode_line,
20324 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20326 mode_line_target = MODE_LINE_DISPLAY;
20328 /* Temporarily make frame's keyboard the current kboard so that
20329 kboard-local variables in the mode_line_format will get the right
20330 values. */
20331 push_kboard (FRAME_KBOARD (it.f));
20332 record_unwind_save_match_data ();
20333 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20334 pop_kboard ();
20336 unbind_to (count, Qnil);
20338 /* Fill up with spaces. */
20339 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20341 compute_line_metrics (&it);
20342 it.glyph_row->full_width_p = 1;
20343 it.glyph_row->continued_p = 0;
20344 it.glyph_row->truncated_on_left_p = 0;
20345 it.glyph_row->truncated_on_right_p = 0;
20347 /* Make a 3D mode-line have a shadow at its right end. */
20348 face = FACE_FROM_ID (it.f, face_id);
20349 extend_face_to_end_of_line (&it);
20350 if (face->box != FACE_NO_BOX)
20352 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20353 + it.glyph_row->used[TEXT_AREA] - 1);
20354 last->right_box_line_p = 1;
20357 return it.glyph_row->height;
20360 /* Move element ELT in LIST to the front of LIST.
20361 Return the updated list. */
20363 static Lisp_Object
20364 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20366 register Lisp_Object tail, prev;
20367 register Lisp_Object tem;
20369 tail = list;
20370 prev = Qnil;
20371 while (CONSP (tail))
20373 tem = XCAR (tail);
20375 if (EQ (elt, tem))
20377 /* Splice out the link TAIL. */
20378 if (NILP (prev))
20379 list = XCDR (tail);
20380 else
20381 Fsetcdr (prev, XCDR (tail));
20383 /* Now make it the first. */
20384 Fsetcdr (tail, list);
20385 return tail;
20387 else
20388 prev = tail;
20389 tail = XCDR (tail);
20390 QUIT;
20393 /* Not found--return unchanged LIST. */
20394 return list;
20397 /* Contribute ELT to the mode line for window IT->w. How it
20398 translates into text depends on its data type.
20400 IT describes the display environment in which we display, as usual.
20402 DEPTH is the depth in recursion. It is used to prevent
20403 infinite recursion here.
20405 FIELD_WIDTH is the number of characters the display of ELT should
20406 occupy in the mode line, and PRECISION is the maximum number of
20407 characters to display from ELT's representation. See
20408 display_string for details.
20410 Returns the hpos of the end of the text generated by ELT.
20412 PROPS is a property list to add to any string we encounter.
20414 If RISKY is nonzero, remove (disregard) any properties in any string
20415 we encounter, and ignore :eval and :propertize.
20417 The global variable `mode_line_target' determines whether the
20418 output is passed to `store_mode_line_noprop',
20419 `store_mode_line_string', or `display_string'. */
20421 static int
20422 display_mode_element (struct it *it, int depth, int field_width, int precision,
20423 Lisp_Object elt, Lisp_Object props, int risky)
20425 int n = 0, field, prec;
20426 int literal = 0;
20428 tail_recurse:
20429 if (depth > 100)
20430 elt = build_string ("*too-deep*");
20432 depth++;
20434 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
20436 case Lisp_String:
20438 /* A string: output it and check for %-constructs within it. */
20439 unsigned char c;
20440 ptrdiff_t offset = 0;
20442 if (SCHARS (elt) > 0
20443 && (!NILP (props) || risky))
20445 Lisp_Object oprops, aelt;
20446 oprops = Ftext_properties_at (make_number (0), elt);
20448 /* If the starting string's properties are not what
20449 we want, translate the string. Also, if the string
20450 is risky, do that anyway. */
20452 if (NILP (Fequal (props, oprops)) || risky)
20454 /* If the starting string has properties,
20455 merge the specified ones onto the existing ones. */
20456 if (! NILP (oprops) && !risky)
20458 Lisp_Object tem;
20460 oprops = Fcopy_sequence (oprops);
20461 tem = props;
20462 while (CONSP (tem))
20464 oprops = Fplist_put (oprops, XCAR (tem),
20465 XCAR (XCDR (tem)));
20466 tem = XCDR (XCDR (tem));
20468 props = oprops;
20471 aelt = Fassoc (elt, mode_line_proptrans_alist);
20472 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20474 /* AELT is what we want. Move it to the front
20475 without consing. */
20476 elt = XCAR (aelt);
20477 mode_line_proptrans_alist
20478 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20480 else
20482 Lisp_Object tem;
20484 /* If AELT has the wrong props, it is useless.
20485 so get rid of it. */
20486 if (! NILP (aelt))
20487 mode_line_proptrans_alist
20488 = Fdelq (aelt, mode_line_proptrans_alist);
20490 elt = Fcopy_sequence (elt);
20491 Fset_text_properties (make_number (0), Flength (elt),
20492 props, elt);
20493 /* Add this item to mode_line_proptrans_alist. */
20494 mode_line_proptrans_alist
20495 = Fcons (Fcons (elt, props),
20496 mode_line_proptrans_alist);
20497 /* Truncate mode_line_proptrans_alist
20498 to at most 50 elements. */
20499 tem = Fnthcdr (make_number (50),
20500 mode_line_proptrans_alist);
20501 if (! NILP (tem))
20502 XSETCDR (tem, Qnil);
20507 offset = 0;
20509 if (literal)
20511 prec = precision - n;
20512 switch (mode_line_target)
20514 case MODE_LINE_NOPROP:
20515 case MODE_LINE_TITLE:
20516 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20517 break;
20518 case MODE_LINE_STRING:
20519 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20520 break;
20521 case MODE_LINE_DISPLAY:
20522 n += display_string (NULL, elt, Qnil, 0, 0, it,
20523 0, prec, 0, STRING_MULTIBYTE (elt));
20524 break;
20527 break;
20530 /* Handle the non-literal case. */
20532 while ((precision <= 0 || n < precision)
20533 && SREF (elt, offset) != 0
20534 && (mode_line_target != MODE_LINE_DISPLAY
20535 || it->current_x < it->last_visible_x))
20537 ptrdiff_t last_offset = offset;
20539 /* Advance to end of string or next format specifier. */
20540 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20543 if (offset - 1 != last_offset)
20545 ptrdiff_t nchars, nbytes;
20547 /* Output to end of string or up to '%'. Field width
20548 is length of string. Don't output more than
20549 PRECISION allows us. */
20550 offset--;
20552 prec = c_string_width (SDATA (elt) + last_offset,
20553 offset - last_offset, precision - n,
20554 &nchars, &nbytes);
20556 switch (mode_line_target)
20558 case MODE_LINE_NOPROP:
20559 case MODE_LINE_TITLE:
20560 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20561 break;
20562 case MODE_LINE_STRING:
20564 ptrdiff_t bytepos = last_offset;
20565 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20566 ptrdiff_t endpos = (precision <= 0
20567 ? string_byte_to_char (elt, offset)
20568 : charpos + nchars);
20570 n += store_mode_line_string (NULL,
20571 Fsubstring (elt, make_number (charpos),
20572 make_number (endpos)),
20573 0, 0, 0, Qnil);
20575 break;
20576 case MODE_LINE_DISPLAY:
20578 ptrdiff_t bytepos = last_offset;
20579 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20581 if (precision <= 0)
20582 nchars = string_byte_to_char (elt, offset) - charpos;
20583 n += display_string (NULL, elt, Qnil, 0, charpos,
20584 it, 0, nchars, 0,
20585 STRING_MULTIBYTE (elt));
20587 break;
20590 else /* c == '%' */
20592 ptrdiff_t percent_position = offset;
20594 /* Get the specified minimum width. Zero means
20595 don't pad. */
20596 field = 0;
20597 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20598 field = field * 10 + c - '0';
20600 /* Don't pad beyond the total padding allowed. */
20601 if (field_width - n > 0 && field > field_width - n)
20602 field = field_width - n;
20604 /* Note that either PRECISION <= 0 or N < PRECISION. */
20605 prec = precision - n;
20607 if (c == 'M')
20608 n += display_mode_element (it, depth, field, prec,
20609 Vglobal_mode_string, props,
20610 risky);
20611 else if (c != 0)
20613 int multibyte;
20614 ptrdiff_t bytepos, charpos;
20615 const char *spec;
20616 Lisp_Object string;
20618 bytepos = percent_position;
20619 charpos = (STRING_MULTIBYTE (elt)
20620 ? string_byte_to_char (elt, bytepos)
20621 : bytepos);
20622 spec = decode_mode_spec (it->w, c, field, &string);
20623 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20625 switch (mode_line_target)
20627 case MODE_LINE_NOPROP:
20628 case MODE_LINE_TITLE:
20629 n += store_mode_line_noprop (spec, field, prec);
20630 break;
20631 case MODE_LINE_STRING:
20633 Lisp_Object tem = build_string (spec);
20634 props = Ftext_properties_at (make_number (charpos), elt);
20635 /* Should only keep face property in props */
20636 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20638 break;
20639 case MODE_LINE_DISPLAY:
20641 int nglyphs_before, nwritten;
20643 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20644 nwritten = display_string (spec, string, elt,
20645 charpos, 0, it,
20646 field, prec, 0,
20647 multibyte);
20649 /* Assign to the glyphs written above the
20650 string where the `%x' came from, position
20651 of the `%'. */
20652 if (nwritten > 0)
20654 struct glyph *glyph
20655 = (it->glyph_row->glyphs[TEXT_AREA]
20656 + nglyphs_before);
20657 int i;
20659 for (i = 0; i < nwritten; ++i)
20661 glyph[i].object = elt;
20662 glyph[i].charpos = charpos;
20665 n += nwritten;
20668 break;
20671 else /* c == 0 */
20672 break;
20676 break;
20678 case Lisp_Symbol:
20679 /* A symbol: process the value of the symbol recursively
20680 as if it appeared here directly. Avoid error if symbol void.
20681 Special case: if value of symbol is a string, output the string
20682 literally. */
20684 register Lisp_Object tem;
20686 /* If the variable is not marked as risky to set
20687 then its contents are risky to use. */
20688 if (NILP (Fget (elt, Qrisky_local_variable)))
20689 risky = 1;
20691 tem = Fboundp (elt);
20692 if (!NILP (tem))
20694 tem = Fsymbol_value (elt);
20695 /* If value is a string, output that string literally:
20696 don't check for % within it. */
20697 if (STRINGP (tem))
20698 literal = 1;
20700 if (!EQ (tem, elt))
20702 /* Give up right away for nil or t. */
20703 elt = tem;
20704 goto tail_recurse;
20708 break;
20710 case Lisp_Cons:
20712 register Lisp_Object car, tem;
20714 /* A cons cell: five distinct cases.
20715 If first element is :eval or :propertize, do something special.
20716 If first element is a string or a cons, process all the elements
20717 and effectively concatenate them.
20718 If first element is a negative number, truncate displaying cdr to
20719 at most that many characters. If positive, pad (with spaces)
20720 to at least that many characters.
20721 If first element is a symbol, process the cadr or caddr recursively
20722 according to whether the symbol's value is non-nil or nil. */
20723 car = XCAR (elt);
20724 if (EQ (car, QCeval))
20726 /* An element of the form (:eval FORM) means evaluate FORM
20727 and use the result as mode line elements. */
20729 if (risky)
20730 break;
20732 if (CONSP (XCDR (elt)))
20734 Lisp_Object spec;
20735 spec = safe_eval (XCAR (XCDR (elt)));
20736 n += display_mode_element (it, depth, field_width - n,
20737 precision - n, spec, props,
20738 risky);
20741 else if (EQ (car, QCpropertize))
20743 /* An element of the form (:propertize ELT PROPS...)
20744 means display ELT but applying properties PROPS. */
20746 if (risky)
20747 break;
20749 if (CONSP (XCDR (elt)))
20750 n += display_mode_element (it, depth, field_width - n,
20751 precision - n, XCAR (XCDR (elt)),
20752 XCDR (XCDR (elt)), risky);
20754 else if (SYMBOLP (car))
20756 tem = Fboundp (car);
20757 elt = XCDR (elt);
20758 if (!CONSP (elt))
20759 goto invalid;
20760 /* elt is now the cdr, and we know it is a cons cell.
20761 Use its car if CAR has a non-nil value. */
20762 if (!NILP (tem))
20764 tem = Fsymbol_value (car);
20765 if (!NILP (tem))
20767 elt = XCAR (elt);
20768 goto tail_recurse;
20771 /* Symbol's value is nil (or symbol is unbound)
20772 Get the cddr of the original list
20773 and if possible find the caddr and use that. */
20774 elt = XCDR (elt);
20775 if (NILP (elt))
20776 break;
20777 else if (!CONSP (elt))
20778 goto invalid;
20779 elt = XCAR (elt);
20780 goto tail_recurse;
20782 else if (INTEGERP (car))
20784 register int lim = XINT (car);
20785 elt = XCDR (elt);
20786 if (lim < 0)
20788 /* Negative int means reduce maximum width. */
20789 if (precision <= 0)
20790 precision = -lim;
20791 else
20792 precision = min (precision, -lim);
20794 else if (lim > 0)
20796 /* Padding specified. Don't let it be more than
20797 current maximum. */
20798 if (precision > 0)
20799 lim = min (precision, lim);
20801 /* If that's more padding than already wanted, queue it.
20802 But don't reduce padding already specified even if
20803 that is beyond the current truncation point. */
20804 field_width = max (lim, field_width);
20806 goto tail_recurse;
20808 else if (STRINGP (car) || CONSP (car))
20810 Lisp_Object halftail = elt;
20811 int len = 0;
20813 while (CONSP (elt)
20814 && (precision <= 0 || n < precision))
20816 n += display_mode_element (it, depth,
20817 /* Do padding only after the last
20818 element in the list. */
20819 (! CONSP (XCDR (elt))
20820 ? field_width - n
20821 : 0),
20822 precision - n, XCAR (elt),
20823 props, risky);
20824 elt = XCDR (elt);
20825 len++;
20826 if ((len & 1) == 0)
20827 halftail = XCDR (halftail);
20828 /* Check for cycle. */
20829 if (EQ (halftail, elt))
20830 break;
20834 break;
20836 default:
20837 invalid:
20838 elt = build_string ("*invalid*");
20839 goto tail_recurse;
20842 /* Pad to FIELD_WIDTH. */
20843 if (field_width > 0 && n < field_width)
20845 switch (mode_line_target)
20847 case MODE_LINE_NOPROP:
20848 case MODE_LINE_TITLE:
20849 n += store_mode_line_noprop ("", field_width - n, 0);
20850 break;
20851 case MODE_LINE_STRING:
20852 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20853 break;
20854 case MODE_LINE_DISPLAY:
20855 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20856 0, 0, 0);
20857 break;
20861 return n;
20864 /* Store a mode-line string element in mode_line_string_list.
20866 If STRING is non-null, display that C string. Otherwise, the Lisp
20867 string LISP_STRING is displayed.
20869 FIELD_WIDTH is the minimum number of output glyphs to produce.
20870 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20871 with spaces. FIELD_WIDTH <= 0 means don't pad.
20873 PRECISION is the maximum number of characters to output from
20874 STRING. PRECISION <= 0 means don't truncate the string.
20876 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20877 properties to the string.
20879 PROPS are the properties to add to the string.
20880 The mode_line_string_face face property is always added to the string.
20883 static int
20884 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20885 int field_width, int precision, Lisp_Object props)
20887 ptrdiff_t len;
20888 int n = 0;
20890 if (string != NULL)
20892 len = strlen (string);
20893 if (precision > 0 && len > precision)
20894 len = precision;
20895 lisp_string = make_string (string, len);
20896 if (NILP (props))
20897 props = mode_line_string_face_prop;
20898 else if (!NILP (mode_line_string_face))
20900 Lisp_Object face = Fplist_get (props, Qface);
20901 props = Fcopy_sequence (props);
20902 if (NILP (face))
20903 face = mode_line_string_face;
20904 else
20905 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20906 props = Fplist_put (props, Qface, face);
20908 Fadd_text_properties (make_number (0), make_number (len),
20909 props, lisp_string);
20911 else
20913 len = XFASTINT (Flength (lisp_string));
20914 if (precision > 0 && len > precision)
20916 len = precision;
20917 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20918 precision = -1;
20920 if (!NILP (mode_line_string_face))
20922 Lisp_Object face;
20923 if (NILP (props))
20924 props = Ftext_properties_at (make_number (0), lisp_string);
20925 face = Fplist_get (props, Qface);
20926 if (NILP (face))
20927 face = mode_line_string_face;
20928 else
20929 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20930 props = Fcons (Qface, Fcons (face, Qnil));
20931 if (copy_string)
20932 lisp_string = Fcopy_sequence (lisp_string);
20934 if (!NILP (props))
20935 Fadd_text_properties (make_number (0), make_number (len),
20936 props, lisp_string);
20939 if (len > 0)
20941 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20942 n += len;
20945 if (field_width > len)
20947 field_width -= len;
20948 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20949 if (!NILP (props))
20950 Fadd_text_properties (make_number (0), make_number (field_width),
20951 props, lisp_string);
20952 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20953 n += field_width;
20956 return n;
20960 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20961 1, 4, 0,
20962 doc: /* Format a string out of a mode line format specification.
20963 First arg FORMAT specifies the mode line format (see `mode-line-format'
20964 for details) to use.
20966 By default, the format is evaluated for the currently selected window.
20968 Optional second arg FACE specifies the face property to put on all
20969 characters for which no face is specified. The value nil means the
20970 default face. The value t means whatever face the window's mode line
20971 currently uses (either `mode-line' or `mode-line-inactive',
20972 depending on whether the window is the selected window or not).
20973 An integer value means the value string has no text
20974 properties.
20976 Optional third and fourth args WINDOW and BUFFER specify the window
20977 and buffer to use as the context for the formatting (defaults
20978 are the selected window and the WINDOW's buffer). */)
20979 (Lisp_Object format, Lisp_Object face,
20980 Lisp_Object window, Lisp_Object buffer)
20982 struct it it;
20983 int len;
20984 struct window *w;
20985 struct buffer *old_buffer = NULL;
20986 int face_id;
20987 int no_props = INTEGERP (face);
20988 ptrdiff_t count = SPECPDL_INDEX ();
20989 Lisp_Object str;
20990 int string_start = 0;
20992 if (NILP (window))
20993 window = selected_window;
20994 CHECK_WINDOW (window);
20995 w = XWINDOW (window);
20997 if (NILP (buffer))
20998 buffer = w->buffer;
20999 CHECK_BUFFER (buffer);
21001 /* Make formatting the modeline a non-op when noninteractive, otherwise
21002 there will be problems later caused by a partially initialized frame. */
21003 if (NILP (format) || noninteractive)
21004 return empty_unibyte_string;
21006 if (no_props)
21007 face = Qnil;
21009 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21010 : EQ (face, Qt) ? (EQ (window, selected_window)
21011 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21012 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21013 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21014 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21015 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21016 : DEFAULT_FACE_ID;
21018 if (XBUFFER (buffer) != current_buffer)
21019 old_buffer = current_buffer;
21021 /* Save things including mode_line_proptrans_alist,
21022 and set that to nil so that we don't alter the outer value. */
21023 record_unwind_protect (unwind_format_mode_line,
21024 format_mode_line_unwind_data
21025 (XFRAME (WINDOW_FRAME (XWINDOW (window))),
21026 old_buffer, selected_window, 1));
21027 mode_line_proptrans_alist = Qnil;
21029 Fselect_window (window, Qt);
21030 if (old_buffer)
21031 set_buffer_internal_1 (XBUFFER (buffer));
21033 init_iterator (&it, w, -1, -1, NULL, face_id);
21035 if (no_props)
21037 mode_line_target = MODE_LINE_NOPROP;
21038 mode_line_string_face_prop = Qnil;
21039 mode_line_string_list = Qnil;
21040 string_start = MODE_LINE_NOPROP_LEN (0);
21042 else
21044 mode_line_target = MODE_LINE_STRING;
21045 mode_line_string_list = Qnil;
21046 mode_line_string_face = face;
21047 mode_line_string_face_prop
21048 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
21051 push_kboard (FRAME_KBOARD (it.f));
21052 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21053 pop_kboard ();
21055 if (no_props)
21057 len = MODE_LINE_NOPROP_LEN (string_start);
21058 str = make_string (mode_line_noprop_buf + string_start, len);
21060 else
21062 mode_line_string_list = Fnreverse (mode_line_string_list);
21063 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21064 empty_unibyte_string);
21067 unbind_to (count, Qnil);
21068 return str;
21071 /* Write a null-terminated, right justified decimal representation of
21072 the positive integer D to BUF using a minimal field width WIDTH. */
21074 static void
21075 pint2str (register char *buf, register int width, register ptrdiff_t d)
21077 register char *p = buf;
21079 if (d <= 0)
21080 *p++ = '0';
21081 else
21083 while (d > 0)
21085 *p++ = d % 10 + '0';
21086 d /= 10;
21090 for (width -= (int) (p - buf); width > 0; --width)
21091 *p++ = ' ';
21092 *p-- = '\0';
21093 while (p > buf)
21095 d = *buf;
21096 *buf++ = *p;
21097 *p-- = d;
21101 /* Write a null-terminated, right justified decimal and "human
21102 readable" representation of the nonnegative integer D to BUF using
21103 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21105 static const char power_letter[] =
21107 0, /* no letter */
21108 'k', /* kilo */
21109 'M', /* mega */
21110 'G', /* giga */
21111 'T', /* tera */
21112 'P', /* peta */
21113 'E', /* exa */
21114 'Z', /* zetta */
21115 'Y' /* yotta */
21118 static void
21119 pint2hrstr (char *buf, int width, ptrdiff_t d)
21121 /* We aim to represent the nonnegative integer D as
21122 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21123 ptrdiff_t quotient = d;
21124 int remainder = 0;
21125 /* -1 means: do not use TENTHS. */
21126 int tenths = -1;
21127 int exponent = 0;
21129 /* Length of QUOTIENT.TENTHS as a string. */
21130 int length;
21132 char * psuffix;
21133 char * p;
21135 if (1000 <= quotient)
21137 /* Scale to the appropriate EXPONENT. */
21140 remainder = quotient % 1000;
21141 quotient /= 1000;
21142 exponent++;
21144 while (1000 <= quotient);
21146 /* Round to nearest and decide whether to use TENTHS or not. */
21147 if (quotient <= 9)
21149 tenths = remainder / 100;
21150 if (50 <= remainder % 100)
21152 if (tenths < 9)
21153 tenths++;
21154 else
21156 quotient++;
21157 if (quotient == 10)
21158 tenths = -1;
21159 else
21160 tenths = 0;
21164 else
21165 if (500 <= remainder)
21167 if (quotient < 999)
21168 quotient++;
21169 else
21171 quotient = 1;
21172 exponent++;
21173 tenths = 0;
21178 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21179 if (tenths == -1 && quotient <= 99)
21180 if (quotient <= 9)
21181 length = 1;
21182 else
21183 length = 2;
21184 else
21185 length = 3;
21186 p = psuffix = buf + max (width, length);
21188 /* Print EXPONENT. */
21189 *psuffix++ = power_letter[exponent];
21190 *psuffix = '\0';
21192 /* Print TENTHS. */
21193 if (tenths >= 0)
21195 *--p = '0' + tenths;
21196 *--p = '.';
21199 /* Print QUOTIENT. */
21202 int digit = quotient % 10;
21203 *--p = '0' + digit;
21205 while ((quotient /= 10) != 0);
21207 /* Print leading spaces. */
21208 while (buf < p)
21209 *--p = ' ';
21212 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21213 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21214 type of CODING_SYSTEM. Return updated pointer into BUF. */
21216 static unsigned char invalid_eol_type[] = "(*invalid*)";
21218 static char *
21219 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21221 Lisp_Object val;
21222 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21223 const unsigned char *eol_str;
21224 int eol_str_len;
21225 /* The EOL conversion we are using. */
21226 Lisp_Object eoltype;
21228 val = CODING_SYSTEM_SPEC (coding_system);
21229 eoltype = Qnil;
21231 if (!VECTORP (val)) /* Not yet decided. */
21233 *buf++ = multibyte ? '-' : ' ';
21234 if (eol_flag)
21235 eoltype = eol_mnemonic_undecided;
21236 /* Don't mention EOL conversion if it isn't decided. */
21238 else
21240 Lisp_Object attrs;
21241 Lisp_Object eolvalue;
21243 attrs = AREF (val, 0);
21244 eolvalue = AREF (val, 2);
21246 *buf++ = multibyte
21247 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21248 : ' ';
21250 if (eol_flag)
21252 /* The EOL conversion that is normal on this system. */
21254 if (NILP (eolvalue)) /* Not yet decided. */
21255 eoltype = eol_mnemonic_undecided;
21256 else if (VECTORP (eolvalue)) /* Not yet decided. */
21257 eoltype = eol_mnemonic_undecided;
21258 else /* eolvalue is Qunix, Qdos, or Qmac. */
21259 eoltype = (EQ (eolvalue, Qunix)
21260 ? eol_mnemonic_unix
21261 : (EQ (eolvalue, Qdos) == 1
21262 ? eol_mnemonic_dos : eol_mnemonic_mac));
21266 if (eol_flag)
21268 /* Mention the EOL conversion if it is not the usual one. */
21269 if (STRINGP (eoltype))
21271 eol_str = SDATA (eoltype);
21272 eol_str_len = SBYTES (eoltype);
21274 else if (CHARACTERP (eoltype))
21276 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21277 int c = XFASTINT (eoltype);
21278 eol_str_len = CHAR_STRING (c, tmp);
21279 eol_str = tmp;
21281 else
21283 eol_str = invalid_eol_type;
21284 eol_str_len = sizeof (invalid_eol_type) - 1;
21286 memcpy (buf, eol_str, eol_str_len);
21287 buf += eol_str_len;
21290 return buf;
21293 /* Return a string for the output of a mode line %-spec for window W,
21294 generated by character C. FIELD_WIDTH > 0 means pad the string
21295 returned with spaces to that value. Return a Lisp string in
21296 *STRING if the resulting string is taken from that Lisp string.
21298 Note we operate on the current buffer for most purposes,
21299 the exception being w->base_line_pos. */
21301 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21303 static const char *
21304 decode_mode_spec (struct window *w, register int c, int field_width,
21305 Lisp_Object *string)
21307 Lisp_Object obj;
21308 struct frame *f = XFRAME (WINDOW_FRAME (w));
21309 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21310 struct buffer *b = current_buffer;
21312 obj = Qnil;
21313 *string = Qnil;
21315 switch (c)
21317 case '*':
21318 if (!NILP (BVAR (b, read_only)))
21319 return "%";
21320 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21321 return "*";
21322 return "-";
21324 case '+':
21325 /* This differs from %* only for a modified read-only buffer. */
21326 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21327 return "*";
21328 if (!NILP (BVAR (b, read_only)))
21329 return "%";
21330 return "-";
21332 case '&':
21333 /* This differs from %* in ignoring read-only-ness. */
21334 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21335 return "*";
21336 return "-";
21338 case '%':
21339 return "%";
21341 case '[':
21343 int i;
21344 char *p;
21346 if (command_loop_level > 5)
21347 return "[[[... ";
21348 p = decode_mode_spec_buf;
21349 for (i = 0; i < command_loop_level; i++)
21350 *p++ = '[';
21351 *p = 0;
21352 return decode_mode_spec_buf;
21355 case ']':
21357 int i;
21358 char *p;
21360 if (command_loop_level > 5)
21361 return " ...]]]";
21362 p = decode_mode_spec_buf;
21363 for (i = 0; i < command_loop_level; i++)
21364 *p++ = ']';
21365 *p = 0;
21366 return decode_mode_spec_buf;
21369 case '-':
21371 register int i;
21373 /* Let lots_of_dashes be a string of infinite length. */
21374 if (mode_line_target == MODE_LINE_NOPROP ||
21375 mode_line_target == MODE_LINE_STRING)
21376 return "--";
21377 if (field_width <= 0
21378 || field_width > sizeof (lots_of_dashes))
21380 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21381 decode_mode_spec_buf[i] = '-';
21382 decode_mode_spec_buf[i] = '\0';
21383 return decode_mode_spec_buf;
21385 else
21386 return lots_of_dashes;
21389 case 'b':
21390 obj = BVAR (b, name);
21391 break;
21393 case 'c':
21394 /* %c and %l are ignored in `frame-title-format'.
21395 (In redisplay_internal, the frame title is drawn _before_ the
21396 windows are updated, so the stuff which depends on actual
21397 window contents (such as %l) may fail to render properly, or
21398 even crash emacs.) */
21399 if (mode_line_target == MODE_LINE_TITLE)
21400 return "";
21401 else
21403 ptrdiff_t col = current_column ();
21404 w->column_number_displayed = make_number (col);
21405 pint2str (decode_mode_spec_buf, field_width, col);
21406 return decode_mode_spec_buf;
21409 case 'e':
21410 #ifndef SYSTEM_MALLOC
21412 if (NILP (Vmemory_full))
21413 return "";
21414 else
21415 return "!MEM FULL! ";
21417 #else
21418 return "";
21419 #endif
21421 case 'F':
21422 /* %F displays the frame name. */
21423 if (!NILP (f->title))
21424 return SSDATA (f->title);
21425 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21426 return SSDATA (f->name);
21427 return "Emacs";
21429 case 'f':
21430 obj = BVAR (b, filename);
21431 break;
21433 case 'i':
21435 ptrdiff_t size = ZV - BEGV;
21436 pint2str (decode_mode_spec_buf, field_width, size);
21437 return decode_mode_spec_buf;
21440 case 'I':
21442 ptrdiff_t size = ZV - BEGV;
21443 pint2hrstr (decode_mode_spec_buf, field_width, size);
21444 return decode_mode_spec_buf;
21447 case 'l':
21449 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21450 ptrdiff_t topline, nlines, height;
21451 ptrdiff_t junk;
21453 /* %c and %l are ignored in `frame-title-format'. */
21454 if (mode_line_target == MODE_LINE_TITLE)
21455 return "";
21457 startpos = XMARKER (w->start)->charpos;
21458 startpos_byte = marker_byte_position (w->start);
21459 height = WINDOW_TOTAL_LINES (w);
21461 /* If we decided that this buffer isn't suitable for line numbers,
21462 don't forget that too fast. */
21463 if (EQ (w->base_line_pos, w->buffer))
21464 goto no_value;
21465 /* But do forget it, if the window shows a different buffer now. */
21466 else if (BUFFERP (w->base_line_pos))
21467 w->base_line_pos = Qnil;
21469 /* If the buffer is very big, don't waste time. */
21470 if (INTEGERP (Vline_number_display_limit)
21471 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21473 w->base_line_pos = Qnil;
21474 w->base_line_number = Qnil;
21475 goto no_value;
21478 if (INTEGERP (w->base_line_number)
21479 && INTEGERP (w->base_line_pos)
21480 && XFASTINT (w->base_line_pos) <= startpos)
21482 line = XFASTINT (w->base_line_number);
21483 linepos = XFASTINT (w->base_line_pos);
21484 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21486 else
21488 line = 1;
21489 linepos = BUF_BEGV (b);
21490 linepos_byte = BUF_BEGV_BYTE (b);
21493 /* Count lines from base line to window start position. */
21494 nlines = display_count_lines (linepos_byte,
21495 startpos_byte,
21496 startpos, &junk);
21498 topline = nlines + line;
21500 /* Determine a new base line, if the old one is too close
21501 or too far away, or if we did not have one.
21502 "Too close" means it's plausible a scroll-down would
21503 go back past it. */
21504 if (startpos == BUF_BEGV (b))
21506 w->base_line_number = make_number (topline);
21507 w->base_line_pos = make_number (BUF_BEGV (b));
21509 else if (nlines < height + 25 || nlines > height * 3 + 50
21510 || linepos == BUF_BEGV (b))
21512 ptrdiff_t limit = BUF_BEGV (b);
21513 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21514 ptrdiff_t position;
21515 ptrdiff_t distance =
21516 (height * 2 + 30) * line_number_display_limit_width;
21518 if (startpos - distance > limit)
21520 limit = startpos - distance;
21521 limit_byte = CHAR_TO_BYTE (limit);
21524 nlines = display_count_lines (startpos_byte,
21525 limit_byte,
21526 - (height * 2 + 30),
21527 &position);
21528 /* If we couldn't find the lines we wanted within
21529 line_number_display_limit_width chars per line,
21530 give up on line numbers for this window. */
21531 if (position == limit_byte && limit == startpos - distance)
21533 w->base_line_pos = w->buffer;
21534 w->base_line_number = Qnil;
21535 goto no_value;
21538 w->base_line_number = make_number (topline - nlines);
21539 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
21542 /* Now count lines from the start pos to point. */
21543 nlines = display_count_lines (startpos_byte,
21544 PT_BYTE, PT, &junk);
21546 /* Record that we did display the line number. */
21547 line_number_displayed = 1;
21549 /* Make the string to show. */
21550 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21551 return decode_mode_spec_buf;
21552 no_value:
21554 char* p = decode_mode_spec_buf;
21555 int pad = field_width - 2;
21556 while (pad-- > 0)
21557 *p++ = ' ';
21558 *p++ = '?';
21559 *p++ = '?';
21560 *p = '\0';
21561 return decode_mode_spec_buf;
21564 break;
21566 case 'm':
21567 obj = BVAR (b, mode_name);
21568 break;
21570 case 'n':
21571 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21572 return " Narrow";
21573 break;
21575 case 'p':
21577 ptrdiff_t pos = marker_position (w->start);
21578 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21580 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21582 if (pos <= BUF_BEGV (b))
21583 return "All";
21584 else
21585 return "Bottom";
21587 else if (pos <= BUF_BEGV (b))
21588 return "Top";
21589 else
21591 if (total > 1000000)
21592 /* Do it differently for a large value, to avoid overflow. */
21593 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21594 else
21595 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21596 /* We can't normally display a 3-digit number,
21597 so get us a 2-digit number that is close. */
21598 if (total == 100)
21599 total = 99;
21600 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21601 return decode_mode_spec_buf;
21605 /* Display percentage of size above the bottom of the screen. */
21606 case 'P':
21608 ptrdiff_t toppos = marker_position (w->start);
21609 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21610 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21612 if (botpos >= BUF_ZV (b))
21614 if (toppos <= BUF_BEGV (b))
21615 return "All";
21616 else
21617 return "Bottom";
21619 else
21621 if (total > 1000000)
21622 /* Do it differently for a large value, to avoid overflow. */
21623 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21624 else
21625 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21626 /* We can't normally display a 3-digit number,
21627 so get us a 2-digit number that is close. */
21628 if (total == 100)
21629 total = 99;
21630 if (toppos <= BUF_BEGV (b))
21631 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21632 else
21633 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21634 return decode_mode_spec_buf;
21638 case 's':
21639 /* status of process */
21640 obj = Fget_buffer_process (Fcurrent_buffer ());
21641 if (NILP (obj))
21642 return "no process";
21643 #ifndef MSDOS
21644 obj = Fsymbol_name (Fprocess_status (obj));
21645 #endif
21646 break;
21648 case '@':
21650 ptrdiff_t count = inhibit_garbage_collection ();
21651 Lisp_Object val = call1 (intern ("file-remote-p"),
21652 BVAR (current_buffer, directory));
21653 unbind_to (count, Qnil);
21655 if (NILP (val))
21656 return "-";
21657 else
21658 return "@";
21661 case 't': /* indicate TEXT or BINARY */
21662 return "T";
21664 case 'z':
21665 /* coding-system (not including end-of-line format) */
21666 case 'Z':
21667 /* coding-system (including end-of-line type) */
21669 int eol_flag = (c == 'Z');
21670 char *p = decode_mode_spec_buf;
21672 if (! FRAME_WINDOW_P (f))
21674 /* No need to mention EOL here--the terminal never needs
21675 to do EOL conversion. */
21676 p = decode_mode_spec_coding (CODING_ID_NAME
21677 (FRAME_KEYBOARD_CODING (f)->id),
21678 p, 0);
21679 p = decode_mode_spec_coding (CODING_ID_NAME
21680 (FRAME_TERMINAL_CODING (f)->id),
21681 p, 0);
21683 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21684 p, eol_flag);
21686 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21687 #ifdef subprocesses
21688 obj = Fget_buffer_process (Fcurrent_buffer ());
21689 if (PROCESSP (obj))
21691 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
21692 p, eol_flag);
21693 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
21694 p, eol_flag);
21696 #endif /* subprocesses */
21697 #endif /* 0 */
21698 *p = 0;
21699 return decode_mode_spec_buf;
21703 if (STRINGP (obj))
21705 *string = obj;
21706 return SSDATA (obj);
21708 else
21709 return "";
21713 /* Count up to COUNT lines starting from START_BYTE.
21714 But don't go beyond LIMIT_BYTE.
21715 Return the number of lines thus found (always nonnegative).
21717 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21719 static ptrdiff_t
21720 display_count_lines (ptrdiff_t start_byte,
21721 ptrdiff_t limit_byte, ptrdiff_t count,
21722 ptrdiff_t *byte_pos_ptr)
21724 register unsigned char *cursor;
21725 unsigned char *base;
21727 register ptrdiff_t ceiling;
21728 register unsigned char *ceiling_addr;
21729 ptrdiff_t orig_count = count;
21731 /* If we are not in selective display mode,
21732 check only for newlines. */
21733 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21734 && !INTEGERP (BVAR (current_buffer, selective_display)));
21736 if (count > 0)
21738 while (start_byte < limit_byte)
21740 ceiling = BUFFER_CEILING_OF (start_byte);
21741 ceiling = min (limit_byte - 1, ceiling);
21742 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21743 base = (cursor = BYTE_POS_ADDR (start_byte));
21744 while (1)
21746 if (selective_display)
21747 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21749 else
21750 while (*cursor != '\n' && ++cursor != ceiling_addr)
21753 if (cursor != ceiling_addr)
21755 if (--count == 0)
21757 start_byte += cursor - base + 1;
21758 *byte_pos_ptr = start_byte;
21759 return orig_count;
21761 else
21762 if (++cursor == ceiling_addr)
21763 break;
21765 else
21766 break;
21768 start_byte += cursor - base;
21771 else
21773 while (start_byte > limit_byte)
21775 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21776 ceiling = max (limit_byte, ceiling);
21777 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21778 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21779 while (1)
21781 if (selective_display)
21782 while (--cursor != ceiling_addr
21783 && *cursor != '\n' && *cursor != 015)
21785 else
21786 while (--cursor != ceiling_addr && *cursor != '\n')
21789 if (cursor != ceiling_addr)
21791 if (++count == 0)
21793 start_byte += cursor - base + 1;
21794 *byte_pos_ptr = start_byte;
21795 /* When scanning backwards, we should
21796 not count the newline posterior to which we stop. */
21797 return - orig_count - 1;
21800 else
21801 break;
21803 /* Here we add 1 to compensate for the last decrement
21804 of CURSOR, which took it past the valid range. */
21805 start_byte += cursor - base + 1;
21809 *byte_pos_ptr = limit_byte;
21811 if (count < 0)
21812 return - orig_count + count;
21813 return orig_count - count;
21819 /***********************************************************************
21820 Displaying strings
21821 ***********************************************************************/
21823 /* Display a NUL-terminated string, starting with index START.
21825 If STRING is non-null, display that C string. Otherwise, the Lisp
21826 string LISP_STRING is displayed. There's a case that STRING is
21827 non-null and LISP_STRING is not nil. It means STRING is a string
21828 data of LISP_STRING. In that case, we display LISP_STRING while
21829 ignoring its text properties.
21831 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21832 FACE_STRING. Display STRING or LISP_STRING with the face at
21833 FACE_STRING_POS in FACE_STRING:
21835 Display the string in the environment given by IT, but use the
21836 standard display table, temporarily.
21838 FIELD_WIDTH is the minimum number of output glyphs to produce.
21839 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21840 with spaces. If STRING has more characters, more than FIELD_WIDTH
21841 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21843 PRECISION is the maximum number of characters to output from
21844 STRING. PRECISION < 0 means don't truncate the string.
21846 This is roughly equivalent to printf format specifiers:
21848 FIELD_WIDTH PRECISION PRINTF
21849 ----------------------------------------
21850 -1 -1 %s
21851 -1 10 %.10s
21852 10 -1 %10s
21853 20 10 %20.10s
21855 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21856 display them, and < 0 means obey the current buffer's value of
21857 enable_multibyte_characters.
21859 Value is the number of columns displayed. */
21861 static int
21862 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21863 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21864 int field_width, int precision, int max_x, int multibyte)
21866 int hpos_at_start = it->hpos;
21867 int saved_face_id = it->face_id;
21868 struct glyph_row *row = it->glyph_row;
21869 ptrdiff_t it_charpos;
21871 /* Initialize the iterator IT for iteration over STRING beginning
21872 with index START. */
21873 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21874 precision, field_width, multibyte);
21875 if (string && STRINGP (lisp_string))
21876 /* LISP_STRING is the one returned by decode_mode_spec. We should
21877 ignore its text properties. */
21878 it->stop_charpos = it->end_charpos;
21880 /* If displaying STRING, set up the face of the iterator from
21881 FACE_STRING, if that's given. */
21882 if (STRINGP (face_string))
21884 ptrdiff_t endptr;
21885 struct face *face;
21887 it->face_id
21888 = face_at_string_position (it->w, face_string, face_string_pos,
21889 0, it->region_beg_charpos,
21890 it->region_end_charpos,
21891 &endptr, it->base_face_id, 0);
21892 face = FACE_FROM_ID (it->f, it->face_id);
21893 it->face_box_p = face->box != FACE_NO_BOX;
21896 /* Set max_x to the maximum allowed X position. Don't let it go
21897 beyond the right edge of the window. */
21898 if (max_x <= 0)
21899 max_x = it->last_visible_x;
21900 else
21901 max_x = min (max_x, it->last_visible_x);
21903 /* Skip over display elements that are not visible. because IT->w is
21904 hscrolled. */
21905 if (it->current_x < it->first_visible_x)
21906 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21907 MOVE_TO_POS | MOVE_TO_X);
21909 row->ascent = it->max_ascent;
21910 row->height = it->max_ascent + it->max_descent;
21911 row->phys_ascent = it->max_phys_ascent;
21912 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21913 row->extra_line_spacing = it->max_extra_line_spacing;
21915 if (STRINGP (it->string))
21916 it_charpos = IT_STRING_CHARPOS (*it);
21917 else
21918 it_charpos = IT_CHARPOS (*it);
21920 /* This condition is for the case that we are called with current_x
21921 past last_visible_x. */
21922 while (it->current_x < max_x)
21924 int x_before, x, n_glyphs_before, i, nglyphs;
21926 /* Get the next display element. */
21927 if (!get_next_display_element (it))
21928 break;
21930 /* Produce glyphs. */
21931 x_before = it->current_x;
21932 n_glyphs_before = row->used[TEXT_AREA];
21933 PRODUCE_GLYPHS (it);
21935 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21936 i = 0;
21937 x = x_before;
21938 while (i < nglyphs)
21940 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21942 if (it->line_wrap != TRUNCATE
21943 && x + glyph->pixel_width > max_x)
21945 /* End of continued line or max_x reached. */
21946 if (CHAR_GLYPH_PADDING_P (*glyph))
21948 /* A wide character is unbreakable. */
21949 if (row->reversed_p)
21950 unproduce_glyphs (it, row->used[TEXT_AREA]
21951 - n_glyphs_before);
21952 row->used[TEXT_AREA] = n_glyphs_before;
21953 it->current_x = x_before;
21955 else
21957 if (row->reversed_p)
21958 unproduce_glyphs (it, row->used[TEXT_AREA]
21959 - (n_glyphs_before + i));
21960 row->used[TEXT_AREA] = n_glyphs_before + i;
21961 it->current_x = x;
21963 break;
21965 else if (x + glyph->pixel_width >= it->first_visible_x)
21967 /* Glyph is at least partially visible. */
21968 ++it->hpos;
21969 if (x < it->first_visible_x)
21970 row->x = x - it->first_visible_x;
21972 else
21974 /* Glyph is off the left margin of the display area.
21975 Should not happen. */
21976 abort ();
21979 row->ascent = max (row->ascent, it->max_ascent);
21980 row->height = max (row->height, it->max_ascent + it->max_descent);
21981 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21982 row->phys_height = max (row->phys_height,
21983 it->max_phys_ascent + it->max_phys_descent);
21984 row->extra_line_spacing = max (row->extra_line_spacing,
21985 it->max_extra_line_spacing);
21986 x += glyph->pixel_width;
21987 ++i;
21990 /* Stop if max_x reached. */
21991 if (i < nglyphs)
21992 break;
21994 /* Stop at line ends. */
21995 if (ITERATOR_AT_END_OF_LINE_P (it))
21997 it->continuation_lines_width = 0;
21998 break;
22001 set_iterator_to_next (it, 1);
22002 if (STRINGP (it->string))
22003 it_charpos = IT_STRING_CHARPOS (*it);
22004 else
22005 it_charpos = IT_CHARPOS (*it);
22007 /* Stop if truncating at the right edge. */
22008 if (it->line_wrap == TRUNCATE
22009 && it->current_x >= it->last_visible_x)
22011 /* Add truncation mark, but don't do it if the line is
22012 truncated at a padding space. */
22013 if (it_charpos < it->string_nchars)
22015 if (!FRAME_WINDOW_P (it->f))
22017 int ii, n;
22019 if (it->current_x > it->last_visible_x)
22021 if (!row->reversed_p)
22023 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22024 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22025 break;
22027 else
22029 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22030 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22031 break;
22032 unproduce_glyphs (it, ii + 1);
22033 ii = row->used[TEXT_AREA] - (ii + 1);
22035 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22037 row->used[TEXT_AREA] = ii;
22038 produce_special_glyphs (it, IT_TRUNCATION);
22041 produce_special_glyphs (it, IT_TRUNCATION);
22043 row->truncated_on_right_p = 1;
22045 break;
22049 /* Maybe insert a truncation at the left. */
22050 if (it->first_visible_x
22051 && it_charpos > 0)
22053 if (!FRAME_WINDOW_P (it->f)
22054 || (row->reversed_p
22055 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22056 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22057 insert_left_trunc_glyphs (it);
22058 row->truncated_on_left_p = 1;
22061 it->face_id = saved_face_id;
22063 /* Value is number of columns displayed. */
22064 return it->hpos - hpos_at_start;
22069 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22070 appears as an element of LIST or as the car of an element of LIST.
22071 If PROPVAL is a list, compare each element against LIST in that
22072 way, and return 1/2 if any element of PROPVAL is found in LIST.
22073 Otherwise return 0. This function cannot quit.
22074 The return value is 2 if the text is invisible but with an ellipsis
22075 and 1 if it's invisible and without an ellipsis. */
22078 invisible_p (register Lisp_Object propval, Lisp_Object list)
22080 register Lisp_Object tail, proptail;
22082 for (tail = list; CONSP (tail); tail = XCDR (tail))
22084 register Lisp_Object tem;
22085 tem = XCAR (tail);
22086 if (EQ (propval, tem))
22087 return 1;
22088 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22089 return NILP (XCDR (tem)) ? 1 : 2;
22092 if (CONSP (propval))
22094 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22096 Lisp_Object propelt;
22097 propelt = XCAR (proptail);
22098 for (tail = list; CONSP (tail); tail = XCDR (tail))
22100 register Lisp_Object tem;
22101 tem = XCAR (tail);
22102 if (EQ (propelt, tem))
22103 return 1;
22104 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22105 return NILP (XCDR (tem)) ? 1 : 2;
22110 return 0;
22113 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22114 doc: /* Non-nil if the property makes the text invisible.
22115 POS-OR-PROP can be a marker or number, in which case it is taken to be
22116 a position in the current buffer and the value of the `invisible' property
22117 is checked; or it can be some other value, which is then presumed to be the
22118 value of the `invisible' property of the text of interest.
22119 The non-nil value returned can be t for truly invisible text or something
22120 else if the text is replaced by an ellipsis. */)
22121 (Lisp_Object pos_or_prop)
22123 Lisp_Object prop
22124 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22125 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22126 : pos_or_prop);
22127 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22128 return (invis == 0 ? Qnil
22129 : invis == 1 ? Qt
22130 : make_number (invis));
22133 /* Calculate a width or height in pixels from a specification using
22134 the following elements:
22136 SPEC ::=
22137 NUM - a (fractional) multiple of the default font width/height
22138 (NUM) - specifies exactly NUM pixels
22139 UNIT - a fixed number of pixels, see below.
22140 ELEMENT - size of a display element in pixels, see below.
22141 (NUM . SPEC) - equals NUM * SPEC
22142 (+ SPEC SPEC ...) - add pixel values
22143 (- SPEC SPEC ...) - subtract pixel values
22144 (- SPEC) - negate pixel value
22146 NUM ::=
22147 INT or FLOAT - a number constant
22148 SYMBOL - use symbol's (buffer local) variable binding.
22150 UNIT ::=
22151 in - pixels per inch *)
22152 mm - pixels per 1/1000 meter *)
22153 cm - pixels per 1/100 meter *)
22154 width - width of current font in pixels.
22155 height - height of current font in pixels.
22157 *) using the ratio(s) defined in display-pixels-per-inch.
22159 ELEMENT ::=
22161 left-fringe - left fringe width in pixels
22162 right-fringe - right fringe width in pixels
22164 left-margin - left margin width in pixels
22165 right-margin - right margin width in pixels
22167 scroll-bar - scroll-bar area width in pixels
22169 Examples:
22171 Pixels corresponding to 5 inches:
22172 (5 . in)
22174 Total width of non-text areas on left side of window (if scroll-bar is on left):
22175 '(space :width (+ left-fringe left-margin scroll-bar))
22177 Align to first text column (in header line):
22178 '(space :align-to 0)
22180 Align to middle of text area minus half the width of variable `my-image'
22181 containing a loaded image:
22182 '(space :align-to (0.5 . (- text my-image)))
22184 Width of left margin minus width of 1 character in the default font:
22185 '(space :width (- left-margin 1))
22187 Width of left margin minus width of 2 characters in the current font:
22188 '(space :width (- left-margin (2 . width)))
22190 Center 1 character over left-margin (in header line):
22191 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22193 Different ways to express width of left fringe plus left margin minus one pixel:
22194 '(space :width (- (+ left-fringe left-margin) (1)))
22195 '(space :width (+ left-fringe left-margin (- (1))))
22196 '(space :width (+ left-fringe left-margin (-1)))
22200 #define NUMVAL(X) \
22201 ((INTEGERP (X) || FLOATP (X)) \
22202 ? XFLOATINT (X) \
22203 : - 1)
22205 static int
22206 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22207 struct font *font, int width_p, int *align_to)
22209 double pixels;
22211 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22212 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22214 if (NILP (prop))
22215 return OK_PIXELS (0);
22217 eassert (FRAME_LIVE_P (it->f));
22219 if (SYMBOLP (prop))
22221 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22223 char *unit = SSDATA (SYMBOL_NAME (prop));
22225 if (unit[0] == 'i' && unit[1] == 'n')
22226 pixels = 1.0;
22227 else if (unit[0] == 'm' && unit[1] == 'm')
22228 pixels = 25.4;
22229 else if (unit[0] == 'c' && unit[1] == 'm')
22230 pixels = 2.54;
22231 else
22232 pixels = 0;
22233 if (pixels > 0)
22235 double ppi;
22236 #ifdef HAVE_WINDOW_SYSTEM
22237 if (FRAME_WINDOW_P (it->f)
22238 && (ppi = (width_p
22239 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22240 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22241 ppi > 0))
22242 return OK_PIXELS (ppi / pixels);
22243 #endif
22245 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22246 || (CONSP (Vdisplay_pixels_per_inch)
22247 && (ppi = (width_p
22248 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22249 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22250 ppi > 0)))
22251 return OK_PIXELS (ppi / pixels);
22253 return 0;
22257 #ifdef HAVE_WINDOW_SYSTEM
22258 if (EQ (prop, Qheight))
22259 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22260 if (EQ (prop, Qwidth))
22261 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22262 #else
22263 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22264 return OK_PIXELS (1);
22265 #endif
22267 if (EQ (prop, Qtext))
22268 return OK_PIXELS (width_p
22269 ? window_box_width (it->w, TEXT_AREA)
22270 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22272 if (align_to && *align_to < 0)
22274 *res = 0;
22275 if (EQ (prop, Qleft))
22276 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22277 if (EQ (prop, Qright))
22278 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22279 if (EQ (prop, Qcenter))
22280 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22281 + window_box_width (it->w, TEXT_AREA) / 2);
22282 if (EQ (prop, Qleft_fringe))
22283 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22284 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22285 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22286 if (EQ (prop, Qright_fringe))
22287 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22288 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22289 : window_box_right_offset (it->w, TEXT_AREA));
22290 if (EQ (prop, Qleft_margin))
22291 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22292 if (EQ (prop, Qright_margin))
22293 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22294 if (EQ (prop, Qscroll_bar))
22295 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22297 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22298 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22299 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22300 : 0)));
22302 else
22304 if (EQ (prop, Qleft_fringe))
22305 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22306 if (EQ (prop, Qright_fringe))
22307 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22308 if (EQ (prop, Qleft_margin))
22309 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22310 if (EQ (prop, Qright_margin))
22311 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22312 if (EQ (prop, Qscroll_bar))
22313 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22316 prop = buffer_local_value_1 (prop, it->w->buffer);
22317 if (EQ (prop, Qunbound))
22318 prop = Qnil;
22321 if (INTEGERP (prop) || FLOATP (prop))
22323 int base_unit = (width_p
22324 ? FRAME_COLUMN_WIDTH (it->f)
22325 : FRAME_LINE_HEIGHT (it->f));
22326 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22329 if (CONSP (prop))
22331 Lisp_Object car = XCAR (prop);
22332 Lisp_Object cdr = XCDR (prop);
22334 if (SYMBOLP (car))
22336 #ifdef HAVE_WINDOW_SYSTEM
22337 if (FRAME_WINDOW_P (it->f)
22338 && valid_image_p (prop))
22340 ptrdiff_t id = lookup_image (it->f, prop);
22341 struct image *img = IMAGE_FROM_ID (it->f, id);
22343 return OK_PIXELS (width_p ? img->width : img->height);
22345 #endif
22346 if (EQ (car, Qplus) || EQ (car, Qminus))
22348 int first = 1;
22349 double px;
22351 pixels = 0;
22352 while (CONSP (cdr))
22354 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22355 font, width_p, align_to))
22356 return 0;
22357 if (first)
22358 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22359 else
22360 pixels += px;
22361 cdr = XCDR (cdr);
22363 if (EQ (car, Qminus))
22364 pixels = -pixels;
22365 return OK_PIXELS (pixels);
22368 car = buffer_local_value_1 (car, it->w->buffer);
22369 if (EQ (car, Qunbound))
22370 car = Qnil;
22373 if (INTEGERP (car) || FLOATP (car))
22375 double fact;
22376 pixels = XFLOATINT (car);
22377 if (NILP (cdr))
22378 return OK_PIXELS (pixels);
22379 if (calc_pixel_width_or_height (&fact, it, cdr,
22380 font, width_p, align_to))
22381 return OK_PIXELS (pixels * fact);
22382 return 0;
22385 return 0;
22388 return 0;
22392 /***********************************************************************
22393 Glyph Display
22394 ***********************************************************************/
22396 #ifdef HAVE_WINDOW_SYSTEM
22398 #ifdef GLYPH_DEBUG
22400 void
22401 dump_glyph_string (struct glyph_string *s)
22403 fprintf (stderr, "glyph string\n");
22404 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22405 s->x, s->y, s->width, s->height);
22406 fprintf (stderr, " ybase = %d\n", s->ybase);
22407 fprintf (stderr, " hl = %d\n", s->hl);
22408 fprintf (stderr, " left overhang = %d, right = %d\n",
22409 s->left_overhang, s->right_overhang);
22410 fprintf (stderr, " nchars = %d\n", s->nchars);
22411 fprintf (stderr, " extends to end of line = %d\n",
22412 s->extends_to_end_of_line_p);
22413 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22414 fprintf (stderr, " bg width = %d\n", s->background_width);
22417 #endif /* GLYPH_DEBUG */
22419 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22420 of XChar2b structures for S; it can't be allocated in
22421 init_glyph_string because it must be allocated via `alloca'. W
22422 is the window on which S is drawn. ROW and AREA are the glyph row
22423 and area within the row from which S is constructed. START is the
22424 index of the first glyph structure covered by S. HL is a
22425 face-override for drawing S. */
22427 #ifdef HAVE_NTGUI
22428 #define OPTIONAL_HDC(hdc) HDC hdc,
22429 #define DECLARE_HDC(hdc) HDC hdc;
22430 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22431 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22432 #endif
22434 #ifndef OPTIONAL_HDC
22435 #define OPTIONAL_HDC(hdc)
22436 #define DECLARE_HDC(hdc)
22437 #define ALLOCATE_HDC(hdc, f)
22438 #define RELEASE_HDC(hdc, f)
22439 #endif
22441 static void
22442 init_glyph_string (struct glyph_string *s,
22443 OPTIONAL_HDC (hdc)
22444 XChar2b *char2b, struct window *w, struct glyph_row *row,
22445 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22447 memset (s, 0, sizeof *s);
22448 s->w = w;
22449 s->f = XFRAME (w->frame);
22450 #ifdef HAVE_NTGUI
22451 s->hdc = hdc;
22452 #endif
22453 s->display = FRAME_X_DISPLAY (s->f);
22454 s->window = FRAME_X_WINDOW (s->f);
22455 s->char2b = char2b;
22456 s->hl = hl;
22457 s->row = row;
22458 s->area = area;
22459 s->first_glyph = row->glyphs[area] + start;
22460 s->height = row->height;
22461 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22462 s->ybase = s->y + row->ascent;
22466 /* Append the list of glyph strings with head H and tail T to the list
22467 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22469 static inline void
22470 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22471 struct glyph_string *h, struct glyph_string *t)
22473 if (h)
22475 if (*head)
22476 (*tail)->next = h;
22477 else
22478 *head = h;
22479 h->prev = *tail;
22480 *tail = t;
22485 /* Prepend the list of glyph strings with head H and tail T to the
22486 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22487 result. */
22489 static inline void
22490 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22491 struct glyph_string *h, struct glyph_string *t)
22493 if (h)
22495 if (*head)
22496 (*head)->prev = t;
22497 else
22498 *tail = t;
22499 t->next = *head;
22500 *head = h;
22505 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22506 Set *HEAD and *TAIL to the resulting list. */
22508 static inline void
22509 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22510 struct glyph_string *s)
22512 s->next = s->prev = NULL;
22513 append_glyph_string_lists (head, tail, s, s);
22517 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22518 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22519 make sure that X resources for the face returned are allocated.
22520 Value is a pointer to a realized face that is ready for display if
22521 DISPLAY_P is non-zero. */
22523 static inline struct face *
22524 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22525 XChar2b *char2b, int display_p)
22527 struct face *face = FACE_FROM_ID (f, face_id);
22529 if (face->font)
22531 unsigned code = face->font->driver->encode_char (face->font, c);
22533 if (code != FONT_INVALID_CODE)
22534 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22535 else
22536 STORE_XCHAR2B (char2b, 0, 0);
22539 /* Make sure X resources of the face are allocated. */
22540 #ifdef HAVE_X_WINDOWS
22541 if (display_p)
22542 #endif
22544 eassert (face != NULL);
22545 PREPARE_FACE_FOR_DISPLAY (f, face);
22548 return face;
22552 /* Get face and two-byte form of character glyph GLYPH on frame F.
22553 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22554 a pointer to a realized face that is ready for display. */
22556 static inline struct face *
22557 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22558 XChar2b *char2b, int *two_byte_p)
22560 struct face *face;
22562 eassert (glyph->type == CHAR_GLYPH);
22563 face = FACE_FROM_ID (f, glyph->face_id);
22565 if (two_byte_p)
22566 *two_byte_p = 0;
22568 if (face->font)
22570 unsigned code;
22572 if (CHAR_BYTE8_P (glyph->u.ch))
22573 code = CHAR_TO_BYTE8 (glyph->u.ch);
22574 else
22575 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22577 if (code != FONT_INVALID_CODE)
22578 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22579 else
22580 STORE_XCHAR2B (char2b, 0, 0);
22583 /* Make sure X resources of the face are allocated. */
22584 eassert (face != NULL);
22585 PREPARE_FACE_FOR_DISPLAY (f, face);
22586 return face;
22590 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22591 Return 1 if FONT has a glyph for C, otherwise return 0. */
22593 static inline int
22594 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22596 unsigned code;
22598 if (CHAR_BYTE8_P (c))
22599 code = CHAR_TO_BYTE8 (c);
22600 else
22601 code = font->driver->encode_char (font, c);
22603 if (code == FONT_INVALID_CODE)
22604 return 0;
22605 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22606 return 1;
22610 /* Fill glyph string S with composition components specified by S->cmp.
22612 BASE_FACE is the base face of the composition.
22613 S->cmp_from is the index of the first component for S.
22615 OVERLAPS non-zero means S should draw the foreground only, and use
22616 its physical height for clipping. See also draw_glyphs.
22618 Value is the index of a component not in S. */
22620 static int
22621 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22622 int overlaps)
22624 int i;
22625 /* For all glyphs of this composition, starting at the offset
22626 S->cmp_from, until we reach the end of the definition or encounter a
22627 glyph that requires the different face, add it to S. */
22628 struct face *face;
22630 eassert (s);
22632 s->for_overlaps = overlaps;
22633 s->face = NULL;
22634 s->font = NULL;
22635 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22637 int c = COMPOSITION_GLYPH (s->cmp, i);
22639 /* TAB in a composition means display glyphs with padding space
22640 on the left or right. */
22641 if (c != '\t')
22643 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22644 -1, Qnil);
22646 face = get_char_face_and_encoding (s->f, c, face_id,
22647 s->char2b + i, 1);
22648 if (face)
22650 if (! s->face)
22652 s->face = face;
22653 s->font = s->face->font;
22655 else if (s->face != face)
22656 break;
22659 ++s->nchars;
22661 s->cmp_to = i;
22663 if (s->face == NULL)
22665 s->face = base_face->ascii_face;
22666 s->font = s->face->font;
22669 /* All glyph strings for the same composition has the same width,
22670 i.e. the width set for the first component of the composition. */
22671 s->width = s->first_glyph->pixel_width;
22673 /* If the specified font could not be loaded, use the frame's
22674 default font, but record the fact that we couldn't load it in
22675 the glyph string so that we can draw rectangles for the
22676 characters of the glyph string. */
22677 if (s->font == NULL)
22679 s->font_not_found_p = 1;
22680 s->font = FRAME_FONT (s->f);
22683 /* Adjust base line for subscript/superscript text. */
22684 s->ybase += s->first_glyph->voffset;
22686 /* This glyph string must always be drawn with 16-bit functions. */
22687 s->two_byte_p = 1;
22689 return s->cmp_to;
22692 static int
22693 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22694 int start, int end, int overlaps)
22696 struct glyph *glyph, *last;
22697 Lisp_Object lgstring;
22698 int i;
22700 s->for_overlaps = overlaps;
22701 glyph = s->row->glyphs[s->area] + start;
22702 last = s->row->glyphs[s->area] + end;
22703 s->cmp_id = glyph->u.cmp.id;
22704 s->cmp_from = glyph->slice.cmp.from;
22705 s->cmp_to = glyph->slice.cmp.to + 1;
22706 s->face = FACE_FROM_ID (s->f, face_id);
22707 lgstring = composition_gstring_from_id (s->cmp_id);
22708 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22709 glyph++;
22710 while (glyph < last
22711 && glyph->u.cmp.automatic
22712 && glyph->u.cmp.id == s->cmp_id
22713 && s->cmp_to == glyph->slice.cmp.from)
22714 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22716 for (i = s->cmp_from; i < s->cmp_to; i++)
22718 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22719 unsigned code = LGLYPH_CODE (lglyph);
22721 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22723 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22724 return glyph - s->row->glyphs[s->area];
22728 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22729 See the comment of fill_glyph_string for arguments.
22730 Value is the index of the first glyph not in S. */
22733 static int
22734 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22735 int start, int end, int overlaps)
22737 struct glyph *glyph, *last;
22738 int voffset;
22740 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22741 s->for_overlaps = overlaps;
22742 glyph = s->row->glyphs[s->area] + start;
22743 last = s->row->glyphs[s->area] + end;
22744 voffset = glyph->voffset;
22745 s->face = FACE_FROM_ID (s->f, face_id);
22746 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22747 s->nchars = 1;
22748 s->width = glyph->pixel_width;
22749 glyph++;
22750 while (glyph < last
22751 && glyph->type == GLYPHLESS_GLYPH
22752 && glyph->voffset == voffset
22753 && glyph->face_id == face_id)
22755 s->nchars++;
22756 s->width += glyph->pixel_width;
22757 glyph++;
22759 s->ybase += voffset;
22760 return glyph - s->row->glyphs[s->area];
22764 /* Fill glyph string S from a sequence of character glyphs.
22766 FACE_ID is the face id of the string. START is the index of the
22767 first glyph to consider, END is the index of the last + 1.
22768 OVERLAPS non-zero means S should draw the foreground only, and use
22769 its physical height for clipping. See also draw_glyphs.
22771 Value is the index of the first glyph not in S. */
22773 static int
22774 fill_glyph_string (struct glyph_string *s, int face_id,
22775 int start, int end, int overlaps)
22777 struct glyph *glyph, *last;
22778 int voffset;
22779 int glyph_not_available_p;
22781 eassert (s->f == XFRAME (s->w->frame));
22782 eassert (s->nchars == 0);
22783 eassert (start >= 0 && end > start);
22785 s->for_overlaps = overlaps;
22786 glyph = s->row->glyphs[s->area] + start;
22787 last = s->row->glyphs[s->area] + end;
22788 voffset = glyph->voffset;
22789 s->padding_p = glyph->padding_p;
22790 glyph_not_available_p = glyph->glyph_not_available_p;
22792 while (glyph < last
22793 && glyph->type == CHAR_GLYPH
22794 && glyph->voffset == voffset
22795 /* Same face id implies same font, nowadays. */
22796 && glyph->face_id == face_id
22797 && glyph->glyph_not_available_p == glyph_not_available_p)
22799 int two_byte_p;
22801 s->face = get_glyph_face_and_encoding (s->f, glyph,
22802 s->char2b + s->nchars,
22803 &two_byte_p);
22804 s->two_byte_p = two_byte_p;
22805 ++s->nchars;
22806 eassert (s->nchars <= end - start);
22807 s->width += glyph->pixel_width;
22808 if (glyph++->padding_p != s->padding_p)
22809 break;
22812 s->font = s->face->font;
22814 /* If the specified font could not be loaded, use the frame's font,
22815 but record the fact that we couldn't load it in
22816 S->font_not_found_p so that we can draw rectangles for the
22817 characters of the glyph string. */
22818 if (s->font == NULL || glyph_not_available_p)
22820 s->font_not_found_p = 1;
22821 s->font = FRAME_FONT (s->f);
22824 /* Adjust base line for subscript/superscript text. */
22825 s->ybase += voffset;
22827 eassert (s->face && s->face->gc);
22828 return glyph - s->row->glyphs[s->area];
22832 /* Fill glyph string S from image glyph S->first_glyph. */
22834 static void
22835 fill_image_glyph_string (struct glyph_string *s)
22837 eassert (s->first_glyph->type == IMAGE_GLYPH);
22838 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22839 eassert (s->img);
22840 s->slice = s->first_glyph->slice.img;
22841 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22842 s->font = s->face->font;
22843 s->width = s->first_glyph->pixel_width;
22845 /* Adjust base line for subscript/superscript text. */
22846 s->ybase += s->first_glyph->voffset;
22850 /* Fill glyph string S from a sequence of stretch glyphs.
22852 START is the index of the first glyph to consider,
22853 END is the index of the last + 1.
22855 Value is the index of the first glyph not in S. */
22857 static int
22858 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22860 struct glyph *glyph, *last;
22861 int voffset, face_id;
22863 eassert (s->first_glyph->type == STRETCH_GLYPH);
22865 glyph = s->row->glyphs[s->area] + start;
22866 last = s->row->glyphs[s->area] + end;
22867 face_id = glyph->face_id;
22868 s->face = FACE_FROM_ID (s->f, face_id);
22869 s->font = s->face->font;
22870 s->width = glyph->pixel_width;
22871 s->nchars = 1;
22872 voffset = glyph->voffset;
22874 for (++glyph;
22875 (glyph < last
22876 && glyph->type == STRETCH_GLYPH
22877 && glyph->voffset == voffset
22878 && glyph->face_id == face_id);
22879 ++glyph)
22880 s->width += glyph->pixel_width;
22882 /* Adjust base line for subscript/superscript text. */
22883 s->ybase += voffset;
22885 /* The case that face->gc == 0 is handled when drawing the glyph
22886 string by calling PREPARE_FACE_FOR_DISPLAY. */
22887 eassert (s->face);
22888 return glyph - s->row->glyphs[s->area];
22891 static struct font_metrics *
22892 get_per_char_metric (struct font *font, XChar2b *char2b)
22894 static struct font_metrics metrics;
22895 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22897 if (! font || code == FONT_INVALID_CODE)
22898 return NULL;
22899 font->driver->text_extents (font, &code, 1, &metrics);
22900 return &metrics;
22903 /* EXPORT for RIF:
22904 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22905 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22906 assumed to be zero. */
22908 void
22909 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22911 *left = *right = 0;
22913 if (glyph->type == CHAR_GLYPH)
22915 struct face *face;
22916 XChar2b char2b;
22917 struct font_metrics *pcm;
22919 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22920 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22922 if (pcm->rbearing > pcm->width)
22923 *right = pcm->rbearing - pcm->width;
22924 if (pcm->lbearing < 0)
22925 *left = -pcm->lbearing;
22928 else if (glyph->type == COMPOSITE_GLYPH)
22930 if (! glyph->u.cmp.automatic)
22932 struct composition *cmp = composition_table[glyph->u.cmp.id];
22934 if (cmp->rbearing > cmp->pixel_width)
22935 *right = cmp->rbearing - cmp->pixel_width;
22936 if (cmp->lbearing < 0)
22937 *left = - cmp->lbearing;
22939 else
22941 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22942 struct font_metrics metrics;
22944 composition_gstring_width (gstring, glyph->slice.cmp.from,
22945 glyph->slice.cmp.to + 1, &metrics);
22946 if (metrics.rbearing > metrics.width)
22947 *right = metrics.rbearing - metrics.width;
22948 if (metrics.lbearing < 0)
22949 *left = - metrics.lbearing;
22955 /* Return the index of the first glyph preceding glyph string S that
22956 is overwritten by S because of S's left overhang. Value is -1
22957 if no glyphs are overwritten. */
22959 static int
22960 left_overwritten (struct glyph_string *s)
22962 int k;
22964 if (s->left_overhang)
22966 int x = 0, i;
22967 struct glyph *glyphs = s->row->glyphs[s->area];
22968 int first = s->first_glyph - glyphs;
22970 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22971 x -= glyphs[i].pixel_width;
22973 k = i + 1;
22975 else
22976 k = -1;
22978 return k;
22982 /* Return the index of the first glyph preceding glyph string S that
22983 is overwriting S because of its right overhang. Value is -1 if no
22984 glyph in front of S overwrites S. */
22986 static int
22987 left_overwriting (struct glyph_string *s)
22989 int i, k, x;
22990 struct glyph *glyphs = s->row->glyphs[s->area];
22991 int first = s->first_glyph - glyphs;
22993 k = -1;
22994 x = 0;
22995 for (i = first - 1; i >= 0; --i)
22997 int left, right;
22998 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22999 if (x + right > 0)
23000 k = i;
23001 x -= glyphs[i].pixel_width;
23004 return k;
23008 /* Return the index of the last glyph following glyph string S that is
23009 overwritten by S because of S's right overhang. Value is -1 if
23010 no such glyph is found. */
23012 static int
23013 right_overwritten (struct glyph_string *s)
23015 int k = -1;
23017 if (s->right_overhang)
23019 int x = 0, i;
23020 struct glyph *glyphs = s->row->glyphs[s->area];
23021 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
23022 int end = s->row->used[s->area];
23024 for (i = first; i < end && s->right_overhang > x; ++i)
23025 x += glyphs[i].pixel_width;
23027 k = i;
23030 return k;
23034 /* Return the index of the last glyph following glyph string S that
23035 overwrites S because of its left overhang. Value is negative
23036 if no such glyph is found. */
23038 static int
23039 right_overwriting (struct glyph_string *s)
23041 int i, k, x;
23042 int end = s->row->used[s->area];
23043 struct glyph *glyphs = s->row->glyphs[s->area];
23044 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
23046 k = -1;
23047 x = 0;
23048 for (i = first; i < end; ++i)
23050 int left, right;
23051 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23052 if (x - left < 0)
23053 k = i;
23054 x += glyphs[i].pixel_width;
23057 return k;
23061 /* Set background width of glyph string S. START is the index of the
23062 first glyph following S. LAST_X is the right-most x-position + 1
23063 in the drawing area. */
23065 static inline void
23066 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23068 /* If the face of this glyph string has to be drawn to the end of
23069 the drawing area, set S->extends_to_end_of_line_p. */
23071 if (start == s->row->used[s->area]
23072 && s->area == TEXT_AREA
23073 && ((s->row->fill_line_p
23074 && (s->hl == DRAW_NORMAL_TEXT
23075 || s->hl == DRAW_IMAGE_RAISED
23076 || s->hl == DRAW_IMAGE_SUNKEN))
23077 || s->hl == DRAW_MOUSE_FACE))
23078 s->extends_to_end_of_line_p = 1;
23080 /* If S extends its face to the end of the line, set its
23081 background_width to the distance to the right edge of the drawing
23082 area. */
23083 if (s->extends_to_end_of_line_p)
23084 s->background_width = last_x - s->x + 1;
23085 else
23086 s->background_width = s->width;
23090 /* Compute overhangs and x-positions for glyph string S and its
23091 predecessors, or successors. X is the starting x-position for S.
23092 BACKWARD_P non-zero means process predecessors. */
23094 static void
23095 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23097 if (backward_p)
23099 while (s)
23101 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23102 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23103 x -= s->width;
23104 s->x = x;
23105 s = s->prev;
23108 else
23110 while (s)
23112 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23113 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23114 s->x = x;
23115 x += s->width;
23116 s = s->next;
23123 /* The following macros are only called from draw_glyphs below.
23124 They reference the following parameters of that function directly:
23125 `w', `row', `area', and `overlap_p'
23126 as well as the following local variables:
23127 `s', `f', and `hdc' (in W32) */
23129 #ifdef HAVE_NTGUI
23130 /* On W32, silently add local `hdc' variable to argument list of
23131 init_glyph_string. */
23132 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23133 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23134 #else
23135 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23136 init_glyph_string (s, char2b, w, row, area, start, hl)
23137 #endif
23139 /* Add a glyph string for a stretch glyph to the list of strings
23140 between HEAD and TAIL. START is the index of the stretch glyph in
23141 row area AREA of glyph row ROW. END is the index of the last glyph
23142 in that glyph row area. X is the current output position assigned
23143 to the new glyph string constructed. HL overrides that face of the
23144 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23145 is the right-most x-position of the drawing area. */
23147 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23148 and below -- keep them on one line. */
23149 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23150 do \
23152 s = alloca (sizeof *s); \
23153 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23154 START = fill_stretch_glyph_string (s, START, END); \
23155 append_glyph_string (&HEAD, &TAIL, s); \
23156 s->x = (X); \
23158 while (0)
23161 /* Add a glyph string for an image glyph to the list of strings
23162 between HEAD and TAIL. START is the index of the image glyph in
23163 row area AREA of glyph row ROW. END is the index of the last glyph
23164 in that glyph row area. X is the current output position assigned
23165 to the new glyph string constructed. HL overrides that face of the
23166 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23167 is the right-most x-position of the drawing area. */
23169 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23170 do \
23172 s = alloca (sizeof *s); \
23173 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23174 fill_image_glyph_string (s); \
23175 append_glyph_string (&HEAD, &TAIL, s); \
23176 ++START; \
23177 s->x = (X); \
23179 while (0)
23182 /* Add a glyph string for a sequence of character glyphs to the list
23183 of strings between HEAD and TAIL. START is the index of the first
23184 glyph in row area AREA of glyph row ROW that is part of the new
23185 glyph string. END is the index of the last glyph in that glyph row
23186 area. X is the current output position assigned to the new glyph
23187 string constructed. HL overrides that face of the glyph; e.g. it
23188 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23189 right-most x-position of the drawing area. */
23191 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23192 do \
23194 int face_id; \
23195 XChar2b *char2b; \
23197 face_id = (row)->glyphs[area][START].face_id; \
23199 s = alloca (sizeof *s); \
23200 char2b = alloca ((END - START) * sizeof *char2b); \
23201 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23202 append_glyph_string (&HEAD, &TAIL, s); \
23203 s->x = (X); \
23204 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23206 while (0)
23209 /* Add a glyph string for a composite sequence to the list of strings
23210 between HEAD and TAIL. START is the index of the first glyph in
23211 row area AREA of glyph row ROW that is part of the new glyph
23212 string. END is the index of the last glyph in that glyph row area.
23213 X is the current output position assigned to the new glyph string
23214 constructed. HL overrides that face of the glyph; e.g. it is
23215 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23216 x-position of the drawing area. */
23218 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23219 do { \
23220 int face_id = (row)->glyphs[area][START].face_id; \
23221 struct face *base_face = FACE_FROM_ID (f, face_id); \
23222 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23223 struct composition *cmp = composition_table[cmp_id]; \
23224 XChar2b *char2b; \
23225 struct glyph_string *first_s = NULL; \
23226 int n; \
23228 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23230 /* Make glyph_strings for each glyph sequence that is drawable by \
23231 the same face, and append them to HEAD/TAIL. */ \
23232 for (n = 0; n < cmp->glyph_len;) \
23234 s = alloca (sizeof *s); \
23235 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23236 append_glyph_string (&(HEAD), &(TAIL), s); \
23237 s->cmp = cmp; \
23238 s->cmp_from = n; \
23239 s->x = (X); \
23240 if (n == 0) \
23241 first_s = s; \
23242 n = fill_composite_glyph_string (s, base_face, overlaps); \
23245 ++START; \
23246 s = first_s; \
23247 } while (0)
23250 /* Add a glyph string for a glyph-string sequence to the list of strings
23251 between HEAD and TAIL. */
23253 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23254 do { \
23255 int face_id; \
23256 XChar2b *char2b; \
23257 Lisp_Object gstring; \
23259 face_id = (row)->glyphs[area][START].face_id; \
23260 gstring = (composition_gstring_from_id \
23261 ((row)->glyphs[area][START].u.cmp.id)); \
23262 s = alloca (sizeof *s); \
23263 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23264 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23265 append_glyph_string (&(HEAD), &(TAIL), s); \
23266 s->x = (X); \
23267 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23268 } while (0)
23271 /* Add a glyph string for a sequence of glyphless character's glyphs
23272 to the list of strings between HEAD and TAIL. The meanings of
23273 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23275 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23276 do \
23278 int face_id; \
23280 face_id = (row)->glyphs[area][START].face_id; \
23282 s = alloca (sizeof *s); \
23283 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23284 append_glyph_string (&HEAD, &TAIL, s); \
23285 s->x = (X); \
23286 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23287 overlaps); \
23289 while (0)
23292 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23293 of AREA of glyph row ROW on window W between indices START and END.
23294 HL overrides the face for drawing glyph strings, e.g. it is
23295 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23296 x-positions of the drawing area.
23298 This is an ugly monster macro construct because we must use alloca
23299 to allocate glyph strings (because draw_glyphs can be called
23300 asynchronously). */
23302 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23303 do \
23305 HEAD = TAIL = NULL; \
23306 while (START < END) \
23308 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23309 switch (first_glyph->type) \
23311 case CHAR_GLYPH: \
23312 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23313 HL, X, LAST_X); \
23314 break; \
23316 case COMPOSITE_GLYPH: \
23317 if (first_glyph->u.cmp.automatic) \
23318 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23319 HL, X, LAST_X); \
23320 else \
23321 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23322 HL, X, LAST_X); \
23323 break; \
23325 case STRETCH_GLYPH: \
23326 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23327 HL, X, LAST_X); \
23328 break; \
23330 case IMAGE_GLYPH: \
23331 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23332 HL, X, LAST_X); \
23333 break; \
23335 case GLYPHLESS_GLYPH: \
23336 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23337 HL, X, LAST_X); \
23338 break; \
23340 default: \
23341 abort (); \
23344 if (s) \
23346 set_glyph_string_background_width (s, START, LAST_X); \
23347 (X) += s->width; \
23350 } while (0)
23353 /* Draw glyphs between START and END in AREA of ROW on window W,
23354 starting at x-position X. X is relative to AREA in W. HL is a
23355 face-override with the following meaning:
23357 DRAW_NORMAL_TEXT draw normally
23358 DRAW_CURSOR draw in cursor face
23359 DRAW_MOUSE_FACE draw in mouse face.
23360 DRAW_INVERSE_VIDEO draw in mode line face
23361 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23362 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23364 If OVERLAPS is non-zero, draw only the foreground of characters and
23365 clip to the physical height of ROW. Non-zero value also defines
23366 the overlapping part to be drawn:
23368 OVERLAPS_PRED overlap with preceding rows
23369 OVERLAPS_SUCC overlap with succeeding rows
23370 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23371 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23373 Value is the x-position reached, relative to AREA of W. */
23375 static int
23376 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23377 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23378 enum draw_glyphs_face hl, int overlaps)
23380 struct glyph_string *head, *tail;
23381 struct glyph_string *s;
23382 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23383 int i, j, x_reached, last_x, area_left = 0;
23384 struct frame *f = XFRAME (WINDOW_FRAME (w));
23385 DECLARE_HDC (hdc);
23387 ALLOCATE_HDC (hdc, f);
23389 /* Let's rather be paranoid than getting a SEGV. */
23390 end = min (end, row->used[area]);
23391 start = max (0, start);
23392 start = min (end, start);
23394 /* Translate X to frame coordinates. Set last_x to the right
23395 end of the drawing area. */
23396 if (row->full_width_p)
23398 /* X is relative to the left edge of W, without scroll bars
23399 or fringes. */
23400 area_left = WINDOW_LEFT_EDGE_X (w);
23401 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23403 else
23405 area_left = window_box_left (w, area);
23406 last_x = area_left + window_box_width (w, area);
23408 x += area_left;
23410 /* Build a doubly-linked list of glyph_string structures between
23411 head and tail from what we have to draw. Note that the macro
23412 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23413 the reason we use a separate variable `i'. */
23414 i = start;
23415 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23416 if (tail)
23417 x_reached = tail->x + tail->background_width;
23418 else
23419 x_reached = x;
23421 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23422 the row, redraw some glyphs in front or following the glyph
23423 strings built above. */
23424 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23426 struct glyph_string *h, *t;
23427 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23428 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23429 int check_mouse_face = 0;
23430 int dummy_x = 0;
23432 /* If mouse highlighting is on, we may need to draw adjacent
23433 glyphs using mouse-face highlighting. */
23434 if (area == TEXT_AREA && row->mouse_face_p)
23436 struct glyph_row *mouse_beg_row, *mouse_end_row;
23438 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23439 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23441 if (row >= mouse_beg_row && row <= mouse_end_row)
23443 check_mouse_face = 1;
23444 mouse_beg_col = (row == mouse_beg_row)
23445 ? hlinfo->mouse_face_beg_col : 0;
23446 mouse_end_col = (row == mouse_end_row)
23447 ? hlinfo->mouse_face_end_col
23448 : row->used[TEXT_AREA];
23452 /* Compute overhangs for all glyph strings. */
23453 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23454 for (s = head; s; s = s->next)
23455 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23457 /* Prepend glyph strings for glyphs in front of the first glyph
23458 string that are overwritten because of the first glyph
23459 string's left overhang. The background of all strings
23460 prepended must be drawn because the first glyph string
23461 draws over it. */
23462 i = left_overwritten (head);
23463 if (i >= 0)
23465 enum draw_glyphs_face overlap_hl;
23467 /* If this row contains mouse highlighting, attempt to draw
23468 the overlapped glyphs with the correct highlight. This
23469 code fails if the overlap encompasses more than one glyph
23470 and mouse-highlight spans only some of these glyphs.
23471 However, making it work perfectly involves a lot more
23472 code, and I don't know if the pathological case occurs in
23473 practice, so we'll stick to this for now. --- cyd */
23474 if (check_mouse_face
23475 && mouse_beg_col < start && mouse_end_col > i)
23476 overlap_hl = DRAW_MOUSE_FACE;
23477 else
23478 overlap_hl = DRAW_NORMAL_TEXT;
23480 j = i;
23481 BUILD_GLYPH_STRINGS (j, start, h, t,
23482 overlap_hl, dummy_x, last_x);
23483 start = i;
23484 compute_overhangs_and_x (t, head->x, 1);
23485 prepend_glyph_string_lists (&head, &tail, h, t);
23486 clip_head = head;
23489 /* Prepend glyph strings for glyphs in front of the first glyph
23490 string that overwrite that glyph string because of their
23491 right overhang. For these strings, only the foreground must
23492 be drawn, because it draws over the glyph string at `head'.
23493 The background must not be drawn because this would overwrite
23494 right overhangs of preceding glyphs for which no glyph
23495 strings exist. */
23496 i = left_overwriting (head);
23497 if (i >= 0)
23499 enum draw_glyphs_face overlap_hl;
23501 if (check_mouse_face
23502 && mouse_beg_col < start && mouse_end_col > i)
23503 overlap_hl = DRAW_MOUSE_FACE;
23504 else
23505 overlap_hl = DRAW_NORMAL_TEXT;
23507 clip_head = head;
23508 BUILD_GLYPH_STRINGS (i, start, h, t,
23509 overlap_hl, dummy_x, last_x);
23510 for (s = h; s; s = s->next)
23511 s->background_filled_p = 1;
23512 compute_overhangs_and_x (t, head->x, 1);
23513 prepend_glyph_string_lists (&head, &tail, h, t);
23516 /* Append glyphs strings for glyphs following the last glyph
23517 string tail that are overwritten by tail. The background of
23518 these strings has to be drawn because tail's foreground draws
23519 over it. */
23520 i = right_overwritten (tail);
23521 if (i >= 0)
23523 enum draw_glyphs_face overlap_hl;
23525 if (check_mouse_face
23526 && mouse_beg_col < i && mouse_end_col > end)
23527 overlap_hl = DRAW_MOUSE_FACE;
23528 else
23529 overlap_hl = DRAW_NORMAL_TEXT;
23531 BUILD_GLYPH_STRINGS (end, i, h, t,
23532 overlap_hl, x, last_x);
23533 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23534 we don't have `end = i;' here. */
23535 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23536 append_glyph_string_lists (&head, &tail, h, t);
23537 clip_tail = tail;
23540 /* Append glyph strings for glyphs following the last glyph
23541 string tail that overwrite tail. The foreground of such
23542 glyphs has to be drawn because it writes into the background
23543 of tail. The background must not be drawn because it could
23544 paint over the foreground of following glyphs. */
23545 i = right_overwriting (tail);
23546 if (i >= 0)
23548 enum draw_glyphs_face overlap_hl;
23549 if (check_mouse_face
23550 && mouse_beg_col < i && mouse_end_col > end)
23551 overlap_hl = DRAW_MOUSE_FACE;
23552 else
23553 overlap_hl = DRAW_NORMAL_TEXT;
23555 clip_tail = tail;
23556 i++; /* We must include the Ith glyph. */
23557 BUILD_GLYPH_STRINGS (end, i, h, t,
23558 overlap_hl, x, last_x);
23559 for (s = h; s; s = s->next)
23560 s->background_filled_p = 1;
23561 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23562 append_glyph_string_lists (&head, &tail, h, t);
23564 if (clip_head || clip_tail)
23565 for (s = head; s; s = s->next)
23567 s->clip_head = clip_head;
23568 s->clip_tail = clip_tail;
23572 /* Draw all strings. */
23573 for (s = head; s; s = s->next)
23574 FRAME_RIF (f)->draw_glyph_string (s);
23576 #ifndef HAVE_NS
23577 /* When focus a sole frame and move horizontally, this sets on_p to 0
23578 causing a failure to erase prev cursor position. */
23579 if (area == TEXT_AREA
23580 && !row->full_width_p
23581 /* When drawing overlapping rows, only the glyph strings'
23582 foreground is drawn, which doesn't erase a cursor
23583 completely. */
23584 && !overlaps)
23586 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23587 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23588 : (tail ? tail->x + tail->background_width : x));
23589 x0 -= area_left;
23590 x1 -= area_left;
23592 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23593 row->y, MATRIX_ROW_BOTTOM_Y (row));
23595 #endif
23597 /* Value is the x-position up to which drawn, relative to AREA of W.
23598 This doesn't include parts drawn because of overhangs. */
23599 if (row->full_width_p)
23600 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23601 else
23602 x_reached -= area_left;
23604 RELEASE_HDC (hdc, f);
23606 return x_reached;
23609 /* Expand row matrix if too narrow. Don't expand if area
23610 is not present. */
23612 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23614 if (!fonts_changed_p \
23615 && (it->glyph_row->glyphs[area] \
23616 < it->glyph_row->glyphs[area + 1])) \
23618 it->w->ncols_scale_factor++; \
23619 fonts_changed_p = 1; \
23623 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23624 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23626 static inline void
23627 append_glyph (struct it *it)
23629 struct glyph *glyph;
23630 enum glyph_row_area area = it->area;
23632 eassert (it->glyph_row);
23633 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23635 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23636 if (glyph < it->glyph_row->glyphs[area + 1])
23638 /* If the glyph row is reversed, we need to prepend the glyph
23639 rather than append it. */
23640 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23642 struct glyph *g;
23644 /* Make room for the additional glyph. */
23645 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23646 g[1] = *g;
23647 glyph = it->glyph_row->glyphs[area];
23649 glyph->charpos = CHARPOS (it->position);
23650 glyph->object = it->object;
23651 if (it->pixel_width > 0)
23653 glyph->pixel_width = it->pixel_width;
23654 glyph->padding_p = 0;
23656 else
23658 /* Assure at least 1-pixel width. Otherwise, cursor can't
23659 be displayed correctly. */
23660 glyph->pixel_width = 1;
23661 glyph->padding_p = 1;
23663 glyph->ascent = it->ascent;
23664 glyph->descent = it->descent;
23665 glyph->voffset = it->voffset;
23666 glyph->type = CHAR_GLYPH;
23667 glyph->avoid_cursor_p = it->avoid_cursor_p;
23668 glyph->multibyte_p = it->multibyte_p;
23669 glyph->left_box_line_p = it->start_of_box_run_p;
23670 glyph->right_box_line_p = it->end_of_box_run_p;
23671 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23672 || it->phys_descent > it->descent);
23673 glyph->glyph_not_available_p = it->glyph_not_available_p;
23674 glyph->face_id = it->face_id;
23675 glyph->u.ch = it->char_to_display;
23676 glyph->slice.img = null_glyph_slice;
23677 glyph->font_type = FONT_TYPE_UNKNOWN;
23678 if (it->bidi_p)
23680 glyph->resolved_level = it->bidi_it.resolved_level;
23681 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23682 abort ();
23683 glyph->bidi_type = it->bidi_it.type;
23685 else
23687 glyph->resolved_level = 0;
23688 glyph->bidi_type = UNKNOWN_BT;
23690 ++it->glyph_row->used[area];
23692 else
23693 IT_EXPAND_MATRIX_WIDTH (it, area);
23696 /* Store one glyph for the composition IT->cmp_it.id in
23697 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23698 non-null. */
23700 static inline void
23701 append_composite_glyph (struct it *it)
23703 struct glyph *glyph;
23704 enum glyph_row_area area = it->area;
23706 eassert (it->glyph_row);
23708 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23709 if (glyph < it->glyph_row->glyphs[area + 1])
23711 /* If the glyph row is reversed, we need to prepend the glyph
23712 rather than append it. */
23713 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23715 struct glyph *g;
23717 /* Make room for the new glyph. */
23718 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23719 g[1] = *g;
23720 glyph = it->glyph_row->glyphs[it->area];
23722 glyph->charpos = it->cmp_it.charpos;
23723 glyph->object = it->object;
23724 glyph->pixel_width = it->pixel_width;
23725 glyph->ascent = it->ascent;
23726 glyph->descent = it->descent;
23727 glyph->voffset = it->voffset;
23728 glyph->type = COMPOSITE_GLYPH;
23729 if (it->cmp_it.ch < 0)
23731 glyph->u.cmp.automatic = 0;
23732 glyph->u.cmp.id = it->cmp_it.id;
23733 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23735 else
23737 glyph->u.cmp.automatic = 1;
23738 glyph->u.cmp.id = it->cmp_it.id;
23739 glyph->slice.cmp.from = it->cmp_it.from;
23740 glyph->slice.cmp.to = it->cmp_it.to - 1;
23742 glyph->avoid_cursor_p = it->avoid_cursor_p;
23743 glyph->multibyte_p = it->multibyte_p;
23744 glyph->left_box_line_p = it->start_of_box_run_p;
23745 glyph->right_box_line_p = it->end_of_box_run_p;
23746 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23747 || it->phys_descent > it->descent);
23748 glyph->padding_p = 0;
23749 glyph->glyph_not_available_p = 0;
23750 glyph->face_id = it->face_id;
23751 glyph->font_type = FONT_TYPE_UNKNOWN;
23752 if (it->bidi_p)
23754 glyph->resolved_level = it->bidi_it.resolved_level;
23755 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23756 abort ();
23757 glyph->bidi_type = it->bidi_it.type;
23759 ++it->glyph_row->used[area];
23761 else
23762 IT_EXPAND_MATRIX_WIDTH (it, area);
23766 /* Change IT->ascent and IT->height according to the setting of
23767 IT->voffset. */
23769 static inline void
23770 take_vertical_position_into_account (struct it *it)
23772 if (it->voffset)
23774 if (it->voffset < 0)
23775 /* Increase the ascent so that we can display the text higher
23776 in the line. */
23777 it->ascent -= it->voffset;
23778 else
23779 /* Increase the descent so that we can display the text lower
23780 in the line. */
23781 it->descent += it->voffset;
23786 /* Produce glyphs/get display metrics for the image IT is loaded with.
23787 See the description of struct display_iterator in dispextern.h for
23788 an overview of struct display_iterator. */
23790 static void
23791 produce_image_glyph (struct it *it)
23793 struct image *img;
23794 struct face *face;
23795 int glyph_ascent, crop;
23796 struct glyph_slice slice;
23798 eassert (it->what == IT_IMAGE);
23800 face = FACE_FROM_ID (it->f, it->face_id);
23801 eassert (face);
23802 /* Make sure X resources of the face is loaded. */
23803 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23805 if (it->image_id < 0)
23807 /* Fringe bitmap. */
23808 it->ascent = it->phys_ascent = 0;
23809 it->descent = it->phys_descent = 0;
23810 it->pixel_width = 0;
23811 it->nglyphs = 0;
23812 return;
23815 img = IMAGE_FROM_ID (it->f, it->image_id);
23816 eassert (img);
23817 /* Make sure X resources of the image is loaded. */
23818 prepare_image_for_display (it->f, img);
23820 slice.x = slice.y = 0;
23821 slice.width = img->width;
23822 slice.height = img->height;
23824 if (INTEGERP (it->slice.x))
23825 slice.x = XINT (it->slice.x);
23826 else if (FLOATP (it->slice.x))
23827 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23829 if (INTEGERP (it->slice.y))
23830 slice.y = XINT (it->slice.y);
23831 else if (FLOATP (it->slice.y))
23832 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23834 if (INTEGERP (it->slice.width))
23835 slice.width = XINT (it->slice.width);
23836 else if (FLOATP (it->slice.width))
23837 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23839 if (INTEGERP (it->slice.height))
23840 slice.height = XINT (it->slice.height);
23841 else if (FLOATP (it->slice.height))
23842 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23844 if (slice.x >= img->width)
23845 slice.x = img->width;
23846 if (slice.y >= img->height)
23847 slice.y = img->height;
23848 if (slice.x + slice.width >= img->width)
23849 slice.width = img->width - slice.x;
23850 if (slice.y + slice.height > img->height)
23851 slice.height = img->height - slice.y;
23853 if (slice.width == 0 || slice.height == 0)
23854 return;
23856 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23858 it->descent = slice.height - glyph_ascent;
23859 if (slice.y == 0)
23860 it->descent += img->vmargin;
23861 if (slice.y + slice.height == img->height)
23862 it->descent += img->vmargin;
23863 it->phys_descent = it->descent;
23865 it->pixel_width = slice.width;
23866 if (slice.x == 0)
23867 it->pixel_width += img->hmargin;
23868 if (slice.x + slice.width == img->width)
23869 it->pixel_width += img->hmargin;
23871 /* It's quite possible for images to have an ascent greater than
23872 their height, so don't get confused in that case. */
23873 if (it->descent < 0)
23874 it->descent = 0;
23876 it->nglyphs = 1;
23878 if (face->box != FACE_NO_BOX)
23880 if (face->box_line_width > 0)
23882 if (slice.y == 0)
23883 it->ascent += face->box_line_width;
23884 if (slice.y + slice.height == img->height)
23885 it->descent += face->box_line_width;
23888 if (it->start_of_box_run_p && slice.x == 0)
23889 it->pixel_width += eabs (face->box_line_width);
23890 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23891 it->pixel_width += eabs (face->box_line_width);
23894 take_vertical_position_into_account (it);
23896 /* Automatically crop wide image glyphs at right edge so we can
23897 draw the cursor on same display row. */
23898 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23899 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23901 it->pixel_width -= crop;
23902 slice.width -= crop;
23905 if (it->glyph_row)
23907 struct glyph *glyph;
23908 enum glyph_row_area area = it->area;
23910 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23911 if (glyph < it->glyph_row->glyphs[area + 1])
23913 glyph->charpos = CHARPOS (it->position);
23914 glyph->object = it->object;
23915 glyph->pixel_width = it->pixel_width;
23916 glyph->ascent = glyph_ascent;
23917 glyph->descent = it->descent;
23918 glyph->voffset = it->voffset;
23919 glyph->type = IMAGE_GLYPH;
23920 glyph->avoid_cursor_p = it->avoid_cursor_p;
23921 glyph->multibyte_p = it->multibyte_p;
23922 glyph->left_box_line_p = it->start_of_box_run_p;
23923 glyph->right_box_line_p = it->end_of_box_run_p;
23924 glyph->overlaps_vertically_p = 0;
23925 glyph->padding_p = 0;
23926 glyph->glyph_not_available_p = 0;
23927 glyph->face_id = it->face_id;
23928 glyph->u.img_id = img->id;
23929 glyph->slice.img = slice;
23930 glyph->font_type = FONT_TYPE_UNKNOWN;
23931 if (it->bidi_p)
23933 glyph->resolved_level = it->bidi_it.resolved_level;
23934 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23935 abort ();
23936 glyph->bidi_type = it->bidi_it.type;
23938 ++it->glyph_row->used[area];
23940 else
23941 IT_EXPAND_MATRIX_WIDTH (it, area);
23946 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23947 of the glyph, WIDTH and HEIGHT are the width and height of the
23948 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23950 static void
23951 append_stretch_glyph (struct it *it, Lisp_Object object,
23952 int width, int height, int ascent)
23954 struct glyph *glyph;
23955 enum glyph_row_area area = it->area;
23957 eassert (ascent >= 0 && ascent <= height);
23959 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23960 if (glyph < it->glyph_row->glyphs[area + 1])
23962 /* If the glyph row is reversed, we need to prepend the glyph
23963 rather than append it. */
23964 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23966 struct glyph *g;
23968 /* Make room for the additional glyph. */
23969 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23970 g[1] = *g;
23971 glyph = it->glyph_row->glyphs[area];
23973 glyph->charpos = CHARPOS (it->position);
23974 glyph->object = object;
23975 glyph->pixel_width = width;
23976 glyph->ascent = ascent;
23977 glyph->descent = height - ascent;
23978 glyph->voffset = it->voffset;
23979 glyph->type = STRETCH_GLYPH;
23980 glyph->avoid_cursor_p = it->avoid_cursor_p;
23981 glyph->multibyte_p = it->multibyte_p;
23982 glyph->left_box_line_p = it->start_of_box_run_p;
23983 glyph->right_box_line_p = it->end_of_box_run_p;
23984 glyph->overlaps_vertically_p = 0;
23985 glyph->padding_p = 0;
23986 glyph->glyph_not_available_p = 0;
23987 glyph->face_id = it->face_id;
23988 glyph->u.stretch.ascent = ascent;
23989 glyph->u.stretch.height = height;
23990 glyph->slice.img = null_glyph_slice;
23991 glyph->font_type = FONT_TYPE_UNKNOWN;
23992 if (it->bidi_p)
23994 glyph->resolved_level = it->bidi_it.resolved_level;
23995 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23996 abort ();
23997 glyph->bidi_type = it->bidi_it.type;
23999 else
24001 glyph->resolved_level = 0;
24002 glyph->bidi_type = UNKNOWN_BT;
24004 ++it->glyph_row->used[area];
24006 else
24007 IT_EXPAND_MATRIX_WIDTH (it, area);
24010 #endif /* HAVE_WINDOW_SYSTEM */
24012 /* Produce a stretch glyph for iterator IT. IT->object is the value
24013 of the glyph property displayed. The value must be a list
24014 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24015 being recognized:
24017 1. `:width WIDTH' specifies that the space should be WIDTH *
24018 canonical char width wide. WIDTH may be an integer or floating
24019 point number.
24021 2. `:relative-width FACTOR' specifies that the width of the stretch
24022 should be computed from the width of the first character having the
24023 `glyph' property, and should be FACTOR times that width.
24025 3. `:align-to HPOS' specifies that the space should be wide enough
24026 to reach HPOS, a value in canonical character units.
24028 Exactly one of the above pairs must be present.
24030 4. `:height HEIGHT' specifies that the height of the stretch produced
24031 should be HEIGHT, measured in canonical character units.
24033 5. `:relative-height FACTOR' specifies that the height of the
24034 stretch should be FACTOR times the height of the characters having
24035 the glyph property.
24037 Either none or exactly one of 4 or 5 must be present.
24039 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24040 of the stretch should be used for the ascent of the stretch.
24041 ASCENT must be in the range 0 <= ASCENT <= 100. */
24043 void
24044 produce_stretch_glyph (struct it *it)
24046 /* (space :width WIDTH :height HEIGHT ...) */
24047 Lisp_Object prop, plist;
24048 int width = 0, height = 0, align_to = -1;
24049 int zero_width_ok_p = 0;
24050 int ascent = 0;
24051 double tem;
24052 struct face *face = NULL;
24053 struct font *font = NULL;
24055 #ifdef HAVE_WINDOW_SYSTEM
24056 int zero_height_ok_p = 0;
24058 if (FRAME_WINDOW_P (it->f))
24060 face = FACE_FROM_ID (it->f, it->face_id);
24061 font = face->font ? face->font : FRAME_FONT (it->f);
24062 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24064 #endif
24066 /* List should start with `space'. */
24067 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24068 plist = XCDR (it->object);
24070 /* Compute the width of the stretch. */
24071 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24072 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24074 /* Absolute width `:width WIDTH' specified and valid. */
24075 zero_width_ok_p = 1;
24076 width = (int)tem;
24078 #ifdef HAVE_WINDOW_SYSTEM
24079 else if (FRAME_WINDOW_P (it->f)
24080 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24082 /* Relative width `:relative-width FACTOR' specified and valid.
24083 Compute the width of the characters having the `glyph'
24084 property. */
24085 struct it it2;
24086 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24088 it2 = *it;
24089 if (it->multibyte_p)
24090 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24091 else
24093 it2.c = it2.char_to_display = *p, it2.len = 1;
24094 if (! ASCII_CHAR_P (it2.c))
24095 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24098 it2.glyph_row = NULL;
24099 it2.what = IT_CHARACTER;
24100 x_produce_glyphs (&it2);
24101 width = NUMVAL (prop) * it2.pixel_width;
24103 #endif /* HAVE_WINDOW_SYSTEM */
24104 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24105 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24107 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24108 align_to = (align_to < 0
24110 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24111 else if (align_to < 0)
24112 align_to = window_box_left_offset (it->w, TEXT_AREA);
24113 width = max (0, (int)tem + align_to - it->current_x);
24114 zero_width_ok_p = 1;
24116 else
24117 /* Nothing specified -> width defaults to canonical char width. */
24118 width = FRAME_COLUMN_WIDTH (it->f);
24120 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24121 width = 1;
24123 #ifdef HAVE_WINDOW_SYSTEM
24124 /* Compute height. */
24125 if (FRAME_WINDOW_P (it->f))
24127 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24128 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24130 height = (int)tem;
24131 zero_height_ok_p = 1;
24133 else if (prop = Fplist_get (plist, QCrelative_height),
24134 NUMVAL (prop) > 0)
24135 height = FONT_HEIGHT (font) * NUMVAL (prop);
24136 else
24137 height = FONT_HEIGHT (font);
24139 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24140 height = 1;
24142 /* Compute percentage of height used for ascent. If
24143 `:ascent ASCENT' is present and valid, use that. Otherwise,
24144 derive the ascent from the font in use. */
24145 if (prop = Fplist_get (plist, QCascent),
24146 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24147 ascent = height * NUMVAL (prop) / 100.0;
24148 else if (!NILP (prop)
24149 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24150 ascent = min (max (0, (int)tem), height);
24151 else
24152 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24154 else
24155 #endif /* HAVE_WINDOW_SYSTEM */
24156 height = 1;
24158 if (width > 0 && it->line_wrap != TRUNCATE
24159 && it->current_x + width > it->last_visible_x)
24161 width = it->last_visible_x - it->current_x;
24162 #ifdef HAVE_WINDOW_SYSTEM
24163 /* Subtract one more pixel from the stretch width, but only on
24164 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24165 width -= FRAME_WINDOW_P (it->f);
24166 #endif
24169 if (width > 0 && height > 0 && it->glyph_row)
24171 Lisp_Object o_object = it->object;
24172 Lisp_Object object = it->stack[it->sp - 1].string;
24173 int n = width;
24175 if (!STRINGP (object))
24176 object = it->w->buffer;
24177 #ifdef HAVE_WINDOW_SYSTEM
24178 if (FRAME_WINDOW_P (it->f))
24179 append_stretch_glyph (it, object, width, height, ascent);
24180 else
24181 #endif
24183 it->object = object;
24184 it->char_to_display = ' ';
24185 it->pixel_width = it->len = 1;
24186 while (n--)
24187 tty_append_glyph (it);
24188 it->object = o_object;
24192 it->pixel_width = width;
24193 #ifdef HAVE_WINDOW_SYSTEM
24194 if (FRAME_WINDOW_P (it->f))
24196 it->ascent = it->phys_ascent = ascent;
24197 it->descent = it->phys_descent = height - it->ascent;
24198 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24199 take_vertical_position_into_account (it);
24201 else
24202 #endif
24203 it->nglyphs = width;
24206 /* Get information about special display element WHAT in an
24207 environment described by IT. WHAT is one of IT_TRUNCATION or
24208 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24209 non-null glyph_row member. This function ensures that fields like
24210 face_id, c, len of IT are left untouched. */
24212 void
24213 produce_special_glyphs (struct it *it, enum display_element_type what)
24215 struct it temp_it;
24216 Lisp_Object gc;
24217 GLYPH glyph;
24219 temp_it = *it;
24220 temp_it.dp = NULL;
24221 temp_it.what = IT_CHARACTER;
24222 temp_it.len = 1;
24223 temp_it.object = make_number (0);
24224 memset (&temp_it.current, 0, sizeof temp_it.current);
24226 if (what == IT_CONTINUATION)
24228 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24229 if (it->bidi_it.paragraph_dir == R2L)
24230 SET_GLYPH_FROM_CHAR (glyph, '/');
24231 else
24232 SET_GLYPH_FROM_CHAR (glyph, '\\');
24233 if (it->dp
24234 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24236 /* FIXME: Should we mirror GC for R2L lines? */
24237 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24238 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24241 else if (what == IT_TRUNCATION)
24243 /* Truncation glyph. */
24244 SET_GLYPH_FROM_CHAR (glyph, '$');
24245 if (it->dp
24246 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24248 /* FIXME: Should we mirror GC for R2L lines? */
24249 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24250 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24253 else
24254 abort ();
24256 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24257 temp_it.face_id = GLYPH_FACE (glyph);
24258 temp_it.len = CHAR_BYTES (temp_it.c);
24260 PRODUCE_GLYPHS (&temp_it);
24261 it->pixel_width = temp_it.pixel_width;
24262 it->nglyphs = temp_it.pixel_width;
24265 #ifdef HAVE_WINDOW_SYSTEM
24267 /* Calculate line-height and line-spacing properties.
24268 An integer value specifies explicit pixel value.
24269 A float value specifies relative value to current face height.
24270 A cons (float . face-name) specifies relative value to
24271 height of specified face font.
24273 Returns height in pixels, or nil. */
24276 static Lisp_Object
24277 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24278 int boff, int override)
24280 Lisp_Object face_name = Qnil;
24281 int ascent, descent, height;
24283 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24284 return val;
24286 if (CONSP (val))
24288 face_name = XCAR (val);
24289 val = XCDR (val);
24290 if (!NUMBERP (val))
24291 val = make_number (1);
24292 if (NILP (face_name))
24294 height = it->ascent + it->descent;
24295 goto scale;
24299 if (NILP (face_name))
24301 font = FRAME_FONT (it->f);
24302 boff = FRAME_BASELINE_OFFSET (it->f);
24304 else if (EQ (face_name, Qt))
24306 override = 0;
24308 else
24310 int face_id;
24311 struct face *face;
24313 face_id = lookup_named_face (it->f, face_name, 0);
24314 if (face_id < 0)
24315 return make_number (-1);
24317 face = FACE_FROM_ID (it->f, face_id);
24318 font = face->font;
24319 if (font == NULL)
24320 return make_number (-1);
24321 boff = font->baseline_offset;
24322 if (font->vertical_centering)
24323 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24326 ascent = FONT_BASE (font) + boff;
24327 descent = FONT_DESCENT (font) - boff;
24329 if (override)
24331 it->override_ascent = ascent;
24332 it->override_descent = descent;
24333 it->override_boff = boff;
24336 height = ascent + descent;
24338 scale:
24339 if (FLOATP (val))
24340 height = (int)(XFLOAT_DATA (val) * height);
24341 else if (INTEGERP (val))
24342 height *= XINT (val);
24344 return make_number (height);
24348 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24349 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24350 and only if this is for a character for which no font was found.
24352 If the display method (it->glyphless_method) is
24353 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24354 length of the acronym or the hexadecimal string, UPPER_XOFF and
24355 UPPER_YOFF are pixel offsets for the upper part of the string,
24356 LOWER_XOFF and LOWER_YOFF are for the lower part.
24358 For the other display methods, LEN through LOWER_YOFF are zero. */
24360 static void
24361 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24362 short upper_xoff, short upper_yoff,
24363 short lower_xoff, short lower_yoff)
24365 struct glyph *glyph;
24366 enum glyph_row_area area = it->area;
24368 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24369 if (glyph < it->glyph_row->glyphs[area + 1])
24371 /* If the glyph row is reversed, we need to prepend the glyph
24372 rather than append it. */
24373 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24375 struct glyph *g;
24377 /* Make room for the additional glyph. */
24378 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24379 g[1] = *g;
24380 glyph = it->glyph_row->glyphs[area];
24382 glyph->charpos = CHARPOS (it->position);
24383 glyph->object = it->object;
24384 glyph->pixel_width = it->pixel_width;
24385 glyph->ascent = it->ascent;
24386 glyph->descent = it->descent;
24387 glyph->voffset = it->voffset;
24388 glyph->type = GLYPHLESS_GLYPH;
24389 glyph->u.glyphless.method = it->glyphless_method;
24390 glyph->u.glyphless.for_no_font = for_no_font;
24391 glyph->u.glyphless.len = len;
24392 glyph->u.glyphless.ch = it->c;
24393 glyph->slice.glyphless.upper_xoff = upper_xoff;
24394 glyph->slice.glyphless.upper_yoff = upper_yoff;
24395 glyph->slice.glyphless.lower_xoff = lower_xoff;
24396 glyph->slice.glyphless.lower_yoff = lower_yoff;
24397 glyph->avoid_cursor_p = it->avoid_cursor_p;
24398 glyph->multibyte_p = it->multibyte_p;
24399 glyph->left_box_line_p = it->start_of_box_run_p;
24400 glyph->right_box_line_p = it->end_of_box_run_p;
24401 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24402 || it->phys_descent > it->descent);
24403 glyph->padding_p = 0;
24404 glyph->glyph_not_available_p = 0;
24405 glyph->face_id = face_id;
24406 glyph->font_type = FONT_TYPE_UNKNOWN;
24407 if (it->bidi_p)
24409 glyph->resolved_level = it->bidi_it.resolved_level;
24410 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24411 abort ();
24412 glyph->bidi_type = it->bidi_it.type;
24414 ++it->glyph_row->used[area];
24416 else
24417 IT_EXPAND_MATRIX_WIDTH (it, area);
24421 /* Produce a glyph for a glyphless character for iterator IT.
24422 IT->glyphless_method specifies which method to use for displaying
24423 the character. See the description of enum
24424 glyphless_display_method in dispextern.h for the detail.
24426 FOR_NO_FONT is nonzero if and only if this is for a character for
24427 which no font was found. ACRONYM, if non-nil, is an acronym string
24428 for the character. */
24430 static void
24431 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24433 int face_id;
24434 struct face *face;
24435 struct font *font;
24436 int base_width, base_height, width, height;
24437 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24438 int len;
24440 /* Get the metrics of the base font. We always refer to the current
24441 ASCII face. */
24442 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24443 font = face->font ? face->font : FRAME_FONT (it->f);
24444 it->ascent = FONT_BASE (font) + font->baseline_offset;
24445 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24446 base_height = it->ascent + it->descent;
24447 base_width = font->average_width;
24449 /* Get a face ID for the glyph by utilizing a cache (the same way as
24450 done for `escape-glyph' in get_next_display_element). */
24451 if (it->f == last_glyphless_glyph_frame
24452 && it->face_id == last_glyphless_glyph_face_id)
24454 face_id = last_glyphless_glyph_merged_face_id;
24456 else
24458 /* Merge the `glyphless-char' face into the current face. */
24459 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24460 last_glyphless_glyph_frame = it->f;
24461 last_glyphless_glyph_face_id = it->face_id;
24462 last_glyphless_glyph_merged_face_id = face_id;
24465 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24467 it->pixel_width = THIN_SPACE_WIDTH;
24468 len = 0;
24469 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24471 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24473 width = CHAR_WIDTH (it->c);
24474 if (width == 0)
24475 width = 1;
24476 else if (width > 4)
24477 width = 4;
24478 it->pixel_width = base_width * width;
24479 len = 0;
24480 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24482 else
24484 char buf[7];
24485 const char *str;
24486 unsigned int code[6];
24487 int upper_len;
24488 int ascent, descent;
24489 struct font_metrics metrics_upper, metrics_lower;
24491 face = FACE_FROM_ID (it->f, face_id);
24492 font = face->font ? face->font : FRAME_FONT (it->f);
24493 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24495 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24497 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24498 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24499 if (CONSP (acronym))
24500 acronym = XCAR (acronym);
24501 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24503 else
24505 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24506 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24507 str = buf;
24509 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24510 code[len] = font->driver->encode_char (font, str[len]);
24511 upper_len = (len + 1) / 2;
24512 font->driver->text_extents (font, code, upper_len,
24513 &metrics_upper);
24514 font->driver->text_extents (font, code + upper_len, len - upper_len,
24515 &metrics_lower);
24519 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24520 width = max (metrics_upper.width, metrics_lower.width) + 4;
24521 upper_xoff = upper_yoff = 2; /* the typical case */
24522 if (base_width >= width)
24524 /* Align the upper to the left, the lower to the right. */
24525 it->pixel_width = base_width;
24526 lower_xoff = base_width - 2 - metrics_lower.width;
24528 else
24530 /* Center the shorter one. */
24531 it->pixel_width = width;
24532 if (metrics_upper.width >= metrics_lower.width)
24533 lower_xoff = (width - metrics_lower.width) / 2;
24534 else
24536 /* FIXME: This code doesn't look right. It formerly was
24537 missing the "lower_xoff = 0;", which couldn't have
24538 been right since it left lower_xoff uninitialized. */
24539 lower_xoff = 0;
24540 upper_xoff = (width - metrics_upper.width) / 2;
24544 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24545 top, bottom, and between upper and lower strings. */
24546 height = (metrics_upper.ascent + metrics_upper.descent
24547 + metrics_lower.ascent + metrics_lower.descent) + 5;
24548 /* Center vertically.
24549 H:base_height, D:base_descent
24550 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24552 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24553 descent = D - H/2 + h/2;
24554 lower_yoff = descent - 2 - ld;
24555 upper_yoff = lower_yoff - la - 1 - ud; */
24556 ascent = - (it->descent - (base_height + height + 1) / 2);
24557 descent = it->descent - (base_height - height) / 2;
24558 lower_yoff = descent - 2 - metrics_lower.descent;
24559 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24560 - metrics_upper.descent);
24561 /* Don't make the height shorter than the base height. */
24562 if (height > base_height)
24564 it->ascent = ascent;
24565 it->descent = descent;
24569 it->phys_ascent = it->ascent;
24570 it->phys_descent = it->descent;
24571 if (it->glyph_row)
24572 append_glyphless_glyph (it, face_id, for_no_font, len,
24573 upper_xoff, upper_yoff,
24574 lower_xoff, lower_yoff);
24575 it->nglyphs = 1;
24576 take_vertical_position_into_account (it);
24580 /* RIF:
24581 Produce glyphs/get display metrics for the display element IT is
24582 loaded with. See the description of struct it in dispextern.h
24583 for an overview of struct it. */
24585 void
24586 x_produce_glyphs (struct it *it)
24588 int extra_line_spacing = it->extra_line_spacing;
24590 it->glyph_not_available_p = 0;
24592 if (it->what == IT_CHARACTER)
24594 XChar2b char2b;
24595 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24596 struct font *font = face->font;
24597 struct font_metrics *pcm = NULL;
24598 int boff; /* baseline offset */
24600 if (font == NULL)
24602 /* When no suitable font is found, display this character by
24603 the method specified in the first extra slot of
24604 Vglyphless_char_display. */
24605 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24607 eassert (it->what == IT_GLYPHLESS);
24608 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24609 goto done;
24612 boff = font->baseline_offset;
24613 if (font->vertical_centering)
24614 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24616 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24618 int stretched_p;
24620 it->nglyphs = 1;
24622 if (it->override_ascent >= 0)
24624 it->ascent = it->override_ascent;
24625 it->descent = it->override_descent;
24626 boff = it->override_boff;
24628 else
24630 it->ascent = FONT_BASE (font) + boff;
24631 it->descent = FONT_DESCENT (font) - boff;
24634 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24636 pcm = get_per_char_metric (font, &char2b);
24637 if (pcm->width == 0
24638 && pcm->rbearing == 0 && pcm->lbearing == 0)
24639 pcm = NULL;
24642 if (pcm)
24644 it->phys_ascent = pcm->ascent + boff;
24645 it->phys_descent = pcm->descent - boff;
24646 it->pixel_width = pcm->width;
24648 else
24650 it->glyph_not_available_p = 1;
24651 it->phys_ascent = it->ascent;
24652 it->phys_descent = it->descent;
24653 it->pixel_width = font->space_width;
24656 if (it->constrain_row_ascent_descent_p)
24658 if (it->descent > it->max_descent)
24660 it->ascent += it->descent - it->max_descent;
24661 it->descent = it->max_descent;
24663 if (it->ascent > it->max_ascent)
24665 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24666 it->ascent = it->max_ascent;
24668 it->phys_ascent = min (it->phys_ascent, it->ascent);
24669 it->phys_descent = min (it->phys_descent, it->descent);
24670 extra_line_spacing = 0;
24673 /* If this is a space inside a region of text with
24674 `space-width' property, change its width. */
24675 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24676 if (stretched_p)
24677 it->pixel_width *= XFLOATINT (it->space_width);
24679 /* If face has a box, add the box thickness to the character
24680 height. If character has a box line to the left and/or
24681 right, add the box line width to the character's width. */
24682 if (face->box != FACE_NO_BOX)
24684 int thick = face->box_line_width;
24686 if (thick > 0)
24688 it->ascent += thick;
24689 it->descent += thick;
24691 else
24692 thick = -thick;
24694 if (it->start_of_box_run_p)
24695 it->pixel_width += thick;
24696 if (it->end_of_box_run_p)
24697 it->pixel_width += thick;
24700 /* If face has an overline, add the height of the overline
24701 (1 pixel) and a 1 pixel margin to the character height. */
24702 if (face->overline_p)
24703 it->ascent += overline_margin;
24705 if (it->constrain_row_ascent_descent_p)
24707 if (it->ascent > it->max_ascent)
24708 it->ascent = it->max_ascent;
24709 if (it->descent > it->max_descent)
24710 it->descent = it->max_descent;
24713 take_vertical_position_into_account (it);
24715 /* If we have to actually produce glyphs, do it. */
24716 if (it->glyph_row)
24718 if (stretched_p)
24720 /* Translate a space with a `space-width' property
24721 into a stretch glyph. */
24722 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24723 / FONT_HEIGHT (font));
24724 append_stretch_glyph (it, it->object, it->pixel_width,
24725 it->ascent + it->descent, ascent);
24727 else
24728 append_glyph (it);
24730 /* If characters with lbearing or rbearing are displayed
24731 in this line, record that fact in a flag of the
24732 glyph row. This is used to optimize X output code. */
24733 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24734 it->glyph_row->contains_overlapping_glyphs_p = 1;
24736 if (! stretched_p && it->pixel_width == 0)
24737 /* We assure that all visible glyphs have at least 1-pixel
24738 width. */
24739 it->pixel_width = 1;
24741 else if (it->char_to_display == '\n')
24743 /* A newline has no width, but we need the height of the
24744 line. But if previous part of the line sets a height,
24745 don't increase that height */
24747 Lisp_Object height;
24748 Lisp_Object total_height = Qnil;
24750 it->override_ascent = -1;
24751 it->pixel_width = 0;
24752 it->nglyphs = 0;
24754 height = get_it_property (it, Qline_height);
24755 /* Split (line-height total-height) list */
24756 if (CONSP (height)
24757 && CONSP (XCDR (height))
24758 && NILP (XCDR (XCDR (height))))
24760 total_height = XCAR (XCDR (height));
24761 height = XCAR (height);
24763 height = calc_line_height_property (it, height, font, boff, 1);
24765 if (it->override_ascent >= 0)
24767 it->ascent = it->override_ascent;
24768 it->descent = it->override_descent;
24769 boff = it->override_boff;
24771 else
24773 it->ascent = FONT_BASE (font) + boff;
24774 it->descent = FONT_DESCENT (font) - boff;
24777 if (EQ (height, Qt))
24779 if (it->descent > it->max_descent)
24781 it->ascent += it->descent - it->max_descent;
24782 it->descent = it->max_descent;
24784 if (it->ascent > it->max_ascent)
24786 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24787 it->ascent = it->max_ascent;
24789 it->phys_ascent = min (it->phys_ascent, it->ascent);
24790 it->phys_descent = min (it->phys_descent, it->descent);
24791 it->constrain_row_ascent_descent_p = 1;
24792 extra_line_spacing = 0;
24794 else
24796 Lisp_Object spacing;
24798 it->phys_ascent = it->ascent;
24799 it->phys_descent = it->descent;
24801 if ((it->max_ascent > 0 || it->max_descent > 0)
24802 && face->box != FACE_NO_BOX
24803 && face->box_line_width > 0)
24805 it->ascent += face->box_line_width;
24806 it->descent += face->box_line_width;
24808 if (!NILP (height)
24809 && XINT (height) > it->ascent + it->descent)
24810 it->ascent = XINT (height) - it->descent;
24812 if (!NILP (total_height))
24813 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24814 else
24816 spacing = get_it_property (it, Qline_spacing);
24817 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24819 if (INTEGERP (spacing))
24821 extra_line_spacing = XINT (spacing);
24822 if (!NILP (total_height))
24823 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24827 else /* i.e. (it->char_to_display == '\t') */
24829 if (font->space_width > 0)
24831 int tab_width = it->tab_width * font->space_width;
24832 int x = it->current_x + it->continuation_lines_width;
24833 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24835 /* If the distance from the current position to the next tab
24836 stop is less than a space character width, use the
24837 tab stop after that. */
24838 if (next_tab_x - x < font->space_width)
24839 next_tab_x += tab_width;
24841 it->pixel_width = next_tab_x - x;
24842 it->nglyphs = 1;
24843 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24844 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24846 if (it->glyph_row)
24848 append_stretch_glyph (it, it->object, it->pixel_width,
24849 it->ascent + it->descent, it->ascent);
24852 else
24854 it->pixel_width = 0;
24855 it->nglyphs = 1;
24859 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24861 /* A static composition.
24863 Note: A composition is represented as one glyph in the
24864 glyph matrix. There are no padding glyphs.
24866 Important note: pixel_width, ascent, and descent are the
24867 values of what is drawn by draw_glyphs (i.e. the values of
24868 the overall glyphs composed). */
24869 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24870 int boff; /* baseline offset */
24871 struct composition *cmp = composition_table[it->cmp_it.id];
24872 int glyph_len = cmp->glyph_len;
24873 struct font *font = face->font;
24875 it->nglyphs = 1;
24877 /* If we have not yet calculated pixel size data of glyphs of
24878 the composition for the current face font, calculate them
24879 now. Theoretically, we have to check all fonts for the
24880 glyphs, but that requires much time and memory space. So,
24881 here we check only the font of the first glyph. This may
24882 lead to incorrect display, but it's very rare, and C-l
24883 (recenter-top-bottom) can correct the display anyway. */
24884 if (! cmp->font || cmp->font != font)
24886 /* Ascent and descent of the font of the first character
24887 of this composition (adjusted by baseline offset).
24888 Ascent and descent of overall glyphs should not be less
24889 than these, respectively. */
24890 int font_ascent, font_descent, font_height;
24891 /* Bounding box of the overall glyphs. */
24892 int leftmost, rightmost, lowest, highest;
24893 int lbearing, rbearing;
24894 int i, width, ascent, descent;
24895 int left_padded = 0, right_padded = 0;
24896 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24897 XChar2b char2b;
24898 struct font_metrics *pcm;
24899 int font_not_found_p;
24900 ptrdiff_t pos;
24902 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24903 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24904 break;
24905 if (glyph_len < cmp->glyph_len)
24906 right_padded = 1;
24907 for (i = 0; i < glyph_len; i++)
24909 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24910 break;
24911 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24913 if (i > 0)
24914 left_padded = 1;
24916 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24917 : IT_CHARPOS (*it));
24918 /* If no suitable font is found, use the default font. */
24919 font_not_found_p = font == NULL;
24920 if (font_not_found_p)
24922 face = face->ascii_face;
24923 font = face->font;
24925 boff = font->baseline_offset;
24926 if (font->vertical_centering)
24927 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24928 font_ascent = FONT_BASE (font) + boff;
24929 font_descent = FONT_DESCENT (font) - boff;
24930 font_height = FONT_HEIGHT (font);
24932 cmp->font = (void *) font;
24934 pcm = NULL;
24935 if (! font_not_found_p)
24937 get_char_face_and_encoding (it->f, c, it->face_id,
24938 &char2b, 0);
24939 pcm = get_per_char_metric (font, &char2b);
24942 /* Initialize the bounding box. */
24943 if (pcm)
24945 width = cmp->glyph_len > 0 ? pcm->width : 0;
24946 ascent = pcm->ascent;
24947 descent = pcm->descent;
24948 lbearing = pcm->lbearing;
24949 rbearing = pcm->rbearing;
24951 else
24953 width = cmp->glyph_len > 0 ? font->space_width : 0;
24954 ascent = FONT_BASE (font);
24955 descent = FONT_DESCENT (font);
24956 lbearing = 0;
24957 rbearing = width;
24960 rightmost = width;
24961 leftmost = 0;
24962 lowest = - descent + boff;
24963 highest = ascent + boff;
24965 if (! font_not_found_p
24966 && font->default_ascent
24967 && CHAR_TABLE_P (Vuse_default_ascent)
24968 && !NILP (Faref (Vuse_default_ascent,
24969 make_number (it->char_to_display))))
24970 highest = font->default_ascent + boff;
24972 /* Draw the first glyph at the normal position. It may be
24973 shifted to right later if some other glyphs are drawn
24974 at the left. */
24975 cmp->offsets[i * 2] = 0;
24976 cmp->offsets[i * 2 + 1] = boff;
24977 cmp->lbearing = lbearing;
24978 cmp->rbearing = rbearing;
24980 /* Set cmp->offsets for the remaining glyphs. */
24981 for (i++; i < glyph_len; i++)
24983 int left, right, btm, top;
24984 int ch = COMPOSITION_GLYPH (cmp, i);
24985 int face_id;
24986 struct face *this_face;
24988 if (ch == '\t')
24989 ch = ' ';
24990 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24991 this_face = FACE_FROM_ID (it->f, face_id);
24992 font = this_face->font;
24994 if (font == NULL)
24995 pcm = NULL;
24996 else
24998 get_char_face_and_encoding (it->f, ch, face_id,
24999 &char2b, 0);
25000 pcm = get_per_char_metric (font, &char2b);
25002 if (! pcm)
25003 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25004 else
25006 width = pcm->width;
25007 ascent = pcm->ascent;
25008 descent = pcm->descent;
25009 lbearing = pcm->lbearing;
25010 rbearing = pcm->rbearing;
25011 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25013 /* Relative composition with or without
25014 alternate chars. */
25015 left = (leftmost + rightmost - width) / 2;
25016 btm = - descent + boff;
25017 if (font->relative_compose
25018 && (! CHAR_TABLE_P (Vignore_relative_composition)
25019 || NILP (Faref (Vignore_relative_composition,
25020 make_number (ch)))))
25023 if (- descent >= font->relative_compose)
25024 /* One extra pixel between two glyphs. */
25025 btm = highest + 1;
25026 else if (ascent <= 0)
25027 /* One extra pixel between two glyphs. */
25028 btm = lowest - 1 - ascent - descent;
25031 else
25033 /* A composition rule is specified by an integer
25034 value that encodes global and new reference
25035 points (GREF and NREF). GREF and NREF are
25036 specified by numbers as below:
25038 0---1---2 -- ascent
25042 9--10--11 -- center
25044 ---3---4---5--- baseline
25046 6---7---8 -- descent
25048 int rule = COMPOSITION_RULE (cmp, i);
25049 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25051 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25052 grefx = gref % 3, nrefx = nref % 3;
25053 grefy = gref / 3, nrefy = nref / 3;
25054 if (xoff)
25055 xoff = font_height * (xoff - 128) / 256;
25056 if (yoff)
25057 yoff = font_height * (yoff - 128) / 256;
25059 left = (leftmost
25060 + grefx * (rightmost - leftmost) / 2
25061 - nrefx * width / 2
25062 + xoff);
25064 btm = ((grefy == 0 ? highest
25065 : grefy == 1 ? 0
25066 : grefy == 2 ? lowest
25067 : (highest + lowest) / 2)
25068 - (nrefy == 0 ? ascent + descent
25069 : nrefy == 1 ? descent - boff
25070 : nrefy == 2 ? 0
25071 : (ascent + descent) / 2)
25072 + yoff);
25075 cmp->offsets[i * 2] = left;
25076 cmp->offsets[i * 2 + 1] = btm + descent;
25078 /* Update the bounding box of the overall glyphs. */
25079 if (width > 0)
25081 right = left + width;
25082 if (left < leftmost)
25083 leftmost = left;
25084 if (right > rightmost)
25085 rightmost = right;
25087 top = btm + descent + ascent;
25088 if (top > highest)
25089 highest = top;
25090 if (btm < lowest)
25091 lowest = btm;
25093 if (cmp->lbearing > left + lbearing)
25094 cmp->lbearing = left + lbearing;
25095 if (cmp->rbearing < left + rbearing)
25096 cmp->rbearing = left + rbearing;
25100 /* If there are glyphs whose x-offsets are negative,
25101 shift all glyphs to the right and make all x-offsets
25102 non-negative. */
25103 if (leftmost < 0)
25105 for (i = 0; i < cmp->glyph_len; i++)
25106 cmp->offsets[i * 2] -= leftmost;
25107 rightmost -= leftmost;
25108 cmp->lbearing -= leftmost;
25109 cmp->rbearing -= leftmost;
25112 if (left_padded && cmp->lbearing < 0)
25114 for (i = 0; i < cmp->glyph_len; i++)
25115 cmp->offsets[i * 2] -= cmp->lbearing;
25116 rightmost -= cmp->lbearing;
25117 cmp->rbearing -= cmp->lbearing;
25118 cmp->lbearing = 0;
25120 if (right_padded && rightmost < cmp->rbearing)
25122 rightmost = cmp->rbearing;
25125 cmp->pixel_width = rightmost;
25126 cmp->ascent = highest;
25127 cmp->descent = - lowest;
25128 if (cmp->ascent < font_ascent)
25129 cmp->ascent = font_ascent;
25130 if (cmp->descent < font_descent)
25131 cmp->descent = font_descent;
25134 if (it->glyph_row
25135 && (cmp->lbearing < 0
25136 || cmp->rbearing > cmp->pixel_width))
25137 it->glyph_row->contains_overlapping_glyphs_p = 1;
25139 it->pixel_width = cmp->pixel_width;
25140 it->ascent = it->phys_ascent = cmp->ascent;
25141 it->descent = it->phys_descent = cmp->descent;
25142 if (face->box != FACE_NO_BOX)
25144 int thick = face->box_line_width;
25146 if (thick > 0)
25148 it->ascent += thick;
25149 it->descent += thick;
25151 else
25152 thick = - thick;
25154 if (it->start_of_box_run_p)
25155 it->pixel_width += thick;
25156 if (it->end_of_box_run_p)
25157 it->pixel_width += thick;
25160 /* If face has an overline, add the height of the overline
25161 (1 pixel) and a 1 pixel margin to the character height. */
25162 if (face->overline_p)
25163 it->ascent += overline_margin;
25165 take_vertical_position_into_account (it);
25166 if (it->ascent < 0)
25167 it->ascent = 0;
25168 if (it->descent < 0)
25169 it->descent = 0;
25171 if (it->glyph_row && cmp->glyph_len > 0)
25172 append_composite_glyph (it);
25174 else if (it->what == IT_COMPOSITION)
25176 /* A dynamic (automatic) composition. */
25177 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25178 Lisp_Object gstring;
25179 struct font_metrics metrics;
25181 it->nglyphs = 1;
25183 gstring = composition_gstring_from_id (it->cmp_it.id);
25184 it->pixel_width
25185 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25186 &metrics);
25187 if (it->glyph_row
25188 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25189 it->glyph_row->contains_overlapping_glyphs_p = 1;
25190 it->ascent = it->phys_ascent = metrics.ascent;
25191 it->descent = it->phys_descent = metrics.descent;
25192 if (face->box != FACE_NO_BOX)
25194 int thick = face->box_line_width;
25196 if (thick > 0)
25198 it->ascent += thick;
25199 it->descent += thick;
25201 else
25202 thick = - thick;
25204 if (it->start_of_box_run_p)
25205 it->pixel_width += thick;
25206 if (it->end_of_box_run_p)
25207 it->pixel_width += thick;
25209 /* If face has an overline, add the height of the overline
25210 (1 pixel) and a 1 pixel margin to the character height. */
25211 if (face->overline_p)
25212 it->ascent += overline_margin;
25213 take_vertical_position_into_account (it);
25214 if (it->ascent < 0)
25215 it->ascent = 0;
25216 if (it->descent < 0)
25217 it->descent = 0;
25219 if (it->glyph_row)
25220 append_composite_glyph (it);
25222 else if (it->what == IT_GLYPHLESS)
25223 produce_glyphless_glyph (it, 0, Qnil);
25224 else if (it->what == IT_IMAGE)
25225 produce_image_glyph (it);
25226 else if (it->what == IT_STRETCH)
25227 produce_stretch_glyph (it);
25229 done:
25230 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25231 because this isn't true for images with `:ascent 100'. */
25232 eassert (it->ascent >= 0 && it->descent >= 0);
25233 if (it->area == TEXT_AREA)
25234 it->current_x += it->pixel_width;
25236 if (extra_line_spacing > 0)
25238 it->descent += extra_line_spacing;
25239 if (extra_line_spacing > it->max_extra_line_spacing)
25240 it->max_extra_line_spacing = extra_line_spacing;
25243 it->max_ascent = max (it->max_ascent, it->ascent);
25244 it->max_descent = max (it->max_descent, it->descent);
25245 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25246 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25249 /* EXPORT for RIF:
25250 Output LEN glyphs starting at START at the nominal cursor position.
25251 Advance the nominal cursor over the text. The global variable
25252 updated_window contains the window being updated, updated_row is
25253 the glyph row being updated, and updated_area is the area of that
25254 row being updated. */
25256 void
25257 x_write_glyphs (struct glyph *start, int len)
25259 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25261 eassert (updated_window && updated_row);
25262 /* When the window is hscrolled, cursor hpos can legitimately be out
25263 of bounds, but we draw the cursor at the corresponding window
25264 margin in that case. */
25265 if (!updated_row->reversed_p && chpos < 0)
25266 chpos = 0;
25267 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25268 chpos = updated_row->used[TEXT_AREA] - 1;
25270 BLOCK_INPUT;
25272 /* Write glyphs. */
25274 hpos = start - updated_row->glyphs[updated_area];
25275 x = draw_glyphs (updated_window, output_cursor.x,
25276 updated_row, updated_area,
25277 hpos, hpos + len,
25278 DRAW_NORMAL_TEXT, 0);
25280 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25281 if (updated_area == TEXT_AREA
25282 && updated_window->phys_cursor_on_p
25283 && updated_window->phys_cursor.vpos == output_cursor.vpos
25284 && chpos >= hpos
25285 && chpos < hpos + len)
25286 updated_window->phys_cursor_on_p = 0;
25288 UNBLOCK_INPUT;
25290 /* Advance the output cursor. */
25291 output_cursor.hpos += len;
25292 output_cursor.x = x;
25296 /* EXPORT for RIF:
25297 Insert LEN glyphs from START at the nominal cursor position. */
25299 void
25300 x_insert_glyphs (struct glyph *start, int len)
25302 struct frame *f;
25303 struct window *w;
25304 int line_height, shift_by_width, shifted_region_width;
25305 struct glyph_row *row;
25306 struct glyph *glyph;
25307 int frame_x, frame_y;
25308 ptrdiff_t hpos;
25310 eassert (updated_window && updated_row);
25311 BLOCK_INPUT;
25312 w = updated_window;
25313 f = XFRAME (WINDOW_FRAME (w));
25315 /* Get the height of the line we are in. */
25316 row = updated_row;
25317 line_height = row->height;
25319 /* Get the width of the glyphs to insert. */
25320 shift_by_width = 0;
25321 for (glyph = start; glyph < start + len; ++glyph)
25322 shift_by_width += glyph->pixel_width;
25324 /* Get the width of the region to shift right. */
25325 shifted_region_width = (window_box_width (w, updated_area)
25326 - output_cursor.x
25327 - shift_by_width);
25329 /* Shift right. */
25330 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25331 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25333 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25334 line_height, shift_by_width);
25336 /* Write the glyphs. */
25337 hpos = start - row->glyphs[updated_area];
25338 draw_glyphs (w, output_cursor.x, row, updated_area,
25339 hpos, hpos + len,
25340 DRAW_NORMAL_TEXT, 0);
25342 /* Advance the output cursor. */
25343 output_cursor.hpos += len;
25344 output_cursor.x += shift_by_width;
25345 UNBLOCK_INPUT;
25349 /* EXPORT for RIF:
25350 Erase the current text line from the nominal cursor position
25351 (inclusive) to pixel column TO_X (exclusive). The idea is that
25352 everything from TO_X onward is already erased.
25354 TO_X is a pixel position relative to updated_area of
25355 updated_window. TO_X == -1 means clear to the end of this area. */
25357 void
25358 x_clear_end_of_line (int to_x)
25360 struct frame *f;
25361 struct window *w = updated_window;
25362 int max_x, min_y, max_y;
25363 int from_x, from_y, to_y;
25365 eassert (updated_window && updated_row);
25366 f = XFRAME (w->frame);
25368 if (updated_row->full_width_p)
25369 max_x = WINDOW_TOTAL_WIDTH (w);
25370 else
25371 max_x = window_box_width (w, updated_area);
25372 max_y = window_text_bottom_y (w);
25374 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25375 of window. For TO_X > 0, truncate to end of drawing area. */
25376 if (to_x == 0)
25377 return;
25378 else if (to_x < 0)
25379 to_x = max_x;
25380 else
25381 to_x = min (to_x, max_x);
25383 to_y = min (max_y, output_cursor.y + updated_row->height);
25385 /* Notice if the cursor will be cleared by this operation. */
25386 if (!updated_row->full_width_p)
25387 notice_overwritten_cursor (w, updated_area,
25388 output_cursor.x, -1,
25389 updated_row->y,
25390 MATRIX_ROW_BOTTOM_Y (updated_row));
25392 from_x = output_cursor.x;
25394 /* Translate to frame coordinates. */
25395 if (updated_row->full_width_p)
25397 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25398 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25400 else
25402 int area_left = window_box_left (w, updated_area);
25403 from_x += area_left;
25404 to_x += area_left;
25407 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25408 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25409 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25411 /* Prevent inadvertently clearing to end of the X window. */
25412 if (to_x > from_x && to_y > from_y)
25414 BLOCK_INPUT;
25415 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25416 to_x - from_x, to_y - from_y);
25417 UNBLOCK_INPUT;
25421 #endif /* HAVE_WINDOW_SYSTEM */
25425 /***********************************************************************
25426 Cursor types
25427 ***********************************************************************/
25429 /* Value is the internal representation of the specified cursor type
25430 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25431 of the bar cursor. */
25433 static enum text_cursor_kinds
25434 get_specified_cursor_type (Lisp_Object arg, int *width)
25436 enum text_cursor_kinds type;
25438 if (NILP (arg))
25439 return NO_CURSOR;
25441 if (EQ (arg, Qbox))
25442 return FILLED_BOX_CURSOR;
25444 if (EQ (arg, Qhollow))
25445 return HOLLOW_BOX_CURSOR;
25447 if (EQ (arg, Qbar))
25449 *width = 2;
25450 return BAR_CURSOR;
25453 if (CONSP (arg)
25454 && EQ (XCAR (arg), Qbar)
25455 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25457 *width = XINT (XCDR (arg));
25458 return BAR_CURSOR;
25461 if (EQ (arg, Qhbar))
25463 *width = 2;
25464 return HBAR_CURSOR;
25467 if (CONSP (arg)
25468 && EQ (XCAR (arg), Qhbar)
25469 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25471 *width = XINT (XCDR (arg));
25472 return HBAR_CURSOR;
25475 /* Treat anything unknown as "hollow box cursor".
25476 It was bad to signal an error; people have trouble fixing
25477 .Xdefaults with Emacs, when it has something bad in it. */
25478 type = HOLLOW_BOX_CURSOR;
25480 return type;
25483 /* Set the default cursor types for specified frame. */
25484 void
25485 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25487 int width = 1;
25488 Lisp_Object tem;
25490 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25491 FRAME_CURSOR_WIDTH (f) = width;
25493 /* By default, set up the blink-off state depending on the on-state. */
25495 tem = Fassoc (arg, Vblink_cursor_alist);
25496 if (!NILP (tem))
25498 FRAME_BLINK_OFF_CURSOR (f)
25499 = get_specified_cursor_type (XCDR (tem), &width);
25500 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25502 else
25503 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25507 #ifdef HAVE_WINDOW_SYSTEM
25509 /* Return the cursor we want to be displayed in window W. Return
25510 width of bar/hbar cursor through WIDTH arg. Return with
25511 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25512 (i.e. if the `system caret' should track this cursor).
25514 In a mini-buffer window, we want the cursor only to appear if we
25515 are reading input from this window. For the selected window, we
25516 want the cursor type given by the frame parameter or buffer local
25517 setting of cursor-type. If explicitly marked off, draw no cursor.
25518 In all other cases, we want a hollow box cursor. */
25520 static enum text_cursor_kinds
25521 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25522 int *active_cursor)
25524 struct frame *f = XFRAME (w->frame);
25525 struct buffer *b = XBUFFER (w->buffer);
25526 int cursor_type = DEFAULT_CURSOR;
25527 Lisp_Object alt_cursor;
25528 int non_selected = 0;
25530 *active_cursor = 1;
25532 /* Echo area */
25533 if (cursor_in_echo_area
25534 && FRAME_HAS_MINIBUF_P (f)
25535 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25537 if (w == XWINDOW (echo_area_window))
25539 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25541 *width = FRAME_CURSOR_WIDTH (f);
25542 return FRAME_DESIRED_CURSOR (f);
25544 else
25545 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25548 *active_cursor = 0;
25549 non_selected = 1;
25552 /* Detect a nonselected window or nonselected frame. */
25553 else if (w != XWINDOW (f->selected_window)
25554 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25556 *active_cursor = 0;
25558 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25559 return NO_CURSOR;
25561 non_selected = 1;
25564 /* Never display a cursor in a window in which cursor-type is nil. */
25565 if (NILP (BVAR (b, cursor_type)))
25566 return NO_CURSOR;
25568 /* Get the normal cursor type for this window. */
25569 if (EQ (BVAR (b, cursor_type), Qt))
25571 cursor_type = FRAME_DESIRED_CURSOR (f);
25572 *width = FRAME_CURSOR_WIDTH (f);
25574 else
25575 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25577 /* Use cursor-in-non-selected-windows instead
25578 for non-selected window or frame. */
25579 if (non_selected)
25581 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25582 if (!EQ (Qt, alt_cursor))
25583 return get_specified_cursor_type (alt_cursor, width);
25584 /* t means modify the normal cursor type. */
25585 if (cursor_type == FILLED_BOX_CURSOR)
25586 cursor_type = HOLLOW_BOX_CURSOR;
25587 else if (cursor_type == BAR_CURSOR && *width > 1)
25588 --*width;
25589 return cursor_type;
25592 /* Use normal cursor if not blinked off. */
25593 if (!w->cursor_off_p)
25595 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25597 if (cursor_type == FILLED_BOX_CURSOR)
25599 /* Using a block cursor on large images can be very annoying.
25600 So use a hollow cursor for "large" images.
25601 If image is not transparent (no mask), also use hollow cursor. */
25602 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25603 if (img != NULL && IMAGEP (img->spec))
25605 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25606 where N = size of default frame font size.
25607 This should cover most of the "tiny" icons people may use. */
25608 if (!img->mask
25609 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25610 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25611 cursor_type = HOLLOW_BOX_CURSOR;
25614 else if (cursor_type != NO_CURSOR)
25616 /* Display current only supports BOX and HOLLOW cursors for images.
25617 So for now, unconditionally use a HOLLOW cursor when cursor is
25618 not a solid box cursor. */
25619 cursor_type = HOLLOW_BOX_CURSOR;
25622 return cursor_type;
25625 /* Cursor is blinked off, so determine how to "toggle" it. */
25627 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25628 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25629 return get_specified_cursor_type (XCDR (alt_cursor), width);
25631 /* Then see if frame has specified a specific blink off cursor type. */
25632 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25634 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25635 return FRAME_BLINK_OFF_CURSOR (f);
25638 #if 0
25639 /* Some people liked having a permanently visible blinking cursor,
25640 while others had very strong opinions against it. So it was
25641 decided to remove it. KFS 2003-09-03 */
25643 /* Finally perform built-in cursor blinking:
25644 filled box <-> hollow box
25645 wide [h]bar <-> narrow [h]bar
25646 narrow [h]bar <-> no cursor
25647 other type <-> no cursor */
25649 if (cursor_type == FILLED_BOX_CURSOR)
25650 return HOLLOW_BOX_CURSOR;
25652 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25654 *width = 1;
25655 return cursor_type;
25657 #endif
25659 return NO_CURSOR;
25663 /* Notice when the text cursor of window W has been completely
25664 overwritten by a drawing operation that outputs glyphs in AREA
25665 starting at X0 and ending at X1 in the line starting at Y0 and
25666 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25667 the rest of the line after X0 has been written. Y coordinates
25668 are window-relative. */
25670 static void
25671 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25672 int x0, int x1, int y0, int y1)
25674 int cx0, cx1, cy0, cy1;
25675 struct glyph_row *row;
25677 if (!w->phys_cursor_on_p)
25678 return;
25679 if (area != TEXT_AREA)
25680 return;
25682 if (w->phys_cursor.vpos < 0
25683 || w->phys_cursor.vpos >= w->current_matrix->nrows
25684 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25685 !(row->enabled_p && row->displays_text_p)))
25686 return;
25688 if (row->cursor_in_fringe_p)
25690 row->cursor_in_fringe_p = 0;
25691 draw_fringe_bitmap (w, row, row->reversed_p);
25692 w->phys_cursor_on_p = 0;
25693 return;
25696 cx0 = w->phys_cursor.x;
25697 cx1 = cx0 + w->phys_cursor_width;
25698 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25699 return;
25701 /* The cursor image will be completely removed from the
25702 screen if the output area intersects the cursor area in
25703 y-direction. When we draw in [y0 y1[, and some part of
25704 the cursor is at y < y0, that part must have been drawn
25705 before. When scrolling, the cursor is erased before
25706 actually scrolling, so we don't come here. When not
25707 scrolling, the rows above the old cursor row must have
25708 changed, and in this case these rows must have written
25709 over the cursor image.
25711 Likewise if part of the cursor is below y1, with the
25712 exception of the cursor being in the first blank row at
25713 the buffer and window end because update_text_area
25714 doesn't draw that row. (Except when it does, but
25715 that's handled in update_text_area.) */
25717 cy0 = w->phys_cursor.y;
25718 cy1 = cy0 + w->phys_cursor_height;
25719 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25720 return;
25722 w->phys_cursor_on_p = 0;
25725 #endif /* HAVE_WINDOW_SYSTEM */
25728 /************************************************************************
25729 Mouse Face
25730 ************************************************************************/
25732 #ifdef HAVE_WINDOW_SYSTEM
25734 /* EXPORT for RIF:
25735 Fix the display of area AREA of overlapping row ROW in window W
25736 with respect to the overlapping part OVERLAPS. */
25738 void
25739 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25740 enum glyph_row_area area, int overlaps)
25742 int i, x;
25744 BLOCK_INPUT;
25746 x = 0;
25747 for (i = 0; i < row->used[area];)
25749 if (row->glyphs[area][i].overlaps_vertically_p)
25751 int start = i, start_x = x;
25755 x += row->glyphs[area][i].pixel_width;
25756 ++i;
25758 while (i < row->used[area]
25759 && row->glyphs[area][i].overlaps_vertically_p);
25761 draw_glyphs (w, start_x, row, area,
25762 start, i,
25763 DRAW_NORMAL_TEXT, overlaps);
25765 else
25767 x += row->glyphs[area][i].pixel_width;
25768 ++i;
25772 UNBLOCK_INPUT;
25776 /* EXPORT:
25777 Draw the cursor glyph of window W in glyph row ROW. See the
25778 comment of draw_glyphs for the meaning of HL. */
25780 void
25781 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25782 enum draw_glyphs_face hl)
25784 /* If cursor hpos is out of bounds, don't draw garbage. This can
25785 happen in mini-buffer windows when switching between echo area
25786 glyphs and mini-buffer. */
25787 if ((row->reversed_p
25788 ? (w->phys_cursor.hpos >= 0)
25789 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25791 int on_p = w->phys_cursor_on_p;
25792 int x1;
25793 int hpos = w->phys_cursor.hpos;
25795 /* When the window is hscrolled, cursor hpos can legitimately be
25796 out of bounds, but we draw the cursor at the corresponding
25797 window margin in that case. */
25798 if (!row->reversed_p && hpos < 0)
25799 hpos = 0;
25800 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25801 hpos = row->used[TEXT_AREA] - 1;
25803 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25804 hl, 0);
25805 w->phys_cursor_on_p = on_p;
25807 if (hl == DRAW_CURSOR)
25808 w->phys_cursor_width = x1 - w->phys_cursor.x;
25809 /* When we erase the cursor, and ROW is overlapped by other
25810 rows, make sure that these overlapping parts of other rows
25811 are redrawn. */
25812 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25814 w->phys_cursor_width = x1 - w->phys_cursor.x;
25816 if (row > w->current_matrix->rows
25817 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25818 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25819 OVERLAPS_ERASED_CURSOR);
25821 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25822 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25823 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25824 OVERLAPS_ERASED_CURSOR);
25830 /* EXPORT:
25831 Erase the image of a cursor of window W from the screen. */
25833 void
25834 erase_phys_cursor (struct window *w)
25836 struct frame *f = XFRAME (w->frame);
25837 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25838 int hpos = w->phys_cursor.hpos;
25839 int vpos = w->phys_cursor.vpos;
25840 int mouse_face_here_p = 0;
25841 struct glyph_matrix *active_glyphs = w->current_matrix;
25842 struct glyph_row *cursor_row;
25843 struct glyph *cursor_glyph;
25844 enum draw_glyphs_face hl;
25846 /* No cursor displayed or row invalidated => nothing to do on the
25847 screen. */
25848 if (w->phys_cursor_type == NO_CURSOR)
25849 goto mark_cursor_off;
25851 /* VPOS >= active_glyphs->nrows means that window has been resized.
25852 Don't bother to erase the cursor. */
25853 if (vpos >= active_glyphs->nrows)
25854 goto mark_cursor_off;
25856 /* If row containing cursor is marked invalid, there is nothing we
25857 can do. */
25858 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25859 if (!cursor_row->enabled_p)
25860 goto mark_cursor_off;
25862 /* If line spacing is > 0, old cursor may only be partially visible in
25863 window after split-window. So adjust visible height. */
25864 cursor_row->visible_height = min (cursor_row->visible_height,
25865 window_text_bottom_y (w) - cursor_row->y);
25867 /* If row is completely invisible, don't attempt to delete a cursor which
25868 isn't there. This can happen if cursor is at top of a window, and
25869 we switch to a buffer with a header line in that window. */
25870 if (cursor_row->visible_height <= 0)
25871 goto mark_cursor_off;
25873 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25874 if (cursor_row->cursor_in_fringe_p)
25876 cursor_row->cursor_in_fringe_p = 0;
25877 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25878 goto mark_cursor_off;
25881 /* This can happen when the new row is shorter than the old one.
25882 In this case, either draw_glyphs or clear_end_of_line
25883 should have cleared the cursor. Note that we wouldn't be
25884 able to erase the cursor in this case because we don't have a
25885 cursor glyph at hand. */
25886 if ((cursor_row->reversed_p
25887 ? (w->phys_cursor.hpos < 0)
25888 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25889 goto mark_cursor_off;
25891 /* When the window is hscrolled, cursor hpos can legitimately be out
25892 of bounds, but we draw the cursor at the corresponding window
25893 margin in that case. */
25894 if (!cursor_row->reversed_p && hpos < 0)
25895 hpos = 0;
25896 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25897 hpos = cursor_row->used[TEXT_AREA] - 1;
25899 /* If the cursor is in the mouse face area, redisplay that when
25900 we clear the cursor. */
25901 if (! NILP (hlinfo->mouse_face_window)
25902 && coords_in_mouse_face_p (w, hpos, vpos)
25903 /* Don't redraw the cursor's spot in mouse face if it is at the
25904 end of a line (on a newline). The cursor appears there, but
25905 mouse highlighting does not. */
25906 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25907 mouse_face_here_p = 1;
25909 /* Maybe clear the display under the cursor. */
25910 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25912 int x, y, left_x;
25913 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25914 int width;
25916 cursor_glyph = get_phys_cursor_glyph (w);
25917 if (cursor_glyph == NULL)
25918 goto mark_cursor_off;
25920 width = cursor_glyph->pixel_width;
25921 left_x = window_box_left_offset (w, TEXT_AREA);
25922 x = w->phys_cursor.x;
25923 if (x < left_x)
25924 width -= left_x - x;
25925 width = min (width, window_box_width (w, TEXT_AREA) - x);
25926 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25927 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25929 if (width > 0)
25930 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25933 /* Erase the cursor by redrawing the character underneath it. */
25934 if (mouse_face_here_p)
25935 hl = DRAW_MOUSE_FACE;
25936 else
25937 hl = DRAW_NORMAL_TEXT;
25938 draw_phys_cursor_glyph (w, cursor_row, hl);
25940 mark_cursor_off:
25941 w->phys_cursor_on_p = 0;
25942 w->phys_cursor_type = NO_CURSOR;
25946 /* EXPORT:
25947 Display or clear cursor of window W. If ON is zero, clear the
25948 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25949 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25951 void
25952 display_and_set_cursor (struct window *w, int on,
25953 int hpos, int vpos, int x, int y)
25955 struct frame *f = XFRAME (w->frame);
25956 int new_cursor_type;
25957 int new_cursor_width;
25958 int active_cursor;
25959 struct glyph_row *glyph_row;
25960 struct glyph *glyph;
25962 /* This is pointless on invisible frames, and dangerous on garbaged
25963 windows and frames; in the latter case, the frame or window may
25964 be in the midst of changing its size, and x and y may be off the
25965 window. */
25966 if (! FRAME_VISIBLE_P (f)
25967 || FRAME_GARBAGED_P (f)
25968 || vpos >= w->current_matrix->nrows
25969 || hpos >= w->current_matrix->matrix_w)
25970 return;
25972 /* If cursor is off and we want it off, return quickly. */
25973 if (!on && !w->phys_cursor_on_p)
25974 return;
25976 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25977 /* If cursor row is not enabled, we don't really know where to
25978 display the cursor. */
25979 if (!glyph_row->enabled_p)
25981 w->phys_cursor_on_p = 0;
25982 return;
25985 glyph = NULL;
25986 if (!glyph_row->exact_window_width_line_p
25987 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25988 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25990 eassert (interrupt_input_blocked);
25992 /* Set new_cursor_type to the cursor we want to be displayed. */
25993 new_cursor_type = get_window_cursor_type (w, glyph,
25994 &new_cursor_width, &active_cursor);
25996 /* If cursor is currently being shown and we don't want it to be or
25997 it is in the wrong place, or the cursor type is not what we want,
25998 erase it. */
25999 if (w->phys_cursor_on_p
26000 && (!on
26001 || w->phys_cursor.x != x
26002 || w->phys_cursor.y != y
26003 || new_cursor_type != w->phys_cursor_type
26004 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26005 && new_cursor_width != w->phys_cursor_width)))
26006 erase_phys_cursor (w);
26008 /* Don't check phys_cursor_on_p here because that flag is only set
26009 to zero in some cases where we know that the cursor has been
26010 completely erased, to avoid the extra work of erasing the cursor
26011 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26012 still not be visible, or it has only been partly erased. */
26013 if (on)
26015 w->phys_cursor_ascent = glyph_row->ascent;
26016 w->phys_cursor_height = glyph_row->height;
26018 /* Set phys_cursor_.* before x_draw_.* is called because some
26019 of them may need the information. */
26020 w->phys_cursor.x = x;
26021 w->phys_cursor.y = glyph_row->y;
26022 w->phys_cursor.hpos = hpos;
26023 w->phys_cursor.vpos = vpos;
26026 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26027 new_cursor_type, new_cursor_width,
26028 on, active_cursor);
26032 /* Switch the display of W's cursor on or off, according to the value
26033 of ON. */
26035 static void
26036 update_window_cursor (struct window *w, int on)
26038 /* Don't update cursor in windows whose frame is in the process
26039 of being deleted. */
26040 if (w->current_matrix)
26042 int hpos = w->phys_cursor.hpos;
26043 int vpos = w->phys_cursor.vpos;
26044 struct glyph_row *row;
26046 if (vpos >= w->current_matrix->nrows
26047 || hpos >= w->current_matrix->matrix_w)
26048 return;
26050 row = MATRIX_ROW (w->current_matrix, vpos);
26052 /* When the window is hscrolled, cursor hpos can legitimately be
26053 out of bounds, but we draw the cursor at the corresponding
26054 window margin in that case. */
26055 if (!row->reversed_p && hpos < 0)
26056 hpos = 0;
26057 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26058 hpos = row->used[TEXT_AREA] - 1;
26060 BLOCK_INPUT;
26061 display_and_set_cursor (w, on, hpos, vpos,
26062 w->phys_cursor.x, w->phys_cursor.y);
26063 UNBLOCK_INPUT;
26068 /* Call update_window_cursor with parameter ON_P on all leaf windows
26069 in the window tree rooted at W. */
26071 static void
26072 update_cursor_in_window_tree (struct window *w, int on_p)
26074 while (w)
26076 if (!NILP (w->hchild))
26077 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
26078 else if (!NILP (w->vchild))
26079 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
26080 else
26081 update_window_cursor (w, on_p);
26083 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26088 /* EXPORT:
26089 Display the cursor on window W, or clear it, according to ON_P.
26090 Don't change the cursor's position. */
26092 void
26093 x_update_cursor (struct frame *f, int on_p)
26095 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26099 /* EXPORT:
26100 Clear the cursor of window W to background color, and mark the
26101 cursor as not shown. This is used when the text where the cursor
26102 is about to be rewritten. */
26104 void
26105 x_clear_cursor (struct window *w)
26107 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26108 update_window_cursor (w, 0);
26111 #endif /* HAVE_WINDOW_SYSTEM */
26113 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26114 and MSDOS. */
26115 static void
26116 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26117 int start_hpos, int end_hpos,
26118 enum draw_glyphs_face draw)
26120 #ifdef HAVE_WINDOW_SYSTEM
26121 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26123 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26124 return;
26126 #endif
26127 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26128 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26129 #endif
26132 /* Display the active region described by mouse_face_* according to DRAW. */
26134 static void
26135 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26137 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26138 struct frame *f = XFRAME (WINDOW_FRAME (w));
26140 if (/* If window is in the process of being destroyed, don't bother
26141 to do anything. */
26142 w->current_matrix != NULL
26143 /* Don't update mouse highlight if hidden */
26144 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26145 /* Recognize when we are called to operate on rows that don't exist
26146 anymore. This can happen when a window is split. */
26147 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26149 int phys_cursor_on_p = w->phys_cursor_on_p;
26150 struct glyph_row *row, *first, *last;
26152 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26153 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26155 for (row = first; row <= last && row->enabled_p; ++row)
26157 int start_hpos, end_hpos, start_x;
26159 /* For all but the first row, the highlight starts at column 0. */
26160 if (row == first)
26162 /* R2L rows have BEG and END in reversed order, but the
26163 screen drawing geometry is always left to right. So
26164 we need to mirror the beginning and end of the
26165 highlighted area in R2L rows. */
26166 if (!row->reversed_p)
26168 start_hpos = hlinfo->mouse_face_beg_col;
26169 start_x = hlinfo->mouse_face_beg_x;
26171 else if (row == last)
26173 start_hpos = hlinfo->mouse_face_end_col;
26174 start_x = hlinfo->mouse_face_end_x;
26176 else
26178 start_hpos = 0;
26179 start_x = 0;
26182 else if (row->reversed_p && row == last)
26184 start_hpos = hlinfo->mouse_face_end_col;
26185 start_x = hlinfo->mouse_face_end_x;
26187 else
26189 start_hpos = 0;
26190 start_x = 0;
26193 if (row == last)
26195 if (!row->reversed_p)
26196 end_hpos = hlinfo->mouse_face_end_col;
26197 else if (row == first)
26198 end_hpos = hlinfo->mouse_face_beg_col;
26199 else
26201 end_hpos = row->used[TEXT_AREA];
26202 if (draw == DRAW_NORMAL_TEXT)
26203 row->fill_line_p = 1; /* Clear to end of line */
26206 else if (row->reversed_p && row == first)
26207 end_hpos = hlinfo->mouse_face_beg_col;
26208 else
26210 end_hpos = row->used[TEXT_AREA];
26211 if (draw == DRAW_NORMAL_TEXT)
26212 row->fill_line_p = 1; /* Clear to end of line */
26215 if (end_hpos > start_hpos)
26217 draw_row_with_mouse_face (w, start_x, row,
26218 start_hpos, end_hpos, draw);
26220 row->mouse_face_p
26221 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26225 #ifdef HAVE_WINDOW_SYSTEM
26226 /* When we've written over the cursor, arrange for it to
26227 be displayed again. */
26228 if (FRAME_WINDOW_P (f)
26229 && phys_cursor_on_p && !w->phys_cursor_on_p)
26231 int hpos = w->phys_cursor.hpos;
26233 /* When the window is hscrolled, cursor hpos can legitimately be
26234 out of bounds, but we draw the cursor at the corresponding
26235 window margin in that case. */
26236 if (!row->reversed_p && hpos < 0)
26237 hpos = 0;
26238 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26239 hpos = row->used[TEXT_AREA] - 1;
26241 BLOCK_INPUT;
26242 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26243 w->phys_cursor.x, w->phys_cursor.y);
26244 UNBLOCK_INPUT;
26246 #endif /* HAVE_WINDOW_SYSTEM */
26249 #ifdef HAVE_WINDOW_SYSTEM
26250 /* Change the mouse cursor. */
26251 if (FRAME_WINDOW_P (f))
26253 if (draw == DRAW_NORMAL_TEXT
26254 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26255 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26256 else if (draw == DRAW_MOUSE_FACE)
26257 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26258 else
26259 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26261 #endif /* HAVE_WINDOW_SYSTEM */
26264 /* EXPORT:
26265 Clear out the mouse-highlighted active region.
26266 Redraw it un-highlighted first. Value is non-zero if mouse
26267 face was actually drawn unhighlighted. */
26270 clear_mouse_face (Mouse_HLInfo *hlinfo)
26272 int cleared = 0;
26274 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26276 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26277 cleared = 1;
26280 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26281 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26282 hlinfo->mouse_face_window = Qnil;
26283 hlinfo->mouse_face_overlay = Qnil;
26284 return cleared;
26287 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26288 within the mouse face on that window. */
26289 static int
26290 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26292 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26294 /* Quickly resolve the easy cases. */
26295 if (!(WINDOWP (hlinfo->mouse_face_window)
26296 && XWINDOW (hlinfo->mouse_face_window) == w))
26297 return 0;
26298 if (vpos < hlinfo->mouse_face_beg_row
26299 || vpos > hlinfo->mouse_face_end_row)
26300 return 0;
26301 if (vpos > hlinfo->mouse_face_beg_row
26302 && vpos < hlinfo->mouse_face_end_row)
26303 return 1;
26305 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26307 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26309 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26310 return 1;
26312 else if ((vpos == hlinfo->mouse_face_beg_row
26313 && hpos >= hlinfo->mouse_face_beg_col)
26314 || (vpos == hlinfo->mouse_face_end_row
26315 && hpos < hlinfo->mouse_face_end_col))
26316 return 1;
26318 else
26320 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26322 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26323 return 1;
26325 else if ((vpos == hlinfo->mouse_face_beg_row
26326 && hpos <= hlinfo->mouse_face_beg_col)
26327 || (vpos == hlinfo->mouse_face_end_row
26328 && hpos > hlinfo->mouse_face_end_col))
26329 return 1;
26331 return 0;
26335 /* EXPORT:
26336 Non-zero if physical cursor of window W is within mouse face. */
26339 cursor_in_mouse_face_p (struct window *w)
26341 int hpos = w->phys_cursor.hpos;
26342 int vpos = w->phys_cursor.vpos;
26343 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26345 /* When the window is hscrolled, cursor hpos can legitimately be out
26346 of bounds, but we draw the cursor at the corresponding window
26347 margin in that case. */
26348 if (!row->reversed_p && hpos < 0)
26349 hpos = 0;
26350 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26351 hpos = row->used[TEXT_AREA] - 1;
26353 return coords_in_mouse_face_p (w, hpos, vpos);
26358 /* Find the glyph rows START_ROW and END_ROW of window W that display
26359 characters between buffer positions START_CHARPOS and END_CHARPOS
26360 (excluding END_CHARPOS). DISP_STRING is a display string that
26361 covers these buffer positions. This is similar to
26362 row_containing_pos, but is more accurate when bidi reordering makes
26363 buffer positions change non-linearly with glyph rows. */
26364 static void
26365 rows_from_pos_range (struct window *w,
26366 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26367 Lisp_Object disp_string,
26368 struct glyph_row **start, struct glyph_row **end)
26370 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26371 int last_y = window_text_bottom_y (w);
26372 struct glyph_row *row;
26374 *start = NULL;
26375 *end = NULL;
26377 while (!first->enabled_p
26378 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26379 first++;
26381 /* Find the START row. */
26382 for (row = first;
26383 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26384 row++)
26386 /* A row can potentially be the START row if the range of the
26387 characters it displays intersects the range
26388 [START_CHARPOS..END_CHARPOS). */
26389 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26390 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26391 /* See the commentary in row_containing_pos, for the
26392 explanation of the complicated way to check whether
26393 some position is beyond the end of the characters
26394 displayed by a row. */
26395 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26396 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26397 && !row->ends_at_zv_p
26398 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26399 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26400 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26401 && !row->ends_at_zv_p
26402 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26404 /* Found a candidate row. Now make sure at least one of the
26405 glyphs it displays has a charpos from the range
26406 [START_CHARPOS..END_CHARPOS).
26408 This is not obvious because bidi reordering could make
26409 buffer positions of a row be 1,2,3,102,101,100, and if we
26410 want to highlight characters in [50..60), we don't want
26411 this row, even though [50..60) does intersect [1..103),
26412 the range of character positions given by the row's start
26413 and end positions. */
26414 struct glyph *g = row->glyphs[TEXT_AREA];
26415 struct glyph *e = g + row->used[TEXT_AREA];
26417 while (g < e)
26419 if (((BUFFERP (g->object) || INTEGERP (g->object))
26420 && start_charpos <= g->charpos && g->charpos < end_charpos)
26421 /* A glyph that comes from DISP_STRING is by
26422 definition to be highlighted. */
26423 || EQ (g->object, disp_string))
26424 *start = row;
26425 g++;
26427 if (*start)
26428 break;
26432 /* Find the END row. */
26433 if (!*start
26434 /* If the last row is partially visible, start looking for END
26435 from that row, instead of starting from FIRST. */
26436 && !(row->enabled_p
26437 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26438 row = first;
26439 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26441 struct glyph_row *next = row + 1;
26442 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26444 if (!next->enabled_p
26445 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26446 /* The first row >= START whose range of displayed characters
26447 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26448 is the row END + 1. */
26449 || (start_charpos < next_start
26450 && end_charpos < next_start)
26451 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26452 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26453 && !next->ends_at_zv_p
26454 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26455 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26456 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26457 && !next->ends_at_zv_p
26458 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26460 *end = row;
26461 break;
26463 else
26465 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26466 but none of the characters it displays are in the range, it is
26467 also END + 1. */
26468 struct glyph *g = next->glyphs[TEXT_AREA];
26469 struct glyph *s = g;
26470 struct glyph *e = g + next->used[TEXT_AREA];
26472 while (g < e)
26474 if (((BUFFERP (g->object) || INTEGERP (g->object))
26475 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26476 /* If the buffer position of the first glyph in
26477 the row is equal to END_CHARPOS, it means
26478 the last character to be highlighted is the
26479 newline of ROW, and we must consider NEXT as
26480 END, not END+1. */
26481 || (((!next->reversed_p && g == s)
26482 || (next->reversed_p && g == e - 1))
26483 && (g->charpos == end_charpos
26484 /* Special case for when NEXT is an
26485 empty line at ZV. */
26486 || (g->charpos == -1
26487 && !row->ends_at_zv_p
26488 && next_start == end_charpos)))))
26489 /* A glyph that comes from DISP_STRING is by
26490 definition to be highlighted. */
26491 || EQ (g->object, disp_string))
26492 break;
26493 g++;
26495 if (g == e)
26497 *end = row;
26498 break;
26500 /* The first row that ends at ZV must be the last to be
26501 highlighted. */
26502 else if (next->ends_at_zv_p)
26504 *end = next;
26505 break;
26511 /* This function sets the mouse_face_* elements of HLINFO, assuming
26512 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26513 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26514 for the overlay or run of text properties specifying the mouse
26515 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26516 before-string and after-string that must also be highlighted.
26517 DISP_STRING, if non-nil, is a display string that may cover some
26518 or all of the highlighted text. */
26520 static void
26521 mouse_face_from_buffer_pos (Lisp_Object window,
26522 Mouse_HLInfo *hlinfo,
26523 ptrdiff_t mouse_charpos,
26524 ptrdiff_t start_charpos,
26525 ptrdiff_t end_charpos,
26526 Lisp_Object before_string,
26527 Lisp_Object after_string,
26528 Lisp_Object disp_string)
26530 struct window *w = XWINDOW (window);
26531 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26532 struct glyph_row *r1, *r2;
26533 struct glyph *glyph, *end;
26534 ptrdiff_t ignore, pos;
26535 int x;
26537 eassert (NILP (disp_string) || STRINGP (disp_string));
26538 eassert (NILP (before_string) || STRINGP (before_string));
26539 eassert (NILP (after_string) || STRINGP (after_string));
26541 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26542 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26543 if (r1 == NULL)
26544 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26545 /* If the before-string or display-string contains newlines,
26546 rows_from_pos_range skips to its last row. Move back. */
26547 if (!NILP (before_string) || !NILP (disp_string))
26549 struct glyph_row *prev;
26550 while ((prev = r1 - 1, prev >= first)
26551 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26552 && prev->used[TEXT_AREA] > 0)
26554 struct glyph *beg = prev->glyphs[TEXT_AREA];
26555 glyph = beg + prev->used[TEXT_AREA];
26556 while (--glyph >= beg && INTEGERP (glyph->object));
26557 if (glyph < beg
26558 || !(EQ (glyph->object, before_string)
26559 || EQ (glyph->object, disp_string)))
26560 break;
26561 r1 = prev;
26564 if (r2 == NULL)
26566 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26567 hlinfo->mouse_face_past_end = 1;
26569 else if (!NILP (after_string))
26571 /* If the after-string has newlines, advance to its last row. */
26572 struct glyph_row *next;
26573 struct glyph_row *last
26574 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26576 for (next = r2 + 1;
26577 next <= last
26578 && next->used[TEXT_AREA] > 0
26579 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26580 ++next)
26581 r2 = next;
26583 /* The rest of the display engine assumes that mouse_face_beg_row is
26584 either above mouse_face_end_row or identical to it. But with
26585 bidi-reordered continued lines, the row for START_CHARPOS could
26586 be below the row for END_CHARPOS. If so, swap the rows and store
26587 them in correct order. */
26588 if (r1->y > r2->y)
26590 struct glyph_row *tem = r2;
26592 r2 = r1;
26593 r1 = tem;
26596 hlinfo->mouse_face_beg_y = r1->y;
26597 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26598 hlinfo->mouse_face_end_y = r2->y;
26599 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26601 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26602 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26603 could be anywhere in the row and in any order. The strategy
26604 below is to find the leftmost and the rightmost glyph that
26605 belongs to either of these 3 strings, or whose position is
26606 between START_CHARPOS and END_CHARPOS, and highlight all the
26607 glyphs between those two. This may cover more than just the text
26608 between START_CHARPOS and END_CHARPOS if the range of characters
26609 strides the bidi level boundary, e.g. if the beginning is in R2L
26610 text while the end is in L2R text or vice versa. */
26611 if (!r1->reversed_p)
26613 /* This row is in a left to right paragraph. Scan it left to
26614 right. */
26615 glyph = r1->glyphs[TEXT_AREA];
26616 end = glyph + r1->used[TEXT_AREA];
26617 x = r1->x;
26619 /* Skip truncation glyphs at the start of the glyph row. */
26620 if (r1->displays_text_p)
26621 for (; glyph < end
26622 && INTEGERP (glyph->object)
26623 && glyph->charpos < 0;
26624 ++glyph)
26625 x += glyph->pixel_width;
26627 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26628 or DISP_STRING, and the first glyph from buffer whose
26629 position is between START_CHARPOS and END_CHARPOS. */
26630 for (; glyph < end
26631 && !INTEGERP (glyph->object)
26632 && !EQ (glyph->object, disp_string)
26633 && !(BUFFERP (glyph->object)
26634 && (glyph->charpos >= start_charpos
26635 && glyph->charpos < end_charpos));
26636 ++glyph)
26638 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26639 are present at buffer positions between START_CHARPOS and
26640 END_CHARPOS, or if they come from an overlay. */
26641 if (EQ (glyph->object, before_string))
26643 pos = string_buffer_position (before_string,
26644 start_charpos);
26645 /* If pos == 0, it means before_string came from an
26646 overlay, not from a buffer position. */
26647 if (!pos || (pos >= start_charpos && pos < end_charpos))
26648 break;
26650 else if (EQ (glyph->object, after_string))
26652 pos = string_buffer_position (after_string, end_charpos);
26653 if (!pos || (pos >= start_charpos && pos < end_charpos))
26654 break;
26656 x += glyph->pixel_width;
26658 hlinfo->mouse_face_beg_x = x;
26659 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26661 else
26663 /* This row is in a right to left paragraph. Scan it right to
26664 left. */
26665 struct glyph *g;
26667 end = r1->glyphs[TEXT_AREA] - 1;
26668 glyph = end + r1->used[TEXT_AREA];
26670 /* Skip truncation glyphs at the start of the glyph row. */
26671 if (r1->displays_text_p)
26672 for (; glyph > end
26673 && INTEGERP (glyph->object)
26674 && glyph->charpos < 0;
26675 --glyph)
26678 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26679 or DISP_STRING, and the first glyph from buffer whose
26680 position is between START_CHARPOS and END_CHARPOS. */
26681 for (; glyph > end
26682 && !INTEGERP (glyph->object)
26683 && !EQ (glyph->object, disp_string)
26684 && !(BUFFERP (glyph->object)
26685 && (glyph->charpos >= start_charpos
26686 && glyph->charpos < end_charpos));
26687 --glyph)
26689 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26690 are present at buffer positions between START_CHARPOS and
26691 END_CHARPOS, or if they come from an overlay. */
26692 if (EQ (glyph->object, before_string))
26694 pos = string_buffer_position (before_string, start_charpos);
26695 /* If pos == 0, it means before_string came from an
26696 overlay, not from a buffer position. */
26697 if (!pos || (pos >= start_charpos && pos < end_charpos))
26698 break;
26700 else if (EQ (glyph->object, after_string))
26702 pos = string_buffer_position (after_string, end_charpos);
26703 if (!pos || (pos >= start_charpos && pos < end_charpos))
26704 break;
26708 glyph++; /* first glyph to the right of the highlighted area */
26709 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26710 x += g->pixel_width;
26711 hlinfo->mouse_face_beg_x = x;
26712 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26715 /* If the highlight ends in a different row, compute GLYPH and END
26716 for the end row. Otherwise, reuse the values computed above for
26717 the row where the highlight begins. */
26718 if (r2 != r1)
26720 if (!r2->reversed_p)
26722 glyph = r2->glyphs[TEXT_AREA];
26723 end = glyph + r2->used[TEXT_AREA];
26724 x = r2->x;
26726 else
26728 end = r2->glyphs[TEXT_AREA] - 1;
26729 glyph = end + r2->used[TEXT_AREA];
26733 if (!r2->reversed_p)
26735 /* Skip truncation and continuation glyphs near the end of the
26736 row, and also blanks and stretch glyphs inserted by
26737 extend_face_to_end_of_line. */
26738 while (end > glyph
26739 && INTEGERP ((end - 1)->object))
26740 --end;
26741 /* Scan the rest of the glyph row from the end, looking for the
26742 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26743 DISP_STRING, or whose position is between START_CHARPOS
26744 and END_CHARPOS */
26745 for (--end;
26746 end > glyph
26747 && !INTEGERP (end->object)
26748 && !EQ (end->object, disp_string)
26749 && !(BUFFERP (end->object)
26750 && (end->charpos >= start_charpos
26751 && end->charpos < end_charpos));
26752 --end)
26754 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26755 are present at buffer positions between START_CHARPOS and
26756 END_CHARPOS, or if they come from an overlay. */
26757 if (EQ (end->object, before_string))
26759 pos = string_buffer_position (before_string, start_charpos);
26760 if (!pos || (pos >= start_charpos && pos < end_charpos))
26761 break;
26763 else if (EQ (end->object, after_string))
26765 pos = string_buffer_position (after_string, end_charpos);
26766 if (!pos || (pos >= start_charpos && pos < end_charpos))
26767 break;
26770 /* Find the X coordinate of the last glyph to be highlighted. */
26771 for (; glyph <= end; ++glyph)
26772 x += glyph->pixel_width;
26774 hlinfo->mouse_face_end_x = x;
26775 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26777 else
26779 /* Skip truncation and continuation glyphs near the end of the
26780 row, and also blanks and stretch glyphs inserted by
26781 extend_face_to_end_of_line. */
26782 x = r2->x;
26783 end++;
26784 while (end < glyph
26785 && INTEGERP (end->object))
26787 x += end->pixel_width;
26788 ++end;
26790 /* Scan the rest of the glyph row from the end, looking for the
26791 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26792 DISP_STRING, or whose position is between START_CHARPOS
26793 and END_CHARPOS */
26794 for ( ;
26795 end < glyph
26796 && !INTEGERP (end->object)
26797 && !EQ (end->object, disp_string)
26798 && !(BUFFERP (end->object)
26799 && (end->charpos >= start_charpos
26800 && end->charpos < end_charpos));
26801 ++end)
26803 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26804 are present at buffer positions between START_CHARPOS and
26805 END_CHARPOS, or if they come from an overlay. */
26806 if (EQ (end->object, before_string))
26808 pos = string_buffer_position (before_string, start_charpos);
26809 if (!pos || (pos >= start_charpos && pos < end_charpos))
26810 break;
26812 else if (EQ (end->object, after_string))
26814 pos = string_buffer_position (after_string, end_charpos);
26815 if (!pos || (pos >= start_charpos && pos < end_charpos))
26816 break;
26818 x += end->pixel_width;
26820 /* If we exited the above loop because we arrived at the last
26821 glyph of the row, and its buffer position is still not in
26822 range, it means the last character in range is the preceding
26823 newline. Bump the end column and x values to get past the
26824 last glyph. */
26825 if (end == glyph
26826 && BUFFERP (end->object)
26827 && (end->charpos < start_charpos
26828 || end->charpos >= end_charpos))
26830 x += end->pixel_width;
26831 ++end;
26833 hlinfo->mouse_face_end_x = x;
26834 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26837 hlinfo->mouse_face_window = window;
26838 hlinfo->mouse_face_face_id
26839 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26840 mouse_charpos + 1,
26841 !hlinfo->mouse_face_hidden, -1);
26842 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26845 /* The following function is not used anymore (replaced with
26846 mouse_face_from_string_pos), but I leave it here for the time
26847 being, in case someone would. */
26849 #if 0 /* not used */
26851 /* Find the position of the glyph for position POS in OBJECT in
26852 window W's current matrix, and return in *X, *Y the pixel
26853 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26855 RIGHT_P non-zero means return the position of the right edge of the
26856 glyph, RIGHT_P zero means return the left edge position.
26858 If no glyph for POS exists in the matrix, return the position of
26859 the glyph with the next smaller position that is in the matrix, if
26860 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26861 exists in the matrix, return the position of the glyph with the
26862 next larger position in OBJECT.
26864 Value is non-zero if a glyph was found. */
26866 static int
26867 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26868 int *hpos, int *vpos, int *x, int *y, int right_p)
26870 int yb = window_text_bottom_y (w);
26871 struct glyph_row *r;
26872 struct glyph *best_glyph = NULL;
26873 struct glyph_row *best_row = NULL;
26874 int best_x = 0;
26876 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26877 r->enabled_p && r->y < yb;
26878 ++r)
26880 struct glyph *g = r->glyphs[TEXT_AREA];
26881 struct glyph *e = g + r->used[TEXT_AREA];
26882 int gx;
26884 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26885 if (EQ (g->object, object))
26887 if (g->charpos == pos)
26889 best_glyph = g;
26890 best_x = gx;
26891 best_row = r;
26892 goto found;
26894 else if (best_glyph == NULL
26895 || ((eabs (g->charpos - pos)
26896 < eabs (best_glyph->charpos - pos))
26897 && (right_p
26898 ? g->charpos < pos
26899 : g->charpos > pos)))
26901 best_glyph = g;
26902 best_x = gx;
26903 best_row = r;
26908 found:
26910 if (best_glyph)
26912 *x = best_x;
26913 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26915 if (right_p)
26917 *x += best_glyph->pixel_width;
26918 ++*hpos;
26921 *y = best_row->y;
26922 *vpos = best_row - w->current_matrix->rows;
26925 return best_glyph != NULL;
26927 #endif /* not used */
26929 /* Find the positions of the first and the last glyphs in window W's
26930 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26931 (assumed to be a string), and return in HLINFO's mouse_face_*
26932 members the pixel and column/row coordinates of those glyphs. */
26934 static void
26935 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26936 Lisp_Object object,
26937 ptrdiff_t startpos, ptrdiff_t endpos)
26939 int yb = window_text_bottom_y (w);
26940 struct glyph_row *r;
26941 struct glyph *g, *e;
26942 int gx;
26943 int found = 0;
26945 /* Find the glyph row with at least one position in the range
26946 [STARTPOS..ENDPOS], and the first glyph in that row whose
26947 position belongs to that range. */
26948 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26949 r->enabled_p && r->y < yb;
26950 ++r)
26952 if (!r->reversed_p)
26954 g = r->glyphs[TEXT_AREA];
26955 e = g + r->used[TEXT_AREA];
26956 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26957 if (EQ (g->object, object)
26958 && startpos <= g->charpos && g->charpos <= endpos)
26960 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26961 hlinfo->mouse_face_beg_y = r->y;
26962 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26963 hlinfo->mouse_face_beg_x = gx;
26964 found = 1;
26965 break;
26968 else
26970 struct glyph *g1;
26972 e = r->glyphs[TEXT_AREA];
26973 g = e + r->used[TEXT_AREA];
26974 for ( ; g > e; --g)
26975 if (EQ ((g-1)->object, object)
26976 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26978 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26979 hlinfo->mouse_face_beg_y = r->y;
26980 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26981 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26982 gx += g1->pixel_width;
26983 hlinfo->mouse_face_beg_x = gx;
26984 found = 1;
26985 break;
26988 if (found)
26989 break;
26992 if (!found)
26993 return;
26995 /* Starting with the next row, look for the first row which does NOT
26996 include any glyphs whose positions are in the range. */
26997 for (++r; r->enabled_p && r->y < yb; ++r)
26999 g = r->glyphs[TEXT_AREA];
27000 e = g + r->used[TEXT_AREA];
27001 found = 0;
27002 for ( ; g < e; ++g)
27003 if (EQ (g->object, object)
27004 && startpos <= g->charpos && g->charpos <= endpos)
27006 found = 1;
27007 break;
27009 if (!found)
27010 break;
27013 /* The highlighted region ends on the previous row. */
27014 r--;
27016 /* Set the end row and its vertical pixel coordinate. */
27017 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
27018 hlinfo->mouse_face_end_y = r->y;
27020 /* Compute and set the end column and the end column's horizontal
27021 pixel coordinate. */
27022 if (!r->reversed_p)
27024 g = r->glyphs[TEXT_AREA];
27025 e = g + r->used[TEXT_AREA];
27026 for ( ; e > g; --e)
27027 if (EQ ((e-1)->object, object)
27028 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27029 break;
27030 hlinfo->mouse_face_end_col = e - g;
27032 for (gx = r->x; g < e; ++g)
27033 gx += g->pixel_width;
27034 hlinfo->mouse_face_end_x = gx;
27036 else
27038 e = r->glyphs[TEXT_AREA];
27039 g = e + r->used[TEXT_AREA];
27040 for (gx = r->x ; e < g; ++e)
27042 if (EQ (e->object, object)
27043 && startpos <= e->charpos && e->charpos <= endpos)
27044 break;
27045 gx += e->pixel_width;
27047 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27048 hlinfo->mouse_face_end_x = gx;
27052 #ifdef HAVE_WINDOW_SYSTEM
27054 /* See if position X, Y is within a hot-spot of an image. */
27056 static int
27057 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27059 if (!CONSP (hot_spot))
27060 return 0;
27062 if (EQ (XCAR (hot_spot), Qrect))
27064 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27065 Lisp_Object rect = XCDR (hot_spot);
27066 Lisp_Object tem;
27067 if (!CONSP (rect))
27068 return 0;
27069 if (!CONSP (XCAR (rect)))
27070 return 0;
27071 if (!CONSP (XCDR (rect)))
27072 return 0;
27073 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27074 return 0;
27075 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27076 return 0;
27077 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27078 return 0;
27079 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27080 return 0;
27081 return 1;
27083 else if (EQ (XCAR (hot_spot), Qcircle))
27085 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27086 Lisp_Object circ = XCDR (hot_spot);
27087 Lisp_Object lr, lx0, ly0;
27088 if (CONSP (circ)
27089 && CONSP (XCAR (circ))
27090 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27091 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27092 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27094 double r = XFLOATINT (lr);
27095 double dx = XINT (lx0) - x;
27096 double dy = XINT (ly0) - y;
27097 return (dx * dx + dy * dy <= r * r);
27100 else if (EQ (XCAR (hot_spot), Qpoly))
27102 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27103 if (VECTORP (XCDR (hot_spot)))
27105 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27106 Lisp_Object *poly = v->contents;
27107 ptrdiff_t n = v->header.size;
27108 ptrdiff_t i;
27109 int inside = 0;
27110 Lisp_Object lx, ly;
27111 int x0, y0;
27113 /* Need an even number of coordinates, and at least 3 edges. */
27114 if (n < 6 || n & 1)
27115 return 0;
27117 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27118 If count is odd, we are inside polygon. Pixels on edges
27119 may or may not be included depending on actual geometry of the
27120 polygon. */
27121 if ((lx = poly[n-2], !INTEGERP (lx))
27122 || (ly = poly[n-1], !INTEGERP (lx)))
27123 return 0;
27124 x0 = XINT (lx), y0 = XINT (ly);
27125 for (i = 0; i < n; i += 2)
27127 int x1 = x0, y1 = y0;
27128 if ((lx = poly[i], !INTEGERP (lx))
27129 || (ly = poly[i+1], !INTEGERP (ly)))
27130 return 0;
27131 x0 = XINT (lx), y0 = XINT (ly);
27133 /* Does this segment cross the X line? */
27134 if (x0 >= x)
27136 if (x1 >= x)
27137 continue;
27139 else if (x1 < x)
27140 continue;
27141 if (y > y0 && y > y1)
27142 continue;
27143 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27144 inside = !inside;
27146 return inside;
27149 return 0;
27152 Lisp_Object
27153 find_hot_spot (Lisp_Object map, int x, int y)
27155 while (CONSP (map))
27157 if (CONSP (XCAR (map))
27158 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27159 return XCAR (map);
27160 map = XCDR (map);
27163 return Qnil;
27166 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27167 3, 3, 0,
27168 doc: /* Lookup in image map MAP coordinates X and Y.
27169 An image map is an alist where each element has the format (AREA ID PLIST).
27170 An AREA is specified as either a rectangle, a circle, or a polygon:
27171 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27172 pixel coordinates of the upper left and bottom right corners.
27173 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27174 and the radius of the circle; r may be a float or integer.
27175 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27176 vector describes one corner in the polygon.
27177 Returns the alist element for the first matching AREA in MAP. */)
27178 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27180 if (NILP (map))
27181 return Qnil;
27183 CHECK_NUMBER (x);
27184 CHECK_NUMBER (y);
27186 return find_hot_spot (map,
27187 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27188 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27192 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27193 static void
27194 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27196 /* Do not change cursor shape while dragging mouse. */
27197 if (!NILP (do_mouse_tracking))
27198 return;
27200 if (!NILP (pointer))
27202 if (EQ (pointer, Qarrow))
27203 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27204 else if (EQ (pointer, Qhand))
27205 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27206 else if (EQ (pointer, Qtext))
27207 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27208 else if (EQ (pointer, intern ("hdrag")))
27209 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27210 #ifdef HAVE_X_WINDOWS
27211 else if (EQ (pointer, intern ("vdrag")))
27212 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27213 #endif
27214 else if (EQ (pointer, intern ("hourglass")))
27215 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27216 else if (EQ (pointer, Qmodeline))
27217 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27218 else
27219 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27222 if (cursor != No_Cursor)
27223 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27226 #endif /* HAVE_WINDOW_SYSTEM */
27228 /* Take proper action when mouse has moved to the mode or header line
27229 or marginal area AREA of window W, x-position X and y-position Y.
27230 X is relative to the start of the text display area of W, so the
27231 width of bitmap areas and scroll bars must be subtracted to get a
27232 position relative to the start of the mode line. */
27234 static void
27235 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27236 enum window_part area)
27238 struct window *w = XWINDOW (window);
27239 struct frame *f = XFRAME (w->frame);
27240 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27241 #ifdef HAVE_WINDOW_SYSTEM
27242 Display_Info *dpyinfo;
27243 #endif
27244 Cursor cursor = No_Cursor;
27245 Lisp_Object pointer = Qnil;
27246 int dx, dy, width, height;
27247 ptrdiff_t charpos;
27248 Lisp_Object string, object = Qnil;
27249 Lisp_Object pos IF_LINT (= Qnil), help;
27251 Lisp_Object mouse_face;
27252 int original_x_pixel = x;
27253 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27254 struct glyph_row *row IF_LINT (= 0);
27256 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27258 int x0;
27259 struct glyph *end;
27261 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27262 returns them in row/column units! */
27263 string = mode_line_string (w, area, &x, &y, &charpos,
27264 &object, &dx, &dy, &width, &height);
27266 row = (area == ON_MODE_LINE
27267 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27268 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27270 /* Find the glyph under the mouse pointer. */
27271 if (row->mode_line_p && row->enabled_p)
27273 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27274 end = glyph + row->used[TEXT_AREA];
27276 for (x0 = original_x_pixel;
27277 glyph < end && x0 >= glyph->pixel_width;
27278 ++glyph)
27279 x0 -= glyph->pixel_width;
27281 if (glyph >= end)
27282 glyph = NULL;
27285 else
27287 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27288 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27289 returns them in row/column units! */
27290 string = marginal_area_string (w, area, &x, &y, &charpos,
27291 &object, &dx, &dy, &width, &height);
27294 help = Qnil;
27296 #ifdef HAVE_WINDOW_SYSTEM
27297 if (IMAGEP (object))
27299 Lisp_Object image_map, hotspot;
27300 if ((image_map = Fplist_get (XCDR (object), QCmap),
27301 !NILP (image_map))
27302 && (hotspot = find_hot_spot (image_map, dx, dy),
27303 CONSP (hotspot))
27304 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27306 Lisp_Object plist;
27308 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27309 If so, we could look for mouse-enter, mouse-leave
27310 properties in PLIST (and do something...). */
27311 hotspot = XCDR (hotspot);
27312 if (CONSP (hotspot)
27313 && (plist = XCAR (hotspot), CONSP (plist)))
27315 pointer = Fplist_get (plist, Qpointer);
27316 if (NILP (pointer))
27317 pointer = Qhand;
27318 help = Fplist_get (plist, Qhelp_echo);
27319 if (!NILP (help))
27321 help_echo_string = help;
27322 XSETWINDOW (help_echo_window, w);
27323 help_echo_object = w->buffer;
27324 help_echo_pos = charpos;
27328 if (NILP (pointer))
27329 pointer = Fplist_get (XCDR (object), QCpointer);
27331 #endif /* HAVE_WINDOW_SYSTEM */
27333 if (STRINGP (string))
27334 pos = make_number (charpos);
27336 /* Set the help text and mouse pointer. If the mouse is on a part
27337 of the mode line without any text (e.g. past the right edge of
27338 the mode line text), use the default help text and pointer. */
27339 if (STRINGP (string) || area == ON_MODE_LINE)
27341 /* Arrange to display the help by setting the global variables
27342 help_echo_string, help_echo_object, and help_echo_pos. */
27343 if (NILP (help))
27345 if (STRINGP (string))
27346 help = Fget_text_property (pos, Qhelp_echo, string);
27348 if (!NILP (help))
27350 help_echo_string = help;
27351 XSETWINDOW (help_echo_window, w);
27352 help_echo_object = string;
27353 help_echo_pos = charpos;
27355 else if (area == ON_MODE_LINE)
27357 Lisp_Object default_help
27358 = buffer_local_value_1 (Qmode_line_default_help_echo,
27359 w->buffer);
27361 if (STRINGP (default_help))
27363 help_echo_string = default_help;
27364 XSETWINDOW (help_echo_window, w);
27365 help_echo_object = Qnil;
27366 help_echo_pos = -1;
27371 #ifdef HAVE_WINDOW_SYSTEM
27372 /* Change the mouse pointer according to what is under it. */
27373 if (FRAME_WINDOW_P (f))
27375 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27376 if (STRINGP (string))
27378 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27380 if (NILP (pointer))
27381 pointer = Fget_text_property (pos, Qpointer, string);
27383 /* Change the mouse pointer according to what is under X/Y. */
27384 if (NILP (pointer)
27385 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27387 Lisp_Object map;
27388 map = Fget_text_property (pos, Qlocal_map, string);
27389 if (!KEYMAPP (map))
27390 map = Fget_text_property (pos, Qkeymap, string);
27391 if (!KEYMAPP (map))
27392 cursor = dpyinfo->vertical_scroll_bar_cursor;
27395 else
27396 /* Default mode-line pointer. */
27397 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27399 #endif
27402 /* Change the mouse face according to what is under X/Y. */
27403 if (STRINGP (string))
27405 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27406 if (!NILP (mouse_face)
27407 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27408 && glyph)
27410 Lisp_Object b, e;
27412 struct glyph * tmp_glyph;
27414 int gpos;
27415 int gseq_length;
27416 int total_pixel_width;
27417 ptrdiff_t begpos, endpos, ignore;
27419 int vpos, hpos;
27421 b = Fprevious_single_property_change (make_number (charpos + 1),
27422 Qmouse_face, string, Qnil);
27423 if (NILP (b))
27424 begpos = 0;
27425 else
27426 begpos = XINT (b);
27428 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27429 if (NILP (e))
27430 endpos = SCHARS (string);
27431 else
27432 endpos = XINT (e);
27434 /* Calculate the glyph position GPOS of GLYPH in the
27435 displayed string, relative to the beginning of the
27436 highlighted part of the string.
27438 Note: GPOS is different from CHARPOS. CHARPOS is the
27439 position of GLYPH in the internal string object. A mode
27440 line string format has structures which are converted to
27441 a flattened string by the Emacs Lisp interpreter. The
27442 internal string is an element of those structures. The
27443 displayed string is the flattened string. */
27444 tmp_glyph = row_start_glyph;
27445 while (tmp_glyph < glyph
27446 && (!(EQ (tmp_glyph->object, glyph->object)
27447 && begpos <= tmp_glyph->charpos
27448 && tmp_glyph->charpos < endpos)))
27449 tmp_glyph++;
27450 gpos = glyph - tmp_glyph;
27452 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27453 the highlighted part of the displayed string to which
27454 GLYPH belongs. Note: GSEQ_LENGTH is different from
27455 SCHARS (STRING), because the latter returns the length of
27456 the internal string. */
27457 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27458 tmp_glyph > glyph
27459 && (!(EQ (tmp_glyph->object, glyph->object)
27460 && begpos <= tmp_glyph->charpos
27461 && tmp_glyph->charpos < endpos));
27462 tmp_glyph--)
27464 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27466 /* Calculate the total pixel width of all the glyphs between
27467 the beginning of the highlighted area and GLYPH. */
27468 total_pixel_width = 0;
27469 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27470 total_pixel_width += tmp_glyph->pixel_width;
27472 /* Pre calculation of re-rendering position. Note: X is in
27473 column units here, after the call to mode_line_string or
27474 marginal_area_string. */
27475 hpos = x - gpos;
27476 vpos = (area == ON_MODE_LINE
27477 ? (w->current_matrix)->nrows - 1
27478 : 0);
27480 /* If GLYPH's position is included in the region that is
27481 already drawn in mouse face, we have nothing to do. */
27482 if ( EQ (window, hlinfo->mouse_face_window)
27483 && (!row->reversed_p
27484 ? (hlinfo->mouse_face_beg_col <= hpos
27485 && hpos < hlinfo->mouse_face_end_col)
27486 /* In R2L rows we swap BEG and END, see below. */
27487 : (hlinfo->mouse_face_end_col <= hpos
27488 && hpos < hlinfo->mouse_face_beg_col))
27489 && hlinfo->mouse_face_beg_row == vpos )
27490 return;
27492 if (clear_mouse_face (hlinfo))
27493 cursor = No_Cursor;
27495 if (!row->reversed_p)
27497 hlinfo->mouse_face_beg_col = hpos;
27498 hlinfo->mouse_face_beg_x = original_x_pixel
27499 - (total_pixel_width + dx);
27500 hlinfo->mouse_face_end_col = hpos + gseq_length;
27501 hlinfo->mouse_face_end_x = 0;
27503 else
27505 /* In R2L rows, show_mouse_face expects BEG and END
27506 coordinates to be swapped. */
27507 hlinfo->mouse_face_end_col = hpos;
27508 hlinfo->mouse_face_end_x = original_x_pixel
27509 - (total_pixel_width + dx);
27510 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27511 hlinfo->mouse_face_beg_x = 0;
27514 hlinfo->mouse_face_beg_row = vpos;
27515 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27516 hlinfo->mouse_face_beg_y = 0;
27517 hlinfo->mouse_face_end_y = 0;
27518 hlinfo->mouse_face_past_end = 0;
27519 hlinfo->mouse_face_window = window;
27521 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27522 charpos,
27523 0, 0, 0,
27524 &ignore,
27525 glyph->face_id,
27527 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27529 if (NILP (pointer))
27530 pointer = Qhand;
27532 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27533 clear_mouse_face (hlinfo);
27535 #ifdef HAVE_WINDOW_SYSTEM
27536 if (FRAME_WINDOW_P (f))
27537 define_frame_cursor1 (f, cursor, pointer);
27538 #endif
27542 /* EXPORT:
27543 Take proper action when the mouse has moved to position X, Y on
27544 frame F as regards highlighting characters that have mouse-face
27545 properties. Also de-highlighting chars where the mouse was before.
27546 X and Y can be negative or out of range. */
27548 void
27549 note_mouse_highlight (struct frame *f, int x, int y)
27551 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27552 enum window_part part = ON_NOTHING;
27553 Lisp_Object window;
27554 struct window *w;
27555 Cursor cursor = No_Cursor;
27556 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27557 struct buffer *b;
27559 /* When a menu is active, don't highlight because this looks odd. */
27560 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27561 if (popup_activated ())
27562 return;
27563 #endif
27565 if (NILP (Vmouse_highlight)
27566 || !f->glyphs_initialized_p
27567 || f->pointer_invisible)
27568 return;
27570 hlinfo->mouse_face_mouse_x = x;
27571 hlinfo->mouse_face_mouse_y = y;
27572 hlinfo->mouse_face_mouse_frame = f;
27574 if (hlinfo->mouse_face_defer)
27575 return;
27577 if (gc_in_progress)
27579 hlinfo->mouse_face_deferred_gc = 1;
27580 return;
27583 /* Which window is that in? */
27584 window = window_from_coordinates (f, x, y, &part, 1);
27586 /* If displaying active text in another window, clear that. */
27587 if (! EQ (window, hlinfo->mouse_face_window)
27588 /* Also clear if we move out of text area in same window. */
27589 || (!NILP (hlinfo->mouse_face_window)
27590 && !NILP (window)
27591 && part != ON_TEXT
27592 && part != ON_MODE_LINE
27593 && part != ON_HEADER_LINE))
27594 clear_mouse_face (hlinfo);
27596 /* Not on a window -> return. */
27597 if (!WINDOWP (window))
27598 return;
27600 /* Reset help_echo_string. It will get recomputed below. */
27601 help_echo_string = Qnil;
27603 /* Convert to window-relative pixel coordinates. */
27604 w = XWINDOW (window);
27605 frame_to_window_pixel_xy (w, &x, &y);
27607 #ifdef HAVE_WINDOW_SYSTEM
27608 /* Handle tool-bar window differently since it doesn't display a
27609 buffer. */
27610 if (EQ (window, f->tool_bar_window))
27612 note_tool_bar_highlight (f, x, y);
27613 return;
27615 #endif
27617 /* Mouse is on the mode, header line or margin? */
27618 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27619 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27621 note_mode_line_or_margin_highlight (window, x, y, part);
27622 return;
27625 #ifdef HAVE_WINDOW_SYSTEM
27626 if (part == ON_VERTICAL_BORDER)
27628 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27629 help_echo_string = build_string ("drag-mouse-1: resize");
27631 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27632 || part == ON_SCROLL_BAR)
27633 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27634 else
27635 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27636 #endif
27638 /* Are we in a window whose display is up to date?
27639 And verify the buffer's text has not changed. */
27640 b = XBUFFER (w->buffer);
27641 if (part == ON_TEXT
27642 && EQ (w->window_end_valid, w->buffer)
27643 && w->last_modified == BUF_MODIFF (b)
27644 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27646 int hpos, vpos, dx, dy, area = LAST_AREA;
27647 ptrdiff_t pos;
27648 struct glyph *glyph;
27649 Lisp_Object object;
27650 Lisp_Object mouse_face = Qnil, position;
27651 Lisp_Object *overlay_vec = NULL;
27652 ptrdiff_t i, noverlays;
27653 struct buffer *obuf;
27654 ptrdiff_t obegv, ozv;
27655 int same_region;
27657 /* Find the glyph under X/Y. */
27658 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27660 #ifdef HAVE_WINDOW_SYSTEM
27661 /* Look for :pointer property on image. */
27662 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27664 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27665 if (img != NULL && IMAGEP (img->spec))
27667 Lisp_Object image_map, hotspot;
27668 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27669 !NILP (image_map))
27670 && (hotspot = find_hot_spot (image_map,
27671 glyph->slice.img.x + dx,
27672 glyph->slice.img.y + dy),
27673 CONSP (hotspot))
27674 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27676 Lisp_Object plist;
27678 /* Could check XCAR (hotspot) to see if we enter/leave
27679 this hot-spot.
27680 If so, we could look for mouse-enter, mouse-leave
27681 properties in PLIST (and do something...). */
27682 hotspot = XCDR (hotspot);
27683 if (CONSP (hotspot)
27684 && (plist = XCAR (hotspot), CONSP (plist)))
27686 pointer = Fplist_get (plist, Qpointer);
27687 if (NILP (pointer))
27688 pointer = Qhand;
27689 help_echo_string = Fplist_get (plist, Qhelp_echo);
27690 if (!NILP (help_echo_string))
27692 help_echo_window = window;
27693 help_echo_object = glyph->object;
27694 help_echo_pos = glyph->charpos;
27698 if (NILP (pointer))
27699 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27702 #endif /* HAVE_WINDOW_SYSTEM */
27704 /* Clear mouse face if X/Y not over text. */
27705 if (glyph == NULL
27706 || area != TEXT_AREA
27707 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27708 /* Glyph's OBJECT is an integer for glyphs inserted by the
27709 display engine for its internal purposes, like truncation
27710 and continuation glyphs and blanks beyond the end of
27711 line's text on text terminals. If we are over such a
27712 glyph, we are not over any text. */
27713 || INTEGERP (glyph->object)
27714 /* R2L rows have a stretch glyph at their front, which
27715 stands for no text, whereas L2R rows have no glyphs at
27716 all beyond the end of text. Treat such stretch glyphs
27717 like we do with NULL glyphs in L2R rows. */
27718 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27719 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27720 && glyph->type == STRETCH_GLYPH
27721 && glyph->avoid_cursor_p))
27723 if (clear_mouse_face (hlinfo))
27724 cursor = No_Cursor;
27725 #ifdef HAVE_WINDOW_SYSTEM
27726 if (FRAME_WINDOW_P (f) && NILP (pointer))
27728 if (area != TEXT_AREA)
27729 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27730 else
27731 pointer = Vvoid_text_area_pointer;
27733 #endif
27734 goto set_cursor;
27737 pos = glyph->charpos;
27738 object = glyph->object;
27739 if (!STRINGP (object) && !BUFFERP (object))
27740 goto set_cursor;
27742 /* If we get an out-of-range value, return now; avoid an error. */
27743 if (BUFFERP (object) && pos > BUF_Z (b))
27744 goto set_cursor;
27746 /* Make the window's buffer temporarily current for
27747 overlays_at and compute_char_face. */
27748 obuf = current_buffer;
27749 current_buffer = b;
27750 obegv = BEGV;
27751 ozv = ZV;
27752 BEGV = BEG;
27753 ZV = Z;
27755 /* Is this char mouse-active or does it have help-echo? */
27756 position = make_number (pos);
27758 if (BUFFERP (object))
27760 /* Put all the overlays we want in a vector in overlay_vec. */
27761 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27762 /* Sort overlays into increasing priority order. */
27763 noverlays = sort_overlays (overlay_vec, noverlays, w);
27765 else
27766 noverlays = 0;
27768 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27770 if (same_region)
27771 cursor = No_Cursor;
27773 /* Check mouse-face highlighting. */
27774 if (! same_region
27775 /* If there exists an overlay with mouse-face overlapping
27776 the one we are currently highlighting, we have to
27777 check if we enter the overlapping overlay, and then
27778 highlight only that. */
27779 || (OVERLAYP (hlinfo->mouse_face_overlay)
27780 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27782 /* Find the highest priority overlay with a mouse-face. */
27783 Lisp_Object overlay = Qnil;
27784 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27786 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27787 if (!NILP (mouse_face))
27788 overlay = overlay_vec[i];
27791 /* If we're highlighting the same overlay as before, there's
27792 no need to do that again. */
27793 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27794 goto check_help_echo;
27795 hlinfo->mouse_face_overlay = overlay;
27797 /* Clear the display of the old active region, if any. */
27798 if (clear_mouse_face (hlinfo))
27799 cursor = No_Cursor;
27801 /* If no overlay applies, get a text property. */
27802 if (NILP (overlay))
27803 mouse_face = Fget_text_property (position, Qmouse_face, object);
27805 /* Next, compute the bounds of the mouse highlighting and
27806 display it. */
27807 if (!NILP (mouse_face) && STRINGP (object))
27809 /* The mouse-highlighting comes from a display string
27810 with a mouse-face. */
27811 Lisp_Object s, e;
27812 ptrdiff_t ignore;
27814 s = Fprevious_single_property_change
27815 (make_number (pos + 1), Qmouse_face, object, Qnil);
27816 e = Fnext_single_property_change
27817 (position, Qmouse_face, object, Qnil);
27818 if (NILP (s))
27819 s = make_number (0);
27820 if (NILP (e))
27821 e = make_number (SCHARS (object) - 1);
27822 mouse_face_from_string_pos (w, hlinfo, object,
27823 XINT (s), XINT (e));
27824 hlinfo->mouse_face_past_end = 0;
27825 hlinfo->mouse_face_window = window;
27826 hlinfo->mouse_face_face_id
27827 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27828 glyph->face_id, 1);
27829 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27830 cursor = No_Cursor;
27832 else
27834 /* The mouse-highlighting, if any, comes from an overlay
27835 or text property in the buffer. */
27836 Lisp_Object buffer IF_LINT (= Qnil);
27837 Lisp_Object disp_string IF_LINT (= Qnil);
27839 if (STRINGP (object))
27841 /* If we are on a display string with no mouse-face,
27842 check if the text under it has one. */
27843 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27844 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27845 pos = string_buffer_position (object, start);
27846 if (pos > 0)
27848 mouse_face = get_char_property_and_overlay
27849 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27850 buffer = w->buffer;
27851 disp_string = object;
27854 else
27856 buffer = object;
27857 disp_string = Qnil;
27860 if (!NILP (mouse_face))
27862 Lisp_Object before, after;
27863 Lisp_Object before_string, after_string;
27864 /* To correctly find the limits of mouse highlight
27865 in a bidi-reordered buffer, we must not use the
27866 optimization of limiting the search in
27867 previous-single-property-change and
27868 next-single-property-change, because
27869 rows_from_pos_range needs the real start and end
27870 positions to DTRT in this case. That's because
27871 the first row visible in a window does not
27872 necessarily display the character whose position
27873 is the smallest. */
27874 Lisp_Object lim1 =
27875 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27876 ? Fmarker_position (w->start)
27877 : Qnil;
27878 Lisp_Object lim2 =
27879 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27880 ? make_number (BUF_Z (XBUFFER (buffer))
27881 - XFASTINT (w->window_end_pos))
27882 : Qnil;
27884 if (NILP (overlay))
27886 /* Handle the text property case. */
27887 before = Fprevious_single_property_change
27888 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27889 after = Fnext_single_property_change
27890 (make_number (pos), Qmouse_face, buffer, lim2);
27891 before_string = after_string = Qnil;
27893 else
27895 /* Handle the overlay case. */
27896 before = Foverlay_start (overlay);
27897 after = Foverlay_end (overlay);
27898 before_string = Foverlay_get (overlay, Qbefore_string);
27899 after_string = Foverlay_get (overlay, Qafter_string);
27901 if (!STRINGP (before_string)) before_string = Qnil;
27902 if (!STRINGP (after_string)) after_string = Qnil;
27905 mouse_face_from_buffer_pos (window, hlinfo, pos,
27906 NILP (before)
27908 : XFASTINT (before),
27909 NILP (after)
27910 ? BUF_Z (XBUFFER (buffer))
27911 : XFASTINT (after),
27912 before_string, after_string,
27913 disp_string);
27914 cursor = No_Cursor;
27919 check_help_echo:
27921 /* Look for a `help-echo' property. */
27922 if (NILP (help_echo_string)) {
27923 Lisp_Object help, overlay;
27925 /* Check overlays first. */
27926 help = overlay = Qnil;
27927 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27929 overlay = overlay_vec[i];
27930 help = Foverlay_get (overlay, Qhelp_echo);
27933 if (!NILP (help))
27935 help_echo_string = help;
27936 help_echo_window = window;
27937 help_echo_object = overlay;
27938 help_echo_pos = pos;
27940 else
27942 Lisp_Object obj = glyph->object;
27943 ptrdiff_t charpos = glyph->charpos;
27945 /* Try text properties. */
27946 if (STRINGP (obj)
27947 && charpos >= 0
27948 && charpos < SCHARS (obj))
27950 help = Fget_text_property (make_number (charpos),
27951 Qhelp_echo, obj);
27952 if (NILP (help))
27954 /* If the string itself doesn't specify a help-echo,
27955 see if the buffer text ``under'' it does. */
27956 struct glyph_row *r
27957 = MATRIX_ROW (w->current_matrix, vpos);
27958 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27959 ptrdiff_t p = string_buffer_position (obj, start);
27960 if (p > 0)
27962 help = Fget_char_property (make_number (p),
27963 Qhelp_echo, w->buffer);
27964 if (!NILP (help))
27966 charpos = p;
27967 obj = w->buffer;
27972 else if (BUFFERP (obj)
27973 && charpos >= BEGV
27974 && charpos < ZV)
27975 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27976 obj);
27978 if (!NILP (help))
27980 help_echo_string = help;
27981 help_echo_window = window;
27982 help_echo_object = obj;
27983 help_echo_pos = charpos;
27988 #ifdef HAVE_WINDOW_SYSTEM
27989 /* Look for a `pointer' property. */
27990 if (FRAME_WINDOW_P (f) && NILP (pointer))
27992 /* Check overlays first. */
27993 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27994 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27996 if (NILP (pointer))
27998 Lisp_Object obj = glyph->object;
27999 ptrdiff_t charpos = glyph->charpos;
28001 /* Try text properties. */
28002 if (STRINGP (obj)
28003 && charpos >= 0
28004 && charpos < SCHARS (obj))
28006 pointer = Fget_text_property (make_number (charpos),
28007 Qpointer, obj);
28008 if (NILP (pointer))
28010 /* If the string itself doesn't specify a pointer,
28011 see if the buffer text ``under'' it does. */
28012 struct glyph_row *r
28013 = MATRIX_ROW (w->current_matrix, vpos);
28014 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28015 ptrdiff_t p = string_buffer_position (obj, start);
28016 if (p > 0)
28017 pointer = Fget_char_property (make_number (p),
28018 Qpointer, w->buffer);
28021 else if (BUFFERP (obj)
28022 && charpos >= BEGV
28023 && charpos < ZV)
28024 pointer = Fget_text_property (make_number (charpos),
28025 Qpointer, obj);
28028 #endif /* HAVE_WINDOW_SYSTEM */
28030 BEGV = obegv;
28031 ZV = ozv;
28032 current_buffer = obuf;
28035 set_cursor:
28037 #ifdef HAVE_WINDOW_SYSTEM
28038 if (FRAME_WINDOW_P (f))
28039 define_frame_cursor1 (f, cursor, pointer);
28040 #else
28041 /* This is here to prevent a compiler error, about "label at end of
28042 compound statement". */
28043 return;
28044 #endif
28048 /* EXPORT for RIF:
28049 Clear any mouse-face on window W. This function is part of the
28050 redisplay interface, and is called from try_window_id and similar
28051 functions to ensure the mouse-highlight is off. */
28053 void
28054 x_clear_window_mouse_face (struct window *w)
28056 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28057 Lisp_Object window;
28059 BLOCK_INPUT;
28060 XSETWINDOW (window, w);
28061 if (EQ (window, hlinfo->mouse_face_window))
28062 clear_mouse_face (hlinfo);
28063 UNBLOCK_INPUT;
28067 /* EXPORT:
28068 Just discard the mouse face information for frame F, if any.
28069 This is used when the size of F is changed. */
28071 void
28072 cancel_mouse_face (struct frame *f)
28074 Lisp_Object window;
28075 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28077 window = hlinfo->mouse_face_window;
28078 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28080 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28081 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28082 hlinfo->mouse_face_window = Qnil;
28088 /***********************************************************************
28089 Exposure Events
28090 ***********************************************************************/
28092 #ifdef HAVE_WINDOW_SYSTEM
28094 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28095 which intersects rectangle R. R is in window-relative coordinates. */
28097 static void
28098 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28099 enum glyph_row_area area)
28101 struct glyph *first = row->glyphs[area];
28102 struct glyph *end = row->glyphs[area] + row->used[area];
28103 struct glyph *last;
28104 int first_x, start_x, x;
28106 if (area == TEXT_AREA && row->fill_line_p)
28107 /* If row extends face to end of line write the whole line. */
28108 draw_glyphs (w, 0, row, area,
28109 0, row->used[area],
28110 DRAW_NORMAL_TEXT, 0);
28111 else
28113 /* Set START_X to the window-relative start position for drawing glyphs of
28114 AREA. The first glyph of the text area can be partially visible.
28115 The first glyphs of other areas cannot. */
28116 start_x = window_box_left_offset (w, area);
28117 x = start_x;
28118 if (area == TEXT_AREA)
28119 x += row->x;
28121 /* Find the first glyph that must be redrawn. */
28122 while (first < end
28123 && x + first->pixel_width < r->x)
28125 x += first->pixel_width;
28126 ++first;
28129 /* Find the last one. */
28130 last = first;
28131 first_x = x;
28132 while (last < end
28133 && x < r->x + r->width)
28135 x += last->pixel_width;
28136 ++last;
28139 /* Repaint. */
28140 if (last > first)
28141 draw_glyphs (w, first_x - start_x, row, area,
28142 first - row->glyphs[area], last - row->glyphs[area],
28143 DRAW_NORMAL_TEXT, 0);
28148 /* Redraw the parts of the glyph row ROW on window W intersecting
28149 rectangle R. R is in window-relative coordinates. Value is
28150 non-zero if mouse-face was overwritten. */
28152 static int
28153 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28155 eassert (row->enabled_p);
28157 if (row->mode_line_p || w->pseudo_window_p)
28158 draw_glyphs (w, 0, row, TEXT_AREA,
28159 0, row->used[TEXT_AREA],
28160 DRAW_NORMAL_TEXT, 0);
28161 else
28163 if (row->used[LEFT_MARGIN_AREA])
28164 expose_area (w, row, r, LEFT_MARGIN_AREA);
28165 if (row->used[TEXT_AREA])
28166 expose_area (w, row, r, TEXT_AREA);
28167 if (row->used[RIGHT_MARGIN_AREA])
28168 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28169 draw_row_fringe_bitmaps (w, row);
28172 return row->mouse_face_p;
28176 /* Redraw those parts of glyphs rows during expose event handling that
28177 overlap other rows. Redrawing of an exposed line writes over parts
28178 of lines overlapping that exposed line; this function fixes that.
28180 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28181 row in W's current matrix that is exposed and overlaps other rows.
28182 LAST_OVERLAPPING_ROW is the last such row. */
28184 static void
28185 expose_overlaps (struct window *w,
28186 struct glyph_row *first_overlapping_row,
28187 struct glyph_row *last_overlapping_row,
28188 XRectangle *r)
28190 struct glyph_row *row;
28192 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28193 if (row->overlapping_p)
28195 eassert (row->enabled_p && !row->mode_line_p);
28197 row->clip = r;
28198 if (row->used[LEFT_MARGIN_AREA])
28199 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28201 if (row->used[TEXT_AREA])
28202 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28204 if (row->used[RIGHT_MARGIN_AREA])
28205 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28206 row->clip = NULL;
28211 /* Return non-zero if W's cursor intersects rectangle R. */
28213 static int
28214 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28216 XRectangle cr, result;
28217 struct glyph *cursor_glyph;
28218 struct glyph_row *row;
28220 if (w->phys_cursor.vpos >= 0
28221 && w->phys_cursor.vpos < w->current_matrix->nrows
28222 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28223 row->enabled_p)
28224 && row->cursor_in_fringe_p)
28226 /* Cursor is in the fringe. */
28227 cr.x = window_box_right_offset (w,
28228 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28229 ? RIGHT_MARGIN_AREA
28230 : TEXT_AREA));
28231 cr.y = row->y;
28232 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28233 cr.height = row->height;
28234 return x_intersect_rectangles (&cr, r, &result);
28237 cursor_glyph = get_phys_cursor_glyph (w);
28238 if (cursor_glyph)
28240 /* r is relative to W's box, but w->phys_cursor.x is relative
28241 to left edge of W's TEXT area. Adjust it. */
28242 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28243 cr.y = w->phys_cursor.y;
28244 cr.width = cursor_glyph->pixel_width;
28245 cr.height = w->phys_cursor_height;
28246 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28247 I assume the effect is the same -- and this is portable. */
28248 return x_intersect_rectangles (&cr, r, &result);
28250 /* If we don't understand the format, pretend we're not in the hot-spot. */
28251 return 0;
28255 /* EXPORT:
28256 Draw a vertical window border to the right of window W if W doesn't
28257 have vertical scroll bars. */
28259 void
28260 x_draw_vertical_border (struct window *w)
28262 struct frame *f = XFRAME (WINDOW_FRAME (w));
28264 /* We could do better, if we knew what type of scroll-bar the adjacent
28265 windows (on either side) have... But we don't :-(
28266 However, I think this works ok. ++KFS 2003-04-25 */
28268 /* Redraw borders between horizontally adjacent windows. Don't
28269 do it for frames with vertical scroll bars because either the
28270 right scroll bar of a window, or the left scroll bar of its
28271 neighbor will suffice as a border. */
28272 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28273 return;
28275 if (!WINDOW_RIGHTMOST_P (w)
28276 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28278 int x0, x1, y0, y1;
28280 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28281 y1 -= 1;
28283 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28284 x1 -= 1;
28286 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28288 else if (!WINDOW_LEFTMOST_P (w)
28289 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28291 int x0, x1, y0, y1;
28293 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28294 y1 -= 1;
28296 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28297 x0 -= 1;
28299 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28304 /* Redraw the part of window W intersection rectangle FR. Pixel
28305 coordinates in FR are frame-relative. Call this function with
28306 input blocked. Value is non-zero if the exposure overwrites
28307 mouse-face. */
28309 static int
28310 expose_window (struct window *w, XRectangle *fr)
28312 struct frame *f = XFRAME (w->frame);
28313 XRectangle wr, r;
28314 int mouse_face_overwritten_p = 0;
28316 /* If window is not yet fully initialized, do nothing. This can
28317 happen when toolkit scroll bars are used and a window is split.
28318 Reconfiguring the scroll bar will generate an expose for a newly
28319 created window. */
28320 if (w->current_matrix == NULL)
28321 return 0;
28323 /* When we're currently updating the window, display and current
28324 matrix usually don't agree. Arrange for a thorough display
28325 later. */
28326 if (w == updated_window)
28328 SET_FRAME_GARBAGED (f);
28329 return 0;
28332 /* Frame-relative pixel rectangle of W. */
28333 wr.x = WINDOW_LEFT_EDGE_X (w);
28334 wr.y = WINDOW_TOP_EDGE_Y (w);
28335 wr.width = WINDOW_TOTAL_WIDTH (w);
28336 wr.height = WINDOW_TOTAL_HEIGHT (w);
28338 if (x_intersect_rectangles (fr, &wr, &r))
28340 int yb = window_text_bottom_y (w);
28341 struct glyph_row *row;
28342 int cursor_cleared_p, phys_cursor_on_p;
28343 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28345 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28346 r.x, r.y, r.width, r.height));
28348 /* Convert to window coordinates. */
28349 r.x -= WINDOW_LEFT_EDGE_X (w);
28350 r.y -= WINDOW_TOP_EDGE_Y (w);
28352 /* Turn off the cursor. */
28353 if (!w->pseudo_window_p
28354 && phys_cursor_in_rect_p (w, &r))
28356 x_clear_cursor (w);
28357 cursor_cleared_p = 1;
28359 else
28360 cursor_cleared_p = 0;
28362 /* If the row containing the cursor extends face to end of line,
28363 then expose_area might overwrite the cursor outside the
28364 rectangle and thus notice_overwritten_cursor might clear
28365 w->phys_cursor_on_p. We remember the original value and
28366 check later if it is changed. */
28367 phys_cursor_on_p = w->phys_cursor_on_p;
28369 /* Update lines intersecting rectangle R. */
28370 first_overlapping_row = last_overlapping_row = NULL;
28371 for (row = w->current_matrix->rows;
28372 row->enabled_p;
28373 ++row)
28375 int y0 = row->y;
28376 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28378 if ((y0 >= r.y && y0 < r.y + r.height)
28379 || (y1 > r.y && y1 < r.y + r.height)
28380 || (r.y >= y0 && r.y < y1)
28381 || (r.y + r.height > y0 && r.y + r.height < y1))
28383 /* A header line may be overlapping, but there is no need
28384 to fix overlapping areas for them. KFS 2005-02-12 */
28385 if (row->overlapping_p && !row->mode_line_p)
28387 if (first_overlapping_row == NULL)
28388 first_overlapping_row = row;
28389 last_overlapping_row = row;
28392 row->clip = fr;
28393 if (expose_line (w, row, &r))
28394 mouse_face_overwritten_p = 1;
28395 row->clip = NULL;
28397 else if (row->overlapping_p)
28399 /* We must redraw a row overlapping the exposed area. */
28400 if (y0 < r.y
28401 ? y0 + row->phys_height > r.y
28402 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28404 if (first_overlapping_row == NULL)
28405 first_overlapping_row = row;
28406 last_overlapping_row = row;
28410 if (y1 >= yb)
28411 break;
28414 /* Display the mode line if there is one. */
28415 if (WINDOW_WANTS_MODELINE_P (w)
28416 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28417 row->enabled_p)
28418 && row->y < r.y + r.height)
28420 if (expose_line (w, row, &r))
28421 mouse_face_overwritten_p = 1;
28424 if (!w->pseudo_window_p)
28426 /* Fix the display of overlapping rows. */
28427 if (first_overlapping_row)
28428 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28429 fr);
28431 /* Draw border between windows. */
28432 x_draw_vertical_border (w);
28434 /* Turn the cursor on again. */
28435 if (cursor_cleared_p
28436 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28437 update_window_cursor (w, 1);
28441 return mouse_face_overwritten_p;
28446 /* Redraw (parts) of all windows in the window tree rooted at W that
28447 intersect R. R contains frame pixel coordinates. Value is
28448 non-zero if the exposure overwrites mouse-face. */
28450 static int
28451 expose_window_tree (struct window *w, XRectangle *r)
28453 struct frame *f = XFRAME (w->frame);
28454 int mouse_face_overwritten_p = 0;
28456 while (w && !FRAME_GARBAGED_P (f))
28458 if (!NILP (w->hchild))
28459 mouse_face_overwritten_p
28460 |= expose_window_tree (XWINDOW (w->hchild), r);
28461 else if (!NILP (w->vchild))
28462 mouse_face_overwritten_p
28463 |= expose_window_tree (XWINDOW (w->vchild), r);
28464 else
28465 mouse_face_overwritten_p |= expose_window (w, r);
28467 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28470 return mouse_face_overwritten_p;
28474 /* EXPORT:
28475 Redisplay an exposed area of frame F. X and Y are the upper-left
28476 corner of the exposed rectangle. W and H are width and height of
28477 the exposed area. All are pixel values. W or H zero means redraw
28478 the entire frame. */
28480 void
28481 expose_frame (struct frame *f, int x, int y, int w, int h)
28483 XRectangle r;
28484 int mouse_face_overwritten_p = 0;
28486 TRACE ((stderr, "expose_frame "));
28488 /* No need to redraw if frame will be redrawn soon. */
28489 if (FRAME_GARBAGED_P (f))
28491 TRACE ((stderr, " garbaged\n"));
28492 return;
28495 /* If basic faces haven't been realized yet, there is no point in
28496 trying to redraw anything. This can happen when we get an expose
28497 event while Emacs is starting, e.g. by moving another window. */
28498 if (FRAME_FACE_CACHE (f) == NULL
28499 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28501 TRACE ((stderr, " no faces\n"));
28502 return;
28505 if (w == 0 || h == 0)
28507 r.x = r.y = 0;
28508 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28509 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28511 else
28513 r.x = x;
28514 r.y = y;
28515 r.width = w;
28516 r.height = h;
28519 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28520 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28522 if (WINDOWP (f->tool_bar_window))
28523 mouse_face_overwritten_p
28524 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28526 #ifdef HAVE_X_WINDOWS
28527 #ifndef MSDOS
28528 #ifndef USE_X_TOOLKIT
28529 if (WINDOWP (f->menu_bar_window))
28530 mouse_face_overwritten_p
28531 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28532 #endif /* not USE_X_TOOLKIT */
28533 #endif
28534 #endif
28536 /* Some window managers support a focus-follows-mouse style with
28537 delayed raising of frames. Imagine a partially obscured frame,
28538 and moving the mouse into partially obscured mouse-face on that
28539 frame. The visible part of the mouse-face will be highlighted,
28540 then the WM raises the obscured frame. With at least one WM, KDE
28541 2.1, Emacs is not getting any event for the raising of the frame
28542 (even tried with SubstructureRedirectMask), only Expose events.
28543 These expose events will draw text normally, i.e. not
28544 highlighted. Which means we must redo the highlight here.
28545 Subsume it under ``we love X''. --gerd 2001-08-15 */
28546 /* Included in Windows version because Windows most likely does not
28547 do the right thing if any third party tool offers
28548 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28549 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28551 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28552 if (f == hlinfo->mouse_face_mouse_frame)
28554 int mouse_x = hlinfo->mouse_face_mouse_x;
28555 int mouse_y = hlinfo->mouse_face_mouse_y;
28556 clear_mouse_face (hlinfo);
28557 note_mouse_highlight (f, mouse_x, mouse_y);
28563 /* EXPORT:
28564 Determine the intersection of two rectangles R1 and R2. Return
28565 the intersection in *RESULT. Value is non-zero if RESULT is not
28566 empty. */
28569 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28571 XRectangle *left, *right;
28572 XRectangle *upper, *lower;
28573 int intersection_p = 0;
28575 /* Rearrange so that R1 is the left-most rectangle. */
28576 if (r1->x < r2->x)
28577 left = r1, right = r2;
28578 else
28579 left = r2, right = r1;
28581 /* X0 of the intersection is right.x0, if this is inside R1,
28582 otherwise there is no intersection. */
28583 if (right->x <= left->x + left->width)
28585 result->x = right->x;
28587 /* The right end of the intersection is the minimum of
28588 the right ends of left and right. */
28589 result->width = (min (left->x + left->width, right->x + right->width)
28590 - result->x);
28592 /* Same game for Y. */
28593 if (r1->y < r2->y)
28594 upper = r1, lower = r2;
28595 else
28596 upper = r2, lower = r1;
28598 /* The upper end of the intersection is lower.y0, if this is inside
28599 of upper. Otherwise, there is no intersection. */
28600 if (lower->y <= upper->y + upper->height)
28602 result->y = lower->y;
28604 /* The lower end of the intersection is the minimum of the lower
28605 ends of upper and lower. */
28606 result->height = (min (lower->y + lower->height,
28607 upper->y + upper->height)
28608 - result->y);
28609 intersection_p = 1;
28613 return intersection_p;
28616 #endif /* HAVE_WINDOW_SYSTEM */
28619 /***********************************************************************
28620 Initialization
28621 ***********************************************************************/
28623 void
28624 syms_of_xdisp (void)
28626 Vwith_echo_area_save_vector = Qnil;
28627 staticpro (&Vwith_echo_area_save_vector);
28629 Vmessage_stack = Qnil;
28630 staticpro (&Vmessage_stack);
28632 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28634 message_dolog_marker1 = Fmake_marker ();
28635 staticpro (&message_dolog_marker1);
28636 message_dolog_marker2 = Fmake_marker ();
28637 staticpro (&message_dolog_marker2);
28638 message_dolog_marker3 = Fmake_marker ();
28639 staticpro (&message_dolog_marker3);
28641 #ifdef GLYPH_DEBUG
28642 defsubr (&Sdump_frame_glyph_matrix);
28643 defsubr (&Sdump_glyph_matrix);
28644 defsubr (&Sdump_glyph_row);
28645 defsubr (&Sdump_tool_bar_row);
28646 defsubr (&Strace_redisplay);
28647 defsubr (&Strace_to_stderr);
28648 #endif
28649 #ifdef HAVE_WINDOW_SYSTEM
28650 defsubr (&Stool_bar_lines_needed);
28651 defsubr (&Slookup_image_map);
28652 #endif
28653 defsubr (&Sformat_mode_line);
28654 defsubr (&Sinvisible_p);
28655 defsubr (&Scurrent_bidi_paragraph_direction);
28657 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28658 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28659 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28660 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28661 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28662 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28663 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28664 DEFSYM (Qeval, "eval");
28665 DEFSYM (QCdata, ":data");
28666 DEFSYM (Qdisplay, "display");
28667 DEFSYM (Qspace_width, "space-width");
28668 DEFSYM (Qraise, "raise");
28669 DEFSYM (Qslice, "slice");
28670 DEFSYM (Qspace, "space");
28671 DEFSYM (Qmargin, "margin");
28672 DEFSYM (Qpointer, "pointer");
28673 DEFSYM (Qleft_margin, "left-margin");
28674 DEFSYM (Qright_margin, "right-margin");
28675 DEFSYM (Qcenter, "center");
28676 DEFSYM (Qline_height, "line-height");
28677 DEFSYM (QCalign_to, ":align-to");
28678 DEFSYM (QCrelative_width, ":relative-width");
28679 DEFSYM (QCrelative_height, ":relative-height");
28680 DEFSYM (QCeval, ":eval");
28681 DEFSYM (QCpropertize, ":propertize");
28682 DEFSYM (QCfile, ":file");
28683 DEFSYM (Qfontified, "fontified");
28684 DEFSYM (Qfontification_functions, "fontification-functions");
28685 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28686 DEFSYM (Qescape_glyph, "escape-glyph");
28687 DEFSYM (Qnobreak_space, "nobreak-space");
28688 DEFSYM (Qimage, "image");
28689 DEFSYM (Qtext, "text");
28690 DEFSYM (Qboth, "both");
28691 DEFSYM (Qboth_horiz, "both-horiz");
28692 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28693 DEFSYM (QCmap, ":map");
28694 DEFSYM (QCpointer, ":pointer");
28695 DEFSYM (Qrect, "rect");
28696 DEFSYM (Qcircle, "circle");
28697 DEFSYM (Qpoly, "poly");
28698 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28699 DEFSYM (Qgrow_only, "grow-only");
28700 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28701 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28702 DEFSYM (Qposition, "position");
28703 DEFSYM (Qbuffer_position, "buffer-position");
28704 DEFSYM (Qobject, "object");
28705 DEFSYM (Qbar, "bar");
28706 DEFSYM (Qhbar, "hbar");
28707 DEFSYM (Qbox, "box");
28708 DEFSYM (Qhollow, "hollow");
28709 DEFSYM (Qhand, "hand");
28710 DEFSYM (Qarrow, "arrow");
28711 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28713 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28714 Fcons (intern_c_string ("void-variable"), Qnil)),
28715 Qnil);
28716 staticpro (&list_of_error);
28718 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28719 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28720 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28721 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28723 echo_buffer[0] = echo_buffer[1] = Qnil;
28724 staticpro (&echo_buffer[0]);
28725 staticpro (&echo_buffer[1]);
28727 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28728 staticpro (&echo_area_buffer[0]);
28729 staticpro (&echo_area_buffer[1]);
28731 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
28732 staticpro (&Vmessages_buffer_name);
28734 mode_line_proptrans_alist = Qnil;
28735 staticpro (&mode_line_proptrans_alist);
28736 mode_line_string_list = Qnil;
28737 staticpro (&mode_line_string_list);
28738 mode_line_string_face = Qnil;
28739 staticpro (&mode_line_string_face);
28740 mode_line_string_face_prop = Qnil;
28741 staticpro (&mode_line_string_face_prop);
28742 Vmode_line_unwind_vector = Qnil;
28743 staticpro (&Vmode_line_unwind_vector);
28745 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28747 help_echo_string = Qnil;
28748 staticpro (&help_echo_string);
28749 help_echo_object = Qnil;
28750 staticpro (&help_echo_object);
28751 help_echo_window = Qnil;
28752 staticpro (&help_echo_window);
28753 previous_help_echo_string = Qnil;
28754 staticpro (&previous_help_echo_string);
28755 help_echo_pos = -1;
28757 DEFSYM (Qright_to_left, "right-to-left");
28758 DEFSYM (Qleft_to_right, "left-to-right");
28760 #ifdef HAVE_WINDOW_SYSTEM
28761 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28762 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28763 For example, if a block cursor is over a tab, it will be drawn as
28764 wide as that tab on the display. */);
28765 x_stretch_cursor_p = 0;
28766 #endif
28768 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28769 doc: /* Non-nil means highlight trailing whitespace.
28770 The face used for trailing whitespace is `trailing-whitespace'. */);
28771 Vshow_trailing_whitespace = Qnil;
28773 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28774 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28775 If the value is t, Emacs highlights non-ASCII chars which have the
28776 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28777 or `escape-glyph' face respectively.
28779 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28780 U+2011 (non-breaking hyphen) are affected.
28782 Any other non-nil value means to display these characters as a escape
28783 glyph followed by an ordinary space or hyphen.
28785 A value of nil means no special handling of these characters. */);
28786 Vnobreak_char_display = Qt;
28788 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28789 doc: /* The pointer shape to show in void text areas.
28790 A value of nil means to show the text pointer. Other options are `arrow',
28791 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28792 Vvoid_text_area_pointer = Qarrow;
28794 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28795 doc: /* Non-nil means don't actually do any redisplay.
28796 This is used for internal purposes. */);
28797 Vinhibit_redisplay = Qnil;
28799 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28800 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28801 Vglobal_mode_string = Qnil;
28803 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28804 doc: /* Marker for where to display an arrow on top of the buffer text.
28805 This must be the beginning of a line in order to work.
28806 See also `overlay-arrow-string'. */);
28807 Voverlay_arrow_position = Qnil;
28809 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28810 doc: /* String to display as an arrow in non-window frames.
28811 See also `overlay-arrow-position'. */);
28812 Voverlay_arrow_string = make_pure_c_string ("=>");
28814 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28815 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28816 The symbols on this list are examined during redisplay to determine
28817 where to display overlay arrows. */);
28818 Voverlay_arrow_variable_list
28819 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28821 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28822 doc: /* The number of lines to try scrolling a window by when point moves out.
28823 If that fails to bring point back on frame, point is centered instead.
28824 If this is zero, point is always centered after it moves off frame.
28825 If you want scrolling to always be a line at a time, you should set
28826 `scroll-conservatively' to a large value rather than set this to 1. */);
28828 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28829 doc: /* Scroll up to this many lines, to bring point back on screen.
28830 If point moves off-screen, redisplay will scroll by up to
28831 `scroll-conservatively' lines in order to bring point just barely
28832 onto the screen again. If that cannot be done, then redisplay
28833 recenters point as usual.
28835 If the value is greater than 100, redisplay will never recenter point,
28836 but will always scroll just enough text to bring point into view, even
28837 if you move far away.
28839 A value of zero means always recenter point if it moves off screen. */);
28840 scroll_conservatively = 0;
28842 DEFVAR_INT ("scroll-margin", scroll_margin,
28843 doc: /* Number of lines of margin at the top and bottom of a window.
28844 Recenter the window whenever point gets within this many lines
28845 of the top or bottom of the window. */);
28846 scroll_margin = 0;
28848 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28849 doc: /* Pixels per inch value for non-window system displays.
28850 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28851 Vdisplay_pixels_per_inch = make_float (72.0);
28853 #ifdef GLYPH_DEBUG
28854 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28855 #endif
28857 DEFVAR_LISP ("truncate-partial-width-windows",
28858 Vtruncate_partial_width_windows,
28859 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28860 For an integer value, truncate lines in each window narrower than the
28861 full frame width, provided the window width is less than that integer;
28862 otherwise, respect the value of `truncate-lines'.
28864 For any other non-nil value, truncate lines in all windows that do
28865 not span the full frame width.
28867 A value of nil means to respect the value of `truncate-lines'.
28869 If `word-wrap' is enabled, you might want to reduce this. */);
28870 Vtruncate_partial_width_windows = make_number (50);
28872 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28873 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28874 Any other value means to use the appropriate face, `mode-line',
28875 `header-line', or `menu' respectively. */);
28876 mode_line_inverse_video = 1;
28878 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28879 doc: /* Maximum buffer size for which line number should be displayed.
28880 If the buffer is bigger than this, the line number does not appear
28881 in the mode line. A value of nil means no limit. */);
28882 Vline_number_display_limit = Qnil;
28884 DEFVAR_INT ("line-number-display-limit-width",
28885 line_number_display_limit_width,
28886 doc: /* Maximum line width (in characters) for line number display.
28887 If the average length of the lines near point is bigger than this, then the
28888 line number may be omitted from the mode line. */);
28889 line_number_display_limit_width = 200;
28891 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28892 doc: /* Non-nil means highlight region even in nonselected windows. */);
28893 highlight_nonselected_windows = 0;
28895 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28896 doc: /* Non-nil if more than one frame is visible on this display.
28897 Minibuffer-only frames don't count, but iconified frames do.
28898 This variable is not guaranteed to be accurate except while processing
28899 `frame-title-format' and `icon-title-format'. */);
28901 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28902 doc: /* Template for displaying the title bar of visible frames.
28903 \(Assuming the window manager supports this feature.)
28905 This variable has the same structure as `mode-line-format', except that
28906 the %c and %l constructs are ignored. It is used only on frames for
28907 which no explicit name has been set \(see `modify-frame-parameters'). */);
28909 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28910 doc: /* Template for displaying the title bar of an iconified frame.
28911 \(Assuming the window manager supports this feature.)
28912 This variable has the same structure as `mode-line-format' (which see),
28913 and is used only on frames for which no explicit name has been set
28914 \(see `modify-frame-parameters'). */);
28915 Vicon_title_format
28916 = Vframe_title_format
28917 = pure_cons (intern_c_string ("multiple-frames"),
28918 pure_cons (make_pure_c_string ("%b"),
28919 pure_cons (pure_cons (empty_unibyte_string,
28920 pure_cons (intern_c_string ("invocation-name"),
28921 pure_cons (make_pure_c_string ("@"),
28922 pure_cons (intern_c_string ("system-name"),
28923 Qnil)))),
28924 Qnil)));
28926 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28927 doc: /* Maximum number of lines to keep in the message log buffer.
28928 If nil, disable message logging. If t, log messages but don't truncate
28929 the buffer when it becomes large. */);
28930 Vmessage_log_max = make_number (100);
28932 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28933 doc: /* Functions called before redisplay, if window sizes have changed.
28934 The value should be a list of functions that take one argument.
28935 Just before redisplay, for each frame, if any of its windows have changed
28936 size since the last redisplay, or have been split or deleted,
28937 all the functions in the list are called, with the frame as argument. */);
28938 Vwindow_size_change_functions = Qnil;
28940 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28941 doc: /* List of functions to call before redisplaying a window with scrolling.
28942 Each function is called with two arguments, the window and its new
28943 display-start position. Note that these functions are also called by
28944 `set-window-buffer'. Also note that the value of `window-end' is not
28945 valid when these functions are called.
28947 Warning: Do not use this feature to alter the way the window
28948 is scrolled. It is not designed for that, and such use probably won't
28949 work. */);
28950 Vwindow_scroll_functions = Qnil;
28952 DEFVAR_LISP ("window-text-change-functions",
28953 Vwindow_text_change_functions,
28954 doc: /* Functions to call in redisplay when text in the window might change. */);
28955 Vwindow_text_change_functions = Qnil;
28957 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28958 doc: /* Functions called when redisplay of a window reaches the end trigger.
28959 Each function is called with two arguments, the window and the end trigger value.
28960 See `set-window-redisplay-end-trigger'. */);
28961 Vredisplay_end_trigger_functions = Qnil;
28963 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28964 doc: /* Non-nil means autoselect window with mouse pointer.
28965 If nil, do not autoselect windows.
28966 A positive number means delay autoselection by that many seconds: a
28967 window is autoselected only after the mouse has remained in that
28968 window for the duration of the delay.
28969 A negative number has a similar effect, but causes windows to be
28970 autoselected only after the mouse has stopped moving. \(Because of
28971 the way Emacs compares mouse events, you will occasionally wait twice
28972 that time before the window gets selected.\)
28973 Any other value means to autoselect window instantaneously when the
28974 mouse pointer enters it.
28976 Autoselection selects the minibuffer only if it is active, and never
28977 unselects the minibuffer if it is active.
28979 When customizing this variable make sure that the actual value of
28980 `focus-follows-mouse' matches the behavior of your window manager. */);
28981 Vmouse_autoselect_window = Qnil;
28983 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28984 doc: /* Non-nil means automatically resize tool-bars.
28985 This dynamically changes the tool-bar's height to the minimum height
28986 that is needed to make all tool-bar items visible.
28987 If value is `grow-only', the tool-bar's height is only increased
28988 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28989 Vauto_resize_tool_bars = Qt;
28991 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28992 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28993 auto_raise_tool_bar_buttons_p = 1;
28995 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28996 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28997 make_cursor_line_fully_visible_p = 1;
28999 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29000 doc: /* Border below tool-bar in pixels.
29001 If an integer, use it as the height of the border.
29002 If it is one of `internal-border-width' or `border-width', use the
29003 value of the corresponding frame parameter.
29004 Otherwise, no border is added below the tool-bar. */);
29005 Vtool_bar_border = Qinternal_border_width;
29007 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29008 doc: /* Margin around tool-bar buttons in pixels.
29009 If an integer, use that for both horizontal and vertical margins.
29010 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29011 HORZ specifying the horizontal margin, and VERT specifying the
29012 vertical margin. */);
29013 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29015 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29016 doc: /* Relief thickness of tool-bar buttons. */);
29017 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29019 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29020 doc: /* Tool bar style to use.
29021 It can be one of
29022 image - show images only
29023 text - show text only
29024 both - show both, text below image
29025 both-horiz - show text to the right of the image
29026 text-image-horiz - show text to the left of the image
29027 any other - use system default or image if no system default.
29029 This variable only affects the GTK+ toolkit version of Emacs. */);
29030 Vtool_bar_style = Qnil;
29032 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29033 doc: /* Maximum number of characters a label can have to be shown.
29034 The tool bar style must also show labels for this to have any effect, see
29035 `tool-bar-style'. */);
29036 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29038 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29039 doc: /* List of functions to call to fontify regions of text.
29040 Each function is called with one argument POS. Functions must
29041 fontify a region starting at POS in the current buffer, and give
29042 fontified regions the property `fontified'. */);
29043 Vfontification_functions = Qnil;
29044 Fmake_variable_buffer_local (Qfontification_functions);
29046 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29047 unibyte_display_via_language_environment,
29048 doc: /* Non-nil means display unibyte text according to language environment.
29049 Specifically, this means that raw bytes in the range 160-255 decimal
29050 are displayed by converting them to the equivalent multibyte characters
29051 according to the current language environment. As a result, they are
29052 displayed according to the current fontset.
29054 Note that this variable affects only how these bytes are displayed,
29055 but does not change the fact they are interpreted as raw bytes. */);
29056 unibyte_display_via_language_environment = 0;
29058 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29059 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29060 If a float, it specifies a fraction of the mini-window frame's height.
29061 If an integer, it specifies a number of lines. */);
29062 Vmax_mini_window_height = make_float (0.25);
29064 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29065 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29066 A value of nil means don't automatically resize mini-windows.
29067 A value of t means resize them to fit the text displayed in them.
29068 A value of `grow-only', the default, means let mini-windows grow only;
29069 they return to their normal size when the minibuffer is closed, or the
29070 echo area becomes empty. */);
29071 Vresize_mini_windows = Qgrow_only;
29073 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29074 doc: /* Alist specifying how to blink the cursor off.
29075 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29076 `cursor-type' frame-parameter or variable equals ON-STATE,
29077 comparing using `equal', Emacs uses OFF-STATE to specify
29078 how to blink it off. ON-STATE and OFF-STATE are values for
29079 the `cursor-type' frame parameter.
29081 If a frame's ON-STATE has no entry in this list,
29082 the frame's other specifications determine how to blink the cursor off. */);
29083 Vblink_cursor_alist = Qnil;
29085 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29086 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29087 If non-nil, windows are automatically scrolled horizontally to make
29088 point visible. */);
29089 automatic_hscrolling_p = 1;
29090 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29092 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29093 doc: /* How many columns away from the window edge point is allowed to get
29094 before automatic hscrolling will horizontally scroll the window. */);
29095 hscroll_margin = 5;
29097 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29098 doc: /* How many columns to scroll the window when point gets too close to the edge.
29099 When point is less than `hscroll-margin' columns from the window
29100 edge, automatic hscrolling will scroll the window by the amount of columns
29101 determined by this variable. If its value is a positive integer, scroll that
29102 many columns. If it's a positive floating-point number, it specifies the
29103 fraction of the window's width to scroll. If it's nil or zero, point will be
29104 centered horizontally after the scroll. Any other value, including negative
29105 numbers, are treated as if the value were zero.
29107 Automatic hscrolling always moves point outside the scroll margin, so if
29108 point was more than scroll step columns inside the margin, the window will
29109 scroll more than the value given by the scroll step.
29111 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29112 and `scroll-right' overrides this variable's effect. */);
29113 Vhscroll_step = make_number (0);
29115 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29116 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29117 Bind this around calls to `message' to let it take effect. */);
29118 message_truncate_lines = 0;
29120 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29121 doc: /* Normal hook run to update the menu bar definitions.
29122 Redisplay runs this hook before it redisplays the menu bar.
29123 This is used to update submenus such as Buffers,
29124 whose contents depend on various data. */);
29125 Vmenu_bar_update_hook = Qnil;
29127 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29128 doc: /* Frame for which we are updating a menu.
29129 The enable predicate for a menu binding should check this variable. */);
29130 Vmenu_updating_frame = Qnil;
29132 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29133 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29134 inhibit_menubar_update = 0;
29136 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29137 doc: /* Prefix prepended to all continuation lines at display time.
29138 The value may be a string, an image, or a stretch-glyph; it is
29139 interpreted in the same way as the value of a `display' text property.
29141 This variable is overridden by any `wrap-prefix' text or overlay
29142 property.
29144 To add a prefix to non-continuation lines, use `line-prefix'. */);
29145 Vwrap_prefix = Qnil;
29146 DEFSYM (Qwrap_prefix, "wrap-prefix");
29147 Fmake_variable_buffer_local (Qwrap_prefix);
29149 DEFVAR_LISP ("line-prefix", Vline_prefix,
29150 doc: /* Prefix prepended to all non-continuation lines at display time.
29151 The value may be a string, an image, or a stretch-glyph; it is
29152 interpreted in the same way as the value of a `display' text property.
29154 This variable is overridden by any `line-prefix' text or overlay
29155 property.
29157 To add a prefix to continuation lines, use `wrap-prefix'. */);
29158 Vline_prefix = Qnil;
29159 DEFSYM (Qline_prefix, "line-prefix");
29160 Fmake_variable_buffer_local (Qline_prefix);
29162 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29163 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29164 inhibit_eval_during_redisplay = 0;
29166 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29167 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29168 inhibit_free_realized_faces = 0;
29170 #ifdef GLYPH_DEBUG
29171 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29172 doc: /* Inhibit try_window_id display optimization. */);
29173 inhibit_try_window_id = 0;
29175 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29176 doc: /* Inhibit try_window_reusing display optimization. */);
29177 inhibit_try_window_reusing = 0;
29179 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29180 doc: /* Inhibit try_cursor_movement display optimization. */);
29181 inhibit_try_cursor_movement = 0;
29182 #endif /* GLYPH_DEBUG */
29184 DEFVAR_INT ("overline-margin", overline_margin,
29185 doc: /* Space between overline and text, in pixels.
29186 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29187 margin to the character height. */);
29188 overline_margin = 2;
29190 DEFVAR_INT ("underline-minimum-offset",
29191 underline_minimum_offset,
29192 doc: /* Minimum distance between baseline and underline.
29193 This can improve legibility of underlined text at small font sizes,
29194 particularly when using variable `x-use-underline-position-properties'
29195 with fonts that specify an UNDERLINE_POSITION relatively close to the
29196 baseline. The default value is 1. */);
29197 underline_minimum_offset = 1;
29199 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29200 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29201 This feature only works when on a window system that can change
29202 cursor shapes. */);
29203 display_hourglass_p = 1;
29205 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29206 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29207 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29209 hourglass_atimer = NULL;
29210 hourglass_shown_p = 0;
29212 DEFSYM (Qglyphless_char, "glyphless-char");
29213 DEFSYM (Qhex_code, "hex-code");
29214 DEFSYM (Qempty_box, "empty-box");
29215 DEFSYM (Qthin_space, "thin-space");
29216 DEFSYM (Qzero_width, "zero-width");
29218 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29219 /* Intern this now in case it isn't already done.
29220 Setting this variable twice is harmless.
29221 But don't staticpro it here--that is done in alloc.c. */
29222 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29223 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29225 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29226 doc: /* Char-table defining glyphless characters.
29227 Each element, if non-nil, should be one of the following:
29228 an ASCII acronym string: display this string in a box
29229 `hex-code': display the hexadecimal code of a character in a box
29230 `empty-box': display as an empty box
29231 `thin-space': display as 1-pixel width space
29232 `zero-width': don't display
29233 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29234 display method for graphical terminals and text terminals respectively.
29235 GRAPHICAL and TEXT should each have one of the values listed above.
29237 The char-table has one extra slot to control the display of a character for
29238 which no font is found. This slot only takes effect on graphical terminals.
29239 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29240 `thin-space'. The default is `empty-box'. */);
29241 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29242 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29243 Qempty_box);
29247 /* Initialize this module when Emacs starts. */
29249 void
29250 init_xdisp (void)
29252 current_header_line_height = current_mode_line_height = -1;
29254 CHARPOS (this_line_start_pos) = 0;
29256 if (!noninteractive)
29258 struct window *m = XWINDOW (minibuf_window);
29259 Lisp_Object frame = m->frame;
29260 struct frame *f = XFRAME (frame);
29261 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29262 struct window *r = XWINDOW (root);
29263 int i;
29265 echo_area_window = minibuf_window;
29267 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
29268 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
29269 XSETFASTINT (r->total_cols, FRAME_COLS (f));
29270 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
29271 XSETFASTINT (m->total_lines, 1);
29272 XSETFASTINT (m->total_cols, FRAME_COLS (f));
29274 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29275 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29276 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29278 /* The default ellipsis glyphs `...'. */
29279 for (i = 0; i < 3; ++i)
29280 default_invis_vector[i] = make_number ('.');
29284 /* Allocate the buffer for frame titles.
29285 Also used for `format-mode-line'. */
29286 int size = 100;
29287 mode_line_noprop_buf = xmalloc (size);
29288 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29289 mode_line_noprop_ptr = mode_line_noprop_buf;
29290 mode_line_target = MODE_LINE_DISPLAY;
29293 help_echo_showing_p = 0;
29296 /* Since w32 does not support atimers, it defines its own implementation of
29297 the following three functions in w32fns.c. */
29298 #ifndef WINDOWSNT
29300 /* Platform-independent portion of hourglass implementation. */
29302 /* Cancel a currently active hourglass timer, and start a new one. */
29303 void
29304 start_hourglass (void)
29306 #if defined (HAVE_WINDOW_SYSTEM)
29307 EMACS_TIME delay;
29309 cancel_hourglass ();
29311 if (INTEGERP (Vhourglass_delay)
29312 && XINT (Vhourglass_delay) > 0)
29313 EMACS_SET_SECS_NSECS (delay,
29314 min (XINT (Vhourglass_delay), TYPE_MAXIMUM (time_t)),
29316 else if (FLOATP (Vhourglass_delay)
29317 && XFLOAT_DATA (Vhourglass_delay) > 0)
29318 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29319 else
29320 EMACS_SET_SECS_NSECS (delay, DEFAULT_HOURGLASS_DELAY, 0);
29322 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29323 show_hourglass, NULL);
29324 #endif
29328 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29329 shown. */
29330 void
29331 cancel_hourglass (void)
29333 #if defined (HAVE_WINDOW_SYSTEM)
29334 if (hourglass_atimer)
29336 cancel_atimer (hourglass_atimer);
29337 hourglass_atimer = NULL;
29340 if (hourglass_shown_p)
29341 hide_hourglass ();
29342 #endif
29344 #endif /* ! WINDOWSNT */