Port to C89.
[emacs.git] / src / xdisp.c
blob12b294e6800d3f3fb664689e32693eb9988f0526
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2013 Free Software Foundation,
4 Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23 Redisplay.
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
54 expose_window (asynchronous) |
56 X expose events -----+
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
85 . try_cursor_movement
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
91 . try_window_reusing_current_matrix
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
97 . try_window_id
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest.
103 . try_window
105 This function performs the full redisplay of a single window
106 assuming that its fonts were not changed and that the cursor
107 will not end up in the scroll margins. (Loading fonts requires
108 re-adjustment of dimensions of glyph matrices, which makes this
109 method impossible to use.)
111 These optimizations are tried in sequence (some can be skipped if
112 it is known that they are not applicable). If none of the
113 optimizations were successful, redisplay calls redisplay_windows,
114 which performs a full redisplay of all windows.
116 Desired matrices.
118 Desired matrices are always built per Emacs window. The function
119 `display_line' is the central function to look at if you are
120 interested. It constructs one row in a desired matrix given an
121 iterator structure containing both a buffer position and a
122 description of the environment in which the text is to be
123 displayed. But this is too early, read on.
125 Characters and pixmaps displayed for a range of buffer text depend
126 on various settings of buffers and windows, on overlays and text
127 properties, on display tables, on selective display. The good news
128 is that all this hairy stuff is hidden behind a small set of
129 interface functions taking an iterator structure (struct it)
130 argument.
132 Iteration over things to be displayed is then simple. It is
133 started by initializing an iterator with a call to init_iterator,
134 passing it the buffer position where to start iteration. For
135 iteration over strings, pass -1 as the position to init_iterator,
136 and call reseat_to_string when the string is ready, to initialize
137 the iterator for that string. Thereafter, calls to
138 get_next_display_element fill the iterator structure with relevant
139 information about the next thing to display. Calls to
140 set_iterator_to_next move the iterator to the next thing.
142 Besides this, an iterator also contains information about the
143 display environment in which glyphs for display elements are to be
144 produced. It has fields for the width and height of the display,
145 the information whether long lines are truncated or continued, a
146 current X and Y position, and lots of other stuff you can better
147 see in dispextern.h.
149 Glyphs in a desired matrix are normally constructed in a loop
150 calling get_next_display_element and then PRODUCE_GLYPHS. The call
151 to PRODUCE_GLYPHS will fill the iterator structure with pixel
152 information about the element being displayed and at the same time
153 produce glyphs for it. If the display element fits on the line
154 being displayed, set_iterator_to_next is called next, otherwise the
155 glyphs produced are discarded. The function display_line is the
156 workhorse of filling glyph rows in the desired matrix with glyphs.
157 In addition to producing glyphs, it also handles line truncation
158 and continuation, word wrap, and cursor positioning (for the
159 latter, see also set_cursor_from_row).
161 Frame matrices.
163 That just couldn't be all, could it? What about terminal types not
164 supporting operations on sub-windows of the screen? To update the
165 display on such a terminal, window-based glyph matrices are not
166 well suited. To be able to reuse part of the display (scrolling
167 lines up and down), we must instead have a view of the whole
168 screen. This is what `frame matrices' are for. They are a trick.
170 Frames on terminals like above have a glyph pool. Windows on such
171 a frame sub-allocate their glyph memory from their frame's glyph
172 pool. The frame itself is given its own glyph matrices. By
173 coincidence---or maybe something else---rows in window glyph
174 matrices are slices of corresponding rows in frame matrices. Thus
175 writing to window matrices implicitly updates a frame matrix which
176 provides us with the view of the whole screen that we originally
177 wanted to have without having to move many bytes around. To be
178 honest, there is a little bit more done, but not much more. If you
179 plan to extend that code, take a look at dispnew.c. The function
180 build_frame_matrix is a good starting point.
182 Bidirectional display.
184 Bidirectional display adds quite some hair to this already complex
185 design. The good news are that a large portion of that hairy stuff
186 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
187 reordering engine which is called by set_iterator_to_next and
188 returns the next character to display in the visual order. See
189 commentary on bidi.c for more details. As far as redisplay is
190 concerned, the effect of calling bidi_move_to_visually_next, the
191 main interface of the reordering engine, is that the iterator gets
192 magically placed on the buffer or string position that is to be
193 displayed next. In other words, a linear iteration through the
194 buffer/string is replaced with a non-linear one. All the rest of
195 the redisplay is oblivious to the bidi reordering.
197 Well, almost oblivious---there are still complications, most of
198 them due to the fact that buffer and string positions no longer
199 change monotonously with glyph indices in a glyph row. Moreover,
200 for continued lines, the buffer positions may not even be
201 monotonously changing with vertical positions. Also, accounting
202 for face changes, overlays, etc. becomes more complex because
203 non-linear iteration could potentially skip many positions with
204 changes, and then cross them again on the way back...
206 One other prominent effect of bidirectional display is that some
207 paragraphs of text need to be displayed starting at the right
208 margin of the window---the so-called right-to-left, or R2L
209 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
210 which have their reversed_p flag set. The bidi reordering engine
211 produces characters in such rows starting from the character which
212 should be the rightmost on display. PRODUCE_GLYPHS then reverses
213 the order, when it fills up the glyph row whose reversed_p flag is
214 set, by prepending each new glyph to what is already there, instead
215 of appending it. When the glyph row is complete, the function
216 extend_face_to_end_of_line fills the empty space to the left of the
217 leftmost character with special glyphs, which will display as,
218 well, empty. On text terminals, these special glyphs are simply
219 blank characters. On graphics terminals, there's a single stretch
220 glyph of a suitably computed width. Both the blanks and the
221 stretch glyph are given the face of the background of the line.
222 This way, the terminal-specific back-end can still draw the glyphs
223 left to right, even for R2L lines.
225 Bidirectional display and character compositions
227 Some scripts cannot be displayed by drawing each character
228 individually, because adjacent characters change each other's shape
229 on display. For example, Arabic and Indic scripts belong to this
230 category.
232 Emacs display supports this by providing "character compositions",
233 most of which is implemented in composite.c. During the buffer
234 scan that delivers characters to PRODUCE_GLYPHS, if the next
235 character to be delivered is a composed character, the iteration
236 calls composition_reseat_it and next_element_from_composition. If
237 they succeed to compose the character with one or more of the
238 following characters, the whole sequence of characters that where
239 composed is recorded in the `struct composition_it' object that is
240 part of the buffer iterator. The composed sequence could produce
241 one or more font glyphs (called "grapheme clusters") on the screen.
242 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
243 in the direction corresponding to the current bidi scan direction
244 (recorded in the scan_dir member of the `struct bidi_it' object
245 that is part of the buffer iterator). In particular, if the bidi
246 iterator currently scans the buffer backwards, the grapheme
247 clusters are delivered back to front. This reorders the grapheme
248 clusters as appropriate for the current bidi context. Note that
249 this means that the grapheme clusters are always stored in the
250 LGSTRING object (see composite.c) in the logical order.
252 Moving an iterator in bidirectional text
253 without producing glyphs
255 Note one important detail mentioned above: that the bidi reordering
256 engine, driven by the iterator, produces characters in R2L rows
257 starting at the character that will be the rightmost on display.
258 As far as the iterator is concerned, the geometry of such rows is
259 still left to right, i.e. the iterator "thinks" the first character
260 is at the leftmost pixel position. The iterator does not know that
261 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
262 delivers. This is important when functions from the move_it_*
263 family are used to get to certain screen position or to match
264 screen coordinates with buffer coordinates: these functions use the
265 iterator geometry, which is left to right even in R2L paragraphs.
266 This works well with most callers of move_it_*, because they need
267 to get to a specific column, and columns are still numbered in the
268 reading order, i.e. the rightmost character in a R2L paragraph is
269 still column zero. But some callers do not get well with this; a
270 notable example is mouse clicks that need to find the character
271 that corresponds to certain pixel coordinates. See
272 buffer_posn_from_coords in dispnew.c for how this is handled. */
274 #include <config.h>
275 #include <stdio.h>
276 #include <limits.h>
278 #include "lisp.h"
279 #include "atimer.h"
280 #include "keyboard.h"
281 #include "frame.h"
282 #include "window.h"
283 #include "termchar.h"
284 #include "dispextern.h"
285 #include "character.h"
286 #include "buffer.h"
287 #include "charset.h"
288 #include "indent.h"
289 #include "commands.h"
290 #include "keymap.h"
291 #include "macros.h"
292 #include "disptab.h"
293 #include "termhooks.h"
294 #include "termopts.h"
295 #include "intervals.h"
296 #include "coding.h"
297 #include "process.h"
298 #include "region-cache.h"
299 #include "font.h"
300 #include "fontset.h"
301 #include "blockinput.h"
303 #ifdef HAVE_X_WINDOWS
304 #include "xterm.h"
305 #endif
306 #ifdef HAVE_NTGUI
307 #include "w32term.h"
308 #endif
309 #ifdef HAVE_NS
310 #include "nsterm.h"
311 #endif
312 #ifdef USE_GTK
313 #include "gtkutil.h"
314 #endif
316 #ifndef FRAME_X_OUTPUT
317 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
318 #endif
320 #define INFINITY 10000000
322 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
323 Lisp_Object Qwindow_scroll_functions;
324 static Lisp_Object Qwindow_text_change_functions;
325 static Lisp_Object Qredisplay_end_trigger_functions;
326 Lisp_Object Qinhibit_point_motion_hooks;
327 static Lisp_Object QCeval, QCpropertize;
328 Lisp_Object QCfile, QCdata;
329 static Lisp_Object Qfontified;
330 static Lisp_Object Qgrow_only;
331 static Lisp_Object Qinhibit_eval_during_redisplay;
332 static Lisp_Object Qbuffer_position, Qposition, Qobject;
333 static Lisp_Object Qright_to_left, Qleft_to_right;
335 /* Cursor shapes. */
336 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
338 /* Pointer shapes. */
339 static Lisp_Object Qarrow, Qhand;
340 Lisp_Object Qtext;
342 /* Holds the list (error). */
343 static Lisp_Object list_of_error;
345 static Lisp_Object Qfontification_functions;
347 static Lisp_Object Qwrap_prefix;
348 static Lisp_Object Qline_prefix;
349 static Lisp_Object Qredisplay_internal;
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)
381 #else /* !HAVE_WINDOW_SYSTEM */
382 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
383 #endif /* HAVE_WINDOW_SYSTEM */
385 /* Test if the display element loaded in IT, or the underlying buffer
386 or string character, is a space or a TAB character. This is used
387 to determine where word wrapping can occur. */
389 #define IT_DISPLAYING_WHITESPACE(it) \
390 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
391 || ((STRINGP (it->string) \
392 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
393 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
394 || (it->s \
395 && (it->s[IT_BYTEPOS (*it)] == ' ' \
396 || it->s[IT_BYTEPOS (*it)] == '\t')) \
397 || (IT_BYTEPOS (*it) < ZV_BYTE \
398 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
399 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
401 /* Name of the face used to highlight trailing whitespace. */
403 static Lisp_Object Qtrailing_whitespace;
405 /* Name and number of the face used to highlight escape glyphs. */
407 static Lisp_Object Qescape_glyph;
409 /* Name and number of the face used to highlight non-breaking spaces. */
411 static Lisp_Object Qnobreak_space;
413 /* The symbol `image' which is the car of the lists used to represent
414 images in Lisp. Also a tool bar style. */
416 Lisp_Object Qimage;
418 /* The image map types. */
419 Lisp_Object QCmap;
420 static Lisp_Object QCpointer;
421 static Lisp_Object Qrect, Qcircle, Qpoly;
423 /* Tool bar styles */
424 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
426 /* Non-zero means print newline to stdout before next mini-buffer
427 message. */
429 int noninteractive_need_newline;
431 /* Non-zero means print newline to message log before next message. */
433 static int message_log_need_newline;
435 /* Three markers that message_dolog uses.
436 It could allocate them itself, but that causes trouble
437 in handling memory-full errors. */
438 static Lisp_Object message_dolog_marker1;
439 static Lisp_Object message_dolog_marker2;
440 static Lisp_Object message_dolog_marker3;
442 /* The buffer position of the first character appearing entirely or
443 partially on the line of the selected window which contains the
444 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
445 redisplay optimization in redisplay_internal. */
447 static struct text_pos this_line_start_pos;
449 /* Number of characters past the end of the line above, including the
450 terminating newline. */
452 static struct text_pos this_line_end_pos;
454 /* The vertical positions and the height of this line. */
456 static int this_line_vpos;
457 static int this_line_y;
458 static int this_line_pixel_height;
460 /* X position at which this display line starts. Usually zero;
461 negative if first character is partially visible. */
463 static int this_line_start_x;
465 /* The smallest character position seen by move_it_* functions as they
466 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
467 hscrolled lines, see display_line. */
469 static struct text_pos this_line_min_pos;
471 /* Buffer that this_line_.* variables are referring to. */
473 static struct buffer *this_line_buffer;
476 /* Values of those variables at last redisplay are stored as
477 properties on `overlay-arrow-position' symbol. However, if
478 Voverlay_arrow_position is a marker, last-arrow-position is its
479 numerical position. */
481 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
483 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
484 properties on a symbol in overlay-arrow-variable-list. */
486 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
488 Lisp_Object Qmenu_bar_update_hook;
490 /* Nonzero if an overlay arrow has been displayed in this window. */
492 static int overlay_arrow_seen;
494 /* Vector containing glyphs for an ellipsis `...'. */
496 static Lisp_Object default_invis_vector[3];
498 /* This is the window where the echo area message was displayed. It
499 is always a mini-buffer window, but it may not be the same window
500 currently active as a mini-buffer. */
502 Lisp_Object echo_area_window;
504 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
505 pushes the current message and the value of
506 message_enable_multibyte on the stack, the function restore_message
507 pops the stack and displays MESSAGE again. */
509 static Lisp_Object Vmessage_stack;
511 /* Nonzero means multibyte characters were enabled when the echo area
512 message was specified. */
514 static int message_enable_multibyte;
516 /* Nonzero if we should redraw the mode lines on the next redisplay. */
518 int update_mode_lines;
520 /* Nonzero if window sizes or contents have changed since last
521 redisplay that finished. */
523 int windows_or_buffers_changed;
525 /* Nonzero means a frame's cursor type has been changed. */
527 int cursor_type_changed;
529 /* Nonzero after display_mode_line if %l was used and it displayed a
530 line number. */
532 static int line_number_displayed;
534 /* The name of the *Messages* buffer, a string. */
536 static Lisp_Object Vmessages_buffer_name;
538 /* Current, index 0, and last displayed echo area message. Either
539 buffers from echo_buffers, or nil to indicate no message. */
541 Lisp_Object echo_area_buffer[2];
543 /* The buffers referenced from echo_area_buffer. */
545 static Lisp_Object echo_buffer[2];
547 /* A vector saved used in with_area_buffer to reduce consing. */
549 static Lisp_Object Vwith_echo_area_save_vector;
551 /* Non-zero means display_echo_area should display the last echo area
552 message again. Set by redisplay_preserve_echo_area. */
554 static int display_last_displayed_message_p;
556 /* Nonzero if echo area is being used by print; zero if being used by
557 message. */
559 static int message_buf_print;
561 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
563 static Lisp_Object Qinhibit_menubar_update;
564 static Lisp_Object Qmessage_truncate_lines;
566 /* Set to 1 in clear_message to make redisplay_internal aware
567 of an emptied echo area. */
569 static int message_cleared_p;
571 /* A scratch glyph row with contents used for generating truncation
572 glyphs. Also used in direct_output_for_insert. */
574 #define MAX_SCRATCH_GLYPHS 100
575 static struct glyph_row scratch_glyph_row;
576 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
578 /* Ascent and height of the last line processed by move_it_to. */
580 static int last_height;
582 /* Non-zero if there's a help-echo in the echo area. */
584 int help_echo_showing_p;
586 /* If >= 0, computed, exact values of mode-line and header-line height
587 to use in the macros CURRENT_MODE_LINE_HEIGHT and
588 CURRENT_HEADER_LINE_HEIGHT. */
590 int current_mode_line_height, current_header_line_height;
592 /* The maximum distance to look ahead for text properties. Values
593 that are too small let us call compute_char_face and similar
594 functions too often which is expensive. Values that are too large
595 let us call compute_char_face and alike too often because we
596 might not be interested in text properties that far away. */
598 #define TEXT_PROP_DISTANCE_LIMIT 100
600 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
601 iterator state and later restore it. This is needed because the
602 bidi iterator on bidi.c keeps a stacked cache of its states, which
603 is really a singleton. When we use scratch iterator objects to
604 move around the buffer, we can cause the bidi cache to be pushed or
605 popped, and therefore we need to restore the cache state when we
606 return to the original iterator. */
607 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
608 do { \
609 if (CACHE) \
610 bidi_unshelve_cache (CACHE, 1); \
611 ITCOPY = ITORIG; \
612 CACHE = bidi_shelve_cache (); \
613 } while (0)
615 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
616 do { \
617 if (pITORIG != pITCOPY) \
618 *(pITORIG) = *(pITCOPY); \
619 bidi_unshelve_cache (CACHE, 0); \
620 CACHE = NULL; \
621 } while (0)
623 #ifdef GLYPH_DEBUG
625 /* Non-zero means print traces of redisplay if compiled with
626 GLYPH_DEBUG defined. */
628 int trace_redisplay_p;
630 #endif /* GLYPH_DEBUG */
632 #ifdef DEBUG_TRACE_MOVE
633 /* Non-zero means trace with TRACE_MOVE to stderr. */
634 int trace_move;
636 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
637 #else
638 #define TRACE_MOVE(x) (void) 0
639 #endif
641 static Lisp_Object Qauto_hscroll_mode;
643 /* Buffer being redisplayed -- for redisplay_window_error. */
645 static struct buffer *displayed_buffer;
647 /* Value returned from text property handlers (see below). */
649 enum prop_handled
651 HANDLED_NORMALLY,
652 HANDLED_RECOMPUTE_PROPS,
653 HANDLED_OVERLAY_STRING_CONSUMED,
654 HANDLED_RETURN
657 /* A description of text properties that redisplay is interested
658 in. */
660 struct props
662 /* The name of the property. */
663 Lisp_Object *name;
665 /* A unique index for the property. */
666 enum prop_idx idx;
668 /* A handler function called to set up iterator IT from the property
669 at IT's current position. Value is used to steer handle_stop. */
670 enum prop_handled (*handler) (struct it *it);
673 static enum prop_handled handle_face_prop (struct it *);
674 static enum prop_handled handle_invisible_prop (struct it *);
675 static enum prop_handled handle_display_prop (struct it *);
676 static enum prop_handled handle_composition_prop (struct it *);
677 static enum prop_handled handle_overlay_change (struct it *);
678 static enum prop_handled handle_fontified_prop (struct it *);
680 /* Properties handled by iterators. */
682 static struct props it_props[] =
684 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
685 /* Handle `face' before `display' because some sub-properties of
686 `display' need to know the face. */
687 {&Qface, FACE_PROP_IDX, handle_face_prop},
688 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
689 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
690 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
691 {NULL, 0, NULL}
694 /* Value is the position described by X. If X is a marker, value is
695 the marker_position of X. Otherwise, value is X. */
697 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
699 /* Enumeration returned by some move_it_.* functions internally. */
701 enum move_it_result
703 /* Not used. Undefined value. */
704 MOVE_UNDEFINED,
706 /* Move ended at the requested buffer position or ZV. */
707 MOVE_POS_MATCH_OR_ZV,
709 /* Move ended at the requested X pixel position. */
710 MOVE_X_REACHED,
712 /* Move within a line ended at the end of a line that must be
713 continued. */
714 MOVE_LINE_CONTINUED,
716 /* Move within a line ended at the end of a line that would
717 be displayed truncated. */
718 MOVE_LINE_TRUNCATED,
720 /* Move within a line ended at a line end. */
721 MOVE_NEWLINE_OR_CR
724 /* This counter is used to clear the face cache every once in a while
725 in redisplay_internal. It is incremented for each redisplay.
726 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
727 cleared. */
729 #define CLEAR_FACE_CACHE_COUNT 500
730 static int clear_face_cache_count;
732 /* Similarly for the image cache. */
734 #ifdef HAVE_WINDOW_SYSTEM
735 #define CLEAR_IMAGE_CACHE_COUNT 101
736 static int clear_image_cache_count;
738 /* Null glyph slice */
739 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
740 #endif
742 /* True while redisplay_internal is in progress. */
744 bool redisplaying_p;
746 static Lisp_Object Qinhibit_free_realized_faces;
747 static Lisp_Object Qmode_line_default_help_echo;
749 /* If a string, XTread_socket generates an event to display that string.
750 (The display is done in read_char.) */
752 Lisp_Object help_echo_string;
753 Lisp_Object help_echo_window;
754 Lisp_Object help_echo_object;
755 ptrdiff_t help_echo_pos;
757 /* Temporary variable for XTread_socket. */
759 Lisp_Object previous_help_echo_string;
761 /* Platform-independent portion of hourglass implementation. */
763 /* Non-zero means an hourglass cursor is currently shown. */
764 int hourglass_shown_p;
766 /* If non-null, an asynchronous timer that, when it expires, displays
767 an hourglass cursor on all frames. */
768 struct atimer *hourglass_atimer;
770 /* Name of the face used to display glyphless characters. */
771 Lisp_Object Qglyphless_char;
773 /* Symbol for the purpose of Vglyphless_char_display. */
774 static Lisp_Object Qglyphless_char_display;
776 /* Method symbols for Vglyphless_char_display. */
777 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
779 /* Default pixel width of `thin-space' display method. */
780 #define THIN_SPACE_WIDTH 1
782 /* Default number of seconds to wait before displaying an hourglass
783 cursor. */
784 #define DEFAULT_HOURGLASS_DELAY 1
787 /* Function prototypes. */
789 static void setup_for_ellipsis (struct it *, int);
790 static void set_iterator_to_next (struct it *, int);
791 static void mark_window_display_accurate_1 (struct window *, int);
792 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
793 static int display_prop_string_p (Lisp_Object, Lisp_Object);
794 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
795 static int cursor_row_p (struct glyph_row *);
796 static int redisplay_mode_lines (Lisp_Object, int);
797 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
799 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
801 static void handle_line_prefix (struct it *);
803 static void pint2str (char *, int, ptrdiff_t);
804 static void pint2hrstr (char *, int, ptrdiff_t);
805 static struct text_pos run_window_scroll_functions (Lisp_Object,
806 struct text_pos);
807 static void reconsider_clip_changes (struct window *, struct buffer *);
808 static int text_outside_line_unchanged_p (struct window *,
809 ptrdiff_t, ptrdiff_t);
810 static void store_mode_line_noprop_char (char);
811 static int store_mode_line_noprop (const char *, int, int);
812 static void handle_stop (struct it *);
813 static void handle_stop_backwards (struct it *, ptrdiff_t);
814 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
815 static void ensure_echo_area_buffers (void);
816 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
817 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
818 static int with_echo_area_buffer (struct window *, int,
819 int (*) (ptrdiff_t, Lisp_Object),
820 ptrdiff_t, Lisp_Object);
821 static void clear_garbaged_frames (void);
822 static int current_message_1 (ptrdiff_t, Lisp_Object);
823 static void pop_message (void);
824 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
825 static void set_message (Lisp_Object);
826 static int set_message_1 (ptrdiff_t, Lisp_Object);
827 static int display_echo_area (struct window *);
828 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
829 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
830 static Lisp_Object unwind_redisplay (Lisp_Object);
831 static int string_char_and_length (const unsigned char *, int *);
832 static struct text_pos display_prop_end (struct it *, Lisp_Object,
833 struct text_pos);
834 static int compute_window_start_on_continuation_line (struct window *);
835 static void insert_left_trunc_glyphs (struct it *);
836 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
837 Lisp_Object);
838 static void extend_face_to_end_of_line (struct it *);
839 static int append_space_for_newline (struct it *, int);
840 static int cursor_row_fully_visible_p (struct window *, int, int);
841 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
842 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
843 static int trailing_whitespace_p (ptrdiff_t);
844 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
845 static void push_it (struct it *, struct text_pos *);
846 static void iterate_out_of_display_property (struct it *);
847 static void pop_it (struct it *);
848 static void sync_frame_with_window_matrix_rows (struct window *);
849 static void redisplay_internal (void);
850 static int echo_area_display (int);
851 static void redisplay_windows (Lisp_Object);
852 static void redisplay_window (Lisp_Object, int);
853 static Lisp_Object redisplay_window_error (Lisp_Object);
854 static Lisp_Object redisplay_window_0 (Lisp_Object);
855 static Lisp_Object redisplay_window_1 (Lisp_Object);
856 static int set_cursor_from_row (struct window *, struct glyph_row *,
857 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
858 int, int);
859 static int update_menu_bar (struct frame *, int, int);
860 static int try_window_reusing_current_matrix (struct window *);
861 static int try_window_id (struct window *);
862 static int display_line (struct it *);
863 static int display_mode_lines (struct window *);
864 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
865 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
866 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
867 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
868 static void display_menu_bar (struct window *);
869 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
870 ptrdiff_t *);
871 static int display_string (const char *, Lisp_Object, Lisp_Object,
872 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
873 static void compute_line_metrics (struct it *);
874 static void run_redisplay_end_trigger_hook (struct it *);
875 static int get_overlay_strings (struct it *, ptrdiff_t);
876 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
877 static void next_overlay_string (struct it *);
878 static void reseat (struct it *, struct text_pos, int);
879 static void reseat_1 (struct it *, struct text_pos, int);
880 static void back_to_previous_visible_line_start (struct it *);
881 static void reseat_at_next_visible_line_start (struct it *, int);
882 static int next_element_from_ellipsis (struct it *);
883 static int next_element_from_display_vector (struct it *);
884 static int next_element_from_string (struct it *);
885 static int next_element_from_c_string (struct it *);
886 static int next_element_from_buffer (struct it *);
887 static int next_element_from_composition (struct it *);
888 static int next_element_from_image (struct it *);
889 static int next_element_from_stretch (struct it *);
890 static void load_overlay_strings (struct it *, ptrdiff_t);
891 static int init_from_display_pos (struct it *, struct window *,
892 struct display_pos *);
893 static void reseat_to_string (struct it *, const char *,
894 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
895 static int get_next_display_element (struct it *);
896 static enum move_it_result
897 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
898 enum move_operation_enum);
899 static void get_visually_first_element (struct it *);
900 static void init_to_row_start (struct it *, struct window *,
901 struct glyph_row *);
902 static int init_to_row_end (struct it *, struct window *,
903 struct glyph_row *);
904 static void back_to_previous_line_start (struct it *);
905 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
906 static struct text_pos string_pos_nchars_ahead (struct text_pos,
907 Lisp_Object, ptrdiff_t);
908 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
909 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
910 static ptrdiff_t number_of_chars (const char *, bool);
911 static void compute_stop_pos (struct it *);
912 static void compute_string_pos (struct text_pos *, struct text_pos,
913 Lisp_Object);
914 static int face_before_or_after_it_pos (struct it *, int);
915 static ptrdiff_t next_overlay_change (ptrdiff_t);
916 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
917 Lisp_Object, struct text_pos *, ptrdiff_t, int);
918 static int handle_single_display_spec (struct it *, Lisp_Object,
919 Lisp_Object, Lisp_Object,
920 struct text_pos *, ptrdiff_t, int, int);
921 static int underlying_face_id (struct it *);
922 static int in_ellipses_for_invisible_text_p (struct display_pos *,
923 struct window *);
925 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
926 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
928 #ifdef HAVE_WINDOW_SYSTEM
930 static void x_consider_frame_title (Lisp_Object);
931 static int tool_bar_lines_needed (struct frame *, int *);
932 static void update_tool_bar (struct frame *, int);
933 static void build_desired_tool_bar_string (struct frame *f);
934 static int redisplay_tool_bar (struct frame *);
935 static void display_tool_bar_line (struct it *, int);
936 static void notice_overwritten_cursor (struct window *,
937 enum glyph_row_area,
938 int, int, int, int);
939 static void append_stretch_glyph (struct it *, Lisp_Object,
940 int, int, int);
943 #endif /* HAVE_WINDOW_SYSTEM */
945 static void produce_special_glyphs (struct it *, enum display_element_type);
946 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
947 static int coords_in_mouse_face_p (struct window *, int, int);
951 /***********************************************************************
952 Window display dimensions
953 ***********************************************************************/
955 /* Return the bottom boundary y-position for text lines in window W.
956 This is the first y position at which a line cannot start.
957 It is relative to the top of the window.
959 This is the height of W minus the height of a mode line, if any. */
962 window_text_bottom_y (struct window *w)
964 int height = WINDOW_TOTAL_HEIGHT (w);
966 if (WINDOW_WANTS_MODELINE_P (w))
967 height -= CURRENT_MODE_LINE_HEIGHT (w);
968 return height;
971 /* Return the pixel width of display area AREA of window W. AREA < 0
972 means return the total width of W, not including fringes to
973 the left and right of the window. */
976 window_box_width (struct window *w, int area)
978 int cols = w->total_cols;
979 int pixels = 0;
981 if (!w->pseudo_window_p)
983 cols -= WINDOW_SCROLL_BAR_COLS (w);
985 if (area == TEXT_AREA)
987 if (INTEGERP (w->left_margin_cols))
988 cols -= XFASTINT (w->left_margin_cols);
989 if (INTEGERP (w->right_margin_cols))
990 cols -= XFASTINT (w->right_margin_cols);
991 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
993 else if (area == LEFT_MARGIN_AREA)
995 cols = (INTEGERP (w->left_margin_cols)
996 ? XFASTINT (w->left_margin_cols) : 0);
997 pixels = 0;
999 else if (area == RIGHT_MARGIN_AREA)
1001 cols = (INTEGERP (w->right_margin_cols)
1002 ? XFASTINT (w->right_margin_cols) : 0);
1003 pixels = 0;
1007 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1011 /* Return the pixel height of the display area of window W, not
1012 including mode lines of W, if any. */
1015 window_box_height (struct window *w)
1017 struct frame *f = XFRAME (w->frame);
1018 int height = WINDOW_TOTAL_HEIGHT (w);
1020 eassert (height >= 0);
1022 /* Note: the code below that determines the mode-line/header-line
1023 height is essentially the same as that contained in the macro
1024 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1025 the appropriate glyph row has its `mode_line_p' flag set,
1026 and if it doesn't, uses estimate_mode_line_height instead. */
1028 if (WINDOW_WANTS_MODELINE_P (w))
1030 struct glyph_row *ml_row
1031 = (w->current_matrix && w->current_matrix->rows
1032 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1033 : 0);
1034 if (ml_row && ml_row->mode_line_p)
1035 height -= ml_row->height;
1036 else
1037 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1040 if (WINDOW_WANTS_HEADER_LINE_P (w))
1042 struct glyph_row *hl_row
1043 = (w->current_matrix && w->current_matrix->rows
1044 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1045 : 0);
1046 if (hl_row && hl_row->mode_line_p)
1047 height -= hl_row->height;
1048 else
1049 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1052 /* With a very small font and a mode-line that's taller than
1053 default, we might end up with a negative height. */
1054 return max (0, height);
1057 /* Return the window-relative coordinate of the left edge of display
1058 area AREA of window W. AREA < 0 means return the left edge of the
1059 whole window, to the right of the left fringe of W. */
1062 window_box_left_offset (struct window *w, int area)
1064 int x;
1066 if (w->pseudo_window_p)
1067 return 0;
1069 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1071 if (area == TEXT_AREA)
1072 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1073 + window_box_width (w, LEFT_MARGIN_AREA));
1074 else if (area == RIGHT_MARGIN_AREA)
1075 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1076 + window_box_width (w, LEFT_MARGIN_AREA)
1077 + window_box_width (w, TEXT_AREA)
1078 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1080 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1081 else if (area == LEFT_MARGIN_AREA
1082 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1083 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1085 return x;
1089 /* Return the window-relative coordinate of the right edge of display
1090 area AREA of window W. AREA < 0 means return the right edge of the
1091 whole window, to the left of the right fringe of W. */
1094 window_box_right_offset (struct window *w, int area)
1096 return window_box_left_offset (w, area) + window_box_width (w, area);
1099 /* Return the frame-relative coordinate of the left edge of display
1100 area AREA of window W. AREA < 0 means return the left edge of the
1101 whole window, to the right of the left fringe of W. */
1104 window_box_left (struct window *w, int area)
1106 struct frame *f = XFRAME (w->frame);
1107 int x;
1109 if (w->pseudo_window_p)
1110 return FRAME_INTERNAL_BORDER_WIDTH (f);
1112 x = (WINDOW_LEFT_EDGE_X (w)
1113 + window_box_left_offset (w, area));
1115 return x;
1119 /* Return the frame-relative coordinate of the right edge of display
1120 area AREA of window W. AREA < 0 means return the right edge of the
1121 whole window, to the left of the right fringe of W. */
1124 window_box_right (struct window *w, int area)
1126 return window_box_left (w, area) + window_box_width (w, area);
1129 /* Get the bounding box of the display area AREA of window W, without
1130 mode lines, in frame-relative coordinates. AREA < 0 means the
1131 whole window, not including the left and right fringes of
1132 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1133 coordinates of the upper-left corner of the box. Return in
1134 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1136 void
1137 window_box (struct window *w, int area, int *box_x, int *box_y,
1138 int *box_width, int *box_height)
1140 if (box_width)
1141 *box_width = window_box_width (w, area);
1142 if (box_height)
1143 *box_height = window_box_height (w);
1144 if (box_x)
1145 *box_x = window_box_left (w, area);
1146 if (box_y)
1148 *box_y = WINDOW_TOP_EDGE_Y (w);
1149 if (WINDOW_WANTS_HEADER_LINE_P (w))
1150 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1155 /* Get the bounding box of the display area AREA of window W, without
1156 mode lines. AREA < 0 means the whole window, not including the
1157 left and right fringe of the window. Return in *TOP_LEFT_X
1158 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1159 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1160 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1161 box. */
1163 static void
1164 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1165 int *bottom_right_x, int *bottom_right_y)
1167 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1168 bottom_right_y);
1169 *bottom_right_x += *top_left_x;
1170 *bottom_right_y += *top_left_y;
1175 /***********************************************************************
1176 Utilities
1177 ***********************************************************************/
1179 /* Return the bottom y-position of the line the iterator IT is in.
1180 This can modify IT's settings. */
1183 line_bottom_y (struct it *it)
1185 int line_height = it->max_ascent + it->max_descent;
1186 int line_top_y = it->current_y;
1188 if (line_height == 0)
1190 if (last_height)
1191 line_height = last_height;
1192 else if (IT_CHARPOS (*it) < ZV)
1194 move_it_by_lines (it, 1);
1195 line_height = (it->max_ascent || it->max_descent
1196 ? it->max_ascent + it->max_descent
1197 : last_height);
1199 else
1201 struct glyph_row *row = it->glyph_row;
1203 /* Use the default character height. */
1204 it->glyph_row = NULL;
1205 it->what = IT_CHARACTER;
1206 it->c = ' ';
1207 it->len = 1;
1208 PRODUCE_GLYPHS (it);
1209 line_height = it->ascent + it->descent;
1210 it->glyph_row = row;
1214 return line_top_y + line_height;
1217 DEFUN ("line-pixel-height", Fline_pixel_height,
1218 Sline_pixel_height, 0, 0, 0,
1219 doc: /* Return height in pixels of text line in the selected window.
1221 Value is the height in pixels of the line at point. */)
1222 (void)
1224 struct it it;
1225 struct text_pos pt;
1226 struct window *w = XWINDOW (selected_window);
1228 SET_TEXT_POS (pt, PT, PT_BYTE);
1229 start_display (&it, w, pt);
1230 it.vpos = it.current_y = 0;
1231 last_height = 0;
1232 return make_number (line_bottom_y (&it));
1235 /* Return the default pixel height of text lines in window W. The
1236 value is the canonical height of the W frame's default font, plus
1237 any extra space required by the line-spacing variable or frame
1238 parameter.
1240 Implementation note: this ignores any line-spacing text properties
1241 put on the newline characters. This is because those properties
1242 only affect the _screen_ line ending in the newline (i.e., in a
1243 continued line, only the last screen line will be affected), which
1244 means only a small number of lines in a buffer can ever use this
1245 feature. Since this function is used to compute the default pixel
1246 equivalent of text lines in a window, we can safely ignore those
1247 few lines. For the same reasons, we ignore the line-height
1248 properties. */
1250 default_line_pixel_height (struct window *w)
1252 struct frame *f = WINDOW_XFRAME (w);
1253 int height = FRAME_LINE_HEIGHT (f);
1255 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1257 struct buffer *b = XBUFFER (w->contents);
1258 Lisp_Object val = BVAR (b, extra_line_spacing);
1260 if (NILP (val))
1261 val = BVAR (&buffer_defaults, extra_line_spacing);
1262 if (!NILP (val))
1264 if (RANGED_INTEGERP (0, val, INT_MAX))
1265 height += XFASTINT (val);
1266 else if (FLOATP (val))
1268 int addon = XFLOAT_DATA (val) * height + 0.5;
1270 if (addon >= 0)
1271 height += addon;
1274 else
1275 height += f->extra_line_spacing;
1278 return height;
1281 /* Subroutine of pos_visible_p below. Extracts a display string, if
1282 any, from the display spec given as its argument. */
1283 static Lisp_Object
1284 string_from_display_spec (Lisp_Object spec)
1286 if (CONSP (spec))
1288 while (CONSP (spec))
1290 if (STRINGP (XCAR (spec)))
1291 return XCAR (spec);
1292 spec = XCDR (spec);
1295 else if (VECTORP (spec))
1297 ptrdiff_t i;
1299 for (i = 0; i < ASIZE (spec); i++)
1301 if (STRINGP (AREF (spec, i)))
1302 return AREF (spec, i);
1304 return Qnil;
1307 return spec;
1311 /* Limit insanely large values of W->hscroll on frame F to the largest
1312 value that will still prevent first_visible_x and last_visible_x of
1313 'struct it' from overflowing an int. */
1314 static int
1315 window_hscroll_limited (struct window *w, struct frame *f)
1317 ptrdiff_t window_hscroll = w->hscroll;
1318 int window_text_width = window_box_width (w, TEXT_AREA);
1319 int colwidth = FRAME_COLUMN_WIDTH (f);
1321 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1322 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1324 return window_hscroll;
1327 /* Return 1 if position CHARPOS is visible in window W.
1328 CHARPOS < 0 means return info about WINDOW_END position.
1329 If visible, set *X and *Y to pixel coordinates of top left corner.
1330 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1331 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1334 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1335 int *rtop, int *rbot, int *rowh, int *vpos)
1337 struct it it;
1338 void *itdata = bidi_shelve_cache ();
1339 struct text_pos top;
1340 int visible_p = 0;
1341 struct buffer *old_buffer = NULL;
1343 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1344 return visible_p;
1346 if (XBUFFER (w->contents) != current_buffer)
1348 old_buffer = current_buffer;
1349 set_buffer_internal_1 (XBUFFER (w->contents));
1352 SET_TEXT_POS_FROM_MARKER (top, w->start);
1353 /* Scrolling a minibuffer window via scroll bar when the echo area
1354 shows long text sometimes resets the minibuffer contents behind
1355 our backs. */
1356 if (CHARPOS (top) > ZV)
1357 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1359 /* Compute exact mode line heights. */
1360 if (WINDOW_WANTS_MODELINE_P (w))
1361 current_mode_line_height
1362 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1363 BVAR (current_buffer, mode_line_format));
1365 if (WINDOW_WANTS_HEADER_LINE_P (w))
1366 current_header_line_height
1367 = display_mode_line (w, HEADER_LINE_FACE_ID,
1368 BVAR (current_buffer, header_line_format));
1370 start_display (&it, w, top);
1371 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1372 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1374 if (charpos >= 0
1375 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1376 && IT_CHARPOS (it) >= charpos)
1377 /* When scanning backwards under bidi iteration, move_it_to
1378 stops at or _before_ CHARPOS, because it stops at or to
1379 the _right_ of the character at CHARPOS. */
1380 || (it.bidi_p && it.bidi_it.scan_dir == -1
1381 && IT_CHARPOS (it) <= charpos)))
1383 /* We have reached CHARPOS, or passed it. How the call to
1384 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1385 or covered by a display property, move_it_to stops at the end
1386 of the invisible text, to the right of CHARPOS. (ii) If
1387 CHARPOS is in a display vector, move_it_to stops on its last
1388 glyph. */
1389 int top_x = it.current_x;
1390 int top_y = it.current_y;
1391 /* Calling line_bottom_y may change it.method, it.position, etc. */
1392 enum it_method it_method = it.method;
1393 int bottom_y = (last_height = 0, line_bottom_y (&it));
1394 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1396 if (top_y < window_top_y)
1397 visible_p = bottom_y > window_top_y;
1398 else if (top_y < it.last_visible_y)
1399 visible_p = 1;
1400 if (bottom_y >= it.last_visible_y
1401 && it.bidi_p && it.bidi_it.scan_dir == -1
1402 && IT_CHARPOS (it) < charpos)
1404 /* When the last line of the window is scanned backwards
1405 under bidi iteration, we could be duped into thinking
1406 that we have passed CHARPOS, when in fact move_it_to
1407 simply stopped short of CHARPOS because it reached
1408 last_visible_y. To see if that's what happened, we call
1409 move_it_to again with a slightly larger vertical limit,
1410 and see if it actually moved vertically; if it did, we
1411 didn't really reach CHARPOS, which is beyond window end. */
1412 struct it save_it = it;
1413 /* Why 10? because we don't know how many canonical lines
1414 will the height of the next line(s) be. So we guess. */
1415 int ten_more_lines = 10 * default_line_pixel_height (w);
1417 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1418 MOVE_TO_POS | MOVE_TO_Y);
1419 if (it.current_y > top_y)
1420 visible_p = 0;
1422 it = save_it;
1424 if (visible_p)
1426 if (it_method == GET_FROM_DISPLAY_VECTOR)
1428 /* We stopped on the last glyph of a display vector.
1429 Try and recompute. Hack alert! */
1430 if (charpos < 2 || top.charpos >= charpos)
1431 top_x = it.glyph_row->x;
1432 else
1434 struct it it2, it2_prev;
1435 /* The idea is to get to the previous buffer
1436 position, consume the character there, and use
1437 the pixel coordinates we get after that. But if
1438 the previous buffer position is also displayed
1439 from a display vector, we need to consume all of
1440 the glyphs from that display vector. */
1441 start_display (&it2, w, top);
1442 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1443 /* If we didn't get to CHARPOS - 1, there's some
1444 replacing display property at that position, and
1445 we stopped after it. That is exactly the place
1446 whose coordinates we want. */
1447 if (IT_CHARPOS (it2) != charpos - 1)
1448 it2_prev = it2;
1449 else
1451 /* Iterate until we get out of the display
1452 vector that displays the character at
1453 CHARPOS - 1. */
1454 do {
1455 get_next_display_element (&it2);
1456 PRODUCE_GLYPHS (&it2);
1457 it2_prev = it2;
1458 set_iterator_to_next (&it2, 1);
1459 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1460 && IT_CHARPOS (it2) < charpos);
1462 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1463 || it2_prev.current_x > it2_prev.last_visible_x)
1464 top_x = it.glyph_row->x;
1465 else
1467 top_x = it2_prev.current_x;
1468 top_y = it2_prev.current_y;
1472 else if (IT_CHARPOS (it) != charpos)
1474 Lisp_Object cpos = make_number (charpos);
1475 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1476 Lisp_Object string = string_from_display_spec (spec);
1477 struct text_pos tpos;
1478 int replacing_spec_p;
1479 bool newline_in_string
1480 = (STRINGP (string)
1481 && memchr (SDATA (string), '\n', SBYTES (string)));
1483 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1484 replacing_spec_p
1485 = (!NILP (spec)
1486 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1487 charpos, FRAME_WINDOW_P (it.f)));
1488 /* The tricky code below is needed because there's a
1489 discrepancy between move_it_to and how we set cursor
1490 when PT is at the beginning of a portion of text
1491 covered by a display property or an overlay with a
1492 display property, or the display line ends in a
1493 newline from a display string. move_it_to will stop
1494 _after_ such display strings, whereas
1495 set_cursor_from_row conspires with cursor_row_p to
1496 place the cursor on the first glyph produced from the
1497 display string. */
1499 /* We have overshoot PT because it is covered by a
1500 display property that replaces the text it covers.
1501 If the string includes embedded newlines, we are also
1502 in the wrong display line. Backtrack to the correct
1503 line, where the display property begins. */
1504 if (replacing_spec_p)
1506 Lisp_Object startpos, endpos;
1507 EMACS_INT start, end;
1508 struct it it3;
1509 int it3_moved;
1511 /* Find the first and the last buffer positions
1512 covered by the display string. */
1513 endpos =
1514 Fnext_single_char_property_change (cpos, Qdisplay,
1515 Qnil, Qnil);
1516 startpos =
1517 Fprevious_single_char_property_change (endpos, Qdisplay,
1518 Qnil, Qnil);
1519 start = XFASTINT (startpos);
1520 end = XFASTINT (endpos);
1521 /* Move to the last buffer position before the
1522 display property. */
1523 start_display (&it3, w, top);
1524 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1525 /* Move forward one more line if the position before
1526 the display string is a newline or if it is the
1527 rightmost character on a line that is
1528 continued or word-wrapped. */
1529 if (it3.method == GET_FROM_BUFFER
1530 && (it3.c == '\n'
1531 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1532 move_it_by_lines (&it3, 1);
1533 else if (move_it_in_display_line_to (&it3, -1,
1534 it3.current_x
1535 + it3.pixel_width,
1536 MOVE_TO_X)
1537 == MOVE_LINE_CONTINUED)
1539 move_it_by_lines (&it3, 1);
1540 /* When we are under word-wrap, the #$@%!
1541 move_it_by_lines moves 2 lines, so we need to
1542 fix that up. */
1543 if (it3.line_wrap == WORD_WRAP)
1544 move_it_by_lines (&it3, -1);
1547 /* Record the vertical coordinate of the display
1548 line where we wound up. */
1549 top_y = it3.current_y;
1550 if (it3.bidi_p)
1552 /* When characters are reordered for display,
1553 the character displayed to the left of the
1554 display string could be _after_ the display
1555 property in the logical order. Use the
1556 smallest vertical position of these two. */
1557 start_display (&it3, w, top);
1558 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1559 if (it3.current_y < top_y)
1560 top_y = it3.current_y;
1562 /* Move from the top of the window to the beginning
1563 of the display line where the display string
1564 begins. */
1565 start_display (&it3, w, top);
1566 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1567 /* If it3_moved stays zero after the 'while' loop
1568 below, that means we already were at a newline
1569 before the loop (e.g., the display string begins
1570 with a newline), so we don't need to (and cannot)
1571 inspect the glyphs of it3.glyph_row, because
1572 PRODUCE_GLYPHS will not produce anything for a
1573 newline, and thus it3.glyph_row stays at its
1574 stale content it got at top of the window. */
1575 it3_moved = 0;
1576 /* Finally, advance the iterator until we hit the
1577 first display element whose character position is
1578 CHARPOS, or until the first newline from the
1579 display string, which signals the end of the
1580 display line. */
1581 while (get_next_display_element (&it3))
1583 PRODUCE_GLYPHS (&it3);
1584 if (IT_CHARPOS (it3) == charpos
1585 || ITERATOR_AT_END_OF_LINE_P (&it3))
1586 break;
1587 it3_moved = 1;
1588 set_iterator_to_next (&it3, 0);
1590 top_x = it3.current_x - it3.pixel_width;
1591 /* Normally, we would exit the above loop because we
1592 found the display element whose character
1593 position is CHARPOS. For the contingency that we
1594 didn't, and stopped at the first newline from the
1595 display string, move back over the glyphs
1596 produced from the string, until we find the
1597 rightmost glyph not from the string. */
1598 if (it3_moved
1599 && newline_in_string
1600 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1602 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1603 + it3.glyph_row->used[TEXT_AREA];
1605 while (EQ ((g - 1)->object, string))
1607 --g;
1608 top_x -= g->pixel_width;
1610 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1611 + it3.glyph_row->used[TEXT_AREA]);
1616 *x = top_x;
1617 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1618 *rtop = max (0, window_top_y - top_y);
1619 *rbot = max (0, bottom_y - it.last_visible_y);
1620 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1621 - max (top_y, window_top_y)));
1622 *vpos = it.vpos;
1625 else
1627 /* We were asked to provide info about WINDOW_END. */
1628 struct it it2;
1629 void *it2data = NULL;
1631 SAVE_IT (it2, it, it2data);
1632 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1633 move_it_by_lines (&it, 1);
1634 if (charpos < IT_CHARPOS (it)
1635 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1637 visible_p = 1;
1638 RESTORE_IT (&it2, &it2, it2data);
1639 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1640 *x = it2.current_x;
1641 *y = it2.current_y + it2.max_ascent - it2.ascent;
1642 *rtop = max (0, -it2.current_y);
1643 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1644 - it.last_visible_y));
1645 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1646 it.last_visible_y)
1647 - max (it2.current_y,
1648 WINDOW_HEADER_LINE_HEIGHT (w))));
1649 *vpos = it2.vpos;
1651 else
1652 bidi_unshelve_cache (it2data, 1);
1654 bidi_unshelve_cache (itdata, 0);
1656 if (old_buffer)
1657 set_buffer_internal_1 (old_buffer);
1659 current_header_line_height = current_mode_line_height = -1;
1661 if (visible_p && w->hscroll > 0)
1662 *x -=
1663 window_hscroll_limited (w, WINDOW_XFRAME (w))
1664 * WINDOW_FRAME_COLUMN_WIDTH (w);
1666 #if 0
1667 /* Debugging code. */
1668 if (visible_p)
1669 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1670 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1671 else
1672 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1673 #endif
1675 return visible_p;
1679 /* Return the next character from STR. Return in *LEN the length of
1680 the character. This is like STRING_CHAR_AND_LENGTH but never
1681 returns an invalid character. If we find one, we return a `?', but
1682 with the length of the invalid character. */
1684 static int
1685 string_char_and_length (const unsigned char *str, int *len)
1687 int c;
1689 c = STRING_CHAR_AND_LENGTH (str, *len);
1690 if (!CHAR_VALID_P (c))
1691 /* We may not change the length here because other places in Emacs
1692 don't use this function, i.e. they silently accept invalid
1693 characters. */
1694 c = '?';
1696 return c;
1701 /* Given a position POS containing a valid character and byte position
1702 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1704 static struct text_pos
1705 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1707 eassert (STRINGP (string) && nchars >= 0);
1709 if (STRING_MULTIBYTE (string))
1711 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1712 int len;
1714 while (nchars--)
1716 string_char_and_length (p, &len);
1717 p += len;
1718 CHARPOS (pos) += 1;
1719 BYTEPOS (pos) += len;
1722 else
1723 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1725 return pos;
1729 /* Value is the text position, i.e. character and byte position,
1730 for character position CHARPOS in STRING. */
1732 static struct text_pos
1733 string_pos (ptrdiff_t charpos, Lisp_Object string)
1735 struct text_pos pos;
1736 eassert (STRINGP (string));
1737 eassert (charpos >= 0);
1738 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1739 return pos;
1743 /* Value is a text position, i.e. character and byte position, for
1744 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1745 means recognize multibyte characters. */
1747 static struct text_pos
1748 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1750 struct text_pos pos;
1752 eassert (s != NULL);
1753 eassert (charpos >= 0);
1755 if (multibyte_p)
1757 int len;
1759 SET_TEXT_POS (pos, 0, 0);
1760 while (charpos--)
1762 string_char_and_length ((const unsigned char *) s, &len);
1763 s += len;
1764 CHARPOS (pos) += 1;
1765 BYTEPOS (pos) += len;
1768 else
1769 SET_TEXT_POS (pos, charpos, charpos);
1771 return pos;
1775 /* Value is the number of characters in C string S. MULTIBYTE_P
1776 non-zero means recognize multibyte characters. */
1778 static ptrdiff_t
1779 number_of_chars (const char *s, bool multibyte_p)
1781 ptrdiff_t nchars;
1783 if (multibyte_p)
1785 ptrdiff_t rest = strlen (s);
1786 int len;
1787 const unsigned char *p = (const unsigned char *) s;
1789 for (nchars = 0; rest > 0; ++nchars)
1791 string_char_and_length (p, &len);
1792 rest -= len, p += len;
1795 else
1796 nchars = strlen (s);
1798 return nchars;
1802 /* Compute byte position NEWPOS->bytepos corresponding to
1803 NEWPOS->charpos. POS is a known position in string STRING.
1804 NEWPOS->charpos must be >= POS.charpos. */
1806 static void
1807 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1809 eassert (STRINGP (string));
1810 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1812 if (STRING_MULTIBYTE (string))
1813 *newpos = string_pos_nchars_ahead (pos, string,
1814 CHARPOS (*newpos) - CHARPOS (pos));
1815 else
1816 BYTEPOS (*newpos) = CHARPOS (*newpos);
1819 /* EXPORT:
1820 Return an estimation of the pixel height of mode or header lines on
1821 frame F. FACE_ID specifies what line's height to estimate. */
1824 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1826 #ifdef HAVE_WINDOW_SYSTEM
1827 if (FRAME_WINDOW_P (f))
1829 int height = FONT_HEIGHT (FRAME_FONT (f));
1831 /* This function is called so early when Emacs starts that the face
1832 cache and mode line face are not yet initialized. */
1833 if (FRAME_FACE_CACHE (f))
1835 struct face *face = FACE_FROM_ID (f, face_id);
1836 if (face)
1838 if (face->font)
1839 height = FONT_HEIGHT (face->font);
1840 if (face->box_line_width > 0)
1841 height += 2 * face->box_line_width;
1845 return height;
1847 #endif
1849 return 1;
1852 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1853 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1854 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1855 not force the value into range. */
1857 void
1858 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1859 int *x, int *y, NativeRectangle *bounds, int noclip)
1862 #ifdef HAVE_WINDOW_SYSTEM
1863 if (FRAME_WINDOW_P (f))
1865 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1866 even for negative values. */
1867 if (pix_x < 0)
1868 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1869 if (pix_y < 0)
1870 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1872 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1873 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1875 if (bounds)
1876 STORE_NATIVE_RECT (*bounds,
1877 FRAME_COL_TO_PIXEL_X (f, pix_x),
1878 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1879 FRAME_COLUMN_WIDTH (f) - 1,
1880 FRAME_LINE_HEIGHT (f) - 1);
1882 if (!noclip)
1884 if (pix_x < 0)
1885 pix_x = 0;
1886 else if (pix_x > FRAME_TOTAL_COLS (f))
1887 pix_x = FRAME_TOTAL_COLS (f);
1889 if (pix_y < 0)
1890 pix_y = 0;
1891 else if (pix_y > FRAME_LINES (f))
1892 pix_y = FRAME_LINES (f);
1895 #endif
1897 *x = pix_x;
1898 *y = pix_y;
1902 /* Find the glyph under window-relative coordinates X/Y in window W.
1903 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1904 strings. Return in *HPOS and *VPOS the row and column number of
1905 the glyph found. Return in *AREA the glyph area containing X.
1906 Value is a pointer to the glyph found or null if X/Y is not on
1907 text, or we can't tell because W's current matrix is not up to
1908 date. */
1910 static
1911 struct glyph *
1912 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1913 int *dx, int *dy, int *area)
1915 struct glyph *glyph, *end;
1916 struct glyph_row *row = NULL;
1917 int x0, i;
1919 /* Find row containing Y. Give up if some row is not enabled. */
1920 for (i = 0; i < w->current_matrix->nrows; ++i)
1922 row = MATRIX_ROW (w->current_matrix, i);
1923 if (!row->enabled_p)
1924 return NULL;
1925 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1926 break;
1929 *vpos = i;
1930 *hpos = 0;
1932 /* Give up if Y is not in the window. */
1933 if (i == w->current_matrix->nrows)
1934 return NULL;
1936 /* Get the glyph area containing X. */
1937 if (w->pseudo_window_p)
1939 *area = TEXT_AREA;
1940 x0 = 0;
1942 else
1944 if (x < window_box_left_offset (w, TEXT_AREA))
1946 *area = LEFT_MARGIN_AREA;
1947 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1949 else if (x < window_box_right_offset (w, TEXT_AREA))
1951 *area = TEXT_AREA;
1952 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1954 else
1956 *area = RIGHT_MARGIN_AREA;
1957 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1961 /* Find glyph containing X. */
1962 glyph = row->glyphs[*area];
1963 end = glyph + row->used[*area];
1964 x -= x0;
1965 while (glyph < end && x >= glyph->pixel_width)
1967 x -= glyph->pixel_width;
1968 ++glyph;
1971 if (glyph == end)
1972 return NULL;
1974 if (dx)
1976 *dx = x;
1977 *dy = y - (row->y + row->ascent - glyph->ascent);
1980 *hpos = glyph - row->glyphs[*area];
1981 return glyph;
1984 /* Convert frame-relative x/y to coordinates relative to window W.
1985 Takes pseudo-windows into account. */
1987 static void
1988 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1990 if (w->pseudo_window_p)
1992 /* A pseudo-window is always full-width, and starts at the
1993 left edge of the frame, plus a frame border. */
1994 struct frame *f = XFRAME (w->frame);
1995 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1996 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1998 else
2000 *x -= WINDOW_LEFT_EDGE_X (w);
2001 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2005 #ifdef HAVE_WINDOW_SYSTEM
2007 /* EXPORT:
2008 Return in RECTS[] at most N clipping rectangles for glyph string S.
2009 Return the number of stored rectangles. */
2012 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2014 XRectangle r;
2016 if (n <= 0)
2017 return 0;
2019 if (s->row->full_width_p)
2021 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2022 r.x = WINDOW_LEFT_EDGE_X (s->w);
2023 r.width = WINDOW_TOTAL_WIDTH (s->w);
2025 /* Unless displaying a mode or menu bar line, which are always
2026 fully visible, clip to the visible part of the row. */
2027 if (s->w->pseudo_window_p)
2028 r.height = s->row->visible_height;
2029 else
2030 r.height = s->height;
2032 else
2034 /* This is a text line that may be partially visible. */
2035 r.x = window_box_left (s->w, s->area);
2036 r.width = window_box_width (s->w, s->area);
2037 r.height = s->row->visible_height;
2040 if (s->clip_head)
2041 if (r.x < s->clip_head->x)
2043 if (r.width >= s->clip_head->x - r.x)
2044 r.width -= s->clip_head->x - r.x;
2045 else
2046 r.width = 0;
2047 r.x = s->clip_head->x;
2049 if (s->clip_tail)
2050 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2052 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2053 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2054 else
2055 r.width = 0;
2058 /* If S draws overlapping rows, it's sufficient to use the top and
2059 bottom of the window for clipping because this glyph string
2060 intentionally draws over other lines. */
2061 if (s->for_overlaps)
2063 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2064 r.height = window_text_bottom_y (s->w) - r.y;
2066 /* Alas, the above simple strategy does not work for the
2067 environments with anti-aliased text: if the same text is
2068 drawn onto the same place multiple times, it gets thicker.
2069 If the overlap we are processing is for the erased cursor, we
2070 take the intersection with the rectangle of the cursor. */
2071 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2073 XRectangle rc, r_save = r;
2075 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2076 rc.y = s->w->phys_cursor.y;
2077 rc.width = s->w->phys_cursor_width;
2078 rc.height = s->w->phys_cursor_height;
2080 x_intersect_rectangles (&r_save, &rc, &r);
2083 else
2085 /* Don't use S->y for clipping because it doesn't take partially
2086 visible lines into account. For example, it can be negative for
2087 partially visible lines at the top of a window. */
2088 if (!s->row->full_width_p
2089 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2090 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2091 else
2092 r.y = max (0, s->row->y);
2095 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2097 /* If drawing the cursor, don't let glyph draw outside its
2098 advertised boundaries. Cleartype does this under some circumstances. */
2099 if (s->hl == DRAW_CURSOR)
2101 struct glyph *glyph = s->first_glyph;
2102 int height, max_y;
2104 if (s->x > r.x)
2106 r.width -= s->x - r.x;
2107 r.x = s->x;
2109 r.width = min (r.width, glyph->pixel_width);
2111 /* If r.y is below window bottom, ensure that we still see a cursor. */
2112 height = min (glyph->ascent + glyph->descent,
2113 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2114 max_y = window_text_bottom_y (s->w) - height;
2115 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2116 if (s->ybase - glyph->ascent > max_y)
2118 r.y = max_y;
2119 r.height = height;
2121 else
2123 /* Don't draw cursor glyph taller than our actual glyph. */
2124 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2125 if (height < r.height)
2127 max_y = r.y + r.height;
2128 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2129 r.height = min (max_y - r.y, height);
2134 if (s->row->clip)
2136 XRectangle r_save = r;
2138 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2139 r.width = 0;
2142 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2143 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2145 #ifdef CONVERT_FROM_XRECT
2146 CONVERT_FROM_XRECT (r, *rects);
2147 #else
2148 *rects = r;
2149 #endif
2150 return 1;
2152 else
2154 /* If we are processing overlapping and allowed to return
2155 multiple clipping rectangles, we exclude the row of the glyph
2156 string from the clipping rectangle. This is to avoid drawing
2157 the same text on the environment with anti-aliasing. */
2158 #ifdef CONVERT_FROM_XRECT
2159 XRectangle rs[2];
2160 #else
2161 XRectangle *rs = rects;
2162 #endif
2163 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2165 if (s->for_overlaps & OVERLAPS_PRED)
2167 rs[i] = r;
2168 if (r.y + r.height > row_y)
2170 if (r.y < row_y)
2171 rs[i].height = row_y - r.y;
2172 else
2173 rs[i].height = 0;
2175 i++;
2177 if (s->for_overlaps & OVERLAPS_SUCC)
2179 rs[i] = r;
2180 if (r.y < row_y + s->row->visible_height)
2182 if (r.y + r.height > row_y + s->row->visible_height)
2184 rs[i].y = row_y + s->row->visible_height;
2185 rs[i].height = r.y + r.height - rs[i].y;
2187 else
2188 rs[i].height = 0;
2190 i++;
2193 n = i;
2194 #ifdef CONVERT_FROM_XRECT
2195 for (i = 0; i < n; i++)
2196 CONVERT_FROM_XRECT (rs[i], rects[i]);
2197 #endif
2198 return n;
2202 /* EXPORT:
2203 Return in *NR the clipping rectangle for glyph string S. */
2205 void
2206 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2208 get_glyph_string_clip_rects (s, nr, 1);
2212 /* EXPORT:
2213 Return the position and height of the phys cursor in window W.
2214 Set w->phys_cursor_width to width of phys cursor.
2217 void
2218 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2219 struct glyph *glyph, int *xp, int *yp, int *heightp)
2221 struct frame *f = XFRAME (WINDOW_FRAME (w));
2222 int x, y, wd, h, h0, y0;
2224 /* Compute the width of the rectangle to draw. If on a stretch
2225 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2226 rectangle as wide as the glyph, but use a canonical character
2227 width instead. */
2228 wd = glyph->pixel_width - 1;
2229 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2230 wd++; /* Why? */
2231 #endif
2233 x = w->phys_cursor.x;
2234 if (x < 0)
2236 wd += x;
2237 x = 0;
2240 if (glyph->type == STRETCH_GLYPH
2241 && !x_stretch_cursor_p)
2242 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2243 w->phys_cursor_width = wd;
2245 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2247 /* If y is below window bottom, ensure that we still see a cursor. */
2248 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2250 h = max (h0, glyph->ascent + glyph->descent);
2251 h0 = min (h0, glyph->ascent + glyph->descent);
2253 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2254 if (y < y0)
2256 h = max (h - (y0 - y) + 1, h0);
2257 y = y0 - 1;
2259 else
2261 y0 = window_text_bottom_y (w) - h0;
2262 if (y > y0)
2264 h += y - y0;
2265 y = y0;
2269 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2270 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2271 *heightp = h;
2275 * Remember which glyph the mouse is over.
2278 void
2279 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2281 Lisp_Object window;
2282 struct window *w;
2283 struct glyph_row *r, *gr, *end_row;
2284 enum window_part part;
2285 enum glyph_row_area area;
2286 int x, y, width, height;
2288 /* Try to determine frame pixel position and size of the glyph under
2289 frame pixel coordinates X/Y on frame F. */
2291 if (!f->glyphs_initialized_p
2292 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2293 NILP (window)))
2295 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2296 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2297 goto virtual_glyph;
2300 w = XWINDOW (window);
2301 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2302 height = WINDOW_FRAME_LINE_HEIGHT (w);
2304 x = window_relative_x_coord (w, part, gx);
2305 y = gy - WINDOW_TOP_EDGE_Y (w);
2307 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2308 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2310 if (w->pseudo_window_p)
2312 area = TEXT_AREA;
2313 part = ON_MODE_LINE; /* Don't adjust margin. */
2314 goto text_glyph;
2317 switch (part)
2319 case ON_LEFT_MARGIN:
2320 area = LEFT_MARGIN_AREA;
2321 goto text_glyph;
2323 case ON_RIGHT_MARGIN:
2324 area = RIGHT_MARGIN_AREA;
2325 goto text_glyph;
2327 case ON_HEADER_LINE:
2328 case ON_MODE_LINE:
2329 gr = (part == ON_HEADER_LINE
2330 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2331 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2332 gy = gr->y;
2333 area = TEXT_AREA;
2334 goto text_glyph_row_found;
2336 case ON_TEXT:
2337 area = TEXT_AREA;
2339 text_glyph:
2340 gr = 0; gy = 0;
2341 for (; r <= end_row && r->enabled_p; ++r)
2342 if (r->y + r->height > y)
2344 gr = r; gy = r->y;
2345 break;
2348 text_glyph_row_found:
2349 if (gr && gy <= y)
2351 struct glyph *g = gr->glyphs[area];
2352 struct glyph *end = g + gr->used[area];
2354 height = gr->height;
2355 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2356 if (gx + g->pixel_width > x)
2357 break;
2359 if (g < end)
2361 if (g->type == IMAGE_GLYPH)
2363 /* Don't remember when mouse is over image, as
2364 image may have hot-spots. */
2365 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2366 return;
2368 width = g->pixel_width;
2370 else
2372 /* Use nominal char spacing at end of line. */
2373 x -= gx;
2374 gx += (x / width) * width;
2377 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2378 gx += window_box_left_offset (w, area);
2380 else
2382 /* Use nominal line height at end of window. */
2383 gx = (x / width) * width;
2384 y -= gy;
2385 gy += (y / height) * height;
2387 break;
2389 case ON_LEFT_FRINGE:
2390 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2391 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2392 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2393 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2394 goto row_glyph;
2396 case ON_RIGHT_FRINGE:
2397 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2398 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2399 : window_box_right_offset (w, TEXT_AREA));
2400 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2401 goto row_glyph;
2403 case ON_SCROLL_BAR:
2404 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2406 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2407 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2408 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2409 : 0)));
2410 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2412 row_glyph:
2413 gr = 0, gy = 0;
2414 for (; r <= end_row && r->enabled_p; ++r)
2415 if (r->y + r->height > y)
2417 gr = r; gy = r->y;
2418 break;
2421 if (gr && gy <= y)
2422 height = gr->height;
2423 else
2425 /* Use nominal line height at end of window. */
2426 y -= gy;
2427 gy += (y / height) * height;
2429 break;
2431 default:
2433 virtual_glyph:
2434 /* If there is no glyph under the mouse, then we divide the screen
2435 into a grid of the smallest glyph in the frame, and use that
2436 as our "glyph". */
2438 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2439 round down even for negative values. */
2440 if (gx < 0)
2441 gx -= width - 1;
2442 if (gy < 0)
2443 gy -= height - 1;
2445 gx = (gx / width) * width;
2446 gy = (gy / height) * height;
2448 goto store_rect;
2451 gx += WINDOW_LEFT_EDGE_X (w);
2452 gy += WINDOW_TOP_EDGE_Y (w);
2454 store_rect:
2455 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2457 /* Visible feedback for debugging. */
2458 #if 0
2459 #if HAVE_X_WINDOWS
2460 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2461 f->output_data.x->normal_gc,
2462 gx, gy, width, height);
2463 #endif
2464 #endif
2468 #endif /* HAVE_WINDOW_SYSTEM */
2471 /***********************************************************************
2472 Lisp form evaluation
2473 ***********************************************************************/
2475 /* Error handler for safe_eval and safe_call. */
2477 static Lisp_Object
2478 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2480 add_to_log ("Error during redisplay: %S signaled %S",
2481 Flist (nargs, args), arg);
2482 return Qnil;
2485 /* Call function FUNC with the rest of NARGS - 1 arguments
2486 following. Return the result, or nil if something went
2487 wrong. Prevent redisplay during the evaluation. */
2489 Lisp_Object
2490 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2492 Lisp_Object val;
2494 if (inhibit_eval_during_redisplay)
2495 val = Qnil;
2496 else
2498 va_list ap;
2499 ptrdiff_t i;
2500 ptrdiff_t count = SPECPDL_INDEX ();
2501 struct gcpro gcpro1;
2502 Lisp_Object *args = alloca (nargs * word_size);
2504 args[0] = func;
2505 va_start (ap, func);
2506 for (i = 1; i < nargs; i++)
2507 args[i] = va_arg (ap, Lisp_Object);
2508 va_end (ap);
2510 GCPRO1 (args[0]);
2511 gcpro1.nvars = nargs;
2512 specbind (Qinhibit_redisplay, Qt);
2513 /* Use Qt to ensure debugger does not run,
2514 so there is no possibility of wanting to redisplay. */
2515 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2516 safe_eval_handler);
2517 UNGCPRO;
2518 val = unbind_to (count, val);
2521 return val;
2525 /* Call function FN with one argument ARG.
2526 Return the result, or nil if something went wrong. */
2528 Lisp_Object
2529 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2531 return safe_call (2, fn, arg);
2534 static Lisp_Object Qeval;
2536 Lisp_Object
2537 safe_eval (Lisp_Object sexpr)
2539 return safe_call1 (Qeval, sexpr);
2542 /* Call function FN with two arguments ARG1 and ARG2.
2543 Return the result, or nil if something went wrong. */
2545 Lisp_Object
2546 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2548 return safe_call (3, fn, arg1, arg2);
2553 /***********************************************************************
2554 Debugging
2555 ***********************************************************************/
2557 #if 0
2559 /* Define CHECK_IT to perform sanity checks on iterators.
2560 This is for debugging. It is too slow to do unconditionally. */
2562 static void
2563 check_it (struct it *it)
2565 if (it->method == GET_FROM_STRING)
2567 eassert (STRINGP (it->string));
2568 eassert (IT_STRING_CHARPOS (*it) >= 0);
2570 else
2572 eassert (IT_STRING_CHARPOS (*it) < 0);
2573 if (it->method == GET_FROM_BUFFER)
2575 /* Check that character and byte positions agree. */
2576 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2580 if (it->dpvec)
2581 eassert (it->current.dpvec_index >= 0);
2582 else
2583 eassert (it->current.dpvec_index < 0);
2586 #define CHECK_IT(IT) check_it ((IT))
2588 #else /* not 0 */
2590 #define CHECK_IT(IT) (void) 0
2592 #endif /* not 0 */
2595 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2597 /* Check that the window end of window W is what we expect it
2598 to be---the last row in the current matrix displaying text. */
2600 static void
2601 check_window_end (struct window *w)
2603 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2605 struct glyph_row *row;
2606 eassert ((row = MATRIX_ROW (w->current_matrix,
2607 XFASTINT (w->window_end_vpos)),
2608 !row->enabled_p
2609 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2610 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2614 #define CHECK_WINDOW_END(W) check_window_end ((W))
2616 #else
2618 #define CHECK_WINDOW_END(W) (void) 0
2620 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2622 /* Return mark position if current buffer has the region of non-zero length,
2623 or -1 otherwise. */
2625 static ptrdiff_t
2626 markpos_of_region (void)
2628 if (!NILP (Vtransient_mark_mode)
2629 && !NILP (BVAR (current_buffer, mark_active))
2630 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2632 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2634 if (markpos != PT)
2635 return markpos;
2637 return -1;
2640 /***********************************************************************
2641 Iterator initialization
2642 ***********************************************************************/
2644 /* Initialize IT for displaying current_buffer in window W, starting
2645 at character position CHARPOS. CHARPOS < 0 means that no buffer
2646 position is specified which is useful when the iterator is assigned
2647 a position later. BYTEPOS is the byte position corresponding to
2648 CHARPOS.
2650 If ROW is not null, calls to produce_glyphs with IT as parameter
2651 will produce glyphs in that row.
2653 BASE_FACE_ID is the id of a base face to use. It must be one of
2654 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2655 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2656 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2658 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2659 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2660 will be initialized to use the corresponding mode line glyph row of
2661 the desired matrix of W. */
2663 void
2664 init_iterator (struct it *it, struct window *w,
2665 ptrdiff_t charpos, ptrdiff_t bytepos,
2666 struct glyph_row *row, enum face_id base_face_id)
2668 ptrdiff_t markpos;
2669 enum face_id remapped_base_face_id = base_face_id;
2671 /* Some precondition checks. */
2672 eassert (w != NULL && it != NULL);
2673 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2674 && charpos <= ZV));
2676 /* If face attributes have been changed since the last redisplay,
2677 free realized faces now because they depend on face definitions
2678 that might have changed. Don't free faces while there might be
2679 desired matrices pending which reference these faces. */
2680 if (face_change_count && !inhibit_free_realized_faces)
2682 face_change_count = 0;
2683 free_all_realized_faces (Qnil);
2686 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2687 if (! NILP (Vface_remapping_alist))
2688 remapped_base_face_id
2689 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2691 /* Use one of the mode line rows of W's desired matrix if
2692 appropriate. */
2693 if (row == NULL)
2695 if (base_face_id == MODE_LINE_FACE_ID
2696 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2697 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2698 else if (base_face_id == HEADER_LINE_FACE_ID)
2699 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2702 /* Clear IT. */
2703 memset (it, 0, sizeof *it);
2704 it->current.overlay_string_index = -1;
2705 it->current.dpvec_index = -1;
2706 it->base_face_id = remapped_base_face_id;
2707 it->string = Qnil;
2708 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2709 it->paragraph_embedding = L2R;
2710 it->bidi_it.string.lstring = Qnil;
2711 it->bidi_it.string.s = NULL;
2712 it->bidi_it.string.bufpos = 0;
2713 it->bidi_it.w = w;
2715 /* The window in which we iterate over current_buffer: */
2716 XSETWINDOW (it->window, w);
2717 it->w = w;
2718 it->f = XFRAME (w->frame);
2720 it->cmp_it.id = -1;
2722 /* Extra space between lines (on window systems only). */
2723 if (base_face_id == DEFAULT_FACE_ID
2724 && FRAME_WINDOW_P (it->f))
2726 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2727 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2728 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2729 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2730 * FRAME_LINE_HEIGHT (it->f));
2731 else if (it->f->extra_line_spacing > 0)
2732 it->extra_line_spacing = it->f->extra_line_spacing;
2733 it->max_extra_line_spacing = 0;
2736 /* If realized faces have been removed, e.g. because of face
2737 attribute changes of named faces, recompute them. When running
2738 in batch mode, the face cache of the initial frame is null. If
2739 we happen to get called, make a dummy face cache. */
2740 if (FRAME_FACE_CACHE (it->f) == NULL)
2741 init_frame_faces (it->f);
2742 if (FRAME_FACE_CACHE (it->f)->used == 0)
2743 recompute_basic_faces (it->f);
2745 /* Current value of the `slice', `space-width', and 'height' properties. */
2746 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2747 it->space_width = Qnil;
2748 it->font_height = Qnil;
2749 it->override_ascent = -1;
2751 /* Are control characters displayed as `^C'? */
2752 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2754 /* -1 means everything between a CR and the following line end
2755 is invisible. >0 means lines indented more than this value are
2756 invisible. */
2757 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2758 ? (clip_to_bounds
2759 (-1, XINT (BVAR (current_buffer, selective_display)),
2760 PTRDIFF_MAX))
2761 : (!NILP (BVAR (current_buffer, selective_display))
2762 ? -1 : 0));
2763 it->selective_display_ellipsis_p
2764 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2766 /* Display table to use. */
2767 it->dp = window_display_table (w);
2769 /* Are multibyte characters enabled in current_buffer? */
2770 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2772 /* If visible region is of non-zero length, set IT->region_beg_charpos
2773 and IT->region_end_charpos to the start and end of a visible region
2774 in window IT->w. Set both to -1 to indicate no region. */
2775 markpos = markpos_of_region ();
2776 if (markpos >= 0
2777 /* Maybe highlight only in selected window. */
2778 && (/* Either show region everywhere. */
2779 highlight_nonselected_windows
2780 /* Or show region in the selected window. */
2781 || w == XWINDOW (selected_window)
2782 /* Or show the region if we are in the mini-buffer and W is
2783 the window the mini-buffer refers to. */
2784 || (MINI_WINDOW_P (XWINDOW (selected_window))
2785 && WINDOWP (minibuf_selected_window)
2786 && w == XWINDOW (minibuf_selected_window))))
2788 it->region_beg_charpos = min (PT, markpos);
2789 it->region_end_charpos = max (PT, markpos);
2791 else
2792 it->region_beg_charpos = it->region_end_charpos = -1;
2794 /* Get the position at which the redisplay_end_trigger hook should
2795 be run, if it is to be run at all. */
2796 if (MARKERP (w->redisplay_end_trigger)
2797 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2798 it->redisplay_end_trigger_charpos
2799 = marker_position (w->redisplay_end_trigger);
2800 else if (INTEGERP (w->redisplay_end_trigger))
2801 it->redisplay_end_trigger_charpos =
2802 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2804 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2806 /* Are lines in the display truncated? */
2807 if (base_face_id != DEFAULT_FACE_ID
2808 || it->w->hscroll
2809 || (! WINDOW_FULL_WIDTH_P (it->w)
2810 && ((!NILP (Vtruncate_partial_width_windows)
2811 && !INTEGERP (Vtruncate_partial_width_windows))
2812 || (INTEGERP (Vtruncate_partial_width_windows)
2813 && (WINDOW_TOTAL_COLS (it->w)
2814 < XINT (Vtruncate_partial_width_windows))))))
2815 it->line_wrap = TRUNCATE;
2816 else if (NILP (BVAR (current_buffer, truncate_lines)))
2817 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2818 ? WINDOW_WRAP : WORD_WRAP;
2819 else
2820 it->line_wrap = TRUNCATE;
2822 /* Get dimensions of truncation and continuation glyphs. These are
2823 displayed as fringe bitmaps under X, but we need them for such
2824 frames when the fringes are turned off. But leave the dimensions
2825 zero for tooltip frames, as these glyphs look ugly there and also
2826 sabotage calculations of tooltip dimensions in x-show-tip. */
2827 #ifdef HAVE_WINDOW_SYSTEM
2828 if (!(FRAME_WINDOW_P (it->f)
2829 && FRAMEP (tip_frame)
2830 && it->f == XFRAME (tip_frame)))
2831 #endif
2833 if (it->line_wrap == TRUNCATE)
2835 /* We will need the truncation glyph. */
2836 eassert (it->glyph_row == NULL);
2837 produce_special_glyphs (it, IT_TRUNCATION);
2838 it->truncation_pixel_width = it->pixel_width;
2840 else
2842 /* We will need the continuation glyph. */
2843 eassert (it->glyph_row == NULL);
2844 produce_special_glyphs (it, IT_CONTINUATION);
2845 it->continuation_pixel_width = it->pixel_width;
2849 /* Reset these values to zero because the produce_special_glyphs
2850 above has changed them. */
2851 it->pixel_width = it->ascent = it->descent = 0;
2852 it->phys_ascent = it->phys_descent = 0;
2854 /* Set this after getting the dimensions of truncation and
2855 continuation glyphs, so that we don't produce glyphs when calling
2856 produce_special_glyphs, above. */
2857 it->glyph_row = row;
2858 it->area = TEXT_AREA;
2860 /* Forget any previous info about this row being reversed. */
2861 if (it->glyph_row)
2862 it->glyph_row->reversed_p = 0;
2864 /* Get the dimensions of the display area. The display area
2865 consists of the visible window area plus a horizontally scrolled
2866 part to the left of the window. All x-values are relative to the
2867 start of this total display area. */
2868 if (base_face_id != DEFAULT_FACE_ID)
2870 /* Mode lines, menu bar in terminal frames. */
2871 it->first_visible_x = 0;
2872 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2874 else
2876 it->first_visible_x =
2877 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2878 it->last_visible_x = (it->first_visible_x
2879 + window_box_width (w, TEXT_AREA));
2881 /* If we truncate lines, leave room for the truncation glyph(s) at
2882 the right margin. Otherwise, leave room for the continuation
2883 glyph(s). Done only if the window has no fringes. Since we
2884 don't know at this point whether there will be any R2L lines in
2885 the window, we reserve space for truncation/continuation glyphs
2886 even if only one of the fringes is absent. */
2887 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2888 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2890 if (it->line_wrap == TRUNCATE)
2891 it->last_visible_x -= it->truncation_pixel_width;
2892 else
2893 it->last_visible_x -= it->continuation_pixel_width;
2896 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2897 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2900 /* Leave room for a border glyph. */
2901 if (!FRAME_WINDOW_P (it->f)
2902 && !WINDOW_RIGHTMOST_P (it->w))
2903 it->last_visible_x -= 1;
2905 it->last_visible_y = window_text_bottom_y (w);
2907 /* For mode lines and alike, arrange for the first glyph having a
2908 left box line if the face specifies a box. */
2909 if (base_face_id != DEFAULT_FACE_ID)
2911 struct face *face;
2913 it->face_id = remapped_base_face_id;
2915 /* If we have a boxed mode line, make the first character appear
2916 with a left box line. */
2917 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2918 if (face->box != FACE_NO_BOX)
2919 it->start_of_box_run_p = 1;
2922 /* If a buffer position was specified, set the iterator there,
2923 getting overlays and face properties from that position. */
2924 if (charpos >= BUF_BEG (current_buffer))
2926 it->end_charpos = ZV;
2927 eassert (charpos == BYTE_TO_CHAR (bytepos));
2928 IT_CHARPOS (*it) = charpos;
2929 IT_BYTEPOS (*it) = bytepos;
2931 /* We will rely on `reseat' to set this up properly, via
2932 handle_face_prop. */
2933 it->face_id = it->base_face_id;
2935 it->start = it->current;
2936 /* Do we need to reorder bidirectional text? Not if this is a
2937 unibyte buffer: by definition, none of the single-byte
2938 characters are strong R2L, so no reordering is needed. And
2939 bidi.c doesn't support unibyte buffers anyway. Also, don't
2940 reorder while we are loading loadup.el, since the tables of
2941 character properties needed for reordering are not yet
2942 available. */
2943 it->bidi_p =
2944 NILP (Vpurify_flag)
2945 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2946 && it->multibyte_p;
2948 /* If we are to reorder bidirectional text, init the bidi
2949 iterator. */
2950 if (it->bidi_p)
2952 /* Note the paragraph direction that this buffer wants to
2953 use. */
2954 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2955 Qleft_to_right))
2956 it->paragraph_embedding = L2R;
2957 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2958 Qright_to_left))
2959 it->paragraph_embedding = R2L;
2960 else
2961 it->paragraph_embedding = NEUTRAL_DIR;
2962 bidi_unshelve_cache (NULL, 0);
2963 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2964 &it->bidi_it);
2967 /* Compute faces etc. */
2968 reseat (it, it->current.pos, 1);
2971 CHECK_IT (it);
2975 /* Initialize IT for the display of window W with window start POS. */
2977 void
2978 start_display (struct it *it, struct window *w, struct text_pos pos)
2980 struct glyph_row *row;
2981 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2983 row = w->desired_matrix->rows + first_vpos;
2984 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2985 it->first_vpos = first_vpos;
2987 /* Don't reseat to previous visible line start if current start
2988 position is in a string or image. */
2989 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2991 int start_at_line_beg_p;
2992 int first_y = it->current_y;
2994 /* If window start is not at a line start, skip forward to POS to
2995 get the correct continuation lines width. */
2996 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2997 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2998 if (!start_at_line_beg_p)
3000 int new_x;
3002 reseat_at_previous_visible_line_start (it);
3003 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3005 new_x = it->current_x + it->pixel_width;
3007 /* If lines are continued, this line may end in the middle
3008 of a multi-glyph character (e.g. a control character
3009 displayed as \003, or in the middle of an overlay
3010 string). In this case move_it_to above will not have
3011 taken us to the start of the continuation line but to the
3012 end of the continued line. */
3013 if (it->current_x > 0
3014 && it->line_wrap != TRUNCATE /* Lines are continued. */
3015 && (/* And glyph doesn't fit on the line. */
3016 new_x > it->last_visible_x
3017 /* Or it fits exactly and we're on a window
3018 system frame. */
3019 || (new_x == it->last_visible_x
3020 && FRAME_WINDOW_P (it->f)
3021 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3022 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3023 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3025 if ((it->current.dpvec_index >= 0
3026 || it->current.overlay_string_index >= 0)
3027 /* If we are on a newline from a display vector or
3028 overlay string, then we are already at the end of
3029 a screen line; no need to go to the next line in
3030 that case, as this line is not really continued.
3031 (If we do go to the next line, C-e will not DTRT.) */
3032 && it->c != '\n')
3034 set_iterator_to_next (it, 1);
3035 move_it_in_display_line_to (it, -1, -1, 0);
3038 it->continuation_lines_width += it->current_x;
3040 /* If the character at POS is displayed via a display
3041 vector, move_it_to above stops at the final glyph of
3042 IT->dpvec. To make the caller redisplay that character
3043 again (a.k.a. start at POS), we need to reset the
3044 dpvec_index to the beginning of IT->dpvec. */
3045 else if (it->current.dpvec_index >= 0)
3046 it->current.dpvec_index = 0;
3048 /* We're starting a new display line, not affected by the
3049 height of the continued line, so clear the appropriate
3050 fields in the iterator structure. */
3051 it->max_ascent = it->max_descent = 0;
3052 it->max_phys_ascent = it->max_phys_descent = 0;
3054 it->current_y = first_y;
3055 it->vpos = 0;
3056 it->current_x = it->hpos = 0;
3062 /* Return 1 if POS is a position in ellipses displayed for invisible
3063 text. W is the window we display, for text property lookup. */
3065 static int
3066 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3068 Lisp_Object prop, window;
3069 int ellipses_p = 0;
3070 ptrdiff_t charpos = CHARPOS (pos->pos);
3072 /* If POS specifies a position in a display vector, this might
3073 be for an ellipsis displayed for invisible text. We won't
3074 get the iterator set up for delivering that ellipsis unless
3075 we make sure that it gets aware of the invisible text. */
3076 if (pos->dpvec_index >= 0
3077 && pos->overlay_string_index < 0
3078 && CHARPOS (pos->string_pos) < 0
3079 && charpos > BEGV
3080 && (XSETWINDOW (window, w),
3081 prop = Fget_char_property (make_number (charpos),
3082 Qinvisible, window),
3083 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3085 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3086 window);
3087 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3090 return ellipses_p;
3094 /* Initialize IT for stepping through current_buffer in window W,
3095 starting at position POS that includes overlay string and display
3096 vector/ control character translation position information. Value
3097 is zero if there are overlay strings with newlines at POS. */
3099 static int
3100 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3102 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3103 int i, overlay_strings_with_newlines = 0;
3105 /* If POS specifies a position in a display vector, this might
3106 be for an ellipsis displayed for invisible text. We won't
3107 get the iterator set up for delivering that ellipsis unless
3108 we make sure that it gets aware of the invisible text. */
3109 if (in_ellipses_for_invisible_text_p (pos, w))
3111 --charpos;
3112 bytepos = 0;
3115 /* Keep in mind: the call to reseat in init_iterator skips invisible
3116 text, so we might end up at a position different from POS. This
3117 is only a problem when POS is a row start after a newline and an
3118 overlay starts there with an after-string, and the overlay has an
3119 invisible property. Since we don't skip invisible text in
3120 display_line and elsewhere immediately after consuming the
3121 newline before the row start, such a POS will not be in a string,
3122 but the call to init_iterator below will move us to the
3123 after-string. */
3124 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3126 /* This only scans the current chunk -- it should scan all chunks.
3127 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3128 to 16 in 22.1 to make this a lesser problem. */
3129 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3131 const char *s = SSDATA (it->overlay_strings[i]);
3132 const char *e = s + SBYTES (it->overlay_strings[i]);
3134 while (s < e && *s != '\n')
3135 ++s;
3137 if (s < e)
3139 overlay_strings_with_newlines = 1;
3140 break;
3144 /* If position is within an overlay string, set up IT to the right
3145 overlay string. */
3146 if (pos->overlay_string_index >= 0)
3148 int relative_index;
3150 /* If the first overlay string happens to have a `display'
3151 property for an image, the iterator will be set up for that
3152 image, and we have to undo that setup first before we can
3153 correct the overlay string index. */
3154 if (it->method == GET_FROM_IMAGE)
3155 pop_it (it);
3157 /* We already have the first chunk of overlay strings in
3158 IT->overlay_strings. Load more until the one for
3159 pos->overlay_string_index is in IT->overlay_strings. */
3160 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3162 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3163 it->current.overlay_string_index = 0;
3164 while (n--)
3166 load_overlay_strings (it, 0);
3167 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3171 it->current.overlay_string_index = pos->overlay_string_index;
3172 relative_index = (it->current.overlay_string_index
3173 % OVERLAY_STRING_CHUNK_SIZE);
3174 it->string = it->overlay_strings[relative_index];
3175 eassert (STRINGP (it->string));
3176 it->current.string_pos = pos->string_pos;
3177 it->method = GET_FROM_STRING;
3178 it->end_charpos = SCHARS (it->string);
3179 /* Set up the bidi iterator for this overlay string. */
3180 if (it->bidi_p)
3182 it->bidi_it.string.lstring = it->string;
3183 it->bidi_it.string.s = NULL;
3184 it->bidi_it.string.schars = SCHARS (it->string);
3185 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3186 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3187 it->bidi_it.string.unibyte = !it->multibyte_p;
3188 it->bidi_it.w = it->w;
3189 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3190 FRAME_WINDOW_P (it->f), &it->bidi_it);
3192 /* Synchronize the state of the bidi iterator with
3193 pos->string_pos. For any string position other than
3194 zero, this will be done automagically when we resume
3195 iteration over the string and get_visually_first_element
3196 is called. But if string_pos is zero, and the string is
3197 to be reordered for display, we need to resync manually,
3198 since it could be that the iteration state recorded in
3199 pos ended at string_pos of 0 moving backwards in string. */
3200 if (CHARPOS (pos->string_pos) == 0)
3202 get_visually_first_element (it);
3203 if (IT_STRING_CHARPOS (*it) != 0)
3204 do {
3205 /* Paranoia. */
3206 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3207 bidi_move_to_visually_next (&it->bidi_it);
3208 } while (it->bidi_it.charpos != 0);
3210 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3211 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3215 if (CHARPOS (pos->string_pos) >= 0)
3217 /* Recorded position is not in an overlay string, but in another
3218 string. This can only be a string from a `display' property.
3219 IT should already be filled with that string. */
3220 it->current.string_pos = pos->string_pos;
3221 eassert (STRINGP (it->string));
3222 if (it->bidi_p)
3223 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3224 FRAME_WINDOW_P (it->f), &it->bidi_it);
3227 /* Restore position in display vector translations, control
3228 character translations or ellipses. */
3229 if (pos->dpvec_index >= 0)
3231 if (it->dpvec == NULL)
3232 get_next_display_element (it);
3233 eassert (it->dpvec && it->current.dpvec_index == 0);
3234 it->current.dpvec_index = pos->dpvec_index;
3237 CHECK_IT (it);
3238 return !overlay_strings_with_newlines;
3242 /* Initialize IT for stepping through current_buffer in window W
3243 starting at ROW->start. */
3245 static void
3246 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3248 init_from_display_pos (it, w, &row->start);
3249 it->start = row->start;
3250 it->continuation_lines_width = row->continuation_lines_width;
3251 CHECK_IT (it);
3255 /* Initialize IT for stepping through current_buffer in window W
3256 starting in the line following ROW, i.e. starting at ROW->end.
3257 Value is zero if there are overlay strings with newlines at ROW's
3258 end position. */
3260 static int
3261 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3263 int success = 0;
3265 if (init_from_display_pos (it, w, &row->end))
3267 if (row->continued_p)
3268 it->continuation_lines_width
3269 = row->continuation_lines_width + row->pixel_width;
3270 CHECK_IT (it);
3271 success = 1;
3274 return success;
3280 /***********************************************************************
3281 Text properties
3282 ***********************************************************************/
3284 /* Called when IT reaches IT->stop_charpos. Handle text property and
3285 overlay changes. Set IT->stop_charpos to the next position where
3286 to stop. */
3288 static void
3289 handle_stop (struct it *it)
3291 enum prop_handled handled;
3292 int handle_overlay_change_p;
3293 struct props *p;
3295 it->dpvec = NULL;
3296 it->current.dpvec_index = -1;
3297 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3298 it->ignore_overlay_strings_at_pos_p = 0;
3299 it->ellipsis_p = 0;
3301 /* Use face of preceding text for ellipsis (if invisible) */
3302 if (it->selective_display_ellipsis_p)
3303 it->saved_face_id = it->face_id;
3307 handled = HANDLED_NORMALLY;
3309 /* Call text property handlers. */
3310 for (p = it_props; p->handler; ++p)
3312 handled = p->handler (it);
3314 if (handled == HANDLED_RECOMPUTE_PROPS)
3315 break;
3316 else if (handled == HANDLED_RETURN)
3318 /* We still want to show before and after strings from
3319 overlays even if the actual buffer text is replaced. */
3320 if (!handle_overlay_change_p
3321 || it->sp > 1
3322 /* Don't call get_overlay_strings_1 if we already
3323 have overlay strings loaded, because doing so
3324 will load them again and push the iterator state
3325 onto the stack one more time, which is not
3326 expected by the rest of the code that processes
3327 overlay strings. */
3328 || (it->current.overlay_string_index < 0
3329 ? !get_overlay_strings_1 (it, 0, 0)
3330 : 0))
3332 if (it->ellipsis_p)
3333 setup_for_ellipsis (it, 0);
3334 /* When handling a display spec, we might load an
3335 empty string. In that case, discard it here. We
3336 used to discard it in handle_single_display_spec,
3337 but that causes get_overlay_strings_1, above, to
3338 ignore overlay strings that we must check. */
3339 if (STRINGP (it->string) && !SCHARS (it->string))
3340 pop_it (it);
3341 return;
3343 else if (STRINGP (it->string) && !SCHARS (it->string))
3344 pop_it (it);
3345 else
3347 it->ignore_overlay_strings_at_pos_p = 1;
3348 it->string_from_display_prop_p = 0;
3349 it->from_disp_prop_p = 0;
3350 handle_overlay_change_p = 0;
3352 handled = HANDLED_RECOMPUTE_PROPS;
3353 break;
3355 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3356 handle_overlay_change_p = 0;
3359 if (handled != HANDLED_RECOMPUTE_PROPS)
3361 /* Don't check for overlay strings below when set to deliver
3362 characters from a display vector. */
3363 if (it->method == GET_FROM_DISPLAY_VECTOR)
3364 handle_overlay_change_p = 0;
3366 /* Handle overlay changes.
3367 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3368 if it finds overlays. */
3369 if (handle_overlay_change_p)
3370 handled = handle_overlay_change (it);
3373 if (it->ellipsis_p)
3375 setup_for_ellipsis (it, 0);
3376 break;
3379 while (handled == HANDLED_RECOMPUTE_PROPS);
3381 /* Determine where to stop next. */
3382 if (handled == HANDLED_NORMALLY)
3383 compute_stop_pos (it);
3387 /* Compute IT->stop_charpos from text property and overlay change
3388 information for IT's current position. */
3390 static void
3391 compute_stop_pos (struct it *it)
3393 register INTERVAL iv, next_iv;
3394 Lisp_Object object, limit, position;
3395 ptrdiff_t charpos, bytepos;
3397 if (STRINGP (it->string))
3399 /* Strings are usually short, so don't limit the search for
3400 properties. */
3401 it->stop_charpos = it->end_charpos;
3402 object = it->string;
3403 limit = Qnil;
3404 charpos = IT_STRING_CHARPOS (*it);
3405 bytepos = IT_STRING_BYTEPOS (*it);
3407 else
3409 ptrdiff_t pos;
3411 /* If end_charpos is out of range for some reason, such as a
3412 misbehaving display function, rationalize it (Bug#5984). */
3413 if (it->end_charpos > ZV)
3414 it->end_charpos = ZV;
3415 it->stop_charpos = it->end_charpos;
3417 /* If next overlay change is in front of the current stop pos
3418 (which is IT->end_charpos), stop there. Note: value of
3419 next_overlay_change is point-max if no overlay change
3420 follows. */
3421 charpos = IT_CHARPOS (*it);
3422 bytepos = IT_BYTEPOS (*it);
3423 pos = next_overlay_change (charpos);
3424 if (pos < it->stop_charpos)
3425 it->stop_charpos = pos;
3427 /* If showing the region, we have to stop at the region
3428 start or end because the face might change there. */
3429 if (it->region_beg_charpos > 0)
3431 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3432 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3433 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3434 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3437 /* Set up variables for computing the stop position from text
3438 property changes. */
3439 XSETBUFFER (object, current_buffer);
3440 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3443 /* Get the interval containing IT's position. Value is a null
3444 interval if there isn't such an interval. */
3445 position = make_number (charpos);
3446 iv = validate_interval_range (object, &position, &position, 0);
3447 if (iv)
3449 Lisp_Object values_here[LAST_PROP_IDX];
3450 struct props *p;
3452 /* Get properties here. */
3453 for (p = it_props; p->handler; ++p)
3454 values_here[p->idx] = textget (iv->plist, *p->name);
3456 /* Look for an interval following iv that has different
3457 properties. */
3458 for (next_iv = next_interval (iv);
3459 (next_iv
3460 && (NILP (limit)
3461 || XFASTINT (limit) > next_iv->position));
3462 next_iv = next_interval (next_iv))
3464 for (p = it_props; p->handler; ++p)
3466 Lisp_Object new_value;
3468 new_value = textget (next_iv->plist, *p->name);
3469 if (!EQ (values_here[p->idx], new_value))
3470 break;
3473 if (p->handler)
3474 break;
3477 if (next_iv)
3479 if (INTEGERP (limit)
3480 && next_iv->position >= XFASTINT (limit))
3481 /* No text property change up to limit. */
3482 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3483 else
3484 /* Text properties change in next_iv. */
3485 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3489 if (it->cmp_it.id < 0)
3491 ptrdiff_t stoppos = it->end_charpos;
3493 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3494 stoppos = -1;
3495 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3496 stoppos, it->string);
3499 eassert (STRINGP (it->string)
3500 || (it->stop_charpos >= BEGV
3501 && it->stop_charpos >= IT_CHARPOS (*it)));
3505 /* Return the position of the next overlay change after POS in
3506 current_buffer. Value is point-max if no overlay change
3507 follows. This is like `next-overlay-change' but doesn't use
3508 xmalloc. */
3510 static ptrdiff_t
3511 next_overlay_change (ptrdiff_t pos)
3513 ptrdiff_t i, noverlays;
3514 ptrdiff_t endpos;
3515 Lisp_Object *overlays;
3517 /* Get all overlays at the given position. */
3518 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3520 /* If any of these overlays ends before endpos,
3521 use its ending point instead. */
3522 for (i = 0; i < noverlays; ++i)
3524 Lisp_Object oend;
3525 ptrdiff_t oendpos;
3527 oend = OVERLAY_END (overlays[i]);
3528 oendpos = OVERLAY_POSITION (oend);
3529 endpos = min (endpos, oendpos);
3532 return endpos;
3535 /* How many characters forward to search for a display property or
3536 display string. Searching too far forward makes the bidi display
3537 sluggish, especially in small windows. */
3538 #define MAX_DISP_SCAN 250
3540 /* Return the character position of a display string at or after
3541 position specified by POSITION. If no display string exists at or
3542 after POSITION, return ZV. A display string is either an overlay
3543 with `display' property whose value is a string, or a `display'
3544 text property whose value is a string. STRING is data about the
3545 string to iterate; if STRING->lstring is nil, we are iterating a
3546 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3547 on a GUI frame. DISP_PROP is set to zero if we searched
3548 MAX_DISP_SCAN characters forward without finding any display
3549 strings, non-zero otherwise. It is set to 2 if the display string
3550 uses any kind of `(space ...)' spec that will produce a stretch of
3551 white space in the text area. */
3552 ptrdiff_t
3553 compute_display_string_pos (struct text_pos *position,
3554 struct bidi_string_data *string,
3555 struct window *w,
3556 int frame_window_p, int *disp_prop)
3558 /* OBJECT = nil means current buffer. */
3559 Lisp_Object object, object1;
3560 Lisp_Object pos, spec, limpos;
3561 int string_p = (string && (STRINGP (string->lstring) || string->s));
3562 ptrdiff_t eob = string_p ? string->schars : ZV;
3563 ptrdiff_t begb = string_p ? 0 : BEGV;
3564 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3565 ptrdiff_t lim =
3566 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3567 struct text_pos tpos;
3568 int rv = 0;
3570 if (string && STRINGP (string->lstring))
3571 object1 = object = string->lstring;
3572 else if (w && !string_p)
3574 XSETWINDOW (object, w);
3575 object1 = Qnil;
3577 else
3578 object1 = object = Qnil;
3580 *disp_prop = 1;
3582 if (charpos >= eob
3583 /* We don't support display properties whose values are strings
3584 that have display string properties. */
3585 || string->from_disp_str
3586 /* C strings cannot have display properties. */
3587 || (string->s && !STRINGP (object)))
3589 *disp_prop = 0;
3590 return eob;
3593 /* If the character at CHARPOS is where the display string begins,
3594 return CHARPOS. */
3595 pos = make_number (charpos);
3596 if (STRINGP (object))
3597 bufpos = string->bufpos;
3598 else
3599 bufpos = charpos;
3600 tpos = *position;
3601 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3602 && (charpos <= begb
3603 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3604 object),
3605 spec))
3606 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3607 frame_window_p)))
3609 if (rv == 2)
3610 *disp_prop = 2;
3611 return charpos;
3614 /* Look forward for the first character with a `display' property
3615 that will replace the underlying text when displayed. */
3616 limpos = make_number (lim);
3617 do {
3618 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3619 CHARPOS (tpos) = XFASTINT (pos);
3620 if (CHARPOS (tpos) >= lim)
3622 *disp_prop = 0;
3623 break;
3625 if (STRINGP (object))
3626 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3627 else
3628 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3629 spec = Fget_char_property (pos, Qdisplay, object);
3630 if (!STRINGP (object))
3631 bufpos = CHARPOS (tpos);
3632 } while (NILP (spec)
3633 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3634 bufpos, frame_window_p)));
3635 if (rv == 2)
3636 *disp_prop = 2;
3638 return CHARPOS (tpos);
3641 /* Return the character position of the end of the display string that
3642 started at CHARPOS. If there's no display string at CHARPOS,
3643 return -1. A display string is either an overlay with `display'
3644 property whose value is a string or a `display' text property whose
3645 value is a string. */
3646 ptrdiff_t
3647 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3649 /* OBJECT = nil means current buffer. */
3650 Lisp_Object object =
3651 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3652 Lisp_Object pos = make_number (charpos);
3653 ptrdiff_t eob =
3654 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3656 if (charpos >= eob || (string->s && !STRINGP (object)))
3657 return eob;
3659 /* It could happen that the display property or overlay was removed
3660 since we found it in compute_display_string_pos above. One way
3661 this can happen is if JIT font-lock was called (through
3662 handle_fontified_prop), and jit-lock-functions remove text
3663 properties or overlays from the portion of buffer that includes
3664 CHARPOS. Muse mode is known to do that, for example. In this
3665 case, we return -1 to the caller, to signal that no display
3666 string is actually present at CHARPOS. See bidi_fetch_char for
3667 how this is handled.
3669 An alternative would be to never look for display properties past
3670 it->stop_charpos. But neither compute_display_string_pos nor
3671 bidi_fetch_char that calls it know or care where the next
3672 stop_charpos is. */
3673 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3674 return -1;
3676 /* Look forward for the first character where the `display' property
3677 changes. */
3678 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3680 return XFASTINT (pos);
3685 /***********************************************************************
3686 Fontification
3687 ***********************************************************************/
3689 /* Handle changes in the `fontified' property of the current buffer by
3690 calling hook functions from Qfontification_functions to fontify
3691 regions of text. */
3693 static enum prop_handled
3694 handle_fontified_prop (struct it *it)
3696 Lisp_Object prop, pos;
3697 enum prop_handled handled = HANDLED_NORMALLY;
3699 if (!NILP (Vmemory_full))
3700 return handled;
3702 /* Get the value of the `fontified' property at IT's current buffer
3703 position. (The `fontified' property doesn't have a special
3704 meaning in strings.) If the value is nil, call functions from
3705 Qfontification_functions. */
3706 if (!STRINGP (it->string)
3707 && it->s == NULL
3708 && !NILP (Vfontification_functions)
3709 && !NILP (Vrun_hooks)
3710 && (pos = make_number (IT_CHARPOS (*it)),
3711 prop = Fget_char_property (pos, Qfontified, Qnil),
3712 /* Ignore the special cased nil value always present at EOB since
3713 no amount of fontifying will be able to change it. */
3714 NILP (prop) && IT_CHARPOS (*it) < Z))
3716 ptrdiff_t count = SPECPDL_INDEX ();
3717 Lisp_Object val;
3718 struct buffer *obuf = current_buffer;
3719 int begv = BEGV, zv = ZV;
3720 int old_clip_changed = current_buffer->clip_changed;
3722 val = Vfontification_functions;
3723 specbind (Qfontification_functions, Qnil);
3725 eassert (it->end_charpos == ZV);
3727 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3728 safe_call1 (val, pos);
3729 else
3731 Lisp_Object fns, fn;
3732 struct gcpro gcpro1, gcpro2;
3734 fns = Qnil;
3735 GCPRO2 (val, fns);
3737 for (; CONSP (val); val = XCDR (val))
3739 fn = XCAR (val);
3741 if (EQ (fn, Qt))
3743 /* A value of t indicates this hook has a local
3744 binding; it means to run the global binding too.
3745 In a global value, t should not occur. If it
3746 does, we must ignore it to avoid an endless
3747 loop. */
3748 for (fns = Fdefault_value (Qfontification_functions);
3749 CONSP (fns);
3750 fns = XCDR (fns))
3752 fn = XCAR (fns);
3753 if (!EQ (fn, Qt))
3754 safe_call1 (fn, pos);
3757 else
3758 safe_call1 (fn, pos);
3761 UNGCPRO;
3764 unbind_to (count, Qnil);
3766 /* Fontification functions routinely call `save-restriction'.
3767 Normally, this tags clip_changed, which can confuse redisplay
3768 (see discussion in Bug#6671). Since we don't perform any
3769 special handling of fontification changes in the case where
3770 `save-restriction' isn't called, there's no point doing so in
3771 this case either. So, if the buffer's restrictions are
3772 actually left unchanged, reset clip_changed. */
3773 if (obuf == current_buffer)
3775 if (begv == BEGV && zv == ZV)
3776 current_buffer->clip_changed = old_clip_changed;
3778 /* There isn't much we can reasonably do to protect against
3779 misbehaving fontification, but here's a fig leaf. */
3780 else if (BUFFER_LIVE_P (obuf))
3781 set_buffer_internal_1 (obuf);
3783 /* The fontification code may have added/removed text.
3784 It could do even a lot worse, but let's at least protect against
3785 the most obvious case where only the text past `pos' gets changed',
3786 as is/was done in grep.el where some escapes sequences are turned
3787 into face properties (bug#7876). */
3788 it->end_charpos = ZV;
3790 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3791 something. This avoids an endless loop if they failed to
3792 fontify the text for which reason ever. */
3793 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3794 handled = HANDLED_RECOMPUTE_PROPS;
3797 return handled;
3802 /***********************************************************************
3803 Faces
3804 ***********************************************************************/
3806 /* Set up iterator IT from face properties at its current position.
3807 Called from handle_stop. */
3809 static enum prop_handled
3810 handle_face_prop (struct it *it)
3812 int new_face_id;
3813 ptrdiff_t next_stop;
3815 if (!STRINGP (it->string))
3817 new_face_id
3818 = face_at_buffer_position (it->w,
3819 IT_CHARPOS (*it),
3820 it->region_beg_charpos,
3821 it->region_end_charpos,
3822 &next_stop,
3823 (IT_CHARPOS (*it)
3824 + TEXT_PROP_DISTANCE_LIMIT),
3825 0, it->base_face_id);
3827 /* Is this a start of a run of characters with box face?
3828 Caveat: this can be called for a freshly initialized
3829 iterator; face_id is -1 in this case. We know that the new
3830 face will not change until limit, i.e. if the new face has a
3831 box, all characters up to limit will have one. But, as
3832 usual, we don't know whether limit is really the end. */
3833 if (new_face_id != it->face_id)
3835 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3836 /* If it->face_id is -1, old_face below will be NULL, see
3837 the definition of FACE_FROM_ID. This will happen if this
3838 is the initial call that gets the face. */
3839 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3841 /* If the value of face_id of the iterator is -1, we have to
3842 look in front of IT's position and see whether there is a
3843 face there that's different from new_face_id. */
3844 if (!old_face && IT_CHARPOS (*it) > BEG)
3846 int prev_face_id = face_before_it_pos (it);
3848 old_face = FACE_FROM_ID (it->f, prev_face_id);
3851 /* If the new face has a box, but the old face does not,
3852 this is the start of a run of characters with box face,
3853 i.e. this character has a shadow on the left side. */
3854 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3855 && (old_face == NULL || !old_face->box));
3856 it->face_box_p = new_face->box != FACE_NO_BOX;
3859 else
3861 int base_face_id;
3862 ptrdiff_t bufpos;
3863 int i;
3864 Lisp_Object from_overlay
3865 = (it->current.overlay_string_index >= 0
3866 ? it->string_overlays[it->current.overlay_string_index
3867 % OVERLAY_STRING_CHUNK_SIZE]
3868 : Qnil);
3870 /* See if we got to this string directly or indirectly from
3871 an overlay property. That includes the before-string or
3872 after-string of an overlay, strings in display properties
3873 provided by an overlay, their text properties, etc.
3875 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3876 if (! NILP (from_overlay))
3877 for (i = it->sp - 1; i >= 0; i--)
3879 if (it->stack[i].current.overlay_string_index >= 0)
3880 from_overlay
3881 = it->string_overlays[it->stack[i].current.overlay_string_index
3882 % OVERLAY_STRING_CHUNK_SIZE];
3883 else if (! NILP (it->stack[i].from_overlay))
3884 from_overlay = it->stack[i].from_overlay;
3886 if (!NILP (from_overlay))
3887 break;
3890 if (! NILP (from_overlay))
3892 bufpos = IT_CHARPOS (*it);
3893 /* For a string from an overlay, the base face depends
3894 only on text properties and ignores overlays. */
3895 base_face_id
3896 = face_for_overlay_string (it->w,
3897 IT_CHARPOS (*it),
3898 it->region_beg_charpos,
3899 it->region_end_charpos,
3900 &next_stop,
3901 (IT_CHARPOS (*it)
3902 + TEXT_PROP_DISTANCE_LIMIT),
3904 from_overlay);
3906 else
3908 bufpos = 0;
3910 /* For strings from a `display' property, use the face at
3911 IT's current buffer position as the base face to merge
3912 with, so that overlay strings appear in the same face as
3913 surrounding text, unless they specify their own
3914 faces. */
3915 base_face_id = it->string_from_prefix_prop_p
3916 ? DEFAULT_FACE_ID
3917 : underlying_face_id (it);
3920 new_face_id = face_at_string_position (it->w,
3921 it->string,
3922 IT_STRING_CHARPOS (*it),
3923 bufpos,
3924 it->region_beg_charpos,
3925 it->region_end_charpos,
3926 &next_stop,
3927 base_face_id, 0);
3929 /* Is this a start of a run of characters with box? Caveat:
3930 this can be called for a freshly allocated iterator; face_id
3931 is -1 is this case. We know that the new face will not
3932 change until the next check pos, i.e. if the new face has a
3933 box, all characters up to that position will have a
3934 box. But, as usual, we don't know whether that position
3935 is really the end. */
3936 if (new_face_id != it->face_id)
3938 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3939 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3941 /* If new face has a box but old face hasn't, this is the
3942 start of a run of characters with box, i.e. it has a
3943 shadow on the left side. */
3944 it->start_of_box_run_p
3945 = new_face->box && (old_face == NULL || !old_face->box);
3946 it->face_box_p = new_face->box != FACE_NO_BOX;
3950 it->face_id = new_face_id;
3951 return HANDLED_NORMALLY;
3955 /* Return the ID of the face ``underlying'' IT's current position,
3956 which is in a string. If the iterator is associated with a
3957 buffer, return the face at IT's current buffer position.
3958 Otherwise, use the iterator's base_face_id. */
3960 static int
3961 underlying_face_id (struct it *it)
3963 int face_id = it->base_face_id, i;
3965 eassert (STRINGP (it->string));
3967 for (i = it->sp - 1; i >= 0; --i)
3968 if (NILP (it->stack[i].string))
3969 face_id = it->stack[i].face_id;
3971 return face_id;
3975 /* Compute the face one character before or after the current position
3976 of IT, in the visual order. BEFORE_P non-zero means get the face
3977 in front (to the left in L2R paragraphs, to the right in R2L
3978 paragraphs) of IT's screen position. Value is the ID of the face. */
3980 static int
3981 face_before_or_after_it_pos (struct it *it, int before_p)
3983 int face_id, limit;
3984 ptrdiff_t next_check_charpos;
3985 struct it it_copy;
3986 void *it_copy_data = NULL;
3988 eassert (it->s == NULL);
3990 if (STRINGP (it->string))
3992 ptrdiff_t bufpos, charpos;
3993 int base_face_id;
3995 /* No face change past the end of the string (for the case
3996 we are padding with spaces). No face change before the
3997 string start. */
3998 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3999 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4000 return it->face_id;
4002 if (!it->bidi_p)
4004 /* Set charpos to the position before or after IT's current
4005 position, in the logical order, which in the non-bidi
4006 case is the same as the visual order. */
4007 if (before_p)
4008 charpos = IT_STRING_CHARPOS (*it) - 1;
4009 else if (it->what == IT_COMPOSITION)
4010 /* For composition, we must check the character after the
4011 composition. */
4012 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4013 else
4014 charpos = IT_STRING_CHARPOS (*it) + 1;
4016 else
4018 if (before_p)
4020 /* With bidi iteration, the character before the current
4021 in the visual order cannot be found by simple
4022 iteration, because "reverse" reordering is not
4023 supported. Instead, we need to use the move_it_*
4024 family of functions. */
4025 /* Ignore face changes before the first visible
4026 character on this display line. */
4027 if (it->current_x <= it->first_visible_x)
4028 return it->face_id;
4029 SAVE_IT (it_copy, *it, it_copy_data);
4030 /* Implementation note: Since move_it_in_display_line
4031 works in the iterator geometry, and thinks the first
4032 character is always the leftmost, even in R2L lines,
4033 we don't need to distinguish between the R2L and L2R
4034 cases here. */
4035 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4036 it_copy.current_x - 1, MOVE_TO_X);
4037 charpos = IT_STRING_CHARPOS (it_copy);
4038 RESTORE_IT (it, it, it_copy_data);
4040 else
4042 /* Set charpos to the string position of the character
4043 that comes after IT's current position in the visual
4044 order. */
4045 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4047 it_copy = *it;
4048 while (n--)
4049 bidi_move_to_visually_next (&it_copy.bidi_it);
4051 charpos = it_copy.bidi_it.charpos;
4054 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4056 if (it->current.overlay_string_index >= 0)
4057 bufpos = IT_CHARPOS (*it);
4058 else
4059 bufpos = 0;
4061 base_face_id = underlying_face_id (it);
4063 /* Get the face for ASCII, or unibyte. */
4064 face_id = face_at_string_position (it->w,
4065 it->string,
4066 charpos,
4067 bufpos,
4068 it->region_beg_charpos,
4069 it->region_end_charpos,
4070 &next_check_charpos,
4071 base_face_id, 0);
4073 /* Correct the face for charsets different from ASCII. Do it
4074 for the multibyte case only. The face returned above is
4075 suitable for unibyte text if IT->string is unibyte. */
4076 if (STRING_MULTIBYTE (it->string))
4078 struct text_pos pos1 = string_pos (charpos, it->string);
4079 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4080 int c, len;
4081 struct face *face = FACE_FROM_ID (it->f, face_id);
4083 c = string_char_and_length (p, &len);
4084 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4087 else
4089 struct text_pos pos;
4091 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4092 || (IT_CHARPOS (*it) <= BEGV && before_p))
4093 return it->face_id;
4095 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4096 pos = it->current.pos;
4098 if (!it->bidi_p)
4100 if (before_p)
4101 DEC_TEXT_POS (pos, it->multibyte_p);
4102 else
4104 if (it->what == IT_COMPOSITION)
4106 /* For composition, we must check the position after
4107 the composition. */
4108 pos.charpos += it->cmp_it.nchars;
4109 pos.bytepos += it->len;
4111 else
4112 INC_TEXT_POS (pos, it->multibyte_p);
4115 else
4117 if (before_p)
4119 /* With bidi iteration, the character before the current
4120 in the visual order cannot be found by simple
4121 iteration, because "reverse" reordering is not
4122 supported. Instead, we need to use the move_it_*
4123 family of functions. */
4124 /* Ignore face changes before the first visible
4125 character on this display line. */
4126 if (it->current_x <= it->first_visible_x)
4127 return it->face_id;
4128 SAVE_IT (it_copy, *it, it_copy_data);
4129 /* Implementation note: Since move_it_in_display_line
4130 works in the iterator geometry, and thinks the first
4131 character is always the leftmost, even in R2L lines,
4132 we don't need to distinguish between the R2L and L2R
4133 cases here. */
4134 move_it_in_display_line (&it_copy, ZV,
4135 it_copy.current_x - 1, MOVE_TO_X);
4136 pos = it_copy.current.pos;
4137 RESTORE_IT (it, it, it_copy_data);
4139 else
4141 /* Set charpos to the buffer position of the character
4142 that comes after IT's current position in the visual
4143 order. */
4144 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4146 it_copy = *it;
4147 while (n--)
4148 bidi_move_to_visually_next (&it_copy.bidi_it);
4150 SET_TEXT_POS (pos,
4151 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4154 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4156 /* Determine face for CHARSET_ASCII, or unibyte. */
4157 face_id = face_at_buffer_position (it->w,
4158 CHARPOS (pos),
4159 it->region_beg_charpos,
4160 it->region_end_charpos,
4161 &next_check_charpos,
4162 limit, 0, -1);
4164 /* Correct the face for charsets different from ASCII. Do it
4165 for the multibyte case only. The face returned above is
4166 suitable for unibyte text if current_buffer is unibyte. */
4167 if (it->multibyte_p)
4169 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4170 struct face *face = FACE_FROM_ID (it->f, face_id);
4171 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4175 return face_id;
4180 /***********************************************************************
4181 Invisible text
4182 ***********************************************************************/
4184 /* Set up iterator IT from invisible properties at its current
4185 position. Called from handle_stop. */
4187 static enum prop_handled
4188 handle_invisible_prop (struct it *it)
4190 enum prop_handled handled = HANDLED_NORMALLY;
4191 int invis_p;
4192 Lisp_Object prop;
4194 if (STRINGP (it->string))
4196 Lisp_Object end_charpos, limit, charpos;
4198 /* Get the value of the invisible text property at the
4199 current position. Value will be nil if there is no such
4200 property. */
4201 charpos = make_number (IT_STRING_CHARPOS (*it));
4202 prop = Fget_text_property (charpos, Qinvisible, it->string);
4203 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4205 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4207 /* Record whether we have to display an ellipsis for the
4208 invisible text. */
4209 int display_ellipsis_p = (invis_p == 2);
4210 ptrdiff_t len, endpos;
4212 handled = HANDLED_RECOMPUTE_PROPS;
4214 /* Get the position at which the next visible text can be
4215 found in IT->string, if any. */
4216 endpos = len = SCHARS (it->string);
4217 XSETINT (limit, len);
4220 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4221 it->string, limit);
4222 if (INTEGERP (end_charpos))
4224 endpos = XFASTINT (end_charpos);
4225 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4226 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4227 if (invis_p == 2)
4228 display_ellipsis_p = 1;
4231 while (invis_p && endpos < len);
4233 if (display_ellipsis_p)
4234 it->ellipsis_p = 1;
4236 if (endpos < len)
4238 /* Text at END_CHARPOS is visible. Move IT there. */
4239 struct text_pos old;
4240 ptrdiff_t oldpos;
4242 old = it->current.string_pos;
4243 oldpos = CHARPOS (old);
4244 if (it->bidi_p)
4246 if (it->bidi_it.first_elt
4247 && it->bidi_it.charpos < SCHARS (it->string))
4248 bidi_paragraph_init (it->paragraph_embedding,
4249 &it->bidi_it, 1);
4250 /* Bidi-iterate out of the invisible text. */
4253 bidi_move_to_visually_next (&it->bidi_it);
4255 while (oldpos <= it->bidi_it.charpos
4256 && it->bidi_it.charpos < endpos);
4258 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4259 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4260 if (IT_CHARPOS (*it) >= endpos)
4261 it->prev_stop = endpos;
4263 else
4265 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4266 compute_string_pos (&it->current.string_pos, old, it->string);
4269 else
4271 /* The rest of the string is invisible. If this is an
4272 overlay string, proceed with the next overlay string
4273 or whatever comes and return a character from there. */
4274 if (it->current.overlay_string_index >= 0
4275 && !display_ellipsis_p)
4277 next_overlay_string (it);
4278 /* Don't check for overlay strings when we just
4279 finished processing them. */
4280 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4282 else
4284 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4285 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4290 else
4292 ptrdiff_t newpos, next_stop, start_charpos, tem;
4293 Lisp_Object pos, overlay;
4295 /* First of all, is there invisible text at this position? */
4296 tem = start_charpos = IT_CHARPOS (*it);
4297 pos = make_number (tem);
4298 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4299 &overlay);
4300 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4302 /* If we are on invisible text, skip over it. */
4303 if (invis_p && start_charpos < it->end_charpos)
4305 /* Record whether we have to display an ellipsis for the
4306 invisible text. */
4307 int display_ellipsis_p = invis_p == 2;
4309 handled = HANDLED_RECOMPUTE_PROPS;
4311 /* Loop skipping over invisible text. The loop is left at
4312 ZV or with IT on the first char being visible again. */
4315 /* Try to skip some invisible text. Return value is the
4316 position reached which can be equal to where we start
4317 if there is nothing invisible there. This skips both
4318 over invisible text properties and overlays with
4319 invisible property. */
4320 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4322 /* If we skipped nothing at all we weren't at invisible
4323 text in the first place. If everything to the end of
4324 the buffer was skipped, end the loop. */
4325 if (newpos == tem || newpos >= ZV)
4326 invis_p = 0;
4327 else
4329 /* We skipped some characters but not necessarily
4330 all there are. Check if we ended up on visible
4331 text. Fget_char_property returns the property of
4332 the char before the given position, i.e. if we
4333 get invis_p = 0, this means that the char at
4334 newpos is visible. */
4335 pos = make_number (newpos);
4336 prop = Fget_char_property (pos, Qinvisible, it->window);
4337 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4340 /* If we ended up on invisible text, proceed to
4341 skip starting with next_stop. */
4342 if (invis_p)
4343 tem = next_stop;
4345 /* If there are adjacent invisible texts, don't lose the
4346 second one's ellipsis. */
4347 if (invis_p == 2)
4348 display_ellipsis_p = 1;
4350 while (invis_p);
4352 /* The position newpos is now either ZV or on visible text. */
4353 if (it->bidi_p)
4355 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4356 int on_newline =
4357 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4358 int after_newline =
4359 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4361 /* If the invisible text ends on a newline or on a
4362 character after a newline, we can avoid the costly,
4363 character by character, bidi iteration to NEWPOS, and
4364 instead simply reseat the iterator there. That's
4365 because all bidi reordering information is tossed at
4366 the newline. This is a big win for modes that hide
4367 complete lines, like Outline, Org, etc. */
4368 if (on_newline || after_newline)
4370 struct text_pos tpos;
4371 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4373 SET_TEXT_POS (tpos, newpos, bpos);
4374 reseat_1 (it, tpos, 0);
4375 /* If we reseat on a newline/ZV, we need to prep the
4376 bidi iterator for advancing to the next character
4377 after the newline/EOB, keeping the current paragraph
4378 direction (so that PRODUCE_GLYPHS does TRT wrt
4379 prepending/appending glyphs to a glyph row). */
4380 if (on_newline)
4382 it->bidi_it.first_elt = 0;
4383 it->bidi_it.paragraph_dir = pdir;
4384 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4385 it->bidi_it.nchars = 1;
4386 it->bidi_it.ch_len = 1;
4389 else /* Must use the slow method. */
4391 /* With bidi iteration, the region of invisible text
4392 could start and/or end in the middle of a
4393 non-base embedding level. Therefore, we need to
4394 skip invisible text using the bidi iterator,
4395 starting at IT's current position, until we find
4396 ourselves outside of the invisible text.
4397 Skipping invisible text _after_ bidi iteration
4398 avoids affecting the visual order of the
4399 displayed text when invisible properties are
4400 added or removed. */
4401 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4403 /* If we were `reseat'ed to a new paragraph,
4404 determine the paragraph base direction. We
4405 need to do it now because
4406 next_element_from_buffer may not have a
4407 chance to do it, if we are going to skip any
4408 text at the beginning, which resets the
4409 FIRST_ELT flag. */
4410 bidi_paragraph_init (it->paragraph_embedding,
4411 &it->bidi_it, 1);
4415 bidi_move_to_visually_next (&it->bidi_it);
4417 while (it->stop_charpos <= it->bidi_it.charpos
4418 && it->bidi_it.charpos < newpos);
4419 IT_CHARPOS (*it) = it->bidi_it.charpos;
4420 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4421 /* If we overstepped NEWPOS, record its position in
4422 the iterator, so that we skip invisible text if
4423 later the bidi iteration lands us in the
4424 invisible region again. */
4425 if (IT_CHARPOS (*it) >= newpos)
4426 it->prev_stop = newpos;
4429 else
4431 IT_CHARPOS (*it) = newpos;
4432 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4435 /* If there are before-strings at the start of invisible
4436 text, and the text is invisible because of a text
4437 property, arrange to show before-strings because 20.x did
4438 it that way. (If the text is invisible because of an
4439 overlay property instead of a text property, this is
4440 already handled in the overlay code.) */
4441 if (NILP (overlay)
4442 && get_overlay_strings (it, it->stop_charpos))
4444 handled = HANDLED_RECOMPUTE_PROPS;
4445 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4447 else if (display_ellipsis_p)
4449 /* Make sure that the glyphs of the ellipsis will get
4450 correct `charpos' values. If we would not update
4451 it->position here, the glyphs would belong to the
4452 last visible character _before_ the invisible
4453 text, which confuses `set_cursor_from_row'.
4455 We use the last invisible position instead of the
4456 first because this way the cursor is always drawn on
4457 the first "." of the ellipsis, whenever PT is inside
4458 the invisible text. Otherwise the cursor would be
4459 placed _after_ the ellipsis when the point is after the
4460 first invisible character. */
4461 if (!STRINGP (it->object))
4463 it->position.charpos = newpos - 1;
4464 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4466 it->ellipsis_p = 1;
4467 /* Let the ellipsis display before
4468 considering any properties of the following char.
4469 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4470 handled = HANDLED_RETURN;
4475 return handled;
4479 /* Make iterator IT return `...' next.
4480 Replaces LEN characters from buffer. */
4482 static void
4483 setup_for_ellipsis (struct it *it, int len)
4485 /* Use the display table definition for `...'. Invalid glyphs
4486 will be handled by the method returning elements from dpvec. */
4487 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4489 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4490 it->dpvec = v->contents;
4491 it->dpend = v->contents + v->header.size;
4493 else
4495 /* Default `...'. */
4496 it->dpvec = default_invis_vector;
4497 it->dpend = default_invis_vector + 3;
4500 it->dpvec_char_len = len;
4501 it->current.dpvec_index = 0;
4502 it->dpvec_face_id = -1;
4504 /* Remember the current face id in case glyphs specify faces.
4505 IT's face is restored in set_iterator_to_next.
4506 saved_face_id was set to preceding char's face in handle_stop. */
4507 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4508 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4510 it->method = GET_FROM_DISPLAY_VECTOR;
4511 it->ellipsis_p = 1;
4516 /***********************************************************************
4517 'display' property
4518 ***********************************************************************/
4520 /* Set up iterator IT from `display' property at its current position.
4521 Called from handle_stop.
4522 We return HANDLED_RETURN if some part of the display property
4523 overrides the display of the buffer text itself.
4524 Otherwise we return HANDLED_NORMALLY. */
4526 static enum prop_handled
4527 handle_display_prop (struct it *it)
4529 Lisp_Object propval, object, overlay;
4530 struct text_pos *position;
4531 ptrdiff_t bufpos;
4532 /* Nonzero if some property replaces the display of the text itself. */
4533 int display_replaced_p = 0;
4535 if (STRINGP (it->string))
4537 object = it->string;
4538 position = &it->current.string_pos;
4539 bufpos = CHARPOS (it->current.pos);
4541 else
4543 XSETWINDOW (object, it->w);
4544 position = &it->current.pos;
4545 bufpos = CHARPOS (*position);
4548 /* Reset those iterator values set from display property values. */
4549 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4550 it->space_width = Qnil;
4551 it->font_height = Qnil;
4552 it->voffset = 0;
4554 /* We don't support recursive `display' properties, i.e. string
4555 values that have a string `display' property, that have a string
4556 `display' property etc. */
4557 if (!it->string_from_display_prop_p)
4558 it->area = TEXT_AREA;
4560 propval = get_char_property_and_overlay (make_number (position->charpos),
4561 Qdisplay, object, &overlay);
4562 if (NILP (propval))
4563 return HANDLED_NORMALLY;
4564 /* Now OVERLAY is the overlay that gave us this property, or nil
4565 if it was a text property. */
4567 if (!STRINGP (it->string))
4568 object = it->w->contents;
4570 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4571 position, bufpos,
4572 FRAME_WINDOW_P (it->f));
4574 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4577 /* Subroutine of handle_display_prop. Returns non-zero if the display
4578 specification in SPEC is a replacing specification, i.e. it would
4579 replace the text covered by `display' property with something else,
4580 such as an image or a display string. If SPEC includes any kind or
4581 `(space ...) specification, the value is 2; this is used by
4582 compute_display_string_pos, which see.
4584 See handle_single_display_spec for documentation of arguments.
4585 frame_window_p is non-zero if the window being redisplayed is on a
4586 GUI frame; this argument is used only if IT is NULL, see below.
4588 IT can be NULL, if this is called by the bidi reordering code
4589 through compute_display_string_pos, which see. In that case, this
4590 function only examines SPEC, but does not otherwise "handle" it, in
4591 the sense that it doesn't set up members of IT from the display
4592 spec. */
4593 static int
4594 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4595 Lisp_Object overlay, struct text_pos *position,
4596 ptrdiff_t bufpos, int frame_window_p)
4598 int replacing_p = 0;
4599 int rv;
4601 if (CONSP (spec)
4602 /* Simple specifications. */
4603 && !EQ (XCAR (spec), Qimage)
4604 && !EQ (XCAR (spec), Qspace)
4605 && !EQ (XCAR (spec), Qwhen)
4606 && !EQ (XCAR (spec), Qslice)
4607 && !EQ (XCAR (spec), Qspace_width)
4608 && !EQ (XCAR (spec), Qheight)
4609 && !EQ (XCAR (spec), Qraise)
4610 /* Marginal area specifications. */
4611 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4612 && !EQ (XCAR (spec), Qleft_fringe)
4613 && !EQ (XCAR (spec), Qright_fringe)
4614 && !NILP (XCAR (spec)))
4616 for (; CONSP (spec); spec = XCDR (spec))
4618 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4619 overlay, position, bufpos,
4620 replacing_p, frame_window_p)))
4622 replacing_p = rv;
4623 /* If some text in a string is replaced, `position' no
4624 longer points to the position of `object'. */
4625 if (!it || STRINGP (object))
4626 break;
4630 else if (VECTORP (spec))
4632 ptrdiff_t i;
4633 for (i = 0; i < ASIZE (spec); ++i)
4634 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4635 overlay, position, bufpos,
4636 replacing_p, frame_window_p)))
4638 replacing_p = rv;
4639 /* If some text in a string is replaced, `position' no
4640 longer points to the position of `object'. */
4641 if (!it || STRINGP (object))
4642 break;
4645 else
4647 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4648 position, bufpos, 0,
4649 frame_window_p)))
4650 replacing_p = rv;
4653 return replacing_p;
4656 /* Value is the position of the end of the `display' property starting
4657 at START_POS in OBJECT. */
4659 static struct text_pos
4660 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4662 Lisp_Object end;
4663 struct text_pos end_pos;
4665 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4666 Qdisplay, object, Qnil);
4667 CHARPOS (end_pos) = XFASTINT (end);
4668 if (STRINGP (object))
4669 compute_string_pos (&end_pos, start_pos, it->string);
4670 else
4671 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4673 return end_pos;
4677 /* Set up IT from a single `display' property specification SPEC. OBJECT
4678 is the object in which the `display' property was found. *POSITION
4679 is the position in OBJECT at which the `display' property was found.
4680 BUFPOS is the buffer position of OBJECT (different from POSITION if
4681 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4682 previously saw a display specification which already replaced text
4683 display with something else, for example an image; we ignore such
4684 properties after the first one has been processed.
4686 OVERLAY is the overlay this `display' property came from,
4687 or nil if it was a text property.
4689 If SPEC is a `space' or `image' specification, and in some other
4690 cases too, set *POSITION to the position where the `display'
4691 property ends.
4693 If IT is NULL, only examine the property specification in SPEC, but
4694 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4695 is intended to be displayed in a window on a GUI frame.
4697 Value is non-zero if something was found which replaces the display
4698 of buffer or string text. */
4700 static int
4701 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4702 Lisp_Object overlay, struct text_pos *position,
4703 ptrdiff_t bufpos, int display_replaced_p,
4704 int frame_window_p)
4706 Lisp_Object form;
4707 Lisp_Object location, value;
4708 struct text_pos start_pos = *position;
4709 int valid_p;
4711 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4712 If the result is non-nil, use VALUE instead of SPEC. */
4713 form = Qt;
4714 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4716 spec = XCDR (spec);
4717 if (!CONSP (spec))
4718 return 0;
4719 form = XCAR (spec);
4720 spec = XCDR (spec);
4723 if (!NILP (form) && !EQ (form, Qt))
4725 ptrdiff_t count = SPECPDL_INDEX ();
4726 struct gcpro gcpro1;
4728 /* Bind `object' to the object having the `display' property, a
4729 buffer or string. Bind `position' to the position in the
4730 object where the property was found, and `buffer-position'
4731 to the current position in the buffer. */
4733 if (NILP (object))
4734 XSETBUFFER (object, current_buffer);
4735 specbind (Qobject, object);
4736 specbind (Qposition, make_number (CHARPOS (*position)));
4737 specbind (Qbuffer_position, make_number (bufpos));
4738 GCPRO1 (form);
4739 form = safe_eval (form);
4740 UNGCPRO;
4741 unbind_to (count, Qnil);
4744 if (NILP (form))
4745 return 0;
4747 /* Handle `(height HEIGHT)' specifications. */
4748 if (CONSP (spec)
4749 && EQ (XCAR (spec), Qheight)
4750 && CONSP (XCDR (spec)))
4752 if (it)
4754 if (!FRAME_WINDOW_P (it->f))
4755 return 0;
4757 it->font_height = XCAR (XCDR (spec));
4758 if (!NILP (it->font_height))
4760 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4761 int new_height = -1;
4763 if (CONSP (it->font_height)
4764 && (EQ (XCAR (it->font_height), Qplus)
4765 || EQ (XCAR (it->font_height), Qminus))
4766 && CONSP (XCDR (it->font_height))
4767 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4769 /* `(+ N)' or `(- N)' where N is an integer. */
4770 int steps = XINT (XCAR (XCDR (it->font_height)));
4771 if (EQ (XCAR (it->font_height), Qplus))
4772 steps = - steps;
4773 it->face_id = smaller_face (it->f, it->face_id, steps);
4775 else if (FUNCTIONP (it->font_height))
4777 /* Call function with current height as argument.
4778 Value is the new height. */
4779 Lisp_Object height;
4780 height = safe_call1 (it->font_height,
4781 face->lface[LFACE_HEIGHT_INDEX]);
4782 if (NUMBERP (height))
4783 new_height = XFLOATINT (height);
4785 else if (NUMBERP (it->font_height))
4787 /* Value is a multiple of the canonical char height. */
4788 struct face *f;
4790 f = FACE_FROM_ID (it->f,
4791 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4792 new_height = (XFLOATINT (it->font_height)
4793 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4795 else
4797 /* Evaluate IT->font_height with `height' bound to the
4798 current specified height to get the new height. */
4799 ptrdiff_t count = SPECPDL_INDEX ();
4801 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4802 value = safe_eval (it->font_height);
4803 unbind_to (count, Qnil);
4805 if (NUMBERP (value))
4806 new_height = XFLOATINT (value);
4809 if (new_height > 0)
4810 it->face_id = face_with_height (it->f, it->face_id, new_height);
4814 return 0;
4817 /* Handle `(space-width WIDTH)'. */
4818 if (CONSP (spec)
4819 && EQ (XCAR (spec), Qspace_width)
4820 && CONSP (XCDR (spec)))
4822 if (it)
4824 if (!FRAME_WINDOW_P (it->f))
4825 return 0;
4827 value = XCAR (XCDR (spec));
4828 if (NUMBERP (value) && XFLOATINT (value) > 0)
4829 it->space_width = value;
4832 return 0;
4835 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4836 if (CONSP (spec)
4837 && EQ (XCAR (spec), Qslice))
4839 Lisp_Object tem;
4841 if (it)
4843 if (!FRAME_WINDOW_P (it->f))
4844 return 0;
4846 if (tem = XCDR (spec), CONSP (tem))
4848 it->slice.x = XCAR (tem);
4849 if (tem = XCDR (tem), CONSP (tem))
4851 it->slice.y = XCAR (tem);
4852 if (tem = XCDR (tem), CONSP (tem))
4854 it->slice.width = XCAR (tem);
4855 if (tem = XCDR (tem), CONSP (tem))
4856 it->slice.height = XCAR (tem);
4862 return 0;
4865 /* Handle `(raise FACTOR)'. */
4866 if (CONSP (spec)
4867 && EQ (XCAR (spec), Qraise)
4868 && CONSP (XCDR (spec)))
4870 if (it)
4872 if (!FRAME_WINDOW_P (it->f))
4873 return 0;
4875 #ifdef HAVE_WINDOW_SYSTEM
4876 value = XCAR (XCDR (spec));
4877 if (NUMBERP (value))
4879 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4880 it->voffset = - (XFLOATINT (value)
4881 * (FONT_HEIGHT (face->font)));
4883 #endif /* HAVE_WINDOW_SYSTEM */
4886 return 0;
4889 /* Don't handle the other kinds of display specifications
4890 inside a string that we got from a `display' property. */
4891 if (it && it->string_from_display_prop_p)
4892 return 0;
4894 /* Characters having this form of property are not displayed, so
4895 we have to find the end of the property. */
4896 if (it)
4898 start_pos = *position;
4899 *position = display_prop_end (it, object, start_pos);
4901 value = Qnil;
4903 /* Stop the scan at that end position--we assume that all
4904 text properties change there. */
4905 if (it)
4906 it->stop_charpos = position->charpos;
4908 /* Handle `(left-fringe BITMAP [FACE])'
4909 and `(right-fringe BITMAP [FACE])'. */
4910 if (CONSP (spec)
4911 && (EQ (XCAR (spec), Qleft_fringe)
4912 || EQ (XCAR (spec), Qright_fringe))
4913 && CONSP (XCDR (spec)))
4915 int fringe_bitmap;
4917 if (it)
4919 if (!FRAME_WINDOW_P (it->f))
4920 /* If we return here, POSITION has been advanced
4921 across the text with this property. */
4923 /* Synchronize the bidi iterator with POSITION. This is
4924 needed because we are not going to push the iterator
4925 on behalf of this display property, so there will be
4926 no pop_it call to do this synchronization for us. */
4927 if (it->bidi_p)
4929 it->position = *position;
4930 iterate_out_of_display_property (it);
4931 *position = it->position;
4933 return 1;
4936 else if (!frame_window_p)
4937 return 1;
4939 #ifdef HAVE_WINDOW_SYSTEM
4940 value = XCAR (XCDR (spec));
4941 if (!SYMBOLP (value)
4942 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4943 /* If we return here, POSITION has been advanced
4944 across the text with this property. */
4946 if (it && it->bidi_p)
4948 it->position = *position;
4949 iterate_out_of_display_property (it);
4950 *position = it->position;
4952 return 1;
4955 if (it)
4957 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4959 if (CONSP (XCDR (XCDR (spec))))
4961 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4962 int face_id2 = lookup_derived_face (it->f, face_name,
4963 FRINGE_FACE_ID, 0);
4964 if (face_id2 >= 0)
4965 face_id = face_id2;
4968 /* Save current settings of IT so that we can restore them
4969 when we are finished with the glyph property value. */
4970 push_it (it, position);
4972 it->area = TEXT_AREA;
4973 it->what = IT_IMAGE;
4974 it->image_id = -1; /* no image */
4975 it->position = start_pos;
4976 it->object = NILP (object) ? it->w->contents : object;
4977 it->method = GET_FROM_IMAGE;
4978 it->from_overlay = Qnil;
4979 it->face_id = face_id;
4980 it->from_disp_prop_p = 1;
4982 /* Say that we haven't consumed the characters with
4983 `display' property yet. The call to pop_it in
4984 set_iterator_to_next will clean this up. */
4985 *position = start_pos;
4987 if (EQ (XCAR (spec), Qleft_fringe))
4989 it->left_user_fringe_bitmap = fringe_bitmap;
4990 it->left_user_fringe_face_id = face_id;
4992 else
4994 it->right_user_fringe_bitmap = fringe_bitmap;
4995 it->right_user_fringe_face_id = face_id;
4998 #endif /* HAVE_WINDOW_SYSTEM */
4999 return 1;
5002 /* Prepare to handle `((margin left-margin) ...)',
5003 `((margin right-margin) ...)' and `((margin nil) ...)'
5004 prefixes for display specifications. */
5005 location = Qunbound;
5006 if (CONSP (spec) && CONSP (XCAR (spec)))
5008 Lisp_Object tem;
5010 value = XCDR (spec);
5011 if (CONSP (value))
5012 value = XCAR (value);
5014 tem = XCAR (spec);
5015 if (EQ (XCAR (tem), Qmargin)
5016 && (tem = XCDR (tem),
5017 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5018 (NILP (tem)
5019 || EQ (tem, Qleft_margin)
5020 || EQ (tem, Qright_margin))))
5021 location = tem;
5024 if (EQ (location, Qunbound))
5026 location = Qnil;
5027 value = spec;
5030 /* After this point, VALUE is the property after any
5031 margin prefix has been stripped. It must be a string,
5032 an image specification, or `(space ...)'.
5034 LOCATION specifies where to display: `left-margin',
5035 `right-margin' or nil. */
5037 valid_p = (STRINGP (value)
5038 #ifdef HAVE_WINDOW_SYSTEM
5039 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5040 && valid_image_p (value))
5041 #endif /* not HAVE_WINDOW_SYSTEM */
5042 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5044 if (valid_p && !display_replaced_p)
5046 int retval = 1;
5048 if (!it)
5050 /* Callers need to know whether the display spec is any kind
5051 of `(space ...)' spec that is about to affect text-area
5052 display. */
5053 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5054 retval = 2;
5055 return retval;
5058 /* Save current settings of IT so that we can restore them
5059 when we are finished with the glyph property value. */
5060 push_it (it, position);
5061 it->from_overlay = overlay;
5062 it->from_disp_prop_p = 1;
5064 if (NILP (location))
5065 it->area = TEXT_AREA;
5066 else if (EQ (location, Qleft_margin))
5067 it->area = LEFT_MARGIN_AREA;
5068 else
5069 it->area = RIGHT_MARGIN_AREA;
5071 if (STRINGP (value))
5073 it->string = value;
5074 it->multibyte_p = STRING_MULTIBYTE (it->string);
5075 it->current.overlay_string_index = -1;
5076 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5077 it->end_charpos = it->string_nchars = SCHARS (it->string);
5078 it->method = GET_FROM_STRING;
5079 it->stop_charpos = 0;
5080 it->prev_stop = 0;
5081 it->base_level_stop = 0;
5082 it->string_from_display_prop_p = 1;
5083 /* Say that we haven't consumed the characters with
5084 `display' property yet. The call to pop_it in
5085 set_iterator_to_next will clean this up. */
5086 if (BUFFERP (object))
5087 *position = start_pos;
5089 /* Force paragraph direction to be that of the parent
5090 object. If the parent object's paragraph direction is
5091 not yet determined, default to L2R. */
5092 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5093 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5094 else
5095 it->paragraph_embedding = L2R;
5097 /* Set up the bidi iterator for this display string. */
5098 if (it->bidi_p)
5100 it->bidi_it.string.lstring = it->string;
5101 it->bidi_it.string.s = NULL;
5102 it->bidi_it.string.schars = it->end_charpos;
5103 it->bidi_it.string.bufpos = bufpos;
5104 it->bidi_it.string.from_disp_str = 1;
5105 it->bidi_it.string.unibyte = !it->multibyte_p;
5106 it->bidi_it.w = it->w;
5107 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5110 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5112 it->method = GET_FROM_STRETCH;
5113 it->object = value;
5114 *position = it->position = start_pos;
5115 retval = 1 + (it->area == TEXT_AREA);
5117 #ifdef HAVE_WINDOW_SYSTEM
5118 else
5120 it->what = IT_IMAGE;
5121 it->image_id = lookup_image (it->f, value);
5122 it->position = start_pos;
5123 it->object = NILP (object) ? it->w->contents : object;
5124 it->method = GET_FROM_IMAGE;
5126 /* Say that we haven't consumed the characters with
5127 `display' property yet. The call to pop_it in
5128 set_iterator_to_next will clean this up. */
5129 *position = start_pos;
5131 #endif /* HAVE_WINDOW_SYSTEM */
5133 return retval;
5136 /* Invalid property or property not supported. Restore
5137 POSITION to what it was before. */
5138 *position = start_pos;
5139 return 0;
5142 /* Check if PROP is a display property value whose text should be
5143 treated as intangible. OVERLAY is the overlay from which PROP
5144 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5145 specify the buffer position covered by PROP. */
5148 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5149 ptrdiff_t charpos, ptrdiff_t bytepos)
5151 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5152 struct text_pos position;
5154 SET_TEXT_POS (position, charpos, bytepos);
5155 return handle_display_spec (NULL, prop, Qnil, overlay,
5156 &position, charpos, frame_window_p);
5160 /* Return 1 if PROP is a display sub-property value containing STRING.
5162 Implementation note: this and the following function are really
5163 special cases of handle_display_spec and
5164 handle_single_display_spec, and should ideally use the same code.
5165 Until they do, these two pairs must be consistent and must be
5166 modified in sync. */
5168 static int
5169 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5171 if (EQ (string, prop))
5172 return 1;
5174 /* Skip over `when FORM'. */
5175 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5177 prop = XCDR (prop);
5178 if (!CONSP (prop))
5179 return 0;
5180 /* Actually, the condition following `when' should be eval'ed,
5181 like handle_single_display_spec does, and we should return
5182 zero if it evaluates to nil. However, this function is
5183 called only when the buffer was already displayed and some
5184 glyph in the glyph matrix was found to come from a display
5185 string. Therefore, the condition was already evaluated, and
5186 the result was non-nil, otherwise the display string wouldn't
5187 have been displayed and we would have never been called for
5188 this property. Thus, we can skip the evaluation and assume
5189 its result is non-nil. */
5190 prop = XCDR (prop);
5193 if (CONSP (prop))
5194 /* Skip over `margin LOCATION'. */
5195 if (EQ (XCAR (prop), Qmargin))
5197 prop = XCDR (prop);
5198 if (!CONSP (prop))
5199 return 0;
5201 prop = XCDR (prop);
5202 if (!CONSP (prop))
5203 return 0;
5206 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5210 /* Return 1 if STRING appears in the `display' property PROP. */
5212 static int
5213 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5215 if (CONSP (prop)
5216 && !EQ (XCAR (prop), Qwhen)
5217 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5219 /* A list of sub-properties. */
5220 while (CONSP (prop))
5222 if (single_display_spec_string_p (XCAR (prop), string))
5223 return 1;
5224 prop = XCDR (prop);
5227 else if (VECTORP (prop))
5229 /* A vector of sub-properties. */
5230 ptrdiff_t i;
5231 for (i = 0; i < ASIZE (prop); ++i)
5232 if (single_display_spec_string_p (AREF (prop, i), string))
5233 return 1;
5235 else
5236 return single_display_spec_string_p (prop, string);
5238 return 0;
5241 /* Look for STRING in overlays and text properties in the current
5242 buffer, between character positions FROM and TO (excluding TO).
5243 BACK_P non-zero means look back (in this case, TO is supposed to be
5244 less than FROM).
5245 Value is the first character position where STRING was found, or
5246 zero if it wasn't found before hitting TO.
5248 This function may only use code that doesn't eval because it is
5249 called asynchronously from note_mouse_highlight. */
5251 static ptrdiff_t
5252 string_buffer_position_lim (Lisp_Object string,
5253 ptrdiff_t from, ptrdiff_t to, int back_p)
5255 Lisp_Object limit, prop, pos;
5256 int found = 0;
5258 pos = make_number (max (from, BEGV));
5260 if (!back_p) /* looking forward */
5262 limit = make_number (min (to, ZV));
5263 while (!found && !EQ (pos, limit))
5265 prop = Fget_char_property (pos, Qdisplay, Qnil);
5266 if (!NILP (prop) && display_prop_string_p (prop, string))
5267 found = 1;
5268 else
5269 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5270 limit);
5273 else /* looking back */
5275 limit = make_number (max (to, BEGV));
5276 while (!found && !EQ (pos, limit))
5278 prop = Fget_char_property (pos, Qdisplay, Qnil);
5279 if (!NILP (prop) && display_prop_string_p (prop, string))
5280 found = 1;
5281 else
5282 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5283 limit);
5287 return found ? XINT (pos) : 0;
5290 /* Determine which buffer position in current buffer STRING comes from.
5291 AROUND_CHARPOS is an approximate position where it could come from.
5292 Value is the buffer position or 0 if it couldn't be determined.
5294 This function is necessary because we don't record buffer positions
5295 in glyphs generated from strings (to keep struct glyph small).
5296 This function may only use code that doesn't eval because it is
5297 called asynchronously from note_mouse_highlight. */
5299 static ptrdiff_t
5300 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5302 const int MAX_DISTANCE = 1000;
5303 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5304 around_charpos + MAX_DISTANCE,
5307 if (!found)
5308 found = string_buffer_position_lim (string, around_charpos,
5309 around_charpos - MAX_DISTANCE, 1);
5310 return found;
5315 /***********************************************************************
5316 `composition' property
5317 ***********************************************************************/
5319 /* Set up iterator IT from `composition' property at its current
5320 position. Called from handle_stop. */
5322 static enum prop_handled
5323 handle_composition_prop (struct it *it)
5325 Lisp_Object prop, string;
5326 ptrdiff_t pos, pos_byte, start, end;
5328 if (STRINGP (it->string))
5330 unsigned char *s;
5332 pos = IT_STRING_CHARPOS (*it);
5333 pos_byte = IT_STRING_BYTEPOS (*it);
5334 string = it->string;
5335 s = SDATA (string) + pos_byte;
5336 it->c = STRING_CHAR (s);
5338 else
5340 pos = IT_CHARPOS (*it);
5341 pos_byte = IT_BYTEPOS (*it);
5342 string = Qnil;
5343 it->c = FETCH_CHAR (pos_byte);
5346 /* If there's a valid composition and point is not inside of the
5347 composition (in the case that the composition is from the current
5348 buffer), draw a glyph composed from the composition components. */
5349 if (find_composition (pos, -1, &start, &end, &prop, string)
5350 && COMPOSITION_VALID_P (start, end, prop)
5351 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5353 if (start < pos)
5354 /* As we can't handle this situation (perhaps font-lock added
5355 a new composition), we just return here hoping that next
5356 redisplay will detect this composition much earlier. */
5357 return HANDLED_NORMALLY;
5358 if (start != pos)
5360 if (STRINGP (it->string))
5361 pos_byte = string_char_to_byte (it->string, start);
5362 else
5363 pos_byte = CHAR_TO_BYTE (start);
5365 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5366 prop, string);
5368 if (it->cmp_it.id >= 0)
5370 it->cmp_it.ch = -1;
5371 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5372 it->cmp_it.nglyphs = -1;
5376 return HANDLED_NORMALLY;
5381 /***********************************************************************
5382 Overlay strings
5383 ***********************************************************************/
5385 /* The following structure is used to record overlay strings for
5386 later sorting in load_overlay_strings. */
5388 struct overlay_entry
5390 Lisp_Object overlay;
5391 Lisp_Object string;
5392 EMACS_INT priority;
5393 int after_string_p;
5397 /* Set up iterator IT from overlay strings at its current position.
5398 Called from handle_stop. */
5400 static enum prop_handled
5401 handle_overlay_change (struct it *it)
5403 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5404 return HANDLED_RECOMPUTE_PROPS;
5405 else
5406 return HANDLED_NORMALLY;
5410 /* Set up the next overlay string for delivery by IT, if there is an
5411 overlay string to deliver. Called by set_iterator_to_next when the
5412 end of the current overlay string is reached. If there are more
5413 overlay strings to display, IT->string and
5414 IT->current.overlay_string_index are set appropriately here.
5415 Otherwise IT->string is set to nil. */
5417 static void
5418 next_overlay_string (struct it *it)
5420 ++it->current.overlay_string_index;
5421 if (it->current.overlay_string_index == it->n_overlay_strings)
5423 /* No more overlay strings. Restore IT's settings to what
5424 they were before overlay strings were processed, and
5425 continue to deliver from current_buffer. */
5427 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5428 pop_it (it);
5429 eassert (it->sp > 0
5430 || (NILP (it->string)
5431 && it->method == GET_FROM_BUFFER
5432 && it->stop_charpos >= BEGV
5433 && it->stop_charpos <= it->end_charpos));
5434 it->current.overlay_string_index = -1;
5435 it->n_overlay_strings = 0;
5436 it->overlay_strings_charpos = -1;
5437 /* If there's an empty display string on the stack, pop the
5438 stack, to resync the bidi iterator with IT's position. Such
5439 empty strings are pushed onto the stack in
5440 get_overlay_strings_1. */
5441 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5442 pop_it (it);
5444 /* If we're at the end of the buffer, record that we have
5445 processed the overlay strings there already, so that
5446 next_element_from_buffer doesn't try it again. */
5447 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5448 it->overlay_strings_at_end_processed_p = 1;
5450 else
5452 /* There are more overlay strings to process. If
5453 IT->current.overlay_string_index has advanced to a position
5454 where we must load IT->overlay_strings with more strings, do
5455 it. We must load at the IT->overlay_strings_charpos where
5456 IT->n_overlay_strings was originally computed; when invisible
5457 text is present, this might not be IT_CHARPOS (Bug#7016). */
5458 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5460 if (it->current.overlay_string_index && i == 0)
5461 load_overlay_strings (it, it->overlay_strings_charpos);
5463 /* Initialize IT to deliver display elements from the overlay
5464 string. */
5465 it->string = it->overlay_strings[i];
5466 it->multibyte_p = STRING_MULTIBYTE (it->string);
5467 SET_TEXT_POS (it->current.string_pos, 0, 0);
5468 it->method = GET_FROM_STRING;
5469 it->stop_charpos = 0;
5470 it->end_charpos = SCHARS (it->string);
5471 if (it->cmp_it.stop_pos >= 0)
5472 it->cmp_it.stop_pos = 0;
5473 it->prev_stop = 0;
5474 it->base_level_stop = 0;
5476 /* Set up the bidi iterator for this overlay string. */
5477 if (it->bidi_p)
5479 it->bidi_it.string.lstring = it->string;
5480 it->bidi_it.string.s = NULL;
5481 it->bidi_it.string.schars = SCHARS (it->string);
5482 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5483 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5484 it->bidi_it.string.unibyte = !it->multibyte_p;
5485 it->bidi_it.w = it->w;
5486 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5490 CHECK_IT (it);
5494 /* Compare two overlay_entry structures E1 and E2. Used as a
5495 comparison function for qsort in load_overlay_strings. Overlay
5496 strings for the same position are sorted so that
5498 1. All after-strings come in front of before-strings, except
5499 when they come from the same overlay.
5501 2. Within after-strings, strings are sorted so that overlay strings
5502 from overlays with higher priorities come first.
5504 2. Within before-strings, strings are sorted so that overlay
5505 strings from overlays with higher priorities come last.
5507 Value is analogous to strcmp. */
5510 static int
5511 compare_overlay_entries (const void *e1, const void *e2)
5513 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5514 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5515 int result;
5517 if (entry1->after_string_p != entry2->after_string_p)
5519 /* Let after-strings appear in front of before-strings if
5520 they come from different overlays. */
5521 if (EQ (entry1->overlay, entry2->overlay))
5522 result = entry1->after_string_p ? 1 : -1;
5523 else
5524 result = entry1->after_string_p ? -1 : 1;
5526 else if (entry1->priority != entry2->priority)
5528 if (entry1->after_string_p)
5529 /* After-strings sorted in order of decreasing priority. */
5530 result = entry2->priority < entry1->priority ? -1 : 1;
5531 else
5532 /* Before-strings sorted in order of increasing priority. */
5533 result = entry1->priority < entry2->priority ? -1 : 1;
5535 else
5536 result = 0;
5538 return result;
5542 /* Load the vector IT->overlay_strings with overlay strings from IT's
5543 current buffer position, or from CHARPOS if that is > 0. Set
5544 IT->n_overlays to the total number of overlay strings found.
5546 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5547 a time. On entry into load_overlay_strings,
5548 IT->current.overlay_string_index gives the number of overlay
5549 strings that have already been loaded by previous calls to this
5550 function.
5552 IT->add_overlay_start contains an additional overlay start
5553 position to consider for taking overlay strings from, if non-zero.
5554 This position comes into play when the overlay has an `invisible'
5555 property, and both before and after-strings. When we've skipped to
5556 the end of the overlay, because of its `invisible' property, we
5557 nevertheless want its before-string to appear.
5558 IT->add_overlay_start will contain the overlay start position
5559 in this case.
5561 Overlay strings are sorted so that after-string strings come in
5562 front of before-string strings. Within before and after-strings,
5563 strings are sorted by overlay priority. See also function
5564 compare_overlay_entries. */
5566 static void
5567 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5569 Lisp_Object overlay, window, str, invisible;
5570 struct Lisp_Overlay *ov;
5571 ptrdiff_t start, end;
5572 ptrdiff_t size = 20;
5573 ptrdiff_t n = 0, i, j;
5574 int invis_p;
5575 struct overlay_entry *entries = alloca (size * sizeof *entries);
5576 USE_SAFE_ALLOCA;
5578 if (charpos <= 0)
5579 charpos = IT_CHARPOS (*it);
5581 /* Append the overlay string STRING of overlay OVERLAY to vector
5582 `entries' which has size `size' and currently contains `n'
5583 elements. AFTER_P non-zero means STRING is an after-string of
5584 OVERLAY. */
5585 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5586 do \
5588 Lisp_Object priority; \
5590 if (n == size) \
5592 struct overlay_entry *old = entries; \
5593 SAFE_NALLOCA (entries, 2, size); \
5594 memcpy (entries, old, size * sizeof *entries); \
5595 size *= 2; \
5598 entries[n].string = (STRING); \
5599 entries[n].overlay = (OVERLAY); \
5600 priority = Foverlay_get ((OVERLAY), Qpriority); \
5601 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5602 entries[n].after_string_p = (AFTER_P); \
5603 ++n; \
5605 while (0)
5607 /* Process overlay before the overlay center. */
5608 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5610 XSETMISC (overlay, ov);
5611 eassert (OVERLAYP (overlay));
5612 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5613 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5615 if (end < charpos)
5616 break;
5618 /* Skip this overlay if it doesn't start or end at IT's current
5619 position. */
5620 if (end != charpos && start != charpos)
5621 continue;
5623 /* Skip this overlay if it doesn't apply to IT->w. */
5624 window = Foverlay_get (overlay, Qwindow);
5625 if (WINDOWP (window) && XWINDOW (window) != it->w)
5626 continue;
5628 /* If the text ``under'' the overlay is invisible, both before-
5629 and after-strings from this overlay are visible; start and
5630 end position are indistinguishable. */
5631 invisible = Foverlay_get (overlay, Qinvisible);
5632 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5634 /* If overlay has a non-empty before-string, record it. */
5635 if ((start == charpos || (end == charpos && invis_p))
5636 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5637 && SCHARS (str))
5638 RECORD_OVERLAY_STRING (overlay, str, 0);
5640 /* If overlay has a non-empty after-string, record it. */
5641 if ((end == charpos || (start == charpos && invis_p))
5642 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5643 && SCHARS (str))
5644 RECORD_OVERLAY_STRING (overlay, str, 1);
5647 /* Process overlays after the overlay center. */
5648 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5650 XSETMISC (overlay, ov);
5651 eassert (OVERLAYP (overlay));
5652 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5653 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5655 if (start > charpos)
5656 break;
5658 /* Skip this overlay if it doesn't start or end at IT's current
5659 position. */
5660 if (end != charpos && start != charpos)
5661 continue;
5663 /* Skip this overlay if it doesn't apply to IT->w. */
5664 window = Foverlay_get (overlay, Qwindow);
5665 if (WINDOWP (window) && XWINDOW (window) != it->w)
5666 continue;
5668 /* If the text ``under'' the overlay is invisible, it has a zero
5669 dimension, and both before- and after-strings apply. */
5670 invisible = Foverlay_get (overlay, Qinvisible);
5671 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5673 /* If overlay has a non-empty before-string, record it. */
5674 if ((start == charpos || (end == charpos && invis_p))
5675 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5676 && SCHARS (str))
5677 RECORD_OVERLAY_STRING (overlay, str, 0);
5679 /* If overlay has a non-empty after-string, record it. */
5680 if ((end == charpos || (start == charpos && invis_p))
5681 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5682 && SCHARS (str))
5683 RECORD_OVERLAY_STRING (overlay, str, 1);
5686 #undef RECORD_OVERLAY_STRING
5688 /* Sort entries. */
5689 if (n > 1)
5690 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5692 /* Record number of overlay strings, and where we computed it. */
5693 it->n_overlay_strings = n;
5694 it->overlay_strings_charpos = charpos;
5696 /* IT->current.overlay_string_index is the number of overlay strings
5697 that have already been consumed by IT. Copy some of the
5698 remaining overlay strings to IT->overlay_strings. */
5699 i = 0;
5700 j = it->current.overlay_string_index;
5701 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5703 it->overlay_strings[i] = entries[j].string;
5704 it->string_overlays[i++] = entries[j++].overlay;
5707 CHECK_IT (it);
5708 SAFE_FREE ();
5712 /* Get the first chunk of overlay strings at IT's current buffer
5713 position, or at CHARPOS if that is > 0. Value is non-zero if at
5714 least one overlay string was found. */
5716 static int
5717 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5719 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5720 process. This fills IT->overlay_strings with strings, and sets
5721 IT->n_overlay_strings to the total number of strings to process.
5722 IT->pos.overlay_string_index has to be set temporarily to zero
5723 because load_overlay_strings needs this; it must be set to -1
5724 when no overlay strings are found because a zero value would
5725 indicate a position in the first overlay string. */
5726 it->current.overlay_string_index = 0;
5727 load_overlay_strings (it, charpos);
5729 /* If we found overlay strings, set up IT to deliver display
5730 elements from the first one. Otherwise set up IT to deliver
5731 from current_buffer. */
5732 if (it->n_overlay_strings)
5734 /* Make sure we know settings in current_buffer, so that we can
5735 restore meaningful values when we're done with the overlay
5736 strings. */
5737 if (compute_stop_p)
5738 compute_stop_pos (it);
5739 eassert (it->face_id >= 0);
5741 /* Save IT's settings. They are restored after all overlay
5742 strings have been processed. */
5743 eassert (!compute_stop_p || it->sp == 0);
5745 /* When called from handle_stop, there might be an empty display
5746 string loaded. In that case, don't bother saving it. But
5747 don't use this optimization with the bidi iterator, since we
5748 need the corresponding pop_it call to resync the bidi
5749 iterator's position with IT's position, after we are done
5750 with the overlay strings. (The corresponding call to pop_it
5751 in case of an empty display string is in
5752 next_overlay_string.) */
5753 if (!(!it->bidi_p
5754 && STRINGP (it->string) && !SCHARS (it->string)))
5755 push_it (it, NULL);
5757 /* Set up IT to deliver display elements from the first overlay
5758 string. */
5759 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5760 it->string = it->overlay_strings[0];
5761 it->from_overlay = Qnil;
5762 it->stop_charpos = 0;
5763 eassert (STRINGP (it->string));
5764 it->end_charpos = SCHARS (it->string);
5765 it->prev_stop = 0;
5766 it->base_level_stop = 0;
5767 it->multibyte_p = STRING_MULTIBYTE (it->string);
5768 it->method = GET_FROM_STRING;
5769 it->from_disp_prop_p = 0;
5771 /* Force paragraph direction to be that of the parent
5772 buffer. */
5773 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5774 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5775 else
5776 it->paragraph_embedding = L2R;
5778 /* Set up the bidi iterator for this overlay string. */
5779 if (it->bidi_p)
5781 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5783 it->bidi_it.string.lstring = it->string;
5784 it->bidi_it.string.s = NULL;
5785 it->bidi_it.string.schars = SCHARS (it->string);
5786 it->bidi_it.string.bufpos = pos;
5787 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5788 it->bidi_it.string.unibyte = !it->multibyte_p;
5789 it->bidi_it.w = it->w;
5790 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5792 return 1;
5795 it->current.overlay_string_index = -1;
5796 return 0;
5799 static int
5800 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5802 it->string = Qnil;
5803 it->method = GET_FROM_BUFFER;
5805 (void) get_overlay_strings_1 (it, charpos, 1);
5807 CHECK_IT (it);
5809 /* Value is non-zero if we found at least one overlay string. */
5810 return STRINGP (it->string);
5815 /***********************************************************************
5816 Saving and restoring state
5817 ***********************************************************************/
5819 /* Save current settings of IT on IT->stack. Called, for example,
5820 before setting up IT for an overlay string, to be able to restore
5821 IT's settings to what they were after the overlay string has been
5822 processed. If POSITION is non-NULL, it is the position to save on
5823 the stack instead of IT->position. */
5825 static void
5826 push_it (struct it *it, struct text_pos *position)
5828 struct iterator_stack_entry *p;
5830 eassert (it->sp < IT_STACK_SIZE);
5831 p = it->stack + it->sp;
5833 p->stop_charpos = it->stop_charpos;
5834 p->prev_stop = it->prev_stop;
5835 p->base_level_stop = it->base_level_stop;
5836 p->cmp_it = it->cmp_it;
5837 eassert (it->face_id >= 0);
5838 p->face_id = it->face_id;
5839 p->string = it->string;
5840 p->method = it->method;
5841 p->from_overlay = it->from_overlay;
5842 switch (p->method)
5844 case GET_FROM_IMAGE:
5845 p->u.image.object = it->object;
5846 p->u.image.image_id = it->image_id;
5847 p->u.image.slice = it->slice;
5848 break;
5849 case GET_FROM_STRETCH:
5850 p->u.stretch.object = it->object;
5851 break;
5853 p->position = position ? *position : it->position;
5854 p->current = it->current;
5855 p->end_charpos = it->end_charpos;
5856 p->string_nchars = it->string_nchars;
5857 p->area = it->area;
5858 p->multibyte_p = it->multibyte_p;
5859 p->avoid_cursor_p = it->avoid_cursor_p;
5860 p->space_width = it->space_width;
5861 p->font_height = it->font_height;
5862 p->voffset = it->voffset;
5863 p->string_from_display_prop_p = it->string_from_display_prop_p;
5864 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5865 p->display_ellipsis_p = 0;
5866 p->line_wrap = it->line_wrap;
5867 p->bidi_p = it->bidi_p;
5868 p->paragraph_embedding = it->paragraph_embedding;
5869 p->from_disp_prop_p = it->from_disp_prop_p;
5870 ++it->sp;
5872 /* Save the state of the bidi iterator as well. */
5873 if (it->bidi_p)
5874 bidi_push_it (&it->bidi_it);
5877 static void
5878 iterate_out_of_display_property (struct it *it)
5880 int buffer_p = !STRINGP (it->string);
5881 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5882 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5884 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5886 /* Maybe initialize paragraph direction. If we are at the beginning
5887 of a new paragraph, next_element_from_buffer may not have a
5888 chance to do that. */
5889 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5890 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5891 /* prev_stop can be zero, so check against BEGV as well. */
5892 while (it->bidi_it.charpos >= bob
5893 && it->prev_stop <= it->bidi_it.charpos
5894 && it->bidi_it.charpos < CHARPOS (it->position)
5895 && it->bidi_it.charpos < eob)
5896 bidi_move_to_visually_next (&it->bidi_it);
5897 /* Record the stop_pos we just crossed, for when we cross it
5898 back, maybe. */
5899 if (it->bidi_it.charpos > CHARPOS (it->position))
5900 it->prev_stop = CHARPOS (it->position);
5901 /* If we ended up not where pop_it put us, resync IT's
5902 positional members with the bidi iterator. */
5903 if (it->bidi_it.charpos != CHARPOS (it->position))
5904 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5905 if (buffer_p)
5906 it->current.pos = it->position;
5907 else
5908 it->current.string_pos = it->position;
5911 /* Restore IT's settings from IT->stack. Called, for example, when no
5912 more overlay strings must be processed, and we return to delivering
5913 display elements from a buffer, or when the end of a string from a
5914 `display' property is reached and we return to delivering display
5915 elements from an overlay string, or from a buffer. */
5917 static void
5918 pop_it (struct it *it)
5920 struct iterator_stack_entry *p;
5921 int from_display_prop = it->from_disp_prop_p;
5923 eassert (it->sp > 0);
5924 --it->sp;
5925 p = it->stack + it->sp;
5926 it->stop_charpos = p->stop_charpos;
5927 it->prev_stop = p->prev_stop;
5928 it->base_level_stop = p->base_level_stop;
5929 it->cmp_it = p->cmp_it;
5930 it->face_id = p->face_id;
5931 it->current = p->current;
5932 it->position = p->position;
5933 it->string = p->string;
5934 it->from_overlay = p->from_overlay;
5935 if (NILP (it->string))
5936 SET_TEXT_POS (it->current.string_pos, -1, -1);
5937 it->method = p->method;
5938 switch (it->method)
5940 case GET_FROM_IMAGE:
5941 it->image_id = p->u.image.image_id;
5942 it->object = p->u.image.object;
5943 it->slice = p->u.image.slice;
5944 break;
5945 case GET_FROM_STRETCH:
5946 it->object = p->u.stretch.object;
5947 break;
5948 case GET_FROM_BUFFER:
5949 it->object = it->w->contents;
5950 break;
5951 case GET_FROM_STRING:
5952 it->object = it->string;
5953 break;
5954 case GET_FROM_DISPLAY_VECTOR:
5955 if (it->s)
5956 it->method = GET_FROM_C_STRING;
5957 else if (STRINGP (it->string))
5958 it->method = GET_FROM_STRING;
5959 else
5961 it->method = GET_FROM_BUFFER;
5962 it->object = it->w->contents;
5965 it->end_charpos = p->end_charpos;
5966 it->string_nchars = p->string_nchars;
5967 it->area = p->area;
5968 it->multibyte_p = p->multibyte_p;
5969 it->avoid_cursor_p = p->avoid_cursor_p;
5970 it->space_width = p->space_width;
5971 it->font_height = p->font_height;
5972 it->voffset = p->voffset;
5973 it->string_from_display_prop_p = p->string_from_display_prop_p;
5974 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5975 it->line_wrap = p->line_wrap;
5976 it->bidi_p = p->bidi_p;
5977 it->paragraph_embedding = p->paragraph_embedding;
5978 it->from_disp_prop_p = p->from_disp_prop_p;
5979 if (it->bidi_p)
5981 bidi_pop_it (&it->bidi_it);
5982 /* Bidi-iterate until we get out of the portion of text, if any,
5983 covered by a `display' text property or by an overlay with
5984 `display' property. (We cannot just jump there, because the
5985 internal coherency of the bidi iterator state can not be
5986 preserved across such jumps.) We also must determine the
5987 paragraph base direction if the overlay we just processed is
5988 at the beginning of a new paragraph. */
5989 if (from_display_prop
5990 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5991 iterate_out_of_display_property (it);
5993 eassert ((BUFFERP (it->object)
5994 && IT_CHARPOS (*it) == it->bidi_it.charpos
5995 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5996 || (STRINGP (it->object)
5997 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5998 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5999 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6005 /***********************************************************************
6006 Moving over lines
6007 ***********************************************************************/
6009 /* Set IT's current position to the previous line start. */
6011 static void
6012 back_to_previous_line_start (struct it *it)
6014 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6016 DEC_BOTH (cp, bp);
6017 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6021 /* Move IT to the next line start.
6023 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6024 we skipped over part of the text (as opposed to moving the iterator
6025 continuously over the text). Otherwise, don't change the value
6026 of *SKIPPED_P.
6028 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6029 iterator on the newline, if it was found.
6031 Newlines may come from buffer text, overlay strings, or strings
6032 displayed via the `display' property. That's the reason we can't
6033 simply use find_newline_no_quit.
6035 Note that this function may not skip over invisible text that is so
6036 because of text properties and immediately follows a newline. If
6037 it would, function reseat_at_next_visible_line_start, when called
6038 from set_iterator_to_next, would effectively make invisible
6039 characters following a newline part of the wrong glyph row, which
6040 leads to wrong cursor motion. */
6042 static int
6043 forward_to_next_line_start (struct it *it, int *skipped_p,
6044 struct bidi_it *bidi_it_prev)
6046 ptrdiff_t old_selective;
6047 int newline_found_p, n;
6048 const int MAX_NEWLINE_DISTANCE = 500;
6050 /* If already on a newline, just consume it to avoid unintended
6051 skipping over invisible text below. */
6052 if (it->what == IT_CHARACTER
6053 && it->c == '\n'
6054 && CHARPOS (it->position) == IT_CHARPOS (*it))
6056 if (it->bidi_p && bidi_it_prev)
6057 *bidi_it_prev = it->bidi_it;
6058 set_iterator_to_next (it, 0);
6059 it->c = 0;
6060 return 1;
6063 /* Don't handle selective display in the following. It's (a)
6064 unnecessary because it's done by the caller, and (b) leads to an
6065 infinite recursion because next_element_from_ellipsis indirectly
6066 calls this function. */
6067 old_selective = it->selective;
6068 it->selective = 0;
6070 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6071 from buffer text. */
6072 for (n = newline_found_p = 0;
6073 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6074 n += STRINGP (it->string) ? 0 : 1)
6076 if (!get_next_display_element (it))
6077 return 0;
6078 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6079 if (newline_found_p && it->bidi_p && bidi_it_prev)
6080 *bidi_it_prev = it->bidi_it;
6081 set_iterator_to_next (it, 0);
6084 /* If we didn't find a newline near enough, see if we can use a
6085 short-cut. */
6086 if (!newline_found_p)
6088 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6089 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6090 1, &bytepos);
6091 Lisp_Object pos;
6093 eassert (!STRINGP (it->string));
6095 /* If there isn't any `display' property in sight, and no
6096 overlays, we can just use the position of the newline in
6097 buffer text. */
6098 if (it->stop_charpos >= limit
6099 || ((pos = Fnext_single_property_change (make_number (start),
6100 Qdisplay, Qnil,
6101 make_number (limit)),
6102 NILP (pos))
6103 && next_overlay_change (start) == ZV))
6105 if (!it->bidi_p)
6107 IT_CHARPOS (*it) = limit;
6108 IT_BYTEPOS (*it) = bytepos;
6110 else
6112 struct bidi_it bprev;
6114 /* Help bidi.c avoid expensive searches for display
6115 properties and overlays, by telling it that there are
6116 none up to `limit'. */
6117 if (it->bidi_it.disp_pos < limit)
6119 it->bidi_it.disp_pos = limit;
6120 it->bidi_it.disp_prop = 0;
6122 do {
6123 bprev = it->bidi_it;
6124 bidi_move_to_visually_next (&it->bidi_it);
6125 } while (it->bidi_it.charpos != limit);
6126 IT_CHARPOS (*it) = limit;
6127 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6128 if (bidi_it_prev)
6129 *bidi_it_prev = bprev;
6131 *skipped_p = newline_found_p = 1;
6133 else
6135 while (get_next_display_element (it)
6136 && !newline_found_p)
6138 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6139 if (newline_found_p && it->bidi_p && bidi_it_prev)
6140 *bidi_it_prev = it->bidi_it;
6141 set_iterator_to_next (it, 0);
6146 it->selective = old_selective;
6147 return newline_found_p;
6151 /* Set IT's current position to the previous visible line start. Skip
6152 invisible text that is so either due to text properties or due to
6153 selective display. Caution: this does not change IT->current_x and
6154 IT->hpos. */
6156 static void
6157 back_to_previous_visible_line_start (struct it *it)
6159 while (IT_CHARPOS (*it) > BEGV)
6161 back_to_previous_line_start (it);
6163 if (IT_CHARPOS (*it) <= BEGV)
6164 break;
6166 /* If selective > 0, then lines indented more than its value are
6167 invisible. */
6168 if (it->selective > 0
6169 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6170 it->selective))
6171 continue;
6173 /* Check the newline before point for invisibility. */
6175 Lisp_Object prop;
6176 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6177 Qinvisible, it->window);
6178 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6179 continue;
6182 if (IT_CHARPOS (*it) <= BEGV)
6183 break;
6186 struct it it2;
6187 void *it2data = NULL;
6188 ptrdiff_t pos;
6189 ptrdiff_t beg, end;
6190 Lisp_Object val, overlay;
6192 SAVE_IT (it2, *it, it2data);
6194 /* If newline is part of a composition, continue from start of composition */
6195 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6196 && beg < IT_CHARPOS (*it))
6197 goto replaced;
6199 /* If newline is replaced by a display property, find start of overlay
6200 or interval and continue search from that point. */
6201 pos = --IT_CHARPOS (it2);
6202 --IT_BYTEPOS (it2);
6203 it2.sp = 0;
6204 bidi_unshelve_cache (NULL, 0);
6205 it2.string_from_display_prop_p = 0;
6206 it2.from_disp_prop_p = 0;
6207 if (handle_display_prop (&it2) == HANDLED_RETURN
6208 && !NILP (val = get_char_property_and_overlay
6209 (make_number (pos), Qdisplay, Qnil, &overlay))
6210 && (OVERLAYP (overlay)
6211 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6212 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6214 RESTORE_IT (it, it, it2data);
6215 goto replaced;
6218 /* Newline is not replaced by anything -- so we are done. */
6219 RESTORE_IT (it, it, it2data);
6220 break;
6222 replaced:
6223 if (beg < BEGV)
6224 beg = BEGV;
6225 IT_CHARPOS (*it) = beg;
6226 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6230 it->continuation_lines_width = 0;
6232 eassert (IT_CHARPOS (*it) >= BEGV);
6233 eassert (IT_CHARPOS (*it) == BEGV
6234 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6235 CHECK_IT (it);
6239 /* Reseat iterator IT at the previous visible line start. Skip
6240 invisible text that is so either due to text properties or due to
6241 selective display. At the end, update IT's overlay information,
6242 face information etc. */
6244 void
6245 reseat_at_previous_visible_line_start (struct it *it)
6247 back_to_previous_visible_line_start (it);
6248 reseat (it, it->current.pos, 1);
6249 CHECK_IT (it);
6253 /* Reseat iterator IT on the next visible line start in the current
6254 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6255 preceding the line start. Skip over invisible text that is so
6256 because of selective display. Compute faces, overlays etc at the
6257 new position. Note that this function does not skip over text that
6258 is invisible because of text properties. */
6260 static void
6261 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6263 int newline_found_p, skipped_p = 0;
6264 struct bidi_it bidi_it_prev;
6266 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6268 /* Skip over lines that are invisible because they are indented
6269 more than the value of IT->selective. */
6270 if (it->selective > 0)
6271 while (IT_CHARPOS (*it) < ZV
6272 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6273 it->selective))
6275 eassert (IT_BYTEPOS (*it) == BEGV
6276 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6277 newline_found_p =
6278 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6281 /* Position on the newline if that's what's requested. */
6282 if (on_newline_p && newline_found_p)
6284 if (STRINGP (it->string))
6286 if (IT_STRING_CHARPOS (*it) > 0)
6288 if (!it->bidi_p)
6290 --IT_STRING_CHARPOS (*it);
6291 --IT_STRING_BYTEPOS (*it);
6293 else
6295 /* We need to restore the bidi iterator to the state
6296 it had on the newline, and resync the IT's
6297 position with that. */
6298 it->bidi_it = bidi_it_prev;
6299 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6300 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6304 else if (IT_CHARPOS (*it) > BEGV)
6306 if (!it->bidi_p)
6308 --IT_CHARPOS (*it);
6309 --IT_BYTEPOS (*it);
6311 else
6313 /* We need to restore the bidi iterator to the state it
6314 had on the newline and resync IT with that. */
6315 it->bidi_it = bidi_it_prev;
6316 IT_CHARPOS (*it) = it->bidi_it.charpos;
6317 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6319 reseat (it, it->current.pos, 0);
6322 else if (skipped_p)
6323 reseat (it, it->current.pos, 0);
6325 CHECK_IT (it);
6330 /***********************************************************************
6331 Changing an iterator's position
6332 ***********************************************************************/
6334 /* Change IT's current position to POS in current_buffer. If FORCE_P
6335 is non-zero, always check for text properties at the new position.
6336 Otherwise, text properties are only looked up if POS >=
6337 IT->check_charpos of a property. */
6339 static void
6340 reseat (struct it *it, struct text_pos pos, int force_p)
6342 ptrdiff_t original_pos = IT_CHARPOS (*it);
6344 reseat_1 (it, pos, 0);
6346 /* Determine where to check text properties. Avoid doing it
6347 where possible because text property lookup is very expensive. */
6348 if (force_p
6349 || CHARPOS (pos) > it->stop_charpos
6350 || CHARPOS (pos) < original_pos)
6352 if (it->bidi_p)
6354 /* For bidi iteration, we need to prime prev_stop and
6355 base_level_stop with our best estimations. */
6356 /* Implementation note: Of course, POS is not necessarily a
6357 stop position, so assigning prev_pos to it is a lie; we
6358 should have called compute_stop_backwards. However, if
6359 the current buffer does not include any R2L characters,
6360 that call would be a waste of cycles, because the
6361 iterator will never move back, and thus never cross this
6362 "fake" stop position. So we delay that backward search
6363 until the time we really need it, in next_element_from_buffer. */
6364 if (CHARPOS (pos) != it->prev_stop)
6365 it->prev_stop = CHARPOS (pos);
6366 if (CHARPOS (pos) < it->base_level_stop)
6367 it->base_level_stop = 0; /* meaning it's unknown */
6368 handle_stop (it);
6370 else
6372 handle_stop (it);
6373 it->prev_stop = it->base_level_stop = 0;
6378 CHECK_IT (it);
6382 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6383 IT->stop_pos to POS, also. */
6385 static void
6386 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6388 /* Don't call this function when scanning a C string. */
6389 eassert (it->s == NULL);
6391 /* POS must be a reasonable value. */
6392 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6394 it->current.pos = it->position = pos;
6395 it->end_charpos = ZV;
6396 it->dpvec = NULL;
6397 it->current.dpvec_index = -1;
6398 it->current.overlay_string_index = -1;
6399 IT_STRING_CHARPOS (*it) = -1;
6400 IT_STRING_BYTEPOS (*it) = -1;
6401 it->string = Qnil;
6402 it->method = GET_FROM_BUFFER;
6403 it->object = it->w->contents;
6404 it->area = TEXT_AREA;
6405 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6406 it->sp = 0;
6407 it->string_from_display_prop_p = 0;
6408 it->string_from_prefix_prop_p = 0;
6410 it->from_disp_prop_p = 0;
6411 it->face_before_selective_p = 0;
6412 if (it->bidi_p)
6414 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6415 &it->bidi_it);
6416 bidi_unshelve_cache (NULL, 0);
6417 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6418 it->bidi_it.string.s = NULL;
6419 it->bidi_it.string.lstring = Qnil;
6420 it->bidi_it.string.bufpos = 0;
6421 it->bidi_it.string.unibyte = 0;
6422 it->bidi_it.w = it->w;
6425 if (set_stop_p)
6427 it->stop_charpos = CHARPOS (pos);
6428 it->base_level_stop = CHARPOS (pos);
6430 /* This make the information stored in it->cmp_it invalidate. */
6431 it->cmp_it.id = -1;
6435 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6436 If S is non-null, it is a C string to iterate over. Otherwise,
6437 STRING gives a Lisp string to iterate over.
6439 If PRECISION > 0, don't return more then PRECISION number of
6440 characters from the string.
6442 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6443 characters have been returned. FIELD_WIDTH < 0 means an infinite
6444 field width.
6446 MULTIBYTE = 0 means disable processing of multibyte characters,
6447 MULTIBYTE > 0 means enable it,
6448 MULTIBYTE < 0 means use IT->multibyte_p.
6450 IT must be initialized via a prior call to init_iterator before
6451 calling this function. */
6453 static void
6454 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6455 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6456 int multibyte)
6458 /* No region in strings. */
6459 it->region_beg_charpos = it->region_end_charpos = -1;
6461 /* No text property checks performed by default, but see below. */
6462 it->stop_charpos = -1;
6464 /* Set iterator position and end position. */
6465 memset (&it->current, 0, sizeof it->current);
6466 it->current.overlay_string_index = -1;
6467 it->current.dpvec_index = -1;
6468 eassert (charpos >= 0);
6470 /* If STRING is specified, use its multibyteness, otherwise use the
6471 setting of MULTIBYTE, if specified. */
6472 if (multibyte >= 0)
6473 it->multibyte_p = multibyte > 0;
6475 /* Bidirectional reordering of strings is controlled by the default
6476 value of bidi-display-reordering. Don't try to reorder while
6477 loading loadup.el, as the necessary character property tables are
6478 not yet available. */
6479 it->bidi_p =
6480 NILP (Vpurify_flag)
6481 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6483 if (s == NULL)
6485 eassert (STRINGP (string));
6486 it->string = string;
6487 it->s = NULL;
6488 it->end_charpos = it->string_nchars = SCHARS (string);
6489 it->method = GET_FROM_STRING;
6490 it->current.string_pos = string_pos (charpos, string);
6492 if (it->bidi_p)
6494 it->bidi_it.string.lstring = string;
6495 it->bidi_it.string.s = NULL;
6496 it->bidi_it.string.schars = it->end_charpos;
6497 it->bidi_it.string.bufpos = 0;
6498 it->bidi_it.string.from_disp_str = 0;
6499 it->bidi_it.string.unibyte = !it->multibyte_p;
6500 it->bidi_it.w = it->w;
6501 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6502 FRAME_WINDOW_P (it->f), &it->bidi_it);
6505 else
6507 it->s = (const unsigned char *) s;
6508 it->string = Qnil;
6510 /* Note that we use IT->current.pos, not it->current.string_pos,
6511 for displaying C strings. */
6512 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6513 if (it->multibyte_p)
6515 it->current.pos = c_string_pos (charpos, s, 1);
6516 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6518 else
6520 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6521 it->end_charpos = it->string_nchars = strlen (s);
6524 if (it->bidi_p)
6526 it->bidi_it.string.lstring = Qnil;
6527 it->bidi_it.string.s = (const unsigned char *) s;
6528 it->bidi_it.string.schars = it->end_charpos;
6529 it->bidi_it.string.bufpos = 0;
6530 it->bidi_it.string.from_disp_str = 0;
6531 it->bidi_it.string.unibyte = !it->multibyte_p;
6532 it->bidi_it.w = it->w;
6533 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6534 &it->bidi_it);
6536 it->method = GET_FROM_C_STRING;
6539 /* PRECISION > 0 means don't return more than PRECISION characters
6540 from the string. */
6541 if (precision > 0 && it->end_charpos - charpos > precision)
6543 it->end_charpos = it->string_nchars = charpos + precision;
6544 if (it->bidi_p)
6545 it->bidi_it.string.schars = it->end_charpos;
6548 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6549 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6550 FIELD_WIDTH < 0 means infinite field width. This is useful for
6551 padding with `-' at the end of a mode line. */
6552 if (field_width < 0)
6553 field_width = INFINITY;
6554 /* Implementation note: We deliberately don't enlarge
6555 it->bidi_it.string.schars here to fit it->end_charpos, because
6556 the bidi iterator cannot produce characters out of thin air. */
6557 if (field_width > it->end_charpos - charpos)
6558 it->end_charpos = charpos + field_width;
6560 /* Use the standard display table for displaying strings. */
6561 if (DISP_TABLE_P (Vstandard_display_table))
6562 it->dp = XCHAR_TABLE (Vstandard_display_table);
6564 it->stop_charpos = charpos;
6565 it->prev_stop = charpos;
6566 it->base_level_stop = 0;
6567 if (it->bidi_p)
6569 it->bidi_it.first_elt = 1;
6570 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6571 it->bidi_it.disp_pos = -1;
6573 if (s == NULL && it->multibyte_p)
6575 ptrdiff_t endpos = SCHARS (it->string);
6576 if (endpos > it->end_charpos)
6577 endpos = it->end_charpos;
6578 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6579 it->string);
6581 CHECK_IT (it);
6586 /***********************************************************************
6587 Iteration
6588 ***********************************************************************/
6590 /* Map enum it_method value to corresponding next_element_from_* function. */
6592 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6594 next_element_from_buffer,
6595 next_element_from_display_vector,
6596 next_element_from_string,
6597 next_element_from_c_string,
6598 next_element_from_image,
6599 next_element_from_stretch
6602 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6605 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6606 (possibly with the following characters). */
6608 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6609 ((IT)->cmp_it.id >= 0 \
6610 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6611 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6612 END_CHARPOS, (IT)->w, \
6613 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6614 (IT)->string)))
6617 /* Lookup the char-table Vglyphless_char_display for character C (-1
6618 if we want information for no-font case), and return the display
6619 method symbol. By side-effect, update it->what and
6620 it->glyphless_method. This function is called from
6621 get_next_display_element for each character element, and from
6622 x_produce_glyphs when no suitable font was found. */
6624 Lisp_Object
6625 lookup_glyphless_char_display (int c, struct it *it)
6627 Lisp_Object glyphless_method = Qnil;
6629 if (CHAR_TABLE_P (Vglyphless_char_display)
6630 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6632 if (c >= 0)
6634 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6635 if (CONSP (glyphless_method))
6636 glyphless_method = FRAME_WINDOW_P (it->f)
6637 ? XCAR (glyphless_method)
6638 : XCDR (glyphless_method);
6640 else
6641 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6644 retry:
6645 if (NILP (glyphless_method))
6647 if (c >= 0)
6648 /* The default is to display the character by a proper font. */
6649 return Qnil;
6650 /* The default for the no-font case is to display an empty box. */
6651 glyphless_method = Qempty_box;
6653 if (EQ (glyphless_method, Qzero_width))
6655 if (c >= 0)
6656 return glyphless_method;
6657 /* This method can't be used for the no-font case. */
6658 glyphless_method = Qempty_box;
6660 if (EQ (glyphless_method, Qthin_space))
6661 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6662 else if (EQ (glyphless_method, Qempty_box))
6663 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6664 else if (EQ (glyphless_method, Qhex_code))
6665 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6666 else if (STRINGP (glyphless_method))
6667 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6668 else
6670 /* Invalid value. We use the default method. */
6671 glyphless_method = Qnil;
6672 goto retry;
6674 it->what = IT_GLYPHLESS;
6675 return glyphless_method;
6678 /* Load IT's display element fields with information about the next
6679 display element from the current position of IT. Value is zero if
6680 end of buffer (or C string) is reached. */
6682 static struct frame *last_escape_glyph_frame = NULL;
6683 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6684 static int last_escape_glyph_merged_face_id = 0;
6686 struct frame *last_glyphless_glyph_frame = NULL;
6687 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6688 int last_glyphless_glyph_merged_face_id = 0;
6690 static int
6691 get_next_display_element (struct it *it)
6693 /* Non-zero means that we found a display element. Zero means that
6694 we hit the end of what we iterate over. Performance note: the
6695 function pointer `method' used here turns out to be faster than
6696 using a sequence of if-statements. */
6697 int success_p;
6699 get_next:
6700 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6702 if (it->what == IT_CHARACTER)
6704 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6705 and only if (a) the resolved directionality of that character
6706 is R..." */
6707 /* FIXME: Do we need an exception for characters from display
6708 tables? */
6709 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6710 it->c = bidi_mirror_char (it->c);
6711 /* Map via display table or translate control characters.
6712 IT->c, IT->len etc. have been set to the next character by
6713 the function call above. If we have a display table, and it
6714 contains an entry for IT->c, translate it. Don't do this if
6715 IT->c itself comes from a display table, otherwise we could
6716 end up in an infinite recursion. (An alternative could be to
6717 count the recursion depth of this function and signal an
6718 error when a certain maximum depth is reached.) Is it worth
6719 it? */
6720 if (success_p && it->dpvec == NULL)
6722 Lisp_Object dv;
6723 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6724 int nonascii_space_p = 0;
6725 int nonascii_hyphen_p = 0;
6726 int c = it->c; /* This is the character to display. */
6728 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6730 eassert (SINGLE_BYTE_CHAR_P (c));
6731 if (unibyte_display_via_language_environment)
6733 c = DECODE_CHAR (unibyte, c);
6734 if (c < 0)
6735 c = BYTE8_TO_CHAR (it->c);
6737 else
6738 c = BYTE8_TO_CHAR (it->c);
6741 if (it->dp
6742 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6743 VECTORP (dv)))
6745 struct Lisp_Vector *v = XVECTOR (dv);
6747 /* Return the first character from the display table
6748 entry, if not empty. If empty, don't display the
6749 current character. */
6750 if (v->header.size)
6752 it->dpvec_char_len = it->len;
6753 it->dpvec = v->contents;
6754 it->dpend = v->contents + v->header.size;
6755 it->current.dpvec_index = 0;
6756 it->dpvec_face_id = -1;
6757 it->saved_face_id = it->face_id;
6758 it->method = GET_FROM_DISPLAY_VECTOR;
6759 it->ellipsis_p = 0;
6761 else
6763 set_iterator_to_next (it, 0);
6765 goto get_next;
6768 if (! NILP (lookup_glyphless_char_display (c, it)))
6770 if (it->what == IT_GLYPHLESS)
6771 goto done;
6772 /* Don't display this character. */
6773 set_iterator_to_next (it, 0);
6774 goto get_next;
6777 /* If `nobreak-char-display' is non-nil, we display
6778 non-ASCII spaces and hyphens specially. */
6779 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6781 if (c == 0xA0)
6782 nonascii_space_p = 1;
6783 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6784 nonascii_hyphen_p = 1;
6787 /* Translate control characters into `\003' or `^C' form.
6788 Control characters coming from a display table entry are
6789 currently not translated because we use IT->dpvec to hold
6790 the translation. This could easily be changed but I
6791 don't believe that it is worth doing.
6793 The characters handled by `nobreak-char-display' must be
6794 translated too.
6796 Non-printable characters and raw-byte characters are also
6797 translated to octal form. */
6798 if (((c < ' ' || c == 127) /* ASCII control chars */
6799 ? (it->area != TEXT_AREA
6800 /* In mode line, treat \n, \t like other crl chars. */
6801 || (c != '\t'
6802 && it->glyph_row
6803 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6804 || (c != '\n' && c != '\t'))
6805 : (nonascii_space_p
6806 || nonascii_hyphen_p
6807 || CHAR_BYTE8_P (c)
6808 || ! CHAR_PRINTABLE_P (c))))
6810 /* C is a control character, non-ASCII space/hyphen,
6811 raw-byte, or a non-printable character which must be
6812 displayed either as '\003' or as `^C' where the '\\'
6813 and '^' can be defined in the display table. Fill
6814 IT->ctl_chars with glyphs for what we have to
6815 display. Then, set IT->dpvec to these glyphs. */
6816 Lisp_Object gc;
6817 int ctl_len;
6818 int face_id;
6819 int lface_id = 0;
6820 int escape_glyph;
6822 /* Handle control characters with ^. */
6824 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6826 int g;
6828 g = '^'; /* default glyph for Control */
6829 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6830 if (it->dp
6831 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6833 g = GLYPH_CODE_CHAR (gc);
6834 lface_id = GLYPH_CODE_FACE (gc);
6836 if (lface_id)
6838 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6840 else if (it->f == last_escape_glyph_frame
6841 && it->face_id == last_escape_glyph_face_id)
6843 face_id = last_escape_glyph_merged_face_id;
6845 else
6847 /* Merge the escape-glyph face into the current face. */
6848 face_id = merge_faces (it->f, Qescape_glyph, 0,
6849 it->face_id);
6850 last_escape_glyph_frame = it->f;
6851 last_escape_glyph_face_id = it->face_id;
6852 last_escape_glyph_merged_face_id = face_id;
6855 XSETINT (it->ctl_chars[0], g);
6856 XSETINT (it->ctl_chars[1], c ^ 0100);
6857 ctl_len = 2;
6858 goto display_control;
6861 /* Handle non-ascii space in the mode where it only gets
6862 highlighting. */
6864 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6866 /* Merge `nobreak-space' into the current face. */
6867 face_id = merge_faces (it->f, Qnobreak_space, 0,
6868 it->face_id);
6869 XSETINT (it->ctl_chars[0], ' ');
6870 ctl_len = 1;
6871 goto display_control;
6874 /* Handle sequences that start with the "escape glyph". */
6876 /* the default escape glyph is \. */
6877 escape_glyph = '\\';
6879 if (it->dp
6880 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6882 escape_glyph = GLYPH_CODE_CHAR (gc);
6883 lface_id = GLYPH_CODE_FACE (gc);
6885 if (lface_id)
6887 /* The display table specified a face.
6888 Merge it into face_id and also into escape_glyph. */
6889 face_id = merge_faces (it->f, Qt, lface_id,
6890 it->face_id);
6892 else if (it->f == last_escape_glyph_frame
6893 && it->face_id == last_escape_glyph_face_id)
6895 face_id = last_escape_glyph_merged_face_id;
6897 else
6899 /* Merge the escape-glyph face into the current face. */
6900 face_id = merge_faces (it->f, Qescape_glyph, 0,
6901 it->face_id);
6902 last_escape_glyph_frame = it->f;
6903 last_escape_glyph_face_id = it->face_id;
6904 last_escape_glyph_merged_face_id = face_id;
6907 /* Draw non-ASCII hyphen with just highlighting: */
6909 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6911 XSETINT (it->ctl_chars[0], '-');
6912 ctl_len = 1;
6913 goto display_control;
6916 /* Draw non-ASCII space/hyphen with escape glyph: */
6918 if (nonascii_space_p || nonascii_hyphen_p)
6920 XSETINT (it->ctl_chars[0], escape_glyph);
6921 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6922 ctl_len = 2;
6923 goto display_control;
6927 char str[10];
6928 int len, i;
6930 if (CHAR_BYTE8_P (c))
6931 /* Display \200 instead of \17777600. */
6932 c = CHAR_TO_BYTE8 (c);
6933 len = sprintf (str, "%03o", c);
6935 XSETINT (it->ctl_chars[0], escape_glyph);
6936 for (i = 0; i < len; i++)
6937 XSETINT (it->ctl_chars[i + 1], str[i]);
6938 ctl_len = len + 1;
6941 display_control:
6942 /* Set up IT->dpvec and return first character from it. */
6943 it->dpvec_char_len = it->len;
6944 it->dpvec = it->ctl_chars;
6945 it->dpend = it->dpvec + ctl_len;
6946 it->current.dpvec_index = 0;
6947 it->dpvec_face_id = face_id;
6948 it->saved_face_id = it->face_id;
6949 it->method = GET_FROM_DISPLAY_VECTOR;
6950 it->ellipsis_p = 0;
6951 goto get_next;
6953 it->char_to_display = c;
6955 else if (success_p)
6957 it->char_to_display = it->c;
6961 /* Adjust face id for a multibyte character. There are no multibyte
6962 character in unibyte text. */
6963 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6964 && it->multibyte_p
6965 && success_p
6966 && FRAME_WINDOW_P (it->f))
6968 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6970 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6972 /* Automatic composition with glyph-string. */
6973 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6975 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6977 else
6979 ptrdiff_t pos = (it->s ? -1
6980 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6981 : IT_CHARPOS (*it));
6982 int c;
6984 if (it->what == IT_CHARACTER)
6985 c = it->char_to_display;
6986 else
6988 struct composition *cmp = composition_table[it->cmp_it.id];
6989 int i;
6991 c = ' ';
6992 for (i = 0; i < cmp->glyph_len; i++)
6993 /* TAB in a composition means display glyphs with
6994 padding space on the left or right. */
6995 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6996 break;
6998 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7002 done:
7003 /* Is this character the last one of a run of characters with
7004 box? If yes, set IT->end_of_box_run_p to 1. */
7005 if (it->face_box_p
7006 && it->s == NULL)
7008 if (it->method == GET_FROM_STRING && it->sp)
7010 int face_id = underlying_face_id (it);
7011 struct face *face = FACE_FROM_ID (it->f, face_id);
7013 if (face)
7015 if (face->box == FACE_NO_BOX)
7017 /* If the box comes from face properties in a
7018 display string, check faces in that string. */
7019 int string_face_id = face_after_it_pos (it);
7020 it->end_of_box_run_p
7021 = (FACE_FROM_ID (it->f, string_face_id)->box
7022 == FACE_NO_BOX);
7024 /* Otherwise, the box comes from the underlying face.
7025 If this is the last string character displayed, check
7026 the next buffer location. */
7027 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7028 && (it->current.overlay_string_index
7029 == it->n_overlay_strings - 1))
7031 ptrdiff_t ignore;
7032 int next_face_id;
7033 struct text_pos pos = it->current.pos;
7034 INC_TEXT_POS (pos, it->multibyte_p);
7036 next_face_id = face_at_buffer_position
7037 (it->w, CHARPOS (pos), it->region_beg_charpos,
7038 it->region_end_charpos, &ignore,
7039 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7040 -1);
7041 it->end_of_box_run_p
7042 = (FACE_FROM_ID (it->f, next_face_id)->box
7043 == FACE_NO_BOX);
7047 else
7049 int face_id = face_after_it_pos (it);
7050 it->end_of_box_run_p
7051 = (face_id != it->face_id
7052 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7055 /* If we reached the end of the object we've been iterating (e.g., a
7056 display string or an overlay string), and there's something on
7057 IT->stack, proceed with what's on the stack. It doesn't make
7058 sense to return zero if there's unprocessed stuff on the stack,
7059 because otherwise that stuff will never be displayed. */
7060 if (!success_p && it->sp > 0)
7062 set_iterator_to_next (it, 0);
7063 success_p = get_next_display_element (it);
7066 /* Value is 0 if end of buffer or string reached. */
7067 return success_p;
7071 /* Move IT to the next display element.
7073 RESEAT_P non-zero means if called on a newline in buffer text,
7074 skip to the next visible line start.
7076 Functions get_next_display_element and set_iterator_to_next are
7077 separate because I find this arrangement easier to handle than a
7078 get_next_display_element function that also increments IT's
7079 position. The way it is we can first look at an iterator's current
7080 display element, decide whether it fits on a line, and if it does,
7081 increment the iterator position. The other way around we probably
7082 would either need a flag indicating whether the iterator has to be
7083 incremented the next time, or we would have to implement a
7084 decrement position function which would not be easy to write. */
7086 void
7087 set_iterator_to_next (struct it *it, int reseat_p)
7089 /* Reset flags indicating start and end of a sequence of characters
7090 with box. Reset them at the start of this function because
7091 moving the iterator to a new position might set them. */
7092 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7094 switch (it->method)
7096 case GET_FROM_BUFFER:
7097 /* The current display element of IT is a character from
7098 current_buffer. Advance in the buffer, and maybe skip over
7099 invisible lines that are so because of selective display. */
7100 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7101 reseat_at_next_visible_line_start (it, 0);
7102 else if (it->cmp_it.id >= 0)
7104 /* We are currently getting glyphs from a composition. */
7105 int i;
7107 if (! it->bidi_p)
7109 IT_CHARPOS (*it) += it->cmp_it.nchars;
7110 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7111 if (it->cmp_it.to < it->cmp_it.nglyphs)
7113 it->cmp_it.from = it->cmp_it.to;
7115 else
7117 it->cmp_it.id = -1;
7118 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7119 IT_BYTEPOS (*it),
7120 it->end_charpos, Qnil);
7123 else if (! it->cmp_it.reversed_p)
7125 /* Composition created while scanning forward. */
7126 /* Update IT's char/byte positions to point to the first
7127 character of the next grapheme cluster, or to the
7128 character visually after the current composition. */
7129 for (i = 0; i < it->cmp_it.nchars; i++)
7130 bidi_move_to_visually_next (&it->bidi_it);
7131 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7132 IT_CHARPOS (*it) = it->bidi_it.charpos;
7134 if (it->cmp_it.to < it->cmp_it.nglyphs)
7136 /* Proceed to the next grapheme cluster. */
7137 it->cmp_it.from = it->cmp_it.to;
7139 else
7141 /* No more grapheme clusters in this composition.
7142 Find the next stop position. */
7143 ptrdiff_t stop = it->end_charpos;
7144 if (it->bidi_it.scan_dir < 0)
7145 /* Now we are scanning backward and don't know
7146 where to stop. */
7147 stop = -1;
7148 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7149 IT_BYTEPOS (*it), stop, Qnil);
7152 else
7154 /* Composition created while scanning backward. */
7155 /* Update IT's char/byte positions to point to the last
7156 character of the previous grapheme cluster, or the
7157 character visually after the current composition. */
7158 for (i = 0; i < it->cmp_it.nchars; i++)
7159 bidi_move_to_visually_next (&it->bidi_it);
7160 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7161 IT_CHARPOS (*it) = it->bidi_it.charpos;
7162 if (it->cmp_it.from > 0)
7164 /* Proceed to the previous grapheme cluster. */
7165 it->cmp_it.to = it->cmp_it.from;
7167 else
7169 /* No more grapheme clusters in this composition.
7170 Find the next stop position. */
7171 ptrdiff_t stop = it->end_charpos;
7172 if (it->bidi_it.scan_dir < 0)
7173 /* Now we are scanning backward and don't know
7174 where to stop. */
7175 stop = -1;
7176 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7177 IT_BYTEPOS (*it), stop, Qnil);
7181 else
7183 eassert (it->len != 0);
7185 if (!it->bidi_p)
7187 IT_BYTEPOS (*it) += it->len;
7188 IT_CHARPOS (*it) += 1;
7190 else
7192 int prev_scan_dir = it->bidi_it.scan_dir;
7193 /* If this is a new paragraph, determine its base
7194 direction (a.k.a. its base embedding level). */
7195 if (it->bidi_it.new_paragraph)
7196 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7197 bidi_move_to_visually_next (&it->bidi_it);
7198 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7199 IT_CHARPOS (*it) = it->bidi_it.charpos;
7200 if (prev_scan_dir != it->bidi_it.scan_dir)
7202 /* As the scan direction was changed, we must
7203 re-compute the stop position for composition. */
7204 ptrdiff_t stop = it->end_charpos;
7205 if (it->bidi_it.scan_dir < 0)
7206 stop = -1;
7207 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7208 IT_BYTEPOS (*it), stop, Qnil);
7211 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7213 break;
7215 case GET_FROM_C_STRING:
7216 /* Current display element of IT is from a C string. */
7217 if (!it->bidi_p
7218 /* If the string position is beyond string's end, it means
7219 next_element_from_c_string is padding the string with
7220 blanks, in which case we bypass the bidi iterator,
7221 because it cannot deal with such virtual characters. */
7222 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7224 IT_BYTEPOS (*it) += it->len;
7225 IT_CHARPOS (*it) += 1;
7227 else
7229 bidi_move_to_visually_next (&it->bidi_it);
7230 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7231 IT_CHARPOS (*it) = it->bidi_it.charpos;
7233 break;
7235 case GET_FROM_DISPLAY_VECTOR:
7236 /* Current display element of IT is from a display table entry.
7237 Advance in the display table definition. Reset it to null if
7238 end reached, and continue with characters from buffers/
7239 strings. */
7240 ++it->current.dpvec_index;
7242 /* Restore face of the iterator to what they were before the
7243 display vector entry (these entries may contain faces). */
7244 it->face_id = it->saved_face_id;
7246 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7248 int recheck_faces = it->ellipsis_p;
7250 if (it->s)
7251 it->method = GET_FROM_C_STRING;
7252 else if (STRINGP (it->string))
7253 it->method = GET_FROM_STRING;
7254 else
7256 it->method = GET_FROM_BUFFER;
7257 it->object = it->w->contents;
7260 it->dpvec = NULL;
7261 it->current.dpvec_index = -1;
7263 /* Skip over characters which were displayed via IT->dpvec. */
7264 if (it->dpvec_char_len < 0)
7265 reseat_at_next_visible_line_start (it, 1);
7266 else if (it->dpvec_char_len > 0)
7268 if (it->method == GET_FROM_STRING
7269 && it->current.overlay_string_index >= 0
7270 && it->n_overlay_strings > 0)
7271 it->ignore_overlay_strings_at_pos_p = 1;
7272 it->len = it->dpvec_char_len;
7273 set_iterator_to_next (it, reseat_p);
7276 /* Maybe recheck faces after display vector */
7277 if (recheck_faces)
7278 it->stop_charpos = IT_CHARPOS (*it);
7280 break;
7282 case GET_FROM_STRING:
7283 /* Current display element is a character from a Lisp string. */
7284 eassert (it->s == NULL && STRINGP (it->string));
7285 /* Don't advance past string end. These conditions are true
7286 when set_iterator_to_next is called at the end of
7287 get_next_display_element, in which case the Lisp string is
7288 already exhausted, and all we want is pop the iterator
7289 stack. */
7290 if (it->current.overlay_string_index >= 0)
7292 /* This is an overlay string, so there's no padding with
7293 spaces, and the number of characters in the string is
7294 where the string ends. */
7295 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7296 goto consider_string_end;
7298 else
7300 /* Not an overlay string. There could be padding, so test
7301 against it->end_charpos . */
7302 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7303 goto consider_string_end;
7305 if (it->cmp_it.id >= 0)
7307 int i;
7309 if (! it->bidi_p)
7311 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7312 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7313 if (it->cmp_it.to < it->cmp_it.nglyphs)
7314 it->cmp_it.from = it->cmp_it.to;
7315 else
7317 it->cmp_it.id = -1;
7318 composition_compute_stop_pos (&it->cmp_it,
7319 IT_STRING_CHARPOS (*it),
7320 IT_STRING_BYTEPOS (*it),
7321 it->end_charpos, it->string);
7324 else if (! it->cmp_it.reversed_p)
7326 for (i = 0; i < it->cmp_it.nchars; i++)
7327 bidi_move_to_visually_next (&it->bidi_it);
7328 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7329 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7331 if (it->cmp_it.to < it->cmp_it.nglyphs)
7332 it->cmp_it.from = it->cmp_it.to;
7333 else
7335 ptrdiff_t stop = it->end_charpos;
7336 if (it->bidi_it.scan_dir < 0)
7337 stop = -1;
7338 composition_compute_stop_pos (&it->cmp_it,
7339 IT_STRING_CHARPOS (*it),
7340 IT_STRING_BYTEPOS (*it), stop,
7341 it->string);
7344 else
7346 for (i = 0; i < it->cmp_it.nchars; i++)
7347 bidi_move_to_visually_next (&it->bidi_it);
7348 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7349 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7350 if (it->cmp_it.from > 0)
7351 it->cmp_it.to = it->cmp_it.from;
7352 else
7354 ptrdiff_t stop = it->end_charpos;
7355 if (it->bidi_it.scan_dir < 0)
7356 stop = -1;
7357 composition_compute_stop_pos (&it->cmp_it,
7358 IT_STRING_CHARPOS (*it),
7359 IT_STRING_BYTEPOS (*it), stop,
7360 it->string);
7364 else
7366 if (!it->bidi_p
7367 /* If the string position is beyond string's end, it
7368 means next_element_from_string is padding the string
7369 with blanks, in which case we bypass the bidi
7370 iterator, because it cannot deal with such virtual
7371 characters. */
7372 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7374 IT_STRING_BYTEPOS (*it) += it->len;
7375 IT_STRING_CHARPOS (*it) += 1;
7377 else
7379 int prev_scan_dir = it->bidi_it.scan_dir;
7381 bidi_move_to_visually_next (&it->bidi_it);
7382 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7383 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7384 if (prev_scan_dir != it->bidi_it.scan_dir)
7386 ptrdiff_t stop = it->end_charpos;
7388 if (it->bidi_it.scan_dir < 0)
7389 stop = -1;
7390 composition_compute_stop_pos (&it->cmp_it,
7391 IT_STRING_CHARPOS (*it),
7392 IT_STRING_BYTEPOS (*it), stop,
7393 it->string);
7398 consider_string_end:
7400 if (it->current.overlay_string_index >= 0)
7402 /* IT->string is an overlay string. Advance to the
7403 next, if there is one. */
7404 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7406 it->ellipsis_p = 0;
7407 next_overlay_string (it);
7408 if (it->ellipsis_p)
7409 setup_for_ellipsis (it, 0);
7412 else
7414 /* IT->string is not an overlay string. If we reached
7415 its end, and there is something on IT->stack, proceed
7416 with what is on the stack. This can be either another
7417 string, this time an overlay string, or a buffer. */
7418 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7419 && it->sp > 0)
7421 pop_it (it);
7422 if (it->method == GET_FROM_STRING)
7423 goto consider_string_end;
7426 break;
7428 case GET_FROM_IMAGE:
7429 case GET_FROM_STRETCH:
7430 /* The position etc with which we have to proceed are on
7431 the stack. The position may be at the end of a string,
7432 if the `display' property takes up the whole string. */
7433 eassert (it->sp > 0);
7434 pop_it (it);
7435 if (it->method == GET_FROM_STRING)
7436 goto consider_string_end;
7437 break;
7439 default:
7440 /* There are no other methods defined, so this should be a bug. */
7441 emacs_abort ();
7444 eassert (it->method != GET_FROM_STRING
7445 || (STRINGP (it->string)
7446 && IT_STRING_CHARPOS (*it) >= 0));
7449 /* Load IT's display element fields with information about the next
7450 display element which comes from a display table entry or from the
7451 result of translating a control character to one of the forms `^C'
7452 or `\003'.
7454 IT->dpvec holds the glyphs to return as characters.
7455 IT->saved_face_id holds the face id before the display vector--it
7456 is restored into IT->face_id in set_iterator_to_next. */
7458 static int
7459 next_element_from_display_vector (struct it *it)
7461 Lisp_Object gc;
7463 /* Precondition. */
7464 eassert (it->dpvec && it->current.dpvec_index >= 0);
7466 it->face_id = it->saved_face_id;
7468 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7469 That seemed totally bogus - so I changed it... */
7470 gc = it->dpvec[it->current.dpvec_index];
7472 if (GLYPH_CODE_P (gc))
7474 it->c = GLYPH_CODE_CHAR (gc);
7475 it->len = CHAR_BYTES (it->c);
7477 /* The entry may contain a face id to use. Such a face id is
7478 the id of a Lisp face, not a realized face. A face id of
7479 zero means no face is specified. */
7480 if (it->dpvec_face_id >= 0)
7481 it->face_id = it->dpvec_face_id;
7482 else
7484 int lface_id = GLYPH_CODE_FACE (gc);
7485 if (lface_id > 0)
7486 it->face_id = merge_faces (it->f, Qt, lface_id,
7487 it->saved_face_id);
7490 else
7491 /* Display table entry is invalid. Return a space. */
7492 it->c = ' ', it->len = 1;
7494 /* Don't change position and object of the iterator here. They are
7495 still the values of the character that had this display table
7496 entry or was translated, and that's what we want. */
7497 it->what = IT_CHARACTER;
7498 return 1;
7501 /* Get the first element of string/buffer in the visual order, after
7502 being reseated to a new position in a string or a buffer. */
7503 static void
7504 get_visually_first_element (struct it *it)
7506 int string_p = STRINGP (it->string) || it->s;
7507 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7508 ptrdiff_t bob = (string_p ? 0 : BEGV);
7510 if (STRINGP (it->string))
7512 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7513 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7515 else
7517 it->bidi_it.charpos = IT_CHARPOS (*it);
7518 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7521 if (it->bidi_it.charpos == eob)
7523 /* Nothing to do, but reset the FIRST_ELT flag, like
7524 bidi_paragraph_init does, because we are not going to
7525 call it. */
7526 it->bidi_it.first_elt = 0;
7528 else if (it->bidi_it.charpos == bob
7529 || (!string_p
7530 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7531 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7533 /* If we are at the beginning of a line/string, we can produce
7534 the next element right away. */
7535 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7536 bidi_move_to_visually_next (&it->bidi_it);
7538 else
7540 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7542 /* We need to prime the bidi iterator starting at the line's or
7543 string's beginning, before we will be able to produce the
7544 next element. */
7545 if (string_p)
7546 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7547 else
7548 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7549 IT_BYTEPOS (*it), -1,
7550 &it->bidi_it.bytepos);
7551 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7554 /* Now return to buffer/string position where we were asked
7555 to get the next display element, and produce that. */
7556 bidi_move_to_visually_next (&it->bidi_it);
7558 while (it->bidi_it.bytepos != orig_bytepos
7559 && it->bidi_it.charpos < eob);
7562 /* Adjust IT's position information to where we ended up. */
7563 if (STRINGP (it->string))
7565 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7566 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7568 else
7570 IT_CHARPOS (*it) = it->bidi_it.charpos;
7571 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7574 if (STRINGP (it->string) || !it->s)
7576 ptrdiff_t stop, charpos, bytepos;
7578 if (STRINGP (it->string))
7580 eassert (!it->s);
7581 stop = SCHARS (it->string);
7582 if (stop > it->end_charpos)
7583 stop = it->end_charpos;
7584 charpos = IT_STRING_CHARPOS (*it);
7585 bytepos = IT_STRING_BYTEPOS (*it);
7587 else
7589 stop = it->end_charpos;
7590 charpos = IT_CHARPOS (*it);
7591 bytepos = IT_BYTEPOS (*it);
7593 if (it->bidi_it.scan_dir < 0)
7594 stop = -1;
7595 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7596 it->string);
7600 /* Load IT with the next display element from Lisp string IT->string.
7601 IT->current.string_pos is the current position within the string.
7602 If IT->current.overlay_string_index >= 0, the Lisp string is an
7603 overlay string. */
7605 static int
7606 next_element_from_string (struct it *it)
7608 struct text_pos position;
7610 eassert (STRINGP (it->string));
7611 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7612 eassert (IT_STRING_CHARPOS (*it) >= 0);
7613 position = it->current.string_pos;
7615 /* With bidi reordering, the character to display might not be the
7616 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7617 that we were reseat()ed to a new string, whose paragraph
7618 direction is not known. */
7619 if (it->bidi_p && it->bidi_it.first_elt)
7621 get_visually_first_element (it);
7622 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7625 /* Time to check for invisible text? */
7626 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7628 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7630 if (!(!it->bidi_p
7631 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7632 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7634 /* With bidi non-linear iteration, we could find
7635 ourselves far beyond the last computed stop_charpos,
7636 with several other stop positions in between that we
7637 missed. Scan them all now, in buffer's logical
7638 order, until we find and handle the last stop_charpos
7639 that precedes our current position. */
7640 handle_stop_backwards (it, it->stop_charpos);
7641 return GET_NEXT_DISPLAY_ELEMENT (it);
7643 else
7645 if (it->bidi_p)
7647 /* Take note of the stop position we just moved
7648 across, for when we will move back across it. */
7649 it->prev_stop = it->stop_charpos;
7650 /* If we are at base paragraph embedding level, take
7651 note of the last stop position seen at this
7652 level. */
7653 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7654 it->base_level_stop = it->stop_charpos;
7656 handle_stop (it);
7658 /* Since a handler may have changed IT->method, we must
7659 recurse here. */
7660 return GET_NEXT_DISPLAY_ELEMENT (it);
7663 else if (it->bidi_p
7664 /* If we are before prev_stop, we may have overstepped
7665 on our way backwards a stop_pos, and if so, we need
7666 to handle that stop_pos. */
7667 && IT_STRING_CHARPOS (*it) < it->prev_stop
7668 /* We can sometimes back up for reasons that have nothing
7669 to do with bidi reordering. E.g., compositions. The
7670 code below is only needed when we are above the base
7671 embedding level, so test for that explicitly. */
7672 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7674 /* If we lost track of base_level_stop, we have no better
7675 place for handle_stop_backwards to start from than string
7676 beginning. This happens, e.g., when we were reseated to
7677 the previous screenful of text by vertical-motion. */
7678 if (it->base_level_stop <= 0
7679 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7680 it->base_level_stop = 0;
7681 handle_stop_backwards (it, it->base_level_stop);
7682 return GET_NEXT_DISPLAY_ELEMENT (it);
7686 if (it->current.overlay_string_index >= 0)
7688 /* Get the next character from an overlay string. In overlay
7689 strings, there is no field width or padding with spaces to
7690 do. */
7691 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7693 it->what = IT_EOB;
7694 return 0;
7696 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7697 IT_STRING_BYTEPOS (*it),
7698 it->bidi_it.scan_dir < 0
7699 ? -1
7700 : SCHARS (it->string))
7701 && next_element_from_composition (it))
7703 return 1;
7705 else if (STRING_MULTIBYTE (it->string))
7707 const unsigned char *s = (SDATA (it->string)
7708 + IT_STRING_BYTEPOS (*it));
7709 it->c = string_char_and_length (s, &it->len);
7711 else
7713 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7714 it->len = 1;
7717 else
7719 /* Get the next character from a Lisp string that is not an
7720 overlay string. Such strings come from the mode line, for
7721 example. We may have to pad with spaces, or truncate the
7722 string. See also next_element_from_c_string. */
7723 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7725 it->what = IT_EOB;
7726 return 0;
7728 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7730 /* Pad with spaces. */
7731 it->c = ' ', it->len = 1;
7732 CHARPOS (position) = BYTEPOS (position) = -1;
7734 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7735 IT_STRING_BYTEPOS (*it),
7736 it->bidi_it.scan_dir < 0
7737 ? -1
7738 : it->string_nchars)
7739 && next_element_from_composition (it))
7741 return 1;
7743 else if (STRING_MULTIBYTE (it->string))
7745 const unsigned char *s = (SDATA (it->string)
7746 + IT_STRING_BYTEPOS (*it));
7747 it->c = string_char_and_length (s, &it->len);
7749 else
7751 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7752 it->len = 1;
7756 /* Record what we have and where it came from. */
7757 it->what = IT_CHARACTER;
7758 it->object = it->string;
7759 it->position = position;
7760 return 1;
7764 /* Load IT with next display element from C string IT->s.
7765 IT->string_nchars is the maximum number of characters to return
7766 from the string. IT->end_charpos may be greater than
7767 IT->string_nchars when this function is called, in which case we
7768 may have to return padding spaces. Value is zero if end of string
7769 reached, including padding spaces. */
7771 static int
7772 next_element_from_c_string (struct it *it)
7774 int success_p = 1;
7776 eassert (it->s);
7777 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7778 it->what = IT_CHARACTER;
7779 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7780 it->object = Qnil;
7782 /* With bidi reordering, the character to display might not be the
7783 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7784 we were reseated to a new string, whose paragraph direction is
7785 not known. */
7786 if (it->bidi_p && it->bidi_it.first_elt)
7787 get_visually_first_element (it);
7789 /* IT's position can be greater than IT->string_nchars in case a
7790 field width or precision has been specified when the iterator was
7791 initialized. */
7792 if (IT_CHARPOS (*it) >= it->end_charpos)
7794 /* End of the game. */
7795 it->what = IT_EOB;
7796 success_p = 0;
7798 else if (IT_CHARPOS (*it) >= it->string_nchars)
7800 /* Pad with spaces. */
7801 it->c = ' ', it->len = 1;
7802 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7804 else if (it->multibyte_p)
7805 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7806 else
7807 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7809 return success_p;
7813 /* Set up IT to return characters from an ellipsis, if appropriate.
7814 The definition of the ellipsis glyphs may come from a display table
7815 entry. This function fills IT with the first glyph from the
7816 ellipsis if an ellipsis is to be displayed. */
7818 static int
7819 next_element_from_ellipsis (struct it *it)
7821 if (it->selective_display_ellipsis_p)
7822 setup_for_ellipsis (it, it->len);
7823 else
7825 /* The face at the current position may be different from the
7826 face we find after the invisible text. Remember what it
7827 was in IT->saved_face_id, and signal that it's there by
7828 setting face_before_selective_p. */
7829 it->saved_face_id = it->face_id;
7830 it->method = GET_FROM_BUFFER;
7831 it->object = it->w->contents;
7832 reseat_at_next_visible_line_start (it, 1);
7833 it->face_before_selective_p = 1;
7836 return GET_NEXT_DISPLAY_ELEMENT (it);
7840 /* Deliver an image display element. The iterator IT is already
7841 filled with image information (done in handle_display_prop). Value
7842 is always 1. */
7845 static int
7846 next_element_from_image (struct it *it)
7848 it->what = IT_IMAGE;
7849 it->ignore_overlay_strings_at_pos_p = 0;
7850 return 1;
7854 /* Fill iterator IT with next display element from a stretch glyph
7855 property. IT->object is the value of the text property. Value is
7856 always 1. */
7858 static int
7859 next_element_from_stretch (struct it *it)
7861 it->what = IT_STRETCH;
7862 return 1;
7865 /* Scan backwards from IT's current position until we find a stop
7866 position, or until BEGV. This is called when we find ourself
7867 before both the last known prev_stop and base_level_stop while
7868 reordering bidirectional text. */
7870 static void
7871 compute_stop_pos_backwards (struct it *it)
7873 const int SCAN_BACK_LIMIT = 1000;
7874 struct text_pos pos;
7875 struct display_pos save_current = it->current;
7876 struct text_pos save_position = it->position;
7877 ptrdiff_t charpos = IT_CHARPOS (*it);
7878 ptrdiff_t where_we_are = charpos;
7879 ptrdiff_t save_stop_pos = it->stop_charpos;
7880 ptrdiff_t save_end_pos = it->end_charpos;
7882 eassert (NILP (it->string) && !it->s);
7883 eassert (it->bidi_p);
7884 it->bidi_p = 0;
7887 it->end_charpos = min (charpos + 1, ZV);
7888 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7889 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7890 reseat_1 (it, pos, 0);
7891 compute_stop_pos (it);
7892 /* We must advance forward, right? */
7893 if (it->stop_charpos <= charpos)
7894 emacs_abort ();
7896 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7898 if (it->stop_charpos <= where_we_are)
7899 it->prev_stop = it->stop_charpos;
7900 else
7901 it->prev_stop = BEGV;
7902 it->bidi_p = 1;
7903 it->current = save_current;
7904 it->position = save_position;
7905 it->stop_charpos = save_stop_pos;
7906 it->end_charpos = save_end_pos;
7909 /* Scan forward from CHARPOS in the current buffer/string, until we
7910 find a stop position > current IT's position. Then handle the stop
7911 position before that. This is called when we bump into a stop
7912 position while reordering bidirectional text. CHARPOS should be
7913 the last previously processed stop_pos (or BEGV/0, if none were
7914 processed yet) whose position is less that IT's current
7915 position. */
7917 static void
7918 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7920 int bufp = !STRINGP (it->string);
7921 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7922 struct display_pos save_current = it->current;
7923 struct text_pos save_position = it->position;
7924 struct text_pos pos1;
7925 ptrdiff_t next_stop;
7927 /* Scan in strict logical order. */
7928 eassert (it->bidi_p);
7929 it->bidi_p = 0;
7932 it->prev_stop = charpos;
7933 if (bufp)
7935 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7936 reseat_1 (it, pos1, 0);
7938 else
7939 it->current.string_pos = string_pos (charpos, it->string);
7940 compute_stop_pos (it);
7941 /* We must advance forward, right? */
7942 if (it->stop_charpos <= it->prev_stop)
7943 emacs_abort ();
7944 charpos = it->stop_charpos;
7946 while (charpos <= where_we_are);
7948 it->bidi_p = 1;
7949 it->current = save_current;
7950 it->position = save_position;
7951 next_stop = it->stop_charpos;
7952 it->stop_charpos = it->prev_stop;
7953 handle_stop (it);
7954 it->stop_charpos = next_stop;
7957 /* Load IT with the next display element from current_buffer. Value
7958 is zero if end of buffer reached. IT->stop_charpos is the next
7959 position at which to stop and check for text properties or buffer
7960 end. */
7962 static int
7963 next_element_from_buffer (struct it *it)
7965 int success_p = 1;
7967 eassert (IT_CHARPOS (*it) >= BEGV);
7968 eassert (NILP (it->string) && !it->s);
7969 eassert (!it->bidi_p
7970 || (EQ (it->bidi_it.string.lstring, Qnil)
7971 && it->bidi_it.string.s == NULL));
7973 /* With bidi reordering, the character to display might not be the
7974 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7975 we were reseat()ed to a new buffer position, which is potentially
7976 a different paragraph. */
7977 if (it->bidi_p && it->bidi_it.first_elt)
7979 get_visually_first_element (it);
7980 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7983 if (IT_CHARPOS (*it) >= it->stop_charpos)
7985 if (IT_CHARPOS (*it) >= it->end_charpos)
7987 int overlay_strings_follow_p;
7989 /* End of the game, except when overlay strings follow that
7990 haven't been returned yet. */
7991 if (it->overlay_strings_at_end_processed_p)
7992 overlay_strings_follow_p = 0;
7993 else
7995 it->overlay_strings_at_end_processed_p = 1;
7996 overlay_strings_follow_p = get_overlay_strings (it, 0);
7999 if (overlay_strings_follow_p)
8000 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8001 else
8003 it->what = IT_EOB;
8004 it->position = it->current.pos;
8005 success_p = 0;
8008 else if (!(!it->bidi_p
8009 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8010 || IT_CHARPOS (*it) == it->stop_charpos))
8012 /* With bidi non-linear iteration, we could find ourselves
8013 far beyond the last computed stop_charpos, with several
8014 other stop positions in between that we missed. Scan
8015 them all now, in buffer's logical order, until we find
8016 and handle the last stop_charpos that precedes our
8017 current position. */
8018 handle_stop_backwards (it, it->stop_charpos);
8019 return GET_NEXT_DISPLAY_ELEMENT (it);
8021 else
8023 if (it->bidi_p)
8025 /* Take note of the stop position we just moved across,
8026 for when we will move back across it. */
8027 it->prev_stop = it->stop_charpos;
8028 /* If we are at base paragraph embedding level, take
8029 note of the last stop position seen at this
8030 level. */
8031 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8032 it->base_level_stop = it->stop_charpos;
8034 handle_stop (it);
8035 return GET_NEXT_DISPLAY_ELEMENT (it);
8038 else if (it->bidi_p
8039 /* If we are before prev_stop, we may have overstepped on
8040 our way backwards a stop_pos, and if so, we need to
8041 handle that stop_pos. */
8042 && IT_CHARPOS (*it) < it->prev_stop
8043 /* We can sometimes back up for reasons that have nothing
8044 to do with bidi reordering. E.g., compositions. The
8045 code below is only needed when we are above the base
8046 embedding level, so test for that explicitly. */
8047 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8049 if (it->base_level_stop <= 0
8050 || IT_CHARPOS (*it) < it->base_level_stop)
8052 /* If we lost track of base_level_stop, we need to find
8053 prev_stop by looking backwards. This happens, e.g., when
8054 we were reseated to the previous screenful of text by
8055 vertical-motion. */
8056 it->base_level_stop = BEGV;
8057 compute_stop_pos_backwards (it);
8058 handle_stop_backwards (it, it->prev_stop);
8060 else
8061 handle_stop_backwards (it, it->base_level_stop);
8062 return GET_NEXT_DISPLAY_ELEMENT (it);
8064 else
8066 /* No face changes, overlays etc. in sight, so just return a
8067 character from current_buffer. */
8068 unsigned char *p;
8069 ptrdiff_t stop;
8071 /* Maybe run the redisplay end trigger hook. Performance note:
8072 This doesn't seem to cost measurable time. */
8073 if (it->redisplay_end_trigger_charpos
8074 && it->glyph_row
8075 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8076 run_redisplay_end_trigger_hook (it);
8078 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8079 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8080 stop)
8081 && next_element_from_composition (it))
8083 return 1;
8086 /* Get the next character, maybe multibyte. */
8087 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8088 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8089 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8090 else
8091 it->c = *p, it->len = 1;
8093 /* Record what we have and where it came from. */
8094 it->what = IT_CHARACTER;
8095 it->object = it->w->contents;
8096 it->position = it->current.pos;
8098 /* Normally we return the character found above, except when we
8099 really want to return an ellipsis for selective display. */
8100 if (it->selective)
8102 if (it->c == '\n')
8104 /* A value of selective > 0 means hide lines indented more
8105 than that number of columns. */
8106 if (it->selective > 0
8107 && IT_CHARPOS (*it) + 1 < ZV
8108 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8109 IT_BYTEPOS (*it) + 1,
8110 it->selective))
8112 success_p = next_element_from_ellipsis (it);
8113 it->dpvec_char_len = -1;
8116 else if (it->c == '\r' && it->selective == -1)
8118 /* A value of selective == -1 means that everything from the
8119 CR to the end of the line is invisible, with maybe an
8120 ellipsis displayed for it. */
8121 success_p = next_element_from_ellipsis (it);
8122 it->dpvec_char_len = -1;
8127 /* Value is zero if end of buffer reached. */
8128 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8129 return success_p;
8133 /* Run the redisplay end trigger hook for IT. */
8135 static void
8136 run_redisplay_end_trigger_hook (struct it *it)
8138 Lisp_Object args[3];
8140 /* IT->glyph_row should be non-null, i.e. we should be actually
8141 displaying something, or otherwise we should not run the hook. */
8142 eassert (it->glyph_row);
8144 /* Set up hook arguments. */
8145 args[0] = Qredisplay_end_trigger_functions;
8146 args[1] = it->window;
8147 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8148 it->redisplay_end_trigger_charpos = 0;
8150 /* Since we are *trying* to run these functions, don't try to run
8151 them again, even if they get an error. */
8152 wset_redisplay_end_trigger (it->w, Qnil);
8153 Frun_hook_with_args (3, args);
8155 /* Notice if it changed the face of the character we are on. */
8156 handle_face_prop (it);
8160 /* Deliver a composition display element. Unlike the other
8161 next_element_from_XXX, this function is not registered in the array
8162 get_next_element[]. It is called from next_element_from_buffer and
8163 next_element_from_string when necessary. */
8165 static int
8166 next_element_from_composition (struct it *it)
8168 it->what = IT_COMPOSITION;
8169 it->len = it->cmp_it.nbytes;
8170 if (STRINGP (it->string))
8172 if (it->c < 0)
8174 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8175 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8176 return 0;
8178 it->position = it->current.string_pos;
8179 it->object = it->string;
8180 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8181 IT_STRING_BYTEPOS (*it), it->string);
8183 else
8185 if (it->c < 0)
8187 IT_CHARPOS (*it) += it->cmp_it.nchars;
8188 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8189 if (it->bidi_p)
8191 if (it->bidi_it.new_paragraph)
8192 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8193 /* Resync the bidi iterator with IT's new position.
8194 FIXME: this doesn't support bidirectional text. */
8195 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8196 bidi_move_to_visually_next (&it->bidi_it);
8198 return 0;
8200 it->position = it->current.pos;
8201 it->object = it->w->contents;
8202 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8203 IT_BYTEPOS (*it), Qnil);
8205 return 1;
8210 /***********************************************************************
8211 Moving an iterator without producing glyphs
8212 ***********************************************************************/
8214 /* Check if iterator is at a position corresponding to a valid buffer
8215 position after some move_it_ call. */
8217 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8218 ((it)->method == GET_FROM_STRING \
8219 ? IT_STRING_CHARPOS (*it) == 0 \
8220 : 1)
8223 /* Move iterator IT to a specified buffer or X position within one
8224 line on the display without producing glyphs.
8226 OP should be a bit mask including some or all of these bits:
8227 MOVE_TO_X: Stop upon reaching x-position TO_X.
8228 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8229 Regardless of OP's value, stop upon reaching the end of the display line.
8231 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8232 This means, in particular, that TO_X includes window's horizontal
8233 scroll amount.
8235 The return value has several possible values that
8236 say what condition caused the scan to stop:
8238 MOVE_POS_MATCH_OR_ZV
8239 - when TO_POS or ZV was reached.
8241 MOVE_X_REACHED
8242 -when TO_X was reached before TO_POS or ZV were reached.
8244 MOVE_LINE_CONTINUED
8245 - when we reached the end of the display area and the line must
8246 be continued.
8248 MOVE_LINE_TRUNCATED
8249 - when we reached the end of the display area and the line is
8250 truncated.
8252 MOVE_NEWLINE_OR_CR
8253 - when we stopped at a line end, i.e. a newline or a CR and selective
8254 display is on. */
8256 static enum move_it_result
8257 move_it_in_display_line_to (struct it *it,
8258 ptrdiff_t to_charpos, int to_x,
8259 enum move_operation_enum op)
8261 enum move_it_result result = MOVE_UNDEFINED;
8262 struct glyph_row *saved_glyph_row;
8263 struct it wrap_it, atpos_it, atx_it, ppos_it;
8264 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8265 void *ppos_data = NULL;
8266 int may_wrap = 0;
8267 enum it_method prev_method = it->method;
8268 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8269 int saw_smaller_pos = prev_pos < to_charpos;
8271 /* Don't produce glyphs in produce_glyphs. */
8272 saved_glyph_row = it->glyph_row;
8273 it->glyph_row = NULL;
8275 /* Use wrap_it to save a copy of IT wherever a word wrap could
8276 occur. Use atpos_it to save a copy of IT at the desired buffer
8277 position, if found, so that we can scan ahead and check if the
8278 word later overshoots the window edge. Use atx_it similarly, for
8279 pixel positions. */
8280 wrap_it.sp = -1;
8281 atpos_it.sp = -1;
8282 atx_it.sp = -1;
8284 /* Use ppos_it under bidi reordering to save a copy of IT for the
8285 position > CHARPOS that is the closest to CHARPOS. We restore
8286 that position in IT when we have scanned the entire display line
8287 without finding a match for CHARPOS and all the character
8288 positions are greater than CHARPOS. */
8289 if (it->bidi_p)
8291 SAVE_IT (ppos_it, *it, ppos_data);
8292 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8293 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8294 SAVE_IT (ppos_it, *it, ppos_data);
8297 #define BUFFER_POS_REACHED_P() \
8298 ((op & MOVE_TO_POS) != 0 \
8299 && BUFFERP (it->object) \
8300 && (IT_CHARPOS (*it) == to_charpos \
8301 || ((!it->bidi_p \
8302 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8303 && IT_CHARPOS (*it) > to_charpos) \
8304 || (it->what == IT_COMPOSITION \
8305 && ((IT_CHARPOS (*it) > to_charpos \
8306 && to_charpos >= it->cmp_it.charpos) \
8307 || (IT_CHARPOS (*it) < to_charpos \
8308 && to_charpos <= it->cmp_it.charpos)))) \
8309 && (it->method == GET_FROM_BUFFER \
8310 || (it->method == GET_FROM_DISPLAY_VECTOR \
8311 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8313 /* If there's a line-/wrap-prefix, handle it. */
8314 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8315 && it->current_y < it->last_visible_y)
8316 handle_line_prefix (it);
8318 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8319 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8321 while (1)
8323 int x, i, ascent = 0, descent = 0;
8325 /* Utility macro to reset an iterator with x, ascent, and descent. */
8326 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8327 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8328 (IT)->max_descent = descent)
8330 /* Stop if we move beyond TO_CHARPOS (after an image or a
8331 display string or stretch glyph). */
8332 if ((op & MOVE_TO_POS) != 0
8333 && BUFFERP (it->object)
8334 && it->method == GET_FROM_BUFFER
8335 && (((!it->bidi_p
8336 /* When the iterator is at base embedding level, we
8337 are guaranteed that characters are delivered for
8338 display in strictly increasing order of their
8339 buffer positions. */
8340 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8341 && IT_CHARPOS (*it) > to_charpos)
8342 || (it->bidi_p
8343 && (prev_method == GET_FROM_IMAGE
8344 || prev_method == GET_FROM_STRETCH
8345 || prev_method == GET_FROM_STRING)
8346 /* Passed TO_CHARPOS from left to right. */
8347 && ((prev_pos < to_charpos
8348 && IT_CHARPOS (*it) > to_charpos)
8349 /* Passed TO_CHARPOS from right to left. */
8350 || (prev_pos > to_charpos
8351 && IT_CHARPOS (*it) < to_charpos)))))
8353 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8355 result = MOVE_POS_MATCH_OR_ZV;
8356 break;
8358 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8359 /* If wrap_it is valid, the current position might be in a
8360 word that is wrapped. So, save the iterator in
8361 atpos_it and continue to see if wrapping happens. */
8362 SAVE_IT (atpos_it, *it, atpos_data);
8365 /* Stop when ZV reached.
8366 We used to stop here when TO_CHARPOS reached as well, but that is
8367 too soon if this glyph does not fit on this line. So we handle it
8368 explicitly below. */
8369 if (!get_next_display_element (it))
8371 result = MOVE_POS_MATCH_OR_ZV;
8372 break;
8375 if (it->line_wrap == TRUNCATE)
8377 if (BUFFER_POS_REACHED_P ())
8379 result = MOVE_POS_MATCH_OR_ZV;
8380 break;
8383 else
8385 if (it->line_wrap == WORD_WRAP)
8387 if (IT_DISPLAYING_WHITESPACE (it))
8388 may_wrap = 1;
8389 else if (may_wrap)
8391 /* We have reached a glyph that follows one or more
8392 whitespace characters. If the position is
8393 already found, we are done. */
8394 if (atpos_it.sp >= 0)
8396 RESTORE_IT (it, &atpos_it, atpos_data);
8397 result = MOVE_POS_MATCH_OR_ZV;
8398 goto done;
8400 if (atx_it.sp >= 0)
8402 RESTORE_IT (it, &atx_it, atx_data);
8403 result = MOVE_X_REACHED;
8404 goto done;
8406 /* Otherwise, we can wrap here. */
8407 SAVE_IT (wrap_it, *it, wrap_data);
8408 may_wrap = 0;
8413 /* Remember the line height for the current line, in case
8414 the next element doesn't fit on the line. */
8415 ascent = it->max_ascent;
8416 descent = it->max_descent;
8418 /* The call to produce_glyphs will get the metrics of the
8419 display element IT is loaded with. Record the x-position
8420 before this display element, in case it doesn't fit on the
8421 line. */
8422 x = it->current_x;
8424 PRODUCE_GLYPHS (it);
8426 if (it->area != TEXT_AREA)
8428 prev_method = it->method;
8429 if (it->method == GET_FROM_BUFFER)
8430 prev_pos = IT_CHARPOS (*it);
8431 set_iterator_to_next (it, 1);
8432 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8433 SET_TEXT_POS (this_line_min_pos,
8434 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8435 if (it->bidi_p
8436 && (op & MOVE_TO_POS)
8437 && IT_CHARPOS (*it) > to_charpos
8438 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8439 SAVE_IT (ppos_it, *it, ppos_data);
8440 continue;
8443 /* The number of glyphs we get back in IT->nglyphs will normally
8444 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8445 character on a terminal frame, or (iii) a line end. For the
8446 second case, IT->nglyphs - 1 padding glyphs will be present.
8447 (On X frames, there is only one glyph produced for a
8448 composite character.)
8450 The behavior implemented below means, for continuation lines,
8451 that as many spaces of a TAB as fit on the current line are
8452 displayed there. For terminal frames, as many glyphs of a
8453 multi-glyph character are displayed in the current line, too.
8454 This is what the old redisplay code did, and we keep it that
8455 way. Under X, the whole shape of a complex character must
8456 fit on the line or it will be completely displayed in the
8457 next line.
8459 Note that both for tabs and padding glyphs, all glyphs have
8460 the same width. */
8461 if (it->nglyphs)
8463 /* More than one glyph or glyph doesn't fit on line. All
8464 glyphs have the same width. */
8465 int single_glyph_width = it->pixel_width / it->nglyphs;
8466 int new_x;
8467 int x_before_this_char = x;
8468 int hpos_before_this_char = it->hpos;
8470 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8472 new_x = x + single_glyph_width;
8474 /* We want to leave anything reaching TO_X to the caller. */
8475 if ((op & MOVE_TO_X) && new_x > to_x)
8477 if (BUFFER_POS_REACHED_P ())
8479 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8480 goto buffer_pos_reached;
8481 if (atpos_it.sp < 0)
8483 SAVE_IT (atpos_it, *it, atpos_data);
8484 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8487 else
8489 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8491 it->current_x = x;
8492 result = MOVE_X_REACHED;
8493 break;
8495 if (atx_it.sp < 0)
8497 SAVE_IT (atx_it, *it, atx_data);
8498 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8503 if (/* Lines are continued. */
8504 it->line_wrap != TRUNCATE
8505 && (/* And glyph doesn't fit on the line. */
8506 new_x > it->last_visible_x
8507 /* Or it fits exactly and we're on a window
8508 system frame. */
8509 || (new_x == it->last_visible_x
8510 && FRAME_WINDOW_P (it->f)
8511 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8512 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8513 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8515 if (/* IT->hpos == 0 means the very first glyph
8516 doesn't fit on the line, e.g. a wide image. */
8517 it->hpos == 0
8518 || (new_x == it->last_visible_x
8519 && FRAME_WINDOW_P (it->f)))
8521 ++it->hpos;
8522 it->current_x = new_x;
8524 /* The character's last glyph just barely fits
8525 in this row. */
8526 if (i == it->nglyphs - 1)
8528 /* If this is the destination position,
8529 return a position *before* it in this row,
8530 now that we know it fits in this row. */
8531 if (BUFFER_POS_REACHED_P ())
8533 if (it->line_wrap != WORD_WRAP
8534 || wrap_it.sp < 0)
8536 it->hpos = hpos_before_this_char;
8537 it->current_x = x_before_this_char;
8538 result = MOVE_POS_MATCH_OR_ZV;
8539 break;
8541 if (it->line_wrap == WORD_WRAP
8542 && atpos_it.sp < 0)
8544 SAVE_IT (atpos_it, *it, atpos_data);
8545 atpos_it.current_x = x_before_this_char;
8546 atpos_it.hpos = hpos_before_this_char;
8550 prev_method = it->method;
8551 if (it->method == GET_FROM_BUFFER)
8552 prev_pos = IT_CHARPOS (*it);
8553 set_iterator_to_next (it, 1);
8554 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8555 SET_TEXT_POS (this_line_min_pos,
8556 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8557 /* On graphical terminals, newlines may
8558 "overflow" into the fringe if
8559 overflow-newline-into-fringe is non-nil.
8560 On text terminals, and on graphical
8561 terminals with no right margin, newlines
8562 may overflow into the last glyph on the
8563 display line.*/
8564 if (!FRAME_WINDOW_P (it->f)
8565 || ((it->bidi_p
8566 && it->bidi_it.paragraph_dir == R2L)
8567 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8568 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8569 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8571 if (!get_next_display_element (it))
8573 result = MOVE_POS_MATCH_OR_ZV;
8574 break;
8576 if (BUFFER_POS_REACHED_P ())
8578 if (ITERATOR_AT_END_OF_LINE_P (it))
8579 result = MOVE_POS_MATCH_OR_ZV;
8580 else
8581 result = MOVE_LINE_CONTINUED;
8582 break;
8584 if (ITERATOR_AT_END_OF_LINE_P (it)
8585 && (it->line_wrap != WORD_WRAP
8586 || wrap_it.sp < 0))
8588 result = MOVE_NEWLINE_OR_CR;
8589 break;
8594 else
8595 IT_RESET_X_ASCENT_DESCENT (it);
8597 if (wrap_it.sp >= 0)
8599 RESTORE_IT (it, &wrap_it, wrap_data);
8600 atpos_it.sp = -1;
8601 atx_it.sp = -1;
8604 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8605 IT_CHARPOS (*it)));
8606 result = MOVE_LINE_CONTINUED;
8607 break;
8610 if (BUFFER_POS_REACHED_P ())
8612 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8613 goto buffer_pos_reached;
8614 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8616 SAVE_IT (atpos_it, *it, atpos_data);
8617 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8621 if (new_x > it->first_visible_x)
8623 /* Glyph is visible. Increment number of glyphs that
8624 would be displayed. */
8625 ++it->hpos;
8629 if (result != MOVE_UNDEFINED)
8630 break;
8632 else if (BUFFER_POS_REACHED_P ())
8634 buffer_pos_reached:
8635 IT_RESET_X_ASCENT_DESCENT (it);
8636 result = MOVE_POS_MATCH_OR_ZV;
8637 break;
8639 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8641 /* Stop when TO_X specified and reached. This check is
8642 necessary here because of lines consisting of a line end,
8643 only. The line end will not produce any glyphs and we
8644 would never get MOVE_X_REACHED. */
8645 eassert (it->nglyphs == 0);
8646 result = MOVE_X_REACHED;
8647 break;
8650 /* Is this a line end? If yes, we're done. */
8651 if (ITERATOR_AT_END_OF_LINE_P (it))
8653 /* If we are past TO_CHARPOS, but never saw any character
8654 positions smaller than TO_CHARPOS, return
8655 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8656 did. */
8657 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8659 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8661 if (IT_CHARPOS (ppos_it) < ZV)
8663 RESTORE_IT (it, &ppos_it, ppos_data);
8664 result = MOVE_POS_MATCH_OR_ZV;
8666 else
8667 goto buffer_pos_reached;
8669 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8670 && IT_CHARPOS (*it) > to_charpos)
8671 goto buffer_pos_reached;
8672 else
8673 result = MOVE_NEWLINE_OR_CR;
8675 else
8676 result = MOVE_NEWLINE_OR_CR;
8677 break;
8680 prev_method = it->method;
8681 if (it->method == GET_FROM_BUFFER)
8682 prev_pos = IT_CHARPOS (*it);
8683 /* The current display element has been consumed. Advance
8684 to the next. */
8685 set_iterator_to_next (it, 1);
8686 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8687 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8688 if (IT_CHARPOS (*it) < to_charpos)
8689 saw_smaller_pos = 1;
8690 if (it->bidi_p
8691 && (op & MOVE_TO_POS)
8692 && IT_CHARPOS (*it) >= to_charpos
8693 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8694 SAVE_IT (ppos_it, *it, ppos_data);
8696 /* Stop if lines are truncated and IT's current x-position is
8697 past the right edge of the window now. */
8698 if (it->line_wrap == TRUNCATE
8699 && it->current_x >= it->last_visible_x)
8701 if (!FRAME_WINDOW_P (it->f)
8702 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8703 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8704 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8705 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8707 int at_eob_p = 0;
8709 if ((at_eob_p = !get_next_display_element (it))
8710 || BUFFER_POS_REACHED_P ()
8711 /* If we are past TO_CHARPOS, but never saw any
8712 character positions smaller than TO_CHARPOS,
8713 return MOVE_POS_MATCH_OR_ZV, like the
8714 unidirectional display did. */
8715 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8716 && !saw_smaller_pos
8717 && IT_CHARPOS (*it) > to_charpos))
8719 if (it->bidi_p
8720 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8721 RESTORE_IT (it, &ppos_it, ppos_data);
8722 result = MOVE_POS_MATCH_OR_ZV;
8723 break;
8725 if (ITERATOR_AT_END_OF_LINE_P (it))
8727 result = MOVE_NEWLINE_OR_CR;
8728 break;
8731 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8732 && !saw_smaller_pos
8733 && IT_CHARPOS (*it) > to_charpos)
8735 if (IT_CHARPOS (ppos_it) < ZV)
8736 RESTORE_IT (it, &ppos_it, ppos_data);
8737 result = MOVE_POS_MATCH_OR_ZV;
8738 break;
8740 result = MOVE_LINE_TRUNCATED;
8741 break;
8743 #undef IT_RESET_X_ASCENT_DESCENT
8746 #undef BUFFER_POS_REACHED_P
8748 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8749 restore the saved iterator. */
8750 if (atpos_it.sp >= 0)
8751 RESTORE_IT (it, &atpos_it, atpos_data);
8752 else if (atx_it.sp >= 0)
8753 RESTORE_IT (it, &atx_it, atx_data);
8755 done:
8757 if (atpos_data)
8758 bidi_unshelve_cache (atpos_data, 1);
8759 if (atx_data)
8760 bidi_unshelve_cache (atx_data, 1);
8761 if (wrap_data)
8762 bidi_unshelve_cache (wrap_data, 1);
8763 if (ppos_data)
8764 bidi_unshelve_cache (ppos_data, 1);
8766 /* Restore the iterator settings altered at the beginning of this
8767 function. */
8768 it->glyph_row = saved_glyph_row;
8769 return result;
8772 /* For external use. */
8773 void
8774 move_it_in_display_line (struct it *it,
8775 ptrdiff_t to_charpos, int to_x,
8776 enum move_operation_enum op)
8778 if (it->line_wrap == WORD_WRAP
8779 && (op & MOVE_TO_X))
8781 struct it save_it;
8782 void *save_data = NULL;
8783 int skip;
8785 SAVE_IT (save_it, *it, save_data);
8786 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8787 /* When word-wrap is on, TO_X may lie past the end
8788 of a wrapped line. Then it->current is the
8789 character on the next line, so backtrack to the
8790 space before the wrap point. */
8791 if (skip == MOVE_LINE_CONTINUED)
8793 int prev_x = max (it->current_x - 1, 0);
8794 RESTORE_IT (it, &save_it, save_data);
8795 move_it_in_display_line_to
8796 (it, -1, prev_x, MOVE_TO_X);
8798 else
8799 bidi_unshelve_cache (save_data, 1);
8801 else
8802 move_it_in_display_line_to (it, to_charpos, to_x, op);
8806 /* Move IT forward until it satisfies one or more of the criteria in
8807 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8809 OP is a bit-mask that specifies where to stop, and in particular,
8810 which of those four position arguments makes a difference. See the
8811 description of enum move_operation_enum.
8813 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8814 screen line, this function will set IT to the next position that is
8815 displayed to the right of TO_CHARPOS on the screen. */
8817 void
8818 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8820 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8821 int line_height, line_start_x = 0, reached = 0;
8822 void *backup_data = NULL;
8824 for (;;)
8826 if (op & MOVE_TO_VPOS)
8828 /* If no TO_CHARPOS and no TO_X specified, stop at the
8829 start of the line TO_VPOS. */
8830 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8832 if (it->vpos == to_vpos)
8834 reached = 1;
8835 break;
8837 else
8838 skip = move_it_in_display_line_to (it, -1, -1, 0);
8840 else
8842 /* TO_VPOS >= 0 means stop at TO_X in the line at
8843 TO_VPOS, or at TO_POS, whichever comes first. */
8844 if (it->vpos == to_vpos)
8846 reached = 2;
8847 break;
8850 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8852 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8854 reached = 3;
8855 break;
8857 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8859 /* We have reached TO_X but not in the line we want. */
8860 skip = move_it_in_display_line_to (it, to_charpos,
8861 -1, MOVE_TO_POS);
8862 if (skip == MOVE_POS_MATCH_OR_ZV)
8864 reached = 4;
8865 break;
8870 else if (op & MOVE_TO_Y)
8872 struct it it_backup;
8874 if (it->line_wrap == WORD_WRAP)
8875 SAVE_IT (it_backup, *it, backup_data);
8877 /* TO_Y specified means stop at TO_X in the line containing
8878 TO_Y---or at TO_CHARPOS if this is reached first. The
8879 problem is that we can't really tell whether the line
8880 contains TO_Y before we have completely scanned it, and
8881 this may skip past TO_X. What we do is to first scan to
8882 TO_X.
8884 If TO_X is not specified, use a TO_X of zero. The reason
8885 is to make the outcome of this function more predictable.
8886 If we didn't use TO_X == 0, we would stop at the end of
8887 the line which is probably not what a caller would expect
8888 to happen. */
8889 skip = move_it_in_display_line_to
8890 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8891 (MOVE_TO_X | (op & MOVE_TO_POS)));
8893 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8894 if (skip == MOVE_POS_MATCH_OR_ZV)
8895 reached = 5;
8896 else if (skip == MOVE_X_REACHED)
8898 /* If TO_X was reached, we want to know whether TO_Y is
8899 in the line. We know this is the case if the already
8900 scanned glyphs make the line tall enough. Otherwise,
8901 we must check by scanning the rest of the line. */
8902 line_height = it->max_ascent + it->max_descent;
8903 if (to_y >= it->current_y
8904 && to_y < it->current_y + line_height)
8906 reached = 6;
8907 break;
8909 SAVE_IT (it_backup, *it, backup_data);
8910 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8911 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8912 op & MOVE_TO_POS);
8913 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8914 line_height = it->max_ascent + it->max_descent;
8915 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8917 if (to_y >= it->current_y
8918 && to_y < it->current_y + line_height)
8920 /* If TO_Y is in this line and TO_X was reached
8921 above, we scanned too far. We have to restore
8922 IT's settings to the ones before skipping. But
8923 keep the more accurate values of max_ascent and
8924 max_descent we've found while skipping the rest
8925 of the line, for the sake of callers, such as
8926 pos_visible_p, that need to know the line
8927 height. */
8928 int max_ascent = it->max_ascent;
8929 int max_descent = it->max_descent;
8931 RESTORE_IT (it, &it_backup, backup_data);
8932 it->max_ascent = max_ascent;
8933 it->max_descent = max_descent;
8934 reached = 6;
8936 else
8938 skip = skip2;
8939 if (skip == MOVE_POS_MATCH_OR_ZV)
8940 reached = 7;
8943 else
8945 /* Check whether TO_Y is in this line. */
8946 line_height = it->max_ascent + it->max_descent;
8947 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8949 if (to_y >= it->current_y
8950 && to_y < it->current_y + line_height)
8952 /* When word-wrap is on, TO_X may lie past the end
8953 of a wrapped line. Then it->current is the
8954 character on the next line, so backtrack to the
8955 space before the wrap point. */
8956 if (skip == MOVE_LINE_CONTINUED
8957 && it->line_wrap == WORD_WRAP)
8959 int prev_x = max (it->current_x - 1, 0);
8960 RESTORE_IT (it, &it_backup, backup_data);
8961 skip = move_it_in_display_line_to
8962 (it, -1, prev_x, MOVE_TO_X);
8964 reached = 6;
8968 if (reached)
8969 break;
8971 else if (BUFFERP (it->object)
8972 && (it->method == GET_FROM_BUFFER
8973 || it->method == GET_FROM_STRETCH)
8974 && IT_CHARPOS (*it) >= to_charpos
8975 /* Under bidi iteration, a call to set_iterator_to_next
8976 can scan far beyond to_charpos if the initial
8977 portion of the next line needs to be reordered. In
8978 that case, give move_it_in_display_line_to another
8979 chance below. */
8980 && !(it->bidi_p
8981 && it->bidi_it.scan_dir == -1))
8982 skip = MOVE_POS_MATCH_OR_ZV;
8983 else
8984 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8986 switch (skip)
8988 case MOVE_POS_MATCH_OR_ZV:
8989 reached = 8;
8990 goto out;
8992 case MOVE_NEWLINE_OR_CR:
8993 set_iterator_to_next (it, 1);
8994 it->continuation_lines_width = 0;
8995 break;
8997 case MOVE_LINE_TRUNCATED:
8998 it->continuation_lines_width = 0;
8999 reseat_at_next_visible_line_start (it, 0);
9000 if ((op & MOVE_TO_POS) != 0
9001 && IT_CHARPOS (*it) > to_charpos)
9003 reached = 9;
9004 goto out;
9006 break;
9008 case MOVE_LINE_CONTINUED:
9009 /* For continued lines ending in a tab, some of the glyphs
9010 associated with the tab are displayed on the current
9011 line. Since it->current_x does not include these glyphs,
9012 we use it->last_visible_x instead. */
9013 if (it->c == '\t')
9015 it->continuation_lines_width += it->last_visible_x;
9016 /* When moving by vpos, ensure that the iterator really
9017 advances to the next line (bug#847, bug#969). Fixme:
9018 do we need to do this in other circumstances? */
9019 if (it->current_x != it->last_visible_x
9020 && (op & MOVE_TO_VPOS)
9021 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9023 line_start_x = it->current_x + it->pixel_width
9024 - it->last_visible_x;
9025 set_iterator_to_next (it, 0);
9028 else
9029 it->continuation_lines_width += it->current_x;
9030 break;
9032 default:
9033 emacs_abort ();
9036 /* Reset/increment for the next run. */
9037 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9038 it->current_x = line_start_x;
9039 line_start_x = 0;
9040 it->hpos = 0;
9041 it->current_y += it->max_ascent + it->max_descent;
9042 ++it->vpos;
9043 last_height = it->max_ascent + it->max_descent;
9044 it->max_ascent = it->max_descent = 0;
9047 out:
9049 /* On text terminals, we may stop at the end of a line in the middle
9050 of a multi-character glyph. If the glyph itself is continued,
9051 i.e. it is actually displayed on the next line, don't treat this
9052 stopping point as valid; move to the next line instead (unless
9053 that brings us offscreen). */
9054 if (!FRAME_WINDOW_P (it->f)
9055 && op & MOVE_TO_POS
9056 && IT_CHARPOS (*it) == to_charpos
9057 && it->what == IT_CHARACTER
9058 && it->nglyphs > 1
9059 && it->line_wrap == WINDOW_WRAP
9060 && it->current_x == it->last_visible_x - 1
9061 && it->c != '\n'
9062 && it->c != '\t'
9063 && it->vpos < XFASTINT (it->w->window_end_vpos))
9065 it->continuation_lines_width += it->current_x;
9066 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9067 it->current_y += it->max_ascent + it->max_descent;
9068 ++it->vpos;
9069 last_height = it->max_ascent + it->max_descent;
9072 if (backup_data)
9073 bidi_unshelve_cache (backup_data, 1);
9075 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9079 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9081 If DY > 0, move IT backward at least that many pixels. DY = 0
9082 means move IT backward to the preceding line start or BEGV. This
9083 function may move over more than DY pixels if IT->current_y - DY
9084 ends up in the middle of a line; in this case IT->current_y will be
9085 set to the top of the line moved to. */
9087 void
9088 move_it_vertically_backward (struct it *it, int dy)
9090 int nlines, h;
9091 struct it it2, it3;
9092 void *it2data = NULL, *it3data = NULL;
9093 ptrdiff_t start_pos;
9094 int nchars_per_row
9095 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9096 ptrdiff_t pos_limit;
9098 move_further_back:
9099 eassert (dy >= 0);
9101 start_pos = IT_CHARPOS (*it);
9103 /* Estimate how many newlines we must move back. */
9104 nlines = max (1, dy / default_line_pixel_height (it->w));
9105 if (it->line_wrap == TRUNCATE)
9106 pos_limit = BEGV;
9107 else
9108 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9110 /* Set the iterator's position that many lines back. But don't go
9111 back more than NLINES full screen lines -- this wins a day with
9112 buffers which have very long lines. */
9113 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9114 back_to_previous_visible_line_start (it);
9116 /* Reseat the iterator here. When moving backward, we don't want
9117 reseat to skip forward over invisible text, set up the iterator
9118 to deliver from overlay strings at the new position etc. So,
9119 use reseat_1 here. */
9120 reseat_1 (it, it->current.pos, 1);
9122 /* We are now surely at a line start. */
9123 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9124 reordering is in effect. */
9125 it->continuation_lines_width = 0;
9127 /* Move forward and see what y-distance we moved. First move to the
9128 start of the next line so that we get its height. We need this
9129 height to be able to tell whether we reached the specified
9130 y-distance. */
9131 SAVE_IT (it2, *it, it2data);
9132 it2.max_ascent = it2.max_descent = 0;
9135 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9136 MOVE_TO_POS | MOVE_TO_VPOS);
9138 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9139 /* If we are in a display string which starts at START_POS,
9140 and that display string includes a newline, and we are
9141 right after that newline (i.e. at the beginning of a
9142 display line), exit the loop, because otherwise we will
9143 infloop, since move_it_to will see that it is already at
9144 START_POS and will not move. */
9145 || (it2.method == GET_FROM_STRING
9146 && IT_CHARPOS (it2) == start_pos
9147 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9148 eassert (IT_CHARPOS (*it) >= BEGV);
9149 SAVE_IT (it3, it2, it3data);
9151 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9152 eassert (IT_CHARPOS (*it) >= BEGV);
9153 /* H is the actual vertical distance from the position in *IT
9154 and the starting position. */
9155 h = it2.current_y - it->current_y;
9156 /* NLINES is the distance in number of lines. */
9157 nlines = it2.vpos - it->vpos;
9159 /* Correct IT's y and vpos position
9160 so that they are relative to the starting point. */
9161 it->vpos -= nlines;
9162 it->current_y -= h;
9164 if (dy == 0)
9166 /* DY == 0 means move to the start of the screen line. The
9167 value of nlines is > 0 if continuation lines were involved,
9168 or if the original IT position was at start of a line. */
9169 RESTORE_IT (it, it, it2data);
9170 if (nlines > 0)
9171 move_it_by_lines (it, nlines);
9172 /* The above code moves us to some position NLINES down,
9173 usually to its first glyph (leftmost in an L2R line), but
9174 that's not necessarily the start of the line, under bidi
9175 reordering. We want to get to the character position
9176 that is immediately after the newline of the previous
9177 line. */
9178 if (it->bidi_p
9179 && !it->continuation_lines_width
9180 && !STRINGP (it->string)
9181 && IT_CHARPOS (*it) > BEGV
9182 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9184 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9186 DEC_BOTH (cp, bp);
9187 cp = find_newline_no_quit (cp, bp, -1, NULL);
9188 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9190 bidi_unshelve_cache (it3data, 1);
9192 else
9194 /* The y-position we try to reach, relative to *IT.
9195 Note that H has been subtracted in front of the if-statement. */
9196 int target_y = it->current_y + h - dy;
9197 int y0 = it3.current_y;
9198 int y1;
9199 int line_height;
9201 RESTORE_IT (&it3, &it3, it3data);
9202 y1 = line_bottom_y (&it3);
9203 line_height = y1 - y0;
9204 RESTORE_IT (it, it, it2data);
9205 /* If we did not reach target_y, try to move further backward if
9206 we can. If we moved too far backward, try to move forward. */
9207 if (target_y < it->current_y
9208 /* This is heuristic. In a window that's 3 lines high, with
9209 a line height of 13 pixels each, recentering with point
9210 on the bottom line will try to move -39/2 = 19 pixels
9211 backward. Try to avoid moving into the first line. */
9212 && (it->current_y - target_y
9213 > min (window_box_height (it->w), line_height * 2 / 3))
9214 && IT_CHARPOS (*it) > BEGV)
9216 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9217 target_y - it->current_y));
9218 dy = it->current_y - target_y;
9219 goto move_further_back;
9221 else if (target_y >= it->current_y + line_height
9222 && IT_CHARPOS (*it) < ZV)
9224 /* Should move forward by at least one line, maybe more.
9226 Note: Calling move_it_by_lines can be expensive on
9227 terminal frames, where compute_motion is used (via
9228 vmotion) to do the job, when there are very long lines
9229 and truncate-lines is nil. That's the reason for
9230 treating terminal frames specially here. */
9232 if (!FRAME_WINDOW_P (it->f))
9233 move_it_vertically (it, target_y - (it->current_y + line_height));
9234 else
9238 move_it_by_lines (it, 1);
9240 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9247 /* Move IT by a specified amount of pixel lines DY. DY negative means
9248 move backwards. DY = 0 means move to start of screen line. At the
9249 end, IT will be on the start of a screen line. */
9251 void
9252 move_it_vertically (struct it *it, int dy)
9254 if (dy <= 0)
9255 move_it_vertically_backward (it, -dy);
9256 else
9258 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9259 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9260 MOVE_TO_POS | MOVE_TO_Y);
9261 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9263 /* If buffer ends in ZV without a newline, move to the start of
9264 the line to satisfy the post-condition. */
9265 if (IT_CHARPOS (*it) == ZV
9266 && ZV > BEGV
9267 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9268 move_it_by_lines (it, 0);
9273 /* Move iterator IT past the end of the text line it is in. */
9275 void
9276 move_it_past_eol (struct it *it)
9278 enum move_it_result rc;
9280 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9281 if (rc == MOVE_NEWLINE_OR_CR)
9282 set_iterator_to_next (it, 0);
9286 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9287 negative means move up. DVPOS == 0 means move to the start of the
9288 screen line.
9290 Optimization idea: If we would know that IT->f doesn't use
9291 a face with proportional font, we could be faster for
9292 truncate-lines nil. */
9294 void
9295 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9298 /* The commented-out optimization uses vmotion on terminals. This
9299 gives bad results, because elements like it->what, on which
9300 callers such as pos_visible_p rely, aren't updated. */
9301 /* struct position pos;
9302 if (!FRAME_WINDOW_P (it->f))
9304 struct text_pos textpos;
9306 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9307 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9308 reseat (it, textpos, 1);
9309 it->vpos += pos.vpos;
9310 it->current_y += pos.vpos;
9312 else */
9314 if (dvpos == 0)
9316 /* DVPOS == 0 means move to the start of the screen line. */
9317 move_it_vertically_backward (it, 0);
9318 /* Let next call to line_bottom_y calculate real line height */
9319 last_height = 0;
9321 else if (dvpos > 0)
9323 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9324 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9326 /* Only move to the next buffer position if we ended up in a
9327 string from display property, not in an overlay string
9328 (before-string or after-string). That is because the
9329 latter don't conceal the underlying buffer position, so
9330 we can ask to move the iterator to the exact position we
9331 are interested in. Note that, even if we are already at
9332 IT_CHARPOS (*it), the call below is not a no-op, as it
9333 will detect that we are at the end of the string, pop the
9334 iterator, and compute it->current_x and it->hpos
9335 correctly. */
9336 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9337 -1, -1, -1, MOVE_TO_POS);
9340 else
9342 struct it it2;
9343 void *it2data = NULL;
9344 ptrdiff_t start_charpos, i;
9345 int nchars_per_row
9346 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9347 ptrdiff_t pos_limit;
9349 /* Start at the beginning of the screen line containing IT's
9350 position. This may actually move vertically backwards,
9351 in case of overlays, so adjust dvpos accordingly. */
9352 dvpos += it->vpos;
9353 move_it_vertically_backward (it, 0);
9354 dvpos -= it->vpos;
9356 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9357 screen lines, and reseat the iterator there. */
9358 start_charpos = IT_CHARPOS (*it);
9359 if (it->line_wrap == TRUNCATE)
9360 pos_limit = BEGV;
9361 else
9362 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9363 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9364 back_to_previous_visible_line_start (it);
9365 reseat (it, it->current.pos, 1);
9367 /* Move further back if we end up in a string or an image. */
9368 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9370 /* First try to move to start of display line. */
9371 dvpos += it->vpos;
9372 move_it_vertically_backward (it, 0);
9373 dvpos -= it->vpos;
9374 if (IT_POS_VALID_AFTER_MOVE_P (it))
9375 break;
9376 /* If start of line is still in string or image,
9377 move further back. */
9378 back_to_previous_visible_line_start (it);
9379 reseat (it, it->current.pos, 1);
9380 dvpos--;
9383 it->current_x = it->hpos = 0;
9385 /* Above call may have moved too far if continuation lines
9386 are involved. Scan forward and see if it did. */
9387 SAVE_IT (it2, *it, it2data);
9388 it2.vpos = it2.current_y = 0;
9389 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9390 it->vpos -= it2.vpos;
9391 it->current_y -= it2.current_y;
9392 it->current_x = it->hpos = 0;
9394 /* If we moved too far back, move IT some lines forward. */
9395 if (it2.vpos > -dvpos)
9397 int delta = it2.vpos + dvpos;
9399 RESTORE_IT (&it2, &it2, it2data);
9400 SAVE_IT (it2, *it, it2data);
9401 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9402 /* Move back again if we got too far ahead. */
9403 if (IT_CHARPOS (*it) >= start_charpos)
9404 RESTORE_IT (it, &it2, it2data);
9405 else
9406 bidi_unshelve_cache (it2data, 1);
9408 else
9409 RESTORE_IT (it, it, it2data);
9413 /* Return 1 if IT points into the middle of a display vector. */
9416 in_display_vector_p (struct it *it)
9418 return (it->method == GET_FROM_DISPLAY_VECTOR
9419 && it->current.dpvec_index > 0
9420 && it->dpvec + it->current.dpvec_index != it->dpend);
9424 /***********************************************************************
9425 Messages
9426 ***********************************************************************/
9429 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9430 to *Messages*. */
9432 void
9433 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9435 Lisp_Object args[3];
9436 Lisp_Object msg, fmt;
9437 char *buffer;
9438 ptrdiff_t len;
9439 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9440 USE_SAFE_ALLOCA;
9442 fmt = msg = Qnil;
9443 GCPRO4 (fmt, msg, arg1, arg2);
9445 args[0] = fmt = build_string (format);
9446 args[1] = arg1;
9447 args[2] = arg2;
9448 msg = Fformat (3, args);
9450 len = SBYTES (msg) + 1;
9451 buffer = SAFE_ALLOCA (len);
9452 memcpy (buffer, SDATA (msg), len);
9454 message_dolog (buffer, len - 1, 1, 0);
9455 SAFE_FREE ();
9457 UNGCPRO;
9461 /* Output a newline in the *Messages* buffer if "needs" one. */
9463 void
9464 message_log_maybe_newline (void)
9466 if (message_log_need_newline)
9467 message_dolog ("", 0, 1, 0);
9471 /* Add a string M of length NBYTES to the message log, optionally
9472 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9473 true, means interpret the contents of M as multibyte. This
9474 function calls low-level routines in order to bypass text property
9475 hooks, etc. which might not be safe to run.
9477 This may GC (insert may run before/after change hooks),
9478 so the buffer M must NOT point to a Lisp string. */
9480 void
9481 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9483 const unsigned char *msg = (const unsigned char *) m;
9485 if (!NILP (Vmemory_full))
9486 return;
9488 if (!NILP (Vmessage_log_max))
9490 struct buffer *oldbuf;
9491 Lisp_Object oldpoint, oldbegv, oldzv;
9492 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9493 ptrdiff_t point_at_end = 0;
9494 ptrdiff_t zv_at_end = 0;
9495 Lisp_Object old_deactivate_mark;
9496 bool shown;
9497 struct gcpro gcpro1;
9499 old_deactivate_mark = Vdeactivate_mark;
9500 oldbuf = current_buffer;
9501 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9502 bset_undo_list (current_buffer, Qt);
9504 oldpoint = message_dolog_marker1;
9505 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9506 oldbegv = message_dolog_marker2;
9507 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9508 oldzv = message_dolog_marker3;
9509 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9510 GCPRO1 (old_deactivate_mark);
9512 if (PT == Z)
9513 point_at_end = 1;
9514 if (ZV == Z)
9515 zv_at_end = 1;
9517 BEGV = BEG;
9518 BEGV_BYTE = BEG_BYTE;
9519 ZV = Z;
9520 ZV_BYTE = Z_BYTE;
9521 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9523 /* Insert the string--maybe converting multibyte to single byte
9524 or vice versa, so that all the text fits the buffer. */
9525 if (multibyte
9526 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9528 ptrdiff_t i;
9529 int c, char_bytes;
9530 char work[1];
9532 /* Convert a multibyte string to single-byte
9533 for the *Message* buffer. */
9534 for (i = 0; i < nbytes; i += char_bytes)
9536 c = string_char_and_length (msg + i, &char_bytes);
9537 work[0] = (ASCII_CHAR_P (c)
9539 : multibyte_char_to_unibyte (c));
9540 insert_1_both (work, 1, 1, 1, 0, 0);
9543 else if (! multibyte
9544 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9546 ptrdiff_t i;
9547 int c, char_bytes;
9548 unsigned char str[MAX_MULTIBYTE_LENGTH];
9549 /* Convert a single-byte string to multibyte
9550 for the *Message* buffer. */
9551 for (i = 0; i < nbytes; i++)
9553 c = msg[i];
9554 MAKE_CHAR_MULTIBYTE (c);
9555 char_bytes = CHAR_STRING (c, str);
9556 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9559 else if (nbytes)
9560 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9562 if (nlflag)
9564 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9565 printmax_t dups;
9567 insert_1_both ("\n", 1, 1, 1, 0, 0);
9569 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9570 this_bol = PT;
9571 this_bol_byte = PT_BYTE;
9573 /* See if this line duplicates the previous one.
9574 If so, combine duplicates. */
9575 if (this_bol > BEG)
9577 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9578 prev_bol = PT;
9579 prev_bol_byte = PT_BYTE;
9581 dups = message_log_check_duplicate (prev_bol_byte,
9582 this_bol_byte);
9583 if (dups)
9585 del_range_both (prev_bol, prev_bol_byte,
9586 this_bol, this_bol_byte, 0);
9587 if (dups > 1)
9589 char dupstr[sizeof " [ times]"
9590 + INT_STRLEN_BOUND (printmax_t)];
9592 /* If you change this format, don't forget to also
9593 change message_log_check_duplicate. */
9594 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9595 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9596 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9601 /* If we have more than the desired maximum number of lines
9602 in the *Messages* buffer now, delete the oldest ones.
9603 This is safe because we don't have undo in this buffer. */
9605 if (NATNUMP (Vmessage_log_max))
9607 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9608 -XFASTINT (Vmessage_log_max) - 1, 0);
9609 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9612 BEGV = marker_position (oldbegv);
9613 BEGV_BYTE = marker_byte_position (oldbegv);
9615 if (zv_at_end)
9617 ZV = Z;
9618 ZV_BYTE = Z_BYTE;
9620 else
9622 ZV = marker_position (oldzv);
9623 ZV_BYTE = marker_byte_position (oldzv);
9626 if (point_at_end)
9627 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9628 else
9629 /* We can't do Fgoto_char (oldpoint) because it will run some
9630 Lisp code. */
9631 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9632 marker_byte_position (oldpoint));
9634 UNGCPRO;
9635 unchain_marker (XMARKER (oldpoint));
9636 unchain_marker (XMARKER (oldbegv));
9637 unchain_marker (XMARKER (oldzv));
9639 shown = buffer_window_count (current_buffer) > 0;
9640 set_buffer_internal (oldbuf);
9641 /* We called insert_1_both above with its 5th argument (PREPARE)
9642 zero, which prevents insert_1_both from calling
9643 prepare_to_modify_buffer, which in turns prevents us from
9644 incrementing windows_or_buffers_changed even if *Messages* is
9645 shown in some window. So we must manually incrementing
9646 windows_or_buffers_changed here to make up for that. */
9647 if (shown)
9648 windows_or_buffers_changed++;
9649 else
9650 windows_or_buffers_changed = old_windows_or_buffers_changed;
9651 message_log_need_newline = !nlflag;
9652 Vdeactivate_mark = old_deactivate_mark;
9657 /* We are at the end of the buffer after just having inserted a newline.
9658 (Note: We depend on the fact we won't be crossing the gap.)
9659 Check to see if the most recent message looks a lot like the previous one.
9660 Return 0 if different, 1 if the new one should just replace it, or a
9661 value N > 1 if we should also append " [N times]". */
9663 static intmax_t
9664 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9666 ptrdiff_t i;
9667 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9668 int seen_dots = 0;
9669 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9670 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9672 for (i = 0; i < len; i++)
9674 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9675 seen_dots = 1;
9676 if (p1[i] != p2[i])
9677 return seen_dots;
9679 p1 += len;
9680 if (*p1 == '\n')
9681 return 2;
9682 if (*p1++ == ' ' && *p1++ == '[')
9684 char *pend;
9685 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9686 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9687 return n + 1;
9689 return 0;
9693 /* Display an echo area message M with a specified length of NBYTES
9694 bytes. The string may include null characters. If M is not a
9695 string, clear out any existing message, and let the mini-buffer
9696 text show through.
9698 This function cancels echoing. */
9700 void
9701 message3 (Lisp_Object m)
9703 struct gcpro gcpro1;
9705 GCPRO1 (m);
9706 clear_message (1,1);
9707 cancel_echoing ();
9709 /* First flush out any partial line written with print. */
9710 message_log_maybe_newline ();
9711 if (STRINGP (m))
9713 ptrdiff_t nbytes = SBYTES (m);
9714 bool multibyte = STRING_MULTIBYTE (m);
9715 USE_SAFE_ALLOCA;
9716 char *buffer = SAFE_ALLOCA (nbytes);
9717 memcpy (buffer, SDATA (m), nbytes);
9718 message_dolog (buffer, nbytes, 1, multibyte);
9719 SAFE_FREE ();
9721 message3_nolog (m);
9723 UNGCPRO;
9727 /* The non-logging version of message3.
9728 This does not cancel echoing, because it is used for echoing.
9729 Perhaps we need to make a separate function for echoing
9730 and make this cancel echoing. */
9732 void
9733 message3_nolog (Lisp_Object m)
9735 struct frame *sf = SELECTED_FRAME ();
9737 if (FRAME_INITIAL_P (sf))
9739 if (noninteractive_need_newline)
9740 putc ('\n', stderr);
9741 noninteractive_need_newline = 0;
9742 if (STRINGP (m))
9743 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9744 if (cursor_in_echo_area == 0)
9745 fprintf (stderr, "\n");
9746 fflush (stderr);
9748 /* Error messages get reported properly by cmd_error, so this must be just an
9749 informative message; if the frame hasn't really been initialized yet, just
9750 toss it. */
9751 else if (INTERACTIVE && sf->glyphs_initialized_p)
9753 /* Get the frame containing the mini-buffer
9754 that the selected frame is using. */
9755 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9756 Lisp_Object frame = XWINDOW (mini_window)->frame;
9757 struct frame *f = XFRAME (frame);
9759 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9760 Fmake_frame_visible (frame);
9762 if (STRINGP (m) && SCHARS (m) > 0)
9764 set_message (m);
9765 if (minibuffer_auto_raise)
9766 Fraise_frame (frame);
9767 /* Assume we are not echoing.
9768 (If we are, echo_now will override this.) */
9769 echo_message_buffer = Qnil;
9771 else
9772 clear_message (1, 1);
9774 do_pending_window_change (0);
9775 echo_area_display (1);
9776 do_pending_window_change (0);
9777 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9778 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9783 /* Display a null-terminated echo area message M. If M is 0, clear
9784 out any existing message, and let the mini-buffer text show through.
9786 The buffer M must continue to exist until after the echo area gets
9787 cleared or some other message gets displayed there. Do not pass
9788 text that is stored in a Lisp string. Do not pass text in a buffer
9789 that was alloca'd. */
9791 void
9792 message1 (const char *m)
9794 message3 (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9798 /* The non-logging counterpart of message1. */
9800 void
9801 message1_nolog (const char *m)
9803 message3_nolog (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9806 /* Display a message M which contains a single %s
9807 which gets replaced with STRING. */
9809 void
9810 message_with_string (const char *m, Lisp_Object string, int log)
9812 CHECK_STRING (string);
9814 if (noninteractive)
9816 if (m)
9818 if (noninteractive_need_newline)
9819 putc ('\n', stderr);
9820 noninteractive_need_newline = 0;
9821 fprintf (stderr, m, SDATA (string));
9822 if (!cursor_in_echo_area)
9823 fprintf (stderr, "\n");
9824 fflush (stderr);
9827 else if (INTERACTIVE)
9829 /* The frame whose minibuffer we're going to display the message on.
9830 It may be larger than the selected frame, so we need
9831 to use its buffer, not the selected frame's buffer. */
9832 Lisp_Object mini_window;
9833 struct frame *f, *sf = SELECTED_FRAME ();
9835 /* Get the frame containing the minibuffer
9836 that the selected frame is using. */
9837 mini_window = FRAME_MINIBUF_WINDOW (sf);
9838 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9840 /* Error messages get reported properly by cmd_error, so this must be
9841 just an informative message; if the frame hasn't really been
9842 initialized yet, just toss it. */
9843 if (f->glyphs_initialized_p)
9845 Lisp_Object args[2], msg;
9846 struct gcpro gcpro1, gcpro2;
9848 args[0] = build_string (m);
9849 args[1] = msg = string;
9850 GCPRO2 (args[0], msg);
9851 gcpro1.nvars = 2;
9853 msg = Fformat (2, args);
9855 if (log)
9856 message3 (msg);
9857 else
9858 message3_nolog (msg);
9860 UNGCPRO;
9862 /* Print should start at the beginning of the message
9863 buffer next time. */
9864 message_buf_print = 0;
9870 /* Dump an informative message to the minibuf. If M is 0, clear out
9871 any existing message, and let the mini-buffer text show through. */
9873 static void
9874 vmessage (const char *m, va_list ap)
9876 if (noninteractive)
9878 if (m)
9880 if (noninteractive_need_newline)
9881 putc ('\n', stderr);
9882 noninteractive_need_newline = 0;
9883 vfprintf (stderr, m, ap);
9884 if (cursor_in_echo_area == 0)
9885 fprintf (stderr, "\n");
9886 fflush (stderr);
9889 else if (INTERACTIVE)
9891 /* The frame whose mini-buffer we're going to display the message
9892 on. It may be larger than the selected frame, so we need to
9893 use its buffer, not the selected frame's buffer. */
9894 Lisp_Object mini_window;
9895 struct frame *f, *sf = SELECTED_FRAME ();
9897 /* Get the frame containing the mini-buffer
9898 that the selected frame is using. */
9899 mini_window = FRAME_MINIBUF_WINDOW (sf);
9900 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9902 /* Error messages get reported properly by cmd_error, so this must be
9903 just an informative message; if the frame hasn't really been
9904 initialized yet, just toss it. */
9905 if (f->glyphs_initialized_p)
9907 if (m)
9909 ptrdiff_t len;
9910 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9911 char *message_buf = alloca (maxsize + 1);
9913 len = doprnt (message_buf, maxsize, m, (char *)0, ap);
9915 message3 (make_string (message_buf, len));
9917 else
9918 message1 (0);
9920 /* Print should start at the beginning of the message
9921 buffer next time. */
9922 message_buf_print = 0;
9927 void
9928 message (const char *m, ...)
9930 va_list ap;
9931 va_start (ap, m);
9932 vmessage (m, ap);
9933 va_end (ap);
9937 #if 0
9938 /* The non-logging version of message. */
9940 void
9941 message_nolog (const char *m, ...)
9943 Lisp_Object old_log_max;
9944 va_list ap;
9945 va_start (ap, m);
9946 old_log_max = Vmessage_log_max;
9947 Vmessage_log_max = Qnil;
9948 vmessage (m, ap);
9949 Vmessage_log_max = old_log_max;
9950 va_end (ap);
9952 #endif
9955 /* Display the current message in the current mini-buffer. This is
9956 only called from error handlers in process.c, and is not time
9957 critical. */
9959 void
9960 update_echo_area (void)
9962 if (!NILP (echo_area_buffer[0]))
9964 Lisp_Object string;
9965 string = Fcurrent_message ();
9966 message3 (string);
9971 /* Make sure echo area buffers in `echo_buffers' are live.
9972 If they aren't, make new ones. */
9974 static void
9975 ensure_echo_area_buffers (void)
9977 int i;
9979 for (i = 0; i < 2; ++i)
9980 if (!BUFFERP (echo_buffer[i])
9981 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9983 char name[30];
9984 Lisp_Object old_buffer;
9985 int j;
9987 old_buffer = echo_buffer[i];
9988 echo_buffer[i] = Fget_buffer_create
9989 (make_formatted_string (name, " *Echo Area %d*", i));
9990 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9991 /* to force word wrap in echo area -
9992 it was decided to postpone this*/
9993 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9995 for (j = 0; j < 2; ++j)
9996 if (EQ (old_buffer, echo_area_buffer[j]))
9997 echo_area_buffer[j] = echo_buffer[i];
10002 /* Call FN with args A1..A2 with either the current or last displayed
10003 echo_area_buffer as current buffer.
10005 WHICH zero means use the current message buffer
10006 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10007 from echo_buffer[] and clear it.
10009 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10010 suitable buffer from echo_buffer[] and clear it.
10012 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10013 that the current message becomes the last displayed one, make
10014 choose a suitable buffer for echo_area_buffer[0], and clear it.
10016 Value is what FN returns. */
10018 static int
10019 with_echo_area_buffer (struct window *w, int which,
10020 int (*fn) (ptrdiff_t, Lisp_Object),
10021 ptrdiff_t a1, Lisp_Object a2)
10023 Lisp_Object buffer;
10024 int this_one, the_other, clear_buffer_p, rc;
10025 ptrdiff_t count = SPECPDL_INDEX ();
10027 /* If buffers aren't live, make new ones. */
10028 ensure_echo_area_buffers ();
10030 clear_buffer_p = 0;
10032 if (which == 0)
10033 this_one = 0, the_other = 1;
10034 else if (which > 0)
10035 this_one = 1, the_other = 0;
10036 else
10038 this_one = 0, the_other = 1;
10039 clear_buffer_p = 1;
10041 /* We need a fresh one in case the current echo buffer equals
10042 the one containing the last displayed echo area message. */
10043 if (!NILP (echo_area_buffer[this_one])
10044 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10045 echo_area_buffer[this_one] = Qnil;
10048 /* Choose a suitable buffer from echo_buffer[] is we don't
10049 have one. */
10050 if (NILP (echo_area_buffer[this_one]))
10052 echo_area_buffer[this_one]
10053 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10054 ? echo_buffer[the_other]
10055 : echo_buffer[this_one]);
10056 clear_buffer_p = 1;
10059 buffer = echo_area_buffer[this_one];
10061 /* Don't get confused by reusing the buffer used for echoing
10062 for a different purpose. */
10063 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10064 cancel_echoing ();
10066 record_unwind_protect (unwind_with_echo_area_buffer,
10067 with_echo_area_buffer_unwind_data (w));
10069 /* Make the echo area buffer current. Note that for display
10070 purposes, it is not necessary that the displayed window's buffer
10071 == current_buffer, except for text property lookup. So, let's
10072 only set that buffer temporarily here without doing a full
10073 Fset_window_buffer. We must also change w->pointm, though,
10074 because otherwise an assertions in unshow_buffer fails, and Emacs
10075 aborts. */
10076 set_buffer_internal_1 (XBUFFER (buffer));
10077 if (w)
10079 wset_buffer (w, buffer);
10080 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10083 bset_undo_list (current_buffer, Qt);
10084 bset_read_only (current_buffer, Qnil);
10085 specbind (Qinhibit_read_only, Qt);
10086 specbind (Qinhibit_modification_hooks, Qt);
10088 if (clear_buffer_p && Z > BEG)
10089 del_range (BEG, Z);
10091 eassert (BEGV >= BEG);
10092 eassert (ZV <= Z && ZV >= BEGV);
10094 rc = fn (a1, a2);
10096 eassert (BEGV >= BEG);
10097 eassert (ZV <= Z && ZV >= BEGV);
10099 unbind_to (count, Qnil);
10100 return rc;
10104 /* Save state that should be preserved around the call to the function
10105 FN called in with_echo_area_buffer. */
10107 static Lisp_Object
10108 with_echo_area_buffer_unwind_data (struct window *w)
10110 int i = 0;
10111 Lisp_Object vector, tmp;
10113 /* Reduce consing by keeping one vector in
10114 Vwith_echo_area_save_vector. */
10115 vector = Vwith_echo_area_save_vector;
10116 Vwith_echo_area_save_vector = Qnil;
10118 if (NILP (vector))
10119 vector = Fmake_vector (make_number (9), Qnil);
10121 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10122 ASET (vector, i, Vdeactivate_mark); ++i;
10123 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10125 if (w)
10127 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10128 ASET (vector, i, w->contents); ++i;
10129 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10130 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10131 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10132 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10134 else
10136 int end = i + 6;
10137 for (; i < end; ++i)
10138 ASET (vector, i, Qnil);
10141 eassert (i == ASIZE (vector));
10142 return vector;
10146 /* Restore global state from VECTOR which was created by
10147 with_echo_area_buffer_unwind_data. */
10149 static Lisp_Object
10150 unwind_with_echo_area_buffer (Lisp_Object vector)
10152 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10153 Vdeactivate_mark = AREF (vector, 1);
10154 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10156 if (WINDOWP (AREF (vector, 3)))
10158 struct window *w;
10159 Lisp_Object buffer;
10161 w = XWINDOW (AREF (vector, 3));
10162 buffer = AREF (vector, 4);
10164 wset_buffer (w, buffer);
10165 set_marker_both (w->pointm, buffer,
10166 XFASTINT (AREF (vector, 5)),
10167 XFASTINT (AREF (vector, 6)));
10168 set_marker_both (w->start, buffer,
10169 XFASTINT (AREF (vector, 7)),
10170 XFASTINT (AREF (vector, 8)));
10173 Vwith_echo_area_save_vector = vector;
10174 return Qnil;
10178 /* Set up the echo area for use by print functions. MULTIBYTE_P
10179 non-zero means we will print multibyte. */
10181 void
10182 setup_echo_area_for_printing (int multibyte_p)
10184 /* If we can't find an echo area any more, exit. */
10185 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10186 Fkill_emacs (Qnil);
10188 ensure_echo_area_buffers ();
10190 if (!message_buf_print)
10192 /* A message has been output since the last time we printed.
10193 Choose a fresh echo area buffer. */
10194 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10195 echo_area_buffer[0] = echo_buffer[1];
10196 else
10197 echo_area_buffer[0] = echo_buffer[0];
10199 /* Switch to that buffer and clear it. */
10200 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10201 bset_truncate_lines (current_buffer, Qnil);
10203 if (Z > BEG)
10205 ptrdiff_t count = SPECPDL_INDEX ();
10206 specbind (Qinhibit_read_only, Qt);
10207 /* Note that undo recording is always disabled. */
10208 del_range (BEG, Z);
10209 unbind_to (count, Qnil);
10211 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10213 /* Set up the buffer for the multibyteness we need. */
10214 if (multibyte_p
10215 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10216 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10218 /* Raise the frame containing the echo area. */
10219 if (minibuffer_auto_raise)
10221 struct frame *sf = SELECTED_FRAME ();
10222 Lisp_Object mini_window;
10223 mini_window = FRAME_MINIBUF_WINDOW (sf);
10224 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10227 message_log_maybe_newline ();
10228 message_buf_print = 1;
10230 else
10232 if (NILP (echo_area_buffer[0]))
10234 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10235 echo_area_buffer[0] = echo_buffer[1];
10236 else
10237 echo_area_buffer[0] = echo_buffer[0];
10240 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10242 /* Someone switched buffers between print requests. */
10243 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10244 bset_truncate_lines (current_buffer, Qnil);
10250 /* Display an echo area message in window W. Value is non-zero if W's
10251 height is changed. If display_last_displayed_message_p is
10252 non-zero, display the message that was last displayed, otherwise
10253 display the current message. */
10255 static int
10256 display_echo_area (struct window *w)
10258 int i, no_message_p, window_height_changed_p;
10260 /* Temporarily disable garbage collections while displaying the echo
10261 area. This is done because a GC can print a message itself.
10262 That message would modify the echo area buffer's contents while a
10263 redisplay of the buffer is going on, and seriously confuse
10264 redisplay. */
10265 ptrdiff_t count = inhibit_garbage_collection ();
10267 /* If there is no message, we must call display_echo_area_1
10268 nevertheless because it resizes the window. But we will have to
10269 reset the echo_area_buffer in question to nil at the end because
10270 with_echo_area_buffer will sets it to an empty buffer. */
10271 i = display_last_displayed_message_p ? 1 : 0;
10272 no_message_p = NILP (echo_area_buffer[i]);
10274 window_height_changed_p
10275 = with_echo_area_buffer (w, display_last_displayed_message_p,
10276 display_echo_area_1,
10277 (intptr_t) w, Qnil);
10279 if (no_message_p)
10280 echo_area_buffer[i] = Qnil;
10282 unbind_to (count, Qnil);
10283 return window_height_changed_p;
10287 /* Helper for display_echo_area. Display the current buffer which
10288 contains the current echo area message in window W, a mini-window,
10289 a pointer to which is passed in A1. A2..A4 are currently not used.
10290 Change the height of W so that all of the message is displayed.
10291 Value is non-zero if height of W was changed. */
10293 static int
10294 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10296 intptr_t i1 = a1;
10297 struct window *w = (struct window *) i1;
10298 Lisp_Object window;
10299 struct text_pos start;
10300 int window_height_changed_p = 0;
10302 /* Do this before displaying, so that we have a large enough glyph
10303 matrix for the display. If we can't get enough space for the
10304 whole text, display the last N lines. That works by setting w->start. */
10305 window_height_changed_p = resize_mini_window (w, 0);
10307 /* Use the starting position chosen by resize_mini_window. */
10308 SET_TEXT_POS_FROM_MARKER (start, w->start);
10310 /* Display. */
10311 clear_glyph_matrix (w->desired_matrix);
10312 XSETWINDOW (window, w);
10313 try_window (window, start, 0);
10315 return window_height_changed_p;
10319 /* Resize the echo area window to exactly the size needed for the
10320 currently displayed message, if there is one. If a mini-buffer
10321 is active, don't shrink it. */
10323 void
10324 resize_echo_area_exactly (void)
10326 if (BUFFERP (echo_area_buffer[0])
10327 && WINDOWP (echo_area_window))
10329 struct window *w = XWINDOW (echo_area_window);
10330 int resized_p;
10331 Lisp_Object resize_exactly;
10333 if (minibuf_level == 0)
10334 resize_exactly = Qt;
10335 else
10336 resize_exactly = Qnil;
10338 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10339 (intptr_t) w, resize_exactly);
10340 if (resized_p)
10342 ++windows_or_buffers_changed;
10343 ++update_mode_lines;
10344 redisplay_internal ();
10350 /* Callback function for with_echo_area_buffer, when used from
10351 resize_echo_area_exactly. A1 contains a pointer to the window to
10352 resize, EXACTLY non-nil means resize the mini-window exactly to the
10353 size of the text displayed. A3 and A4 are not used. Value is what
10354 resize_mini_window returns. */
10356 static int
10357 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10359 intptr_t i1 = a1;
10360 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10364 /* Resize mini-window W to fit the size of its contents. EXACT_P
10365 means size the window exactly to the size needed. Otherwise, it's
10366 only enlarged until W's buffer is empty.
10368 Set W->start to the right place to begin display. If the whole
10369 contents fit, start at the beginning. Otherwise, start so as
10370 to make the end of the contents appear. This is particularly
10371 important for y-or-n-p, but seems desirable generally.
10373 Value is non-zero if the window height has been changed. */
10376 resize_mini_window (struct window *w, int exact_p)
10378 struct frame *f = XFRAME (w->frame);
10379 int window_height_changed_p = 0;
10381 eassert (MINI_WINDOW_P (w));
10383 /* By default, start display at the beginning. */
10384 set_marker_both (w->start, w->contents,
10385 BUF_BEGV (XBUFFER (w->contents)),
10386 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10388 /* Don't resize windows while redisplaying a window; it would
10389 confuse redisplay functions when the size of the window they are
10390 displaying changes from under them. Such a resizing can happen,
10391 for instance, when which-func prints a long message while
10392 we are running fontification-functions. We're running these
10393 functions with safe_call which binds inhibit-redisplay to t. */
10394 if (!NILP (Vinhibit_redisplay))
10395 return 0;
10397 /* Nil means don't try to resize. */
10398 if (NILP (Vresize_mini_windows)
10399 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10400 return 0;
10402 if (!FRAME_MINIBUF_ONLY_P (f))
10404 struct it it;
10405 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10406 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10407 int height;
10408 EMACS_INT max_height;
10409 int unit = FRAME_LINE_HEIGHT (f);
10410 struct text_pos start;
10411 struct buffer *old_current_buffer = NULL;
10413 if (current_buffer != XBUFFER (w->contents))
10415 old_current_buffer = current_buffer;
10416 set_buffer_internal (XBUFFER (w->contents));
10419 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10421 /* Compute the max. number of lines specified by the user. */
10422 if (FLOATP (Vmax_mini_window_height))
10423 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10424 else if (INTEGERP (Vmax_mini_window_height))
10425 max_height = XINT (Vmax_mini_window_height);
10426 else
10427 max_height = total_height / 4;
10429 /* Correct that max. height if it's bogus. */
10430 max_height = clip_to_bounds (1, max_height, total_height);
10432 /* Find out the height of the text in the window. */
10433 if (it.line_wrap == TRUNCATE)
10434 height = 1;
10435 else
10437 last_height = 0;
10438 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10439 if (it.max_ascent == 0 && it.max_descent == 0)
10440 height = it.current_y + last_height;
10441 else
10442 height = it.current_y + it.max_ascent + it.max_descent;
10443 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10444 height = (height + unit - 1) / unit;
10447 /* Compute a suitable window start. */
10448 if (height > max_height)
10450 height = max_height;
10451 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10452 move_it_vertically_backward (&it, (height - 1) * unit);
10453 start = it.current.pos;
10455 else
10456 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10457 SET_MARKER_FROM_TEXT_POS (w->start, start);
10459 if (EQ (Vresize_mini_windows, Qgrow_only))
10461 /* Let it grow only, until we display an empty message, in which
10462 case the window shrinks again. */
10463 if (height > WINDOW_TOTAL_LINES (w))
10465 int old_height = WINDOW_TOTAL_LINES (w);
10466 freeze_window_starts (f, 1);
10467 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10468 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10470 else if (height < WINDOW_TOTAL_LINES (w)
10471 && (exact_p || BEGV == ZV))
10473 int old_height = WINDOW_TOTAL_LINES (w);
10474 freeze_window_starts (f, 0);
10475 shrink_mini_window (w);
10476 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10479 else
10481 /* Always resize to exact size needed. */
10482 if (height > WINDOW_TOTAL_LINES (w))
10484 int old_height = WINDOW_TOTAL_LINES (w);
10485 freeze_window_starts (f, 1);
10486 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10487 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10489 else if (height < WINDOW_TOTAL_LINES (w))
10491 int old_height = WINDOW_TOTAL_LINES (w);
10492 freeze_window_starts (f, 0);
10493 shrink_mini_window (w);
10495 if (height)
10497 freeze_window_starts (f, 1);
10498 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10501 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10505 if (old_current_buffer)
10506 set_buffer_internal (old_current_buffer);
10509 return window_height_changed_p;
10513 /* Value is the current message, a string, or nil if there is no
10514 current message. */
10516 Lisp_Object
10517 current_message (void)
10519 Lisp_Object msg;
10521 if (!BUFFERP (echo_area_buffer[0]))
10522 msg = Qnil;
10523 else
10525 with_echo_area_buffer (0, 0, current_message_1,
10526 (intptr_t) &msg, Qnil);
10527 if (NILP (msg))
10528 echo_area_buffer[0] = Qnil;
10531 return msg;
10535 static int
10536 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10538 intptr_t i1 = a1;
10539 Lisp_Object *msg = (Lisp_Object *) i1;
10541 if (Z > BEG)
10542 *msg = make_buffer_string (BEG, Z, 1);
10543 else
10544 *msg = Qnil;
10545 return 0;
10549 /* Push the current message on Vmessage_stack for later restoration
10550 by restore_message. Value is non-zero if the current message isn't
10551 empty. This is a relatively infrequent operation, so it's not
10552 worth optimizing. */
10554 bool
10555 push_message (void)
10557 Lisp_Object msg = current_message ();
10558 Vmessage_stack = Fcons (msg, Vmessage_stack);
10559 return STRINGP (msg);
10563 /* Restore message display from the top of Vmessage_stack. */
10565 void
10566 restore_message (void)
10568 eassert (CONSP (Vmessage_stack));
10569 message3_nolog (XCAR (Vmessage_stack));
10573 /* Handler for record_unwind_protect calling pop_message. */
10575 Lisp_Object
10576 pop_message_unwind (Lisp_Object dummy)
10578 pop_message ();
10579 return Qnil;
10582 /* Pop the top-most entry off Vmessage_stack. */
10584 static void
10585 pop_message (void)
10587 eassert (CONSP (Vmessage_stack));
10588 Vmessage_stack = XCDR (Vmessage_stack);
10592 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10593 exits. If the stack is not empty, we have a missing pop_message
10594 somewhere. */
10596 void
10597 check_message_stack (void)
10599 if (!NILP (Vmessage_stack))
10600 emacs_abort ();
10604 /* Truncate to NCHARS what will be displayed in the echo area the next
10605 time we display it---but don't redisplay it now. */
10607 void
10608 truncate_echo_area (ptrdiff_t nchars)
10610 if (nchars == 0)
10611 echo_area_buffer[0] = Qnil;
10612 else if (!noninteractive
10613 && INTERACTIVE
10614 && !NILP (echo_area_buffer[0]))
10616 struct frame *sf = SELECTED_FRAME ();
10617 /* Error messages get reported properly by cmd_error, so this must be
10618 just an informative message; if the frame hasn't really been
10619 initialized yet, just toss it. */
10620 if (sf->glyphs_initialized_p)
10621 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10626 /* Helper function for truncate_echo_area. Truncate the current
10627 message to at most NCHARS characters. */
10629 static int
10630 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10632 if (BEG + nchars < Z)
10633 del_range (BEG + nchars, Z);
10634 if (Z == BEG)
10635 echo_area_buffer[0] = Qnil;
10636 return 0;
10639 /* Set the current message to STRING. */
10641 static void
10642 set_message (Lisp_Object string)
10644 eassert (STRINGP (string));
10646 message_enable_multibyte = STRING_MULTIBYTE (string);
10648 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10649 message_buf_print = 0;
10650 help_echo_showing_p = 0;
10652 if (STRINGP (Vdebug_on_message)
10653 && STRINGP (string)
10654 && fast_string_match (Vdebug_on_message, string) >= 0)
10655 call_debugger (list2 (Qerror, string));
10659 /* Helper function for set_message. First argument is ignored and second
10660 argument has the same meaning as for set_message.
10661 This function is called with the echo area buffer being current. */
10663 static int
10664 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10666 eassert (STRINGP (string));
10668 /* Change multibyteness of the echo buffer appropriately. */
10669 if (message_enable_multibyte
10670 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10671 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10673 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10674 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10675 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10677 /* Insert new message at BEG. */
10678 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10680 /* This function takes care of single/multibyte conversion.
10681 We just have to ensure that the echo area buffer has the right
10682 setting of enable_multibyte_characters. */
10683 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10685 return 0;
10689 /* Clear messages. CURRENT_P non-zero means clear the current
10690 message. LAST_DISPLAYED_P non-zero means clear the message
10691 last displayed. */
10693 void
10694 clear_message (int current_p, int last_displayed_p)
10696 if (current_p)
10698 echo_area_buffer[0] = Qnil;
10699 message_cleared_p = 1;
10702 if (last_displayed_p)
10703 echo_area_buffer[1] = Qnil;
10705 message_buf_print = 0;
10708 /* Clear garbaged frames.
10710 This function is used where the old redisplay called
10711 redraw_garbaged_frames which in turn called redraw_frame which in
10712 turn called clear_frame. The call to clear_frame was a source of
10713 flickering. I believe a clear_frame is not necessary. It should
10714 suffice in the new redisplay to invalidate all current matrices,
10715 and ensure a complete redisplay of all windows. */
10717 static void
10718 clear_garbaged_frames (void)
10720 if (frame_garbaged)
10722 Lisp_Object tail, frame;
10723 int changed_count = 0;
10725 FOR_EACH_FRAME (tail, frame)
10727 struct frame *f = XFRAME (frame);
10729 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10731 if (f->resized_p)
10733 redraw_frame (f);
10734 f->force_flush_display_p = 1;
10736 clear_current_matrices (f);
10737 changed_count++;
10738 f->garbaged = 0;
10739 f->resized_p = 0;
10743 frame_garbaged = 0;
10744 if (changed_count)
10745 ++windows_or_buffers_changed;
10750 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10751 is non-zero update selected_frame. Value is non-zero if the
10752 mini-windows height has been changed. */
10754 static int
10755 echo_area_display (int update_frame_p)
10757 Lisp_Object mini_window;
10758 struct window *w;
10759 struct frame *f;
10760 int window_height_changed_p = 0;
10761 struct frame *sf = SELECTED_FRAME ();
10763 mini_window = FRAME_MINIBUF_WINDOW (sf);
10764 w = XWINDOW (mini_window);
10765 f = XFRAME (WINDOW_FRAME (w));
10767 /* Don't display if frame is invisible or not yet initialized. */
10768 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10769 return 0;
10771 #ifdef HAVE_WINDOW_SYSTEM
10772 /* When Emacs starts, selected_frame may be the initial terminal
10773 frame. If we let this through, a message would be displayed on
10774 the terminal. */
10775 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10776 return 0;
10777 #endif /* HAVE_WINDOW_SYSTEM */
10779 /* Redraw garbaged frames. */
10780 clear_garbaged_frames ();
10782 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10784 echo_area_window = mini_window;
10785 window_height_changed_p = display_echo_area (w);
10786 w->must_be_updated_p = 1;
10788 /* Update the display, unless called from redisplay_internal.
10789 Also don't update the screen during redisplay itself. The
10790 update will happen at the end of redisplay, and an update
10791 here could cause confusion. */
10792 if (update_frame_p && !redisplaying_p)
10794 int n = 0;
10796 /* If the display update has been interrupted by pending
10797 input, update mode lines in the frame. Due to the
10798 pending input, it might have been that redisplay hasn't
10799 been called, so that mode lines above the echo area are
10800 garbaged. This looks odd, so we prevent it here. */
10801 if (!display_completed)
10802 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10804 if (window_height_changed_p
10805 /* Don't do this if Emacs is shutting down. Redisplay
10806 needs to run hooks. */
10807 && !NILP (Vrun_hooks))
10809 /* Must update other windows. Likewise as in other
10810 cases, don't let this update be interrupted by
10811 pending input. */
10812 ptrdiff_t count = SPECPDL_INDEX ();
10813 specbind (Qredisplay_dont_pause, Qt);
10814 windows_or_buffers_changed = 1;
10815 redisplay_internal ();
10816 unbind_to (count, Qnil);
10818 else if (FRAME_WINDOW_P (f) && n == 0)
10820 /* Window configuration is the same as before.
10821 Can do with a display update of the echo area,
10822 unless we displayed some mode lines. */
10823 update_single_window (w, 1);
10824 FRAME_RIF (f)->flush_display (f);
10826 else
10827 update_frame (f, 1, 1);
10829 /* If cursor is in the echo area, make sure that the next
10830 redisplay displays the minibuffer, so that the cursor will
10831 be replaced with what the minibuffer wants. */
10832 if (cursor_in_echo_area)
10833 ++windows_or_buffers_changed;
10836 else if (!EQ (mini_window, selected_window))
10837 windows_or_buffers_changed++;
10839 /* Last displayed message is now the current message. */
10840 echo_area_buffer[1] = echo_area_buffer[0];
10841 /* Inform read_char that we're not echoing. */
10842 echo_message_buffer = Qnil;
10844 /* Prevent redisplay optimization in redisplay_internal by resetting
10845 this_line_start_pos. This is done because the mini-buffer now
10846 displays the message instead of its buffer text. */
10847 if (EQ (mini_window, selected_window))
10848 CHARPOS (this_line_start_pos) = 0;
10850 return window_height_changed_p;
10853 /* Nonzero if the current window's buffer is shown in more than one
10854 window and was modified since last redisplay. */
10856 static int
10857 buffer_shared_and_changed (void)
10859 return (buffer_window_count (current_buffer) > 1
10860 && UNCHANGED_MODIFIED < MODIFF);
10863 /* Nonzero if W doesn't reflect the actual state of current buffer due
10864 to its text or overlays change. FIXME: this may be called when
10865 XBUFFER (w->contents) != current_buffer, which looks suspicious. */
10867 static int
10868 window_outdated (struct window *w)
10870 return (w->last_modified < MODIFF
10871 || w->last_overlay_modified < OVERLAY_MODIFF);
10874 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10875 is enabled and mark of W's buffer was changed since last W's update. */
10877 static int
10878 window_buffer_changed (struct window *w)
10880 struct buffer *b = XBUFFER (w->contents);
10882 eassert (BUFFER_LIVE_P (b));
10884 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10885 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10886 != (w->region_showing != 0)));
10889 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10891 static int
10892 mode_line_update_needed (struct window *w)
10894 return (w->column_number_displayed != -1
10895 && !(PT == w->last_point && !window_outdated (w))
10896 && (w->column_number_displayed != current_column ()));
10899 /***********************************************************************
10900 Mode Lines and Frame Titles
10901 ***********************************************************************/
10903 /* A buffer for constructing non-propertized mode-line strings and
10904 frame titles in it; allocated from the heap in init_xdisp and
10905 resized as needed in store_mode_line_noprop_char. */
10907 static char *mode_line_noprop_buf;
10909 /* The buffer's end, and a current output position in it. */
10911 static char *mode_line_noprop_buf_end;
10912 static char *mode_line_noprop_ptr;
10914 #define MODE_LINE_NOPROP_LEN(start) \
10915 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10917 static enum {
10918 MODE_LINE_DISPLAY = 0,
10919 MODE_LINE_TITLE,
10920 MODE_LINE_NOPROP,
10921 MODE_LINE_STRING
10922 } mode_line_target;
10924 /* Alist that caches the results of :propertize.
10925 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10926 static Lisp_Object mode_line_proptrans_alist;
10928 /* List of strings making up the mode-line. */
10929 static Lisp_Object mode_line_string_list;
10931 /* Base face property when building propertized mode line string. */
10932 static Lisp_Object mode_line_string_face;
10933 static Lisp_Object mode_line_string_face_prop;
10936 /* Unwind data for mode line strings */
10938 static Lisp_Object Vmode_line_unwind_vector;
10940 static Lisp_Object
10941 format_mode_line_unwind_data (struct frame *target_frame,
10942 struct buffer *obuf,
10943 Lisp_Object owin,
10944 int save_proptrans)
10946 Lisp_Object vector, tmp;
10948 /* Reduce consing by keeping one vector in
10949 Vwith_echo_area_save_vector. */
10950 vector = Vmode_line_unwind_vector;
10951 Vmode_line_unwind_vector = Qnil;
10953 if (NILP (vector))
10954 vector = Fmake_vector (make_number (10), Qnil);
10956 ASET (vector, 0, make_number (mode_line_target));
10957 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10958 ASET (vector, 2, mode_line_string_list);
10959 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10960 ASET (vector, 4, mode_line_string_face);
10961 ASET (vector, 5, mode_line_string_face_prop);
10963 if (obuf)
10964 XSETBUFFER (tmp, obuf);
10965 else
10966 tmp = Qnil;
10967 ASET (vector, 6, tmp);
10968 ASET (vector, 7, owin);
10969 if (target_frame)
10971 /* Similarly to `with-selected-window', if the operation selects
10972 a window on another frame, we must restore that frame's
10973 selected window, and (for a tty) the top-frame. */
10974 ASET (vector, 8, target_frame->selected_window);
10975 if (FRAME_TERMCAP_P (target_frame))
10976 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10979 return vector;
10982 static Lisp_Object
10983 unwind_format_mode_line (Lisp_Object vector)
10985 Lisp_Object old_window = AREF (vector, 7);
10986 Lisp_Object target_frame_window = AREF (vector, 8);
10987 Lisp_Object old_top_frame = AREF (vector, 9);
10989 mode_line_target = XINT (AREF (vector, 0));
10990 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10991 mode_line_string_list = AREF (vector, 2);
10992 if (! EQ (AREF (vector, 3), Qt))
10993 mode_line_proptrans_alist = AREF (vector, 3);
10994 mode_line_string_face = AREF (vector, 4);
10995 mode_line_string_face_prop = AREF (vector, 5);
10997 /* Select window before buffer, since it may change the buffer. */
10998 if (!NILP (old_window))
11000 /* If the operation that we are unwinding had selected a window
11001 on a different frame, reset its frame-selected-window. For a
11002 text terminal, reset its top-frame if necessary. */
11003 if (!NILP (target_frame_window))
11005 Lisp_Object frame
11006 = WINDOW_FRAME (XWINDOW (target_frame_window));
11008 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11009 Fselect_window (target_frame_window, Qt);
11011 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11012 Fselect_frame (old_top_frame, Qt);
11015 Fselect_window (old_window, Qt);
11018 if (!NILP (AREF (vector, 6)))
11020 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11021 ASET (vector, 6, Qnil);
11024 Vmode_line_unwind_vector = vector;
11025 return Qnil;
11029 /* Store a single character C for the frame title in mode_line_noprop_buf.
11030 Re-allocate mode_line_noprop_buf if necessary. */
11032 static void
11033 store_mode_line_noprop_char (char c)
11035 /* If output position has reached the end of the allocated buffer,
11036 increase the buffer's size. */
11037 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11039 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11040 ptrdiff_t size = len;
11041 mode_line_noprop_buf =
11042 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11043 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11044 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11047 *mode_line_noprop_ptr++ = c;
11051 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11052 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11053 characters that yield more columns than PRECISION; PRECISION <= 0
11054 means copy the whole string. Pad with spaces until FIELD_WIDTH
11055 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11056 pad. Called from display_mode_element when it is used to build a
11057 frame title. */
11059 static int
11060 store_mode_line_noprop (const char *string, int field_width, int precision)
11062 const unsigned char *str = (const unsigned char *) string;
11063 int n = 0;
11064 ptrdiff_t dummy, nbytes;
11066 /* Copy at most PRECISION chars from STR. */
11067 nbytes = strlen (string);
11068 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11069 while (nbytes--)
11070 store_mode_line_noprop_char (*str++);
11072 /* Fill up with spaces until FIELD_WIDTH reached. */
11073 while (field_width > 0
11074 && n < field_width)
11076 store_mode_line_noprop_char (' ');
11077 ++n;
11080 return n;
11083 /***********************************************************************
11084 Frame Titles
11085 ***********************************************************************/
11087 #ifdef HAVE_WINDOW_SYSTEM
11089 /* Set the title of FRAME, if it has changed. The title format is
11090 Vicon_title_format if FRAME is iconified, otherwise it is
11091 frame_title_format. */
11093 static void
11094 x_consider_frame_title (Lisp_Object frame)
11096 struct frame *f = XFRAME (frame);
11098 if (FRAME_WINDOW_P (f)
11099 || FRAME_MINIBUF_ONLY_P (f)
11100 || f->explicit_name)
11102 /* Do we have more than one visible frame on this X display? */
11103 Lisp_Object tail, other_frame, fmt;
11104 ptrdiff_t title_start;
11105 char *title;
11106 ptrdiff_t len;
11107 struct it it;
11108 ptrdiff_t count = SPECPDL_INDEX ();
11110 FOR_EACH_FRAME (tail, other_frame)
11112 struct frame *tf = XFRAME (other_frame);
11114 if (tf != f
11115 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11116 && !FRAME_MINIBUF_ONLY_P (tf)
11117 && !EQ (other_frame, tip_frame)
11118 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11119 break;
11122 /* Set global variable indicating that multiple frames exist. */
11123 multiple_frames = CONSP (tail);
11125 /* Switch to the buffer of selected window of the frame. Set up
11126 mode_line_target so that display_mode_element will output into
11127 mode_line_noprop_buf; then display the title. */
11128 record_unwind_protect (unwind_format_mode_line,
11129 format_mode_line_unwind_data
11130 (f, current_buffer, selected_window, 0));
11132 Fselect_window (f->selected_window, Qt);
11133 set_buffer_internal_1
11134 (XBUFFER (XWINDOW (f->selected_window)->contents));
11135 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11137 mode_line_target = MODE_LINE_TITLE;
11138 title_start = MODE_LINE_NOPROP_LEN (0);
11139 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11140 NULL, DEFAULT_FACE_ID);
11141 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11142 len = MODE_LINE_NOPROP_LEN (title_start);
11143 title = mode_line_noprop_buf + title_start;
11144 unbind_to (count, Qnil);
11146 /* Set the title only if it's changed. This avoids consing in
11147 the common case where it hasn't. (If it turns out that we've
11148 already wasted too much time by walking through the list with
11149 display_mode_element, then we might need to optimize at a
11150 higher level than this.) */
11151 if (! STRINGP (f->name)
11152 || SBYTES (f->name) != len
11153 || memcmp (title, SDATA (f->name), len) != 0)
11154 x_implicitly_set_name (f, make_string (title, len), Qnil);
11158 #endif /* not HAVE_WINDOW_SYSTEM */
11161 /***********************************************************************
11162 Menu Bars
11163 ***********************************************************************/
11166 /* Prepare for redisplay by updating menu-bar item lists when
11167 appropriate. This can call eval. */
11169 void
11170 prepare_menu_bars (void)
11172 int all_windows;
11173 struct gcpro gcpro1, gcpro2;
11174 struct frame *f;
11175 Lisp_Object tooltip_frame;
11177 #ifdef HAVE_WINDOW_SYSTEM
11178 tooltip_frame = tip_frame;
11179 #else
11180 tooltip_frame = Qnil;
11181 #endif
11183 /* Update all frame titles based on their buffer names, etc. We do
11184 this before the menu bars so that the buffer-menu will show the
11185 up-to-date frame titles. */
11186 #ifdef HAVE_WINDOW_SYSTEM
11187 if (windows_or_buffers_changed || update_mode_lines)
11189 Lisp_Object tail, frame;
11191 FOR_EACH_FRAME (tail, frame)
11193 f = XFRAME (frame);
11194 if (!EQ (frame, tooltip_frame)
11195 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11196 x_consider_frame_title (frame);
11199 #endif /* HAVE_WINDOW_SYSTEM */
11201 /* Update the menu bar item lists, if appropriate. This has to be
11202 done before any actual redisplay or generation of display lines. */
11203 all_windows = (update_mode_lines
11204 || buffer_shared_and_changed ()
11205 || windows_or_buffers_changed);
11206 if (all_windows)
11208 Lisp_Object tail, frame;
11209 ptrdiff_t count = SPECPDL_INDEX ();
11210 /* 1 means that update_menu_bar has run its hooks
11211 so any further calls to update_menu_bar shouldn't do so again. */
11212 int menu_bar_hooks_run = 0;
11214 record_unwind_save_match_data ();
11216 FOR_EACH_FRAME (tail, frame)
11218 f = XFRAME (frame);
11220 /* Ignore tooltip frame. */
11221 if (EQ (frame, tooltip_frame))
11222 continue;
11224 /* If a window on this frame changed size, report that to
11225 the user and clear the size-change flag. */
11226 if (FRAME_WINDOW_SIZES_CHANGED (f))
11228 Lisp_Object functions;
11230 /* Clear flag first in case we get an error below. */
11231 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11232 functions = Vwindow_size_change_functions;
11233 GCPRO2 (tail, functions);
11235 while (CONSP (functions))
11237 if (!EQ (XCAR (functions), Qt))
11238 call1 (XCAR (functions), frame);
11239 functions = XCDR (functions);
11241 UNGCPRO;
11244 GCPRO1 (tail);
11245 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11246 #ifdef HAVE_WINDOW_SYSTEM
11247 update_tool_bar (f, 0);
11248 #endif
11249 #ifdef HAVE_NS
11250 if (windows_or_buffers_changed
11251 && FRAME_NS_P (f))
11252 ns_set_doc_edited
11253 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11254 #endif
11255 UNGCPRO;
11258 unbind_to (count, Qnil);
11260 else
11262 struct frame *sf = SELECTED_FRAME ();
11263 update_menu_bar (sf, 1, 0);
11264 #ifdef HAVE_WINDOW_SYSTEM
11265 update_tool_bar (sf, 1);
11266 #endif
11271 /* Update the menu bar item list for frame F. This has to be done
11272 before we start to fill in any display lines, because it can call
11273 eval.
11275 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11277 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11278 already ran the menu bar hooks for this redisplay, so there
11279 is no need to run them again. The return value is the
11280 updated value of this flag, to pass to the next call. */
11282 static int
11283 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11285 Lisp_Object window;
11286 register struct window *w;
11288 /* If called recursively during a menu update, do nothing. This can
11289 happen when, for instance, an activate-menubar-hook causes a
11290 redisplay. */
11291 if (inhibit_menubar_update)
11292 return hooks_run;
11294 window = FRAME_SELECTED_WINDOW (f);
11295 w = XWINDOW (window);
11297 if (FRAME_WINDOW_P (f)
11299 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11300 || defined (HAVE_NS) || defined (USE_GTK)
11301 FRAME_EXTERNAL_MENU_BAR (f)
11302 #else
11303 FRAME_MENU_BAR_LINES (f) > 0
11304 #endif
11305 : FRAME_MENU_BAR_LINES (f) > 0)
11307 /* If the user has switched buffers or windows, we need to
11308 recompute to reflect the new bindings. But we'll
11309 recompute when update_mode_lines is set too; that means
11310 that people can use force-mode-line-update to request
11311 that the menu bar be recomputed. The adverse effect on
11312 the rest of the redisplay algorithm is about the same as
11313 windows_or_buffers_changed anyway. */
11314 if (windows_or_buffers_changed
11315 /* This used to test w->update_mode_line, but we believe
11316 there is no need to recompute the menu in that case. */
11317 || update_mode_lines
11318 || window_buffer_changed (w))
11320 struct buffer *prev = current_buffer;
11321 ptrdiff_t count = SPECPDL_INDEX ();
11323 specbind (Qinhibit_menubar_update, Qt);
11325 set_buffer_internal_1 (XBUFFER (w->contents));
11326 if (save_match_data)
11327 record_unwind_save_match_data ();
11328 if (NILP (Voverriding_local_map_menu_flag))
11330 specbind (Qoverriding_terminal_local_map, Qnil);
11331 specbind (Qoverriding_local_map, Qnil);
11334 if (!hooks_run)
11336 /* Run the Lucid hook. */
11337 safe_run_hooks (Qactivate_menubar_hook);
11339 /* If it has changed current-menubar from previous value,
11340 really recompute the menu-bar from the value. */
11341 if (! NILP (Vlucid_menu_bar_dirty_flag))
11342 call0 (Qrecompute_lucid_menubar);
11344 safe_run_hooks (Qmenu_bar_update_hook);
11346 hooks_run = 1;
11349 XSETFRAME (Vmenu_updating_frame, f);
11350 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11352 /* Redisplay the menu bar in case we changed it. */
11353 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11354 || defined (HAVE_NS) || defined (USE_GTK)
11355 if (FRAME_WINDOW_P (f))
11357 #if defined (HAVE_NS)
11358 /* All frames on Mac OS share the same menubar. So only
11359 the selected frame should be allowed to set it. */
11360 if (f == SELECTED_FRAME ())
11361 #endif
11362 set_frame_menubar (f, 0, 0);
11364 else
11365 /* On a terminal screen, the menu bar is an ordinary screen
11366 line, and this makes it get updated. */
11367 w->update_mode_line = 1;
11368 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11369 /* In the non-toolkit version, the menu bar is an ordinary screen
11370 line, and this makes it get updated. */
11371 w->update_mode_line = 1;
11372 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11374 unbind_to (count, Qnil);
11375 set_buffer_internal_1 (prev);
11379 return hooks_run;
11384 /***********************************************************************
11385 Output Cursor
11386 ***********************************************************************/
11388 #ifdef HAVE_WINDOW_SYSTEM
11390 /* EXPORT:
11391 Nominal cursor position -- where to draw output.
11392 HPOS and VPOS are window relative glyph matrix coordinates.
11393 X and Y are window relative pixel coordinates. */
11395 struct cursor_pos output_cursor;
11398 /* EXPORT:
11399 Set the global variable output_cursor to CURSOR. All cursor
11400 positions are relative to updated_window. */
11402 void
11403 set_output_cursor (struct cursor_pos *cursor)
11405 output_cursor.hpos = cursor->hpos;
11406 output_cursor.vpos = cursor->vpos;
11407 output_cursor.x = cursor->x;
11408 output_cursor.y = cursor->y;
11412 /* EXPORT for RIF:
11413 Set a nominal cursor position.
11415 HPOS and VPOS are column/row positions in a window glyph matrix. X
11416 and Y are window text area relative pixel positions.
11418 If this is done during an update, updated_window will contain the
11419 window that is being updated and the position is the future output
11420 cursor position for that window. If updated_window is null, use
11421 selected_window and display the cursor at the given position. */
11423 void
11424 x_cursor_to (int vpos, int hpos, int y, int x)
11426 struct window *w;
11428 /* If updated_window is not set, work on selected_window. */
11429 if (updated_window)
11430 w = updated_window;
11431 else
11432 w = XWINDOW (selected_window);
11434 /* Set the output cursor. */
11435 output_cursor.hpos = hpos;
11436 output_cursor.vpos = vpos;
11437 output_cursor.x = x;
11438 output_cursor.y = y;
11440 /* If not called as part of an update, really display the cursor.
11441 This will also set the cursor position of W. */
11442 if (updated_window == NULL)
11444 block_input ();
11445 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11446 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11447 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11448 unblock_input ();
11452 #endif /* HAVE_WINDOW_SYSTEM */
11455 /***********************************************************************
11456 Tool-bars
11457 ***********************************************************************/
11459 #ifdef HAVE_WINDOW_SYSTEM
11461 /* Where the mouse was last time we reported a mouse event. */
11463 FRAME_PTR last_mouse_frame;
11465 /* Tool-bar item index of the item on which a mouse button was pressed
11466 or -1. */
11468 int last_tool_bar_item;
11470 /* Select `frame' temporarily without running all the code in
11471 do_switch_frame.
11472 FIXME: Maybe do_switch_frame should be trimmed down similarly
11473 when `norecord' is set. */
11474 static Lisp_Object
11475 fast_set_selected_frame (Lisp_Object frame)
11477 if (!EQ (selected_frame, frame))
11479 selected_frame = frame;
11480 selected_window = XFRAME (frame)->selected_window;
11482 return Qnil;
11485 /* Update the tool-bar item list for frame F. This has to be done
11486 before we start to fill in any display lines. Called from
11487 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11488 and restore it here. */
11490 static void
11491 update_tool_bar (struct frame *f, int save_match_data)
11493 #if defined (USE_GTK) || defined (HAVE_NS)
11494 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11495 #else
11496 int do_update = WINDOWP (f->tool_bar_window)
11497 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11498 #endif
11500 if (do_update)
11502 Lisp_Object window;
11503 struct window *w;
11505 window = FRAME_SELECTED_WINDOW (f);
11506 w = XWINDOW (window);
11508 /* If the user has switched buffers or windows, we need to
11509 recompute to reflect the new bindings. But we'll
11510 recompute when update_mode_lines is set too; that means
11511 that people can use force-mode-line-update to request
11512 that the menu bar be recomputed. The adverse effect on
11513 the rest of the redisplay algorithm is about the same as
11514 windows_or_buffers_changed anyway. */
11515 if (windows_or_buffers_changed
11516 || w->update_mode_line
11517 || update_mode_lines
11518 || window_buffer_changed (w))
11520 struct buffer *prev = current_buffer;
11521 ptrdiff_t count = SPECPDL_INDEX ();
11522 Lisp_Object frame, new_tool_bar;
11523 int new_n_tool_bar;
11524 struct gcpro gcpro1;
11526 /* Set current_buffer to the buffer of the selected
11527 window of the frame, so that we get the right local
11528 keymaps. */
11529 set_buffer_internal_1 (XBUFFER (w->contents));
11531 /* Save match data, if we must. */
11532 if (save_match_data)
11533 record_unwind_save_match_data ();
11535 /* Make sure that we don't accidentally use bogus keymaps. */
11536 if (NILP (Voverriding_local_map_menu_flag))
11538 specbind (Qoverriding_terminal_local_map, Qnil);
11539 specbind (Qoverriding_local_map, Qnil);
11542 GCPRO1 (new_tool_bar);
11544 /* We must temporarily set the selected frame to this frame
11545 before calling tool_bar_items, because the calculation of
11546 the tool-bar keymap uses the selected frame (see
11547 `tool-bar-make-keymap' in tool-bar.el). */
11548 eassert (EQ (selected_window,
11549 /* Since we only explicitly preserve selected_frame,
11550 check that selected_window would be redundant. */
11551 XFRAME (selected_frame)->selected_window));
11552 record_unwind_protect (fast_set_selected_frame, selected_frame);
11553 XSETFRAME (frame, f);
11554 fast_set_selected_frame (frame);
11556 /* Build desired tool-bar items from keymaps. */
11557 new_tool_bar
11558 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11559 &new_n_tool_bar);
11561 /* Redisplay the tool-bar if we changed it. */
11562 if (new_n_tool_bar != f->n_tool_bar_items
11563 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11565 /* Redisplay that happens asynchronously due to an expose event
11566 may access f->tool_bar_items. Make sure we update both
11567 variables within BLOCK_INPUT so no such event interrupts. */
11568 block_input ();
11569 fset_tool_bar_items (f, new_tool_bar);
11570 f->n_tool_bar_items = new_n_tool_bar;
11571 w->update_mode_line = 1;
11572 unblock_input ();
11575 UNGCPRO;
11577 unbind_to (count, Qnil);
11578 set_buffer_internal_1 (prev);
11584 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11585 F's desired tool-bar contents. F->tool_bar_items must have
11586 been set up previously by calling prepare_menu_bars. */
11588 static void
11589 build_desired_tool_bar_string (struct frame *f)
11591 int i, size, size_needed;
11592 struct gcpro gcpro1, gcpro2, gcpro3;
11593 Lisp_Object image, plist, props;
11595 image = plist = props = Qnil;
11596 GCPRO3 (image, plist, props);
11598 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11599 Otherwise, make a new string. */
11601 /* The size of the string we might be able to reuse. */
11602 size = (STRINGP (f->desired_tool_bar_string)
11603 ? SCHARS (f->desired_tool_bar_string)
11604 : 0);
11606 /* We need one space in the string for each image. */
11607 size_needed = f->n_tool_bar_items;
11609 /* Reuse f->desired_tool_bar_string, if possible. */
11610 if (size < size_needed || NILP (f->desired_tool_bar_string))
11611 fset_desired_tool_bar_string
11612 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11613 else
11615 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11616 Fremove_text_properties (make_number (0), make_number (size),
11617 props, f->desired_tool_bar_string);
11620 /* Put a `display' property on the string for the images to display,
11621 put a `menu_item' property on tool-bar items with a value that
11622 is the index of the item in F's tool-bar item vector. */
11623 for (i = 0; i < f->n_tool_bar_items; ++i)
11625 #define PROP(IDX) \
11626 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11628 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11629 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11630 int hmargin, vmargin, relief, idx, end;
11632 /* If image is a vector, choose the image according to the
11633 button state. */
11634 image = PROP (TOOL_BAR_ITEM_IMAGES);
11635 if (VECTORP (image))
11637 if (enabled_p)
11638 idx = (selected_p
11639 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11640 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11641 else
11642 idx = (selected_p
11643 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11644 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11646 eassert (ASIZE (image) >= idx);
11647 image = AREF (image, idx);
11649 else
11650 idx = -1;
11652 /* Ignore invalid image specifications. */
11653 if (!valid_image_p (image))
11654 continue;
11656 /* Display the tool-bar button pressed, or depressed. */
11657 plist = Fcopy_sequence (XCDR (image));
11659 /* Compute margin and relief to draw. */
11660 relief = (tool_bar_button_relief >= 0
11661 ? tool_bar_button_relief
11662 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11663 hmargin = vmargin = relief;
11665 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11666 INT_MAX - max (hmargin, vmargin)))
11668 hmargin += XFASTINT (Vtool_bar_button_margin);
11669 vmargin += XFASTINT (Vtool_bar_button_margin);
11671 else if (CONSP (Vtool_bar_button_margin))
11673 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11674 INT_MAX - hmargin))
11675 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11677 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11678 INT_MAX - vmargin))
11679 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11682 if (auto_raise_tool_bar_buttons_p)
11684 /* Add a `:relief' property to the image spec if the item is
11685 selected. */
11686 if (selected_p)
11688 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11689 hmargin -= relief;
11690 vmargin -= relief;
11693 else
11695 /* If image is selected, display it pressed, i.e. with a
11696 negative relief. If it's not selected, display it with a
11697 raised relief. */
11698 plist = Fplist_put (plist, QCrelief,
11699 (selected_p
11700 ? make_number (-relief)
11701 : make_number (relief)));
11702 hmargin -= relief;
11703 vmargin -= relief;
11706 /* Put a margin around the image. */
11707 if (hmargin || vmargin)
11709 if (hmargin == vmargin)
11710 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11711 else
11712 plist = Fplist_put (plist, QCmargin,
11713 Fcons (make_number (hmargin),
11714 make_number (vmargin)));
11717 /* If button is not enabled, and we don't have special images
11718 for the disabled state, make the image appear disabled by
11719 applying an appropriate algorithm to it. */
11720 if (!enabled_p && idx < 0)
11721 plist = Fplist_put (plist, QCconversion, Qdisabled);
11723 /* Put a `display' text property on the string for the image to
11724 display. Put a `menu-item' property on the string that gives
11725 the start of this item's properties in the tool-bar items
11726 vector. */
11727 image = Fcons (Qimage, plist);
11728 props = list4 (Qdisplay, image,
11729 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11731 /* Let the last image hide all remaining spaces in the tool bar
11732 string. The string can be longer than needed when we reuse a
11733 previous string. */
11734 if (i + 1 == f->n_tool_bar_items)
11735 end = SCHARS (f->desired_tool_bar_string);
11736 else
11737 end = i + 1;
11738 Fadd_text_properties (make_number (i), make_number (end),
11739 props, f->desired_tool_bar_string);
11740 #undef PROP
11743 UNGCPRO;
11747 /* Display one line of the tool-bar of frame IT->f.
11749 HEIGHT specifies the desired height of the tool-bar line.
11750 If the actual height of the glyph row is less than HEIGHT, the
11751 row's height is increased to HEIGHT, and the icons are centered
11752 vertically in the new height.
11754 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11755 count a final empty row in case the tool-bar width exactly matches
11756 the window width.
11759 static void
11760 display_tool_bar_line (struct it *it, int height)
11762 struct glyph_row *row = it->glyph_row;
11763 int max_x = it->last_visible_x;
11764 struct glyph *last;
11766 prepare_desired_row (row);
11767 row->y = it->current_y;
11769 /* Note that this isn't made use of if the face hasn't a box,
11770 so there's no need to check the face here. */
11771 it->start_of_box_run_p = 1;
11773 while (it->current_x < max_x)
11775 int x, n_glyphs_before, i, nglyphs;
11776 struct it it_before;
11778 /* Get the next display element. */
11779 if (!get_next_display_element (it))
11781 /* Don't count empty row if we are counting needed tool-bar lines. */
11782 if (height < 0 && !it->hpos)
11783 return;
11784 break;
11787 /* Produce glyphs. */
11788 n_glyphs_before = row->used[TEXT_AREA];
11789 it_before = *it;
11791 PRODUCE_GLYPHS (it);
11793 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11794 i = 0;
11795 x = it_before.current_x;
11796 while (i < nglyphs)
11798 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11800 if (x + glyph->pixel_width > max_x)
11802 /* Glyph doesn't fit on line. Backtrack. */
11803 row->used[TEXT_AREA] = n_glyphs_before;
11804 *it = it_before;
11805 /* If this is the only glyph on this line, it will never fit on the
11806 tool-bar, so skip it. But ensure there is at least one glyph,
11807 so we don't accidentally disable the tool-bar. */
11808 if (n_glyphs_before == 0
11809 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11810 break;
11811 goto out;
11814 ++it->hpos;
11815 x += glyph->pixel_width;
11816 ++i;
11819 /* Stop at line end. */
11820 if (ITERATOR_AT_END_OF_LINE_P (it))
11821 break;
11823 set_iterator_to_next (it, 1);
11826 out:;
11828 row->displays_text_p = row->used[TEXT_AREA] != 0;
11830 /* Use default face for the border below the tool bar.
11832 FIXME: When auto-resize-tool-bars is grow-only, there is
11833 no additional border below the possibly empty tool-bar lines.
11834 So to make the extra empty lines look "normal", we have to
11835 use the tool-bar face for the border too. */
11836 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11837 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11838 it->face_id = DEFAULT_FACE_ID;
11840 extend_face_to_end_of_line (it);
11841 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11842 last->right_box_line_p = 1;
11843 if (last == row->glyphs[TEXT_AREA])
11844 last->left_box_line_p = 1;
11846 /* Make line the desired height and center it vertically. */
11847 if ((height -= it->max_ascent + it->max_descent) > 0)
11849 /* Don't add more than one line height. */
11850 height %= FRAME_LINE_HEIGHT (it->f);
11851 it->max_ascent += height / 2;
11852 it->max_descent += (height + 1) / 2;
11855 compute_line_metrics (it);
11857 /* If line is empty, make it occupy the rest of the tool-bar. */
11858 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11860 row->height = row->phys_height = it->last_visible_y - row->y;
11861 row->visible_height = row->height;
11862 row->ascent = row->phys_ascent = 0;
11863 row->extra_line_spacing = 0;
11866 row->full_width_p = 1;
11867 row->continued_p = 0;
11868 row->truncated_on_left_p = 0;
11869 row->truncated_on_right_p = 0;
11871 it->current_x = it->hpos = 0;
11872 it->current_y += row->height;
11873 ++it->vpos;
11874 ++it->glyph_row;
11878 /* Max tool-bar height. */
11880 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11881 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11883 /* Value is the number of screen lines needed to make all tool-bar
11884 items of frame F visible. The number of actual rows needed is
11885 returned in *N_ROWS if non-NULL. */
11887 static int
11888 tool_bar_lines_needed (struct frame *f, int *n_rows)
11890 struct window *w = XWINDOW (f->tool_bar_window);
11891 struct it it;
11892 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11893 the desired matrix, so use (unused) mode-line row as temporary row to
11894 avoid destroying the first tool-bar row. */
11895 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11897 /* Initialize an iterator for iteration over
11898 F->desired_tool_bar_string in the tool-bar window of frame F. */
11899 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11900 it.first_visible_x = 0;
11901 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11902 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11903 it.paragraph_embedding = L2R;
11905 while (!ITERATOR_AT_END_P (&it))
11907 clear_glyph_row (temp_row);
11908 it.glyph_row = temp_row;
11909 display_tool_bar_line (&it, -1);
11911 clear_glyph_row (temp_row);
11913 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11914 if (n_rows)
11915 *n_rows = it.vpos > 0 ? it.vpos : -1;
11917 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11921 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11922 0, 1, 0,
11923 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11924 If FRAME is nil or omitted, use the selected frame. */)
11925 (Lisp_Object frame)
11927 struct frame *f = decode_any_frame (frame);
11928 struct window *w;
11929 int nlines = 0;
11931 if (WINDOWP (f->tool_bar_window)
11932 && (w = XWINDOW (f->tool_bar_window),
11933 WINDOW_TOTAL_LINES (w) > 0))
11935 update_tool_bar (f, 1);
11936 if (f->n_tool_bar_items)
11938 build_desired_tool_bar_string (f);
11939 nlines = tool_bar_lines_needed (f, NULL);
11943 return make_number (nlines);
11947 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11948 height should be changed. */
11950 static int
11951 redisplay_tool_bar (struct frame *f)
11953 struct window *w;
11954 struct it it;
11955 struct glyph_row *row;
11957 #if defined (USE_GTK) || defined (HAVE_NS)
11958 if (FRAME_EXTERNAL_TOOL_BAR (f))
11959 update_frame_tool_bar (f);
11960 return 0;
11961 #endif
11963 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11964 do anything. This means you must start with tool-bar-lines
11965 non-zero to get the auto-sizing effect. Or in other words, you
11966 can turn off tool-bars by specifying tool-bar-lines zero. */
11967 if (!WINDOWP (f->tool_bar_window)
11968 || (w = XWINDOW (f->tool_bar_window),
11969 WINDOW_TOTAL_LINES (w) == 0))
11970 return 0;
11972 /* Set up an iterator for the tool-bar window. */
11973 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11974 it.first_visible_x = 0;
11975 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11976 row = it.glyph_row;
11978 /* Build a string that represents the contents of the tool-bar. */
11979 build_desired_tool_bar_string (f);
11980 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11981 /* FIXME: This should be controlled by a user option. But it
11982 doesn't make sense to have an R2L tool bar if the menu bar cannot
11983 be drawn also R2L, and making the menu bar R2L is tricky due
11984 toolkit-specific code that implements it. If an R2L tool bar is
11985 ever supported, display_tool_bar_line should also be augmented to
11986 call unproduce_glyphs like display_line and display_string
11987 do. */
11988 it.paragraph_embedding = L2R;
11990 if (f->n_tool_bar_rows == 0)
11992 int nlines;
11994 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11995 nlines != WINDOW_TOTAL_LINES (w)))
11997 Lisp_Object frame;
11998 int old_height = WINDOW_TOTAL_LINES (w);
12000 XSETFRAME (frame, f);
12001 Fmodify_frame_parameters (frame,
12002 Fcons (Fcons (Qtool_bar_lines,
12003 make_number (nlines)),
12004 Qnil));
12005 if (WINDOW_TOTAL_LINES (w) != old_height)
12007 clear_glyph_matrix (w->desired_matrix);
12008 fonts_changed_p = 1;
12009 return 1;
12014 /* Display as many lines as needed to display all tool-bar items. */
12016 if (f->n_tool_bar_rows > 0)
12018 int border, rows, height, extra;
12020 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12021 border = XINT (Vtool_bar_border);
12022 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12023 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12024 else if (EQ (Vtool_bar_border, Qborder_width))
12025 border = f->border_width;
12026 else
12027 border = 0;
12028 if (border < 0)
12029 border = 0;
12031 rows = f->n_tool_bar_rows;
12032 height = max (1, (it.last_visible_y - border) / rows);
12033 extra = it.last_visible_y - border - height * rows;
12035 while (it.current_y < it.last_visible_y)
12037 int h = 0;
12038 if (extra > 0 && rows-- > 0)
12040 h = (extra + rows - 1) / rows;
12041 extra -= h;
12043 display_tool_bar_line (&it, height + h);
12046 else
12048 while (it.current_y < it.last_visible_y)
12049 display_tool_bar_line (&it, 0);
12052 /* It doesn't make much sense to try scrolling in the tool-bar
12053 window, so don't do it. */
12054 w->desired_matrix->no_scrolling_p = 1;
12055 w->must_be_updated_p = 1;
12057 if (!NILP (Vauto_resize_tool_bars))
12059 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12060 int change_height_p = 0;
12062 /* If we couldn't display everything, change the tool-bar's
12063 height if there is room for more. */
12064 if (IT_STRING_CHARPOS (it) < it.end_charpos
12065 && it.current_y < max_tool_bar_height)
12066 change_height_p = 1;
12068 row = it.glyph_row - 1;
12070 /* If there are blank lines at the end, except for a partially
12071 visible blank line at the end that is smaller than
12072 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12073 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12074 && row->height >= FRAME_LINE_HEIGHT (f))
12075 change_height_p = 1;
12077 /* If row displays tool-bar items, but is partially visible,
12078 change the tool-bar's height. */
12079 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12080 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12081 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12082 change_height_p = 1;
12084 /* Resize windows as needed by changing the `tool-bar-lines'
12085 frame parameter. */
12086 if (change_height_p)
12088 Lisp_Object frame;
12089 int old_height = WINDOW_TOTAL_LINES (w);
12090 int nrows;
12091 int nlines = tool_bar_lines_needed (f, &nrows);
12093 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12094 && !f->minimize_tool_bar_window_p)
12095 ? (nlines > old_height)
12096 : (nlines != old_height));
12097 f->minimize_tool_bar_window_p = 0;
12099 if (change_height_p)
12101 XSETFRAME (frame, f);
12102 Fmodify_frame_parameters (frame,
12103 Fcons (Fcons (Qtool_bar_lines,
12104 make_number (nlines)),
12105 Qnil));
12106 if (WINDOW_TOTAL_LINES (w) != old_height)
12108 clear_glyph_matrix (w->desired_matrix);
12109 f->n_tool_bar_rows = nrows;
12110 fonts_changed_p = 1;
12111 return 1;
12117 f->minimize_tool_bar_window_p = 0;
12118 return 0;
12122 /* Get information about the tool-bar item which is displayed in GLYPH
12123 on frame F. Return in *PROP_IDX the index where tool-bar item
12124 properties start in F->tool_bar_items. Value is zero if
12125 GLYPH doesn't display a tool-bar item. */
12127 static int
12128 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12130 Lisp_Object prop;
12131 int success_p;
12132 int charpos;
12134 /* This function can be called asynchronously, which means we must
12135 exclude any possibility that Fget_text_property signals an
12136 error. */
12137 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12138 charpos = max (0, charpos);
12140 /* Get the text property `menu-item' at pos. The value of that
12141 property is the start index of this item's properties in
12142 F->tool_bar_items. */
12143 prop = Fget_text_property (make_number (charpos),
12144 Qmenu_item, f->current_tool_bar_string);
12145 if (INTEGERP (prop))
12147 *prop_idx = XINT (prop);
12148 success_p = 1;
12150 else
12151 success_p = 0;
12153 return success_p;
12157 /* Get information about the tool-bar item at position X/Y on frame F.
12158 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12159 the current matrix of the tool-bar window of F, or NULL if not
12160 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12161 item in F->tool_bar_items. Value is
12163 -1 if X/Y is not on a tool-bar item
12164 0 if X/Y is on the same item that was highlighted before.
12165 1 otherwise. */
12167 static int
12168 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12169 int *hpos, int *vpos, int *prop_idx)
12171 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12172 struct window *w = XWINDOW (f->tool_bar_window);
12173 int area;
12175 /* Find the glyph under X/Y. */
12176 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12177 if (*glyph == NULL)
12178 return -1;
12180 /* Get the start of this tool-bar item's properties in
12181 f->tool_bar_items. */
12182 if (!tool_bar_item_info (f, *glyph, prop_idx))
12183 return -1;
12185 /* Is mouse on the highlighted item? */
12186 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12187 && *vpos >= hlinfo->mouse_face_beg_row
12188 && *vpos <= hlinfo->mouse_face_end_row
12189 && (*vpos > hlinfo->mouse_face_beg_row
12190 || *hpos >= hlinfo->mouse_face_beg_col)
12191 && (*vpos < hlinfo->mouse_face_end_row
12192 || *hpos < hlinfo->mouse_face_end_col
12193 || hlinfo->mouse_face_past_end))
12194 return 0;
12196 return 1;
12200 /* EXPORT:
12201 Handle mouse button event on the tool-bar of frame F, at
12202 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12203 0 for button release. MODIFIERS is event modifiers for button
12204 release. */
12206 void
12207 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12208 int modifiers)
12210 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12211 struct window *w = XWINDOW (f->tool_bar_window);
12212 int hpos, vpos, prop_idx;
12213 struct glyph *glyph;
12214 Lisp_Object enabled_p;
12215 int ts;
12217 /* If not on the highlighted tool-bar item, and mouse-highlight is
12218 non-nil, return. This is so we generate the tool-bar button
12219 click only when the mouse button is released on the same item as
12220 where it was pressed. However, when mouse-highlight is disabled,
12221 generate the click when the button is released regardless of the
12222 highlight, since tool-bar items are not highlighted in that
12223 case. */
12224 frame_to_window_pixel_xy (w, &x, &y);
12225 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12226 if (ts == -1
12227 || (ts != 0 && !NILP (Vmouse_highlight)))
12228 return;
12230 /* When mouse-highlight is off, generate the click for the item
12231 where the button was pressed, disregarding where it was
12232 released. */
12233 if (NILP (Vmouse_highlight) && !down_p)
12234 prop_idx = last_tool_bar_item;
12236 /* If item is disabled, do nothing. */
12237 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12238 if (NILP (enabled_p))
12239 return;
12241 if (down_p)
12243 /* Show item in pressed state. */
12244 if (!NILP (Vmouse_highlight))
12245 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12246 last_tool_bar_item = prop_idx;
12248 else
12250 Lisp_Object key, frame;
12251 struct input_event event;
12252 EVENT_INIT (event);
12254 /* Show item in released state. */
12255 if (!NILP (Vmouse_highlight))
12256 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12258 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12260 XSETFRAME (frame, f);
12261 event.kind = TOOL_BAR_EVENT;
12262 event.frame_or_window = frame;
12263 event.arg = frame;
12264 kbd_buffer_store_event (&event);
12266 event.kind = TOOL_BAR_EVENT;
12267 event.frame_or_window = frame;
12268 event.arg = key;
12269 event.modifiers = modifiers;
12270 kbd_buffer_store_event (&event);
12271 last_tool_bar_item = -1;
12276 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12277 tool-bar window-relative coordinates X/Y. Called from
12278 note_mouse_highlight. */
12280 static void
12281 note_tool_bar_highlight (struct frame *f, int x, int y)
12283 Lisp_Object window = f->tool_bar_window;
12284 struct window *w = XWINDOW (window);
12285 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12286 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12287 int hpos, vpos;
12288 struct glyph *glyph;
12289 struct glyph_row *row;
12290 int i;
12291 Lisp_Object enabled_p;
12292 int prop_idx;
12293 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12294 int mouse_down_p, rc;
12296 /* Function note_mouse_highlight is called with negative X/Y
12297 values when mouse moves outside of the frame. */
12298 if (x <= 0 || y <= 0)
12300 clear_mouse_face (hlinfo);
12301 return;
12304 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12305 if (rc < 0)
12307 /* Not on tool-bar item. */
12308 clear_mouse_face (hlinfo);
12309 return;
12311 else if (rc == 0)
12312 /* On same tool-bar item as before. */
12313 goto set_help_echo;
12315 clear_mouse_face (hlinfo);
12317 /* Mouse is down, but on different tool-bar item? */
12318 mouse_down_p = (dpyinfo->grabbed
12319 && f == last_mouse_frame
12320 && FRAME_LIVE_P (f));
12321 if (mouse_down_p
12322 && last_tool_bar_item != prop_idx)
12323 return;
12325 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12327 /* If tool-bar item is not enabled, don't highlight it. */
12328 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12329 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12331 /* Compute the x-position of the glyph. In front and past the
12332 image is a space. We include this in the highlighted area. */
12333 row = MATRIX_ROW (w->current_matrix, vpos);
12334 for (i = x = 0; i < hpos; ++i)
12335 x += row->glyphs[TEXT_AREA][i].pixel_width;
12337 /* Record this as the current active region. */
12338 hlinfo->mouse_face_beg_col = hpos;
12339 hlinfo->mouse_face_beg_row = vpos;
12340 hlinfo->mouse_face_beg_x = x;
12341 hlinfo->mouse_face_beg_y = row->y;
12342 hlinfo->mouse_face_past_end = 0;
12344 hlinfo->mouse_face_end_col = hpos + 1;
12345 hlinfo->mouse_face_end_row = vpos;
12346 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12347 hlinfo->mouse_face_end_y = row->y;
12348 hlinfo->mouse_face_window = window;
12349 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12351 /* Display it as active. */
12352 show_mouse_face (hlinfo, draw);
12355 set_help_echo:
12357 /* Set help_echo_string to a help string to display for this tool-bar item.
12358 XTread_socket does the rest. */
12359 help_echo_object = help_echo_window = Qnil;
12360 help_echo_pos = -1;
12361 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12362 if (NILP (help_echo_string))
12363 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12366 #endif /* HAVE_WINDOW_SYSTEM */
12370 /************************************************************************
12371 Horizontal scrolling
12372 ************************************************************************/
12374 static int hscroll_window_tree (Lisp_Object);
12375 static int hscroll_windows (Lisp_Object);
12377 /* For all leaf windows in the window tree rooted at WINDOW, set their
12378 hscroll value so that PT is (i) visible in the window, and (ii) so
12379 that it is not within a certain margin at the window's left and
12380 right border. Value is non-zero if any window's hscroll has been
12381 changed. */
12383 static int
12384 hscroll_window_tree (Lisp_Object window)
12386 int hscrolled_p = 0;
12387 int hscroll_relative_p = FLOATP (Vhscroll_step);
12388 int hscroll_step_abs = 0;
12389 double hscroll_step_rel = 0;
12391 if (hscroll_relative_p)
12393 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12394 if (hscroll_step_rel < 0)
12396 hscroll_relative_p = 0;
12397 hscroll_step_abs = 0;
12400 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12402 hscroll_step_abs = XINT (Vhscroll_step);
12403 if (hscroll_step_abs < 0)
12404 hscroll_step_abs = 0;
12406 else
12407 hscroll_step_abs = 0;
12409 while (WINDOWP (window))
12411 struct window *w = XWINDOW (window);
12413 if (WINDOWP (w->contents))
12414 hscrolled_p |= hscroll_window_tree (w->contents);
12415 else if (w->cursor.vpos >= 0)
12417 int h_margin;
12418 int text_area_width;
12419 struct glyph_row *current_cursor_row
12420 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12421 struct glyph_row *desired_cursor_row
12422 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12423 struct glyph_row *cursor_row
12424 = (desired_cursor_row->enabled_p
12425 ? desired_cursor_row
12426 : current_cursor_row);
12427 int row_r2l_p = cursor_row->reversed_p;
12429 text_area_width = window_box_width (w, TEXT_AREA);
12431 /* Scroll when cursor is inside this scroll margin. */
12432 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12434 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12435 /* For left-to-right rows, hscroll when cursor is either
12436 (i) inside the right hscroll margin, or (ii) if it is
12437 inside the left margin and the window is already
12438 hscrolled. */
12439 && ((!row_r2l_p
12440 && ((w->hscroll
12441 && w->cursor.x <= h_margin)
12442 || (cursor_row->enabled_p
12443 && cursor_row->truncated_on_right_p
12444 && (w->cursor.x >= text_area_width - h_margin))))
12445 /* For right-to-left rows, the logic is similar,
12446 except that rules for scrolling to left and right
12447 are reversed. E.g., if cursor.x <= h_margin, we
12448 need to hscroll "to the right" unconditionally,
12449 and that will scroll the screen to the left so as
12450 to reveal the next portion of the row. */
12451 || (row_r2l_p
12452 && ((cursor_row->enabled_p
12453 /* FIXME: It is confusing to set the
12454 truncated_on_right_p flag when R2L rows
12455 are actually truncated on the left. */
12456 && cursor_row->truncated_on_right_p
12457 && w->cursor.x <= h_margin)
12458 || (w->hscroll
12459 && (w->cursor.x >= text_area_width - h_margin))))))
12461 struct it it;
12462 ptrdiff_t hscroll;
12463 struct buffer *saved_current_buffer;
12464 ptrdiff_t pt;
12465 int wanted_x;
12467 /* Find point in a display of infinite width. */
12468 saved_current_buffer = current_buffer;
12469 current_buffer = XBUFFER (w->contents);
12471 if (w == XWINDOW (selected_window))
12472 pt = PT;
12473 else
12474 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12476 /* Move iterator to pt starting at cursor_row->start in
12477 a line with infinite width. */
12478 init_to_row_start (&it, w, cursor_row);
12479 it.last_visible_x = INFINITY;
12480 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12481 current_buffer = saved_current_buffer;
12483 /* Position cursor in window. */
12484 if (!hscroll_relative_p && hscroll_step_abs == 0)
12485 hscroll = max (0, (it.current_x
12486 - (ITERATOR_AT_END_OF_LINE_P (&it)
12487 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12488 : (text_area_width / 2))))
12489 / FRAME_COLUMN_WIDTH (it.f);
12490 else if ((!row_r2l_p
12491 && w->cursor.x >= text_area_width - h_margin)
12492 || (row_r2l_p && w->cursor.x <= h_margin))
12494 if (hscroll_relative_p)
12495 wanted_x = text_area_width * (1 - hscroll_step_rel)
12496 - h_margin;
12497 else
12498 wanted_x = text_area_width
12499 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12500 - h_margin;
12501 hscroll
12502 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12504 else
12506 if (hscroll_relative_p)
12507 wanted_x = text_area_width * hscroll_step_rel
12508 + h_margin;
12509 else
12510 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12511 + h_margin;
12512 hscroll
12513 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12515 hscroll = max (hscroll, w->min_hscroll);
12517 /* Don't prevent redisplay optimizations if hscroll
12518 hasn't changed, as it will unnecessarily slow down
12519 redisplay. */
12520 if (w->hscroll != hscroll)
12522 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12523 w->hscroll = hscroll;
12524 hscrolled_p = 1;
12529 window = w->next;
12532 /* Value is non-zero if hscroll of any leaf window has been changed. */
12533 return hscrolled_p;
12537 /* Set hscroll so that cursor is visible and not inside horizontal
12538 scroll margins for all windows in the tree rooted at WINDOW. See
12539 also hscroll_window_tree above. Value is non-zero if any window's
12540 hscroll has been changed. If it has, desired matrices on the frame
12541 of WINDOW are cleared. */
12543 static int
12544 hscroll_windows (Lisp_Object window)
12546 int hscrolled_p = hscroll_window_tree (window);
12547 if (hscrolled_p)
12548 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12549 return hscrolled_p;
12554 /************************************************************************
12555 Redisplay
12556 ************************************************************************/
12558 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12559 to a non-zero value. This is sometimes handy to have in a debugger
12560 session. */
12562 #ifdef GLYPH_DEBUG
12564 /* First and last unchanged row for try_window_id. */
12566 static int debug_first_unchanged_at_end_vpos;
12567 static int debug_last_unchanged_at_beg_vpos;
12569 /* Delta vpos and y. */
12571 static int debug_dvpos, debug_dy;
12573 /* Delta in characters and bytes for try_window_id. */
12575 static ptrdiff_t debug_delta, debug_delta_bytes;
12577 /* Values of window_end_pos and window_end_vpos at the end of
12578 try_window_id. */
12580 static ptrdiff_t debug_end_vpos;
12582 /* Append a string to W->desired_matrix->method. FMT is a printf
12583 format string. If trace_redisplay_p is non-zero also printf the
12584 resulting string to stderr. */
12586 static void debug_method_add (struct window *, char const *, ...)
12587 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12589 static void
12590 debug_method_add (struct window *w, char const *fmt, ...)
12592 void *ptr = w;
12593 char *method = w->desired_matrix->method;
12594 int len = strlen (method);
12595 int size = sizeof w->desired_matrix->method;
12596 int remaining = size - len - 1;
12597 va_list ap;
12599 if (len && remaining)
12601 method[len] = '|';
12602 --remaining, ++len;
12605 va_start (ap, fmt);
12606 vsnprintf (method + len, remaining + 1, fmt, ap);
12607 va_end (ap);
12609 if (trace_redisplay_p)
12610 fprintf (stderr, "%p (%s): %s\n",
12611 ptr,
12612 ((BUFFERP (w->contents)
12613 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12614 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12615 : "no buffer"),
12616 method + len);
12619 #endif /* GLYPH_DEBUG */
12622 /* Value is non-zero if all changes in window W, which displays
12623 current_buffer, are in the text between START and END. START is a
12624 buffer position, END is given as a distance from Z. Used in
12625 redisplay_internal for display optimization. */
12627 static int
12628 text_outside_line_unchanged_p (struct window *w,
12629 ptrdiff_t start, ptrdiff_t end)
12631 int unchanged_p = 1;
12633 /* If text or overlays have changed, see where. */
12634 if (window_outdated (w))
12636 /* Gap in the line? */
12637 if (GPT < start || Z - GPT < end)
12638 unchanged_p = 0;
12640 /* Changes start in front of the line, or end after it? */
12641 if (unchanged_p
12642 && (BEG_UNCHANGED < start - 1
12643 || END_UNCHANGED < end))
12644 unchanged_p = 0;
12646 /* If selective display, can't optimize if changes start at the
12647 beginning of the line. */
12648 if (unchanged_p
12649 && INTEGERP (BVAR (current_buffer, selective_display))
12650 && XINT (BVAR (current_buffer, selective_display)) > 0
12651 && (BEG_UNCHANGED < start || GPT <= start))
12652 unchanged_p = 0;
12654 /* If there are overlays at the start or end of the line, these
12655 may have overlay strings with newlines in them. A change at
12656 START, for instance, may actually concern the display of such
12657 overlay strings as well, and they are displayed on different
12658 lines. So, quickly rule out this case. (For the future, it
12659 might be desirable to implement something more telling than
12660 just BEG/END_UNCHANGED.) */
12661 if (unchanged_p)
12663 if (BEG + BEG_UNCHANGED == start
12664 && overlay_touches_p (start))
12665 unchanged_p = 0;
12666 if (END_UNCHANGED == end
12667 && overlay_touches_p (Z - end))
12668 unchanged_p = 0;
12671 /* Under bidi reordering, adding or deleting a character in the
12672 beginning of a paragraph, before the first strong directional
12673 character, can change the base direction of the paragraph (unless
12674 the buffer specifies a fixed paragraph direction), which will
12675 require to redisplay the whole paragraph. It might be worthwhile
12676 to find the paragraph limits and widen the range of redisplayed
12677 lines to that, but for now just give up this optimization. */
12678 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12679 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12680 unchanged_p = 0;
12683 return unchanged_p;
12687 /* Do a frame update, taking possible shortcuts into account. This is
12688 the main external entry point for redisplay.
12690 If the last redisplay displayed an echo area message and that message
12691 is no longer requested, we clear the echo area or bring back the
12692 mini-buffer if that is in use. */
12694 void
12695 redisplay (void)
12697 redisplay_internal ();
12701 static Lisp_Object
12702 overlay_arrow_string_or_property (Lisp_Object var)
12704 Lisp_Object val;
12706 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12707 return val;
12709 return Voverlay_arrow_string;
12712 /* Return 1 if there are any overlay-arrows in current_buffer. */
12713 static int
12714 overlay_arrow_in_current_buffer_p (void)
12716 Lisp_Object vlist;
12718 for (vlist = Voverlay_arrow_variable_list;
12719 CONSP (vlist);
12720 vlist = XCDR (vlist))
12722 Lisp_Object var = XCAR (vlist);
12723 Lisp_Object val;
12725 if (!SYMBOLP (var))
12726 continue;
12727 val = find_symbol_value (var);
12728 if (MARKERP (val)
12729 && current_buffer == XMARKER (val)->buffer)
12730 return 1;
12732 return 0;
12736 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12737 has changed. */
12739 static int
12740 overlay_arrows_changed_p (void)
12742 Lisp_Object vlist;
12744 for (vlist = Voverlay_arrow_variable_list;
12745 CONSP (vlist);
12746 vlist = XCDR (vlist))
12748 Lisp_Object var = XCAR (vlist);
12749 Lisp_Object val, pstr;
12751 if (!SYMBOLP (var))
12752 continue;
12753 val = find_symbol_value (var);
12754 if (!MARKERP (val))
12755 continue;
12756 if (! EQ (COERCE_MARKER (val),
12757 Fget (var, Qlast_arrow_position))
12758 || ! (pstr = overlay_arrow_string_or_property (var),
12759 EQ (pstr, Fget (var, Qlast_arrow_string))))
12760 return 1;
12762 return 0;
12765 /* Mark overlay arrows to be updated on next redisplay. */
12767 static void
12768 update_overlay_arrows (int up_to_date)
12770 Lisp_Object vlist;
12772 for (vlist = Voverlay_arrow_variable_list;
12773 CONSP (vlist);
12774 vlist = XCDR (vlist))
12776 Lisp_Object var = XCAR (vlist);
12778 if (!SYMBOLP (var))
12779 continue;
12781 if (up_to_date > 0)
12783 Lisp_Object val = find_symbol_value (var);
12784 Fput (var, Qlast_arrow_position,
12785 COERCE_MARKER (val));
12786 Fput (var, Qlast_arrow_string,
12787 overlay_arrow_string_or_property (var));
12789 else if (up_to_date < 0
12790 || !NILP (Fget (var, Qlast_arrow_position)))
12792 Fput (var, Qlast_arrow_position, Qt);
12793 Fput (var, Qlast_arrow_string, Qt);
12799 /* Return overlay arrow string to display at row.
12800 Return integer (bitmap number) for arrow bitmap in left fringe.
12801 Return nil if no overlay arrow. */
12803 static Lisp_Object
12804 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12806 Lisp_Object vlist;
12808 for (vlist = Voverlay_arrow_variable_list;
12809 CONSP (vlist);
12810 vlist = XCDR (vlist))
12812 Lisp_Object var = XCAR (vlist);
12813 Lisp_Object val;
12815 if (!SYMBOLP (var))
12816 continue;
12818 val = find_symbol_value (var);
12820 if (MARKERP (val)
12821 && current_buffer == XMARKER (val)->buffer
12822 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12824 if (FRAME_WINDOW_P (it->f)
12825 /* FIXME: if ROW->reversed_p is set, this should test
12826 the right fringe, not the left one. */
12827 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12829 #ifdef HAVE_WINDOW_SYSTEM
12830 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12832 int fringe_bitmap;
12833 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12834 return make_number (fringe_bitmap);
12836 #endif
12837 return make_number (-1); /* Use default arrow bitmap. */
12839 return overlay_arrow_string_or_property (var);
12843 return Qnil;
12846 /* Return 1 if point moved out of or into a composition. Otherwise
12847 return 0. PREV_BUF and PREV_PT are the last point buffer and
12848 position. BUF and PT are the current point buffer and position. */
12850 static int
12851 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12852 struct buffer *buf, ptrdiff_t pt)
12854 ptrdiff_t start, end;
12855 Lisp_Object prop;
12856 Lisp_Object buffer;
12858 XSETBUFFER (buffer, buf);
12859 /* Check a composition at the last point if point moved within the
12860 same buffer. */
12861 if (prev_buf == buf)
12863 if (prev_pt == pt)
12864 /* Point didn't move. */
12865 return 0;
12867 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12868 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12869 && COMPOSITION_VALID_P (start, end, prop)
12870 && start < prev_pt && end > prev_pt)
12871 /* The last point was within the composition. Return 1 iff
12872 point moved out of the composition. */
12873 return (pt <= start || pt >= end);
12876 /* Check a composition at the current point. */
12877 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12878 && find_composition (pt, -1, &start, &end, &prop, buffer)
12879 && COMPOSITION_VALID_P (start, end, prop)
12880 && start < pt && end > pt);
12884 /* Reconsider the setting of B->clip_changed which is displayed
12885 in window W. */
12887 static void
12888 reconsider_clip_changes (struct window *w, struct buffer *b)
12890 if (b->clip_changed
12891 && w->window_end_valid
12892 && w->current_matrix->buffer == b
12893 && w->current_matrix->zv == BUF_ZV (b)
12894 && w->current_matrix->begv == BUF_BEGV (b))
12895 b->clip_changed = 0;
12897 /* If display wasn't paused, and W is not a tool bar window, see if
12898 point has been moved into or out of a composition. In that case,
12899 we set b->clip_changed to 1 to force updating the screen. If
12900 b->clip_changed has already been set to 1, we can skip this
12901 check. */
12902 if (!b->clip_changed && BUFFERP (w->contents) && w->window_end_valid)
12904 ptrdiff_t pt;
12906 if (w == XWINDOW (selected_window))
12907 pt = PT;
12908 else
12909 pt = marker_position (w->pointm);
12911 if ((w->current_matrix->buffer != XBUFFER (w->contents)
12912 || pt != w->last_point)
12913 && check_point_in_composition (w->current_matrix->buffer,
12914 w->last_point,
12915 XBUFFER (w->contents), pt))
12916 b->clip_changed = 1;
12921 #define STOP_POLLING \
12922 do { if (! polling_stopped_here) stop_polling (); \
12923 polling_stopped_here = 1; } while (0)
12925 #define RESUME_POLLING \
12926 do { if (polling_stopped_here) start_polling (); \
12927 polling_stopped_here = 0; } while (0)
12930 /* Perhaps in the future avoid recentering windows if it
12931 is not necessary; currently that causes some problems. */
12933 static void
12934 redisplay_internal (void)
12936 struct window *w = XWINDOW (selected_window);
12937 struct window *sw;
12938 struct frame *fr;
12939 int pending;
12940 int must_finish = 0;
12941 struct text_pos tlbufpos, tlendpos;
12942 int number_of_visible_frames;
12943 ptrdiff_t count, count1;
12944 struct frame *sf;
12945 int polling_stopped_here = 0;
12946 Lisp_Object tail, frame;
12948 /* Non-zero means redisplay has to consider all windows on all
12949 frames. Zero means, only selected_window is considered. */
12950 int consider_all_windows_p;
12952 /* Non-zero means redisplay has to redisplay the miniwindow. */
12953 int update_miniwindow_p = 0;
12955 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12957 /* No redisplay if running in batch mode or frame is not yet fully
12958 initialized, or redisplay is explicitly turned off by setting
12959 Vinhibit_redisplay. */
12960 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12961 || !NILP (Vinhibit_redisplay))
12962 return;
12964 /* Don't examine these until after testing Vinhibit_redisplay.
12965 When Emacs is shutting down, perhaps because its connection to
12966 X has dropped, we should not look at them at all. */
12967 fr = XFRAME (w->frame);
12968 sf = SELECTED_FRAME ();
12970 if (!fr->glyphs_initialized_p)
12971 return;
12973 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12974 if (popup_activated ())
12975 return;
12976 #endif
12978 /* I don't think this happens but let's be paranoid. */
12979 if (redisplaying_p)
12980 return;
12982 /* Record a function that clears redisplaying_p
12983 when we leave this function. */
12984 count = SPECPDL_INDEX ();
12985 record_unwind_protect (unwind_redisplay, selected_frame);
12986 redisplaying_p = 1;
12987 specbind (Qinhibit_free_realized_faces, Qnil);
12989 /* Record this function, so it appears on the profiler's backtraces. */
12990 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
12992 FOR_EACH_FRAME (tail, frame)
12993 XFRAME (frame)->already_hscrolled_p = 0;
12995 retry:
12996 /* Remember the currently selected window. */
12997 sw = w;
12999 pending = 0;
13000 reconsider_clip_changes (w, current_buffer);
13001 last_escape_glyph_frame = NULL;
13002 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13003 last_glyphless_glyph_frame = NULL;
13004 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13006 /* If new fonts have been loaded that make a glyph matrix adjustment
13007 necessary, do it. */
13008 if (fonts_changed_p)
13010 adjust_glyphs (NULL);
13011 ++windows_or_buffers_changed;
13012 fonts_changed_p = 0;
13015 /* If face_change_count is non-zero, init_iterator will free all
13016 realized faces, which includes the faces referenced from current
13017 matrices. So, we can't reuse current matrices in this case. */
13018 if (face_change_count)
13019 ++windows_or_buffers_changed;
13021 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13022 && FRAME_TTY (sf)->previous_frame != sf)
13024 /* Since frames on a single ASCII terminal share the same
13025 display area, displaying a different frame means redisplay
13026 the whole thing. */
13027 windows_or_buffers_changed++;
13028 SET_FRAME_GARBAGED (sf);
13029 #ifndef DOS_NT
13030 set_tty_color_mode (FRAME_TTY (sf), sf);
13031 #endif
13032 FRAME_TTY (sf)->previous_frame = sf;
13035 /* Set the visible flags for all frames. Do this before checking for
13036 resized or garbaged frames; they want to know if their frames are
13037 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13038 number_of_visible_frames = 0;
13040 FOR_EACH_FRAME (tail, frame)
13042 struct frame *f = XFRAME (frame);
13044 if (FRAME_VISIBLE_P (f))
13045 ++number_of_visible_frames;
13046 clear_desired_matrices (f);
13049 /* Notice any pending interrupt request to change frame size. */
13050 do_pending_window_change (1);
13052 /* do_pending_window_change could change the selected_window due to
13053 frame resizing which makes the selected window too small. */
13054 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13056 sw = w;
13057 reconsider_clip_changes (w, current_buffer);
13060 /* Clear frames marked as garbaged. */
13061 clear_garbaged_frames ();
13063 /* Build menubar and tool-bar items. */
13064 if (NILP (Vmemory_full))
13065 prepare_menu_bars ();
13067 if (windows_or_buffers_changed)
13068 update_mode_lines++;
13070 /* Detect case that we need to write or remove a star in the mode line. */
13071 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13073 w->update_mode_line = 1;
13074 if (buffer_shared_and_changed ())
13075 update_mode_lines++;
13078 /* Avoid invocation of point motion hooks by `current_column' below. */
13079 count1 = SPECPDL_INDEX ();
13080 specbind (Qinhibit_point_motion_hooks, Qt);
13082 if (mode_line_update_needed (w))
13083 w->update_mode_line = 1;
13085 unbind_to (count1, Qnil);
13087 consider_all_windows_p = (update_mode_lines
13088 || buffer_shared_and_changed ()
13089 || cursor_type_changed);
13091 /* If specs for an arrow have changed, do thorough redisplay
13092 to ensure we remove any arrow that should no longer exist. */
13093 if (overlay_arrows_changed_p ())
13094 consider_all_windows_p = windows_or_buffers_changed = 1;
13096 /* Normally the message* functions will have already displayed and
13097 updated the echo area, but the frame may have been trashed, or
13098 the update may have been preempted, so display the echo area
13099 again here. Checking message_cleared_p captures the case that
13100 the echo area should be cleared. */
13101 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13102 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13103 || (message_cleared_p
13104 && minibuf_level == 0
13105 /* If the mini-window is currently selected, this means the
13106 echo-area doesn't show through. */
13107 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13109 int window_height_changed_p = echo_area_display (0);
13111 if (message_cleared_p)
13112 update_miniwindow_p = 1;
13114 must_finish = 1;
13116 /* If we don't display the current message, don't clear the
13117 message_cleared_p flag, because, if we did, we wouldn't clear
13118 the echo area in the next redisplay which doesn't preserve
13119 the echo area. */
13120 if (!display_last_displayed_message_p)
13121 message_cleared_p = 0;
13123 if (fonts_changed_p)
13124 goto retry;
13125 else if (window_height_changed_p)
13127 consider_all_windows_p = 1;
13128 ++update_mode_lines;
13129 ++windows_or_buffers_changed;
13131 /* If window configuration was changed, frames may have been
13132 marked garbaged. Clear them or we will experience
13133 surprises wrt scrolling. */
13134 clear_garbaged_frames ();
13137 else if (EQ (selected_window, minibuf_window)
13138 && (current_buffer->clip_changed || window_outdated (w))
13139 && resize_mini_window (w, 0))
13141 /* Resized active mini-window to fit the size of what it is
13142 showing if its contents might have changed. */
13143 must_finish = 1;
13144 /* FIXME: this causes all frames to be updated, which seems unnecessary
13145 since only the current frame needs to be considered. This function
13146 needs to be rewritten with two variables, consider_all_windows and
13147 consider_all_frames. */
13148 consider_all_windows_p = 1;
13149 ++windows_or_buffers_changed;
13150 ++update_mode_lines;
13152 /* If window configuration was changed, frames may have been
13153 marked garbaged. Clear them or we will experience
13154 surprises wrt scrolling. */
13155 clear_garbaged_frames ();
13158 /* If showing the region, and mark has changed, we must redisplay
13159 the whole window. The assignment to this_line_start_pos prevents
13160 the optimization directly below this if-statement. */
13161 if (((!NILP (Vtransient_mark_mode)
13162 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13163 != (w->region_showing > 0))
13164 || (w->region_showing
13165 && w->region_showing
13166 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13167 CHARPOS (this_line_start_pos) = 0;
13169 /* Optimize the case that only the line containing the cursor in the
13170 selected window has changed. Variables starting with this_ are
13171 set in display_line and record information about the line
13172 containing the cursor. */
13173 tlbufpos = this_line_start_pos;
13174 tlendpos = this_line_end_pos;
13175 if (!consider_all_windows_p
13176 && CHARPOS (tlbufpos) > 0
13177 && !w->update_mode_line
13178 && !current_buffer->clip_changed
13179 && !current_buffer->prevent_redisplay_optimizations_p
13180 && FRAME_VISIBLE_P (XFRAME (w->frame))
13181 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13182 /* Make sure recorded data applies to current buffer, etc. */
13183 && this_line_buffer == current_buffer
13184 && current_buffer == XBUFFER (w->contents)
13185 && !w->force_start
13186 && !w->optional_new_start
13187 /* Point must be on the line that we have info recorded about. */
13188 && PT >= CHARPOS (tlbufpos)
13189 && PT <= Z - CHARPOS (tlendpos)
13190 /* All text outside that line, including its final newline,
13191 must be unchanged. */
13192 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13193 CHARPOS (tlendpos)))
13195 if (CHARPOS (tlbufpos) > BEGV
13196 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13197 && (CHARPOS (tlbufpos) == ZV
13198 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13199 /* Former continuation line has disappeared by becoming empty. */
13200 goto cancel;
13201 else if (window_outdated (w) || MINI_WINDOW_P (w))
13203 /* We have to handle the case of continuation around a
13204 wide-column character (see the comment in indent.c around
13205 line 1340).
13207 For instance, in the following case:
13209 -------- Insert --------
13210 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13211 J_I_ ==> J_I_ `^^' are cursors.
13212 ^^ ^^
13213 -------- --------
13215 As we have to redraw the line above, we cannot use this
13216 optimization. */
13218 struct it it;
13219 int line_height_before = this_line_pixel_height;
13221 /* Note that start_display will handle the case that the
13222 line starting at tlbufpos is a continuation line. */
13223 start_display (&it, w, tlbufpos);
13225 /* Implementation note: It this still necessary? */
13226 if (it.current_x != this_line_start_x)
13227 goto cancel;
13229 TRACE ((stderr, "trying display optimization 1\n"));
13230 w->cursor.vpos = -1;
13231 overlay_arrow_seen = 0;
13232 it.vpos = this_line_vpos;
13233 it.current_y = this_line_y;
13234 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13235 display_line (&it);
13237 /* If line contains point, is not continued,
13238 and ends at same distance from eob as before, we win. */
13239 if (w->cursor.vpos >= 0
13240 /* Line is not continued, otherwise this_line_start_pos
13241 would have been set to 0 in display_line. */
13242 && CHARPOS (this_line_start_pos)
13243 /* Line ends as before. */
13244 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13245 /* Line has same height as before. Otherwise other lines
13246 would have to be shifted up or down. */
13247 && this_line_pixel_height == line_height_before)
13249 /* If this is not the window's last line, we must adjust
13250 the charstarts of the lines below. */
13251 if (it.current_y < it.last_visible_y)
13253 struct glyph_row *row
13254 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13255 ptrdiff_t delta, delta_bytes;
13257 /* We used to distinguish between two cases here,
13258 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13259 when the line ends in a newline or the end of the
13260 buffer's accessible portion. But both cases did
13261 the same, so they were collapsed. */
13262 delta = (Z
13263 - CHARPOS (tlendpos)
13264 - MATRIX_ROW_START_CHARPOS (row));
13265 delta_bytes = (Z_BYTE
13266 - BYTEPOS (tlendpos)
13267 - MATRIX_ROW_START_BYTEPOS (row));
13269 increment_matrix_positions (w->current_matrix,
13270 this_line_vpos + 1,
13271 w->current_matrix->nrows,
13272 delta, delta_bytes);
13275 /* If this row displays text now but previously didn't,
13276 or vice versa, w->window_end_vpos may have to be
13277 adjusted. */
13278 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13280 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13281 wset_window_end_vpos (w, make_number (this_line_vpos));
13283 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13284 && this_line_vpos > 0)
13285 wset_window_end_vpos (w, make_number (this_line_vpos - 1));
13286 w->window_end_valid = 0;
13288 /* Update hint: No need to try to scroll in update_window. */
13289 w->desired_matrix->no_scrolling_p = 1;
13291 #ifdef GLYPH_DEBUG
13292 *w->desired_matrix->method = 0;
13293 debug_method_add (w, "optimization 1");
13294 #endif
13295 #ifdef HAVE_WINDOW_SYSTEM
13296 update_window_fringes (w, 0);
13297 #endif
13298 goto update;
13300 else
13301 goto cancel;
13303 else if (/* Cursor position hasn't changed. */
13304 PT == w->last_point
13305 /* Make sure the cursor was last displayed
13306 in this window. Otherwise we have to reposition it. */
13307 && 0 <= w->cursor.vpos
13308 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13310 if (!must_finish)
13312 do_pending_window_change (1);
13313 /* If selected_window changed, redisplay again. */
13314 if (WINDOWP (selected_window)
13315 && (w = XWINDOW (selected_window)) != sw)
13316 goto retry;
13318 /* We used to always goto end_of_redisplay here, but this
13319 isn't enough if we have a blinking cursor. */
13320 if (w->cursor_off_p == w->last_cursor_off_p)
13321 goto end_of_redisplay;
13323 goto update;
13325 /* If highlighting the region, or if the cursor is in the echo area,
13326 then we can't just move the cursor. */
13327 else if (! (!NILP (Vtransient_mark_mode)
13328 && !NILP (BVAR (current_buffer, mark_active)))
13329 && (EQ (selected_window,
13330 BVAR (current_buffer, last_selected_window))
13331 || highlight_nonselected_windows)
13332 && !w->region_showing
13333 && NILP (Vshow_trailing_whitespace)
13334 && !cursor_in_echo_area)
13336 struct it it;
13337 struct glyph_row *row;
13339 /* Skip from tlbufpos to PT and see where it is. Note that
13340 PT may be in invisible text. If so, we will end at the
13341 next visible position. */
13342 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13343 NULL, DEFAULT_FACE_ID);
13344 it.current_x = this_line_start_x;
13345 it.current_y = this_line_y;
13346 it.vpos = this_line_vpos;
13348 /* The call to move_it_to stops in front of PT, but
13349 moves over before-strings. */
13350 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13352 if (it.vpos == this_line_vpos
13353 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13354 row->enabled_p))
13356 eassert (this_line_vpos == it.vpos);
13357 eassert (this_line_y == it.current_y);
13358 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13359 #ifdef GLYPH_DEBUG
13360 *w->desired_matrix->method = 0;
13361 debug_method_add (w, "optimization 3");
13362 #endif
13363 goto update;
13365 else
13366 goto cancel;
13369 cancel:
13370 /* Text changed drastically or point moved off of line. */
13371 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13374 CHARPOS (this_line_start_pos) = 0;
13375 consider_all_windows_p |= buffer_shared_and_changed ();
13376 ++clear_face_cache_count;
13377 #ifdef HAVE_WINDOW_SYSTEM
13378 ++clear_image_cache_count;
13379 #endif
13381 /* Build desired matrices, and update the display. If
13382 consider_all_windows_p is non-zero, do it for all windows on all
13383 frames. Otherwise do it for selected_window, only. */
13385 if (consider_all_windows_p)
13387 FOR_EACH_FRAME (tail, frame)
13388 XFRAME (frame)->updated_p = 0;
13390 FOR_EACH_FRAME (tail, frame)
13392 struct frame *f = XFRAME (frame);
13394 /* We don't have to do anything for unselected terminal
13395 frames. */
13396 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13397 && !EQ (FRAME_TTY (f)->top_frame, frame))
13398 continue;
13400 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13402 /* Mark all the scroll bars to be removed; we'll redeem
13403 the ones we want when we redisplay their windows. */
13404 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13405 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13407 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13408 redisplay_windows (FRAME_ROOT_WINDOW (f));
13410 /* The X error handler may have deleted that frame. */
13411 if (!FRAME_LIVE_P (f))
13412 continue;
13414 /* Any scroll bars which redisplay_windows should have
13415 nuked should now go away. */
13416 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13417 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13419 /* If fonts changed, display again. */
13420 /* ??? rms: I suspect it is a mistake to jump all the way
13421 back to retry here. It should just retry this frame. */
13422 if (fonts_changed_p)
13423 goto retry;
13425 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13427 /* See if we have to hscroll. */
13428 if (!f->already_hscrolled_p)
13430 f->already_hscrolled_p = 1;
13431 if (hscroll_windows (f->root_window))
13432 goto retry;
13435 /* Prevent various kinds of signals during display
13436 update. stdio is not robust about handling
13437 signals, which can cause an apparent I/O
13438 error. */
13439 if (interrupt_input)
13440 unrequest_sigio ();
13441 STOP_POLLING;
13443 /* Update the display. */
13444 set_window_update_flags (XWINDOW (f->root_window), 1);
13445 pending |= update_frame (f, 0, 0);
13446 f->updated_p = 1;
13451 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13453 if (!pending)
13455 /* Do the mark_window_display_accurate after all windows have
13456 been redisplayed because this call resets flags in buffers
13457 which are needed for proper redisplay. */
13458 FOR_EACH_FRAME (tail, frame)
13460 struct frame *f = XFRAME (frame);
13461 if (f->updated_p)
13463 mark_window_display_accurate (f->root_window, 1);
13464 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13465 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13470 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13472 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13473 struct frame *mini_frame;
13475 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13476 /* Use list_of_error, not Qerror, so that
13477 we catch only errors and don't run the debugger. */
13478 internal_condition_case_1 (redisplay_window_1, selected_window,
13479 list_of_error,
13480 redisplay_window_error);
13481 if (update_miniwindow_p)
13482 internal_condition_case_1 (redisplay_window_1, mini_window,
13483 list_of_error,
13484 redisplay_window_error);
13486 /* Compare desired and current matrices, perform output. */
13488 update:
13489 /* If fonts changed, display again. */
13490 if (fonts_changed_p)
13491 goto retry;
13493 /* Prevent various kinds of signals during display update.
13494 stdio is not robust about handling signals,
13495 which can cause an apparent I/O error. */
13496 if (interrupt_input)
13497 unrequest_sigio ();
13498 STOP_POLLING;
13500 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13502 if (hscroll_windows (selected_window))
13503 goto retry;
13505 XWINDOW (selected_window)->must_be_updated_p = 1;
13506 pending = update_frame (sf, 0, 0);
13509 /* We may have called echo_area_display at the top of this
13510 function. If the echo area is on another frame, that may
13511 have put text on a frame other than the selected one, so the
13512 above call to update_frame would not have caught it. Catch
13513 it here. */
13514 mini_window = FRAME_MINIBUF_WINDOW (sf);
13515 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13517 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13519 XWINDOW (mini_window)->must_be_updated_p = 1;
13520 pending |= update_frame (mini_frame, 0, 0);
13521 if (!pending && hscroll_windows (mini_window))
13522 goto retry;
13526 /* If display was paused because of pending input, make sure we do a
13527 thorough update the next time. */
13528 if (pending)
13530 /* Prevent the optimization at the beginning of
13531 redisplay_internal that tries a single-line update of the
13532 line containing the cursor in the selected window. */
13533 CHARPOS (this_line_start_pos) = 0;
13535 /* Let the overlay arrow be updated the next time. */
13536 update_overlay_arrows (0);
13538 /* If we pause after scrolling, some rows in the current
13539 matrices of some windows are not valid. */
13540 if (!WINDOW_FULL_WIDTH_P (w)
13541 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13542 update_mode_lines = 1;
13544 else
13546 if (!consider_all_windows_p)
13548 /* This has already been done above if
13549 consider_all_windows_p is set. */
13550 mark_window_display_accurate_1 (w, 1);
13552 /* Say overlay arrows are up to date. */
13553 update_overlay_arrows (1);
13555 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13556 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13559 update_mode_lines = 0;
13560 windows_or_buffers_changed = 0;
13561 cursor_type_changed = 0;
13564 /* Start SIGIO interrupts coming again. Having them off during the
13565 code above makes it less likely one will discard output, but not
13566 impossible, since there might be stuff in the system buffer here.
13567 But it is much hairier to try to do anything about that. */
13568 if (interrupt_input)
13569 request_sigio ();
13570 RESUME_POLLING;
13572 /* If a frame has become visible which was not before, redisplay
13573 again, so that we display it. Expose events for such a frame
13574 (which it gets when becoming visible) don't call the parts of
13575 redisplay constructing glyphs, so simply exposing a frame won't
13576 display anything in this case. So, we have to display these
13577 frames here explicitly. */
13578 if (!pending)
13580 int new_count = 0;
13582 FOR_EACH_FRAME (tail, frame)
13584 int this_is_visible = 0;
13586 if (XFRAME (frame)->visible)
13587 this_is_visible = 1;
13589 if (this_is_visible)
13590 new_count++;
13593 if (new_count != number_of_visible_frames)
13594 windows_or_buffers_changed++;
13597 /* Change frame size now if a change is pending. */
13598 do_pending_window_change (1);
13600 /* If we just did a pending size change, or have additional
13601 visible frames, or selected_window changed, redisplay again. */
13602 if ((windows_or_buffers_changed && !pending)
13603 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13604 goto retry;
13606 /* Clear the face and image caches.
13608 We used to do this only if consider_all_windows_p. But the cache
13609 needs to be cleared if a timer creates images in the current
13610 buffer (e.g. the test case in Bug#6230). */
13612 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13614 clear_face_cache (0);
13615 clear_face_cache_count = 0;
13618 #ifdef HAVE_WINDOW_SYSTEM
13619 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13621 clear_image_caches (Qnil);
13622 clear_image_cache_count = 0;
13624 #endif /* HAVE_WINDOW_SYSTEM */
13626 end_of_redisplay:
13627 unbind_to (count, Qnil);
13628 RESUME_POLLING;
13632 /* Redisplay, but leave alone any recent echo area message unless
13633 another message has been requested in its place.
13635 This is useful in situations where you need to redisplay but no
13636 user action has occurred, making it inappropriate for the message
13637 area to be cleared. See tracking_off and
13638 wait_reading_process_output for examples of these situations.
13640 FROM_WHERE is an integer saying from where this function was
13641 called. This is useful for debugging. */
13643 void
13644 redisplay_preserve_echo_area (int from_where)
13646 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13648 if (!NILP (echo_area_buffer[1]))
13650 /* We have a previously displayed message, but no current
13651 message. Redisplay the previous message. */
13652 display_last_displayed_message_p = 1;
13653 redisplay_internal ();
13654 display_last_displayed_message_p = 0;
13656 else
13657 redisplay_internal ();
13659 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13660 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13661 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13665 /* Function registered with record_unwind_protect in redisplay_internal.
13666 Clear redisplaying_p. Also select the previously selected frame. */
13668 static Lisp_Object
13669 unwind_redisplay (Lisp_Object old_frame)
13671 redisplaying_p = 0;
13672 return Qnil;
13676 /* Mark the display of leaf window W as accurate or inaccurate.
13677 If ACCURATE_P is non-zero mark display of W as accurate. If
13678 ACCURATE_P is zero, arrange for W to be redisplayed the next
13679 time redisplay_internal is called. */
13681 static void
13682 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13684 struct buffer *b = XBUFFER (w->contents);
13686 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13687 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13688 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13690 if (accurate_p)
13692 b->clip_changed = 0;
13693 b->prevent_redisplay_optimizations_p = 0;
13695 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13696 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13697 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13698 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13700 w->current_matrix->buffer = b;
13701 w->current_matrix->begv = BUF_BEGV (b);
13702 w->current_matrix->zv = BUF_ZV (b);
13704 w->last_cursor = w->cursor;
13705 w->last_cursor_off_p = w->cursor_off_p;
13707 if (w == XWINDOW (selected_window))
13708 w->last_point = BUF_PT (b);
13709 else
13710 w->last_point = marker_position (w->pointm);
13712 w->window_end_valid = 1;
13713 w->update_mode_line = 0;
13718 /* Mark the display of windows in the window tree rooted at WINDOW as
13719 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13720 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13721 be redisplayed the next time redisplay_internal is called. */
13723 void
13724 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13726 struct window *w;
13728 for (; !NILP (window); window = w->next)
13730 w = XWINDOW (window);
13731 if (WINDOWP (w->contents))
13732 mark_window_display_accurate (w->contents, accurate_p);
13733 else
13734 mark_window_display_accurate_1 (w, accurate_p);
13737 if (accurate_p)
13738 update_overlay_arrows (1);
13739 else
13740 /* Force a thorough redisplay the next time by setting
13741 last_arrow_position and last_arrow_string to t, which is
13742 unequal to any useful value of Voverlay_arrow_... */
13743 update_overlay_arrows (-1);
13747 /* Return value in display table DP (Lisp_Char_Table *) for character
13748 C. Since a display table doesn't have any parent, we don't have to
13749 follow parent. Do not call this function directly but use the
13750 macro DISP_CHAR_VECTOR. */
13752 Lisp_Object
13753 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13755 Lisp_Object val;
13757 if (ASCII_CHAR_P (c))
13759 val = dp->ascii;
13760 if (SUB_CHAR_TABLE_P (val))
13761 val = XSUB_CHAR_TABLE (val)->contents[c];
13763 else
13765 Lisp_Object table;
13767 XSETCHAR_TABLE (table, dp);
13768 val = char_table_ref (table, c);
13770 if (NILP (val))
13771 val = dp->defalt;
13772 return val;
13777 /***********************************************************************
13778 Window Redisplay
13779 ***********************************************************************/
13781 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13783 static void
13784 redisplay_windows (Lisp_Object window)
13786 while (!NILP (window))
13788 struct window *w = XWINDOW (window);
13790 if (WINDOWP (w->contents))
13791 redisplay_windows (w->contents);
13792 else if (BUFFERP (w->contents))
13794 displayed_buffer = XBUFFER (w->contents);
13795 /* Use list_of_error, not Qerror, so that
13796 we catch only errors and don't run the debugger. */
13797 internal_condition_case_1 (redisplay_window_0, window,
13798 list_of_error,
13799 redisplay_window_error);
13802 window = w->next;
13806 static Lisp_Object
13807 redisplay_window_error (Lisp_Object ignore)
13809 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13810 return Qnil;
13813 static Lisp_Object
13814 redisplay_window_0 (Lisp_Object window)
13816 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13817 redisplay_window (window, 0);
13818 return Qnil;
13821 static Lisp_Object
13822 redisplay_window_1 (Lisp_Object window)
13824 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13825 redisplay_window (window, 1);
13826 return Qnil;
13830 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13831 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13832 which positions recorded in ROW differ from current buffer
13833 positions.
13835 Return 0 if cursor is not on this row, 1 otherwise. */
13837 static int
13838 set_cursor_from_row (struct window *w, struct glyph_row *row,
13839 struct glyph_matrix *matrix,
13840 ptrdiff_t delta, ptrdiff_t delta_bytes,
13841 int dy, int dvpos)
13843 struct glyph *glyph = row->glyphs[TEXT_AREA];
13844 struct glyph *end = glyph + row->used[TEXT_AREA];
13845 struct glyph *cursor = NULL;
13846 /* The last known character position in row. */
13847 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13848 int x = row->x;
13849 ptrdiff_t pt_old = PT - delta;
13850 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13851 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13852 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13853 /* A glyph beyond the edge of TEXT_AREA which we should never
13854 touch. */
13855 struct glyph *glyphs_end = end;
13856 /* Non-zero means we've found a match for cursor position, but that
13857 glyph has the avoid_cursor_p flag set. */
13858 int match_with_avoid_cursor = 0;
13859 /* Non-zero means we've seen at least one glyph that came from a
13860 display string. */
13861 int string_seen = 0;
13862 /* Largest and smallest buffer positions seen so far during scan of
13863 glyph row. */
13864 ptrdiff_t bpos_max = pos_before;
13865 ptrdiff_t bpos_min = pos_after;
13866 /* Last buffer position covered by an overlay string with an integer
13867 `cursor' property. */
13868 ptrdiff_t bpos_covered = 0;
13869 /* Non-zero means the display string on which to display the cursor
13870 comes from a text property, not from an overlay. */
13871 int string_from_text_prop = 0;
13873 /* Don't even try doing anything if called for a mode-line or
13874 header-line row, since the rest of the code isn't prepared to
13875 deal with such calamities. */
13876 eassert (!row->mode_line_p);
13877 if (row->mode_line_p)
13878 return 0;
13880 /* Skip over glyphs not having an object at the start and the end of
13881 the row. These are special glyphs like truncation marks on
13882 terminal frames. */
13883 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13885 if (!row->reversed_p)
13887 while (glyph < end
13888 && INTEGERP (glyph->object)
13889 && glyph->charpos < 0)
13891 x += glyph->pixel_width;
13892 ++glyph;
13894 while (end > glyph
13895 && INTEGERP ((end - 1)->object)
13896 /* CHARPOS is zero for blanks and stretch glyphs
13897 inserted by extend_face_to_end_of_line. */
13898 && (end - 1)->charpos <= 0)
13899 --end;
13900 glyph_before = glyph - 1;
13901 glyph_after = end;
13903 else
13905 struct glyph *g;
13907 /* If the glyph row is reversed, we need to process it from back
13908 to front, so swap the edge pointers. */
13909 glyphs_end = end = glyph - 1;
13910 glyph += row->used[TEXT_AREA] - 1;
13912 while (glyph > end + 1
13913 && INTEGERP (glyph->object)
13914 && glyph->charpos < 0)
13916 --glyph;
13917 x -= glyph->pixel_width;
13919 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13920 --glyph;
13921 /* By default, in reversed rows we put the cursor on the
13922 rightmost (first in the reading order) glyph. */
13923 for (g = end + 1; g < glyph; g++)
13924 x += g->pixel_width;
13925 while (end < glyph
13926 && INTEGERP ((end + 1)->object)
13927 && (end + 1)->charpos <= 0)
13928 ++end;
13929 glyph_before = glyph + 1;
13930 glyph_after = end;
13933 else if (row->reversed_p)
13935 /* In R2L rows that don't display text, put the cursor on the
13936 rightmost glyph. Case in point: an empty last line that is
13937 part of an R2L paragraph. */
13938 cursor = end - 1;
13939 /* Avoid placing the cursor on the last glyph of the row, where
13940 on terminal frames we hold the vertical border between
13941 adjacent windows. */
13942 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13943 && !WINDOW_RIGHTMOST_P (w)
13944 && cursor == row->glyphs[LAST_AREA] - 1)
13945 cursor--;
13946 x = -1; /* will be computed below, at label compute_x */
13949 /* Step 1: Try to find the glyph whose character position
13950 corresponds to point. If that's not possible, find 2 glyphs
13951 whose character positions are the closest to point, one before
13952 point, the other after it. */
13953 if (!row->reversed_p)
13954 while (/* not marched to end of glyph row */
13955 glyph < end
13956 /* glyph was not inserted by redisplay for internal purposes */
13957 && !INTEGERP (glyph->object))
13959 if (BUFFERP (glyph->object))
13961 ptrdiff_t dpos = glyph->charpos - pt_old;
13963 if (glyph->charpos > bpos_max)
13964 bpos_max = glyph->charpos;
13965 if (glyph->charpos < bpos_min)
13966 bpos_min = glyph->charpos;
13967 if (!glyph->avoid_cursor_p)
13969 /* If we hit point, we've found the glyph on which to
13970 display the cursor. */
13971 if (dpos == 0)
13973 match_with_avoid_cursor = 0;
13974 break;
13976 /* See if we've found a better approximation to
13977 POS_BEFORE or to POS_AFTER. */
13978 if (0 > dpos && dpos > pos_before - pt_old)
13980 pos_before = glyph->charpos;
13981 glyph_before = glyph;
13983 else if (0 < dpos && dpos < pos_after - pt_old)
13985 pos_after = glyph->charpos;
13986 glyph_after = glyph;
13989 else if (dpos == 0)
13990 match_with_avoid_cursor = 1;
13992 else if (STRINGP (glyph->object))
13994 Lisp_Object chprop;
13995 ptrdiff_t glyph_pos = glyph->charpos;
13997 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13998 glyph->object);
13999 if (!NILP (chprop))
14001 /* If the string came from a `display' text property,
14002 look up the buffer position of that property and
14003 use that position to update bpos_max, as if we
14004 actually saw such a position in one of the row's
14005 glyphs. This helps with supporting integer values
14006 of `cursor' property on the display string in
14007 situations where most or all of the row's buffer
14008 text is completely covered by display properties,
14009 so that no glyph with valid buffer positions is
14010 ever seen in the row. */
14011 ptrdiff_t prop_pos =
14012 string_buffer_position_lim (glyph->object, pos_before,
14013 pos_after, 0);
14015 if (prop_pos >= pos_before)
14016 bpos_max = prop_pos - 1;
14018 if (INTEGERP (chprop))
14020 bpos_covered = bpos_max + XINT (chprop);
14021 /* If the `cursor' property covers buffer positions up
14022 to and including point, we should display cursor on
14023 this glyph. Note that, if a `cursor' property on one
14024 of the string's characters has an integer value, we
14025 will break out of the loop below _before_ we get to
14026 the position match above. IOW, integer values of
14027 the `cursor' property override the "exact match for
14028 point" strategy of positioning the cursor. */
14029 /* Implementation note: bpos_max == pt_old when, e.g.,
14030 we are in an empty line, where bpos_max is set to
14031 MATRIX_ROW_START_CHARPOS, see above. */
14032 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14034 cursor = glyph;
14035 break;
14039 string_seen = 1;
14041 x += glyph->pixel_width;
14042 ++glyph;
14044 else if (glyph > end) /* row is reversed */
14045 while (!INTEGERP (glyph->object))
14047 if (BUFFERP (glyph->object))
14049 ptrdiff_t dpos = glyph->charpos - pt_old;
14051 if (glyph->charpos > bpos_max)
14052 bpos_max = glyph->charpos;
14053 if (glyph->charpos < bpos_min)
14054 bpos_min = glyph->charpos;
14055 if (!glyph->avoid_cursor_p)
14057 if (dpos == 0)
14059 match_with_avoid_cursor = 0;
14060 break;
14062 if (0 > dpos && dpos > pos_before - pt_old)
14064 pos_before = glyph->charpos;
14065 glyph_before = glyph;
14067 else if (0 < dpos && dpos < pos_after - pt_old)
14069 pos_after = glyph->charpos;
14070 glyph_after = glyph;
14073 else if (dpos == 0)
14074 match_with_avoid_cursor = 1;
14076 else if (STRINGP (glyph->object))
14078 Lisp_Object chprop;
14079 ptrdiff_t glyph_pos = glyph->charpos;
14081 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14082 glyph->object);
14083 if (!NILP (chprop))
14085 ptrdiff_t prop_pos =
14086 string_buffer_position_lim (glyph->object, pos_before,
14087 pos_after, 0);
14089 if (prop_pos >= pos_before)
14090 bpos_max = prop_pos - 1;
14092 if (INTEGERP (chprop))
14094 bpos_covered = bpos_max + XINT (chprop);
14095 /* If the `cursor' property covers buffer positions up
14096 to and including point, we should display cursor on
14097 this glyph. */
14098 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14100 cursor = glyph;
14101 break;
14104 string_seen = 1;
14106 --glyph;
14107 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14109 x--; /* can't use any pixel_width */
14110 break;
14112 x -= glyph->pixel_width;
14115 /* Step 2: If we didn't find an exact match for point, we need to
14116 look for a proper place to put the cursor among glyphs between
14117 GLYPH_BEFORE and GLYPH_AFTER. */
14118 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14119 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14120 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14122 /* An empty line has a single glyph whose OBJECT is zero and
14123 whose CHARPOS is the position of a newline on that line.
14124 Note that on a TTY, there are more glyphs after that, which
14125 were produced by extend_face_to_end_of_line, but their
14126 CHARPOS is zero or negative. */
14127 int empty_line_p =
14128 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14129 && INTEGERP (glyph->object) && glyph->charpos > 0
14130 /* On a TTY, continued and truncated rows also have a glyph at
14131 their end whose OBJECT is zero and whose CHARPOS is
14132 positive (the continuation and truncation glyphs), but such
14133 rows are obviously not "empty". */
14134 && !(row->continued_p || row->truncated_on_right_p);
14136 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14138 ptrdiff_t ellipsis_pos;
14140 /* Scan back over the ellipsis glyphs. */
14141 if (!row->reversed_p)
14143 ellipsis_pos = (glyph - 1)->charpos;
14144 while (glyph > row->glyphs[TEXT_AREA]
14145 && (glyph - 1)->charpos == ellipsis_pos)
14146 glyph--, x -= glyph->pixel_width;
14147 /* That loop always goes one position too far, including
14148 the glyph before the ellipsis. So scan forward over
14149 that one. */
14150 x += glyph->pixel_width;
14151 glyph++;
14153 else /* row is reversed */
14155 ellipsis_pos = (glyph + 1)->charpos;
14156 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14157 && (glyph + 1)->charpos == ellipsis_pos)
14158 glyph++, x += glyph->pixel_width;
14159 x -= glyph->pixel_width;
14160 glyph--;
14163 else if (match_with_avoid_cursor)
14165 cursor = glyph_after;
14166 x = -1;
14168 else if (string_seen)
14170 int incr = row->reversed_p ? -1 : +1;
14172 /* Need to find the glyph that came out of a string which is
14173 present at point. That glyph is somewhere between
14174 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14175 positioned between POS_BEFORE and POS_AFTER in the
14176 buffer. */
14177 struct glyph *start, *stop;
14178 ptrdiff_t pos = pos_before;
14180 x = -1;
14182 /* If the row ends in a newline from a display string,
14183 reordering could have moved the glyphs belonging to the
14184 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14185 in this case we extend the search to the last glyph in
14186 the row that was not inserted by redisplay. */
14187 if (row->ends_in_newline_from_string_p)
14189 glyph_after = end;
14190 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14193 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14194 correspond to POS_BEFORE and POS_AFTER, respectively. We
14195 need START and STOP in the order that corresponds to the
14196 row's direction as given by its reversed_p flag. If the
14197 directionality of characters between POS_BEFORE and
14198 POS_AFTER is the opposite of the row's base direction,
14199 these characters will have been reordered for display,
14200 and we need to reverse START and STOP. */
14201 if (!row->reversed_p)
14203 start = min (glyph_before, glyph_after);
14204 stop = max (glyph_before, glyph_after);
14206 else
14208 start = max (glyph_before, glyph_after);
14209 stop = min (glyph_before, glyph_after);
14211 for (glyph = start + incr;
14212 row->reversed_p ? glyph > stop : glyph < stop; )
14215 /* Any glyphs that come from the buffer are here because
14216 of bidi reordering. Skip them, and only pay
14217 attention to glyphs that came from some string. */
14218 if (STRINGP (glyph->object))
14220 Lisp_Object str;
14221 ptrdiff_t tem;
14222 /* If the display property covers the newline, we
14223 need to search for it one position farther. */
14224 ptrdiff_t lim = pos_after
14225 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14227 string_from_text_prop = 0;
14228 str = glyph->object;
14229 tem = string_buffer_position_lim (str, pos, lim, 0);
14230 if (tem == 0 /* from overlay */
14231 || pos <= tem)
14233 /* If the string from which this glyph came is
14234 found in the buffer at point, or at position
14235 that is closer to point than pos_after, then
14236 we've found the glyph we've been looking for.
14237 If it comes from an overlay (tem == 0), and
14238 it has the `cursor' property on one of its
14239 glyphs, record that glyph as a candidate for
14240 displaying the cursor. (As in the
14241 unidirectional version, we will display the
14242 cursor on the last candidate we find.) */
14243 if (tem == 0
14244 || tem == pt_old
14245 || (tem - pt_old > 0 && tem < pos_after))
14247 /* The glyphs from this string could have
14248 been reordered. Find the one with the
14249 smallest string position. Or there could
14250 be a character in the string with the
14251 `cursor' property, which means display
14252 cursor on that character's glyph. */
14253 ptrdiff_t strpos = glyph->charpos;
14255 if (tem)
14257 cursor = glyph;
14258 string_from_text_prop = 1;
14260 for ( ;
14261 (row->reversed_p ? glyph > stop : glyph < stop)
14262 && EQ (glyph->object, str);
14263 glyph += incr)
14265 Lisp_Object cprop;
14266 ptrdiff_t gpos = glyph->charpos;
14268 cprop = Fget_char_property (make_number (gpos),
14269 Qcursor,
14270 glyph->object);
14271 if (!NILP (cprop))
14273 cursor = glyph;
14274 break;
14276 if (tem && glyph->charpos < strpos)
14278 strpos = glyph->charpos;
14279 cursor = glyph;
14283 if (tem == pt_old
14284 || (tem - pt_old > 0 && tem < pos_after))
14285 goto compute_x;
14287 if (tem)
14288 pos = tem + 1; /* don't find previous instances */
14290 /* This string is not what we want; skip all of the
14291 glyphs that came from it. */
14292 while ((row->reversed_p ? glyph > stop : glyph < stop)
14293 && EQ (glyph->object, str))
14294 glyph += incr;
14296 else
14297 glyph += incr;
14300 /* If we reached the end of the line, and END was from a string,
14301 the cursor is not on this line. */
14302 if (cursor == NULL
14303 && (row->reversed_p ? glyph <= end : glyph >= end)
14304 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14305 && STRINGP (end->object)
14306 && row->continued_p)
14307 return 0;
14309 /* A truncated row may not include PT among its character positions.
14310 Setting the cursor inside the scroll margin will trigger
14311 recalculation of hscroll in hscroll_window_tree. But if a
14312 display string covers point, defer to the string-handling
14313 code below to figure this out. */
14314 else if (row->truncated_on_left_p && pt_old < bpos_min)
14316 cursor = glyph_before;
14317 x = -1;
14319 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14320 /* Zero-width characters produce no glyphs. */
14321 || (!empty_line_p
14322 && (row->reversed_p
14323 ? glyph_after > glyphs_end
14324 : glyph_after < glyphs_end)))
14326 cursor = glyph_after;
14327 x = -1;
14331 compute_x:
14332 if (cursor != NULL)
14333 glyph = cursor;
14334 else if (glyph == glyphs_end
14335 && pos_before == pos_after
14336 && STRINGP ((row->reversed_p
14337 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14338 : row->glyphs[TEXT_AREA])->object))
14340 /* If all the glyphs of this row came from strings, put the
14341 cursor on the first glyph of the row. This avoids having the
14342 cursor outside of the text area in this very rare and hard
14343 use case. */
14344 glyph =
14345 row->reversed_p
14346 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14347 : row->glyphs[TEXT_AREA];
14349 if (x < 0)
14351 struct glyph *g;
14353 /* Need to compute x that corresponds to GLYPH. */
14354 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14356 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14357 emacs_abort ();
14358 x += g->pixel_width;
14362 /* ROW could be part of a continued line, which, under bidi
14363 reordering, might have other rows whose start and end charpos
14364 occlude point. Only set w->cursor if we found a better
14365 approximation to the cursor position than we have from previously
14366 examined candidate rows belonging to the same continued line. */
14367 if (/* we already have a candidate row */
14368 w->cursor.vpos >= 0
14369 /* that candidate is not the row we are processing */
14370 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14371 /* Make sure cursor.vpos specifies a row whose start and end
14372 charpos occlude point, and it is valid candidate for being a
14373 cursor-row. This is because some callers of this function
14374 leave cursor.vpos at the row where the cursor was displayed
14375 during the last redisplay cycle. */
14376 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14377 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14378 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14380 struct glyph *g1 =
14381 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14383 /* Don't consider glyphs that are outside TEXT_AREA. */
14384 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14385 return 0;
14386 /* Keep the candidate whose buffer position is the closest to
14387 point or has the `cursor' property. */
14388 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14389 w->cursor.hpos >= 0
14390 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14391 && ((BUFFERP (g1->object)
14392 && (g1->charpos == pt_old /* an exact match always wins */
14393 || (BUFFERP (glyph->object)
14394 && eabs (g1->charpos - pt_old)
14395 < eabs (glyph->charpos - pt_old))))
14396 /* previous candidate is a glyph from a string that has
14397 a non-nil `cursor' property */
14398 || (STRINGP (g1->object)
14399 && (!NILP (Fget_char_property (make_number (g1->charpos),
14400 Qcursor, g1->object))
14401 /* previous candidate is from the same display
14402 string as this one, and the display string
14403 came from a text property */
14404 || (EQ (g1->object, glyph->object)
14405 && string_from_text_prop)
14406 /* this candidate is from newline and its
14407 position is not an exact match */
14408 || (INTEGERP (glyph->object)
14409 && glyph->charpos != pt_old)))))
14410 return 0;
14411 /* If this candidate gives an exact match, use that. */
14412 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14413 /* If this candidate is a glyph created for the
14414 terminating newline of a line, and point is on that
14415 newline, it wins because it's an exact match. */
14416 || (!row->continued_p
14417 && INTEGERP (glyph->object)
14418 && glyph->charpos == 0
14419 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14420 /* Otherwise, keep the candidate that comes from a row
14421 spanning less buffer positions. This may win when one or
14422 both candidate positions are on glyphs that came from
14423 display strings, for which we cannot compare buffer
14424 positions. */
14425 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14426 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14427 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14428 return 0;
14430 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14431 w->cursor.x = x;
14432 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14433 w->cursor.y = row->y + dy;
14435 if (w == XWINDOW (selected_window))
14437 if (!row->continued_p
14438 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14439 && row->x == 0)
14441 this_line_buffer = XBUFFER (w->contents);
14443 CHARPOS (this_line_start_pos)
14444 = MATRIX_ROW_START_CHARPOS (row) + delta;
14445 BYTEPOS (this_line_start_pos)
14446 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14448 CHARPOS (this_line_end_pos)
14449 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14450 BYTEPOS (this_line_end_pos)
14451 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14453 this_line_y = w->cursor.y;
14454 this_line_pixel_height = row->height;
14455 this_line_vpos = w->cursor.vpos;
14456 this_line_start_x = row->x;
14458 else
14459 CHARPOS (this_line_start_pos) = 0;
14462 return 1;
14466 /* Run window scroll functions, if any, for WINDOW with new window
14467 start STARTP. Sets the window start of WINDOW to that position.
14469 We assume that the window's buffer is really current. */
14471 static struct text_pos
14472 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14474 struct window *w = XWINDOW (window);
14475 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14477 if (current_buffer != XBUFFER (w->contents))
14478 emacs_abort ();
14480 if (!NILP (Vwindow_scroll_functions))
14482 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14483 make_number (CHARPOS (startp)));
14484 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14485 /* In case the hook functions switch buffers. */
14486 set_buffer_internal (XBUFFER (w->contents));
14489 return startp;
14493 /* Make sure the line containing the cursor is fully visible.
14494 A value of 1 means there is nothing to be done.
14495 (Either the line is fully visible, or it cannot be made so,
14496 or we cannot tell.)
14498 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14499 is higher than window.
14501 A value of 0 means the caller should do scrolling
14502 as if point had gone off the screen. */
14504 static int
14505 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14507 struct glyph_matrix *matrix;
14508 struct glyph_row *row;
14509 int window_height;
14511 if (!make_cursor_line_fully_visible_p)
14512 return 1;
14514 /* It's not always possible to find the cursor, e.g, when a window
14515 is full of overlay strings. Don't do anything in that case. */
14516 if (w->cursor.vpos < 0)
14517 return 1;
14519 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14520 row = MATRIX_ROW (matrix, w->cursor.vpos);
14522 /* If the cursor row is not partially visible, there's nothing to do. */
14523 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14524 return 1;
14526 /* If the row the cursor is in is taller than the window's height,
14527 it's not clear what to do, so do nothing. */
14528 window_height = window_box_height (w);
14529 if (row->height >= window_height)
14531 if (!force_p || MINI_WINDOW_P (w)
14532 || w->vscroll || w->cursor.vpos == 0)
14533 return 1;
14535 return 0;
14539 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14540 non-zero means only WINDOW is redisplayed in redisplay_internal.
14541 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14542 in redisplay_window to bring a partially visible line into view in
14543 the case that only the cursor has moved.
14545 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14546 last screen line's vertical height extends past the end of the screen.
14548 Value is
14550 1 if scrolling succeeded
14552 0 if scrolling didn't find point.
14554 -1 if new fonts have been loaded so that we must interrupt
14555 redisplay, adjust glyph matrices, and try again. */
14557 enum
14559 SCROLLING_SUCCESS,
14560 SCROLLING_FAILED,
14561 SCROLLING_NEED_LARGER_MATRICES
14564 /* If scroll-conservatively is more than this, never recenter.
14566 If you change this, don't forget to update the doc string of
14567 `scroll-conservatively' and the Emacs manual. */
14568 #define SCROLL_LIMIT 100
14570 static int
14571 try_scrolling (Lisp_Object window, int just_this_one_p,
14572 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14573 int temp_scroll_step, int last_line_misfit)
14575 struct window *w = XWINDOW (window);
14576 struct frame *f = XFRAME (w->frame);
14577 struct text_pos pos, startp;
14578 struct it it;
14579 int this_scroll_margin, scroll_max, rc, height;
14580 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14581 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14582 Lisp_Object aggressive;
14583 /* We will never try scrolling more than this number of lines. */
14584 int scroll_limit = SCROLL_LIMIT;
14585 int frame_line_height = default_line_pixel_height (w);
14586 int window_total_lines
14587 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14589 #ifdef GLYPH_DEBUG
14590 debug_method_add (w, "try_scrolling");
14591 #endif
14593 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14595 /* Compute scroll margin height in pixels. We scroll when point is
14596 within this distance from the top or bottom of the window. */
14597 if (scroll_margin > 0)
14598 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14599 * frame_line_height;
14600 else
14601 this_scroll_margin = 0;
14603 /* Force arg_scroll_conservatively to have a reasonable value, to
14604 avoid scrolling too far away with slow move_it_* functions. Note
14605 that the user can supply scroll-conservatively equal to
14606 `most-positive-fixnum', which can be larger than INT_MAX. */
14607 if (arg_scroll_conservatively > scroll_limit)
14609 arg_scroll_conservatively = scroll_limit + 1;
14610 scroll_max = scroll_limit * frame_line_height;
14612 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14613 /* Compute how much we should try to scroll maximally to bring
14614 point into view. */
14615 scroll_max = (max (scroll_step,
14616 max (arg_scroll_conservatively, temp_scroll_step))
14617 * frame_line_height);
14618 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14619 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14620 /* We're trying to scroll because of aggressive scrolling but no
14621 scroll_step is set. Choose an arbitrary one. */
14622 scroll_max = 10 * frame_line_height;
14623 else
14624 scroll_max = 0;
14626 too_near_end:
14628 /* Decide whether to scroll down. */
14629 if (PT > CHARPOS (startp))
14631 int scroll_margin_y;
14633 /* Compute the pixel ypos of the scroll margin, then move IT to
14634 either that ypos or PT, whichever comes first. */
14635 start_display (&it, w, startp);
14636 scroll_margin_y = it.last_visible_y - this_scroll_margin
14637 - frame_line_height * extra_scroll_margin_lines;
14638 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14639 (MOVE_TO_POS | MOVE_TO_Y));
14641 if (PT > CHARPOS (it.current.pos))
14643 int y0 = line_bottom_y (&it);
14644 /* Compute how many pixels below window bottom to stop searching
14645 for PT. This avoids costly search for PT that is far away if
14646 the user limited scrolling by a small number of lines, but
14647 always finds PT if scroll_conservatively is set to a large
14648 number, such as most-positive-fixnum. */
14649 int slack = max (scroll_max, 10 * frame_line_height);
14650 int y_to_move = it.last_visible_y + slack;
14652 /* Compute the distance from the scroll margin to PT or to
14653 the scroll limit, whichever comes first. This should
14654 include the height of the cursor line, to make that line
14655 fully visible. */
14656 move_it_to (&it, PT, -1, y_to_move,
14657 -1, MOVE_TO_POS | MOVE_TO_Y);
14658 dy = line_bottom_y (&it) - y0;
14660 if (dy > scroll_max)
14661 return SCROLLING_FAILED;
14663 if (dy > 0)
14664 scroll_down_p = 1;
14668 if (scroll_down_p)
14670 /* Point is in or below the bottom scroll margin, so move the
14671 window start down. If scrolling conservatively, move it just
14672 enough down to make point visible. If scroll_step is set,
14673 move it down by scroll_step. */
14674 if (arg_scroll_conservatively)
14675 amount_to_scroll
14676 = min (max (dy, frame_line_height),
14677 frame_line_height * arg_scroll_conservatively);
14678 else if (scroll_step || temp_scroll_step)
14679 amount_to_scroll = scroll_max;
14680 else
14682 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14683 height = WINDOW_BOX_TEXT_HEIGHT (w);
14684 if (NUMBERP (aggressive))
14686 double float_amount = XFLOATINT (aggressive) * height;
14687 int aggressive_scroll = float_amount;
14688 if (aggressive_scroll == 0 && float_amount > 0)
14689 aggressive_scroll = 1;
14690 /* Don't let point enter the scroll margin near top of
14691 the window. This could happen if the value of
14692 scroll_up_aggressively is too large and there are
14693 non-zero margins, because scroll_up_aggressively
14694 means put point that fraction of window height
14695 _from_the_bottom_margin_. */
14696 if (aggressive_scroll + 2*this_scroll_margin > height)
14697 aggressive_scroll = height - 2*this_scroll_margin;
14698 amount_to_scroll = dy + aggressive_scroll;
14702 if (amount_to_scroll <= 0)
14703 return SCROLLING_FAILED;
14705 start_display (&it, w, startp);
14706 if (arg_scroll_conservatively <= scroll_limit)
14707 move_it_vertically (&it, amount_to_scroll);
14708 else
14710 /* Extra precision for users who set scroll-conservatively
14711 to a large number: make sure the amount we scroll
14712 the window start is never less than amount_to_scroll,
14713 which was computed as distance from window bottom to
14714 point. This matters when lines at window top and lines
14715 below window bottom have different height. */
14716 struct it it1;
14717 void *it1data = NULL;
14718 /* We use a temporary it1 because line_bottom_y can modify
14719 its argument, if it moves one line down; see there. */
14720 int start_y;
14722 SAVE_IT (it1, it, it1data);
14723 start_y = line_bottom_y (&it1);
14724 do {
14725 RESTORE_IT (&it, &it, it1data);
14726 move_it_by_lines (&it, 1);
14727 SAVE_IT (it1, it, it1data);
14728 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14731 /* If STARTP is unchanged, move it down another screen line. */
14732 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14733 move_it_by_lines (&it, 1);
14734 startp = it.current.pos;
14736 else
14738 struct text_pos scroll_margin_pos = startp;
14739 int y_offset = 0;
14741 /* See if point is inside the scroll margin at the top of the
14742 window. */
14743 if (this_scroll_margin)
14745 int y_start;
14747 start_display (&it, w, startp);
14748 y_start = it.current_y;
14749 move_it_vertically (&it, this_scroll_margin);
14750 scroll_margin_pos = it.current.pos;
14751 /* If we didn't move enough before hitting ZV, request
14752 additional amount of scroll, to move point out of the
14753 scroll margin. */
14754 if (IT_CHARPOS (it) == ZV
14755 && it.current_y - y_start < this_scroll_margin)
14756 y_offset = this_scroll_margin - (it.current_y - y_start);
14759 if (PT < CHARPOS (scroll_margin_pos))
14761 /* Point is in the scroll margin at the top of the window or
14762 above what is displayed in the window. */
14763 int y0, y_to_move;
14765 /* Compute the vertical distance from PT to the scroll
14766 margin position. Move as far as scroll_max allows, or
14767 one screenful, or 10 screen lines, whichever is largest.
14768 Give up if distance is greater than scroll_max or if we
14769 didn't reach the scroll margin position. */
14770 SET_TEXT_POS (pos, PT, PT_BYTE);
14771 start_display (&it, w, pos);
14772 y0 = it.current_y;
14773 y_to_move = max (it.last_visible_y,
14774 max (scroll_max, 10 * frame_line_height));
14775 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14776 y_to_move, -1,
14777 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14778 dy = it.current_y - y0;
14779 if (dy > scroll_max
14780 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14781 return SCROLLING_FAILED;
14783 /* Additional scroll for when ZV was too close to point. */
14784 dy += y_offset;
14786 /* Compute new window start. */
14787 start_display (&it, w, startp);
14789 if (arg_scroll_conservatively)
14790 amount_to_scroll = max (dy, frame_line_height *
14791 max (scroll_step, temp_scroll_step));
14792 else if (scroll_step || temp_scroll_step)
14793 amount_to_scroll = scroll_max;
14794 else
14796 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14797 height = WINDOW_BOX_TEXT_HEIGHT (w);
14798 if (NUMBERP (aggressive))
14800 double float_amount = XFLOATINT (aggressive) * height;
14801 int aggressive_scroll = float_amount;
14802 if (aggressive_scroll == 0 && float_amount > 0)
14803 aggressive_scroll = 1;
14804 /* Don't let point enter the scroll margin near
14805 bottom of the window, if the value of
14806 scroll_down_aggressively happens to be too
14807 large. */
14808 if (aggressive_scroll + 2*this_scroll_margin > height)
14809 aggressive_scroll = height - 2*this_scroll_margin;
14810 amount_to_scroll = dy + aggressive_scroll;
14814 if (amount_to_scroll <= 0)
14815 return SCROLLING_FAILED;
14817 move_it_vertically_backward (&it, amount_to_scroll);
14818 startp = it.current.pos;
14822 /* Run window scroll functions. */
14823 startp = run_window_scroll_functions (window, startp);
14825 /* Display the window. Give up if new fonts are loaded, or if point
14826 doesn't appear. */
14827 if (!try_window (window, startp, 0))
14828 rc = SCROLLING_NEED_LARGER_MATRICES;
14829 else if (w->cursor.vpos < 0)
14831 clear_glyph_matrix (w->desired_matrix);
14832 rc = SCROLLING_FAILED;
14834 else
14836 /* Maybe forget recorded base line for line number display. */
14837 if (!just_this_one_p
14838 || current_buffer->clip_changed
14839 || BEG_UNCHANGED < CHARPOS (startp))
14840 w->base_line_number = 0;
14842 /* If cursor ends up on a partially visible line,
14843 treat that as being off the bottom of the screen. */
14844 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14845 /* It's possible that the cursor is on the first line of the
14846 buffer, which is partially obscured due to a vscroll
14847 (Bug#7537). In that case, avoid looping forever . */
14848 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14850 clear_glyph_matrix (w->desired_matrix);
14851 ++extra_scroll_margin_lines;
14852 goto too_near_end;
14854 rc = SCROLLING_SUCCESS;
14857 return rc;
14861 /* Compute a suitable window start for window W if display of W starts
14862 on a continuation line. Value is non-zero if a new window start
14863 was computed.
14865 The new window start will be computed, based on W's width, starting
14866 from the start of the continued line. It is the start of the
14867 screen line with the minimum distance from the old start W->start. */
14869 static int
14870 compute_window_start_on_continuation_line (struct window *w)
14872 struct text_pos pos, start_pos;
14873 int window_start_changed_p = 0;
14875 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14877 /* If window start is on a continuation line... Window start may be
14878 < BEGV in case there's invisible text at the start of the
14879 buffer (M-x rmail, for example). */
14880 if (CHARPOS (start_pos) > BEGV
14881 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14883 struct it it;
14884 struct glyph_row *row;
14886 /* Handle the case that the window start is out of range. */
14887 if (CHARPOS (start_pos) < BEGV)
14888 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14889 else if (CHARPOS (start_pos) > ZV)
14890 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14892 /* Find the start of the continued line. This should be fast
14893 because find_newline is fast (newline cache). */
14894 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14895 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14896 row, DEFAULT_FACE_ID);
14897 reseat_at_previous_visible_line_start (&it);
14899 /* If the line start is "too far" away from the window start,
14900 say it takes too much time to compute a new window start. */
14901 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14902 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14904 int min_distance, distance;
14906 /* Move forward by display lines to find the new window
14907 start. If window width was enlarged, the new start can
14908 be expected to be > the old start. If window width was
14909 decreased, the new window start will be < the old start.
14910 So, we're looking for the display line start with the
14911 minimum distance from the old window start. */
14912 pos = it.current.pos;
14913 min_distance = INFINITY;
14914 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14915 distance < min_distance)
14917 min_distance = distance;
14918 pos = it.current.pos;
14919 move_it_by_lines (&it, 1);
14922 /* Set the window start there. */
14923 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14924 window_start_changed_p = 1;
14928 return window_start_changed_p;
14932 /* Try cursor movement in case text has not changed in window WINDOW,
14933 with window start STARTP. Value is
14935 CURSOR_MOVEMENT_SUCCESS if successful
14937 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14939 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14940 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14941 we want to scroll as if scroll-step were set to 1. See the code.
14943 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14944 which case we have to abort this redisplay, and adjust matrices
14945 first. */
14947 enum
14949 CURSOR_MOVEMENT_SUCCESS,
14950 CURSOR_MOVEMENT_CANNOT_BE_USED,
14951 CURSOR_MOVEMENT_MUST_SCROLL,
14952 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14955 static int
14956 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14958 struct window *w = XWINDOW (window);
14959 struct frame *f = XFRAME (w->frame);
14960 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14962 #ifdef GLYPH_DEBUG
14963 if (inhibit_try_cursor_movement)
14964 return rc;
14965 #endif
14967 /* Previously, there was a check for Lisp integer in the
14968 if-statement below. Now, this field is converted to
14969 ptrdiff_t, thus zero means invalid position in a buffer. */
14970 eassert (w->last_point > 0);
14972 /* Handle case where text has not changed, only point, and it has
14973 not moved off the frame. */
14974 if (/* Point may be in this window. */
14975 PT >= CHARPOS (startp)
14976 /* Selective display hasn't changed. */
14977 && !current_buffer->clip_changed
14978 /* Function force-mode-line-update is used to force a thorough
14979 redisplay. It sets either windows_or_buffers_changed or
14980 update_mode_lines. So don't take a shortcut here for these
14981 cases. */
14982 && !update_mode_lines
14983 && !windows_or_buffers_changed
14984 && !cursor_type_changed
14985 /* Can't use this case if highlighting a region. When a
14986 region exists, cursor movement has to do more than just
14987 set the cursor. */
14988 && markpos_of_region () < 0
14989 && !w->region_showing
14990 && NILP (Vshow_trailing_whitespace)
14991 /* This code is not used for mini-buffer for the sake of the case
14992 of redisplaying to replace an echo area message; since in
14993 that case the mini-buffer contents per se are usually
14994 unchanged. This code is of no real use in the mini-buffer
14995 since the handling of this_line_start_pos, etc., in redisplay
14996 handles the same cases. */
14997 && !EQ (window, minibuf_window)
14998 /* When splitting windows or for new windows, it happens that
14999 redisplay is called with a nil window_end_vpos or one being
15000 larger than the window. This should really be fixed in
15001 window.c. I don't have this on my list, now, so we do
15002 approximately the same as the old redisplay code. --gerd. */
15003 && INTEGERP (w->window_end_vpos)
15004 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
15005 && (FRAME_WINDOW_P (f)
15006 || !overlay_arrow_in_current_buffer_p ()))
15008 int this_scroll_margin, top_scroll_margin;
15009 struct glyph_row *row = NULL;
15010 int frame_line_height = default_line_pixel_height (w);
15011 int window_total_lines
15012 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15014 #ifdef GLYPH_DEBUG
15015 debug_method_add (w, "cursor movement");
15016 #endif
15018 /* Scroll if point within this distance from the top or bottom
15019 of the window. This is a pixel value. */
15020 if (scroll_margin > 0)
15022 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15023 this_scroll_margin *= frame_line_height;
15025 else
15026 this_scroll_margin = 0;
15028 top_scroll_margin = this_scroll_margin;
15029 if (WINDOW_WANTS_HEADER_LINE_P (w))
15030 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15032 /* Start with the row the cursor was displayed during the last
15033 not paused redisplay. Give up if that row is not valid. */
15034 if (w->last_cursor.vpos < 0
15035 || w->last_cursor.vpos >= w->current_matrix->nrows)
15036 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15037 else
15039 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15040 if (row->mode_line_p)
15041 ++row;
15042 if (!row->enabled_p)
15043 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15046 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15048 int scroll_p = 0, must_scroll = 0;
15049 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15051 if (PT > w->last_point)
15053 /* Point has moved forward. */
15054 while (MATRIX_ROW_END_CHARPOS (row) < PT
15055 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15057 eassert (row->enabled_p);
15058 ++row;
15061 /* If the end position of a row equals the start
15062 position of the next row, and PT is at that position,
15063 we would rather display cursor in the next line. */
15064 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15065 && MATRIX_ROW_END_CHARPOS (row) == PT
15066 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15067 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15068 && !cursor_row_p (row))
15069 ++row;
15071 /* If within the scroll margin, scroll. Note that
15072 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15073 the next line would be drawn, and that
15074 this_scroll_margin can be zero. */
15075 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15076 || PT > MATRIX_ROW_END_CHARPOS (row)
15077 /* Line is completely visible last line in window
15078 and PT is to be set in the next line. */
15079 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15080 && PT == MATRIX_ROW_END_CHARPOS (row)
15081 && !row->ends_at_zv_p
15082 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15083 scroll_p = 1;
15085 else if (PT < w->last_point)
15087 /* Cursor has to be moved backward. Note that PT >=
15088 CHARPOS (startp) because of the outer if-statement. */
15089 while (!row->mode_line_p
15090 && (MATRIX_ROW_START_CHARPOS (row) > PT
15091 || (MATRIX_ROW_START_CHARPOS (row) == PT
15092 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15093 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15094 row > w->current_matrix->rows
15095 && (row-1)->ends_in_newline_from_string_p))))
15096 && (row->y > top_scroll_margin
15097 || CHARPOS (startp) == BEGV))
15099 eassert (row->enabled_p);
15100 --row;
15103 /* Consider the following case: Window starts at BEGV,
15104 there is invisible, intangible text at BEGV, so that
15105 display starts at some point START > BEGV. It can
15106 happen that we are called with PT somewhere between
15107 BEGV and START. Try to handle that case. */
15108 if (row < w->current_matrix->rows
15109 || row->mode_line_p)
15111 row = w->current_matrix->rows;
15112 if (row->mode_line_p)
15113 ++row;
15116 /* Due to newlines in overlay strings, we may have to
15117 skip forward over overlay strings. */
15118 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15119 && MATRIX_ROW_END_CHARPOS (row) == PT
15120 && !cursor_row_p (row))
15121 ++row;
15123 /* If within the scroll margin, scroll. */
15124 if (row->y < top_scroll_margin
15125 && CHARPOS (startp) != BEGV)
15126 scroll_p = 1;
15128 else
15130 /* Cursor did not move. So don't scroll even if cursor line
15131 is partially visible, as it was so before. */
15132 rc = CURSOR_MOVEMENT_SUCCESS;
15135 if (PT < MATRIX_ROW_START_CHARPOS (row)
15136 || PT > MATRIX_ROW_END_CHARPOS (row))
15138 /* if PT is not in the glyph row, give up. */
15139 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15140 must_scroll = 1;
15142 else if (rc != CURSOR_MOVEMENT_SUCCESS
15143 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15145 struct glyph_row *row1;
15147 /* If rows are bidi-reordered and point moved, back up
15148 until we find a row that does not belong to a
15149 continuation line. This is because we must consider
15150 all rows of a continued line as candidates for the
15151 new cursor positioning, since row start and end
15152 positions change non-linearly with vertical position
15153 in such rows. */
15154 /* FIXME: Revisit this when glyph ``spilling'' in
15155 continuation lines' rows is implemented for
15156 bidi-reordered rows. */
15157 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15158 MATRIX_ROW_CONTINUATION_LINE_P (row);
15159 --row)
15161 /* If we hit the beginning of the displayed portion
15162 without finding the first row of a continued
15163 line, give up. */
15164 if (row <= row1)
15166 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15167 break;
15169 eassert (row->enabled_p);
15172 if (must_scroll)
15174 else if (rc != CURSOR_MOVEMENT_SUCCESS
15175 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15176 /* Make sure this isn't a header line by any chance, since
15177 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15178 && !row->mode_line_p
15179 && make_cursor_line_fully_visible_p)
15181 if (PT == MATRIX_ROW_END_CHARPOS (row)
15182 && !row->ends_at_zv_p
15183 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15184 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15185 else if (row->height > window_box_height (w))
15187 /* If we end up in a partially visible line, let's
15188 make it fully visible, except when it's taller
15189 than the window, in which case we can't do much
15190 about it. */
15191 *scroll_step = 1;
15192 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15194 else
15196 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15197 if (!cursor_row_fully_visible_p (w, 0, 1))
15198 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15199 else
15200 rc = CURSOR_MOVEMENT_SUCCESS;
15203 else if (scroll_p)
15204 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15205 else if (rc != CURSOR_MOVEMENT_SUCCESS
15206 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15208 /* With bidi-reordered rows, there could be more than
15209 one candidate row whose start and end positions
15210 occlude point. We need to let set_cursor_from_row
15211 find the best candidate. */
15212 /* FIXME: Revisit this when glyph ``spilling'' in
15213 continuation lines' rows is implemented for
15214 bidi-reordered rows. */
15215 int rv = 0;
15219 int at_zv_p = 0, exact_match_p = 0;
15221 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15222 && PT <= MATRIX_ROW_END_CHARPOS (row)
15223 && cursor_row_p (row))
15224 rv |= set_cursor_from_row (w, row, w->current_matrix,
15225 0, 0, 0, 0);
15226 /* As soon as we've found the exact match for point,
15227 or the first suitable row whose ends_at_zv_p flag
15228 is set, we are done. */
15229 at_zv_p =
15230 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15231 if (rv && !at_zv_p
15232 && w->cursor.hpos >= 0
15233 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15234 w->cursor.vpos))
15236 struct glyph_row *candidate =
15237 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15238 struct glyph *g =
15239 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15240 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15242 exact_match_p =
15243 (BUFFERP (g->object) && g->charpos == PT)
15244 || (INTEGERP (g->object)
15245 && (g->charpos == PT
15246 || (g->charpos == 0 && endpos - 1 == PT)));
15248 if (rv && (at_zv_p || exact_match_p))
15250 rc = CURSOR_MOVEMENT_SUCCESS;
15251 break;
15253 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15254 break;
15255 ++row;
15257 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15258 || row->continued_p)
15259 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15260 || (MATRIX_ROW_START_CHARPOS (row) == PT
15261 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15262 /* If we didn't find any candidate rows, or exited the
15263 loop before all the candidates were examined, signal
15264 to the caller that this method failed. */
15265 if (rc != CURSOR_MOVEMENT_SUCCESS
15266 && !(rv
15267 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15268 && !row->continued_p))
15269 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15270 else if (rv)
15271 rc = CURSOR_MOVEMENT_SUCCESS;
15273 else
15277 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15279 rc = CURSOR_MOVEMENT_SUCCESS;
15280 break;
15282 ++row;
15284 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15285 && MATRIX_ROW_START_CHARPOS (row) == PT
15286 && cursor_row_p (row));
15291 return rc;
15294 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15295 static
15296 #endif
15297 void
15298 set_vertical_scroll_bar (struct window *w)
15300 ptrdiff_t start, end, whole;
15302 /* Calculate the start and end positions for the current window.
15303 At some point, it would be nice to choose between scrollbars
15304 which reflect the whole buffer size, with special markers
15305 indicating narrowing, and scrollbars which reflect only the
15306 visible region.
15308 Note that mini-buffers sometimes aren't displaying any text. */
15309 if (!MINI_WINDOW_P (w)
15310 || (w == XWINDOW (minibuf_window)
15311 && NILP (echo_area_buffer[0])))
15313 struct buffer *buf = XBUFFER (w->contents);
15314 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15315 start = marker_position (w->start) - BUF_BEGV (buf);
15316 /* I don't think this is guaranteed to be right. For the
15317 moment, we'll pretend it is. */
15318 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15320 if (end < start)
15321 end = start;
15322 if (whole < (end - start))
15323 whole = end - start;
15325 else
15326 start = end = whole = 0;
15328 /* Indicate what this scroll bar ought to be displaying now. */
15329 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15330 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15331 (w, end - start, whole, start);
15335 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15336 selected_window is redisplayed.
15338 We can return without actually redisplaying the window if
15339 fonts_changed_p. In that case, redisplay_internal will
15340 retry. */
15342 static void
15343 redisplay_window (Lisp_Object window, int just_this_one_p)
15345 struct window *w = XWINDOW (window);
15346 struct frame *f = XFRAME (w->frame);
15347 struct buffer *buffer = XBUFFER (w->contents);
15348 struct buffer *old = current_buffer;
15349 struct text_pos lpoint, opoint, startp;
15350 int update_mode_line;
15351 int tem;
15352 struct it it;
15353 /* Record it now because it's overwritten. */
15354 int current_matrix_up_to_date_p = 0;
15355 int used_current_matrix_p = 0;
15356 /* This is less strict than current_matrix_up_to_date_p.
15357 It indicates that the buffer contents and narrowing are unchanged. */
15358 int buffer_unchanged_p = 0;
15359 int temp_scroll_step = 0;
15360 ptrdiff_t count = SPECPDL_INDEX ();
15361 int rc;
15362 int centering_position = -1;
15363 int last_line_misfit = 0;
15364 ptrdiff_t beg_unchanged, end_unchanged;
15365 int frame_line_height;
15367 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15368 opoint = lpoint;
15370 #ifdef GLYPH_DEBUG
15371 *w->desired_matrix->method = 0;
15372 #endif
15374 /* Make sure that both W's markers are valid. */
15375 eassert (XMARKER (w->start)->buffer == buffer);
15376 eassert (XMARKER (w->pointm)->buffer == buffer);
15378 restart:
15379 reconsider_clip_changes (w, buffer);
15380 frame_line_height = default_line_pixel_height (w);
15382 /* Has the mode line to be updated? */
15383 update_mode_line = (w->update_mode_line
15384 || update_mode_lines
15385 || buffer->clip_changed
15386 || buffer->prevent_redisplay_optimizations_p);
15388 if (MINI_WINDOW_P (w))
15390 if (w == XWINDOW (echo_area_window)
15391 && !NILP (echo_area_buffer[0]))
15393 if (update_mode_line)
15394 /* We may have to update a tty frame's menu bar or a
15395 tool-bar. Example `M-x C-h C-h C-g'. */
15396 goto finish_menu_bars;
15397 else
15398 /* We've already displayed the echo area glyphs in this window. */
15399 goto finish_scroll_bars;
15401 else if ((w != XWINDOW (minibuf_window)
15402 || minibuf_level == 0)
15403 /* When buffer is nonempty, redisplay window normally. */
15404 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15405 /* Quail displays non-mini buffers in minibuffer window.
15406 In that case, redisplay the window normally. */
15407 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15409 /* W is a mini-buffer window, but it's not active, so clear
15410 it. */
15411 int yb = window_text_bottom_y (w);
15412 struct glyph_row *row;
15413 int y;
15415 for (y = 0, row = w->desired_matrix->rows;
15416 y < yb;
15417 y += row->height, ++row)
15418 blank_row (w, row, y);
15419 goto finish_scroll_bars;
15422 clear_glyph_matrix (w->desired_matrix);
15425 /* Otherwise set up data on this window; select its buffer and point
15426 value. */
15427 /* Really select the buffer, for the sake of buffer-local
15428 variables. */
15429 set_buffer_internal_1 (XBUFFER (w->contents));
15431 current_matrix_up_to_date_p
15432 = (w->window_end_valid
15433 && !current_buffer->clip_changed
15434 && !current_buffer->prevent_redisplay_optimizations_p
15435 && !window_outdated (w));
15437 /* Run the window-bottom-change-functions
15438 if it is possible that the text on the screen has changed
15439 (either due to modification of the text, or any other reason). */
15440 if (!current_matrix_up_to_date_p
15441 && !NILP (Vwindow_text_change_functions))
15443 safe_run_hooks (Qwindow_text_change_functions);
15444 goto restart;
15447 beg_unchanged = BEG_UNCHANGED;
15448 end_unchanged = END_UNCHANGED;
15450 SET_TEXT_POS (opoint, PT, PT_BYTE);
15452 specbind (Qinhibit_point_motion_hooks, Qt);
15454 buffer_unchanged_p
15455 = (w->window_end_valid
15456 && !current_buffer->clip_changed
15457 && !window_outdated (w));
15459 /* When windows_or_buffers_changed is non-zero, we can't rely on
15460 the window end being valid, so set it to nil there. */
15461 if (windows_or_buffers_changed)
15463 /* If window starts on a continuation line, maybe adjust the
15464 window start in case the window's width changed. */
15465 if (XMARKER (w->start)->buffer == current_buffer)
15466 compute_window_start_on_continuation_line (w);
15468 w->window_end_valid = 0;
15471 /* Some sanity checks. */
15472 CHECK_WINDOW_END (w);
15473 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15474 emacs_abort ();
15475 if (BYTEPOS (opoint) < CHARPOS (opoint))
15476 emacs_abort ();
15478 if (mode_line_update_needed (w))
15479 update_mode_line = 1;
15481 /* Point refers normally to the selected window. For any other
15482 window, set up appropriate value. */
15483 if (!EQ (window, selected_window))
15485 ptrdiff_t new_pt = marker_position (w->pointm);
15486 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15487 if (new_pt < BEGV)
15489 new_pt = BEGV;
15490 new_pt_byte = BEGV_BYTE;
15491 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15493 else if (new_pt > (ZV - 1))
15495 new_pt = ZV;
15496 new_pt_byte = ZV_BYTE;
15497 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15500 /* We don't use SET_PT so that the point-motion hooks don't run. */
15501 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15504 /* If any of the character widths specified in the display table
15505 have changed, invalidate the width run cache. It's true that
15506 this may be a bit late to catch such changes, but the rest of
15507 redisplay goes (non-fatally) haywire when the display table is
15508 changed, so why should we worry about doing any better? */
15509 if (current_buffer->width_run_cache)
15511 struct Lisp_Char_Table *disptab = buffer_display_table ();
15513 if (! disptab_matches_widthtab
15514 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15516 invalidate_region_cache (current_buffer,
15517 current_buffer->width_run_cache,
15518 BEG, Z);
15519 recompute_width_table (current_buffer, disptab);
15523 /* If window-start is screwed up, choose a new one. */
15524 if (XMARKER (w->start)->buffer != current_buffer)
15525 goto recenter;
15527 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15529 /* If someone specified a new starting point but did not insist,
15530 check whether it can be used. */
15531 if (w->optional_new_start
15532 && CHARPOS (startp) >= BEGV
15533 && CHARPOS (startp) <= ZV)
15535 w->optional_new_start = 0;
15536 start_display (&it, w, startp);
15537 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15538 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15539 if (IT_CHARPOS (it) == PT)
15540 w->force_start = 1;
15541 /* IT may overshoot PT if text at PT is invisible. */
15542 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15543 w->force_start = 1;
15546 force_start:
15548 /* Handle case where place to start displaying has been specified,
15549 unless the specified location is outside the accessible range. */
15550 if (w->force_start || w->frozen_window_start_p)
15552 /* We set this later on if we have to adjust point. */
15553 int new_vpos = -1;
15555 w->force_start = 0;
15556 w->vscroll = 0;
15557 w->window_end_valid = 0;
15559 /* Forget any recorded base line for line number display. */
15560 if (!buffer_unchanged_p)
15561 w->base_line_number = 0;
15563 /* Redisplay the mode line. Select the buffer properly for that.
15564 Also, run the hook window-scroll-functions
15565 because we have scrolled. */
15566 /* Note, we do this after clearing force_start because
15567 if there's an error, it is better to forget about force_start
15568 than to get into an infinite loop calling the hook functions
15569 and having them get more errors. */
15570 if (!update_mode_line
15571 || ! NILP (Vwindow_scroll_functions))
15573 update_mode_line = 1;
15574 w->update_mode_line = 1;
15575 startp = run_window_scroll_functions (window, startp);
15578 w->last_modified = 0;
15579 w->last_overlay_modified = 0;
15580 if (CHARPOS (startp) < BEGV)
15581 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15582 else if (CHARPOS (startp) > ZV)
15583 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15585 /* Redisplay, then check if cursor has been set during the
15586 redisplay. Give up if new fonts were loaded. */
15587 /* We used to issue a CHECK_MARGINS argument to try_window here,
15588 but this causes scrolling to fail when point begins inside
15589 the scroll margin (bug#148) -- cyd */
15590 if (!try_window (window, startp, 0))
15592 w->force_start = 1;
15593 clear_glyph_matrix (w->desired_matrix);
15594 goto need_larger_matrices;
15597 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15599 /* If point does not appear, try to move point so it does
15600 appear. The desired matrix has been built above, so we
15601 can use it here. */
15602 new_vpos = window_box_height (w) / 2;
15605 if (!cursor_row_fully_visible_p (w, 0, 0))
15607 /* Point does appear, but on a line partly visible at end of window.
15608 Move it back to a fully-visible line. */
15609 new_vpos = window_box_height (w);
15611 else if (w->cursor.vpos >=0)
15613 /* Some people insist on not letting point enter the scroll
15614 margin, even though this part handles windows that didn't
15615 scroll at all. */
15616 int window_total_lines
15617 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15618 int margin = min (scroll_margin, window_total_lines / 4);
15619 int pixel_margin = margin * frame_line_height;
15620 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15622 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15623 below, which finds the row to move point to, advances by
15624 the Y coordinate of the _next_ row, see the definition of
15625 MATRIX_ROW_BOTTOM_Y. */
15626 if (w->cursor.vpos < margin + header_line)
15627 new_vpos
15628 = pixel_margin + (header_line
15629 ? CURRENT_HEADER_LINE_HEIGHT (w)
15630 : 0) + frame_line_height;
15631 else
15633 int window_height = window_box_height (w);
15635 if (header_line)
15636 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15637 if (w->cursor.y >= window_height - pixel_margin)
15638 new_vpos = window_height - pixel_margin;
15642 /* If we need to move point for either of the above reasons,
15643 now actually do it. */
15644 if (new_vpos >= 0)
15646 struct glyph_row *row;
15648 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15649 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15650 ++row;
15652 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15653 MATRIX_ROW_START_BYTEPOS (row));
15655 if (w != XWINDOW (selected_window))
15656 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15657 else if (current_buffer == old)
15658 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15660 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15662 /* If we are highlighting the region, then we just changed
15663 the region, so redisplay to show it. */
15664 if (markpos_of_region () >= 0)
15666 clear_glyph_matrix (w->desired_matrix);
15667 if (!try_window (window, startp, 0))
15668 goto need_larger_matrices;
15672 #ifdef GLYPH_DEBUG
15673 debug_method_add (w, "forced window start");
15674 #endif
15675 goto done;
15678 /* Handle case where text has not changed, only point, and it has
15679 not moved off the frame, and we are not retrying after hscroll.
15680 (current_matrix_up_to_date_p is nonzero when retrying.) */
15681 if (current_matrix_up_to_date_p
15682 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15683 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15685 switch (rc)
15687 case CURSOR_MOVEMENT_SUCCESS:
15688 used_current_matrix_p = 1;
15689 goto done;
15691 case CURSOR_MOVEMENT_MUST_SCROLL:
15692 goto try_to_scroll;
15694 default:
15695 emacs_abort ();
15698 /* If current starting point was originally the beginning of a line
15699 but no longer is, find a new starting point. */
15700 else if (w->start_at_line_beg
15701 && !(CHARPOS (startp) <= BEGV
15702 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15704 #ifdef GLYPH_DEBUG
15705 debug_method_add (w, "recenter 1");
15706 #endif
15707 goto recenter;
15710 /* Try scrolling with try_window_id. Value is > 0 if update has
15711 been done, it is -1 if we know that the same window start will
15712 not work. It is 0 if unsuccessful for some other reason. */
15713 else if ((tem = try_window_id (w)) != 0)
15715 #ifdef GLYPH_DEBUG
15716 debug_method_add (w, "try_window_id %d", tem);
15717 #endif
15719 if (fonts_changed_p)
15720 goto need_larger_matrices;
15721 if (tem > 0)
15722 goto done;
15724 /* Otherwise try_window_id has returned -1 which means that we
15725 don't want the alternative below this comment to execute. */
15727 else if (CHARPOS (startp) >= BEGV
15728 && CHARPOS (startp) <= ZV
15729 && PT >= CHARPOS (startp)
15730 && (CHARPOS (startp) < ZV
15731 /* Avoid starting at end of buffer. */
15732 || CHARPOS (startp) == BEGV
15733 || !window_outdated (w)))
15735 int d1, d2, d3, d4, d5, d6;
15737 /* If first window line is a continuation line, and window start
15738 is inside the modified region, but the first change is before
15739 current window start, we must select a new window start.
15741 However, if this is the result of a down-mouse event (e.g. by
15742 extending the mouse-drag-overlay), we don't want to select a
15743 new window start, since that would change the position under
15744 the mouse, resulting in an unwanted mouse-movement rather
15745 than a simple mouse-click. */
15746 if (!w->start_at_line_beg
15747 && NILP (do_mouse_tracking)
15748 && CHARPOS (startp) > BEGV
15749 && CHARPOS (startp) > BEG + beg_unchanged
15750 && CHARPOS (startp) <= Z - end_unchanged
15751 /* Even if w->start_at_line_beg is nil, a new window may
15752 start at a line_beg, since that's how set_buffer_window
15753 sets it. So, we need to check the return value of
15754 compute_window_start_on_continuation_line. (See also
15755 bug#197). */
15756 && XMARKER (w->start)->buffer == current_buffer
15757 && compute_window_start_on_continuation_line (w)
15758 /* It doesn't make sense to force the window start like we
15759 do at label force_start if it is already known that point
15760 will not be visible in the resulting window, because
15761 doing so will move point from its correct position
15762 instead of scrolling the window to bring point into view.
15763 See bug#9324. */
15764 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15766 w->force_start = 1;
15767 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15768 goto force_start;
15771 #ifdef GLYPH_DEBUG
15772 debug_method_add (w, "same window start");
15773 #endif
15775 /* Try to redisplay starting at same place as before.
15776 If point has not moved off frame, accept the results. */
15777 if (!current_matrix_up_to_date_p
15778 /* Don't use try_window_reusing_current_matrix in this case
15779 because a window scroll function can have changed the
15780 buffer. */
15781 || !NILP (Vwindow_scroll_functions)
15782 || MINI_WINDOW_P (w)
15783 || !(used_current_matrix_p
15784 = try_window_reusing_current_matrix (w)))
15786 IF_DEBUG (debug_method_add (w, "1"));
15787 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15788 /* -1 means we need to scroll.
15789 0 means we need new matrices, but fonts_changed_p
15790 is set in that case, so we will detect it below. */
15791 goto try_to_scroll;
15794 if (fonts_changed_p)
15795 goto need_larger_matrices;
15797 if (w->cursor.vpos >= 0)
15799 if (!just_this_one_p
15800 || current_buffer->clip_changed
15801 || BEG_UNCHANGED < CHARPOS (startp))
15802 /* Forget any recorded base line for line number display. */
15803 w->base_line_number = 0;
15805 if (!cursor_row_fully_visible_p (w, 1, 0))
15807 clear_glyph_matrix (w->desired_matrix);
15808 last_line_misfit = 1;
15810 /* Drop through and scroll. */
15811 else
15812 goto done;
15814 else
15815 clear_glyph_matrix (w->desired_matrix);
15818 try_to_scroll:
15820 w->last_modified = 0;
15821 w->last_overlay_modified = 0;
15823 /* Redisplay the mode line. Select the buffer properly for that. */
15824 if (!update_mode_line)
15826 update_mode_line = 1;
15827 w->update_mode_line = 1;
15830 /* Try to scroll by specified few lines. */
15831 if ((scroll_conservatively
15832 || emacs_scroll_step
15833 || temp_scroll_step
15834 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15835 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15836 && CHARPOS (startp) >= BEGV
15837 && CHARPOS (startp) <= ZV)
15839 /* The function returns -1 if new fonts were loaded, 1 if
15840 successful, 0 if not successful. */
15841 int ss = try_scrolling (window, just_this_one_p,
15842 scroll_conservatively,
15843 emacs_scroll_step,
15844 temp_scroll_step, last_line_misfit);
15845 switch (ss)
15847 case SCROLLING_SUCCESS:
15848 goto done;
15850 case SCROLLING_NEED_LARGER_MATRICES:
15851 goto need_larger_matrices;
15853 case SCROLLING_FAILED:
15854 break;
15856 default:
15857 emacs_abort ();
15861 /* Finally, just choose a place to start which positions point
15862 according to user preferences. */
15864 recenter:
15866 #ifdef GLYPH_DEBUG
15867 debug_method_add (w, "recenter");
15868 #endif
15870 /* Forget any previously recorded base line for line number display. */
15871 if (!buffer_unchanged_p)
15872 w->base_line_number = 0;
15874 /* Determine the window start relative to point. */
15875 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15876 it.current_y = it.last_visible_y;
15877 if (centering_position < 0)
15879 int window_total_lines
15880 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15881 int margin =
15882 scroll_margin > 0
15883 ? min (scroll_margin, window_total_lines / 4)
15884 : 0;
15885 ptrdiff_t margin_pos = CHARPOS (startp);
15886 Lisp_Object aggressive;
15887 int scrolling_up;
15889 /* If there is a scroll margin at the top of the window, find
15890 its character position. */
15891 if (margin
15892 /* Cannot call start_display if startp is not in the
15893 accessible region of the buffer. This can happen when we
15894 have just switched to a different buffer and/or changed
15895 its restriction. In that case, startp is initialized to
15896 the character position 1 (BEGV) because we did not yet
15897 have chance to display the buffer even once. */
15898 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15900 struct it it1;
15901 void *it1data = NULL;
15903 SAVE_IT (it1, it, it1data);
15904 start_display (&it1, w, startp);
15905 move_it_vertically (&it1, margin * frame_line_height);
15906 margin_pos = IT_CHARPOS (it1);
15907 RESTORE_IT (&it, &it, it1data);
15909 scrolling_up = PT > margin_pos;
15910 aggressive =
15911 scrolling_up
15912 ? BVAR (current_buffer, scroll_up_aggressively)
15913 : BVAR (current_buffer, scroll_down_aggressively);
15915 if (!MINI_WINDOW_P (w)
15916 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15918 int pt_offset = 0;
15920 /* Setting scroll-conservatively overrides
15921 scroll-*-aggressively. */
15922 if (!scroll_conservatively && NUMBERP (aggressive))
15924 double float_amount = XFLOATINT (aggressive);
15926 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15927 if (pt_offset == 0 && float_amount > 0)
15928 pt_offset = 1;
15929 if (pt_offset && margin > 0)
15930 margin -= 1;
15932 /* Compute how much to move the window start backward from
15933 point so that point will be displayed where the user
15934 wants it. */
15935 if (scrolling_up)
15937 centering_position = it.last_visible_y;
15938 if (pt_offset)
15939 centering_position -= pt_offset;
15940 centering_position -=
15941 frame_line_height * (1 + margin + (last_line_misfit != 0))
15942 + WINDOW_HEADER_LINE_HEIGHT (w);
15943 /* Don't let point enter the scroll margin near top of
15944 the window. */
15945 if (centering_position < margin * frame_line_height)
15946 centering_position = margin * frame_line_height;
15948 else
15949 centering_position = margin * frame_line_height + pt_offset;
15951 else
15952 /* Set the window start half the height of the window backward
15953 from point. */
15954 centering_position = window_box_height (w) / 2;
15956 move_it_vertically_backward (&it, centering_position);
15958 eassert (IT_CHARPOS (it) >= BEGV);
15960 /* The function move_it_vertically_backward may move over more
15961 than the specified y-distance. If it->w is small, e.g. a
15962 mini-buffer window, we may end up in front of the window's
15963 display area. Start displaying at the start of the line
15964 containing PT in this case. */
15965 if (it.current_y <= 0)
15967 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15968 move_it_vertically_backward (&it, 0);
15969 it.current_y = 0;
15972 it.current_x = it.hpos = 0;
15974 /* Set the window start position here explicitly, to avoid an
15975 infinite loop in case the functions in window-scroll-functions
15976 get errors. */
15977 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15979 /* Run scroll hooks. */
15980 startp = run_window_scroll_functions (window, it.current.pos);
15982 /* Redisplay the window. */
15983 if (!current_matrix_up_to_date_p
15984 || windows_or_buffers_changed
15985 || cursor_type_changed
15986 /* Don't use try_window_reusing_current_matrix in this case
15987 because it can have changed the buffer. */
15988 || !NILP (Vwindow_scroll_functions)
15989 || !just_this_one_p
15990 || MINI_WINDOW_P (w)
15991 || !(used_current_matrix_p
15992 = try_window_reusing_current_matrix (w)))
15993 try_window (window, startp, 0);
15995 /* If new fonts have been loaded (due to fontsets), give up. We
15996 have to start a new redisplay since we need to re-adjust glyph
15997 matrices. */
15998 if (fonts_changed_p)
15999 goto need_larger_matrices;
16001 /* If cursor did not appear assume that the middle of the window is
16002 in the first line of the window. Do it again with the next line.
16003 (Imagine a window of height 100, displaying two lines of height
16004 60. Moving back 50 from it->last_visible_y will end in the first
16005 line.) */
16006 if (w->cursor.vpos < 0)
16008 if (w->window_end_valid && PT >= Z - XFASTINT (w->window_end_pos))
16010 clear_glyph_matrix (w->desired_matrix);
16011 move_it_by_lines (&it, 1);
16012 try_window (window, it.current.pos, 0);
16014 else if (PT < IT_CHARPOS (it))
16016 clear_glyph_matrix (w->desired_matrix);
16017 move_it_by_lines (&it, -1);
16018 try_window (window, it.current.pos, 0);
16020 else
16022 /* Not much we can do about it. */
16026 /* Consider the following case: Window starts at BEGV, there is
16027 invisible, intangible text at BEGV, so that display starts at
16028 some point START > BEGV. It can happen that we are called with
16029 PT somewhere between BEGV and START. Try to handle that case. */
16030 if (w->cursor.vpos < 0)
16032 struct glyph_row *row = w->current_matrix->rows;
16033 if (row->mode_line_p)
16034 ++row;
16035 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16038 if (!cursor_row_fully_visible_p (w, 0, 0))
16040 /* If vscroll is enabled, disable it and try again. */
16041 if (w->vscroll)
16043 w->vscroll = 0;
16044 clear_glyph_matrix (w->desired_matrix);
16045 goto recenter;
16048 /* Users who set scroll-conservatively to a large number want
16049 point just above/below the scroll margin. If we ended up
16050 with point's row partially visible, move the window start to
16051 make that row fully visible and out of the margin. */
16052 if (scroll_conservatively > SCROLL_LIMIT)
16054 int window_total_lines
16055 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16056 int margin =
16057 scroll_margin > 0
16058 ? min (scroll_margin, window_total_lines / 4)
16059 : 0;
16060 int move_down = w->cursor.vpos >= window_total_lines / 2;
16062 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16063 clear_glyph_matrix (w->desired_matrix);
16064 if (1 == try_window (window, it.current.pos,
16065 TRY_WINDOW_CHECK_MARGINS))
16066 goto done;
16069 /* If centering point failed to make the whole line visible,
16070 put point at the top instead. That has to make the whole line
16071 visible, if it can be done. */
16072 if (centering_position == 0)
16073 goto done;
16075 clear_glyph_matrix (w->desired_matrix);
16076 centering_position = 0;
16077 goto recenter;
16080 done:
16082 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16083 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16084 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16086 /* Display the mode line, if we must. */
16087 if ((update_mode_line
16088 /* If window not full width, must redo its mode line
16089 if (a) the window to its side is being redone and
16090 (b) we do a frame-based redisplay. This is a consequence
16091 of how inverted lines are drawn in frame-based redisplay. */
16092 || (!just_this_one_p
16093 && !FRAME_WINDOW_P (f)
16094 && !WINDOW_FULL_WIDTH_P (w))
16095 /* Line number to display. */
16096 || w->base_line_pos > 0
16097 /* Column number is displayed and different from the one displayed. */
16098 || (w->column_number_displayed != -1
16099 && (w->column_number_displayed != current_column ())))
16100 /* This means that the window has a mode line. */
16101 && (WINDOW_WANTS_MODELINE_P (w)
16102 || WINDOW_WANTS_HEADER_LINE_P (w)))
16104 display_mode_lines (w);
16106 /* If mode line height has changed, arrange for a thorough
16107 immediate redisplay using the correct mode line height. */
16108 if (WINDOW_WANTS_MODELINE_P (w)
16109 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16111 fonts_changed_p = 1;
16112 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16113 = DESIRED_MODE_LINE_HEIGHT (w);
16116 /* If header line height has changed, arrange for a thorough
16117 immediate redisplay using the correct header line height. */
16118 if (WINDOW_WANTS_HEADER_LINE_P (w)
16119 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16121 fonts_changed_p = 1;
16122 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16123 = DESIRED_HEADER_LINE_HEIGHT (w);
16126 if (fonts_changed_p)
16127 goto need_larger_matrices;
16130 if (!line_number_displayed && w->base_line_pos != -1)
16132 w->base_line_pos = 0;
16133 w->base_line_number = 0;
16136 finish_menu_bars:
16138 /* When we reach a frame's selected window, redo the frame's menu bar. */
16139 if (update_mode_line
16140 && EQ (FRAME_SELECTED_WINDOW (f), window))
16142 int redisplay_menu_p = 0;
16144 if (FRAME_WINDOW_P (f))
16146 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16147 || defined (HAVE_NS) || defined (USE_GTK)
16148 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16149 #else
16150 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16151 #endif
16153 else
16154 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16156 if (redisplay_menu_p)
16157 display_menu_bar (w);
16159 #ifdef HAVE_WINDOW_SYSTEM
16160 if (FRAME_WINDOW_P (f))
16162 #if defined (USE_GTK) || defined (HAVE_NS)
16163 if (FRAME_EXTERNAL_TOOL_BAR (f))
16164 redisplay_tool_bar (f);
16165 #else
16166 if (WINDOWP (f->tool_bar_window)
16167 && (FRAME_TOOL_BAR_LINES (f) > 0
16168 || !NILP (Vauto_resize_tool_bars))
16169 && redisplay_tool_bar (f))
16170 ignore_mouse_drag_p = 1;
16171 #endif
16173 #endif
16176 #ifdef HAVE_WINDOW_SYSTEM
16177 if (FRAME_WINDOW_P (f)
16178 && update_window_fringes (w, (just_this_one_p
16179 || (!used_current_matrix_p && !overlay_arrow_seen)
16180 || w->pseudo_window_p)))
16182 update_begin (f);
16183 block_input ();
16184 if (draw_window_fringes (w, 1))
16185 x_draw_vertical_border (w);
16186 unblock_input ();
16187 update_end (f);
16189 #endif /* HAVE_WINDOW_SYSTEM */
16191 /* We go to this label, with fonts_changed_p set,
16192 if it is necessary to try again using larger glyph matrices.
16193 We have to redeem the scroll bar even in this case,
16194 because the loop in redisplay_internal expects that. */
16195 need_larger_matrices:
16197 finish_scroll_bars:
16199 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16201 /* Set the thumb's position and size. */
16202 set_vertical_scroll_bar (w);
16204 /* Note that we actually used the scroll bar attached to this
16205 window, so it shouldn't be deleted at the end of redisplay. */
16206 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16207 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16210 /* Restore current_buffer and value of point in it. The window
16211 update may have changed the buffer, so first make sure `opoint'
16212 is still valid (Bug#6177). */
16213 if (CHARPOS (opoint) < BEGV)
16214 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16215 else if (CHARPOS (opoint) > ZV)
16216 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16217 else
16218 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16220 set_buffer_internal_1 (old);
16221 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16222 shorter. This can be caused by log truncation in *Messages*. */
16223 if (CHARPOS (lpoint) <= ZV)
16224 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16226 unbind_to (count, Qnil);
16230 /* Build the complete desired matrix of WINDOW with a window start
16231 buffer position POS.
16233 Value is 1 if successful. It is zero if fonts were loaded during
16234 redisplay which makes re-adjusting glyph matrices necessary, and -1
16235 if point would appear in the scroll margins.
16236 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16237 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16238 set in FLAGS.) */
16241 try_window (Lisp_Object window, struct text_pos pos, int flags)
16243 struct window *w = XWINDOW (window);
16244 struct it it;
16245 struct glyph_row *last_text_row = NULL;
16246 struct frame *f = XFRAME (w->frame);
16247 int frame_line_height = default_line_pixel_height (w);
16249 /* Make POS the new window start. */
16250 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16252 /* Mark cursor position as unknown. No overlay arrow seen. */
16253 w->cursor.vpos = -1;
16254 overlay_arrow_seen = 0;
16256 /* Initialize iterator and info to start at POS. */
16257 start_display (&it, w, pos);
16259 /* Display all lines of W. */
16260 while (it.current_y < it.last_visible_y)
16262 if (display_line (&it))
16263 last_text_row = it.glyph_row - 1;
16264 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16265 return 0;
16268 /* Don't let the cursor end in the scroll margins. */
16269 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16270 && !MINI_WINDOW_P (w))
16272 int this_scroll_margin;
16273 int window_total_lines
16274 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16276 if (scroll_margin > 0)
16278 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16279 this_scroll_margin *= frame_line_height;
16281 else
16282 this_scroll_margin = 0;
16284 if ((w->cursor.y >= 0 /* not vscrolled */
16285 && w->cursor.y < this_scroll_margin
16286 && CHARPOS (pos) > BEGV
16287 && IT_CHARPOS (it) < ZV)
16288 /* rms: considering make_cursor_line_fully_visible_p here
16289 seems to give wrong results. We don't want to recenter
16290 when the last line is partly visible, we want to allow
16291 that case to be handled in the usual way. */
16292 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16294 w->cursor.vpos = -1;
16295 clear_glyph_matrix (w->desired_matrix);
16296 return -1;
16300 /* If bottom moved off end of frame, change mode line percentage. */
16301 if (XFASTINT (w->window_end_pos) <= 0
16302 && Z != IT_CHARPOS (it))
16303 w->update_mode_line = 1;
16305 /* Set window_end_pos to the offset of the last character displayed
16306 on the window from the end of current_buffer. Set
16307 window_end_vpos to its row number. */
16308 if (last_text_row)
16310 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16311 w->window_end_bytepos
16312 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16313 wset_window_end_pos
16314 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16315 wset_window_end_vpos
16316 (w, make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16317 eassert
16318 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16319 XFASTINT (w->window_end_vpos))));
16321 else
16323 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16324 wset_window_end_pos (w, make_number (Z - ZV));
16325 wset_window_end_vpos (w, make_number (0));
16328 /* But that is not valid info until redisplay finishes. */
16329 w->window_end_valid = 0;
16330 return 1;
16335 /************************************************************************
16336 Window redisplay reusing current matrix when buffer has not changed
16337 ************************************************************************/
16339 /* Try redisplay of window W showing an unchanged buffer with a
16340 different window start than the last time it was displayed by
16341 reusing its current matrix. Value is non-zero if successful.
16342 W->start is the new window start. */
16344 static int
16345 try_window_reusing_current_matrix (struct window *w)
16347 struct frame *f = XFRAME (w->frame);
16348 struct glyph_row *bottom_row;
16349 struct it it;
16350 struct run run;
16351 struct text_pos start, new_start;
16352 int nrows_scrolled, i;
16353 struct glyph_row *last_text_row;
16354 struct glyph_row *last_reused_text_row;
16355 struct glyph_row *start_row;
16356 int start_vpos, min_y, max_y;
16358 #ifdef GLYPH_DEBUG
16359 if (inhibit_try_window_reusing)
16360 return 0;
16361 #endif
16363 if (/* This function doesn't handle terminal frames. */
16364 !FRAME_WINDOW_P (f)
16365 /* Don't try to reuse the display if windows have been split
16366 or such. */
16367 || windows_or_buffers_changed
16368 || cursor_type_changed)
16369 return 0;
16371 /* Can't do this if region may have changed. */
16372 if (markpos_of_region () >= 0
16373 || w->region_showing
16374 || !NILP (Vshow_trailing_whitespace))
16375 return 0;
16377 /* If top-line visibility has changed, give up. */
16378 if (WINDOW_WANTS_HEADER_LINE_P (w)
16379 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16380 return 0;
16382 /* Give up if old or new display is scrolled vertically. We could
16383 make this function handle this, but right now it doesn't. */
16384 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16385 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16386 return 0;
16388 /* The variable new_start now holds the new window start. The old
16389 start `start' can be determined from the current matrix. */
16390 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16391 start = start_row->minpos;
16392 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16394 /* Clear the desired matrix for the display below. */
16395 clear_glyph_matrix (w->desired_matrix);
16397 if (CHARPOS (new_start) <= CHARPOS (start))
16399 /* Don't use this method if the display starts with an ellipsis
16400 displayed for invisible text. It's not easy to handle that case
16401 below, and it's certainly not worth the effort since this is
16402 not a frequent case. */
16403 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16404 return 0;
16406 IF_DEBUG (debug_method_add (w, "twu1"));
16408 /* Display up to a row that can be reused. The variable
16409 last_text_row is set to the last row displayed that displays
16410 text. Note that it.vpos == 0 if or if not there is a
16411 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16412 start_display (&it, w, new_start);
16413 w->cursor.vpos = -1;
16414 last_text_row = last_reused_text_row = NULL;
16416 while (it.current_y < it.last_visible_y
16417 && !fonts_changed_p)
16419 /* If we have reached into the characters in the START row,
16420 that means the line boundaries have changed. So we
16421 can't start copying with the row START. Maybe it will
16422 work to start copying with the following row. */
16423 while (IT_CHARPOS (it) > CHARPOS (start))
16425 /* Advance to the next row as the "start". */
16426 start_row++;
16427 start = start_row->minpos;
16428 /* If there are no more rows to try, or just one, give up. */
16429 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16430 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16431 || CHARPOS (start) == ZV)
16433 clear_glyph_matrix (w->desired_matrix);
16434 return 0;
16437 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16439 /* If we have reached alignment, we can copy the rest of the
16440 rows. */
16441 if (IT_CHARPOS (it) == CHARPOS (start)
16442 /* Don't accept "alignment" inside a display vector,
16443 since start_row could have started in the middle of
16444 that same display vector (thus their character
16445 positions match), and we have no way of telling if
16446 that is the case. */
16447 && it.current.dpvec_index < 0)
16448 break;
16450 if (display_line (&it))
16451 last_text_row = it.glyph_row - 1;
16455 /* A value of current_y < last_visible_y means that we stopped
16456 at the previous window start, which in turn means that we
16457 have at least one reusable row. */
16458 if (it.current_y < it.last_visible_y)
16460 struct glyph_row *row;
16462 /* IT.vpos always starts from 0; it counts text lines. */
16463 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16465 /* Find PT if not already found in the lines displayed. */
16466 if (w->cursor.vpos < 0)
16468 int dy = it.current_y - start_row->y;
16470 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16471 row = row_containing_pos (w, PT, row, NULL, dy);
16472 if (row)
16473 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16474 dy, nrows_scrolled);
16475 else
16477 clear_glyph_matrix (w->desired_matrix);
16478 return 0;
16482 /* Scroll the display. Do it before the current matrix is
16483 changed. The problem here is that update has not yet
16484 run, i.e. part of the current matrix is not up to date.
16485 scroll_run_hook will clear the cursor, and use the
16486 current matrix to get the height of the row the cursor is
16487 in. */
16488 run.current_y = start_row->y;
16489 run.desired_y = it.current_y;
16490 run.height = it.last_visible_y - it.current_y;
16492 if (run.height > 0 && run.current_y != run.desired_y)
16494 update_begin (f);
16495 FRAME_RIF (f)->update_window_begin_hook (w);
16496 FRAME_RIF (f)->clear_window_mouse_face (w);
16497 FRAME_RIF (f)->scroll_run_hook (w, &run);
16498 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16499 update_end (f);
16502 /* Shift current matrix down by nrows_scrolled lines. */
16503 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16504 rotate_matrix (w->current_matrix,
16505 start_vpos,
16506 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16507 nrows_scrolled);
16509 /* Disable lines that must be updated. */
16510 for (i = 0; i < nrows_scrolled; ++i)
16511 (start_row + i)->enabled_p = 0;
16513 /* Re-compute Y positions. */
16514 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16515 max_y = it.last_visible_y;
16516 for (row = start_row + nrows_scrolled;
16517 row < bottom_row;
16518 ++row)
16520 row->y = it.current_y;
16521 row->visible_height = row->height;
16523 if (row->y < min_y)
16524 row->visible_height -= min_y - row->y;
16525 if (row->y + row->height > max_y)
16526 row->visible_height -= row->y + row->height - max_y;
16527 if (row->fringe_bitmap_periodic_p)
16528 row->redraw_fringe_bitmaps_p = 1;
16530 it.current_y += row->height;
16532 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16533 last_reused_text_row = row;
16534 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16535 break;
16538 /* Disable lines in the current matrix which are now
16539 below the window. */
16540 for (++row; row < bottom_row; ++row)
16541 row->enabled_p = row->mode_line_p = 0;
16544 /* Update window_end_pos etc.; last_reused_text_row is the last
16545 reused row from the current matrix containing text, if any.
16546 The value of last_text_row is the last displayed line
16547 containing text. */
16548 if (last_reused_text_row)
16550 w->window_end_bytepos
16551 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16552 wset_window_end_pos
16553 (w, make_number (Z
16554 - MATRIX_ROW_END_CHARPOS (last_reused_text_row)));
16555 wset_window_end_vpos
16556 (w, make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16557 w->current_matrix)));
16559 else if (last_text_row)
16561 w->window_end_bytepos
16562 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16563 wset_window_end_pos
16564 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16565 wset_window_end_vpos
16566 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16567 w->desired_matrix)));
16569 else
16571 /* This window must be completely empty. */
16572 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16573 wset_window_end_pos (w, make_number (Z - ZV));
16574 wset_window_end_vpos (w, make_number (0));
16576 w->window_end_valid = 0;
16578 /* Update hint: don't try scrolling again in update_window. */
16579 w->desired_matrix->no_scrolling_p = 1;
16581 #ifdef GLYPH_DEBUG
16582 debug_method_add (w, "try_window_reusing_current_matrix 1");
16583 #endif
16584 return 1;
16586 else if (CHARPOS (new_start) > CHARPOS (start))
16588 struct glyph_row *pt_row, *row;
16589 struct glyph_row *first_reusable_row;
16590 struct glyph_row *first_row_to_display;
16591 int dy;
16592 int yb = window_text_bottom_y (w);
16594 /* Find the row starting at new_start, if there is one. Don't
16595 reuse a partially visible line at the end. */
16596 first_reusable_row = start_row;
16597 while (first_reusable_row->enabled_p
16598 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16599 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16600 < CHARPOS (new_start)))
16601 ++first_reusable_row;
16603 /* Give up if there is no row to reuse. */
16604 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16605 || !first_reusable_row->enabled_p
16606 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16607 != CHARPOS (new_start)))
16608 return 0;
16610 /* We can reuse fully visible rows beginning with
16611 first_reusable_row to the end of the window. Set
16612 first_row_to_display to the first row that cannot be reused.
16613 Set pt_row to the row containing point, if there is any. */
16614 pt_row = NULL;
16615 for (first_row_to_display = first_reusable_row;
16616 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16617 ++first_row_to_display)
16619 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16620 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16621 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16622 && first_row_to_display->ends_at_zv_p
16623 && pt_row == NULL)))
16624 pt_row = first_row_to_display;
16627 /* Start displaying at the start of first_row_to_display. */
16628 eassert (first_row_to_display->y < yb);
16629 init_to_row_start (&it, w, first_row_to_display);
16631 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16632 - start_vpos);
16633 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16634 - nrows_scrolled);
16635 it.current_y = (first_row_to_display->y - first_reusable_row->y
16636 + WINDOW_HEADER_LINE_HEIGHT (w));
16638 /* Display lines beginning with first_row_to_display in the
16639 desired matrix. Set last_text_row to the last row displayed
16640 that displays text. */
16641 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16642 if (pt_row == NULL)
16643 w->cursor.vpos = -1;
16644 last_text_row = NULL;
16645 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16646 if (display_line (&it))
16647 last_text_row = it.glyph_row - 1;
16649 /* If point is in a reused row, adjust y and vpos of the cursor
16650 position. */
16651 if (pt_row)
16653 w->cursor.vpos -= nrows_scrolled;
16654 w->cursor.y -= first_reusable_row->y - start_row->y;
16657 /* Give up if point isn't in a row displayed or reused. (This
16658 also handles the case where w->cursor.vpos < nrows_scrolled
16659 after the calls to display_line, which can happen with scroll
16660 margins. See bug#1295.) */
16661 if (w->cursor.vpos < 0)
16663 clear_glyph_matrix (w->desired_matrix);
16664 return 0;
16667 /* Scroll the display. */
16668 run.current_y = first_reusable_row->y;
16669 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16670 run.height = it.last_visible_y - run.current_y;
16671 dy = run.current_y - run.desired_y;
16673 if (run.height)
16675 update_begin (f);
16676 FRAME_RIF (f)->update_window_begin_hook (w);
16677 FRAME_RIF (f)->clear_window_mouse_face (w);
16678 FRAME_RIF (f)->scroll_run_hook (w, &run);
16679 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16680 update_end (f);
16683 /* Adjust Y positions of reused rows. */
16684 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16685 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16686 max_y = it.last_visible_y;
16687 for (row = first_reusable_row; row < first_row_to_display; ++row)
16689 row->y -= dy;
16690 row->visible_height = row->height;
16691 if (row->y < min_y)
16692 row->visible_height -= min_y - row->y;
16693 if (row->y + row->height > max_y)
16694 row->visible_height -= row->y + row->height - max_y;
16695 if (row->fringe_bitmap_periodic_p)
16696 row->redraw_fringe_bitmaps_p = 1;
16699 /* Scroll the current matrix. */
16700 eassert (nrows_scrolled > 0);
16701 rotate_matrix (w->current_matrix,
16702 start_vpos,
16703 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16704 -nrows_scrolled);
16706 /* Disable rows not reused. */
16707 for (row -= nrows_scrolled; row < bottom_row; ++row)
16708 row->enabled_p = 0;
16710 /* Point may have moved to a different line, so we cannot assume that
16711 the previous cursor position is valid; locate the correct row. */
16712 if (pt_row)
16714 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16715 row < bottom_row
16716 && PT >= MATRIX_ROW_END_CHARPOS (row)
16717 && !row->ends_at_zv_p;
16718 row++)
16720 w->cursor.vpos++;
16721 w->cursor.y = row->y;
16723 if (row < bottom_row)
16725 /* Can't simply scan the row for point with
16726 bidi-reordered glyph rows. Let set_cursor_from_row
16727 figure out where to put the cursor, and if it fails,
16728 give up. */
16729 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16731 if (!set_cursor_from_row (w, row, w->current_matrix,
16732 0, 0, 0, 0))
16734 clear_glyph_matrix (w->desired_matrix);
16735 return 0;
16738 else
16740 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16741 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16743 for (; glyph < end
16744 && (!BUFFERP (glyph->object)
16745 || glyph->charpos < PT);
16746 glyph++)
16748 w->cursor.hpos++;
16749 w->cursor.x += glyph->pixel_width;
16755 /* Adjust window end. A null value of last_text_row means that
16756 the window end is in reused rows which in turn means that
16757 only its vpos can have changed. */
16758 if (last_text_row)
16760 w->window_end_bytepos
16761 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16762 wset_window_end_pos
16763 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16764 wset_window_end_vpos
16765 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16766 w->desired_matrix)));
16768 else
16770 wset_window_end_vpos
16771 (w, make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled));
16774 w->window_end_valid = 0;
16775 w->desired_matrix->no_scrolling_p = 1;
16777 #ifdef GLYPH_DEBUG
16778 debug_method_add (w, "try_window_reusing_current_matrix 2");
16779 #endif
16780 return 1;
16783 return 0;
16788 /************************************************************************
16789 Window redisplay reusing current matrix when buffer has changed
16790 ************************************************************************/
16792 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16793 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16794 ptrdiff_t *, ptrdiff_t *);
16795 static struct glyph_row *
16796 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16797 struct glyph_row *);
16800 /* Return the last row in MATRIX displaying text. If row START is
16801 non-null, start searching with that row. IT gives the dimensions
16802 of the display. Value is null if matrix is empty; otherwise it is
16803 a pointer to the row found. */
16805 static struct glyph_row *
16806 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16807 struct glyph_row *start)
16809 struct glyph_row *row, *row_found;
16811 /* Set row_found to the last row in IT->w's current matrix
16812 displaying text. The loop looks funny but think of partially
16813 visible lines. */
16814 row_found = NULL;
16815 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16816 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16818 eassert (row->enabled_p);
16819 row_found = row;
16820 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16821 break;
16822 ++row;
16825 return row_found;
16829 /* Return the last row in the current matrix of W that is not affected
16830 by changes at the start of current_buffer that occurred since W's
16831 current matrix was built. Value is null if no such row exists.
16833 BEG_UNCHANGED us the number of characters unchanged at the start of
16834 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16835 first changed character in current_buffer. Characters at positions <
16836 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16837 when the current matrix was built. */
16839 static struct glyph_row *
16840 find_last_unchanged_at_beg_row (struct window *w)
16842 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16843 struct glyph_row *row;
16844 struct glyph_row *row_found = NULL;
16845 int yb = window_text_bottom_y (w);
16847 /* Find the last row displaying unchanged text. */
16848 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16849 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16850 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16851 ++row)
16853 if (/* If row ends before first_changed_pos, it is unchanged,
16854 except in some case. */
16855 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16856 /* When row ends in ZV and we write at ZV it is not
16857 unchanged. */
16858 && !row->ends_at_zv_p
16859 /* When first_changed_pos is the end of a continued line,
16860 row is not unchanged because it may be no longer
16861 continued. */
16862 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16863 && (row->continued_p
16864 || row->exact_window_width_line_p))
16865 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16866 needs to be recomputed, so don't consider this row as
16867 unchanged. This happens when the last line was
16868 bidi-reordered and was killed immediately before this
16869 redisplay cycle. In that case, ROW->end stores the
16870 buffer position of the first visual-order character of
16871 the killed text, which is now beyond ZV. */
16872 && CHARPOS (row->end.pos) <= ZV)
16873 row_found = row;
16875 /* Stop if last visible row. */
16876 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16877 break;
16880 return row_found;
16884 /* Find the first glyph row in the current matrix of W that is not
16885 affected by changes at the end of current_buffer since the
16886 time W's current matrix was built.
16888 Return in *DELTA the number of chars by which buffer positions in
16889 unchanged text at the end of current_buffer must be adjusted.
16891 Return in *DELTA_BYTES the corresponding number of bytes.
16893 Value is null if no such row exists, i.e. all rows are affected by
16894 changes. */
16896 static struct glyph_row *
16897 find_first_unchanged_at_end_row (struct window *w,
16898 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16900 struct glyph_row *row;
16901 struct glyph_row *row_found = NULL;
16903 *delta = *delta_bytes = 0;
16905 /* Display must not have been paused, otherwise the current matrix
16906 is not up to date. */
16907 eassert (w->window_end_valid);
16909 /* A value of window_end_pos >= END_UNCHANGED means that the window
16910 end is in the range of changed text. If so, there is no
16911 unchanged row at the end of W's current matrix. */
16912 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16913 return NULL;
16915 /* Set row to the last row in W's current matrix displaying text. */
16916 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16918 /* If matrix is entirely empty, no unchanged row exists. */
16919 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16921 /* The value of row is the last glyph row in the matrix having a
16922 meaningful buffer position in it. The end position of row
16923 corresponds to window_end_pos. This allows us to translate
16924 buffer positions in the current matrix to current buffer
16925 positions for characters not in changed text. */
16926 ptrdiff_t Z_old =
16927 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16928 ptrdiff_t Z_BYTE_old =
16929 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16930 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16931 struct glyph_row *first_text_row
16932 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16934 *delta = Z - Z_old;
16935 *delta_bytes = Z_BYTE - Z_BYTE_old;
16937 /* Set last_unchanged_pos to the buffer position of the last
16938 character in the buffer that has not been changed. Z is the
16939 index + 1 of the last character in current_buffer, i.e. by
16940 subtracting END_UNCHANGED we get the index of the last
16941 unchanged character, and we have to add BEG to get its buffer
16942 position. */
16943 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16944 last_unchanged_pos_old = last_unchanged_pos - *delta;
16946 /* Search backward from ROW for a row displaying a line that
16947 starts at a minimum position >= last_unchanged_pos_old. */
16948 for (; row > first_text_row; --row)
16950 /* This used to abort, but it can happen.
16951 It is ok to just stop the search instead here. KFS. */
16952 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16953 break;
16955 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16956 row_found = row;
16960 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16962 return row_found;
16966 /* Make sure that glyph rows in the current matrix of window W
16967 reference the same glyph memory as corresponding rows in the
16968 frame's frame matrix. This function is called after scrolling W's
16969 current matrix on a terminal frame in try_window_id and
16970 try_window_reusing_current_matrix. */
16972 static void
16973 sync_frame_with_window_matrix_rows (struct window *w)
16975 struct frame *f = XFRAME (w->frame);
16976 struct glyph_row *window_row, *window_row_end, *frame_row;
16978 /* Preconditions: W must be a leaf window and full-width. Its frame
16979 must have a frame matrix. */
16980 eassert (BUFFERP (w->contents));
16981 eassert (WINDOW_FULL_WIDTH_P (w));
16982 eassert (!FRAME_WINDOW_P (f));
16984 /* If W is a full-width window, glyph pointers in W's current matrix
16985 have, by definition, to be the same as glyph pointers in the
16986 corresponding frame matrix. Note that frame matrices have no
16987 marginal areas (see build_frame_matrix). */
16988 window_row = w->current_matrix->rows;
16989 window_row_end = window_row + w->current_matrix->nrows;
16990 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16991 while (window_row < window_row_end)
16993 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16994 struct glyph *end = window_row->glyphs[LAST_AREA];
16996 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16997 frame_row->glyphs[TEXT_AREA] = start;
16998 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16999 frame_row->glyphs[LAST_AREA] = end;
17001 /* Disable frame rows whose corresponding window rows have
17002 been disabled in try_window_id. */
17003 if (!window_row->enabled_p)
17004 frame_row->enabled_p = 0;
17006 ++window_row, ++frame_row;
17011 /* Find the glyph row in window W containing CHARPOS. Consider all
17012 rows between START and END (not inclusive). END null means search
17013 all rows to the end of the display area of W. Value is the row
17014 containing CHARPOS or null. */
17016 struct glyph_row *
17017 row_containing_pos (struct window *w, ptrdiff_t charpos,
17018 struct glyph_row *start, struct glyph_row *end, int dy)
17020 struct glyph_row *row = start;
17021 struct glyph_row *best_row = NULL;
17022 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17023 int last_y;
17025 /* If we happen to start on a header-line, skip that. */
17026 if (row->mode_line_p)
17027 ++row;
17029 if ((end && row >= end) || !row->enabled_p)
17030 return NULL;
17032 last_y = window_text_bottom_y (w) - dy;
17034 while (1)
17036 /* Give up if we have gone too far. */
17037 if (end && row >= end)
17038 return NULL;
17039 /* This formerly returned if they were equal.
17040 I think that both quantities are of a "last plus one" type;
17041 if so, when they are equal, the row is within the screen. -- rms. */
17042 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17043 return NULL;
17045 /* If it is in this row, return this row. */
17046 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17047 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17048 /* The end position of a row equals the start
17049 position of the next row. If CHARPOS is there, we
17050 would rather consider it displayed in the next
17051 line, except when this line ends in ZV. */
17052 && !row_for_charpos_p (row, charpos)))
17053 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17055 struct glyph *g;
17057 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17058 || (!best_row && !row->continued_p))
17059 return row;
17060 /* In bidi-reordered rows, there could be several rows whose
17061 edges surround CHARPOS, all of these rows belonging to
17062 the same continued line. We need to find the row which
17063 fits CHARPOS the best. */
17064 for (g = row->glyphs[TEXT_AREA];
17065 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17066 g++)
17068 if (!STRINGP (g->object))
17070 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17072 mindif = eabs (g->charpos - charpos);
17073 best_row = row;
17074 /* Exact match always wins. */
17075 if (mindif == 0)
17076 return best_row;
17081 else if (best_row && !row->continued_p)
17082 return best_row;
17083 ++row;
17088 /* Try to redisplay window W by reusing its existing display. W's
17089 current matrix must be up to date when this function is called,
17090 i.e. window_end_valid must be nonzero.
17092 Value is
17094 1 if display has been updated
17095 0 if otherwise unsuccessful
17096 -1 if redisplay with same window start is known not to succeed
17098 The following steps are performed:
17100 1. Find the last row in the current matrix of W that is not
17101 affected by changes at the start of current_buffer. If no such row
17102 is found, give up.
17104 2. Find the first row in W's current matrix that is not affected by
17105 changes at the end of current_buffer. Maybe there is no such row.
17107 3. Display lines beginning with the row + 1 found in step 1 to the
17108 row found in step 2 or, if step 2 didn't find a row, to the end of
17109 the window.
17111 4. If cursor is not known to appear on the window, give up.
17113 5. If display stopped at the row found in step 2, scroll the
17114 display and current matrix as needed.
17116 6. Maybe display some lines at the end of W, if we must. This can
17117 happen under various circumstances, like a partially visible line
17118 becoming fully visible, or because newly displayed lines are displayed
17119 in smaller font sizes.
17121 7. Update W's window end information. */
17123 static int
17124 try_window_id (struct window *w)
17126 struct frame *f = XFRAME (w->frame);
17127 struct glyph_matrix *current_matrix = w->current_matrix;
17128 struct glyph_matrix *desired_matrix = w->desired_matrix;
17129 struct glyph_row *last_unchanged_at_beg_row;
17130 struct glyph_row *first_unchanged_at_end_row;
17131 struct glyph_row *row;
17132 struct glyph_row *bottom_row;
17133 int bottom_vpos;
17134 struct it it;
17135 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17136 int dvpos, dy;
17137 struct text_pos start_pos;
17138 struct run run;
17139 int first_unchanged_at_end_vpos = 0;
17140 struct glyph_row *last_text_row, *last_text_row_at_end;
17141 struct text_pos start;
17142 ptrdiff_t first_changed_charpos, last_changed_charpos;
17144 #ifdef GLYPH_DEBUG
17145 if (inhibit_try_window_id)
17146 return 0;
17147 #endif
17149 /* This is handy for debugging. */
17150 #if 0
17151 #define GIVE_UP(X) \
17152 do { \
17153 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17154 return 0; \
17155 } while (0)
17156 #else
17157 #define GIVE_UP(X) return 0
17158 #endif
17160 SET_TEXT_POS_FROM_MARKER (start, w->start);
17162 /* Don't use this for mini-windows because these can show
17163 messages and mini-buffers, and we don't handle that here. */
17164 if (MINI_WINDOW_P (w))
17165 GIVE_UP (1);
17167 /* This flag is used to prevent redisplay optimizations. */
17168 if (windows_or_buffers_changed || cursor_type_changed)
17169 GIVE_UP (2);
17171 /* Verify that narrowing has not changed.
17172 Also verify that we were not told to prevent redisplay optimizations.
17173 It would be nice to further
17174 reduce the number of cases where this prevents try_window_id. */
17175 if (current_buffer->clip_changed
17176 || current_buffer->prevent_redisplay_optimizations_p)
17177 GIVE_UP (3);
17179 /* Window must either use window-based redisplay or be full width. */
17180 if (!FRAME_WINDOW_P (f)
17181 && (!FRAME_LINE_INS_DEL_OK (f)
17182 || !WINDOW_FULL_WIDTH_P (w)))
17183 GIVE_UP (4);
17185 /* Give up if point is known NOT to appear in W. */
17186 if (PT < CHARPOS (start))
17187 GIVE_UP (5);
17189 /* Another way to prevent redisplay optimizations. */
17190 if (w->last_modified == 0)
17191 GIVE_UP (6);
17193 /* Verify that window is not hscrolled. */
17194 if (w->hscroll != 0)
17195 GIVE_UP (7);
17197 /* Verify that display wasn't paused. */
17198 if (!w->window_end_valid)
17199 GIVE_UP (8);
17201 /* Can't use this if highlighting a region because a cursor movement
17202 will do more than just set the cursor. */
17203 if (markpos_of_region () >= 0)
17204 GIVE_UP (9);
17206 /* Likewise if highlighting trailing whitespace. */
17207 if (!NILP (Vshow_trailing_whitespace))
17208 GIVE_UP (11);
17210 /* Likewise if showing a region. */
17211 if (w->region_showing)
17212 GIVE_UP (10);
17214 /* Can't use this if overlay arrow position and/or string have
17215 changed. */
17216 if (overlay_arrows_changed_p ())
17217 GIVE_UP (12);
17219 /* When word-wrap is on, adding a space to the first word of a
17220 wrapped line can change the wrap position, altering the line
17221 above it. It might be worthwhile to handle this more
17222 intelligently, but for now just redisplay from scratch. */
17223 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17224 GIVE_UP (21);
17226 /* Under bidi reordering, adding or deleting a character in the
17227 beginning of a paragraph, before the first strong directional
17228 character, can change the base direction of the paragraph (unless
17229 the buffer specifies a fixed paragraph direction), which will
17230 require to redisplay the whole paragraph. It might be worthwhile
17231 to find the paragraph limits and widen the range of redisplayed
17232 lines to that, but for now just give up this optimization and
17233 redisplay from scratch. */
17234 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17235 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17236 GIVE_UP (22);
17238 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17239 only if buffer has really changed. The reason is that the gap is
17240 initially at Z for freshly visited files. The code below would
17241 set end_unchanged to 0 in that case. */
17242 if (MODIFF > SAVE_MODIFF
17243 /* This seems to happen sometimes after saving a buffer. */
17244 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17246 if (GPT - BEG < BEG_UNCHANGED)
17247 BEG_UNCHANGED = GPT - BEG;
17248 if (Z - GPT < END_UNCHANGED)
17249 END_UNCHANGED = Z - GPT;
17252 /* The position of the first and last character that has been changed. */
17253 first_changed_charpos = BEG + BEG_UNCHANGED;
17254 last_changed_charpos = Z - END_UNCHANGED;
17256 /* If window starts after a line end, and the last change is in
17257 front of that newline, then changes don't affect the display.
17258 This case happens with stealth-fontification. Note that although
17259 the display is unchanged, glyph positions in the matrix have to
17260 be adjusted, of course. */
17261 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17262 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17263 && ((last_changed_charpos < CHARPOS (start)
17264 && CHARPOS (start) == BEGV)
17265 || (last_changed_charpos < CHARPOS (start) - 1
17266 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17268 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17269 struct glyph_row *r0;
17271 /* Compute how many chars/bytes have been added to or removed
17272 from the buffer. */
17273 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17274 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17275 Z_delta = Z - Z_old;
17276 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17278 /* Give up if PT is not in the window. Note that it already has
17279 been checked at the start of try_window_id that PT is not in
17280 front of the window start. */
17281 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17282 GIVE_UP (13);
17284 /* If window start is unchanged, we can reuse the whole matrix
17285 as is, after adjusting glyph positions. No need to compute
17286 the window end again, since its offset from Z hasn't changed. */
17287 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17288 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17289 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17290 /* PT must not be in a partially visible line. */
17291 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17292 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17294 /* Adjust positions in the glyph matrix. */
17295 if (Z_delta || Z_delta_bytes)
17297 struct glyph_row *r1
17298 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17299 increment_matrix_positions (w->current_matrix,
17300 MATRIX_ROW_VPOS (r0, current_matrix),
17301 MATRIX_ROW_VPOS (r1, current_matrix),
17302 Z_delta, Z_delta_bytes);
17305 /* Set the cursor. */
17306 row = row_containing_pos (w, PT, r0, NULL, 0);
17307 if (row)
17308 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17309 else
17310 emacs_abort ();
17311 return 1;
17315 /* Handle the case that changes are all below what is displayed in
17316 the window, and that PT is in the window. This shortcut cannot
17317 be taken if ZV is visible in the window, and text has been added
17318 there that is visible in the window. */
17319 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17320 /* ZV is not visible in the window, or there are no
17321 changes at ZV, actually. */
17322 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17323 || first_changed_charpos == last_changed_charpos))
17325 struct glyph_row *r0;
17327 /* Give up if PT is not in the window. Note that it already has
17328 been checked at the start of try_window_id that PT is not in
17329 front of the window start. */
17330 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17331 GIVE_UP (14);
17333 /* If window start is unchanged, we can reuse the whole matrix
17334 as is, without changing glyph positions since no text has
17335 been added/removed in front of the window end. */
17336 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17337 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17338 /* PT must not be in a partially visible line. */
17339 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17340 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17342 /* We have to compute the window end anew since text
17343 could have been added/removed after it. */
17344 wset_window_end_pos
17345 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17346 w->window_end_bytepos
17347 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17349 /* Set the cursor. */
17350 row = row_containing_pos (w, PT, r0, NULL, 0);
17351 if (row)
17352 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17353 else
17354 emacs_abort ();
17355 return 2;
17359 /* Give up if window start is in the changed area.
17361 The condition used to read
17363 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17365 but why that was tested escapes me at the moment. */
17366 if (CHARPOS (start) >= first_changed_charpos
17367 && CHARPOS (start) <= last_changed_charpos)
17368 GIVE_UP (15);
17370 /* Check that window start agrees with the start of the first glyph
17371 row in its current matrix. Check this after we know the window
17372 start is not in changed text, otherwise positions would not be
17373 comparable. */
17374 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17375 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17376 GIVE_UP (16);
17378 /* Give up if the window ends in strings. Overlay strings
17379 at the end are difficult to handle, so don't try. */
17380 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17381 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17382 GIVE_UP (20);
17384 /* Compute the position at which we have to start displaying new
17385 lines. Some of the lines at the top of the window might be
17386 reusable because they are not displaying changed text. Find the
17387 last row in W's current matrix not affected by changes at the
17388 start of current_buffer. Value is null if changes start in the
17389 first line of window. */
17390 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17391 if (last_unchanged_at_beg_row)
17393 /* Avoid starting to display in the middle of a character, a TAB
17394 for instance. This is easier than to set up the iterator
17395 exactly, and it's not a frequent case, so the additional
17396 effort wouldn't really pay off. */
17397 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17398 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17399 && last_unchanged_at_beg_row > w->current_matrix->rows)
17400 --last_unchanged_at_beg_row;
17402 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17403 GIVE_UP (17);
17405 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17406 GIVE_UP (18);
17407 start_pos = it.current.pos;
17409 /* Start displaying new lines in the desired matrix at the same
17410 vpos we would use in the current matrix, i.e. below
17411 last_unchanged_at_beg_row. */
17412 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17413 current_matrix);
17414 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17415 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17417 eassert (it.hpos == 0 && it.current_x == 0);
17419 else
17421 /* There are no reusable lines at the start of the window.
17422 Start displaying in the first text line. */
17423 start_display (&it, w, start);
17424 it.vpos = it.first_vpos;
17425 start_pos = it.current.pos;
17428 /* Find the first row that is not affected by changes at the end of
17429 the buffer. Value will be null if there is no unchanged row, in
17430 which case we must redisplay to the end of the window. delta
17431 will be set to the value by which buffer positions beginning with
17432 first_unchanged_at_end_row have to be adjusted due to text
17433 changes. */
17434 first_unchanged_at_end_row
17435 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17436 IF_DEBUG (debug_delta = delta);
17437 IF_DEBUG (debug_delta_bytes = delta_bytes);
17439 /* Set stop_pos to the buffer position up to which we will have to
17440 display new lines. If first_unchanged_at_end_row != NULL, this
17441 is the buffer position of the start of the line displayed in that
17442 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17443 that we don't stop at a buffer position. */
17444 stop_pos = 0;
17445 if (first_unchanged_at_end_row)
17447 eassert (last_unchanged_at_beg_row == NULL
17448 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17450 /* If this is a continuation line, move forward to the next one
17451 that isn't. Changes in lines above affect this line.
17452 Caution: this may move first_unchanged_at_end_row to a row
17453 not displaying text. */
17454 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17455 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17456 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17457 < it.last_visible_y))
17458 ++first_unchanged_at_end_row;
17460 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17461 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17462 >= it.last_visible_y))
17463 first_unchanged_at_end_row = NULL;
17464 else
17466 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17467 + delta);
17468 first_unchanged_at_end_vpos
17469 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17470 eassert (stop_pos >= Z - END_UNCHANGED);
17473 else if (last_unchanged_at_beg_row == NULL)
17474 GIVE_UP (19);
17477 #ifdef GLYPH_DEBUG
17479 /* Either there is no unchanged row at the end, or the one we have
17480 now displays text. This is a necessary condition for the window
17481 end pos calculation at the end of this function. */
17482 eassert (first_unchanged_at_end_row == NULL
17483 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17485 debug_last_unchanged_at_beg_vpos
17486 = (last_unchanged_at_beg_row
17487 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17488 : -1);
17489 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17491 #endif /* GLYPH_DEBUG */
17494 /* Display new lines. Set last_text_row to the last new line
17495 displayed which has text on it, i.e. might end up as being the
17496 line where the window_end_vpos is. */
17497 w->cursor.vpos = -1;
17498 last_text_row = NULL;
17499 overlay_arrow_seen = 0;
17500 while (it.current_y < it.last_visible_y
17501 && !fonts_changed_p
17502 && (first_unchanged_at_end_row == NULL
17503 || IT_CHARPOS (it) < stop_pos))
17505 if (display_line (&it))
17506 last_text_row = it.glyph_row - 1;
17509 if (fonts_changed_p)
17510 return -1;
17513 /* Compute differences in buffer positions, y-positions etc. for
17514 lines reused at the bottom of the window. Compute what we can
17515 scroll. */
17516 if (first_unchanged_at_end_row
17517 /* No lines reused because we displayed everything up to the
17518 bottom of the window. */
17519 && it.current_y < it.last_visible_y)
17521 dvpos = (it.vpos
17522 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17523 current_matrix));
17524 dy = it.current_y - first_unchanged_at_end_row->y;
17525 run.current_y = first_unchanged_at_end_row->y;
17526 run.desired_y = run.current_y + dy;
17527 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17529 else
17531 delta = delta_bytes = dvpos = dy
17532 = run.current_y = run.desired_y = run.height = 0;
17533 first_unchanged_at_end_row = NULL;
17535 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17538 /* Find the cursor if not already found. We have to decide whether
17539 PT will appear on this window (it sometimes doesn't, but this is
17540 not a very frequent case.) This decision has to be made before
17541 the current matrix is altered. A value of cursor.vpos < 0 means
17542 that PT is either in one of the lines beginning at
17543 first_unchanged_at_end_row or below the window. Don't care for
17544 lines that might be displayed later at the window end; as
17545 mentioned, this is not a frequent case. */
17546 if (w->cursor.vpos < 0)
17548 /* Cursor in unchanged rows at the top? */
17549 if (PT < CHARPOS (start_pos)
17550 && last_unchanged_at_beg_row)
17552 row = row_containing_pos (w, PT,
17553 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17554 last_unchanged_at_beg_row + 1, 0);
17555 if (row)
17556 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17559 /* Start from first_unchanged_at_end_row looking for PT. */
17560 else if (first_unchanged_at_end_row)
17562 row = row_containing_pos (w, PT - delta,
17563 first_unchanged_at_end_row, NULL, 0);
17564 if (row)
17565 set_cursor_from_row (w, row, w->current_matrix, delta,
17566 delta_bytes, dy, dvpos);
17569 /* Give up if cursor was not found. */
17570 if (w->cursor.vpos < 0)
17572 clear_glyph_matrix (w->desired_matrix);
17573 return -1;
17577 /* Don't let the cursor end in the scroll margins. */
17579 int this_scroll_margin, cursor_height;
17580 int frame_line_height = default_line_pixel_height (w);
17581 int window_total_lines
17582 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17584 this_scroll_margin =
17585 max (0, min (scroll_margin, window_total_lines / 4));
17586 this_scroll_margin *= frame_line_height;
17587 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17589 if ((w->cursor.y < this_scroll_margin
17590 && CHARPOS (start) > BEGV)
17591 /* Old redisplay didn't take scroll margin into account at the bottom,
17592 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17593 || (w->cursor.y + (make_cursor_line_fully_visible_p
17594 ? cursor_height + this_scroll_margin
17595 : 1)) > it.last_visible_y)
17597 w->cursor.vpos = -1;
17598 clear_glyph_matrix (w->desired_matrix);
17599 return -1;
17603 /* Scroll the display. Do it before changing the current matrix so
17604 that xterm.c doesn't get confused about where the cursor glyph is
17605 found. */
17606 if (dy && run.height)
17608 update_begin (f);
17610 if (FRAME_WINDOW_P (f))
17612 FRAME_RIF (f)->update_window_begin_hook (w);
17613 FRAME_RIF (f)->clear_window_mouse_face (w);
17614 FRAME_RIF (f)->scroll_run_hook (w, &run);
17615 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17617 else
17619 /* Terminal frame. In this case, dvpos gives the number of
17620 lines to scroll by; dvpos < 0 means scroll up. */
17621 int from_vpos
17622 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17623 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17624 int end = (WINDOW_TOP_EDGE_LINE (w)
17625 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17626 + window_internal_height (w));
17628 #if defined (HAVE_GPM) || defined (MSDOS)
17629 x_clear_window_mouse_face (w);
17630 #endif
17631 /* Perform the operation on the screen. */
17632 if (dvpos > 0)
17634 /* Scroll last_unchanged_at_beg_row to the end of the
17635 window down dvpos lines. */
17636 set_terminal_window (f, end);
17638 /* On dumb terminals delete dvpos lines at the end
17639 before inserting dvpos empty lines. */
17640 if (!FRAME_SCROLL_REGION_OK (f))
17641 ins_del_lines (f, end - dvpos, -dvpos);
17643 /* Insert dvpos empty lines in front of
17644 last_unchanged_at_beg_row. */
17645 ins_del_lines (f, from, dvpos);
17647 else if (dvpos < 0)
17649 /* Scroll up last_unchanged_at_beg_vpos to the end of
17650 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17651 set_terminal_window (f, end);
17653 /* Delete dvpos lines in front of
17654 last_unchanged_at_beg_vpos. ins_del_lines will set
17655 the cursor to the given vpos and emit |dvpos| delete
17656 line sequences. */
17657 ins_del_lines (f, from + dvpos, dvpos);
17659 /* On a dumb terminal insert dvpos empty lines at the
17660 end. */
17661 if (!FRAME_SCROLL_REGION_OK (f))
17662 ins_del_lines (f, end + dvpos, -dvpos);
17665 set_terminal_window (f, 0);
17668 update_end (f);
17671 /* Shift reused rows of the current matrix to the right position.
17672 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17673 text. */
17674 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17675 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17676 if (dvpos < 0)
17678 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17679 bottom_vpos, dvpos);
17680 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17681 bottom_vpos);
17683 else if (dvpos > 0)
17685 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17686 bottom_vpos, dvpos);
17687 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17688 first_unchanged_at_end_vpos + dvpos);
17691 /* For frame-based redisplay, make sure that current frame and window
17692 matrix are in sync with respect to glyph memory. */
17693 if (!FRAME_WINDOW_P (f))
17694 sync_frame_with_window_matrix_rows (w);
17696 /* Adjust buffer positions in reused rows. */
17697 if (delta || delta_bytes)
17698 increment_matrix_positions (current_matrix,
17699 first_unchanged_at_end_vpos + dvpos,
17700 bottom_vpos, delta, delta_bytes);
17702 /* Adjust Y positions. */
17703 if (dy)
17704 shift_glyph_matrix (w, current_matrix,
17705 first_unchanged_at_end_vpos + dvpos,
17706 bottom_vpos, dy);
17708 if (first_unchanged_at_end_row)
17710 first_unchanged_at_end_row += dvpos;
17711 if (first_unchanged_at_end_row->y >= it.last_visible_y
17712 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17713 first_unchanged_at_end_row = NULL;
17716 /* If scrolling up, there may be some lines to display at the end of
17717 the window. */
17718 last_text_row_at_end = NULL;
17719 if (dy < 0)
17721 /* Scrolling up can leave for example a partially visible line
17722 at the end of the window to be redisplayed. */
17723 /* Set last_row to the glyph row in the current matrix where the
17724 window end line is found. It has been moved up or down in
17725 the matrix by dvpos. */
17726 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17727 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17729 /* If last_row is the window end line, it should display text. */
17730 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17732 /* If window end line was partially visible before, begin
17733 displaying at that line. Otherwise begin displaying with the
17734 line following it. */
17735 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17737 init_to_row_start (&it, w, last_row);
17738 it.vpos = last_vpos;
17739 it.current_y = last_row->y;
17741 else
17743 init_to_row_end (&it, w, last_row);
17744 it.vpos = 1 + last_vpos;
17745 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17746 ++last_row;
17749 /* We may start in a continuation line. If so, we have to
17750 get the right continuation_lines_width and current_x. */
17751 it.continuation_lines_width = last_row->continuation_lines_width;
17752 it.hpos = it.current_x = 0;
17754 /* Display the rest of the lines at the window end. */
17755 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17756 while (it.current_y < it.last_visible_y
17757 && !fonts_changed_p)
17759 /* Is it always sure that the display agrees with lines in
17760 the current matrix? I don't think so, so we mark rows
17761 displayed invalid in the current matrix by setting their
17762 enabled_p flag to zero. */
17763 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17764 if (display_line (&it))
17765 last_text_row_at_end = it.glyph_row - 1;
17769 /* Update window_end_pos and window_end_vpos. */
17770 if (first_unchanged_at_end_row
17771 && !last_text_row_at_end)
17773 /* Window end line if one of the preserved rows from the current
17774 matrix. Set row to the last row displaying text in current
17775 matrix starting at first_unchanged_at_end_row, after
17776 scrolling. */
17777 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17778 row = find_last_row_displaying_text (w->current_matrix, &it,
17779 first_unchanged_at_end_row);
17780 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17782 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17783 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17784 wset_window_end_vpos
17785 (w, make_number (MATRIX_ROW_VPOS (row, w->current_matrix)));
17786 eassert (w->window_end_bytepos >= 0);
17787 IF_DEBUG (debug_method_add (w, "A"));
17789 else if (last_text_row_at_end)
17791 wset_window_end_pos
17792 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end)));
17793 w->window_end_bytepos
17794 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17795 wset_window_end_vpos
17796 (w, make_number (MATRIX_ROW_VPOS (last_text_row_at_end,
17797 desired_matrix)));
17798 eassert (w->window_end_bytepos >= 0);
17799 IF_DEBUG (debug_method_add (w, "B"));
17801 else if (last_text_row)
17803 /* We have displayed either to the end of the window or at the
17804 end of the window, i.e. the last row with text is to be found
17805 in the desired matrix. */
17806 wset_window_end_pos
17807 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
17808 w->window_end_bytepos
17809 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17810 wset_window_end_vpos
17811 (w, make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix)));
17812 eassert (w->window_end_bytepos >= 0);
17814 else if (first_unchanged_at_end_row == NULL
17815 && last_text_row == NULL
17816 && last_text_row_at_end == NULL)
17818 /* Displayed to end of window, but no line containing text was
17819 displayed. Lines were deleted at the end of the window. */
17820 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17821 int vpos = XFASTINT (w->window_end_vpos);
17822 struct glyph_row *current_row = current_matrix->rows + vpos;
17823 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17825 for (row = NULL;
17826 row == NULL && vpos >= first_vpos;
17827 --vpos, --current_row, --desired_row)
17829 if (desired_row->enabled_p)
17831 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17832 row = desired_row;
17834 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17835 row = current_row;
17838 eassert (row != NULL);
17839 wset_window_end_vpos (w, make_number (vpos + 1));
17840 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17841 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17842 eassert (w->window_end_bytepos >= 0);
17843 IF_DEBUG (debug_method_add (w, "C"));
17845 else
17846 emacs_abort ();
17848 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17849 debug_end_vpos = XFASTINT (w->window_end_vpos));
17851 /* Record that display has not been completed. */
17852 w->window_end_valid = 0;
17853 w->desired_matrix->no_scrolling_p = 1;
17854 return 3;
17856 #undef GIVE_UP
17861 /***********************************************************************
17862 More debugging support
17863 ***********************************************************************/
17865 #ifdef GLYPH_DEBUG
17867 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17868 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17869 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17872 /* Dump the contents of glyph matrix MATRIX on stderr.
17874 GLYPHS 0 means don't show glyph contents.
17875 GLYPHS 1 means show glyphs in short form
17876 GLYPHS > 1 means show glyphs in long form. */
17878 void
17879 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17881 int i;
17882 for (i = 0; i < matrix->nrows; ++i)
17883 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17887 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17888 the glyph row and area where the glyph comes from. */
17890 void
17891 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17893 if (glyph->type == CHAR_GLYPH
17894 || glyph->type == GLYPHLESS_GLYPH)
17896 fprintf (stderr,
17897 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17898 glyph - row->glyphs[TEXT_AREA],
17899 (glyph->type == CHAR_GLYPH
17900 ? 'C'
17901 : 'G'),
17902 glyph->charpos,
17903 (BUFFERP (glyph->object)
17904 ? 'B'
17905 : (STRINGP (glyph->object)
17906 ? 'S'
17907 : (INTEGERP (glyph->object)
17908 ? '0'
17909 : '-'))),
17910 glyph->pixel_width,
17911 glyph->u.ch,
17912 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17913 ? glyph->u.ch
17914 : '.'),
17915 glyph->face_id,
17916 glyph->left_box_line_p,
17917 glyph->right_box_line_p);
17919 else if (glyph->type == STRETCH_GLYPH)
17921 fprintf (stderr,
17922 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17923 glyph - row->glyphs[TEXT_AREA],
17924 'S',
17925 glyph->charpos,
17926 (BUFFERP (glyph->object)
17927 ? 'B'
17928 : (STRINGP (glyph->object)
17929 ? 'S'
17930 : (INTEGERP (glyph->object)
17931 ? '0'
17932 : '-'))),
17933 glyph->pixel_width,
17935 ' ',
17936 glyph->face_id,
17937 glyph->left_box_line_p,
17938 glyph->right_box_line_p);
17940 else if (glyph->type == IMAGE_GLYPH)
17942 fprintf (stderr,
17943 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17944 glyph - row->glyphs[TEXT_AREA],
17945 'I',
17946 glyph->charpos,
17947 (BUFFERP (glyph->object)
17948 ? 'B'
17949 : (STRINGP (glyph->object)
17950 ? 'S'
17951 : (INTEGERP (glyph->object)
17952 ? '0'
17953 : '-'))),
17954 glyph->pixel_width,
17955 glyph->u.img_id,
17956 '.',
17957 glyph->face_id,
17958 glyph->left_box_line_p,
17959 glyph->right_box_line_p);
17961 else if (glyph->type == COMPOSITE_GLYPH)
17963 fprintf (stderr,
17964 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17965 glyph - row->glyphs[TEXT_AREA],
17966 '+',
17967 glyph->charpos,
17968 (BUFFERP (glyph->object)
17969 ? 'B'
17970 : (STRINGP (glyph->object)
17971 ? 'S'
17972 : (INTEGERP (glyph->object)
17973 ? '0'
17974 : '-'))),
17975 glyph->pixel_width,
17976 glyph->u.cmp.id);
17977 if (glyph->u.cmp.automatic)
17978 fprintf (stderr,
17979 "[%d-%d]",
17980 glyph->slice.cmp.from, glyph->slice.cmp.to);
17981 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17982 glyph->face_id,
17983 glyph->left_box_line_p,
17984 glyph->right_box_line_p);
17989 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17990 GLYPHS 0 means don't show glyph contents.
17991 GLYPHS 1 means show glyphs in short form
17992 GLYPHS > 1 means show glyphs in long form. */
17994 void
17995 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17997 if (glyphs != 1)
17999 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18000 fprintf (stderr, "==============================================================================\n");
18002 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18003 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18004 vpos,
18005 MATRIX_ROW_START_CHARPOS (row),
18006 MATRIX_ROW_END_CHARPOS (row),
18007 row->used[TEXT_AREA],
18008 row->contains_overlapping_glyphs_p,
18009 row->enabled_p,
18010 row->truncated_on_left_p,
18011 row->truncated_on_right_p,
18012 row->continued_p,
18013 MATRIX_ROW_CONTINUATION_LINE_P (row),
18014 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18015 row->ends_at_zv_p,
18016 row->fill_line_p,
18017 row->ends_in_middle_of_char_p,
18018 row->starts_in_middle_of_char_p,
18019 row->mouse_face_p,
18020 row->x,
18021 row->y,
18022 row->pixel_width,
18023 row->height,
18024 row->visible_height,
18025 row->ascent,
18026 row->phys_ascent);
18027 /* The next 3 lines should align to "Start" in the header. */
18028 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18029 row->end.overlay_string_index,
18030 row->continuation_lines_width);
18031 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18032 CHARPOS (row->start.string_pos),
18033 CHARPOS (row->end.string_pos));
18034 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18035 row->end.dpvec_index);
18038 if (glyphs > 1)
18040 int area;
18042 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18044 struct glyph *glyph = row->glyphs[area];
18045 struct glyph *glyph_end = glyph + row->used[area];
18047 /* Glyph for a line end in text. */
18048 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18049 ++glyph_end;
18051 if (glyph < glyph_end)
18052 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18054 for (; glyph < glyph_end; ++glyph)
18055 dump_glyph (row, glyph, area);
18058 else if (glyphs == 1)
18060 int area;
18062 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18064 char *s = alloca (row->used[area] + 4);
18065 int i;
18067 for (i = 0; i < row->used[area]; ++i)
18069 struct glyph *glyph = row->glyphs[area] + i;
18070 if (i == row->used[area] - 1
18071 && area == TEXT_AREA
18072 && INTEGERP (glyph->object)
18073 && glyph->type == CHAR_GLYPH
18074 && glyph->u.ch == ' ')
18076 strcpy (&s[i], "[\\n]");
18077 i += 4;
18079 else if (glyph->type == CHAR_GLYPH
18080 && glyph->u.ch < 0x80
18081 && glyph->u.ch >= ' ')
18082 s[i] = glyph->u.ch;
18083 else
18084 s[i] = '.';
18087 s[i] = '\0';
18088 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18094 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18095 Sdump_glyph_matrix, 0, 1, "p",
18096 doc: /* Dump the current matrix of the selected window to stderr.
18097 Shows contents of glyph row structures. With non-nil
18098 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18099 glyphs in short form, otherwise show glyphs in long form. */)
18100 (Lisp_Object glyphs)
18102 struct window *w = XWINDOW (selected_window);
18103 struct buffer *buffer = XBUFFER (w->contents);
18105 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18106 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18107 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18108 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18109 fprintf (stderr, "=============================================\n");
18110 dump_glyph_matrix (w->current_matrix,
18111 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18112 return Qnil;
18116 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18117 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18118 (void)
18120 struct frame *f = XFRAME (selected_frame);
18121 dump_glyph_matrix (f->current_matrix, 1);
18122 return Qnil;
18126 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18127 doc: /* Dump glyph row ROW to stderr.
18128 GLYPH 0 means don't dump glyphs.
18129 GLYPH 1 means dump glyphs in short form.
18130 GLYPH > 1 or omitted means dump glyphs in long form. */)
18131 (Lisp_Object row, Lisp_Object glyphs)
18133 struct glyph_matrix *matrix;
18134 EMACS_INT vpos;
18136 CHECK_NUMBER (row);
18137 matrix = XWINDOW (selected_window)->current_matrix;
18138 vpos = XINT (row);
18139 if (vpos >= 0 && vpos < matrix->nrows)
18140 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18141 vpos,
18142 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18143 return Qnil;
18147 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18148 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18149 GLYPH 0 means don't dump glyphs.
18150 GLYPH 1 means dump glyphs in short form.
18151 GLYPH > 1 or omitted means dump glyphs in long form. */)
18152 (Lisp_Object row, Lisp_Object glyphs)
18154 struct frame *sf = SELECTED_FRAME ();
18155 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18156 EMACS_INT vpos;
18158 CHECK_NUMBER (row);
18159 vpos = XINT (row);
18160 if (vpos >= 0 && vpos < m->nrows)
18161 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18162 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18163 return Qnil;
18167 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18168 doc: /* Toggle tracing of redisplay.
18169 With ARG, turn tracing on if and only if ARG is positive. */)
18170 (Lisp_Object arg)
18172 if (NILP (arg))
18173 trace_redisplay_p = !trace_redisplay_p;
18174 else
18176 arg = Fprefix_numeric_value (arg);
18177 trace_redisplay_p = XINT (arg) > 0;
18180 return Qnil;
18184 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18185 doc: /* Like `format', but print result to stderr.
18186 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18187 (ptrdiff_t nargs, Lisp_Object *args)
18189 Lisp_Object s = Fformat (nargs, args);
18190 fprintf (stderr, "%s", SDATA (s));
18191 return Qnil;
18194 #endif /* GLYPH_DEBUG */
18198 /***********************************************************************
18199 Building Desired Matrix Rows
18200 ***********************************************************************/
18202 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18203 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18205 static struct glyph_row *
18206 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18208 struct frame *f = XFRAME (WINDOW_FRAME (w));
18209 struct buffer *buffer = XBUFFER (w->contents);
18210 struct buffer *old = current_buffer;
18211 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18212 int arrow_len = SCHARS (overlay_arrow_string);
18213 const unsigned char *arrow_end = arrow_string + arrow_len;
18214 const unsigned char *p;
18215 struct it it;
18216 bool multibyte_p;
18217 int n_glyphs_before;
18219 set_buffer_temp (buffer);
18220 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18221 it.glyph_row->used[TEXT_AREA] = 0;
18222 SET_TEXT_POS (it.position, 0, 0);
18224 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18225 p = arrow_string;
18226 while (p < arrow_end)
18228 Lisp_Object face, ilisp;
18230 /* Get the next character. */
18231 if (multibyte_p)
18232 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18233 else
18235 it.c = it.char_to_display = *p, it.len = 1;
18236 if (! ASCII_CHAR_P (it.c))
18237 it.char_to_display = BYTE8_TO_CHAR (it.c);
18239 p += it.len;
18241 /* Get its face. */
18242 ilisp = make_number (p - arrow_string);
18243 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18244 it.face_id = compute_char_face (f, it.char_to_display, face);
18246 /* Compute its width, get its glyphs. */
18247 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18248 SET_TEXT_POS (it.position, -1, -1);
18249 PRODUCE_GLYPHS (&it);
18251 /* If this character doesn't fit any more in the line, we have
18252 to remove some glyphs. */
18253 if (it.current_x > it.last_visible_x)
18255 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18256 break;
18260 set_buffer_temp (old);
18261 return it.glyph_row;
18265 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18266 glyphs to insert is determined by produce_special_glyphs. */
18268 static void
18269 insert_left_trunc_glyphs (struct it *it)
18271 struct it truncate_it;
18272 struct glyph *from, *end, *to, *toend;
18274 eassert (!FRAME_WINDOW_P (it->f)
18275 || (!it->glyph_row->reversed_p
18276 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18277 || (it->glyph_row->reversed_p
18278 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18280 /* Get the truncation glyphs. */
18281 truncate_it = *it;
18282 truncate_it.current_x = 0;
18283 truncate_it.face_id = DEFAULT_FACE_ID;
18284 truncate_it.glyph_row = &scratch_glyph_row;
18285 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18286 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18287 truncate_it.object = make_number (0);
18288 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18290 /* Overwrite glyphs from IT with truncation glyphs. */
18291 if (!it->glyph_row->reversed_p)
18293 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18295 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18296 end = from + tused;
18297 to = it->glyph_row->glyphs[TEXT_AREA];
18298 toend = to + it->glyph_row->used[TEXT_AREA];
18299 if (FRAME_WINDOW_P (it->f))
18301 /* On GUI frames, when variable-size fonts are displayed,
18302 the truncation glyphs may need more pixels than the row's
18303 glyphs they overwrite. We overwrite more glyphs to free
18304 enough screen real estate, and enlarge the stretch glyph
18305 on the right (see display_line), if there is one, to
18306 preserve the screen position of the truncation glyphs on
18307 the right. */
18308 int w = 0;
18309 struct glyph *g = to;
18310 short used;
18312 /* The first glyph could be partially visible, in which case
18313 it->glyph_row->x will be negative. But we want the left
18314 truncation glyphs to be aligned at the left margin of the
18315 window, so we override the x coordinate at which the row
18316 will begin. */
18317 it->glyph_row->x = 0;
18318 while (g < toend && w < it->truncation_pixel_width)
18320 w += g->pixel_width;
18321 ++g;
18323 if (g - to - tused > 0)
18325 memmove (to + tused, g, (toend - g) * sizeof(*g));
18326 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18328 used = it->glyph_row->used[TEXT_AREA];
18329 if (it->glyph_row->truncated_on_right_p
18330 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18331 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18332 == STRETCH_GLYPH)
18334 int extra = w - it->truncation_pixel_width;
18336 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18340 while (from < end)
18341 *to++ = *from++;
18343 /* There may be padding glyphs left over. Overwrite them too. */
18344 if (!FRAME_WINDOW_P (it->f))
18346 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18348 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18349 while (from < end)
18350 *to++ = *from++;
18354 if (to > toend)
18355 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18357 else
18359 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18361 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18362 that back to front. */
18363 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18364 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18365 toend = it->glyph_row->glyphs[TEXT_AREA];
18366 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18367 if (FRAME_WINDOW_P (it->f))
18369 int w = 0;
18370 struct glyph *g = to;
18372 while (g >= toend && w < it->truncation_pixel_width)
18374 w += g->pixel_width;
18375 --g;
18377 if (to - g - tused > 0)
18378 to = g + tused;
18379 if (it->glyph_row->truncated_on_right_p
18380 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18381 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18383 int extra = w - it->truncation_pixel_width;
18385 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18389 while (from >= end && to >= toend)
18390 *to-- = *from--;
18391 if (!FRAME_WINDOW_P (it->f))
18393 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18395 from =
18396 truncate_it.glyph_row->glyphs[TEXT_AREA]
18397 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18398 while (from >= end && to >= toend)
18399 *to-- = *from--;
18402 if (from >= end)
18404 /* Need to free some room before prepending additional
18405 glyphs. */
18406 int move_by = from - end + 1;
18407 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18408 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18410 for ( ; g >= g0; g--)
18411 g[move_by] = *g;
18412 while (from >= end)
18413 *to-- = *from--;
18414 it->glyph_row->used[TEXT_AREA] += move_by;
18419 /* Compute the hash code for ROW. */
18420 unsigned
18421 row_hash (struct glyph_row *row)
18423 int area, k;
18424 unsigned hashval = 0;
18426 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18427 for (k = 0; k < row->used[area]; ++k)
18428 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18429 + row->glyphs[area][k].u.val
18430 + row->glyphs[area][k].face_id
18431 + row->glyphs[area][k].padding_p
18432 + (row->glyphs[area][k].type << 2));
18434 return hashval;
18437 /* Compute the pixel height and width of IT->glyph_row.
18439 Most of the time, ascent and height of a display line will be equal
18440 to the max_ascent and max_height values of the display iterator
18441 structure. This is not the case if
18443 1. We hit ZV without displaying anything. In this case, max_ascent
18444 and max_height will be zero.
18446 2. We have some glyphs that don't contribute to the line height.
18447 (The glyph row flag contributes_to_line_height_p is for future
18448 pixmap extensions).
18450 The first case is easily covered by using default values because in
18451 these cases, the line height does not really matter, except that it
18452 must not be zero. */
18454 static void
18455 compute_line_metrics (struct it *it)
18457 struct glyph_row *row = it->glyph_row;
18459 if (FRAME_WINDOW_P (it->f))
18461 int i, min_y, max_y;
18463 /* The line may consist of one space only, that was added to
18464 place the cursor on it. If so, the row's height hasn't been
18465 computed yet. */
18466 if (row->height == 0)
18468 if (it->max_ascent + it->max_descent == 0)
18469 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18470 row->ascent = it->max_ascent;
18471 row->height = it->max_ascent + it->max_descent;
18472 row->phys_ascent = it->max_phys_ascent;
18473 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18474 row->extra_line_spacing = it->max_extra_line_spacing;
18477 /* Compute the width of this line. */
18478 row->pixel_width = row->x;
18479 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18480 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18482 eassert (row->pixel_width >= 0);
18483 eassert (row->ascent >= 0 && row->height > 0);
18485 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18486 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18488 /* If first line's physical ascent is larger than its logical
18489 ascent, use the physical ascent, and make the row taller.
18490 This makes accented characters fully visible. */
18491 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18492 && row->phys_ascent > row->ascent)
18494 row->height += row->phys_ascent - row->ascent;
18495 row->ascent = row->phys_ascent;
18498 /* Compute how much of the line is visible. */
18499 row->visible_height = row->height;
18501 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18502 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18504 if (row->y < min_y)
18505 row->visible_height -= min_y - row->y;
18506 if (row->y + row->height > max_y)
18507 row->visible_height -= row->y + row->height - max_y;
18509 else
18511 row->pixel_width = row->used[TEXT_AREA];
18512 if (row->continued_p)
18513 row->pixel_width -= it->continuation_pixel_width;
18514 else if (row->truncated_on_right_p)
18515 row->pixel_width -= it->truncation_pixel_width;
18516 row->ascent = row->phys_ascent = 0;
18517 row->height = row->phys_height = row->visible_height = 1;
18518 row->extra_line_spacing = 0;
18521 /* Compute a hash code for this row. */
18522 row->hash = row_hash (row);
18524 it->max_ascent = it->max_descent = 0;
18525 it->max_phys_ascent = it->max_phys_descent = 0;
18529 /* Append one space to the glyph row of iterator IT if doing a
18530 window-based redisplay. The space has the same face as
18531 IT->face_id. Value is non-zero if a space was added.
18533 This function is called to make sure that there is always one glyph
18534 at the end of a glyph row that the cursor can be set on under
18535 window-systems. (If there weren't such a glyph we would not know
18536 how wide and tall a box cursor should be displayed).
18538 At the same time this space let's a nicely handle clearing to the
18539 end of the line if the row ends in italic text. */
18541 static int
18542 append_space_for_newline (struct it *it, int default_face_p)
18544 if (FRAME_WINDOW_P (it->f))
18546 int n = it->glyph_row->used[TEXT_AREA];
18548 if (it->glyph_row->glyphs[TEXT_AREA] + n
18549 < it->glyph_row->glyphs[1 + TEXT_AREA])
18551 /* Save some values that must not be changed.
18552 Must save IT->c and IT->len because otherwise
18553 ITERATOR_AT_END_P wouldn't work anymore after
18554 append_space_for_newline has been called. */
18555 enum display_element_type saved_what = it->what;
18556 int saved_c = it->c, saved_len = it->len;
18557 int saved_char_to_display = it->char_to_display;
18558 int saved_x = it->current_x;
18559 int saved_face_id = it->face_id;
18560 int saved_box_end = it->end_of_box_run_p;
18561 struct text_pos saved_pos;
18562 Lisp_Object saved_object;
18563 struct face *face;
18565 saved_object = it->object;
18566 saved_pos = it->position;
18568 it->what = IT_CHARACTER;
18569 memset (&it->position, 0, sizeof it->position);
18570 it->object = make_number (0);
18571 it->c = it->char_to_display = ' ';
18572 it->len = 1;
18574 /* If the default face was remapped, be sure to use the
18575 remapped face for the appended newline. */
18576 if (default_face_p)
18577 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18578 else if (it->face_before_selective_p)
18579 it->face_id = it->saved_face_id;
18580 face = FACE_FROM_ID (it->f, it->face_id);
18581 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18582 /* In R2L rows, we will prepend a stretch glyph that will
18583 have the end_of_box_run_p flag set for it, so there's no
18584 need for the appended newline glyph to have that flag
18585 set. */
18586 if (it->glyph_row->reversed_p
18587 /* But if the appended newline glyph goes all the way to
18588 the end of the row, there will be no stretch glyph,
18589 so leave the box flag set. */
18590 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18591 it->end_of_box_run_p = 0;
18593 PRODUCE_GLYPHS (it);
18595 it->override_ascent = -1;
18596 it->constrain_row_ascent_descent_p = 0;
18597 it->current_x = saved_x;
18598 it->object = saved_object;
18599 it->position = saved_pos;
18600 it->what = saved_what;
18601 it->face_id = saved_face_id;
18602 it->len = saved_len;
18603 it->c = saved_c;
18604 it->char_to_display = saved_char_to_display;
18605 it->end_of_box_run_p = saved_box_end;
18606 return 1;
18610 return 0;
18614 /* Extend the face of the last glyph in the text area of IT->glyph_row
18615 to the end of the display line. Called from display_line. If the
18616 glyph row is empty, add a space glyph to it so that we know the
18617 face to draw. Set the glyph row flag fill_line_p. If the glyph
18618 row is R2L, prepend a stretch glyph to cover the empty space to the
18619 left of the leftmost glyph. */
18621 static void
18622 extend_face_to_end_of_line (struct it *it)
18624 struct face *face, *default_face;
18625 struct frame *f = it->f;
18627 /* If line is already filled, do nothing. Non window-system frames
18628 get a grace of one more ``pixel'' because their characters are
18629 1-``pixel'' wide, so they hit the equality too early. This grace
18630 is needed only for R2L rows that are not continued, to produce
18631 one extra blank where we could display the cursor. */
18632 if (it->current_x >= it->last_visible_x
18633 + (!FRAME_WINDOW_P (f)
18634 && it->glyph_row->reversed_p
18635 && !it->glyph_row->continued_p))
18636 return;
18638 /* The default face, possibly remapped. */
18639 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18641 /* Face extension extends the background and box of IT->face_id
18642 to the end of the line. If the background equals the background
18643 of the frame, we don't have to do anything. */
18644 if (it->face_before_selective_p)
18645 face = FACE_FROM_ID (f, it->saved_face_id);
18646 else
18647 face = FACE_FROM_ID (f, it->face_id);
18649 if (FRAME_WINDOW_P (f)
18650 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18651 && face->box == FACE_NO_BOX
18652 && face->background == FRAME_BACKGROUND_PIXEL (f)
18653 && !face->stipple
18654 && !it->glyph_row->reversed_p)
18655 return;
18657 /* Set the glyph row flag indicating that the face of the last glyph
18658 in the text area has to be drawn to the end of the text area. */
18659 it->glyph_row->fill_line_p = 1;
18661 /* If current character of IT is not ASCII, make sure we have the
18662 ASCII face. This will be automatically undone the next time
18663 get_next_display_element returns a multibyte character. Note
18664 that the character will always be single byte in unibyte
18665 text. */
18666 if (!ASCII_CHAR_P (it->c))
18668 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18671 if (FRAME_WINDOW_P (f))
18673 /* If the row is empty, add a space with the current face of IT,
18674 so that we know which face to draw. */
18675 if (it->glyph_row->used[TEXT_AREA] == 0)
18677 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18678 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18679 it->glyph_row->used[TEXT_AREA] = 1;
18681 #ifdef HAVE_WINDOW_SYSTEM
18682 if (it->glyph_row->reversed_p)
18684 /* Prepend a stretch glyph to the row, such that the
18685 rightmost glyph will be drawn flushed all the way to the
18686 right margin of the window. The stretch glyph that will
18687 occupy the empty space, if any, to the left of the
18688 glyphs. */
18689 struct font *font = face->font ? face->font : FRAME_FONT (f);
18690 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18691 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18692 struct glyph *g;
18693 int row_width, stretch_ascent, stretch_width;
18694 struct text_pos saved_pos;
18695 int saved_face_id, saved_avoid_cursor, saved_box_start;
18697 for (row_width = 0, g = row_start; g < row_end; g++)
18698 row_width += g->pixel_width;
18699 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18700 if (stretch_width > 0)
18702 stretch_ascent =
18703 (((it->ascent + it->descent)
18704 * FONT_BASE (font)) / FONT_HEIGHT (font));
18705 saved_pos = it->position;
18706 memset (&it->position, 0, sizeof it->position);
18707 saved_avoid_cursor = it->avoid_cursor_p;
18708 it->avoid_cursor_p = 1;
18709 saved_face_id = it->face_id;
18710 saved_box_start = it->start_of_box_run_p;
18711 /* The last row's stretch glyph should get the default
18712 face, to avoid painting the rest of the window with
18713 the region face, if the region ends at ZV. */
18714 if (it->glyph_row->ends_at_zv_p)
18715 it->face_id = default_face->id;
18716 else
18717 it->face_id = face->id;
18718 it->start_of_box_run_p = 0;
18719 append_stretch_glyph (it, make_number (0), stretch_width,
18720 it->ascent + it->descent, stretch_ascent);
18721 it->position = saved_pos;
18722 it->avoid_cursor_p = saved_avoid_cursor;
18723 it->face_id = saved_face_id;
18724 it->start_of_box_run_p = saved_box_start;
18727 #endif /* HAVE_WINDOW_SYSTEM */
18729 else
18731 /* Save some values that must not be changed. */
18732 int saved_x = it->current_x;
18733 struct text_pos saved_pos;
18734 Lisp_Object saved_object;
18735 enum display_element_type saved_what = it->what;
18736 int saved_face_id = it->face_id;
18738 saved_object = it->object;
18739 saved_pos = it->position;
18741 it->what = IT_CHARACTER;
18742 memset (&it->position, 0, sizeof it->position);
18743 it->object = make_number (0);
18744 it->c = it->char_to_display = ' ';
18745 it->len = 1;
18746 /* The last row's blank glyphs should get the default face, to
18747 avoid painting the rest of the window with the region face,
18748 if the region ends at ZV. */
18749 if (it->glyph_row->ends_at_zv_p)
18750 it->face_id = default_face->id;
18751 else
18752 it->face_id = face->id;
18754 PRODUCE_GLYPHS (it);
18756 while (it->current_x <= it->last_visible_x)
18757 PRODUCE_GLYPHS (it);
18759 /* Don't count these blanks really. It would let us insert a left
18760 truncation glyph below and make us set the cursor on them, maybe. */
18761 it->current_x = saved_x;
18762 it->object = saved_object;
18763 it->position = saved_pos;
18764 it->what = saved_what;
18765 it->face_id = saved_face_id;
18770 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18771 trailing whitespace. */
18773 static int
18774 trailing_whitespace_p (ptrdiff_t charpos)
18776 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18777 int c = 0;
18779 while (bytepos < ZV_BYTE
18780 && (c = FETCH_CHAR (bytepos),
18781 c == ' ' || c == '\t'))
18782 ++bytepos;
18784 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18786 if (bytepos != PT_BYTE)
18787 return 1;
18789 return 0;
18793 /* Highlight trailing whitespace, if any, in ROW. */
18795 static void
18796 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18798 int used = row->used[TEXT_AREA];
18800 if (used)
18802 struct glyph *start = row->glyphs[TEXT_AREA];
18803 struct glyph *glyph = start + used - 1;
18805 if (row->reversed_p)
18807 /* Right-to-left rows need to be processed in the opposite
18808 direction, so swap the edge pointers. */
18809 glyph = start;
18810 start = row->glyphs[TEXT_AREA] + used - 1;
18813 /* Skip over glyphs inserted to display the cursor at the
18814 end of a line, for extending the face of the last glyph
18815 to the end of the line on terminals, and for truncation
18816 and continuation glyphs. */
18817 if (!row->reversed_p)
18819 while (glyph >= start
18820 && glyph->type == CHAR_GLYPH
18821 && INTEGERP (glyph->object))
18822 --glyph;
18824 else
18826 while (glyph <= start
18827 && glyph->type == CHAR_GLYPH
18828 && INTEGERP (glyph->object))
18829 ++glyph;
18832 /* If last glyph is a space or stretch, and it's trailing
18833 whitespace, set the face of all trailing whitespace glyphs in
18834 IT->glyph_row to `trailing-whitespace'. */
18835 if ((row->reversed_p ? glyph <= start : glyph >= start)
18836 && BUFFERP (glyph->object)
18837 && (glyph->type == STRETCH_GLYPH
18838 || (glyph->type == CHAR_GLYPH
18839 && glyph->u.ch == ' '))
18840 && trailing_whitespace_p (glyph->charpos))
18842 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18843 if (face_id < 0)
18844 return;
18846 if (!row->reversed_p)
18848 while (glyph >= start
18849 && BUFFERP (glyph->object)
18850 && (glyph->type == STRETCH_GLYPH
18851 || (glyph->type == CHAR_GLYPH
18852 && glyph->u.ch == ' ')))
18853 (glyph--)->face_id = face_id;
18855 else
18857 while (glyph <= start
18858 && BUFFERP (glyph->object)
18859 && (glyph->type == STRETCH_GLYPH
18860 || (glyph->type == CHAR_GLYPH
18861 && glyph->u.ch == ' ')))
18862 (glyph++)->face_id = face_id;
18869 /* Value is non-zero if glyph row ROW should be
18870 considered to hold the buffer position CHARPOS. */
18872 static int
18873 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18875 int result = 1;
18877 if (charpos == CHARPOS (row->end.pos)
18878 || charpos == MATRIX_ROW_END_CHARPOS (row))
18880 /* Suppose the row ends on a string.
18881 Unless the row is continued, that means it ends on a newline
18882 in the string. If it's anything other than a display string
18883 (e.g., a before-string from an overlay), we don't want the
18884 cursor there. (This heuristic seems to give the optimal
18885 behavior for the various types of multi-line strings.)
18886 One exception: if the string has `cursor' property on one of
18887 its characters, we _do_ want the cursor there. */
18888 if (CHARPOS (row->end.string_pos) >= 0)
18890 if (row->continued_p)
18891 result = 1;
18892 else
18894 /* Check for `display' property. */
18895 struct glyph *beg = row->glyphs[TEXT_AREA];
18896 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18897 struct glyph *glyph;
18899 result = 0;
18900 for (glyph = end; glyph >= beg; --glyph)
18901 if (STRINGP (glyph->object))
18903 Lisp_Object prop
18904 = Fget_char_property (make_number (charpos),
18905 Qdisplay, Qnil);
18906 result =
18907 (!NILP (prop)
18908 && display_prop_string_p (prop, glyph->object));
18909 /* If there's a `cursor' property on one of the
18910 string's characters, this row is a cursor row,
18911 even though this is not a display string. */
18912 if (!result)
18914 Lisp_Object s = glyph->object;
18916 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18918 ptrdiff_t gpos = glyph->charpos;
18920 if (!NILP (Fget_char_property (make_number (gpos),
18921 Qcursor, s)))
18923 result = 1;
18924 break;
18928 break;
18932 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18934 /* If the row ends in middle of a real character,
18935 and the line is continued, we want the cursor here.
18936 That's because CHARPOS (ROW->end.pos) would equal
18937 PT if PT is before the character. */
18938 if (!row->ends_in_ellipsis_p)
18939 result = row->continued_p;
18940 else
18941 /* If the row ends in an ellipsis, then
18942 CHARPOS (ROW->end.pos) will equal point after the
18943 invisible text. We want that position to be displayed
18944 after the ellipsis. */
18945 result = 0;
18947 /* If the row ends at ZV, display the cursor at the end of that
18948 row instead of at the start of the row below. */
18949 else if (row->ends_at_zv_p)
18950 result = 1;
18951 else
18952 result = 0;
18955 return result;
18958 /* Value is non-zero if glyph row ROW should be
18959 used to hold the cursor. */
18961 static int
18962 cursor_row_p (struct glyph_row *row)
18964 return row_for_charpos_p (row, PT);
18969 /* Push the property PROP so that it will be rendered at the current
18970 position in IT. Return 1 if PROP was successfully pushed, 0
18971 otherwise. Called from handle_line_prefix to handle the
18972 `line-prefix' and `wrap-prefix' properties. */
18974 static int
18975 push_prefix_prop (struct it *it, Lisp_Object prop)
18977 struct text_pos pos =
18978 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18980 eassert (it->method == GET_FROM_BUFFER
18981 || it->method == GET_FROM_DISPLAY_VECTOR
18982 || it->method == GET_FROM_STRING);
18984 /* We need to save the current buffer/string position, so it will be
18985 restored by pop_it, because iterate_out_of_display_property
18986 depends on that being set correctly, but some situations leave
18987 it->position not yet set when this function is called. */
18988 push_it (it, &pos);
18990 if (STRINGP (prop))
18992 if (SCHARS (prop) == 0)
18994 pop_it (it);
18995 return 0;
18998 it->string = prop;
18999 it->string_from_prefix_prop_p = 1;
19000 it->multibyte_p = STRING_MULTIBYTE (it->string);
19001 it->current.overlay_string_index = -1;
19002 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19003 it->end_charpos = it->string_nchars = SCHARS (it->string);
19004 it->method = GET_FROM_STRING;
19005 it->stop_charpos = 0;
19006 it->prev_stop = 0;
19007 it->base_level_stop = 0;
19009 /* Force paragraph direction to be that of the parent
19010 buffer/string. */
19011 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19012 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19013 else
19014 it->paragraph_embedding = L2R;
19016 /* Set up the bidi iterator for this display string. */
19017 if (it->bidi_p)
19019 it->bidi_it.string.lstring = it->string;
19020 it->bidi_it.string.s = NULL;
19021 it->bidi_it.string.schars = it->end_charpos;
19022 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19023 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19024 it->bidi_it.string.unibyte = !it->multibyte_p;
19025 it->bidi_it.w = it->w;
19026 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19029 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19031 it->method = GET_FROM_STRETCH;
19032 it->object = prop;
19034 #ifdef HAVE_WINDOW_SYSTEM
19035 else if (IMAGEP (prop))
19037 it->what = IT_IMAGE;
19038 it->image_id = lookup_image (it->f, prop);
19039 it->method = GET_FROM_IMAGE;
19041 #endif /* HAVE_WINDOW_SYSTEM */
19042 else
19044 pop_it (it); /* bogus display property, give up */
19045 return 0;
19048 return 1;
19051 /* Return the character-property PROP at the current position in IT. */
19053 static Lisp_Object
19054 get_it_property (struct it *it, Lisp_Object prop)
19056 Lisp_Object position, object = it->object;
19058 if (STRINGP (object))
19059 position = make_number (IT_STRING_CHARPOS (*it));
19060 else if (BUFFERP (object))
19062 position = make_number (IT_CHARPOS (*it));
19063 object = it->window;
19065 else
19066 return Qnil;
19068 return Fget_char_property (position, prop, object);
19071 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19073 static void
19074 handle_line_prefix (struct it *it)
19076 Lisp_Object prefix;
19078 if (it->continuation_lines_width > 0)
19080 prefix = get_it_property (it, Qwrap_prefix);
19081 if (NILP (prefix))
19082 prefix = Vwrap_prefix;
19084 else
19086 prefix = get_it_property (it, Qline_prefix);
19087 if (NILP (prefix))
19088 prefix = Vline_prefix;
19090 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19092 /* If the prefix is wider than the window, and we try to wrap
19093 it, it would acquire its own wrap prefix, and so on till the
19094 iterator stack overflows. So, don't wrap the prefix. */
19095 it->line_wrap = TRUNCATE;
19096 it->avoid_cursor_p = 1;
19102 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19103 only for R2L lines from display_line and display_string, when they
19104 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19105 the line/string needs to be continued on the next glyph row. */
19106 static void
19107 unproduce_glyphs (struct it *it, int n)
19109 struct glyph *glyph, *end;
19111 eassert (it->glyph_row);
19112 eassert (it->glyph_row->reversed_p);
19113 eassert (it->area == TEXT_AREA);
19114 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19116 if (n > it->glyph_row->used[TEXT_AREA])
19117 n = it->glyph_row->used[TEXT_AREA];
19118 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19119 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19120 for ( ; glyph < end; glyph++)
19121 glyph[-n] = *glyph;
19124 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19125 and ROW->maxpos. */
19126 static void
19127 find_row_edges (struct it *it, struct glyph_row *row,
19128 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19129 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19131 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19132 lines' rows is implemented for bidi-reordered rows. */
19134 /* ROW->minpos is the value of min_pos, the minimal buffer position
19135 we have in ROW, or ROW->start.pos if that is smaller. */
19136 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19137 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19138 else
19139 /* We didn't find buffer positions smaller than ROW->start, or
19140 didn't find _any_ valid buffer positions in any of the glyphs,
19141 so we must trust the iterator's computed positions. */
19142 row->minpos = row->start.pos;
19143 if (max_pos <= 0)
19145 max_pos = CHARPOS (it->current.pos);
19146 max_bpos = BYTEPOS (it->current.pos);
19149 /* Here are the various use-cases for ending the row, and the
19150 corresponding values for ROW->maxpos:
19152 Line ends in a newline from buffer eol_pos + 1
19153 Line is continued from buffer max_pos + 1
19154 Line is truncated on right it->current.pos
19155 Line ends in a newline from string max_pos + 1(*)
19156 (*) + 1 only when line ends in a forward scan
19157 Line is continued from string max_pos
19158 Line is continued from display vector max_pos
19159 Line is entirely from a string min_pos == max_pos
19160 Line is entirely from a display vector min_pos == max_pos
19161 Line that ends at ZV ZV
19163 If you discover other use-cases, please add them here as
19164 appropriate. */
19165 if (row->ends_at_zv_p)
19166 row->maxpos = it->current.pos;
19167 else if (row->used[TEXT_AREA])
19169 int seen_this_string = 0;
19170 struct glyph_row *r1 = row - 1;
19172 /* Did we see the same display string on the previous row? */
19173 if (STRINGP (it->object)
19174 /* this is not the first row */
19175 && row > it->w->desired_matrix->rows
19176 /* previous row is not the header line */
19177 && !r1->mode_line_p
19178 /* previous row also ends in a newline from a string */
19179 && r1->ends_in_newline_from_string_p)
19181 struct glyph *start, *end;
19183 /* Search for the last glyph of the previous row that came
19184 from buffer or string. Depending on whether the row is
19185 L2R or R2L, we need to process it front to back or the
19186 other way round. */
19187 if (!r1->reversed_p)
19189 start = r1->glyphs[TEXT_AREA];
19190 end = start + r1->used[TEXT_AREA];
19191 /* Glyphs inserted by redisplay have an integer (zero)
19192 as their object. */
19193 while (end > start
19194 && INTEGERP ((end - 1)->object)
19195 && (end - 1)->charpos <= 0)
19196 --end;
19197 if (end > start)
19199 if (EQ ((end - 1)->object, it->object))
19200 seen_this_string = 1;
19202 else
19203 /* If all the glyphs of the previous row were inserted
19204 by redisplay, it means the previous row was
19205 produced from a single newline, which is only
19206 possible if that newline came from the same string
19207 as the one which produced this ROW. */
19208 seen_this_string = 1;
19210 else
19212 end = r1->glyphs[TEXT_AREA] - 1;
19213 start = end + r1->used[TEXT_AREA];
19214 while (end < start
19215 && INTEGERP ((end + 1)->object)
19216 && (end + 1)->charpos <= 0)
19217 ++end;
19218 if (end < start)
19220 if (EQ ((end + 1)->object, it->object))
19221 seen_this_string = 1;
19223 else
19224 seen_this_string = 1;
19227 /* Take note of each display string that covers a newline only
19228 once, the first time we see it. This is for when a display
19229 string includes more than one newline in it. */
19230 if (row->ends_in_newline_from_string_p && !seen_this_string)
19232 /* If we were scanning the buffer forward when we displayed
19233 the string, we want to account for at least one buffer
19234 position that belongs to this row (position covered by
19235 the display string), so that cursor positioning will
19236 consider this row as a candidate when point is at the end
19237 of the visual line represented by this row. This is not
19238 required when scanning back, because max_pos will already
19239 have a much larger value. */
19240 if (CHARPOS (row->end.pos) > max_pos)
19241 INC_BOTH (max_pos, max_bpos);
19242 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19244 else if (CHARPOS (it->eol_pos) > 0)
19245 SET_TEXT_POS (row->maxpos,
19246 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19247 else if (row->continued_p)
19249 /* If max_pos is different from IT's current position, it
19250 means IT->method does not belong to the display element
19251 at max_pos. However, it also means that the display
19252 element at max_pos was displayed in its entirety on this
19253 line, which is equivalent to saying that the next line
19254 starts at the next buffer position. */
19255 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19256 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19257 else
19259 INC_BOTH (max_pos, max_bpos);
19260 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19263 else if (row->truncated_on_right_p)
19264 /* display_line already called reseat_at_next_visible_line_start,
19265 which puts the iterator at the beginning of the next line, in
19266 the logical order. */
19267 row->maxpos = it->current.pos;
19268 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19269 /* A line that is entirely from a string/image/stretch... */
19270 row->maxpos = row->minpos;
19271 else
19272 emacs_abort ();
19274 else
19275 row->maxpos = it->current.pos;
19278 /* Construct the glyph row IT->glyph_row in the desired matrix of
19279 IT->w from text at the current position of IT. See dispextern.h
19280 for an overview of struct it. Value is non-zero if
19281 IT->glyph_row displays text, as opposed to a line displaying ZV
19282 only. */
19284 static int
19285 display_line (struct it *it)
19287 struct glyph_row *row = it->glyph_row;
19288 Lisp_Object overlay_arrow_string;
19289 struct it wrap_it;
19290 void *wrap_data = NULL;
19291 int may_wrap = 0, wrap_x IF_LINT (= 0);
19292 int wrap_row_used = -1;
19293 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19294 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19295 int wrap_row_extra_line_spacing IF_LINT (= 0);
19296 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19297 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19298 int cvpos;
19299 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19300 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19302 /* We always start displaying at hpos zero even if hscrolled. */
19303 eassert (it->hpos == 0 && it->current_x == 0);
19305 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19306 >= it->w->desired_matrix->nrows)
19308 it->w->nrows_scale_factor++;
19309 fonts_changed_p = 1;
19310 return 0;
19313 /* Is IT->w showing the region? */
19314 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19316 /* Clear the result glyph row and enable it. */
19317 prepare_desired_row (row);
19319 row->y = it->current_y;
19320 row->start = it->start;
19321 row->continuation_lines_width = it->continuation_lines_width;
19322 row->displays_text_p = 1;
19323 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19324 it->starts_in_middle_of_char_p = 0;
19326 /* Arrange the overlays nicely for our purposes. Usually, we call
19327 display_line on only one line at a time, in which case this
19328 can't really hurt too much, or we call it on lines which appear
19329 one after another in the buffer, in which case all calls to
19330 recenter_overlay_lists but the first will be pretty cheap. */
19331 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19333 /* Move over display elements that are not visible because we are
19334 hscrolled. This may stop at an x-position < IT->first_visible_x
19335 if the first glyph is partially visible or if we hit a line end. */
19336 if (it->current_x < it->first_visible_x)
19338 enum move_it_result move_result;
19340 this_line_min_pos = row->start.pos;
19341 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19342 MOVE_TO_POS | MOVE_TO_X);
19343 /* If we are under a large hscroll, move_it_in_display_line_to
19344 could hit the end of the line without reaching
19345 it->first_visible_x. Pretend that we did reach it. This is
19346 especially important on a TTY, where we will call
19347 extend_face_to_end_of_line, which needs to know how many
19348 blank glyphs to produce. */
19349 if (it->current_x < it->first_visible_x
19350 && (move_result == MOVE_NEWLINE_OR_CR
19351 || move_result == MOVE_POS_MATCH_OR_ZV))
19352 it->current_x = it->first_visible_x;
19354 /* Record the smallest positions seen while we moved over
19355 display elements that are not visible. This is needed by
19356 redisplay_internal for optimizing the case where the cursor
19357 stays inside the same line. The rest of this function only
19358 considers positions that are actually displayed, so
19359 RECORD_MAX_MIN_POS will not otherwise record positions that
19360 are hscrolled to the left of the left edge of the window. */
19361 min_pos = CHARPOS (this_line_min_pos);
19362 min_bpos = BYTEPOS (this_line_min_pos);
19364 else
19366 /* We only do this when not calling `move_it_in_display_line_to'
19367 above, because move_it_in_display_line_to calls
19368 handle_line_prefix itself. */
19369 handle_line_prefix (it);
19372 /* Get the initial row height. This is either the height of the
19373 text hscrolled, if there is any, or zero. */
19374 row->ascent = it->max_ascent;
19375 row->height = it->max_ascent + it->max_descent;
19376 row->phys_ascent = it->max_phys_ascent;
19377 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19378 row->extra_line_spacing = it->max_extra_line_spacing;
19380 /* Utility macro to record max and min buffer positions seen until now. */
19381 #define RECORD_MAX_MIN_POS(IT) \
19382 do \
19384 int composition_p = !STRINGP ((IT)->string) \
19385 && ((IT)->what == IT_COMPOSITION); \
19386 ptrdiff_t current_pos = \
19387 composition_p ? (IT)->cmp_it.charpos \
19388 : IT_CHARPOS (*(IT)); \
19389 ptrdiff_t current_bpos = \
19390 composition_p ? CHAR_TO_BYTE (current_pos) \
19391 : IT_BYTEPOS (*(IT)); \
19392 if (current_pos < min_pos) \
19394 min_pos = current_pos; \
19395 min_bpos = current_bpos; \
19397 if (IT_CHARPOS (*it) > max_pos) \
19399 max_pos = IT_CHARPOS (*it); \
19400 max_bpos = IT_BYTEPOS (*it); \
19403 while (0)
19405 /* Loop generating characters. The loop is left with IT on the next
19406 character to display. */
19407 while (1)
19409 int n_glyphs_before, hpos_before, x_before;
19410 int x, nglyphs;
19411 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19413 /* Retrieve the next thing to display. Value is zero if end of
19414 buffer reached. */
19415 if (!get_next_display_element (it))
19417 /* Maybe add a space at the end of this line that is used to
19418 display the cursor there under X. Set the charpos of the
19419 first glyph of blank lines not corresponding to any text
19420 to -1. */
19421 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19422 row->exact_window_width_line_p = 1;
19423 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19424 || row->used[TEXT_AREA] == 0)
19426 row->glyphs[TEXT_AREA]->charpos = -1;
19427 row->displays_text_p = 0;
19429 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19430 && (!MINI_WINDOW_P (it->w)
19431 || (minibuf_level && EQ (it->window, minibuf_window))))
19432 row->indicate_empty_line_p = 1;
19435 it->continuation_lines_width = 0;
19436 row->ends_at_zv_p = 1;
19437 /* A row that displays right-to-left text must always have
19438 its last face extended all the way to the end of line,
19439 even if this row ends in ZV, because we still write to
19440 the screen left to right. We also need to extend the
19441 last face if the default face is remapped to some
19442 different face, otherwise the functions that clear
19443 portions of the screen will clear with the default face's
19444 background color. */
19445 if (row->reversed_p
19446 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19447 extend_face_to_end_of_line (it);
19448 break;
19451 /* Now, get the metrics of what we want to display. This also
19452 generates glyphs in `row' (which is IT->glyph_row). */
19453 n_glyphs_before = row->used[TEXT_AREA];
19454 x = it->current_x;
19456 /* Remember the line height so far in case the next element doesn't
19457 fit on the line. */
19458 if (it->line_wrap != TRUNCATE)
19460 ascent = it->max_ascent;
19461 descent = it->max_descent;
19462 phys_ascent = it->max_phys_ascent;
19463 phys_descent = it->max_phys_descent;
19465 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19467 if (IT_DISPLAYING_WHITESPACE (it))
19468 may_wrap = 1;
19469 else if (may_wrap)
19471 SAVE_IT (wrap_it, *it, wrap_data);
19472 wrap_x = x;
19473 wrap_row_used = row->used[TEXT_AREA];
19474 wrap_row_ascent = row->ascent;
19475 wrap_row_height = row->height;
19476 wrap_row_phys_ascent = row->phys_ascent;
19477 wrap_row_phys_height = row->phys_height;
19478 wrap_row_extra_line_spacing = row->extra_line_spacing;
19479 wrap_row_min_pos = min_pos;
19480 wrap_row_min_bpos = min_bpos;
19481 wrap_row_max_pos = max_pos;
19482 wrap_row_max_bpos = max_bpos;
19483 may_wrap = 0;
19488 PRODUCE_GLYPHS (it);
19490 /* If this display element was in marginal areas, continue with
19491 the next one. */
19492 if (it->area != TEXT_AREA)
19494 row->ascent = max (row->ascent, it->max_ascent);
19495 row->height = max (row->height, it->max_ascent + it->max_descent);
19496 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19497 row->phys_height = max (row->phys_height,
19498 it->max_phys_ascent + it->max_phys_descent);
19499 row->extra_line_spacing = max (row->extra_line_spacing,
19500 it->max_extra_line_spacing);
19501 set_iterator_to_next (it, 1);
19502 continue;
19505 /* Does the display element fit on the line? If we truncate
19506 lines, we should draw past the right edge of the window. If
19507 we don't truncate, we want to stop so that we can display the
19508 continuation glyph before the right margin. If lines are
19509 continued, there are two possible strategies for characters
19510 resulting in more than 1 glyph (e.g. tabs): Display as many
19511 glyphs as possible in this line and leave the rest for the
19512 continuation line, or display the whole element in the next
19513 line. Original redisplay did the former, so we do it also. */
19514 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19515 hpos_before = it->hpos;
19516 x_before = x;
19518 if (/* Not a newline. */
19519 nglyphs > 0
19520 /* Glyphs produced fit entirely in the line. */
19521 && it->current_x < it->last_visible_x)
19523 it->hpos += nglyphs;
19524 row->ascent = max (row->ascent, it->max_ascent);
19525 row->height = max (row->height, it->max_ascent + it->max_descent);
19526 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19527 row->phys_height = max (row->phys_height,
19528 it->max_phys_ascent + it->max_phys_descent);
19529 row->extra_line_spacing = max (row->extra_line_spacing,
19530 it->max_extra_line_spacing);
19531 if (it->current_x - it->pixel_width < it->first_visible_x)
19532 row->x = x - it->first_visible_x;
19533 /* Record the maximum and minimum buffer positions seen so
19534 far in glyphs that will be displayed by this row. */
19535 if (it->bidi_p)
19536 RECORD_MAX_MIN_POS (it);
19538 else
19540 int i, new_x;
19541 struct glyph *glyph;
19543 for (i = 0; i < nglyphs; ++i, x = new_x)
19545 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19546 new_x = x + glyph->pixel_width;
19548 if (/* Lines are continued. */
19549 it->line_wrap != TRUNCATE
19550 && (/* Glyph doesn't fit on the line. */
19551 new_x > it->last_visible_x
19552 /* Or it fits exactly on a window system frame. */
19553 || (new_x == it->last_visible_x
19554 && FRAME_WINDOW_P (it->f)
19555 && (row->reversed_p
19556 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19557 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19559 /* End of a continued line. */
19561 if (it->hpos == 0
19562 || (new_x == it->last_visible_x
19563 && FRAME_WINDOW_P (it->f)
19564 && (row->reversed_p
19565 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19566 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19568 /* Current glyph is the only one on the line or
19569 fits exactly on the line. We must continue
19570 the line because we can't draw the cursor
19571 after the glyph. */
19572 row->continued_p = 1;
19573 it->current_x = new_x;
19574 it->continuation_lines_width += new_x;
19575 ++it->hpos;
19576 if (i == nglyphs - 1)
19578 /* If line-wrap is on, check if a previous
19579 wrap point was found. */
19580 if (wrap_row_used > 0
19581 /* Even if there is a previous wrap
19582 point, continue the line here as
19583 usual, if (i) the previous character
19584 was a space or tab AND (ii) the
19585 current character is not. */
19586 && (!may_wrap
19587 || IT_DISPLAYING_WHITESPACE (it)))
19588 goto back_to_wrap;
19590 /* Record the maximum and minimum buffer
19591 positions seen so far in glyphs that will be
19592 displayed by this row. */
19593 if (it->bidi_p)
19594 RECORD_MAX_MIN_POS (it);
19595 set_iterator_to_next (it, 1);
19596 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19598 if (!get_next_display_element (it))
19600 row->exact_window_width_line_p = 1;
19601 it->continuation_lines_width = 0;
19602 row->continued_p = 0;
19603 row->ends_at_zv_p = 1;
19605 else if (ITERATOR_AT_END_OF_LINE_P (it))
19607 row->continued_p = 0;
19608 row->exact_window_width_line_p = 1;
19612 else if (it->bidi_p)
19613 RECORD_MAX_MIN_POS (it);
19615 else if (CHAR_GLYPH_PADDING_P (*glyph)
19616 && !FRAME_WINDOW_P (it->f))
19618 /* A padding glyph that doesn't fit on this line.
19619 This means the whole character doesn't fit
19620 on the line. */
19621 if (row->reversed_p)
19622 unproduce_glyphs (it, row->used[TEXT_AREA]
19623 - n_glyphs_before);
19624 row->used[TEXT_AREA] = n_glyphs_before;
19626 /* Fill the rest of the row with continuation
19627 glyphs like in 20.x. */
19628 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19629 < row->glyphs[1 + TEXT_AREA])
19630 produce_special_glyphs (it, IT_CONTINUATION);
19632 row->continued_p = 1;
19633 it->current_x = x_before;
19634 it->continuation_lines_width += x_before;
19636 /* Restore the height to what it was before the
19637 element not fitting on the line. */
19638 it->max_ascent = ascent;
19639 it->max_descent = descent;
19640 it->max_phys_ascent = phys_ascent;
19641 it->max_phys_descent = phys_descent;
19643 else if (wrap_row_used > 0)
19645 back_to_wrap:
19646 if (row->reversed_p)
19647 unproduce_glyphs (it,
19648 row->used[TEXT_AREA] - wrap_row_used);
19649 RESTORE_IT (it, &wrap_it, wrap_data);
19650 it->continuation_lines_width += wrap_x;
19651 row->used[TEXT_AREA] = wrap_row_used;
19652 row->ascent = wrap_row_ascent;
19653 row->height = wrap_row_height;
19654 row->phys_ascent = wrap_row_phys_ascent;
19655 row->phys_height = wrap_row_phys_height;
19656 row->extra_line_spacing = wrap_row_extra_line_spacing;
19657 min_pos = wrap_row_min_pos;
19658 min_bpos = wrap_row_min_bpos;
19659 max_pos = wrap_row_max_pos;
19660 max_bpos = wrap_row_max_bpos;
19661 row->continued_p = 1;
19662 row->ends_at_zv_p = 0;
19663 row->exact_window_width_line_p = 0;
19664 it->continuation_lines_width += x;
19666 /* Make sure that a non-default face is extended
19667 up to the right margin of the window. */
19668 extend_face_to_end_of_line (it);
19670 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19672 /* A TAB that extends past the right edge of the
19673 window. This produces a single glyph on
19674 window system frames. We leave the glyph in
19675 this row and let it fill the row, but don't
19676 consume the TAB. */
19677 if ((row->reversed_p
19678 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19679 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19680 produce_special_glyphs (it, IT_CONTINUATION);
19681 it->continuation_lines_width += it->last_visible_x;
19682 row->ends_in_middle_of_char_p = 1;
19683 row->continued_p = 1;
19684 glyph->pixel_width = it->last_visible_x - x;
19685 it->starts_in_middle_of_char_p = 1;
19687 else
19689 /* Something other than a TAB that draws past
19690 the right edge of the window. Restore
19691 positions to values before the element. */
19692 if (row->reversed_p)
19693 unproduce_glyphs (it, row->used[TEXT_AREA]
19694 - (n_glyphs_before + i));
19695 row->used[TEXT_AREA] = n_glyphs_before + i;
19697 /* Display continuation glyphs. */
19698 it->current_x = x_before;
19699 it->continuation_lines_width += x;
19700 if (!FRAME_WINDOW_P (it->f)
19701 || (row->reversed_p
19702 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19703 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19704 produce_special_glyphs (it, IT_CONTINUATION);
19705 row->continued_p = 1;
19707 extend_face_to_end_of_line (it);
19709 if (nglyphs > 1 && i > 0)
19711 row->ends_in_middle_of_char_p = 1;
19712 it->starts_in_middle_of_char_p = 1;
19715 /* Restore the height to what it was before the
19716 element not fitting on the line. */
19717 it->max_ascent = ascent;
19718 it->max_descent = descent;
19719 it->max_phys_ascent = phys_ascent;
19720 it->max_phys_descent = phys_descent;
19723 break;
19725 else if (new_x > it->first_visible_x)
19727 /* Increment number of glyphs actually displayed. */
19728 ++it->hpos;
19730 /* Record the maximum and minimum buffer positions
19731 seen so far in glyphs that will be displayed by
19732 this row. */
19733 if (it->bidi_p)
19734 RECORD_MAX_MIN_POS (it);
19736 if (x < it->first_visible_x)
19737 /* Glyph is partially visible, i.e. row starts at
19738 negative X position. */
19739 row->x = x - it->first_visible_x;
19741 else
19743 /* Glyph is completely off the left margin of the
19744 window. This should not happen because of the
19745 move_it_in_display_line at the start of this
19746 function, unless the text display area of the
19747 window is empty. */
19748 eassert (it->first_visible_x <= it->last_visible_x);
19751 /* Even if this display element produced no glyphs at all,
19752 we want to record its position. */
19753 if (it->bidi_p && nglyphs == 0)
19754 RECORD_MAX_MIN_POS (it);
19756 row->ascent = max (row->ascent, it->max_ascent);
19757 row->height = max (row->height, it->max_ascent + it->max_descent);
19758 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19759 row->phys_height = max (row->phys_height,
19760 it->max_phys_ascent + it->max_phys_descent);
19761 row->extra_line_spacing = max (row->extra_line_spacing,
19762 it->max_extra_line_spacing);
19764 /* End of this display line if row is continued. */
19765 if (row->continued_p || row->ends_at_zv_p)
19766 break;
19769 at_end_of_line:
19770 /* Is this a line end? If yes, we're also done, after making
19771 sure that a non-default face is extended up to the right
19772 margin of the window. */
19773 if (ITERATOR_AT_END_OF_LINE_P (it))
19775 int used_before = row->used[TEXT_AREA];
19777 row->ends_in_newline_from_string_p = STRINGP (it->object);
19779 /* Add a space at the end of the line that is used to
19780 display the cursor there. */
19781 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19782 append_space_for_newline (it, 0);
19784 /* Extend the face to the end of the line. */
19785 extend_face_to_end_of_line (it);
19787 /* Make sure we have the position. */
19788 if (used_before == 0)
19789 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19791 /* Record the position of the newline, for use in
19792 find_row_edges. */
19793 it->eol_pos = it->current.pos;
19795 /* Consume the line end. This skips over invisible lines. */
19796 set_iterator_to_next (it, 1);
19797 it->continuation_lines_width = 0;
19798 break;
19801 /* Proceed with next display element. Note that this skips
19802 over lines invisible because of selective display. */
19803 set_iterator_to_next (it, 1);
19805 /* If we truncate lines, we are done when the last displayed
19806 glyphs reach past the right margin of the window. */
19807 if (it->line_wrap == TRUNCATE
19808 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19809 ? (it->current_x >= it->last_visible_x)
19810 : (it->current_x > it->last_visible_x)))
19812 /* Maybe add truncation glyphs. */
19813 if (!FRAME_WINDOW_P (it->f)
19814 || (row->reversed_p
19815 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19816 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19818 int i, n;
19820 if (!row->reversed_p)
19822 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19823 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19824 break;
19826 else
19828 for (i = 0; i < row->used[TEXT_AREA]; i++)
19829 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19830 break;
19831 /* Remove any padding glyphs at the front of ROW, to
19832 make room for the truncation glyphs we will be
19833 adding below. The loop below always inserts at
19834 least one truncation glyph, so also remove the
19835 last glyph added to ROW. */
19836 unproduce_glyphs (it, i + 1);
19837 /* Adjust i for the loop below. */
19838 i = row->used[TEXT_AREA] - (i + 1);
19841 it->current_x = x_before;
19842 if (!FRAME_WINDOW_P (it->f))
19844 for (n = row->used[TEXT_AREA]; i < n; ++i)
19846 row->used[TEXT_AREA] = i;
19847 produce_special_glyphs (it, IT_TRUNCATION);
19850 else
19852 row->used[TEXT_AREA] = i;
19853 produce_special_glyphs (it, IT_TRUNCATION);
19856 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19858 /* Don't truncate if we can overflow newline into fringe. */
19859 if (!get_next_display_element (it))
19861 it->continuation_lines_width = 0;
19862 row->ends_at_zv_p = 1;
19863 row->exact_window_width_line_p = 1;
19864 break;
19866 if (ITERATOR_AT_END_OF_LINE_P (it))
19868 row->exact_window_width_line_p = 1;
19869 goto at_end_of_line;
19871 it->current_x = x_before;
19874 row->truncated_on_right_p = 1;
19875 it->continuation_lines_width = 0;
19876 reseat_at_next_visible_line_start (it, 0);
19877 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19878 it->hpos = hpos_before;
19879 break;
19883 if (wrap_data)
19884 bidi_unshelve_cache (wrap_data, 1);
19886 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19887 at the left window margin. */
19888 if (it->first_visible_x
19889 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19891 if (!FRAME_WINDOW_P (it->f)
19892 || (row->reversed_p
19893 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19894 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19895 insert_left_trunc_glyphs (it);
19896 row->truncated_on_left_p = 1;
19899 /* Remember the position at which this line ends.
19901 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19902 cannot be before the call to find_row_edges below, since that is
19903 where these positions are determined. */
19904 row->end = it->current;
19905 if (!it->bidi_p)
19907 row->minpos = row->start.pos;
19908 row->maxpos = row->end.pos;
19910 else
19912 /* ROW->minpos and ROW->maxpos must be the smallest and
19913 `1 + the largest' buffer positions in ROW. But if ROW was
19914 bidi-reordered, these two positions can be anywhere in the
19915 row, so we must determine them now. */
19916 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19919 /* If the start of this line is the overlay arrow-position, then
19920 mark this glyph row as the one containing the overlay arrow.
19921 This is clearly a mess with variable size fonts. It would be
19922 better to let it be displayed like cursors under X. */
19923 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19924 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19925 !NILP (overlay_arrow_string)))
19927 /* Overlay arrow in window redisplay is a fringe bitmap. */
19928 if (STRINGP (overlay_arrow_string))
19930 struct glyph_row *arrow_row
19931 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19932 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19933 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19934 struct glyph *p = row->glyphs[TEXT_AREA];
19935 struct glyph *p2, *end;
19937 /* Copy the arrow glyphs. */
19938 while (glyph < arrow_end)
19939 *p++ = *glyph++;
19941 /* Throw away padding glyphs. */
19942 p2 = p;
19943 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19944 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19945 ++p2;
19946 if (p2 > p)
19948 while (p2 < end)
19949 *p++ = *p2++;
19950 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19953 else
19955 eassert (INTEGERP (overlay_arrow_string));
19956 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19958 overlay_arrow_seen = 1;
19961 /* Highlight trailing whitespace. */
19962 if (!NILP (Vshow_trailing_whitespace))
19963 highlight_trailing_whitespace (it->f, it->glyph_row);
19965 /* Compute pixel dimensions of this line. */
19966 compute_line_metrics (it);
19968 /* Implementation note: No changes in the glyphs of ROW or in their
19969 faces can be done past this point, because compute_line_metrics
19970 computes ROW's hash value and stores it within the glyph_row
19971 structure. */
19973 /* Record whether this row ends inside an ellipsis. */
19974 row->ends_in_ellipsis_p
19975 = (it->method == GET_FROM_DISPLAY_VECTOR
19976 && it->ellipsis_p);
19978 /* Save fringe bitmaps in this row. */
19979 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19980 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19981 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19982 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19984 it->left_user_fringe_bitmap = 0;
19985 it->left_user_fringe_face_id = 0;
19986 it->right_user_fringe_bitmap = 0;
19987 it->right_user_fringe_face_id = 0;
19989 /* Maybe set the cursor. */
19990 cvpos = it->w->cursor.vpos;
19991 if ((cvpos < 0
19992 /* In bidi-reordered rows, keep checking for proper cursor
19993 position even if one has been found already, because buffer
19994 positions in such rows change non-linearly with ROW->VPOS,
19995 when a line is continued. One exception: when we are at ZV,
19996 display cursor on the first suitable glyph row, since all
19997 the empty rows after that also have their position set to ZV. */
19998 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19999 lines' rows is implemented for bidi-reordered rows. */
20000 || (it->bidi_p
20001 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20002 && PT >= MATRIX_ROW_START_CHARPOS (row)
20003 && PT <= MATRIX_ROW_END_CHARPOS (row)
20004 && cursor_row_p (row))
20005 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20007 /* Prepare for the next line. This line starts horizontally at (X
20008 HPOS) = (0 0). Vertical positions are incremented. As a
20009 convenience for the caller, IT->glyph_row is set to the next
20010 row to be used. */
20011 it->current_x = it->hpos = 0;
20012 it->current_y += row->height;
20013 SET_TEXT_POS (it->eol_pos, 0, 0);
20014 ++it->vpos;
20015 ++it->glyph_row;
20016 /* The next row should by default use the same value of the
20017 reversed_p flag as this one. set_iterator_to_next decides when
20018 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20019 the flag accordingly. */
20020 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20021 it->glyph_row->reversed_p = row->reversed_p;
20022 it->start = row->end;
20023 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20025 #undef RECORD_MAX_MIN_POS
20028 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20029 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20030 doc: /* Return paragraph direction at point in BUFFER.
20031 Value is either `left-to-right' or `right-to-left'.
20032 If BUFFER is omitted or nil, it defaults to the current buffer.
20034 Paragraph direction determines how the text in the paragraph is displayed.
20035 In left-to-right paragraphs, text begins at the left margin of the window
20036 and the reading direction is generally left to right. In right-to-left
20037 paragraphs, text begins at the right margin and is read from right to left.
20039 See also `bidi-paragraph-direction'. */)
20040 (Lisp_Object buffer)
20042 struct buffer *buf = current_buffer;
20043 struct buffer *old = buf;
20045 if (! NILP (buffer))
20047 CHECK_BUFFER (buffer);
20048 buf = XBUFFER (buffer);
20051 if (NILP (BVAR (buf, bidi_display_reordering))
20052 || NILP (BVAR (buf, enable_multibyte_characters))
20053 /* When we are loading loadup.el, the character property tables
20054 needed for bidi iteration are not yet available. */
20055 || !NILP (Vpurify_flag))
20056 return Qleft_to_right;
20057 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20058 return BVAR (buf, bidi_paragraph_direction);
20059 else
20061 /* Determine the direction from buffer text. We could try to
20062 use current_matrix if it is up to date, but this seems fast
20063 enough as it is. */
20064 struct bidi_it itb;
20065 ptrdiff_t pos = BUF_PT (buf);
20066 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20067 int c;
20068 void *itb_data = bidi_shelve_cache ();
20070 set_buffer_temp (buf);
20071 /* bidi_paragraph_init finds the base direction of the paragraph
20072 by searching forward from paragraph start. We need the base
20073 direction of the current or _previous_ paragraph, so we need
20074 to make sure we are within that paragraph. To that end, find
20075 the previous non-empty line. */
20076 if (pos >= ZV && pos > BEGV)
20077 DEC_BOTH (pos, bytepos);
20078 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20079 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20081 while ((c = FETCH_BYTE (bytepos)) == '\n'
20082 || c == ' ' || c == '\t' || c == '\f')
20084 if (bytepos <= BEGV_BYTE)
20085 break;
20086 bytepos--;
20087 pos--;
20089 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20090 bytepos--;
20092 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20093 itb.paragraph_dir = NEUTRAL_DIR;
20094 itb.string.s = NULL;
20095 itb.string.lstring = Qnil;
20096 itb.string.bufpos = 0;
20097 itb.string.unibyte = 0;
20098 /* We have no window to use here for ignoring window-specific
20099 overlays. Using NULL for window pointer will cause
20100 compute_display_string_pos to use the current buffer. */
20101 itb.w = NULL;
20102 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20103 bidi_unshelve_cache (itb_data, 0);
20104 set_buffer_temp (old);
20105 switch (itb.paragraph_dir)
20107 case L2R:
20108 return Qleft_to_right;
20109 break;
20110 case R2L:
20111 return Qright_to_left;
20112 break;
20113 default:
20114 emacs_abort ();
20119 DEFUN ("move-point-visually", Fmove_point_visually,
20120 Smove_point_visually, 1, 1, 0,
20121 doc: /* Move point in the visual order in the specified DIRECTION.
20122 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20123 left.
20125 Value is the new character position of point. */)
20126 (Lisp_Object direction)
20128 struct window *w = XWINDOW (selected_window);
20129 struct buffer *b = NULL;
20130 struct glyph_row *row;
20131 int dir;
20132 Lisp_Object paragraph_dir;
20134 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20135 (!(ROW)->continued_p \
20136 && INTEGERP ((GLYPH)->object) \
20137 && (GLYPH)->type == CHAR_GLYPH \
20138 && (GLYPH)->u.ch == ' ' \
20139 && (GLYPH)->charpos >= 0 \
20140 && !(GLYPH)->avoid_cursor_p)
20142 CHECK_NUMBER (direction);
20143 dir = XINT (direction);
20144 if (dir > 0)
20145 dir = 1;
20146 else
20147 dir = -1;
20149 if (BUFFERP (w->contents))
20150 b = XBUFFER (w->contents);
20152 /* If current matrix is up-to-date, we can use the information
20153 recorded in the glyphs, at least as long as the goal is on the
20154 screen. */
20155 if (w->window_end_valid
20156 && !windows_or_buffers_changed
20157 && b
20158 && !b->clip_changed
20159 && !b->prevent_redisplay_optimizations_p
20160 && w->last_modified >= BUF_MODIFF (b)
20161 && w->last_overlay_modified >= BUF_OVERLAY_MODIFF (b)
20162 && w->cursor.vpos >= 0
20163 && w->cursor.vpos < w->current_matrix->nrows
20164 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20166 struct glyph *g = row->glyphs[TEXT_AREA];
20167 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20168 struct glyph *gpt = g + w->cursor.hpos;
20170 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20172 if (BUFFERP (g->object) && g->charpos != PT)
20174 SET_PT (g->charpos);
20175 w->cursor.vpos = -1;
20176 return make_number (PT);
20178 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20180 ptrdiff_t new_pos;
20182 if (BUFFERP (gpt->object))
20184 new_pos = PT;
20185 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20186 new_pos += (row->reversed_p ? -dir : dir);
20187 else
20188 new_pos -= (row->reversed_p ? -dir : dir);;
20190 else if (BUFFERP (g->object))
20191 new_pos = g->charpos;
20192 else
20193 break;
20194 SET_PT (new_pos);
20195 w->cursor.vpos = -1;
20196 return make_number (PT);
20198 else if (ROW_GLYPH_NEWLINE_P (row, g))
20200 /* Glyphs inserted at the end of a non-empty line for
20201 positioning the cursor have zero charpos, so we must
20202 deduce the value of point by other means. */
20203 if (g->charpos > 0)
20204 SET_PT (g->charpos);
20205 else if (row->ends_at_zv_p && PT != ZV)
20206 SET_PT (ZV);
20207 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20208 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20209 else
20210 break;
20211 w->cursor.vpos = -1;
20212 return make_number (PT);
20215 if (g == e || INTEGERP (g->object))
20217 if (row->truncated_on_left_p || row->truncated_on_right_p)
20218 goto simulate_display;
20219 if (!row->reversed_p)
20220 row += dir;
20221 else
20222 row -= dir;
20223 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20224 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20225 goto simulate_display;
20227 if (dir > 0)
20229 if (row->reversed_p && !row->continued_p)
20231 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20232 w->cursor.vpos = -1;
20233 return make_number (PT);
20235 g = row->glyphs[TEXT_AREA];
20236 e = g + row->used[TEXT_AREA];
20237 for ( ; g < e; g++)
20239 if (BUFFERP (g->object)
20240 /* Empty lines have only one glyph, which stands
20241 for the newline, and whose charpos is the
20242 buffer position of the newline. */
20243 || ROW_GLYPH_NEWLINE_P (row, g)
20244 /* When the buffer ends in a newline, the line at
20245 EOB also has one glyph, but its charpos is -1. */
20246 || (row->ends_at_zv_p
20247 && !row->reversed_p
20248 && INTEGERP (g->object)
20249 && g->type == CHAR_GLYPH
20250 && g->u.ch == ' '))
20252 if (g->charpos > 0)
20253 SET_PT (g->charpos);
20254 else if (!row->reversed_p
20255 && row->ends_at_zv_p
20256 && PT != ZV)
20257 SET_PT (ZV);
20258 else
20259 continue;
20260 w->cursor.vpos = -1;
20261 return make_number (PT);
20265 else
20267 if (!row->reversed_p && !row->continued_p)
20269 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20270 w->cursor.vpos = -1;
20271 return make_number (PT);
20273 e = row->glyphs[TEXT_AREA];
20274 g = e + row->used[TEXT_AREA] - 1;
20275 for ( ; g >= e; g--)
20277 if (BUFFERP (g->object)
20278 || (ROW_GLYPH_NEWLINE_P (row, g)
20279 && g->charpos > 0)
20280 /* Empty R2L lines on GUI frames have the buffer
20281 position of the newline stored in the stretch
20282 glyph. */
20283 || g->type == STRETCH_GLYPH
20284 || (row->ends_at_zv_p
20285 && row->reversed_p
20286 && INTEGERP (g->object)
20287 && g->type == CHAR_GLYPH
20288 && g->u.ch == ' '))
20290 if (g->charpos > 0)
20291 SET_PT (g->charpos);
20292 else if (row->reversed_p
20293 && row->ends_at_zv_p
20294 && PT != ZV)
20295 SET_PT (ZV);
20296 else
20297 continue;
20298 w->cursor.vpos = -1;
20299 return make_number (PT);
20306 simulate_display:
20308 /* If we wind up here, we failed to move by using the glyphs, so we
20309 need to simulate display instead. */
20311 if (b)
20312 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20313 else
20314 paragraph_dir = Qleft_to_right;
20315 if (EQ (paragraph_dir, Qright_to_left))
20316 dir = -dir;
20317 if (PT <= BEGV && dir < 0)
20318 xsignal0 (Qbeginning_of_buffer);
20319 else if (PT >= ZV && dir > 0)
20320 xsignal0 (Qend_of_buffer);
20321 else
20323 struct text_pos pt;
20324 struct it it;
20325 int pt_x, target_x, pixel_width, pt_vpos;
20326 bool at_eol_p;
20327 bool overshoot_expected = false;
20328 bool target_is_eol_p = false;
20330 /* Setup the arena. */
20331 SET_TEXT_POS (pt, PT, PT_BYTE);
20332 start_display (&it, w, pt);
20334 if (it.cmp_it.id < 0
20335 && it.method == GET_FROM_STRING
20336 && it.area == TEXT_AREA
20337 && it.string_from_display_prop_p
20338 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20339 overshoot_expected = true;
20341 /* Find the X coordinate of point. We start from the beginning
20342 of this or previous line to make sure we are before point in
20343 the logical order (since the move_it_* functions can only
20344 move forward). */
20345 reseat_at_previous_visible_line_start (&it);
20346 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20347 if (IT_CHARPOS (it) != PT)
20348 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20349 -1, -1, -1, MOVE_TO_POS);
20350 pt_x = it.current_x;
20351 pt_vpos = it.vpos;
20352 if (dir > 0 || overshoot_expected)
20354 struct glyph_row *row = it.glyph_row;
20356 /* When point is at beginning of line, we don't have
20357 information about the glyph there loaded into struct
20358 it. Calling get_next_display_element fixes that. */
20359 if (pt_x == 0)
20360 get_next_display_element (&it);
20361 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20362 it.glyph_row = NULL;
20363 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20364 it.glyph_row = row;
20365 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20366 it, lest it will become out of sync with it's buffer
20367 position. */
20368 it.current_x = pt_x;
20370 else
20371 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20372 pixel_width = it.pixel_width;
20373 if (overshoot_expected && at_eol_p)
20374 pixel_width = 0;
20375 else if (pixel_width <= 0)
20376 pixel_width = 1;
20378 /* If there's a display string at point, we are actually at the
20379 glyph to the left of point, so we need to correct the X
20380 coordinate. */
20381 if (overshoot_expected)
20382 pt_x += pixel_width;
20384 /* Compute target X coordinate, either to the left or to the
20385 right of point. On TTY frames, all characters have the same
20386 pixel width of 1, so we can use that. On GUI frames we don't
20387 have an easy way of getting at the pixel width of the
20388 character to the left of point, so we use a different method
20389 of getting to that place. */
20390 if (dir > 0)
20391 target_x = pt_x + pixel_width;
20392 else
20393 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20395 /* Target X coordinate could be one line above or below the line
20396 of point, in which case we need to adjust the target X
20397 coordinate. Also, if moving to the left, we need to begin at
20398 the left edge of the point's screen line. */
20399 if (dir < 0)
20401 if (pt_x > 0)
20403 start_display (&it, w, pt);
20404 reseat_at_previous_visible_line_start (&it);
20405 it.current_x = it.current_y = it.hpos = 0;
20406 if (pt_vpos != 0)
20407 move_it_by_lines (&it, pt_vpos);
20409 else
20411 move_it_by_lines (&it, -1);
20412 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20413 target_is_eol_p = true;
20416 else
20418 if (at_eol_p
20419 || (target_x >= it.last_visible_x
20420 && it.line_wrap != TRUNCATE))
20422 if (pt_x > 0)
20423 move_it_by_lines (&it, 0);
20424 move_it_by_lines (&it, 1);
20425 target_x = 0;
20429 /* Move to the target X coordinate. */
20430 #ifdef HAVE_WINDOW_SYSTEM
20431 /* On GUI frames, as we don't know the X coordinate of the
20432 character to the left of point, moving point to the left
20433 requires walking, one grapheme cluster at a time, until we
20434 find ourself at a place immediately to the left of the
20435 character at point. */
20436 if (FRAME_WINDOW_P (it.f) && dir < 0)
20438 struct text_pos new_pos = it.current.pos;
20439 enum move_it_result rc = MOVE_X_REACHED;
20441 while (it.current_x + it.pixel_width <= target_x
20442 && rc == MOVE_X_REACHED)
20444 int new_x = it.current_x + it.pixel_width;
20446 new_pos = it.current.pos;
20447 if (new_x == it.current_x)
20448 new_x++;
20449 rc = move_it_in_display_line_to (&it, ZV, new_x,
20450 MOVE_TO_POS | MOVE_TO_X);
20451 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20452 break;
20454 /* If we ended up on a composed character inside
20455 bidi-reordered text (e.g., Hebrew text with diacritics),
20456 the iterator gives us the buffer position of the last (in
20457 logical order) character of the composed grapheme cluster,
20458 which is not what we want. So we cheat: we compute the
20459 character position of the character that follows (in the
20460 logical order) the one where the above loop stopped. That
20461 character will appear on display to the left of point. */
20462 if (it.bidi_p
20463 && it.bidi_it.scan_dir == -1
20464 && new_pos.charpos - IT_CHARPOS (it) > 1)
20466 new_pos.charpos = IT_CHARPOS (it) + 1;
20467 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20469 it.current.pos = new_pos;
20471 else
20472 #endif
20473 if (it.current_x != target_x)
20474 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20476 /* When lines are truncated, the above loop will stop at the
20477 window edge. But we want to get to the end of line, even if
20478 it is beyond the window edge; automatic hscroll will then
20479 scroll the window to show point as appropriate. */
20480 if (target_is_eol_p && it.line_wrap == TRUNCATE
20481 && get_next_display_element (&it))
20483 struct text_pos new_pos = it.current.pos;
20485 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20487 set_iterator_to_next (&it, 0);
20488 if (it.method == GET_FROM_BUFFER)
20489 new_pos = it.current.pos;
20490 if (!get_next_display_element (&it))
20491 break;
20494 it.current.pos = new_pos;
20497 /* If we ended up in a display string that covers point, move to
20498 buffer position to the right in the visual order. */
20499 if (dir > 0)
20501 while (IT_CHARPOS (it) == PT)
20503 set_iterator_to_next (&it, 0);
20504 if (!get_next_display_element (&it))
20505 break;
20509 /* Move point to that position. */
20510 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20513 return make_number (PT);
20515 #undef ROW_GLYPH_NEWLINE_P
20519 /***********************************************************************
20520 Menu Bar
20521 ***********************************************************************/
20523 /* Redisplay the menu bar in the frame for window W.
20525 The menu bar of X frames that don't have X toolkit support is
20526 displayed in a special window W->frame->menu_bar_window.
20528 The menu bar of terminal frames is treated specially as far as
20529 glyph matrices are concerned. Menu bar lines are not part of
20530 windows, so the update is done directly on the frame matrix rows
20531 for the menu bar. */
20533 static void
20534 display_menu_bar (struct window *w)
20536 struct frame *f = XFRAME (WINDOW_FRAME (w));
20537 struct it it;
20538 Lisp_Object items;
20539 int i;
20541 /* Don't do all this for graphical frames. */
20542 #ifdef HAVE_NTGUI
20543 if (FRAME_W32_P (f))
20544 return;
20545 #endif
20546 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20547 if (FRAME_X_P (f))
20548 return;
20549 #endif
20551 #ifdef HAVE_NS
20552 if (FRAME_NS_P (f))
20553 return;
20554 #endif /* HAVE_NS */
20556 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20557 eassert (!FRAME_WINDOW_P (f));
20558 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20559 it.first_visible_x = 0;
20560 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20561 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20562 if (FRAME_WINDOW_P (f))
20564 /* Menu bar lines are displayed in the desired matrix of the
20565 dummy window menu_bar_window. */
20566 struct window *menu_w;
20567 menu_w = XWINDOW (f->menu_bar_window);
20568 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20569 MENU_FACE_ID);
20570 it.first_visible_x = 0;
20571 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20573 else
20574 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20576 /* This is a TTY frame, i.e. character hpos/vpos are used as
20577 pixel x/y. */
20578 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20579 MENU_FACE_ID);
20580 it.first_visible_x = 0;
20581 it.last_visible_x = FRAME_COLS (f);
20584 /* FIXME: This should be controlled by a user option. See the
20585 comments in redisplay_tool_bar and display_mode_line about
20586 this. */
20587 it.paragraph_embedding = L2R;
20589 /* Clear all rows of the menu bar. */
20590 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20592 struct glyph_row *row = it.glyph_row + i;
20593 clear_glyph_row (row);
20594 row->enabled_p = 1;
20595 row->full_width_p = 1;
20598 /* Display all items of the menu bar. */
20599 items = FRAME_MENU_BAR_ITEMS (it.f);
20600 for (i = 0; i < ASIZE (items); i += 4)
20602 Lisp_Object string;
20604 /* Stop at nil string. */
20605 string = AREF (items, i + 1);
20606 if (NILP (string))
20607 break;
20609 /* Remember where item was displayed. */
20610 ASET (items, i + 3, make_number (it.hpos));
20612 /* Display the item, pad with one space. */
20613 if (it.current_x < it.last_visible_x)
20614 display_string (NULL, string, Qnil, 0, 0, &it,
20615 SCHARS (string) + 1, 0, 0, -1);
20618 /* Fill out the line with spaces. */
20619 if (it.current_x < it.last_visible_x)
20620 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20622 /* Compute the total height of the lines. */
20623 compute_line_metrics (&it);
20628 /***********************************************************************
20629 Mode Line
20630 ***********************************************************************/
20632 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20633 FORCE is non-zero, redisplay mode lines unconditionally.
20634 Otherwise, redisplay only mode lines that are garbaged. Value is
20635 the number of windows whose mode lines were redisplayed. */
20637 static int
20638 redisplay_mode_lines (Lisp_Object window, int force)
20640 int nwindows = 0;
20642 while (!NILP (window))
20644 struct window *w = XWINDOW (window);
20646 if (WINDOWP (w->contents))
20647 nwindows += redisplay_mode_lines (w->contents, force);
20648 else if (force
20649 || FRAME_GARBAGED_P (XFRAME (w->frame))
20650 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20652 struct text_pos lpoint;
20653 struct buffer *old = current_buffer;
20655 /* Set the window's buffer for the mode line display. */
20656 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20657 set_buffer_internal_1 (XBUFFER (w->contents));
20659 /* Point refers normally to the selected window. For any
20660 other window, set up appropriate value. */
20661 if (!EQ (window, selected_window))
20663 struct text_pos pt;
20665 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20666 if (CHARPOS (pt) < BEGV)
20667 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20668 else if (CHARPOS (pt) > (ZV - 1))
20669 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20670 else
20671 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20674 /* Display mode lines. */
20675 clear_glyph_matrix (w->desired_matrix);
20676 if (display_mode_lines (w))
20678 ++nwindows;
20679 w->must_be_updated_p = 1;
20682 /* Restore old settings. */
20683 set_buffer_internal_1 (old);
20684 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20687 window = w->next;
20690 return nwindows;
20694 /* Display the mode and/or header line of window W. Value is the
20695 sum number of mode lines and header lines displayed. */
20697 static int
20698 display_mode_lines (struct window *w)
20700 Lisp_Object old_selected_window = selected_window;
20701 Lisp_Object old_selected_frame = selected_frame;
20702 Lisp_Object new_frame = w->frame;
20703 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20704 int n = 0;
20706 selected_frame = new_frame;
20707 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20708 or window's point, then we'd need select_window_1 here as well. */
20709 XSETWINDOW (selected_window, w);
20710 XFRAME (new_frame)->selected_window = selected_window;
20712 /* These will be set while the mode line specs are processed. */
20713 line_number_displayed = 0;
20714 w->column_number_displayed = -1;
20716 if (WINDOW_WANTS_MODELINE_P (w))
20718 struct window *sel_w = XWINDOW (old_selected_window);
20720 /* Select mode line face based on the real selected window. */
20721 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20722 BVAR (current_buffer, mode_line_format));
20723 ++n;
20726 if (WINDOW_WANTS_HEADER_LINE_P (w))
20728 display_mode_line (w, HEADER_LINE_FACE_ID,
20729 BVAR (current_buffer, header_line_format));
20730 ++n;
20733 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20734 selected_frame = old_selected_frame;
20735 selected_window = old_selected_window;
20736 return n;
20740 /* Display mode or header line of window W. FACE_ID specifies which
20741 line to display; it is either MODE_LINE_FACE_ID or
20742 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20743 display. Value is the pixel height of the mode/header line
20744 displayed. */
20746 static int
20747 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20749 struct it it;
20750 struct face *face;
20751 ptrdiff_t count = SPECPDL_INDEX ();
20753 init_iterator (&it, w, -1, -1, NULL, face_id);
20754 /* Don't extend on a previously drawn mode-line.
20755 This may happen if called from pos_visible_p. */
20756 it.glyph_row->enabled_p = 0;
20757 prepare_desired_row (it.glyph_row);
20759 it.glyph_row->mode_line_p = 1;
20761 /* FIXME: This should be controlled by a user option. But
20762 supporting such an option is not trivial, since the mode line is
20763 made up of many separate strings. */
20764 it.paragraph_embedding = L2R;
20766 record_unwind_protect (unwind_format_mode_line,
20767 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20769 mode_line_target = MODE_LINE_DISPLAY;
20771 /* Temporarily make frame's keyboard the current kboard so that
20772 kboard-local variables in the mode_line_format will get the right
20773 values. */
20774 push_kboard (FRAME_KBOARD (it.f));
20775 record_unwind_save_match_data ();
20776 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20777 pop_kboard ();
20779 unbind_to (count, Qnil);
20781 /* Fill up with spaces. */
20782 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20784 compute_line_metrics (&it);
20785 it.glyph_row->full_width_p = 1;
20786 it.glyph_row->continued_p = 0;
20787 it.glyph_row->truncated_on_left_p = 0;
20788 it.glyph_row->truncated_on_right_p = 0;
20790 /* Make a 3D mode-line have a shadow at its right end. */
20791 face = FACE_FROM_ID (it.f, face_id);
20792 extend_face_to_end_of_line (&it);
20793 if (face->box != FACE_NO_BOX)
20795 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20796 + it.glyph_row->used[TEXT_AREA] - 1);
20797 last->right_box_line_p = 1;
20800 return it.glyph_row->height;
20803 /* Move element ELT in LIST to the front of LIST.
20804 Return the updated list. */
20806 static Lisp_Object
20807 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20809 register Lisp_Object tail, prev;
20810 register Lisp_Object tem;
20812 tail = list;
20813 prev = Qnil;
20814 while (CONSP (tail))
20816 tem = XCAR (tail);
20818 if (EQ (elt, tem))
20820 /* Splice out the link TAIL. */
20821 if (NILP (prev))
20822 list = XCDR (tail);
20823 else
20824 Fsetcdr (prev, XCDR (tail));
20826 /* Now make it the first. */
20827 Fsetcdr (tail, list);
20828 return tail;
20830 else
20831 prev = tail;
20832 tail = XCDR (tail);
20833 QUIT;
20836 /* Not found--return unchanged LIST. */
20837 return list;
20840 /* Contribute ELT to the mode line for window IT->w. How it
20841 translates into text depends on its data type.
20843 IT describes the display environment in which we display, as usual.
20845 DEPTH is the depth in recursion. It is used to prevent
20846 infinite recursion here.
20848 FIELD_WIDTH is the number of characters the display of ELT should
20849 occupy in the mode line, and PRECISION is the maximum number of
20850 characters to display from ELT's representation. See
20851 display_string for details.
20853 Returns the hpos of the end of the text generated by ELT.
20855 PROPS is a property list to add to any string we encounter.
20857 If RISKY is nonzero, remove (disregard) any properties in any string
20858 we encounter, and ignore :eval and :propertize.
20860 The global variable `mode_line_target' determines whether the
20861 output is passed to `store_mode_line_noprop',
20862 `store_mode_line_string', or `display_string'. */
20864 static int
20865 display_mode_element (struct it *it, int depth, int field_width, int precision,
20866 Lisp_Object elt, Lisp_Object props, int risky)
20868 int n = 0, field, prec;
20869 int literal = 0;
20871 tail_recurse:
20872 if (depth > 100)
20873 elt = build_string ("*too-deep*");
20875 depth++;
20877 switch (XTYPE (elt))
20879 case Lisp_String:
20881 /* A string: output it and check for %-constructs within it. */
20882 unsigned char c;
20883 ptrdiff_t offset = 0;
20885 if (SCHARS (elt) > 0
20886 && (!NILP (props) || risky))
20888 Lisp_Object oprops, aelt;
20889 oprops = Ftext_properties_at (make_number (0), elt);
20891 /* If the starting string's properties are not what
20892 we want, translate the string. Also, if the string
20893 is risky, do that anyway. */
20895 if (NILP (Fequal (props, oprops)) || risky)
20897 /* If the starting string has properties,
20898 merge the specified ones onto the existing ones. */
20899 if (! NILP (oprops) && !risky)
20901 Lisp_Object tem;
20903 oprops = Fcopy_sequence (oprops);
20904 tem = props;
20905 while (CONSP (tem))
20907 oprops = Fplist_put (oprops, XCAR (tem),
20908 XCAR (XCDR (tem)));
20909 tem = XCDR (XCDR (tem));
20911 props = oprops;
20914 aelt = Fassoc (elt, mode_line_proptrans_alist);
20915 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20917 /* AELT is what we want. Move it to the front
20918 without consing. */
20919 elt = XCAR (aelt);
20920 mode_line_proptrans_alist
20921 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20923 else
20925 Lisp_Object tem;
20927 /* If AELT has the wrong props, it is useless.
20928 so get rid of it. */
20929 if (! NILP (aelt))
20930 mode_line_proptrans_alist
20931 = Fdelq (aelt, mode_line_proptrans_alist);
20933 elt = Fcopy_sequence (elt);
20934 Fset_text_properties (make_number (0), Flength (elt),
20935 props, elt);
20936 /* Add this item to mode_line_proptrans_alist. */
20937 mode_line_proptrans_alist
20938 = Fcons (Fcons (elt, props),
20939 mode_line_proptrans_alist);
20940 /* Truncate mode_line_proptrans_alist
20941 to at most 50 elements. */
20942 tem = Fnthcdr (make_number (50),
20943 mode_line_proptrans_alist);
20944 if (! NILP (tem))
20945 XSETCDR (tem, Qnil);
20950 offset = 0;
20952 if (literal)
20954 prec = precision - n;
20955 switch (mode_line_target)
20957 case MODE_LINE_NOPROP:
20958 case MODE_LINE_TITLE:
20959 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20960 break;
20961 case MODE_LINE_STRING:
20962 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20963 break;
20964 case MODE_LINE_DISPLAY:
20965 n += display_string (NULL, elt, Qnil, 0, 0, it,
20966 0, prec, 0, STRING_MULTIBYTE (elt));
20967 break;
20970 break;
20973 /* Handle the non-literal case. */
20975 while ((precision <= 0 || n < precision)
20976 && SREF (elt, offset) != 0
20977 && (mode_line_target != MODE_LINE_DISPLAY
20978 || it->current_x < it->last_visible_x))
20980 ptrdiff_t last_offset = offset;
20982 /* Advance to end of string or next format specifier. */
20983 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20986 if (offset - 1 != last_offset)
20988 ptrdiff_t nchars, nbytes;
20990 /* Output to end of string or up to '%'. Field width
20991 is length of string. Don't output more than
20992 PRECISION allows us. */
20993 offset--;
20995 prec = c_string_width (SDATA (elt) + last_offset,
20996 offset - last_offset, precision - n,
20997 &nchars, &nbytes);
20999 switch (mode_line_target)
21001 case MODE_LINE_NOPROP:
21002 case MODE_LINE_TITLE:
21003 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21004 break;
21005 case MODE_LINE_STRING:
21007 ptrdiff_t bytepos = last_offset;
21008 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21009 ptrdiff_t endpos = (precision <= 0
21010 ? string_byte_to_char (elt, offset)
21011 : charpos + nchars);
21013 n += store_mode_line_string (NULL,
21014 Fsubstring (elt, make_number (charpos),
21015 make_number (endpos)),
21016 0, 0, 0, Qnil);
21018 break;
21019 case MODE_LINE_DISPLAY:
21021 ptrdiff_t bytepos = last_offset;
21022 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21024 if (precision <= 0)
21025 nchars = string_byte_to_char (elt, offset) - charpos;
21026 n += display_string (NULL, elt, Qnil, 0, charpos,
21027 it, 0, nchars, 0,
21028 STRING_MULTIBYTE (elt));
21030 break;
21033 else /* c == '%' */
21035 ptrdiff_t percent_position = offset;
21037 /* Get the specified minimum width. Zero means
21038 don't pad. */
21039 field = 0;
21040 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21041 field = field * 10 + c - '0';
21043 /* Don't pad beyond the total padding allowed. */
21044 if (field_width - n > 0 && field > field_width - n)
21045 field = field_width - n;
21047 /* Note that either PRECISION <= 0 or N < PRECISION. */
21048 prec = precision - n;
21050 if (c == 'M')
21051 n += display_mode_element (it, depth, field, prec,
21052 Vglobal_mode_string, props,
21053 risky);
21054 else if (c != 0)
21056 bool multibyte;
21057 ptrdiff_t bytepos, charpos;
21058 const char *spec;
21059 Lisp_Object string;
21061 bytepos = percent_position;
21062 charpos = (STRING_MULTIBYTE (elt)
21063 ? string_byte_to_char (elt, bytepos)
21064 : bytepos);
21065 spec = decode_mode_spec (it->w, c, field, &string);
21066 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21068 switch (mode_line_target)
21070 case MODE_LINE_NOPROP:
21071 case MODE_LINE_TITLE:
21072 n += store_mode_line_noprop (spec, field, prec);
21073 break;
21074 case MODE_LINE_STRING:
21076 Lisp_Object tem = build_string (spec);
21077 props = Ftext_properties_at (make_number (charpos), elt);
21078 /* Should only keep face property in props */
21079 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21081 break;
21082 case MODE_LINE_DISPLAY:
21084 int nglyphs_before, nwritten;
21086 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21087 nwritten = display_string (spec, string, elt,
21088 charpos, 0, it,
21089 field, prec, 0,
21090 multibyte);
21092 /* Assign to the glyphs written above the
21093 string where the `%x' came from, position
21094 of the `%'. */
21095 if (nwritten > 0)
21097 struct glyph *glyph
21098 = (it->glyph_row->glyphs[TEXT_AREA]
21099 + nglyphs_before);
21100 int i;
21102 for (i = 0; i < nwritten; ++i)
21104 glyph[i].object = elt;
21105 glyph[i].charpos = charpos;
21108 n += nwritten;
21111 break;
21114 else /* c == 0 */
21115 break;
21119 break;
21121 case Lisp_Symbol:
21122 /* A symbol: process the value of the symbol recursively
21123 as if it appeared here directly. Avoid error if symbol void.
21124 Special case: if value of symbol is a string, output the string
21125 literally. */
21127 register Lisp_Object tem;
21129 /* If the variable is not marked as risky to set
21130 then its contents are risky to use. */
21131 if (NILP (Fget (elt, Qrisky_local_variable)))
21132 risky = 1;
21134 tem = Fboundp (elt);
21135 if (!NILP (tem))
21137 tem = Fsymbol_value (elt);
21138 /* If value is a string, output that string literally:
21139 don't check for % within it. */
21140 if (STRINGP (tem))
21141 literal = 1;
21143 if (!EQ (tem, elt))
21145 /* Give up right away for nil or t. */
21146 elt = tem;
21147 goto tail_recurse;
21151 break;
21153 case Lisp_Cons:
21155 register Lisp_Object car, tem;
21157 /* A cons cell: five distinct cases.
21158 If first element is :eval or :propertize, do something special.
21159 If first element is a string or a cons, process all the elements
21160 and effectively concatenate them.
21161 If first element is a negative number, truncate displaying cdr to
21162 at most that many characters. If positive, pad (with spaces)
21163 to at least that many characters.
21164 If first element is a symbol, process the cadr or caddr recursively
21165 according to whether the symbol's value is non-nil or nil. */
21166 car = XCAR (elt);
21167 if (EQ (car, QCeval))
21169 /* An element of the form (:eval FORM) means evaluate FORM
21170 and use the result as mode line elements. */
21172 if (risky)
21173 break;
21175 if (CONSP (XCDR (elt)))
21177 Lisp_Object spec;
21178 spec = safe_eval (XCAR (XCDR (elt)));
21179 n += display_mode_element (it, depth, field_width - n,
21180 precision - n, spec, props,
21181 risky);
21184 else if (EQ (car, QCpropertize))
21186 /* An element of the form (:propertize ELT PROPS...)
21187 means display ELT but applying properties PROPS. */
21189 if (risky)
21190 break;
21192 if (CONSP (XCDR (elt)))
21193 n += display_mode_element (it, depth, field_width - n,
21194 precision - n, XCAR (XCDR (elt)),
21195 XCDR (XCDR (elt)), risky);
21197 else if (SYMBOLP (car))
21199 tem = Fboundp (car);
21200 elt = XCDR (elt);
21201 if (!CONSP (elt))
21202 goto invalid;
21203 /* elt is now the cdr, and we know it is a cons cell.
21204 Use its car if CAR has a non-nil value. */
21205 if (!NILP (tem))
21207 tem = Fsymbol_value (car);
21208 if (!NILP (tem))
21210 elt = XCAR (elt);
21211 goto tail_recurse;
21214 /* Symbol's value is nil (or symbol is unbound)
21215 Get the cddr of the original list
21216 and if possible find the caddr and use that. */
21217 elt = XCDR (elt);
21218 if (NILP (elt))
21219 break;
21220 else if (!CONSP (elt))
21221 goto invalid;
21222 elt = XCAR (elt);
21223 goto tail_recurse;
21225 else if (INTEGERP (car))
21227 register int lim = XINT (car);
21228 elt = XCDR (elt);
21229 if (lim < 0)
21231 /* Negative int means reduce maximum width. */
21232 if (precision <= 0)
21233 precision = -lim;
21234 else
21235 precision = min (precision, -lim);
21237 else if (lim > 0)
21239 /* Padding specified. Don't let it be more than
21240 current maximum. */
21241 if (precision > 0)
21242 lim = min (precision, lim);
21244 /* If that's more padding than already wanted, queue it.
21245 But don't reduce padding already specified even if
21246 that is beyond the current truncation point. */
21247 field_width = max (lim, field_width);
21249 goto tail_recurse;
21251 else if (STRINGP (car) || CONSP (car))
21253 Lisp_Object halftail = elt;
21254 int len = 0;
21256 while (CONSP (elt)
21257 && (precision <= 0 || n < precision))
21259 n += display_mode_element (it, depth,
21260 /* Do padding only after the last
21261 element in the list. */
21262 (! CONSP (XCDR (elt))
21263 ? field_width - n
21264 : 0),
21265 precision - n, XCAR (elt),
21266 props, risky);
21267 elt = XCDR (elt);
21268 len++;
21269 if ((len & 1) == 0)
21270 halftail = XCDR (halftail);
21271 /* Check for cycle. */
21272 if (EQ (halftail, elt))
21273 break;
21277 break;
21279 default:
21280 invalid:
21281 elt = build_string ("*invalid*");
21282 goto tail_recurse;
21285 /* Pad to FIELD_WIDTH. */
21286 if (field_width > 0 && n < field_width)
21288 switch (mode_line_target)
21290 case MODE_LINE_NOPROP:
21291 case MODE_LINE_TITLE:
21292 n += store_mode_line_noprop ("", field_width - n, 0);
21293 break;
21294 case MODE_LINE_STRING:
21295 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21296 break;
21297 case MODE_LINE_DISPLAY:
21298 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21299 0, 0, 0);
21300 break;
21304 return n;
21307 /* Store a mode-line string element in mode_line_string_list.
21309 If STRING is non-null, display that C string. Otherwise, the Lisp
21310 string LISP_STRING is displayed.
21312 FIELD_WIDTH is the minimum number of output glyphs to produce.
21313 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21314 with spaces. FIELD_WIDTH <= 0 means don't pad.
21316 PRECISION is the maximum number of characters to output from
21317 STRING. PRECISION <= 0 means don't truncate the string.
21319 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21320 properties to the string.
21322 PROPS are the properties to add to the string.
21323 The mode_line_string_face face property is always added to the string.
21326 static int
21327 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21328 int field_width, int precision, Lisp_Object props)
21330 ptrdiff_t len;
21331 int n = 0;
21333 if (string != NULL)
21335 len = strlen (string);
21336 if (precision > 0 && len > precision)
21337 len = precision;
21338 lisp_string = make_string (string, len);
21339 if (NILP (props))
21340 props = mode_line_string_face_prop;
21341 else if (!NILP (mode_line_string_face))
21343 Lisp_Object face = Fplist_get (props, Qface);
21344 props = Fcopy_sequence (props);
21345 if (NILP (face))
21346 face = mode_line_string_face;
21347 else
21348 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
21349 props = Fplist_put (props, Qface, face);
21351 Fadd_text_properties (make_number (0), make_number (len),
21352 props, lisp_string);
21354 else
21356 len = XFASTINT (Flength (lisp_string));
21357 if (precision > 0 && len > precision)
21359 len = precision;
21360 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21361 precision = -1;
21363 if (!NILP (mode_line_string_face))
21365 Lisp_Object face;
21366 if (NILP (props))
21367 props = Ftext_properties_at (make_number (0), lisp_string);
21368 face = Fplist_get (props, Qface);
21369 if (NILP (face))
21370 face = mode_line_string_face;
21371 else
21372 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
21373 props = Fcons (Qface, Fcons (face, Qnil));
21374 if (copy_string)
21375 lisp_string = Fcopy_sequence (lisp_string);
21377 if (!NILP (props))
21378 Fadd_text_properties (make_number (0), make_number (len),
21379 props, lisp_string);
21382 if (len > 0)
21384 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21385 n += len;
21388 if (field_width > len)
21390 field_width -= len;
21391 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21392 if (!NILP (props))
21393 Fadd_text_properties (make_number (0), make_number (field_width),
21394 props, lisp_string);
21395 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21396 n += field_width;
21399 return n;
21403 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21404 1, 4, 0,
21405 doc: /* Format a string out of a mode line format specification.
21406 First arg FORMAT specifies the mode line format (see `mode-line-format'
21407 for details) to use.
21409 By default, the format is evaluated for the currently selected window.
21411 Optional second arg FACE specifies the face property to put on all
21412 characters for which no face is specified. The value nil means the
21413 default face. The value t means whatever face the window's mode line
21414 currently uses (either `mode-line' or `mode-line-inactive',
21415 depending on whether the window is the selected window or not).
21416 An integer value means the value string has no text
21417 properties.
21419 Optional third and fourth args WINDOW and BUFFER specify the window
21420 and buffer to use as the context for the formatting (defaults
21421 are the selected window and the WINDOW's buffer). */)
21422 (Lisp_Object format, Lisp_Object face,
21423 Lisp_Object window, Lisp_Object buffer)
21425 struct it it;
21426 int len;
21427 struct window *w;
21428 struct buffer *old_buffer = NULL;
21429 int face_id;
21430 int no_props = INTEGERP (face);
21431 ptrdiff_t count = SPECPDL_INDEX ();
21432 Lisp_Object str;
21433 int string_start = 0;
21435 w = decode_any_window (window);
21436 XSETWINDOW (window, w);
21438 if (NILP (buffer))
21439 buffer = w->contents;
21440 CHECK_BUFFER (buffer);
21442 /* Make formatting the modeline a non-op when noninteractive, otherwise
21443 there will be problems later caused by a partially initialized frame. */
21444 if (NILP (format) || noninteractive)
21445 return empty_unibyte_string;
21447 if (no_props)
21448 face = Qnil;
21450 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21451 : EQ (face, Qt) ? (EQ (window, selected_window)
21452 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21453 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21454 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21455 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21456 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21457 : DEFAULT_FACE_ID;
21459 old_buffer = current_buffer;
21461 /* Save things including mode_line_proptrans_alist,
21462 and set that to nil so that we don't alter the outer value. */
21463 record_unwind_protect (unwind_format_mode_line,
21464 format_mode_line_unwind_data
21465 (XFRAME (WINDOW_FRAME (w)),
21466 old_buffer, selected_window, 1));
21467 mode_line_proptrans_alist = Qnil;
21469 Fselect_window (window, Qt);
21470 set_buffer_internal_1 (XBUFFER (buffer));
21472 init_iterator (&it, w, -1, -1, NULL, face_id);
21474 if (no_props)
21476 mode_line_target = MODE_LINE_NOPROP;
21477 mode_line_string_face_prop = Qnil;
21478 mode_line_string_list = Qnil;
21479 string_start = MODE_LINE_NOPROP_LEN (0);
21481 else
21483 mode_line_target = MODE_LINE_STRING;
21484 mode_line_string_list = Qnil;
21485 mode_line_string_face = face;
21486 mode_line_string_face_prop
21487 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
21490 push_kboard (FRAME_KBOARD (it.f));
21491 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21492 pop_kboard ();
21494 if (no_props)
21496 len = MODE_LINE_NOPROP_LEN (string_start);
21497 str = make_string (mode_line_noprop_buf + string_start, len);
21499 else
21501 mode_line_string_list = Fnreverse (mode_line_string_list);
21502 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21503 empty_unibyte_string);
21506 unbind_to (count, Qnil);
21507 return str;
21510 /* Write a null-terminated, right justified decimal representation of
21511 the positive integer D to BUF using a minimal field width WIDTH. */
21513 static void
21514 pint2str (register char *buf, register int width, register ptrdiff_t d)
21516 register char *p = buf;
21518 if (d <= 0)
21519 *p++ = '0';
21520 else
21522 while (d > 0)
21524 *p++ = d % 10 + '0';
21525 d /= 10;
21529 for (width -= (int) (p - buf); width > 0; --width)
21530 *p++ = ' ';
21531 *p-- = '\0';
21532 while (p > buf)
21534 d = *buf;
21535 *buf++ = *p;
21536 *p-- = d;
21540 /* Write a null-terminated, right justified decimal and "human
21541 readable" representation of the nonnegative integer D to BUF using
21542 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21544 static const char power_letter[] =
21546 0, /* no letter */
21547 'k', /* kilo */
21548 'M', /* mega */
21549 'G', /* giga */
21550 'T', /* tera */
21551 'P', /* peta */
21552 'E', /* exa */
21553 'Z', /* zetta */
21554 'Y' /* yotta */
21557 static void
21558 pint2hrstr (char *buf, int width, ptrdiff_t d)
21560 /* We aim to represent the nonnegative integer D as
21561 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21562 ptrdiff_t quotient = d;
21563 int remainder = 0;
21564 /* -1 means: do not use TENTHS. */
21565 int tenths = -1;
21566 int exponent = 0;
21568 /* Length of QUOTIENT.TENTHS as a string. */
21569 int length;
21571 char * psuffix;
21572 char * p;
21574 if (quotient >= 1000)
21576 /* Scale to the appropriate EXPONENT. */
21579 remainder = quotient % 1000;
21580 quotient /= 1000;
21581 exponent++;
21583 while (quotient >= 1000);
21585 /* Round to nearest and decide whether to use TENTHS or not. */
21586 if (quotient <= 9)
21588 tenths = remainder / 100;
21589 if (remainder % 100 >= 50)
21591 if (tenths < 9)
21592 tenths++;
21593 else
21595 quotient++;
21596 if (quotient == 10)
21597 tenths = -1;
21598 else
21599 tenths = 0;
21603 else
21604 if (remainder >= 500)
21606 if (quotient < 999)
21607 quotient++;
21608 else
21610 quotient = 1;
21611 exponent++;
21612 tenths = 0;
21617 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21618 if (tenths == -1 && quotient <= 99)
21619 if (quotient <= 9)
21620 length = 1;
21621 else
21622 length = 2;
21623 else
21624 length = 3;
21625 p = psuffix = buf + max (width, length);
21627 /* Print EXPONENT. */
21628 *psuffix++ = power_letter[exponent];
21629 *psuffix = '\0';
21631 /* Print TENTHS. */
21632 if (tenths >= 0)
21634 *--p = '0' + tenths;
21635 *--p = '.';
21638 /* Print QUOTIENT. */
21641 int digit = quotient % 10;
21642 *--p = '0' + digit;
21644 while ((quotient /= 10) != 0);
21646 /* Print leading spaces. */
21647 while (buf < p)
21648 *--p = ' ';
21651 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21652 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21653 type of CODING_SYSTEM. Return updated pointer into BUF. */
21655 static unsigned char invalid_eol_type[] = "(*invalid*)";
21657 static char *
21658 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21660 Lisp_Object val;
21661 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21662 const unsigned char *eol_str;
21663 int eol_str_len;
21664 /* The EOL conversion we are using. */
21665 Lisp_Object eoltype;
21667 val = CODING_SYSTEM_SPEC (coding_system);
21668 eoltype = Qnil;
21670 if (!VECTORP (val)) /* Not yet decided. */
21672 *buf++ = multibyte ? '-' : ' ';
21673 if (eol_flag)
21674 eoltype = eol_mnemonic_undecided;
21675 /* Don't mention EOL conversion if it isn't decided. */
21677 else
21679 Lisp_Object attrs;
21680 Lisp_Object eolvalue;
21682 attrs = AREF (val, 0);
21683 eolvalue = AREF (val, 2);
21685 *buf++ = multibyte
21686 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21687 : ' ';
21689 if (eol_flag)
21691 /* The EOL conversion that is normal on this system. */
21693 if (NILP (eolvalue)) /* Not yet decided. */
21694 eoltype = eol_mnemonic_undecided;
21695 else if (VECTORP (eolvalue)) /* Not yet decided. */
21696 eoltype = eol_mnemonic_undecided;
21697 else /* eolvalue is Qunix, Qdos, or Qmac. */
21698 eoltype = (EQ (eolvalue, Qunix)
21699 ? eol_mnemonic_unix
21700 : (EQ (eolvalue, Qdos) == 1
21701 ? eol_mnemonic_dos : eol_mnemonic_mac));
21705 if (eol_flag)
21707 /* Mention the EOL conversion if it is not the usual one. */
21708 if (STRINGP (eoltype))
21710 eol_str = SDATA (eoltype);
21711 eol_str_len = SBYTES (eoltype);
21713 else if (CHARACTERP (eoltype))
21715 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21716 int c = XFASTINT (eoltype);
21717 eol_str_len = CHAR_STRING (c, tmp);
21718 eol_str = tmp;
21720 else
21722 eol_str = invalid_eol_type;
21723 eol_str_len = sizeof (invalid_eol_type) - 1;
21725 memcpy (buf, eol_str, eol_str_len);
21726 buf += eol_str_len;
21729 return buf;
21732 /* Return a string for the output of a mode line %-spec for window W,
21733 generated by character C. FIELD_WIDTH > 0 means pad the string
21734 returned with spaces to that value. Return a Lisp string in
21735 *STRING if the resulting string is taken from that Lisp string.
21737 Note we operate on the current buffer for most purposes. */
21739 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21741 static const char *
21742 decode_mode_spec (struct window *w, register int c, int field_width,
21743 Lisp_Object *string)
21745 Lisp_Object obj;
21746 struct frame *f = XFRAME (WINDOW_FRAME (w));
21747 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21748 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21749 produce strings from numerical values, so limit preposterously
21750 large values of FIELD_WIDTH to avoid overrunning the buffer's
21751 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21752 bytes plus the terminating null. */
21753 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21754 struct buffer *b = current_buffer;
21756 obj = Qnil;
21757 *string = Qnil;
21759 switch (c)
21761 case '*':
21762 if (!NILP (BVAR (b, read_only)))
21763 return "%";
21764 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21765 return "*";
21766 return "-";
21768 case '+':
21769 /* This differs from %* only for a modified read-only buffer. */
21770 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21771 return "*";
21772 if (!NILP (BVAR (b, read_only)))
21773 return "%";
21774 return "-";
21776 case '&':
21777 /* This differs from %* in ignoring read-only-ness. */
21778 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21779 return "*";
21780 return "-";
21782 case '%':
21783 return "%";
21785 case '[':
21787 int i;
21788 char *p;
21790 if (command_loop_level > 5)
21791 return "[[[... ";
21792 p = decode_mode_spec_buf;
21793 for (i = 0; i < command_loop_level; i++)
21794 *p++ = '[';
21795 *p = 0;
21796 return decode_mode_spec_buf;
21799 case ']':
21801 int i;
21802 char *p;
21804 if (command_loop_level > 5)
21805 return " ...]]]";
21806 p = decode_mode_spec_buf;
21807 for (i = 0; i < command_loop_level; i++)
21808 *p++ = ']';
21809 *p = 0;
21810 return decode_mode_spec_buf;
21813 case '-':
21815 register int i;
21817 /* Let lots_of_dashes be a string of infinite length. */
21818 if (mode_line_target == MODE_LINE_NOPROP
21819 || mode_line_target == MODE_LINE_STRING)
21820 return "--";
21821 if (field_width <= 0
21822 || field_width > sizeof (lots_of_dashes))
21824 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21825 decode_mode_spec_buf[i] = '-';
21826 decode_mode_spec_buf[i] = '\0';
21827 return decode_mode_spec_buf;
21829 else
21830 return lots_of_dashes;
21833 case 'b':
21834 obj = BVAR (b, name);
21835 break;
21837 case 'c':
21838 /* %c and %l are ignored in `frame-title-format'.
21839 (In redisplay_internal, the frame title is drawn _before_ the
21840 windows are updated, so the stuff which depends on actual
21841 window contents (such as %l) may fail to render properly, or
21842 even crash emacs.) */
21843 if (mode_line_target == MODE_LINE_TITLE)
21844 return "";
21845 else
21847 ptrdiff_t col = current_column ();
21848 w->column_number_displayed = col;
21849 pint2str (decode_mode_spec_buf, width, col);
21850 return decode_mode_spec_buf;
21853 case 'e':
21854 #ifndef SYSTEM_MALLOC
21856 if (NILP (Vmemory_full))
21857 return "";
21858 else
21859 return "!MEM FULL! ";
21861 #else
21862 return "";
21863 #endif
21865 case 'F':
21866 /* %F displays the frame name. */
21867 if (!NILP (f->title))
21868 return SSDATA (f->title);
21869 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21870 return SSDATA (f->name);
21871 return "Emacs";
21873 case 'f':
21874 obj = BVAR (b, filename);
21875 break;
21877 case 'i':
21879 ptrdiff_t size = ZV - BEGV;
21880 pint2str (decode_mode_spec_buf, width, size);
21881 return decode_mode_spec_buf;
21884 case 'I':
21886 ptrdiff_t size = ZV - BEGV;
21887 pint2hrstr (decode_mode_spec_buf, width, size);
21888 return decode_mode_spec_buf;
21891 case 'l':
21893 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21894 ptrdiff_t topline, nlines, height;
21895 ptrdiff_t junk;
21897 /* %c and %l are ignored in `frame-title-format'. */
21898 if (mode_line_target == MODE_LINE_TITLE)
21899 return "";
21901 startpos = marker_position (w->start);
21902 startpos_byte = marker_byte_position (w->start);
21903 height = WINDOW_TOTAL_LINES (w);
21905 /* If we decided that this buffer isn't suitable for line numbers,
21906 don't forget that too fast. */
21907 if (w->base_line_pos == -1)
21908 goto no_value;
21910 /* If the buffer is very big, don't waste time. */
21911 if (INTEGERP (Vline_number_display_limit)
21912 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21914 w->base_line_pos = 0;
21915 w->base_line_number = 0;
21916 goto no_value;
21919 if (w->base_line_number > 0
21920 && w->base_line_pos > 0
21921 && w->base_line_pos <= startpos)
21923 line = w->base_line_number;
21924 linepos = w->base_line_pos;
21925 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21927 else
21929 line = 1;
21930 linepos = BUF_BEGV (b);
21931 linepos_byte = BUF_BEGV_BYTE (b);
21934 /* Count lines from base line to window start position. */
21935 nlines = display_count_lines (linepos_byte,
21936 startpos_byte,
21937 startpos, &junk);
21939 topline = nlines + line;
21941 /* Determine a new base line, if the old one is too close
21942 or too far away, or if we did not have one.
21943 "Too close" means it's plausible a scroll-down would
21944 go back past it. */
21945 if (startpos == BUF_BEGV (b))
21947 w->base_line_number = topline;
21948 w->base_line_pos = BUF_BEGV (b);
21950 else if (nlines < height + 25 || nlines > height * 3 + 50
21951 || linepos == BUF_BEGV (b))
21953 ptrdiff_t limit = BUF_BEGV (b);
21954 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21955 ptrdiff_t position;
21956 ptrdiff_t distance =
21957 (height * 2 + 30) * line_number_display_limit_width;
21959 if (startpos - distance > limit)
21961 limit = startpos - distance;
21962 limit_byte = CHAR_TO_BYTE (limit);
21965 nlines = display_count_lines (startpos_byte,
21966 limit_byte,
21967 - (height * 2 + 30),
21968 &position);
21969 /* If we couldn't find the lines we wanted within
21970 line_number_display_limit_width chars per line,
21971 give up on line numbers for this window. */
21972 if (position == limit_byte && limit == startpos - distance)
21974 w->base_line_pos = -1;
21975 w->base_line_number = 0;
21976 goto no_value;
21979 w->base_line_number = topline - nlines;
21980 w->base_line_pos = BYTE_TO_CHAR (position);
21983 /* Now count lines from the start pos to point. */
21984 nlines = display_count_lines (startpos_byte,
21985 PT_BYTE, PT, &junk);
21987 /* Record that we did display the line number. */
21988 line_number_displayed = 1;
21990 /* Make the string to show. */
21991 pint2str (decode_mode_spec_buf, width, topline + nlines);
21992 return decode_mode_spec_buf;
21993 no_value:
21995 char* p = decode_mode_spec_buf;
21996 int pad = width - 2;
21997 while (pad-- > 0)
21998 *p++ = ' ';
21999 *p++ = '?';
22000 *p++ = '?';
22001 *p = '\0';
22002 return decode_mode_spec_buf;
22005 break;
22007 case 'm':
22008 obj = BVAR (b, mode_name);
22009 break;
22011 case 'n':
22012 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22013 return " Narrow";
22014 break;
22016 case 'p':
22018 ptrdiff_t pos = marker_position (w->start);
22019 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22021 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
22023 if (pos <= BUF_BEGV (b))
22024 return "All";
22025 else
22026 return "Bottom";
22028 else if (pos <= BUF_BEGV (b))
22029 return "Top";
22030 else
22032 if (total > 1000000)
22033 /* Do it differently for a large value, to avoid overflow. */
22034 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22035 else
22036 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22037 /* We can't normally display a 3-digit number,
22038 so get us a 2-digit number that is close. */
22039 if (total == 100)
22040 total = 99;
22041 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22042 return decode_mode_spec_buf;
22046 /* Display percentage of size above the bottom of the screen. */
22047 case 'P':
22049 ptrdiff_t toppos = marker_position (w->start);
22050 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
22051 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22053 if (botpos >= BUF_ZV (b))
22055 if (toppos <= BUF_BEGV (b))
22056 return "All";
22057 else
22058 return "Bottom";
22060 else
22062 if (total > 1000000)
22063 /* Do it differently for a large value, to avoid overflow. */
22064 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22065 else
22066 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22067 /* We can't normally display a 3-digit number,
22068 so get us a 2-digit number that is close. */
22069 if (total == 100)
22070 total = 99;
22071 if (toppos <= BUF_BEGV (b))
22072 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22073 else
22074 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22075 return decode_mode_spec_buf;
22079 case 's':
22080 /* status of process */
22081 obj = Fget_buffer_process (Fcurrent_buffer ());
22082 if (NILP (obj))
22083 return "no process";
22084 #ifndef MSDOS
22085 obj = Fsymbol_name (Fprocess_status (obj));
22086 #endif
22087 break;
22089 case '@':
22091 ptrdiff_t count = inhibit_garbage_collection ();
22092 Lisp_Object val = call1 (intern ("file-remote-p"),
22093 BVAR (current_buffer, directory));
22094 unbind_to (count, Qnil);
22096 if (NILP (val))
22097 return "-";
22098 else
22099 return "@";
22102 case 'z':
22103 /* coding-system (not including end-of-line format) */
22104 case 'Z':
22105 /* coding-system (including end-of-line type) */
22107 int eol_flag = (c == 'Z');
22108 char *p = decode_mode_spec_buf;
22110 if (! FRAME_WINDOW_P (f))
22112 /* No need to mention EOL here--the terminal never needs
22113 to do EOL conversion. */
22114 p = decode_mode_spec_coding (CODING_ID_NAME
22115 (FRAME_KEYBOARD_CODING (f)->id),
22116 p, 0);
22117 p = decode_mode_spec_coding (CODING_ID_NAME
22118 (FRAME_TERMINAL_CODING (f)->id),
22119 p, 0);
22121 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22122 p, eol_flag);
22124 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22125 #ifdef subprocesses
22126 obj = Fget_buffer_process (Fcurrent_buffer ());
22127 if (PROCESSP (obj))
22129 p = decode_mode_spec_coding
22130 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22131 p = decode_mode_spec_coding
22132 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22134 #endif /* subprocesses */
22135 #endif /* 0 */
22136 *p = 0;
22137 return decode_mode_spec_buf;
22141 if (STRINGP (obj))
22143 *string = obj;
22144 return SSDATA (obj);
22146 else
22147 return "";
22151 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22152 means count lines back from START_BYTE. But don't go beyond
22153 LIMIT_BYTE. Return the number of lines thus found (always
22154 nonnegative).
22156 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22157 either the position COUNT lines after/before START_BYTE, if we
22158 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22159 COUNT lines. */
22161 static ptrdiff_t
22162 display_count_lines (ptrdiff_t start_byte,
22163 ptrdiff_t limit_byte, ptrdiff_t count,
22164 ptrdiff_t *byte_pos_ptr)
22166 register unsigned char *cursor;
22167 unsigned char *base;
22169 register ptrdiff_t ceiling;
22170 register unsigned char *ceiling_addr;
22171 ptrdiff_t orig_count = count;
22173 /* If we are not in selective display mode,
22174 check only for newlines. */
22175 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22176 && !INTEGERP (BVAR (current_buffer, selective_display)));
22178 if (count > 0)
22180 while (start_byte < limit_byte)
22182 ceiling = BUFFER_CEILING_OF (start_byte);
22183 ceiling = min (limit_byte - 1, ceiling);
22184 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22185 base = (cursor = BYTE_POS_ADDR (start_byte));
22189 if (selective_display)
22191 while (*cursor != '\n' && *cursor != 015
22192 && ++cursor != ceiling_addr)
22193 continue;
22194 if (cursor == ceiling_addr)
22195 break;
22197 else
22199 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22200 if (! cursor)
22201 break;
22204 cursor++;
22206 if (--count == 0)
22208 start_byte += cursor - base;
22209 *byte_pos_ptr = start_byte;
22210 return orig_count;
22213 while (cursor < ceiling_addr);
22215 start_byte += ceiling_addr - base;
22218 else
22220 while (start_byte > limit_byte)
22222 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22223 ceiling = max (limit_byte, ceiling);
22224 ceiling_addr = BYTE_POS_ADDR (ceiling);
22225 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22226 while (1)
22228 if (selective_display)
22230 while (--cursor >= ceiling_addr
22231 && *cursor != '\n' && *cursor != 015)
22232 continue;
22233 if (cursor < ceiling_addr)
22234 break;
22236 else
22238 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22239 if (! cursor)
22240 break;
22243 if (++count == 0)
22245 start_byte += cursor - base + 1;
22246 *byte_pos_ptr = start_byte;
22247 /* When scanning backwards, we should
22248 not count the newline posterior to which we stop. */
22249 return - orig_count - 1;
22252 start_byte += ceiling_addr - base;
22256 *byte_pos_ptr = limit_byte;
22258 if (count < 0)
22259 return - orig_count + count;
22260 return orig_count - count;
22266 /***********************************************************************
22267 Displaying strings
22268 ***********************************************************************/
22270 /* Display a NUL-terminated string, starting with index START.
22272 If STRING is non-null, display that C string. Otherwise, the Lisp
22273 string LISP_STRING is displayed. There's a case that STRING is
22274 non-null and LISP_STRING is not nil. It means STRING is a string
22275 data of LISP_STRING. In that case, we display LISP_STRING while
22276 ignoring its text properties.
22278 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22279 FACE_STRING. Display STRING or LISP_STRING with the face at
22280 FACE_STRING_POS in FACE_STRING:
22282 Display the string in the environment given by IT, but use the
22283 standard display table, temporarily.
22285 FIELD_WIDTH is the minimum number of output glyphs to produce.
22286 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22287 with spaces. If STRING has more characters, more than FIELD_WIDTH
22288 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22290 PRECISION is the maximum number of characters to output from
22291 STRING. PRECISION < 0 means don't truncate the string.
22293 This is roughly equivalent to printf format specifiers:
22295 FIELD_WIDTH PRECISION PRINTF
22296 ----------------------------------------
22297 -1 -1 %s
22298 -1 10 %.10s
22299 10 -1 %10s
22300 20 10 %20.10s
22302 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22303 display them, and < 0 means obey the current buffer's value of
22304 enable_multibyte_characters.
22306 Value is the number of columns displayed. */
22308 static int
22309 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22310 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22311 int field_width, int precision, int max_x, int multibyte)
22313 int hpos_at_start = it->hpos;
22314 int saved_face_id = it->face_id;
22315 struct glyph_row *row = it->glyph_row;
22316 ptrdiff_t it_charpos;
22318 /* Initialize the iterator IT for iteration over STRING beginning
22319 with index START. */
22320 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22321 precision, field_width, multibyte);
22322 if (string && STRINGP (lisp_string))
22323 /* LISP_STRING is the one returned by decode_mode_spec. We should
22324 ignore its text properties. */
22325 it->stop_charpos = it->end_charpos;
22327 /* If displaying STRING, set up the face of the iterator from
22328 FACE_STRING, if that's given. */
22329 if (STRINGP (face_string))
22331 ptrdiff_t endptr;
22332 struct face *face;
22334 it->face_id
22335 = face_at_string_position (it->w, face_string, face_string_pos,
22336 0, it->region_beg_charpos,
22337 it->region_end_charpos,
22338 &endptr, it->base_face_id, 0);
22339 face = FACE_FROM_ID (it->f, it->face_id);
22340 it->face_box_p = face->box != FACE_NO_BOX;
22343 /* Set max_x to the maximum allowed X position. Don't let it go
22344 beyond the right edge of the window. */
22345 if (max_x <= 0)
22346 max_x = it->last_visible_x;
22347 else
22348 max_x = min (max_x, it->last_visible_x);
22350 /* Skip over display elements that are not visible. because IT->w is
22351 hscrolled. */
22352 if (it->current_x < it->first_visible_x)
22353 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22354 MOVE_TO_POS | MOVE_TO_X);
22356 row->ascent = it->max_ascent;
22357 row->height = it->max_ascent + it->max_descent;
22358 row->phys_ascent = it->max_phys_ascent;
22359 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22360 row->extra_line_spacing = it->max_extra_line_spacing;
22362 if (STRINGP (it->string))
22363 it_charpos = IT_STRING_CHARPOS (*it);
22364 else
22365 it_charpos = IT_CHARPOS (*it);
22367 /* This condition is for the case that we are called with current_x
22368 past last_visible_x. */
22369 while (it->current_x < max_x)
22371 int x_before, x, n_glyphs_before, i, nglyphs;
22373 /* Get the next display element. */
22374 if (!get_next_display_element (it))
22375 break;
22377 /* Produce glyphs. */
22378 x_before = it->current_x;
22379 n_glyphs_before = row->used[TEXT_AREA];
22380 PRODUCE_GLYPHS (it);
22382 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22383 i = 0;
22384 x = x_before;
22385 while (i < nglyphs)
22387 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22389 if (it->line_wrap != TRUNCATE
22390 && x + glyph->pixel_width > max_x)
22392 /* End of continued line or max_x reached. */
22393 if (CHAR_GLYPH_PADDING_P (*glyph))
22395 /* A wide character is unbreakable. */
22396 if (row->reversed_p)
22397 unproduce_glyphs (it, row->used[TEXT_AREA]
22398 - n_glyphs_before);
22399 row->used[TEXT_AREA] = n_glyphs_before;
22400 it->current_x = x_before;
22402 else
22404 if (row->reversed_p)
22405 unproduce_glyphs (it, row->used[TEXT_AREA]
22406 - (n_glyphs_before + i));
22407 row->used[TEXT_AREA] = n_glyphs_before + i;
22408 it->current_x = x;
22410 break;
22412 else if (x + glyph->pixel_width >= it->first_visible_x)
22414 /* Glyph is at least partially visible. */
22415 ++it->hpos;
22416 if (x < it->first_visible_x)
22417 row->x = x - it->first_visible_x;
22419 else
22421 /* Glyph is off the left margin of the display area.
22422 Should not happen. */
22423 emacs_abort ();
22426 row->ascent = max (row->ascent, it->max_ascent);
22427 row->height = max (row->height, it->max_ascent + it->max_descent);
22428 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22429 row->phys_height = max (row->phys_height,
22430 it->max_phys_ascent + it->max_phys_descent);
22431 row->extra_line_spacing = max (row->extra_line_spacing,
22432 it->max_extra_line_spacing);
22433 x += glyph->pixel_width;
22434 ++i;
22437 /* Stop if max_x reached. */
22438 if (i < nglyphs)
22439 break;
22441 /* Stop at line ends. */
22442 if (ITERATOR_AT_END_OF_LINE_P (it))
22444 it->continuation_lines_width = 0;
22445 break;
22448 set_iterator_to_next (it, 1);
22449 if (STRINGP (it->string))
22450 it_charpos = IT_STRING_CHARPOS (*it);
22451 else
22452 it_charpos = IT_CHARPOS (*it);
22454 /* Stop if truncating at the right edge. */
22455 if (it->line_wrap == TRUNCATE
22456 && it->current_x >= it->last_visible_x)
22458 /* Add truncation mark, but don't do it if the line is
22459 truncated at a padding space. */
22460 if (it_charpos < it->string_nchars)
22462 if (!FRAME_WINDOW_P (it->f))
22464 int ii, n;
22466 if (it->current_x > it->last_visible_x)
22468 if (!row->reversed_p)
22470 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22471 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22472 break;
22474 else
22476 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22477 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22478 break;
22479 unproduce_glyphs (it, ii + 1);
22480 ii = row->used[TEXT_AREA] - (ii + 1);
22482 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22484 row->used[TEXT_AREA] = ii;
22485 produce_special_glyphs (it, IT_TRUNCATION);
22488 produce_special_glyphs (it, IT_TRUNCATION);
22490 row->truncated_on_right_p = 1;
22492 break;
22496 /* Maybe insert a truncation at the left. */
22497 if (it->first_visible_x
22498 && it_charpos > 0)
22500 if (!FRAME_WINDOW_P (it->f)
22501 || (row->reversed_p
22502 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22503 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22504 insert_left_trunc_glyphs (it);
22505 row->truncated_on_left_p = 1;
22508 it->face_id = saved_face_id;
22510 /* Value is number of columns displayed. */
22511 return it->hpos - hpos_at_start;
22516 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22517 appears as an element of LIST or as the car of an element of LIST.
22518 If PROPVAL is a list, compare each element against LIST in that
22519 way, and return 1/2 if any element of PROPVAL is found in LIST.
22520 Otherwise return 0. This function cannot quit.
22521 The return value is 2 if the text is invisible but with an ellipsis
22522 and 1 if it's invisible and without an ellipsis. */
22525 invisible_p (register Lisp_Object propval, Lisp_Object list)
22527 register Lisp_Object tail, proptail;
22529 for (tail = list; CONSP (tail); tail = XCDR (tail))
22531 register Lisp_Object tem;
22532 tem = XCAR (tail);
22533 if (EQ (propval, tem))
22534 return 1;
22535 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22536 return NILP (XCDR (tem)) ? 1 : 2;
22539 if (CONSP (propval))
22541 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22543 Lisp_Object propelt;
22544 propelt = XCAR (proptail);
22545 for (tail = list; CONSP (tail); tail = XCDR (tail))
22547 register Lisp_Object tem;
22548 tem = XCAR (tail);
22549 if (EQ (propelt, tem))
22550 return 1;
22551 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22552 return NILP (XCDR (tem)) ? 1 : 2;
22557 return 0;
22560 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22561 doc: /* Non-nil if the property makes the text invisible.
22562 POS-OR-PROP can be a marker or number, in which case it is taken to be
22563 a position in the current buffer and the value of the `invisible' property
22564 is checked; or it can be some other value, which is then presumed to be the
22565 value of the `invisible' property of the text of interest.
22566 The non-nil value returned can be t for truly invisible text or something
22567 else if the text is replaced by an ellipsis. */)
22568 (Lisp_Object pos_or_prop)
22570 Lisp_Object prop
22571 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22572 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22573 : pos_or_prop);
22574 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22575 return (invis == 0 ? Qnil
22576 : invis == 1 ? Qt
22577 : make_number (invis));
22580 /* Calculate a width or height in pixels from a specification using
22581 the following elements:
22583 SPEC ::=
22584 NUM - a (fractional) multiple of the default font width/height
22585 (NUM) - specifies exactly NUM pixels
22586 UNIT - a fixed number of pixels, see below.
22587 ELEMENT - size of a display element in pixels, see below.
22588 (NUM . SPEC) - equals NUM * SPEC
22589 (+ SPEC SPEC ...) - add pixel values
22590 (- SPEC SPEC ...) - subtract pixel values
22591 (- SPEC) - negate pixel value
22593 NUM ::=
22594 INT or FLOAT - a number constant
22595 SYMBOL - use symbol's (buffer local) variable binding.
22597 UNIT ::=
22598 in - pixels per inch *)
22599 mm - pixels per 1/1000 meter *)
22600 cm - pixels per 1/100 meter *)
22601 width - width of current font in pixels.
22602 height - height of current font in pixels.
22604 *) using the ratio(s) defined in display-pixels-per-inch.
22606 ELEMENT ::=
22608 left-fringe - left fringe width in pixels
22609 right-fringe - right fringe width in pixels
22611 left-margin - left margin width in pixels
22612 right-margin - right margin width in pixels
22614 scroll-bar - scroll-bar area width in pixels
22616 Examples:
22618 Pixels corresponding to 5 inches:
22619 (5 . in)
22621 Total width of non-text areas on left side of window (if scroll-bar is on left):
22622 '(space :width (+ left-fringe left-margin scroll-bar))
22624 Align to first text column (in header line):
22625 '(space :align-to 0)
22627 Align to middle of text area minus half the width of variable `my-image'
22628 containing a loaded image:
22629 '(space :align-to (0.5 . (- text my-image)))
22631 Width of left margin minus width of 1 character in the default font:
22632 '(space :width (- left-margin 1))
22634 Width of left margin minus width of 2 characters in the current font:
22635 '(space :width (- left-margin (2 . width)))
22637 Center 1 character over left-margin (in header line):
22638 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22640 Different ways to express width of left fringe plus left margin minus one pixel:
22641 '(space :width (- (+ left-fringe left-margin) (1)))
22642 '(space :width (+ left-fringe left-margin (- (1))))
22643 '(space :width (+ left-fringe left-margin (-1)))
22647 static int
22648 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22649 struct font *font, int width_p, int *align_to)
22651 double pixels;
22653 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22654 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22656 if (NILP (prop))
22657 return OK_PIXELS (0);
22659 eassert (FRAME_LIVE_P (it->f));
22661 if (SYMBOLP (prop))
22663 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22665 char *unit = SSDATA (SYMBOL_NAME (prop));
22667 if (unit[0] == 'i' && unit[1] == 'n')
22668 pixels = 1.0;
22669 else if (unit[0] == 'm' && unit[1] == 'm')
22670 pixels = 25.4;
22671 else if (unit[0] == 'c' && unit[1] == 'm')
22672 pixels = 2.54;
22673 else
22674 pixels = 0;
22675 if (pixels > 0)
22677 double ppi = (width_p ? FRAME_RES_X (it->f)
22678 : FRAME_RES_Y (it->f));
22680 if (ppi > 0)
22681 return OK_PIXELS (ppi / pixels);
22682 return 0;
22686 #ifdef HAVE_WINDOW_SYSTEM
22687 if (EQ (prop, Qheight))
22688 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22689 if (EQ (prop, Qwidth))
22690 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22691 #else
22692 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22693 return OK_PIXELS (1);
22694 #endif
22696 if (EQ (prop, Qtext))
22697 return OK_PIXELS (width_p
22698 ? window_box_width (it->w, TEXT_AREA)
22699 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22701 if (align_to && *align_to < 0)
22703 *res = 0;
22704 if (EQ (prop, Qleft))
22705 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22706 if (EQ (prop, Qright))
22707 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22708 if (EQ (prop, Qcenter))
22709 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22710 + window_box_width (it->w, TEXT_AREA) / 2);
22711 if (EQ (prop, Qleft_fringe))
22712 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22713 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22714 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22715 if (EQ (prop, Qright_fringe))
22716 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22717 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22718 : window_box_right_offset (it->w, TEXT_AREA));
22719 if (EQ (prop, Qleft_margin))
22720 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22721 if (EQ (prop, Qright_margin))
22722 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22723 if (EQ (prop, Qscroll_bar))
22724 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22726 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22727 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22728 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22729 : 0)));
22731 else
22733 if (EQ (prop, Qleft_fringe))
22734 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22735 if (EQ (prop, Qright_fringe))
22736 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22737 if (EQ (prop, Qleft_margin))
22738 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22739 if (EQ (prop, Qright_margin))
22740 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22741 if (EQ (prop, Qscroll_bar))
22742 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22745 prop = buffer_local_value_1 (prop, it->w->contents);
22746 if (EQ (prop, Qunbound))
22747 prop = Qnil;
22750 if (INTEGERP (prop) || FLOATP (prop))
22752 int base_unit = (width_p
22753 ? FRAME_COLUMN_WIDTH (it->f)
22754 : FRAME_LINE_HEIGHT (it->f));
22755 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22758 if (CONSP (prop))
22760 Lisp_Object car = XCAR (prop);
22761 Lisp_Object cdr = XCDR (prop);
22763 if (SYMBOLP (car))
22765 #ifdef HAVE_WINDOW_SYSTEM
22766 if (FRAME_WINDOW_P (it->f)
22767 && valid_image_p (prop))
22769 ptrdiff_t id = lookup_image (it->f, prop);
22770 struct image *img = IMAGE_FROM_ID (it->f, id);
22772 return OK_PIXELS (width_p ? img->width : img->height);
22774 #endif
22775 if (EQ (car, Qplus) || EQ (car, Qminus))
22777 int first = 1;
22778 double px;
22780 pixels = 0;
22781 while (CONSP (cdr))
22783 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22784 font, width_p, align_to))
22785 return 0;
22786 if (first)
22787 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22788 else
22789 pixels += px;
22790 cdr = XCDR (cdr);
22792 if (EQ (car, Qminus))
22793 pixels = -pixels;
22794 return OK_PIXELS (pixels);
22797 car = buffer_local_value_1 (car, it->w->contents);
22798 if (EQ (car, Qunbound))
22799 car = Qnil;
22802 if (INTEGERP (car) || FLOATP (car))
22804 double fact;
22805 pixels = XFLOATINT (car);
22806 if (NILP (cdr))
22807 return OK_PIXELS (pixels);
22808 if (calc_pixel_width_or_height (&fact, it, cdr,
22809 font, width_p, align_to))
22810 return OK_PIXELS (pixels * fact);
22811 return 0;
22814 return 0;
22817 return 0;
22821 /***********************************************************************
22822 Glyph Display
22823 ***********************************************************************/
22825 #ifdef HAVE_WINDOW_SYSTEM
22827 #ifdef GLYPH_DEBUG
22829 void
22830 dump_glyph_string (struct glyph_string *s)
22832 fprintf (stderr, "glyph string\n");
22833 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22834 s->x, s->y, s->width, s->height);
22835 fprintf (stderr, " ybase = %d\n", s->ybase);
22836 fprintf (stderr, " hl = %d\n", s->hl);
22837 fprintf (stderr, " left overhang = %d, right = %d\n",
22838 s->left_overhang, s->right_overhang);
22839 fprintf (stderr, " nchars = %d\n", s->nchars);
22840 fprintf (stderr, " extends to end of line = %d\n",
22841 s->extends_to_end_of_line_p);
22842 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22843 fprintf (stderr, " bg width = %d\n", s->background_width);
22846 #endif /* GLYPH_DEBUG */
22848 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22849 of XChar2b structures for S; it can't be allocated in
22850 init_glyph_string because it must be allocated via `alloca'. W
22851 is the window on which S is drawn. ROW and AREA are the glyph row
22852 and area within the row from which S is constructed. START is the
22853 index of the first glyph structure covered by S. HL is a
22854 face-override for drawing S. */
22856 #ifdef HAVE_NTGUI
22857 #define OPTIONAL_HDC(hdc) HDC hdc,
22858 #define DECLARE_HDC(hdc) HDC hdc;
22859 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22860 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22861 #endif
22863 #ifndef OPTIONAL_HDC
22864 #define OPTIONAL_HDC(hdc)
22865 #define DECLARE_HDC(hdc)
22866 #define ALLOCATE_HDC(hdc, f)
22867 #define RELEASE_HDC(hdc, f)
22868 #endif
22870 static void
22871 init_glyph_string (struct glyph_string *s,
22872 OPTIONAL_HDC (hdc)
22873 XChar2b *char2b, struct window *w, struct glyph_row *row,
22874 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22876 memset (s, 0, sizeof *s);
22877 s->w = w;
22878 s->f = XFRAME (w->frame);
22879 #ifdef HAVE_NTGUI
22880 s->hdc = hdc;
22881 #endif
22882 s->display = FRAME_X_DISPLAY (s->f);
22883 s->window = FRAME_X_WINDOW (s->f);
22884 s->char2b = char2b;
22885 s->hl = hl;
22886 s->row = row;
22887 s->area = area;
22888 s->first_glyph = row->glyphs[area] + start;
22889 s->height = row->height;
22890 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22891 s->ybase = s->y + row->ascent;
22895 /* Append the list of glyph strings with head H and tail T to the list
22896 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22898 static void
22899 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22900 struct glyph_string *h, struct glyph_string *t)
22902 if (h)
22904 if (*head)
22905 (*tail)->next = h;
22906 else
22907 *head = h;
22908 h->prev = *tail;
22909 *tail = t;
22914 /* Prepend the list of glyph strings with head H and tail T to the
22915 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22916 result. */
22918 static void
22919 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22920 struct glyph_string *h, struct glyph_string *t)
22922 if (h)
22924 if (*head)
22925 (*head)->prev = t;
22926 else
22927 *tail = t;
22928 t->next = *head;
22929 *head = h;
22934 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22935 Set *HEAD and *TAIL to the resulting list. */
22937 static void
22938 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22939 struct glyph_string *s)
22941 s->next = s->prev = NULL;
22942 append_glyph_string_lists (head, tail, s, s);
22946 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22947 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22948 make sure that X resources for the face returned are allocated.
22949 Value is a pointer to a realized face that is ready for display if
22950 DISPLAY_P is non-zero. */
22952 static struct face *
22953 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22954 XChar2b *char2b, int display_p)
22956 struct face *face = FACE_FROM_ID (f, face_id);
22957 unsigned code = 0;
22959 if (face->font)
22961 code = face->font->driver->encode_char (face->font, c);
22963 if (code == FONT_INVALID_CODE)
22964 code = 0;
22966 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22968 /* Make sure X resources of the face are allocated. */
22969 #ifdef HAVE_X_WINDOWS
22970 if (display_p)
22971 #endif
22973 eassert (face != NULL);
22974 PREPARE_FACE_FOR_DISPLAY (f, face);
22977 return face;
22981 /* Get face and two-byte form of character glyph GLYPH on frame F.
22982 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22983 a pointer to a realized face that is ready for display. */
22985 static struct face *
22986 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22987 XChar2b *char2b, int *two_byte_p)
22989 struct face *face;
22990 unsigned code = 0;
22992 eassert (glyph->type == CHAR_GLYPH);
22993 face = FACE_FROM_ID (f, glyph->face_id);
22995 /* Make sure X resources of the face are allocated. */
22996 eassert (face != NULL);
22997 PREPARE_FACE_FOR_DISPLAY (f, face);
22999 if (two_byte_p)
23000 *two_byte_p = 0;
23002 if (face->font)
23004 if (CHAR_BYTE8_P (glyph->u.ch))
23005 code = CHAR_TO_BYTE8 (glyph->u.ch);
23006 else
23007 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23009 if (code == FONT_INVALID_CODE)
23010 code = 0;
23013 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23014 return face;
23018 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23019 Return 1 if FONT has a glyph for C, otherwise return 0. */
23021 static int
23022 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23024 unsigned code;
23026 if (CHAR_BYTE8_P (c))
23027 code = CHAR_TO_BYTE8 (c);
23028 else
23029 code = font->driver->encode_char (font, c);
23031 if (code == FONT_INVALID_CODE)
23032 return 0;
23033 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23034 return 1;
23038 /* Fill glyph string S with composition components specified by S->cmp.
23040 BASE_FACE is the base face of the composition.
23041 S->cmp_from is the index of the first component for S.
23043 OVERLAPS non-zero means S should draw the foreground only, and use
23044 its physical height for clipping. See also draw_glyphs.
23046 Value is the index of a component not in S. */
23048 static int
23049 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23050 int overlaps)
23052 int i;
23053 /* For all glyphs of this composition, starting at the offset
23054 S->cmp_from, until we reach the end of the definition or encounter a
23055 glyph that requires the different face, add it to S. */
23056 struct face *face;
23058 eassert (s);
23060 s->for_overlaps = overlaps;
23061 s->face = NULL;
23062 s->font = NULL;
23063 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23065 int c = COMPOSITION_GLYPH (s->cmp, i);
23067 /* TAB in a composition means display glyphs with padding space
23068 on the left or right. */
23069 if (c != '\t')
23071 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23072 -1, Qnil);
23074 face = get_char_face_and_encoding (s->f, c, face_id,
23075 s->char2b + i, 1);
23076 if (face)
23078 if (! s->face)
23080 s->face = face;
23081 s->font = s->face->font;
23083 else if (s->face != face)
23084 break;
23087 ++s->nchars;
23089 s->cmp_to = i;
23091 if (s->face == NULL)
23093 s->face = base_face->ascii_face;
23094 s->font = s->face->font;
23097 /* All glyph strings for the same composition has the same width,
23098 i.e. the width set for the first component of the composition. */
23099 s->width = s->first_glyph->pixel_width;
23101 /* If the specified font could not be loaded, use the frame's
23102 default font, but record the fact that we couldn't load it in
23103 the glyph string so that we can draw rectangles for the
23104 characters of the glyph string. */
23105 if (s->font == NULL)
23107 s->font_not_found_p = 1;
23108 s->font = FRAME_FONT (s->f);
23111 /* Adjust base line for subscript/superscript text. */
23112 s->ybase += s->first_glyph->voffset;
23114 /* This glyph string must always be drawn with 16-bit functions. */
23115 s->two_byte_p = 1;
23117 return s->cmp_to;
23120 static int
23121 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23122 int start, int end, int overlaps)
23124 struct glyph *glyph, *last;
23125 Lisp_Object lgstring;
23126 int i;
23128 s->for_overlaps = overlaps;
23129 glyph = s->row->glyphs[s->area] + start;
23130 last = s->row->glyphs[s->area] + end;
23131 s->cmp_id = glyph->u.cmp.id;
23132 s->cmp_from = glyph->slice.cmp.from;
23133 s->cmp_to = glyph->slice.cmp.to + 1;
23134 s->face = FACE_FROM_ID (s->f, face_id);
23135 lgstring = composition_gstring_from_id (s->cmp_id);
23136 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23137 glyph++;
23138 while (glyph < last
23139 && glyph->u.cmp.automatic
23140 && glyph->u.cmp.id == s->cmp_id
23141 && s->cmp_to == glyph->slice.cmp.from)
23142 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23144 for (i = s->cmp_from; i < s->cmp_to; i++)
23146 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23147 unsigned code = LGLYPH_CODE (lglyph);
23149 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23151 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23152 return glyph - s->row->glyphs[s->area];
23156 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23157 See the comment of fill_glyph_string for arguments.
23158 Value is the index of the first glyph not in S. */
23161 static int
23162 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23163 int start, int end, int overlaps)
23165 struct glyph *glyph, *last;
23166 int voffset;
23168 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23169 s->for_overlaps = overlaps;
23170 glyph = s->row->glyphs[s->area] + start;
23171 last = s->row->glyphs[s->area] + end;
23172 voffset = glyph->voffset;
23173 s->face = FACE_FROM_ID (s->f, face_id);
23174 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23175 s->nchars = 1;
23176 s->width = glyph->pixel_width;
23177 glyph++;
23178 while (glyph < last
23179 && glyph->type == GLYPHLESS_GLYPH
23180 && glyph->voffset == voffset
23181 && glyph->face_id == face_id)
23183 s->nchars++;
23184 s->width += glyph->pixel_width;
23185 glyph++;
23187 s->ybase += voffset;
23188 return glyph - s->row->glyphs[s->area];
23192 /* Fill glyph string S from a sequence of character glyphs.
23194 FACE_ID is the face id of the string. START is the index of the
23195 first glyph to consider, END is the index of the last + 1.
23196 OVERLAPS non-zero means S should draw the foreground only, and use
23197 its physical height for clipping. See also draw_glyphs.
23199 Value is the index of the first glyph not in S. */
23201 static int
23202 fill_glyph_string (struct glyph_string *s, int face_id,
23203 int start, int end, int overlaps)
23205 struct glyph *glyph, *last;
23206 int voffset;
23207 int glyph_not_available_p;
23209 eassert (s->f == XFRAME (s->w->frame));
23210 eassert (s->nchars == 0);
23211 eassert (start >= 0 && end > start);
23213 s->for_overlaps = overlaps;
23214 glyph = s->row->glyphs[s->area] + start;
23215 last = s->row->glyphs[s->area] + end;
23216 voffset = glyph->voffset;
23217 s->padding_p = glyph->padding_p;
23218 glyph_not_available_p = glyph->glyph_not_available_p;
23220 while (glyph < last
23221 && glyph->type == CHAR_GLYPH
23222 && glyph->voffset == voffset
23223 /* Same face id implies same font, nowadays. */
23224 && glyph->face_id == face_id
23225 && glyph->glyph_not_available_p == glyph_not_available_p)
23227 int two_byte_p;
23229 s->face = get_glyph_face_and_encoding (s->f, glyph,
23230 s->char2b + s->nchars,
23231 &two_byte_p);
23232 s->two_byte_p = two_byte_p;
23233 ++s->nchars;
23234 eassert (s->nchars <= end - start);
23235 s->width += glyph->pixel_width;
23236 if (glyph++->padding_p != s->padding_p)
23237 break;
23240 s->font = s->face->font;
23242 /* If the specified font could not be loaded, use the frame's font,
23243 but record the fact that we couldn't load it in
23244 S->font_not_found_p so that we can draw rectangles for the
23245 characters of the glyph string. */
23246 if (s->font == NULL || glyph_not_available_p)
23248 s->font_not_found_p = 1;
23249 s->font = FRAME_FONT (s->f);
23252 /* Adjust base line for subscript/superscript text. */
23253 s->ybase += voffset;
23255 eassert (s->face && s->face->gc);
23256 return glyph - s->row->glyphs[s->area];
23260 /* Fill glyph string S from image glyph S->first_glyph. */
23262 static void
23263 fill_image_glyph_string (struct glyph_string *s)
23265 eassert (s->first_glyph->type == IMAGE_GLYPH);
23266 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23267 eassert (s->img);
23268 s->slice = s->first_glyph->slice.img;
23269 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23270 s->font = s->face->font;
23271 s->width = s->first_glyph->pixel_width;
23273 /* Adjust base line for subscript/superscript text. */
23274 s->ybase += s->first_glyph->voffset;
23278 /* Fill glyph string S from a sequence of stretch glyphs.
23280 START is the index of the first glyph to consider,
23281 END is the index of the last + 1.
23283 Value is the index of the first glyph not in S. */
23285 static int
23286 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23288 struct glyph *glyph, *last;
23289 int voffset, face_id;
23291 eassert (s->first_glyph->type == STRETCH_GLYPH);
23293 glyph = s->row->glyphs[s->area] + start;
23294 last = s->row->glyphs[s->area] + end;
23295 face_id = glyph->face_id;
23296 s->face = FACE_FROM_ID (s->f, face_id);
23297 s->font = s->face->font;
23298 s->width = glyph->pixel_width;
23299 s->nchars = 1;
23300 voffset = glyph->voffset;
23302 for (++glyph;
23303 (glyph < last
23304 && glyph->type == STRETCH_GLYPH
23305 && glyph->voffset == voffset
23306 && glyph->face_id == face_id);
23307 ++glyph)
23308 s->width += glyph->pixel_width;
23310 /* Adjust base line for subscript/superscript text. */
23311 s->ybase += voffset;
23313 /* The case that face->gc == 0 is handled when drawing the glyph
23314 string by calling PREPARE_FACE_FOR_DISPLAY. */
23315 eassert (s->face);
23316 return glyph - s->row->glyphs[s->area];
23319 static struct font_metrics *
23320 get_per_char_metric (struct font *font, XChar2b *char2b)
23322 static struct font_metrics metrics;
23323 unsigned code;
23325 if (! font)
23326 return NULL;
23327 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23328 if (code == FONT_INVALID_CODE)
23329 return NULL;
23330 font->driver->text_extents (font, &code, 1, &metrics);
23331 return &metrics;
23334 /* EXPORT for RIF:
23335 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23336 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23337 assumed to be zero. */
23339 void
23340 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23342 *left = *right = 0;
23344 if (glyph->type == CHAR_GLYPH)
23346 struct face *face;
23347 XChar2b char2b;
23348 struct font_metrics *pcm;
23350 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23351 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23353 if (pcm->rbearing > pcm->width)
23354 *right = pcm->rbearing - pcm->width;
23355 if (pcm->lbearing < 0)
23356 *left = -pcm->lbearing;
23359 else if (glyph->type == COMPOSITE_GLYPH)
23361 if (! glyph->u.cmp.automatic)
23363 struct composition *cmp = composition_table[glyph->u.cmp.id];
23365 if (cmp->rbearing > cmp->pixel_width)
23366 *right = cmp->rbearing - cmp->pixel_width;
23367 if (cmp->lbearing < 0)
23368 *left = - cmp->lbearing;
23370 else
23372 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23373 struct font_metrics metrics;
23375 composition_gstring_width (gstring, glyph->slice.cmp.from,
23376 glyph->slice.cmp.to + 1, &metrics);
23377 if (metrics.rbearing > metrics.width)
23378 *right = metrics.rbearing - metrics.width;
23379 if (metrics.lbearing < 0)
23380 *left = - metrics.lbearing;
23386 /* Return the index of the first glyph preceding glyph string S that
23387 is overwritten by S because of S's left overhang. Value is -1
23388 if no glyphs are overwritten. */
23390 static int
23391 left_overwritten (struct glyph_string *s)
23393 int k;
23395 if (s->left_overhang)
23397 int x = 0, i;
23398 struct glyph *glyphs = s->row->glyphs[s->area];
23399 int first = s->first_glyph - glyphs;
23401 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23402 x -= glyphs[i].pixel_width;
23404 k = i + 1;
23406 else
23407 k = -1;
23409 return k;
23413 /* Return the index of the first glyph preceding glyph string S that
23414 is overwriting S because of its right overhang. Value is -1 if no
23415 glyph in front of S overwrites S. */
23417 static int
23418 left_overwriting (struct glyph_string *s)
23420 int i, k, x;
23421 struct glyph *glyphs = s->row->glyphs[s->area];
23422 int first = s->first_glyph - glyphs;
23424 k = -1;
23425 x = 0;
23426 for (i = first - 1; i >= 0; --i)
23428 int left, right;
23429 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23430 if (x + right > 0)
23431 k = i;
23432 x -= glyphs[i].pixel_width;
23435 return k;
23439 /* Return the index of the last glyph following glyph string S that is
23440 overwritten by S because of S's right overhang. Value is -1 if
23441 no such glyph is found. */
23443 static int
23444 right_overwritten (struct glyph_string *s)
23446 int k = -1;
23448 if (s->right_overhang)
23450 int x = 0, i;
23451 struct glyph *glyphs = s->row->glyphs[s->area];
23452 int first = (s->first_glyph - glyphs
23453 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23454 int end = s->row->used[s->area];
23456 for (i = first; i < end && s->right_overhang > x; ++i)
23457 x += glyphs[i].pixel_width;
23459 k = i;
23462 return k;
23466 /* Return the index of the last glyph following glyph string S that
23467 overwrites S because of its left overhang. Value is negative
23468 if no such glyph is found. */
23470 static int
23471 right_overwriting (struct glyph_string *s)
23473 int i, k, x;
23474 int end = s->row->used[s->area];
23475 struct glyph *glyphs = s->row->glyphs[s->area];
23476 int first = (s->first_glyph - glyphs
23477 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23479 k = -1;
23480 x = 0;
23481 for (i = first; i < end; ++i)
23483 int left, right;
23484 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23485 if (x - left < 0)
23486 k = i;
23487 x += glyphs[i].pixel_width;
23490 return k;
23494 /* Set background width of glyph string S. START is the index of the
23495 first glyph following S. LAST_X is the right-most x-position + 1
23496 in the drawing area. */
23498 static void
23499 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23501 /* If the face of this glyph string has to be drawn to the end of
23502 the drawing area, set S->extends_to_end_of_line_p. */
23504 if (start == s->row->used[s->area]
23505 && s->area == TEXT_AREA
23506 && ((s->row->fill_line_p
23507 && (s->hl == DRAW_NORMAL_TEXT
23508 || s->hl == DRAW_IMAGE_RAISED
23509 || s->hl == DRAW_IMAGE_SUNKEN))
23510 || s->hl == DRAW_MOUSE_FACE))
23511 s->extends_to_end_of_line_p = 1;
23513 /* If S extends its face to the end of the line, set its
23514 background_width to the distance to the right edge of the drawing
23515 area. */
23516 if (s->extends_to_end_of_line_p)
23517 s->background_width = last_x - s->x + 1;
23518 else
23519 s->background_width = s->width;
23523 /* Compute overhangs and x-positions for glyph string S and its
23524 predecessors, or successors. X is the starting x-position for S.
23525 BACKWARD_P non-zero means process predecessors. */
23527 static void
23528 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23530 if (backward_p)
23532 while (s)
23534 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23535 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23536 x -= s->width;
23537 s->x = x;
23538 s = s->prev;
23541 else
23543 while (s)
23545 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23546 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23547 s->x = x;
23548 x += s->width;
23549 s = s->next;
23556 /* The following macros are only called from draw_glyphs below.
23557 They reference the following parameters of that function directly:
23558 `w', `row', `area', and `overlap_p'
23559 as well as the following local variables:
23560 `s', `f', and `hdc' (in W32) */
23562 #ifdef HAVE_NTGUI
23563 /* On W32, silently add local `hdc' variable to argument list of
23564 init_glyph_string. */
23565 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23566 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23567 #else
23568 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23569 init_glyph_string (s, char2b, w, row, area, start, hl)
23570 #endif
23572 /* Add a glyph string for a stretch glyph to the list of strings
23573 between HEAD and TAIL. START is the index of the stretch glyph in
23574 row area AREA of glyph row ROW. END is the index of the last glyph
23575 in that glyph row area. X is the current output position assigned
23576 to the new glyph string constructed. HL overrides that face of the
23577 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23578 is the right-most x-position of the drawing area. */
23580 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23581 and below -- keep them on one line. */
23582 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23583 do \
23585 s = alloca (sizeof *s); \
23586 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23587 START = fill_stretch_glyph_string (s, START, END); \
23588 append_glyph_string (&HEAD, &TAIL, s); \
23589 s->x = (X); \
23591 while (0)
23594 /* Add a glyph string for an image glyph to the list of strings
23595 between HEAD and TAIL. START is the index of the image glyph in
23596 row area AREA of glyph row ROW. END is the index of the last glyph
23597 in that glyph row area. X is the current output position assigned
23598 to the new glyph string constructed. HL overrides that face of the
23599 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23600 is the right-most x-position of the drawing area. */
23602 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23603 do \
23605 s = alloca (sizeof *s); \
23606 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23607 fill_image_glyph_string (s); \
23608 append_glyph_string (&HEAD, &TAIL, s); \
23609 ++START; \
23610 s->x = (X); \
23612 while (0)
23615 /* Add a glyph string for a sequence of character glyphs to the list
23616 of strings between HEAD and TAIL. START is the index of the first
23617 glyph in row area AREA of glyph row ROW that is part of the new
23618 glyph string. END is the index of the last glyph in that glyph row
23619 area. X is the current output position assigned to the new glyph
23620 string constructed. HL overrides that face of the glyph; e.g. it
23621 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23622 right-most x-position of the drawing area. */
23624 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23625 do \
23627 int face_id; \
23628 XChar2b *char2b; \
23630 face_id = (row)->glyphs[area][START].face_id; \
23632 s = alloca (sizeof *s); \
23633 char2b = alloca ((END - START) * sizeof *char2b); \
23634 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23635 append_glyph_string (&HEAD, &TAIL, s); \
23636 s->x = (X); \
23637 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23639 while (0)
23642 /* Add a glyph string for a composite sequence to the list of strings
23643 between HEAD and TAIL. START is the index of the first glyph in
23644 row area AREA of glyph row ROW that is part of the new glyph
23645 string. END is the index of the last glyph in that glyph row area.
23646 X is the current output position assigned to the new glyph string
23647 constructed. HL overrides that face of the glyph; e.g. it is
23648 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23649 x-position of the drawing area. */
23651 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23652 do { \
23653 int face_id = (row)->glyphs[area][START].face_id; \
23654 struct face *base_face = FACE_FROM_ID (f, face_id); \
23655 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23656 struct composition *cmp = composition_table[cmp_id]; \
23657 XChar2b *char2b; \
23658 struct glyph_string *first_s = NULL; \
23659 int n; \
23661 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23663 /* Make glyph_strings for each glyph sequence that is drawable by \
23664 the same face, and append them to HEAD/TAIL. */ \
23665 for (n = 0; n < cmp->glyph_len;) \
23667 s = alloca (sizeof *s); \
23668 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23669 append_glyph_string (&(HEAD), &(TAIL), s); \
23670 s->cmp = cmp; \
23671 s->cmp_from = n; \
23672 s->x = (X); \
23673 if (n == 0) \
23674 first_s = s; \
23675 n = fill_composite_glyph_string (s, base_face, overlaps); \
23678 ++START; \
23679 s = first_s; \
23680 } while (0)
23683 /* Add a glyph string for a glyph-string sequence to the list of strings
23684 between HEAD and TAIL. */
23686 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23687 do { \
23688 int face_id; \
23689 XChar2b *char2b; \
23690 Lisp_Object gstring; \
23692 face_id = (row)->glyphs[area][START].face_id; \
23693 gstring = (composition_gstring_from_id \
23694 ((row)->glyphs[area][START].u.cmp.id)); \
23695 s = alloca (sizeof *s); \
23696 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23697 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23698 append_glyph_string (&(HEAD), &(TAIL), s); \
23699 s->x = (X); \
23700 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23701 } while (0)
23704 /* Add a glyph string for a sequence of glyphless character's glyphs
23705 to the list of strings between HEAD and TAIL. The meanings of
23706 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23708 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23709 do \
23711 int face_id; \
23713 face_id = (row)->glyphs[area][START].face_id; \
23715 s = alloca (sizeof *s); \
23716 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23717 append_glyph_string (&HEAD, &TAIL, s); \
23718 s->x = (X); \
23719 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23720 overlaps); \
23722 while (0)
23725 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23726 of AREA of glyph row ROW on window W between indices START and END.
23727 HL overrides the face for drawing glyph strings, e.g. it is
23728 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23729 x-positions of the drawing area.
23731 This is an ugly monster macro construct because we must use alloca
23732 to allocate glyph strings (because draw_glyphs can be called
23733 asynchronously). */
23735 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23736 do \
23738 HEAD = TAIL = NULL; \
23739 while (START < END) \
23741 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23742 switch (first_glyph->type) \
23744 case CHAR_GLYPH: \
23745 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23746 HL, X, LAST_X); \
23747 break; \
23749 case COMPOSITE_GLYPH: \
23750 if (first_glyph->u.cmp.automatic) \
23751 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23752 HL, X, LAST_X); \
23753 else \
23754 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23755 HL, X, LAST_X); \
23756 break; \
23758 case STRETCH_GLYPH: \
23759 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23760 HL, X, LAST_X); \
23761 break; \
23763 case IMAGE_GLYPH: \
23764 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23765 HL, X, LAST_X); \
23766 break; \
23768 case GLYPHLESS_GLYPH: \
23769 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23770 HL, X, LAST_X); \
23771 break; \
23773 default: \
23774 emacs_abort (); \
23777 if (s) \
23779 set_glyph_string_background_width (s, START, LAST_X); \
23780 (X) += s->width; \
23783 } while (0)
23786 /* Draw glyphs between START and END in AREA of ROW on window W,
23787 starting at x-position X. X is relative to AREA in W. HL is a
23788 face-override with the following meaning:
23790 DRAW_NORMAL_TEXT draw normally
23791 DRAW_CURSOR draw in cursor face
23792 DRAW_MOUSE_FACE draw in mouse face.
23793 DRAW_INVERSE_VIDEO draw in mode line face
23794 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23795 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23797 If OVERLAPS is non-zero, draw only the foreground of characters and
23798 clip to the physical height of ROW. Non-zero value also defines
23799 the overlapping part to be drawn:
23801 OVERLAPS_PRED overlap with preceding rows
23802 OVERLAPS_SUCC overlap with succeeding rows
23803 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23804 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23806 Value is the x-position reached, relative to AREA of W. */
23808 static int
23809 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23810 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23811 enum draw_glyphs_face hl, int overlaps)
23813 struct glyph_string *head, *tail;
23814 struct glyph_string *s;
23815 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23816 int i, j, x_reached, last_x, area_left = 0;
23817 struct frame *f = XFRAME (WINDOW_FRAME (w));
23818 DECLARE_HDC (hdc);
23820 ALLOCATE_HDC (hdc, f);
23822 /* Let's rather be paranoid than getting a SEGV. */
23823 end = min (end, row->used[area]);
23824 start = clip_to_bounds (0, start, end);
23826 /* Translate X to frame coordinates. Set last_x to the right
23827 end of the drawing area. */
23828 if (row->full_width_p)
23830 /* X is relative to the left edge of W, without scroll bars
23831 or fringes. */
23832 area_left = WINDOW_LEFT_EDGE_X (w);
23833 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23835 else
23837 area_left = window_box_left (w, area);
23838 last_x = area_left + window_box_width (w, area);
23840 x += area_left;
23842 /* Build a doubly-linked list of glyph_string structures between
23843 head and tail from what we have to draw. Note that the macro
23844 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23845 the reason we use a separate variable `i'. */
23846 i = start;
23847 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23848 if (tail)
23849 x_reached = tail->x + tail->background_width;
23850 else
23851 x_reached = x;
23853 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23854 the row, redraw some glyphs in front or following the glyph
23855 strings built above. */
23856 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23858 struct glyph_string *h, *t;
23859 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23860 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23861 int check_mouse_face = 0;
23862 int dummy_x = 0;
23864 /* If mouse highlighting is on, we may need to draw adjacent
23865 glyphs using mouse-face highlighting. */
23866 if (area == TEXT_AREA && row->mouse_face_p
23867 && hlinfo->mouse_face_beg_row >= 0
23868 && hlinfo->mouse_face_end_row >= 0)
23870 struct glyph_row *mouse_beg_row, *mouse_end_row;
23872 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23873 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23875 if (row >= mouse_beg_row && row <= mouse_end_row)
23877 check_mouse_face = 1;
23878 mouse_beg_col = (row == mouse_beg_row)
23879 ? hlinfo->mouse_face_beg_col : 0;
23880 mouse_end_col = (row == mouse_end_row)
23881 ? hlinfo->mouse_face_end_col
23882 : row->used[TEXT_AREA];
23886 /* Compute overhangs for all glyph strings. */
23887 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23888 for (s = head; s; s = s->next)
23889 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23891 /* Prepend glyph strings for glyphs in front of the first glyph
23892 string that are overwritten because of the first glyph
23893 string's left overhang. The background of all strings
23894 prepended must be drawn because the first glyph string
23895 draws over it. */
23896 i = left_overwritten (head);
23897 if (i >= 0)
23899 enum draw_glyphs_face overlap_hl;
23901 /* If this row contains mouse highlighting, attempt to draw
23902 the overlapped glyphs with the correct highlight. This
23903 code fails if the overlap encompasses more than one glyph
23904 and mouse-highlight spans only some of these glyphs.
23905 However, making it work perfectly involves a lot more
23906 code, and I don't know if the pathological case occurs in
23907 practice, so we'll stick to this for now. --- cyd */
23908 if (check_mouse_face
23909 && mouse_beg_col < start && mouse_end_col > i)
23910 overlap_hl = DRAW_MOUSE_FACE;
23911 else
23912 overlap_hl = DRAW_NORMAL_TEXT;
23914 j = i;
23915 BUILD_GLYPH_STRINGS (j, start, h, t,
23916 overlap_hl, dummy_x, last_x);
23917 start = i;
23918 compute_overhangs_and_x (t, head->x, 1);
23919 prepend_glyph_string_lists (&head, &tail, h, t);
23920 clip_head = head;
23923 /* Prepend glyph strings for glyphs in front of the first glyph
23924 string that overwrite that glyph string because of their
23925 right overhang. For these strings, only the foreground must
23926 be drawn, because it draws over the glyph string at `head'.
23927 The background must not be drawn because this would overwrite
23928 right overhangs of preceding glyphs for which no glyph
23929 strings exist. */
23930 i = left_overwriting (head);
23931 if (i >= 0)
23933 enum draw_glyphs_face overlap_hl;
23935 if (check_mouse_face
23936 && mouse_beg_col < start && mouse_end_col > i)
23937 overlap_hl = DRAW_MOUSE_FACE;
23938 else
23939 overlap_hl = DRAW_NORMAL_TEXT;
23941 clip_head = head;
23942 BUILD_GLYPH_STRINGS (i, start, h, t,
23943 overlap_hl, dummy_x, last_x);
23944 for (s = h; s; s = s->next)
23945 s->background_filled_p = 1;
23946 compute_overhangs_and_x (t, head->x, 1);
23947 prepend_glyph_string_lists (&head, &tail, h, t);
23950 /* Append glyphs strings for glyphs following the last glyph
23951 string tail that are overwritten by tail. The background of
23952 these strings has to be drawn because tail's foreground draws
23953 over it. */
23954 i = right_overwritten (tail);
23955 if (i >= 0)
23957 enum draw_glyphs_face overlap_hl;
23959 if (check_mouse_face
23960 && mouse_beg_col < i && mouse_end_col > end)
23961 overlap_hl = DRAW_MOUSE_FACE;
23962 else
23963 overlap_hl = DRAW_NORMAL_TEXT;
23965 BUILD_GLYPH_STRINGS (end, i, h, t,
23966 overlap_hl, x, last_x);
23967 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23968 we don't have `end = i;' here. */
23969 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23970 append_glyph_string_lists (&head, &tail, h, t);
23971 clip_tail = tail;
23974 /* Append glyph strings for glyphs following the last glyph
23975 string tail that overwrite tail. The foreground of such
23976 glyphs has to be drawn because it writes into the background
23977 of tail. The background must not be drawn because it could
23978 paint over the foreground of following glyphs. */
23979 i = right_overwriting (tail);
23980 if (i >= 0)
23982 enum draw_glyphs_face overlap_hl;
23983 if (check_mouse_face
23984 && mouse_beg_col < i && mouse_end_col > end)
23985 overlap_hl = DRAW_MOUSE_FACE;
23986 else
23987 overlap_hl = DRAW_NORMAL_TEXT;
23989 clip_tail = tail;
23990 i++; /* We must include the Ith glyph. */
23991 BUILD_GLYPH_STRINGS (end, i, h, t,
23992 overlap_hl, x, last_x);
23993 for (s = h; s; s = s->next)
23994 s->background_filled_p = 1;
23995 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23996 append_glyph_string_lists (&head, &tail, h, t);
23998 if (clip_head || clip_tail)
23999 for (s = head; s; s = s->next)
24001 s->clip_head = clip_head;
24002 s->clip_tail = clip_tail;
24006 /* Draw all strings. */
24007 for (s = head; s; s = s->next)
24008 FRAME_RIF (f)->draw_glyph_string (s);
24010 #ifndef HAVE_NS
24011 /* When focus a sole frame and move horizontally, this sets on_p to 0
24012 causing a failure to erase prev cursor position. */
24013 if (area == TEXT_AREA
24014 && !row->full_width_p
24015 /* When drawing overlapping rows, only the glyph strings'
24016 foreground is drawn, which doesn't erase a cursor
24017 completely. */
24018 && !overlaps)
24020 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24021 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24022 : (tail ? tail->x + tail->background_width : x));
24023 x0 -= area_left;
24024 x1 -= area_left;
24026 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24027 row->y, MATRIX_ROW_BOTTOM_Y (row));
24029 #endif
24031 /* Value is the x-position up to which drawn, relative to AREA of W.
24032 This doesn't include parts drawn because of overhangs. */
24033 if (row->full_width_p)
24034 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24035 else
24036 x_reached -= area_left;
24038 RELEASE_HDC (hdc, f);
24040 return x_reached;
24043 /* Expand row matrix if too narrow. Don't expand if area
24044 is not present. */
24046 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24048 if (!fonts_changed_p \
24049 && (it->glyph_row->glyphs[area] \
24050 < it->glyph_row->glyphs[area + 1])) \
24052 it->w->ncols_scale_factor++; \
24053 fonts_changed_p = 1; \
24057 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24058 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24060 static void
24061 append_glyph (struct it *it)
24063 struct glyph *glyph;
24064 enum glyph_row_area area = it->area;
24066 eassert (it->glyph_row);
24067 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24069 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24070 if (glyph < it->glyph_row->glyphs[area + 1])
24072 /* If the glyph row is reversed, we need to prepend the glyph
24073 rather than append it. */
24074 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24076 struct glyph *g;
24078 /* Make room for the additional glyph. */
24079 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24080 g[1] = *g;
24081 glyph = it->glyph_row->glyphs[area];
24083 glyph->charpos = CHARPOS (it->position);
24084 glyph->object = it->object;
24085 if (it->pixel_width > 0)
24087 glyph->pixel_width = it->pixel_width;
24088 glyph->padding_p = 0;
24090 else
24092 /* Assure at least 1-pixel width. Otherwise, cursor can't
24093 be displayed correctly. */
24094 glyph->pixel_width = 1;
24095 glyph->padding_p = 1;
24097 glyph->ascent = it->ascent;
24098 glyph->descent = it->descent;
24099 glyph->voffset = it->voffset;
24100 glyph->type = CHAR_GLYPH;
24101 glyph->avoid_cursor_p = it->avoid_cursor_p;
24102 glyph->multibyte_p = it->multibyte_p;
24103 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24105 /* In R2L rows, the left and the right box edges need to be
24106 drawn in reverse direction. */
24107 glyph->right_box_line_p = it->start_of_box_run_p;
24108 glyph->left_box_line_p = it->end_of_box_run_p;
24110 else
24112 glyph->left_box_line_p = it->start_of_box_run_p;
24113 glyph->right_box_line_p = it->end_of_box_run_p;
24115 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24116 || it->phys_descent > it->descent);
24117 glyph->glyph_not_available_p = it->glyph_not_available_p;
24118 glyph->face_id = it->face_id;
24119 glyph->u.ch = it->char_to_display;
24120 glyph->slice.img = null_glyph_slice;
24121 glyph->font_type = FONT_TYPE_UNKNOWN;
24122 if (it->bidi_p)
24124 glyph->resolved_level = it->bidi_it.resolved_level;
24125 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24126 emacs_abort ();
24127 glyph->bidi_type = it->bidi_it.type;
24129 else
24131 glyph->resolved_level = 0;
24132 glyph->bidi_type = UNKNOWN_BT;
24134 ++it->glyph_row->used[area];
24136 else
24137 IT_EXPAND_MATRIX_WIDTH (it, area);
24140 /* Store one glyph for the composition IT->cmp_it.id in
24141 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24142 non-null. */
24144 static void
24145 append_composite_glyph (struct it *it)
24147 struct glyph *glyph;
24148 enum glyph_row_area area = it->area;
24150 eassert (it->glyph_row);
24152 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24153 if (glyph < it->glyph_row->glyphs[area + 1])
24155 /* If the glyph row is reversed, we need to prepend the glyph
24156 rather than append it. */
24157 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24159 struct glyph *g;
24161 /* Make room for the new glyph. */
24162 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24163 g[1] = *g;
24164 glyph = it->glyph_row->glyphs[it->area];
24166 glyph->charpos = it->cmp_it.charpos;
24167 glyph->object = it->object;
24168 glyph->pixel_width = it->pixel_width;
24169 glyph->ascent = it->ascent;
24170 glyph->descent = it->descent;
24171 glyph->voffset = it->voffset;
24172 glyph->type = COMPOSITE_GLYPH;
24173 if (it->cmp_it.ch < 0)
24175 glyph->u.cmp.automatic = 0;
24176 glyph->u.cmp.id = it->cmp_it.id;
24177 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24179 else
24181 glyph->u.cmp.automatic = 1;
24182 glyph->u.cmp.id = it->cmp_it.id;
24183 glyph->slice.cmp.from = it->cmp_it.from;
24184 glyph->slice.cmp.to = it->cmp_it.to - 1;
24186 glyph->avoid_cursor_p = it->avoid_cursor_p;
24187 glyph->multibyte_p = it->multibyte_p;
24188 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24190 /* In R2L rows, the left and the right box edges need to be
24191 drawn in reverse direction. */
24192 glyph->right_box_line_p = it->start_of_box_run_p;
24193 glyph->left_box_line_p = it->end_of_box_run_p;
24195 else
24197 glyph->left_box_line_p = it->start_of_box_run_p;
24198 glyph->right_box_line_p = it->end_of_box_run_p;
24200 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24201 || it->phys_descent > it->descent);
24202 glyph->padding_p = 0;
24203 glyph->glyph_not_available_p = 0;
24204 glyph->face_id = it->face_id;
24205 glyph->font_type = FONT_TYPE_UNKNOWN;
24206 if (it->bidi_p)
24208 glyph->resolved_level = it->bidi_it.resolved_level;
24209 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24210 emacs_abort ();
24211 glyph->bidi_type = it->bidi_it.type;
24213 ++it->glyph_row->used[area];
24215 else
24216 IT_EXPAND_MATRIX_WIDTH (it, area);
24220 /* Change IT->ascent and IT->height according to the setting of
24221 IT->voffset. */
24223 static void
24224 take_vertical_position_into_account (struct it *it)
24226 if (it->voffset)
24228 if (it->voffset < 0)
24229 /* Increase the ascent so that we can display the text higher
24230 in the line. */
24231 it->ascent -= it->voffset;
24232 else
24233 /* Increase the descent so that we can display the text lower
24234 in the line. */
24235 it->descent += it->voffset;
24240 /* Produce glyphs/get display metrics for the image IT is loaded with.
24241 See the description of struct display_iterator in dispextern.h for
24242 an overview of struct display_iterator. */
24244 static void
24245 produce_image_glyph (struct it *it)
24247 struct image *img;
24248 struct face *face;
24249 int glyph_ascent, crop;
24250 struct glyph_slice slice;
24252 eassert (it->what == IT_IMAGE);
24254 face = FACE_FROM_ID (it->f, it->face_id);
24255 eassert (face);
24256 /* Make sure X resources of the face is loaded. */
24257 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24259 if (it->image_id < 0)
24261 /* Fringe bitmap. */
24262 it->ascent = it->phys_ascent = 0;
24263 it->descent = it->phys_descent = 0;
24264 it->pixel_width = 0;
24265 it->nglyphs = 0;
24266 return;
24269 img = IMAGE_FROM_ID (it->f, it->image_id);
24270 eassert (img);
24271 /* Make sure X resources of the image is loaded. */
24272 prepare_image_for_display (it->f, img);
24274 slice.x = slice.y = 0;
24275 slice.width = img->width;
24276 slice.height = img->height;
24278 if (INTEGERP (it->slice.x))
24279 slice.x = XINT (it->slice.x);
24280 else if (FLOATP (it->slice.x))
24281 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24283 if (INTEGERP (it->slice.y))
24284 slice.y = XINT (it->slice.y);
24285 else if (FLOATP (it->slice.y))
24286 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24288 if (INTEGERP (it->slice.width))
24289 slice.width = XINT (it->slice.width);
24290 else if (FLOATP (it->slice.width))
24291 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24293 if (INTEGERP (it->slice.height))
24294 slice.height = XINT (it->slice.height);
24295 else if (FLOATP (it->slice.height))
24296 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24298 if (slice.x >= img->width)
24299 slice.x = img->width;
24300 if (slice.y >= img->height)
24301 slice.y = img->height;
24302 if (slice.x + slice.width >= img->width)
24303 slice.width = img->width - slice.x;
24304 if (slice.y + slice.height > img->height)
24305 slice.height = img->height - slice.y;
24307 if (slice.width == 0 || slice.height == 0)
24308 return;
24310 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24312 it->descent = slice.height - glyph_ascent;
24313 if (slice.y == 0)
24314 it->descent += img->vmargin;
24315 if (slice.y + slice.height == img->height)
24316 it->descent += img->vmargin;
24317 it->phys_descent = it->descent;
24319 it->pixel_width = slice.width;
24320 if (slice.x == 0)
24321 it->pixel_width += img->hmargin;
24322 if (slice.x + slice.width == img->width)
24323 it->pixel_width += img->hmargin;
24325 /* It's quite possible for images to have an ascent greater than
24326 their height, so don't get confused in that case. */
24327 if (it->descent < 0)
24328 it->descent = 0;
24330 it->nglyphs = 1;
24332 if (face->box != FACE_NO_BOX)
24334 if (face->box_line_width > 0)
24336 if (slice.y == 0)
24337 it->ascent += face->box_line_width;
24338 if (slice.y + slice.height == img->height)
24339 it->descent += face->box_line_width;
24342 if (it->start_of_box_run_p && slice.x == 0)
24343 it->pixel_width += eabs (face->box_line_width);
24344 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24345 it->pixel_width += eabs (face->box_line_width);
24348 take_vertical_position_into_account (it);
24350 /* Automatically crop wide image glyphs at right edge so we can
24351 draw the cursor on same display row. */
24352 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24353 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24355 it->pixel_width -= crop;
24356 slice.width -= crop;
24359 if (it->glyph_row)
24361 struct glyph *glyph;
24362 enum glyph_row_area area = it->area;
24364 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24365 if (glyph < it->glyph_row->glyphs[area + 1])
24367 glyph->charpos = CHARPOS (it->position);
24368 glyph->object = it->object;
24369 glyph->pixel_width = it->pixel_width;
24370 glyph->ascent = glyph_ascent;
24371 glyph->descent = it->descent;
24372 glyph->voffset = it->voffset;
24373 glyph->type = IMAGE_GLYPH;
24374 glyph->avoid_cursor_p = it->avoid_cursor_p;
24375 glyph->multibyte_p = it->multibyte_p;
24376 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24378 /* In R2L rows, the left and the right box edges need to be
24379 drawn in reverse direction. */
24380 glyph->right_box_line_p = it->start_of_box_run_p;
24381 glyph->left_box_line_p = it->end_of_box_run_p;
24383 else
24385 glyph->left_box_line_p = it->start_of_box_run_p;
24386 glyph->right_box_line_p = it->end_of_box_run_p;
24388 glyph->overlaps_vertically_p = 0;
24389 glyph->padding_p = 0;
24390 glyph->glyph_not_available_p = 0;
24391 glyph->face_id = it->face_id;
24392 glyph->u.img_id = img->id;
24393 glyph->slice.img = slice;
24394 glyph->font_type = FONT_TYPE_UNKNOWN;
24395 if (it->bidi_p)
24397 glyph->resolved_level = it->bidi_it.resolved_level;
24398 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24399 emacs_abort ();
24400 glyph->bidi_type = it->bidi_it.type;
24402 ++it->glyph_row->used[area];
24404 else
24405 IT_EXPAND_MATRIX_WIDTH (it, area);
24410 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24411 of the glyph, WIDTH and HEIGHT are the width and height of the
24412 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24414 static void
24415 append_stretch_glyph (struct it *it, Lisp_Object object,
24416 int width, int height, int ascent)
24418 struct glyph *glyph;
24419 enum glyph_row_area area = it->area;
24421 eassert (ascent >= 0 && ascent <= height);
24423 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24424 if (glyph < it->glyph_row->glyphs[area + 1])
24426 /* If the glyph row is reversed, we need to prepend the glyph
24427 rather than append it. */
24428 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24430 struct glyph *g;
24432 /* Make room for the additional glyph. */
24433 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24434 g[1] = *g;
24435 glyph = it->glyph_row->glyphs[area];
24437 glyph->charpos = CHARPOS (it->position);
24438 glyph->object = object;
24439 glyph->pixel_width = width;
24440 glyph->ascent = ascent;
24441 glyph->descent = height - ascent;
24442 glyph->voffset = it->voffset;
24443 glyph->type = STRETCH_GLYPH;
24444 glyph->avoid_cursor_p = it->avoid_cursor_p;
24445 glyph->multibyte_p = it->multibyte_p;
24446 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24448 /* In R2L rows, the left and the right box edges need to be
24449 drawn in reverse direction. */
24450 glyph->right_box_line_p = it->start_of_box_run_p;
24451 glyph->left_box_line_p = it->end_of_box_run_p;
24453 else
24455 glyph->left_box_line_p = it->start_of_box_run_p;
24456 glyph->right_box_line_p = it->end_of_box_run_p;
24458 glyph->overlaps_vertically_p = 0;
24459 glyph->padding_p = 0;
24460 glyph->glyph_not_available_p = 0;
24461 glyph->face_id = it->face_id;
24462 glyph->u.stretch.ascent = ascent;
24463 glyph->u.stretch.height = height;
24464 glyph->slice.img = null_glyph_slice;
24465 glyph->font_type = FONT_TYPE_UNKNOWN;
24466 if (it->bidi_p)
24468 glyph->resolved_level = it->bidi_it.resolved_level;
24469 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24470 emacs_abort ();
24471 glyph->bidi_type = it->bidi_it.type;
24473 else
24475 glyph->resolved_level = 0;
24476 glyph->bidi_type = UNKNOWN_BT;
24478 ++it->glyph_row->used[area];
24480 else
24481 IT_EXPAND_MATRIX_WIDTH (it, area);
24484 #endif /* HAVE_WINDOW_SYSTEM */
24486 /* Produce a stretch glyph for iterator IT. IT->object is the value
24487 of the glyph property displayed. The value must be a list
24488 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24489 being recognized:
24491 1. `:width WIDTH' specifies that the space should be WIDTH *
24492 canonical char width wide. WIDTH may be an integer or floating
24493 point number.
24495 2. `:relative-width FACTOR' specifies that the width of the stretch
24496 should be computed from the width of the first character having the
24497 `glyph' property, and should be FACTOR times that width.
24499 3. `:align-to HPOS' specifies that the space should be wide enough
24500 to reach HPOS, a value in canonical character units.
24502 Exactly one of the above pairs must be present.
24504 4. `:height HEIGHT' specifies that the height of the stretch produced
24505 should be HEIGHT, measured in canonical character units.
24507 5. `:relative-height FACTOR' specifies that the height of the
24508 stretch should be FACTOR times the height of the characters having
24509 the glyph property.
24511 Either none or exactly one of 4 or 5 must be present.
24513 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24514 of the stretch should be used for the ascent of the stretch.
24515 ASCENT must be in the range 0 <= ASCENT <= 100. */
24517 void
24518 produce_stretch_glyph (struct it *it)
24520 /* (space :width WIDTH :height HEIGHT ...) */
24521 Lisp_Object prop, plist;
24522 int width = 0, height = 0, align_to = -1;
24523 int zero_width_ok_p = 0;
24524 double tem;
24525 struct font *font = NULL;
24527 #ifdef HAVE_WINDOW_SYSTEM
24528 int ascent = 0;
24529 int zero_height_ok_p = 0;
24531 if (FRAME_WINDOW_P (it->f))
24533 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24534 font = face->font ? face->font : FRAME_FONT (it->f);
24535 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24537 #endif
24539 /* List should start with `space'. */
24540 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24541 plist = XCDR (it->object);
24543 /* Compute the width of the stretch. */
24544 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24545 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24547 /* Absolute width `:width WIDTH' specified and valid. */
24548 zero_width_ok_p = 1;
24549 width = (int)tem;
24551 #ifdef HAVE_WINDOW_SYSTEM
24552 else if (FRAME_WINDOW_P (it->f)
24553 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24555 /* Relative width `:relative-width FACTOR' specified and valid.
24556 Compute the width of the characters having the `glyph'
24557 property. */
24558 struct it it2;
24559 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24561 it2 = *it;
24562 if (it->multibyte_p)
24563 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24564 else
24566 it2.c = it2.char_to_display = *p, it2.len = 1;
24567 if (! ASCII_CHAR_P (it2.c))
24568 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24571 it2.glyph_row = NULL;
24572 it2.what = IT_CHARACTER;
24573 x_produce_glyphs (&it2);
24574 width = NUMVAL (prop) * it2.pixel_width;
24576 #endif /* HAVE_WINDOW_SYSTEM */
24577 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24578 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24580 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24581 align_to = (align_to < 0
24583 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24584 else if (align_to < 0)
24585 align_to = window_box_left_offset (it->w, TEXT_AREA);
24586 width = max (0, (int)tem + align_to - it->current_x);
24587 zero_width_ok_p = 1;
24589 else
24590 /* Nothing specified -> width defaults to canonical char width. */
24591 width = FRAME_COLUMN_WIDTH (it->f);
24593 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24594 width = 1;
24596 #ifdef HAVE_WINDOW_SYSTEM
24597 /* Compute height. */
24598 if (FRAME_WINDOW_P (it->f))
24600 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24601 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24603 height = (int)tem;
24604 zero_height_ok_p = 1;
24606 else if (prop = Fplist_get (plist, QCrelative_height),
24607 NUMVAL (prop) > 0)
24608 height = FONT_HEIGHT (font) * NUMVAL (prop);
24609 else
24610 height = FONT_HEIGHT (font);
24612 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24613 height = 1;
24615 /* Compute percentage of height used for ascent. If
24616 `:ascent ASCENT' is present and valid, use that. Otherwise,
24617 derive the ascent from the font in use. */
24618 if (prop = Fplist_get (plist, QCascent),
24619 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24620 ascent = height * NUMVAL (prop) / 100.0;
24621 else if (!NILP (prop)
24622 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24623 ascent = min (max (0, (int)tem), height);
24624 else
24625 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24627 else
24628 #endif /* HAVE_WINDOW_SYSTEM */
24629 height = 1;
24631 if (width > 0 && it->line_wrap != TRUNCATE
24632 && it->current_x + width > it->last_visible_x)
24634 width = it->last_visible_x - it->current_x;
24635 #ifdef HAVE_WINDOW_SYSTEM
24636 /* Subtract one more pixel from the stretch width, but only on
24637 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24638 width -= FRAME_WINDOW_P (it->f);
24639 #endif
24642 if (width > 0 && height > 0 && it->glyph_row)
24644 Lisp_Object o_object = it->object;
24645 Lisp_Object object = it->stack[it->sp - 1].string;
24646 int n = width;
24648 if (!STRINGP (object))
24649 object = it->w->contents;
24650 #ifdef HAVE_WINDOW_SYSTEM
24651 if (FRAME_WINDOW_P (it->f))
24652 append_stretch_glyph (it, object, width, height, ascent);
24653 else
24654 #endif
24656 it->object = object;
24657 it->char_to_display = ' ';
24658 it->pixel_width = it->len = 1;
24659 while (n--)
24660 tty_append_glyph (it);
24661 it->object = o_object;
24665 it->pixel_width = width;
24666 #ifdef HAVE_WINDOW_SYSTEM
24667 if (FRAME_WINDOW_P (it->f))
24669 it->ascent = it->phys_ascent = ascent;
24670 it->descent = it->phys_descent = height - it->ascent;
24671 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24672 take_vertical_position_into_account (it);
24674 else
24675 #endif
24676 it->nglyphs = width;
24679 /* Get information about special display element WHAT in an
24680 environment described by IT. WHAT is one of IT_TRUNCATION or
24681 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24682 non-null glyph_row member. This function ensures that fields like
24683 face_id, c, len of IT are left untouched. */
24685 static void
24686 produce_special_glyphs (struct it *it, enum display_element_type what)
24688 struct it temp_it;
24689 Lisp_Object gc;
24690 GLYPH glyph;
24692 temp_it = *it;
24693 temp_it.object = make_number (0);
24694 memset (&temp_it.current, 0, sizeof temp_it.current);
24696 if (what == IT_CONTINUATION)
24698 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24699 if (it->bidi_it.paragraph_dir == R2L)
24700 SET_GLYPH_FROM_CHAR (glyph, '/');
24701 else
24702 SET_GLYPH_FROM_CHAR (glyph, '\\');
24703 if (it->dp
24704 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24706 /* FIXME: Should we mirror GC for R2L lines? */
24707 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24708 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24711 else if (what == IT_TRUNCATION)
24713 /* Truncation glyph. */
24714 SET_GLYPH_FROM_CHAR (glyph, '$');
24715 if (it->dp
24716 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24718 /* FIXME: Should we mirror GC for R2L lines? */
24719 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24720 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24723 else
24724 emacs_abort ();
24726 #ifdef HAVE_WINDOW_SYSTEM
24727 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24728 is turned off, we precede the truncation/continuation glyphs by a
24729 stretch glyph whose width is computed such that these special
24730 glyphs are aligned at the window margin, even when very different
24731 fonts are used in different glyph rows. */
24732 if (FRAME_WINDOW_P (temp_it.f)
24733 /* init_iterator calls this with it->glyph_row == NULL, and it
24734 wants only the pixel width of the truncation/continuation
24735 glyphs. */
24736 && temp_it.glyph_row
24737 /* insert_left_trunc_glyphs calls us at the beginning of the
24738 row, and it has its own calculation of the stretch glyph
24739 width. */
24740 && temp_it.glyph_row->used[TEXT_AREA] > 0
24741 && (temp_it.glyph_row->reversed_p
24742 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24743 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24745 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24747 if (stretch_width > 0)
24749 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24750 struct font *font =
24751 face->font ? face->font : FRAME_FONT (temp_it.f);
24752 int stretch_ascent =
24753 (((temp_it.ascent + temp_it.descent)
24754 * FONT_BASE (font)) / FONT_HEIGHT (font));
24756 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24757 temp_it.ascent + temp_it.descent,
24758 stretch_ascent);
24761 #endif
24763 temp_it.dp = NULL;
24764 temp_it.what = IT_CHARACTER;
24765 temp_it.len = 1;
24766 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24767 temp_it.face_id = GLYPH_FACE (glyph);
24768 temp_it.len = CHAR_BYTES (temp_it.c);
24770 PRODUCE_GLYPHS (&temp_it);
24771 it->pixel_width = temp_it.pixel_width;
24772 it->nglyphs = temp_it.pixel_width;
24775 #ifdef HAVE_WINDOW_SYSTEM
24777 /* Calculate line-height and line-spacing properties.
24778 An integer value specifies explicit pixel value.
24779 A float value specifies relative value to current face height.
24780 A cons (float . face-name) specifies relative value to
24781 height of specified face font.
24783 Returns height in pixels, or nil. */
24786 static Lisp_Object
24787 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24788 int boff, int override)
24790 Lisp_Object face_name = Qnil;
24791 int ascent, descent, height;
24793 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24794 return val;
24796 if (CONSP (val))
24798 face_name = XCAR (val);
24799 val = XCDR (val);
24800 if (!NUMBERP (val))
24801 val = make_number (1);
24802 if (NILP (face_name))
24804 height = it->ascent + it->descent;
24805 goto scale;
24809 if (NILP (face_name))
24811 font = FRAME_FONT (it->f);
24812 boff = FRAME_BASELINE_OFFSET (it->f);
24814 else if (EQ (face_name, Qt))
24816 override = 0;
24818 else
24820 int face_id;
24821 struct face *face;
24823 face_id = lookup_named_face (it->f, face_name, 0);
24824 if (face_id < 0)
24825 return make_number (-1);
24827 face = FACE_FROM_ID (it->f, face_id);
24828 font = face->font;
24829 if (font == NULL)
24830 return make_number (-1);
24831 boff = font->baseline_offset;
24832 if (font->vertical_centering)
24833 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24836 ascent = FONT_BASE (font) + boff;
24837 descent = FONT_DESCENT (font) - boff;
24839 if (override)
24841 it->override_ascent = ascent;
24842 it->override_descent = descent;
24843 it->override_boff = boff;
24846 height = ascent + descent;
24848 scale:
24849 if (FLOATP (val))
24850 height = (int)(XFLOAT_DATA (val) * height);
24851 else if (INTEGERP (val))
24852 height *= XINT (val);
24854 return make_number (height);
24858 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24859 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24860 and only if this is for a character for which no font was found.
24862 If the display method (it->glyphless_method) is
24863 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24864 length of the acronym or the hexadecimal string, UPPER_XOFF and
24865 UPPER_YOFF are pixel offsets for the upper part of the string,
24866 LOWER_XOFF and LOWER_YOFF are for the lower part.
24868 For the other display methods, LEN through LOWER_YOFF are zero. */
24870 static void
24871 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24872 short upper_xoff, short upper_yoff,
24873 short lower_xoff, short lower_yoff)
24875 struct glyph *glyph;
24876 enum glyph_row_area area = it->area;
24878 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24879 if (glyph < it->glyph_row->glyphs[area + 1])
24881 /* If the glyph row is reversed, we need to prepend the glyph
24882 rather than append it. */
24883 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24885 struct glyph *g;
24887 /* Make room for the additional glyph. */
24888 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24889 g[1] = *g;
24890 glyph = it->glyph_row->glyphs[area];
24892 glyph->charpos = CHARPOS (it->position);
24893 glyph->object = it->object;
24894 glyph->pixel_width = it->pixel_width;
24895 glyph->ascent = it->ascent;
24896 glyph->descent = it->descent;
24897 glyph->voffset = it->voffset;
24898 glyph->type = GLYPHLESS_GLYPH;
24899 glyph->u.glyphless.method = it->glyphless_method;
24900 glyph->u.glyphless.for_no_font = for_no_font;
24901 glyph->u.glyphless.len = len;
24902 glyph->u.glyphless.ch = it->c;
24903 glyph->slice.glyphless.upper_xoff = upper_xoff;
24904 glyph->slice.glyphless.upper_yoff = upper_yoff;
24905 glyph->slice.glyphless.lower_xoff = lower_xoff;
24906 glyph->slice.glyphless.lower_yoff = lower_yoff;
24907 glyph->avoid_cursor_p = it->avoid_cursor_p;
24908 glyph->multibyte_p = it->multibyte_p;
24909 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24911 /* In R2L rows, the left and the right box edges need to be
24912 drawn in reverse direction. */
24913 glyph->right_box_line_p = it->start_of_box_run_p;
24914 glyph->left_box_line_p = it->end_of_box_run_p;
24916 else
24918 glyph->left_box_line_p = it->start_of_box_run_p;
24919 glyph->right_box_line_p = it->end_of_box_run_p;
24921 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24922 || it->phys_descent > it->descent);
24923 glyph->padding_p = 0;
24924 glyph->glyph_not_available_p = 0;
24925 glyph->face_id = face_id;
24926 glyph->font_type = FONT_TYPE_UNKNOWN;
24927 if (it->bidi_p)
24929 glyph->resolved_level = it->bidi_it.resolved_level;
24930 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24931 emacs_abort ();
24932 glyph->bidi_type = it->bidi_it.type;
24934 ++it->glyph_row->used[area];
24936 else
24937 IT_EXPAND_MATRIX_WIDTH (it, area);
24941 /* Produce a glyph for a glyphless character for iterator IT.
24942 IT->glyphless_method specifies which method to use for displaying
24943 the character. See the description of enum
24944 glyphless_display_method in dispextern.h for the detail.
24946 FOR_NO_FONT is nonzero if and only if this is for a character for
24947 which no font was found. ACRONYM, if non-nil, is an acronym string
24948 for the character. */
24950 static void
24951 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24953 int face_id;
24954 struct face *face;
24955 struct font *font;
24956 int base_width, base_height, width, height;
24957 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24958 int len;
24960 /* Get the metrics of the base font. We always refer to the current
24961 ASCII face. */
24962 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24963 font = face->font ? face->font : FRAME_FONT (it->f);
24964 it->ascent = FONT_BASE (font) + font->baseline_offset;
24965 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24966 base_height = it->ascent + it->descent;
24967 base_width = font->average_width;
24969 /* Get a face ID for the glyph by utilizing a cache (the same way as
24970 done for `escape-glyph' in get_next_display_element). */
24971 if (it->f == last_glyphless_glyph_frame
24972 && it->face_id == last_glyphless_glyph_face_id)
24974 face_id = last_glyphless_glyph_merged_face_id;
24976 else
24978 /* Merge the `glyphless-char' face into the current face. */
24979 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24980 last_glyphless_glyph_frame = it->f;
24981 last_glyphless_glyph_face_id = it->face_id;
24982 last_glyphless_glyph_merged_face_id = face_id;
24985 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24987 it->pixel_width = THIN_SPACE_WIDTH;
24988 len = 0;
24989 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24991 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24993 width = CHAR_WIDTH (it->c);
24994 if (width == 0)
24995 width = 1;
24996 else if (width > 4)
24997 width = 4;
24998 it->pixel_width = base_width * width;
24999 len = 0;
25000 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25002 else
25004 char buf[7];
25005 const char *str;
25006 unsigned int code[6];
25007 int upper_len;
25008 int ascent, descent;
25009 struct font_metrics metrics_upper, metrics_lower;
25011 face = FACE_FROM_ID (it->f, face_id);
25012 font = face->font ? face->font : FRAME_FONT (it->f);
25013 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25015 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25017 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25018 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25019 if (CONSP (acronym))
25020 acronym = XCAR (acronym);
25021 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25023 else
25025 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25026 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25027 str = buf;
25029 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25030 code[len] = font->driver->encode_char (font, str[len]);
25031 upper_len = (len + 1) / 2;
25032 font->driver->text_extents (font, code, upper_len,
25033 &metrics_upper);
25034 font->driver->text_extents (font, code + upper_len, len - upper_len,
25035 &metrics_lower);
25039 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25040 width = max (metrics_upper.width, metrics_lower.width) + 4;
25041 upper_xoff = upper_yoff = 2; /* the typical case */
25042 if (base_width >= width)
25044 /* Align the upper to the left, the lower to the right. */
25045 it->pixel_width = base_width;
25046 lower_xoff = base_width - 2 - metrics_lower.width;
25048 else
25050 /* Center the shorter one. */
25051 it->pixel_width = width;
25052 if (metrics_upper.width >= metrics_lower.width)
25053 lower_xoff = (width - metrics_lower.width) / 2;
25054 else
25056 /* FIXME: This code doesn't look right. It formerly was
25057 missing the "lower_xoff = 0;", which couldn't have
25058 been right since it left lower_xoff uninitialized. */
25059 lower_xoff = 0;
25060 upper_xoff = (width - metrics_upper.width) / 2;
25064 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25065 top, bottom, and between upper and lower strings. */
25066 height = (metrics_upper.ascent + metrics_upper.descent
25067 + metrics_lower.ascent + metrics_lower.descent) + 5;
25068 /* Center vertically.
25069 H:base_height, D:base_descent
25070 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25072 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25073 descent = D - H/2 + h/2;
25074 lower_yoff = descent - 2 - ld;
25075 upper_yoff = lower_yoff - la - 1 - ud; */
25076 ascent = - (it->descent - (base_height + height + 1) / 2);
25077 descent = it->descent - (base_height - height) / 2;
25078 lower_yoff = descent - 2 - metrics_lower.descent;
25079 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25080 - metrics_upper.descent);
25081 /* Don't make the height shorter than the base height. */
25082 if (height > base_height)
25084 it->ascent = ascent;
25085 it->descent = descent;
25089 it->phys_ascent = it->ascent;
25090 it->phys_descent = it->descent;
25091 if (it->glyph_row)
25092 append_glyphless_glyph (it, face_id, for_no_font, len,
25093 upper_xoff, upper_yoff,
25094 lower_xoff, lower_yoff);
25095 it->nglyphs = 1;
25096 take_vertical_position_into_account (it);
25100 /* RIF:
25101 Produce glyphs/get display metrics for the display element IT is
25102 loaded with. See the description of struct it in dispextern.h
25103 for an overview of struct it. */
25105 void
25106 x_produce_glyphs (struct it *it)
25108 int extra_line_spacing = it->extra_line_spacing;
25110 it->glyph_not_available_p = 0;
25112 if (it->what == IT_CHARACTER)
25114 XChar2b char2b;
25115 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25116 struct font *font = face->font;
25117 struct font_metrics *pcm = NULL;
25118 int boff; /* baseline offset */
25120 if (font == NULL)
25122 /* When no suitable font is found, display this character by
25123 the method specified in the first extra slot of
25124 Vglyphless_char_display. */
25125 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25127 eassert (it->what == IT_GLYPHLESS);
25128 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25129 goto done;
25132 boff = font->baseline_offset;
25133 if (font->vertical_centering)
25134 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25136 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25138 int stretched_p;
25140 it->nglyphs = 1;
25142 if (it->override_ascent >= 0)
25144 it->ascent = it->override_ascent;
25145 it->descent = it->override_descent;
25146 boff = it->override_boff;
25148 else
25150 it->ascent = FONT_BASE (font) + boff;
25151 it->descent = FONT_DESCENT (font) - boff;
25154 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25156 pcm = get_per_char_metric (font, &char2b);
25157 if (pcm->width == 0
25158 && pcm->rbearing == 0 && pcm->lbearing == 0)
25159 pcm = NULL;
25162 if (pcm)
25164 it->phys_ascent = pcm->ascent + boff;
25165 it->phys_descent = pcm->descent - boff;
25166 it->pixel_width = pcm->width;
25168 else
25170 it->glyph_not_available_p = 1;
25171 it->phys_ascent = it->ascent;
25172 it->phys_descent = it->descent;
25173 it->pixel_width = font->space_width;
25176 if (it->constrain_row_ascent_descent_p)
25178 if (it->descent > it->max_descent)
25180 it->ascent += it->descent - it->max_descent;
25181 it->descent = it->max_descent;
25183 if (it->ascent > it->max_ascent)
25185 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25186 it->ascent = it->max_ascent;
25188 it->phys_ascent = min (it->phys_ascent, it->ascent);
25189 it->phys_descent = min (it->phys_descent, it->descent);
25190 extra_line_spacing = 0;
25193 /* If this is a space inside a region of text with
25194 `space-width' property, change its width. */
25195 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25196 if (stretched_p)
25197 it->pixel_width *= XFLOATINT (it->space_width);
25199 /* If face has a box, add the box thickness to the character
25200 height. If character has a box line to the left and/or
25201 right, add the box line width to the character's width. */
25202 if (face->box != FACE_NO_BOX)
25204 int thick = face->box_line_width;
25206 if (thick > 0)
25208 it->ascent += thick;
25209 it->descent += thick;
25211 else
25212 thick = -thick;
25214 if (it->start_of_box_run_p)
25215 it->pixel_width += thick;
25216 if (it->end_of_box_run_p)
25217 it->pixel_width += thick;
25220 /* If face has an overline, add the height of the overline
25221 (1 pixel) and a 1 pixel margin to the character height. */
25222 if (face->overline_p)
25223 it->ascent += overline_margin;
25225 if (it->constrain_row_ascent_descent_p)
25227 if (it->ascent > it->max_ascent)
25228 it->ascent = it->max_ascent;
25229 if (it->descent > it->max_descent)
25230 it->descent = it->max_descent;
25233 take_vertical_position_into_account (it);
25235 /* If we have to actually produce glyphs, do it. */
25236 if (it->glyph_row)
25238 if (stretched_p)
25240 /* Translate a space with a `space-width' property
25241 into a stretch glyph. */
25242 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25243 / FONT_HEIGHT (font));
25244 append_stretch_glyph (it, it->object, it->pixel_width,
25245 it->ascent + it->descent, ascent);
25247 else
25248 append_glyph (it);
25250 /* If characters with lbearing or rbearing are displayed
25251 in this line, record that fact in a flag of the
25252 glyph row. This is used to optimize X output code. */
25253 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25254 it->glyph_row->contains_overlapping_glyphs_p = 1;
25256 if (! stretched_p && it->pixel_width == 0)
25257 /* We assure that all visible glyphs have at least 1-pixel
25258 width. */
25259 it->pixel_width = 1;
25261 else if (it->char_to_display == '\n')
25263 /* A newline has no width, but we need the height of the
25264 line. But if previous part of the line sets a height,
25265 don't increase that height */
25267 Lisp_Object height;
25268 Lisp_Object total_height = Qnil;
25270 it->override_ascent = -1;
25271 it->pixel_width = 0;
25272 it->nglyphs = 0;
25274 height = get_it_property (it, Qline_height);
25275 /* Split (line-height total-height) list */
25276 if (CONSP (height)
25277 && CONSP (XCDR (height))
25278 && NILP (XCDR (XCDR (height))))
25280 total_height = XCAR (XCDR (height));
25281 height = XCAR (height);
25283 height = calc_line_height_property (it, height, font, boff, 1);
25285 if (it->override_ascent >= 0)
25287 it->ascent = it->override_ascent;
25288 it->descent = it->override_descent;
25289 boff = it->override_boff;
25291 else
25293 it->ascent = FONT_BASE (font) + boff;
25294 it->descent = FONT_DESCENT (font) - boff;
25297 if (EQ (height, Qt))
25299 if (it->descent > it->max_descent)
25301 it->ascent += it->descent - it->max_descent;
25302 it->descent = it->max_descent;
25304 if (it->ascent > it->max_ascent)
25306 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25307 it->ascent = it->max_ascent;
25309 it->phys_ascent = min (it->phys_ascent, it->ascent);
25310 it->phys_descent = min (it->phys_descent, it->descent);
25311 it->constrain_row_ascent_descent_p = 1;
25312 extra_line_spacing = 0;
25314 else
25316 Lisp_Object spacing;
25318 it->phys_ascent = it->ascent;
25319 it->phys_descent = it->descent;
25321 if ((it->max_ascent > 0 || it->max_descent > 0)
25322 && face->box != FACE_NO_BOX
25323 && face->box_line_width > 0)
25325 it->ascent += face->box_line_width;
25326 it->descent += face->box_line_width;
25328 if (!NILP (height)
25329 && XINT (height) > it->ascent + it->descent)
25330 it->ascent = XINT (height) - it->descent;
25332 if (!NILP (total_height))
25333 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25334 else
25336 spacing = get_it_property (it, Qline_spacing);
25337 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25339 if (INTEGERP (spacing))
25341 extra_line_spacing = XINT (spacing);
25342 if (!NILP (total_height))
25343 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25347 else /* i.e. (it->char_to_display == '\t') */
25349 if (font->space_width > 0)
25351 int tab_width = it->tab_width * font->space_width;
25352 int x = it->current_x + it->continuation_lines_width;
25353 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25355 /* If the distance from the current position to the next tab
25356 stop is less than a space character width, use the
25357 tab stop after that. */
25358 if (next_tab_x - x < font->space_width)
25359 next_tab_x += tab_width;
25361 it->pixel_width = next_tab_x - x;
25362 it->nglyphs = 1;
25363 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25364 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25366 if (it->glyph_row)
25368 append_stretch_glyph (it, it->object, it->pixel_width,
25369 it->ascent + it->descent, it->ascent);
25372 else
25374 it->pixel_width = 0;
25375 it->nglyphs = 1;
25379 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25381 /* A static composition.
25383 Note: A composition is represented as one glyph in the
25384 glyph matrix. There are no padding glyphs.
25386 Important note: pixel_width, ascent, and descent are the
25387 values of what is drawn by draw_glyphs (i.e. the values of
25388 the overall glyphs composed). */
25389 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25390 int boff; /* baseline offset */
25391 struct composition *cmp = composition_table[it->cmp_it.id];
25392 int glyph_len = cmp->glyph_len;
25393 struct font *font = face->font;
25395 it->nglyphs = 1;
25397 /* If we have not yet calculated pixel size data of glyphs of
25398 the composition for the current face font, calculate them
25399 now. Theoretically, we have to check all fonts for the
25400 glyphs, but that requires much time and memory space. So,
25401 here we check only the font of the first glyph. This may
25402 lead to incorrect display, but it's very rare, and C-l
25403 (recenter-top-bottom) can correct the display anyway. */
25404 if (! cmp->font || cmp->font != font)
25406 /* Ascent and descent of the font of the first character
25407 of this composition (adjusted by baseline offset).
25408 Ascent and descent of overall glyphs should not be less
25409 than these, respectively. */
25410 int font_ascent, font_descent, font_height;
25411 /* Bounding box of the overall glyphs. */
25412 int leftmost, rightmost, lowest, highest;
25413 int lbearing, rbearing;
25414 int i, width, ascent, descent;
25415 int left_padded = 0, right_padded = 0;
25416 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25417 XChar2b char2b;
25418 struct font_metrics *pcm;
25419 int font_not_found_p;
25420 ptrdiff_t pos;
25422 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25423 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25424 break;
25425 if (glyph_len < cmp->glyph_len)
25426 right_padded = 1;
25427 for (i = 0; i < glyph_len; i++)
25429 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25430 break;
25431 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25433 if (i > 0)
25434 left_padded = 1;
25436 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25437 : IT_CHARPOS (*it));
25438 /* If no suitable font is found, use the default font. */
25439 font_not_found_p = font == NULL;
25440 if (font_not_found_p)
25442 face = face->ascii_face;
25443 font = face->font;
25445 boff = font->baseline_offset;
25446 if (font->vertical_centering)
25447 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25448 font_ascent = FONT_BASE (font) + boff;
25449 font_descent = FONT_DESCENT (font) - boff;
25450 font_height = FONT_HEIGHT (font);
25452 cmp->font = font;
25454 pcm = NULL;
25455 if (! font_not_found_p)
25457 get_char_face_and_encoding (it->f, c, it->face_id,
25458 &char2b, 0);
25459 pcm = get_per_char_metric (font, &char2b);
25462 /* Initialize the bounding box. */
25463 if (pcm)
25465 width = cmp->glyph_len > 0 ? pcm->width : 0;
25466 ascent = pcm->ascent;
25467 descent = pcm->descent;
25468 lbearing = pcm->lbearing;
25469 rbearing = pcm->rbearing;
25471 else
25473 width = cmp->glyph_len > 0 ? font->space_width : 0;
25474 ascent = FONT_BASE (font);
25475 descent = FONT_DESCENT (font);
25476 lbearing = 0;
25477 rbearing = width;
25480 rightmost = width;
25481 leftmost = 0;
25482 lowest = - descent + boff;
25483 highest = ascent + boff;
25485 if (! font_not_found_p
25486 && font->default_ascent
25487 && CHAR_TABLE_P (Vuse_default_ascent)
25488 && !NILP (Faref (Vuse_default_ascent,
25489 make_number (it->char_to_display))))
25490 highest = font->default_ascent + boff;
25492 /* Draw the first glyph at the normal position. It may be
25493 shifted to right later if some other glyphs are drawn
25494 at the left. */
25495 cmp->offsets[i * 2] = 0;
25496 cmp->offsets[i * 2 + 1] = boff;
25497 cmp->lbearing = lbearing;
25498 cmp->rbearing = rbearing;
25500 /* Set cmp->offsets for the remaining glyphs. */
25501 for (i++; i < glyph_len; i++)
25503 int left, right, btm, top;
25504 int ch = COMPOSITION_GLYPH (cmp, i);
25505 int face_id;
25506 struct face *this_face;
25508 if (ch == '\t')
25509 ch = ' ';
25510 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25511 this_face = FACE_FROM_ID (it->f, face_id);
25512 font = this_face->font;
25514 if (font == NULL)
25515 pcm = NULL;
25516 else
25518 get_char_face_and_encoding (it->f, ch, face_id,
25519 &char2b, 0);
25520 pcm = get_per_char_metric (font, &char2b);
25522 if (! pcm)
25523 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25524 else
25526 width = pcm->width;
25527 ascent = pcm->ascent;
25528 descent = pcm->descent;
25529 lbearing = pcm->lbearing;
25530 rbearing = pcm->rbearing;
25531 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25533 /* Relative composition with or without
25534 alternate chars. */
25535 left = (leftmost + rightmost - width) / 2;
25536 btm = - descent + boff;
25537 if (font->relative_compose
25538 && (! CHAR_TABLE_P (Vignore_relative_composition)
25539 || NILP (Faref (Vignore_relative_composition,
25540 make_number (ch)))))
25543 if (- descent >= font->relative_compose)
25544 /* One extra pixel between two glyphs. */
25545 btm = highest + 1;
25546 else if (ascent <= 0)
25547 /* One extra pixel between two glyphs. */
25548 btm = lowest - 1 - ascent - descent;
25551 else
25553 /* A composition rule is specified by an integer
25554 value that encodes global and new reference
25555 points (GREF and NREF). GREF and NREF are
25556 specified by numbers as below:
25558 0---1---2 -- ascent
25562 9--10--11 -- center
25564 ---3---4---5--- baseline
25566 6---7---8 -- descent
25568 int rule = COMPOSITION_RULE (cmp, i);
25569 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25571 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25572 grefx = gref % 3, nrefx = nref % 3;
25573 grefy = gref / 3, nrefy = nref / 3;
25574 if (xoff)
25575 xoff = font_height * (xoff - 128) / 256;
25576 if (yoff)
25577 yoff = font_height * (yoff - 128) / 256;
25579 left = (leftmost
25580 + grefx * (rightmost - leftmost) / 2
25581 - nrefx * width / 2
25582 + xoff);
25584 btm = ((grefy == 0 ? highest
25585 : grefy == 1 ? 0
25586 : grefy == 2 ? lowest
25587 : (highest + lowest) / 2)
25588 - (nrefy == 0 ? ascent + descent
25589 : nrefy == 1 ? descent - boff
25590 : nrefy == 2 ? 0
25591 : (ascent + descent) / 2)
25592 + yoff);
25595 cmp->offsets[i * 2] = left;
25596 cmp->offsets[i * 2 + 1] = btm + descent;
25598 /* Update the bounding box of the overall glyphs. */
25599 if (width > 0)
25601 right = left + width;
25602 if (left < leftmost)
25603 leftmost = left;
25604 if (right > rightmost)
25605 rightmost = right;
25607 top = btm + descent + ascent;
25608 if (top > highest)
25609 highest = top;
25610 if (btm < lowest)
25611 lowest = btm;
25613 if (cmp->lbearing > left + lbearing)
25614 cmp->lbearing = left + lbearing;
25615 if (cmp->rbearing < left + rbearing)
25616 cmp->rbearing = left + rbearing;
25620 /* If there are glyphs whose x-offsets are negative,
25621 shift all glyphs to the right and make all x-offsets
25622 non-negative. */
25623 if (leftmost < 0)
25625 for (i = 0; i < cmp->glyph_len; i++)
25626 cmp->offsets[i * 2] -= leftmost;
25627 rightmost -= leftmost;
25628 cmp->lbearing -= leftmost;
25629 cmp->rbearing -= leftmost;
25632 if (left_padded && cmp->lbearing < 0)
25634 for (i = 0; i < cmp->glyph_len; i++)
25635 cmp->offsets[i * 2] -= cmp->lbearing;
25636 rightmost -= cmp->lbearing;
25637 cmp->rbearing -= cmp->lbearing;
25638 cmp->lbearing = 0;
25640 if (right_padded && rightmost < cmp->rbearing)
25642 rightmost = cmp->rbearing;
25645 cmp->pixel_width = rightmost;
25646 cmp->ascent = highest;
25647 cmp->descent = - lowest;
25648 if (cmp->ascent < font_ascent)
25649 cmp->ascent = font_ascent;
25650 if (cmp->descent < font_descent)
25651 cmp->descent = font_descent;
25654 if (it->glyph_row
25655 && (cmp->lbearing < 0
25656 || cmp->rbearing > cmp->pixel_width))
25657 it->glyph_row->contains_overlapping_glyphs_p = 1;
25659 it->pixel_width = cmp->pixel_width;
25660 it->ascent = it->phys_ascent = cmp->ascent;
25661 it->descent = it->phys_descent = cmp->descent;
25662 if (face->box != FACE_NO_BOX)
25664 int thick = face->box_line_width;
25666 if (thick > 0)
25668 it->ascent += thick;
25669 it->descent += thick;
25671 else
25672 thick = - thick;
25674 if (it->start_of_box_run_p)
25675 it->pixel_width += thick;
25676 if (it->end_of_box_run_p)
25677 it->pixel_width += thick;
25680 /* If face has an overline, add the height of the overline
25681 (1 pixel) and a 1 pixel margin to the character height. */
25682 if (face->overline_p)
25683 it->ascent += overline_margin;
25685 take_vertical_position_into_account (it);
25686 if (it->ascent < 0)
25687 it->ascent = 0;
25688 if (it->descent < 0)
25689 it->descent = 0;
25691 if (it->glyph_row && cmp->glyph_len > 0)
25692 append_composite_glyph (it);
25694 else if (it->what == IT_COMPOSITION)
25696 /* A dynamic (automatic) composition. */
25697 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25698 Lisp_Object gstring;
25699 struct font_metrics metrics;
25701 it->nglyphs = 1;
25703 gstring = composition_gstring_from_id (it->cmp_it.id);
25704 it->pixel_width
25705 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25706 &metrics);
25707 if (it->glyph_row
25708 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25709 it->glyph_row->contains_overlapping_glyphs_p = 1;
25710 it->ascent = it->phys_ascent = metrics.ascent;
25711 it->descent = it->phys_descent = metrics.descent;
25712 if (face->box != FACE_NO_BOX)
25714 int thick = face->box_line_width;
25716 if (thick > 0)
25718 it->ascent += thick;
25719 it->descent += thick;
25721 else
25722 thick = - thick;
25724 if (it->start_of_box_run_p)
25725 it->pixel_width += thick;
25726 if (it->end_of_box_run_p)
25727 it->pixel_width += thick;
25729 /* If face has an overline, add the height of the overline
25730 (1 pixel) and a 1 pixel margin to the character height. */
25731 if (face->overline_p)
25732 it->ascent += overline_margin;
25733 take_vertical_position_into_account (it);
25734 if (it->ascent < 0)
25735 it->ascent = 0;
25736 if (it->descent < 0)
25737 it->descent = 0;
25739 if (it->glyph_row)
25740 append_composite_glyph (it);
25742 else if (it->what == IT_GLYPHLESS)
25743 produce_glyphless_glyph (it, 0, Qnil);
25744 else if (it->what == IT_IMAGE)
25745 produce_image_glyph (it);
25746 else if (it->what == IT_STRETCH)
25747 produce_stretch_glyph (it);
25749 done:
25750 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25751 because this isn't true for images with `:ascent 100'. */
25752 eassert (it->ascent >= 0 && it->descent >= 0);
25753 if (it->area == TEXT_AREA)
25754 it->current_x += it->pixel_width;
25756 if (extra_line_spacing > 0)
25758 it->descent += extra_line_spacing;
25759 if (extra_line_spacing > it->max_extra_line_spacing)
25760 it->max_extra_line_spacing = extra_line_spacing;
25763 it->max_ascent = max (it->max_ascent, it->ascent);
25764 it->max_descent = max (it->max_descent, it->descent);
25765 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25766 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25769 /* EXPORT for RIF:
25770 Output LEN glyphs starting at START at the nominal cursor position.
25771 Advance the nominal cursor over the text. The global variable
25772 updated_window contains the window being updated, updated_row is
25773 the glyph row being updated, and updated_area is the area of that
25774 row being updated. */
25776 void
25777 x_write_glyphs (struct glyph *start, int len)
25779 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25781 eassert (updated_window && updated_row);
25782 /* When the window is hscrolled, cursor hpos can legitimately be out
25783 of bounds, but we draw the cursor at the corresponding window
25784 margin in that case. */
25785 if (!updated_row->reversed_p && chpos < 0)
25786 chpos = 0;
25787 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25788 chpos = updated_row->used[TEXT_AREA] - 1;
25790 block_input ();
25792 /* Write glyphs. */
25794 hpos = start - updated_row->glyphs[updated_area];
25795 x = draw_glyphs (updated_window, output_cursor.x,
25796 updated_row, updated_area,
25797 hpos, hpos + len,
25798 DRAW_NORMAL_TEXT, 0);
25800 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25801 if (updated_area == TEXT_AREA
25802 && updated_window->phys_cursor_on_p
25803 && updated_window->phys_cursor.vpos == output_cursor.vpos
25804 && chpos >= hpos
25805 && chpos < hpos + len)
25806 updated_window->phys_cursor_on_p = 0;
25808 unblock_input ();
25810 /* Advance the output cursor. */
25811 output_cursor.hpos += len;
25812 output_cursor.x = x;
25816 /* EXPORT for RIF:
25817 Insert LEN glyphs from START at the nominal cursor position. */
25819 void
25820 x_insert_glyphs (struct glyph *start, int len)
25822 struct frame *f;
25823 struct window *w;
25824 int line_height, shift_by_width, shifted_region_width;
25825 struct glyph_row *row;
25826 struct glyph *glyph;
25827 int frame_x, frame_y;
25828 ptrdiff_t hpos;
25830 eassert (updated_window && updated_row);
25831 block_input ();
25832 w = updated_window;
25833 f = XFRAME (WINDOW_FRAME (w));
25835 /* Get the height of the line we are in. */
25836 row = updated_row;
25837 line_height = row->height;
25839 /* Get the width of the glyphs to insert. */
25840 shift_by_width = 0;
25841 for (glyph = start; glyph < start + len; ++glyph)
25842 shift_by_width += glyph->pixel_width;
25844 /* Get the width of the region to shift right. */
25845 shifted_region_width = (window_box_width (w, updated_area)
25846 - output_cursor.x
25847 - shift_by_width);
25849 /* Shift right. */
25850 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25851 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25853 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25854 line_height, shift_by_width);
25856 /* Write the glyphs. */
25857 hpos = start - row->glyphs[updated_area];
25858 draw_glyphs (w, output_cursor.x, row, updated_area,
25859 hpos, hpos + len,
25860 DRAW_NORMAL_TEXT, 0);
25862 /* Advance the output cursor. */
25863 output_cursor.hpos += len;
25864 output_cursor.x += shift_by_width;
25865 unblock_input ();
25869 /* EXPORT for RIF:
25870 Erase the current text line from the nominal cursor position
25871 (inclusive) to pixel column TO_X (exclusive). The idea is that
25872 everything from TO_X onward is already erased.
25874 TO_X is a pixel position relative to updated_area of
25875 updated_window. TO_X == -1 means clear to the end of this area. */
25877 void
25878 x_clear_end_of_line (int to_x)
25880 struct frame *f;
25881 struct window *w = updated_window;
25882 int max_x, min_y, max_y;
25883 int from_x, from_y, to_y;
25885 eassert (updated_window && updated_row);
25886 f = XFRAME (w->frame);
25888 if (updated_row->full_width_p)
25889 max_x = WINDOW_TOTAL_WIDTH (w);
25890 else
25891 max_x = window_box_width (w, updated_area);
25892 max_y = window_text_bottom_y (w);
25894 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25895 of window. For TO_X > 0, truncate to end of drawing area. */
25896 if (to_x == 0)
25897 return;
25898 else if (to_x < 0)
25899 to_x = max_x;
25900 else
25901 to_x = min (to_x, max_x);
25903 to_y = min (max_y, output_cursor.y + updated_row->height);
25905 /* Notice if the cursor will be cleared by this operation. */
25906 if (!updated_row->full_width_p)
25907 notice_overwritten_cursor (w, updated_area,
25908 output_cursor.x, -1,
25909 updated_row->y,
25910 MATRIX_ROW_BOTTOM_Y (updated_row));
25912 from_x = output_cursor.x;
25914 /* Translate to frame coordinates. */
25915 if (updated_row->full_width_p)
25917 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25918 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25920 else
25922 int area_left = window_box_left (w, updated_area);
25923 from_x += area_left;
25924 to_x += area_left;
25927 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25928 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25929 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25931 /* Prevent inadvertently clearing to end of the X window. */
25932 if (to_x > from_x && to_y > from_y)
25934 block_input ();
25935 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25936 to_x - from_x, to_y - from_y);
25937 unblock_input ();
25941 #endif /* HAVE_WINDOW_SYSTEM */
25945 /***********************************************************************
25946 Cursor types
25947 ***********************************************************************/
25949 /* Value is the internal representation of the specified cursor type
25950 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25951 of the bar cursor. */
25953 static enum text_cursor_kinds
25954 get_specified_cursor_type (Lisp_Object arg, int *width)
25956 enum text_cursor_kinds type;
25958 if (NILP (arg))
25959 return NO_CURSOR;
25961 if (EQ (arg, Qbox))
25962 return FILLED_BOX_CURSOR;
25964 if (EQ (arg, Qhollow))
25965 return HOLLOW_BOX_CURSOR;
25967 if (EQ (arg, Qbar))
25969 *width = 2;
25970 return BAR_CURSOR;
25973 if (CONSP (arg)
25974 && EQ (XCAR (arg), Qbar)
25975 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25977 *width = XINT (XCDR (arg));
25978 return BAR_CURSOR;
25981 if (EQ (arg, Qhbar))
25983 *width = 2;
25984 return HBAR_CURSOR;
25987 if (CONSP (arg)
25988 && EQ (XCAR (arg), Qhbar)
25989 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25991 *width = XINT (XCDR (arg));
25992 return HBAR_CURSOR;
25995 /* Treat anything unknown as "hollow box cursor".
25996 It was bad to signal an error; people have trouble fixing
25997 .Xdefaults with Emacs, when it has something bad in it. */
25998 type = HOLLOW_BOX_CURSOR;
26000 return type;
26003 /* Set the default cursor types for specified frame. */
26004 void
26005 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26007 int width = 1;
26008 Lisp_Object tem;
26010 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26011 FRAME_CURSOR_WIDTH (f) = width;
26013 /* By default, set up the blink-off state depending on the on-state. */
26015 tem = Fassoc (arg, Vblink_cursor_alist);
26016 if (!NILP (tem))
26018 FRAME_BLINK_OFF_CURSOR (f)
26019 = get_specified_cursor_type (XCDR (tem), &width);
26020 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26022 else
26023 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26027 #ifdef HAVE_WINDOW_SYSTEM
26029 /* Return the cursor we want to be displayed in window W. Return
26030 width of bar/hbar cursor through WIDTH arg. Return with
26031 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26032 (i.e. if the `system caret' should track this cursor).
26034 In a mini-buffer window, we want the cursor only to appear if we
26035 are reading input from this window. For the selected window, we
26036 want the cursor type given by the frame parameter or buffer local
26037 setting of cursor-type. If explicitly marked off, draw no cursor.
26038 In all other cases, we want a hollow box cursor. */
26040 static enum text_cursor_kinds
26041 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26042 int *active_cursor)
26044 struct frame *f = XFRAME (w->frame);
26045 struct buffer *b = XBUFFER (w->contents);
26046 int cursor_type = DEFAULT_CURSOR;
26047 Lisp_Object alt_cursor;
26048 int non_selected = 0;
26050 *active_cursor = 1;
26052 /* Echo area */
26053 if (cursor_in_echo_area
26054 && FRAME_HAS_MINIBUF_P (f)
26055 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26057 if (w == XWINDOW (echo_area_window))
26059 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26061 *width = FRAME_CURSOR_WIDTH (f);
26062 return FRAME_DESIRED_CURSOR (f);
26064 else
26065 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26068 *active_cursor = 0;
26069 non_selected = 1;
26072 /* Detect a nonselected window or nonselected frame. */
26073 else if (w != XWINDOW (f->selected_window)
26074 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
26076 *active_cursor = 0;
26078 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26079 return NO_CURSOR;
26081 non_selected = 1;
26084 /* Never display a cursor in a window in which cursor-type is nil. */
26085 if (NILP (BVAR (b, cursor_type)))
26086 return NO_CURSOR;
26088 /* Get the normal cursor type for this window. */
26089 if (EQ (BVAR (b, cursor_type), Qt))
26091 cursor_type = FRAME_DESIRED_CURSOR (f);
26092 *width = FRAME_CURSOR_WIDTH (f);
26094 else
26095 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26097 /* Use cursor-in-non-selected-windows instead
26098 for non-selected window or frame. */
26099 if (non_selected)
26101 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26102 if (!EQ (Qt, alt_cursor))
26103 return get_specified_cursor_type (alt_cursor, width);
26104 /* t means modify the normal cursor type. */
26105 if (cursor_type == FILLED_BOX_CURSOR)
26106 cursor_type = HOLLOW_BOX_CURSOR;
26107 else if (cursor_type == BAR_CURSOR && *width > 1)
26108 --*width;
26109 return cursor_type;
26112 /* Use normal cursor if not blinked off. */
26113 if (!w->cursor_off_p)
26115 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26117 if (cursor_type == FILLED_BOX_CURSOR)
26119 /* Using a block cursor on large images can be very annoying.
26120 So use a hollow cursor for "large" images.
26121 If image is not transparent (no mask), also use hollow cursor. */
26122 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26123 if (img != NULL && IMAGEP (img->spec))
26125 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26126 where N = size of default frame font size.
26127 This should cover most of the "tiny" icons people may use. */
26128 if (!img->mask
26129 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26130 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26131 cursor_type = HOLLOW_BOX_CURSOR;
26134 else if (cursor_type != NO_CURSOR)
26136 /* Display current only supports BOX and HOLLOW cursors for images.
26137 So for now, unconditionally use a HOLLOW cursor when cursor is
26138 not a solid box cursor. */
26139 cursor_type = HOLLOW_BOX_CURSOR;
26142 return cursor_type;
26145 /* Cursor is blinked off, so determine how to "toggle" it. */
26147 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26148 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26149 return get_specified_cursor_type (XCDR (alt_cursor), width);
26151 /* Then see if frame has specified a specific blink off cursor type. */
26152 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26154 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26155 return FRAME_BLINK_OFF_CURSOR (f);
26158 #if 0
26159 /* Some people liked having a permanently visible blinking cursor,
26160 while others had very strong opinions against it. So it was
26161 decided to remove it. KFS 2003-09-03 */
26163 /* Finally perform built-in cursor blinking:
26164 filled box <-> hollow box
26165 wide [h]bar <-> narrow [h]bar
26166 narrow [h]bar <-> no cursor
26167 other type <-> no cursor */
26169 if (cursor_type == FILLED_BOX_CURSOR)
26170 return HOLLOW_BOX_CURSOR;
26172 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26174 *width = 1;
26175 return cursor_type;
26177 #endif
26179 return NO_CURSOR;
26183 /* Notice when the text cursor of window W has been completely
26184 overwritten by a drawing operation that outputs glyphs in AREA
26185 starting at X0 and ending at X1 in the line starting at Y0 and
26186 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26187 the rest of the line after X0 has been written. Y coordinates
26188 are window-relative. */
26190 static void
26191 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26192 int x0, int x1, int y0, int y1)
26194 int cx0, cx1, cy0, cy1;
26195 struct glyph_row *row;
26197 if (!w->phys_cursor_on_p)
26198 return;
26199 if (area != TEXT_AREA)
26200 return;
26202 if (w->phys_cursor.vpos < 0
26203 || w->phys_cursor.vpos >= w->current_matrix->nrows
26204 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26205 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26206 return;
26208 if (row->cursor_in_fringe_p)
26210 row->cursor_in_fringe_p = 0;
26211 draw_fringe_bitmap (w, row, row->reversed_p);
26212 w->phys_cursor_on_p = 0;
26213 return;
26216 cx0 = w->phys_cursor.x;
26217 cx1 = cx0 + w->phys_cursor_width;
26218 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26219 return;
26221 /* The cursor image will be completely removed from the
26222 screen if the output area intersects the cursor area in
26223 y-direction. When we draw in [y0 y1[, and some part of
26224 the cursor is at y < y0, that part must have been drawn
26225 before. When scrolling, the cursor is erased before
26226 actually scrolling, so we don't come here. When not
26227 scrolling, the rows above the old cursor row must have
26228 changed, and in this case these rows must have written
26229 over the cursor image.
26231 Likewise if part of the cursor is below y1, with the
26232 exception of the cursor being in the first blank row at
26233 the buffer and window end because update_text_area
26234 doesn't draw that row. (Except when it does, but
26235 that's handled in update_text_area.) */
26237 cy0 = w->phys_cursor.y;
26238 cy1 = cy0 + w->phys_cursor_height;
26239 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26240 return;
26242 w->phys_cursor_on_p = 0;
26245 #endif /* HAVE_WINDOW_SYSTEM */
26248 /************************************************************************
26249 Mouse Face
26250 ************************************************************************/
26252 #ifdef HAVE_WINDOW_SYSTEM
26254 /* EXPORT for RIF:
26255 Fix the display of area AREA of overlapping row ROW in window W
26256 with respect to the overlapping part OVERLAPS. */
26258 void
26259 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26260 enum glyph_row_area area, int overlaps)
26262 int i, x;
26264 block_input ();
26266 x = 0;
26267 for (i = 0; i < row->used[area];)
26269 if (row->glyphs[area][i].overlaps_vertically_p)
26271 int start = i, start_x = x;
26275 x += row->glyphs[area][i].pixel_width;
26276 ++i;
26278 while (i < row->used[area]
26279 && row->glyphs[area][i].overlaps_vertically_p);
26281 draw_glyphs (w, start_x, row, area,
26282 start, i,
26283 DRAW_NORMAL_TEXT, overlaps);
26285 else
26287 x += row->glyphs[area][i].pixel_width;
26288 ++i;
26292 unblock_input ();
26296 /* EXPORT:
26297 Draw the cursor glyph of window W in glyph row ROW. See the
26298 comment of draw_glyphs for the meaning of HL. */
26300 void
26301 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26302 enum draw_glyphs_face hl)
26304 /* If cursor hpos is out of bounds, don't draw garbage. This can
26305 happen in mini-buffer windows when switching between echo area
26306 glyphs and mini-buffer. */
26307 if ((row->reversed_p
26308 ? (w->phys_cursor.hpos >= 0)
26309 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26311 int on_p = w->phys_cursor_on_p;
26312 int x1;
26313 int hpos = w->phys_cursor.hpos;
26315 /* When the window is hscrolled, cursor hpos can legitimately be
26316 out of bounds, but we draw the cursor at the corresponding
26317 window margin in that case. */
26318 if (!row->reversed_p && hpos < 0)
26319 hpos = 0;
26320 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26321 hpos = row->used[TEXT_AREA] - 1;
26323 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26324 hl, 0);
26325 w->phys_cursor_on_p = on_p;
26327 if (hl == DRAW_CURSOR)
26328 w->phys_cursor_width = x1 - w->phys_cursor.x;
26329 /* When we erase the cursor, and ROW is overlapped by other
26330 rows, make sure that these overlapping parts of other rows
26331 are redrawn. */
26332 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26334 w->phys_cursor_width = x1 - w->phys_cursor.x;
26336 if (row > w->current_matrix->rows
26337 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26338 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26339 OVERLAPS_ERASED_CURSOR);
26341 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26342 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26343 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26344 OVERLAPS_ERASED_CURSOR);
26350 /* EXPORT:
26351 Erase the image of a cursor of window W from the screen. */
26353 void
26354 erase_phys_cursor (struct window *w)
26356 struct frame *f = XFRAME (w->frame);
26357 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26358 int hpos = w->phys_cursor.hpos;
26359 int vpos = w->phys_cursor.vpos;
26360 int mouse_face_here_p = 0;
26361 struct glyph_matrix *active_glyphs = w->current_matrix;
26362 struct glyph_row *cursor_row;
26363 struct glyph *cursor_glyph;
26364 enum draw_glyphs_face hl;
26366 /* No cursor displayed or row invalidated => nothing to do on the
26367 screen. */
26368 if (w->phys_cursor_type == NO_CURSOR)
26369 goto mark_cursor_off;
26371 /* VPOS >= active_glyphs->nrows means that window has been resized.
26372 Don't bother to erase the cursor. */
26373 if (vpos >= active_glyphs->nrows)
26374 goto mark_cursor_off;
26376 /* If row containing cursor is marked invalid, there is nothing we
26377 can do. */
26378 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26379 if (!cursor_row->enabled_p)
26380 goto mark_cursor_off;
26382 /* If line spacing is > 0, old cursor may only be partially visible in
26383 window after split-window. So adjust visible height. */
26384 cursor_row->visible_height = min (cursor_row->visible_height,
26385 window_text_bottom_y (w) - cursor_row->y);
26387 /* If row is completely invisible, don't attempt to delete a cursor which
26388 isn't there. This can happen if cursor is at top of a window, and
26389 we switch to a buffer with a header line in that window. */
26390 if (cursor_row->visible_height <= 0)
26391 goto mark_cursor_off;
26393 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26394 if (cursor_row->cursor_in_fringe_p)
26396 cursor_row->cursor_in_fringe_p = 0;
26397 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26398 goto mark_cursor_off;
26401 /* This can happen when the new row is shorter than the old one.
26402 In this case, either draw_glyphs or clear_end_of_line
26403 should have cleared the cursor. Note that we wouldn't be
26404 able to erase the cursor in this case because we don't have a
26405 cursor glyph at hand. */
26406 if ((cursor_row->reversed_p
26407 ? (w->phys_cursor.hpos < 0)
26408 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26409 goto mark_cursor_off;
26411 /* When the window is hscrolled, cursor hpos can legitimately be out
26412 of bounds, but we draw the cursor at the corresponding window
26413 margin in that case. */
26414 if (!cursor_row->reversed_p && hpos < 0)
26415 hpos = 0;
26416 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26417 hpos = cursor_row->used[TEXT_AREA] - 1;
26419 /* If the cursor is in the mouse face area, redisplay that when
26420 we clear the cursor. */
26421 if (! NILP (hlinfo->mouse_face_window)
26422 && coords_in_mouse_face_p (w, hpos, vpos)
26423 /* Don't redraw the cursor's spot in mouse face if it is at the
26424 end of a line (on a newline). The cursor appears there, but
26425 mouse highlighting does not. */
26426 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26427 mouse_face_here_p = 1;
26429 /* Maybe clear the display under the cursor. */
26430 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26432 int x, y, left_x;
26433 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26434 int width;
26436 cursor_glyph = get_phys_cursor_glyph (w);
26437 if (cursor_glyph == NULL)
26438 goto mark_cursor_off;
26440 width = cursor_glyph->pixel_width;
26441 left_x = window_box_left_offset (w, TEXT_AREA);
26442 x = w->phys_cursor.x;
26443 if (x < left_x)
26444 width -= left_x - x;
26445 width = min (width, window_box_width (w, TEXT_AREA) - x);
26446 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26447 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26449 if (width > 0)
26450 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26453 /* Erase the cursor by redrawing the character underneath it. */
26454 if (mouse_face_here_p)
26455 hl = DRAW_MOUSE_FACE;
26456 else
26457 hl = DRAW_NORMAL_TEXT;
26458 draw_phys_cursor_glyph (w, cursor_row, hl);
26460 mark_cursor_off:
26461 w->phys_cursor_on_p = 0;
26462 w->phys_cursor_type = NO_CURSOR;
26466 /* EXPORT:
26467 Display or clear cursor of window W. If ON is zero, clear the
26468 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26469 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26471 void
26472 display_and_set_cursor (struct window *w, int on,
26473 int hpos, int vpos, int x, int y)
26475 struct frame *f = XFRAME (w->frame);
26476 int new_cursor_type;
26477 int new_cursor_width;
26478 int active_cursor;
26479 struct glyph_row *glyph_row;
26480 struct glyph *glyph;
26482 /* This is pointless on invisible frames, and dangerous on garbaged
26483 windows and frames; in the latter case, the frame or window may
26484 be in the midst of changing its size, and x and y may be off the
26485 window. */
26486 if (! FRAME_VISIBLE_P (f)
26487 || FRAME_GARBAGED_P (f)
26488 || vpos >= w->current_matrix->nrows
26489 || hpos >= w->current_matrix->matrix_w)
26490 return;
26492 /* If cursor is off and we want it off, return quickly. */
26493 if (!on && !w->phys_cursor_on_p)
26494 return;
26496 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26497 /* If cursor row is not enabled, we don't really know where to
26498 display the cursor. */
26499 if (!glyph_row->enabled_p)
26501 w->phys_cursor_on_p = 0;
26502 return;
26505 glyph = NULL;
26506 if (!glyph_row->exact_window_width_line_p
26507 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26508 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26510 eassert (input_blocked_p ());
26512 /* Set new_cursor_type to the cursor we want to be displayed. */
26513 new_cursor_type = get_window_cursor_type (w, glyph,
26514 &new_cursor_width, &active_cursor);
26516 /* If cursor is currently being shown and we don't want it to be or
26517 it is in the wrong place, or the cursor type is not what we want,
26518 erase it. */
26519 if (w->phys_cursor_on_p
26520 && (!on
26521 || w->phys_cursor.x != x
26522 || w->phys_cursor.y != y
26523 || new_cursor_type != w->phys_cursor_type
26524 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26525 && new_cursor_width != w->phys_cursor_width)))
26526 erase_phys_cursor (w);
26528 /* Don't check phys_cursor_on_p here because that flag is only set
26529 to zero in some cases where we know that the cursor has been
26530 completely erased, to avoid the extra work of erasing the cursor
26531 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26532 still not be visible, or it has only been partly erased. */
26533 if (on)
26535 w->phys_cursor_ascent = glyph_row->ascent;
26536 w->phys_cursor_height = glyph_row->height;
26538 /* Set phys_cursor_.* before x_draw_.* is called because some
26539 of them may need the information. */
26540 w->phys_cursor.x = x;
26541 w->phys_cursor.y = glyph_row->y;
26542 w->phys_cursor.hpos = hpos;
26543 w->phys_cursor.vpos = vpos;
26546 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26547 new_cursor_type, new_cursor_width,
26548 on, active_cursor);
26552 /* Switch the display of W's cursor on or off, according to the value
26553 of ON. */
26555 static void
26556 update_window_cursor (struct window *w, int on)
26558 /* Don't update cursor in windows whose frame is in the process
26559 of being deleted. */
26560 if (w->current_matrix)
26562 int hpos = w->phys_cursor.hpos;
26563 int vpos = w->phys_cursor.vpos;
26564 struct glyph_row *row;
26566 if (vpos >= w->current_matrix->nrows
26567 || hpos >= w->current_matrix->matrix_w)
26568 return;
26570 row = MATRIX_ROW (w->current_matrix, vpos);
26572 /* When the window is hscrolled, cursor hpos can legitimately be
26573 out of bounds, but we draw the cursor at the corresponding
26574 window margin in that case. */
26575 if (!row->reversed_p && hpos < 0)
26576 hpos = 0;
26577 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26578 hpos = row->used[TEXT_AREA] - 1;
26580 block_input ();
26581 display_and_set_cursor (w, on, hpos, vpos,
26582 w->phys_cursor.x, w->phys_cursor.y);
26583 unblock_input ();
26588 /* Call update_window_cursor with parameter ON_P on all leaf windows
26589 in the window tree rooted at W. */
26591 static void
26592 update_cursor_in_window_tree (struct window *w, int on_p)
26594 while (w)
26596 if (WINDOWP (w->contents))
26597 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26598 else
26599 update_window_cursor (w, on_p);
26601 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26606 /* EXPORT:
26607 Display the cursor on window W, or clear it, according to ON_P.
26608 Don't change the cursor's position. */
26610 void
26611 x_update_cursor (struct frame *f, int on_p)
26613 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26617 /* EXPORT:
26618 Clear the cursor of window W to background color, and mark the
26619 cursor as not shown. This is used when the text where the cursor
26620 is about to be rewritten. */
26622 void
26623 x_clear_cursor (struct window *w)
26625 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26626 update_window_cursor (w, 0);
26629 #endif /* HAVE_WINDOW_SYSTEM */
26631 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26632 and MSDOS. */
26633 static void
26634 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26635 int start_hpos, int end_hpos,
26636 enum draw_glyphs_face draw)
26638 #ifdef HAVE_WINDOW_SYSTEM
26639 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26641 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26642 return;
26644 #endif
26645 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26646 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26647 #endif
26650 /* Display the active region described by mouse_face_* according to DRAW. */
26652 static void
26653 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26655 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26656 struct frame *f = XFRAME (WINDOW_FRAME (w));
26658 if (/* If window is in the process of being destroyed, don't bother
26659 to do anything. */
26660 w->current_matrix != NULL
26661 /* Don't update mouse highlight if hidden */
26662 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26663 /* Recognize when we are called to operate on rows that don't exist
26664 anymore. This can happen when a window is split. */
26665 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26667 int phys_cursor_on_p = w->phys_cursor_on_p;
26668 struct glyph_row *row, *first, *last;
26670 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26671 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26673 for (row = first; row <= last && row->enabled_p; ++row)
26675 int start_hpos, end_hpos, start_x;
26677 /* For all but the first row, the highlight starts at column 0. */
26678 if (row == first)
26680 /* R2L rows have BEG and END in reversed order, but the
26681 screen drawing geometry is always left to right. So
26682 we need to mirror the beginning and end of the
26683 highlighted area in R2L rows. */
26684 if (!row->reversed_p)
26686 start_hpos = hlinfo->mouse_face_beg_col;
26687 start_x = hlinfo->mouse_face_beg_x;
26689 else if (row == last)
26691 start_hpos = hlinfo->mouse_face_end_col;
26692 start_x = hlinfo->mouse_face_end_x;
26694 else
26696 start_hpos = 0;
26697 start_x = 0;
26700 else if (row->reversed_p && row == last)
26702 start_hpos = hlinfo->mouse_face_end_col;
26703 start_x = hlinfo->mouse_face_end_x;
26705 else
26707 start_hpos = 0;
26708 start_x = 0;
26711 if (row == last)
26713 if (!row->reversed_p)
26714 end_hpos = hlinfo->mouse_face_end_col;
26715 else if (row == first)
26716 end_hpos = hlinfo->mouse_face_beg_col;
26717 else
26719 end_hpos = row->used[TEXT_AREA];
26720 if (draw == DRAW_NORMAL_TEXT)
26721 row->fill_line_p = 1; /* Clear to end of line */
26724 else if (row->reversed_p && row == first)
26725 end_hpos = hlinfo->mouse_face_beg_col;
26726 else
26728 end_hpos = row->used[TEXT_AREA];
26729 if (draw == DRAW_NORMAL_TEXT)
26730 row->fill_line_p = 1; /* Clear to end of line */
26733 if (end_hpos > start_hpos)
26735 draw_row_with_mouse_face (w, start_x, row,
26736 start_hpos, end_hpos, draw);
26738 row->mouse_face_p
26739 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26743 #ifdef HAVE_WINDOW_SYSTEM
26744 /* When we've written over the cursor, arrange for it to
26745 be displayed again. */
26746 if (FRAME_WINDOW_P (f)
26747 && phys_cursor_on_p && !w->phys_cursor_on_p)
26749 int hpos = w->phys_cursor.hpos;
26751 /* When the window is hscrolled, cursor hpos can legitimately be
26752 out of bounds, but we draw the cursor at the corresponding
26753 window margin in that case. */
26754 if (!row->reversed_p && hpos < 0)
26755 hpos = 0;
26756 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26757 hpos = row->used[TEXT_AREA] - 1;
26759 block_input ();
26760 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26761 w->phys_cursor.x, w->phys_cursor.y);
26762 unblock_input ();
26764 #endif /* HAVE_WINDOW_SYSTEM */
26767 #ifdef HAVE_WINDOW_SYSTEM
26768 /* Change the mouse cursor. */
26769 if (FRAME_WINDOW_P (f))
26771 if (draw == DRAW_NORMAL_TEXT
26772 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26773 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26774 else if (draw == DRAW_MOUSE_FACE)
26775 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26776 else
26777 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26779 #endif /* HAVE_WINDOW_SYSTEM */
26782 /* EXPORT:
26783 Clear out the mouse-highlighted active region.
26784 Redraw it un-highlighted first. Value is non-zero if mouse
26785 face was actually drawn unhighlighted. */
26788 clear_mouse_face (Mouse_HLInfo *hlinfo)
26790 int cleared = 0;
26792 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26794 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26795 cleared = 1;
26798 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26799 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26800 hlinfo->mouse_face_window = Qnil;
26801 hlinfo->mouse_face_overlay = Qnil;
26802 return cleared;
26805 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26806 within the mouse face on that window. */
26807 static int
26808 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26810 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26812 /* Quickly resolve the easy cases. */
26813 if (!(WINDOWP (hlinfo->mouse_face_window)
26814 && XWINDOW (hlinfo->mouse_face_window) == w))
26815 return 0;
26816 if (vpos < hlinfo->mouse_face_beg_row
26817 || vpos > hlinfo->mouse_face_end_row)
26818 return 0;
26819 if (vpos > hlinfo->mouse_face_beg_row
26820 && vpos < hlinfo->mouse_face_end_row)
26821 return 1;
26823 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26825 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26827 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26828 return 1;
26830 else if ((vpos == hlinfo->mouse_face_beg_row
26831 && hpos >= hlinfo->mouse_face_beg_col)
26832 || (vpos == hlinfo->mouse_face_end_row
26833 && hpos < hlinfo->mouse_face_end_col))
26834 return 1;
26836 else
26838 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26840 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26841 return 1;
26843 else if ((vpos == hlinfo->mouse_face_beg_row
26844 && hpos <= hlinfo->mouse_face_beg_col)
26845 || (vpos == hlinfo->mouse_face_end_row
26846 && hpos > hlinfo->mouse_face_end_col))
26847 return 1;
26849 return 0;
26853 /* EXPORT:
26854 Non-zero if physical cursor of window W is within mouse face. */
26857 cursor_in_mouse_face_p (struct window *w)
26859 int hpos = w->phys_cursor.hpos;
26860 int vpos = w->phys_cursor.vpos;
26861 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26863 /* When the window is hscrolled, cursor hpos can legitimately be out
26864 of bounds, but we draw the cursor at the corresponding window
26865 margin in that case. */
26866 if (!row->reversed_p && hpos < 0)
26867 hpos = 0;
26868 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26869 hpos = row->used[TEXT_AREA] - 1;
26871 return coords_in_mouse_face_p (w, hpos, vpos);
26876 /* Find the glyph rows START_ROW and END_ROW of window W that display
26877 characters between buffer positions START_CHARPOS and END_CHARPOS
26878 (excluding END_CHARPOS). DISP_STRING is a display string that
26879 covers these buffer positions. This is similar to
26880 row_containing_pos, but is more accurate when bidi reordering makes
26881 buffer positions change non-linearly with glyph rows. */
26882 static void
26883 rows_from_pos_range (struct window *w,
26884 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26885 Lisp_Object disp_string,
26886 struct glyph_row **start, struct glyph_row **end)
26888 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26889 int last_y = window_text_bottom_y (w);
26890 struct glyph_row *row;
26892 *start = NULL;
26893 *end = NULL;
26895 while (!first->enabled_p
26896 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26897 first++;
26899 /* Find the START row. */
26900 for (row = first;
26901 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26902 row++)
26904 /* A row can potentially be the START row if the range of the
26905 characters it displays intersects the range
26906 [START_CHARPOS..END_CHARPOS). */
26907 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26908 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26909 /* See the commentary in row_containing_pos, for the
26910 explanation of the complicated way to check whether
26911 some position is beyond the end of the characters
26912 displayed by a row. */
26913 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26914 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26915 && !row->ends_at_zv_p
26916 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26917 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26918 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26919 && !row->ends_at_zv_p
26920 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26922 /* Found a candidate row. Now make sure at least one of the
26923 glyphs it displays has a charpos from the range
26924 [START_CHARPOS..END_CHARPOS).
26926 This is not obvious because bidi reordering could make
26927 buffer positions of a row be 1,2,3,102,101,100, and if we
26928 want to highlight characters in [50..60), we don't want
26929 this row, even though [50..60) does intersect [1..103),
26930 the range of character positions given by the row's start
26931 and end positions. */
26932 struct glyph *g = row->glyphs[TEXT_AREA];
26933 struct glyph *e = g + row->used[TEXT_AREA];
26935 while (g < e)
26937 if (((BUFFERP (g->object) || INTEGERP (g->object))
26938 && start_charpos <= g->charpos && g->charpos < end_charpos)
26939 /* A glyph that comes from DISP_STRING is by
26940 definition to be highlighted. */
26941 || EQ (g->object, disp_string))
26942 *start = row;
26943 g++;
26945 if (*start)
26946 break;
26950 /* Find the END row. */
26951 if (!*start
26952 /* If the last row is partially visible, start looking for END
26953 from that row, instead of starting from FIRST. */
26954 && !(row->enabled_p
26955 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26956 row = first;
26957 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26959 struct glyph_row *next = row + 1;
26960 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26962 if (!next->enabled_p
26963 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26964 /* The first row >= START whose range of displayed characters
26965 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26966 is the row END + 1. */
26967 || (start_charpos < next_start
26968 && end_charpos < next_start)
26969 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26970 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26971 && !next->ends_at_zv_p
26972 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26973 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26974 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26975 && !next->ends_at_zv_p
26976 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26978 *end = row;
26979 break;
26981 else
26983 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26984 but none of the characters it displays are in the range, it is
26985 also END + 1. */
26986 struct glyph *g = next->glyphs[TEXT_AREA];
26987 struct glyph *s = g;
26988 struct glyph *e = g + next->used[TEXT_AREA];
26990 while (g < e)
26992 if (((BUFFERP (g->object) || INTEGERP (g->object))
26993 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26994 /* If the buffer position of the first glyph in
26995 the row is equal to END_CHARPOS, it means
26996 the last character to be highlighted is the
26997 newline of ROW, and we must consider NEXT as
26998 END, not END+1. */
26999 || (((!next->reversed_p && g == s)
27000 || (next->reversed_p && g == e - 1))
27001 && (g->charpos == end_charpos
27002 /* Special case for when NEXT is an
27003 empty line at ZV. */
27004 || (g->charpos == -1
27005 && !row->ends_at_zv_p
27006 && next_start == end_charpos)))))
27007 /* A glyph that comes from DISP_STRING is by
27008 definition to be highlighted. */
27009 || EQ (g->object, disp_string))
27010 break;
27011 g++;
27013 if (g == e)
27015 *end = row;
27016 break;
27018 /* The first row that ends at ZV must be the last to be
27019 highlighted. */
27020 else if (next->ends_at_zv_p)
27022 *end = next;
27023 break;
27029 /* This function sets the mouse_face_* elements of HLINFO, assuming
27030 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27031 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27032 for the overlay or run of text properties specifying the mouse
27033 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27034 before-string and after-string that must also be highlighted.
27035 DISP_STRING, if non-nil, is a display string that may cover some
27036 or all of the highlighted text. */
27038 static void
27039 mouse_face_from_buffer_pos (Lisp_Object window,
27040 Mouse_HLInfo *hlinfo,
27041 ptrdiff_t mouse_charpos,
27042 ptrdiff_t start_charpos,
27043 ptrdiff_t end_charpos,
27044 Lisp_Object before_string,
27045 Lisp_Object after_string,
27046 Lisp_Object disp_string)
27048 struct window *w = XWINDOW (window);
27049 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27050 struct glyph_row *r1, *r2;
27051 struct glyph *glyph, *end;
27052 ptrdiff_t ignore, pos;
27053 int x;
27055 eassert (NILP (disp_string) || STRINGP (disp_string));
27056 eassert (NILP (before_string) || STRINGP (before_string));
27057 eassert (NILP (after_string) || STRINGP (after_string));
27059 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27060 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27061 if (r1 == NULL)
27062 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
27063 /* If the before-string or display-string contains newlines,
27064 rows_from_pos_range skips to its last row. Move back. */
27065 if (!NILP (before_string) || !NILP (disp_string))
27067 struct glyph_row *prev;
27068 while ((prev = r1 - 1, prev >= first)
27069 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27070 && prev->used[TEXT_AREA] > 0)
27072 struct glyph *beg = prev->glyphs[TEXT_AREA];
27073 glyph = beg + prev->used[TEXT_AREA];
27074 while (--glyph >= beg && INTEGERP (glyph->object));
27075 if (glyph < beg
27076 || !(EQ (glyph->object, before_string)
27077 || EQ (glyph->object, disp_string)))
27078 break;
27079 r1 = prev;
27082 if (r2 == NULL)
27084 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
27085 hlinfo->mouse_face_past_end = 1;
27087 else if (!NILP (after_string))
27089 /* If the after-string has newlines, advance to its last row. */
27090 struct glyph_row *next;
27091 struct glyph_row *last
27092 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
27094 for (next = r2 + 1;
27095 next <= last
27096 && next->used[TEXT_AREA] > 0
27097 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27098 ++next)
27099 r2 = next;
27101 /* The rest of the display engine assumes that mouse_face_beg_row is
27102 either above mouse_face_end_row or identical to it. But with
27103 bidi-reordered continued lines, the row for START_CHARPOS could
27104 be below the row for END_CHARPOS. If so, swap the rows and store
27105 them in correct order. */
27106 if (r1->y > r2->y)
27108 struct glyph_row *tem = r2;
27110 r2 = r1;
27111 r1 = tem;
27114 hlinfo->mouse_face_beg_y = r1->y;
27115 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27116 hlinfo->mouse_face_end_y = r2->y;
27117 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27119 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27120 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27121 could be anywhere in the row and in any order. The strategy
27122 below is to find the leftmost and the rightmost glyph that
27123 belongs to either of these 3 strings, or whose position is
27124 between START_CHARPOS and END_CHARPOS, and highlight all the
27125 glyphs between those two. This may cover more than just the text
27126 between START_CHARPOS and END_CHARPOS if the range of characters
27127 strides the bidi level boundary, e.g. if the beginning is in R2L
27128 text while the end is in L2R text or vice versa. */
27129 if (!r1->reversed_p)
27131 /* This row is in a left to right paragraph. Scan it left to
27132 right. */
27133 glyph = r1->glyphs[TEXT_AREA];
27134 end = glyph + r1->used[TEXT_AREA];
27135 x = r1->x;
27137 /* Skip truncation glyphs at the start of the glyph row. */
27138 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27139 for (; glyph < end
27140 && INTEGERP (glyph->object)
27141 && glyph->charpos < 0;
27142 ++glyph)
27143 x += glyph->pixel_width;
27145 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27146 or DISP_STRING, and the first glyph from buffer whose
27147 position is between START_CHARPOS and END_CHARPOS. */
27148 for (; glyph < end
27149 && !INTEGERP (glyph->object)
27150 && !EQ (glyph->object, disp_string)
27151 && !(BUFFERP (glyph->object)
27152 && (glyph->charpos >= start_charpos
27153 && glyph->charpos < end_charpos));
27154 ++glyph)
27156 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27157 are present at buffer positions between START_CHARPOS and
27158 END_CHARPOS, or if they come from an overlay. */
27159 if (EQ (glyph->object, before_string))
27161 pos = string_buffer_position (before_string,
27162 start_charpos);
27163 /* If pos == 0, it means before_string came from an
27164 overlay, not from a buffer position. */
27165 if (!pos || (pos >= start_charpos && pos < end_charpos))
27166 break;
27168 else if (EQ (glyph->object, after_string))
27170 pos = string_buffer_position (after_string, end_charpos);
27171 if (!pos || (pos >= start_charpos && pos < end_charpos))
27172 break;
27174 x += glyph->pixel_width;
27176 hlinfo->mouse_face_beg_x = x;
27177 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27179 else
27181 /* This row is in a right to left paragraph. Scan it right to
27182 left. */
27183 struct glyph *g;
27185 end = r1->glyphs[TEXT_AREA] - 1;
27186 glyph = end + r1->used[TEXT_AREA];
27188 /* Skip truncation glyphs at the start of the glyph row. */
27189 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27190 for (; glyph > end
27191 && INTEGERP (glyph->object)
27192 && glyph->charpos < 0;
27193 --glyph)
27196 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27197 or DISP_STRING, and the first glyph from buffer whose
27198 position is between START_CHARPOS and END_CHARPOS. */
27199 for (; glyph > end
27200 && !INTEGERP (glyph->object)
27201 && !EQ (glyph->object, disp_string)
27202 && !(BUFFERP (glyph->object)
27203 && (glyph->charpos >= start_charpos
27204 && glyph->charpos < end_charpos));
27205 --glyph)
27207 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27208 are present at buffer positions between START_CHARPOS and
27209 END_CHARPOS, or if they come from an overlay. */
27210 if (EQ (glyph->object, before_string))
27212 pos = string_buffer_position (before_string, start_charpos);
27213 /* If pos == 0, it means before_string came from an
27214 overlay, not from a buffer position. */
27215 if (!pos || (pos >= start_charpos && pos < end_charpos))
27216 break;
27218 else if (EQ (glyph->object, after_string))
27220 pos = string_buffer_position (after_string, end_charpos);
27221 if (!pos || (pos >= start_charpos && pos < end_charpos))
27222 break;
27226 glyph++; /* first glyph to the right of the highlighted area */
27227 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27228 x += g->pixel_width;
27229 hlinfo->mouse_face_beg_x = x;
27230 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27233 /* If the highlight ends in a different row, compute GLYPH and END
27234 for the end row. Otherwise, reuse the values computed above for
27235 the row where the highlight begins. */
27236 if (r2 != r1)
27238 if (!r2->reversed_p)
27240 glyph = r2->glyphs[TEXT_AREA];
27241 end = glyph + r2->used[TEXT_AREA];
27242 x = r2->x;
27244 else
27246 end = r2->glyphs[TEXT_AREA] - 1;
27247 glyph = end + r2->used[TEXT_AREA];
27251 if (!r2->reversed_p)
27253 /* Skip truncation and continuation glyphs near the end of the
27254 row, and also blanks and stretch glyphs inserted by
27255 extend_face_to_end_of_line. */
27256 while (end > glyph
27257 && INTEGERP ((end - 1)->object))
27258 --end;
27259 /* Scan the rest of the glyph row from the end, looking for the
27260 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27261 DISP_STRING, or whose position is between START_CHARPOS
27262 and END_CHARPOS */
27263 for (--end;
27264 end > glyph
27265 && !INTEGERP (end->object)
27266 && !EQ (end->object, disp_string)
27267 && !(BUFFERP (end->object)
27268 && (end->charpos >= start_charpos
27269 && end->charpos < end_charpos));
27270 --end)
27272 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27273 are present at buffer positions between START_CHARPOS and
27274 END_CHARPOS, or if they come from an overlay. */
27275 if (EQ (end->object, before_string))
27277 pos = string_buffer_position (before_string, start_charpos);
27278 if (!pos || (pos >= start_charpos && pos < end_charpos))
27279 break;
27281 else if (EQ (end->object, after_string))
27283 pos = string_buffer_position (after_string, end_charpos);
27284 if (!pos || (pos >= start_charpos && pos < end_charpos))
27285 break;
27288 /* Find the X coordinate of the last glyph to be highlighted. */
27289 for (; glyph <= end; ++glyph)
27290 x += glyph->pixel_width;
27292 hlinfo->mouse_face_end_x = x;
27293 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27295 else
27297 /* Skip truncation and continuation glyphs near the end of the
27298 row, and also blanks and stretch glyphs inserted by
27299 extend_face_to_end_of_line. */
27300 x = r2->x;
27301 end++;
27302 while (end < glyph
27303 && INTEGERP (end->object))
27305 x += end->pixel_width;
27306 ++end;
27308 /* Scan the rest of the glyph row from the end, looking for the
27309 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27310 DISP_STRING, or whose position is between START_CHARPOS
27311 and END_CHARPOS */
27312 for ( ;
27313 end < glyph
27314 && !INTEGERP (end->object)
27315 && !EQ (end->object, disp_string)
27316 && !(BUFFERP (end->object)
27317 && (end->charpos >= start_charpos
27318 && end->charpos < end_charpos));
27319 ++end)
27321 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27322 are present at buffer positions between START_CHARPOS and
27323 END_CHARPOS, or if they come from an overlay. */
27324 if (EQ (end->object, before_string))
27326 pos = string_buffer_position (before_string, start_charpos);
27327 if (!pos || (pos >= start_charpos && pos < end_charpos))
27328 break;
27330 else if (EQ (end->object, after_string))
27332 pos = string_buffer_position (after_string, end_charpos);
27333 if (!pos || (pos >= start_charpos && pos < end_charpos))
27334 break;
27336 x += end->pixel_width;
27338 /* If we exited the above loop because we arrived at the last
27339 glyph of the row, and its buffer position is still not in
27340 range, it means the last character in range is the preceding
27341 newline. Bump the end column and x values to get past the
27342 last glyph. */
27343 if (end == glyph
27344 && BUFFERP (end->object)
27345 && (end->charpos < start_charpos
27346 || end->charpos >= end_charpos))
27348 x += end->pixel_width;
27349 ++end;
27351 hlinfo->mouse_face_end_x = x;
27352 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27355 hlinfo->mouse_face_window = window;
27356 hlinfo->mouse_face_face_id
27357 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27358 mouse_charpos + 1,
27359 !hlinfo->mouse_face_hidden, -1);
27360 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27363 /* The following function is not used anymore (replaced with
27364 mouse_face_from_string_pos), but I leave it here for the time
27365 being, in case someone would. */
27367 #if 0 /* not used */
27369 /* Find the position of the glyph for position POS in OBJECT in
27370 window W's current matrix, and return in *X, *Y the pixel
27371 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27373 RIGHT_P non-zero means return the position of the right edge of the
27374 glyph, RIGHT_P zero means return the left edge position.
27376 If no glyph for POS exists in the matrix, return the position of
27377 the glyph with the next smaller position that is in the matrix, if
27378 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27379 exists in the matrix, return the position of the glyph with the
27380 next larger position in OBJECT.
27382 Value is non-zero if a glyph was found. */
27384 static int
27385 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27386 int *hpos, int *vpos, int *x, int *y, int right_p)
27388 int yb = window_text_bottom_y (w);
27389 struct glyph_row *r;
27390 struct glyph *best_glyph = NULL;
27391 struct glyph_row *best_row = NULL;
27392 int best_x = 0;
27394 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27395 r->enabled_p && r->y < yb;
27396 ++r)
27398 struct glyph *g = r->glyphs[TEXT_AREA];
27399 struct glyph *e = g + r->used[TEXT_AREA];
27400 int gx;
27402 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27403 if (EQ (g->object, object))
27405 if (g->charpos == pos)
27407 best_glyph = g;
27408 best_x = gx;
27409 best_row = r;
27410 goto found;
27412 else if (best_glyph == NULL
27413 || ((eabs (g->charpos - pos)
27414 < eabs (best_glyph->charpos - pos))
27415 && (right_p
27416 ? g->charpos < pos
27417 : g->charpos > pos)))
27419 best_glyph = g;
27420 best_x = gx;
27421 best_row = r;
27426 found:
27428 if (best_glyph)
27430 *x = best_x;
27431 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27433 if (right_p)
27435 *x += best_glyph->pixel_width;
27436 ++*hpos;
27439 *y = best_row->y;
27440 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27443 return best_glyph != NULL;
27445 #endif /* not used */
27447 /* Find the positions of the first and the last glyphs in window W's
27448 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27449 (assumed to be a string), and return in HLINFO's mouse_face_*
27450 members the pixel and column/row coordinates of those glyphs. */
27452 static void
27453 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27454 Lisp_Object object,
27455 ptrdiff_t startpos, ptrdiff_t endpos)
27457 int yb = window_text_bottom_y (w);
27458 struct glyph_row *r;
27459 struct glyph *g, *e;
27460 int gx;
27461 int found = 0;
27463 /* Find the glyph row with at least one position in the range
27464 [STARTPOS..ENDPOS], and the first glyph in that row whose
27465 position belongs to that range. */
27466 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27467 r->enabled_p && r->y < yb;
27468 ++r)
27470 if (!r->reversed_p)
27472 g = r->glyphs[TEXT_AREA];
27473 e = g + r->used[TEXT_AREA];
27474 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27475 if (EQ (g->object, object)
27476 && startpos <= g->charpos && g->charpos <= endpos)
27478 hlinfo->mouse_face_beg_row
27479 = MATRIX_ROW_VPOS (r, w->current_matrix);
27480 hlinfo->mouse_face_beg_y = r->y;
27481 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27482 hlinfo->mouse_face_beg_x = gx;
27483 found = 1;
27484 break;
27487 else
27489 struct glyph *g1;
27491 e = r->glyphs[TEXT_AREA];
27492 g = e + r->used[TEXT_AREA];
27493 for ( ; g > e; --g)
27494 if (EQ ((g-1)->object, object)
27495 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27497 hlinfo->mouse_face_beg_row
27498 = MATRIX_ROW_VPOS (r, w->current_matrix);
27499 hlinfo->mouse_face_beg_y = r->y;
27500 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27501 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27502 gx += g1->pixel_width;
27503 hlinfo->mouse_face_beg_x = gx;
27504 found = 1;
27505 break;
27508 if (found)
27509 break;
27512 if (!found)
27513 return;
27515 /* Starting with the next row, look for the first row which does NOT
27516 include any glyphs whose positions are in the range. */
27517 for (++r; r->enabled_p && r->y < yb; ++r)
27519 g = r->glyphs[TEXT_AREA];
27520 e = g + r->used[TEXT_AREA];
27521 found = 0;
27522 for ( ; g < e; ++g)
27523 if (EQ (g->object, object)
27524 && startpos <= g->charpos && g->charpos <= endpos)
27526 found = 1;
27527 break;
27529 if (!found)
27530 break;
27533 /* The highlighted region ends on the previous row. */
27534 r--;
27536 /* Set the end row and its vertical pixel coordinate. */
27537 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27538 hlinfo->mouse_face_end_y = r->y;
27540 /* Compute and set the end column and the end column's horizontal
27541 pixel coordinate. */
27542 if (!r->reversed_p)
27544 g = r->glyphs[TEXT_AREA];
27545 e = g + r->used[TEXT_AREA];
27546 for ( ; e > g; --e)
27547 if (EQ ((e-1)->object, object)
27548 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27549 break;
27550 hlinfo->mouse_face_end_col = e - g;
27552 for (gx = r->x; g < e; ++g)
27553 gx += g->pixel_width;
27554 hlinfo->mouse_face_end_x = gx;
27556 else
27558 e = r->glyphs[TEXT_AREA];
27559 g = e + r->used[TEXT_AREA];
27560 for (gx = r->x ; e < g; ++e)
27562 if (EQ (e->object, object)
27563 && startpos <= e->charpos && e->charpos <= endpos)
27564 break;
27565 gx += e->pixel_width;
27567 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27568 hlinfo->mouse_face_end_x = gx;
27572 #ifdef HAVE_WINDOW_SYSTEM
27574 /* See if position X, Y is within a hot-spot of an image. */
27576 static int
27577 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27579 if (!CONSP (hot_spot))
27580 return 0;
27582 if (EQ (XCAR (hot_spot), Qrect))
27584 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27585 Lisp_Object rect = XCDR (hot_spot);
27586 Lisp_Object tem;
27587 if (!CONSP (rect))
27588 return 0;
27589 if (!CONSP (XCAR (rect)))
27590 return 0;
27591 if (!CONSP (XCDR (rect)))
27592 return 0;
27593 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27594 return 0;
27595 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27596 return 0;
27597 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27598 return 0;
27599 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27600 return 0;
27601 return 1;
27603 else if (EQ (XCAR (hot_spot), Qcircle))
27605 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27606 Lisp_Object circ = XCDR (hot_spot);
27607 Lisp_Object lr, lx0, ly0;
27608 if (CONSP (circ)
27609 && CONSP (XCAR (circ))
27610 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27611 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27612 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27614 double r = XFLOATINT (lr);
27615 double dx = XINT (lx0) - x;
27616 double dy = XINT (ly0) - y;
27617 return (dx * dx + dy * dy <= r * r);
27620 else if (EQ (XCAR (hot_spot), Qpoly))
27622 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27623 if (VECTORP (XCDR (hot_spot)))
27625 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27626 Lisp_Object *poly = v->contents;
27627 ptrdiff_t n = v->header.size;
27628 ptrdiff_t i;
27629 int inside = 0;
27630 Lisp_Object lx, ly;
27631 int x0, y0;
27633 /* Need an even number of coordinates, and at least 3 edges. */
27634 if (n < 6 || n & 1)
27635 return 0;
27637 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27638 If count is odd, we are inside polygon. Pixels on edges
27639 may or may not be included depending on actual geometry of the
27640 polygon. */
27641 if ((lx = poly[n-2], !INTEGERP (lx))
27642 || (ly = poly[n-1], !INTEGERP (lx)))
27643 return 0;
27644 x0 = XINT (lx), y0 = XINT (ly);
27645 for (i = 0; i < n; i += 2)
27647 int x1 = x0, y1 = y0;
27648 if ((lx = poly[i], !INTEGERP (lx))
27649 || (ly = poly[i+1], !INTEGERP (ly)))
27650 return 0;
27651 x0 = XINT (lx), y0 = XINT (ly);
27653 /* Does this segment cross the X line? */
27654 if (x0 >= x)
27656 if (x1 >= x)
27657 continue;
27659 else if (x1 < x)
27660 continue;
27661 if (y > y0 && y > y1)
27662 continue;
27663 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27664 inside = !inside;
27666 return inside;
27669 return 0;
27672 Lisp_Object
27673 find_hot_spot (Lisp_Object map, int x, int y)
27675 while (CONSP (map))
27677 if (CONSP (XCAR (map))
27678 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27679 return XCAR (map);
27680 map = XCDR (map);
27683 return Qnil;
27686 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27687 3, 3, 0,
27688 doc: /* Lookup in image map MAP coordinates X and Y.
27689 An image map is an alist where each element has the format (AREA ID PLIST).
27690 An AREA is specified as either a rectangle, a circle, or a polygon:
27691 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27692 pixel coordinates of the upper left and bottom right corners.
27693 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27694 and the radius of the circle; r may be a float or integer.
27695 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27696 vector describes one corner in the polygon.
27697 Returns the alist element for the first matching AREA in MAP. */)
27698 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27700 if (NILP (map))
27701 return Qnil;
27703 CHECK_NUMBER (x);
27704 CHECK_NUMBER (y);
27706 return find_hot_spot (map,
27707 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27708 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27712 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27713 static void
27714 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27716 /* Do not change cursor shape while dragging mouse. */
27717 if (!NILP (do_mouse_tracking))
27718 return;
27720 if (!NILP (pointer))
27722 if (EQ (pointer, Qarrow))
27723 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27724 else if (EQ (pointer, Qhand))
27725 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27726 else if (EQ (pointer, Qtext))
27727 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27728 else if (EQ (pointer, intern ("hdrag")))
27729 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27730 #ifdef HAVE_X_WINDOWS
27731 else if (EQ (pointer, intern ("vdrag")))
27732 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27733 #endif
27734 else if (EQ (pointer, intern ("hourglass")))
27735 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27736 else if (EQ (pointer, Qmodeline))
27737 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27738 else
27739 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27742 if (cursor != No_Cursor)
27743 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27746 #endif /* HAVE_WINDOW_SYSTEM */
27748 /* Take proper action when mouse has moved to the mode or header line
27749 or marginal area AREA of window W, x-position X and y-position Y.
27750 X is relative to the start of the text display area of W, so the
27751 width of bitmap areas and scroll bars must be subtracted to get a
27752 position relative to the start of the mode line. */
27754 static void
27755 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27756 enum window_part area)
27758 struct window *w = XWINDOW (window);
27759 struct frame *f = XFRAME (w->frame);
27760 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27761 #ifdef HAVE_WINDOW_SYSTEM
27762 Display_Info *dpyinfo;
27763 #endif
27764 Cursor cursor = No_Cursor;
27765 Lisp_Object pointer = Qnil;
27766 int dx, dy, width, height;
27767 ptrdiff_t charpos;
27768 Lisp_Object string, object = Qnil;
27769 Lisp_Object pos IF_LINT (= Qnil), help;
27771 Lisp_Object mouse_face;
27772 int original_x_pixel = x;
27773 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27774 struct glyph_row *row IF_LINT (= 0);
27776 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27778 int x0;
27779 struct glyph *end;
27781 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27782 returns them in row/column units! */
27783 string = mode_line_string (w, area, &x, &y, &charpos,
27784 &object, &dx, &dy, &width, &height);
27786 row = (area == ON_MODE_LINE
27787 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27788 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27790 /* Find the glyph under the mouse pointer. */
27791 if (row->mode_line_p && row->enabled_p)
27793 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27794 end = glyph + row->used[TEXT_AREA];
27796 for (x0 = original_x_pixel;
27797 glyph < end && x0 >= glyph->pixel_width;
27798 ++glyph)
27799 x0 -= glyph->pixel_width;
27801 if (glyph >= end)
27802 glyph = NULL;
27805 else
27807 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27808 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27809 returns them in row/column units! */
27810 string = marginal_area_string (w, area, &x, &y, &charpos,
27811 &object, &dx, &dy, &width, &height);
27814 help = Qnil;
27816 #ifdef HAVE_WINDOW_SYSTEM
27817 if (IMAGEP (object))
27819 Lisp_Object image_map, hotspot;
27820 if ((image_map = Fplist_get (XCDR (object), QCmap),
27821 !NILP (image_map))
27822 && (hotspot = find_hot_spot (image_map, dx, dy),
27823 CONSP (hotspot))
27824 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27826 Lisp_Object plist;
27828 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27829 If so, we could look for mouse-enter, mouse-leave
27830 properties in PLIST (and do something...). */
27831 hotspot = XCDR (hotspot);
27832 if (CONSP (hotspot)
27833 && (plist = XCAR (hotspot), CONSP (plist)))
27835 pointer = Fplist_get (plist, Qpointer);
27836 if (NILP (pointer))
27837 pointer = Qhand;
27838 help = Fplist_get (plist, Qhelp_echo);
27839 if (!NILP (help))
27841 help_echo_string = help;
27842 XSETWINDOW (help_echo_window, w);
27843 help_echo_object = w->contents;
27844 help_echo_pos = charpos;
27848 if (NILP (pointer))
27849 pointer = Fplist_get (XCDR (object), QCpointer);
27851 #endif /* HAVE_WINDOW_SYSTEM */
27853 if (STRINGP (string))
27854 pos = make_number (charpos);
27856 /* Set the help text and mouse pointer. If the mouse is on a part
27857 of the mode line without any text (e.g. past the right edge of
27858 the mode line text), use the default help text and pointer. */
27859 if (STRINGP (string) || area == ON_MODE_LINE)
27861 /* Arrange to display the help by setting the global variables
27862 help_echo_string, help_echo_object, and help_echo_pos. */
27863 if (NILP (help))
27865 if (STRINGP (string))
27866 help = Fget_text_property (pos, Qhelp_echo, string);
27868 if (!NILP (help))
27870 help_echo_string = help;
27871 XSETWINDOW (help_echo_window, w);
27872 help_echo_object = string;
27873 help_echo_pos = charpos;
27875 else if (area == ON_MODE_LINE)
27877 Lisp_Object default_help
27878 = buffer_local_value_1 (Qmode_line_default_help_echo,
27879 w->contents);
27881 if (STRINGP (default_help))
27883 help_echo_string = default_help;
27884 XSETWINDOW (help_echo_window, w);
27885 help_echo_object = Qnil;
27886 help_echo_pos = -1;
27891 #ifdef HAVE_WINDOW_SYSTEM
27892 /* Change the mouse pointer according to what is under it. */
27893 if (FRAME_WINDOW_P (f))
27895 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27896 if (STRINGP (string))
27898 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27900 if (NILP (pointer))
27901 pointer = Fget_text_property (pos, Qpointer, string);
27903 /* Change the mouse pointer according to what is under X/Y. */
27904 if (NILP (pointer)
27905 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27907 Lisp_Object map;
27908 map = Fget_text_property (pos, Qlocal_map, string);
27909 if (!KEYMAPP (map))
27910 map = Fget_text_property (pos, Qkeymap, string);
27911 if (!KEYMAPP (map))
27912 cursor = dpyinfo->vertical_scroll_bar_cursor;
27915 else
27916 /* Default mode-line pointer. */
27917 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27919 #endif
27922 /* Change the mouse face according to what is under X/Y. */
27923 if (STRINGP (string))
27925 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27926 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
27927 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27928 && glyph)
27930 Lisp_Object b, e;
27932 struct glyph * tmp_glyph;
27934 int gpos;
27935 int gseq_length;
27936 int total_pixel_width;
27937 ptrdiff_t begpos, endpos, ignore;
27939 int vpos, hpos;
27941 b = Fprevious_single_property_change (make_number (charpos + 1),
27942 Qmouse_face, string, Qnil);
27943 if (NILP (b))
27944 begpos = 0;
27945 else
27946 begpos = XINT (b);
27948 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27949 if (NILP (e))
27950 endpos = SCHARS (string);
27951 else
27952 endpos = XINT (e);
27954 /* Calculate the glyph position GPOS of GLYPH in the
27955 displayed string, relative to the beginning of the
27956 highlighted part of the string.
27958 Note: GPOS is different from CHARPOS. CHARPOS is the
27959 position of GLYPH in the internal string object. A mode
27960 line string format has structures which are converted to
27961 a flattened string by the Emacs Lisp interpreter. The
27962 internal string is an element of those structures. The
27963 displayed string is the flattened string. */
27964 tmp_glyph = row_start_glyph;
27965 while (tmp_glyph < glyph
27966 && (!(EQ (tmp_glyph->object, glyph->object)
27967 && begpos <= tmp_glyph->charpos
27968 && tmp_glyph->charpos < endpos)))
27969 tmp_glyph++;
27970 gpos = glyph - tmp_glyph;
27972 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27973 the highlighted part of the displayed string to which
27974 GLYPH belongs. Note: GSEQ_LENGTH is different from
27975 SCHARS (STRING), because the latter returns the length of
27976 the internal string. */
27977 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27978 tmp_glyph > glyph
27979 && (!(EQ (tmp_glyph->object, glyph->object)
27980 && begpos <= tmp_glyph->charpos
27981 && tmp_glyph->charpos < endpos));
27982 tmp_glyph--)
27984 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27986 /* Calculate the total pixel width of all the glyphs between
27987 the beginning of the highlighted area and GLYPH. */
27988 total_pixel_width = 0;
27989 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27990 total_pixel_width += tmp_glyph->pixel_width;
27992 /* Pre calculation of re-rendering position. Note: X is in
27993 column units here, after the call to mode_line_string or
27994 marginal_area_string. */
27995 hpos = x - gpos;
27996 vpos = (area == ON_MODE_LINE
27997 ? (w->current_matrix)->nrows - 1
27998 : 0);
28000 /* If GLYPH's position is included in the region that is
28001 already drawn in mouse face, we have nothing to do. */
28002 if ( EQ (window, hlinfo->mouse_face_window)
28003 && (!row->reversed_p
28004 ? (hlinfo->mouse_face_beg_col <= hpos
28005 && hpos < hlinfo->mouse_face_end_col)
28006 /* In R2L rows we swap BEG and END, see below. */
28007 : (hlinfo->mouse_face_end_col <= hpos
28008 && hpos < hlinfo->mouse_face_beg_col))
28009 && hlinfo->mouse_face_beg_row == vpos )
28010 return;
28012 if (clear_mouse_face (hlinfo))
28013 cursor = No_Cursor;
28015 if (!row->reversed_p)
28017 hlinfo->mouse_face_beg_col = hpos;
28018 hlinfo->mouse_face_beg_x = original_x_pixel
28019 - (total_pixel_width + dx);
28020 hlinfo->mouse_face_end_col = hpos + gseq_length;
28021 hlinfo->mouse_face_end_x = 0;
28023 else
28025 /* In R2L rows, show_mouse_face expects BEG and END
28026 coordinates to be swapped. */
28027 hlinfo->mouse_face_end_col = hpos;
28028 hlinfo->mouse_face_end_x = original_x_pixel
28029 - (total_pixel_width + dx);
28030 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28031 hlinfo->mouse_face_beg_x = 0;
28034 hlinfo->mouse_face_beg_row = vpos;
28035 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28036 hlinfo->mouse_face_beg_y = 0;
28037 hlinfo->mouse_face_end_y = 0;
28038 hlinfo->mouse_face_past_end = 0;
28039 hlinfo->mouse_face_window = window;
28041 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28042 charpos,
28043 0, 0, 0,
28044 &ignore,
28045 glyph->face_id,
28047 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28049 if (NILP (pointer))
28050 pointer = Qhand;
28052 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28053 clear_mouse_face (hlinfo);
28055 #ifdef HAVE_WINDOW_SYSTEM
28056 if (FRAME_WINDOW_P (f))
28057 define_frame_cursor1 (f, cursor, pointer);
28058 #endif
28062 /* EXPORT:
28063 Take proper action when the mouse has moved to position X, Y on
28064 frame F with regards to highlighting portions of display that have
28065 mouse-face properties. Also de-highlight portions of display where
28066 the mouse was before, set the mouse pointer shape as appropriate
28067 for the mouse coordinates, and activate help echo (tooltips).
28068 X and Y can be negative or out of range. */
28070 void
28071 note_mouse_highlight (struct frame *f, int x, int y)
28073 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28074 enum window_part part = ON_NOTHING;
28075 Lisp_Object window;
28076 struct window *w;
28077 Cursor cursor = No_Cursor;
28078 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28079 struct buffer *b;
28081 /* When a menu is active, don't highlight because this looks odd. */
28082 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28083 if (popup_activated ())
28084 return;
28085 #endif
28087 if (!f->glyphs_initialized_p
28088 || f->pointer_invisible)
28089 return;
28091 hlinfo->mouse_face_mouse_x = x;
28092 hlinfo->mouse_face_mouse_y = y;
28093 hlinfo->mouse_face_mouse_frame = f;
28095 if (hlinfo->mouse_face_defer)
28096 return;
28098 /* Which window is that in? */
28099 window = window_from_coordinates (f, x, y, &part, 1);
28101 /* If displaying active text in another window, clear that. */
28102 if (! EQ (window, hlinfo->mouse_face_window)
28103 /* Also clear if we move out of text area in same window. */
28104 || (!NILP (hlinfo->mouse_face_window)
28105 && !NILP (window)
28106 && part != ON_TEXT
28107 && part != ON_MODE_LINE
28108 && part != ON_HEADER_LINE))
28109 clear_mouse_face (hlinfo);
28111 /* Not on a window -> return. */
28112 if (!WINDOWP (window))
28113 return;
28115 /* Reset help_echo_string. It will get recomputed below. */
28116 help_echo_string = Qnil;
28118 /* Convert to window-relative pixel coordinates. */
28119 w = XWINDOW (window);
28120 frame_to_window_pixel_xy (w, &x, &y);
28122 #ifdef HAVE_WINDOW_SYSTEM
28123 /* Handle tool-bar window differently since it doesn't display a
28124 buffer. */
28125 if (EQ (window, f->tool_bar_window))
28127 note_tool_bar_highlight (f, x, y);
28128 return;
28130 #endif
28132 /* Mouse is on the mode, header line or margin? */
28133 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28134 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28136 note_mode_line_or_margin_highlight (window, x, y, part);
28137 return;
28140 #ifdef HAVE_WINDOW_SYSTEM
28141 if (part == ON_VERTICAL_BORDER)
28143 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28144 help_echo_string = build_string ("drag-mouse-1: resize");
28146 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28147 || part == ON_SCROLL_BAR)
28148 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28149 else
28150 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28151 #endif
28153 /* Are we in a window whose display is up to date?
28154 And verify the buffer's text has not changed. */
28155 b = XBUFFER (w->contents);
28156 if (part == ON_TEXT
28157 && w->window_end_valid
28158 && w->last_modified == BUF_MODIFF (b)
28159 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
28161 int hpos, vpos, dx, dy, area = LAST_AREA;
28162 ptrdiff_t pos;
28163 struct glyph *glyph;
28164 Lisp_Object object;
28165 Lisp_Object mouse_face = Qnil, position;
28166 Lisp_Object *overlay_vec = NULL;
28167 ptrdiff_t i, noverlays;
28168 struct buffer *obuf;
28169 ptrdiff_t obegv, ozv;
28170 int same_region;
28172 /* Find the glyph under X/Y. */
28173 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28175 #ifdef HAVE_WINDOW_SYSTEM
28176 /* Look for :pointer property on image. */
28177 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28179 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28180 if (img != NULL && IMAGEP (img->spec))
28182 Lisp_Object image_map, hotspot;
28183 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28184 !NILP (image_map))
28185 && (hotspot = find_hot_spot (image_map,
28186 glyph->slice.img.x + dx,
28187 glyph->slice.img.y + dy),
28188 CONSP (hotspot))
28189 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28191 Lisp_Object plist;
28193 /* Could check XCAR (hotspot) to see if we enter/leave
28194 this hot-spot.
28195 If so, we could look for mouse-enter, mouse-leave
28196 properties in PLIST (and do something...). */
28197 hotspot = XCDR (hotspot);
28198 if (CONSP (hotspot)
28199 && (plist = XCAR (hotspot), CONSP (plist)))
28201 pointer = Fplist_get (plist, Qpointer);
28202 if (NILP (pointer))
28203 pointer = Qhand;
28204 help_echo_string = Fplist_get (plist, Qhelp_echo);
28205 if (!NILP (help_echo_string))
28207 help_echo_window = window;
28208 help_echo_object = glyph->object;
28209 help_echo_pos = glyph->charpos;
28213 if (NILP (pointer))
28214 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28217 #endif /* HAVE_WINDOW_SYSTEM */
28219 /* Clear mouse face if X/Y not over text. */
28220 if (glyph == NULL
28221 || area != TEXT_AREA
28222 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28223 /* Glyph's OBJECT is an integer for glyphs inserted by the
28224 display engine for its internal purposes, like truncation
28225 and continuation glyphs and blanks beyond the end of
28226 line's text on text terminals. If we are over such a
28227 glyph, we are not over any text. */
28228 || INTEGERP (glyph->object)
28229 /* R2L rows have a stretch glyph at their front, which
28230 stands for no text, whereas L2R rows have no glyphs at
28231 all beyond the end of text. Treat such stretch glyphs
28232 like we do with NULL glyphs in L2R rows. */
28233 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28234 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28235 && glyph->type == STRETCH_GLYPH
28236 && glyph->avoid_cursor_p))
28238 if (clear_mouse_face (hlinfo))
28239 cursor = No_Cursor;
28240 #ifdef HAVE_WINDOW_SYSTEM
28241 if (FRAME_WINDOW_P (f) && NILP (pointer))
28243 if (area != TEXT_AREA)
28244 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28245 else
28246 pointer = Vvoid_text_area_pointer;
28248 #endif
28249 goto set_cursor;
28252 pos = glyph->charpos;
28253 object = glyph->object;
28254 if (!STRINGP (object) && !BUFFERP (object))
28255 goto set_cursor;
28257 /* If we get an out-of-range value, return now; avoid an error. */
28258 if (BUFFERP (object) && pos > BUF_Z (b))
28259 goto set_cursor;
28261 /* Make the window's buffer temporarily current for
28262 overlays_at and compute_char_face. */
28263 obuf = current_buffer;
28264 current_buffer = b;
28265 obegv = BEGV;
28266 ozv = ZV;
28267 BEGV = BEG;
28268 ZV = Z;
28270 /* Is this char mouse-active or does it have help-echo? */
28271 position = make_number (pos);
28273 if (BUFFERP (object))
28275 /* Put all the overlays we want in a vector in overlay_vec. */
28276 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28277 /* Sort overlays into increasing priority order. */
28278 noverlays = sort_overlays (overlay_vec, noverlays, w);
28280 else
28281 noverlays = 0;
28283 if (NILP (Vmouse_highlight))
28285 clear_mouse_face (hlinfo);
28286 goto check_help_echo;
28289 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28291 if (same_region)
28292 cursor = No_Cursor;
28294 /* Check mouse-face highlighting. */
28295 if (! same_region
28296 /* If there exists an overlay with mouse-face overlapping
28297 the one we are currently highlighting, we have to
28298 check if we enter the overlapping overlay, and then
28299 highlight only that. */
28300 || (OVERLAYP (hlinfo->mouse_face_overlay)
28301 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28303 /* Find the highest priority overlay with a mouse-face. */
28304 Lisp_Object overlay = Qnil;
28305 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28307 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28308 if (!NILP (mouse_face))
28309 overlay = overlay_vec[i];
28312 /* If we're highlighting the same overlay as before, there's
28313 no need to do that again. */
28314 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28315 goto check_help_echo;
28316 hlinfo->mouse_face_overlay = overlay;
28318 /* Clear the display of the old active region, if any. */
28319 if (clear_mouse_face (hlinfo))
28320 cursor = No_Cursor;
28322 /* If no overlay applies, get a text property. */
28323 if (NILP (overlay))
28324 mouse_face = Fget_text_property (position, Qmouse_face, object);
28326 /* Next, compute the bounds of the mouse highlighting and
28327 display it. */
28328 if (!NILP (mouse_face) && STRINGP (object))
28330 /* The mouse-highlighting comes from a display string
28331 with a mouse-face. */
28332 Lisp_Object s, e;
28333 ptrdiff_t ignore;
28335 s = Fprevious_single_property_change
28336 (make_number (pos + 1), Qmouse_face, object, Qnil);
28337 e = Fnext_single_property_change
28338 (position, Qmouse_face, object, Qnil);
28339 if (NILP (s))
28340 s = make_number (0);
28341 if (NILP (e))
28342 e = make_number (SCHARS (object) - 1);
28343 mouse_face_from_string_pos (w, hlinfo, object,
28344 XINT (s), XINT (e));
28345 hlinfo->mouse_face_past_end = 0;
28346 hlinfo->mouse_face_window = window;
28347 hlinfo->mouse_face_face_id
28348 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28349 glyph->face_id, 1);
28350 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28351 cursor = No_Cursor;
28353 else
28355 /* The mouse-highlighting, if any, comes from an overlay
28356 or text property in the buffer. */
28357 Lisp_Object buffer IF_LINT (= Qnil);
28358 Lisp_Object disp_string IF_LINT (= Qnil);
28360 if (STRINGP (object))
28362 /* If we are on a display string with no mouse-face,
28363 check if the text under it has one. */
28364 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28365 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28366 pos = string_buffer_position (object, start);
28367 if (pos > 0)
28369 mouse_face = get_char_property_and_overlay
28370 (make_number (pos), Qmouse_face, w->contents, &overlay);
28371 buffer = w->contents;
28372 disp_string = object;
28375 else
28377 buffer = object;
28378 disp_string = Qnil;
28381 if (!NILP (mouse_face))
28383 Lisp_Object before, after;
28384 Lisp_Object before_string, after_string;
28385 /* To correctly find the limits of mouse highlight
28386 in a bidi-reordered buffer, we must not use the
28387 optimization of limiting the search in
28388 previous-single-property-change and
28389 next-single-property-change, because
28390 rows_from_pos_range needs the real start and end
28391 positions to DTRT in this case. That's because
28392 the first row visible in a window does not
28393 necessarily display the character whose position
28394 is the smallest. */
28395 Lisp_Object lim1 =
28396 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28397 ? Fmarker_position (w->start)
28398 : Qnil;
28399 Lisp_Object lim2 =
28400 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28401 ? make_number (BUF_Z (XBUFFER (buffer))
28402 - XFASTINT (w->window_end_pos))
28403 : Qnil;
28405 if (NILP (overlay))
28407 /* Handle the text property case. */
28408 before = Fprevious_single_property_change
28409 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28410 after = Fnext_single_property_change
28411 (make_number (pos), Qmouse_face, buffer, lim2);
28412 before_string = after_string = Qnil;
28414 else
28416 /* Handle the overlay case. */
28417 before = Foverlay_start (overlay);
28418 after = Foverlay_end (overlay);
28419 before_string = Foverlay_get (overlay, Qbefore_string);
28420 after_string = Foverlay_get (overlay, Qafter_string);
28422 if (!STRINGP (before_string)) before_string = Qnil;
28423 if (!STRINGP (after_string)) after_string = Qnil;
28426 mouse_face_from_buffer_pos (window, hlinfo, pos,
28427 NILP (before)
28429 : XFASTINT (before),
28430 NILP (after)
28431 ? BUF_Z (XBUFFER (buffer))
28432 : XFASTINT (after),
28433 before_string, after_string,
28434 disp_string);
28435 cursor = No_Cursor;
28440 check_help_echo:
28442 /* Look for a `help-echo' property. */
28443 if (NILP (help_echo_string)) {
28444 Lisp_Object help, overlay;
28446 /* Check overlays first. */
28447 help = overlay = Qnil;
28448 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28450 overlay = overlay_vec[i];
28451 help = Foverlay_get (overlay, Qhelp_echo);
28454 if (!NILP (help))
28456 help_echo_string = help;
28457 help_echo_window = window;
28458 help_echo_object = overlay;
28459 help_echo_pos = pos;
28461 else
28463 Lisp_Object obj = glyph->object;
28464 ptrdiff_t charpos = glyph->charpos;
28466 /* Try text properties. */
28467 if (STRINGP (obj)
28468 && charpos >= 0
28469 && charpos < SCHARS (obj))
28471 help = Fget_text_property (make_number (charpos),
28472 Qhelp_echo, obj);
28473 if (NILP (help))
28475 /* If the string itself doesn't specify a help-echo,
28476 see if the buffer text ``under'' it does. */
28477 struct glyph_row *r
28478 = MATRIX_ROW (w->current_matrix, vpos);
28479 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28480 ptrdiff_t p = string_buffer_position (obj, start);
28481 if (p > 0)
28483 help = Fget_char_property (make_number (p),
28484 Qhelp_echo, w->contents);
28485 if (!NILP (help))
28487 charpos = p;
28488 obj = w->contents;
28493 else if (BUFFERP (obj)
28494 && charpos >= BEGV
28495 && charpos < ZV)
28496 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28497 obj);
28499 if (!NILP (help))
28501 help_echo_string = help;
28502 help_echo_window = window;
28503 help_echo_object = obj;
28504 help_echo_pos = charpos;
28509 #ifdef HAVE_WINDOW_SYSTEM
28510 /* Look for a `pointer' property. */
28511 if (FRAME_WINDOW_P (f) && NILP (pointer))
28513 /* Check overlays first. */
28514 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28515 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28517 if (NILP (pointer))
28519 Lisp_Object obj = glyph->object;
28520 ptrdiff_t charpos = glyph->charpos;
28522 /* Try text properties. */
28523 if (STRINGP (obj)
28524 && charpos >= 0
28525 && charpos < SCHARS (obj))
28527 pointer = Fget_text_property (make_number (charpos),
28528 Qpointer, obj);
28529 if (NILP (pointer))
28531 /* If the string itself doesn't specify a pointer,
28532 see if the buffer text ``under'' it does. */
28533 struct glyph_row *r
28534 = MATRIX_ROW (w->current_matrix, vpos);
28535 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28536 ptrdiff_t p = string_buffer_position (obj, start);
28537 if (p > 0)
28538 pointer = Fget_char_property (make_number (p),
28539 Qpointer, w->contents);
28542 else if (BUFFERP (obj)
28543 && charpos >= BEGV
28544 && charpos < ZV)
28545 pointer = Fget_text_property (make_number (charpos),
28546 Qpointer, obj);
28549 #endif /* HAVE_WINDOW_SYSTEM */
28551 BEGV = obegv;
28552 ZV = ozv;
28553 current_buffer = obuf;
28556 set_cursor:
28558 #ifdef HAVE_WINDOW_SYSTEM
28559 if (FRAME_WINDOW_P (f))
28560 define_frame_cursor1 (f, cursor, pointer);
28561 #else
28562 /* This is here to prevent a compiler error, about "label at end of
28563 compound statement". */
28564 return;
28565 #endif
28569 /* EXPORT for RIF:
28570 Clear any mouse-face on window W. This function is part of the
28571 redisplay interface, and is called from try_window_id and similar
28572 functions to ensure the mouse-highlight is off. */
28574 void
28575 x_clear_window_mouse_face (struct window *w)
28577 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28578 Lisp_Object window;
28580 block_input ();
28581 XSETWINDOW (window, w);
28582 if (EQ (window, hlinfo->mouse_face_window))
28583 clear_mouse_face (hlinfo);
28584 unblock_input ();
28588 /* EXPORT:
28589 Just discard the mouse face information for frame F, if any.
28590 This is used when the size of F is changed. */
28592 void
28593 cancel_mouse_face (struct frame *f)
28595 Lisp_Object window;
28596 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28598 window = hlinfo->mouse_face_window;
28599 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28601 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28602 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28603 hlinfo->mouse_face_window = Qnil;
28609 /***********************************************************************
28610 Exposure Events
28611 ***********************************************************************/
28613 #ifdef HAVE_WINDOW_SYSTEM
28615 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28616 which intersects rectangle R. R is in window-relative coordinates. */
28618 static void
28619 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28620 enum glyph_row_area area)
28622 struct glyph *first = row->glyphs[area];
28623 struct glyph *end = row->glyphs[area] + row->used[area];
28624 struct glyph *last;
28625 int first_x, start_x, x;
28627 if (area == TEXT_AREA && row->fill_line_p)
28628 /* If row extends face to end of line write the whole line. */
28629 draw_glyphs (w, 0, row, area,
28630 0, row->used[area],
28631 DRAW_NORMAL_TEXT, 0);
28632 else
28634 /* Set START_X to the window-relative start position for drawing glyphs of
28635 AREA. The first glyph of the text area can be partially visible.
28636 The first glyphs of other areas cannot. */
28637 start_x = window_box_left_offset (w, area);
28638 x = start_x;
28639 if (area == TEXT_AREA)
28640 x += row->x;
28642 /* Find the first glyph that must be redrawn. */
28643 while (first < end
28644 && x + first->pixel_width < r->x)
28646 x += first->pixel_width;
28647 ++first;
28650 /* Find the last one. */
28651 last = first;
28652 first_x = x;
28653 while (last < end
28654 && x < r->x + r->width)
28656 x += last->pixel_width;
28657 ++last;
28660 /* Repaint. */
28661 if (last > first)
28662 draw_glyphs (w, first_x - start_x, row, area,
28663 first - row->glyphs[area], last - row->glyphs[area],
28664 DRAW_NORMAL_TEXT, 0);
28669 /* Redraw the parts of the glyph row ROW on window W intersecting
28670 rectangle R. R is in window-relative coordinates. Value is
28671 non-zero if mouse-face was overwritten. */
28673 static int
28674 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28676 eassert (row->enabled_p);
28678 if (row->mode_line_p || w->pseudo_window_p)
28679 draw_glyphs (w, 0, row, TEXT_AREA,
28680 0, row->used[TEXT_AREA],
28681 DRAW_NORMAL_TEXT, 0);
28682 else
28684 if (row->used[LEFT_MARGIN_AREA])
28685 expose_area (w, row, r, LEFT_MARGIN_AREA);
28686 if (row->used[TEXT_AREA])
28687 expose_area (w, row, r, TEXT_AREA);
28688 if (row->used[RIGHT_MARGIN_AREA])
28689 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28690 draw_row_fringe_bitmaps (w, row);
28693 return row->mouse_face_p;
28697 /* Redraw those parts of glyphs rows during expose event handling that
28698 overlap other rows. Redrawing of an exposed line writes over parts
28699 of lines overlapping that exposed line; this function fixes that.
28701 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28702 row in W's current matrix that is exposed and overlaps other rows.
28703 LAST_OVERLAPPING_ROW is the last such row. */
28705 static void
28706 expose_overlaps (struct window *w,
28707 struct glyph_row *first_overlapping_row,
28708 struct glyph_row *last_overlapping_row,
28709 XRectangle *r)
28711 struct glyph_row *row;
28713 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28714 if (row->overlapping_p)
28716 eassert (row->enabled_p && !row->mode_line_p);
28718 row->clip = r;
28719 if (row->used[LEFT_MARGIN_AREA])
28720 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28722 if (row->used[TEXT_AREA])
28723 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28725 if (row->used[RIGHT_MARGIN_AREA])
28726 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28727 row->clip = NULL;
28732 /* Return non-zero if W's cursor intersects rectangle R. */
28734 static int
28735 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28737 XRectangle cr, result;
28738 struct glyph *cursor_glyph;
28739 struct glyph_row *row;
28741 if (w->phys_cursor.vpos >= 0
28742 && w->phys_cursor.vpos < w->current_matrix->nrows
28743 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28744 row->enabled_p)
28745 && row->cursor_in_fringe_p)
28747 /* Cursor is in the fringe. */
28748 cr.x = window_box_right_offset (w,
28749 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28750 ? RIGHT_MARGIN_AREA
28751 : TEXT_AREA));
28752 cr.y = row->y;
28753 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28754 cr.height = row->height;
28755 return x_intersect_rectangles (&cr, r, &result);
28758 cursor_glyph = get_phys_cursor_glyph (w);
28759 if (cursor_glyph)
28761 /* r is relative to W's box, but w->phys_cursor.x is relative
28762 to left edge of W's TEXT area. Adjust it. */
28763 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28764 cr.y = w->phys_cursor.y;
28765 cr.width = cursor_glyph->pixel_width;
28766 cr.height = w->phys_cursor_height;
28767 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28768 I assume the effect is the same -- and this is portable. */
28769 return x_intersect_rectangles (&cr, r, &result);
28771 /* If we don't understand the format, pretend we're not in the hot-spot. */
28772 return 0;
28776 /* EXPORT:
28777 Draw a vertical window border to the right of window W if W doesn't
28778 have vertical scroll bars. */
28780 void
28781 x_draw_vertical_border (struct window *w)
28783 struct frame *f = XFRAME (WINDOW_FRAME (w));
28785 /* We could do better, if we knew what type of scroll-bar the adjacent
28786 windows (on either side) have... But we don't :-(
28787 However, I think this works ok. ++KFS 2003-04-25 */
28789 /* Redraw borders between horizontally adjacent windows. Don't
28790 do it for frames with vertical scroll bars because either the
28791 right scroll bar of a window, or the left scroll bar of its
28792 neighbor will suffice as a border. */
28793 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28794 return;
28796 /* Note: It is necessary to redraw both the left and the right
28797 borders, for when only this single window W is being
28798 redisplayed. */
28799 if (!WINDOW_RIGHTMOST_P (w)
28800 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28802 int x0, x1, y0, y1;
28804 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28805 y1 -= 1;
28807 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28808 x1 -= 1;
28810 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28812 if (!WINDOW_LEFTMOST_P (w)
28813 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28815 int x0, x1, y0, y1;
28817 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28818 y1 -= 1;
28820 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28821 x0 -= 1;
28823 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28828 /* Redraw the part of window W intersection rectangle FR. Pixel
28829 coordinates in FR are frame-relative. Call this function with
28830 input blocked. Value is non-zero if the exposure overwrites
28831 mouse-face. */
28833 static int
28834 expose_window (struct window *w, XRectangle *fr)
28836 struct frame *f = XFRAME (w->frame);
28837 XRectangle wr, r;
28838 int mouse_face_overwritten_p = 0;
28840 /* If window is not yet fully initialized, do nothing. This can
28841 happen when toolkit scroll bars are used and a window is split.
28842 Reconfiguring the scroll bar will generate an expose for a newly
28843 created window. */
28844 if (w->current_matrix == NULL)
28845 return 0;
28847 /* When we're currently updating the window, display and current
28848 matrix usually don't agree. Arrange for a thorough display
28849 later. */
28850 if (w == updated_window)
28852 SET_FRAME_GARBAGED (f);
28853 return 0;
28856 /* Frame-relative pixel rectangle of W. */
28857 wr.x = WINDOW_LEFT_EDGE_X (w);
28858 wr.y = WINDOW_TOP_EDGE_Y (w);
28859 wr.width = WINDOW_TOTAL_WIDTH (w);
28860 wr.height = WINDOW_TOTAL_HEIGHT (w);
28862 if (x_intersect_rectangles (fr, &wr, &r))
28864 int yb = window_text_bottom_y (w);
28865 struct glyph_row *row;
28866 int cursor_cleared_p, phys_cursor_on_p;
28867 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28869 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28870 r.x, r.y, r.width, r.height));
28872 /* Convert to window coordinates. */
28873 r.x -= WINDOW_LEFT_EDGE_X (w);
28874 r.y -= WINDOW_TOP_EDGE_Y (w);
28876 /* Turn off the cursor. */
28877 if (!w->pseudo_window_p
28878 && phys_cursor_in_rect_p (w, &r))
28880 x_clear_cursor (w);
28881 cursor_cleared_p = 1;
28883 else
28884 cursor_cleared_p = 0;
28886 /* If the row containing the cursor extends face to end of line,
28887 then expose_area might overwrite the cursor outside the
28888 rectangle and thus notice_overwritten_cursor might clear
28889 w->phys_cursor_on_p. We remember the original value and
28890 check later if it is changed. */
28891 phys_cursor_on_p = w->phys_cursor_on_p;
28893 /* Update lines intersecting rectangle R. */
28894 first_overlapping_row = last_overlapping_row = NULL;
28895 for (row = w->current_matrix->rows;
28896 row->enabled_p;
28897 ++row)
28899 int y0 = row->y;
28900 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28902 if ((y0 >= r.y && y0 < r.y + r.height)
28903 || (y1 > r.y && y1 < r.y + r.height)
28904 || (r.y >= y0 && r.y < y1)
28905 || (r.y + r.height > y0 && r.y + r.height < y1))
28907 /* A header line may be overlapping, but there is no need
28908 to fix overlapping areas for them. KFS 2005-02-12 */
28909 if (row->overlapping_p && !row->mode_line_p)
28911 if (first_overlapping_row == NULL)
28912 first_overlapping_row = row;
28913 last_overlapping_row = row;
28916 row->clip = fr;
28917 if (expose_line (w, row, &r))
28918 mouse_face_overwritten_p = 1;
28919 row->clip = NULL;
28921 else if (row->overlapping_p)
28923 /* We must redraw a row overlapping the exposed area. */
28924 if (y0 < r.y
28925 ? y0 + row->phys_height > r.y
28926 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28928 if (first_overlapping_row == NULL)
28929 first_overlapping_row = row;
28930 last_overlapping_row = row;
28934 if (y1 >= yb)
28935 break;
28938 /* Display the mode line if there is one. */
28939 if (WINDOW_WANTS_MODELINE_P (w)
28940 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28941 row->enabled_p)
28942 && row->y < r.y + r.height)
28944 if (expose_line (w, row, &r))
28945 mouse_face_overwritten_p = 1;
28948 if (!w->pseudo_window_p)
28950 /* Fix the display of overlapping rows. */
28951 if (first_overlapping_row)
28952 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28953 fr);
28955 /* Draw border between windows. */
28956 x_draw_vertical_border (w);
28958 /* Turn the cursor on again. */
28959 if (cursor_cleared_p
28960 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28961 update_window_cursor (w, 1);
28965 return mouse_face_overwritten_p;
28970 /* Redraw (parts) of all windows in the window tree rooted at W that
28971 intersect R. R contains frame pixel coordinates. Value is
28972 non-zero if the exposure overwrites mouse-face. */
28974 static int
28975 expose_window_tree (struct window *w, XRectangle *r)
28977 struct frame *f = XFRAME (w->frame);
28978 int mouse_face_overwritten_p = 0;
28980 while (w && !FRAME_GARBAGED_P (f))
28982 if (WINDOWP (w->contents))
28983 mouse_face_overwritten_p
28984 |= expose_window_tree (XWINDOW (w->contents), r);
28985 else
28986 mouse_face_overwritten_p |= expose_window (w, r);
28988 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28991 return mouse_face_overwritten_p;
28995 /* EXPORT:
28996 Redisplay an exposed area of frame F. X and Y are the upper-left
28997 corner of the exposed rectangle. W and H are width and height of
28998 the exposed area. All are pixel values. W or H zero means redraw
28999 the entire frame. */
29001 void
29002 expose_frame (struct frame *f, int x, int y, int w, int h)
29004 XRectangle r;
29005 int mouse_face_overwritten_p = 0;
29007 TRACE ((stderr, "expose_frame "));
29009 /* No need to redraw if frame will be redrawn soon. */
29010 if (FRAME_GARBAGED_P (f))
29012 TRACE ((stderr, " garbaged\n"));
29013 return;
29016 /* If basic faces haven't been realized yet, there is no point in
29017 trying to redraw anything. This can happen when we get an expose
29018 event while Emacs is starting, e.g. by moving another window. */
29019 if (FRAME_FACE_CACHE (f) == NULL
29020 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29022 TRACE ((stderr, " no faces\n"));
29023 return;
29026 if (w == 0 || h == 0)
29028 r.x = r.y = 0;
29029 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29030 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29032 else
29034 r.x = x;
29035 r.y = y;
29036 r.width = w;
29037 r.height = h;
29040 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29041 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29043 if (WINDOWP (f->tool_bar_window))
29044 mouse_face_overwritten_p
29045 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29047 #ifdef HAVE_X_WINDOWS
29048 #ifndef MSDOS
29049 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29050 if (WINDOWP (f->menu_bar_window))
29051 mouse_face_overwritten_p
29052 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29053 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29054 #endif
29055 #endif
29057 /* Some window managers support a focus-follows-mouse style with
29058 delayed raising of frames. Imagine a partially obscured frame,
29059 and moving the mouse into partially obscured mouse-face on that
29060 frame. The visible part of the mouse-face will be highlighted,
29061 then the WM raises the obscured frame. With at least one WM, KDE
29062 2.1, Emacs is not getting any event for the raising of the frame
29063 (even tried with SubstructureRedirectMask), only Expose events.
29064 These expose events will draw text normally, i.e. not
29065 highlighted. Which means we must redo the highlight here.
29066 Subsume it under ``we love X''. --gerd 2001-08-15 */
29067 /* Included in Windows version because Windows most likely does not
29068 do the right thing if any third party tool offers
29069 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29070 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29072 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29073 if (f == hlinfo->mouse_face_mouse_frame)
29075 int mouse_x = hlinfo->mouse_face_mouse_x;
29076 int mouse_y = hlinfo->mouse_face_mouse_y;
29077 clear_mouse_face (hlinfo);
29078 note_mouse_highlight (f, mouse_x, mouse_y);
29084 /* EXPORT:
29085 Determine the intersection of two rectangles R1 and R2. Return
29086 the intersection in *RESULT. Value is non-zero if RESULT is not
29087 empty. */
29090 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29092 XRectangle *left, *right;
29093 XRectangle *upper, *lower;
29094 int intersection_p = 0;
29096 /* Rearrange so that R1 is the left-most rectangle. */
29097 if (r1->x < r2->x)
29098 left = r1, right = r2;
29099 else
29100 left = r2, right = r1;
29102 /* X0 of the intersection is right.x0, if this is inside R1,
29103 otherwise there is no intersection. */
29104 if (right->x <= left->x + left->width)
29106 result->x = right->x;
29108 /* The right end of the intersection is the minimum of
29109 the right ends of left and right. */
29110 result->width = (min (left->x + left->width, right->x + right->width)
29111 - result->x);
29113 /* Same game for Y. */
29114 if (r1->y < r2->y)
29115 upper = r1, lower = r2;
29116 else
29117 upper = r2, lower = r1;
29119 /* The upper end of the intersection is lower.y0, if this is inside
29120 of upper. Otherwise, there is no intersection. */
29121 if (lower->y <= upper->y + upper->height)
29123 result->y = lower->y;
29125 /* The lower end of the intersection is the minimum of the lower
29126 ends of upper and lower. */
29127 result->height = (min (lower->y + lower->height,
29128 upper->y + upper->height)
29129 - result->y);
29130 intersection_p = 1;
29134 return intersection_p;
29137 #endif /* HAVE_WINDOW_SYSTEM */
29140 /***********************************************************************
29141 Initialization
29142 ***********************************************************************/
29144 void
29145 syms_of_xdisp (void)
29147 Vwith_echo_area_save_vector = Qnil;
29148 staticpro (&Vwith_echo_area_save_vector);
29150 Vmessage_stack = Qnil;
29151 staticpro (&Vmessage_stack);
29153 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29154 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29156 message_dolog_marker1 = Fmake_marker ();
29157 staticpro (&message_dolog_marker1);
29158 message_dolog_marker2 = Fmake_marker ();
29159 staticpro (&message_dolog_marker2);
29160 message_dolog_marker3 = Fmake_marker ();
29161 staticpro (&message_dolog_marker3);
29163 #ifdef GLYPH_DEBUG
29164 defsubr (&Sdump_frame_glyph_matrix);
29165 defsubr (&Sdump_glyph_matrix);
29166 defsubr (&Sdump_glyph_row);
29167 defsubr (&Sdump_tool_bar_row);
29168 defsubr (&Strace_redisplay);
29169 defsubr (&Strace_to_stderr);
29170 #endif
29171 #ifdef HAVE_WINDOW_SYSTEM
29172 defsubr (&Stool_bar_lines_needed);
29173 defsubr (&Slookup_image_map);
29174 #endif
29175 defsubr (&Sline_pixel_height);
29176 defsubr (&Sformat_mode_line);
29177 defsubr (&Sinvisible_p);
29178 defsubr (&Scurrent_bidi_paragraph_direction);
29179 defsubr (&Smove_point_visually);
29181 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29182 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29183 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29184 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29185 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29186 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29187 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29188 DEFSYM (Qeval, "eval");
29189 DEFSYM (QCdata, ":data");
29190 DEFSYM (Qdisplay, "display");
29191 DEFSYM (Qspace_width, "space-width");
29192 DEFSYM (Qraise, "raise");
29193 DEFSYM (Qslice, "slice");
29194 DEFSYM (Qspace, "space");
29195 DEFSYM (Qmargin, "margin");
29196 DEFSYM (Qpointer, "pointer");
29197 DEFSYM (Qleft_margin, "left-margin");
29198 DEFSYM (Qright_margin, "right-margin");
29199 DEFSYM (Qcenter, "center");
29200 DEFSYM (Qline_height, "line-height");
29201 DEFSYM (QCalign_to, ":align-to");
29202 DEFSYM (QCrelative_width, ":relative-width");
29203 DEFSYM (QCrelative_height, ":relative-height");
29204 DEFSYM (QCeval, ":eval");
29205 DEFSYM (QCpropertize, ":propertize");
29206 DEFSYM (QCfile, ":file");
29207 DEFSYM (Qfontified, "fontified");
29208 DEFSYM (Qfontification_functions, "fontification-functions");
29209 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29210 DEFSYM (Qescape_glyph, "escape-glyph");
29211 DEFSYM (Qnobreak_space, "nobreak-space");
29212 DEFSYM (Qimage, "image");
29213 DEFSYM (Qtext, "text");
29214 DEFSYM (Qboth, "both");
29215 DEFSYM (Qboth_horiz, "both-horiz");
29216 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29217 DEFSYM (QCmap, ":map");
29218 DEFSYM (QCpointer, ":pointer");
29219 DEFSYM (Qrect, "rect");
29220 DEFSYM (Qcircle, "circle");
29221 DEFSYM (Qpoly, "poly");
29222 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29223 DEFSYM (Qgrow_only, "grow-only");
29224 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29225 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29226 DEFSYM (Qposition, "position");
29227 DEFSYM (Qbuffer_position, "buffer-position");
29228 DEFSYM (Qobject, "object");
29229 DEFSYM (Qbar, "bar");
29230 DEFSYM (Qhbar, "hbar");
29231 DEFSYM (Qbox, "box");
29232 DEFSYM (Qhollow, "hollow");
29233 DEFSYM (Qhand, "hand");
29234 DEFSYM (Qarrow, "arrow");
29235 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29237 list_of_error = Fcons (Fcons (intern_c_string ("error"),
29238 Fcons (intern_c_string ("void-variable"), Qnil)),
29239 Qnil);
29240 staticpro (&list_of_error);
29242 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29243 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29244 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29245 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29247 echo_buffer[0] = echo_buffer[1] = Qnil;
29248 staticpro (&echo_buffer[0]);
29249 staticpro (&echo_buffer[1]);
29251 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29252 staticpro (&echo_area_buffer[0]);
29253 staticpro (&echo_area_buffer[1]);
29255 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29256 staticpro (&Vmessages_buffer_name);
29258 mode_line_proptrans_alist = Qnil;
29259 staticpro (&mode_line_proptrans_alist);
29260 mode_line_string_list = Qnil;
29261 staticpro (&mode_line_string_list);
29262 mode_line_string_face = Qnil;
29263 staticpro (&mode_line_string_face);
29264 mode_line_string_face_prop = Qnil;
29265 staticpro (&mode_line_string_face_prop);
29266 Vmode_line_unwind_vector = Qnil;
29267 staticpro (&Vmode_line_unwind_vector);
29269 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29271 help_echo_string = Qnil;
29272 staticpro (&help_echo_string);
29273 help_echo_object = Qnil;
29274 staticpro (&help_echo_object);
29275 help_echo_window = Qnil;
29276 staticpro (&help_echo_window);
29277 previous_help_echo_string = Qnil;
29278 staticpro (&previous_help_echo_string);
29279 help_echo_pos = -1;
29281 DEFSYM (Qright_to_left, "right-to-left");
29282 DEFSYM (Qleft_to_right, "left-to-right");
29284 #ifdef HAVE_WINDOW_SYSTEM
29285 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29286 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29287 For example, if a block cursor is over a tab, it will be drawn as
29288 wide as that tab on the display. */);
29289 x_stretch_cursor_p = 0;
29290 #endif
29292 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29293 doc: /* Non-nil means highlight trailing whitespace.
29294 The face used for trailing whitespace is `trailing-whitespace'. */);
29295 Vshow_trailing_whitespace = Qnil;
29297 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29298 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29299 If the value is t, Emacs highlights non-ASCII chars which have the
29300 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29301 or `escape-glyph' face respectively.
29303 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29304 U+2011 (non-breaking hyphen) are affected.
29306 Any other non-nil value means to display these characters as a escape
29307 glyph followed by an ordinary space or hyphen.
29309 A value of nil means no special handling of these characters. */);
29310 Vnobreak_char_display = Qt;
29312 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29313 doc: /* The pointer shape to show in void text areas.
29314 A value of nil means to show the text pointer. Other options are `arrow',
29315 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29316 Vvoid_text_area_pointer = Qarrow;
29318 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29319 doc: /* Non-nil means don't actually do any redisplay.
29320 This is used for internal purposes. */);
29321 Vinhibit_redisplay = Qnil;
29323 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29324 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29325 Vglobal_mode_string = Qnil;
29327 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29328 doc: /* Marker for where to display an arrow on top of the buffer text.
29329 This must be the beginning of a line in order to work.
29330 See also `overlay-arrow-string'. */);
29331 Voverlay_arrow_position = Qnil;
29333 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29334 doc: /* String to display as an arrow in non-window frames.
29335 See also `overlay-arrow-position'. */);
29336 Voverlay_arrow_string = build_pure_c_string ("=>");
29338 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29339 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29340 The symbols on this list are examined during redisplay to determine
29341 where to display overlay arrows. */);
29342 Voverlay_arrow_variable_list
29343 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
29345 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29346 doc: /* The number of lines to try scrolling a window by when point moves out.
29347 If that fails to bring point back on frame, point is centered instead.
29348 If this is zero, point is always centered after it moves off frame.
29349 If you want scrolling to always be a line at a time, you should set
29350 `scroll-conservatively' to a large value rather than set this to 1. */);
29352 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29353 doc: /* Scroll up to this many lines, to bring point back on screen.
29354 If point moves off-screen, redisplay will scroll by up to
29355 `scroll-conservatively' lines in order to bring point just barely
29356 onto the screen again. If that cannot be done, then redisplay
29357 recenters point as usual.
29359 If the value is greater than 100, redisplay will never recenter point,
29360 but will always scroll just enough text to bring point into view, even
29361 if you move far away.
29363 A value of zero means always recenter point if it moves off screen. */);
29364 scroll_conservatively = 0;
29366 DEFVAR_INT ("scroll-margin", scroll_margin,
29367 doc: /* Number of lines of margin at the top and bottom of a window.
29368 Recenter the window whenever point gets within this many lines
29369 of the top or bottom of the window. */);
29370 scroll_margin = 0;
29372 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29373 doc: /* Pixels per inch value for non-window system displays.
29374 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29375 Vdisplay_pixels_per_inch = make_float (72.0);
29377 #ifdef GLYPH_DEBUG
29378 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29379 #endif
29381 DEFVAR_LISP ("truncate-partial-width-windows",
29382 Vtruncate_partial_width_windows,
29383 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29384 For an integer value, truncate lines in each window narrower than the
29385 full frame width, provided the window width is less than that integer;
29386 otherwise, respect the value of `truncate-lines'.
29388 For any other non-nil value, truncate lines in all windows that do
29389 not span the full frame width.
29391 A value of nil means to respect the value of `truncate-lines'.
29393 If `word-wrap' is enabled, you might want to reduce this. */);
29394 Vtruncate_partial_width_windows = make_number (50);
29396 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29397 doc: /* Maximum buffer size for which line number should be displayed.
29398 If the buffer is bigger than this, the line number does not appear
29399 in the mode line. A value of nil means no limit. */);
29400 Vline_number_display_limit = Qnil;
29402 DEFVAR_INT ("line-number-display-limit-width",
29403 line_number_display_limit_width,
29404 doc: /* Maximum line width (in characters) for line number display.
29405 If the average length of the lines near point is bigger than this, then the
29406 line number may be omitted from the mode line. */);
29407 line_number_display_limit_width = 200;
29409 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29410 doc: /* Non-nil means highlight region even in nonselected windows. */);
29411 highlight_nonselected_windows = 0;
29413 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29414 doc: /* Non-nil if more than one frame is visible on this display.
29415 Minibuffer-only frames don't count, but iconified frames do.
29416 This variable is not guaranteed to be accurate except while processing
29417 `frame-title-format' and `icon-title-format'. */);
29419 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29420 doc: /* Template for displaying the title bar of visible frames.
29421 \(Assuming the window manager supports this feature.)
29423 This variable has the same structure as `mode-line-format', except that
29424 the %c and %l constructs are ignored. It is used only on frames for
29425 which no explicit name has been set \(see `modify-frame-parameters'). */);
29427 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29428 doc: /* Template for displaying the title bar of an iconified frame.
29429 \(Assuming the window manager supports this feature.)
29430 This variable has the same structure as `mode-line-format' (which see),
29431 and is used only on frames for which no explicit name has been set
29432 \(see `modify-frame-parameters'). */);
29433 Vicon_title_format
29434 = Vframe_title_format
29435 = listn (CONSTYPE_PURE, 3,
29436 intern_c_string ("multiple-frames"),
29437 build_pure_c_string ("%b"),
29438 listn (CONSTYPE_PURE, 4,
29439 empty_unibyte_string,
29440 intern_c_string ("invocation-name"),
29441 build_pure_c_string ("@"),
29442 intern_c_string ("system-name")));
29444 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29445 doc: /* Maximum number of lines to keep in the message log buffer.
29446 If nil, disable message logging. If t, log messages but don't truncate
29447 the buffer when it becomes large. */);
29448 Vmessage_log_max = make_number (1000);
29450 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29451 doc: /* Functions called before redisplay, if window sizes have changed.
29452 The value should be a list of functions that take one argument.
29453 Just before redisplay, for each frame, if any of its windows have changed
29454 size since the last redisplay, or have been split or deleted,
29455 all the functions in the list are called, with the frame as argument. */);
29456 Vwindow_size_change_functions = Qnil;
29458 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29459 doc: /* List of functions to call before redisplaying a window with scrolling.
29460 Each function is called with two arguments, the window and its new
29461 display-start position. Note that these functions are also called by
29462 `set-window-buffer'. Also note that the value of `window-end' is not
29463 valid when these functions are called.
29465 Warning: Do not use this feature to alter the way the window
29466 is scrolled. It is not designed for that, and such use probably won't
29467 work. */);
29468 Vwindow_scroll_functions = Qnil;
29470 DEFVAR_LISP ("window-text-change-functions",
29471 Vwindow_text_change_functions,
29472 doc: /* Functions to call in redisplay when text in the window might change. */);
29473 Vwindow_text_change_functions = Qnil;
29475 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29476 doc: /* Functions called when redisplay of a window reaches the end trigger.
29477 Each function is called with two arguments, the window and the end trigger value.
29478 See `set-window-redisplay-end-trigger'. */);
29479 Vredisplay_end_trigger_functions = Qnil;
29481 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29482 doc: /* Non-nil means autoselect window with mouse pointer.
29483 If nil, do not autoselect windows.
29484 A positive number means delay autoselection by that many seconds: a
29485 window is autoselected only after the mouse has remained in that
29486 window for the duration of the delay.
29487 A negative number has a similar effect, but causes windows to be
29488 autoselected only after the mouse has stopped moving. \(Because of
29489 the way Emacs compares mouse events, you will occasionally wait twice
29490 that time before the window gets selected.\)
29491 Any other value means to autoselect window instantaneously when the
29492 mouse pointer enters it.
29494 Autoselection selects the minibuffer only if it is active, and never
29495 unselects the minibuffer if it is active.
29497 When customizing this variable make sure that the actual value of
29498 `focus-follows-mouse' matches the behavior of your window manager. */);
29499 Vmouse_autoselect_window = Qnil;
29501 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29502 doc: /* Non-nil means automatically resize tool-bars.
29503 This dynamically changes the tool-bar's height to the minimum height
29504 that is needed to make all tool-bar items visible.
29505 If value is `grow-only', the tool-bar's height is only increased
29506 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29507 Vauto_resize_tool_bars = Qt;
29509 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29510 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29511 auto_raise_tool_bar_buttons_p = 1;
29513 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29514 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29515 make_cursor_line_fully_visible_p = 1;
29517 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29518 doc: /* Border below tool-bar in pixels.
29519 If an integer, use it as the height of the border.
29520 If it is one of `internal-border-width' or `border-width', use the
29521 value of the corresponding frame parameter.
29522 Otherwise, no border is added below the tool-bar. */);
29523 Vtool_bar_border = Qinternal_border_width;
29525 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29526 doc: /* Margin around tool-bar buttons in pixels.
29527 If an integer, use that for both horizontal and vertical margins.
29528 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29529 HORZ specifying the horizontal margin, and VERT specifying the
29530 vertical margin. */);
29531 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29533 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29534 doc: /* Relief thickness of tool-bar buttons. */);
29535 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29537 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29538 doc: /* Tool bar style to use.
29539 It can be one of
29540 image - show images only
29541 text - show text only
29542 both - show both, text below image
29543 both-horiz - show text to the right of the image
29544 text-image-horiz - show text to the left of the image
29545 any other - use system default or image if no system default.
29547 This variable only affects the GTK+ toolkit version of Emacs. */);
29548 Vtool_bar_style = Qnil;
29550 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29551 doc: /* Maximum number of characters a label can have to be shown.
29552 The tool bar style must also show labels for this to have any effect, see
29553 `tool-bar-style'. */);
29554 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29556 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29557 doc: /* List of functions to call to fontify regions of text.
29558 Each function is called with one argument POS. Functions must
29559 fontify a region starting at POS in the current buffer, and give
29560 fontified regions the property `fontified'. */);
29561 Vfontification_functions = Qnil;
29562 Fmake_variable_buffer_local (Qfontification_functions);
29564 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29565 unibyte_display_via_language_environment,
29566 doc: /* Non-nil means display unibyte text according to language environment.
29567 Specifically, this means that raw bytes in the range 160-255 decimal
29568 are displayed by converting them to the equivalent multibyte characters
29569 according to the current language environment. As a result, they are
29570 displayed according to the current fontset.
29572 Note that this variable affects only how these bytes are displayed,
29573 but does not change the fact they are interpreted as raw bytes. */);
29574 unibyte_display_via_language_environment = 0;
29576 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29577 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29578 If a float, it specifies a fraction of the mini-window frame's height.
29579 If an integer, it specifies a number of lines. */);
29580 Vmax_mini_window_height = make_float (0.25);
29582 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29583 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29584 A value of nil means don't automatically resize mini-windows.
29585 A value of t means resize them to fit the text displayed in them.
29586 A value of `grow-only', the default, means let mini-windows grow only;
29587 they return to their normal size when the minibuffer is closed, or the
29588 echo area becomes empty. */);
29589 Vresize_mini_windows = Qgrow_only;
29591 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29592 doc: /* Alist specifying how to blink the cursor off.
29593 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29594 `cursor-type' frame-parameter or variable equals ON-STATE,
29595 comparing using `equal', Emacs uses OFF-STATE to specify
29596 how to blink it off. ON-STATE and OFF-STATE are values for
29597 the `cursor-type' frame parameter.
29599 If a frame's ON-STATE has no entry in this list,
29600 the frame's other specifications determine how to blink the cursor off. */);
29601 Vblink_cursor_alist = Qnil;
29603 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29604 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29605 If non-nil, windows are automatically scrolled horizontally to make
29606 point visible. */);
29607 automatic_hscrolling_p = 1;
29608 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29610 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29611 doc: /* How many columns away from the window edge point is allowed to get
29612 before automatic hscrolling will horizontally scroll the window. */);
29613 hscroll_margin = 5;
29615 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29616 doc: /* How many columns to scroll the window when point gets too close to the edge.
29617 When point is less than `hscroll-margin' columns from the window
29618 edge, automatic hscrolling will scroll the window by the amount of columns
29619 determined by this variable. If its value is a positive integer, scroll that
29620 many columns. If it's a positive floating-point number, it specifies the
29621 fraction of the window's width to scroll. If it's nil or zero, point will be
29622 centered horizontally after the scroll. Any other value, including negative
29623 numbers, are treated as if the value were zero.
29625 Automatic hscrolling always moves point outside the scroll margin, so if
29626 point was more than scroll step columns inside the margin, the window will
29627 scroll more than the value given by the scroll step.
29629 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29630 and `scroll-right' overrides this variable's effect. */);
29631 Vhscroll_step = make_number (0);
29633 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29634 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29635 Bind this around calls to `message' to let it take effect. */);
29636 message_truncate_lines = 0;
29638 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29639 doc: /* Normal hook run to update the menu bar definitions.
29640 Redisplay runs this hook before it redisplays the menu bar.
29641 This is used to update submenus such as Buffers,
29642 whose contents depend on various data. */);
29643 Vmenu_bar_update_hook = Qnil;
29645 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29646 doc: /* Frame for which we are updating a menu.
29647 The enable predicate for a menu binding should check this variable. */);
29648 Vmenu_updating_frame = Qnil;
29650 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29651 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29652 inhibit_menubar_update = 0;
29654 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29655 doc: /* Prefix prepended to all continuation lines at display time.
29656 The value may be a string, an image, or a stretch-glyph; it is
29657 interpreted in the same way as the value of a `display' text property.
29659 This variable is overridden by any `wrap-prefix' text or overlay
29660 property.
29662 To add a prefix to non-continuation lines, use `line-prefix'. */);
29663 Vwrap_prefix = Qnil;
29664 DEFSYM (Qwrap_prefix, "wrap-prefix");
29665 Fmake_variable_buffer_local (Qwrap_prefix);
29667 DEFVAR_LISP ("line-prefix", Vline_prefix,
29668 doc: /* Prefix prepended to all non-continuation lines at display time.
29669 The value may be a string, an image, or a stretch-glyph; it is
29670 interpreted in the same way as the value of a `display' text property.
29672 This variable is overridden by any `line-prefix' text or overlay
29673 property.
29675 To add a prefix to continuation lines, use `wrap-prefix'. */);
29676 Vline_prefix = Qnil;
29677 DEFSYM (Qline_prefix, "line-prefix");
29678 Fmake_variable_buffer_local (Qline_prefix);
29680 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29681 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29682 inhibit_eval_during_redisplay = 0;
29684 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29685 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29686 inhibit_free_realized_faces = 0;
29688 #ifdef GLYPH_DEBUG
29689 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29690 doc: /* Inhibit try_window_id display optimization. */);
29691 inhibit_try_window_id = 0;
29693 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29694 doc: /* Inhibit try_window_reusing display optimization. */);
29695 inhibit_try_window_reusing = 0;
29697 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29698 doc: /* Inhibit try_cursor_movement display optimization. */);
29699 inhibit_try_cursor_movement = 0;
29700 #endif /* GLYPH_DEBUG */
29702 DEFVAR_INT ("overline-margin", overline_margin,
29703 doc: /* Space between overline and text, in pixels.
29704 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29705 margin to the character height. */);
29706 overline_margin = 2;
29708 DEFVAR_INT ("underline-minimum-offset",
29709 underline_minimum_offset,
29710 doc: /* Minimum distance between baseline and underline.
29711 This can improve legibility of underlined text at small font sizes,
29712 particularly when using variable `x-use-underline-position-properties'
29713 with fonts that specify an UNDERLINE_POSITION relatively close to the
29714 baseline. The default value is 1. */);
29715 underline_minimum_offset = 1;
29717 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29718 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29719 This feature only works when on a window system that can change
29720 cursor shapes. */);
29721 display_hourglass_p = 1;
29723 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29724 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29725 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29727 hourglass_atimer = NULL;
29728 hourglass_shown_p = 0;
29730 DEFSYM (Qglyphless_char, "glyphless-char");
29731 DEFSYM (Qhex_code, "hex-code");
29732 DEFSYM (Qempty_box, "empty-box");
29733 DEFSYM (Qthin_space, "thin-space");
29734 DEFSYM (Qzero_width, "zero-width");
29736 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29737 /* Intern this now in case it isn't already done.
29738 Setting this variable twice is harmless.
29739 But don't staticpro it here--that is done in alloc.c. */
29740 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29741 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29743 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29744 doc: /* Char-table defining glyphless characters.
29745 Each element, if non-nil, should be one of the following:
29746 an ASCII acronym string: display this string in a box
29747 `hex-code': display the hexadecimal code of a character in a box
29748 `empty-box': display as an empty box
29749 `thin-space': display as 1-pixel width space
29750 `zero-width': don't display
29751 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29752 display method for graphical terminals and text terminals respectively.
29753 GRAPHICAL and TEXT should each have one of the values listed above.
29755 The char-table has one extra slot to control the display of a character for
29756 which no font is found. This slot only takes effect on graphical terminals.
29757 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29758 `thin-space'. The default is `empty-box'. */);
29759 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29760 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29761 Qempty_box);
29763 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29764 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29765 Vdebug_on_message = Qnil;
29769 /* Initialize this module when Emacs starts. */
29771 void
29772 init_xdisp (void)
29774 current_header_line_height = current_mode_line_height = -1;
29776 CHARPOS (this_line_start_pos) = 0;
29778 if (!noninteractive)
29780 struct window *m = XWINDOW (minibuf_window);
29781 Lisp_Object frame = m->frame;
29782 struct frame *f = XFRAME (frame);
29783 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29784 struct window *r = XWINDOW (root);
29785 int i;
29787 echo_area_window = minibuf_window;
29789 r->top_line = FRAME_TOP_MARGIN (f);
29790 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29791 r->total_cols = FRAME_COLS (f);
29793 m->top_line = FRAME_LINES (f) - 1;
29794 m->total_lines = 1;
29795 m->total_cols = FRAME_COLS (f);
29797 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29798 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29799 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29801 /* The default ellipsis glyphs `...'. */
29802 for (i = 0; i < 3; ++i)
29803 default_invis_vector[i] = make_number ('.');
29807 /* Allocate the buffer for frame titles.
29808 Also used for `format-mode-line'. */
29809 int size = 100;
29810 mode_line_noprop_buf = xmalloc (size);
29811 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29812 mode_line_noprop_ptr = mode_line_noprop_buf;
29813 mode_line_target = MODE_LINE_DISPLAY;
29816 help_echo_showing_p = 0;
29819 /* Platform-independent portion of hourglass implementation. */
29821 /* Cancel a currently active hourglass timer, and start a new one. */
29822 void
29823 start_hourglass (void)
29825 #if defined (HAVE_WINDOW_SYSTEM)
29826 EMACS_TIME delay;
29828 cancel_hourglass ();
29830 if (INTEGERP (Vhourglass_delay)
29831 && XINT (Vhourglass_delay) > 0)
29832 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29833 TYPE_MAXIMUM (time_t)),
29835 else if (FLOATP (Vhourglass_delay)
29836 && XFLOAT_DATA (Vhourglass_delay) > 0)
29837 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29838 else
29839 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29841 #ifdef HAVE_NTGUI
29843 extern void w32_note_current_window (void);
29844 w32_note_current_window ();
29846 #endif /* HAVE_NTGUI */
29848 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29849 show_hourglass, NULL);
29850 #endif
29854 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29855 shown. */
29856 void
29857 cancel_hourglass (void)
29859 #if defined (HAVE_WINDOW_SYSTEM)
29860 if (hourglass_atimer)
29862 cancel_atimer (hourglass_atimer);
29863 hourglass_atimer = NULL;
29866 if (hourglass_shown_p)
29867 hide_hourglass ();
29868 #endif